Customize: Fix handling of the nav menu item labels (titles) that match defaults (original titles) and fix the display of item type labels.
* Show default labels for nav menu item as placeholders in a control's label field instead of showing blank. * Store empty string as label instead of copying default labels. * Prevent labels for post type archive items from being dropped in preview. * Also ensure that the item type label is displayed on nav menu item controls for settings that are loaded from an existing changeset. Amends [38618]. See #38015. Fixes #38955. git-svn-id: https://develop.svn.wordpress.org/trunk@39393 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
a27570c98a
commit
79182c0fe2
@ -233,6 +233,9 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
|||||||
} else {
|
} else {
|
||||||
$value = $post_value;
|
$value = $post_value;
|
||||||
}
|
}
|
||||||
|
if ( ! empty( $value ) && empty( $value['original_title'] ) ) {
|
||||||
|
$value['original_title'] = $this->get_original_title( (object) $value );
|
||||||
|
}
|
||||||
} elseif ( isset( $this->value ) ) {
|
} elseif ( isset( $this->value ) ) {
|
||||||
$value = $this->value;
|
$value = $this->value;
|
||||||
} else {
|
} else {
|
||||||
@ -260,6 +263,10 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
|||||||
$value = $this->value;
|
$value = $this->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( ! empty( $value ) && empty( $value['type_label'] ) ) {
|
||||||
|
$value['type_label'] = $this->get_type_label( (object) $value );
|
||||||
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,11 +280,8 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
|||||||
* @return string The original title.
|
* @return string The original title.
|
||||||
*/
|
*/
|
||||||
protected function get_original_title( $item ) {
|
protected function get_original_title( $item ) {
|
||||||
if ( empty( $item->object_id ) ) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
$original_title = '';
|
$original_title = '';
|
||||||
if ( 'post_type' === $item->type ) {
|
if ( 'post_type' === $item->type && ! empty( $item->object_id ) ) {
|
||||||
$original_object = get_post( $item->object_id );
|
$original_object = get_post( $item->object_id );
|
||||||
if ( $original_object ) {
|
if ( $original_object ) {
|
||||||
/** This filter is documented in wp-includes/post-template.php */
|
/** This filter is documented in wp-includes/post-template.php */
|
||||||
@ -288,16 +292,53 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
|||||||
$original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
|
$original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ( 'taxonomy' === $item->type ) {
|
} elseif ( 'taxonomy' === $item->type && ! empty( $item->object_id ) ) {
|
||||||
$original_term_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
|
$original_term_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
|
||||||
if ( ! is_wp_error( $original_term_title ) ) {
|
if ( ! is_wp_error( $original_term_title ) ) {
|
||||||
$original_title = $original_term_title;
|
$original_title = $original_term_title;
|
||||||
}
|
}
|
||||||
|
} elseif ( 'post_type_archive' === $item->type ) {
|
||||||
|
$original_object = get_post_type_object( $item->object );
|
||||||
|
if ( $original_object ) {
|
||||||
|
$original_title = $original_object->labels->archives;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$original_title = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
|
$original_title = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
|
||||||
return $original_title;
|
return $original_title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get type label.
|
||||||
|
*
|
||||||
|
* @since 4.7.0
|
||||||
|
* @access protected
|
||||||
|
*
|
||||||
|
* @param object $item Nav menu item.
|
||||||
|
* @returns string The type label.
|
||||||
|
*/
|
||||||
|
protected function get_type_label( $item ) {
|
||||||
|
if ( 'post_type' === $item->type ) {
|
||||||
|
$object = get_post_type_object( $item->object );
|
||||||
|
if ( $object ) {
|
||||||
|
$type_label = $object->labels->singular_name;
|
||||||
|
} else {
|
||||||
|
$type_label = $item->object;
|
||||||
|
}
|
||||||
|
} elseif ( 'taxonomy' === $item->type ) {
|
||||||
|
$object = get_taxonomy( $item->object );
|
||||||
|
if ( $object ) {
|
||||||
|
$type_label = $object->labels->singular_name;
|
||||||
|
} else {
|
||||||
|
$type_label = $item->object;
|
||||||
|
}
|
||||||
|
} elseif ( 'post_type_archive' === $item->type ) {
|
||||||
|
$type_label = __( 'Post Type Archive' );
|
||||||
|
} else {
|
||||||
|
$type_label = __( 'Custom Link' );
|
||||||
|
}
|
||||||
|
return $type_label;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure that the value is fully populated with the necessary properties.
|
* Ensure that the value is fully populated with the necessary properties.
|
||||||
*
|
*
|
||||||
@ -584,23 +625,7 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( ! isset( $post->type_label ) ) {
|
if ( ! isset( $post->type_label ) ) {
|
||||||
if ( 'post_type' === $post->type ) {
|
$post->type_label = $this->get_type_label( $post );
|
||||||
$object = get_post_type_object( $post->object );
|
|
||||||
if ( $object ) {
|
|
||||||
$post->type_label = $object->labels->singular_name;
|
|
||||||
} else {
|
|
||||||
$post->type_label = $post->object;
|
|
||||||
}
|
|
||||||
} elseif ( 'taxonomy' === $post->type ) {
|
|
||||||
$object = get_taxonomy( $post->object );
|
|
||||||
if ( $object ) {
|
|
||||||
$post->type_label = $object->labels->singular_name;
|
|
||||||
} else {
|
|
||||||
$post->type_label = $post->object;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$post->type_label = __( 'Custom Link' );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure nav menu item URL is set according to linked object.
|
// Ensure nav menu item URL is set according to linked object.
|
||||||
|
@ -811,8 +811,11 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
|||||||
|
|
||||||
$setting->preview();
|
$setting->preview();
|
||||||
|
|
||||||
|
$item_value = $setting->value();
|
||||||
|
$this->assertArrayHasKey( 'type_label', $item_value );
|
||||||
$nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
|
$nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
|
||||||
$this->assertEquals( 'Custom Link', $nav_menu_item->type_label );
|
$this->assertEquals( 'Custom Link', $nav_menu_item->type_label );
|
||||||
|
$this->assertEquals( $item_value['type_label'], $nav_menu_item->type_label );
|
||||||
add_filter( 'wp_setup_nav_menu_item', array( $this, 'filter_type_label' ) );
|
add_filter( 'wp_setup_nav_menu_item', array( $this, 'filter_type_label' ) );
|
||||||
$nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
|
$nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
|
||||||
$this->assertEquals( 'Custom Label', $nav_menu_item->type_label );
|
$this->assertEquals( 'Custom Label', $nav_menu_item->type_label );
|
||||||
@ -894,6 +897,167 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
|||||||
$this->assertEquals( get_post_type_archive_link( 'press_release' ), $nav_menu_item->url );
|
$this->assertEquals( get_post_type_archive_link( 'press_release' ), $nav_menu_item->url );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item() for obtaining original title.
|
||||||
|
*
|
||||||
|
* @ticket 38945
|
||||||
|
* @covers WP_Customize_Nav_Menu_Item_Setting::get_original_title()
|
||||||
|
*/
|
||||||
|
function test_get_original_title() {
|
||||||
|
$menu_id = wp_create_nav_menu( 'Menu' );
|
||||||
|
register_post_type( 'press_release', array(
|
||||||
|
'has_archive' => true,
|
||||||
|
'labels' => array(
|
||||||
|
'name' => 'PRs',
|
||||||
|
'singular_name' => 'PR',
|
||||||
|
'archives' => 'All PRs',
|
||||||
|
),
|
||||||
|
) );
|
||||||
|
$original_post_title = 'The PR Post';
|
||||||
|
$post_id = self::factory()->post->create( array( 'post_type' => 'press_release', 'post_title' => $original_post_title ) );
|
||||||
|
$original_term_title = 'The Category Term';
|
||||||
|
$term_id = self::factory()->term->create( array( 'taxonomy' => 'category', 'name' => $original_term_title ) );
|
||||||
|
|
||||||
|
// Post: existing nav menu item.
|
||||||
|
$nav_menu_item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||||
|
'menu-item-object-id' => $post_id,
|
||||||
|
'menu-item-type' => 'post_type',
|
||||||
|
'menu-item-object' => 'press_release',
|
||||||
|
'menu-item-title' => '',
|
||||||
|
'menu-item-status' => 'publish',
|
||||||
|
) );
|
||||||
|
$setting = new WP_Customize_Nav_Menu_Item_Setting(
|
||||||
|
$this->wp_customize,
|
||||||
|
'nav_menu_item[' . $nav_menu_item_id . ']'
|
||||||
|
);
|
||||||
|
$item_value = $setting->value();
|
||||||
|
$this->assertEquals( $original_post_title, $item_value['original_title'] );
|
||||||
|
$this->assertEquals( '', $item_value['title'] );
|
||||||
|
$item = $setting->value_as_wp_post_nav_menu_item();
|
||||||
|
$this->assertObjectHasAttribute( 'type_label', $item );
|
||||||
|
$this->assertEquals( $original_post_title, $item->original_title );
|
||||||
|
$this->assertEquals( $original_post_title, $item->title );
|
||||||
|
$this->assertArrayHasKey( 'type_label', $item_value );
|
||||||
|
$this->assertEquals( get_post_type_object( 'press_release' )->labels->singular_name, $item_value['type_label'] );
|
||||||
|
$this->assertEquals( $item->type_label, $item_value['type_label'] );
|
||||||
|
|
||||||
|
// Post: staged nav menu item.
|
||||||
|
$setting = new WP_Customize_Nav_Menu_Item_Setting(
|
||||||
|
$this->wp_customize,
|
||||||
|
'nav_menu_item[-1]'
|
||||||
|
);
|
||||||
|
$this->wp_customize->set_post_value( $setting->id, array(
|
||||||
|
'object_id' => $post_id,
|
||||||
|
'type' => 'post_type',
|
||||||
|
'object' => 'press_release',
|
||||||
|
'title' => '',
|
||||||
|
'status' => 'publish',
|
||||||
|
) );
|
||||||
|
$setting->preview();
|
||||||
|
$item_value = $setting->value();
|
||||||
|
$this->assertEquals( $original_post_title, $item_value['original_title'] );
|
||||||
|
$this->assertEquals( '', $item_value['title'] );
|
||||||
|
$item = $setting->value_as_wp_post_nav_menu_item();
|
||||||
|
$this->assertObjectHasAttribute( 'type_label', $item );
|
||||||
|
$this->assertEquals( $original_post_title, $item->original_title );
|
||||||
|
$this->assertEquals( $original_post_title, $item->title );
|
||||||
|
$this->assertArrayHasKey( 'type_label', $item_value );
|
||||||
|
$this->assertEquals( get_post_type_object( 'press_release' )->labels->singular_name, $item_value['type_label'] );
|
||||||
|
$this->assertEquals( $item->type_label, $item_value['type_label'] );
|
||||||
|
|
||||||
|
// Term: existing nav menu item.
|
||||||
|
$nav_menu_item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||||
|
'menu-item-object-id' => $term_id,
|
||||||
|
'menu-item-type' => 'taxonomy',
|
||||||
|
'menu-item-object' => 'category',
|
||||||
|
'menu-item-title' => '',
|
||||||
|
'menu-item-status' => 'publish',
|
||||||
|
) );
|
||||||
|
$setting = new WP_Customize_Nav_Menu_Item_Setting(
|
||||||
|
$this->wp_customize,
|
||||||
|
'nav_menu_item[' . $nav_menu_item_id . ']'
|
||||||
|
);
|
||||||
|
$item_value = $setting->value();
|
||||||
|
$this->assertEquals( $original_term_title, $item_value['original_title'] );
|
||||||
|
$this->assertEquals( '', $item_value['title'] );
|
||||||
|
$item = $setting->value_as_wp_post_nav_menu_item();
|
||||||
|
$this->assertObjectHasAttribute( 'type_label', $item );
|
||||||
|
$this->assertEquals( $original_term_title, $item->original_title );
|
||||||
|
$this->assertEquals( $original_term_title, $item->title );
|
||||||
|
$this->assertArrayHasKey( 'type_label', $item_value );
|
||||||
|
$this->assertEquals( get_taxonomy( 'category' )->labels->singular_name, $item_value['type_label'] );
|
||||||
|
$this->assertEquals( $item->type_label, $item_value['type_label'] );
|
||||||
|
|
||||||
|
// Term: staged nav menu item.
|
||||||
|
$setting = new WP_Customize_Nav_Menu_Item_Setting(
|
||||||
|
$this->wp_customize,
|
||||||
|
'nav_menu_item[-2]'
|
||||||
|
);
|
||||||
|
$this->wp_customize->set_post_value( $setting->id, array(
|
||||||
|
'object_id' => $term_id,
|
||||||
|
'type' => 'taxonomy',
|
||||||
|
'object' => 'category',
|
||||||
|
'title' => '',
|
||||||
|
'status' => 'publish',
|
||||||
|
) );
|
||||||
|
$setting->preview();
|
||||||
|
$item_value = $setting->value();
|
||||||
|
$this->assertEquals( $original_term_title, $item_value['original_title'] );
|
||||||
|
$this->assertEquals( '', $item_value['title'] );
|
||||||
|
$item = $setting->value_as_wp_post_nav_menu_item();
|
||||||
|
$this->assertObjectHasAttribute( 'type_label', $item );
|
||||||
|
$this->assertEquals( $original_term_title, $item->original_title );
|
||||||
|
$this->assertEquals( $original_term_title, $item->title );
|
||||||
|
$this->assertArrayHasKey( 'type_label', $item_value );
|
||||||
|
$this->assertEquals( get_taxonomy( 'category' )->labels->singular_name, $item_value['type_label'] );
|
||||||
|
$this->assertEquals( $item->type_label, $item_value['type_label'] );
|
||||||
|
|
||||||
|
// Post Type Archive: existing nav menu item.
|
||||||
|
$nav_menu_item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||||
|
'menu-item-type' => 'post_type_archive',
|
||||||
|
'menu-item-object' => 'press_release',
|
||||||
|
'menu-item-title' => '',
|
||||||
|
'menu-item-status' => 'publish',
|
||||||
|
) );
|
||||||
|
$setting = new WP_Customize_Nav_Menu_Item_Setting(
|
||||||
|
$this->wp_customize,
|
||||||
|
'nav_menu_item[' . $nav_menu_item_id . ']'
|
||||||
|
);
|
||||||
|
$item_value = $setting->value();
|
||||||
|
$this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item_value['original_title'] );
|
||||||
|
$this->assertEquals( '', $item_value['title'] );
|
||||||
|
$item = $setting->value_as_wp_post_nav_menu_item();
|
||||||
|
$this->assertObjectHasAttribute( 'type_label', $item );
|
||||||
|
$this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item->original_title );
|
||||||
|
$this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item->title );
|
||||||
|
$this->assertArrayHasKey( 'type_label', $item_value );
|
||||||
|
$this->assertEquals( __( 'Post Type Archive' ), $item_value['type_label'] );
|
||||||
|
$this->assertEquals( $item->type_label, $item_value['type_label'] );
|
||||||
|
|
||||||
|
// Post Type Archive: staged nav menu item.
|
||||||
|
$setting = new WP_Customize_Nav_Menu_Item_Setting(
|
||||||
|
$this->wp_customize,
|
||||||
|
'nav_menu_item[-3]'
|
||||||
|
);
|
||||||
|
$this->wp_customize->set_post_value( $setting->id, array(
|
||||||
|
'type' => 'post_type_archive',
|
||||||
|
'object' => 'press_release',
|
||||||
|
'title' => '',
|
||||||
|
'status' => 'publish',
|
||||||
|
) );
|
||||||
|
$setting->preview();
|
||||||
|
$item_value = $setting->value();
|
||||||
|
$this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item_value['original_title'] );
|
||||||
|
$this->assertEquals( '', $item_value['title'] );
|
||||||
|
$item = $setting->value_as_wp_post_nav_menu_item();
|
||||||
|
$this->assertObjectHasAttribute( 'type_label', $item );
|
||||||
|
$this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item->original_title );
|
||||||
|
$this->assertEquals( get_post_type_object( 'press_release' )->labels->archives, $item->title );
|
||||||
|
$this->assertArrayHasKey( 'type_label', $item_value );
|
||||||
|
$this->assertEquals( __( 'Post Type Archive' ), $item_value['type_label'] );
|
||||||
|
$this->assertEquals( $item->type_label, $item_value['type_label'] );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item() where title is empty.
|
* Test WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item() where title is empty.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user