~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-includes/author-template.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.0
 
17
 *
 
18
 * @uses $authordata The current author's DB object.
 
19
 * @uses apply_filters() Calls 'the_author' hook on the author display name.
 
20
 *
 
21
 * @param string $deprecated Deprecated.
 
22
 * @return string The author's display name.
 
23
 */
 
24
function get_the_author($deprecated = '') {
 
25
        global $authordata;
 
26
 
 
27
        if ( !empty( $deprecated ) )
 
28
                _deprecated_argument( __FUNCTION__, '2.1' );
 
29
 
 
30
        /**
 
31
         * Filter the display name of the current post's author.
 
32
         *
 
33
         * @since 2.9.0
 
34
         *
 
35
         * @param string $authordata->display_name The author's display name.
 
36
         */
 
37
        return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null);
 
38
}
 
39
 
 
40
/**
 
41
 * Display the name of the author of the current post.
 
42
 *
 
43
 * The behavior of this function is based off of old functionality predating
 
44
 * get_the_author(). This function is not deprecated, but is designed to echo
 
45
 * the value from get_the_author() and as an result of any old theme that might
 
46
 * still use the old behavior will also pass the value from get_the_author().
 
47
 *
 
48
 * The normal, expected behavior of this function is to echo the author and not
 
49
 * return it. However, backwards compatibility has to be maintained.
 
50
 *
 
51
 * @since 0.71
 
52
 * @see get_the_author()
 
53
 * @link http://codex.wordpress.org/Template_Tags/the_author
 
54
 *
 
55
 * @param string $deprecated Deprecated.
 
56
 * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
 
57
 * @return string The author's display name, from get_the_author().
 
58
 */
 
59
function the_author( $deprecated = '', $deprecated_echo = true ) {
 
60
        if ( !empty( $deprecated ) )
 
61
                _deprecated_argument( __FUNCTION__, '2.1' );
 
62
        if ( $deprecated_echo !== true )
 
63
                _deprecated_argument( __FUNCTION__, '1.5', __('Use <code>get_the_author()</code> instead if you do not want the value echoed.') );
 
64
        if ( $deprecated_echo )
 
65
                echo get_the_author();
 
66
        return get_the_author();
 
67
}
 
68
 
 
69
/**
 
70
 * Retrieve the author who last edited the current post.
 
71
 *
 
72
 * @since 2.8.0
 
73
 *
 
74
 * @uses $post The current post's DB object.
 
75
 * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
 
76
 * @uses get_userdata() Retrieves the author's DB object.
 
77
 * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
 
78
 * @return string The author's display name.
 
79
 */
 
80
function get_the_modified_author() {
 
81
        if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
 
82
                $last_user = get_userdata($last_id);
 
83
 
 
84
                /**
 
85
                 * Filter the display name of the author who last edited the current post.
 
86
                 *
 
87
                 * @since 2.8.0
 
88
                 *
 
89
                 * @param string $last_user->display_name The author's display name.
 
90
                 */
 
91
                return apply_filters('the_modified_author', $last_user->display_name);
 
92
        }
 
93
}
 
94
 
 
95
/**
 
96
 * Display the name of the author who last edited the current post.
 
97
 *
 
98
 * @since 2.8.0
 
99
 *
 
100
 * @see get_the_author()
 
101
 * @return string The author's display name, from get_the_modified_author().
 
102
 */
 
103
function the_modified_author() {
 
104
        echo get_the_modified_author();
 
105
}
 
106
 
 
107
/**
 
108
 * Retrieve the requested data of the author of the current post.
 
109
 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
 
110
 * @since 2.8.0
 
111
 * @uses $authordata The current author's DB object (if $user_id not specified).
 
112
 * @param string $field selects the field of the users record.
 
113
 * @param int $user_id Optional. User ID.
 
114
 * @return string The author's field from the current author's DB object.
 
115
 */
 
