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.


git-svn-id: https://develop.svn.wordpress.org/trunk@33366 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter
2015-07-22 20:28:03 +00:00
parent 2d85378c52
commit 9e383a560a
7 changed files with 266 additions and 172 deletions
+25 -25
View File
@@ -122,7 +122,7 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
'administrator',
array(
'success' => false,
'data' => 'nav_menus_missing_obj_type_or_type_parameter',
'data' => 'nav_menus_missing_type_or_object_parameter',
),
),
);
@@ -172,41 +172,41 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
// Testing empty obj_type and type.
array(
array(
'obj_type' => '',
'type' => '',
'object' => '',
),
array(
'success' => false,
'data' => 'nav_menus_missing_obj_type_or_type_parameter',
'data' => 'nav_menus_missing_type_or_object_parameter',
),
),
// Testing empty obj_type.
array(
array(
'obj_type' => '',
'type' => 'post',
'type' => '',
'object' => 'post',
),
array(
'success' => false,
'data' => 'nav_menus_missing_obj_type_or_type_parameter',
'data' => 'nav_menus_missing_type_or_object_parameter',
),
),
// Testing empty type.
array(
array(
'obj_type' => '',
'type' => 'post',
'type' => '',
'object' => 'post',
),
array(
'success' => false,
'data' => 'nav_menus_missing_obj_type_or_type_parameter',
'data' => 'nav_menus_missing_type_or_object_parameter',
),
),
// Testing incorrect type option.
array(
array(
'obj_type' => 'post_type',
'type' => 'invalid',
'type' => 'post_type',
'object' => 'invalid',
),
array(
'success' => false,
@@ -259,29 +259,29 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
return array(
array(
array(
'obj_type' => 'post_type',
'type' => 'post',
'type' => 'post_type',
'object' => 'post',
),
true,
),
array(
array(
'obj_type' => 'post_type',
'type' => 'page',
'type' => 'post_type',
'object' => 'page',
),
true,
),
array(
array(
'obj_type' => 'post_type',
'type' => 'custom',
'type' => 'post_type',
'object' => 'custom',
),
false,
),
array(
array(
'obj_type' => 'taxonomy',
'type' => 'post_tag',
'type' => 'taxonomy',
'object' => 'post_tag',
),
true,
),
@@ -363,20 +363,20 @@ class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
return array(
array(
array(
'obj_type' => 'post_type',
'type' => 'post',
'type' => 'post_type',
'object' => 'post',
),
),
array(
array(
'obj_type' => 'post_type',
'type' => 'page',
'type' => 'post_type',
'object' => 'page',
),
),
array(
array(
'obj_type' => 'taxonomy',
'type' => 'post_tag',
'type' => 'taxonomy',
'object' => 'post_tag',
),
),
);
@@ -37,6 +37,20 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
parent::clean_up_global_scope();
}
/**
* Filter to add a custom menu item type label.
*
* @param object $menu_item Menu item.
* @return object
*/
function filter_type_label( $menu_item ) {
if ( 'custom_type' === $menu_item->type ) {
$menu_item->type_label = 'Custom Label';
}
return $menu_item;
}
/**
* Test constants and statics.
*/
@@ -205,6 +219,34 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
$this->assertEquals( 'Salutations', $value['original_title'] );
}
/**
* Test value method with a custom object.
*
* @see WP_Customize_Nav_Menu_Item_Setting::value()
*/
function test_custom_type_label() {
do_action( 'customize_register', $this->wp_customize );
add_filter( 'wp_setup_nav_menu_item', array( $this, 'filter_type_label' ) );
$menu_id = wp_create_nav_menu( 'Menu' );
$item_id = wp_update_nav_menu_item( $menu_id, 0, array(
'menu-item-type' => 'custom_type',
'menu-item-object' => 'custom_object',
'menu-item-title' => 'Cool beans',
'menu-item-status' => 'publish',
) );
$post = get_post( $item_id );
$menu_item = wp_setup_nav_menu_item( $post );
$setting_id = "nav_menu_item[$item_id]";
$setting = new WP_Customize_Nav_Menu_Item_Setting( $this->wp_customize, $setting_id );
$value = $setting->value();
$this->assertEquals( $menu_item->type_label, 'Custom Label' );
$this->assertEquals( $menu_item->type_label, $value['type_label'] );
}
/**
* Test value method returns zero for nav_menu_term_id when previewing a new menu.
*
+93 -31
View File
@@ -37,6 +37,44 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
parent::clean_up_global_scope();
}
/**
* Filter to add custom menu item types.
*
* @param array $items Menu item types.
* @return array Menu item types.
*/
function filter_item_types( $items ) {
$items[] = array(
'title' => 'Custom',
'type' => 'custom_type',
'object' => 'custom_object',
);
return $items;
}
/**
* Filter to add custom menu items.
*
* @param array $items The menu items.
* @param string $type The object type (e.g. taxonomy).
* @param string $object The object name (e.g. category).
* @return array Menu items.
*/
function filter_items( $items, $type, $object ) {
$items[] = array(
'id' => 'custom-1',
'title' => 'Cool beans',
'type' => $type,
'type_label' => 'Custom Label',
'object' => $object,
'url' => home_url( '/cool-beans/' ),
'classes' => 'custom-menu-item cool-beans',
);
return $items;
}
/**
* Test constructor.
*
@@ -206,6 +244,31 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$this->assertContains( $expected, $items );
}
/**
* Test the load_available_items_query method returns custom item.
*
* @see WP_Customize_Nav_Menus::load_available_items_query()
*/
function test_load_available_items_query_returns_custom_item() {
add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
add_filter( 'customize_nav_menu_available_items', array( $this, 'filter_items' ), 10, 4 );
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
// Expected menu item array.
$expected = array(
'id' => 'custom-1',
'title' => 'Cool beans',
'type' => 'custom_type',
'type_label' => 'Custom Label',
'object' => 'custom_object',
'url' => home_url( '/cool-beans/' ),
'classes' => 'custom-menu-item cool-beans',
);
$items = $menus->load_available_items_query( 'custom_type', 'custom_object', 0 );
$this->assertContains( $expected, $items );
}
/**
* Test the search_available_items_query method.
*
@@ -361,40 +424,31 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
function test_available_item_types() {
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
$expected = array(
'postTypes' => array(
'post' => array( 'label' => 'Post' ),
'page' => array( 'label' => 'Page' ),
),
'taxonomies' => array(
'category' => array( 'label' => 'Category' ),
'post_tag' => array( 'label' => 'Tag' ),
),
array( 'title' => 'Post', 'type' => 'post_type', 'object' => 'post' ),
array( 'title' => 'Page', 'type' => 'post_type', 'object' => 'page' ),
array( 'title' => 'Category', 'type' => 'taxonomy', 'object' => 'category' ),
array( 'title' => 'Tag', 'type' => 'taxonomy', 'object' => 'post_tag' ),
);
if ( current_theme_supports( 'post-formats' ) ) {
$expected['taxonomies']['post_format'] = array( 'label' => 'Format' );
$expected[] = array( 'title' => 'Format', 'type' => 'taxonomy', 'object' => 'post_format' );
}
$this->assertEquals( $expected, $menus->available_item_types() );
register_taxonomy( 'wptests_tax', array( 'post' ), array( 'labels' => array( 'name' => 'Foo' ) ) );
$expected = array(
'postTypes' => array(
'post' => array( 'label' => 'Post' ),
'page' => array( 'label' => 'Page' ),
),
'taxonomies' => array(
'category' => array( 'label' => 'Category' ),
'post_tag' => array( 'label' => 'Tag' ),
'wptests_tax' => array( 'label' => 'Foo' ),
),
);
if ( current_theme_supports( 'post-formats' ) ) {
$wptests_tax = array_pop( $expected['taxonomies'] );
$expected['taxonomies']['post_format'] = array( 'label' => 'Format' );
$expected['taxonomies']['wptests_tax'] = $wptests_tax;
}
$expected[] = array( 'title' => 'Foo', 'type' => 'taxonomy', 'object' => 'wptests_tax' );
$this->assertEquals( $expected, $menus->available_item_types() );
$expected[] = array( 'title' => 'Custom', 'type' => 'custom_type', 'object' => 'custom_object' );
add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
$this->assertEquals( $expected, $menus->available_item_types() );
remove_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
}
/**
@@ -427,6 +481,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
* @see WP_Customize_Nav_Menus::available_items_template()
*/
function test_available_items_template() {
add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
do_action( 'customize_register', $this->wp_customize );
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
@@ -441,20 +496,27 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
if ( $post_types ) {
foreach ( $post_types as $type ) {
$this->assertContains( 'available-menu-items-' . esc_attr( $type->name ), $template );
$this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $type->label ), $template );
$this->assertContains( 'data-type="' . esc_attr( $type->name ) . '" data-obj_type="post_type"', $template );
$this->assertContains( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
$this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $type->labels->singular_name ), $template );
$this->assertContains( 'data-type="post_type"', $template );
$this->assertContains( 'data-object="' . esc_attr( $type->name ) . '"', $template );
}
}
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
if ( $taxonomies ) {
foreach ( $taxonomies as $tax ) {
$this->assertContains( 'available-menu-items-' . esc_attr( $tax->name ), $template );
$this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $tax->label ), $template );
$this->assertContains( 'data-type="' . esc_attr( $tax->name ) . '" data-obj_type="taxonomy"', $template );
$this->assertContains( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
$this->assertContains( '<h4 class="accordion-section-title">' . esc_html( $tax->labels->singular_name ), $template );
$this->assertContains( 'data-type="taxonomy"', $template );
$this->assertContains( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
}
}
$this->assertContains( 'available-menu-items-custom_type', $template );
$this->assertContains( '<h4 class="accordion-section-title">Custom', $template );
$this->assertContains( 'data-type="custom_type"', $template );
$this->assertContains( 'data-object="custom_object"', $template );
}
/**