Ticket #41502: 41502.diff
File 41502.diff, 3.9 KB (added by , 7 years ago) |
---|
-
src/wp-admin/includes/nav-menu.php
function wp_nav_menu_update_menu_items( 1223 1223 * If a JSON blob of navigation menu data is in POST data, expand it and inject 1224 1224 * it into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134. 1225 1225 * 1226 1226 * @ignore 1227 1227 * @since 4.5.3 1228 1228 * @access private 1229 1229 */ 1230 1230 function _wp_expand_nav_menu_post_data() { 1231 1231 if ( ! isset( $_POST['nav-menu-data'] ) ) { 1232 1232 return; 1233 1233 } 1234 1234 1235 1235 $data = json_decode( stripslashes( $_POST['nav-menu-data'] ) ); 1236 1236 1237 1237 if ( ! is_null( $data ) && $data ) { 1238 $seen_empty_data_keys = array(); 1238 1239 foreach ( $data as $post_input_data ) { 1239 1240 // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`), 1240 1241 // derive the array path keys via regex and set the value in $_POST. 1241 preg_match( '#([^\[]*)(\[(. +)\])?#', $post_input_data->name, $matches );1242 preg_match( '#([^\[]*)(\[(.*)\])?#', $post_input_data->name, $matches ); 1242 1243 1243 1244 $array_bits = array( $matches[1] ); 1244 1245 1245 if ( isset( $matches[3] ) ) { 1246 if ( !empty( $matches[3] ) ) { 1247 // $name[..] 1246 1248 $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) ); 1249 } elseif ( isset( $matches[2] ) ) { 1250 // $name[] 1251 if ( ! isset( $seen_empty_data_keys[ $matches[1] ] ) ) { 1252 $seen_empty_data_keys[ $matches[1] ] = 0; 1253 } 1254 1255 $array_bits[] = $seen_empty_data_keys[ $matches[1] ]++; 1247 1256 } 1248 1257 1249 1258 $new_post_data = array(); 1250 1259 1251 1260 // Build the new array value from leaf to trunk. 1252 1261 for ( $i = count( $array_bits ) - 1; $i >= 0; $i -- ) { 1253 1262 if ( $i == count( $array_bits ) - 1 ) { 1254 1263 $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value ); 1255 1264 } else { 1256 1265 $new_post_data = array( $array_bits[ $i ] => $new_post_data ); 1257 1266 } 1258 1267 } 1259 1268 1260 1269 $_POST = array_replace_recursive( $_POST, $new_post_data ); 1261 1270 } -
tests/phpunit/tests/menu/wpExpandNavMenuPostData.php
class Tests_Menu_WpExpandNavMenuPostData 16 16 $data[0] = new StdClass; 17 17 $data[0]->name = 'yesorno'; 18 18 $data[0]->value = 'yes'; 19 19 $_POST['nav-menu-data'] = addslashes( json_encode( $data ) ); 20 20 21 21 _wp_expand_nav_menu_post_data(); 22 22 23 23 $expected = array( 24 24 'nav-menu-data' => $_POST['nav-menu-data'], 25 25 'yesorno' => 'yes', 26 26 ); 27 27 28 28 $this->assertEquals( $expected, $_POST ); 29 29 } 30 30 31 /** 32 * @ticket 41502 33 */ 34 public function test_ungrouped_data_should_expand() { 35 include_once( ABSPATH . 'wp-admin/includes/nav-menu.php' ); 36 37 if ( empty( $_POST ) ) { 38 $_POST = array(); 39 } 40 41 $data = array(); 42 $data[0] = new StdClass; 43 $data[0]->name = 'yesorno[]'; 44 $data[0]->value = 'yes'; 45 $data[1] = new StdClass; 46 $data[1]->name = 'yesorno[]'; 47 $data[1]->value = 'maybe'; 48 $_POST['nav-menu-data'] = addslashes( json_encode( $data ) ); 49 50 _wp_expand_nav_menu_post_data(); 51 52 $expected = array( 53 'nav-menu-data' => $_POST['nav-menu-data'], 54 'yesorno' => array( 55 'yes', 56 'maybe' 57 ), 58 ); 59 60 $this->assertEquals( $expected, $_POST ); 61 } 62 31 63 public function test_multidimensional_nested_array_should_expand() { 32 64 include_once( ABSPATH . 'wp-admin/includes/nav-menu.php' ); 33 65 34 66 if ( empty( $_POST ) ) { 35 67 $_POST = array(); 36 68 } 37 69 38 70 $data = array(); 39 71 $data[0] = new StdClass; 40 72 $data[0]->name = 'would[1][do][the][trick]'; 41 73 $data[0]->value = 'yes'; 42 74 $_POST['nav-menu-data'] = addslashes( json_encode( $data ) ); 43 75 44 76 _wp_expand_nav_menu_post_data(); 45 77