wp/wp-includes/author-template.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    18  * @global object $authordata The current author's DB object.
    18  * @global object $authordata The current author's DB object.
    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 = '' ) {
    24 	global $authordata;
    24 	global $authordata;
    25 
    25 
    26 	if ( !empty( $deprecated ) )
    26 	if ( ! empty( $deprecated ) ) {
    27 		_deprecated_argument( __FUNCTION__, '2.1.0' );
    27 		_deprecated_argument( __FUNCTION__, '2.1.0' );
       
    28 	}
    28 
    29 
    29 	/**
    30 	/**
    30 	 * Filters the display name of the current post's author.
    31 	 * Filters the display name of the current post's author.
    31 	 *
    32 	 *
    32 	 * @since 2.9.0
    33 	 * @since 2.9.0
    33 	 *
    34 	 *
    34 	 * @param string $authordata->display_name The author's display name.
    35 	 * @param string $authordata->display_name The author's display name.
    35 	 */
    36 	 */
    36 	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 );
    37 }
    38 }
    38 
    39 
    39 /**
    40 /**
    40  * Display the name of the author of the current post.
    41  * Display the name of the author of the current post.
    41  *
    42  *
    49  *
    50  *
    50  * @since 0.71
    51  * @since 0.71
    51  * @see get_the_author()
    52  * @see get_the_author()
    52  * @link https://codex.wordpress.org/Template_Tags/the_author
    53  * @link https://codex.wordpress.org/Template_Tags/the_author
    53  *
    54  *
    54  * @param string $deprecated Deprecated.
    55  * @param string $deprecated      Deprecated.
    55  * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
    56  * @param bool   $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
    56  * @return string|null The author's display name, from get_the_author().
    57  * @return string|null The author's display name, from get_the_author().
    57  */
    58  */
    58 function the_author( $deprecated = '', $deprecated_echo = true ) {
    59 function the_author( $deprecated = '', $deprecated_echo = true ) {
    59 	if ( ! empty( $deprecated ) ) {
    60 	if ( ! empty( $deprecated ) ) {
    60 		_deprecated_argument( __FUNCTION__, '2.1.0' );
    61 		_deprecated_argument( __FUNCTION__, '2.1.0' );
    61 	}
    62 	}
    62 
    63 
    63 	if ( true !== $deprecated_echo ) {
    64 	if ( true !== $deprecated_echo ) {
    64 		_deprecated_argument( __FUNCTION__, '1.5.0',
    65 		_deprecated_argument(
       
    66 			__FUNCTION__,
       
    67 			'1.5.0',
    65 			/* translators: %s: get_the_author() */
    68 			/* translators: %s: get_the_author() */
    66 			sprintf( __( 'Use %s instead if you do not want the value echoed.' ),
    69 			sprintf(
       
    70 				__( 'Use %s instead if you do not want the value echoed.' ),
    67 				'<code>get_the_author()</code>'
    71 				'<code>get_the_author()</code>'
    68 			)
    72 			)
    69 		);
    73 		);
    70 	}
    74 	}
    71 
    75 
    82  * @since 2.8.0
    86  * @since 2.8.0
    83  *
    87  *
    84  * @return string|void The author's display name.
    88  * @return string|void The author's display name.
    85  */
    89  */
    86 function get_the_modified_author() {
    90 function get_the_modified_author() {
    87 	if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
    91 	$last_id = get_post_meta( get_post()->ID, '_edit_last', true );
    88 		$last_user = get_userdata($last_id);
    92 
       
    93 	if ( $last_id ) {
       
    94 		$last_user = get_userdata( $last_id );
    89 
    95 
    90 		/**
    96 		/**
    91 		 * Filters the display name of the author who last edited the current post.
    97 		 * Filters the display name of the author who last edited the current post.
    92 		 *
    98 		 *
    93 		 * @since 2.8.0
    99 		 * @since 2.8.0
    94 		 *
   100 		 *
    95 		 * @param string $last_user->display_name The author's display name.
   101 		 * @param string $last_user->display_name The author's display name.
    96 		 */
   102 		 */
    97 		return apply_filters('the_modified_author', $last_user->display_name);
   103 		return apply_filters( 'the_modified_author', $last_user->display_name );
    98 	}
   104 	}
    99 }
   105 }
   100 
   106 
   101 /**
   107 /**
   102  * Display the name of the author who last edited the current post,
   108  * Display the name of the author who last edited the current post,
   145  *
   151  *
   146  * @since 2.8.0
   152  * @since 2.8.0
   147  *
   153  *
   148  * @global object $authordata The current author's DB object.
   154  * @global object $authordata The current author's DB object.
   149  *
   155  *
   150  * @param string $field   Optional. The user field to retrieve. Default empty.
   156  * @param string    $field   Optional. The user field to retrieve. Default empty.
   151  * @param int    $user_id Optional. User ID.
   157  * @param int|false $user_id Optional. User ID.
   152  * @return string The author's field from the current author's DB object, otherwise an empty string.
   158  * @return string The author's field from the current author's DB object, otherwise an empty string.
   153  */
   159  */
   154 function get_the_author_meta( $field = '', $user_id = false ) {
   160 function get_the_author_meta( $field = '', $user_id = false ) {
   155 	$original_user_id = $user_id;
   161 	$original_user_id = $user_id;
   156 
   162 
   159 		$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
   165 		$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
   160 	} else {
   166 	} else {
   161 		$authordata = get_userdata( $user_id );
   167 		$authordata = get_userdata( $user_id );
   162 	}
   168 	}
   163 
   169 
   164 	if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
   170 	if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) ) {
   165 		$field = 'user_' . $field;
   171 		$field = 'user_' . $field;
       
   172 	}
   166 
   173 
   167 	$value = isset( $authordata->$field ) ? $authordata->$field : '';
   174 	$value = isset( $authordata->$field ) ? $authordata->$field : '';
   168 
   175 
   169 	/**
   176 	/**
   170 	 * Filters the value of the requested user metadata.
   177 	 * Filters the value of the requested user metadata.
   172 	 * The filter name is dynamic and depends on the $field parameter of the function.
   179 	 * The filter name is dynamic and depends on the $field parameter of the function.
   173 	 *
   180 	 *
   174 	 * @since 2.8.0
   181 	 * @since 2.8.0
   175 	 * @since 4.3.0 The `$original_user_id` parameter was added.
   182 	 * @since 4.3.0 The `$original_user_id` parameter was added.
   176 	 *
   183 	 *
   177 	 * @param string   $value            The value of the metadata.
   184 	 * @param string    $value            The value of the metadata.
   178 	 * @param int      $user_id          The user ID for the value.
   185 	 * @param int       $user_id          The user ID for the value.
   179 	 * @param int|bool $original_user_id The original user ID, as passed to the function.
   186 	 * @param int|false $original_user_id The original user ID, as passed to the function.
   180 	 */
   187 	 */
   181 	return apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id );
   188 	return apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id );
   182 }
   189 }
   183 
   190 
   184 /**
   191 /**
   185  * Outputs the field from the user's DB object. Defaults to current post's author.
   192  * Outputs the field from the user's DB object. Defaults to current post's author.
   186  *
   193  *
   187  * @since 2.8.0
   194  * @since 2.8.0
   188  *
   195  *
   189  * @param string $field   Selects the field of the users record. See get_the_author_meta()
   196  * @param string    $field   Selects the field of the users record. See get_the_author_meta()
   190  *                        for the list of possible fields.
   197  *                           for the list of possible fields.
   191  * @param int    $user_id Optional. User ID.
   198  * @param int|false $user_id Optional. User ID.
   192  *
   199  *
   193  * @see get_the_author_meta()
   200  * @see get_the_author_meta()
   194  */
   201  */
   195 function the_author_meta( $field = '', $user_id = false ) {
   202 function the_author_meta( $field = '', $user_id = false ) {
   196 	$author_meta = get_the_author_meta( $field, $user_id );
   203 	$author_meta = get_the_author_meta( $field, $user_id );
   200 	 *
   207 	 *
   201 	 * The filter name is dynamic and depends on the $field parameter of the function.
   208 	 * The filter name is dynamic and depends on the $field parameter of the function.
   202 	 *
   209 	 *
   203 	 * @since 2.8.0
   210 	 * @since 2.8.0
   204 	 *
   211 	 *
   205 	 * @param string $author_meta The value of the metadata.
   212 	 * @param string    $author_meta The value of the metadata.
   206 	 * @param int    $user_id     The user ID.
   213 	 * @param int|false $user_id     The user ID.
   207 	 */
   214 	 */
   208 	echo apply_filters( "the_author_{$field}", $author_meta, $user_id );
   215 	echo apply_filters( "the_author_{$field}", $author_meta, $user_id );
   209 }
   216 }
   210 
   217 
   211 /**
   218 /**
   218  *
   225  *
   219  * @return string|null An HTML link if the author's url exist in user meta,
   226  * @return string|null An HTML link if the author's url exist in user meta,
   220  *                     else the result of get_the_author().
   227  *                     else the result of get_the_author().
   221  */
   228  */
   222 function get_the_author_link() {
   229 function get_the_author_link() {
   223 	if ( get_the_author_meta('url') ) {
   230 	if ( get_the_author_meta( 'url' ) ) {
   224 		return sprintf( '<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
   231 		return sprintf(
   225 			esc_url( get_the_author_meta('url') ),
   232 			'<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
       
   233 			esc_url( get_the_author_meta( 'url' ) ),
   226 			/* translators: %s: author's display name */
   234 			/* translators: %s: author's display name */
   227 			esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
   235 			esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
   228 			get_the_author()
   236 			get_the_author()
   229 		);
   237 		);
   230 	} else {
   238 	} else {
   278  *
   286  *
   279  * @since 4.4.0
   287  * @since 4.4.0
   280  *
   288  *
   281  * @global object $authordata The current author's DB object.
   289  * @global object $authordata The current author's DB object.
   282  *
   290  *
   283  * @return string An HTML link to the author page.
   291  * @return string An HTML link to the author page, or an empty string if $authordata isn't defined.
   284  */
   292  */
   285 function get_the_author_posts_link() {
   293 function get_the_author_posts_link() {
   286 	global $authordata;
   294 	global $authordata;
   287 	if ( ! is_object( $authordata ) ) {
   295 	if ( ! is_object( $authordata ) ) {
   288 		return;
   296 		return '';
   289 	}
   297 	}
   290 
   298 
   291 	$link = sprintf( '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
   299 	$link = sprintf(
       
   300 		'<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
   292 		esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
   301 		esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
   293 		/* translators: %s: author's display name */
   302 		/* translators: %s: author's display name */
   294 		esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
   303 		esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
   295 		get_the_author()
   304 		get_the_author()
   296 	);
   305 	);
   332  * @return string The URL to the author's page.
   341  * @return string The URL to the author's page.
   333  */
   342  */
   334 function get_author_posts_url( $author_id, $author_nicename = '' ) {
   343 function get_author_posts_url( $author_id, $author_nicename = '' ) {
   335 	global $wp_rewrite;
   344 	global $wp_rewrite;
   336 	$auth_ID = (int) $author_id;
   345 	$auth_ID = (int) $author_id;
   337 	$link = $wp_rewrite->get_author_permastruct();
   346 	$link    = $wp_rewrite->get_author_permastruct();
   338 
   347 
   339 	if ( empty($link) ) {
   348 	if ( empty( $link ) ) {
   340 		$file = home_url( '/' );
   349 		$file = home_url( '/' );
   341 		$link = $file . '?author=' . $auth_ID;
   350 		$link = $file . '?author=' . $auth_ID;
   342 	} else {
   351 	} else {
   343 		if ( '' == $author_nicename ) {
   352 		if ( '' == $author_nicename ) {
   344 			$user = get_userdata($author_id);
   353 			$user = get_userdata( $author_id );
   345 			if ( !empty($user->user_nicename) )
   354 			if ( ! empty( $user->user_nicename ) ) {
   346 				$author_nicename = $user->user_nicename;
   355 				$author_nicename = $user->user_nicename;
   347 		}
   356 			}
   348 		$link = str_replace('%author%', $author_nicename, $link);
   357 		}
       
   358 		$link = str_replace( '%author%', $author_nicename, $link );
   349 		$link = home_url( user_trailingslashit( $link ) );
   359 		$link = home_url( user_trailingslashit( $link ) );
   350 	}
   360 	}
   351 
   361 
   352 	/**
   362 	/**
   353 	 * Filters the URL to the author's page.
   363 	 * Filters the URL to the author's page.
   379  *                                       'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
   389  *                                       'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
   380  *                                       'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
   390  *                                       'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
   381  *     @type string       $order         Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
   391  *     @type string       $order         Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
   382  *     @type int          $number        Maximum authors to return or display. Default empty (all authors).
   392  *     @type int          $number        Maximum authors to return or display. Default empty (all authors).
   383  *     @type bool         $optioncount   Show the count in parenthesis next to the author's name. Default false.
   393  *     @type bool         $optioncount   Show the count in parenthesis next to the author's name. Default false.
   384  *     @type bool         $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
   394  *     @type bool         $exclude_admin Whether to exclude the 'admin' account, if it exists. Default true.
   385  *     @type bool         $show_fullname Whether to show the author's full name. Default false.
   395  *     @type bool         $show_fullname Whether to show the author's full name. Default false.
   386  *     @type bool         $hide_empty    Whether to hide any authors with no posts. Default true.
   396  *     @type bool         $hide_empty    Whether to hide any authors with no posts. Default true.
   387  *     @type string       $feed          If not empty, show a link to the author's feed and use this text as the alt
   397  *     @type string       $feed          If not empty, show a link to the author's feed and use this text as the alt
   388  *                                       parameter of the link. Default empty.
   398  *                                       parameter of the link. Default empty.
   389  *     @type string       $feed_image    If not empty, show a link to the author's feed and use this image URL as
   399  *     @type string       $feed_image    If not empty, show a link to the author's feed and use this image URL as
   400  */
   410  */
   401 function wp_list_authors( $args = '' ) {
   411 function wp_list_authors( $args = '' ) {
   402 	global $wpdb;
   412 	global $wpdb;
   403 
   413 
   404 	$defaults = array(
   414 	$defaults = array(
   405 		'orderby' => 'name', 'order' => 'ASC', 'number' => '',
   415 		'orderby'       => 'name',
   406 		'optioncount' => false, 'exclude_admin' => true,
   416 		'order'         => 'ASC',
   407 		'show_fullname' => false, 'hide_empty' => true,
   417 		'number'        => '',
   408 		'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
   418 		'optioncount'   => false,
   409 		'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
   419 		'exclude_admin' => true,
       
   420 		'show_fullname' => false,
       
   421 		'hide_empty'    => true,
       
   422 		'feed'          => '',
       
   423 		'feed_image'    => '',
       
   424 		'feed_type'     => '',
       
   425 		'echo'          => true,
       
   426 		'style'         => 'list',
       
   427 		'html'          => true,
       
   428 		'exclude'       => '',
       
   429 		'include'       => '',
   410 	);
   430 	);
   411 
   431 
   412 	$args = wp_parse_args( $args, $defaults );
   432 	$args = wp_parse_args( $args, $defaults );
   413 
   433 
   414 	$return = '';
   434 	$return = '';
   415 
   435 
   416 	$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
   436 	$query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
   417 	$query_args['fields'] = 'ids';
   437 	$query_args['fields'] = 'ids';
   418 	$authors = get_users( $query_args );
   438 	$authors              = get_users( $query_args );
   419 
   439 
   420 	$author_count = array();
   440 	$author_count = array();
   421 	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 ) {
   441 	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 ) {
   422 		$author_count[$row->post_author] = $row->count;
   442 		$author_count[ $row->post_author ] = $row->count;
   423 	}
   443 	}
   424 	foreach ( $authors as $author_id ) {
   444 	foreach ( $authors as $author_id ) {
       
   445 		$posts = isset( $author_count[ $author_id ] ) ? $author_count[ $author_id ] : 0;
       
   446 
       
   447 		if ( ! $posts && $args['hide_empty'] ) {
       
   448 			continue;
       
   449 		}
       
   450 
   425 		$author = get_userdata( $author_id );
   451 		$author = get_userdata( $author_id );
   426 
   452 
   427 		if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
   453 		if ( $args['exclude_admin'] && 'admin' === $author->display_name ) {
   428 			continue;
       
   429 		}
       
   430 
       
   431 		$posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
       
   432 
       
   433 		if ( ! $posts && $args['hide_empty'] ) {
       
   434 			continue;
   454 			continue;
   435 		}
   455 		}
   436 
   456 
   437 		if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
   457 		if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
   438 			$name = "$author->first_name $author->last_name";
   458 			$name = "$author->first_name $author->last_name";
   448 
   468 
   449 		if ( 'list' == $args['style'] ) {
   469 		if ( 'list' == $args['style'] ) {
   450 			$return .= '<li>';
   470 			$return .= '<li>';
   451 		}
   471 		}
   452 
   472 
   453 		$link = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>',
   473 		$link = sprintf(
       
   474 			'<a href="%1$s" title="%2$s">%3$s</a>',
   454 			get_author_posts_url( $author->ID, $author->user_nicename ),
   475 			get_author_posts_url( $author->ID, $author->user_nicename ),
   455 			/* translators: %s: author's display name */
   476 			/* translators: %s: author's display name */
   456 			esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
   477 			esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
   457 			$name
   478 			$name
   458 		);
   479 		);
   465 
   486 
   466 			$link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
   487 			$link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
   467 
   488 
   468 			$alt = '';
   489 			$alt = '';
   469 			if ( ! empty( $args['feed'] ) ) {
   490 			if ( ! empty( $args['feed'] ) ) {
   470 				$alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
   491 				$alt  = ' alt="' . esc_attr( $args['feed'] ) . '"';
   471 				$name = $args['feed'];
   492 				$name = $args['feed'];
   472 			}
   493 			}
   473 
   494 
   474 			$link .= '>';
   495 			$link .= '>';
   475 
   496 
   485 				$link .= ')';
   506 				$link .= ')';
   486 			}
   507 			}
   487 		}
   508 		}
   488 
   509 
   489 		if ( $args['optioncount'] ) {
   510 		if ( $args['optioncount'] ) {
   490 			$link .= ' ('. $posts . ')';
   511 			$link .= ' (' . $posts . ')';
   491 		}
   512 		}
   492 
   513 
   493 		$return .= $link;
   514 		$return .= $link;
   494 		$return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
   515 		$return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
   495 	}
   516 	}
   501 	}
   522 	}
   502 	echo $return;
   523 	echo $return;
   503 }
   524 }
   504 
   525 
   505 /**
   526 /**
   506  * Does this site have more than one author
   527  * Determines whether this site has more than one author.
   507  *
   528  *
   508  * Checks to see if more than one author has published posts.
   529  * Checks to see if more than one author has published posts.
       
   530  *
       
   531  * For more information on this and similar theme functions, check out
       
   532  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
       
   533  * Conditional Tags} article in the Theme Developer Handbook.
   509  *
   534  *
   510  * @since 3.2.0
   535  * @since 3.2.0
   511  *
   536  *
   512  * @global wpdb $wpdb WordPress database abstraction object.
   537  * @global wpdb $wpdb WordPress database abstraction object.
   513  *
   538  *
   515  */
   540  */
   516 function is_multi_author() {
   541 function is_multi_author() {
   517 	global $wpdb;
   542 	global $wpdb;
   518 
   543 
   519 	if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
   544 	if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
   520 		$rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
   545 		$rows            = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" );
   521 		$is_multi_author = 1 < count( $rows ) ? 1 : 0;
   546 		$is_multi_author = 1 < count( $rows ) ? 1 : 0;
   522 		set_transient( 'is_multi_author', $is_multi_author );
   547 		set_transient( 'is_multi_author', $is_multi_author );
   523 	}
   548 	}
   524 
   549 
   525 	/**
   550 	/**