web/wp-includes/author-template.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    20  * @param string $deprecated Deprecated.
    20  * @param string $deprecated Deprecated.
    21  * @return string The author's display name.
    21  * @return string 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 
       
    26 	if ( !empty( $deprecated ) )
       
    27 		_deprecated_argument( __FUNCTION__, '2.1' );
       
    28 
    25 	return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null);
    29 	return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null);
    26 }
    30 }
    27 
    31 
    28 /**
    32 /**
    29  * Display the name of the author of the current post.
    33  * Display the name of the author of the current post.
    32  * get_the_author(). This function is not deprecated, but is designed to echo
    36  * get_the_author(). This function is not deprecated, but is designed to echo
    33  * the value from get_the_author() and as an result of any old theme that might
    37  * the value from get_the_author() and as an result of any old theme that might
    34  * still use the old behavior will also pass the value from get_the_author().
    38  * still use the old behavior will also pass the value from get_the_author().
    35  *
    39  *
    36  * The normal, expected behavior of this function is to echo the author and not
    40  * The normal, expected behavior of this function is to echo the author and not
    37  * return it. However, backwards compatiability has to be maintained.
    41  * return it. However, backwards compatibility has to be maintained.
    38  *
    42  *
    39  * @since 0.71
    43  * @since 0.71
    40  * @see get_the_author()
    44  * @see get_the_author()
    41  * @link http://codex.wordpress.org/Template_Tags/the_author
    45  * @link http://codex.wordpress.org/Template_Tags/the_author
    42  *
    46  *
    43  * @param string $deprecated Deprecated.
    47  * @param string $deprecated Deprecated.
    44  * @param string $deprecated_echo Echo the string or return it.
    48  * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
    45  * @return string The author's display name, from get_the_author().
    49  * @return string The author's display name, from get_the_author().
    46  */
    50  */
    47 function the_author($deprecated = '', $deprecated_echo = true) {
    51 function the_author( $deprecated = '', $deprecated_echo = true ) {
       
    52 	if ( !empty( $deprecated ) )
       
    53 		_deprecated_argument( __FUNCTION__, '2.1' );
       
    54 	if ( $deprecated_echo !== true )
       
    55 		_deprecated_argument( __FUNCTION__, '1.5', __('Use <code>get_the_author()</code> instead if you do not want the value echoed.') );
    48 	if ( $deprecated_echo )
    56 	if ( $deprecated_echo )
    49 		echo get_the_author();
    57 		echo get_the_author();
    50 	return get_the_author();
    58 	return get_the_author();
    51 }
    59 }
    52 
    60 
    86  * @uses $authordata The current author's DB object (if $user_id not specified).
    94  * @uses $authordata The current author's DB object (if $user_id not specified).
    87  * @param string $field selects the field of the users record.
    95  * @param string $field selects the field of the users record.
    88  * @param int $user_id Optional. User ID.
    96  * @param int $user_id Optional. User ID.
    89  * @return string The author's field from the current author's DB object.
    97  * @return string The author's field from the current author's DB object.
    90  */
    98  */
    91 function get_the_author_meta($field = '', $user_id = false) {
    99 function get_the_author_meta( $field = '', $user_id = false ) {
    92 	if ( ! $user_id )
   100 	if ( ! $user_id ) {
    93 		global $authordata;
   101 		global $authordata;
    94 	else
   102 		$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
       
   103 	} else {
    95 		$authordata = get_userdata( $user_id );
   104 		$authordata = get_userdata( $user_id );
    96 
   105 	}
    97 	$field = strtolower($field);
   106 
    98 	$user_field = "user_$field";
   107 	if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
    99 
   108 		$field = 'user_' . $field;
   100 	if ( 'id' == $field )
   109 
   101 		$value = isset($authordata->ID) ? (int)$authordata->ID : 0;
   110 	$value = isset( $authordata->$field ) ? $authordata->$field : '';
   102 	elseif ( isset($authordata->$user_field) )
   111 
   103 		$value = $authordata->$user_field;
   112 	return apply_filters( 'get_the_author_' . $field, $value, $user_id );
   104 	else
       
   105 		$value = isset($authordata->$field) ? $authordata->$field : '';
       
   106 
       
   107 	return apply_filters('get_the_author_' . $field, $value, $user_id);
       
   108 }
   113 }
   109 
   114 
   110 /**
   115 /**
   111  * Retrieve the requested data of the author of the current post.
   116  * Retrieve the requested data of the author of the current post.
   112  * @link http://codex.wordpress.org/Template_Tags/the_author_meta
   117  * @link http://codex.wordpress.org/Template_Tags/the_author_meta
   118 function the_author_meta($field = '', $user_id = false) {
   123 function the_author_meta($field = '', $user_id = false) {
   119 	echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id), $user_id);
   124 	echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id), $user_id);
   120 }
   125 }
   121 
   126 
   122 /**
   127 /**
       
   128  * Retrieve either author's link or author's name.
       
   129  *
       
   130  * If the author has a home page set, return an HTML link, otherwise just return the
       
   131  * author's name.
       
   132  *
       
   133  * @uses get_the_author_meta()
       
   134  * @uses get_the_author()
       
   135  */
       
   136 function get_the_author_link() {
       
   137 	if ( get_the_author_meta('url') ) {
       
   138 		return '<a href="' . get_the_author_meta('url') . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), get_the_author()) ) . '" rel="author external">' . get_the_author() . '</a>';
       
   139 	} else {
       
   140 		return get_the_author();
       
   141 	}
       
   142 }
       
   143 
       
   144 /**
   123  * Display either author's link or author's name.
   145  * Display either author's link or author's name.
   124  *
   146  *
   125  * If the author has a home page set, echo an HTML link, otherwise just echo the
   147  * If the author has a home page set, echo an HTML link, otherwise just echo the
   126  * author's name.
   148  * author's name.
   127  *
   149  *
   128  * @link http://codex.wordpress.org/Template_Tags/the_author_link
   150  * @link http://codex.wordpress.org/Template_Tags/the_author_link
   129  * @since 2.1
   151  * @since 2.1
   130  * @uses get_the_author_meta()
   152  * @uses get_the_author_link()
   131  * @uses the_author()
       
   132  */
   153  */
   133 function the_author_link() {
   154 function the_author_link() {
   134 	if ( get_the_author_meta('url') ) {
   155 	echo get_the_author_link();
   135 		echo '<a href="' . get_the_author_meta('url') . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), get_the_author()) ) . '" rel="external">' . get_the_author() . '</a>';
       
   136 	} else {
       
   137 		the_author();
       
   138 	}
       
   139 }
   156 }
   140 
   157 
   141 /**
   158 /**
   142  * Retrieve the number of posts by the author of the current post.
   159  * Retrieve the number of posts by the author of the current post.
   143  *
   160  *
   144  * @since 1.5
   161  * @since 1.5
   145  * @uses $post The current post in the Loop's DB object.
   162  * @uses $post The current post in the Loop's DB object.
   146  * @uses get_usernumposts()
   163  * @uses count_user_posts()
   147  * @return int The number of posts by the author.
   164  * @return int The number of posts by the author.
   148  */
   165  */
   149 function get_the_author_posts() {
   166 function get_the_author_posts() {
   150 	global $post;
   167 	global $post;
   151 	return get_usernumposts($post->post_author);
   168 	return count_user_posts($post->post_author);
   152 }
   169 }
   153 
   170 
   154 /**
   171 /**
   155  * Display the number of posts by the author of the current post.
   172  * Display the number of posts by the author of the current post.
   156  *
   173  *
   157  * @link http://codex.wordpress.org/Template_Tags/the_author_posts
   174  * @link http://codex.wordpress.org/Template_Tags/the_author_posts
   158  * @since 0.71
   175  * @since 0.71
   159  * @uses get_the_author_posts() Echos returned value from function.
   176  * @uses get_the_author_posts() Echoes returned value from function.
   160  */
   177  */
   161 function the_author_posts() {
   178 function the_author_posts() {
   162 	echo get_the_author_posts();
   179 	echo get_the_author_posts();
   163 }
   180 }
   164 
   181 
   175  * @uses get_author_posts_url()
   192  * @uses get_author_posts_url()
   176  * @uses get_the_author()
   193  * @uses get_the_author()
   177  * @param string $deprecated Deprecated.
   194  * @param string $deprecated Deprecated.
   178  */
   195  */
   179 function the_author_posts_link($deprecated = '') {
   196 function the_author_posts_link($deprecated = '') {
       
   197 	if ( !empty( $deprecated ) )
       
   198 		_deprecated_argument( __FUNCTION__, '2.1' );
       
   199 
   180 	global $authordata;
   200 	global $authordata;
       
   201 	if ( !is_object( $authordata ) )
       
   202 		return false;
   181 	$link = sprintf(
   203 	$link = sprintf(
   182 		'<a href="%1$s" title="%2$s">%3$s</a>',
   204 		'<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
   183 		get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
   205 		get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
   184 		esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
   206 		esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
   185 		get_the_author()
   207 		get_the_author()
   186 	);
   208 	);
   187 	echo apply_filters( 'the_author_posts_link', $link );
   209 	echo apply_filters( 'the_author_posts_link', $link );
   188 }
   210 }
   189 
   211 
   190 /**
   212 /**
   191  * Retrieve the URL to the author page of the author of the current post.
   213  * Retrieve the URL to the author page for the user with the ID provided.
   192  *
   214  *
   193  * @since 2.1.0
   215  * @since 2.1.0
   194  * @uses $wp_rewrite WP_Rewrite
   216  * @uses $wp_rewrite WP_Rewrite
   195  * @return string The URL to the author's page.
   217  * @return string The URL to the author's page.
   196  */
   218  */
   198 	global $wp_rewrite;
   220 	global $wp_rewrite;
   199 	$auth_ID = (int) $author_id;
   221 	$auth_ID = (int) $author_id;
   200 	$link = $wp_rewrite->get_author_permastruct();
   222 	$link = $wp_rewrite->get_author_permastruct();
   201 
   223 
   202 	if ( empty($link) ) {
   224 	if ( empty($link) ) {
   203 		$file = get_option('home') . '/';
   225 		$file = home_url( '/' );
   204 		$link = $file . '?author=' . $auth_ID;
   226 		$link = $file . '?author=' . $auth_ID;
   205 	} else {
   227 	} else {
   206 		if ( '' == $author_nicename ) {
   228 		if ( '' == $author_nicename ) {
   207 			$user = get_userdata($author_id);
   229 			$user = get_userdata($author_id);
   208 			if ( !empty($user->user_nicename) )
   230 			if ( !empty($user->user_nicename) )
   209 				$author_nicename = $user->user_nicename;
   231 				$author_nicename = $user->user_nicename;
   210 		}
   232 		}
   211 		$link = str_replace('%author%', $author_nicename, $link);
   233 		$link = str_replace('%author%', $author_nicename, $link);
   212 		$link = get_option('home') . trailingslashit($link);
   234 		$link = home_url( user_trailingslashit( $link ) );
   213 	}
   235 	}
   214 
   236 
   215 	$link = apply_filters('author_link', $link, $author_id, $author_nicename);
   237 	$link = apply_filters('author_link', $link, $author_id, $author_nicename);
   216 
   238 
   217 	return $link;
   239 	return $link;
   232  * feeds.</li>
   254  * feeds.</li>
   233  * <li>echo (boolean) (true): Set to false to return the output, instead of
   255  * <li>echo (boolean) (true): Set to false to return the output, instead of
   234  * echoing.</li>
   256  * echoing.</li>
   235  * <li>style (string) ('list'): Whether to display list of authors in list form
   257  * <li>style (string) ('list'): Whether to display list of authors in list form
   236  * or as a string.</li>
   258  * or as a string.</li>
   237  * <li>html (bool) (true): Whether to list the items in html for or plaintext.
   259  * <li>html (bool) (true): Whether to list the items in html form or plaintext.
   238  * </li>
   260  * </li>
   239  * </ul>
   261  * </ul>
   240  *
   262  *
   241  * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
   263  * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
   242  * @since 1.2.0
   264  * @since 1.2.0
   245  */
   267  */
   246 function wp_list_authors($args = '') {
   268 function wp_list_authors($args = '') {
   247 	global $wpdb;
   269 	global $wpdb;
   248 
   270 
   249 	$defaults = array(
   271 	$defaults = array(
       
   272 		'orderby' => 'name', 'order' => 'ASC', 'number' => '',
   250 		'optioncount' => false, 'exclude_admin' => true,
   273 		'optioncount' => false, 'exclude_admin' => true,
   251 		'show_fullname' => false, 'hide_empty' => true,
   274 		'show_fullname' => false, 'hide_empty' => true,
   252 		'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
   275 		'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
   253 		'style' => 'list', 'html' => true
   276 		'style' => 'list', 'html' => true
   254 	);
   277 	);
   255 
   278 
   256 	$r = wp_parse_args( $args, $defaults );
   279 	$args = wp_parse_args( $args, $defaults );
   257 	extract($r, EXTR_SKIP);
   280 	extract( $args, EXTR_SKIP );
       
   281 
   258 	$return = '';
   282 	$return = '';
   259 
   283 
   260 	/** @todo Move select to get_authors(). */
   284 	$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
   261 	$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");
   285 	$query_args['fields'] = 'ids';
       
   286 	$authors = get_users( $query_args );
   262 
   287 
   263 	$author_count = array();
   288 	$author_count = array();
   264 	foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) {
   289 	foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
   265 		$author_count[$row->post_author] = $row->count;
   290 		$author_count[$row->post_author] = $row->count;
   266 	}
   291 
   267 
   292 	foreach ( $authors as $author_id ) {
   268 	foreach ( (array) $authors as $author ) {
   293 		$author = get_userdata( $author_id );
       
   294 
       
   295 		if ( $exclude_admin && 'admin' == $author->display_name )
       
   296 			continue;
       
   297 
       
   298 		$posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
       
   299 
       
   300 		if ( !$posts && $hide_empty )
       
   301 			continue;
   269 
   302 
   270 		$link = '';
   303 		$link = '';
   271 
   304 
   272 		$author = get_userdata( $author->ID );
   305 		if ( $show_fullname && $author->first_name && $author->last_name )
   273 		$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
       
   274 		$name = $author->display_name;
       
   275 
       
   276 		if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
       
   277 			$name = "$author->first_name $author->last_name";
   306 			$name = "$author->first_name $author->last_name";
   278 
   307 		else
   279 		if( !$html ) {
   308 			$name = $author->display_name;
   280 			if ( $posts == 0 ) {
   309 
   281 				if ( ! $hide_empty )
   310 		if ( !$html ) {
   282 					$return .= $name . ', ';
   311 			$return .= $name . ', ';
   283 			} else
   312 
   284 				$return .= $name . ', ';
   313 			continue; // No need to go further to process HTML.
   285 
       
   286 			// No need to go further to process HTML.
       
   287 			continue;
       
   288 		}
   314 		}
   289 
   315 
   290 		if ( !($posts == 0 && $hide_empty) && 'list' == $style )
   316 		if ( 'list' == $style ) {
   291 			$return .= '<li>';
   317 			$return .= '<li>';
   292 		if ( $posts == 0 ) {
   318 		}
   293 			if ( ! $hide_empty )
   319 
   294 				$link = $name;
   320 		$link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
   295 		} else {
   321 
   296 			$link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
   322 		if ( !empty( $feed_image ) || !empty( $feed ) ) {
   297 
   323 			$link .= ' ';
   298 			if ( (! empty($feed_image)) || (! empty($feed)) ) {
   324 			if ( empty( $feed_image ) ) {
   299 				$link .= ' ';
   325 				$link .= '(';
   300 				if (empty($feed_image))
       
   301 					$link .= '(';
       
   302 				$link .= '<a href="' . get_author_feed_link($author->ID) . '"';
       
   303 
       
   304 				if ( !empty($feed) ) {
       
   305 					$title = ' title="' . esc_attr($feed) . '"';
       
   306 					$alt = ' alt="' . esc_attr($feed) . '"';
       
   307 					$name = $feed;
       
   308 					$link .= $title;
       
   309 				}
       
   310 
       
   311 				$link .= '>';
       
   312 
       
   313 				if ( !empty($feed_image) )
       
   314 					$link .= "<img src=\"" . esc_url($feed_image) . "\" style=\"border: none;\"$alt$title" . ' />';
       
   315 				else
       
   316 					$link .= $name;
       
   317 
       
   318 				$link .= '</a>';
       
   319 
       
   320 				if ( empty($feed_image) )
       
   321 					$link .= ')';
       
   322 			}
   326 			}
   323 
   327 
   324 			if ( $optioncount )
   328 			$link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';
   325 				$link .= ' ('. $posts . ')';
   329 
   326 
   330 			$alt = $title = '';
       
   331 			if ( !empty( $feed ) ) {
       
   332 				$title = ' title="' . esc_attr( $feed ) . '"';
       
   333 				$alt = ' alt="' . esc_attr( $feed ) . '"';
       
   334 				$name = $feed;
       
   335 				$link .= $title;
       
   336 			}
       
   337 
       
   338 			$link .= '>';
       
   339 
       
   340 			if ( !empty( $feed_image ) )
       
   341 				$link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
       
   342 			else
       
   343 				$link .= $name;
       
   344 
       
   345 			$link .= '</a>';
       
   346 
       
   347 			if ( empty( $feed_image ) )
       
   348 				$link .= ')';
   327 		}
   349 		}
   328 
   350 
   329 		if ( !($posts == 0 && $hide_empty) && 'list' == $style )
   351 		if ( $optioncount )
   330 			$return .= $link . '</li>';
   352 			$link .= ' ('. $posts . ')';
   331 		else if ( ! $hide_empty )
   353 
   332 			$return .= $link . ', ';
   354 		$return .= $link;
   333 	}
   355 		$return .= ( 'list' == $style ) ? '</li>' : ', ';
   334 
   356 	}
   335 	$return = trim($return, ', ');
   357 
   336 
   358 	$return = rtrim($return, ', ');
   337 	if ( ! $echo )
   359 
       
   360 	if ( !$echo )
   338 		return $return;
   361 		return $return;
       
   362 
   339 	echo $return;
   363 	echo $return;
   340 }
   364 }
   341 
   365 
   342 ?>
   366 /**
       
   367  * Does this site have more than one author
       
   368  *
       
   369  * Checks to see if more than one author has published posts.
       
   370  *
       
   371  * @since 3.2.0
       
   372  * @return bool Whether or not we have more than one author
       
   373  */
       
   374 function is_multi_author() {
       
   375 	global $wpdb;
       
   376 
       
   377 	if ( false === ( $is_multi_author = wp_cache_get('is_multi_author', 'posts') ) ) {
       
   378 		$rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
       
   379 		$is_multi_author = 1 < count( $rows ) ? 1 : 0;
       
   380 		wp_cache_set('is_multi_author', $is_multi_author, 'posts');
       
   381 	}
       
   382 
       
   383 	return apply_filters( 'is_multi_author', (bool) $is_multi_author );
       
   384 }
       
   385 
       
   386 /**
       
   387  * Helper function to clear the cache for number of authors.
       
   388  *
       
   389  * @private
       
   390  */
       
   391 function __clear_multi_author_cache() {
       
   392 	wp_cache_delete('is_multi_author', 'posts');
       
   393 }
       
   394 add_action('transition_post_status', '__clear_multi_author_cache');