Make WordPress Core


Ignore:
Timestamp:
06/19/2016 12:24:23 PM (8 years ago)
Author:
swissspidy
Message:

Menus: Support nested array variables in POST data when saving menus.

[36510] allowed larger menus to be created in the Edit Menu screen by JSON-encoding the entire form into a single input field. However, it did not correctly handle nested arrays.

This introduces a new _wp_expand_nav_menu_post_data() helper function to handle this POST data which uses array_replace_recursive() internally. Since the latter is only available on PHP 5.3+, we add a compatibility function to ensure PHP 5.2 support.

Props ericlewis, neverything, swissspidy.
Fixes #36590 for trunk. See #14134.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r37674 r37748  
    436436}
    437437
     438if ( ! function_exists( 'array_replace_recursive' ) ) :
     439    /**
     440     * PHP-agnostic version of {@link array_replace_recursive()}.
     441     *
     442     * The array_replace_recursive() function is a PHP 5.3 function. WordPress
     443     * currently supports down to PHP 5.2, so this method is a workaround
     444     * for PHP 5.2.
     445     *
     446     * Note: array_replace_recursive() supports infinite arguments, but for our use-
     447     * case, we only need to support two arguments.
     448     *
     449     * Subject to removal once WordPress makes PHP 5.3.0 the minimum requirement.
     450     *
     451     * @since 4.5.3
     452     *
     453     * @see http://php.net/manual/en/function.array-replace-recursive.php#109390
     454     *
     455     * @param  array $base         Array with keys needing to be replaced.
     456     * @param  array $replacements Array with the replaced keys.
     457     *
     458     * @return array
     459     */
     460    function array_replace_recursive( $base = array(), $replacements = array() ) {
     461        foreach ( array_slice( func_get_args(), 1 ) as $replacements ) {
     462            $bref_stack = array( &$base );
     463            $head_stack = array( $replacements );
     464
     465            do {
     466                end( $bref_stack );
     467
     468                $bref = &$bref_stack[ key( $bref_stack ) ];
     469                $head = array_pop( $head_stack );
     470
     471                unset( $bref_stack[ key( $bref_stack ) ] );
     472
     473                foreach ( array_keys( $head ) as $key ) {
     474                    if ( isset( $key, $bref ) && is_array( $bref[ $key ] ) && is_array( $head[ $key ] ) ) {
     475                        $bref_stack[] = &$bref[ $key ];
     476                        $head_stack[] = $head[ $key ];
     477                    } else {
     478                        $bref[ $key ] = $head[ $key ];
     479                    }
     480                }
     481            } while ( count( $head_stack ) );
     482        }
     483
     484        return $base;
     485    }
     486endif;
     487
    438488// SPL can be disabled on PHP 5.2
    439489if ( ! function_exists( 'spl_autoload_register' ) ):
Note: See TracChangeset for help on using the changeset viewer.