Make WordPress Core

Ticket #32708: 32708.5.diff

File 32708.5.diff, 27.1 KB (added by valendesigns, 10 years ago)
  • 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 58c27a8..7b8fbf5 100644
     
    1818        // Link settings.
    1919        api.Menus.data = {
    2020                nonce: '',
    21                 itemTypes: {
    22                         taxonomies: {},
    23                         postTypes: {}
    24                 },
     21                itemTypes: [],
    2522                l10n: {},
    2623                menuItemTransport: 'postMessage',
    2724                phpIntMax: 0,
     
    280277                        var self = this;
    281278
    282279                        // Render the template for each item by type.
    283                         _.each( api.Menus.data.itemTypes, function( typeObjects, type ) {
    284                                 _.each( typeObjects, function( typeObject, slug ) {
    285                                         if ( 'postTypes' === type ) {
    286                                                 type = 'post_type';
    287                                         } else if ( 'taxonomies' === type ) {
    288                                                 type = 'taxonomy';
    289                                         }
    290                                         self.pages[ slug ] = 0; // @todo should prefix with type
    291                                         self.loadItems( slug, type );
    292                                 } );
     280                        _.each( api.Menus.data.itemTypes, function( itemType ) {
     281                                self.pages[ itemType.type + ':' + itemType.object ] = 0;
     282                                self.loadItems( itemType.type, itemType.object ); // @todo we need to combine these Ajax requests.
    293283                        } );
    294284                },
    295285
    296286                // Load available menu items.
    297                 loadItems: function( type, obj_type ) {
    298                         var self = this, params, request, itemTemplate;
     287                loadItems: function( type, object ) {
     288                        var self = this, params, request, itemTemplate, availableMenuItemContainer;
    299289                        itemTemplate = wp.template( 'available-menu-item' );
    300290
    301                         if ( 0 > self.pages[ type ] ) {
     291                        if ( -1 === self.pages[ type + ':' + object ] ) {
    302292                                return;
    303293                        }
    304                         $( '#available-menu-items-' + type + ' .accordion-section-title' ).addClass( 'loading' );
     294                        availableMenuItemContainer = $( '#available-menu-items-' + type + '-' + object );
     295                        availableMenuItemContainer.find( '.accordion-section-title' ).addClass( 'loading' );
    305296                        self.loading = true;
    306297                        params = {
    307298                                'customize-menus-nonce': api.Menus.data.nonce,
    308299                                'wp_customize': 'on',
    309300                                'type': type,
    310                                 'obj_type': obj_type,
    311                                 'page': self.pages[ type ]
     301                                'object': object,
     302                                'page': self.pages[ type + ':' + object ]
    312303                        };
    313304                        request = wp.ajax.post( 'load-available-menu-items-customizer', params );
    314305
     
    316307                                var items, typeInner;
    317308                                items = data.items;
    318309                                if ( 0 === items.length ) {
    319                                         if ( 0 === self.pages[ type ] ) {
    320                                                 $( '#available-menu-items-' + type )
     310                                        if ( 0 === self.pages[ type + ':' + object ] ) {
     311                                                availableMenuItemContainer
    321312                                                        .addClass( 'cannot-expand' )
    322313                                                        .removeClass( 'loading' )
    323314                                                        .find( '.accordion-section-title > button' )
    324315                                                        .prop( 'tabIndex', -1 );
    325316                                        }
    326                                         self.pages[ type ] = -1;
     317                                        self.pages[ type + ':' + object ] = -1;
    327318                                        return;
    328319                                }
    329320                                items = new api.Menus.AvailableItemCollection( items ); // @todo Why is this collection created and then thrown away?
    330321                                self.collection.add( items.models );
    331                                 typeInner = $( '#available-menu-items-' + type + ' .accordion-section-content' );
    332                                 items.each(function( menu_item ) {
    333                                         typeInner.append( itemTemplate( menu_item.attributes ) );
     322                                typeInner = availableMenuItemContainer.find( '.accordion-section-content' );
     323                                items.each(function( menuItem ) {
     324                                        typeInner.append( itemTemplate( menuItem.attributes ) );
    334325                                });
    335                                 self.pages[ type ] = self.pages[ type ] + 1;
     326                                self.pages[ type + ':' + object ] += 1;
    336327                        });
    337328                        request.fail(function( data ) {
    338329                                if ( typeof console !== 'undefined' && console.error ) {
     
    340331                                }
    341332                        });
    342333                        request.always(function() {
    343                                 $( '#available-menu-items-' + type + ' .accordion-section-title' ).removeClass( 'loading' );
     334                                availableMenuItemContainer.find( '.accordion-section-title' ).removeClass( 'loading' );
    344335                                self.loading = false;
    345336                        });
    346337                },
     
    12751266                        }
    12761267
    12771268                        control.params.el_classes = containerClasses.join( ' ' );
    1278                         control.params.item_type_label = api.Menus.getTypeLabel( settingValue.type, settingValue.object );
     1269                        control.params.item_type_label = settingValue.type_label;
    12791270                        control.params.item_type = settingValue.type;
    12801271                        control.params.url = settingValue.url;
    12811272                        control.params.target = settingValue.target;
     
    25522543        };
    25532544
    25542545        /**
    2555          * Given a menu item type & object, get the label associated with it.
    2556          *
    2557          * @param {string} type
    2558          * @param {string} object
    2559          * @return {string}
    2560          */
    2561         api.Menus.getTypeLabel = function( type, object ) {
    2562                 var label,
    2563                         data = api.Menus.data;
    2564 
    2565                 if ( 'post_type' === type ) {
    2566                         if ( data.itemTypes.postTypes[ object ] ) {
    2567                                 label = data.itemTypes.postTypes[ object ].label;
    2568                         } else {
    2569                                 label = data.l10n.postTypeLabel;
    2570                         }
    2571                 } else if ( 'taxonomy' === type ) {
    2572                         if ( data.itemTypes.taxonomies[ object ] ) {
    2573                                 label = data.itemTypes.taxonomies[ object ].label;
    2574                         } else {
    2575                                 label = data.l10n.taxonomyTermLabel;
    2576                         }
    2577                 } else {
    2578                         label = data.l10n.custom_label;
    2579                 }
    2580 
    2581                 return label;
    2582         };
    2583 
    2584         /**
    25852546         * Given a menu item ID, get the control associated with it.
    25862547         *
    25872548         * @param {string} menuItemId
  • src/wp-includes/class-wp-customize-control.php

    diff --git src/wp-includes/class-wp-customize-control.php src/wp-includes/class-wp-customize-control.php
    index cc9469e..c67fb15 100644
    class WP_Customize_Nav_Menu_Item_Control extends WP_Customize_Control { 
    17391739                        </p>
    17401740
    17411741                        <div class="menu-item-actions description-thin submitbox">
    1742                                 <# if ( 'custom' != data.item_type && '' != data.original_title ) { #>
     1742                                <# if ( ( 'post_type' === data.item_type || 'taxonomy' === data.item_type ) && '' !== data.original_title ) { #>
    17431743                                <p class="link-to-original">
    17441744                                        <?php printf( __( 'Original: %s' ), '<a class="original-link" href="{{ data.url }}">{{ data.original_title }}</a>' ); ?>
    17451745                                </p>
  • 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 502ad2b..ce1a9be 100644
    final class WP_Customize_Nav_Menus { 
    7575                        wp_die( -1 );
    7676                }
    7777
    78                 if ( empty( $_POST['obj_type'] ) || empty( $_POST['type'] ) ) {
    79                         wp_send_json_error( 'nav_menus_missing_obj_type_or_type_parameter' );
     78                if ( empty( $_POST['type'] ) || empty( $_POST['object'] ) ) {
     79                        wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
    8080                }
    8181
    82                 $obj_type = sanitize_key( $_POST['obj_type'] );
    83                 $obj_name = sanitize_key( $_POST['type'] );
     82                $type = sanitize_key( $_POST['type'] );
     83                $object = sanitize_key( $_POST['object'] );
    8484                $page = empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] );
    85                 $items = $this->load_available_items_query( $obj_type, $obj_name, $page );
     85                $items = $this->load_available_items_query( $type, $object, $page );
    8686
    8787                if ( is_wp_error( $items ) ) {
    8888                        wp_send_json_error( $items->get_error_code() );
    final class WP_Customize_Nav_Menus { 
    9797         * @since 4.3.0
    9898         * @access public
    9999         *
    100          * @param string $obj_type Optional. Accepts any custom object type and has built-in support for
     100         * @param string $type  Optional. Accepts any custom object type and has built-in support for
    101101         *                         'post_type' and 'taxonomy'. Default is 'post_type'.
    102          * @param string $obj_name Optional. Accepts any registered taxonomy or post type name. Default is 'page'.
    103          * @param int    $page     Optional. The page number used to generate the query offset. Default is '0'.
     102         * @param string $object Optional. Accepts any registered taxonomy or post type name. Default is 'page'.
     103         * @param int    $page   Optional. The page number used to generate the query offset. Default is '0'.
    104104         * @return WP_Error|array Returns either a WP_Error object or an array of menu items.
    105105         */
    106         public function load_available_items_query( $obj_type = 'post_type', $obj_name = 'page', $page = 0 ) {
     106        public function load_available_items_query( $type = 'post_type', $object = 'page', $page = 0 ) {
    107107                $items = array();
    108108
    109                 if ( 'post_type' === $obj_type ) {
    110                         if ( ! get_post_type_object( $obj_name ) ) {
     109                if ( 'post_type' === $type ) {
     110                        if ( ! get_post_type_object( $object ) ) {
    111111                                return new WP_Error( 'nav_menus_invalid_post_type' );
    112112                        }
    113113
    114                         if ( 0 === $page && 'page' === $obj_name ) {
     114                        if ( 0 === $page && 'page' === $object ) {
    115115                                // Add "Home" link. Treat as a page, but switch to custom on add.
    116116                                $items[] = array(
    117117                                        'id'         => 'home',
    final class WP_Customize_Nav_Menus { 
    128128                                'offset'      => 10 * $page,
    129129                                'orderby'     => 'date',
    130130                                'order'       => 'DESC',
    131                                 'post_type'   => $obj_name,
     131                                'post_type'   => $object,
    132132                        ) );
    133133                        foreach ( $posts as $post ) {
    134134                                $post_title = $post->post_title;
    final class WP_Customize_Nav_Menus { 
    146146                                        'url'        => get_permalink( intval( $post->ID ) ),
    147147                                );
    148148                        }
    149                 } elseif ( 'taxonomy' === $obj_type ) {
    150                         $terms = get_terms( $obj_name, array(
     149                } elseif ( 'taxonomy' === $type ) {
     150                        $terms = get_terms( $object, array(
    151151                                'child_of'     => 0,
    152152                                'exclude'      => '',
    153153                                'hide_empty'   => false,
    final class WP_Customize_Nav_Menus { 
    176176                        }
    177177                }
    178178
     179                /**
     180                 * Filter the available menu items.
     181                 *
     182                 * @since 4.3.0
     183                 *
     184                 * @param array  $items  The array of menu items.
     185                 * @param string $type   The object type.
     186                 * @param string $object The object name.
     187                 * @param int    $page   The current page number.
     188                 */
     189                $items = apply_filters( 'customize_nav_menu_available_items', $items, $type, $object, $page );
     190
    179191                return $items;
    180192        }
    181193
    final class WP_Customize_Nav_Menus { 
    588600         *
    589601         * @since 4.3.0
    590602         * @access public
     603         *
     604         * @return array The available menu item types.
    591605         */
    592606        public function available_item_types() {
    593                 $items = array(
    594                         'postTypes'  => array(),
    595                         'taxonomies' => array(),
    596                 );
     607                $item_types = array();
    597608
    598609                $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
    599                 foreach ( $post_types as $slug => $post_type ) {
    600                         $items['postTypes'][ $slug ] = array(
    601                                 'label' => $post_type->labels->singular_name,
    602                         );
     610                if ( $post_types ) {
     611                        foreach ( $post_types as $slug => $post_type ) {
     612                                $item_types[] = array(
     613                                        'title'  => $post_type->labels->singular_name,
     614                                        'type'   => 'post_type',
     615                                        'object' => $post_type->name,
     616                                );
     617                        }
    603618                }
    604619
    605620                $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' );
    606                 foreach ( $taxonomies as $slug => $taxonomy ) {
    607                         if ( 'post_format' === $taxonomy && ! current_theme_supports( 'post-formats' ) ) {
    608                                 continue;
     621                if ( $taxonomies ) {
     622                        foreach ( $taxonomies as $slug => $taxonomy ) {
     623                                if ( 'post_format' === $taxonomy && ! current_theme_supports( 'post-formats' ) ) {
     624                                        continue;
     625                                }
     626                                $item_types[] = array(
     627                                        'title'  => $taxonomy->labels->singular_name,
     628                                        'type'   => 'taxonomy',
     629                                        'object' => $taxonomy->name,
     630                                );
    609631                        }
    610                         $items['taxonomies'][ $slug ] = array(
    611                                 'label' => $taxonomy->labels->singular_name,
    612                         );
    613632                }
    614                 return $items;
     633
     634                /**
     635                 * Filter the available menu item types.
     636                 *
     637                 * @since 4.3.0
     638                 *
     639                 * @param array $item_types Custom menu item types.
     640                 */
     641                $item_types = apply_filters( 'customize_nav_menu_available_item_types', $item_types );
     642
     643                return $item_types;
    615644        }
    616645
    617646        /**
    final class WP_Customize_Nav_Menus { 
    716745                                </div>
    717746                        </div>
    718747                        <?php
    719 
    720                         // @todo: consider using add_meta_box/do_accordion_section and making screen-optional?
    721748                        // Containers for per-post-type item browsing; items added with JS.
    722                         $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
    723                         if ( $post_types ) :
    724                                 foreach ( $post_types as $type ) :
    725                                         ?>
    726                                         <div id="available-menu-items-<?php echo esc_attr( $type->name ); ?>" class="accordion-section">
    727                                                 <h4 class="accordion-section-title"><?php echo esc_html( $type->label ); ?> <span class="spinner"></span> <span class="no-items"><?php _e( 'No items' ); ?></span> <button type="button" class="not-a-button"><span class="screen-reader-text"><?php _e( 'Toggle' ); ?></span></button></h4>
    728                                                 <ul class="accordion-section-content" data-type="<?php echo esc_attr( $type->name ); ?>" data-obj_type="post_type"></ul>
    729                                         </div>
    730                                 <?php
    731                                 endforeach;
    732                         endif;
    733 
    734                         $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
    735                         if ( $taxonomies ) :
    736                                 foreach ( $taxonomies as $tax ) :
    737                                         ?>
    738                                         <div id="available-menu-items-<?php echo esc_attr( $tax->name ); ?>" class="accordion-section">
    739                                                 <h4 class="accordion-section-title"><?php echo esc_html( $tax->label ); ?> <span class="spinner"></span> <span class="no-items"><?php _e( 'No items' ); ?></span> <button type="button" class="not-a-button"><span class="screen-reader-text"><?php _e( 'Toggle' ); ?></span></button></h4>
    740                                                 <ul class="accordion-section-content" data-type="<?php echo esc_attr( $tax->name ); ?>" data-obj_type="taxonomy"></ul>
    741                                         </div>
     749                        foreach ( $this->available_item_types() as $available_item_type ) {
     750                                $id = sprintf( 'available-menu-items-%s-%s', $available_item_type['type'], $available_item_type['object'] );
     751                                ?>
     752                                <div id="<?php echo esc_attr( $id ); ?>" class="accordion-section">
     753                                        <h4 class="accordion-section-title"><?php echo esc_html( $available_item_type['title'] ); ?> <span class="no-items"><?php _e( 'No items' ); ?></span><span class="spinner"></span> <button type="button" class="not-a-button"><span class="screen-reader-text"><?php _e( 'Toggle' ); ?></span></button></h4>
     754                                        <ul class="accordion-section-content" data-type="<?php echo esc_attr( $available_item_type['type'] ); ?>" data-object="<?php echo esc_attr( $available_item_type['object'] ); ?>"></ul>
     755                                </div>
    742756                                <?php
    743                                 endforeach;
    744                         endif;
     757                        }
    745758                        ?>
    746759                </div><!-- #available-menu-items -->
    747760        <?php
  • src/wp-includes/class-wp-customize-setting.php

    diff --git src/wp-includes/class-wp-customize-setting.php src/wp-includes/class-wp-customize-setting.php
    index 5b63822..e9ca5bf 100644
    class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting { 
    968968                        'post_title',
    969969                        'post_type',
    970970                        'to_ping',
    971                         'type_label',
    972971                );
    973972                foreach ( $irrelevant_properties as $property ) {
    974973                        unset( $this->value[ $property ] );
    class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting { 
    11431142                }
    11441143
    11451144                if ( ! isset( $post->type_label ) ) {
    1146                         $post->type_label = null;
     1145                        if ( 'post_type' === $post->type ) {
     1146                                $object = get_post_type_object( $post->object );
     1147                                if ( $object ) {
     1148                                        $post->type_label = $object->labels->singular_name;
     1149                                } else {
     1150                                        $post->type_label = $post->object;
     1151                                }
     1152                        } elseif ( 'taxonomy' == $post->type ) {
     1153                                $object = get_taxonomy( $post->object );
     1154                                if ( $object ) {
     1155                                        $post->type_label = $object->labels->singular_name;
     1156                                } else {
     1157                                        $post->type_label = $post->object;
     1158                                }
     1159                        } else {
     1160                                $post->type_label = __( 'Custom Link' );
     1161                        }
    11471162                }
     1163
    11481164                return $post;
    11491165        }
    11501166
  • tests/phpunit/tests/ajax/CustomizeMenus.php

    diff --git tests/phpunit/tests/ajax/CustomizeMenus.php tests/phpunit/tests/ajax/CustomizeMenus.php
    index c39e9b5..abf6300 100644
    class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { 
    122122                                'administrator',
    123123                                array(
    124124                                        'success' => false,
    125                                         'data'    => 'nav_menus_missing_obj_type_or_type_parameter',
     125                                        'data'    => 'nav_menus_missing_type_or_object_parameter',
    126126                                ),
    127127                        ),
    128128                );
    class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { 
    172172                        // Testing empty obj_type and type.
    173173                        array(
    174174                                array(
    175                                         'obj_type' => '',
    176175                                        'type'     => '',
     176                                        'object'   => '',
    177177                                ),
    178178                                array(
    179179                                        'success'  => false,
    180                                         'data'     => 'nav_menus_missing_obj_type_or_type_parameter',
     180                                        'data'     => 'nav_menus_missing_type_or_object_parameter',
    181181                                ),
    182182                        ),
    183183                        // Testing empty obj_type.
    184184                        array(
    185185                                array(
    186                                         'obj_type' => '',
    187                                         'type'     => 'post',
     186                                        'type'    => '',
     187                                        'object'   => 'post',
    188188                                ),
    189189                                array(
    190190                                        'success'  => false,
    191                                         'data'     => 'nav_menus_missing_obj_type_or_type_parameter',
     191                                        'data'     => 'nav_menus_missing_type_or_object_parameter',
    192192                                ),
    193193                        ),
    194194                        // Testing empty type.
    195195                        array(
    196196                                array(
    197                                         'obj_type' => '',
    198                                         'type'     => 'post',
     197                                        'type'    => '',
     198                                        'object'   => 'post',
    199199                                ),
    200200                                array(
    201201                                        'success'  => false,
    202                                         'data'     => 'nav_menus_missing_obj_type_or_type_parameter',
     202                                        'data'     => 'nav_menus_missing_type_or_object_parameter',
    203203                                ),
    204204                        ),
    205205                        // Testing incorrect type option.
    206206                        array(
    207207                                array(
    208                                         'obj_type' => 'post_type',
    209                                         'type'     => 'invalid',
     208                                        'type'    => 'post_type',
     209                                        'object'   => 'invalid',
    210210                                ),
    211211                                array(
    212212                                        'success'  => false,
    class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { 
    259259                return array(
    260260                        array(
    261261                                array(
    262                                         'obj_type' => 'post_type',
    263                                         'type'     => 'post',
     262                                        'type'    => 'post_type',
     263                                        'object'   => 'post',
    264264                                ),
    265265                                true,
    266266                        ),
    267267                        array(
    268268                                array(
    269                                         'obj_type' => 'post_type',
    270                                         'type'     => 'page',
     269                                        'type'    => 'post_type',
     270                                        'object'   => 'page',
    271271                                ),
    272272                                true,
    273273                        ),
    274274                        array(
    275275                                array(
    276                                         'obj_type' => 'post_type',
    277                                         'type'     => 'custom',
     276                                        'type'    => 'post_type',
     277                                        'object'   => 'custom',
    278278                                ),
    279279                                false,
    280280                        ),
    281281                        array(
    282282                                array(
    283                                         'obj_type' => 'taxonomy',
    284                                         'type'     => 'post_tag',
     283                                        'type'    => 'taxonomy',
     284                                        'object'   => 'post_tag',
    285285                                ),
    286286                                true,
    287287                        ),
    class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { 
    363363                return array(
    364364                        array(
    365365                                array(
    366                                         'obj_type' => 'post_type',
    367                                         'type'     => 'post',
     366                                        'type'    => 'post_type',
     367                                        'object'   => 'post',
    368368                                ),
    369369                        ),
    370370                        array(
    371371                                array(
    372                                         'obj_type' => 'post_type',
    373                                         'type'     => 'page',
     372                                        'type'    => 'post_type',
     373                                        'object'   => 'page',
    374374                                ),
    375375                        ),
    376376                        array(
    377377                                array(
    378                                         'obj_type' => 'taxonomy',
    379                                         'type'     => 'post_tag',
     378                                        'type'    => 'taxonomy',
     379                                        'object'   => 'post_tag',
    380380                                ),
    381381                        ),
    382382                );
  • tests/phpunit/tests/customize/nav-menu-item-setting.php

    diff --git tests/phpunit/tests/customize/nav-menu-item-setting.php tests/phpunit/tests/customize/nav-menu-item-setting.php
    index f432517..2e96570 100644
    class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase { 
    3838        }
    3939
    4040        /**
     41         * Filter to add a custom menu item type label.
     42         *
     43         * @param object $menu_item Menu item.
     44         * @return object
     45         */
     46        function filter_type_label( $menu_item ) {
     47                if ( 'custom_type' === $menu_item->type ) {
     48                        $menu_item->type_label = 'Custom Label';
     49                }
     50
     51                return $menu_item;
     52        }
     53
     54        /**
    4155         * Test constants and statics.
    4256         */
    4357        function test_constants() {
    class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase { 
    206220        }
    207221
    208222        /**
     223         * Test value method with a custom object.
     224         *
     225         * @see WP_Customize_Nav_Menu_Item_Setting::value()
     226         */
     227        function test_custom_type_label() {
     228                do_action( 'customize_register', $this->wp_customize );
     229                add_filter( 'wp_setup_nav_menu_item', array( $this, 'filter_type_label' ) );
     230
     231                $menu_id = wp_create_nav_menu( 'Menu' );
     232                $item_id = wp_update_nav_menu_item( $menu_id, 0, array(
     233                        'menu-item-type'   => 'custom_type',
     234                        'menu-item-object' => 'custom_object',
     235                        'menu-item-title'  => 'Cool beans',
     236                        'menu-item-status' => 'publish',
     237                ) );
     238
     239                $post = get_post( $item_id );
     240                $menu_item = wp_setup_nav_menu_item( $post );
     241
     242                $setting_id = "nav_menu_item[$item_id]";
     243                $setting = new WP_Customize_Nav_Menu_Item_Setting( $this->wp_customize, $setting_id );
     244
     245                $value = $setting->value();
     246                $this->assertEquals( $menu_item->type_label, 'Custom Label' );
     247                $this->assertEquals( $menu_item->type_label, $value['type_label'] );
     248        }
     249
     250        /**
    209251         * Test value method returns zero for nav_menu_term_id when previewing a new menu.
    210252         *
    211253         * @see WP_Customize_Nav_Menu_Item_Setting::value()
  • tests/phpunit/tests/customize/nav-menus.php

    diff --git tests/phpunit/tests/customize/nav-menus.php tests/phpunit/tests/customize/nav-menus.php
    index 41991a8..2eba4b3 100644
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    3838        }
    3939
    4040        /**
     41         * Filter to add custom menu item types.
     42         *
     43         * @param array $items Menu item types.
     44         * @return array Menu item types.
     45         */
     46        function filter_item_types( $items ) {
     47                $items[] = array(
     48                        'title'  => 'Custom',
     49                        'type'   => 'custom_type',
     50                        'object' => 'custom_object',
     51                );
     52
     53                return $items;
     54        }
     55
     56        /**
     57         * Filter to add custom menu items.
     58         *
     59         * @param array  $items  The menu items.
     60         * @param string $type   The object type (e.g. taxonomy).
     61         * @param string $object The object name (e.g. category).
     62         * @return array Menu items.
     63         */
     64        function filter_items( $items, $type, $object ) {
     65                $items[] = array(
     66                        'id'         => 'custom-1',
     67                        'title'      => 'Cool beans',
     68                        'type'       => $type,
     69                        'type_label' => 'Custom Label',
     70                        'object'     => $object,
     71                        'url'        => home_url( '/cool-beans/' ),
     72                        'classes'    => 'custom-menu-item cool-beans',
     73                );
     74
     75                return $items;
     76        }
     77
     78        /**
    4179         * Test constructor.
    4280         *
    4381         * @see WP_Customize_Nav_Menus::__construct()
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    207245        }
    208246
    209247        /**
     248         * Test the load_available_items_query method returns custom item.
     249         *
     250         * @see WP_Customize_Nav_Menus::load_available_items_query()
     251         */
     252        function test_load_available_items_query_returns_custom_item() {
     253                add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
     254                add_filter( 'customize_nav_menu_available_items', array( $this, 'filter_items' ), 10, 4 );
     255                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     256
     257                // Expected menu item array.
     258                $expected = array(
     259                        'id'         => 'custom-1',
     260                        'title'      => 'Cool beans',
     261                        'type'       => 'custom_type',
     262                        'type_label' => 'Custom Label',
     263                        'object'     => 'custom_object',
     264                        'url'        => home_url( '/cool-beans/' ),
     265                        'classes'    => 'custom-menu-item cool-beans',
     266                );
     267
     268                $items = $menus->load_available_items_query( 'custom_type', 'custom_object', 0 );
     269                $this->assertContains( $expected, $items );
     270        }
     271
     272        /**
    210273         * Test the search_available_items_query method.
    211274         *
    212275         * @see WP_Customize_Nav_Menus::search_available_items_query()
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    361424        function test_available_item_types() {
    362425
    363426                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     427
    364428                $expected = array(
    365                         'postTypes' => array(
    366                                 'post' => array( 'label' => 'Post' ),
    367                                 'page' => array( 'label' => 'Page' ),
    368                         ),
    369                         'taxonomies' => array(
    370                                 'category' => array( 'label' => 'Category' ),
    371                                 'post_tag' => array( 'label' => 'Tag' ),
    372                         ),
     429                        array( 'title' => 'Post', 'type' => 'post_type', 'object' => 'post' ),
     430                        array( 'title' => 'Page', 'type' => 'post_type', 'object' => 'page' ),
     431                        array( 'title' => 'Category', 'type' => 'taxonomy', 'object' => 'category' ),
     432                        array( 'title' => 'Tag', 'type' => 'taxonomy', 'object' => 'post_tag' ),
    373433                );
     434
    374435                if ( current_theme_supports( 'post-formats' ) ) {
    375                         $expected['taxonomies']['post_format'] = array( 'label' => 'Format' );
     436                        $expected[] = array( 'title' => 'Format', 'type' => 'taxonomy', 'object' => 'post_format' );
    376437                }
     438
    377439                $this->assertEquals( $expected, $menus->available_item_types() );
    378440
    379441                register_taxonomy( 'wptests_tax', array( 'post' ), array( 'labels' => array( 'name' => 'Foo' ) ) );
    380                 $expected = array(
    381                         'postTypes' => array(
    382                                 'post' => array( 'label' => 'Post' ),
    383                                 'page' => array( 'label' => 'Page' ),
    384                         ),
    385                         'taxonomies' => array(
    386                                 'category'    => array( 'label' => 'Category' ),
    387                                 'post_tag'    => array( 'label' => 'Tag' ),
    388                                 'wptests_tax' => array( 'label' => 'Foo' ),
    389                         ),
    390                 );
    391                 if ( current_theme_supports( 'post-formats' ) ) {
    392                         $wptests_tax = array_pop( $expected['taxonomies'] );
    393                         $expected['taxonomies']['post_format'] = array( 'label' => 'Format' );
    394                         $expected['taxonomies']['wptests_tax'] = $wptests_tax;
    395                 }
     442                $expected[] = array( 'title' => 'Foo', 'type' => 'taxonomy', 'object' => 'wptests_tax' );
     443
    396444                $this->assertEquals( $expected, $menus->available_item_types() );
    397445
     446                $expected[] = array( 'title' => 'Custom', 'type' => 'custom_type', 'object' => 'custom_object' );
     447
     448                add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
     449                $this->assertEquals( $expected, $menus->available_item_types() );
     450                remove_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
     451
    398452        }
    399453
    400454        /**
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    427481         * @see WP_Customize_Nav_Menus::available_items_template()
    428482         */
    429483        function test_available_items_template() {
     484                add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
    430485                do_action( 'customize_register', $this->wp_customize );
    431486                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
    432487
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    441496                $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
    442497                if ( $post_types ) {
    443498                        foreach ( $post_types as $type ) {
    444                                 $this->assertContains( 'available-menu-items-' . esc_attr( $type->name ), $template );
    445                                 $this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $type->label ), $template );
    446                                 $this->assertContains( 'data-type="' . esc_attr( $type->name ) . '" data-obj_type="post_type"', $template );
     499                                $this->assertContains( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
     500                                $this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $type->labels->singular_name ), $template );
     501                                $this->assertContains( 'data-type="post_type"', $template );
     502                                $this->assertContains( 'data-object="' . esc_attr( $type->name ) . '"', $template );
    447503                        }
    448504                }
    449505
    450506                $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
    451507                if ( $taxonomies ) {
    452508                        foreach ( $taxonomies as $tax ) {
    453                                 $this->assertContains( 'available-menu-items-' . esc_attr( $tax->name ), $template );
    454                                 $this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $tax->label ), $template );
    455                                 $this->assertContains( 'data-type="' . esc_attr( $tax->name ) . '" data-obj_type="taxonomy"', $template );
     509                                $this->assertContains( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
     510                                $this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $tax->labels->singular_name ), $template );
     511                                $this->assertContains( 'data-type="taxonomy"', $template );
     512                                $this->assertContains( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
    456513                        }
    457514                }
     515
     516                $this->assertContains( 'available-menu-items-custom_type', $template );
     517                $this->assertContains( '<h4 class="accordion-section-title">Custom', $template );
     518                $this->assertContains( 'data-type="custom_type"', $template );
     519                $this->assertContains( 'data-object="custom_object"', $template );
    458520        }
    459521
    460522        /**