wp/wp-includes/author-template.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    12 
    12 
    13 /**
    13 /**
    14  * Retrieves the author of the current post.
    14  * Retrieves the author of the current post.
    15  *
    15  *
    16  * @since 1.5.0
    16  * @since 1.5.0
       
    17  * @since 6.3.0 Returns an empty string if the author's display name is unknown.
    17  *
    18  *
    18  * @global WP_User $authordata The current author's data.
    19  * @global WP_User $authordata The current author's data.
    19  *
    20  *
    20  * @param string $deprecated Deprecated.
    21  * @param string $deprecated Deprecated.
    21  * @return string|null The author's display name.
    22  * @return string The author's display name, empty string if unknown.
    22  */
    23  */
    23 function get_the_author( $deprecated = '' ) {
    24 function get_the_author( $deprecated = '' ) {
    24 	global $authordata;
    25 	global $authordata;
    25 
    26 
    26 	if ( ! empty( $deprecated ) ) {
    27 	if ( ! empty( $deprecated ) ) {
    30 	/**
    31 	/**
    31 	 * Filters the display name of the current post's author.
    32 	 * Filters the display name of the current post's author.
    32 	 *
    33 	 *
    33 	 * @since 2.9.0
    34 	 * @since 2.9.0
    34 	 *
    35 	 *
    35 	 * @param string|null $display_name The author's display name.
    36 	 * @param string $display_name The author's display name.
    36 	 */
    37 	 */
    37 	return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null );
    38 	return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : '' );
    38 }
    39 }
    39 
    40 
    40 /**
    41 /**
    41  * Displays the name of the author of the current post.
    42  * Displays the name of the author of the current post.
    42  *
    43  *
    53  * @see get_the_author()
    54  * @see get_the_author()
    54  * @link https://developer.wordpress.org/reference/functions/the_author/
    55  * @link https://developer.wordpress.org/reference/functions/the_author/
    55  *
    56  *
    56  * @param string $deprecated      Deprecated.
    57  * @param string $deprecated      Deprecated.
    57  * @param bool   $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
    58  * @param bool   $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
    58  * @return string|null The author's display name, from get_the_author().
    59  * @return string The author's display name, from get_the_author().
    59  */
    60  */
    60 function the_author( $deprecated = '', $deprecated_echo = true ) {
    61 function the_author( $deprecated = '', $deprecated_echo = true ) {
    61 	if ( ! empty( $deprecated ) ) {
    62 	if ( ! empty( $deprecated ) ) {
    62 		_deprecated_argument( __FUNCTION__, '2.1.0' );
    63 		_deprecated_argument( __FUNCTION__, '2.1.0' );
    63 	}
    64 	}
   153  * @since 2.8.0
   154  * @since 2.8.0
   154  *
   155  *
   155  * @global WP_User $authordata The current author's data.
   156  * @global WP_User $authordata The current author's data.
   156  *
   157  *
   157  * @param string    $field   Optional. The user field to retrieve. Default empty.
   158  * @param string    $field   Optional. The user field to retrieve. Default empty.
   158  * @param int|false $user_id Optional. User ID.
   159  * @param int|false $user_id Optional. User ID. Defaults to the current post author.
   159  * @return string The author's field from the current author's DB object, otherwise an empty string.
   160  * @return string The author's field from the current author's DB object, otherwise an empty string.
   160  */
   161  */
   161 function get_the_author_meta( $field = '', $user_id = false ) {
   162 function get_the_author_meta( $field = '', $user_id = false ) {
   162 	$original_user_id = $user_id;
   163 	$original_user_id = $user_id;
   163 
   164 
   194  *
   195  *
   195  * @since 2.8.0
   196  * @since 2.8.0
   196  *
   197  *
   197  * @param string    $field   Selects the field of the users record. See get_the_author_meta()
   198  * @param string    $field   Selects the field of the users record. See get_the_author_meta()
   198  *                           for the list of possible fields.
   199  *                           for the list of possible fields.
   199  * @param int|false $user_id Optional. User ID.
   200  * @param int|false $user_id Optional. User ID. Defaults to the current post author.
   200  *
   201  *
   201  * @see get_the_author_meta()
   202  * @see get_the_author_meta()
   202  */
   203  */
   203 function the_author_meta( $field = '', $user_id = false ) {
   204 function the_author_meta( $field = '', $user_id = false ) {
   204 	$author_meta = get_the_author_meta( $field, $user_id );
   205 	$author_meta = get_the_author_meta( $field, $user_id );
   217 }
   218 }
   218 
   219 
   219 /**
   220 /**
   220  * Retrieves either author's link or author's name.
   221  * Retrieves either author's link or author's name.
   221  *
   222  *
   222  * If the author has a home page set, return an HTML link, otherwise just return the
   223  * If the author has a home page set, return an HTML link, otherwise just return
   223  * author's name.
   224  * the author's name.
   224  *
   225  *
   225  * @since 3.0.0
   226  * @since 3.0.0
   226  *
   227  *
   227  * @global WP_User $authordata The current author's data.
   228  * @global WP_User $authordata The current author's data.
   228  *
   229  *
   229  * @return string|null An HTML link if the author's url exist in user meta,
   230  * @return string An HTML link if the author's URL exists in user meta,
   230  *                     else the result of get_the_author().
   231  *                otherwise the result of get_the_author().
   231  */
   232  */
   232 function get_the_author_link() {
   233 function get_the_author_link() {
   233 	if ( get_the_author_meta( 'url' ) ) {
   234 	if ( get_the_author_meta( 'url' ) ) {
   234 		global $authordata;
   235 		global $authordata;
   235 
   236 
   305  *
   306  *
   306  * @since 4.4.0
   307  * @since 4.4.0
   307  *
   308  *
   308  * @global WP_User $authordata The current author's data.
   309  * @global WP_User $authordata The current author's data.
   309  *
   310  *
   310  * @return string An HTML link to the author page, or an empty string if $authordata isn't defined.
   311  * @return string An HTML link to the author page, or an empty string if $authordata is not set.
   311  */
   312  */
   312 function get_the_author_posts_link() {
   313 function get_the_author_posts_link() {
   313 	global $authordata;
   314 	global $authordata;
       
   315 
   314 	if ( ! is_object( $authordata ) ) {
   316 	if ( ! is_object( $authordata ) ) {
   315 		return '';
   317 		return '';
   316 	}
   318 	}
   317 
   319 
   318 	$link = sprintf(
   320 	$link = sprintf(
   448 		'html'          => true,
   450 		'html'          => true,
   449 		'exclude'       => '',
   451 		'exclude'       => '',
   450 		'include'       => '',
   452 		'include'       => '',
   451 	);
   453 	);
   452 
   454 
   453 	$args = wp_parse_args( $args, $defaults );
   455 	$parsed_args = wp_parse_args( $args, $defaults );
   454 
   456 
   455 	$return = '';
   457 	$return = '';
   456 
   458 
   457 	$query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
   459 	$query_args           = wp_array_slice_assoc( $parsed_args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
   458 	$query_args['fields'] = 'ids';
   460 	$query_args['fields'] = 'ids';
   459 	$authors              = get_users( $query_args );
   461 
   460 
   462 	/**
   461 	$author_count = array();
   463 	 * Filters the query arguments for the list of all authors of the site.
   462 	foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . ' GROUP BY post_author' ) as $row ) {
   464 	 *
   463 		$author_count[ $row->post_author ] = $row->count;
   465 	 * @since 6.1.0
   464 	}
   466 	 *
       
   467 	 * @param array $query_args  The query arguments for get_users().
       
   468 	 * @param array $parsed_args The arguments passed to wp_list_authors() combined with the defaults.
       
   469 	 */
       
   470 	$query_args = apply_filters( 'wp_list_authors_args', $query_args, $parsed_args );
       
   471 
       
   472 	$authors     = get_users( $query_args );
       
   473 	$post_counts = array();
       
   474 
       
   475 	/**
       
   476 	 * Filters whether to short-circuit performing the query for author post counts.
       
   477 	 *
       
   478 	 * @since 6.1.0
       
   479 	 *
       
   480 	 * @param int[]|false $post_counts Array of post counts, keyed by author ID.
       
   481 	 * @param array       $parsed_args The arguments passed to wp_list_authors() combined with the defaults.
       
   482 	 */
       
   483 	$post_counts = apply_filters( 'pre_wp_list_authors_post_counts_query', false, $parsed_args );
       
   484 
       
   485 	if ( ! is_array( $post_counts ) ) {
       
   486 		$post_counts       = array();
       
   487 		$post_counts_query = $wpdb->get_results(
       
   488 			"SELECT DISTINCT post_author, COUNT(ID) AS count
       
   489 			FROM $wpdb->posts
       
   490 			WHERE " . get_private_posts_cap_sql( 'post' ) . '
       
   491 			GROUP BY post_author'
       
   492 		);
       
   493 
       
   494 		foreach ( (array) $post_counts_query as $row ) {
       
   495 			$post_counts[ $row->post_author ] = $row->count;
       
   496 		}
       
   497 	}
       
   498 
   465 	foreach ( $authors as $author_id ) {
   499 	foreach ( $authors as $author_id ) {
   466 		$posts = isset( $author_count[ $author_id ] ) ? $author_count[ $author_id ] : 0;
   500 		$posts = isset( $post_counts[ $author_id ] ) ? $post_counts[ $author_id ] : 0;
   467 
   501 
   468 		if ( ! $posts && $args['hide_empty'] ) {
   502 		if ( ! $posts && $parsed_args['hide_empty'] ) {
   469 			continue;
   503 			continue;
   470 		}
   504 		}
   471 
   505 
   472 		$author = get_userdata( $author_id );
   506 		$author = get_userdata( $author_id );
   473 
   507 
   474 		if ( $args['exclude_admin'] && 'admin' === $author->display_name ) {
   508 		if ( $parsed_args['exclude_admin'] && 'admin' === $author->display_name ) {
   475 			continue;
   509 			continue;
   476 		}
   510 		}
   477 
   511 
   478 		if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
   512 		if ( $parsed_args['show_fullname'] && $author->first_name && $author->last_name ) {
   479 			$name = "$author->first_name $author->last_name";
   513 			$name = sprintf(
       
   514 				/* translators: 1: User's first name, 2: Last name. */
       
   515 				_x( '%1$s %2$s', 'Display name based on first name and last name' ),
       
   516 				$author->first_name,
       
   517 				$author->last_name
       
   518 			);
   480 		} else {
   519 		} else {
   481 			$name = $author->display_name;
   520 			$name = $author->display_name;
   482 		}
   521 		}
   483 
   522 
   484 		if ( ! $args['html'] ) {
   523 		if ( ! $parsed_args['html'] ) {
   485 			$return .= $name . ', ';
   524 			$return .= $name . ', ';
   486 
   525 
   487 			continue; // No need to go further to process HTML.
   526 			continue; // No need to go further to process HTML.
   488 		}
   527 		}
   489 
   528 
   490 		if ( 'list' === $args['style'] ) {
   529 		if ( 'list' === $parsed_args['style'] ) {
   491 			$return .= '<li>';
   530 			$return .= '<li>';
   492 		}
   531 		}
   493 
   532 
   494 		$link = sprintf(
   533 		$link = sprintf(
   495 			'<a href="%1$s" title="%2$s">%3$s</a>',
   534 			'<a href="%1$s" title="%2$s">%3$s</a>',
   497 			/* translators: %s: Author's display name. */
   536 			/* translators: %s: Author's display name. */
   498 			esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
   537 			esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
   499 			$name
   538 			$name
   500 		);
   539 		);
   501 
   540 
   502 		if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
   541 		if ( ! empty( $parsed_args['feed_image'] ) || ! empty( $parsed_args['feed'] ) ) {
   503 			$link .= ' ';
   542 			$link .= ' ';
   504 			if ( empty( $args['feed_image'] ) ) {
   543 			if ( empty( $parsed_args['feed_image'] ) ) {
   505 				$link .= '(';
   544 				$link .= '(';
   506 			}
   545 			}
   507 
   546 
   508 			$link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
   547 			$link .= '<a href="' . get_author_feed_link( $author->ID, $parsed_args['feed_type'] ) . '"';
   509 
   548 
   510 			$alt = '';
   549 			$alt = '';
   511 			if ( ! empty( $args['feed'] ) ) {
   550 			if ( ! empty( $parsed_args['feed'] ) ) {
   512 				$alt  = ' alt="' . esc_attr( $args['feed'] ) . '"';
   551 				$alt  = ' alt="' . esc_attr( $parsed_args['feed'] ) . '"';
   513 				$name = $args['feed'];
   552 				$name = $parsed_args['feed'];
   514 			}
   553 			}
   515 
   554 
   516 			$link .= '>';
   555 			$link .= '>';
   517 
   556 
   518 			if ( ! empty( $args['feed_image'] ) ) {
   557 			if ( ! empty( $parsed_args['feed_image'] ) ) {
   519 				$link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
   558 				$link .= '<img src="' . esc_url( $parsed_args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
   520 			} else {
   559 			} else {
   521 				$link .= $name;
   560 				$link .= $name;
   522 			}
   561 			}
   523 
   562 
   524 			$link .= '</a>';
   563 			$link .= '</a>';
   525 
   564 
   526 			if ( empty( $args['feed_image'] ) ) {
   565 			if ( empty( $parsed_args['feed_image'] ) ) {
   527 				$link .= ')';
   566 				$link .= ')';
   528 			}
   567 			}
   529 		}
   568 		}
   530 
   569 
   531 		if ( $args['optioncount'] ) {
   570 		if ( $parsed_args['optioncount'] ) {
   532 			$link .= ' (' . $posts . ')';
   571 			$link .= ' (' . $posts . ')';
   533 		}
   572 		}
   534 
   573 
   535 		$return .= $link;
   574 		$return .= $link;
   536 		$return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
   575 		$return .= ( 'list' === $parsed_args['style'] ) ? '</li>' : ', ';
   537 	}
   576 	}
   538 
   577 
   539 	$return = rtrim( $return, ', ' );
   578 	$return = rtrim( $return, ', ' );
   540 
   579 
   541 	if ( $args['echo'] ) {
   580 	if ( $parsed_args['echo'] ) {
   542 		echo $return;
   581 		echo $return;
   543 	} else {
   582 	} else {
   544 		return $return;
   583 		return $return;
   545 	}
   584 	}
   546 }
   585 }