Make WordPress Core


Ignore:
Timestamp:
06/19/2016 12:24:23 PM (7 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-admin/nav-menus.php

    r37488 r37748  
    5454 * into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134.
    5555 */
    56 if ( isset( $_POST['nav-menu-data'] ) ) {
    57     $data = json_decode( stripslashes( $_POST['nav-menu-data'] ) );
    58     if ( ! is_null( $data ) && $data ) {
    59         foreach ( $data as $post_input_data ) {
    60             // For input names that are arrays (e.g. `menu-item-db-id[3]`), derive the array path keys via regex.
    61             if ( preg_match( '#(.*)\[(\w+)\]#', $post_input_data->name, $matches ) ) {
    62                 if ( empty( $_POST[ $matches[1] ] ) ) {
    63                     $_POST[ $matches[1] ] = array();
    64                 }
    65                 // Cast input elements with a numeric array index to integers.
    66                 if ( is_numeric( $matches[2] ) ) {
    67                     $matches[2] = (int) $matches[2];
    68                 }
    69                 $_POST[ $matches[1] ][ $matches[2] ] = wp_slash( $post_input_data->value );
    70             } else {
    71                 $_POST[ $post_input_data->name ] = wp_slash( $post_input_data->value );
    72             }
    73         }
    74     }
    75 }
     56_wp_expand_nav_menu_post_data();
     57
    7658switch ( $action ) {
    7759    case 'add-menu-item':
Note: See TracChangeset for help on using the changeset viewer.