diff --git a/lib/global-styles.php b/lib/global-styles.php index 166d84df01badc..9e23f31eaba109 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -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; @@ -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; } diff --git a/lib/utils.php b/lib/utils.php index f6a503234d0a13..9d38be7a762a29 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -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 ] ]; }