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

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