Make WordPress Core


Ignore:
Timestamp:
07/29/2015 04:02:08 PM (9 years ago)
Author:
westonruter
Message:

Customizer: Ensure that all existing menus are shown in the Custom Menu widget's dropdown.

  • Ensure that a Custom Menu widget selecting a newly-inserted menu gets updated to use the new menu ID upon Save & Publish.
  • Dynamically update the visibility of the Custom Menu widget's "no menus" message when the number of menus changes between 0 and 1+.
  • Send all dirty Customized settings in update-widget Ajax request and preview() them so that the widget update/form callbacks have access to any data dependencies in the current Customizer session (such as newly created unsaved menus).
  • Update link in Custom Menu widget to point to Menus panel as opposed to Menus admin page, when in the Customizer.
  • Fix an issue with extra space at top immediately after creating new menu.
  • Fix doubled update-widget Ajax requests when changing select dropdown; prevent initial from being aborted.
  • Add missing wp_get_nav_menus() hooks to preview Customizer updates/inserts for nav_menu settings; includes tests.
  • Update wp_get_nav_menu_object() to allow a menu object to be passed in (and thus passed through).

Props westonruter, adamsilverstein.
Fixes #32814.

File:
1 edited

Legend:

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

    r33427 r33488  
    16261626        $this->_previewed_blog_id = get_current_blog_id();
    16271627
     1628        add_filter( 'wp_get_nav_menus', array( $this, 'filter_wp_get_nav_menus' ), 10, 2 );
    16281629        add_filter( 'wp_get_nav_menu_object', array( $this, 'filter_wp_get_nav_menu_object' ), 10, 2 );
    16291630        add_filter( 'default_option_nav_menu_options', array( $this, 'filter_nav_menu_options' ) );
    16301631        add_filter( 'option_nav_menu_options', array( $this, 'filter_nav_menu_options' ) );
     1632    }
     1633
     1634    /**
     1635     * Filter the wp_get_nav_menus() result to ensure the inserted menu object is included, and the deleted one is removed.
     1636     *
     1637     * @since 4.3.0
     1638     * @access public
     1639     *
     1640     * @see wp_get_nav_menus()
     1641     *
     1642     * @param array $menus An array of menu objects.
     1643     * @param array $args  An array of arguments used to retrieve menu objects.
     1644     * @return array
     1645     */
     1646    public function filter_wp_get_nav_menus( $menus, $args ) {
     1647        if ( get_current_blog_id() !== $this->_previewed_blog_id ) {
     1648            return $menus;
     1649        }
     1650
     1651        $setting_value = $this->value();
     1652        $is_delete = ( false === $setting_value );
     1653        $index = -1;
     1654
     1655        // Find the existing menu item's position in the list.
     1656        foreach ( $menus as $i => $menu ) {
     1657            if ( (int) $this->term_id === (int) $menu->term_id || (int) $this->previous_term_id === (int) $menu->term_id ) {
     1658                $index = $i;
     1659                break;
     1660            }
     1661        }
     1662
     1663        if ( $is_delete ) {
     1664            // Handle deleted menu by removing it from the list.
     1665            if ( -1 !== $index ) {
     1666                array_splice( $menus, $index, 1 );
     1667            }
     1668        } else {
     1669            // Handle menus being updated or inserted.
     1670            $menu_obj = (object) array_merge( array(
     1671                'term_id'          => $this->term_id,
     1672                'term_taxonomy_id' => $this->term_id,
     1673                'slug'             => sanitize_title( $setting_value['name'] ),
     1674                'count'            => 0,
     1675                'term_group'       => 0,
     1676                'taxonomy'         => self::TAXONOMY,
     1677                'filter'           => 'raw',
     1678            ), $setting_value );
     1679
     1680            array_splice( $menus, $index, ( -1 === $index ? 0 : 1 ), array( $menu_obj ) );
     1681        }
     1682
     1683        // Make sure the menu objects get re-sorted after an update/insert.
     1684        if ( ! $is_delete && ! empty( $args['orderby'] ) ) {
     1685            $this->_current_menus_sort_orderby = $args['orderby'];
     1686            usort( $menus, array( $this, '_sort_menus_by_orderby' ) );
     1687        }
     1688        // @todo add support for $args['hide_empty'] === true
     1689
     1690        return $menus;
     1691    }
     1692
     1693    /**
     1694     * Temporary non-closure passing of orderby value to function.
     1695     *
     1696     * @since 4.3.0
     1697     * @access protected
     1698     * @var string
     1699     *
     1700     * @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
     1701     * @see WP_Customize_Nav_Menu_Setting::_sort_menus_by_orderby()
     1702     */
     1703    protected $_current_menus_sort_orderby;
     1704
     1705    /**
     1706     * Sort menu objects by the class-supplied orderby property.
     1707     *
     1708     * This is a workaround for a lack of closures.
     1709     *
     1710     * @since 4.3.0
     1711     * @access protected
     1712     * @param object $menu1
     1713     * @param object $menu2
     1714     * @return int
     1715     *
     1716     * @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
     1717     */
     1718    protected function _sort_menus_by_orderby( $menu1, $menu2 ) {
     1719        $key = $this->_current_menus_sort_orderby;
     1720        return strcmp( $menu1->$key, $menu2->$key );
    16311721    }
    16321722
     
    17531843
    17541844    /**
     1845     * Storage for data to be sent back to client in customize_save_response filter.
     1846     *
     1847     * @access protected
     1848     * @since 4.3.0
     1849     * @var array
     1850     *
     1851     * @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
     1852     */
     1853    protected $_widget_nav_menu_updates = array();
     1854
     1855    /**
    17551856     * Create/update the nav_menu term for this setting.
    17561857     *
     
    17621863     *
    17631864     * @since 4.3.0
    1764      * @access public
     1865     * @access protected
    17651866     *
    17661867     * @see wp_update_nav_menu_object()
     
    18451946        }
    18461947
    1847         // Make sure that new menus assigned to nav menu locations use their new IDs.
    18481948        if ( 'inserted' === $this->update_status ) {
     1949            // Make sure that new menus assigned to nav menu locations use their new IDs.
    18491950            foreach ( $this->manager->settings() as $setting ) {
    18501951                if ( ! preg_match( '/^nav_menu_locations\[/', $setting->id ) ) {
     
    18581959                }
    18591960            }
     1961
     1962            // Make sure that any nav_menu widgets referencing the placeholder nav menu get updated and sent back to client.
     1963            foreach ( array_keys( $this->manager->unsanitized_post_values() ) as $setting_id ) {
     1964                $nav_menu_widget_setting = $this->manager->get_setting( $setting_id );
     1965                if ( ! $nav_menu_widget_setting || ! preg_match( '/^widget_nav_menu\[/', $nav_menu_widget_setting->id ) ) {
     1966                    continue;
     1967                }
     1968
     1969                $widget_instance = $nav_menu_widget_setting->post_value(); // Note that this calls WP_Customize_Widgets::sanitize_widget_instance().
     1970                if ( empty( $widget_instance['nav_menu'] ) || intval( $widget_instance['nav_menu'] ) !== $this->previous_term_id ) {
     1971                    continue;
     1972                }
     1973
     1974                $widget_instance['nav_menu'] = $this->term_id;
     1975                $updated_widget_instance = $this->manager->widgets->sanitize_widget_js_instance( $widget_instance );
     1976                $this->manager->set_post_value( $nav_menu_widget_setting->id, $updated_widget_instance );
     1977                $nav_menu_widget_setting->save();
     1978
     1979                $this->_widget_nav_menu_updates[ $nav_menu_widget_setting->id ] = $updated_widget_instance;
     1980            }
    18601981        }
    18611982    }
     
    18651986     *
    18661987     * @since 4.3.0
    1867      * @access public
     1988     * @access protected
    18681989     *
    18691990     * @see WP_Customize_Nav_Menu_Setting::filter_nav_menu_options()
     
    19052026        if ( ! isset( $data['nav_menu_updates'] ) ) {
    19062027            $data['nav_menu_updates'] = array();
     2028        }
     2029        if ( ! isset( $data['widget_nav_menu_updates'] ) ) {
     2030            $data['widget_nav_menu_updates'] = array();
    19072031        }
    19082032
     
    19152039        );
    19162040
     2041        $data['widget_nav_menu_updates'] = array_merge(
     2042            $data['widget_nav_menu_updates'],
     2043            $this->_widget_nav_menu_updates
     2044        );
     2045        $this->_widget_nav_menu_updates = array();
     2046
    19172047        return $data;
    19182048    }
Note: See TracChangeset for help on using the changeset viewer.