WordPress.org

Make WordPress Core

Changeset 41204


Ignore:
Timestamp:
08/02/2017 05:10:05 AM (4 years ago)
Author:
westonruter
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-nav-menus.php

    r41162 r41204  
    12661266     * @param string $nav_menu_content The HTML content for the navigation menu.
    12671267     * @param object $args             An object containing wp_nav_menu() arguments.
    1268      * @return null
     1268     * @return string Nav menu HTML with selective refresh attributes added if partial can be refreshed.
    12691269     */
    12701270    public function filter_wp_nav_menu( $nav_menu_content, $args ) {
     
    12731273            $attributes .= ' data-customize-partial-type="nav_menu_instance"';
    12741274            $attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $args->customize_preview_nav_menus_args ) ) );
    1275             $nav_menu_content = preg_replace( '#^(<\w+)#', '$1 ' . $attributes, $nav_menu_content, 1 );
     1275            $nav_menu_content = preg_replace( '#^(<\w+)#', '$1 ' . str_replace( '\\', '\\\\', $attributes ), $nav_menu_content, 1 );
    12761276        }
    12771277        return $nav_menu_content;
  • trunk/tests/phpunit/tests/customize/nav-menus.php

    r39924 r41204  
    862862     * Test the filter_wp_nav_menu method.
    863863     *
    864      * @see WP_Customize_Nav_Menus::filter_wp_nav_menu()
     864     * @covers WP_Customize_Nav_Menus::filter_wp_nav_menu()
     865     * @covers WP_Customize_Nav_Menus::filter_wp_nav_menu_args()
    865866     */
    866867    function test_filter_wp_nav_menu() {
     
    868869        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
    869870
    870         $args = $menus->filter_wp_nav_menu_args( array(
     871        $original_args = array(
    871872            'echo'        => true,
    872873            'menu'        => wp_create_nav_menu( 'Foo' ),
     
    874875            'walker'      => '',
    875876            'items_wrap'  => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    876         ) );
     877        );
     878
     879        // Add global namespace prefix to check #41488.
     880        if ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
     881            $original_args['fallback_cb'] = '\\' . $original_args['fallback_cb'];
     882        }
     883
     884        $args = $menus->filter_wp_nav_menu_args( $original_args );
    877885
    878886        ob_start();
     
    884892        $this->assertContains( sprintf( ' data-customize-partial-id="nav_menu_instance[%s]"', $args['customize_preview_nav_menus_args']['args_hmac'] ), $result );
    885893        $this->assertContains( ' data-customize-partial-type="nav_menu_instance"', $result );
    886         $this->assertContains( ' data-customize-partial-placement-context="', $result );
     894        $this->assertTrue( (bool) preg_match( '/data-customize-partial-placement-context="(.+?)"/', $result, $matches ) );
     895        $context = json_decode( html_entity_decode( $matches[1] ), true );
     896        $this->assertEquals( $original_args, wp_array_slice_assoc( $context, array_keys( $original_args ) ) ); // Because assertArraySubset is not available in PHP 5.2.
     897        $this->assertTrue( $context['can_partial_refresh'] );
    887898    }
    888899
Note: See TracChangeset for help on using the changeset viewer.