Skip to content

Commit 200c293

Browse files
Coding Standards: Simplify the initials avatar type handling in get_avatar_data().
Includes: * Converting a long ternary to an `if/elseif` block for better readability. * Reusing an existing translatable string for user's first name and last name. * Replacing `empty()` checks with more specific checks for an empty string. * Replacing `is_object()` check with a check for a `WP_Comment` instance. Follow-up to [53501], [60269]. See #63087. git-svn-id: https://develop.svn.wordpress.org/trunk@60897 602fd350-edb4-49c9-b593-d223f7449a82
1 parent abc5dd8 commit 200c293

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/wp-includes/link-template.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4553,22 +4553,31 @@ function get_avatar_data( $id_or_email, $args = null ) {
45534553
'r' => $args['rating'],
45544554
);
45554555

4556-
// Handle additional parameters for the 'initials' avatar type
4556+
// Handle additional parameters for the 'initials' avatar type.
45574557
if ( 'initials' === $args['default'] ) {
45584558
$name = '';
45594559

45604560
if ( $user ) {
4561-
$name = ! empty( $user->display_name ) ? $user->display_name :
4562-
( ! empty( $user->first_name ) && ! empty( $user->last_name ) ?
4563-
$user->first_name . ' ' . $user->last_name : $user->user_login );
4564-
} elseif ( is_object( $id_or_email ) && isset( $id_or_email->comment_author ) ) {
4561+
if ( '' !== $user->display_name ) {
4562+
$name = $user->display_name;
4563+
} elseif ( '' !== $user->first_name && '' !== $user->last_name ) {
4564+
$name = sprintf(
4565+
/* translators: 1: User's first name, 2: Last name. */
4566+
_x( '%1$s %2$s', 'Display name based on first name and last name' ),
4567+
$user->first_name,
4568+
$user->last_name
4569+
);
4570+
} else {
4571+
$name = $user->user_login;
4572+
}
4573+
} elseif ( $id_or_email instanceof WP_Comment ) {
45654574
$name = $id_or_email->comment_author;
45664575
} elseif ( is_string( $id_or_email ) && false !== strpos( $id_or_email, '@' ) ) {
45674576
$name = str_replace( array( '.', '_', '-' ), ' ', substr( $id_or_email, 0, strpos( $id_or_email, '@' ) ) );
45684577
}
45694578

4570-
if ( ! empty( $name ) ) {
4571-
if ( preg_match( '/\p{Han}|\p{Hiragana}|\p{Katakana}|\p{Hangul}/u', $name ) || false === strpos( $name, ' ' ) ) {
4579+
if ( '' !== $name ) {
4580+
if ( false === strpos( $name, ' ' ) || preg_match( '/\p{Han}|\p{Hiragana}|\p{Katakana}|\p{Hangul}/u', $name ) ) {
45724581
$initials = mb_substr( $name, 0, min( 2, mb_strlen( $name, 'UTF-8' ) ), 'UTF-8' );
45734582
} else {
45744583
$first = mb_substr( $name, 0, 1, 'UTF-8' );

0 commit comments

Comments
 (0)