116
function get_the_author_meta( $field = '', $user_id = false ) {
 
117
        if ( ! $user_id ) {
 
118
                global $authordata;
 
119
                $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
 
120
        } else {
 
121
                $authordata = get_userdata( $user_id );
 
122
        }
 
123
 
 
124
        if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
 
125
                $field = 'user_' . $field;
 
126
 
 
127
        $value = isset( $authordata->$field ) ? $authordata->$field : '';
 
128
 
 
129
        /**
 
130
         * Filter the value of the requested user metadata.
 
131
         *
 
132
         * The filter name is dynamic and depends on the $field parameter of the function.
 
133
         *
 
134
         * @since 2.8.0
 
135
         *
 
136
         * @param string $value   The value of the metadata.
 
137
         * @param int    $user_id The user ID.
 
138
         */
 
139
        return apply_filters( 'get_the_author_' . $field, $value, $user_id );
 
140
}
 
141
 
 
142
/**
 
143
 * Retrieve the requested data of the author of the current post.
 
144
 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
 
145
 * @since 2.8.0
 
146
 * @param string $field selects the field of the users record.
 
147
 * @param int $user_id Optional. User ID.
 
148
 * @echo string The author's field from the current author's DB object.
 
149
 */
 
150
function the_author_meta( $field = '', $user_id = false ) {
 
151
        $author_meta = get_the_author_meta( $field, $user_id );
 
152
 
 
153
        /**
 
154
         * The value of the requested user metadata.
 
155
         *
 
156
         * The filter name is dynamic and depends on the $field parameter of the function.
 
157
         *
 
158
         * @since 2.8.0
 
159
         *
 
160
         * @param string $author_meta The value of the metadata.
 
161
         * @param int    $user_id     The user ID.
 
162
         */
 
163
        echo apply_filters( 'the_author_' . $field, $author_meta, $user_id );
 
164
}
 
165
 
 
166
/**
 
167
 * Retrieve either author's link or author's name.
 
168
 *
 
169
 * If the author has a home page set, return an HTML link, otherwise just return the
 
170
 * author's name.
 
171
 *
 
172
 * @uses get_the_author_meta()
 
173
 * @uses get_the_author()
 
174
 */
 
175
function get_the_author_link() {
 
176
        if ( get_the_author_meta('url') ) {
 
177
                return '<a href="' . esc_url( get_the_author_meta('url') ) . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), get_the_author()) ) . '" rel="author external">' . get_the_author() . '</a>';
 
178
        } else {
 
179
                return get_the_author();
 
180
        }
 
181
}
 
182
 
 
183
/**
 
184
 * Display either author's link or author's name.
 
185
 *
 
186
 * If the author has a home page set, echo an HTML link, otherwise just echo the
 
187
 * author's name.
 
188
 *
 
189
 * @link http://codex.wordpress.org/Template_Tags/the_author_link
 
190
 *
 
191
 * @since 2.1.0
 
192
 *
 
193
 * @uses get_the_author_link()
 
194
 */
 
195
function the_author_link() {
 
196
        echo get_the_author_link();
 
197
}
 
198
 
 
199
/**
 
200
 * Retrieve the number of posts by the author of the current post.
 
201
 *
 
202
 * @since 1.5.0
 
203
 *
 
204
 * @uses $post The current post in the Loop's DB object.
 
205
 * @uses count_user_posts()
 
206
 * @return int The number of posts by the author.
 
207
 */
 
208
function get_the_author_posts() {
 
209
        $post = get_post();
 
210
        if ( ! $post ) {
 
211
                return 0;
 
212
        }
 
213
        return count_user_posts( $post->post_author );
 
214
}
 
215
 
 
216
/**
 
217
 * Display the number of posts by the author of the current post.
 
218
 *
 
219
 * @link http://codex.wordpress.org/Template_Tags/the_author_posts
 
220
 * @since 0.71
 
221
 * @uses get_the_author_posts() Echoes returned value from function.
 
222
 */
 
223
function the_author_posts() {
 
224
        echo get_the_author_posts();
 
225
}
 
226
 
 
227
/**
 
228
 * Display an HTML link to the author page of the author of the current post.
 
229
 *
 
230
 * Does just echo get_author_posts_url() function, like the others do. The
 
231
 * reason for this, is that another function is used to help in printing the
 
232
 * link to the author's posts.
 
233
 *
 
234
 * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
 
235
 * @since 1.2.0
 
236
 * @uses $authordata The current author's DB object.
 
237
 * @uses get_author_posts_url()
 
238
 * @uses get_the_author()
 
239
 * @param string $deprecated Deprecated.
 
240
 */
 
