web/wp-includes/author-template.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /**
       
     3  * Author Template functions for use in themes.
       
     4  *
       
     5  * These functions must be used within the WordPress Loop.
       
     6  *
       
     7  * @link http://codex.wordpress.org/Author_Templates
       
     8  *
       
     9  * @package WordPress
       
    10  * @subpackage Template
       
    11  */
       
    12 
       
    13 /**
       
    14  * Retrieve the author of the current post.
       
    15  *
       
    16  * @since 1.5
       
    17  * @uses $authordata The current author's DB object.
       
    18  * @uses apply_filters() Calls 'the_author' hook on the author display name.
       
    19  *
       
    20  * @param string $deprecated Deprecated.
       
    21  * @return string The author's display name.
       
    22  */
       
    23 function get_the_author($deprecated = '') {
       
    24 	global $authordata;
       
    25 	return apply_filters('the_author', $authordata->display_name);
       
    26 }
       
    27 
       
    28 /**
       
    29  * Display the name of the author of the current post.
       
    30  *
       
    31  * The behavior of this function is based off of old functionality predating
       
    32  * 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
       
    34  * still use the old behavior will also pass the value from get_the_author().
       
    35  *
       
    36  * The normal, expected behavior of this function is to echo the author and not
       
    37  * return it. However, backwards compatiability has to be maintained.
       
    38  *
       
    39  * @since 0.71
       
    40  * @see get_the_author()
       
    41  * @link http://codex.wordpress.org/Template_Tags/the_author
       
    42  *
       
    43  * @param string $deprecated Deprecated.
       
    44  * @param string $deprecated_echo Echo the string or return it.
       
    45  * @return string The author's display name, from get_the_author().
       
    46  */
       
    47 function the_author($deprecated = '', $deprecated_echo = true) {
       
    48 	if ( $deprecated_echo )
       
    49 		echo get_the_author();
       
    50 	return get_the_author();
       
    51 }
       
    52 
       
    53 /**
       
    54  * Retrieve the author who last edited the current post.
       
    55  *
       
    56  * @since 2.8
       
    57  * @uses $post The current post's DB object.
       
    58  * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
       
    59  * @uses get_userdata() Retrieves the author's DB object.
       
    60  * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
       
    61  * @return string The author's display name.
       
    62  */
       
    63 function get_the_modified_author() {
       
    64 	global $post;
       
    65 	if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) {
       
    66 		$last_user = get_userdata($last_id);
       
    67 		return apply_filters('the_modified_author', $last_user->display_name);
       
    68 	}
       
    69 }
       
    70 
       
    71 /**
       
    72  * Display the name of the author who last edited the current post.
       
    73  *
       
    74  * @since 2.8
       
    75  * @see get_the_author()
       
    76  * @return string The author's display name, from get_the_modified_author().
       
    77  */
       
    78 function the_modified_author() {
       
    79 	echo get_the_modified_author();
       
    80 }
       
    81 
       
    82 /**
       
    83  * Retrieve the requested data of the author of the current post.
       
    84  * @link http://codex.wordpress.org/Template_Tags/the_author_meta
       
    85  * @since 2.8.0
       
    86  * @uses $authordata The current author's DB object (if $user_id not specified).
       
    87  * @param string $field selects the field of the users record.
       
    88  * @param int $user_id Optional. User ID.
       
    89  * @return string The author's field from the current author's DB object.
       
    90  */
       
    91 function get_the_author_meta($field = '', $user_id = false) {
       
    92 	if ( ! $user_id )
       
    93 		global $authordata;
       
    94 	else
       
    95 		$authordata = get_userdata( $user_id );
       
    96 
       
    97 	$field = strtolower($field);
       
    98 	$user_field = "user_$field";
       
    99 
       
   100 	if ( 'id' == $field )
       
   101 		$value = isset($authordata->ID) ? (int)$authordata->ID : 0;
       
   102 	elseif ( isset($authordata->$user_field) )
       
   103 		$value = $authordata->$user_field;
       
   104 	else
       
   105 		$value = isset($authordata->$field) ? $authordata->$field : '';
       
   106 
       
   107 	return apply_filters('get_the_author_' . $field, $value);
       
   108 }
       
   109 
       
   110 /**
       
   111  * Retrieve the requested data of the author of the current post.
       
   112  * @link http://codex.wordpress.org/Template_Tags/the_author_meta
       
   113  * @since 2.8.0
       
   114  * @param string $field selects the field of the users record.
       
   115  * @param int $user_id Optional. User ID.
       
   116  * @echo string The author's field from the current author's DB object.
       
   117  */
       
   118 function the_author_meta($field = '', $user_id = false) {
       
   119 	echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id));
       
   120 }
       
   121 
       
   122 /**
       
   123  * Display either author's link or author's name.
       
   124  *
       
   125  * If the author has a home page set, echo an HTML link, otherwise just echo the
       
   126  * author's name.
       
   127  *
       
   128  * @link http://codex.wordpress.org/Template_Tags/the_author_link
       
   129  * @since 2.1
       
   130  * @uses get_the_author_meta()
       
   131  * @uses the_author()
       
   132  */
       
   133 function the_author_link() {
       
   134 	if ( get_the_author_meta('url') ) {
       
   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 }
       
   140 
       
   141 /**
       
   142  * Retrieve the number of posts by the author of the current post.
       
   143  *
       
   144  * @since 1.5
       
   145  * @uses $post The current post in the Loop's DB object.
       
   146  * @uses get_usernumposts()
       
   147  * @return int The number of posts by the author.
       
   148  */
       
   149 function get_the_author_posts() {
       
   150 	global $post;
       
   151 	return get_usernumposts($post->post_author);
       
   152 }
       
   153 
       
   154 /**
       
   155  * Display the number of posts by the author of the current post.
       
   156  *
       
   157  * @link http://codex.wordpress.org/Template_Tags/the_author_posts
       
   158  * @since 0.71
       
   159  * @uses get_the_author_posts() Echos returned value from function.
       
   160  */
       
   161 function the_author_posts() {
       
   162 	echo get_the_author_posts();
       
   163 }
       
   164 
       
   165 /**
       
   166  * Display an HTML link to the author page of the author of the current post.
       
   167  *
       
   168  * Does just echo get_author_posts_url() function, like the others do. The
       
   169  * reason for this, is that another function is used to help in printing the
       
   170  * link to the author's posts.
       
   171  *
       
   172  * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
       
   173  * @since 1.2.0
       
   174  * @uses $authordata The current author's DB object.
       
   175  * @uses get_author_posts_url()
       
   176  * @uses get_the_author()
       
   177  * @param string $deprecated Deprecated.
       
   178  */
       
   179 function the_author_posts_link($deprecated = '') {
       
   180 	global $authordata;
       
   181 	printf(
       
   182 		'<a href="%1$s" title="%2$s">%3$s</a>',
       
   183 		get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
       
   184 		esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
       
   185 		get_the_author()
       
   186 	);
       
   187 }
       
   188 
       
   189 /**
       
   190  * Retrieve the URL to the author page of the author of the current post.
       
   191  *
       
   192  * @since 2.1.0
       
   193  * @uses $wp_rewrite WP_Rewrite
       
   194  * @return string The URL to the author's page.
       
   195  */
       
   196 function get_author_posts_url($author_id, $author_nicename = '') {
       
   197 	global $wp_rewrite;
       
   198 	$auth_ID = (int) $author_id;
       
   199 	$link = $wp_rewrite->get_author_permastruct();
       
   200 
       
   201 	if ( empty($link) ) {
       
   202 		$file = get_option('home') . '/';
       
   203 		$link = $file . '?author=' . $auth_ID;
       
   204 	} else {
       
   205 		if ( '' == $author_nicename ) {
       
   206 			$user = get_userdata($author_id);
       
   207 			if ( !empty($user->user_nicename) )
       
   208 				$author_nicename = $user->user_nicename;
       
   209 		}
       
   210 		$link = str_replace('%author%', $author_nicename, $link);
       
   211 		$link = get_option('home') . trailingslashit($link);
       
   212 	}
       
   213 
       
   214 	$link = apply_filters('author_link', $link, $author_id, $author_nicename);
       
   215 
       
   216 	return $link;
       
   217 }
       
   218 
       
   219 /**
       
   220  * List all the authors of the blog, with several options available.
       
   221  *
       
   222  * <ul>
       
   223  * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
       
   224  * author's name.</li>
       
   225  * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
       
   226  * installed bydefault.</li>
       
   227  * <li>show_fullname (boolean) (false): Show their full names.</li>
       
   228  * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
       
   229  * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
       
   230  * <li>feed_image (string) (''): If isn't empty, use this image to link to
       
   231  * feeds.</li>
       
   232  * <li>echo (boolean) (true): Set to false to return the output, instead of
       
   233  * echoing.</li>
       
   234  * <li>style (string) ('list'): Whether to display list of authors in list form
       
   235  * or as a string.</li>
       
   236  * <li>html (bool) (true): Whether to list the items in html for or plaintext.
       
   237  * </li>
       
   238  * </ul>
       
   239  *
       
   240  * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
       
   241  * @since 1.2.0
       
   242  * @param array $args The argument array.
       
   243  * @return null|string The output, if echo is set to false.
       
   244  */
       
   245 function wp_list_authors($args = '') {
       
   246 	global $wpdb;
       
   247 
       
   248 	$defaults = array(
       
   249 		'optioncount' => false, 'exclude_admin' => true,
       
   250 		'show_fullname' => false, 'hide_empty' => true,
       
   251 		'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
       
   252 		'style' => 'list', 'html' => true
       
   253 	);
       
   254 
       
   255 	$r = wp_parse_args( $args, $defaults );
       
   256 	extract($r, EXTR_SKIP);
       
   257 	$return = '';
       
   258 
       
   259 	/** @todo Move select to get_authors(). */
       
   260 	$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");
       
   261 
       
   262 	$author_count = array();
       
   263 	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) {
       
   264 		$author_count[$row->post_author] = $row->count;
       
   265 	}
       
   266 
       
   267 	foreach ( (array) $authors as $author ) {
       
   268 
       
   269 		$link = '';
       
   270 
       
   271 		$author = get_userdata( $author->ID );
       
   272 		$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
       
   273 		$name = $author->display_name;
       
   274 
       
   275 		if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
       
   276 			$name = "$author->first_name $author->last_name";
       
   277 
       
   278 		if( !$html ) {
       
   279 			if ( $posts == 0 ) {
       
   280 				if ( ! $hide_empty )
       
   281 					$return .= $name . ', ';
       
   282 			} else
       
   283 				$return .= $name . ', ';
       
   284 
       
   285 			// No need to go further to process HTML.
       
   286 			continue;
       
   287 		}
       
   288 
       
   289 		if ( !($posts == 0 && $hide_empty) && 'list' == $style )
       
   290 			$return .= '<li>';
       
   291 		if ( $posts == 0 ) {
       
   292 			if ( ! $hide_empty )
       
   293 				$link = $name;
       
   294 		} else {
       
   295 			$link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
       
   296 
       
   297 			if ( (! empty($feed_image)) || (! empty($feed)) ) {
       
   298 				$link .= ' ';
       
   299 				if (empty($feed_image))
       
   300 					$link .= '(';
       
   301 				$link .= '<a href="' . get_author_feed_link($author->ID) . '"';
       
   302 
       
   303 				if ( !empty($feed) ) {
       
   304 					$title = ' title="' . esc_attr($feed) . '"';
       
   305 					$alt = ' alt="' . esc_attr($feed) . '"';
       
   306 					$name = $feed;
       
   307 					$link .= $title;
       
   308 				}
       
   309 
       
   310 				$link .= '>';
       
   311 
       
   312 				if ( !empty($feed_image) )
       
   313 					$link .= "<img src=\"" . esc_url($feed_image) . "\" style=\"border: none;\"$alt$title" . ' />';
       
   314 				else
       
   315 					$link .= $name;
       
   316 
       
   317 				$link .= '</a>';
       
   318 
       
   319 				if ( empty($feed_image) )
       
   320 					$link .= ')';
       
   321 			}
       
   322 
       
   323 			if ( $optioncount )
       
   324 				$link .= ' ('. $posts . ')';
       
   325 
       
   326 		}
       
   327 
       
   328 		if ( !($posts == 0 && $hide_empty) && 'list' == $style )
       
   329 			$return .= $link . '</li>';
       
   330 		else if ( ! $hide_empty )
       
   331 			$return .= $link . ', ';
       
   332 	}
       
   333 
       
   334 	$return = trim($return, ', ');
       
   335 
       
   336 	if ( ! $echo )
       
   337 		return $return;
       
   338 	echo $return;
       
   339 }
       
   340 
       
   341 ?>