Make WordPress Core


Ignore:
Timestamp:
03/09/2016 12:08:51 AM (9 years ago)
Author:
westonruter
Message:

Customize: Fix regressions and harden implementation of selective refresh for nav menus.

  • Request full refresh if there are nav menu instances that lack partials for a changed setting.
  • Restore WP_Customize_Nav_Menus::$preview_nav_menu_instance_args and WP_Customize_Nav_Menus::export_preview_data() from 4.3, and keeping a tally of all wp_nav_menu() calls regardless of whether they can use selective refresh.
  • Ensure that all instances of wp_nav_menu() are tallied, regardless of whether they are made during the initial preview call or during subsequent partial renderings. Export nav_menu_instance_args with each partial rendering response just as they are returned when rendering the preview as a whole.
  • Fix issues with Custom Menu widget where nav menu items would fail to render when switching between menus when a menu lacked items to begin with.
  • Make sure the fallback behavior is invoked when the partial is no longer associated with a menu.
  • Do fallback behavior to refresh preview when all menu items are removed from a menu.

Follows [36586].
See #27355.
Fixes #35362.

File:
1 edited

Legend:

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

    r36836 r36889  
    823823
    824824    /**
     825     * Nav menu args used for each instance, keyed by the args HMAC.
     826     *
     827     * @since 4.3.0
     828     * @access public
     829     * @var array
     830     */
     831    public $preview_nav_menu_instance_args = array();
     832
     833    /**
    825834     * Filter arguments for dynamic nav_menu selective refresh partials.
    826835     *
     
    863872        add_filter( 'wp_nav_menu_args', array( $this, 'filter_wp_nav_menu_args' ), 1000 );
    864873        add_filter( 'wp_nav_menu', array( $this, 'filter_wp_nav_menu' ), 10, 2 );
     874        add_filter( 'wp_footer', array( $this, 'export_preview_data' ), 1 );
     875        add_filter( 'customize_render_partials_response', array( $this, 'export_partial_rendered_nav_menu_instances' ) );
    865876    }
    866877
     
    882893         * selective refreshed if...
    883894         */
    884         $can_selective_refresh = (
     895        $can_partial_refresh = (
    885896            // ...if wp_nav_menu() is directly echoing out the menu (and thus isn't manipulating the string after generated),
    886897            ! empty( $args['echo'] )
     
    905916            )
    906917        );
    907 
    908         if ( ! $can_selective_refresh ) {
    909             return $args;
    910         }
     918        $args['can_partial_refresh'] = $can_partial_refresh;
    911919
    912920        $exported_args = $args;
     921
     922        // Empty out args which may not be JSON-serializable.
     923        if ( ! $can_partial_refresh ) {
     924            $exported_args['fallback_cb'] = '';
     925            $exported_args['walker'] = '';
     926        }
    913927
    914928        /*
     
    924938
    925939        $args['customize_preview_nav_menus_args'] = $exported_args;
    926 
     940        $this->preview_nav_menu_instance_args[ $exported_args['args_hmac'] ] = $exported_args;
    927941        return $args;
    928942    }
     
    943957     */
    944958    public function filter_wp_nav_menu( $nav_menu_content, $args ) {
    945         if ( ! empty( $args->customize_preview_nav_menus_args ) ) {
     959        if ( isset( $args->customize_preview_nav_menus_args['can_partial_refresh'] ) && $args->customize_preview_nav_menus_args['can_partial_refresh'] ) {
    946960            $attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'nav_menu_instance[' . $args->customize_preview_nav_menus_args['args_hmac'] . ']' ) );
    947961            $attributes .= ' data-customize-partial-type="nav_menu_instance"';
     
    9881002     *
    9891003     * @since 4.3.0
    990      * @deprecated 4.5.0 Obsolete
    9911004     * @access public
    9921005     */
    9931006    public function export_preview_data() {
    994         _deprecated_function( __METHOD__, '4.5.0' );
     1007
     1008        // Why not wp_localize_script? Because we're not localizing, and it forces values into strings.
     1009        $exports = array(
     1010            'navMenuInstanceArgs' => $this->preview_nav_menu_instance_args,
     1011        );
     1012        printf( '<script>var _wpCustomizePreviewNavMenusExports = %s;</script>', wp_json_encode( $exports ) );
     1013    }
     1014
     1015    /**
     1016     * Export any wp_nav_menu() calls during the rendering of any partials.
     1017     *
     1018     * @since 4.5.0
     1019     * @access public
     1020     *
     1021     * @param array $response Response.
     1022     * @return array Response.
     1023     */
     1024    public function export_partial_rendered_nav_menu_instances( $response ) {
     1025        $response['nav_menu_instance_args'] = $this->preview_nav_menu_instance_args;
     1026        return $response;
    9951027    }
    9961028
Note: See TracChangeset for help on using the changeset viewer.