Make WordPress Core

Ticket #32814: 32814.2.diff

File 32814.2.diff, 6.8 KB (added by adamsilverstein, 10 years ago)

continue custom menu widget integration

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

     
    16511651                                                select.append( new Option( name, menuId ) );
    16521652                                        }
    16531653                                } );
    1654                                 $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first' ).append( new Option( name, menuId ) );
    16551654                        }
    16561655                },
    16571656
     
    16951694                                                var select = widgetControl.container.find( 'select' );
    16961695                                                select.find( 'option[value=' + String( menuId ) + ']' ).text( name );
    16971696                                        });
    1698                                         $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).text( name );
    16991697                                }
    17001698                        } );
    17011699
     
    17071705                },
    17081706
    17091707                /**
     1708                 * Whenever a menu is changed, ensure any custom menu widgets also get refreshed.
     1709                 */
     1710                _resetCustomMenuWidgets: function() {
     1711                        api.each( function( setting ) {
     1712                                        setting.preview();
     1713                        } );
     1714                },
     1715
     1716                /**
    17101717                 * Allow items in each menu to be re-ordered, and for the order to be previewed.
    17111718                 *
    17121719                 * Notice that the UI aspects here are handled by wpNavMenu.initSortables()
     
    17671774                                                menuItemControl.setting.set( setting );
    17681775                                        });
    17691776                                });
     1777                                control._resetCustomMenuWidgets();
    17701778                        });
    1771 
    17721779                        control.isReordering = false;
    17731780
    17741781                        /**
     
    18351842                                }
    18361843                                select.find( 'option[value=' + String( menuId ) + ']' ).remove();
    18371844                        });
    1838                         $( '#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value=' + String( menuId ) + ']' ).remove();
     1845                        control._resetCustomMenuWidgets();
    18391846                },
    18401847
    18411848                // Setup theme location checkboxes.
     
    22322239
    22332240                        // Focus on the new menu section.
    22342241                        api.section( customizeId ).focus(); // @todo should we focus on the new menu's control and open the add-items panel? Thinking user flow...
     2242                        // Fix an issue with extra space at top immediately after creating new menu.
     2243                        $( "#menu-to-edit" ).css( 'margin-top', 0 );
    22352244                }
    22362245        });
    22372246
     
    23592368                                oldSetting.preview();
    23602369                                newSetting.preview();
    23612370
     2371                                /**
     2372                                 * Update the saved menu in any custom menu widgets.
     2373                                 * If the previous_term_id item is selected, reselect the
     2374                                 * item with the updated term_id.
     2375                                 */
     2376                                api.control.each( function( setting ) {
     2377                                        /**
     2378                                         * Only act on nav_menu widgets
     2379                                         */
     2380                                        if ( ! setting.extended( api.controlConstructor.widget_form ) ||
     2381                                                'nav_menu' !== setting.params.widget_id_base ) {
     2382                                                return;
     2383                                        }
     2384                                        var select        = setting.container.find( 'select' );
     2385                                        var oldMenuOption = select.find( 'option[value=' + String( update.
     2386                                                previous_term_id ) + ']' );
     2387
     2388                                        /**
     2389                                         * Adjust menu options matching the old ID, setting them to the new ID.
     2390                                         */
     2391                                        if ( oldMenuOption.length !== 0 ) {
     2392                                                console.log( oldMenuOption );
     2393                                                oldMenuOption.attr( 'value', update.term_id );
     2394                                        }
     2395                                } );
     2396
    23622397                                // Update nav_menu_locations to reference the new ID.
    23632398                                api.each( function( setting ) {
    23642399                                        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                                        $menu_key = 'nav_menu[' . $menu . ']';
     83                                        $item_key = 'nav_menu_item[' . $menu . ']';
     84                                        if ( isset( $customized[ $menu_key ] ) ) {
     85                                                $menu_item = $customized[ $menu_key ];
     86                                        } else if ( isset( $customized[ $item_key ] ) ) {
     87                                                $menu_item = $customized[ $item_key ];
     88                                        }
     89
     90                                        if ( isset( $menu_item ) ) {
     91
     92                                                /**
     93                                                 * Initialize the $menu_obj, required for PHP 5.2 compatibility.
     94                                                 */
     95                                                $menu_obj = new stdClass();
     96
     97                                                /**
     98                                                 * Build menu object from the customized data.
     99                                                 */
     100                                                $menu_obj->name             = $menu_item['name'];
     101                                                $menu_obj->parent           = $menu_item['parent'];
     102                                                $menu_obj->term_id          = (int) $menu;
     103                                                $menu_obj->term_taxonomy_id = (int) $menu;
     104                                                $menu_obj->slug             = sanitize_title( $menu_obj->name );
     105                                                $menu_obj->taxonomy         = 'nav_menu';
     106                                                $menu_obj->count            = isset( $menu_item['count'] ) ? $menu_item['count'] : '';
     107                                                $menu_obj->description      = $menu_item['description'];
     108                                                $menu_obj->filter           = isset( $menu_item['raw'] ) ? $menu_item['raw'] : '';
     109                                                $menu_obj->ID               = (int) $menu;
     110                                        } else {
     111                                                $menu_obj = false;
     112                                        }
     113                                }
     114                        }
     115                }
     116                return $menu_obj;
     117        }
     118
     119
     120        /**
    65121         * Ajax handler for loading available menu items.
    66122         *
    67123         * @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 ) {