From 9115246f7292b8ead3b0cca45a5fa604f2d45706 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 12 Nov 2020 20:20:12 +0000 Subject: [PATCH] General: Convert `wp_array_get()` to a "private" function and add tests. This function may be promoted in the future if it's deemed useful enough. Props dd32, jorgefilipecosta, Hareesh Pillai Fixes #51720 Built from https://develop.svn.wordpress.org/trunk@49580 git-svn-id: http://core.svn.wordpress.org/trunk@49318 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-supports/align.php | 4 +- wp-includes/block-supports/colors.php | 18 ++++----- .../block-supports/custom-classname.php | 4 +- .../block-supports/generated-classname.php | 2 +- wp-includes/block-supports/typography.php | 8 ++-- wp-includes/functions.php | 40 +++++++++++++------ wp-includes/version.php | 2 +- 7 files changed, 47 insertions(+), 31 deletions(-) diff --git a/wp-includes/block-supports/align.php b/wp-includes/block-supports/align.php index d81663d5c4..7a8bca129b 100644 --- a/wp-includes/block-supports/align.php +++ b/wp-includes/block-supports/align.php @@ -15,7 +15,7 @@ function wp_register_alignment_support( $block_type ) { $has_align_support = false; if ( property_exists( $block_type, 'supports' ) ) { - $has_align_support = wp_array_get( $block_type->supports, array( 'align' ), false ); + $has_align_support = _wp_array_get( $block_type->supports, array( 'align' ), false ); } if ( $has_align_support ) { if ( ! $block_type->attributes ) { @@ -46,7 +46,7 @@ function wp_apply_alignment_support( $block_type, $block_attributes ) { $attributes = array(); $has_align_support = false; if ( property_exists( $block_type, 'supports' ) ) { - $has_align_support = wp_array_get( $block_type->supports, array( 'align' ), false ); + $has_align_support = _wp_array_get( $block_type->supports, array( 'align' ), false ); } if ( $has_align_support ) { $has_block_alignment = array_key_exists( 'align', $block_attributes ); diff --git a/wp-includes/block-supports/colors.php b/wp-includes/block-supports/colors.php index 71cbcec64b..f6e5a3cf93 100644 --- a/wp-includes/block-supports/colors.php +++ b/wp-includes/block-supports/colors.php @@ -15,11 +15,11 @@ function wp_register_colors_support( $block_type ) { $color_support = false; if ( property_exists( $block_type, 'supports' ) ) { - $color_support = wp_array_get( $block_type->supports, array( '__experimentalColor' ), false ); + $color_support = _wp_array_get( $block_type->supports, array( '__experimentalColor' ), false ); } - $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && wp_array_get( $color_support, array( 'text' ), true ) ); - $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && wp_array_get( $color_support, array( 'background' ), true ) ); - $has_gradients_support = wp_array_get( $color_support, array( 'gradients' ), false ); + $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) ); + $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) ); + $has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false ); if ( ! $block_type->attributes ) { $block_type->attributes = array(); @@ -63,11 +63,11 @@ function wp_register_colors_support( $block_type ) { * @return array Colors CSS classes and inline styles. */ function wp_apply_colors_support( $block_type, $block_attributes ) { - $color_support = wp_array_get( $block_type->supports, array( '__experimentalColor' ), false ); - $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && wp_array_get( $color_support, array( 'text' ), true ) ); - $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && wp_array_get( $color_support, array( 'background' ), true ) ); - $has_link_colors_support = wp_array_get( $color_support, array( 'linkColor' ), false ); - $has_gradients_support = wp_array_get( $color_support, array( 'gradients' ), false ); + $color_support = _wp_array_get( $block_type->supports, array( '__experimentalColor' ), false ); + $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) ); + $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) ); + $has_link_colors_support = _wp_array_get( $color_support, array( 'linkColor' ), false ); + $has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false ); $classes = array(); $styles = array(); diff --git a/wp-includes/block-supports/custom-classname.php b/wp-includes/block-supports/custom-classname.php index 7d45738afa..6ac1b49de8 100644 --- a/wp-includes/block-supports/custom-classname.php +++ b/wp-includes/block-supports/custom-classname.php @@ -15,7 +15,7 @@ function wp_register_custom_classname_support( $block_type ) { $has_custom_classname_support = true; if ( property_exists( $block_type, 'supports' ) ) { - $has_custom_classname_support = wp_array_get( $block_type->supports, array( 'customClassName' ), true ); + $has_custom_classname_support = _wp_array_get( $block_type->supports, array( 'customClassName' ), true ); } if ( $has_custom_classname_support ) { if ( ! $block_type->attributes ) { @@ -44,7 +44,7 @@ function wp_apply_custom_classname_support( $block_type, $block_attributes ) { $has_custom_classname_support = true; $attributes = array(); if ( property_exists( $block_type, 'supports' ) ) { - $has_custom_classname_support = wp_array_get( $block_type->supports, array( 'customClassName' ), true ); + $has_custom_classname_support = _wp_array_get( $block_type->supports, array( 'customClassName' ), true ); } if ( $has_custom_classname_support ) { $has_custom_classnames = array_key_exists( 'className', $block_attributes ); diff --git a/wp-includes/block-supports/generated-classname.php b/wp-includes/block-supports/generated-classname.php index 5bb1a14ae7..4f38fd3103 100644 --- a/wp-includes/block-supports/generated-classname.php +++ b/wp-includes/block-supports/generated-classname.php @@ -47,7 +47,7 @@ function wp_apply_generated_classname_support( $block_type, $block_attributes ) $has_generated_classname_support = true; $attributes = array(); if ( property_exists( $block_type, 'supports' ) ) { - $has_generated_classname_support = wp_array_get( $block_type->supports, array( 'className' ), true ); + $has_generated_classname_support = _wp_array_get( $block_type->supports, array( 'className' ), true ); } if ( $has_generated_classname_support ) { $block_classname = wp_get_block_default_classname( $block_type->name ); diff --git a/wp-includes/block-supports/typography.php b/wp-includes/block-supports/typography.php index 3fbfd2f068..4aae06c980 100644 --- a/wp-includes/block-supports/typography.php +++ b/wp-includes/block-supports/typography.php @@ -15,12 +15,12 @@ function wp_register_typography_support( $block_type ) { $has_font_size_support = false; if ( property_exists( $block_type, 'supports' ) ) { - $has_font_size_support = wp_array_get( $block_type->supports, array( '__experimentalFontSize' ), false ); + $has_font_size_support = _wp_array_get( $block_type->supports, array( '__experimentalFontSize' ), false ); } $has_line_height_support = false; if ( property_exists( $block_type, 'supports' ) ) { - $has_line_height_support = wp_array_get( $block_type->supports, array( '__experimentalLineHeight' ), false ); + $has_line_height_support = _wp_array_get( $block_type->supports, array( '__experimentalLineHeight' ), false ); } if ( ! $block_type->attributes ) { @@ -56,12 +56,12 @@ function wp_apply_typography_support( $block_type, $block_attributes ) { $classes = array(); $styles = array(); if ( property_exists( $block_type, 'supports' ) ) { - $has_font_size_support = wp_array_get( $block_type->supports, array( 'fontSize' ), false ); + $has_font_size_support = _wp_array_get( $block_type->supports, array( 'fontSize' ), false ); } $has_line_height_support = false; if ( property_exists( $block_type, 'supports' ) ) { - $has_line_height_support = wp_array_get( $block_type->supports, array( 'lineHeight' ), false ); + $has_line_height_support = _wp_array_get( $block_type->supports, array( 'lineHeight' ), false ); } // Font Size. diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 08bae745bd..58a098f916 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -4528,30 +4528,46 @@ function wp_array_slice_assoc( $array, $keys ) { /** * Accesses an array in depth based on a path of keys. * - * It is the PHP equivalent of JavaScript's lodash.get, and mirroring it may help other components + * It is the PHP equivalent of JavaScript's `lodash.get()` and mirroring it may help other components * retain some symmetry between client and server implementations. * + * Example usage: + * + * $array = array( + * 'a' => array( + * 'b' => array( + * 'c' => 1, + * ), + * ), + * ); + * _wp_array_get( $array, array( 'a', 'b', 'c' ); + * + * @internal + * * @since 5.6.0 + * @access private * * @param array $array An array from which we want to retrieve some information. * @param array $path An array of keys describing the path with which to retrieve information. - * @param array $default The return value if the path is not set on the array, - * or if the types of array and path are not arrays. - * @return array An array matching the path specified. + * @param mixed $default The return value if the path does not exist within the array, + * or if `$array` or `$path` are not arrays. + * @return mixed The value from the path specified. */ -function wp_array_get( $array, $path, $default = array() ) { - // Confirm input values are expected type to avoid notice warnings. - if ( ! is_array( $array ) || ! is_array( $path ) ) { +function _wp_array_get( $array, $path, $default = null ) { + // Confirm $path is valid. + if ( ! is_array( $path ) || 0 === count( $path ) ) { return $default; } - $path_length = count( $path ); - - for ( $i = 0; $i < $path_length; ++$i ) { - if ( ! isset( $array[ $path[ $i ] ] ) ) { + foreach ( $path as $path_element ) { + if ( + ! is_array( $array ) || + ( ! is_string( $path_element ) && ! is_integer( $path_element ) && ! is_null( $path_element ) ) || + ! array_key_exists( $path_element, $array ) + ) { return $default; } - $array = $array[ $path[ $i ] ]; + $array = $array[ $path_element ]; } return $array; diff --git a/wp-includes/version.php b/wp-includes/version.php index b557ec0049..24be89acda 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.6-beta3-49579'; +$wp_version = '5.6-beta3-49580'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.