Ticket #32708: 32708.3.diff
File 32708.3.diff, 16.2 KB (added by , 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..9828e97 100644
1275 1275 } 1276 1276 1277 1277 control.params.el_classes = containerClasses.join( ' ' ); 1278 control.params.item_type_label = api.Menus.getTypeLabel( settingValue.type, settingValue.object );1278 control.params.item_type_label = settingValue.type_label; 1279 1279 control.params.item_type = settingValue.type; 1280 1280 control.params.url = settingValue.url; 1281 1281 control.params.target = settingValue.target; … … 2552 2552 }; 2553 2553 2554 2554 /** 2555 * Given a menu item type & object, get the label associated with it.2556 *2557 * @param {string} type2558 * @param {string} object2559 * @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 /**2585 2555 * Given a menu item ID, get the control associated with it. 2586 2556 * 2587 2557 * @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..03632bd 100644
class WP_Customize_Nav_Menu_Item_Control extends WP_Customize_Control { 1739 1739 </p> 1740 1740 1741 1741 <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 ) { #> 1743 1743 <p class="link-to-original"> 1744 1744 <?php printf( __( 'Original: %s' ), '<a class="original-link" href="{{ data.url }}">{{ data.original_title }}</a>' ); ?> 1745 1745 </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..3904148 100644
final class WP_Customize_Nav_Menus { 176 176 } 177 177 } 178 178 179 /** 180 * Filter the available menu items. 181 * 182 * @param array $items The array of menu items. 183 * @param string $obj_type The object type. 184 * @param string $obj_name The object name. 185 * @param int $page The current page number. 186 */ 187 $items = apply_filters( 'customize_nav_menu_available_items', $items, $obj_type, $obj_name, $page ); 188 179 189 return $items; 180 190 } 181 191 … … final class WP_Customize_Nav_Menus { 596 606 ); 597 607 598 608 $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 ); 609 if ( $post_types ) { 610 foreach ( $post_types as $slug => $post_type ) { 611 $items['postTypes'][ $slug ] = array( 612 'title' => $post_type->labels->singular_name, 613 'object' => 'post_type', 614 'type' => $post_type->name, 615 ); 616 } 603 617 } 604 618 605 619 $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; 620 if ( $taxonomies ) { 621 foreach ( $taxonomies as $slug => $taxonomy ) { 622 if ( 'post_format' === $taxonomy && ! current_theme_supports( 'post-formats' ) ) { 623 continue; 624 } 625 $items['taxonomies'][ $slug ] = array( 626 'title' => $taxonomy->labels->singular_name, 627 'object' => 'taxonomy', 628 'type' => $taxonomy->name, 629 ); 609 630 } 610 $items['taxonomies'][ $slug ] = array(611 'label' => $taxonomy->labels->singular_name,612 );613 631 } 632 633 /** 634 * Filter the available menu item types. 635 * 636 * @param array $items Custom menu item types. 637 */ 638 $items = apply_filters( 'customize_nav_menu_available_item_types', $items ); 639 614 640 return $items; 615 641 } 616 642 … … final class WP_Customize_Nav_Menus { 716 742 </div> 717 743 </div> 718 744 <?php 719 720 // @todo: consider using add_meta_box/do_accordion_section and making screen-optional?721 745 // 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; 746 foreach ( (array) self::available_item_types() as $item_type ) { 747 if ( empty( $item_type ) ) { 748 continue; 749 } 733 750 734 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' ); 735 if ( $taxonomies ) : 736 foreach ( $taxonomies as $tax ) : 751 foreach( $item_type as $item ) { 737 752 ?> 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>753 <div id="available-menu-items-<?php echo esc_attr( $item['type'] ); ?>" class="accordion-section"> 754 <h4 class="accordion-section-title"><?php echo esc_html( $item['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> 755 <ul class="accordion-section-content" data-type="<?php echo esc_attr( $item['type'] ); ?>" data-obj_type="<?php echo esc_attr( $item['object'] ); ?>"></ul> 741 756 </div> 742 <?php743 endforeach;744 endif;757 <?php 758 } 759 } 745 760 ?> 746 761 </div><!-- #available-menu-items --> 747 762 <?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 { 968 968 'post_title', 969 969 'post_type', 970 970 'to_ping', 971 'type_label',972 971 ); 973 972 foreach ( $irrelevant_properties as $property ) { 974 973 unset( $this->value[ $property ] ); … … class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting { 1143 1142 } 1144 1143 1145 1144 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 } 1147 1162 } 1163 1148 1164 return $post; 1149 1165 } 1150 1166 -
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..9565d27 100644
class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase { 38 38 } 39 39 40 40 /** 41 * Filter to add a custom menu item type label. 42 */ 43 function filter_type_label( $menu_item ) { 44 if ( 'custom_type' === $menu_item->type ) { 45 $menu_item->type_label = 'Custom Label'; 46 } 47 48 return $menu_item; 49 } 50 51 /** 41 52 * Test constants and statics. 42 53 */ 43 54 function test_constants() { … … class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase { 206 217 } 207 218 208 219 /** 220 * Test value method with a custom object. 221 * 222 * @see WP_Customize_Nav_Menu_Item_Setting::value() 223 */ 224 function test_custom_type_label() { 225 do_action( 'customize_register', $this->wp_customize ); 226 add_filter( 'wp_setup_nav_menu_item', array( $this, 'filter_type_label' ) ); 227 228 $menu_id = wp_create_nav_menu( 'Menu' ); 229 $item_id = wp_update_nav_menu_item( $menu_id, 0, array( 230 'menu-item-type' => 'custom_type', 231 'menu-item-object' => 'custom_object', 232 'menu-item-title' => 'Cool beans', 233 'menu-item-status' => 'publish', 234 ) ); 235 236 $post = get_post( $item_id ); 237 $menu_item = wp_setup_nav_menu_item( $post ); 238 239 $setting_id = "nav_menu_item[$item_id]"; 240 $setting = new WP_Customize_Nav_Menu_Item_Setting( $this->wp_customize, $setting_id ); 241 242 $value = $setting->value(); 243 $this->assertEquals( $menu_item->type_label, 'Custom Label' ); 244 } 245 246 /** 209 247 * Test value method returns zero for nav_menu_term_id when previewing a new menu. 210 248 * 211 249 * @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..f95c357 100644
class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 38 38 } 39 39 40 40 /** 41 * Filter to add custom menu item types. 42 */ 43 function filter_item_types( $items ) { 44 $items['custom_object']['custom_type'] = array( 45 'title' => 'Custom', 46 'object' => 'custom_object', 47 'type' => 'custom_type', 48 ); 49 50 return $items; 51 } 52 53 /** 54 * Filter to add custom menu items. 55 */ 56 function filter_items( $items, $obj_type, $obj_name, $page ) { 57 $items[] = array( 58 'id' => 'custom-1', 59 'title' => 'Cool beans', 60 'type' => $obj_name, 61 'type_label' => 'Custom Label', 62 'object' => $obj_type, 63 'url' => home_url( '/cool-beans/' ), 64 'classes' => 'custom-menu-item cool-beans', 65 ); 66 67 return $items; 68 } 69 70 /** 41 71 * Test constructor. 42 72 * 43 73 * @see WP_Customize_Nav_Menus::__construct() … … class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 207 237 } 208 238 209 239 /** 240 * Test the load_available_items_query method returns custom item. 241 * 242 * @see WP_Customize_Nav_Menus::load_available_items_query() 243 */ 244 function test_load_available_items_query_returns_custom_item() { 245 add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) ); 246 add_filter( 'customize_nav_menu_available_items', array( $this, 'filter_items' ), 10, 4 ); 247 $menus = new WP_Customize_Nav_Menus( $this->wp_customize ); 248 249 // Expected menu item array. 250 $expected = array( 251 'id' => 'custom-1', 252 'title' => 'Cool beans', 253 'type' => 'custom_type', 254 'type_label' => 'Custom Label', 255 'object' => 'custom_object', 256 'url' => home_url( '/cool-beans/' ), 257 'classes' => 'custom-menu-item cool-beans', 258 ); 259 260 $items = $menus->load_available_items_query( 'custom_object', 'custom_type', 0 ); 261 $this->assertContains( $expected, $items ); 262 } 263 264 /** 210 265 * Test the search_available_items_query method. 211 266 * 212 267 * @see WP_Customize_Nav_Menus::search_available_items_query() … … class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 361 416 function test_available_item_types() { 362 417 363 418 $menus = new WP_Customize_Nav_Menus( $this->wp_customize ); 419 364 420 $expected = array( 365 421 'postTypes' => array( 366 'post' => array( ' label' => 'Post' ),367 'page' => array( ' label' => 'Page' ),422 'post' => array( 'title' => 'Post', 'object' => 'post_type', 'type' => 'post' ), 423 'page' => array( 'title' => 'Page', 'object' => 'post_type', 'type' => 'page' ), 368 424 ), 369 425 'taxonomies' => array( 370 'category' => array( 'label' => 'Category' ),371 'post_tag' => array( 'label' => 'Tag' ),426 'category' => array( 'title' => 'Category', 'object' => 'taxonomy', 'type' => 'category' ), 427 'post_tag' => array( 'title' => 'Tag', 'object' => 'taxonomy', 'type' => 'post_tag' ), 372 428 ), 373 429 ); 430 374 431 if ( current_theme_supports( 'post-formats' ) ) { 375 $expected['taxonomies']['post_format'] = array( ' label' => 'Format' );432 $expected['taxonomies']['post_format'] = array( 'title' => 'Format', 'object' => 'taxonomy', 'type' => 'post_format' ); 376 433 } 434 377 435 $this->assertEquals( $expected, $menus->available_item_types() ); 378 436 379 437 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 } 438 $expected['taxonomies']['wptests_tax'] = array( 'title' => 'Foo', 'object' => 'taxonomy', 'type' => 'wptests_tax' ); 439 396 440 $this->assertEquals( $expected, $menus->available_item_types() ); 441 442 $expected['custom_object']['custom_type'] = array( 'title' => 'Custom', 'object' => 'custom_object', 'type' => 'custom_type' ); 443 444 add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) ); 445 $this->assertEquals( $expected, $menus->available_item_types() ); 446 remove_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) ); 397 447 398 448 } 399 449 … … class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 427 477 * @see WP_Customize_Nav_Menus::available_items_template() 428 478 */ 429 479 function test_available_items_template() { 480 add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) ); 430 481 do_action( 'customize_register', $this->wp_customize ); 431 482 $menus = new WP_Customize_Nav_Menus( $this->wp_customize ); 432 483 … … class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 442 493 if ( $post_types ) { 443 494 foreach ( $post_types as $type ) { 444 495 $this->assertContains( 'available-menu-items-' . esc_attr( $type->name ), $template ); 445 $this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $type->label ), $template );496 $this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $type->labels->singular_name ), $template ); 446 497 $this->assertContains( 'data-type="' . esc_attr( $type->name ) . '" data-obj_type="post_type"', $template ); 447 498 } 448 499 } … … class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 451 502 if ( $taxonomies ) { 452 503 foreach ( $taxonomies as $tax ) { 453 504 $this->assertContains( 'available-menu-items-' . esc_attr( $tax->name ), $template ); 454 $this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $tax->label ), $template );505 $this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $tax->labels->singular_name ), $template ); 455 506 $this->assertContains( 'data-type="' . esc_attr( $tax->name ) . '" data-obj_type="taxonomy"', $template ); 456 507 } 457 508 } 509 510 $this->assertContains( 'available-menu-items-custom_type', $template ); 511 $this->assertContains( '<h4 class="accordion-section-title">Custom', $template ); 512 $this->assertContains( 'data-type="custom_type" data-obj_type="custom_object"', $template ); 458 513 } 459 514 460 515 /**