Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#41488: Fix handling of backslashes encoded in in partial placement context data #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/wp-includes/class-wp-customize-nav-menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -1265,14 +1265,14 @@ public function filter_wp_nav_menu_args( $args ) {
*
* @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;
}
Expand Down
19 changes: 15 additions & 4 deletions tests/phpunit/tests/customize/nav-menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,19 +861,27 @@ function test_filter_wp_nav_menu_args() {
/**
* 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 );
Expand All @@ -883,7 +891,10 @@ function test_filter_wp_nav_menu() {

$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'] );
}

/**
Expand Down