Make WordPress Core


Ignore:
Timestamp:
07/22/2015 08:28:03 PM (11 years ago)
Author:
westonruter
Message:

Customizer: Introduce customize_nav_menu_available_item_types and customize_nav_menu_available_items filters.

Allows for new available menu item types/objects to be registered in addition to filtering the available items that are returned for each menu item type/object.

Props valendesigns, imath, westonruter.
See #32832.
Fixes #32708.

File:
1 edited

Legend:

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

    r33346 r33366  
    7676        }
    7777
    78         if ( empty( $_POST['obj_type'] ) || empty( $_POST['type'] ) ) {
    79             wp_send_json_error( 'nav_menus_missing_obj_type_or_type_parameter' );
    80         }
    81 
    82         $obj_type = sanitize_key( $_POST['obj_type'] );
    83         $obj_name = sanitize_key( $_POST['type'] );
     78        if ( empty( $_POST['type'] ) || empty( $_POST['object'] ) ) {
     79            wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
     80        }
     81
     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 ) ) {
     
    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(
     
    129129                'orderby'     => 'date',
    130130                'order'       => 'DESC',
    131                 'post_type'   => $obj_name,
     131                'post_type'   => $object,
    132132            ) );
    133133            foreach ( $posts as $post ) {
     
    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'      => '',
     
    176176            }
    177177        }
     178
     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 );
    178190
    179191        return $items;
     
    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;
    609             }
    610             $items['taxonomies'][ $slug ] = array(
    611                 'label' => $taxonomy->labels->singular_name,
    612             );
    613         }
    614         return $items;
     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                );
     631            }
     632        }
     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
     
    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>
     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>
    730756                <?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>
    742                 <?php
    743                 endforeach;
    744             endif;
     757            }
    745758            ?>
    746759        </div><!-- #available-menu-items -->
Note: See TracChangeset for help on using the changeset viewer.