Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check theme support is an array before indexing #23104

Merged
merged 6 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,34 +199,28 @@ function gutenberg_experimental_global_styles_get_core() {
function gutenberg_experimental_global_styles_get_theme_presets() {
$theme_presets = array();

$theme_colors = get_theme_support( 'editor-color-palette' )[0];
if ( is_array( $theme_colors ) ) {
foreach ( $theme_colors as $color ) {
$theme_presets['global']['presets']['color'][] = array(
'slug' => $color['slug'],
'value' => $color['color'],
);
}
$theme_colors = gutenberg_experimental_get( get_theme_support( 'editor-color-palette' ), array( '0' ) );
foreach ( $theme_colors as $color ) {
$theme_presets['global']['presets']['color'][] = array(
'slug' => $color['slug'],
'value' => $color['color'],
);
}

$theme_gradients = get_theme_support( 'editor-gradient-presets' )[0];
if ( is_array( $theme_gradients ) ) {
foreach ( $theme_gradients as $gradient ) {
$theme_presets['global']['presets']['gradient'][] = array(
'slug' => $gradient['slug'],
'value' => $gradient['gradient'],
);
}
$theme_gradients = gutenberg_experimental_get( get_theme_support( 'editor-gradient-presets' ), array( '0' ) );
foreach ( $theme_gradients as $gradient ) {
$theme_presets['global']['presets']['gradient'][] = array(
'slug' => $gradient['slug'],
'value' => $gradient['gradient'],
);
}

$theme_font_sizes = get_theme_support( 'editor-font-sizes' )[0];
if ( is_array( $theme_font_sizes ) ) {
foreach ( $theme_font_sizes as $font_size ) {
$theme_presets['global']['presets']['font-size'][] = array(
'slug' => $font_size['slug'],
'value' => $font_size['size'],
);
}
$theme_font_sizes = gutenberg_experimental_get( get_theme_support( 'editor-font-sizes' ), array( '0' ) );
foreach ( $theme_font_sizes as $font_size ) {
$theme_presets['global']['presets']['font-size'][] = array(
'slug' => $font_size['slug'],
'value' => $font_size['size'],
);
}

return $theme_presets;
Expand Down Expand Up @@ -386,7 +380,7 @@ function gutenberg_experimental_global_styles_flatten_styles_tree( $styles ) {
$result = array();

foreach ( $mappings as $key => $path ) {
$value = gutenberg_experimental_get( $styles, $path );
$value = gutenberg_experimental_get( $styles, $path, null );
if ( null !== $value ) {
$result[ $key ] = $value;
}
Expand Down
16 changes: 11 additions & 5 deletions lib/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
* It is equivalent to want lodash get provides for JavaScript and is useful to have something similar
* in php so functions that do the same thing on the client and sever can have identical code.
*
* @param array $array An array from where we want to retrieve some information from.
* @param array $path An array containing the path we want to retrieve.
* @param array $array An array from where we want to retrieve some information from.
* @param array $path An array containing the path we want to retrieve.
* @param array $default The return value if $array or $path is not expected input type.
*
* @return array Containing a set of css rules.
* @return array An array matching the path specified.
*/
function gutenberg_experimental_get( $array, $path ) {
function gutenberg_experimental_get( $array, $path, $default = array() ) {
// Confirm input values are expected type to avoid notice warnings.
if ( ! is_array( $array ) || ! is_array( $path ) ) {
return $default;
}

$path_length = count( $path );
for ( $i = 0; $i < $path_length; ++$i ) {
if ( empty( $array[ $path[ $i ] ] ) ) {
mkaz marked this conversation as resolved.
Show resolved Hide resolved
return null;
return $default;
}
$array = $array[ $path[ $i ] ];
}
Expand Down