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
This commit is contained in:
John Blackbourn 2020-11-12 20:20:12 +00:00
parent 1d738052b1
commit 9115246f72
7 changed files with 47 additions and 31 deletions

View File

@ -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 );

View File

@ -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();

View File

@ -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 );

View File

@ -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 );

View File

@ -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.

View File

@ -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;

View File

@ -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.