author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Author Template functions for use in themes. |
|
4 |
* |
|
5 |
* These functions must be used within the WordPress Loop. |
|
6 |
* |
|
5 | 7 |
* @link https://codex.wordpress.org/Author_Templates |
0 | 8 |
* |
9 |
* @package WordPress |
|
10 |
* @subpackage Template |
|
11 |
*/ |
|
12 |
||
13 |
/** |
|
14 |
* Retrieve the author of the current post. |
|
15 |
* |
|
5 | 16 |
* @since 1.5.0 |
17 |
* |
|
16 | 18 |
* @global WP_User $authordata The current author's data. |
0 | 19 |
* |
20 |
* @param string $deprecated Deprecated. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* @return string|null The author's display name. |
0 | 22 |
*/ |
9 | 23 |
function get_the_author( $deprecated = '' ) { |
0 | 24 |
global $authordata; |
25 |
||
9 | 26 |
if ( ! empty( $deprecated ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
_deprecated_argument( __FUNCTION__, '2.1.0' ); |
9 | 28 |
} |
0 | 29 |
|
30 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
* Filters the display name of the current post's author. |
0 | 32 |
* |
33 |
* @since 2.9.0 |
|
34 |
* |
|
16 | 35 |
* @param string|null $display_name The author's display name. |
0 | 36 |
*/ |
9 | 37 |
return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null ); |
0 | 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 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* return it. However, backward compatibility has to be maintained. |
0 | 50 |
* |
51 |
* @since 0.71 |
|
16 | 52 |
* |
0 | 53 |
* @see get_the_author() |
16 | 54 |
* @link https://developer.wordpress.org/reference/functions/the_author/ |
0 | 55 |
* |
9 | 56 |
* @param string $deprecated Deprecated. |
57 |
* @param bool $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* @return string|null The author's display name, from get_the_author(). |
0 | 59 |
*/ |
60 |
function the_author( $deprecated = '', $deprecated_echo = true ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
if ( ! empty( $deprecated ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
_deprecated_argument( __FUNCTION__, '2.1.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
if ( true !== $deprecated_echo ) { |
9 | 66 |
_deprecated_argument( |
67 |
__FUNCTION__, |
|
68 |
'1.5.0', |
|
69 |
sprintf( |
|
16 | 70 |
/* translators: %s: get_the_author() */ |
9 | 71 |
__( 'Use %s instead if you do not want the value echoed.' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
'<code>get_the_author()</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
if ( $deprecated_echo ) { |
0 | 78 |
echo get_the_author(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
|
0 | 81 |
return get_the_author(); |
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Retrieve the author who last edited the current post. |
|
86 |
* |
|
5 | 87 |
* @since 2.8.0 |
88 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
* @return string|void The author's display name. |
0 | 90 |
*/ |
91 |
function get_the_modified_author() { |
|
9 | 92 |
$last_id = get_post_meta( get_post()->ID, '_edit_last', true ); |
93 |
||
94 |
if ( $last_id ) { |
|
95 |
$last_user = get_userdata( $last_id ); |
|
0 | 96 |
|
97 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
* Filters the display name of the author who last edited the current post. |
0 | 99 |
* |
100 |
* @since 2.8.0 |
|
101 |
* |
|
16 | 102 |
* @param string $display_name The author's display name. |
0 | 103 |
*/ |
9 | 104 |
return apply_filters( 'the_modified_author', $last_user->display_name ); |
0 | 105 |
} |
106 |
} |
|
107 |
||
108 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
* Display the name of the author who last edited the current post, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
* if the author's ID is available. |
0 | 111 |
* |
5 | 112 |
* @since 2.8.0 |
113 |
* |
|
0 | 114 |
* @see get_the_author() |
115 |
*/ |
|
116 |
function the_modified_author() { |
|
117 |
echo get_the_modified_author(); |
|
118 |
} |
|
119 |
||
120 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
* Retrieves the requested data of the author of the current post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
* Valid values for the `$field` parameter include: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* - admin_color |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* - aim |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
* - comment_shortcuts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
* - description |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
* - display_name |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
* - first_name |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
131 |
* - ID |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
* - jabber |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
* - last_name |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
* - nickname |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* - plugins_last_view |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
* - plugins_per_page |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
* - rich_editing |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
* - syntax_highlighting |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
* - user_activation_key |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
* - user_description |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
* - user_email |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
* - user_firstname |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
* - user_lastname |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
* - user_level |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
* - user_login |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
* - user_nicename |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
* - user_pass |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
* - user_registered |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
* - user_status |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
* - user_url |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
* - yim |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* |
0 | 153 |
* @since 2.8.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
* |
16 | 155 |
* @global WP_User $authordata The current author's data. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
* |
9 | 157 |
* @param string $field Optional. The user field to retrieve. Default empty. |
158 |
* @param int|false $user_id Optional. User ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
* @return string The author's field from the current author's DB object, otherwise an empty string. |
0 | 160 |
*/ |
161 |
function get_the_author_meta( $field = '', $user_id = false ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
$original_user_id = $user_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
|
0 | 164 |
if ( ! $user_id ) { |
165 |
global $authordata; |
|
166 |
$user_id = isset( $authordata->ID ) ? $authordata->ID : 0; |
|
167 |
} else { |
|
168 |
$authordata = get_userdata( $user_id ); |
|
169 |
} |
|
170 |
||
16 | 171 |
if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ), true ) ) { |
0 | 172 |
$field = 'user_' . $field; |
9 | 173 |
} |
0 | 174 |
|
175 |
$value = isset( $authordata->$field ) ? $authordata->$field : ''; |
|
176 |
||
177 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* Filters the value of the requested user metadata. |
0 | 179 |
* |
180 |
* The filter name is dynamic and depends on the $field parameter of the function. |
|
181 |
* |
|
182 |
* @since 2.8.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
* @since 4.3.0 The `$original_user_id` parameter was added. |
0 | 184 |
* |
9 | 185 |
* @param string $value The value of the metadata. |
186 |
* @param int $user_id The user ID for the value. |
|
187 |
* @param int|false $original_user_id The original user ID, as passed to the function. |
|
0 | 188 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
return apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id ); |
0 | 190 |
} |
191 |
||
192 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
* Outputs the field from the user's DB object. Defaults to current post's author. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
* |
0 | 195 |
* @since 2.8.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
* |
9 | 197 |
* @param string $field Selects the field of the users record. See get_the_author_meta() |
198 |
* for the list of possible fields. |
|
199 |
* @param int|false $user_id Optional. User ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
* @see get_the_author_meta() |
0 | 202 |
*/ |
203 |
function the_author_meta( $field = '', $user_id = false ) { |
|
204 |
$author_meta = get_the_author_meta( $field, $user_id ); |
|
205 |
||
206 |
/** |
|
207 |
* The value of the requested user metadata. |
|
208 |
* |
|
209 |
* The filter name is dynamic and depends on the $field parameter of the function. |
|
210 |
* |
|
211 |
* @since 2.8.0 |
|
212 |
* |
|
9 | 213 |
* @param string $author_meta The value of the metadata. |
214 |
* @param int|false $user_id The user ID. |
|
0 | 215 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
216 |
echo apply_filters( "the_author_{$field}", $author_meta, $user_id ); |
0 | 217 |
} |
218 |
||
219 |
/** |
|
220 |
* Retrieve either author's link or author's name. |
|
221 |
* |
|
222 |
* If the author has a home page set, return an HTML link, otherwise just return the |
|
223 |
* author's name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
* @return string|null An HTML link if the author's url exist in user meta, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
* else the result of get_the_author(). |
0 | 229 |
*/ |
230 |
function get_the_author_link() { |
|
9 | 231 |
if ( get_the_author_meta( 'url' ) ) { |
232 |
return sprintf( |
|
233 |
'<a href="%1$s" title="%2$s" rel="author external">%3$s</a>', |
|
234 |
esc_url( get_the_author_meta( 'url' ) ), |
|
16 | 235 |
/* translators: %s: Author's display name. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
esc_attr( sprintf( __( 'Visit %s’s website' ), get_the_author() ) ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
get_the_author() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
); |
0 | 239 |
} else { |
240 |
return get_the_author(); |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* Display either author's link or author's name. |
|
246 |
* |
|
247 |
* If the author has a home page set, echo an HTML link, otherwise just echo the |
|
248 |
* author's name. |
|
249 |
* |
|
16 | 250 |
* @link https://developer.wordpress.org/reference/functions/the_author_link/ |
5 | 251 |
* |
252 |
* @since 2.1.0 |
|
0 | 253 |
*/ |
254 |
function the_author_link() { |
|
255 |
echo get_the_author_link(); |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Retrieve the number of posts by the author of the current post. |
|
260 |
* |
|
5 | 261 |
* @since 1.5.0 |
262 |
* |
|
0 | 263 |
* @return int The number of posts by the author. |
264 |
*/ |
|
265 |
function get_the_author_posts() { |
|
5 | 266 |
$post = get_post(); |
267 |
if ( ! $post ) { |
|
268 |
return 0; |
|
269 |
} |
|
270 |
return count_user_posts( $post->post_author, $post->post_type ); |
|
0 | 271 |
} |
272 |
||
273 |
/** |
|
274 |
* Display the number of posts by the author of the current post. |
|
275 |
* |
|
16 | 276 |
* @link https://developer.wordpress.org/reference/functions/the_author_posts/ |
0 | 277 |
* @since 0.71 |
278 |
*/ |
|
279 |
function the_author_posts() { |
|
280 |
echo get_the_author_posts(); |
|
281 |
} |
|
282 |
||
283 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
* Retrieves an HTML link to the author page of the current post's author. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
* Returns an HTML-formatted link using get_author_posts_url(). |
0 | 287 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
* @since 4.4.0 |
0 | 289 |
* |
16 | 290 |
* @global WP_User $authordata The current author's data. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
* |
9 | 292 |
* @return string An HTML link to the author page, or an empty string if $authordata isn't defined. |
0 | 293 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
294 |
function get_the_author_posts_link() { |
0 | 295 |
global $authordata; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
if ( ! is_object( $authordata ) ) { |
9 | 297 |
return ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
299 |
|
9 | 300 |
$link = sprintf( |
301 |
'<a href="%1$s" title="%2$s" rel="author">%3$s</a>', |
|
0 | 302 |
esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ), |
16 | 303 |
/* translators: %s: Author's display name. */ |
0 | 304 |
esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ), |
305 |
get_the_author() |
|
306 |
); |
|
307 |
||
308 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
* Filters the link to the author page of the author of the current post. |
0 | 310 |
* |
311 |
* @since 2.9.0 |
|
312 |
* |
|
313 |
* @param string $link HTML link. |
|
314 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
return apply_filters( 'the_author_posts_link', $link ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
* Displays an HTML link to the author page of the current post's author. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
* @since 1.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
* @since 4.4.0 Converted into a wrapper for get_the_author_posts_link() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* @param string $deprecated Unused. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
function the_author_posts_link( $deprecated = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
if ( ! empty( $deprecated ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
_deprecated_argument( __FUNCTION__, '2.1.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
echo get_the_author_posts_link(); |
0 | 331 |
} |
332 |
||
333 |
/** |
|
334 |
* Retrieve the URL to the author page for the user with the ID provided. |
|
335 |
* |
|
336 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
* |
16 | 338 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
* @param int $author_id Author ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
* @param string $author_nicename Optional. The author's nicename (slug). Default empty. |
0 | 342 |
* @return string The URL to the author's page. |
343 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
function get_author_posts_url( $author_id, $author_nicename = '' ) { |
0 | 345 |
global $wp_rewrite; |
346 |
$auth_ID = (int) $author_id; |
|
9 | 347 |
$link = $wp_rewrite->get_author_permastruct(); |
0 | 348 |
|
9 | 349 |
if ( empty( $link ) ) { |
0 | 350 |
$file = home_url( '/' ); |
351 |
$link = $file . '?author=' . $auth_ID; |
|
352 |
} else { |
|
16 | 353 |
if ( '' === $author_nicename ) { |
9 | 354 |
$user = get_userdata( $author_id ); |
355 |
if ( ! empty( $user->user_nicename ) ) { |
|
0 | 356 |
$author_nicename = $user->user_nicename; |
9 | 357 |
} |
0 | 358 |
} |
9 | 359 |
$link = str_replace( '%author%', $author_nicename, $link ); |
0 | 360 |
$link = home_url( user_trailingslashit( $link ) ); |
361 |
} |
|
362 |
||
363 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
* Filters the URL to the author's page. |
0 | 365 |
* |
366 |
* @since 2.1.0 |
|
367 |
* |
|
368 |
* @param string $link The URL to the author's page. |
|
16 | 369 |
* @param int $author_id The author's ID. |
0 | 370 |
* @param string $author_nicename The author's nice name. |
371 |
*/ |
|
372 |
$link = apply_filters( 'author_link', $link, $author_id, $author_nicename ); |
|
373 |
||
374 |
return $link; |
|
375 |
} |
|
376 |
||
377 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
378 |
* List all the authors of the site, with several options available. |
0 | 379 |
* |
16 | 380 |
* @link https://developer.wordpress.org/reference/functions/wp_list_authors/ |
5 | 381 |
* |
382 |
* @since 1.2.0 |
|
383 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
* |
5 | 386 |
* @param string|array $args { |
387 |
* Optional. Array or string of default arguments. |
|
0 | 388 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
* @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
* 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
* 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
* @type int $number Maximum authors to return or display. Default empty (all authors). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
* @type bool $optioncount Show the count in parenthesis next to the author's name. Default false. |
9 | 395 |
* @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default true. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
* @type bool $show_fullname Whether to show the author's full name. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
* @type bool $hide_empty Whether to hide any authors with no posts. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* @type string $feed If not empty, show a link to the author's feed and use this text as the alt |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* parameter of the link. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
* @type string $feed_image If not empty, show a link to the author's feed and use this image URL as |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
* clickable anchor. Default empty. |
16 | 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(). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
* @type bool $echo Whether to output the result or instead return it. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
* @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
* will be separated by commas. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
* @type bool $html Whether to list the items in HTML form or plaintext. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
* @type array|string $exclude Array or comma/space-separated list of author IDs to exclude. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
* @type array|string $include Array or comma/space-separated list of author IDs to include. Default empty. |
5 | 410 |
* } |
16 | 411 |
* @return void|string Void if 'echo' argument is true, list of authors if 'echo' is false. |
0 | 412 |
*/ |
5 | 413 |
function wp_list_authors( $args = '' ) { |
0 | 414 |
global $wpdb; |
415 |
||
416 |
$defaults = array( |
|
9 | 417 |
'orderby' => 'name', |
418 |
'order' => 'ASC', |
|
419 |
'number' => '', |
|
420 |
'optioncount' => false, |
|
421 |
'exclude_admin' => true, |
|
422 |
'show_fullname' => false, |
|
423 |
'hide_empty' => true, |
|
424 |
'feed' => '', |
|
425 |
'feed_image' => '', |
|
426 |
'feed_type' => '', |
|
427 |
'echo' => true, |
|
428 |
'style' => 'list', |
|
429 |
'html' => true, |
|
430 |
'exclude' => '', |
|
431 |
'include' => '', |
|
0 | 432 |
); |
433 |
||
434 |
$args = wp_parse_args( $args, $defaults ); |
|
435 |
||
436 |
$return = ''; |
|
437 |
||
9 | 438 |
$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) ); |
0 | 439 |
$query_args['fields'] = 'ids'; |
9 | 440 |
$authors = get_users( $query_args ); |
0 | 441 |
|
442 |
$author_count = array(); |
|
9 | 443 |
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 ) { |
444 |
$author_count[ $row->post_author ] = $row->count; |
|
5 | 445 |
} |
0 | 446 |
foreach ( $authors as $author_id ) { |
9 | 447 |
$posts = isset( $author_count[ $author_id ] ) ? $author_count[ $author_id ] : 0; |
0 | 448 |
|
9 | 449 |
if ( ! $posts && $args['hide_empty'] ) { |
0 | 450 |
continue; |
5 | 451 |
} |
0 | 452 |
|
9 | 453 |
$author = get_userdata( $author_id ); |
0 | 454 |
|
9 | 455 |
if ( $args['exclude_admin'] && 'admin' === $author->display_name ) { |
0 | 456 |
continue; |
5 | 457 |
} |
0 | 458 |
|
5 | 459 |
if ( $args['show_fullname'] && $author->first_name && $author->last_name ) { |
0 | 460 |
$name = "$author->first_name $author->last_name"; |
5 | 461 |
} else { |
0 | 462 |
$name = $author->display_name; |
5 | 463 |
} |
0 | 464 |
|
5 | 465 |
if ( ! $args['html'] ) { |
0 | 466 |
$return .= $name . ', '; |
467 |
||
468 |
continue; // No need to go further to process HTML. |
|
469 |
} |
|
470 |
||
16 | 471 |
if ( 'list' === $args['style'] ) { |
0 | 472 |
$return .= '<li>'; |
473 |
} |
|
474 |
||
9 | 475 |
$link = sprintf( |
476 |
'<a href="%1$s" title="%2$s">%3$s</a>', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
get_author_posts_url( $author->ID, $author->user_nicename ), |
16 | 478 |
/* translators: %s: Author's display name. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
$name |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
); |
0 | 482 |
|
5 | 483 |
if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) { |
0 | 484 |
$link .= ' '; |
5 | 485 |
if ( empty( $args['feed_image'] ) ) { |
0 | 486 |
$link .= '('; |
487 |
} |
|
488 |
||
5 | 489 |
$link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"'; |
0 | 490 |
|
5 | 491 |
$alt = ''; |
492 |
if ( ! empty( $args['feed'] ) ) { |
|
9 | 493 |
$alt = ' alt="' . esc_attr( $args['feed'] ) . '"'; |
5 | 494 |
$name = $args['feed']; |
0 | 495 |
} |
496 |
||
497 |
$link .= '>'; |
|
498 |
||
5 | 499 |
if ( ! empty( $args['feed_image'] ) ) { |
500 |
$link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />'; |
|
501 |
} else { |
|
0 | 502 |
$link .= $name; |
5 | 503 |
} |
0 | 504 |
|
505 |
$link .= '</a>'; |
|
506 |
||
5 | 507 |
if ( empty( $args['feed_image'] ) ) { |
0 | 508 |
$link .= ')'; |
5 | 509 |
} |
0 | 510 |
} |
511 |
||
5 | 512 |
if ( $args['optioncount'] ) { |
9 | 513 |
$link .= ' (' . $posts . ')'; |
5 | 514 |
} |
0 | 515 |
|
516 |
$return .= $link; |
|
16 | 517 |
$return .= ( 'list' === $args['style'] ) ? '</li>' : ', '; |
0 | 518 |
} |
519 |
||
5 | 520 |
$return = rtrim( $return, ', ' ); |
0 | 521 |
|
16 | 522 |
if ( $args['echo'] ) { |
523 |
echo $return; |
|
524 |
} else { |
|
0 | 525 |
return $return; |
5 | 526 |
} |
0 | 527 |
} |
528 |
||
529 |
/** |
|
9 | 530 |
* Determines whether this site has more than one author. |
0 | 531 |
* |
532 |
* Checks to see if more than one author has published posts. |
|
533 |
* |
|
9 | 534 |
* For more information on this and similar theme functions, check out |
535 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
536 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
537 |
* |
|
0 | 538 |
* @since 3.2.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
540 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
* |
0 | 542 |
* @return bool Whether or not we have more than one author |
543 |
*/ |
|
544 |
function is_multi_author() { |
|
545 |
global $wpdb; |
|
546 |
||
16 | 547 |
$is_multi_author = get_transient( 'is_multi_author' ); |
548 |
if ( false === $is_multi_author ) { |
|
9 | 549 |
$rows = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" ); |
0 | 550 |
$is_multi_author = 1 < count( $rows ) ? 1 : 0; |
551 |
set_transient( 'is_multi_author', $is_multi_author ); |
|
552 |
} |
|
553 |
||
554 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
555 |
* Filters whether the site has more than one author with published posts. |
0 | 556 |
* |
557 |
* @since 3.2.0 |
|
558 |
* |
|
559 |
* @param bool $is_multi_author Whether $is_multi_author should evaluate as true. |
|
560 |
*/ |
|
561 |
return apply_filters( 'is_multi_author', (bool) $is_multi_author ); |
|
562 |
} |
|
563 |
||
564 |
/** |
|
565 |
* Helper function to clear the cache for number of authors. |
|
566 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
* @since 3.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
* @access private |
0 | 569 |
*/ |
16 | 570 |
function __clear_multi_author_cache() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
0 | 571 |
delete_transient( 'is_multi_author' ); |
572 |
} |