Customize: Ensure nav menu items lacking a label use the title from the original object.
Use original title as placeholder for label and in control title. Prevent original title from overriding empty label in initial setting values. Fixes #38015. git-svn-id: https://develop.svn.wordpress.org/trunk@38618 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
998c06222f
commit
87dc042623
@ -1397,14 +1397,14 @@
|
||||
}
|
||||
|
||||
var titleEl = control.container.find( '.menu-item-title' ),
|
||||
titleText = item.title || api.Menus.data.l10n.untitled;
|
||||
titleText = item.title || item.original_title || api.Menus.data.l10n.untitled;
|
||||
|
||||
if ( item._invalid ) {
|
||||
titleText = api.Menus.data.l10n.invalidTitleTpl.replace( '%s', titleText );
|
||||
}
|
||||
|
||||
// Don't update to an empty title.
|
||||
if ( item.title ) {
|
||||
if ( item.title || item.original_title ) {
|
||||
titleEl
|
||||
.text( titleText )
|
||||
.removeClass( 'no-title' );
|
||||
|
@ -587,6 +587,10 @@ final class WP_Customize_Nav_Menus {
|
||||
$menu_item_setting_id = 'nav_menu_item[' . $item->ID . ']';
|
||||
|
||||
$value = (array) $item;
|
||||
if ( empty( $value['post_title'] ) ) {
|
||||
$value['title'] = '';
|
||||
}
|
||||
|
||||
$value['nav_menu_term_id'] = $menu_id;
|
||||
$this->manager->add_setting( new WP_Customize_Nav_Menu_Item_Setting( $this->manager, $menu_item_setting_id, array(
|
||||
'value' => $value,
|
||||
|
@ -69,7 +69,7 @@ class WP_Customize_Nav_Menu_Item_Control extends WP_Customize_Control {
|
||||
<span class="item-type" aria-hidden="true">{{ data.item_type_label }}</span>
|
||||
<span class="item-title" aria-hidden="true">
|
||||
<span class="spinner"></span>
|
||||
<span class="menu-item-title<# if ( ! data.title ) { #> no-title<# } #>">{{ data.title || wp.customize.Menus.data.l10n.untitled }}</span>
|
||||
<span class="menu-item-title<# if ( ! data.title && ! data.original_title ) { #> no-title<# } #>">{{ data.title || data.original_title || wp.customize.Menus.data.l10n.untitled }}</span>
|
||||
</span>
|
||||
<span class="item-controls">
|
||||
<button type="button" class="button-link item-edit" aria-expanded="false"><span class="screen-reader-text"><?php
|
||||
@ -96,7 +96,7 @@ class WP_Customize_Nav_Menu_Item_Control extends WP_Customize_Control {
|
||||
<p class="description description-thin">
|
||||
<label for="edit-menu-item-title-{{ data.menu_item_id }}">
|
||||
<?php _e( 'Navigation Label' ); ?><br />
|
||||
<input type="text" id="edit-menu-item-title-{{ data.menu_item_id }}" class="widefat edit-menu-item-title" name="menu-item-title" />
|
||||
<input type="text" id="edit-menu-item-title-{{ data.menu_item_id }}" placeholder="{{ data.original_title }}" class="widefat edit-menu-item-title" name="menu-item-title" />
|
||||
</label>
|
||||
</p>
|
||||
<p class="field-link-target description description-thin">
|
||||
|
@ -233,7 +233,7 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
||||
} else {
|
||||
$value = $post_value;
|
||||
}
|
||||
} else if ( isset( $this->value ) ) {
|
||||
} elseif ( isset( $this->value ) ) {
|
||||
$value = $this->value;
|
||||
} else {
|
||||
$value = false;
|
||||
@ -242,7 +242,11 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
||||
if ( $this->post_id > 0 ) {
|
||||
$post = get_post( $this->post_id );
|
||||
if ( $post && self::POST_TYPE === $post->post_type ) {
|
||||
$is_title_empty = empty( $post->post_title );
|
||||
$value = (array) wp_setup_nav_menu_item( $post );
|
||||
if ( $is_title_empty ) {
|
||||
$value['title'] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -259,6 +263,40 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get original title.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @param object $item Nav menu item.
|
||||
* @return string The original title.
|
||||
*/
|
||||
protected function get_original_title( $item ) {
|
||||
if ( empty( $item->object_id ) ) {
|
||||
return '';
|
||||
}
|
||||
$original_title = '';
|
||||
if ( 'post_type' === $item->type ) {
|
||||
$original_object = get_post( $item->object_id );
|
||||
if ( $original_object ) {
|
||||
/** This filter is documented in wp-includes/post-template.php */
|
||||
$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
|
||||
|
||||
if ( '' === $original_title ) {
|
||||
/* translators: %d: ID of a post */
|
||||
$original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
|
||||
}
|
||||
}
|
||||
} elseif ( 'taxonomy' === $item->type ) {
|
||||
$original_term_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
|
||||
if ( ! is_wp_error( $original_term_title ) ) {
|
||||
$original_title = $original_term_title;
|
||||
}
|
||||
}
|
||||
$original_title = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
|
||||
return $original_title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the value is fully populated with the necessary properties.
|
||||
*
|
||||
@ -284,16 +322,7 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
||||
}
|
||||
|
||||
if ( ! isset( $this->value['original_title'] ) ) {
|
||||
$original_title = '';
|
||||
if ( 'post_type' === $this->value['type'] ) {
|
||||
$original_title = get_the_title( $this->value['object_id'] );
|
||||
} elseif ( 'taxonomy' === $this->value['type'] ) {
|
||||
$original_title = get_term_field( 'name', $this->value['object_id'], $this->value['object'], 'raw' );
|
||||
if ( is_wp_error( $original_title ) ) {
|
||||
$original_title = '';
|
||||
}
|
||||
}
|
||||
$this->value['original_title'] = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
|
||||
$this->value['original_title'] = $this->get_original_title( (object) $this->value );
|
||||
}
|
||||
|
||||
if ( ! isset( $this->value['nav_menu_term_id'] ) && $this->post_id > 0 ) {
|
||||
@ -534,6 +563,12 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
||||
$item->menu_order = $item->position;
|
||||
unset( $item->position );
|
||||
|
||||
if ( empty( $item->original_title ) ) {
|
||||
$item->original_title = $this->get_original_title( $item );
|
||||
}
|
||||
if ( empty( $item->title ) && ! empty( $item->original_title ) ) {
|
||||
$item->title = $item->original_title;
|
||||
}
|
||||
if ( $item->title ) {
|
||||
$item->post_title = $item->title;
|
||||
}
|
||||
@ -554,7 +589,7 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
||||
} else {
|
||||
$post->type_label = $post->object;
|
||||
}
|
||||
} elseif ( 'taxonomy' == $post->type ) {
|
||||
} elseif ( 'taxonomy' === $post->type ) {
|
||||
$object = get_taxonomy( $post->object );
|
||||
if ( $object ) {
|
||||
$post->type_label = $object->labels->singular_name;
|
||||
|
@ -184,6 +184,34 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
$this->assertEquals( $other_menu_id, $value['nav_menu_term_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test value method with post without nav menu item title (label).
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::value()
|
||||
*/
|
||||
function test_value_type_post_type_without_label() {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
|
||||
$original_title = 'Hello World';
|
||||
$post_id = self::factory()->post->create( array( 'post_title' => $original_title ) );
|
||||
|
||||
$menu_id = wp_create_nav_menu( 'Menu' );
|
||||
$item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||
'menu-item-type' => 'post_type',
|
||||
'menu-item-object' => 'post',
|
||||
'menu-item-object-id' => $post_id,
|
||||
'menu-item-title' => '',
|
||||
'menu-item-status' => 'publish',
|
||||
) );
|
||||
|
||||
$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( '', $value['title'] );
|
||||
$this->assertEquals( $original_title, $value['original_title'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test value method with taxonomy.
|
||||
*
|
||||
@ -804,4 +832,37 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
$this->assertEquals( $expected, $nav_menu_item->attr_title );
|
||||
$this->assertEquals( 'Attempted \o/ o’o markup', $nav_menu_item->description );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item() where title is empty.
|
||||
*
|
||||
* @ticket 38015
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()
|
||||
*/
|
||||
function test_value_as_wp_post_nav_menu_item_with_empty_title() {
|
||||
$original_title = 'The Original Title';
|
||||
$post_id = self::factory()->post->create( array( 'post_title' => $original_title ) );
|
||||
|
||||
$setting = new WP_Customize_Nav_Menu_Item_Setting(
|
||||
$this->wp_customize,
|
||||
'nav_menu_item[123]'
|
||||
);
|
||||
|
||||
$post_value = array_merge(
|
||||
$setting->default,
|
||||
array(
|
||||
'object_id' => $post_id,
|
||||
'object' => 'post',
|
||||
'type' => 'post_type',
|
||||
'status' => 'publish',
|
||||
'nav_menu_term_id' => 0,
|
||||
)
|
||||
);
|
||||
$this->wp_customize->set_post_value( $setting->id, $post_value );
|
||||
|
||||
$setting->preview();
|
||||
|
||||
$nav_menu_item = $setting->value_as_wp_post_nav_menu_item();
|
||||
$this->assertEquals( $original_title, $nav_menu_item->title );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user