Make WordPress Core

Ticket #32814: 32814.4.diff

File 32814.4.diff, 7.7 KB (added by adamsilverstein, 10 years ago)
  • wp-admin/js/customize-nav-menus.js

     
    11111111                                                control.getMenuControl().debouncedReflowMenuItems();
    11121112                                        }
    11131113                                }
     1114                                /**
     1115                                 * After updating, refresh any custom menu widgets containing this menu.
     1116                                 */
     1117                                control.getMenuControl()._resetCustomMenuWidgets( control.getMenuControl() );
     1118
    11141119                        });
    11151120                },
    11161121
     
    16511656                                                select.append( new Option( name, menuId ) );
    16521657                                        }
    16531658                                } );
    1654                                 $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first' ).append( new Option( name, menuId ) );
    16551659                        }
    16561660                },
    16571661
     
    16951699                                                var select = widgetControl.container.find( 'select' );
    16961700                                                select.find( 'option[value=' + String( menuId ) + ']' ).text( name );
    16971701                                        });
    1698                                         $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).text( name );
    16991702                                }
    17001703                        } );
    17011704
     
    17071710                },
    17081711
    17091712                /**
     1713                 * Helper function to keep custom menu widgets in sync with menu changes.
     1714                 *
     1715                 * Whenever a menu is changed, ensure any custom menu widgets also get refreshed.
     1716                 */
     1717                _resetCustomMenuWidgets: function( control ) {
     1718                        api.control.each( function( widgetControl ) {
     1719                                if ( ! widgetControl.extended( api.controlConstructor.widget_form ) || 'nav_menu' !== widgetControl.params.widget_id_base ) {
     1720                                        return;
     1721                                }
     1722                                var 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                                });
     1791                                /**
     1792                                 * After sorting, refresh any custom menu widgets containing this menu.
     1793                                 */
     1794                                control._resetCustomMenuWidgets( control );
     1795
    17701796                        });
    1771 
    17721797                        control.isReordering = false;
    17731798
    17741799                        /**
     
    18351860                                }
    18361861                                select.find( 'option[value=' + String( menuId ) + ']' ).remove();
    18371862                        });
    1838                         $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).remove();
     1863
     1864                        /**
     1865                         * After deleting, refresh any custom menu widgets containing this menu.
     1866                         */
     1867                        control._resetCustomMenuWidgets( control );
    18391868                },
    18401869
    18411870                // Setup theme location checkboxes.
     
    22322261
    22332262                        // Focus on the new menu section.
    22342263                        api.section( customizeId ).focus(); // @todo should we focus on the new menu's control and open the add-items panel? Thinking user flow...
     2264                        // Fix an issue with extra space at top immediately after creating new menu.
     2265                        $( "#menu-to-edit" ).css( 'margin-top', 0 );
    22352266                }
    22362267        });
    22372268
     
    23592390                                oldSetting.preview();
    23602391                                newSetting.preview();
    23612392
     2393                                /**
     2394                                 * Update the saved menu in any custom menu widgets.
     2395                                 * If the previous_term_id item is selected, reselect the
     2396                                 * item with the updated term_id.
     2397                                 */
     2398                                api.control.each( function( setting ) {
     2399                                        /**
     2400                                         * Only act on nav_menu widgets
     2401                                         */
     2402                                        if ( ! setting.extended( api.controlConstructor.widget_form ) ||
     2403                                                'nav_menu' !== setting.params.widget_id_base ) {
     2404                                                return;
     2405                                        }
     2406                                        var select        = setting.container.find( 'select' );
     2407                                        var oldMenuOption = select.find( 'option[value=' + String( update.
     2408                                                previous_term_id ) + ']' );
     2409
     2410                                        /**
     2411                                         * Adjust menu options matching the old ID, setting them to the new ID.
     2412                                         */
     2413                                        if ( oldMenuOption.length !== 0 ) {
     2414                                                oldMenuOption.attr( 'value', update.term_id );
     2415                                        }
     2416                                } );
     2417
    23622418                                // Update nav_menu_locations to reference the new ID.
    23632419                                api.each( function( setting ) {
    23642420                                        var wasSaved = api.state( 'saved' ).get();
  • wp-admin/js/customize-widgets.js

     
    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 );
  • wp-includes/class-wp-customize-nav-menus.php

     
    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
    6264        }
    6365
    6466        /**
     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                /**
     73                 * Customized menus always have a negative ID.
     74                 */
     75                if ( is_int( $menu ) && $menu < 0 ) {
     76                        /**
     77                         * Check to see if there is a customized menu item matching this id.
     78                         */
     79                        if ( isset( $_POST['customized'] ) && current_user_can( 'customize' ) ) {
     80                                $customized = json_decode( wp_unslash( $_POST['customized'] ), true );
     81                                if ( ! empty( $customized ) ) {
     82                                        $item_key = 'nav_menu_item[' . $menu . ']';
     83                                        if ( isset( $customized[ $item_key ] ) ) {
     84                                                $menu_item = $customized[ $item_key ];
     85                                        }
     86
     87                                        if ( isset( $menu_item ) ) {
     88
     89                                                /**
     90                                                 * Initialize the $menu_obj, required for PHP 5.2 compatibility.
     91                                                 */
     92                                                $menu_obj = new stdClass();
     93
     94                                                /**
     95                                                 * Build menu object from the customized data.
     96                                                 */
     97                                                $menu_obj->term_id          = $menu;
     98                                                $menu_obj->name             = isset( $menu_item['name'] ) ? $menu_item['name'] : '';
     99                                                $menu_obj->slug             = sanitize_title( $menu_obj->name );
     100                                                $menu_obj->term_group       = 0;
     101                                                $menu_obj->term_taxonomy_id = $menu;
     102                                                $menu_obj->taxonomy         = 'nav_menu';
     103                                                $menu_obj->parent           = isset( $menu_item['parent'] ) ? $menu_item['parent'] : '';
     104                                                $menu_obj->count            = isset( $menu_item['count'] ) ? $menu_item['count'] : '';
     105                                                $menu_obj->description      = $menu_item['description'];
     106                                                $menu_obj->filter           = isset( $menu_item['filter'] ) ? $menu_item['filter'] : '';
     107                                                $menu_obj->ID               = $menu;
     108                                        } else {
     109                                                $menu_obj = false;
     110                                        }
     111                                }
     112                        }
     113                }
     114                return $menu_obj;
     115        }
     116
     117        /**
    65118         * Ajax handler for loading available menu items.
    66119         *
    67120         * @since 4.3.0
  • wp-includes/nav-menu.php

     
    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 ) {