Customize: For selective refresh of nav menus, prevent preg_replace()
from dropping backslashes in JSON replaced into the data-customize-partial-placement-context
HTML attribute.
Props dlh, westonruter. Fixes #41488. git-svn-id: https://develop.svn.wordpress.org/trunk@41204 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
bce1b14c9a
commit
57e57505ba
@ -1265,14 +1265,14 @@ final class WP_Customize_Nav_Menus {
|
||||
*
|
||||
* @param string $nav_menu_content The HTML content for the navigation menu.
|
||||
* @param object $args An object containing wp_nav_menu() arguments.
|
||||
* @return null
|
||||
* @return string Nav menu HTML with selective refresh attributes added if partial can be refreshed.
|
||||
*/
|
||||
public function filter_wp_nav_menu( $nav_menu_content, $args ) {
|
||||
if ( isset( $args->customize_preview_nav_menus_args['can_partial_refresh'] ) && $args->customize_preview_nav_menus_args['can_partial_refresh'] ) {
|
||||
$attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'nav_menu_instance[' . $args->customize_preview_nav_menus_args['args_hmac'] . ']' ) );
|
||||
$attributes .= ' data-customize-partial-type="nav_menu_instance"';
|
||||
$attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $args->customize_preview_nav_menus_args ) ) );
|
||||
$nav_menu_content = preg_replace( '#^(<\w+)#', '$1 ' . $attributes, $nav_menu_content, 1 );
|
||||
$nav_menu_content = preg_replace( '#^(<\w+)#', '$1 ' . str_replace( '\\', '\\\\', $attributes ), $nav_menu_content, 1 );
|
||||
}
|
||||
return $nav_menu_content;
|
||||
}
|
||||
|
@ -861,19 +861,27 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
/**
|
||||
* Test the filter_wp_nav_menu method.
|
||||
*
|
||||
* @see WP_Customize_Nav_Menus::filter_wp_nav_menu()
|
||||
* @covers WP_Customize_Nav_Menus::filter_wp_nav_menu()
|
||||
* @covers WP_Customize_Nav_Menus::filter_wp_nav_menu_args()
|
||||
*/
|
||||
function test_filter_wp_nav_menu() {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
|
||||
|
||||
$args = $menus->filter_wp_nav_menu_args( array(
|
||||
$original_args = array(
|
||||
'echo' => true,
|
||||
'menu' => wp_create_nav_menu( 'Foo' ),
|
||||
'fallback_cb' => 'wp_page_menu',
|
||||
'walker' => '',
|
||||
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
|
||||
) );
|
||||
);
|
||||
|
||||
// Add global namespace prefix to check #41488.
|
||||
if ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
|
||||
$original_args['fallback_cb'] = '\\' . $original_args['fallback_cb'];
|
||||
}
|
||||
|
||||
$args = $menus->filter_wp_nav_menu_args( $original_args );
|
||||
|
||||
ob_start();
|
||||
wp_nav_menu( $args );
|
||||
@ -883,7 +891,10 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
|
||||
$this->assertContains( sprintf( ' data-customize-partial-id="nav_menu_instance[%s]"', $args['customize_preview_nav_menus_args']['args_hmac'] ), $result );
|
||||
$this->assertContains( ' data-customize-partial-type="nav_menu_instance"', $result );
|
||||
$this->assertContains( ' data-customize-partial-placement-context="', $result );
|
||||
$this->assertTrue( (bool) preg_match( '/data-customize-partial-placement-context="(.+?)"/', $result, $matches ) );
|
||||
$context = json_decode( html_entity_decode( $matches[1] ), true );
|
||||
$this->assertEquals( $original_args, wp_array_slice_assoc( $context, array_keys( $original_args ) ) ); // Because assertArraySubset is not available in PHP 5.2.
|
||||
$this->assertTrue( $context['can_partial_refresh'] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user