wp/wp-includes/author-template.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    13 /**
    13 /**
    14  * Retrieve the author of the current post.
    14  * Retrieve the author of the current post.
    15  *
    15  *
    16  * @since 1.5.0
    16  * @since 1.5.0
    17  *
    17  *
    18  * @global object $authordata The current author's DB object.
    18  * @global WP_User $authordata The current author's data.
    19  *
    19  *
    20  * @param string $deprecated Deprecated.
    20  * @param string $deprecated Deprecated.
    21  * @return string|null The author's display name.
    21  * @return string|null The author's display name.
    22  */
    22  */
    23 function get_the_author( $deprecated = '' ) {
    23 function get_the_author( $deprecated = '' ) {
    30 	/**
    30 	/**
    31 	 * Filters the display name of the current post's author.
    31 	 * Filters the display name of the current post's author.
    32 	 *
    32 	 *
    33 	 * @since 2.9.0
    33 	 * @since 2.9.0
    34 	 *
    34 	 *
    35 	 * @param string $authordata->display_name The author's display name.
    35 	 * @param string|null $display_name The author's display name.
    36 	 */
    36 	 */
    37 	return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null );
    37 	return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null );
    38 }
    38 }
    39 
    39 
    40 /**
    40 /**
    47  *
    47  *
    48  * The normal, expected behavior of this function is to echo the author and not
    48  * The normal, expected behavior of this function is to echo the author and not
    49  * return it. However, backward compatibility has to be maintained.
    49  * return it. However, backward compatibility has to be maintained.
    50  *
    50  *
    51  * @since 0.71
    51  * @since 0.71
       
    52  *
    52  * @see get_the_author()
    53  * @see get_the_author()
    53  * @link https://codex.wordpress.org/Template_Tags/the_author
    54  * @link https://developer.wordpress.org/reference/functions/the_author/
    54  *
    55  *
    55  * @param string $deprecated      Deprecated.
    56  * @param string $deprecated      Deprecated.
    56  * @param bool   $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
    57  * @param bool   $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
    57  * @return string|null The author's display name, from get_the_author().
    58  * @return string|null The author's display name, from get_the_author().
    58  */
    59  */
    63 
    64 
    64 	if ( true !== $deprecated_echo ) {
    65 	if ( true !== $deprecated_echo ) {
    65 		_deprecated_argument(
    66 		_deprecated_argument(
    66 			__FUNCTION__,
    67 			__FUNCTION__,
    67 			'1.5.0',
    68 			'1.5.0',
    68 			/* translators: %s: get_the_author() */
       
    69 			sprintf(
    69 			sprintf(
       
    70 				/* translators: %s: get_the_author() */
    70 				__( 'Use %s instead if you do not want the value echoed.' ),
    71 				__( 'Use %s instead if you do not want the value echoed.' ),
    71 				'<code>get_the_author()</code>'
    72 				'<code>get_the_author()</code>'
    72 			)
    73 			)
    73 		);
    74 		);
    74 	}
    75 	}
    96 		/**
    97 		/**
    97 		 * Filters the display name of the author who last edited the current post.
    98 		 * Filters the display name of the author who last edited the current post.
    98 		 *
    99 		 *
    99 		 * @since 2.8.0
   100 		 * @since 2.8.0
   100 		 *
   101 		 *
   101 		 * @param string $last_user->display_name The author's display name.
   102 		 * @param string $display_name The author's display name.
   102 		 */
   103 		 */
   103 		return apply_filters( 'the_modified_author', $last_user->display_name );
   104 		return apply_filters( 'the_modified_author', $last_user->display_name );
   104 	}
   105 	}
   105 }
   106 }
   106 
   107 
   149  * - user_url
   150  * - user_url
   150  * - yim
   151  * - yim
   151  *
   152  *
   152  * @since 2.8.0
   153  * @since 2.8.0
   153  *
   154  *
   154  * @global object $authordata The current author's DB object.
   155  * @global WP_User $authordata The current author's data.
   155  *
   156  *
   156  * @param string    $field   Optional. The user field to retrieve. Default empty.
   157  * @param string    $field   Optional. The user field to retrieve. Default empty.
   157  * @param int|false $user_id Optional. User ID.
   158  * @param int|false $user_id Optional. User ID.
   158  * @return string The author's field from the current author's DB object, otherwise an empty string.
   159  * @return string The author's field from the current author's DB object, otherwise an empty string.
   159  */
   160  */
   165 		$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
   166 		$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
   166 	} else {
   167 	} else {
   167 		$authordata = get_userdata( $user_id );
   168 		$authordata = get_userdata( $user_id );
   168 	}
   169 	}
   169 
   170 
   170 	if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) ) {
   171 	if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ), true ) ) {
   171 		$field = 'user_' . $field;
   172 		$field = 'user_' . $field;
   172 	}
   173 	}
   173 
   174 
   174 	$value = isset( $authordata->$field ) ? $authordata->$field : '';
   175 	$value = isset( $authordata->$field ) ? $authordata->$field : '';
   175 
   176 
   229 function get_the_author_link() {
   230 function get_the_author_link() {
   230 	if ( get_the_author_meta( 'url' ) ) {
   231 	if ( get_the_author_meta( 'url' ) ) {
   231 		return sprintf(
   232 		return sprintf(
   232 			'<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
   233 			'<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
   233 			esc_url( get_the_author_meta( 'url' ) ),
   234 			esc_url( get_the_author_meta( 'url' ) ),
   234 			/* translators: %s: author's display name */
   235 			/* translators: %s: Author's display name. */
   235 			esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
   236 			esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
   236 			get_the_author()
   237 			get_the_author()
   237 		);
   238 		);
   238 	} else {
   239 	} else {
   239 		return get_the_author();
   240 		return get_the_author();
   244  * Display either author's link or author's name.
   245  * Display either author's link or author's name.
   245  *
   246  *
   246  * If the author has a home page set, echo an HTML link, otherwise just echo the
   247  * If the author has a home page set, echo an HTML link, otherwise just echo the
   247  * author's name.
   248  * author's name.
   248  *
   249  *
   249  * @link https://codex.wordpress.org/Template_Tags/the_author_link
   250  * @link https://developer.wordpress.org/reference/functions/the_author_link/
   250  *
   251  *
   251  * @since 2.1.0
   252  * @since 2.1.0
   252  */
   253  */
   253 function the_author_link() {
   254 function the_author_link() {
   254 	echo get_the_author_link();
   255 	echo get_the_author_link();
   270 }
   271 }
   271 
   272 
   272 /**
   273 /**
   273  * Display the number of posts by the author of the current post.
   274  * Display the number of posts by the author of the current post.
   274  *
   275  *
   275  * @link https://codex.wordpress.org/Template_Tags/the_author_posts
   276  * @link https://developer.wordpress.org/reference/functions/the_author_posts/
   276  * @since 0.71
   277  * @since 0.71
   277  */
   278  */
   278 function the_author_posts() {
   279 function the_author_posts() {
   279 	echo get_the_author_posts();
   280 	echo get_the_author_posts();
   280 }
   281 }
   284  *
   285  *
   285  * Returns an HTML-formatted link using get_author_posts_url().
   286  * Returns an HTML-formatted link using get_author_posts_url().
   286  *
   287  *
   287  * @since 4.4.0
   288  * @since 4.4.0
   288  *
   289  *
   289  * @global object $authordata The current author's DB object.
   290  * @global WP_User $authordata The current author's data.
   290  *
   291  *
   291  * @return string An HTML link to the author page, or an empty string if $authordata isn't defined.
   292  * @return string An HTML link to the author page, or an empty string if $authordata isn't defined.
   292  */
   293  */
   293 function get_the_author_posts_link() {
   294 function get_the_author_posts_link() {
   294 	global $authordata;
   295 	global $authordata;
   297 	}
   298 	}
   298 
   299 
   299 	$link = sprintf(
   300 	$link = sprintf(
   300 		'<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
   301 		'<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
   301 		esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
   302 		esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
   302 		/* translators: %s: author's display name */
   303 		/* translators: %s: Author's display name. */
   303 		esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
   304 		esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
   304 		get_the_author()
   305 		get_the_author()
   305 	);
   306 	);
   306 
   307 
   307 	/**
   308 	/**
   332 /**
   333 /**
   333  * Retrieve the URL to the author page for the user with the ID provided.
   334  * Retrieve the URL to the author page for the user with the ID provided.
   334  *
   335  *
   335  * @since 2.1.0
   336  * @since 2.1.0
   336  *
   337  *
   337  * @global WP_Rewrite $wp_rewrite
   338  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   338  *
   339  *
   339  * @param int    $author_id       Author ID.
   340  * @param int    $author_id       Author ID.
   340  * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
   341  * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
   341  * @return string The URL to the author's page.
   342  * @return string The URL to the author's page.
   342  */
   343  */
   347 
   348 
   348 	if ( empty( $link ) ) {
   349 	if ( empty( $link ) ) {
   349 		$file = home_url( '/' );
   350 		$file = home_url( '/' );
   350 		$link = $file . '?author=' . $auth_ID;
   351 		$link = $file . '?author=' . $auth_ID;
   351 	} else {
   352 	} else {
   352 		if ( '' == $author_nicename ) {
   353 		if ( '' === $author_nicename ) {
   353 			$user = get_userdata( $author_id );
   354 			$user = get_userdata( $author_id );
   354 			if ( ! empty( $user->user_nicename ) ) {
   355 			if ( ! empty( $user->user_nicename ) ) {
   355 				$author_nicename = $user->user_nicename;
   356 				$author_nicename = $user->user_nicename;
   356 			}
   357 			}
   357 		}
   358 		}
   363 	 * Filters the URL to the author's page.
   364 	 * Filters the URL to the author's page.
   364 	 *
   365 	 *
   365 	 * @since 2.1.0
   366 	 * @since 2.1.0
   366 	 *
   367 	 *
   367 	 * @param string $link            The URL to the author's page.
   368 	 * @param string $link            The URL to the author's page.
   368 	 * @param int    $author_id       The author's id.
   369 	 * @param int    $author_id       The author's ID.
   369 	 * @param string $author_nicename The author's nice name.
   370 	 * @param string $author_nicename The author's nice name.
   370 	 */
   371 	 */
   371 	$link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
   372 	$link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
   372 
   373 
   373 	return $link;
   374 	return $link;
   374 }
   375 }
   375 
   376 
   376 /**
   377 /**
   377  * List all the authors of the site, with several options available.
   378  * List all the authors of the site, with several options available.
   378  *
   379  *
   379  * @link https://codex.wordpress.org/Template_Tags/wp_list_authors
   380  * @link https://developer.wordpress.org/reference/functions/wp_list_authors/
   380  *
   381  *
   381  * @since 1.2.0
   382  * @since 1.2.0
   382  *
   383  *
   383  * @global wpdb $wpdb WordPress database abstraction object.
   384  * @global wpdb $wpdb WordPress database abstraction object.
   384  *
   385  *
   396  *     @type bool         $hide_empty    Whether to hide any authors with no posts. Default true.
   397  *     @type bool         $hide_empty    Whether to hide any authors with no posts. Default true.
   397  *     @type string       $feed          If not empty, show a link to the author's feed and use this text as the alt
   398  *     @type string       $feed          If not empty, show a link to the author's feed and use this text as the alt
   398  *                                       parameter of the link. Default empty.
   399  *                                       parameter of the link. Default empty.
   399  *     @type string       $feed_image    If not empty, show a link to the author's feed and use this image URL as
   400  *     @type string       $feed_image    If not empty, show a link to the author's feed and use this image URL as
   400  *                                       clickable anchor. Default empty.
   401  *                                       clickable anchor. Default empty.
   401  *     @type string       $feed_type     The feed type to link to, such as 'rss2'. Defaults to default feed type.
   402  *     @type string       $feed_type     The feed type to link to. Possible values include 'rss2', 'atom'.
       
   403  *                                       Default is the value of get_default_feed().
   402  *     @type bool         $echo          Whether to output the result or instead return it. Default true.
   404  *     @type bool         $echo          Whether to output the result or instead return it. Default true.
   403  *     @type string       $style         If 'list', each author is wrapped in an `<li>` element, otherwise the authors
   405  *     @type string       $style         If 'list', each author is wrapped in an `<li>` element, otherwise the authors
   404  *                                       will be separated by commas.
   406  *                                       will be separated by commas.
   405  *     @type bool         $html          Whether to list the items in HTML form or plaintext. Default true.
   407  *     @type bool         $html          Whether to list the items in HTML form or plaintext. Default true.
   406  *     @type array|string $exclude       Array or comma/space-separated list of author IDs to exclude. Default empty.
   408  *     @type array|string $exclude       Array or comma/space-separated list of author IDs to exclude. Default empty.
   407  *     @type array|string $include       Array or comma/space-separated list of author IDs to include. Default empty.
   409  *     @type array|string $include       Array or comma/space-separated list of author IDs to include. Default empty.
   408  * }
   410  * }
   409  * @return string|void The output, if echo is set to false.
   411  * @return void|string Void if 'echo' argument is true, list of authors if 'echo' is false.
   410  */
   412  */
   411 function wp_list_authors( $args = '' ) {
   413 function wp_list_authors( $args = '' ) {
   412 	global $wpdb;
   414 	global $wpdb;
   413 
   415 
   414 	$defaults = array(
   416 	$defaults = array(
   464 			$return .= $name . ', ';
   466 			$return .= $name . ', ';
   465 
   467 
   466 			continue; // No need to go further to process HTML.
   468 			continue; // No need to go further to process HTML.
   467 		}
   469 		}
   468 
   470 
   469 		if ( 'list' == $args['style'] ) {
   471 		if ( 'list' === $args['style'] ) {
   470 			$return .= '<li>';
   472 			$return .= '<li>';
   471 		}
   473 		}
   472 
   474 
   473 		$link = sprintf(
   475 		$link = sprintf(
   474 			'<a href="%1$s" title="%2$s">%3$s</a>',
   476 			'<a href="%1$s" title="%2$s">%3$s</a>',
   475 			get_author_posts_url( $author->ID, $author->user_nicename ),
   477 			get_author_posts_url( $author->ID, $author->user_nicename ),
   476 			/* translators: %s: author's display name */
   478 			/* translators: %s: Author's display name. */
   477 			esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
   479 			esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
   478 			$name
   480 			$name
   479 		);
   481 		);
   480 
   482 
   481 		if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
   483 		if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
   510 		if ( $args['optioncount'] ) {
   512 		if ( $args['optioncount'] ) {
   511 			$link .= ' (' . $posts . ')';
   513 			$link .= ' (' . $posts . ')';
   512 		}
   514 		}
   513 
   515 
   514 		$return .= $link;
   516 		$return .= $link;
   515 		$return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
   517 		$return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
   516 	}
   518 	}
   517 
   519 
   518 	$return = rtrim( $return, ', ' );
   520 	$return = rtrim( $return, ', ' );
   519 
   521 
   520 	if ( ! $args['echo'] ) {
   522 	if ( $args['echo'] ) {
       
   523 		echo $return;
       
   524 	} else {
   521 		return $return;
   525 		return $return;
   522 	}
   526 	}
   523 	echo $return;
       
   524 }
   527 }
   525 
   528 
   526 /**
   529 /**
   527  * Determines whether this site has more than one author.
   530  * Determines whether this site has more than one author.
   528  *
   531  *
   539  * @return bool Whether or not we have more than one author
   542  * @return bool Whether or not we have more than one author
   540  */
   543  */
   541 function is_multi_author() {
   544 function is_multi_author() {
   542 	global $wpdb;
   545 	global $wpdb;
   543 
   546 
   544 	if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
   547 	$is_multi_author = get_transient( 'is_multi_author' );
       
   548 	if ( false === $is_multi_author ) {
   545 		$rows            = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" );
   549 		$rows            = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" );
   546 		$is_multi_author = 1 < count( $rows ) ? 1 : 0;
   550 		$is_multi_author = 1 < count( $rows ) ? 1 : 0;
   547 		set_transient( 'is_multi_author', $is_multi_author );
   551 		set_transient( 'is_multi_author', $is_multi_author );
   548 	}
   552 	}
   549 
   553 
   561  * Helper function to clear the cache for number of authors.
   565  * Helper function to clear the cache for number of authors.
   562  *
   566  *
   563  * @since 3.2.0
   567  * @since 3.2.0
   564  * @access private
   568  * @access private
   565  */
   569  */
   566 function __clear_multi_author_cache() {
   570 function __clear_multi_author_cache() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
   567 	delete_transient( 'is_multi_author' );
   571 	delete_transient( 'is_multi_author' );
   568 }
   572 }