241
function the_author_posts_link($deprecated = '') {
 
242
        if ( !empty( $deprecated ) )
 
243
                _deprecated_argument( __FUNCTION__, '2.1' );
 
244
 
 
245
        global $authordata;
 
246
        if ( !is_object( $authordata ) )
 
247
                return false;
 
248
        $link = sprintf(
 
249
                '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
 
250
                esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
 
251
                esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
 
252
                get_the_author()
 
253
        );
 
254
 
 
255
        /**
 
256
         * Filter the link to the author page of the author of the current post.
 
257
         *
 
258
         * @since 2.9.0
 
259
         *
 
260
         * @param string $link HTML link.
 
261
         */
 
262
        echo apply_filters( 'the_author_posts_link', $link );
 
263
}
 
264
 
 
265
/**
 
266
 * Retrieve the URL to the author page for the user with the ID provided.
 
267
 *
 
268
 * @since 2.1.0
 
269
 * @uses $wp_rewrite WP_Rewrite
 
270
 * @return string The URL to the author's page.
 
271
 */
 
272
function get_author_posts_url($author_id, $author_nicename = '') {
 
273
        global $wp_rewrite;
 
274
        $auth_ID = (int) $author_id;
 
275
        $link = $wp_rewrite->get_author_permastruct();
 
276
 
 
277
        if ( empty($link) ) {
 
278
                $file = home_url( '/' );
 
279
                $link = $file . '?author=' . $auth_ID;
 
280
        } else {
 
281
                if ( '' == $author_nicename ) {
 
282
                        $user = get_userdata($author_id);
 
283
                        if ( !empty($user->user_nicename) )
 
284
                                $author_nicename = $user->user_nicename;
 
285
                }
 
286
                $link = str_replace('%author%', $author_nicename, $link);
 
287
                $link = home_url( user_trailingslashit( $link ) );
 
288
        }
 
289
 
 
290
        /**
 
291
         * Filter the URL to the author's page.
 
292
         *
 
293
         * @since 2.1.0
 
294
         *
 
295
         * @param string $link            The URL to the author's page.
 
296
         * @param int    $author_id       The author's id.
 
297
         * @param string $author_nicename The author's nice name.
 
298
         */
 
299
        $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
 
300
 
 
301
        return $link;
 
302
}
 
303
 
 
304
/**
 
305
 * List all the authors of the blog, with several options available.
 
306
 *
 
307
 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
 
308
 *
 
309
 * @since 1.2.0
 
310
 *
 
311
 * @param string|array $args {
 
312
 *     Optional. Array or string of default arguments.
 
313
 *
 
314
 *     @type string $orderby       How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
 
315
 *                                 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
 
316
 *                                 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
 
317
 *     @type string $order         Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
 
318
 *     @type int    $number        Maximum authors to return or display. Default empty (all authors).
 
319
 *     @type bool   $optioncount   Show the count in parenthesis next to the author's name. Default false.
 
320
 *     @type bool   $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
 
321
 *     @type bool   $show_fullname Whether to show the author's full name. Default false.
 
322
 *     @type bool   $hide_empty    Whether to hide any authors with no posts. Default true.
 
323
 *     @type string $feed          If not empty, show a link to the author's feed and use this text as the alt
 
324
 *                                 parameter of the link. Default empty.
 
325
 *     @type string $feed_image    If not empty, show a link to the author's feed and use this image URL as
 
326
 *                                 clickable anchor. Default empty.
 
327
 *     @type string $feed_type     The feed type to link to, such as 'rss2'. Defaults to default feed type.
 
328
 *     @type bool   $echo          Whether to output the result or instead return it. Default true.
 
329
 *     @type string $style         If 'list', each author is wrapped in an `<li>` element, otherwise the authors
 
330
 *                                 will be separated by commas.
 
331
 *     @type bool   $html          Whether to list the items in HTML form or plaintext. Default true.
 
332
 *     @type string $exclude       An array, comma-, or space-separated list of author IDs to exclude. Default empty.
 
333
 *     @type string $exclude       An array, comma-, or space-separated list of author IDs to include. Default empty.
 
334
 * }
 
335
 * @return null|string The output, if echo is set to false. Otherwise null.
 
336
 */
 
