Make WordPress Core

Changeset 49580


Ignore:
Timestamp:
11/12/2020 08:18:08 PM (4 years ago)
Author:
johnbillion
Message:

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

Location:
trunk/src/wp-includes
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/align.php

    r49226 r49580  
    1616    $has_align_support = false;
    1717    if ( property_exists( $block_type, 'supports' ) ) {
    18         $has_align_support = wp_array_get( $block_type->supports, array( 'align' ), false );
     18        $has_align_support = _wp_array_get( $block_type->supports, array( 'align' ), false );
    1919    }
    2020    if ( $has_align_support ) {
     
    4747    $has_align_support = false;
    4848    if ( property_exists( $block_type, 'supports' ) ) {
    49         $has_align_support = wp_array_get( $block_type->supports, array( 'align' ), false );
     49        $has_align_support = _wp_array_get( $block_type->supports, array( 'align' ), false );
    5050    }
    5151    if ( $has_align_support ) {
  • trunk/src/wp-includes/block-supports/colors.php

    r49226 r49580  
    1616    $color_support = false;
    1717    if ( property_exists( $block_type, 'supports' ) ) {
    18         $color_support = wp_array_get( $block_type->supports, array( '__experimentalColor' ), false );
     18        $color_support = _wp_array_get( $block_type->supports, array( '__experimentalColor' ), false );
    1919    }
    20     $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && wp_array_get( $color_support, array( 'text' ), true ) );
    21     $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && wp_array_get( $color_support, array( 'background' ), true ) );
    22     $has_gradients_support         = wp_array_get( $color_support, array( 'gradients' ), false );
     20    $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
     21    $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
     22    $has_gradients_support         = _wp_array_get( $color_support, array( 'gradients' ), false );
    2323
    2424    if ( ! $block_type->attributes ) {
     
    6464 */
    6565function wp_apply_colors_support( $block_type, $block_attributes ) {
    66     $color_support                 = wp_array_get( $block_type->supports, array( '__experimentalColor' ), false );
    67     $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && wp_array_get( $color_support, array( 'text' ), true ) );
    68     $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && wp_array_get( $color_support, array( 'background' ), true ) );
    69     $has_link_colors_support       = wp_array_get( $color_support, array( 'linkColor' ), false );
    70     $has_gradients_support         = wp_array_get( $color_support, array( 'gradients' ), false );
     66    $color_support                 = _wp_array_get( $block_type->supports, array( '__experimentalColor' ), false );
     67    $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
     68    $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
     69    $has_link_colors_support       = _wp_array_get( $color_support, array( 'linkColor' ), false );
     70    $has_gradients_support         = _wp_array_get( $color_support, array( 'gradients' ), false );
    7171    $classes                       = array();
    7272    $styles                        = array();
  • trunk/src/wp-includes/block-supports/custom-classname.php

    r49226 r49580  
    1616    $has_custom_classname_support = true;
    1717    if ( property_exists( $block_type, 'supports' ) ) {
    18         $has_custom_classname_support = wp_array_get( $block_type->supports, array( 'customClassName' ), true );
     18        $has_custom_classname_support = _wp_array_get( $block_type->supports, array( 'customClassName' ), true );
    1919    }
    2020    if ( $has_custom_classname_support ) {
     
    4545    $attributes                   = array();
    4646    if ( property_exists( $block_type, 'supports' ) ) {
    47         $has_custom_classname_support = wp_array_get( $block_type->supports, array( 'customClassName' ), true );
     47        $has_custom_classname_support = _wp_array_get( $block_type->supports, array( 'customClassName' ), true );
    4848    }
    4949    if ( $has_custom_classname_support ) {
  • trunk/src/wp-includes/block-supports/generated-classname.php

    r49226 r49580  
    4848    $attributes                      = array();
    4949    if ( property_exists( $block_type, 'supports' ) ) {
    50         $has_generated_classname_support = wp_array_get( $block_type->supports, array( 'className' ), true );
     50        $has_generated_classname_support = _wp_array_get( $block_type->supports, array( 'className' ), true );
    5151    }
    5252    if ( $has_generated_classname_support ) {
  • trunk/src/wp-includes/block-supports/typography.php

    r49226 r49580  
    1616    $has_font_size_support = false;
    1717    if ( property_exists( $block_type, 'supports' ) ) {
    18         $has_font_size_support = wp_array_get( $block_type->supports, array( '__experimentalFontSize' ), false );
     18        $has_font_size_support = _wp_array_get( $block_type->supports, array( '__experimentalFontSize' ), false );
    1919    }
    2020
    2121    $has_line_height_support = false;
    2222    if ( property_exists( $block_type, 'supports' ) ) {
    23         $has_line_height_support = wp_array_get( $block_type->supports, array( '__experimentalLineHeight' ), false );
     23        $has_line_height_support = _wp_array_get( $block_type->supports, array( '__experimentalLineHeight' ), false );
    2424    }
    2525
     
    5757    $styles                = array();
    5858    if ( property_exists( $block_type, 'supports' ) ) {
    59         $has_font_size_support = wp_array_get( $block_type->supports, array( 'fontSize' ), false );
     59        $has_font_size_support = _wp_array_get( $block_type->supports, array( 'fontSize' ), false );
    6060    }
    6161
    6262    $has_line_height_support = false;
    6363    if ( property_exists( $block_type, 'supports' ) ) {
    64         $has_line_height_support = wp_array_get( $block_type->supports, array( 'lineHeight' ), false );
     64        $has_line_height_support = _wp_array_get( $block_type->supports, array( 'lineHeight' ), false );
    6565    }
    6666
  • trunk/src/wp-includes/functions.php

    r49552 r49580  
    45294529 * Accesses an array in depth based on a path of keys.
    45304530 *
    4531  * It is the PHP equivalent of JavaScript's lodash.get, and mirroring it may help other components
     4531 * It is the PHP equivalent of JavaScript's `lodash.get()` and mirroring it may help other components
    45324532 * retain some symmetry between client and server implementations.
    45334533 *
     4534 * Example usage:
     4535 *
     4536 *     $array = array(
     4537 *         'a' => array(
     4538 *             'b' => array(
     4539 *                 'c' => 1,
     4540 *             ),
     4541 *         ),
     4542 *     );
     4543 *     _wp_array_get( $array, array( 'a', 'b', 'c' );
     4544 *
     4545 * @internal
     4546 *
    45344547 * @since 5.6.0
     4548 * @access private
    45354549 *
    45364550 * @param array $array   An array from which we want to retrieve some information.
    45374551 * @param array $path    An array of keys describing the path with which to retrieve information.
    4538  * @param array $default The return value if the path is not set on the array,
    4539  *                       or if the types of array and path are not arrays.
    4540  * @return array An array matching the path specified.
    4541  */
    4542 function wp_array_get( $array, $path, $default = array() ) {
    4543     // Confirm input values are expected type to avoid notice warnings.
    4544     if ( ! is_array( $array ) || ! is_array( $path ) ) {
     4552 * @param mixed $default The return value if the path does not exist within the array,
     4553 *                       or if `$array` or `$path` are not arrays.
     4554 * @return mixed The value from the path specified.
     4555 */
     4556function _wp_array_get( $array, $path, $default = null ) {
     4557    // Confirm $path is valid.
     4558    if ( ! is_array( $path ) || 0 === count( $path ) ) {
    45454559        return $default;
    45464560    }
    45474561
    4548     $path_length = count( $path );
    4549 
    4550     for ( $i = 0; $i < $path_length; ++$i ) {
    4551         if ( ! isset( $array[ $path[ $i ] ] ) ) {
     4562    foreach ( $path as $path_element ) {
     4563        if (
     4564            ! is_array( $array ) ||
     4565            ( ! is_string( $path_element ) && ! is_integer( $path_element ) && ! is_null( $path_element ) ) ||
     4566            ! array_key_exists( $path_element, $array )
     4567        ) {
    45524568            return $default;
    45534569        }
    4554         $array = $array[ $path[ $i ] ];
     4570        $array = $array[ $path_element ];
    45554571    }
    45564572
Note: See TracChangeset for help on using the changeset viewer.