From 415a5c60da411721de13e2e33af79984458fa26d Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 9 May 2013 00:22:02 +0000 Subject: [PATCH] * Pass ellipsis as a parameter to wp_html_excerpt() instead of appending it manually. * Consolidate the logic to avoid appending ellipsis if the entire string is shown. * Show ellipsis after truncated filenames and post titles. props solarissmoke, bpetty, SergeyBiryukov. fixes #11446. git-svn-id: http://core.svn.wordpress.org/trunk@24214 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/async-upload.php | 2 +- wp-admin/edit-comments.php | 12 ++++++------ wp-admin/includes/dashboard.php | 2 +- wp-admin/includes/media.php | 2 +- wp-admin/includes/nav-menu.php | 5 +---- wp-admin/nav-menus.php | 9 ++------- wp-includes/admin-bar.php | 4 +--- wp-includes/class-wp-customize-manager.php | 4 +--- wp-includes/comment.php | 2 +- wp-includes/formatting.php | 15 ++++++++++----- wp-trackback.php | 4 ++-- 11 files changed, 27 insertions(+), 34 deletions(-) diff --git a/wp-admin/async-upload.php b/wp-admin/async-upload.php index 5f2938e84f..63497642f9 100644 --- a/wp-admin/async-upload.php +++ b/wp-admin/async-upload.php @@ -57,7 +57,7 @@ if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id echo ''; echo '' . _x( 'Edit', 'media item' ) . ''; $title = $post->post_title ? $post->post_title : wp_basename( $post->guid ); // title shouldn't ever be empty, but use filename just in cas.e - echo '
' . esc_html( wp_html_excerpt( $title, 60 ) ) . '
'; + echo '
' . esc_html( wp_html_excerpt( $title, 60, '…' ) ) . '
'; break; case 2 : add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2); diff --git a/wp-admin/edit-comments.php b/wp-admin/edit-comments.php index 46476abb0e..889270938b 100644 --- a/wp-admin/edit-comments.php +++ b/wp-admin/edit-comments.php @@ -105,7 +105,7 @@ wp_enqueue_script('admin-comments'); enqueue_comment_hotkeys_js(); if ( $post_id ) - $title = sprintf(__('Comments on “%s”'), wp_html_excerpt(_draft_or_post_title($post_id), 50)); + $title = sprintf( __( 'Comments on “%s”' ), wp_html_excerpt( _draft_or_post_title( $post_id ), 50, '…' ) ); else $title = __('Comments'); @@ -143,17 +143,17 @@ require_once('./admin-header.php');

%s', - get_edit_post_link($post_id), - wp_html_excerpt(_draft_or_post_title($post_id), 50) + echo sprintf( __( 'Comments on “%s”' ), + sprintf( '%s', + get_edit_post_link( $post_id ), + wp_html_excerpt( _draft_or_post_title( $post_id ), 50, '…' ) ) ); else echo __('Comments'); if ( isset($_REQUEST['s']) && $_REQUEST['s'] ) - printf( '' . sprintf( __( 'Search results for “%s”' ), wp_html_excerpt( esc_html( wp_unslash( $_REQUEST['s'] ) ), 50 ) ) . '' ); ?> + printf( '' . sprintf( __( 'Search results for “%s”' ), wp_html_excerpt( esc_html( wp_unslash( $_REQUEST['s'] ) ), 50, '…' ) ) . '' ); ?>

