Ticket #36590: 36590-4.5.2.diff
File 36590-4.5.2.diff, 7.0 KB (added by , 8 years ago) |
---|
-
src/wp-admin/nav-menus.php
diff --git src/wp-admin/nav-menus.php src/wp-admin/nav-menus.php index ab15bed..9d05df8 100644
$num_locations = count( array_keys( $locations ) ); 49 49 // Allowed actions: add, update, delete 50 50 $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'edit'; 51 51 52 /* 53 * If a JSON blob of navigation menu data is found, expand it and inject it 54 * into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134. 52 /** 53 * If a JSON blob of navigation menu data is in POST data, expand it and inject 54 * it into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134. 55 * 56 * @ignore 57 * @since 4.5.2 58 * @access private 55 59 */ 56 if ( isset( $_POST['nav-menu-data'] ) ) { 60 function _wp_expand_nav_menu_post_data() { 61 if ( ! isset( $_POST['nav-menu-data'] ) ) { 62 return; 63 } 64 57 65 $data = json_decode( stripslashes( $_POST['nav-menu-data'] ) ); 66 58 67 if ( ! is_null( $data ) && $data ) { 59 68 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]; 69 // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`), 70 // derive the array path keys via regex and set the value in $_POST. 71 preg_match( '#([^\[]*)(\[(.+)\])?#', $post_input_data->name, $matches ); 72 73 $array_bits = array( $matches[1] ); 74 75 if ( isset( $matches[3] ) ) { 76 $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) ); 77 } 78 79 $new_post_data = array(); 80 81 // Build the new array value from leaf to trunk. 82 for ( $i = count( $array_bits ) - 1; $i >= 0; $i -- ) { 83 if ( $i == count( $array_bits ) - 1 ) { 84 $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value ); 85 } else { 86 $new_post_data = array( $array_bits[ $i ] => $new_post_data ); 68 87 } 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 88 } 89 90 $_POST = array_replace_recursive( $_POST, $new_post_data ); 73 91 } 74 92 } 75 93 } 94 95 if ( ! function_exists( 'array_replace_recursive' ) ) : 96 /** 97 * PHP-agnostic version of {@link array_replace_recursive()}. 98 * 99 * The array_replace_recursive() function is a PHP 5.3 function. WordPress 100 * currently supports down to PHP 5.2, so this method is a workaround 101 * for PHP 5.2. 102 * 103 * Note: array_replace_recursive() supports infinite arguments, but for our use- 104 * case, we only need to support two arguments. 105 * 106 * Subject to removal once WordPress makes PHP 5.3.0 the minimum requirement. 107 * 108 * @since 4.5.2 109 * 110 * @see http://php.net/manual/en/function.array-replace-recursive.php#109390 111 * 112 * @param array $base Array with keys needing to be replaced. 113 * @param array $replacements Array with the replaced keys. 114 * 115 * @return array 116 */ 117 function array_replace_recursive( $base = array(), $replacements = array() ) { 118 if ( function_exists( 'array_replace_recursive' ) ) { 119 return array_replace_recursive( $base, $replacements ); 120 } 121 122 // PHP 5.2-compatible version 123 // http://php.net/manual/en/function.array-replace-recursive.php#109390. 124 foreach ( array_slice( func_get_args(), 1 ) as $replacements ) { 125 $bref_stack = array( &$base ); 126 $head_stack = array( $replacements ); 127 128 do { 129 end( $bref_stack ); 130 131 $bref = &$bref_stack[ key( $bref_stack ) ]; 132 $head = array_pop( $head_stack ); 133 134 unset( $bref_stack[ key( $bref_stack ) ] ); 135 136 foreach ( array_keys( $head ) as $key ) { 137 if ( isset( $key, $bref ) && is_array( $bref[ $key ] ) && is_array( $head[ $key ] ) ) { 138 $bref_stack[] = &$bref[ $key ]; 139 $head_stack[] = $head[ $key ]; 140 } else { 141 $bref[ $key ] = $head[ $key ]; 142 } 143 } 144 } while ( count( $head_stack ) ); 145 } 146 147 return $base; 148 } 149 endif; 150 151 /* 152 * If a JSON blob of navigation menu data is found, expand it and inject it 153 * into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134. 154 */ 155 _wp_expand_nav_menu_post_data(); 156 76 157 switch ( $action ) { 77 158 case 'add-menu-item': 78 159 check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' ); -
new file tests/phpunit/tests/menu/wpExpandNavMenuPostData.php
diff --git tests/phpunit/tests/menu/wpExpandNavMenuPostData.php tests/phpunit/tests/menu/wpExpandNavMenuPostData.php new file mode 100644 index 0000000..361c4f9
- + 1 <?php 2 3 /** 4 * @group menu 5 * @ticket 36590 6 */ 7 class Tests_Menu_WpExpandNavMenuPostData extends WP_UnitTestCase { 8 public function test_unnested_data_should_expand() { 9 include_once( ABSPATH . 'wp-admin/includes/nav-menu.php' ); 10 11 if ( empty( $_POST ) ) { 12 $_POST = array(); 13 } 14 15 $data = array(); 16 $data[0] = new StdClass; 17 $data[0]->name = 'yesorno'; 18 $data[0]->value = 'yes'; 19 $_POST['nav-menu-data'] = addslashes( json_encode( $data ) ); 20 21 _wp_expand_nav_menu_post_data(); 22 23 $expected = array( 24 'nav-menu-data' => $_POST['nav-menu-data'], 25 'yesorno' => 'yes' 26 ); 27 28 $this->assertEquals( $expected, $_POST ); 29 } 30 31 public function test_multidimensional_nested_array_should_expand() { 32 include_once( ABSPATH . 'wp-admin/includes/nav-menu.php' ); 33 34 if ( empty( $_POST ) ) { 35 $_POST = array(); 36 } 37 38 $data = array(); 39 $data[0] = new StdClass; 40 $data[0]->name = 'would[1][do][the][trick]'; 41 $data[0]->value = 'yes'; 42 $_POST['nav-menu-data'] = addslashes( json_encode( $data ) ); 43 44 _wp_expand_nav_menu_post_data(); 45 46 $expected = array( 47 'nav-menu-data' => $_POST['nav-menu-data'], 48 'would' => array( 49 1 => array( 50 'do' => array( 51 'the' => array( 52 'trick' => 'yes', 53 ), 54 ), 55 ), 56 ), 57 ); 58 $this->assertEquals( $expected, $_POST ); 59 } 60 61 public function test_multidimensional_nested_array_should_expand_and_merge() { 62 include_once( ABSPATH . 'wp-admin/includes/nav-menu.php' ); 63 64 if ( empty( $_POST ) ) { 65 $_POST = array(); 66 } 67 68 $data = array(); 69 $data[0] = new StdClass; 70 $data[0]->name = 'would[1][do][the][trick]'; 71 $data[0]->value = 'yes'; 72 $data[1] = new StdClass; 73 $data[1]->name = 'would[2][do][the][trick]'; 74 $data[1]->value = 'yes'; 75 $data[2] = new StdClass; 76 $data[2]->name = 'would[2][do][the][job]'; 77 $data[2]->value = 'yes'; 78 $_POST['nav-menu-data'] = addslashes( json_encode( $data ) ); 79 80 _wp_expand_nav_menu_post_data(); 81 82 $expected = array( 83 'nav-menu-data' => $_POST['nav-menu-data'], 84 'would' => array( 85 1 => array( 86 'do' => array( 87 'the' => array( 88 'trick' => 'yes', 89 ), 90 ), 91 ), 92 2 => array( 93 'do' => array( 94 'the' => array( 95 'trick' => 'yes', 96 'job' => 'yes', 97 ), 98 ), 99 ), 100 ), 101 ); 102 103 $this->assertEquals( $expected, $_POST ); 104 } 105 }