Make WordPress Core

Ticket #32708: 32708.patch

File 32708.patch, 6.1 KB (added by imath, 10 years ago)
  • 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 126b9f0..0ccc3e8 100644
    final class WP_Customize_Nav_Menus { 
    7676                        wp_send_json_error( array( 'message' => __( 'Missing obj_type or type param.' ) ) );
    7777                }
    7878
    79                 $obj_type = sanitize_key( $_POST['obj_type'] );
    80                 if ( ! in_array( $obj_type, array( 'post_type', 'taxonomy' ) ) ) {
     79                $obj_type        = sanitize_key( $_POST['obj_type'] );
     80                $valid_obj_types = array();
     81
     82                if ( empty( $this->items ) ) {
     83                        $this->items = $this->available_item_types();
     84
     85                        foreach ( $this->items as $item_types ) {
     86                                // Continue if no item types set for the item
     87                                if ( empty( $item_types ) ) {
     88                                        continue;
     89                                }
     90
     91                                // Loop through item types to set valid types
     92                                foreach( $item_types as $item_type ) {
     93                                        if ( isset( $item_type['type'] ) && ! in_array( $item_type['type'], $valid_obj_types ) ) {
     94                                                $valid_obj_types[] = $item_type['type'];
     95                                        }
     96                                }
     97                        }
     98
     99                // Defaults to post_type & taxonomy objects
     100                } else {
     101                        $valid_obj_types = array( 'post_type', 'taxonomy' );
     102                }
     103
     104                if ( ! in_array( $obj_type, $valid_obj_types ) ) {
    81105                        wp_send_json_error( array( 'message' => __( 'Invalid obj_type param: ' . $obj_type ) ) );
    82106                }
    83107                $taxonomy_or_post_type = sanitize_key( $_POST['type'] );
    final class WP_Customize_Nav_Menus { 
    145169                                        'object_id'  => $term->term_id,
    146170                                );
    147171                        }
     172
     173                // Let plugins populate their custom items
     174                } else {
     175                        $custom_obj_types = array_diff( $valid_obj_types, array( 'post_type', 'taxonomy' ) );
     176
     177                        if ( ! empty( $custom_obj_types ) ) {
     178                                foreach( $custom_obj_types as $custom_obj_type ) {
     179                                        /**
     180                                         * Filter here to add your custom items
     181                                         *
     182                                         * @param array  $items
     183                                         * @param string $taxonomy_or_post_type the requested type
     184                                         * @param int    $page the page num being requested
     185                                         */
     186                                        $items = apply_filters( "customizer_nav_menus_{$custom_obj_type}_get_items", array(), $taxonomy_or_post_type, $page );
     187                                }
     188                        }
    148189                }
    149190
    150191                wp_send_json_success( array( 'items' => $items ) );
    final class WP_Customize_Nav_Menus { 
    531572                );
    532573
    533574                $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
    534                 foreach ( $post_types as $slug => $post_type ) {
    535                         $items['postTypes'][ $slug ] = array(
    536                                 'label' => $post_type->labels->singular_name,
    537                         );
     575
     576                if ( $post_types ) {
     577                        foreach ( $post_types as $slug => $post_type ) {
     578                                $items['postTypes'][ $slug ] = array(
     579                                        'label' => $post_type->labels->singular_name,
     580                                        'name'  => $post_type->name,
     581                                        'type'  => 'post_type',
     582                                );
     583                        }
    538584                }
    539585
    540586                $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' );
    541                 foreach ( $taxonomies as $slug => $taxonomy ) {
    542                         if ( 'post_format' === $taxonomy && ! current_theme_supports( 'post-formats' ) ) {
    543                                 continue;
     587
     588                if ( $taxonomies ) {
     589                        foreach ( $taxonomies as $slug => $taxonomy ) {
     590                                if ( 'post_format' === $taxonomy && ! current_theme_supports( 'post-formats' ) ) {
     591                                        continue;
     592                                }
     593                                $items['taxonomies'][ $slug ] = array(
     594                                        'label' => $taxonomy->labels->singular_name,
     595                                        'name'  => $taxonomy->name,
     596                                        'type'  => 'taxonomy',
     597                                );
    544598                        }
    545                         $items['taxonomies'][ $slug ] = array(
    546                                 'label' => $taxonomy->labels->singular_name,
    547                         );
    548599                }
    549                 return $items;
     600
     601                // Allow plugins to add their custom types
     602                $this->items = apply_filters( 'customizer_nav_menus_item_types', $items );
     603
     604                return $this->items;
    550605        }
    551606
    552607        /**
    final class WP_Customize_Nav_Menus { 
    649704                                </div>
    650705                        </div>
    651706                        <?php
    652 
    653707                        // @todo: consider using add_meta_box/do_accordion_section and making screen-optional?
    654708                        // Containers for per-post-type item browsing; items added with JS.
    655                         $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
    656                         if ( $post_types ) :
    657                                 foreach ( $post_types as $type ) :
    658                                         ?>
    659                                         <div id="available-menu-items-<?php echo esc_attr( $type->name ); ?>" class="accordion-section">
    660                                                 <h4 class="accordion-section-title"><?php echo esc_html( $type->label ); ?> <span class="spinner"></span> <button type="button" class="not-a-button"><span class="screen-reader-text"><?php _e( 'Toggle' ); ?></span></button></h4>
    661                                                 <div class="accordion-section-content" data-type="<?php echo esc_attr( $type->name ); ?>" data-obj_type="post_type"></div>
    662                                         </div>
    663                                 <?php
    664                                 endforeach;
    665                         endif;
    666 
    667                         $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
    668                         if ( $taxonomies ) :
    669                                 foreach ( $taxonomies as $tax ) :
    670                                         ?>
    671                                         <div id="available-menu-items-<?php echo esc_attr( $tax->name ); ?>" class="accordion-section">
    672                                                 <h4 class="accordion-section-title"><?php echo esc_html( $tax->label ); ?> <span class="spinner"></span> <button type="button" class="not-a-button"><span class="screen-reader-text"><?php _e( 'Toggle' ); ?></span></button></h4>
    673                                                 <div class="accordion-section-content" data-type="<?php echo esc_attr( $tax->name ); ?>" data-obj_type="taxonomy"></div>
     709
     710                        // Stop if no items set
     711                        if ( empty( $this->items ) ) {
     712                                return;
     713                        }
     714
     715                        foreach ( $this->items as $item_types ) {
     716                                // Continue if no item types set for the item
     717                                if ( empty( $item_types ) ) {
     718                                        continue;
     719                                }
     720
     721                                // Loop through item types to build templates
     722                                foreach( $item_types as $item_type ) : ?>
     723                                        <div id="available-menu-items-<?php echo esc_attr( $item_type['name'] ); ?>" class="accordion-section">
     724                                                <h4 class="accordion-section-title"><?php echo esc_html( $item_type['label'] ); ?> <span class="spinner"></span> <button type="button" class="not-a-button"><span class="screen-reader-text"><?php _e( 'Toggle' ); ?></span></button></h4>
     725                                                <div class="accordion-section-content" data-type="<?php echo esc_attr( $item_type['name'] ); ?>" data-obj_type="<?php echo esc_attr( $item_type['type'] ); ?>"></div>
    674726                                        </div>
    675                                 <?php
    676                                 endforeach;
    677                         endif;
     727                                <?php endforeach;
     728                        }
    678729                        ?>
    679730                </div><!-- #available-menu-items -->
    680731        <?php