author | ymh <ymh.work@gmail.com> |
Tue, 22 Oct 2019 16:11:46 +0200 | |
changeset 15 | 3d4e9c994f10 |
parent 13 | d255fe9cd479 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* These functions can be replaced via plugins. If plugins do not redefine these |
|
4 |
* functions, then these will be used instead. |
|
5 |
* |
|
6 |
* @package WordPress |
|
7 |
*/ |
|
8 |
||
9 | 9 |
if ( ! function_exists( 'wp_set_current_user' ) ) : |
10 |
/** |
|
11 |
* Changes the current user by ID or name. |
|
12 |
* |
|
13 |
* Set $id to null and specify a name if you do not know a user's ID. |
|
14 |
* |
|
15 |
* Some WordPress functionality is based on the current user and not based on |
|
16 |
* the signed in user. Therefore, it opens the ability to edit and perform |
|
17 |
* actions on users who aren't signed in. |
|
18 |
* |
|
19 |
* @since 2.0.3 |
|
20 |
* @global WP_User $current_user The current user object which holds the user data. |
|
21 |
* |
|
22 |
* @param int $id User ID |
|
23 |
* @param string $name User's username |
|
24 |
* @return WP_User Current user User object |
|
25 |
*/ |
|
26 |
function wp_set_current_user( $id, $name = '' ) { |
|
27 |
global $current_user; |
|
0 | 28 |
|
9 | 29 |
// If `$id` matches the current user, there is nothing to do. |
30 |
if ( isset( $current_user ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
&& ( $current_user instanceof WP_User ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
&& ( $id == $current_user->ID ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
&& ( null !== $id ) |
9 | 34 |
) { |
35 |
return $current_user; |
|
36 |
} |
|
37 |
||
38 |
$current_user = new WP_User( $id, $name ); |
|
39 |
||
40 |
setup_userdata( $current_user->ID ); |
|
41 |
||
42 |
/** |
|
43 |
* Fires after the current user is set. |
|
44 |
* |
|
45 |
* @since 2.0.1 |
|
46 |
*/ |
|
47 |
do_action( 'set_current_user' ); |
|
48 |
||
0 | 49 |
return $current_user; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
} |
0 | 51 |
endif; |
52 |
||
9 | 53 |
if ( ! function_exists( 'wp_get_current_user' ) ) : |
54 |
/** |
|
55 |
* Retrieve the current user object. |
|
56 |
* |
|
57 |
* Will set the current user, if the current user is not set. The current user |
|
58 |
* will be set to the logged-in person. If no user is logged-in, then it will |
|
59 |
* set the current user to 0, which is invalid and won't have any permissions. |
|
60 |
* |
|
61 |
* @since 2.0.3 |
|
62 |
* |
|
63 |
* @see _wp_get_current_user() |
|
64 |
* @global WP_User $current_user Checks if the current user is set. |
|
65 |
* |
|
66 |
* @return WP_User Current WP_User instance. |
|
67 |
*/ |
|
68 |
function wp_get_current_user() { |
|
69 |
return _wp_get_current_user(); |
|
70 |
} |
|
0 | 71 |
endif; |
72 |
||
9 | 73 |
if ( ! function_exists( 'get_userdata' ) ) : |
74 |
/** |
|
75 |
* Retrieve user info by user ID. |
|
76 |
* |
|
77 |
* @since 0.71 |
|
78 |
* |
|
79 |
* @param int $user_id User ID |
|
80 |
* @return WP_User|false WP_User object on success, false on failure. |
|
81 |
*/ |
|
82 |
function get_userdata( $user_id ) { |
|
83 |
return get_user_by( 'id', $user_id ); |
|
84 |
} |
|
0 | 85 |
endif; |
86 |
||
9 | 87 |
if ( ! function_exists( 'get_user_by' ) ) : |
88 |
/** |
|
89 |
* Retrieve user info by a given field |
|
90 |
* |
|
91 |
* @since 2.8.0 |
|
92 |
* @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. |
|
93 |
* |
|
94 |
* @param string $field The field to retrieve the user with. id | ID | slug | email | login. |
|
95 |
* @param int|string $value A value for $field. A user ID, slug, email address, or login name. |
|
96 |
* @return WP_User|false WP_User object on success, false on failure. |
|
97 |
*/ |
|
98 |
function get_user_by( $field, $value ) { |
|
99 |
$userdata = WP_User::get_data_by( $field, $value ); |
|
0 | 100 |
|
9 | 101 |
if ( ! $userdata ) { |
102 |
return false; |
|
103 |
} |
|
0 | 104 |
|
9 | 105 |
$user = new WP_User; |
106 |
$user->init( $userdata ); |
|
0 | 107 |
|
9 | 108 |
return $user; |
109 |
} |
|
0 | 110 |
endif; |
111 |
||
9 | 112 |
if ( ! function_exists( 'cache_users' ) ) : |
113 |
/** |
|
114 |
* Retrieve info for user lists to prevent multiple queries by get_userdata() |
|
115 |
* |
|
116 |
* @since 3.0.0 |
|
117 |
* |
|
118 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
119 |
* |
|
120 |
* @param array $user_ids User ID numbers list |
|
121 |
*/ |
|
122 |
function cache_users( $user_ids ) { |
|
123 |
global $wpdb; |
|
0 | 124 |
|
9 | 125 |
$clean = _get_non_cached_ids( $user_ids, 'users' ); |
0 | 126 |
|
9 | 127 |
if ( empty( $clean ) ) { |
128 |
return; |
|
129 |
} |
|
0 | 130 |
|
9 | 131 |
$list = implode( ',', $clean ); |
0 | 132 |
|
9 | 133 |
$users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" ); |
0 | 134 |
|
9 | 135 |
$ids = array(); |
136 |
foreach ( $users as $user ) { |
|
137 |
update_user_caches( $user ); |
|
138 |
$ids[] = $user->ID; |
|
139 |
} |
|
140 |
update_meta_cache( 'user', $ids ); |
|
0 | 141 |
} |
142 |
endif; |
|
143 |
||
9 | 144 |
if ( ! function_exists( 'wp_mail' ) ) : |
5 | 145 |
/** |
9 | 146 |
* Send mail, similar to PHP's mail |
147 |
* |
|
148 |
* A true return value does not automatically mean that the user received the |
|
149 |
* email successfully. It just only means that the method used was able to |
|
150 |
* process the request without any errors. |
|
151 |
* |
|
152 |
* Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from |
|
153 |
* creating a from address like 'Name <email@address.com>' when both are set. If |
|
154 |
* just 'wp_mail_from' is set, then just the email address will be used with no |
|
155 |
* name. |
|
5 | 156 |
* |
9 | 157 |
* The default content type is 'text/plain' which does not allow using HTML. |
158 |
* However, you can set the content type of the email by using the |
|
159 |
* {@see 'wp_mail_content_type'} filter. |
|
160 |
* |
|
161 |
* The default charset is based on the charset used on the blog. The charset can |
|
162 |
* be set using the {@see 'wp_mail_charset'} filter. |
|
163 |
* |
|
164 |
* @since 1.2.1 |
|
165 |
* |
|
166 |
* @global PHPMailer $phpmailer |
|
5 | 167 |
* |
9 | 168 |
* @param string|array $to Array or comma-separated list of email addresses to send message. |
169 |
* @param string $subject Email subject |
|
170 |
* @param string $message Message contents |
|
171 |
* @param string|array $headers Optional. Additional headers. |
|
172 |
* @param string|array $attachments Optional. Files to attach. |
|
173 |
* @return bool Whether the email contents were sent successfully. |
|
5 | 174 |
*/ |
9 | 175 |
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { |
176 |
// Compact the input, apply the filters, and extract them back out |
|
5 | 177 |
|
9 | 178 |
/** |
179 |
* Filters the wp_mail() arguments. |
|
180 |
* |
|
181 |
* @since 2.2.0 |
|
182 |
* |
|
183 |
* @param array $args A compacted array of wp_mail() arguments, including the "to" email, |
|
184 |
* subject, message, headers, and attachments values. |
|
185 |
*/ |
|
186 |
$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ); |
|
0 | 187 |
|
9 | 188 |
if ( isset( $atts['to'] ) ) { |
189 |
$to = $atts['to']; |
|
190 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
|
9 | 192 |
if ( ! is_array( $to ) ) { |
193 |
$to = explode( ',', $to ); |
|
194 |
} |
|
5 | 195 |
|
9 | 196 |
if ( isset( $atts['subject'] ) ) { |
197 |
$subject = $atts['subject']; |
|
198 |
} |
|
5 | 199 |
|
9 | 200 |
if ( isset( $atts['message'] ) ) { |
201 |
$message = $atts['message']; |
|
202 |
} |
|
5 | 203 |
|
9 | 204 |
if ( isset( $atts['headers'] ) ) { |
205 |
$headers = $atts['headers']; |
|
206 |
} |
|
5 | 207 |
|
9 | 208 |
if ( isset( $atts['attachments'] ) ) { |
209 |
$attachments = $atts['attachments']; |
|
210 |
} |
|
0 | 211 |
|
9 | 212 |
if ( ! is_array( $attachments ) ) { |
213 |
$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) ); |
|
214 |
} |
|
215 |
global $phpmailer; |
|
216 |
||
217 |
// (Re)create it, if it's gone missing |
|
218 |
if ( ! ( $phpmailer instanceof PHPMailer ) ) { |
|
219 |
require_once ABSPATH . WPINC . '/class-phpmailer.php'; |
|
220 |
require_once ABSPATH . WPINC . '/class-smtp.php'; |
|
221 |
$phpmailer = new PHPMailer( true ); |
|
222 |
} |
|
0 | 223 |
|
9 | 224 |
// Headers |
225 |
$cc = $bcc = $reply_to = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
|
9 | 227 |
if ( empty( $headers ) ) { |
228 |
$headers = array(); |
|
0 | 229 |
} else { |
9 | 230 |
if ( ! is_array( $headers ) ) { |
231 |
// Explode the headers out, so this function can take both |
|
232 |
// string headers and an array of headers. |
|
233 |
$tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) ); |
|
234 |
} else { |
|
235 |
$tempheaders = $headers; |
|
236 |
} |
|
237 |
$headers = array(); |
|
0 | 238 |
|
9 | 239 |
// If it's actually got contents |
240 |
if ( ! empty( $tempheaders ) ) { |
|
241 |
// Iterate through the raw headers |
|
242 |
foreach ( (array) $tempheaders as $header ) { |
|
243 |
if ( strpos( $header, ':' ) === false ) { |
|
244 |
if ( false !== stripos( $header, 'boundary=' ) ) { |
|
245 |
$parts = preg_split( '/boundary=/i', trim( $header ) ); |
|
246 |
$boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) ); |
|
247 |
} |
|
248 |
continue; |
|
0 | 249 |
} |
9 | 250 |
// Explode them out |
251 |
list( $name, $content ) = explode( ':', trim( $header ), 2 ); |
|
0 | 252 |
|
9 | 253 |
// Cleanup crew |
254 |
$name = trim( $name ); |
|
255 |
$content = trim( $content ); |
|
0 | 256 |
|
9 | 257 |
switch ( strtolower( $name ) ) { |
258 |
// Mainly for legacy -- process a From: header if it's there |
|
259 |
case 'from': |
|
260 |
$bracket_pos = strpos( $content, '<' ); |
|
261 |
if ( $bracket_pos !== false ) { |
|
262 |
// Text before the bracketed email is the "From" name. |
|
263 |
if ( $bracket_pos > 0 ) { |
|
264 |
$from_name = substr( $content, 0, $bracket_pos - 1 ); |
|
265 |
$from_name = str_replace( '"', '', $from_name ); |
|
266 |
$from_name = trim( $from_name ); |
|
267 |
} |
|
0 | 268 |
|
9 | 269 |
$from_email = substr( $content, $bracket_pos + 1 ); |
270 |
$from_email = str_replace( '>', '', $from_email ); |
|
271 |
$from_email = trim( $from_email ); |
|
5 | 272 |
|
9 | 273 |
// Avoid setting an empty $from_email. |
274 |
} elseif ( '' !== trim( $content ) ) { |
|
275 |
$from_email = trim( $content ); |
|
0 | 276 |
} |
9 | 277 |
break; |
278 |
case 'content-type': |
|
279 |
if ( strpos( $content, ';' ) !== false ) { |
|
280 |
list( $type, $charset_content ) = explode( ';', $content ); |
|
281 |
$content_type = trim( $type ); |
|
282 |
if ( false !== stripos( $charset_content, 'charset=' ) ) { |
|
283 |
$charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) ); |
|
284 |
} elseif ( false !== stripos( $charset_content, 'boundary=' ) ) { |
|
285 |
$boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) ); |
|
286 |
$charset = ''; |
|
287 |
} |
|
5 | 288 |
|
9 | 289 |
// Avoid setting an empty $content_type. |
290 |
} elseif ( '' !== trim( $content ) ) { |
|
291 |
$content_type = trim( $content ); |
|
292 |
} |
|
293 |
break; |
|
294 |
case 'cc': |
|
295 |
$cc = array_merge( (array) $cc, explode( ',', $content ) ); |
|
296 |
break; |
|
297 |
case 'bcc': |
|
298 |
$bcc = array_merge( (array) $bcc, explode( ',', $content ) ); |
|
299 |
break; |
|
300 |
case 'reply-to': |
|
301 |
$reply_to = array_merge( (array) $reply_to, explode( ',', $content ) ); |
|
302 |
break; |
|
303 |
default: |
|
304 |
// Add it to our grand headers array |
|
305 |
$headers[ trim( $name ) ] = trim( $content ); |
|
306 |
break; |
|
307 |
} |
|
0 | 308 |
} |
309 |
} |
|
310 |
} |
|
9 | 311 |
|
312 |
// Empty out the values that may be set |
|
313 |
$phpmailer->clearAllRecipients(); |
|
314 |
$phpmailer->clearAttachments(); |
|
315 |
$phpmailer->clearCustomHeaders(); |
|
316 |
$phpmailer->clearReplyTos(); |
|
317 |
||
318 |
// From email and name |
|
319 |
// If we don't have a name from the input headers |
|
320 |
if ( ! isset( $from_name ) ) { |
|
321 |
$from_name = 'WordPress'; |
|
322 |
} |
|
0 | 323 |
|
9 | 324 |
/* If we don't have an email from the input headers default to wordpress@$sitename |
325 |
* Some hosts will block outgoing mail from this address if it doesn't exist but |
|
326 |
* there's no easy alternative. Defaulting to admin_email might appear to be another |
|
327 |
* option but some hosts may refuse to relay mail from an unknown domain. See |
|
328 |
* https://core.trac.wordpress.org/ticket/5007. |
|
329 |
*/ |
|
0 | 330 |
|
9 | 331 |
if ( ! isset( $from_email ) ) { |
332 |
// Get the site domain and get rid of www. |
|
333 |
$sitename = strtolower( $_SERVER['SERVER_NAME'] ); |
|
334 |
if ( substr( $sitename, 0, 4 ) == 'www.' ) { |
|
335 |
$sitename = substr( $sitename, 4 ); |
|
336 |
} |
|
0 | 337 |
|
9 | 338 |
$from_email = 'wordpress@' . $sitename; |
339 |
} |
|
340 |
||
341 |
/** |
|
342 |
* Filters the email address to send from. |
|
343 |
* |
|
344 |
* @since 2.2.0 |
|
345 |
* |
|
346 |
* @param string $from_email Email address to send from. |
|
347 |
*/ |
|
348 |
$from_email = apply_filters( 'wp_mail_from', $from_email ); |
|
0 | 349 |
|
9 | 350 |
/** |
351 |
* Filters the name to associate with the "from" email address. |
|
352 |
* |
|
353 |
* @since 2.3.0 |
|
354 |
* |
|
355 |
* @param string $from_name Name associated with the "from" email address. |
|
356 |
*/ |
|
357 |
$from_name = apply_filters( 'wp_mail_from_name', $from_name ); |
|
358 |
||
359 |
try { |
|
360 |
$phpmailer->setFrom( $from_email, $from_name, false ); |
|
361 |
} catch ( phpmailerException $e ) { |
|
362 |
$mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); |
|
363 |
$mail_error_data['phpmailer_exception_code'] = $e->getCode(); |
|
364 |
||
365 |
/** This filter is documented in wp-includes/pluggable.php */ |
|
366 |
do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) ); |
|
367 |
||
368 |
return false; |
|
0 | 369 |
} |
370 |
||
9 | 371 |
// Set mail's subject and body |
372 |
$phpmailer->Subject = $subject; |
|
373 |
$phpmailer->Body = $message; |
|
374 |
||
375 |
// Set destination addresses, using appropriate methods for handling addresses |
|
376 |
$address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' ); |
|
377 |
||
378 |
foreach ( $address_headers as $address_header => $addresses ) { |
|
379 |
if ( empty( $addresses ) ) { |
|
380 |
continue; |
|
381 |
} |
|
0 | 382 |
|
9 | 383 |
foreach ( (array) $addresses as $address ) { |
384 |
try { |
|
385 |
// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>" |
|
386 |
$recipient_name = ''; |
|
5 | 387 |
|
9 | 388 |
if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) { |
389 |
if ( count( $matches ) == 3 ) { |
|
390 |
$recipient_name = $matches[1]; |
|
391 |
$address = $matches[2]; |
|
392 |
} |
|
393 |
} |
|
0 | 394 |
|
9 | 395 |
switch ( $address_header ) { |
396 |
case 'to': |
|
397 |
$phpmailer->addAddress( $address, $recipient_name ); |
|
398 |
break; |
|
399 |
case 'cc': |
|
400 |
$phpmailer->addCc( $address, $recipient_name ); |
|
401 |
break; |
|
402 |
case 'bcc': |
|
403 |
$phpmailer->addBcc( $address, $recipient_name ); |
|
404 |
break; |
|
405 |
case 'reply_to': |
|
406 |
$phpmailer->addReplyTo( $address, $recipient_name ); |
|
407 |
break; |
|
408 |
} |
|
409 |
} catch ( phpmailerException $e ) { |
|
410 |
continue; |
|
411 |
} |
|
412 |
} |
|
413 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
|
9 | 415 |
// Set to use PHP's mail() |
416 |
$phpmailer->isMail(); |
|
0 | 417 |
|
9 | 418 |
// Set Content-Type and charset |
419 |
// If we don't have a content-type from the input headers |
|
420 |
if ( ! isset( $content_type ) ) { |
|
421 |
$content_type = 'text/plain'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
|
9 | 424 |
/** |
425 |
* Filters the wp_mail() content type. |
|
426 |
* |
|
427 |
* @since 2.3.0 |
|
428 |
* |
|
429 |
* @param string $content_type Default wp_mail() content type. |
|
430 |
*/ |
|
431 |
$content_type = apply_filters( 'wp_mail_content_type', $content_type ); |
|
432 |
||
433 |
$phpmailer->ContentType = $content_type; |
|
434 |
||
435 |
// Set whether it's plaintext, depending on $content_type |
|
436 |
if ( 'text/html' == $content_type ) { |
|
437 |
$phpmailer->isHTML( true ); |
|
438 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
|
9 | 440 |
// If we don't have a charset from the input headers |
441 |
if ( ! isset( $charset ) ) { |
|
442 |
$charset = get_bloginfo( 'charset' ); |
|
443 |
} |
|
444 |
||
445 |
// Set the content-type and charset |
|
446 |
||
447 |
/** |
|
448 |
* Filters the default wp_mail() charset. |
|
449 |
* |
|
450 |
* @since 2.3.0 |
|
451 |
* |
|
452 |
* @param string $charset Default email charset. |
|
453 |
*/ |
|
454 |
$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset ); |
|
455 |
||
456 |
// Set custom headers |
|
457 |
if ( ! empty( $headers ) ) { |
|
458 |
foreach ( (array) $headers as $name => $content ) { |
|
459 |
$phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); |
|
460 |
} |
|
0 | 461 |
|
9 | 462 |
if ( false !== stripos( $content_type, 'multipart' ) && ! empty( $boundary ) ) { |
463 |
$phpmailer->addCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) ); |
|
464 |
} |
|
465 |
} |
|
466 |
||
467 |
if ( ! empty( $attachments ) ) { |
|
468 |
foreach ( $attachments as $attachment ) { |
|
469 |
try { |
|
470 |
$phpmailer->addAttachment( $attachment ); |
|
471 |
} catch ( phpmailerException $e ) { |
|
472 |
continue; |
|
0 | 473 |
} |
474 |
} |
|
475 |
} |
|
9 | 476 |
|
477 |
/** |
|
478 |
* Fires after PHPMailer is initialized. |
|
479 |
* |
|
480 |
* @since 2.2.0 |
|
481 |
* |
|
482 |
* @param PHPMailer $phpmailer The PHPMailer instance (passed by reference). |
|
483 |
*/ |
|
484 |
do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) ); |
|
485 |
||
486 |
// Send! |
|
487 |
try { |
|
488 |
return $phpmailer->send(); |
|
489 |
} catch ( phpmailerException $e ) { |
|
490 |
||
491 |
$mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); |
|
492 |
$mail_error_data['phpmailer_exception_code'] = $e->getCode(); |
|
493 |
||
494 |
/** |
|
495 |
* Fires after a phpmailerException is caught. |
|
496 |
* |
|
497 |
* @since 4.4.0 |
|
498 |
* |
|
499 |
* @param WP_Error $error A WP_Error object with the phpmailerException message, and an array |
|
500 |
* containing the mail recipient, subject, message, headers, and attachments. |
|
501 |
*/ |
|
502 |
do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) ); |
|
503 |
||
504 |
return false; |
|
505 |
} |
|
0 | 506 |
} |
9 | 507 |
endif; |
0 | 508 |
|
9 | 509 |
if ( ! function_exists( 'wp_authenticate' ) ) : |
5 | 510 |
/** |
9 | 511 |
* Authenticate a user, confirming the login credentials are valid. |
5 | 512 |
* |
9 | 513 |
* @since 2.5.0 |
514 |
* @since 4.5.0 `$username` now accepts an email address. |
|
5 | 515 |
* |
9 | 516 |
* @param string $username User's username or email address. |
517 |
* @param string $password User's password. |
|
518 |
* @return WP_User|WP_Error WP_User object if the credentials are valid, |
|
519 |
* otherwise WP_Error. |
|
5 | 520 |
*/ |
9 | 521 |
function wp_authenticate( $username, $password ) { |
522 |
$username = sanitize_user( $username ); |
|
523 |
$password = trim( $password ); |
|
0 | 524 |
|
9 | 525 |
/** |
526 |
* Filters whether a set of user login credentials are valid. |
|
527 |
* |
|
528 |
* A WP_User object is returned if the credentials authenticate a user. |
|
529 |
* WP_Error or null otherwise. |
|
530 |
* |
|
531 |
* @since 2.8.0 |
|
532 |
* @since 4.5.0 `$username` now accepts an email address. |
|
533 |
* |
|
534 |
* @param null|WP_User|WP_Error $user WP_User if the user is authenticated. |
|
535 |
* WP_Error or null otherwise. |
|
536 |
* @param string $username Username or email address. |
|
537 |
* @param string $password User password |
|
538 |
*/ |
|
539 |
$user = apply_filters( 'authenticate', null, $username, $password ); |
|
5 | 540 |
|
9 | 541 |
if ( $user == null ) { |
542 |
// TODO what should the error message be? (Or would these even happen?) |
|
543 |
// Only needed if all authentication handlers fail to return anything. |
|
544 |
$user = new WP_Error( 'authentication_failed', __( '<strong>ERROR</strong>: Invalid username, email address or incorrect password.' ) ); |
|
0 | 545 |
} |
546 |
||
9 | 547 |
$ignore_codes = array( 'empty_username', 'empty_password' ); |
0 | 548 |
|
9 | 549 |
if ( is_wp_error( $user ) && ! in_array( $user->get_error_code(), $ignore_codes ) ) { |
550 |
/** |
|
551 |
* Fires after a user login has failed. |
|
552 |
* |
|
553 |
* @since 2.5.0 |
|
554 |
* @since 4.5.0 The value of `$username` can now be an email address. |
|
555 |
* |
|
556 |
* @param string $username Username or email address. |
|
557 |
*/ |
|
558 |
do_action( 'wp_login_failed', $username ); |
|
0 | 559 |
} |
9 | 560 |
|
561 |
return $user; |
|
0 | 562 |
} |
9 | 563 |
endif; |
0 | 564 |
|
9 | 565 |
if ( ! function_exists( 'wp_logout' ) ) : |
5 | 566 |
/** |
9 | 567 |
* Log the current user out. |
5 | 568 |
* |
9 | 569 |
* @since 2.5.0 |
5 | 570 |
*/ |
9 | 571 |
function wp_logout() { |
572 |
wp_destroy_current_session(); |
|
573 |
wp_clear_auth_cookie(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
575 |
/** |
9 | 576 |
* Fires after a user is logged-out. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
* |
9 | 578 |
* @since 1.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
579 |
*/ |
9 | 580 |
do_action( 'wp_logout' ); |
0 | 581 |
} |
582 |
endif; |
|
583 |
||
9 | 584 |
if ( ! function_exists( 'wp_validate_auth_cookie' ) ) : |
5 | 585 |
/** |
9 | 586 |
* Validates authentication cookie. |
587 |
* |
|
588 |
* The checks include making sure that the authentication cookie is set and |
|
589 |
* pulling in the contents (if $cookie is not used). |
|
590 |
* |
|
591 |
* Makes sure the cookie is not expired. Verifies the hash in cookie is what is |
|
592 |
* should be and compares the two. |
|
593 |
* |
|
594 |
* @since 2.5.0 |
|
595 |
* |
|
596 |
* @global int $login_grace_period |
|
5 | 597 |
* |
9 | 598 |
* @param string $cookie Optional. If used, will validate contents instead of cookie's |
599 |
* @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in |
|
600 |
* @return false|int False if invalid cookie, User ID if valid. |
|
5 | 601 |
*/ |
9 | 602 |
function wp_validate_auth_cookie( $cookie = '', $scheme = '' ) { |
603 |
if ( ! $cookie_elements = wp_parse_auth_cookie( $cookie, $scheme ) ) { |
|
604 |
/** |
|
605 |
* Fires if an authentication cookie is malformed. |
|
606 |
* |
|
607 |
* @since 2.7.0 |
|
608 |
* |
|
609 |
* @param string $cookie Malformed auth cookie. |
|
610 |
* @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth', |
|
611 |
* or 'logged_in'. |
|
612 |
*/ |
|
613 |
do_action( 'auth_cookie_malformed', $cookie, $scheme ); |
|
614 |
return false; |
|
615 |
} |
|
616 |
||
617 |
$scheme = $cookie_elements['scheme']; |
|
618 |
$username = $cookie_elements['username']; |
|
619 |
$hmac = $cookie_elements['hmac']; |
|
620 |
$token = $cookie_elements['token']; |
|
621 |
$expired = $expiration = $cookie_elements['expiration']; |
|
622 |
||
623 |
// Allow a grace period for POST and Ajax requests |
|
624 |
if ( wp_doing_ajax() || 'POST' == $_SERVER['REQUEST_METHOD'] ) { |
|
625 |
$expired += HOUR_IN_SECONDS; |
|
626 |
} |
|
627 |
||
628 |
// Quick check to see if an honest cookie has expired |
|
629 |
if ( $expired < time() ) { |
|
630 |
/** |
|
631 |
* Fires once an authentication cookie has expired. |
|
632 |
* |
|
633 |
* @since 2.7.0 |
|
634 |
* |
|
635 |
* @param array $cookie_elements An array of data for the authentication cookie. |
|
636 |
*/ |
|
637 |
do_action( 'auth_cookie_expired', $cookie_elements ); |
|
638 |
return false; |
|
639 |
} |
|
0 | 640 |
|
9 | 641 |
$user = get_user_by( 'login', $username ); |
642 |
if ( ! $user ) { |
|
643 |
/** |
|
644 |
* Fires if a bad username is entered in the user authentication process. |
|
645 |
* |
|
646 |
* @since 2.7.0 |
|
647 |
* |
|
648 |
* @param array $cookie_elements An array of data for the authentication cookie. |
|
649 |
*/ |
|
650 |
do_action( 'auth_cookie_bad_username', $cookie_elements ); |
|
651 |
return false; |
|
652 |
} |
|
653 |
||
654 |
$pass_frag = substr( $user->user_pass, 8, 4 ); |
|
655 |
||
656 |
$key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); |
|
657 |
||
658 |
// If ext/hash is not present, compat.php's hash_hmac() does not support sha256. |
|
659 |
$algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; |
|
660 |
$hash = hash_hmac( $algo, $username . '|' . $expiration . '|' . $token, $key ); |
|
0 | 661 |
|
9 | 662 |
if ( ! hash_equals( $hash, $hmac ) ) { |
663 |
/** |
|
664 |
* Fires if a bad authentication cookie hash is encountered. |
|
665 |
* |
|
666 |
* @since 2.7.0 |
|
667 |
* |
|
668 |
* @param array $cookie_elements An array of data for the authentication cookie. |
|
669 |
*/ |
|
670 |
do_action( 'auth_cookie_bad_hash', $cookie_elements ); |
|
671 |
return false; |
|
672 |
} |
|
0 | 673 |
|
9 | 674 |
$manager = WP_Session_Tokens::get_instance( $user->ID ); |
675 |
if ( ! $manager->verify( $token ) ) { |
|
676 |
do_action( 'auth_cookie_bad_session_token', $cookie_elements ); |
|
677 |
return false; |
|
678 |
} |
|
679 |
||
680 |
// Ajax/POST grace period set above |
|
681 |
if ( $expiration < time() ) { |
|
682 |
$GLOBALS['login_grace_period'] = 1; |
|
683 |
} |
|
684 |
||
5 | 685 |
/** |
9 | 686 |
* Fires once an authentication cookie has been validated. |
5 | 687 |
* |
688 |
* @since 2.7.0 |
|
689 |
* |
|
9 | 690 |
* @param array $cookie_elements An array of data for the authentication cookie. |
691 |
* @param WP_User $user User object. |
|
5 | 692 |
*/ |
9 | 693 |
do_action( 'auth_cookie_valid', $cookie_elements, $user ); |
0 | 694 |
|
9 | 695 |
return $user->ID; |
0 | 696 |
} |
697 |
endif; |
|
698 |
||
9 | 699 |
if ( ! function_exists( 'wp_generate_auth_cookie' ) ) : |
5 | 700 |
/** |
9 | 701 |
* Generate authentication cookie contents. |
5 | 702 |
* |
703 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
704 |
* @since 4.0.0 The `$token` parameter was added. |
5 | 705 |
* |
9 | 706 |
* @param int $user_id User ID |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
707 |
* @param int $expiration The time the cookie expires as a UNIX timestamp. |
9 | 708 |
* @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in |
709 |
* @param string $token User's session token to use for this cookie |
|
710 |
* @return string Authentication cookie contents. Empty string if user does not exist. |
|
5 | 711 |
*/ |
9 | 712 |
function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) { |
713 |
$user = get_userdata( $user_id ); |
|
714 |
if ( ! $user ) { |
|
715 |
return ''; |
|
716 |
} |
|
717 |
||
718 |
if ( ! $token ) { |
|
719 |
$manager = WP_Session_Tokens::get_instance( $user_id ); |
|
720 |
$token = $manager->create( $expiration ); |
|
721 |
} |
|
722 |
||
723 |
$pass_frag = substr( $user->user_pass, 8, 4 ); |
|
724 |
||
725 |
$key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); |
|
0 | 726 |
|
9 | 727 |
// If ext/hash is not present, compat.php's hash_hmac() does not support sha256. |
728 |
$algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; |
|
729 |
$hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key ); |
|
730 |
||
731 |
$cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash; |
|
0 | 732 |
|
9 | 733 |
/** |
734 |
* Filters the authentication cookie. |
|
735 |
* |
|
736 |
* @since 2.5.0 |
|
737 |
* @since 4.0.0 The `$token` parameter was added. |
|
738 |
* |
|
739 |
* @param string $cookie Authentication cookie. |
|
740 |
* @param int $user_id User ID. |
|
741 |
* @param int $expiration The time the cookie expires as a UNIX timestamp. |
|
742 |
* @param string $scheme Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'. |
|
743 |
* @param string $token User's session token used. |
|
744 |
*/ |
|
745 |
return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token ); |
|
0 | 746 |
} |
747 |
endif; |
|
748 |
||
9 | 749 |
if ( ! function_exists( 'wp_parse_auth_cookie' ) ) : |
750 |
/** |
|
751 |
* Parse a cookie into its components |
|
752 |
* |
|
753 |
* @since 2.7.0 |
|
754 |
* |
|
755 |
* @param string $cookie |
|
756 |
* @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in |
|
757 |
* @return array|false Authentication cookie components |
|
758 |
*/ |
|
759 |
function wp_parse_auth_cookie( $cookie = '', $scheme = '' ) { |
|
760 |
if ( empty( $cookie ) ) { |
|
761 |
switch ( $scheme ) { |
|
762 |
case 'auth': |
|
763 |
$cookie_name = AUTH_COOKIE; |
|
764 |
break; |
|
765 |
case 'secure_auth': |
|
766 |
$cookie_name = SECURE_AUTH_COOKIE; |
|
767 |
break; |
|
768 |
case 'logged_in': |
|
769 |
$cookie_name = LOGGED_IN_COOKIE; |
|
770 |
break; |
|
771 |
default: |
|
772 |
if ( is_ssl() ) { |
|
773 |
$cookie_name = SECURE_AUTH_COOKIE; |
|
774 |
$scheme = 'secure_auth'; |
|
775 |
} else { |
|
776 |
$cookie_name = AUTH_COOKIE; |
|
777 |
$scheme = 'auth'; |
|
778 |
} |
|
779 |
} |
|
5 | 780 |
|
9 | 781 |
if ( empty( $_COOKIE[ $cookie_name ] ) ) { |
782 |
return false; |
|
783 |
} |
|
784 |
$cookie = $_COOKIE[ $cookie_name ]; |
|
785 |
} |
|
0 | 786 |
|
9 | 787 |
$cookie_elements = explode( '|', $cookie ); |
788 |
if ( count( $cookie_elements ) !== 4 ) { |
|
789 |
return false; |
|
790 |
} |
|
791 |
||
792 |
list( $username, $expiration, $token, $hmac ) = $cookie_elements; |
|
793 |
||
794 |
return compact( 'username', 'expiration', 'token', 'hmac', 'scheme' ); |
|
5 | 795 |
} |
9 | 796 |
endif; |
5 | 797 |
|
9 | 798 |
if ( ! function_exists( 'wp_set_auth_cookie' ) ) : |
5 | 799 |
/** |
9 | 800 |
* Log in a user by setting authentication cookies. |
5 | 801 |
* |
9 | 802 |
* The $remember parameter increases the time that the cookie will be kept. The |
803 |
* default the cookie is kept without remembering is two days. When $remember is |
|
804 |
* set, the cookies will be kept for 14 days or two weeks. |
|
5 | 805 |
* |
806 |
* @since 2.5.0 |
|
9 | 807 |
* @since 4.3.0 Added the `$token` parameter. |
5 | 808 |
* |
9 | 809 |
* @param int $user_id User ID |
810 |
* @param bool $remember Whether to remember the user |
|
811 |
* @param mixed $secure Whether the admin cookies should only be sent over HTTPS. |
|
812 |
* Default is_ssl(). |
|
813 |
* @param string $token Optional. User's session token to use for this cookie. |
|
5 | 814 |
*/ |
9 | 815 |
function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) { |
816 |
if ( $remember ) { |
|
817 |
/** |
|
818 |
* Filters the duration of the authentication cookie expiration period. |
|
819 |
* |
|
820 |
* @since 2.8.0 |
|
821 |
* |
|
822 |
* @param int $length Duration of the expiration period in seconds. |
|
823 |
* @param int $user_id User ID. |
|
824 |
* @param bool $remember Whether to remember the user login. Default false. |
|
825 |
*/ |
|
826 |
$expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember ); |
|
827 |
||
828 |
/* |
|
829 |
* Ensure the browser will continue to send the cookie after the expiration time is reached. |
|
830 |
* Needed for the login grace period in wp_validate_auth_cookie(). |
|
831 |
*/ |
|
832 |
$expire = $expiration + ( 12 * HOUR_IN_SECONDS ); |
|
833 |
} else { |
|
834 |
/** This filter is documented in wp-includes/pluggable.php */ |
|
835 |
$expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember ); |
|
836 |
$expire = 0; |
|
837 |
} |
|
838 |
||
839 |
if ( '' === $secure ) { |
|
840 |
$secure = is_ssl(); |
|
841 |
} |
|
842 |
||
843 |
// Front-end cookie is secure when the auth cookie is secure and the site's home URL is forced HTTPS. |
|
844 |
$secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME ); |
|
845 |
||
846 |
/** |
|
847 |
* Filters whether the connection is secure. |
|
848 |
* |
|
849 |
* @since 3.1.0 |
|
850 |
* |
|
851 |
* @param bool $secure Whether the connection is secure. |
|
852 |
* @param int $user_id User ID. |
|
853 |
*/ |
|
854 |
$secure = apply_filters( 'secure_auth_cookie', $secure, $user_id ); |
|
855 |
||
856 |
/** |
|
857 |
* Filters whether to use a secure cookie when logged-in. |
|
858 |
* |
|
859 |
* @since 3.1.0 |
|
860 |
* |
|
861 |
* @param bool $secure_logged_in_cookie Whether to use a secure cookie when logged-in. |
|
862 |
* @param int $user_id User ID. |
|
863 |
* @param bool $secure Whether the connection is secure. |
|
864 |
*/ |
|
865 |
$secure_logged_in_cookie = apply_filters( 'secure_logged_in_cookie', $secure_logged_in_cookie, $user_id, $secure ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
|
9 | 867 |
if ( $secure ) { |
868 |
$auth_cookie_name = SECURE_AUTH_COOKIE; |
|
869 |
$scheme = 'secure_auth'; |
|
870 |
} else { |
|
871 |
$auth_cookie_name = AUTH_COOKIE; |
|
872 |
$scheme = 'auth'; |
|
873 |
} |
|
874 |
||
875 |
if ( '' === $token ) { |
|
876 |
$manager = WP_Session_Tokens::get_instance( $user_id ); |
|
877 |
$token = $manager->create( $expiration ); |
|
878 |
} |
|
879 |
||
880 |
$auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token ); |
|
881 |
$logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token ); |
|
882 |
||
883 |
/** |
|
884 |
* Fires immediately before the authentication cookie is set. |
|
885 |
* |
|
886 |
* @since 2.5.0 |
|
887 |
* @since 4.9.0 The `$token` parameter was added. |
|
888 |
* |
|
889 |
* @param string $auth_cookie Authentication cookie value. |
|
890 |
* @param int $expire The time the login grace period expires as a UNIX timestamp. |
|
891 |
* Default is 12 hours past the cookie's expiration time. |
|
892 |
* @param int $expiration The time when the authentication cookie expires as a UNIX timestamp. |
|
893 |
* Default is 14 days from now. |
|
894 |
* @param int $user_id User ID. |
|
895 |
* @param string $scheme Authentication scheme. Values include 'auth' or 'secure_auth'. |
|
896 |
* @param string $token User's session token to use for this cookie. |
|
897 |
*/ |
|
898 |
do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme, $token ); |
|
899 |
||
900 |
/** |
|
901 |
* Fires immediately before the logged-in authentication cookie is set. |
|
902 |
* |
|
903 |
* @since 2.6.0 |
|
904 |
* @since 4.9.0 The `$token` parameter was added. |
|
905 |
* |
|
906 |
* @param string $logged_in_cookie The logged-in cookie value. |
|
907 |
* @param int $expire The time the login grace period expires as a UNIX timestamp. |
|
908 |
* Default is 12 hours past the cookie's expiration time. |
|
909 |
* @param int $expiration The time when the logged-in authentication cookie expires as a UNIX timestamp. |
|
910 |
* Default is 14 days from now. |
|
911 |
* @param int $user_id User ID. |
|
912 |
* @param string $scheme Authentication scheme. Default 'logged_in'. |
|
913 |
* @param string $token User's session token to use for this cookie. |
|
914 |
*/ |
|
915 |
do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in', $token ); |
|
916 |
||
917 |
/** |
|
918 |
* Allows preventing auth cookies from actually being sent to the client. |
|
919 |
* |
|
920 |
* @since 4.7.4 |
|
921 |
* |
|
922 |
* @param bool $send Whether to send auth cookies to the client. |
|
923 |
*/ |
|
924 |
if ( ! apply_filters( 'send_auth_cookies', true ) ) { |
|
925 |
return; |
|
926 |
} |
|
927 |
||
928 |
setcookie( $auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true ); |
|
929 |
setcookie( $auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true ); |
|
930 |
setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true ); |
|
931 |
if ( COOKIEPATH != SITECOOKIEPATH ) { |
|
932 |
setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true ); |
|
933 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
934 |
} |
0 | 935 |
endif; |
936 |
||
9 | 937 |
if ( ! function_exists( 'wp_clear_auth_cookie' ) ) : |
5 | 938 |
/** |
9 | 939 |
* Removes all of the cookies associated with authentication. |
5 | 940 |
* |
9 | 941 |
* @since 2.5.0 |
5 | 942 |
*/ |
9 | 943 |
function wp_clear_auth_cookie() { |
944 |
/** |
|
945 |
* Fires just before the authentication cookies are cleared. |
|
946 |
* |
|
947 |
* @since 2.7.0 |
|
948 |
*/ |
|
949 |
do_action( 'clear_auth_cookie' ); |
|
0 | 950 |
|
9 | 951 |
/** This filter is documented in wp-includes/pluggable.php */ |
952 |
if ( ! apply_filters( 'send_auth_cookies', true ) ) { |
|
953 |
return; |
|
954 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
|
9 | 956 |
// Auth cookies |
957 |
setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN ); |
|
958 |
setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN ); |
|
959 |
setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN ); |
|
960 |
setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN ); |
|
961 |
setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
|
962 |
setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); |
|
0 | 963 |
|
9 | 964 |
// Settings cookies |
965 |
setcookie( 'wp-settings-' . get_current_user_id(), ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH ); |
|
966 |
setcookie( 'wp-settings-time-' . get_current_user_id(), ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
967 |
|
9 | 968 |
// Old cookies |
969 |
setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
|
970 |
setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); |
|
971 |
setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
|
972 |
setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); |
|
0 | 973 |
|
9 | 974 |
// Even older cookies |
975 |
setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
|
976 |
setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
|
977 |
setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); |
|
978 |
setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
|
9 | 980 |
// Post password cookie |
981 |
setcookie( 'wp-postpass_' . COOKIEHASH, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
|
982 |
} |
|
0 | 983 |
endif; |
984 |
||
9 | 985 |
if ( ! function_exists( 'is_user_logged_in' ) ) : |
986 |
/** |
|
987 |
* Determines whether the current visitor is a logged in user. |
|
988 |
* |
|
989 |
* For more information on this and similar theme functions, check out |
|
990 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
991 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
992 |
* |
|
993 |
* @since 2.0.0 |
|
994 |
* |
|
995 |
* @return bool True if user is logged in, false if not logged in. |
|
996 |
*/ |
|
997 |
function is_user_logged_in() { |
|
998 |
$user = wp_get_current_user(); |
|
0 | 999 |
|
9 | 1000 |
return $user->exists(); |
1001 |
} |
|
0 | 1002 |
endif; |
1003 |
||
9 | 1004 |
if ( ! function_exists( 'auth_redirect' ) ) : |
1005 |
/** |
|
1006 |
* Checks if a user is logged in, if not it redirects them to the login page. |
|
1007 |
* |
|
1008 |
* @since 1.5.0 |
|
1009 |
*/ |
|
1010 |
function auth_redirect() { |
|
1011 |
// Checks if a user is logged in, if not redirects them to the login page |
|
0 | 1012 |
|
9 | 1013 |
$secure = ( is_ssl() || force_ssl_admin() ); |
0 | 1014 |
|
9 | 1015 |
/** |
1016 |
* Filters whether to use a secure authentication redirect. |
|
1017 |
* |
|
1018 |
* @since 3.1.0 |
|
1019 |
* |
|
1020 |
* @param bool $secure Whether to use a secure authentication redirect. Default false. |
|
1021 |
*/ |
|
1022 |
$secure = apply_filters( 'secure_auth_redirect', $secure ); |
|
0 | 1023 |
|
9 | 1024 |
// If https is required and request is http, redirect |
1025 |
if ( $secure && ! is_ssl() && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) { |
|
0 | 1026 |
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { |
1027 |
wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); |
|
1028 |
exit(); |
|
1029 |
} else { |
|
1030 |
wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); |
|
1031 |
exit(); |
|
1032 |
} |
|
1033 |
} |
|
1034 |
||
9 | 1035 |
/** |
1036 |
* Filters the authentication redirect scheme. |
|
1037 |
* |
|
1038 |
* @since 2.9.0 |
|
1039 |
* |
|
1040 |
* @param string $scheme Authentication redirect scheme. Default empty. |
|
1041 |
*/ |
|
1042 |
$scheme = apply_filters( 'auth_redirect_scheme', '' ); |
|
0 | 1043 |
|
9 | 1044 |
if ( $user_id = wp_validate_auth_cookie( '', $scheme ) ) { |
1045 |
/** |
|
1046 |
* Fires before the authentication redirect. |
|
1047 |
* |
|
1048 |
* @since 2.8.0 |
|
1049 |
* |
|
1050 |
* @param int $user_id User ID. |
|
1051 |
*/ |
|
1052 |
do_action( 'auth_redirect', $user_id ); |
|
0 | 1053 |
|
9 | 1054 |
// If the user wants ssl but the session is not ssl, redirect. |
1055 |
if ( ! $secure && get_user_option( 'use_ssl', $user_id ) && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) { |
|
1056 |
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { |
|
1057 |
wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); |
|
1058 |
exit(); |
|
1059 |
} else { |
|
1060 |
wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); |
|
1061 |
exit(); |
|
1062 |
} |
|
1063 |
} |
|
0 | 1064 |
|
9 | 1065 |
return; // The cookie is good so we're done |
1066 |
} |
|
1067 |
||
1068 |
// The cookie is no good so force login |
|
1069 |
nocache_headers(); |
|
1070 |
||
1071 |
$redirect = ( strpos( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); |
|
1072 |
||
1073 |
$login_url = wp_login_url( $redirect, true ); |
|
1074 |
||
1075 |
wp_redirect( $login_url ); |
|
1076 |
exit(); |
|
1077 |
} |
|
0 | 1078 |
endif; |
1079 |
||
9 | 1080 |
if ( ! function_exists( 'check_admin_referer' ) ) : |
1081 |
/** |
|
1082 |
* Makes sure that a user was referred from another admin page. |
|
1083 |
* |
|
1084 |
* To avoid security exploits. |
|
1085 |
* |
|
1086 |
* @since 1.2.0 |
|
1087 |
* |
|
1088 |
* @param int|string $action Action nonce. |
|
1089 |
* @param string $query_arg Optional. Key to check for nonce in `$_REQUEST` (since 2.5). |
|
1090 |
* Default '_wpnonce'. |
|
1091 |
* @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between |
|
1092 |
* 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. |
|
1093 |
*/ |
|
1094 |
function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) { |
|
13 | 1095 |
if ( -1 === $action ) { |
9 | 1096 |
_doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2.0' ); |
1097 |
} |
|
0 | 1098 |
|
9 | 1099 |
$adminurl = strtolower( admin_url() ); |
1100 |
$referer = strtolower( wp_get_referer() ); |
|
1101 |
$result = isset( $_REQUEST[ $query_arg ] ) ? wp_verify_nonce( $_REQUEST[ $query_arg ], $action ) : false; |
|
5 | 1102 |
|
9 | 1103 |
/** |
1104 |
* Fires once the admin request has been validated or not. |
|
1105 |
* |
|
1106 |
* @since 1.5.1 |
|
1107 |
* |
|
1108 |
* @param string $action The nonce action. |
|
1109 |
* @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between |
|
1110 |
* 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. |
|
1111 |
*/ |
|
1112 |
do_action( 'check_admin_referer', $action, $result ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1113 |
|
13 | 1114 |
if ( ! $result && ! ( -1 === $action && strpos( $referer, $adminurl ) === 0 ) ) { |
9 | 1115 |
wp_nonce_ays( $action ); |
1116 |
die(); |
|
1117 |
} |
|
1118 |
||
1119 |
return $result; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
} |
0 | 1121 |
endif; |
1122 |
||
9 | 1123 |
if ( ! function_exists( 'check_ajax_referer' ) ) : |
1124 |
/** |
|
1125 |
* Verifies the Ajax request to prevent processing requests external of the blog. |
|
1126 |
* |
|
1127 |
* @since 2.0.3 |
|
1128 |
* |
|
1129 |
* @param int|string $action Action nonce. |
|
1130 |
* @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false, |
|
1131 |
* `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce' |
|
1132 |
* (in that order). Default false. |
|
1133 |
* @param bool $die Optional. Whether to die early when the nonce cannot be verified. |
|
1134 |
* Default true. |
|
1135 |
* @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between |
|
1136 |
* 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. |
|
1137 |
*/ |
|
1138 |
function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) { |
|
1139 |
if ( -1 == $action ) { |
|
1140 |
_doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '4.7' ); |
|
1141 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
|
9 | 1143 |
$nonce = ''; |
0 | 1144 |
|
9 | 1145 |
if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) ) { |
1146 |
$nonce = $_REQUEST[ $query_arg ]; |
|
1147 |
} elseif ( isset( $_REQUEST['_ajax_nonce'] ) ) { |
|
1148 |
$nonce = $_REQUEST['_ajax_nonce']; |
|
1149 |
} elseif ( isset( $_REQUEST['_wpnonce'] ) ) { |
|
1150 |
$nonce = $_REQUEST['_wpnonce']; |
|
1151 |
} |
|
0 | 1152 |
|
9 | 1153 |
$result = wp_verify_nonce( $nonce, $action ); |
0 | 1154 |
|
9 | 1155 |
/** |
1156 |
* Fires once the Ajax request has been validated or not. |
|
1157 |
* |
|
1158 |
* @since 2.1.0 |
|
1159 |
* |
|
1160 |
* @param string $action The Ajax nonce action. |
|
1161 |
* @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between |
|
1162 |
* 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. |
|
1163 |
*/ |
|
1164 |
do_action( 'check_ajax_referer', $action, $result ); |
|
0 | 1165 |
|
9 | 1166 |
if ( $die && false === $result ) { |
1167 |
if ( wp_doing_ajax() ) { |
|
1168 |
wp_die( -1, 403 ); |
|
1169 |
} else { |
|
1170 |
die( '-1' ); |
|
1171 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1172 |
} |
9 | 1173 |
|
1174 |
return $result; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
} |
0 | 1176 |
endif; |
1177 |
||
9 | 1178 |
if ( ! function_exists( 'wp_redirect' ) ) : |
1179 |
/** |
|
1180 |
* Redirects to another page. |
|
1181 |
* |
|
1182 |
* Note: wp_redirect() does not exit automatically, and should almost always be |
|
1183 |
* followed by a call to `exit;`: |
|
1184 |
* |
|
1185 |
* wp_redirect( $url ); |
|
1186 |
* exit; |
|
1187 |
* |
|
1188 |
* Exiting can also be selectively manipulated by using wp_redirect() as a conditional |
|
1189 |
* in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_location'} filters: |
|
1190 |
* |
|
1191 |
* if ( wp_redirect( $url ) ) { |
|
1192 |
* exit; |
|
1193 |
* } |
|
1194 |
* |
|
1195 |
* @since 1.5.1 |
|
1196 |
* @since 5.1.0 The `$x_redirect_by` parameter was added. |
|
1197 |
* |
|
1198 |
* @global bool $is_IIS |
|
1199 |
* |
|
1200 |
* @param string $location The path or URL to redirect to. |
|
1201 |
* @param int $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily). |
|
1202 |
* @param string $x_redirect_by Optional. The application doing the redirect. Default 'WordPress'. |
|
1203 |
* @return bool False if the redirect was cancelled, true otherwise. |
|
1204 |
*/ |
|
1205 |
function wp_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) { |
|
1206 |
global $is_IIS; |
|
1207 |
||
1208 |
/** |
|
1209 |
* Filters the redirect location. |
|
1210 |
* |
|
1211 |
* @since 2.1.0 |
|
1212 |
* |
|
1213 |
* @param string $location The path or URL to redirect to. |
|
1214 |
* @param int $status The HTTP response status code to use. |
|
1215 |
*/ |
|
1216 |
$location = apply_filters( 'wp_redirect', $location, $status ); |
|
0 | 1217 |
|
9 | 1218 |
/** |
1219 |
* Filters the redirect HTTP response status code to use. |
|
1220 |
* |
|
1221 |
* @since 2.3.0 |
|
1222 |
* |
|
1223 |
* @param int $status The HTTP response status code to use. |
|
1224 |
* @param string $location The path or URL to redirect to. |
|
1225 |
*/ |
|
1226 |
$status = apply_filters( 'wp_redirect_status', $status, $location ); |
|
1227 |
||
1228 |
if ( ! $location ) { |
|
1229 |
return false; |
|
1230 |
} |
|
1231 |
||
1232 |
$location = wp_sanitize_redirect( $location ); |
|
1233 |
||
1234 |
if ( ! $is_IIS && PHP_SAPI != 'cgi-fcgi' ) { |
|
1235 |
status_header( $status ); // This causes problems on IIS and some FastCGI setups |
|
1236 |
} |
|
1237 |
||
1238 |
/** |
|
1239 |
* Filters the X-Redirect-By header. |
|
1240 |
* |
|
1241 |
* Allows applications to identify themselves when they're doing a redirect. |
|
1242 |
* |
|
1243 |
* @since 5.1.0 |
|
1244 |
* |
|
1245 |
* @param string $x_redirect_by The application doing the redirect. |
|
1246 |
* @param int $status Status code to use. |
|
1247 |
* @param string $location The path to redirect to. |
|
1248 |
*/ |
|
1249 |
$x_redirect_by = apply_filters( 'x_redirect_by', $x_redirect_by, $status, $location ); |
|
1250 |
if ( is_string( $x_redirect_by ) ) { |
|
1251 |
header( "X-Redirect-By: $x_redirect_by" ); |
|
1252 |
} |
|
1253 |
||
1254 |
header( "Location: $location", true, $status ); |
|
1255 |
||
1256 |
return true; |
|
1257 |
} |
|
1258 |
endif; |
|
1259 |
||
1260 |
if ( ! function_exists( 'wp_sanitize_redirect' ) ) : |
|
0 | 1261 |
/** |
9 | 1262 |
* Sanitizes a URL for use in a redirect. |
0 | 1263 |
* |
1264 |
* @since 2.3.0 |
|
1265 |
* |
|
1266 |
* @param string $location The path to redirect to. |
|
9 | 1267 |
* @return string Redirect-sanitized URL. |
0 | 1268 |
*/ |
9 | 1269 |
function wp_sanitize_redirect( $location ) { |
1270 |
$regex = '/ |
|
5 | 1271 |
( |
1272 |
(?: [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx |
|
1273 |
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 |
|
1274 |
| [\xE1-\xEC][\x80-\xBF]{2} |
|
1275 |
| \xED[\x80-\x9F][\x80-\xBF] |
|
1276 |
| [\xEE-\xEF][\x80-\xBF]{2} |
|
1277 |
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 |
|
1278 |
| [\xF1-\xF3][\x80-\xBF]{3} |
|
1279 |
| \xF4[\x80-\x8F][\x80-\xBF]{2} |
|
1280 |
){1,40} # ...one or more times |
|
1281 |
)/x'; |
|
9 | 1282 |
$location = preg_replace_callback( $regex, '_wp_sanitize_utf8_in_redirect', $location ); |
1283 |
$location = preg_replace( '|[^a-z0-9-~+_.?#=&;,/:%!*\[\]()@]|i', '', $location ); |
|
1284 |
$location = wp_kses_no_null( $location ); |
|
0 | 1285 |
|
9 | 1286 |
// remove %0d and %0a from location |
1287 |
$strip = array( '%0d', '%0a', '%0D', '%0A' ); |
|
1288 |
return _deep_replace( $strip, $location ); |
|
5 | 1289 |
} |
1290 |
||
1291 |
/** |
|
9 | 1292 |
* URL encode UTF-8 characters in a URL. |
5 | 1293 |
* |
9 | 1294 |
* @ignore |
1295 |
* @since 4.2.0 |
|
1296 |
* @access private |
|
5 | 1297 |
* |
9 | 1298 |
* @see wp_sanitize_redirect() |
5 | 1299 |
* |
9 | 1300 |
* @param array $matches RegEx matches against the redirect location. |
1301 |
* @return string URL-encoded version of the first RegEx match. |
|
5 | 1302 |
*/ |
9 | 1303 |
function _wp_sanitize_utf8_in_redirect( $matches ) { |
1304 |
return urlencode( $matches[0] ); |
|
5 | 1305 |
} |
9 | 1306 |
endif; |
0 | 1307 |
|
9 | 1308 |
if ( ! function_exists( 'wp_safe_redirect' ) ) : |
1309 |
/** |
|
1310 |
* Performs a safe (local) redirect, using wp_redirect(). |
|
1311 |
* |
|
1312 |
* Checks whether the $location is using an allowed host, if it has an absolute |
|
1313 |
* path. A plugin can therefore set or remove allowed host(s) to or from the |
|
1314 |
* list. |
|
1315 |
* |
|
1316 |
* If the host is not allowed, then the redirect defaults to wp-admin on the siteurl |
|
1317 |
* instead. This prevents malicious redirects which redirect to another host, |
|
1318 |
* but only used in a few places. |
|
1319 |
* |
|
1320 |
* Note: wp_safe_redirect() does not exit automatically, and should almost always be |
|
1321 |
* followed by a call to `exit;`: |
|
1322 |
* |
|
1323 |
* wp_safe_redirect( $url ); |
|
1324 |
* exit; |
|
1325 |
* |
|
1326 |
* Exiting can also be selectively manipulated by using wp_safe_redirect() as a conditional |
|
1327 |
* in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_location'} filters: |
|
1328 |
* |
|
1329 |
* if ( wp_safe_redirect( $url ) ) { |
|
1330 |
* exit; |
|
1331 |
* } |
|
1332 |
* |
|
1333 |
* @since 2.3.0 |
|
1334 |
* @since 5.1.0 The return value from wp_redirect() is now passed on, and the `$x_redirect_by` parameter was added. |
|
1335 |
* |
|
1336 |
* @param string $location The path or URL to redirect to. |
|
1337 |
* @param int $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily). |
|
1338 |
* @param string $x_redirect_by Optional. The application doing the redirect. Default 'WordPress'. |
|
1339 |
* @return bool $redirect False if the redirect was cancelled, true otherwise. |
|
1340 |
*/ |
|
1341 |
function wp_safe_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) { |
|
0 | 1342 |
|
9 | 1343 |
// Need to look at the URL the way it will end up in wp_redirect() |
1344 |
$location = wp_sanitize_redirect( $location ); |
|
1345 |
||
1346 |
/** |
|
1347 |
* Filters the redirect fallback URL for when the provided redirect is not safe (local). |
|
1348 |
* |
|
1349 |
* @since 4.3.0 |
|
1350 |
* |
|
1351 |
* @param string $fallback_url The fallback URL to use by default. |
|
1352 |
* @param int $status The HTTP response status code to use. |
|
1353 |
*/ |
|
1354 |
$location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) ); |
|
1355 |
||
1356 |
return wp_redirect( $location, $status, $x_redirect_by ); |
|
1357 |
} |
|
1358 |
endif; |
|
0 | 1359 |
|
9 | 1360 |
if ( ! function_exists( 'wp_validate_redirect' ) ) : |
1361 |
/** |
|
1362 |
* Validates a URL for use in a redirect. |
|
1363 |
* |
|
1364 |
* Checks whether the $location is using an allowed host, if it has an absolute |
|
1365 |
* path. A plugin can therefore set or remove allowed host(s) to or from the |
|
1366 |
* list. |
|
1367 |
* |
|
1368 |
* If the host is not allowed, then the redirect is to $default supplied |
|
1369 |
* |
|
1370 |
* @since 2.8.1 |
|
1371 |
* |
|
1372 |
* @param string $location The redirect to validate |
|
1373 |
* @param string $default The value to return if $location is not allowed |
|
1374 |
* @return string redirect-sanitized URL |
|
1375 |
*/ |
|
1376 |
function wp_validate_redirect( $location, $default = '' ) { |
|
1377 |
$location = trim( $location, " \t\n\r\0\x08\x0B" ); |
|
1378 |
// browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//' |
|
1379 |
if ( substr( $location, 0, 2 ) == '//' ) { |
|
1380 |
$location = 'http:' . $location; |
|
1381 |
} |
|
0 | 1382 |
|
9 | 1383 |
// In php 5 parse_url may fail if the URL query part contains http://, bug #38143 |
1384 |
$test = ( $cut = strpos( $location, '?' ) ) ? substr( $location, 0, $cut ) : $location; |
|
1385 |
||
1386 |
// @-operator is used to prevent possible warnings in PHP < 5.3.3. |
|
1387 |
$lp = @parse_url( $test ); |
|
1388 |
||
1389 |
// Give up if malformed URL |
|
1390 |
if ( false === $lp ) { |
|
1391 |
return $default; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
} |
0 | 1393 |
|
9 | 1394 |
// Allow only http and https schemes. No data:, etc. |
1395 |
if ( isset( $lp['scheme'] ) && ! ( 'http' == $lp['scheme'] || 'https' == $lp['scheme'] ) ) { |
|
1396 |
return $default; |
|
1397 |
} |
|
0 | 1398 |
|
9 | 1399 |
if ( ! isset( $lp['host'] ) && ! empty( $lp['path'] ) && '/' !== $lp['path'][0] ) { |
1400 |
$path = ''; |
|
1401 |
if ( ! empty( $_SERVER['REQUEST_URI'] ) ) { |
|
1402 |
$path = dirname( parse_url( 'http://placeholder' . $_SERVER['REQUEST_URI'], PHP_URL_PATH ) . '?' ); |
|
13 | 1403 |
$path = wp_normalize_path( $path ); |
9 | 1404 |
} |
1405 |
$location = '/' . ltrim( $path . '/', '/' ) . $location; |
|
1406 |
} |
|
0 | 1407 |
|
9 | 1408 |
// Reject if certain components are set but host is not. This catches urls like https:host.com for which parse_url does not set the host field. |
1409 |
if ( ! isset( $lp['host'] ) && ( isset( $lp['scheme'] ) || isset( $lp['user'] ) || isset( $lp['pass'] ) || isset( $lp['port'] ) ) ) { |
|
1410 |
return $default; |
|
1411 |
} |
|
0 | 1412 |
|
9 | 1413 |
// Reject malformed components parse_url() can return on odd inputs. |
1414 |
foreach ( array( 'user', 'pass', 'host' ) as $component ) { |
|
1415 |
if ( isset( $lp[ $component ] ) && strpbrk( $lp[ $component ], ':/?#@' ) ) { |
|
1416 |
return $default; |
|
1417 |
} |
|
1418 |
} |
|
1419 |
||
1420 |
$wpp = parse_url( home_url() ); |
|
5 | 1421 |
|
9 | 1422 |
/** |
1423 |
* Filters the whitelist of hosts to redirect to. |
|
1424 |
* |
|
1425 |
* @since 2.3.0 |
|
1426 |
* |
|
1427 |
* @param array $hosts An array of allowed hosts. |
|
1428 |
* @param bool|string $host The parsed host; empty if not isset. |
|
1429 |
*/ |
|
1430 |
$allowed_hosts = (array) apply_filters( 'allowed_redirect_hosts', array( $wpp['host'] ), isset( $lp['host'] ) ? $lp['host'] : '' ); |
|
0 | 1431 |
|
9 | 1432 |
if ( isset( $lp['host'] ) && ( ! in_array( $lp['host'], $allowed_hosts ) && $lp['host'] != strtolower( $wpp['host'] ) ) ) { |
1433 |
$location = $default; |
|
1434 |
} |
|
0 | 1435 |
|
9 | 1436 |
return $location; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1437 |
} |
0 | 1438 |
endif; |
1439 |
||
9 | 1440 |
if ( ! function_exists( 'wp_notify_postauthor' ) ) : |
1441 |
/** |
|
1442 |
* Notify an author (and/or others) of a comment/trackback/pingback on a post. |
|
1443 |
* |
|
1444 |
* @since 1.0.0 |
|
1445 |
* |
|
1446 |
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object. |
|
1447 |
* @param string $deprecated Not used |
|
1448 |
* @return bool True on completion. False if no email addresses were specified. |
|
1449 |
*/ |
|
1450 |
function wp_notify_postauthor( $comment_id, $deprecated = null ) { |
|
1451 |
if ( null !== $deprecated ) { |
|
1452 |
_deprecated_argument( __FUNCTION__, '3.8.0' ); |
|
1453 |
} |
|
1454 |
||
1455 |
$comment = get_comment( $comment_id ); |
|
1456 |
if ( empty( $comment ) || empty( $comment->comment_post_ID ) ) { |
|
1457 |
return false; |
|
1458 |
} |
|
1459 |
||
1460 |
$post = get_post( $comment->comment_post_ID ); |
|
1461 |
$author = get_userdata( $post->post_author ); |
|
1462 |
||
1463 |
// Who to notify? By default, just the post author, but others can be added. |
|
1464 |
$emails = array(); |
|
1465 |
if ( $author ) { |
|
1466 |
$emails[] = $author->user_email; |
|
1467 |
} |
|
1468 |
||
1469 |
/** |
|
1470 |
* Filters the list of email addresses to receive a comment notification. |
|
1471 |
* |
|
1472 |
* By default, only post authors are notified of comments. This filter allows |
|
1473 |
* others to be added. |
|
1474 |
* |
|
1475 |
* @since 3.7.0 |
|
1476 |
* |
|
1477 |
* @param array $emails An array of email addresses to receive a comment notification. |
|
1478 |
* @param int $comment_id The comment ID. |
|
1479 |
*/ |
|
1480 |
$emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID ); |
|
1481 |
$emails = array_filter( $emails ); |
|
1482 |
||
1483 |
// If there are no addresses to send the comment to, bail. |
|
1484 |
if ( ! count( $emails ) ) { |
|
1485 |
return false; |
|
1486 |
} |
|
1487 |
||
1488 |
// Facilitate unsetting below without knowing the keys. |
|
1489 |
$emails = array_flip( $emails ); |
|
1490 |
||
1491 |
/** |
|
1492 |
* Filters whether to notify comment authors of their comments on their own posts. |
|
1493 |
* |
|
1494 |
* By default, comment authors aren't notified of their comments on their own |
|
1495 |
* posts. This filter allows you to override that. |
|
1496 |
* |
|
1497 |
* @since 3.8.0 |
|
1498 |
* |
|
1499 |
* @param bool $notify Whether to notify the post author of their own comment. |
|
1500 |
* Default false. |
|
1501 |
* @param int $comment_id The comment ID. |
|
1502 |
*/ |
|
1503 |
$notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID ); |
|
1504 |
||
1505 |
// The comment was left by the author |
|
1506 |
if ( $author && ! $notify_author && $comment->user_id == $post->post_author ) { |
|
1507 |
unset( $emails[ $author->user_email ] ); |
|
1508 |
} |
|
1509 |
||
1510 |
// The author moderated a comment on their own post |
|
1511 |
if ( $author && ! $notify_author && $post->post_author == get_current_user_id() ) { |
|
1512 |
unset( $emails[ $author->user_email ] ); |
|
1513 |
} |
|
1514 |
||
1515 |
// The post author is no longer a member of the blog |
|
1516 |
if ( $author && ! $notify_author && ! user_can( $post->post_author, 'read_post', $post->ID ) ) { |
|
1517 |
unset( $emails[ $author->user_email ] ); |
|
1518 |
} |
|
1519 |
||
1520 |
// If there's no email to send the comment to, bail, otherwise flip array back around for use below |
|
1521 |
if ( ! count( $emails ) ) { |
|
1522 |
return false; |
|
1523 |
} else { |
|
1524 |
$emails = array_flip( $emails ); |
|
1525 |
} |
|
1526 |
||
1527 |
$switched_locale = switch_to_locale( get_locale() ); |
|
1528 |
||
1529 |
$comment_author_domain = @gethostbyaddr( $comment->comment_author_IP ); |
|
1530 |
||
1531 |
// The blogname option is escaped with esc_html on the way into the database in sanitize_option |
|
1532 |
// we want to reverse this for the plain text arena of emails. |
|
1533 |
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
|
1534 |
$comment_content = wp_specialchars_decode( $comment->comment_content ); |
|
0 | 1535 |
|
9 | 1536 |
switch ( $comment->comment_type ) { |
1537 |
case 'trackback': |
|
1538 |
/* translators: %s: post title */ |
|
1539 |
$notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n"; |
|
1540 |
/* translators: 1: trackback/pingback website name, 2: website IP address, 3: website hostname */ |
|
1541 |
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
|
1542 |
/* translators: %s: trackback/pingback/comment author URL */ |
|
1543 |
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
|
1544 |
/* translators: %s: comment text */ |
|
1545 |
$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
|
1546 |
$notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n"; |
|
1547 |
/* translators: Trackback notification email subject. 1: Site title, 2: Post title */ |
|
1548 |
$subject = sprintf( __( '[%1$s] Trackback: "%2$s"' ), $blogname, $post->post_title ); |
|
1549 |
break; |
|
1550 |
case 'pingback': |
|
1551 |
/* translators: %s: post title */ |
|
1552 |
$notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n"; |
|
1553 |
/* translators: 1: trackback/pingback website name, 2: website IP address, 3: website hostname */ |
|
1554 |
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
|
1555 |
/* translators: %s: trackback/pingback/comment author URL */ |
|
1556 |
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
|
1557 |
/* translators: %s: comment text */ |
|
1558 |
$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
|
1559 |
$notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n"; |
|
1560 |
/* translators: Pingback notification email subject. 1: Site title, 2: Post title */ |
|
1561 |
$subject = sprintf( __( '[%1$s] Pingback: "%2$s"' ), $blogname, $post->post_title ); |
|
1562 |
break; |
|
1563 |
default: // Comments |
|
1564 |
/* translators: %s: post title */ |
|
1565 |
$notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n"; |
|
1566 |
/* translators: 1: comment author's name, 2: comment author's IP address, 3: comment author's hostname */ |
|
1567 |
$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
|
1568 |
/* translators: %s: comment author email */ |
|
1569 |
$notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; |
|
1570 |
/* translators: %s: trackback/pingback/comment author URL */ |
|
1571 |
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
|
1572 |
/* translators: %s: comment text */ |
|
1573 |
$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
|
1574 |
$notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n"; |
|
1575 |
/* translators: Comment notification email subject. 1: Site title, 2: Post title */ |
|
1576 |
$subject = sprintf( __( '[%1$s] Comment: "%2$s"' ), $blogname, $post->post_title ); |
|
1577 |
break; |
|
1578 |
} |
|
1579 |
$notify_message .= get_permalink( $comment->comment_post_ID ) . "#comments\r\n\r\n"; |
|
1580 |
$notify_message .= sprintf( __( 'Permalink: %s' ), get_comment_link( $comment ) ) . "\r\n"; |
|
1581 |
||
1582 |
if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) { |
|
1583 |
if ( EMPTY_TRASH_DAYS ) { |
|
1584 |
/* translators: Comment moderation. %s: Comment action URL */ |
|
1585 |
$notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; |
|
1586 |
} else { |
|
1587 |
/* translators: Comment moderation. %s: Comment action URL */ |
|
1588 |
$notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; |
|
1589 |
} |
|
1590 |
/* translators: Comment moderation. %s: Comment action URL */ |
|
1591 |
$notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; |
|
1592 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1593 |
|
9 | 1594 |
$wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', strtolower( $_SERVER['SERVER_NAME'] ) ); |
1595 |
||
1596 |
if ( '' == $comment->comment_author ) { |
|
1597 |
$from = "From: \"$blogname\" <$wp_email>"; |
|
1598 |
if ( '' != $comment->comment_author_email ) { |
|
1599 |
$reply_to = "Reply-To: $comment->comment_author_email"; |
|
1600 |
} |
|
1601 |
} else { |
|
1602 |
$from = "From: \"$comment->comment_author\" <$wp_email>"; |
|
1603 |
if ( '' != $comment->comment_author_email ) { |
|
1604 |
$reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>"; |
|
1605 |
} |
|
1606 |
} |
|
1607 |
||
1608 |
$message_headers = "$from\n" |
|
1609 |
. 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n"; |
|
1610 |
||
1611 |
if ( isset( $reply_to ) ) { |
|
1612 |
$message_headers .= $reply_to . "\n"; |
|
1613 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1614 |
|
9 | 1615 |
/** |
1616 |
* Filters the comment notification email text. |
|
1617 |
* |
|
1618 |
* @since 1.5.2 |
|
1619 |
* |
|
1620 |
* @param string $notify_message The comment notification email text. |
|
1621 |
* @param int $comment_id Comment ID. |
|
1622 |
*/ |
|
1623 |
$notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID ); |
|
1624 |
||
1625 |
/** |
|
1626 |
* Filters the comment notification email subject. |
|
1627 |
* |
|
1628 |
* @since 1.5.2 |
|
1629 |
* |
|
1630 |
* @param string $subject The comment notification email subject. |
|
1631 |
* @param int $comment_id Comment ID. |
|
1632 |
*/ |
|
1633 |
$subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID ); |
|
1634 |
||
1635 |
/** |
|
1636 |
* Filters the comment notification email headers. |
|
1637 |
* |
|
1638 |
* @since 1.5.2 |
|
1639 |
* |
|
1640 |
* @param string $message_headers Headers for the comment notification email. |
|
1641 |
* @param int $comment_id Comment ID. |
|
1642 |
*/ |
|
1643 |
$message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID ); |
|
1644 |
||
1645 |
foreach ( $emails as $email ) { |
|
1646 |
@wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); |
|
1647 |
} |
|
1648 |
||
1649 |
if ( $switched_locale ) { |
|
1650 |
restore_previous_locale(); |
|
1651 |
} |
|
1652 |
||
0 | 1653 |
return true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1654 |
} |
0 | 1655 |
endif; |
1656 |
||
9 | 1657 |
if ( ! function_exists( 'wp_notify_moderator' ) ) : |
1658 |
/** |
|
1659 |
* Notifies the moderator of the site about a new comment that is awaiting approval. |
|
1660 |
* |
|
1661 |
* @since 1.0.0 |
|
1662 |
* |
|
1663 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
1664 |
* |
|
1665 |
* Uses the {@see 'notify_moderator'} filter to determine whether the site moderator |
|
1666 |
* should be notified, overriding the site setting. |
|
1667 |
* |
|
1668 |
* @param int $comment_id Comment ID. |
|
1669 |
* @return true Always returns true. |
|
1670 |
*/ |
|
1671 |
function wp_notify_moderator( $comment_id ) { |
|
1672 |
global $wpdb; |
|
1673 |
||
1674 |
$maybe_notify = get_option( 'moderation_notify' ); |
|
1675 |
||
1676 |
/** |
|
1677 |
* Filters whether to send the site moderator email notifications, overriding the site setting. |
|
1678 |
* |
|
1679 |
* @since 4.4.0 |
|
1680 |
* |
|
1681 |
* @param bool $maybe_notify Whether to notify blog moderator. |
|
1682 |
* @param int $comment_ID The id of the comment for the notification. |
|
1683 |
*/ |
|
1684 |
$maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id ); |
|
1685 |
||
1686 |
if ( ! $maybe_notify ) { |
|
1687 |
return true; |
|
1688 |
} |
|
1689 |
||
1690 |
$comment = get_comment( $comment_id ); |
|
1691 |
$post = get_post( $comment->comment_post_ID ); |
|
1692 |
$user = get_userdata( $post->post_author ); |
|
1693 |
// Send to the administration and to the post author if the author can modify the comment. |
|
1694 |
$emails = array( get_option( 'admin_email' ) ); |
|
1695 |
if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) { |
|
1696 |
if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) { |
|
1697 |
$emails[] = $user->user_email; |
|
1698 |
} |
|
1699 |
} |
|
1700 |
||
1701 |
$switched_locale = switch_to_locale( get_locale() ); |
|
1702 |
||
1703 |
$comment_author_domain = @gethostbyaddr( $comment->comment_author_IP ); |
|
1704 |
$comments_waiting = $wpdb->get_var( "SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'" ); |
|
1705 |
||
0 | 1706 |
// The blogname option is escaped with esc_html on the way into the database in sanitize_option |
1707 |
// we want to reverse this for the plain text arena of emails. |
|
9 | 1708 |
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
1709 |
$comment_content = wp_specialchars_decode( $comment->comment_content ); |
|
1710 |
||
1711 |
switch ( $comment->comment_type ) { |
|
1712 |
case 'trackback': |
|
1713 |
/* translators: %s: post title */ |
|
1714 |
$notify_message = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; |
|
1715 |
$notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; |
|
1716 |
/* translators: 1: trackback/pingback website name, 2: website IP address, 3: website hostname */ |
|
1717 |
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
|
1718 |
/* translators: %s: trackback/pingback/comment author URL */ |
|
1719 |
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
|
1720 |
$notify_message .= __( 'Trackback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n"; |
|
1721 |
break; |
|
1722 |
case 'pingback': |
|
1723 |
/* translators: %s: post title */ |
|
1724 |
$notify_message = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; |
|
1725 |
$notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; |
|
1726 |
/* translators: 1: trackback/pingback website name, 2: website IP address, 3: website hostname */ |
|
1727 |
$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
|
1728 |
/* translators: %s: trackback/pingback/comment author URL */ |
|
1729 |
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
|
1730 |
$notify_message .= __( 'Pingback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n"; |
|
1731 |
break; |
|
1732 |
default: // Comments |
|
1733 |
/* translators: %s: post title */ |
|
1734 |
$notify_message = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; |
|
1735 |
$notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; |
|
1736 |
/* translators: 1: comment author's name, 2: comment author's IP address, 3: comment author's hostname */ |
|
1737 |
$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; |
|
1738 |
/* translators: %s: comment author email */ |
|
1739 |
$notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; |
|
1740 |
/* translators: %s: trackback/pingback/comment author URL */ |
|
1741 |
$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; |
|
1742 |
/* translators: %s: comment text */ |
|
1743 |
$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; |
|
1744 |
break; |
|
1745 |
} |
|
1746 |
||
1747 |
/* translators: Comment moderation. %s: Comment action URL */ |
|
1748 |
$notify_message .= sprintf( __( 'Approve it: %s' ), admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) ) . "\r\n"; |
|
1749 |
||
1750 |
if ( EMPTY_TRASH_DAYS ) { |
|
1751 |
/* translators: Comment moderation. %s: Comment action URL */ |
|
1752 |
$notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) ) . "\r\n"; |
|
1753 |
} else { |
|
1754 |
/* translators: Comment moderation. %s: Comment action URL */ |
|
1755 |
$notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) ) . "\r\n"; |
|
1756 |
} |
|
1757 |
||
1758 |
/* translators: Comment moderation. %s: Comment action URL */ |
|
1759 |
$notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) ) . "\r\n"; |
|
1760 |
||
1761 |
/* translators: Comment moderation. %s: Number of comments awaiting approval */ |
|
1762 |
$notify_message .= sprintf( |
|
1763 |
_n( |
|
1764 |
'Currently %s comment is waiting for approval. Please visit the moderation panel:', |
|
1765 |
'Currently %s comments are waiting for approval. Please visit the moderation panel:', |
|
1766 |
$comments_waiting |
|
1767 |
), |
|
1768 |
number_format_i18n( $comments_waiting ) |
|
1769 |
) . "\r\n"; |
|
1770 |
$notify_message .= admin_url( 'edit-comments.php?comment_status=moderated#wpbody-content' ) . "\r\n"; |
|
1771 |
||
1772 |
/* translators: Comment moderation notification email subject. 1: Site name, 2: Post title */ |
|
1773 |
$subject = sprintf( __( '[%1$s] Please moderate: "%2$s"' ), $blogname, $post->post_title ); |
|
1774 |
$message_headers = ''; |
|
1775 |
||
1776 |
/** |
|
1777 |
* Filters the list of recipients for comment moderation emails. |
|
1778 |
* |
|
1779 |
* @since 3.7.0 |
|
1780 |
* |
|
1781 |
* @param array $emails List of email addresses to notify for comment moderation. |
|
1782 |
* @param int $comment_id Comment ID. |
|
1783 |
*/ |
|
1784 |
$emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id ); |
|
1785 |
||
1786 |
/** |
|
1787 |
* Filters the comment moderation email text. |
|
1788 |
* |
|
1789 |
* @since 1.5.2 |
|
1790 |
* |
|
1791 |
* @param string $notify_message Text of the comment moderation email. |
|
1792 |
* @param int $comment_id Comment ID. |
|
1793 |
*/ |
|
1794 |
$notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id ); |
|
1795 |
||
1796 |
/** |
|
1797 |
* Filters the comment moderation email subject. |
|
1798 |
* |
|
1799 |
* @since 1.5.2 |
|
1800 |
* |
|
1801 |
* @param string $subject Subject of the comment moderation email. |
|
1802 |
* @param int $comment_id Comment ID. |
|
1803 |
*/ |
|
1804 |
$subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id ); |
|
1805 |
||
1806 |
/** |
|
1807 |
* Filters the comment moderation email headers. |
|
1808 |
* |
|
1809 |
* @since 2.8.0 |
|
1810 |
* |
|
1811 |
* @param string $message_headers Headers for the comment moderation email. |
|
1812 |
* @param int $comment_id Comment ID. |
|
1813 |
*/ |
|
1814 |
$message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id ); |
|
1815 |
||
1816 |
foreach ( $emails as $email ) { |
|
1817 |
@wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); |
|
1818 |
} |
|
1819 |
||
1820 |
if ( $switched_locale ) { |
|
1821 |
restore_previous_locale(); |
|
1822 |
} |
|
1823 |
||
1824 |
return true; |
|
1825 |
} |
|
1826 |
endif; |
|
1827 |
||
1828 |
if ( ! function_exists( 'wp_password_change_notification' ) ) : |
|
1829 |
/** |
|
1830 |
* Notify the blog admin of a user changing password, normally via email. |
|
1831 |
* |
|
1832 |
* @since 2.7.0 |
|
1833 |
* |
|
1834 |
* @param WP_User $user User object. |
|
1835 |
*/ |
|
1836 |
function wp_password_change_notification( $user ) { |
|
1837 |
// send a copy of password change notification to the admin |
|
1838 |
// but check to see if it's the admin whose password we're changing, and skip this |
|
1839 |
if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) { |
|
1840 |
/* translators: %s: user name */ |
|
1841 |
$message = sprintf( __( 'Password changed for user: %s' ), $user->user_login ) . "\r\n"; |
|
1842 |
// The blogname option is escaped with esc_html on the way into the database in sanitize_option |
|
1843 |
// we want to reverse this for the plain text arena of emails. |
|
1844 |
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1845 |
|
9 | 1846 |
$wp_password_change_notification_email = array( |
1847 |
'to' => get_option( 'admin_email' ), |
|
1848 |
/* translators: Password change notification email subject. %s: Site title */ |
|
1849 |
'subject' => __( '[%s] Password Changed' ), |
|
1850 |
'message' => $message, |
|
1851 |
'headers' => '', |
|
1852 |
); |
|
1853 |
||
1854 |
/** |
|
1855 |
* Filters the contents of the password change notification email sent to the site admin. |
|
1856 |
* |
|
1857 |
* @since 4.9.0 |
|
1858 |
* |
|
1859 |
* @param array $wp_password_change_notification_email { |
|
1860 |
* Used to build wp_mail(). |
|
1861 |
* |
|
1862 |
* @type string $to The intended recipient - site admin email address. |
|
1863 |
* @type string $subject The subject of the email. |
|
1864 |
* @type string $message The body of the email. |
|
1865 |
* @type string $headers The headers of the email. |
|
1866 |
* } |
|
1867 |
* @param WP_User $user User object for user whose password was changed. |
|
1868 |
* @param string $blogname The site title. |
|
1869 |
*/ |
|
1870 |
$wp_password_change_notification_email = apply_filters( 'wp_password_change_notification_email', $wp_password_change_notification_email, $user, $blogname ); |
|
1871 |
||
1872 |
wp_mail( |
|
1873 |
$wp_password_change_notification_email['to'], |
|
1874 |
wp_specialchars_decode( sprintf( $wp_password_change_notification_email['subject'], $blogname ) ), |
|
1875 |
$wp_password_change_notification_email['message'], |
|
1876 |
$wp_password_change_notification_email['headers'] |
|
1877 |
); |
|
1878 |
} |
|
1879 |
} |
|
1880 |
endif; |
|
1881 |
||
1882 |
if ( ! function_exists( 'wp_new_user_notification' ) ) : |
|
1883 |
/** |
|
1884 |
* Email login credentials to a newly-registered user. |
|
1885 |
* |
|
1886 |
* A new user registration notification is also sent to admin email. |
|
1887 |
* |
|
1888 |
* @since 2.0.0 |
|
1889 |
* @since 4.3.0 The `$plaintext_pass` parameter was changed to `$notify`. |
|
1890 |
* @since 4.3.1 The `$plaintext_pass` parameter was deprecated. `$notify` added as a third parameter. |
|
1891 |
* @since 4.6.0 The `$notify` parameter accepts 'user' for sending notification only to the user created. |
|
1892 |
* |
|
1893 |
* @global wpdb $wpdb WordPress database object for queries. |
|
1894 |
* @global PasswordHash $wp_hasher Portable PHP password hashing framework instance. |
|
1895 |
* |
|
1896 |
* @param int $user_id User ID. |
|
1897 |
* @param null $deprecated Not used (argument deprecated). |
|
1898 |
* @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty |
|
1899 |
* string (admin only), 'user', or 'both' (admin and user). Default empty. |
|
1900 |
*/ |
|
1901 |
function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) { |
|
1902 |
if ( $deprecated !== null ) { |
|
1903 |
_deprecated_argument( __FUNCTION__, '4.3.1' ); |
|
1904 |
} |
|
1905 |
||
1906 |
// Accepts only 'user', 'admin' , 'both' or default '' as $notify |
|
1907 |
if ( ! in_array( $notify, array( 'user', 'admin', 'both', '' ), true ) ) { |
|
1908 |
return; |
|
1909 |
} |
|
1910 |
||
1911 |
global $wpdb, $wp_hasher; |
|
1912 |
$user = get_userdata( $user_id ); |
|
1913 |
||
1914 |
// The blogname option is escaped with esc_html on the way into the database in sanitize_option |
|
1915 |
// we want to reverse this for the plain text arena of emails. |
|
1916 |
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
|
1917 |
||
1918 |
if ( 'user' !== $notify ) { |
|
1919 |
$switched_locale = switch_to_locale( get_locale() ); |
|
1920 |
||
1921 |
/* translators: %s: site title */ |
|
1922 |
$message = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n"; |
|
1923 |
/* translators: %s: user login */ |
|
1924 |
$message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n"; |
|
1925 |
/* translators: %s: user email address */ |
|
1926 |
$message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n"; |
|
1927 |
||
1928 |
$wp_new_user_notification_email_admin = array( |
|
1929 |
'to' => get_option( 'admin_email' ), |
|
1930 |
/* translators: New user registration notification email subject. %s: Site title */ |
|
1931 |
'subject' => __( '[%s] New User Registration' ), |
|
1932 |
'message' => $message, |
|
1933 |
'headers' => '', |
|
1934 |
); |
|
1935 |
||
1936 |
/** |
|
1937 |
* Filters the contents of the new user notification email sent to the site admin. |
|
1938 |
* |
|
1939 |
* @since 4.9.0 |
|
1940 |
* |
|
1941 |
* @param array $wp_new_user_notification_email { |
|
1942 |
* Used to build wp_mail(). |
|
1943 |
* |
|
1944 |
* @type string $to The intended recipient - site admin email address. |
|
1945 |
* @type string $subject The subject of the email. |
|
1946 |
* @type string $message The body of the email. |
|
1947 |
* @type string $headers The headers of the email. |
|
1948 |
* } |
|
1949 |
* @param WP_User $user User object for new user. |
|
1950 |
* @param string $blogname The site title. |
|
1951 |
*/ |
|
1952 |
$wp_new_user_notification_email_admin = apply_filters( 'wp_new_user_notification_email_admin', $wp_new_user_notification_email_admin, $user, $blogname ); |
|
1953 |
||
1954 |
@wp_mail( |
|
1955 |
$wp_new_user_notification_email_admin['to'], |
|
1956 |
wp_specialchars_decode( sprintf( $wp_new_user_notification_email_admin['subject'], $blogname ) ), |
|
1957 |
$wp_new_user_notification_email_admin['message'], |
|
1958 |
$wp_new_user_notification_email_admin['headers'] |
|
1959 |
); |
|
1960 |
||
1961 |
if ( $switched_locale ) { |
|
1962 |
restore_previous_locale(); |
|
1963 |
} |
|
1964 |
} |
|
1965 |
||
1966 |
// `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification. |
|
1967 |
if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) { |
|
1968 |
return; |
|
1969 |
} |
|
1970 |
||
1971 |
// Generate something random for a password reset key. |
|
1972 |
$key = wp_generate_password( 20, false ); |
|
1973 |
||
1974 |
/** This action is documented in wp-login.php */ |
|
1975 |
do_action( 'retrieve_password_key', $user->user_login, $key ); |
|
1976 |
||
1977 |
// Now insert the key, hashed, into the DB. |
|
1978 |
if ( empty( $wp_hasher ) ) { |
|
1979 |
require_once ABSPATH . WPINC . '/class-phpass.php'; |
|
1980 |
$wp_hasher = new PasswordHash( 8, true ); |
|
1981 |
} |
|
1982 |
$hashed = time() . ':' . $wp_hasher->HashPassword( $key ); |
|
1983 |
$wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) ); |
|
1984 |
||
1985 |
$switched_locale = switch_to_locale( get_user_locale( $user ) ); |
|
1986 |
||
1987 |
/* translators: %s: user login */ |
|
1988 |
$message = sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n"; |
|
1989 |
$message .= __( 'To set your password, visit the following address:' ) . "\r\n\r\n"; |
|
1990 |
$message .= '<' . network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user->user_login ), 'login' ) . ">\r\n\r\n"; |
|
1991 |
||
1992 |
$message .= wp_login_url() . "\r\n"; |
|
1993 |
||
1994 |
$wp_new_user_notification_email = array( |
|
1995 |
'to' => $user->user_email, |
|
1996 |
/* translators: Login details notification email subject. %s: Site title */ |
|
1997 |
'subject' => __( '[%s] Login Details' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1998 |
'message' => $message, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1999 |
'headers' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2000 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2001 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2002 |
/** |
9 | 2003 |
* Filters the contents of the new user notification email sent to the new user. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2004 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2005 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2006 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2007 |
* @param array $wp_new_user_notification_email { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2008 |
* Used to build wp_mail(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2009 |
* |
9 | 2010 |
* @type string $to The intended recipient - New user email address. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2011 |
* @type string $subject The subject of the email. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2012 |
* @type string $message The body of the email. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2013 |
* @type string $headers The headers of the email. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2014 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2015 |
* @param WP_User $user User object for new user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2016 |
* @param string $blogname The site title. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2017 |
*/ |
9 | 2018 |
$wp_new_user_notification_email = apply_filters( 'wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname ); |
0 | 2019 |
|
9 | 2020 |
wp_mail( |
2021 |
$wp_new_user_notification_email['to'], |
|
2022 |
wp_specialchars_decode( sprintf( $wp_new_user_notification_email['subject'], $blogname ) ), |
|
2023 |
$wp_new_user_notification_email['message'], |
|
2024 |
$wp_new_user_notification_email['headers'] |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2025 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2026 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2027 |
if ( $switched_locale ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2028 |
restore_previous_locale(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2029 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2030 |
} |
0 | 2031 |
endif; |
2032 |
||
9 | 2033 |
if ( ! function_exists( 'wp_nonce_tick' ) ) : |
5 | 2034 |
/** |
9 | 2035 |
* Get the time-dependent variable for nonce creation. |
2036 |
* |
|
2037 |
* A nonce has a lifespan of two ticks. Nonces in their second tick may be |
|
2038 |
* updated, e.g. by autosave. |
|
5 | 2039 |
* |
2040 |
* @since 2.5.0 |
|
2041 |
* |
|
9 | 2042 |
* @return float Float value rounded up to the next highest integer. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2043 |
*/ |
9 | 2044 |
function wp_nonce_tick() { |
5 | 2045 |
/** |
9 | 2046 |
* Filters the lifespan of nonces in seconds. |
5 | 2047 |
* |
2048 |
* @since 2.5.0 |
|
2049 |
* |
|
9 | 2050 |
* @param int $lifespan Lifespan of nonces in seconds. Default 86,400 seconds, or one day. |
5 | 2051 |
*/ |
9 | 2052 |
$nonce_life = apply_filters( 'nonce_life', DAY_IN_SECONDS ); |
0 | 2053 |
|
9 | 2054 |
return ceil( time() / ( $nonce_life / 2 ) ); |
0 | 2055 |
} |
2056 |
endif; |
|
2057 |
||
9 | 2058 |
if ( ! function_exists( 'wp_verify_nonce' ) ) : |
2059 |
/** |
|
2060 |
* Verify that correct nonce was used with time limit. |
|
2061 |
* |
|
2062 |
* The user is given an amount of time to use the token, so therefore, since the |
|
2063 |
* UID and $action remain the same, the independent variable is the time. |
|
2064 |
* |
|
2065 |
* @since 2.0.3 |
|
2066 |
* |
|
2067 |
* @param string $nonce Nonce that was used in the form to verify |
|
2068 |
* @param string|int $action Should give context to what is taking place and be the same when nonce was created. |
|
2069 |
* @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between |
|
2070 |
* 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. |
|
2071 |
*/ |
|
2072 |
function wp_verify_nonce( $nonce, $action = -1 ) { |
|
2073 |
$nonce = (string) $nonce; |
|
2074 |
$user = wp_get_current_user(); |
|
2075 |
$uid = (int) $user->ID; |
|
2076 |
if ( ! $uid ) { |
|
2077 |
/** |
|
2078 |
* Filters whether the user who generated the nonce is logged out. |
|
2079 |
* |
|
2080 |
* @since 3.5.0 |
|
2081 |
* |
|
2082 |
* @param int $uid ID of the nonce-owning user. |
|
2083 |
* @param string $action The nonce action. |
|
2084 |
*/ |
|
2085 |
$uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); |
|
2086 |
} |
|
2087 |
||
2088 |
if ( empty( $nonce ) ) { |
|
2089 |
return false; |
|
2090 |
} |
|
2091 |
||
2092 |
$token = wp_get_session_token(); |
|
2093 |
$i = wp_nonce_tick(); |
|
2094 |
||
2095 |
// Nonce generated 0-12 hours ago |
|
2096 |
$expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ); |
|
2097 |
if ( hash_equals( $expected, $nonce ) ) { |
|
2098 |
return 1; |
|
2099 |
} |
|
0 | 2100 |
|
9 | 2101 |
// Nonce generated 12-24 hours ago |
2102 |
$expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ); |
|
2103 |
if ( hash_equals( $expected, $nonce ) ) { |
|
2104 |
return 2; |
|
2105 |
} |
|
2106 |
||
2107 |
/** |
|
2108 |
* Fires when nonce verification fails. |
|
2109 |
* |
|
2110 |
* @since 4.4.0 |
|
2111 |
* |
|
2112 |
* @param string $nonce The invalid nonce. |
|
2113 |
* @param string|int $action The nonce action. |
|
2114 |
* @param WP_User $user The current user object. |
|
2115 |
* @param string $token The user's session token. |
|
2116 |
*/ |
|
2117 |
do_action( 'wp_verify_nonce_failed', $nonce, $action, $user, $token ); |
|
2118 |
||
2119 |
// Invalid nonce |
|
2120 |
return false; |
|
0 | 2121 |
} |
9 | 2122 |
endif; |
0 | 2123 |
|
9 | 2124 |
if ( ! function_exists( 'wp_create_nonce' ) ) : |
5 | 2125 |
/** |
9 | 2126 |
* Creates a cryptographic token tied to a specific action, user, user session, |
2127 |
* and window of time. |
|
5 | 2128 |
* |
9 | 2129 |
* @since 2.0.3 |
2130 |
* @since 4.0.0 Session tokens were integrated with nonce creation |
|
5 | 2131 |
* |
9 | 2132 |
* @param string|int $action Scalar value to add context to the nonce. |
2133 |
* @return string The token. |
|
5 | 2134 |
*/ |
9 | 2135 |
function wp_create_nonce( $action = -1 ) { |
2136 |
$user = wp_get_current_user(); |
|
2137 |
$uid = (int) $user->ID; |
|
2138 |
if ( ! $uid ) { |
|
2139 |
/** This filter is documented in wp-includes/pluggable.php */ |
|
2140 |
$uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); |
|
2141 |
} |
|
2142 |
||
2143 |
$token = wp_get_session_token(); |
|
2144 |
$i = wp_nonce_tick(); |
|
2145 |
||
2146 |
return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ); |
|
2147 |
} |
|
0 | 2148 |
endif; |
2149 |
||
9 | 2150 |
if ( ! function_exists( 'wp_salt' ) ) : |
2151 |
/** |
|
2152 |
* Get salt to add to hashes. |
|
2153 |
* |
|
2154 |
* Salts are created using secret keys. Secret keys are located in two places: |
|
2155 |
* in the database and in the wp-config.php file. The secret key in the database |
|
2156 |
* is randomly generated and will be appended to the secret keys in wp-config.php. |
|
2157 |
* |
|
2158 |
* The secret keys in wp-config.php should be updated to strong, random keys to maximize |
|
2159 |
* security. Below is an example of how the secret key constants are defined. |
|
2160 |
* Do not paste this example directly into wp-config.php. Instead, have a |
|
2161 |
* {@link https://api.wordpress.org/secret-key/1.1/salt/ secret key created} just |
|
2162 |
* for you. |
|
2163 |
* |
|
2164 |
* define('AUTH_KEY', ' Xakm<o xQy rw4EMsLKM-?!T+,PFF})H4lzcW57AF0U@N@< >M%G4Yt>f`z]MON'); |
|
2165 |
* define('SECURE_AUTH_KEY', 'LzJ}op]mr|6+![P}Ak:uNdJCJZd>(Hx.-Mh#Tz)pCIU#uGEnfFz|f ;;eU%/U^O~'); |
|
2166 |
* define('LOGGED_IN_KEY', '|i|Ux`9<p-h$aFf(qnT:sDO:D1P^wZ$$/Ra@miTJi9G;ddp_<q}6H1)o|a +&JCM'); |
|
2167 |
* define('NONCE_KEY', '%:R{[P|,s.KuMltH5}cI;/k<Gx~j!f0I)m_sIyu+&NJZ)-iO>z7X>QYR0Z_XnZ@|'); |
|
2168 |
* define('AUTH_SALT', 'eZyT)-Naw]F8CwA*VaW#q*|.)g@o}||wf~@C-YSt}(dh_r6EbI#A,y|nU2{B#JBW'); |
|
2169 |
* define('SECURE_AUTH_SALT', '!=oLUTXh,QW=H `}`L|9/^4-3 STz},T(w}W<I`.JjPi)<Bmf1v,HpGe}T1:Xt7n'); |
|
2170 |
* define('LOGGED_IN_SALT', '+XSqHc;@Q*K_b|Z?NC[3H!!EONbh.n<+=uKR:>*c(u`g~EJBf#8u#R{mUEZrozmm'); |
|
2171 |
* define('NONCE_SALT', 'h`GXHhD>SLWVfg1(1(N{;.V!MoE(SfbA_ksP@&`+AycHcAV$+?@3q+rxV{%^VyKT'); |
|
2172 |
* |
|
2173 |
* Salting passwords helps against tools which has stored hashed values of |
|
2174 |
* common dictionary strings. The added values makes it harder to crack. |
|
2175 |
* |
|
2176 |
* @since 2.5.0 |
|
2177 |
* |
|
2178 |
* @link https://api.wordpress.org/secret-key/1.1/salt/ Create secrets for wp-config.php |
|
2179 |
* |
|
2180 |
* @staticvar array $cached_salts |
|
2181 |
* @staticvar array $duplicated_keys |
|
2182 |
* |
|
2183 |
* @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce) |
|
2184 |
* @return string Salt value |
|
2185 |
*/ |
|
2186 |
function wp_salt( $scheme = 'auth' ) { |
|
2187 |
static $cached_salts = array(); |
|
2188 |
if ( isset( $cached_salts[ $scheme ] ) ) { |
|
2189 |
/** |
|
2190 |
* Filters the WordPress salt. |
|
2191 |
* |
|
2192 |
* @since 2.5.0 |
|
2193 |
* |
|
2194 |
* @param string $cached_salt Cached salt for the given scheme. |
|
2195 |
* @param string $scheme Authentication scheme. Values include 'auth', |
|
2196 |
* 'secure_auth', 'logged_in', and 'nonce'. |
|
2197 |
*/ |
|
2198 |
return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme ); |
|
2199 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2200 |
|
9 | 2201 |
static $duplicated_keys; |
2202 |
if ( null === $duplicated_keys ) { |
|
2203 |
$duplicated_keys = array( 'put your unique phrase here' => true ); |
|
2204 |
foreach ( array( 'AUTH', 'SECURE_AUTH', 'LOGGED_IN', 'NONCE', 'SECRET' ) as $first ) { |
|
2205 |
foreach ( array( 'KEY', 'SALT' ) as $second ) { |
|
2206 |
if ( ! defined( "{$first}_{$second}" ) ) { |
|
2207 |
continue; |
|
2208 |
} |
|
2209 |
$value = constant( "{$first}_{$second}" ); |
|
2210 |
$duplicated_keys[ $value ] = isset( $duplicated_keys[ $value ] ); |
|
2211 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2212 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2213 |
} |
9 | 2214 |
|
2215 |
$values = array( |
|
2216 |
'key' => '', |
|
2217 |
'salt' => '', |
|
2218 |
); |
|
2219 |
if ( defined( 'SECRET_KEY' ) && SECRET_KEY && empty( $duplicated_keys[ SECRET_KEY ] ) ) { |
|
2220 |
$values['key'] = SECRET_KEY; |
|
2221 |
} |
|
2222 |
if ( 'auth' == $scheme && defined( 'SECRET_SALT' ) && SECRET_SALT && empty( $duplicated_keys[ SECRET_SALT ] ) ) { |
|
2223 |
$values['salt'] = SECRET_SALT; |
|
2224 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2225 |
|
9 | 2226 |
if ( in_array( $scheme, array( 'auth', 'secure_auth', 'logged_in', 'nonce' ) ) ) { |
2227 |
foreach ( array( 'key', 'salt' ) as $type ) { |
|
2228 |
$const = strtoupper( "{$scheme}_{$type}" ); |
|
2229 |
if ( defined( $const ) && constant( $const ) && empty( $duplicated_keys[ constant( $const ) ] ) ) { |
|
2230 |
$values[ $type ] = constant( $const ); |
|
2231 |
} elseif ( ! $values[ $type ] ) { |
|
2232 |
$values[ $type ] = get_site_option( "{$scheme}_{$type}" ); |
|
2233 |
if ( ! $values[ $type ] ) { |
|
2234 |
$values[ $type ] = wp_generate_password( 64, true, true ); |
|
2235 |
update_site_option( "{$scheme}_{$type}", $values[ $type ] ); |
|
2236 |
} |
|
2237 |
} |
|
2238 |
} |
|
2239 |
} else { |
|
2240 |
if ( ! $values['key'] ) { |
|
2241 |
$values['key'] = get_site_option( 'secret_key' ); |
|
2242 |
if ( ! $values['key'] ) { |
|
2243 |
$values['key'] = wp_generate_password( 64, true, true ); |
|
2244 |
update_site_option( 'secret_key', $values['key'] ); |
|
2245 |
} |
|
2246 |
} |
|
2247 |
$values['salt'] = hash_hmac( 'md5', $scheme, $values['key'] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2248 |
} |
9 | 2249 |
|
2250 |
$cached_salts[ $scheme ] = $values['key'] . $values['salt']; |
|
2251 |
||
2252 |
/** This filter is documented in wp-includes/pluggable.php */ |
|
2253 |
return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme ); |
|
0 | 2254 |
} |
2255 |
endif; |
|
2256 |
||
9 | 2257 |
if ( ! function_exists( 'wp_hash' ) ) : |
2258 |
/** |
|
2259 |
* Get hash of given string. |
|
2260 |
* |
|
2261 |
* @since 2.0.3 |
|
2262 |
* |
|
2263 |
* @param string $data Plain text to hash |
|
2264 |
* @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce) |
|
2265 |
* @return string Hash of $data |
|
2266 |
*/ |
|
2267 |
function wp_hash( $data, $scheme = 'auth' ) { |
|
2268 |
$salt = wp_salt( $scheme ); |
|
2269 |
||
2270 |
return hash_hmac( 'md5', $data, $salt ); |
|
2271 |
} |
|
2272 |
endif; |
|
2273 |
||
2274 |
if ( ! function_exists( 'wp_hash_password' ) ) : |
|
2275 |
/** |
|
2276 |
* Create a hash (encrypt) of a plain text password. |
|
2277 |
* |
|
2278 |
* For integration with other applications, this function can be overwritten to |
|
2279 |
* instead use the other package password checking algorithm. |
|
2280 |
* |
|
2281 |
* @since 2.5.0 |
|
2282 |
* |
|
2283 |
* @global PasswordHash $wp_hasher PHPass object |
|
2284 |
* |
|
2285 |
* @param string $password Plain text user password to hash |
|
2286 |
* @return string The hash string of the password |
|
2287 |
*/ |
|
2288 |
function wp_hash_password( $password ) { |
|
2289 |
global $wp_hasher; |
|
2290 |
||
2291 |
if ( empty( $wp_hasher ) ) { |
|
2292 |
require_once( ABSPATH . WPINC . '/class-phpass.php' ); |
|
2293 |
// By default, use the portable hash from phpass |
|
2294 |
$wp_hasher = new PasswordHash( 8, true ); |
|
2295 |
} |
|
2296 |
||
2297 |
return $wp_hasher->HashPassword( trim( $password ) ); |
|
2298 |
} |
|
2299 |
endif; |
|
0 | 2300 |
|
9 | 2301 |
if ( ! function_exists( 'wp_check_password' ) ) : |
2302 |
/** |
|
2303 |
* Checks the plaintext password against the encrypted Password. |
|
2304 |
* |
|
2305 |
* Maintains compatibility between old version and the new cookie authentication |
|
2306 |
* protocol using PHPass library. The $hash parameter is the encrypted password |
|
2307 |
* and the function compares the plain text password when encrypted similarly |
|
2308 |
* against the already encrypted password to see if they match. |
|
2309 |
* |
|
2310 |
* For integration with other applications, this function can be overwritten to |
|
2311 |
* instead use the other package password checking algorithm. |
|
2312 |
* |
|
2313 |
* @since 2.5.0 |
|
2314 |
* |
|
2315 |
* @global PasswordHash $wp_hasher PHPass object used for checking the password |
|
2316 |
* against the $hash + $password |
|
2317 |
* @uses PasswordHash::CheckPassword |
|
2318 |
* |
|
2319 |
* @param string $password Plaintext user's password |
|
2320 |
* @param string $hash Hash of the user's password to check against. |
|
2321 |
* @param string|int $user_id Optional. User ID. |
|
2322 |
* @return bool False, if the $password does not match the hashed password |
|
2323 |
*/ |
|
2324 |
function wp_check_password( $password, $hash, $user_id = '' ) { |
|
2325 |
global $wp_hasher; |
|
0 | 2326 |
|
9 | 2327 |
// If the hash is still md5... |
2328 |
if ( strlen( $hash ) <= 32 ) { |
|
2329 |
$check = hash_equals( $hash, md5( $password ) ); |
|
2330 |
if ( $check && $user_id ) { |
|
2331 |
// Rehash using new hash. |
|
2332 |
wp_set_password( $password, $user_id ); |
|
2333 |
$hash = wp_hash_password( $password ); |
|
2334 |
} |
|
2335 |
||
2336 |
/** |
|
2337 |
* Filters whether the plaintext password matches the encrypted password. |
|
2338 |
* |
|
2339 |
* @since 2.5.0 |
|
2340 |
* |
|
2341 |
* @param bool $check Whether the passwords match. |
|
2342 |
* @param string $password The plaintext password. |
|
2343 |
* @param string $hash The hashed password. |
|
2344 |
* @param string|int $user_id User ID. Can be empty. |
|
2345 |
*/ |
|
2346 |
return apply_filters( 'check_password', $check, $password, $hash, $user_id ); |
|
2347 |
} |
|
2348 |
||
2349 |
// If the stored hash is longer than an MD5, presume the |
|
2350 |
// new style phpass portable hash. |
|
2351 |
if ( empty( $wp_hasher ) ) { |
|
2352 |
require_once( ABSPATH . WPINC . '/class-phpass.php' ); |
|
2353 |
// By default, use the portable hash from phpass |
|
2354 |
$wp_hasher = new PasswordHash( 8, true ); |
|
2355 |
} |
|
2356 |
||
2357 |
$check = $wp_hasher->CheckPassword( $password, $hash ); |
|
2358 |
||
2359 |
/** This filter is documented in wp-includes/pluggable.php */ |
|
2360 |
return apply_filters( 'check_password', $check, $password, $hash, $user_id ); |
|
2361 |
} |
|
0 | 2362 |
endif; |
2363 |
||
9 | 2364 |
if ( ! function_exists( 'wp_generate_password' ) ) : |
2365 |
/** |
|
2366 |
* Generates a random password drawn from the defined set of characters. |
|
2367 |
* |
|
2368 |
* Uses wp_rand() is used to create passwords with far less predictability |
|
2369 |
* than similar native PHP functions like `rand()` or `mt_rand()`. |
|
2370 |
* |
|
2371 |
* @since 2.5.0 |
|
2372 |
* |
|
2373 |
* @param int $length Optional. The length of password to generate. Default 12. |
|
2374 |
* @param bool $special_chars Optional. Whether to include standard special characters. |
|
2375 |
* Default true. |
|
2376 |
* @param bool $extra_special_chars Optional. Whether to include other special characters. |
|
2377 |
* Used when generating secret keys and salts. Default false. |
|
2378 |
* @return string The random password. |
|
2379 |
*/ |
|
2380 |
function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) { |
|
2381 |
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; |
|
2382 |
if ( $special_chars ) { |
|
2383 |
$chars .= '!@#$%^&*()'; |
|
2384 |
} |
|
2385 |
if ( $extra_special_chars ) { |
|
2386 |
$chars .= '-_ []{}<>~`+=,.;:/?|'; |
|
2387 |
} |
|
2388 |
||
2389 |
$password = ''; |
|
2390 |
for ( $i = 0; $i < $length; $i++ ) { |
|
2391 |
$password .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 ); |
|
2392 |
} |
|
2393 |
||
2394 |
/** |
|
2395 |
* Filters the randomly-generated password. |
|
2396 |
* |
|
2397 |
* @since 3.0.0 |
|
2398 |
* |
|
2399 |
* @param string $password The generated password. |
|
2400 |
*/ |
|
2401 |
return apply_filters( 'random_password', $password ); |
|
2402 |
} |
|
2403 |
endif; |
|
2404 |
||
2405 |
if ( ! function_exists( 'wp_rand' ) ) : |
|
2406 |
/** |
|
2407 |
* Generates a random number. |
|
2408 |
* |
|
2409 |
* @since 2.6.2 |
|
2410 |
* @since 4.4.0 Uses PHP7 random_int() or the random_compat library if available. |
|
2411 |
* |
|
2412 |
* @global string $rnd_value |
|
2413 |
* @staticvar string $seed |
|
2414 |
* @staticvar bool $use_random_int_functionality |
|
2415 |
* |
|
2416 |
* @param int $min Lower limit for the generated number |
|
2417 |
* @param int $max Upper limit for the generated number |
|
2418 |
* @return int A random number between min and max |
|
2419 |
*/ |
|
2420 |
function wp_rand( $min = 0, $max = 0 ) { |
|
2421 |
global $rnd_value; |
|
2422 |
||
2423 |
// Some misconfigured 32bit environments (Entropy PHP, for example) truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats. |
|
2424 |
$max_random_number = 3000000000 === 2147483647 ? (float) '4294967295' : 4294967295; // 4294967295 = 0xffffffff |
|
2425 |
||
2426 |
// We only handle Ints, floats are truncated to their integer value. |
|
2427 |
$min = (int) $min; |
|
2428 |
$max = (int) $max; |
|
2429 |
||
2430 |
// Use PHP's CSPRNG, or a compatible method |
|
2431 |
static $use_random_int_functionality = true; |
|
2432 |
if ( $use_random_int_functionality ) { |
|
2433 |
try { |
|
2434 |
$_max = ( 0 != $max ) ? $max : $max_random_number; |
|
2435 |
// wp_rand() can accept arguments in either order, PHP cannot. |
|
2436 |
$_max = max( $min, $_max ); |
|
2437 |
$_min = min( $min, $_max ); |
|
2438 |
$val = random_int( $_min, $_max ); |
|
2439 |
if ( false !== $val ) { |
|
2440 |
return absint( $val ); |
|
2441 |
} else { |
|
2442 |
$use_random_int_functionality = false; |
|
2443 |
} |
|
2444 |
} catch ( Error $e ) { |
|
2445 |
$use_random_int_functionality = false; |
|
2446 |
} catch ( Exception $e ) { |
|
2447 |
$use_random_int_functionality = false; |
|
2448 |
} |
|
2449 |
} |
|
2450 |
||
2451 |
// Reset $rnd_value after 14 uses |
|
2452 |
// 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value |
|
2453 |
if ( strlen( $rnd_value ) < 8 ) { |
|
2454 |
if ( defined( 'WP_SETUP_CONFIG' ) ) { |
|
2455 |
static $seed = ''; |
|
2456 |
} else { |
|
2457 |
$seed = get_transient( 'random_seed' ); |
|
2458 |
} |
|
2459 |
$rnd_value = md5( uniqid( microtime() . mt_rand(), true ) . $seed ); |
|
2460 |
$rnd_value .= sha1( $rnd_value ); |
|
2461 |
$rnd_value .= sha1( $rnd_value . $seed ); |
|
2462 |
$seed = md5( $seed . $rnd_value ); |
|
2463 |
if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) { |
|
2464 |
set_transient( 'random_seed', $seed ); |
|
2465 |
} |
|
2466 |
} |
|
2467 |
||
2468 |
// Take the first 8 digits for our value |
|
2469 |
$value = substr( $rnd_value, 0, 8 ); |
|
2470 |
||
2471 |
// Strip the first eight, leaving the remainder for the next call to wp_rand(). |
|
2472 |
$rnd_value = substr( $rnd_value, 8 ); |
|
2473 |
||
2474 |
$value = abs( hexdec( $value ) ); |
|
2475 |
||
2476 |
// Reduce the value to be within the min - max range |
|
2477 |
if ( $max != 0 ) { |
|
2478 |
$value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 ); |
|
2479 |
} |
|
2480 |
||
2481 |
return abs( intval( $value ) ); |
|
2482 |
} |
|
2483 |
endif; |
|
2484 |
||
2485 |
if ( ! function_exists( 'wp_set_password' ) ) : |
|
2486 |
/** |
|
2487 |
* Updates the user's password with a new encrypted one. |
|
2488 |
* |
|
2489 |
* For integration with other applications, this function can be overwritten to |
|
2490 |
* instead use the other package password checking algorithm. |
|
2491 |
* |
|
2492 |
* Please note: This function should be used sparingly and is really only meant for single-time |
|
2493 |
* application. Leveraging this improperly in a plugin or theme could result in an endless loop |
|
2494 |
* of password resets if precautions are not taken to ensure it does not execute on every page load. |
|
2495 |
* |
|
2496 |
* @since 2.5.0 |
|
2497 |
* |
|
2498 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
2499 |
* |
|
2500 |
* @param string $password The plaintext new user password |
|
2501 |
* @param int $user_id User ID |
|
2502 |
*/ |
|
2503 |
function wp_set_password( $password, $user_id ) { |
|
2504 |
global $wpdb; |
|
2505 |
||
2506 |
$hash = wp_hash_password( $password ); |
|
2507 |
$wpdb->update( |
|
2508 |
$wpdb->users, |
|
2509 |
array( |
|
2510 |
'user_pass' => $hash, |
|
2511 |
'user_activation_key' => '', |
|
2512 |
), |
|
2513 |
array( 'ID' => $user_id ) |
|
2514 |
); |
|
0 | 2515 |
|
9 | 2516 |
wp_cache_delete( $user_id, 'users' ); |
5 | 2517 |
} |
9 | 2518 |
endif; |
0 | 2519 |
|
9 | 2520 |
if ( ! function_exists( 'get_avatar' ) ) : |
2521 |
/** |
|
2522 |
* Retrieve the avatar `<img>` tag for a user, email address, MD5 hash, comment, or post. |
|
2523 |
* |
|
2524 |
* @since 2.5.0 |
|
2525 |
* @since 4.2.0 Optional `$args` parameter added. |
|
2526 |
* |
|
2527 |
* @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, |
|
2528 |
* user email, WP_User object, WP_Post object, or WP_Comment object. |
|
2529 |
* @param int $size Optional. Height and width of the avatar image file in pixels. Default 96. |
|
2530 |
* @param string $default Optional. URL for the default image or a default type. Accepts '404' |
|
2531 |
* (return a 404 instead of a default image), 'retro' (8bit), 'monsterid' |
|
2532 |
* (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"), |
|
2533 |
* 'mystery', 'mm', or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), |
|
2534 |
* or 'gravatar_default' (the Gravatar logo). Default is the value of the |
|
2535 |
* 'avatar_default' option, with a fallback of 'mystery'. |
|
2536 |
* @param string $alt Optional. Alternative text to use in <img> tag. Default empty. |
|
2537 |
* @param array $args { |
|
2538 |
* Optional. Extra arguments to retrieve the avatar. |
|
2539 |
* |
|
2540 |
* @type int $height Display height of the avatar in pixels. Defaults to $size. |
|
2541 |
* @type int $width Display width of the avatar in pixels. Defaults to $size. |
|
2542 |
* @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. |
|
2543 |
* @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are |
|
2544 |
* judged in that order. Default is the value of the 'avatar_rating' option. |
|
2545 |
* @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. |
|
2546 |
* Default null. |
|
2547 |
* @type array|string $class Array or string of additional classes to add to the <img> element. |
|
2548 |
* Default null. |
|
2549 |
* @type bool $force_display Whether to always show the avatar - ignores the show_avatars option. |
|
2550 |
* Default false. |
|
2551 |
* @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty. |
|
2552 |
* } |
|
2553 |
* @return false|string `<img>` tag for the user's avatar. False on failure. |
|
2554 |
*/ |
|
2555 |
function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) { |
|
2556 |
$defaults = array( |
|
2557 |
// get_avatar_data() args. |
|
2558 |
'size' => 96, |
|
2559 |
'height' => null, |
|
2560 |
'width' => null, |
|
2561 |
'default' => get_option( 'avatar_default', 'mystery' ), |
|
2562 |
'force_default' => false, |
|
2563 |
'rating' => get_option( 'avatar_rating' ), |
|
2564 |
'scheme' => null, |
|
2565 |
'alt' => '', |
|
2566 |
'class' => null, |
|
2567 |
'force_display' => false, |
|
2568 |
'extra_attr' => '', |
|
2569 |
); |
|
5 | 2570 |
|
9 | 2571 |
if ( empty( $args ) ) { |
2572 |
$args = array(); |
|
2573 |
} |
|
2574 |
||
2575 |
$args['size'] = (int) $size; |
|
2576 |
$args['default'] = $default; |
|
2577 |
$args['alt'] = $alt; |
|
2578 |
||
2579 |
$args = wp_parse_args( $args, $defaults ); |
|
2580 |
||
2581 |
if ( empty( $args['height'] ) ) { |
|
2582 |
$args['height'] = $args['size']; |
|
2583 |
} |
|
2584 |
if ( empty( $args['width'] ) ) { |
|
2585 |
$args['width'] = $args['size']; |
|
2586 |
} |
|
2587 |
||
2588 |
if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { |
|
2589 |
$id_or_email = get_comment( $id_or_email ); |
|
2590 |
} |
|
0 | 2591 |
|
9 | 2592 |
/** |
2593 |
* Filters whether to retrieve the avatar URL early. |
|
2594 |
* |
|
2595 |
* Passing a non-null value will effectively short-circuit get_avatar(), passing |
|
2596 |
* the value through the {@see 'get_avatar'} filter and returning early. |
|
2597 |
* |
|
2598 |
* @since 4.2.0 |
|
2599 |
* |
|
2600 |
* @param string $avatar HTML for the user's avatar. Default null. |
|
2601 |
* @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, |
|
2602 |
* user email, WP_User object, WP_Post object, or WP_Comment object. |
|
2603 |
* @param array $args Arguments passed to get_avatar_url(), after processing. |
|
2604 |
*/ |
|
2605 |
$avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args ); |
|
2606 |
||
2607 |
if ( ! is_null( $avatar ) ) { |
|
2608 |
/** This filter is documented in wp-includes/pluggable.php */ |
|
2609 |
return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args ); |
|
2610 |
} |
|
2611 |
||
2612 |
if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) { |
|
2613 |
return false; |
|
2614 |
} |
|
2615 |
||
2616 |
$url2x = get_avatar_url( $id_or_email, array_merge( $args, array( 'size' => $args['size'] * 2 ) ) ); |
|
2617 |
||
2618 |
$args = get_avatar_data( $id_or_email, $args ); |
|
2619 |
||
2620 |
$url = $args['url']; |
|
2621 |
||
2622 |
if ( ! $url || is_wp_error( $url ) ) { |
|
2623 |
return false; |
|
2624 |
} |
|
2625 |
||
2626 |
$class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2627 |
|
9 | 2628 |
if ( ! $args['found_avatar'] || $args['force_default'] ) { |
2629 |
$class[] = 'avatar-default'; |
|
2630 |
} |
|
2631 |
||
2632 |
if ( $args['class'] ) { |
|
2633 |
if ( is_array( $args['class'] ) ) { |
|
2634 |
$class = array_merge( $class, $args['class'] ); |
|
2635 |
} else { |
|
2636 |
$class[] = $args['class']; |
|
2637 |
} |
|
2638 |
} |
|
5 | 2639 |
|
9 | 2640 |
$avatar = sprintf( |
2641 |
"<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>", |
|
2642 |
esc_attr( $args['alt'] ), |
|
2643 |
esc_url( $url ), |
|
2644 |
esc_url( $url2x ) . ' 2x', |
|
2645 |
esc_attr( join( ' ', $class ) ), |
|
2646 |
(int) $args['height'], |
|
2647 |
(int) $args['width'], |
|
2648 |
$args['extra_attr'] |
|
2649 |
); |
|
2650 |
||
2651 |
/** |
|
2652 |
* Filters the avatar to retrieve. |
|
2653 |
* |
|
2654 |
* @since 2.5.0 |
|
2655 |
* @since 4.2.0 The `$args` parameter was added. |
|
2656 |
* |
|
2657 |
* @param string $avatar <img> tag for the user's avatar. |
|
2658 |
* @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, |
|
2659 |
* user email, WP_User object, WP_Post object, or WP_Comment object. |
|
2660 |
* @param int $size Square avatar width and height in pixels to retrieve. |
|
2661 |
* @param string $default URL for the default image or a default type. Accepts '404', 'retro', 'monsterid', |
|
2662 |
* 'wavatar', 'indenticon','mystery' (or 'mm', or 'mysteryman'), 'blank', or 'gravatar_default'. |
|
2663 |
* Default is the value of the 'avatar_default' option, with a fallback of 'mystery'. |
|
2664 |
* @param string $alt Alternative text to use in the avatar image tag. Default empty. |
|
2665 |
* @param array $args Arguments passed to get_avatar_data(), after processing. |
|
2666 |
*/ |
|
5 | 2667 |
return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args ); |
0 | 2668 |
} |
2669 |
endif; |
|
2670 |
||
9 | 2671 |
if ( ! function_exists( 'wp_text_diff' ) ) : |
2672 |
/** |
|
2673 |
* Displays a human readable HTML representation of the difference between two strings. |
|
2674 |
* |
|
2675 |
* The Diff is available for getting the changes between versions. The output is |
|
2676 |
* HTML, so the primary use is for displaying the changes. If the two strings |
|
2677 |
* are equivalent, then an empty string will be returned. |
|
2678 |
* |
|
2679 |
* The arguments supported and can be changed are listed below. |
|
2680 |
* |
|
2681 |
* 'title' : Default is an empty string. Titles the diff in a manner compatible |
|
2682 |
* with the output. |
|
2683 |
* 'title_left' : Default is an empty string. Change the HTML to the left of the |
|
2684 |
* title. |
|
2685 |
* 'title_right' : Default is an empty string. Change the HTML to the right of |
|
2686 |
* the title. |
|
2687 |
* |
|
2688 |
* @since 2.6.0 |
|
2689 |
* |
|
2690 |
* @see wp_parse_args() Used to change defaults to user defined settings. |
|
2691 |
* @uses Text_Diff |
|
2692 |
* @uses WP_Text_Diff_Renderer_Table |
|
2693 |
* |
|
2694 |
* @param string $left_string "old" (left) version of string |
|
2695 |
* @param string $right_string "new" (right) version of string |
|
2696 |
* @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults. |
|
2697 |
* @return string Empty string if strings are equivalent or HTML with differences. |
|
2698 |
*/ |
|
2699 |
function wp_text_diff( $left_string, $right_string, $args = null ) { |
|
2700 |
$defaults = array( |
|
2701 |
'title' => '', |
|
2702 |
'title_left' => '', |
|
2703 |
'title_right' => '', |
|
2704 |
); |
|
2705 |
$args = wp_parse_args( $args, $defaults ); |
|
0 | 2706 |
|
9 | 2707 |
if ( ! class_exists( 'WP_Text_Diff_Renderer_Table', false ) ) { |
2708 |
require( ABSPATH . WPINC . '/wp-diff.php' ); |
|
2709 |
} |
|
0 | 2710 |
|
9 | 2711 |
$left_string = normalize_whitespace( $left_string ); |
2712 |
$right_string = normalize_whitespace( $right_string ); |
|
2713 |
||
2714 |
$left_lines = explode( "\n", $left_string ); |
|
2715 |
$right_lines = explode( "\n", $right_string ); |
|
2716 |
$text_diff = new Text_Diff( $left_lines, $right_lines ); |
|
2717 |
$renderer = new WP_Text_Diff_Renderer_Table( $args ); |
|
2718 |
$diff = $renderer->render( $text_diff ); |
|
0 | 2719 |
|
9 | 2720 |
if ( ! $diff ) { |
2721 |
return ''; |
|
2722 |
} |
|
2723 |
||
2724 |
$r = "<table class='diff'>\n"; |
|
2725 |
||
2726 |
if ( ! empty( $args['show_split_view'] ) ) { |
|
2727 |
$r .= "<col class='content diffsplit left' /><col class='content diffsplit middle' /><col class='content diffsplit right' />"; |
|
2728 |
} else { |
|
2729 |
$r .= "<col class='content' />"; |
|
2730 |
} |
|
0 | 2731 |
|
9 | 2732 |
if ( $args['title'] || $args['title_left'] || $args['title_right'] ) { |
2733 |
$r .= '<thead>'; |
|
2734 |
} |
|
2735 |
if ( $args['title'] ) { |
|
2736 |
$r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n"; |
|
2737 |
} |
|
2738 |
if ( $args['title_left'] || $args['title_right'] ) { |
|
2739 |
$r .= "<tr class='diff-sub-title'>\n"; |
|
2740 |
$r .= "\t<td></td><th>$args[title_left]</th>\n"; |
|
2741 |
$r .= "\t<td></td><th>$args[title_right]</th>\n"; |
|
2742 |
$r .= "</tr>\n"; |
|
2743 |
} |
|
2744 |
if ( $args['title'] || $args['title_left'] || $args['title_right'] ) { |
|
2745 |
$r .= "</thead>\n"; |
|
2746 |
} |
|
0 | 2747 |
|
9 | 2748 |
$r .= "<tbody>\n$diff\n</tbody>\n"; |
2749 |
$r .= '</table>'; |
|
2750 |
||
2751 |
return $r; |
|
0 | 2752 |
} |
2753 |
endif; |
|
2754 |