mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@37748 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group menu
|
||||
* @ticket 36590
|
||||
*/
|
||||
class Tests_Menu_WpExpandNavMenuPostData extends WP_UnitTestCase {
|
||||
public function test_unnested_data_should_expand() {
|
||||
include_once( ABSPATH . 'wp-admin/includes/nav-menu.php' );
|
||||
|
||||
if ( empty( $_POST ) ) {
|
||||
$_POST = array();
|
||||
}
|
||||
|
||||
$data = array();
|
||||
$data[0] = new StdClass;
|
||||
$data[0]->name = 'yesorno';
|
||||
$data[0]->value = 'yes';
|
||||
$_POST['nav-menu-data'] = addslashes( json_encode( $data ) );
|
||||
|
||||
_wp_expand_nav_menu_post_data();
|
||||
|
||||
$expected = array(
|
||||
'nav-menu-data' => $_POST['nav-menu-data'],
|
||||
'yesorno' => 'yes'
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected, $_POST );
|
||||
}
|
||||
|
||||
public function test_multidimensional_nested_array_should_expand() {
|
||||
include_once( ABSPATH . 'wp-admin/includes/nav-menu.php' );
|
||||
|
||||
if ( empty( $_POST ) ) {
|
||||
$_POST = array();
|
||||
}
|
||||
|
||||
$data = array();
|
||||
$data[0] = new StdClass;
|
||||
$data[0]->name = 'would[1][do][the][trick]';
|
||||
$data[0]->value = 'yes';
|
||||
$_POST['nav-menu-data'] = addslashes( json_encode( $data ) );
|
||||
|
||||
_wp_expand_nav_menu_post_data();
|
||||
|
||||
$expected = array(
|
||||
'nav-menu-data' => $_POST['nav-menu-data'],
|
||||
'would' => array(
|
||||
1 => array(
|
||||
'do' => array(
|
||||
'the' => array(
|
||||
'trick' => 'yes',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->assertEquals( $expected, $_POST );
|
||||
}
|
||||
|
||||
public function test_multidimensional_nested_array_should_expand_and_merge() {
|
||||
include_once( ABSPATH . 'wp-admin/includes/nav-menu.php' );
|
||||
|
||||
if ( empty( $_POST ) ) {
|
||||
$_POST = array();
|
||||
}
|
||||
|
||||
$data = array();
|
||||
$data[0] = new StdClass;
|
||||
$data[0]->name = 'would[1][do][the][trick]';
|
||||
$data[0]->value = 'yes';
|
||||
$data[1] = new StdClass;
|
||||
$data[1]->name = 'would[2][do][the][trick]';
|
||||
$data[1]->value = 'yes';
|
||||
$data[2] = new StdClass;
|
||||
$data[2]->name = 'would[2][do][the][job]';
|
||||
$data[2]->value = 'yes';
|
||||
$_POST['nav-menu-data'] = addslashes( json_encode( $data ) );
|
||||
|
||||
_wp_expand_nav_menu_post_data();
|
||||
|
||||
$expected = array(
|
||||
'nav-menu-data' => $_POST['nav-menu-data'],
|
||||
'would' => array(
|
||||
1 => array(
|
||||
'do' => array(
|
||||
'the' => array(
|
||||
'trick' => 'yes',
|
||||
),
|
||||
),
|
||||
),
|
||||
2 => array(
|
||||
'do' => array(
|
||||
'the' => array(
|
||||
'trick' => 'yes',
|
||||
'job' => 'yes',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected, $_POST );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user