ap_user_display_name( WP_Comment|array|integer $args = array() )

Description #

Return or echo user display name.

Get display name from comments if WP_Comment object is passed. Else fetch name form user profile. If anonymous user then fetch name from current question, answer or comment.

Parameters #

  • $args
    WP_Comment | array | integer (Optional) Arguments or WP_Comment or user ID.
    • 'user_id'
      (integer) User ID.
    • 'html'
      (boolean) Shall return just text name or name with html markup.
    • 'echo'
      (boolean) Return or echo.
    • 'anonymous_label'
      (string) A placeholder name for anonymous user if no name found in post or comment.
    Default value: array()

Changelog #

VersionDescription
4.1.2Improved args and PHPDoc.
0.1Introduced.

Source #

File: includes/functions.php

1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
function ap_user_display_name( $args = array() ) {
    global $post;
 
    $defaults = array(
        'user_id'         => get_the_author_meta( 'ID' ),
        'html'            => false,
        'echo'            => false,
        'anonymous_label' => __( 'Anonymous', 'anspress-question-answer' ),
    );
 
    // When only user id passed.
    if ( is_numeric( $args ) ) {
        $defaults['user_id'] = $args;
        $args                = $defaults;
    } elseif ( $args instanceof WP_Comment ) {
        $defaults['user_id']         = $args->user_id;
        $defaults['anonymous_label'] = $args->comment_author;
        $args                        = $defaults;
    } else {
        $args = wp_parse_args( $args, $defaults );
    }
 
    extract( $args ); // @codingStandardsIgnoreLine
 
    $user = get_userdata( $user_id );
 
    if ( $user ) {
        $return = ! $html ? $user->display_name : '<a href="' . esc_url( ap_user_link( $user_id ) ) . '" itemprop="url"><span itemprop="name">' . esc_html( $user->display_name ) . '</span></a>';
    } elseif ( $post && in_array( $post->post_type, array( 'question', 'answer' ), true ) ) {
        $post_fields = ap_get_post_field( 'fields' );
 
        if ( ! $html ) {
            if ( is_array( $post_fields ) && ! empty( $post_fields['anonymous_name'] ) ) {
                $return = $post_fields['anonymous_name'];
            } else {
                $return = $anonymous_label;
            }
        } elseif ( is_array( $post_fields ) && ! empty( $post_fields['anonymous_name'] ) ) {
                $return = $post_fields['anonymous_name'] . esc_attr__( ' (anonymous)', 'anspress-question-answer' );
        } else {
            $return = $anonymous_label;
        }
    } elseif ( ! $html ) {
            $return = $anonymous_label;
    } else {
        $return = $anonymous_label;
    }
 
    /**
     * Filter AnsPress user display name.
     *
     * Filter can be used to alter user display name or
     * appending some extra information of user, like: rank, reputation etc.
     * Make sure to return plain text when `$args['html']` is true.
     *
     * @param string $return Name of user to return.
     * @param array  $args   Arguments.
     *
     * @since 2.0.1
     */
    $return = apply_filters( 'ap_user_display_name', $return, $args );
 
    if ( ! $args['echo'] ) {
        return $return;
    }
 
    echo $return; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment