Skip to content

Commit

Permalink
Add check theme support is an array before indexing (#23104)
Browse files Browse the repository at this point in the history
* Add check theme support is an array before indexing

* Update to use gutenberg_experiment_get function

* Linting

* Add default value, add check for is_aray($path)

* Updated to return default, if empty

* Fix doc string, end stop.
  • Loading branch information
mkaz authored Jun 12, 2020
1 parent 80841ef commit 830651a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
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 ] ] ) ) {
return null;
return $default;
}
$array = $array[ $path[ $i ] ];
}
Expand Down

0 comments on commit 830651a

Please sign in to comment.