Make WordPress Core

Changeset 60897


Ignore:
Timestamp:
10/03/2025 11:50:40 PM (5 months ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r60733 r60897  
    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, '@' ) ) {
     
    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 {
Note: See TracChangeset for help on using the changeset viewer.