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