Make WordPress Core

Changeset 37754

Timestamp:
06/20/2016 07:49:49 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.

Merge of [37748] and [37750] to the 4.5 branch.

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

Location:
branches/4.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.5

  • branches/4.5/src/wp-admin/nav-menus.php

    r36852 r37754  
    5050$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'edit';
    5151
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
     117
     118
     119
     120
     121
     122
     123
     124
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
    52148/*
    53149 * If a JSON blob of navigation menu data is found, expand it and inject it
    54150 * into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134.
    55151 */
    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 }
     152_wp_expand_nav_menu_post_data();
     153
    76154switch ( $action ) {
    77155    case 'add-menu-item':
Note: See TracChangeset for help on using the changeset viewer.