Make WordPress Core

Ticket #37586: 37586.2.diff

File 37586.2.diff, 2.3 KB (added by dinhtungdu, 4 years ago)

Introduce new function to get filtered available item types.

  • src/wp-includes/class-wp-customize-nav-menus.php

     
    305305        public function search_available_items_query( $args = array() ) {
    306306                $items = array();
    307307
    308                 $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
     308                $post_type_objects = $this->get_available_item_types( 'post_type' );
    309309                $query             = array(
    310310                        'post_type'              => array_keys( $post_type_objects ),
    311311                        'suppress_filters'       => true,
     
    363363                }
    364364
    365365                // Query taxonomy terms.
    366                 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'names' );
    367                 $terms      = get_terms(
    368                         array(
    369                                 'taxonomies' => $taxonomies,
    370                                 'name__like' => $args['s'],
    371                                 'number'     => 20,
    372                                 'offset'     => 20 * ( $args['pagenum'] - 1 ),
    373                         )
    374                 );
     366                $taxonomies = $this->get_available_item_types( 'taxonomy', 'object' );
    375367
     368                if( empty( $taxonomies ) ) {
     369                        $terms = array();
     370                } else {
     371                        $terms = get_terms(
     372                                array(
     373                                        'taxonomy'   => $taxonomies,
     374                                        'name__like' => $args['s'],
     375                                        'number'     => 20,
     376                                        'offset'     => 20 * ( $args['pagenum'] - 1 ),
     377                                )
     378                        );
     379                }
     380
    376381                // Check if any taxonomies were found.
    377382                if ( ! empty( $terms ) ) {
    378383                        foreach ( $terms as $term ) {
     
    864869        }
    865870
    866871        /**
     872         * Get specific type from available item types.
     873         *
     874         * @since 5.4.0
     875         *
     876         * @param string $item_type  Item type. Can be `post_type` or `taxonomy`.
     877         * @param mixed  $return_key Return specific key of the item type array.
     878         *                           Default set to false which returns whole object.
     879         *
     880         * @return array
     881         */
     882        public function get_available_item_types( $item_type, $return_key = false ) {
     883                $item_types = array_filter(
     884                        $this->available_item_types(),
     885                        function( $item ) use ( $item_type ) {
     886                                return $item_type === $item['type'];
     887                        }
     888                );
     889
     890                if( ! $return_key ) {
     891                        return $item_types;
     892                }
     893
     894                return array_map(
     895                        function( $item ) use ( $return_key ) {
     896                                return $item[ $return_key ];
     897                        },
     898                        $item_types
     899                );
     900        }
     901
     902
     903        /**
    867904         * Add a new `auto-draft` post.
    868905         *
    869906         * @since 4.7.0