$publisher"; $content = $item->get_content(); - $content = wp_html_excerpt($content, 50) . ' …'; + $content = wp_html_excerpt( $content, 50, ' …' ); if ( $link ) /* translators: incoming links feed, %1$s is other person, %3$s is content */ diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index a033e5822f..4c8412f5bb 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -1159,7 +1159,7 @@ function get_media_item( $attachment_id, $args = null ) { } $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case - $display_title = $show_title ? "
" . wp_html_excerpt( $display_title, 60 ) . "
" : ''; + $display_title = $show_title ? "
" . wp_html_excerpt( $display_title, 60, '…' ) . "
" : ''; $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) ); $order = ''; diff --git a/wp-admin/includes/nav-menu.php b/wp-admin/includes/nav-menu.php index 79ed9d6289..41324f0a5b 100644 --- a/wp-admin/includes/nav-menu.php +++ b/wp-admin/includes/nav-menu.php @@ -515,10 +515,7 @@ function wp_nav_menu_locations_meta_box() { term_id ); ?> - value="term_id; ?>">name, 40 ); - echo $truncated_name == $menu->name ? $menu->name : trim( $truncated_name ) . '…'; - ?> + value="term_id; ?>">name, 40, '…' ); ?> diff --git a/wp-admin/nav-menus.php b/wp-admin/nav-menus.php index bdf7d42382..9da80dcc7a 100644 --- a/wp-admin/nav-menus.php +++ b/wp-admin/nav-menus.php @@ -436,11 +436,7 @@ if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) { // Generate truncated menu names foreach( (array) $nav_menus as $key => $_nav_menu ) { - $_nav_menu->truncated_name = trim( wp_html_excerpt( $_nav_menu->name, 40 ) ); - if ( $_nav_menu->truncated_name != $_nav_menu->name ) - $_nav_menu->truncated_name .= '…'; - - $nav_menus[$key]->truncated_name = $_nav_menu->truncated_name; + $nav_menus[$key]->truncated_name = wp_html_excerpt( $_nav_menu->name, 40, '…' ); } // Retrieve menu locations @@ -572,8 +568,7 @@ require_once( './admin-header.php' ); term_id; ?> diff --git a/wp-includes/admin-bar.php b/wp-includes/admin-bar.php index 7f5cdf4c27..109db6c64c 100644 --- a/wp-includes/admin-bar.php +++ b/wp-includes/admin-bar.php @@ -229,9 +229,7 @@ function wp_admin_bar_site_menu( $wp_admin_bar ) { $blogname = sprintf( __('Global Dashboard: %s'), esc_html( $current_site->site_name ) ); } - $title = wp_html_excerpt( $blogname, 40 ); - if ( $title != $blogname ) - $title = trim( $title ) . '…'; + $title = wp_html_excerpt( $blogname, 40, '…' ); $wp_admin_bar->add_menu( array( 'id' => 'site-name', diff --git a/wp-includes/class-wp-customize-manager.php b/wp-includes/class-wp-customize-manager.php index 1e41e8fafc..c8e78b798d 100644 --- a/wp-includes/class-wp-customize-manager.php +++ b/wp-includes/class-wp-customize-manager.php @@ -897,9 +897,7 @@ final class WP_Customize_Manager { if ( $menus ) { $choices = array( 0 => __( '— Select —' ) ); foreach ( $menus as $menu ) { - $truncated_name = wp_html_excerpt( $menu->name, 40 ); - $truncated_name = ( $truncated_name == $menu->name ) ? $menu->name : trim( $truncated_name ) . '…'; - $choices[ $menu->term_id ] = $truncated_name; + $choices[ $menu->term_id ] = wp_html_excerpt( $menu->name, 40, '…' ); } foreach ( $locations as $location => $description ) { diff --git a/wp-includes/comment.php b/wp-includes/comment.php index fd91b0ec85..16286cf037 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -1753,7 +1753,7 @@ function do_trackbacks($post_id) { else $excerpt = apply_filters('the_excerpt', $post->post_excerpt); $excerpt = str_replace(']]>', ']]>', $excerpt); - $excerpt = wp_html_excerpt($excerpt, 252) . '…'; + $excerpt = wp_html_excerpt($excerpt, 252, '…'); $post_title = apply_filters('the_title', $post->post_title, $post->ID); $post_title = strip_tags($post_title); diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 246c333bdb..a0bf5c6fe0 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -2199,7 +2199,7 @@ function wp_trim_excerpt($text = '') { * * @param string $text Text to trim. * @param int $num_words Number of words. Default 55. - * @param string $more What to append if $text needs to be trimmed. Default '…'. + * @param string $more Optional. What to append if $text needs to be trimmed. Default '…'. * @return string Trimmed text. */ function wp_trim_words( $text, $num_words = 55, $more = null ) { @@ -3118,14 +3118,19 @@ function wp_sprintf_l($pattern, $args) { * * @param integer $str String to get the excerpt from. * @param integer $count Maximum number of characters to take. + * @param string $more Optional. What to append if $str needs to be trimmed. Defaults to empty string. * @return string The excerpt. */ -function wp_html_excerpt( $str, $count ) { +function wp_html_excerpt( $str, $count, $more = null ) { + if ( null === $more ) + $more = ''; $str = wp_strip_all_tags( $str, true ); - $str = mb_substr( $str, 0, $count ); + $excerpt = mb_substr( $str, 0, $count ); // remove part of an entity at the end - $str = preg_replace( '/&[^;\s]{0,6}$/', '', $str ); - return $str; + $excerpt = preg_replace( '/&[^;\s]{0,6}$/', '', $excerpt ); + if ( $str != $excerpt ) + $excerpt = trim( $excerpt ) . $more; + return $excerpt; } /** diff --git a/wp-trackback.php b/wp-trackback.php index ef6439ef2f..8b41505f2b 100644 --- a/wp-trackback.php +++ b/wp-trackback.php @@ -87,8 +87,8 @@ if ( !empty($tb_url) && !empty($title) ) { if ( !pings_open($tb_id) ) trackback_response(1, 'Sorry, trackbacks are closed for this item.'); - $title = wp_html_excerpt( $title, 250 ) . '…'; - $excerpt = wp_html_excerpt( $excerpt, 252 ) . '…'; + $title = wp_html_excerpt( $title, 250, '…' ); + $excerpt = wp_html_excerpt( $excerpt, 252, '…' ); $comment_post_ID = (int) $tb_id; $comment_author = $blog_name;