Make WordPress Core

Ticket #32814: 32814.6.diff

File 32814.6.diff, 7.7 KB (added by westonruter, 10 years ago)

Additional change: https://github.com/xwp/wordpress-develop/commit/60674ed62676c7748b2b98306235deb6c669f8a5 PR: https://github.com/xwp/wordpress-develop/pull/98

  • src/wp-admin/js/customize-nav-menus.js

    diff --git src/wp-admin/js/customize-nav-menus.js src/wp-admin/js/customize-nav-menus.js
    index e745561..b1cd591 100644
     
    11111111                                                control.getMenuControl().debouncedReflowMenuItems();
    11121112                                        }
    11131113                                }
     1114
     1115                                // After updating, refresh any custom menu widgets containing this menu.
     1116                                control.getMenuControl()._resetCustomMenuWidgets( control.getMenuControl() );
     1117
    11141118                        });
    11151119                },
    11161120
     
    16511655                                                select.append( new Option( name, menuId ) );
    16521656                                        }
    16531657                                } );
    1654                                 $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first' ).append( new Option( name, menuId ) );
    16551658                        }
    16561659                },
    16571660
     
    16951698                                                var select = widgetControl.container.find( 'select' );
    16961699                                                select.find( 'option[value=' + String( menuId ) + ']' ).text( name );
    16971700                                        });
    1698                                         $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).text( name );
    16991701                                }
    17001702                        } );
    17011703
     
    17071709                },
    17081710
    17091711                /**
     1712                 * Helper function to keep custom menu widgets in sync with menu changes.
     1713                 *
     1714                 * Whenever a menu is changed, ensure any custom menu widgets also get refreshed.
     1715                 */
     1716                _resetCustomMenuWidgets: function( control ) {
     1717                        api.control.each( function( widgetControl ) {
     1718                                if ( ! widgetControl.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== widgetControl.params.widget_id_base ) {
     1719                                        return;
     1720                                }
     1721                                var select, option;
     1722                                select = widgetControl.container.find( 'select' );
     1723                                option = select.find( 'option[value=' + control.id.match(/(-?\d+)/)[0] + ']' );
     1724                                if ( option.length > 0 ) {
     1725                                        widgetControl.previewer.trigger( 'refresh' );
     1726                                }
     1727                        });
     1728                },
     1729
     1730                /**
    17101731                 * Allow items in each menu to be re-ordered, and for the order to be previewed.
    17111732                 *
    17121733                 * Notice that the UI aspects here are handled by wpNavMenu.initSortables()
     
    17671788                                                menuItemControl.setting.set( setting );
    17681789                                        });
    17691790                                });
    1770                         });
    17711791
     1792                                // After sorting, refresh any custom menu widgets containing this menu.
     1793                                control._resetCustomMenuWidgets( control );
     1794
     1795                        });
    17721796                        control.isReordering = false;
    17731797
    17741798                        /**
     
    18351859                                }
    18361860                                select.find( 'option[value=' + String( menuId ) + ']' ).remove();
    18371861                        });
    1838                         $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).remove();
     1862
     1863                         // After deleting, refresh any custom menu widgets containing this menu.
     1864                        control._resetCustomMenuWidgets( control );
    18391865                },
    18401866
    18411867                // Setup theme location checkboxes.
     
    22322258
    22332259                        // Focus on the new menu section.
    22342260                        api.section( customizeId ).focus(); // @todo should we focus on the new menu's control and open the add-items panel? Thinking user flow...
     2261
     2262                        // Fix an issue with extra space at top immediately after creating new menu.
     2263                        $( '#menu-to-edit' ).css( 'margin-top', 0 );
    22352264                }
    22362265        });
    22372266
     
    23702399                                        }
    23712400                                } );
    23722401
     2402                                /**
     2403                                 * Update the saved menu in any custom menu widgets.
     2404                                 * If the previous_term_id item is selected, reselect the
     2405                                 * item with the updated term_id.
     2406                                 */
     2407                                api.control.each( function( setting ) {
     2408                                        // Only act on nav_menu widgets.
     2409                                        if ( ! setting.extended( api.controlConstructor.widget_form ) ||
     2410                                                'nav_menu' !== setting.params.widget_id_base ) {
     2411                                                return;
     2412                                        }
     2413                                        var select, oldMenuOption, oldMenuSelected, newMenuOption;
     2414                                        select          = setting.container.find( 'select' );
     2415                                        oldMenuOption   = select.find( 'option[value=' + String( update.previous_term_id ) + ']' );
     2416                                        oldMenuSelected = select.find( 'option[value=' + String( update.previous_term_id ) + ']:selected' );
     2417                                        newMenuOption   = select.find( 'option[value=' + String( update.term_id ) + ']' );
     2418
     2419                                        // Adjust menu options matching the old ID, setting them to the new ID.
     2420                                        if ( oldMenuSelected.length !== 0 ) {
     2421                                                newMenuOption.attr( 'selected', true );
     2422                                        }
     2423
     2424                                        // Remove the old option.
     2425                                        oldMenuOption.remove();
     2426                                } );
     2427
    23732428                                if ( oldSection.expanded.get() ) {
    23742429                                        // @todo This doesn't seem to be working.
    23752430                                        newSection.expand();
  • src/wp-admin/js/customize-widgets.js

    diff --git src/wp-admin/js/customize-widgets.js src/wp-admin/js/customize-widgets.js
    index 2907b4f..f56a048 100644
     
    10411041                        params.wp_customize = 'on';
    10421042                        params.nonce = api.Widgets.data.nonce;
    10431043                        params.theme = api.settings.theme.stylesheet;
     1044                        params.customized = wp.customize.previewer.query().customized;
    10441045
    10451046                        data = $.param( params );
    10461047                        $inputs = this._getInputs( $widgetContent );
  • src/wp-includes/class-wp-customize-nav-menus.php

    diff --git src/wp-includes/class-wp-customize-nav-menus.php src/wp-includes/class-wp-customize-nav-menus.php
    index e7b7e0f..46a3e2f 100644
    final class WP_Customize_Nav_Menus { 
    5959                add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_templates' ) );
    6060                add_action( 'customize_controls_print_footer_scripts', array( $this, 'available_items_template' ) );
    6161                add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) );
     62                add_filter( 'wp_get_nav_menu_object', array( $this, 'filter_nav_menu_object' ), 10, 2 );
     63
     64        }
     65
     66        /**
     67         * Filter the nav menu object to inject `customized` menus.
     68         *
     69         * @since 4.3.0
     70         */
     71        public function filter_nav_menu_object( $menu_obj, $menu ) {
     72                // Newly-created menus always have a negative ID.
     73                if ( is_int( $menu ) && $menu < 0 ) {
     74                        // Check to see if there is a customized menu item matching this id.
     75                        if ( isset( $_POST['customized'] ) && current_user_can( 'customize' ) ) {
     76                                $customized = json_decode( wp_unslash( $_POST['customized'] ), true );
     77                                if ( ! empty( $customized ) ) {
     78                                        $item_key = 'nav_menu_item[' . $menu . ']';
     79                                        if ( isset( $customized[ $item_key ] ) ) {
     80                                                $menu_item = $customized[ $item_key ];
     81                                        }
     82
     83                                        if ( isset( $menu_item ) ) {
     84
     85                                                // Initialize the $menu_obj, required for PHP 5.2 compatibility.
     86                                                $menu_obj = new stdClass();
     87
     88                                                // Build menu object from the customized data.
     89                                                $menu_obj->term_id          = $menu;
     90                                                $menu_obj->name             = isset( $menu_item['name'] ) ? $menu_item['name'] : '';
     91                                                $menu_obj->slug             = sanitize_title( $menu_obj->name );
     92                                                $menu_obj->term_group       = 0;
     93                                                $menu_obj->term_taxonomy_id = $menu;
     94                                                $menu_obj->taxonomy         = 'nav_menu';
     95                                                $menu_obj->parent           = isset( $menu_item['parent'] ) ? $menu_item['parent'] : '';
     96                                                $menu_obj->count            = isset( $menu_item['count'] ) ? $menu_item['count'] : '';
     97                                                $menu_obj->description      = isset( $menu_item['description'] ) ? $menu_item['description'] : '';
     98                                                $menu_obj->filter           = isset( $menu_item['filter'] ) ? $menu_item['filter'] : '';
     99                                                $menu_obj->ID               = $menu;
     100                                        } else {
     101                                                $menu_obj = false;
     102                                        }
     103                                }
     104                        }
     105                }
     106                return $menu_obj;
    62107        }
    63108
    64109        /**
  • src/wp-includes/nav-menu.php

    diff --git src/wp-includes/nav-menu.php src/wp-includes/nav-menu.php
    index d255859..fc681b8 100644
     
    1212 *
    1313 * @since 3.0.0
    1414 *
    15  * @param string $menu Menu ID, slug, or name.
     15 * @param string $menu Menu ID, slug, or name - or the menu object.
    1616 * @return object|false False if $menu param isn't supplied or term does not exist, menu object if successful.
    1717 */
    1818function wp_get_nav_menu_object( $menu ) {
    1919        $menu_obj = false;
    20         if ( $menu ) {
     20
     21        if ( is_object( $menu ) ) {
     22                $menu_obj = $menu;
     23        }
     24
     25        if ( $menu && ! $menu_obj ) {
    2126                $menu_obj = get_term( $menu, 'nav_menu' );
    2227
    2328                if ( ! $menu_obj ) {