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.

Built from https://develop.svn.wordpress.org/trunk@38618


git-svn-id: http://core.svn.wordpress.org/trunk@38561 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2016-09-17 21:50:30 +00:00
parent ea69657b59
commit 037a236e42
6 changed files with 58 additions and 19 deletions

View File

@ -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' );

File diff suppressed because one or more lines are too long

View File

@ -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,

View File

@ -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">

View File

@ -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;

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-alpha-38617';
$wp_version = '4.7-alpha-38618';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.