337
function wp_list_authors( $args = '' ) {
 
338
        global $wpdb;
 
339
 
 
340
        $defaults = array(
 
341
                'orderby' => 'name', 'order' => 'ASC', 'number' => '',
 
342
                'optioncount' => false, 'exclude_admin' => true,
 
343
                'show_fullname' => false, 'hide_empty' => true,
 
344
                'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
 
345
                'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
 
346
        );
 
347
 
 
348
        $args = wp_parse_args( $args, $defaults );
 
349
 
 
350
        $return = '';
 
351
 
 
352
        $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
 
353
        $query_args['fields'] = 'ids';
 
354
        $authors = get_users( $query_args );
 
355
 
 
356
        $author_count = array();
 
357
        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 ) {
 
358
                $author_count[$row->post_author] = $row->count;
 
359
        }
 
360
        foreach ( $authors as $author_id ) {
 
361
                $author = get_userdata( $author_id );
 
362
 
 
363
                if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
 
364
                        continue;
 
365
                }
 
366
 
 
367
                $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
 
368
 
 
369
                if ( ! $posts && $args['hide_empty'] ) {
 
370
                        continue;
 
371
                }
 
372
 
 
373
                if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
 
374
                        $name = "$author->first_name $author->last_name";
 
375
                } else {
 
376
                        $name = $author->display_name;
 
377
                }
 
378
 
 
379
                if ( ! $args['html'] ) {
 
380
                        $return .= $name . ', ';
 
381
 
 
382
                        continue; // No need to go further to process HTML.
 
383
                }
 
384
 
 
385
                if ( 'list' == $args['style'] ) {
 
386
                        $return .= '<li>';
 
387
                }
 
388
 
 
389
                $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
 
390
 
 
391
                if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
 
392
                        $link .= ' ';
 
393
                        if ( empty( $args['feed_image'] ) ) {
 
394
                                $link .= '(';
 
395
                        }
 
396
 
 
397
                        $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
 
398
 
 
399
                        $alt = '';
 
400
                        if ( ! empty( $args['feed'] ) ) {
 
401
                                $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
 
402
                                $name = $args['feed'];
 
403
                        }
 
404
 
 
405
                        $link .= '>';
 
406
 
 
407
                        if ( ! empty( $args['feed_image'] ) ) {
 
408
                                $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
 
409
                        } else {
 
410
                                $link .= $name;
 
411
                        }
 
412
 
 
413
                        $link .= '</a>';
 
414
 
 
415
                        if ( empty( $args['feed_image'] ) ) {
 
416
                                $link .= ')';
 
417
                        }
 
418
                }
 
419
 
 
420
                if ( $args['optioncount'] ) {
 
421
                        $link .= ' ('. $posts . ')';
 
422
                }
 
423
 
 
424
                $return .= $link;
 
425
                $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
 
426
        }
 
427
 
 
428
        $return = rtrim( $return, ', ' );
 
429
 
 
430
        if ( ! $args['echo'] ) {
 
431
                return $return;
 
432
        }
 
433
        echo $return;
 
434
}
 
435
 
 
436
/**
 
437
 * Does this site have more than one author
 
438
 *
 
439
 * Checks to see if more than one author has published posts.
 
440
 *
 
441
 * @since 3.2.0
 
442
 * @return bool Whether or not we have more than one author
 
443
 */
 
444
function is_multi_author() {
 
445
        global $wpdb;
 
446
 
 
447
        if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
 
448
                $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
 
449
                $is_multi_author = 1 < count( $rows ) ? 1 : 0;
 
450
                set_transient( 'is_multi_author', $is_multi_author );
 
451
        }
 
452
 
 
453
        /**
 
454
         * Filter whether the site has more than one author with published posts.
 
455
         *
 
456
         * @since 3.2.0
 
457
         *
 
458
         * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
 
459
         */
 
460
        return apply_filters( 'is_multi_author', (bool) $is_multi_author );
 
461
}
 
462
 
 
463
/**
 
464
 * Helper function to clear the cache for number of authors.
 
465
 *
 
466
 * @private
 
467
 */
 
468
function __clear_multi_author_cache() {
 
469
        delete_transient( 'is_multi_author' );
 
470
}
 
471
add_action('transition_post_status', '__clear_multi_author_cache');