| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 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 * 21 * @global WP_User $current_user The current user object which holds the user data. 22 * 23 * @param int|null $id User ID. 24 * @param string $name User's username. 25 * @return WP_User Current user User object. 26 */ 27 function wp_set_current_user( $id, $name = '' ) { 28 global $current_user; 29 30 // If `$id` matches the current user, there is nothing to do. 31 if ( isset( $current_user ) 32 && ( $current_user instanceof WP_User ) 33 && ( (int) $id === $current_user->ID ) 34 && ( null !== $id ) 35 ) { 36 return $current_user; 37 } 38 39 $current_user = new WP_User( $id, $name ); 40 41 setup_userdata( $current_user->ID ); 42 43 /** 44 * Fires after the current user is set. 45 * 46 * @since 2.0.1 47 */ 48 do_action( 'set_current_user' ); 49 50 return $current_user; 51 } 52 endif; 53 54 if ( ! function_exists( 'wp_get_current_user' ) ) : 55 /** 56 * Retrieves the current user object. 57 * 58 * Will set the current user, if the current user is not set. The current user 59 * will be set to the logged-in person. If no user is logged-in, then it will 60 * set the current user to 0, which is invalid and won't have any permissions. 61 * 62 * @since 2.0.3 63 * 64 * @see _wp_get_current_user() 65 * @global WP_User $current_user Checks if the current user is set. 66 * 67 * @return WP_User Current WP_User instance. 68 */ 69 function wp_get_current_user() { 70 return _wp_get_current_user(); 71 } 72 endif; 73 74 if ( ! function_exists( 'get_userdata' ) ) : 75 /** 76 * Retrieves user info by user ID. 77 * 78 * @since 0.71 79 * 80 * @param int $user_id User ID 81 * @return WP_User|false WP_User object on success, false on failure. 82 */ 83 function get_userdata( $user_id ) { 84 return get_user_by( 'id', $user_id ); 85 } 86 endif; 87 88 if ( ! function_exists( 'get_user_by' ) ) : 89 /** 90 * Retrieves user info by a given field. 91 * 92 * @since 2.8.0 93 * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. 94 * 95 * @global WP_User $current_user The current user object which holds the user data. 96 * 97 * @param string $field The field to retrieve the user with. id | ID | slug | email | login. 98 * @param int|string $value A value for $field. A user ID, slug, email address, or login name. 99 * @return WP_User|false WP_User object on success, false on failure. 100 */ 101 function get_user_by( $field, $value ) { 102 $userdata = WP_User::get_data_by( $field, $value ); 103 104 if ( ! $userdata ) { 105 return false; 106 } 107 108 $user = new WP_User(); 109 $user->init( $userdata ); 110 111 return $user; 112 } 113 endif; 114 115 if ( ! function_exists( 'cache_users' ) ) : 116 /** 117 * Retrieves info for user lists to prevent multiple queries by get_userdata(). 118 * 119 * @since 3.0.0 120 * 121 * @global wpdb $wpdb WordPress database abstraction object. 122 * 123 * @param int[] $user_ids User ID numbers list 124 */ 125 function cache_users( $user_ids ) { 126 global $wpdb; 127 128 update_meta_cache( 'user', $user_ids ); 129 130 $clean = _get_non_cached_ids( $user_ids, 'users' ); 131 132 if ( empty( $clean ) ) { 133 return; 134 } 135 136 $list = implode( ',', $clean ); 137 138 $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" ); 139 140 foreach ( $users as $user ) { 141 update_user_caches( $user ); 142 } 143 } 144 endif; 145 146 if ( ! function_exists( 'wp_mail' ) ) : 147 /** 148 * Sends an email, similar to PHP's mail function. 149 * 150 * A true return value does not automatically mean that the user received the 151 * email successfully. It just only means that the method used was able to 152 * process the request without any errors. 153 * 154 * The default content type is `text/plain` which does not allow using HTML. 155 * However, you can set the content type of the email by using the 156 * {@see 'wp_mail_content_type'} filter. 157 * 158 * The default charset is based on the charset used on the blog. The charset can 159 * be set using the {@see 'wp_mail_charset'} filter. 160 * 161 * When using the `$embeds` parameter to embed images for use in HTML emails, 162 * reference the embedded file in your HTML with a `cid:` URL whose value 163 * matches the file's Content-ID. By default, the Content-ID (`cid`) used for 164 * each embedded file is the key in the embeds array, unless modified via the 165 * {@see 'wp_mail_embed_args'} filter. For example: 166 * 167 * `<img src="cid:0" alt="Logo">` 168 * `<img src="cid:my-image" alt="Image">` 169 * 170 * You may also customize the Content-ID for each file by using the 171 * {@see 'wp_mail_embed_args'} filter and setting the `cid` value. 172 * 173 * @since 1.2.1 174 * @since 5.5.0 is_email() is used for email validation, 175 * instead of PHPMailer's default validator. 176 * @since 6.9.0 The `$embeds` parameter was added. 177 * @since 6.9.0 Improved Content-Type header handling for multipart messages. 178 * 179 * @global PHPMailer\PHPMailer\PHPMailer $phpmailer 180 * 181 * @param string|string[] $to Array or comma-separated list of email addresses to send message. 182 * @param string $subject Email subject. 183 * @param string $message Message contents. 184 * @param string|string[] $headers Optional. Additional headers. 185 * @param string|string[] $attachments Optional. Paths to files to attach. 186 * @param string|string[] $embeds Optional. Paths to files to embed. 187 * @return bool Whether the email was sent successfully. 188 */ 189 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array(), $embeds = array() ) { 190 // Compact the input, apply the filters, and extract them back out. 191 192 /** 193 * Filters the wp_mail() arguments. 194 * 195 * @since 2.2.0 196 * @since 6.9.0 The `$embeds` element was added to the `$args` array. 197 * 198 * @param array $args { 199 * Array of the `wp_mail()` arguments. 200 * 201 * @type string|string[] $to Array or comma-separated list of email addresses to send message. 202 * @type string $subject Email subject. 203 * @type string $message Message contents. 204 * @type string|string[] $headers Additional headers. 205 * @type string|string[] $attachments Paths to files to attach. 206 * @type string|string[] $embeds Paths to files to embed. 207 * } 208 */ 209 $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments', 'embeds' ) ); 210 211 /** 212 * Filters whether to preempt sending an email. 213 * 214 * Returning a non-null value will short-circuit {@see wp_mail()}, returning 215 * that value instead. A boolean return value should be used to indicate whether 216 * the email was successfully sent. 217 * 218 * @since 5.7.0 219 * @since 6.9.0 The `$embeds` element was added to the `$atts` array. 220 * 221 * @param null|bool $return Short-circuit return value. 222 * @param array $atts { 223 * Array of the `wp_mail()` arguments. 224 * 225 * @type string|string[] $to Array or comma-separated list of email addresses to send message. 226 * @type string $subject Email subject. 227 * @type string $message Message contents. 228 * @type string|string[] $headers Additional headers. 229 * @type string|string[] $attachments Paths to files to attach. 230 * @type string|string[] $embeds Paths to files to embed. 231 * } 232 */ 233 $pre_wp_mail = apply_filters( 'pre_wp_mail', null, $atts ); 234 235 if ( null !== $pre_wp_mail ) { 236 return $pre_wp_mail; 237 } 238 239 if ( isset( $atts['to'] ) ) { 240 $to = $atts['to']; 241 } 242 243 if ( ! is_array( $to ) ) { 244 $to = explode( ',', $to ); 245 } 246 247 if ( isset( $atts['subject'] ) ) { 248 $subject = $atts['subject']; 249 } 250 251 if ( isset( $atts['message'] ) ) { 252 $message = $atts['message']; 253 } 254 255 if ( isset( $atts['headers'] ) ) { 256 $headers = $atts['headers']; 257 } 258 259 if ( isset( $atts['attachments'] ) ) { 260 $attachments = $atts['attachments']; 261 } 262 263 if ( ! is_array( $attachments ) ) { 264 $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) ); 265 } 266 267 if ( isset( $atts['embeds'] ) ) { 268 $embeds = $atts['embeds']; 269 } 270 271 if ( ! is_array( $embeds ) ) { 272 $embeds = explode( "\n", str_replace( "\r\n", "\n", $embeds ) ); 273 } 274 275 global $phpmailer; 276 277 // (Re)create it, if it's gone missing. 278 if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) { 279 require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php'; 280 require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php'; 281 require_once ABSPATH . WPINC . '/PHPMailer/Exception.php'; 282 require_once ABSPATH . WPINC . '/class-wp-phpmailer.php'; 283 $phpmailer = new WP_PHPMailer( true ); 284 285 $phpmailer::$validator = static function ( $email ) { 286 return (bool) is_email( $email ); 287 }; 288 } 289 290 // Headers. 291 $cc = array(); 292 $bcc = array(); 293 $reply_to = array(); 294 295 if ( empty( $headers ) ) { 296 $headers = array(); 297 } else { 298 if ( ! is_array( $headers ) ) { 299 /* 300 * Explode the headers out, so this function can take 301 * both string headers and an array of headers. 302 */ 303 $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) ); 304 } else { 305 $tempheaders = $headers; 306 } 307 $headers = array(); 308 309 // If it's actually got contents. 310 if ( ! empty( $tempheaders ) ) { 311 // Iterate through the raw headers. 312 foreach ( (array) $tempheaders as $header ) { 313 if ( ! str_contains( $header, ':' ) ) { 314 if ( false !== stripos( $header, 'boundary=' ) ) { 315 $parts = preg_split( '/boundary=/i', trim( $header ) ); 316 $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) ); 317 } 318 continue; 319 } 320 // Explode them out. 321 list( $name, $content ) = explode( ':', trim( $header ), 2 ); 322 323 // Cleanup crew. 324 $name = trim( $name ); 325 $content = trim( $content ); 326 327 switch ( strtolower( $name ) ) { 328 // Mainly for legacy -- process a "From:" header if it's there. 329 case 'from': 330 $bracket_pos = strpos( $content, '<' ); 331 if ( false !== $bracket_pos ) { 332 // Text before the bracketed email is the "From" name. 333 if ( $bracket_pos > 0 ) { 334 $from_name = substr( $content, 0, $bracket_pos ); 335 $from_name = str_replace( '"', '', $from_name ); 336 $from_name = trim( $from_name ); 337 } 338 339 $from_email = substr( $content, $bracket_pos + 1 ); 340 $from_email = str_replace( '>', '', $from_email ); 341 $from_email = trim( $from_email ); 342 343 // Avoid setting an empty $from_email. 344 } elseif ( '' !== trim( $content ) ) { 345 $from_email = trim( $content ); 346 } 347 break; 348 case 'content-type': 349 if ( str_contains( $content, ';' ) ) { 350 list( $type, $charset_content ) = explode( ';', $content ); 351 $content_type = trim( $type ); 352 if ( false !== stripos( $charset_content, 'charset=' ) ) { 353 $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) ); 354 } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) { 355 $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) ); 356 $charset = ''; 357 if ( preg_match( '~^multipart/(\S+)~', $content_type, $matches ) ) { 358 $content_type = 'multipart/' . strtolower( $matches[1] ) . '; boundary="' . $boundary . '"'; 359 } 360 } 361 362 // Avoid setting an empty $content_type. 363 } elseif ( '' !== trim( $content ) ) { 364 $content_type = trim( $content ); 365 } 366 break; 367 case 'cc': 368 $cc = array_merge( (array) $cc, explode( ',', $content ) ); 369 break; 370 case 'bcc': 371 $bcc = array_merge( (array) $bcc, explode( ',', $content ) ); 372 break; 373 case 'reply-to': 374 $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) ); 375 break; 376 default: 377 // Add it to our grand headers array. 378 $headers[ trim( $name ) ] = trim( $content ); 379 break; 380 } 381 } 382 } 383 } 384 385 // Empty out the values that may be set. 386 $phpmailer->clearAllRecipients(); 387 $phpmailer->clearAttachments(); 388 $phpmailer->clearCustomHeaders(); 389 $phpmailer->clearReplyTos(); 390 $phpmailer->Body = ''; 391 $phpmailer->AltBody = ''; 392 393 /* 394 * Reset encoding to 8-bit, as it may have been automatically downgraded 395 * to 7-bit by PHPMailer (based on the body contents) in a previous call 396 * to wp_mail(). 397 * 398 * See https://core.trac.wordpress.org/ticket/33972 399 */ 400 $phpmailer->Encoding = PHPMailer\PHPMailer\PHPMailer::ENCODING_8BIT; 401 402 // Set "From" name and email. 403 404 // If we don't have a name from the input headers. 405 if ( ! isset( $from_name ) ) { 406 $from_name = 'WordPress'; 407 } 408 409 /* 410 * If we don't have an email from the input headers, default to wordpress@$sitename 411 * Some hosts will block outgoing mail from this address if it doesn't exist, 412 * but there's no easy alternative. Defaulting to admin_email might appear to be 413 * another option, but some hosts may refuse to relay mail from an unknown domain. 414 * See https://core.trac.wordpress.org/ticket/5007. 415 */ 416 if ( ! isset( $from_email ) ) { 417 // Get the site domain and get rid of www. 418 $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST ); 419 $from_email = 'wordpress@'; 420 421 if ( null !== $sitename ) { 422 if ( str_starts_with( $sitename, 'www.' ) ) { 423 $sitename = substr( $sitename, 4 ); 424 } 425 426 $from_email .= $sitename; 427 } 428 } 429 430 /** 431 * Filters the email address to send from. 432 * 433 * @since 2.2.0 434 * 435 * @param string $from_email Email address to send from. 436 */ 437 $from_email = apply_filters( 'wp_mail_from', $from_email ); 438 439 /** 440 * Filters the name to associate with the "from" email address. 441 * 442 * @since 2.3.0 443 * 444 * @param string $from_name Name associated with the "from" email address. 445 */ 446 $from_name = apply_filters( 'wp_mail_from_name', $from_name ); 447 448 try { 449 $phpmailer->setFrom( $from_email, $from_name, false ); 450 } catch ( PHPMailer\PHPMailer\Exception $e ) { 451 $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); 452 $mail_error_data['phpmailer_exception_code'] = $e->getCode(); 453 454 /** This filter is documented in wp-includes/pluggable.php */ 455 do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) ); 456 457 return false; 458 } 459 460 // Set mail's subject and body. 461 $phpmailer->Subject = $subject; 462 $phpmailer->Body = $message; 463 464 // Set destination addresses, using appropriate methods for handling addresses. 465 $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' ); 466 467 foreach ( $address_headers as $address_header => $addresses ) { 468 if ( empty( $addresses ) ) { 469 continue; 470 } 471 472 foreach ( (array) $addresses as $address ) { 473 try { 474 // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>". 475 $recipient_name = ''; 476 477 if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) { 478 if ( count( $matches ) === 3 ) { 479 $recipient_name = $matches[1]; 480 $address = $matches[2]; 481 } 482 } 483 484 switch ( $address_header ) { 485 case 'to': 486 $phpmailer->addAddress( $address, $recipient_name ); 487 break; 488 case 'cc': 489 $phpmailer->addCC( $address, $recipient_name ); 490 break; 491 case 'bcc': 492 $phpmailer->addBCC( $address, $recipient_name ); 493 break; 494 case 'reply_to': 495 $phpmailer->addReplyTo( $address, $recipient_name ); 496 break; 497 } 498 } catch ( PHPMailer\PHPMailer\Exception $e ) { 499 continue; 500 } 501 } 502 } 503 504 // Set to use PHP's mail(). 505 $phpmailer->isMail(); 506 507 // Set Content-Type and charset. 508 509 // If we don't have a Content-Type from the input headers. 510 if ( ! isset( $content_type ) ) { 511 $content_type = 'text/plain'; 512 } 513 514 /** 515 * Filters the wp_mail() content type. 516 * 517 * @since 2.3.0 518 * 519 * @param string $content_type Default wp_mail() content type. 520 */ 521 $content_type = apply_filters( 'wp_mail_content_type', $content_type ); 522 523 $phpmailer->ContentType = $content_type; 524 525 // Set whether it's plaintext, depending on $content_type. 526 if ( 'text/html' === $content_type ) { 527 $phpmailer->isHTML( true ); 528 } 529 530 // If we don't have a charset from the input headers. 531 if ( ! isset( $charset ) ) { 532 $charset = get_bloginfo( 'charset' ); 533 } 534 535 /** 536 * Filters the default wp_mail() charset. 537 * 538 * @since 2.3.0 539 * 540 * @param string $charset Default email charset. 541 */ 542 $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset ); 543 544 // Set custom headers. 545 if ( ! empty( $headers ) ) { 546 foreach ( (array) $headers as $name => $content ) { 547 // Only add custom headers not added automatically by PHPMailer. 548 if ( ! in_array( $name, array( 'MIME-Version', 'X-Mailer' ), true ) ) { 549 try { 550 $phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); 551 } catch ( PHPMailer\PHPMailer\Exception $e ) { 552 continue; 553 } 554 } 555 } 556 } 557 558 if ( ! empty( $attachments ) ) { 559 foreach ( $attachments as $filename => $attachment ) { 560 $filename = is_string( $filename ) ? $filename : ''; 561 562 try { 563 $phpmailer->addAttachment( $attachment, $filename ); 564 } catch ( PHPMailer\PHPMailer\Exception $e ) { 565 continue; 566 } 567 } 568 } 569 570 if ( ! empty( $embeds ) ) { 571 foreach ( $embeds as $key => $embed_path ) { 572 /** 573 * Filters the arguments for PHPMailer's addEmbeddedImage() method. 574 * 575 * @since 6.9.0 576 * 577 * @param array $args { 578 * An array of arguments for PHPMailer's addEmbeddedImage() method. 579 * 580 * @type string $path The path to the file. 581 * @type string $cid The Content-ID of the image. Default: The key in the embeds array. 582 * @type string $name The filename of the image. 583 * @type string $encoding The encoding of the image. Default: 'base64'. 584 * @type string $type The MIME type of the image. Default: empty string, which lets PHPMailer auto-detect. 585 * @type string $disposition The disposition of the image. Default: 'inline'. 586 * } 587 */ 588 $embed_args = apply_filters( 589 'wp_mail_embed_args', 590 array( 591 'path' => $embed_path, 592 'cid' => (string) $key, 593 'name' => basename( $embed_path ), 594 'encoding' => 'base64', 595 'type' => '', 596 'disposition' => 'inline', 597 ) 598 ); 599 600 try { 601 $phpmailer->addEmbeddedImage( 602 $embed_args['path'], 603 $embed_args['cid'], 604 $embed_args['name'], 605 $embed_args['encoding'], 606 $embed_args['type'], 607 $embed_args['disposition'] 608 ); 609 } catch ( PHPMailer\PHPMailer\Exception $e ) { 610 continue; 611 } 612 } 613 } 614 615 /** 616 * Fires after PHPMailer is initialized. 617 * 618 * @since 2.2.0 619 * 620 * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference). 621 */ 622 do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) ); 623 624 $mail_data = compact( 'to', 'subject', 'message', 'headers', 'attachments', 'embeds' ); 625 626 // Send! 627 try { 628 $send = $phpmailer->send(); 629 630 /** 631 * Fires after PHPMailer has successfully sent an email. 632 * 633 * The firing of this action does not necessarily mean that the recipient(s) received the 634 * email successfully. It only means that the `send` method above was able to 635 * process the request without any errors. 636 * 637 * @since 5.9.0 638 * @since 6.9.0 The `$embeds` element was added to the `$mail_data` array. 639 * 640 * @param array $mail_data { 641 * An array containing the email recipient(s), subject, message, headers, attachments, and embeds. 642 * 643 * @type string[] $to Email addresses to send message. 644 * @type string $subject Email subject. 645 * @type string $message Message contents. 646 * @type string[] $headers Additional headers. 647 * @type string[] $attachments Paths to files to attach. 648 * @type string[] $embeds Paths to files to embed. 649 * } 650 */ 651 do_action( 'wp_mail_succeeded', $mail_data ); 652 653 return $send; 654 } catch ( PHPMailer\PHPMailer\Exception $e ) { 655 $mail_data['phpmailer_exception_code'] = $e->getCode(); 656 657 /** 658 * Fires after a PHPMailer exception is caught. 659 * 660 * @since 4.4.0 661 * 662 * @param WP_Error $error A WP_Error object with the PHPMailer\PHPMailer\Exception message, and an array 663 * containing the mail recipient, subject, message, headers, attachments, and embeds. 664 */ 665 do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_data ) ); 666 667 return false; 668 } 669 } 670 endif; 671 672 if ( ! function_exists( 'wp_authenticate' ) ) : 673 /** 674 * Authenticates a user, confirming the login credentials are valid. 675 * 676 * @since 2.5.0 677 * @since 4.5.0 `$username` now accepts an email address. 678 * 679 * @param string $username User's username or email address. 680 * @param string $password User's password. 681 * @return WP_User|WP_Error WP_User object if the credentials are valid, 682 * otherwise WP_Error. 683 */ 684 function wp_authenticate( 685 $username, 686 #[\SensitiveParameter] 687 $password 688 ) { 689 $username = sanitize_user( $username ); 690 $password = trim( $password ); 691 692 /** 693 * Filters whether a set of user login credentials are valid. 694 * 695 * A WP_User object is returned if the credentials authenticate a user. 696 * WP_Error or null otherwise. 697 * 698 * @since 2.8.0 699 * @since 4.5.0 `$username` now accepts an email address. 700 * 701 * @param null|WP_User|WP_Error $user WP_User if the user is authenticated. 702 * WP_Error or null otherwise. 703 * @param string $username Username or email address. 704 * @param string $password User password. 705 */ 706 $user = apply_filters( 'authenticate', null, $username, $password ); 707 708 if ( null === $user || false === $user ) { 709 /* 710 * TODO: What should the error message be? (Or would these even happen?) 711 * Only needed if all authentication handlers fail to return anything. 712 */ 713 $user = new WP_Error( 'authentication_failed', __( '<strong>Error:</strong> Invalid username, email address or incorrect password.' ) ); 714 } 715 716 $ignore_codes = array( 'empty_username', 'empty_password' ); 717 718 if ( is_wp_error( $user ) && ! in_array( $user->get_error_code(), $ignore_codes, true ) ) { 719 $error = $user; 720 721 /** 722 * Fires after a user login has failed. 723 * 724 * @since 2.5.0 725 * @since 4.5.0 The value of `$username` can now be an email address. 726 * @since 5.4.0 The `$error` parameter was added. 727 * 728 * @param string $username Username or email address. 729 * @param WP_Error $error A WP_Error object with the authentication failure details. 730 */ 731 do_action( 'wp_login_failed', $username, $error ); 732 } 733 734 return $user; 735 } 736 endif; 737 738 if ( ! function_exists( 'wp_logout' ) ) : 739 /** 740 * Logs the current user out. 741 * 742 * @since 2.5.0 743 */ 744 function wp_logout() { 745 $user_id = get_current_user_id(); 746 747 wp_destroy_current_session(); 748 wp_clear_auth_cookie(); 749 wp_set_current_user( 0 ); 750 751 /** 752 * Fires after a user is logged out. 753 * 754 * @since 1.5.0 755 * @since 5.5.0 Added the `$user_id` parameter. 756 * 757 * @param int $user_id ID of the user that was logged out. 758 */ 759 do_action( 'wp_logout', $user_id ); 760 } 761 endif; 762 763 if ( ! function_exists( 'wp_validate_auth_cookie' ) ) : 764 /** 765 * Validates authentication cookie. 766 * 767 * The checks include making sure that the authentication cookie is set and 768 * pulling in the contents (if $cookie is not used). 769 * 770 * Makes sure the cookie is not expired. Verifies the hash in cookie is what is 771 * should be and compares the two. 772 * 773 * @since 2.5.0 774 * 775 * @global int $login_grace_period 776 * 777 * @param string $cookie Optional. If used, will validate contents instead of cookie's. 778 * @param string $scheme Optional. The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'. 779 * Note: This does *not* default to 'auth' like other cookie functions. 780 * @return int|false User ID if valid cookie, false if invalid. 781 */ 782 function wp_validate_auth_cookie( $cookie = '', $scheme = '' ) { 783 $cookie_elements = wp_parse_auth_cookie( $cookie, $scheme ); 784 if ( ! $cookie_elements ) { 785 /** 786 * Fires if an authentication cookie is malformed. 787 * 788 * @since 2.7.0 789 * 790 * @param string $cookie Malformed auth cookie. 791 * @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth', 792 * or 'logged_in'. 793 */ 794 do_action( 'auth_cookie_malformed', $cookie, $scheme ); 795 return false; 796 } 797 798 $scheme = $cookie_elements['scheme']; 799 $username = $cookie_elements['username']; 800 $hmac = $cookie_elements['hmac']; 801 $token = $cookie_elements['token']; 802 $expiration = $cookie_elements['expiration']; 803 804 $expired = (int) $expiration; 805 806 // Allow a grace period for POST and Ajax requests. 807 if ( wp_doing_ajax() || 'POST' === $_SERVER['REQUEST_METHOD'] ) { 808 $expired += HOUR_IN_SECONDS; 809 } 810 811 // Quick check to see if an honest cookie has expired. 812 if ( $expired < time() ) { 813 /** 814 * Fires once an authentication cookie has expired. 815 * 816 * @since 2.7.0 817 * 818 * @param string[] $cookie_elements { 819 * Authentication cookie components. None of the components should be assumed 820 * to be valid as they come directly from a client-provided cookie value. 821 * 822 * @type string $username User's username. 823 * @type string $expiration The time the cookie expires as a UNIX timestamp. 824 * @type string $token User's session token used. 825 * @type string $hmac The security hash for the cookie. 826 * @type string $scheme The cookie scheme to use. 827 * } 828 */ 829 do_action( 'auth_cookie_expired', $cookie_elements ); 830 return false; 831 } 832 833 $user = get_user_by( 'login', $username ); 834 if ( ! $user ) { 835 /** 836 * Fires if a bad username is entered in the user authentication process. 837 * 838 * @since 2.7.0 839 * 840 * @param string[] $cookie_elements { 841 * Authentication cookie components. None of the components should be assumed 842 * to be valid as they come directly from a client-provided cookie value. 843 * 844 * @type string $username User's username. 845 * @type string $expiration The time the cookie expires as a UNIX timestamp. 846 * @type string $token User's session token used. 847 * @type string $hmac The security hash for the cookie. 848 * @type string $scheme The cookie scheme to use. 849 * } 850 */ 851 do_action( 'auth_cookie_bad_username', $cookie_elements ); 852 return false; 853 } 854 855 if ( str_starts_with( $user->user_pass, '$P$' ) || str_starts_with( $user->user_pass, '$2y$' ) ) { 856 // Retain previous behaviour of phpass or vanilla bcrypt hashed passwords. 857 $pass_frag = substr( $user->user_pass, 8, 4 ); 858 } else { 859 // Otherwise, use a substring from the end of the hash to avoid dealing with potentially long hash prefixes. 860 $pass_frag = substr( $user->user_pass, -4 ); 861 } 862 863 $key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); 864 865 $hash = hash_hmac( 'sha256', $username . '|' . $expiration . '|' . $token, $key ); 866 867 if ( ! hash_equals( $hash, $hmac ) ) { 868 /** 869 * Fires if a bad authentication cookie hash is encountered. 870 * 871 * @since 2.7.0 872 * 873 * @param string[] $cookie_elements { 874 * Authentication cookie components. None of the components should be assumed 875 * to be valid as they come directly from a client-provided cookie value. 876 * 877 * @type string $username User's username. 878 * @type string $expiration The time the cookie expires as a UNIX timestamp. 879 * @type string $token User's session token used. 880 * @type string $hmac The security hash for the cookie. 881 * @type string $scheme The cookie scheme to use. 882 * } 883 */ 884 do_action( 'auth_cookie_bad_hash', $cookie_elements ); 885 return false; 886 } 887 888 $manager = WP_Session_Tokens::get_instance( $user->ID ); 889 if ( ! $manager->verify( $token ) ) { 890 /** 891 * Fires if a bad session token is encountered. 892 * 893 * @since 4.0.0 894 * 895 * @param string[] $cookie_elements { 896 * Authentication cookie components. None of the components should be assumed 897 * to be valid as they come directly from a client-provided cookie value. 898 * 899 * @type string $username User's username. 900 * @type string $expiration The time the cookie expires as a UNIX timestamp. 901 * @type string $token User's session token used. 902 * @type string $hmac The security hash for the cookie. 903 * @type string $scheme The cookie scheme to use. 904 * } 905 */ 906 do_action( 'auth_cookie_bad_session_token', $cookie_elements ); 907 return false; 908 } 909 910 // Ajax/POST grace period set above. 911 if ( $expiration < time() ) { 912 $GLOBALS['login_grace_period'] = 1; 913 } 914 915 /** 916 * Fires once an authentication cookie has been validated. 917 * 918 * @since 2.7.0 919 * 920 * @param string[] $cookie_elements { 921 * Authentication cookie components. 922 * 923 * @type string $username User's username. 924 * @type string $expiration The time the cookie expires as a UNIX timestamp. 925 * @type string $token User's session token used. 926 * @type string $hmac The security hash for the cookie. 927 * @type string $scheme The cookie scheme to use. 928 * } 929 * @param WP_User $user User object. 930 */ 931 do_action( 'auth_cookie_valid', $cookie_elements, $user ); 932 933 return $user->ID; 934 } 935 endif; 936 937 if ( ! function_exists( 'wp_generate_auth_cookie' ) ) : 938 /** 939 * Generates authentication cookie contents. 940 * 941 * @since 2.5.0 942 * @since 4.0.0 The `$token` parameter was added. 943 * 944 * @param int $user_id User ID. 945 * @param int $expiration The time the cookie expires as a UNIX timestamp. 946 * @param string $scheme Optional. The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'. 947 * Default 'auth'. 948 * @param string $token User's session token to use for this cookie. 949 * @return string Authentication cookie contents. Empty string if user does not exist. 950 */ 951 function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) { 952 $user = get_userdata( $user_id ); 953 if ( ! $user ) { 954 return ''; 955 } 956 957 if ( ! $token ) { 958 $manager = WP_Session_Tokens::get_instance( $user_id ); 959 $token = $manager->create( $expiration ); 960 } 961 962 if ( str_starts_with( $user->user_pass, '$P$' ) || str_starts_with( $user->user_pass, '$2y$' ) ) { 963 // Retain previous behaviour of phpass or vanilla bcrypt hashed passwords. 964 $pass_frag = substr( $user->user_pass, 8, 4 ); 965 } else { 966 // Otherwise, use a substring from the end of the hash to avoid dealing with potentially long hash prefixes. 967 $pass_frag = substr( $user->user_pass, -4 ); 968 } 969 970 $key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); 971 972 $hash = hash_hmac( 'sha256', $user->user_login . '|' . $expiration . '|' . $token, $key ); 973 974 $cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash; 975 976 /** 977 * Filters the authentication cookie. 978 * 979 * @since 2.5.0 980 * @since 4.0.0 The `$token` parameter was added. 981 * 982 * @param string $cookie Authentication cookie. 983 * @param int $user_id User ID. 984 * @param int $expiration The time the cookie expires as a UNIX timestamp. 985 * @param string $scheme Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'. 986 * @param string $token User's session token used. 987 */ 988 return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token ); 989 } 990 endif; 991 992 if ( ! function_exists( 'wp_parse_auth_cookie' ) ) : 993 /** 994 * Parses a cookie into its components. 995 * 996 * @since 2.7.0 997 * @since 4.0.0 The `$token` element was added to the return value. 998 * 999 * @param string $cookie Authentication cookie. 1000 * @param string $scheme Optional. The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'. 1001 * @return string[]|false { 1002 * Authentication cookie components. None of the components should be assumed 1003 * to be valid as they come directly from a client-provided cookie value. If 1004 * the cookie value is malformed, false is returned. 1005 * 1006 * @type string $username User's username. 1007 * @type string $expiration The time the cookie expires as a UNIX timestamp. 1008 * @type string $token User's session token used. 1009 * @type string $hmac The security hash for the cookie. 1010 * @type string $scheme The cookie scheme to use. 1011 * } 1012 */ 1013 function wp_parse_auth_cookie( $cookie = '', $scheme = '' ) { 1014 if ( empty( $cookie ) ) { 1015 switch ( $scheme ) { 1016 case 'auth': 1017 $cookie_name = AUTH_COOKIE; 1018 break; 1019 case 'secure_auth': 1020 $cookie_name = SECURE_AUTH_COOKIE; 1021 break; 1022 case 'logged_in': 1023 $cookie_name = LOGGED_IN_COOKIE; 1024 break; 1025 default: 1026 if ( is_ssl() ) { 1027 $cookie_name = SECURE_AUTH_COOKIE; 1028 $scheme = 'secure_auth'; 1029 } else { 1030 $cookie_name = AUTH_COOKIE; 1031 $scheme = 'auth'; 1032 } 1033 } 1034 1035 if ( empty( $_COOKIE[ $cookie_name ] ) ) { 1036 return false; 1037 } 1038 $cookie = $_COOKIE[ $cookie_name ]; 1039 } 1040 1041 $cookie_elements = explode( '|', $cookie ); 1042 if ( count( $cookie_elements ) !== 4 ) { 1043 return false; 1044 } 1045 1046 list( $username, $expiration, $token, $hmac ) = $cookie_elements; 1047 1048 return compact( 'username', 'expiration', 'token', 'hmac', 'scheme' ); 1049 } 1050 endif; 1051 1052 if ( ! function_exists( 'wp_set_auth_cookie' ) ) : 1053 /** 1054 * Sets the authentication cookies for a given user ID. 1055 * 1056 * The `$remember` parameter controls cookie persistence: 1057 * - If true, the cookie is persistent (default 14 days, filterable via {@see 'auth_cookie_expiration'}). 1058 * - If false, the cookie is a browser session cookie (expires when the browser closes). 1059 * Internally, {@see 'auth_cookie_expiration'} is still applied, to expire the login after 1060 * two days or when the browser is closed, whichever occurs first. 1061 * 1062 * @since 2.5.0 1063 * @since 4.3.0 Added the `$token` parameter. 1064 * 1065 * @param int $user_id User ID. 1066 * @param bool $remember Whether to remember the user. 1067 * @param bool|string $secure Whether the auth cookie should only be sent over HTTPS. Default is an empty 1068 * string which means the value of `is_ssl()` will be used. 1069 * @param string $token Optional. User's session token to use for this cookie. 1070 */ 1071 function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) { 1072 if ( $remember ) { 1073 /** 1074 * Filters the duration of the authentication cookie expiration period. 1075 * 1076 * @since 2.8.0 1077 * 1078 * @param int $length Duration of the expiration period in seconds. 1079 * @param int $user_id User ID. 1080 * @param bool $remember Whether to remember the user login. Default false. 1081 */ 1082 $expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember ); 1083 1084 /* 1085 * Ensure the browser will continue to send the cookie after the expiration time is reached. 1086 * Needed for the login grace period in wp_validate_auth_cookie(). 1087 */ 1088 $expire = $expiration + ( 12 * HOUR_IN_SECONDS ); 1089 } else { 1090 /** This filter is documented in wp-includes/pluggable.php */ 1091 $expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember ); 1092 $expire = 0; 1093 } 1094 1095 if ( '' === $secure ) { 1096 $secure = is_ssl(); 1097 } 1098 1099 // Front-end cookie is secure when the auth cookie is secure and the site's home URL uses HTTPS. 1100 $secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME ); 1101 1102 /** 1103 * Filters whether the auth cookie should only be sent over HTTPS. 1104 * 1105 * @since 3.1.0 1106 * 1107 * @param bool $secure Whether the cookie should only be sent over HTTPS. 1108 * @param int $user_id User ID. 1109 */ 1110 $secure = apply_filters( 'secure_auth_cookie', $secure, $user_id ); 1111 1112 /** 1113 * Filters whether the logged in cookie should only be sent over HTTPS. 1114 * 1115 * @since 3.1.0 1116 * 1117 * @param bool $secure_logged_in_cookie Whether the logged in cookie should only be sent over HTTPS. 1118 * @param int $user_id User ID. 1119 * @param bool $secure Whether the auth cookie should only be sent over HTTPS. 1120 */ 1121 $secure_logged_in_cookie = apply_filters( 'secure_logged_in_cookie', $secure_logged_in_cookie, $user_id, $secure ); 1122 1123 if ( $secure ) { 1124 $auth_cookie_name = SECURE_AUTH_COOKIE; 1125 $scheme = 'secure_auth'; 1126 } else { 1127 $auth_cookie_name = AUTH_COOKIE; 1128 $scheme = 'auth'; 1129 } 1130 1131 if ( '' === $token ) { 1132 $manager = WP_Session_Tokens::get_instance( $user_id ); 1133 $token = $manager->create( $expiration ); 1134 } 1135 1136 $auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token ); 1137 $logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token ); 1138 1139 /** 1140 * Fires immediately before the authentication cookie is set. 1141 * 1142 * @since 2.5.0 1143 * @since 4.9.0 The `$token` parameter was added. 1144 * 1145 * @param string $auth_cookie Authentication cookie value. 1146 * @param int $expire The time the login grace period expires as a UNIX timestamp. 1147 * Default is 12 hours past the cookie's expiration time. 1148 * @param int $expiration The time when the authentication cookie expires as a UNIX timestamp. 1149 * Default is 14 days from now. 1150 * @param int $user_id User ID. 1151 * @param string $scheme Authentication scheme. Values include 'auth' or 'secure_auth'. 1152 * @param string $token User's session token to use for this cookie. 1153 */ 1154 do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme, $token ); 1155 1156 /** 1157 * Fires immediately before the logged-in authentication cookie is set. 1158 * 1159 * @since 2.6.0 1160 * @since 4.9.0 The `$token` parameter was added. 1161 * 1162 * @param string $logged_in_cookie The logged-in cookie value. 1163 * @param int $expire The time the login grace period expires as a UNIX timestamp. 1164 * Default is 12 hours past the cookie's expiration time. 1165 * @param int $expiration The time when the logged-in authentication cookie expires as a UNIX timestamp. 1166 * Default is 14 days from now. 1167 * @param int $user_id User ID. 1168 * @param string $scheme Authentication scheme. Default 'logged_in'. 1169 * @param string $token User's session token to use for this cookie. 1170 */ 1171 do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in', $token ); 1172 1173 /** 1174 * Allows preventing auth cookies from actually being sent to the client. 1175 * 1176 * @since 4.7.4 1177 * @since 6.2.0 The `$expire`, `$expiration`, `$user_id`, `$scheme`, and `$token` parameters were added. 1178 * 1179 * @param bool $send Whether to send auth cookies to the client. Default true. 1180 * @param int $expire The time the login grace period expires as a UNIX timestamp. 1181 * Default is 12 hours past the cookie's expiration time. Zero when clearing cookies. 1182 * @param int $expiration The time when the logged-in authentication cookie expires as a UNIX timestamp. 1183 * Default is 14 days from now. Zero when clearing cookies. 1184 * @param int $user_id User ID. Zero when clearing cookies. 1185 * @param string $scheme Authentication scheme. Values include 'auth' or 'secure_auth'. 1186 * Empty string when clearing cookies. 1187 * @param string $token User's session token to use for this cookie. Empty string when clearing cookies. 1188 */ 1189 if ( ! apply_filters( 'send_auth_cookies', true, $expire, $expiration, $user_id, $scheme, $token ) ) { 1190 return; 1191 } 1192 1193 setcookie( $auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true ); 1194 setcookie( $auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true ); 1195 setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true ); 1196 if ( COOKIEPATH !== SITECOOKIEPATH ) { 1197 setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true ); 1198 } 1199 } 1200 endif; 1201 1202 if ( ! function_exists( 'wp_clear_auth_cookie' ) ) : 1203 /** 1204 * Removes all of the cookies associated with authentication. 1205 * 1206 * @since 2.5.0 1207 */ 1208 function wp_clear_auth_cookie() { 1209 /** 1210 * Fires just before the authentication cookies are cleared. 1211 * 1212 * @since 2.7.0 1213 */ 1214 do_action( 'clear_auth_cookie' ); 1215 1216 /** This filter is documented in wp-includes/pluggable.php */ 1217 if ( ! apply_filters( 'send_auth_cookies', true, 0, 0, 0, '', '' ) ) { 1218 return; 1219 } 1220 1221 // Auth cookies. 1222 setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN ); 1223 setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN ); 1224 setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN ); 1225 setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN ); 1226 setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 1227 setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 1228 1229 // Settings cookies. 1230 setcookie( 'wp-settings-' . get_current_user_id(), ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH ); 1231 setcookie( 'wp-settings-time-' . get_current_user_id(), ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH ); 1232 1233 // Old cookies. 1234 setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 1235 setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 1236 setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 1237 setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 1238 1239 // Even older cookies. 1240 setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 1241 setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 1242 setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 1243 setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 1244 1245 // Post password cookie. 1246 setcookie( 'wp-postpass_' . COOKIEHASH, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 1247 } 1248 endif; 1249 1250 if ( ! function_exists( 'is_user_logged_in' ) ) : 1251 /** 1252 * Determines whether the current visitor is a logged in user. 1253 * 1254 * For more information on this and similar theme functions, check out 1255 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1256 * Conditional Tags} article in the Theme Developer Handbook. 1257 * 1258 * @since 2.0.0 1259 * 1260 * @return bool True if user is logged in, false if not logged in. 1261 */ 1262 function is_user_logged_in() { 1263 $user = wp_get_current_user(); 1264 1265 return $user->exists(); 1266 } 1267 endif; 1268 1269 if ( ! function_exists( 'auth_redirect' ) ) : 1270 /** 1271 * Checks if a user is logged in, if not it redirects them to the login page. 1272 * 1273 * When this code is called from a page, it checks to see if the user viewing the page is logged in. 1274 * If the user is not logged in, they are redirected to the login page. The user is redirected 1275 * in such a way that, upon logging in, they will be sent directly to the page they were originally 1276 * trying to access. 1277 * 1278 * @since 1.5.0 1279 */ 1280 function auth_redirect() { 1281 $secure = ( is_ssl() || force_ssl_admin() ); 1282 1283 /** 1284 * Filters whether to use a secure authentication redirect. 1285 * 1286 * @since 3.1.0 1287 * 1288 * @param bool $secure Whether to use a secure authentication redirect. Default false. 1289 */ 1290 $secure = apply_filters( 'secure_auth_redirect', $secure ); 1291 1292 // If https is required and request is http, redirect. 1293 if ( $secure && ! is_ssl() && str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) { 1294 if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) { 1295 wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); 1296 exit; 1297 } else { 1298 wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 1299 exit; 1300 } 1301 } 1302 1303 /** 1304 * Filters the authentication redirect scheme. 1305 * 1306 * @since 2.9.0 1307 * 1308 * @param string $scheme Authentication redirect scheme. Default empty. 1309 */ 1310 $scheme = apply_filters( 'auth_redirect_scheme', '' ); 1311 1312 $user_id = wp_validate_auth_cookie( '', $scheme ); 1313 if ( $user_id ) { 1314 /** 1315 * Fires before the authentication redirect. 1316 * 1317 * @since 2.8.0 1318 * 1319 * @param int $user_id User ID. 1320 */ 1321 do_action( 'auth_redirect', $user_id ); 1322 1323 // If the user wants ssl but the session is not ssl, redirect. 1324 if ( ! $secure && get_user_option( 'use_ssl', $user_id ) && str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) { 1325 if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) { 1326 wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); 1327 exit; 1328 } else { 1329 wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 1330 exit; 1331 } 1332 } 1333 1334 return; // The cookie is good, so we're done. 1335 } 1336 1337 // The cookie is no good, so force login. 1338 nocache_headers(); 1339 1340 if ( str_contains( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) { 1341 $redirect = wp_get_referer(); 1342 } else { 1343 $redirect = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 1344 } 1345 1346 $login_url = wp_login_url( $redirect, true ); 1347 1348 wp_redirect( $login_url ); 1349 exit; 1350 } 1351 endif; 1352 1353 if ( ! function_exists( 'check_admin_referer' ) ) : 1354 /** 1355 * Ensures intent by verifying that a user was referred from another admin page with the correct security nonce. 1356 * 1357 * This function ensures the user intends to perform a given action, which helps protect against clickjacking style 1358 * attacks. It verifies intent, not authorization, therefore it does not verify the user's capabilities. This should 1359 * be performed with `current_user_can()` or similar. 1360 * 1361 * If the nonce value is invalid, the function will exit with an "Are You Sure?" style message. 1362 * 1363 * @since 1.2.0 1364 * @since 2.5.0 The `$query_arg` parameter was added. 1365 * 1366 * @param int|string $action The nonce action. 1367 * @param string $query_arg Optional. Key to check for nonce in `$_REQUEST`. Default '_wpnonce'. 1368 * @return int|false 1 if the nonce is valid and generated between 0-12 hours ago, 1369 * 2 if the nonce is valid and generated between 12-24 hours ago. 1370 * False if the nonce is invalid. Only possible when `$action` is -1, 1371 * as the function otherwise exits rather than returning false. 1372 * @phpstan-return ( $action is -1 ? int|false : int ) 1373 */ 1374 function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) { 1375 if ( -1 === $action ) { 1376 _doing_it_wrong( __FUNCTION__, __( 'You should specify an action to be verified by using the first parameter.' ), '3.2.0' ); 1377 } 1378 1379 $adminurl = strtolower( admin_url() ); 1380 $referer = strtolower( wp_get_referer() ); 1381 $result = isset( $_REQUEST[ $query_arg ] ) ? wp_verify_nonce( $_REQUEST[ $query_arg ], $action ) : false; 1382 1383 /** 1384 * Fires once the admin request has been validated or not. 1385 * 1386 * @since 1.5.1 1387 * 1388 * @param string $action The nonce action. 1389 * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between 1390 * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. 1391 */ 1392 do_action( 'check_admin_referer', $action, $result ); 1393 1394 if ( ! $result && ! ( -1 === $action && str_starts_with( $referer, $adminurl ) ) ) { 1395 wp_nonce_ays( $action ); 1396 die(); 1397 } 1398 1399 return $result; 1400 } 1401 endif; 1402 1403 if ( ! function_exists( 'check_ajax_referer' ) ) : 1404 /** 1405 * Verifies the Ajax request to prevent processing requests external of the blog. 1406 * 1407 * @since 2.0.3 1408 * 1409 * @param int|string $action Action nonce. 1410 * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false, 1411 * `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce' 1412 * (in that order). Default false. 1413 * @param bool $stop Optional. Whether to stop early when the nonce cannot be verified. 1414 * Default true. 1415 * @return int|false 1 if the nonce is valid and generated between 0-12 hours ago, 1416 * 2 if the nonce is valid and generated between 12-24 hours ago. 1417 * False if the nonce is invalid. Only possible when `$stop` is false, 1418 * as the function otherwise exits rather than returning false. 1419 * @phpstan-return ( $stop is true ? int : int|false ) 1420 */ 1421 function check_ajax_referer( $action = -1, $query_arg = false, $stop = true ) { 1422 if ( -1 === $action ) { 1423 _doing_it_wrong( __FUNCTION__, __( 'You should specify an action to be verified by using the first parameter.' ), '4.7.0' ); 1424 } 1425 1426 $nonce = ''; 1427 1428 if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) ) { 1429 $nonce = $_REQUEST[ $query_arg ]; 1430 } elseif ( isset( $_REQUEST['_ajax_nonce'] ) ) { 1431 $nonce = $_REQUEST['_ajax_nonce']; 1432 } elseif ( isset( $_REQUEST['_wpnonce'] ) ) { 1433 $nonce = $_REQUEST['_wpnonce']; 1434 } 1435 1436 $result = wp_verify_nonce( $nonce, $action ); 1437 1438 /** 1439 * Fires once the Ajax request has been validated or not. 1440 * 1441 * @since 2.1.0 1442 * 1443 * @param string $action The Ajax nonce action. 1444 * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between 1445 * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. 1446 */ 1447 do_action( 'check_ajax_referer', $action, $result ); 1448 1449 if ( $stop && false === $result ) { 1450 if ( wp_doing_ajax() ) { 1451 wp_die( -1, 403 ); 1452 } else { 1453 die( '-1' ); 1454 } 1455 } 1456 1457 return $result; 1458 } 1459 endif; 1460 1461 if ( ! function_exists( 'wp_redirect' ) ) : 1462 /** 1463 * Redirects to another page. 1464 * 1465 * Note: wp_redirect() does not exit automatically, and should almost always be 1466 * followed by a call to `exit;`: 1467 * 1468 * wp_redirect( $url ); 1469 * exit; 1470 * 1471 * Exiting can also be selectively manipulated by using wp_redirect() as a conditional 1472 * in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_status'} filters: 1473 * 1474 * if ( wp_redirect( $url ) ) { 1475 * exit; 1476 * } 1477 * 1478 * @since 1.5.1 1479 * @since 5.1.0 The `$x_redirect_by` parameter was added. 1480 * @since 5.4.0 On invalid status codes, wp_die() is called. 1481 * 1482 * @global bool $is_IIS 1483 * 1484 * @param string $location The path or URL to redirect to. 1485 * @param int $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily). 1486 * @param string|false $x_redirect_by Optional. The application doing the redirect or false to omit. Default 'WordPress'. 1487 * @return bool False if the redirect was canceled, true otherwise. 1488 */ 1489 function wp_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) { 1490 global $is_IIS; 1491 1492 /** 1493 * Filters the redirect location. 1494 * 1495 * @since 2.1.0 1496 * 1497 * @param string $location The path or URL to redirect to. 1498 * @param int $status The HTTP response status code to use. 1499 */ 1500 $location = apply_filters( 'wp_redirect', $location, $status ); 1501 1502 /** 1503 * Filters the redirect HTTP response status code to use. 1504 * 1505 * @since 2.3.0 1506 * 1507 * @param int $status The HTTP response status code to use. 1508 * @param string $location The path or URL to redirect to. 1509 */ 1510 $status = apply_filters( 'wp_redirect_status', $status, $location ); 1511 1512 if ( ! $location ) { 1513 return false; 1514 } 1515 1516 if ( $status < 300 || 399 < $status ) { 1517 wp_die( __( 'HTTP redirect status code must be a redirection code, 3xx.' ) ); 1518 } 1519 1520 $location = wp_sanitize_redirect( $location ); 1521 1522 if ( ! $is_IIS && 'cgi-fcgi' !== PHP_SAPI ) { 1523 status_header( $status ); // This causes problems on IIS and some FastCGI setups. 1524 } 1525 1526 /** 1527 * Filters the value of the `X-Redirect-By` HTTP header. 1528 * 1529 * Allows applications to identify themselves when they're doing a redirect. 1530 * 1531 * @since 5.1.0 1532 * 1533 * @param string|false $x_redirect_by The application doing the redirect or false to omit the header. 1534 * @param int $status Status code to use. 1535 * @param string $location The path to redirect to. 1536 */ 1537 $x_redirect_by = apply_filters( 'x_redirect_by', $x_redirect_by, $status, $location ); 1538 if ( is_string( $x_redirect_by ) ) { 1539 header( "X-Redirect-By: $x_redirect_by" ); 1540 } 1541 1542 header( "Location: $location", true, $status ); 1543 1544 return true; 1545 } 1546 endif; 1547 1548 if ( ! function_exists( 'wp_sanitize_redirect' ) ) : 1549 /** 1550 * Sanitizes a URL for use in a redirect. 1551 * 1552 * @since 2.3.0 1553 * 1554 * @param string $location The path to redirect to. 1555 * @return string Redirect-sanitized URL. 1556 */ 1557 function wp_sanitize_redirect( $location ) { 1558 // Encode spaces. 1559 $location = str_replace( ' ', '%20', $location ); 1560 1561 $regex = '/ 1562 ( 1563 (?: [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx 1564 | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 1565 | [\xE1-\xEC][\x80-\xBF]{2} 1566 | \xED[\x80-\x9F][\x80-\xBF] 1567 | [\xEE-\xEF][\x80-\xBF]{2} 1568 | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 1569 | [\xF1-\xF3][\x80-\xBF]{3} 1570 | \xF4[\x80-\x8F][\x80-\xBF]{2} 1571 ){1,40} # ...one or more times 1572 )/x'; 1573 $location = preg_replace_callback( $regex, '_wp_sanitize_utf8_in_redirect', $location ); 1574 $location = preg_replace( '|[^a-z0-9-~+_.?#=&;,/:%!*\[\]()@]|i', '', $location ); 1575 $location = wp_kses_no_null( $location ); 1576 1577 // Remove %0D and %0A from location. 1578 $strip = array( '%0d', '%0a', '%0D', '%0A' ); 1579 return _deep_replace( $strip, $location ); 1580 } 1581 1582 /** 1583 * URL encodes UTF-8 characters in a URL. 1584 * 1585 * @ignore 1586 * @since 4.2.0 1587 * @access private 1588 * 1589 * @see wp_sanitize_redirect() 1590 * 1591 * @param array $matches RegEx matches against the redirect location. 1592 * @return string URL-encoded version of the first RegEx match. 1593 */ 1594 function _wp_sanitize_utf8_in_redirect( $matches ) { 1595 return urlencode( $matches[0] ); 1596 } 1597 endif; 1598 1599 if ( ! function_exists( 'wp_safe_redirect' ) ) : 1600 /** 1601 * Performs a safe (local) redirect, using wp_redirect(). 1602 * 1603 * Checks whether the $location is using an allowed host, if it has an absolute 1604 * path. A plugin can therefore set or remove allowed host(s) to or from the 1605 * list. 1606 * 1607 * If the host is not allowed, then the redirect defaults to wp-admin on the siteurl 1608 * instead. This prevents malicious redirects which redirect to another host, 1609 * but only used in a few places. 1610 * 1611 * Note: wp_safe_redirect() does not exit automatically, and should almost always be 1612 * followed by a call to `exit;`: 1613 * 1614 * wp_safe_redirect( $url ); 1615 * exit; 1616 * 1617 * Exiting can also be selectively manipulated by using wp_safe_redirect() as a conditional 1618 * in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_status'} filters: 1619 * 1620 * if ( wp_safe_redirect( $url ) ) { 1621 * exit; 1622 * } 1623 * 1624 * @since 2.3.0 1625 * @since 5.1.0 The return value from wp_redirect() is now passed on, and the `$x_redirect_by` parameter was added. 1626 * 1627 * @param string $location The path or URL to redirect to. 1628 * @param int $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily). 1629 * @param string|false $x_redirect_by Optional. The application doing the redirect or false to omit. Default 'WordPress'. 1630 * @return bool False if the redirect was canceled, true otherwise. 1631 */ 1632 function wp_safe_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) { 1633 1634 // Need to look at the URL the way it will end up in wp_redirect(). 1635 $location = wp_sanitize_redirect( $location ); 1636 1637 /** 1638 * Filters the redirect fallback URL for when the provided redirect is not safe (local). 1639 * 1640 * @since 4.3.0 1641 * 1642 * @param string $fallback_url The fallback URL to use by default. 1643 * @param int $status The HTTP response status code to use. 1644 */ 1645 $fallback_url = apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ); 1646 1647 $location = wp_validate_redirect( $location, $fallback_url ); 1648 1649 return wp_redirect( $location, $status, $x_redirect_by ); 1650 } 1651 endif; 1652 1653 if ( ! function_exists( 'wp_validate_redirect' ) ) : 1654 /** 1655 * Validates a URL for use in a redirect. 1656 * 1657 * Checks whether the $location is using an allowed host, if it has an absolute 1658 * path. A plugin can therefore set or remove allowed host(s) to or from the 1659 * list. 1660 * 1661 * If the host is not allowed, then the redirect is to $fallback_url supplied. 1662 * 1663 * @since 2.8.1 1664 * 1665 * @param string $location The redirect to validate. 1666 * @param string $fallback_url The value to return if $location is not allowed. 1667 * @return string Redirect-sanitized URL. 1668 */ 1669 function wp_validate_redirect( $location, $fallback_url = '' ) { 1670 $location = wp_sanitize_redirect( trim( $location, " \t\n\r\0\x08\x0B" ) ); 1671 // Browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'. 1672 if ( str_starts_with( $location, '//' ) ) { 1673 $location = 'http:' . $location; 1674 } 1675 1676 /* 1677 * In PHP 5 parse_url() may fail if the URL query part contains 'http://'. 1678 * See https://bugs.php.net/bug.php?id=38143 1679 */ 1680 $cut = strpos( $location, '?' ); 1681 $test = $cut ? substr( $location, 0, $cut ) : $location; 1682 1683 $lp = parse_url( $test ); 1684 1685 // Give up if malformed URL. 1686 if ( false === $lp ) { 1687 return $fallback_url; 1688 } 1689 1690 // Allow only 'http' and 'https' schemes. No 'data:', etc. 1691 if ( isset( $lp['scheme'] ) && ! ( 'http' === $lp['scheme'] || 'https' === $lp['scheme'] ) ) { 1692 return $fallback_url; 1693 } 1694 1695 if ( ! isset( $lp['host'] ) && ! empty( $lp['path'] ) && '/' !== $lp['path'][0] ) { 1696 $path = ''; 1697 if ( ! empty( $_SERVER['REQUEST_URI'] ) ) { 1698 $path = dirname( parse_url( 'http://placeholder' . $_SERVER['REQUEST_URI'], PHP_URL_PATH ) . '?' ); 1699 $path = wp_normalize_path( $path ); 1700 } 1701 $location = '/' . ltrim( $path . '/', '/' ) . $location; 1702 } 1703 1704 /* 1705 * Reject if certain components are set but host is not. 1706 * This catches URLs like https:host.com for which parse_url() does not set the host field. 1707 */ 1708 if ( ! isset( $lp['host'] ) && ( isset( $lp['scheme'] ) || isset( $lp['user'] ) || isset( $lp['pass'] ) || isset( $lp['port'] ) ) ) { 1709 return $fallback_url; 1710 } 1711 1712 // Reject malformed components parse_url() can return on odd inputs. 1713 foreach ( array( 'user', 'pass', 'host' ) as $component ) { 1714 if ( isset( $lp[ $component ] ) && strpbrk( $lp[ $component ], ':/?#@' ) ) { 1715 return $fallback_url; 1716 } 1717 } 1718 1719 $wpp = parse_url( home_url() ); 1720 1721 /** 1722 * Filters the list of allowed hosts to redirect to. 1723 * 1724 * @since 2.3.0 1725 * 1726 * @param string[] $hosts An array of allowed host names. 1727 * @param string $host The host name of the redirect destination; empty string if not set. 1728 */ 1729 $allowed_hosts = (array) apply_filters( 'allowed_redirect_hosts', array( $wpp['host'] ), $lp['host'] ?? '' ); 1730 1731 if ( isset( $lp['host'] ) && ( ! in_array( $lp['host'], $allowed_hosts, true ) && strtolower( $wpp['host'] ) !== $lp['host'] ) ) { 1732 $location = $fallback_url; 1733 } 1734 1735 return $location; 1736 } 1737 endif; 1738 1739 if ( ! function_exists( 'wp_notify_postauthor' ) ) : 1740 /** 1741 * Notifies an author (and/or others) of a comment/trackback/pingback on a post. 1742 * 1743 * @since 1.0.0 1744 * 1745 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 1746 * @param string $deprecated Not used. 1747 * @return bool True on completion. False if no email addresses were specified. 1748 */ 1749 function wp_notify_postauthor( $comment_id, $deprecated = null ) { 1750 if ( null !== $deprecated ) { 1751 _deprecated_argument( __FUNCTION__, '3.8.0' ); 1752 } 1753 1754 $comment = get_comment( $comment_id ); 1755 if ( empty( $comment ) || empty( $comment->comment_post_ID ) ) { 1756 return false; 1757 } 1758 1759 $post = get_post( $comment->comment_post_ID ); 1760 $author = get_userdata( $post->post_author ); 1761 1762 // Who to notify? By default, just the post author, but others can be added. 1763 $emails = array(); 1764 if ( $author ) { 1765 $emails[] = $author->user_email; 1766 } 1767 1768 /** 1769 * Filters the list of email addresses to receive a comment notification. 1770 * 1771 * By default, only post authors are notified of comments. This filter allows 1772 * others to be added. 1773 * 1774 * @since 3.7.0 1775 * 1776 * @param string[] $emails An array of email addresses to receive a comment notification. 1777 * @param string $comment_id The comment ID as a numeric string. 1778 */ 1779 $emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID ); 1780 $emails = array_filter( $emails ); 1781 1782 // If there are no addresses to send the comment to, bail. 1783 if ( ! count( $emails ) ) { 1784 return false; 1785 } 1786 1787 // Facilitate unsetting below without knowing the keys. 1788 $emails = array_flip( $emails ); 1789 1790 /** 1791 * Filters whether to notify comment authors of their comments on their own posts. 1792 * 1793 * By default, comment authors aren't notified of their comments on their own 1794 * posts. This filter allows you to override that. 1795 * 1796 * @since 3.8.0 1797 * 1798 * @param bool $notify Whether to notify the post author of their own comment. 1799 * Default false. 1800 * @param string $comment_id The comment ID as a numeric string. 1801 */ 1802 $notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID ); 1803 1804 // The comment was left by the author. 1805 if ( $author && ! $notify_author && (int) $comment->user_id === (int) $post->post_author ) { 1806 unset( $emails[ $author->user_email ] ); 1807 } 1808 1809 // The author moderated a comment on their own post. 1810 if ( $author && ! $notify_author && get_current_user_id() === (int) $post->post_author ) { 1811 unset( $emails[ $author->user_email ] ); 1812 } 1813 1814 // The post author is no longer a member of the blog. 1815 if ( $author && ! $notify_author && ! user_can( $post->post_author, 'read_post', $post->ID ) ) { 1816 unset( $emails[ $author->user_email ] ); 1817 } 1818 1819 // If there's no email to send the comment to, bail, otherwise flip array back around for use below. 1820 if ( ! count( $emails ) ) { 1821 return false; 1822 } else { 1823 $emails = array_flip( $emails ); 1824 } 1825 1826 $comment_author_domain = ''; 1827 if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) { 1828 $comment_author_domain = gethostbyaddr( $comment->comment_author_IP ); 1829 } 1830 1831 /* 1832 * The blogname option is escaped with esc_html() on the way into the database in sanitize_option(). 1833 * We want to reverse this for the plain text arena of emails. 1834 */ 1835 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 1836 $comment_content = wp_specialchars_decode( $comment->comment_content ); 1837 1838 $wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', wp_parse_url( network_home_url(), PHP_URL_HOST ) ); 1839 1840 if ( '' === $comment->comment_author ) { 1841 $from = "From: \"$blogname\" <$wp_email>"; 1842 if ( '' !== $comment->comment_author_email ) { 1843 $reply_to = "Reply-To: $comment->comment_author_email"; 1844 } 1845 } else { 1846 $from = "From: \"$comment->comment_author\" <$wp_email>"; 1847 if ( '' !== $comment->comment_author_email ) { 1848 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>"; 1849 } 1850 } 1851 1852 $message_headers = "$from\n" 1853 . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n"; 1854 1855 if ( isset( $reply_to ) ) { 1856 $message_headers .= $reply_to . "\n"; 1857 } 1858 1859 /** 1860 * Filters the comment notification email headers. 1861 * 1862 * @since 1.5.2 1863 * 1864 * @param string $message_headers Headers for the comment notification email. 1865 * @param string $comment_id Comment ID as a numeric string. 1866 */ 1867 $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID ); 1868 1869 foreach ( $emails as $email ) { 1870 $user = get_user_by( 'email', $email ); 1871 1872 if ( $user ) { 1873 $switched_locale = switch_to_user_locale( $user->ID ); 1874 } else { 1875 $switched_locale = switch_to_locale( get_locale() ); 1876 } 1877 1878 switch ( $comment->comment_type ) { 1879 case 'trackback': 1880 /* translators: %s: Post title. */ 1881 $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n"; 1882 /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */ 1883 $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1884 /* translators: %s: Trackback/pingback/comment author URL. */ 1885 $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 1886 /* translators: %s: Comment text. */ 1887 $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 1888 $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n"; 1889 /* translators: Trackback notification email subject. 1: Site title, 2: Post title. */ 1890 $subject = sprintf( __( '[%1$s] Trackback: "%2$s"' ), $blogname, $post->post_title ); 1891 break; 1892 1893 case 'pingback': 1894 /* translators: %s: Post title. */ 1895 $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n"; 1896 /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */ 1897 $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1898 /* translators: %s: Trackback/pingback/comment author URL. */ 1899 $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 1900 /* translators: %s: Comment text. */ 1901 $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 1902 $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n"; 1903 /* translators: Pingback notification email subject. 1: Site title, 2: Post title. */ 1904 $subject = sprintf( __( '[%1$s] Pingback: "%2$s"' ), $blogname, $post->post_title ); 1905 break; 1906 1907 case 'note': 1908 /* translators: %s: Post title. */ 1909 $notify_message = sprintf( __( 'New note on your post "%s"' ), $post->post_title ) . "\r\n"; 1910 /* translators: 1: Note author's name, 2: Note author's IP address, 3: Note author's hostname. */ 1911 $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1912 /* translators: %s: Note author email. */ 1913 $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; 1914 /* translators: %s: Note text. */ 1915 $notify_message .= sprintf( __( 'Note: %s' ), "\r\n" . ( empty( $comment_content ) ? __( 'resolved/reopened' ) : $comment_content ) ) . "\r\n\r\n"; 1916 $notify_message .= __( 'You can see all notes on this post here:' ) . "\r\n"; 1917 /* translators: Note notification email subject. 1: Site title, 2: Post title. */ 1918 $subject = sprintf( __( '[%1$s] Note: "%2$s"' ), $blogname, $post->post_title ); 1919 break; 1920 1921 default: // Comments. 1922 /* translators: %s: Post title. */ 1923 $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n"; 1924 /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */ 1925 $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 1926 /* translators: %s: Comment author email. */ 1927 $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; 1928 /* translators: %s: Trackback/pingback/comment author URL. */ 1929 $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 1930 1931 if ( $comment->comment_parent && user_can( $post->post_author, 'edit_comment', $comment->comment_parent ) ) { 1932 /* translators: Comment moderation. %s: Parent comment edit URL. */ 1933 $notify_message .= sprintf( __( 'In reply to: %s' ), admin_url( "comment.php?action=editcomment&c={$comment->comment_parent}#wpbody-content" ) ) . "\r\n"; 1934 } 1935 1936 /* translators: %s: Comment text. */ 1937 $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 1938 $notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n"; 1939 /* translators: Comment notification email subject. 1: Site title, 2: Post title. */ 1940 $subject = sprintf( __( '[%1$s] Comment: "%2$s"' ), $blogname, $post->post_title ); 1941 break; 1942 } 1943 1944 if ( 'note' === $comment->comment_type ) { 1945 $notify_message .= get_edit_post_link( $comment->comment_post_ID, 'url' ) . "\r\n"; 1946 } else { 1947 $notify_message .= get_permalink( $comment->comment_post_ID ) . "#comments\r\n\r\n"; 1948 /* translators: %s: Comment URL. */ 1949 $notify_message .= sprintf( __( 'Permalink: %s' ), get_comment_link( $comment ) ) . "\r\n"; 1950 } 1951 1952 if ( 'note' !== $comment->comment_type && user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) { 1953 if ( EMPTY_TRASH_DAYS ) { 1954 /* translators: Comment moderation. %s: Comment action URL. */ 1955 $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; 1956 } else { 1957 /* translators: Comment moderation. %s: Comment action URL. */ 1958 $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; 1959 } 1960 /* translators: Comment moderation. %s: Comment action URL. */ 1961 $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; 1962 } 1963 1964 /** 1965 * Filters the comment notification email text. 1966 * 1967 * @since 1.5.2 1968 * 1969 * @param string $notify_message The comment notification email text. 1970 * @param string $comment_id Comment ID as a numeric string. 1971 */ 1972 $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID ); 1973 1974 /** 1975 * Filters the comment notification email subject. 1976 * 1977 * @since 1.5.2 1978 * 1979 * @param string $subject The comment notification email subject. 1980 * @param string $comment_id Comment ID as a numeric string. 1981 */ 1982 $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID ); 1983 1984 wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); 1985 1986 if ( $switched_locale ) { 1987 restore_previous_locale(); 1988 } 1989 } 1990 1991 return true; 1992 } 1993 endif; 1994 1995 if ( ! function_exists( 'wp_notify_moderator' ) ) : 1996 /** 1997 * Notifies the moderator of the site about a new comment that is awaiting approval. 1998 * 1999 * @since 1.0.0 2000 * 2001 * @global wpdb $wpdb WordPress database abstraction object. 2002 * 2003 * Uses the {@see 'notify_moderator'} filter to determine whether the site moderator 2004 * should be notified, overriding the site setting. 2005 * 2006 * @param int $comment_id Comment ID. 2007 * @return true Always returns true. 2008 */ 2009 function wp_notify_moderator( $comment_id ) { 2010 global $wpdb; 2011 2012 $maybe_notify = get_option( 'moderation_notify' ); 2013 2014 /** 2015 * Filters whether to send the site moderator email notifications, overriding the site setting. 2016 * 2017 * @since 4.4.0 2018 * 2019 * @param bool $maybe_notify Whether to notify blog moderator. 2020 * @param int $comment_id The ID of the comment for the notification. 2021 */ 2022 $maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id ); 2023 2024 if ( ! $maybe_notify ) { 2025 return true; 2026 } 2027 2028 $comment = get_comment( $comment_id ); 2029 $post = get_post( $comment->comment_post_ID ); 2030 $user = get_userdata( $post->post_author ); 2031 // Send to the administration and to the post author if the author can modify the comment. 2032 $emails = array( get_option( 'admin_email' ) ); 2033 if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) { 2034 if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) { 2035 $emails[] = $user->user_email; 2036 } 2037 } 2038 2039 $comment_author_domain = ''; 2040 if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) { 2041 $comment_author_domain = gethostbyaddr( $comment->comment_author_IP ); 2042 } 2043 2044 $comments_waiting = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '0'" ); 2045 2046 /* 2047 * The blogname option is escaped with esc_html() on the way into the database in sanitize_option(). 2048 * We want to reverse this for the plain text arena of emails. 2049 */ 2050 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 2051 $comment_content = wp_specialchars_decode( $comment->comment_content ); 2052 2053 $message_headers = ''; 2054 2055 /** 2056 * Filters the list of recipients for comment moderation emails. 2057 * 2058 * @since 3.7.0 2059 * 2060 * @param string[] $emails List of email addresses to notify for comment moderation. 2061 * @param int $comment_id Comment ID. 2062 */ 2063 $emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id ); 2064 2065 /** 2066 * Filters the comment moderation email headers. 2067 * 2068 * @since 2.8.0 2069 * 2070 * @param string $message_headers Headers for the comment moderation email. 2071 * @param int $comment_id Comment ID. 2072 */ 2073 $message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id ); 2074 2075 foreach ( $emails as $email ) { 2076 $user = get_user_by( 'email', $email ); 2077 2078 if ( $user ) { 2079 $switched_locale = switch_to_user_locale( $user->ID ); 2080 } else { 2081 $switched_locale = switch_to_locale( get_locale() ); 2082 } 2083 2084 switch ( $comment->comment_type ) { 2085 case 'trackback': 2086 /* translators: %s: Post title. */ 2087 $notify_message = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; 2088 $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; 2089 /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */ 2090 $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 2091 /* translators: %s: Trackback/pingback/comment author URL. */ 2092 $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 2093 /* translators: %s: Trackback excerpt text. */ 2094 $notify_message .= sprintf( __( 'Trackback excerpt: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 2095 break; 2096 2097 case 'pingback': 2098 /* translators: %s: Post title. */ 2099 $notify_message = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; 2100 $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; 2101 /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */ 2102 $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 2103 /* translators: %s: Trackback/pingback/comment author URL. */ 2104 $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 2105 /* translators: %s: Pingback excerpt text. */ 2106 $notify_message .= sprintf( __( 'Pingback excerpt: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 2107 break; 2108 2109 default: // Comments. 2110 /* translators: %s: Post title. */ 2111 $notify_message = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n"; 2112 $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n"; 2113 /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */ 2114 $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 2115 /* translators: %s: Comment author email. */ 2116 $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; 2117 /* translators: %s: Trackback/pingback/comment author URL. */ 2118 $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 2119 2120 if ( $comment->comment_parent ) { 2121 /* translators: Comment moderation. %s: Parent comment edit URL. */ 2122 $notify_message .= sprintf( __( 'In reply to: %s' ), admin_url( "comment.php?action=editcomment&c={$comment->comment_parent}#wpbody-content" ) ) . "\r\n"; 2123 } 2124 2125 /* translators: %s: Comment text. */ 2126 $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 2127 break; 2128 } 2129 2130 /* translators: Comment moderation. %s: Comment action URL. */ 2131 $notify_message .= sprintf( __( 'Approve it: %s' ), admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) ) . "\r\n"; 2132 2133 if ( EMPTY_TRASH_DAYS ) { 2134 /* translators: Comment moderation. %s: Comment action URL. */ 2135 $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) ) . "\r\n"; 2136 } else { 2137 /* translators: Comment moderation. %s: Comment action URL. */ 2138 $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) ) . "\r\n"; 2139 } 2140 2141 /* translators: Comment moderation. %s: Comment action URL. */ 2142 $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) ) . "\r\n"; 2143 2144 $notify_message .= sprintf( 2145 /* translators: Comment moderation. %s: Number of comments awaiting approval. */ 2146 _n( 2147 'Currently %s comment is waiting for approval. Please visit the moderation panel:', 2148 'Currently %s comments are waiting for approval. Please visit the moderation panel:', 2149 $comments_waiting 2150 ), 2151 number_format_i18n( $comments_waiting ) 2152 ) . "\r\n"; 2153 $notify_message .= admin_url( 'edit-comments.php?comment_status=moderated#wpbody-content' ) . "\r\n"; 2154 2155 /* translators: Comment moderation notification email subject. 1: Site title, 2: Post title. */ 2156 $subject = sprintf( __( '[%1$s] Please moderate: "%2$s"' ), $blogname, $post->post_title ); 2157 2158 /** 2159 * Filters the comment moderation email text. 2160 * 2161 * @since 1.5.2 2162 * 2163 * @param string $notify_message Text of the comment moderation email. 2164 * @param int $comment_id Comment ID. 2165 */ 2166 $notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id ); 2167 2168 /** 2169 * Filters the comment moderation email subject. 2170 * 2171 * @since 1.5.2 2172 * 2173 * @param string $subject Subject of the comment moderation email. 2174 * @param int $comment_id Comment ID. 2175 */ 2176 $subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id ); 2177 2178 wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); 2179 2180 if ( $switched_locale ) { 2181 restore_previous_locale(); 2182 } 2183 } 2184 2185 return true; 2186 } 2187 endif; 2188 2189 if ( ! function_exists( 'wp_password_change_notification' ) ) : 2190 /** 2191 * Notifies the blog admin of a user changing password, normally via email. 2192 * 2193 * @since 2.7.0 2194 * 2195 * @param WP_User $user User object. 2196 */ 2197 function wp_password_change_notification( $user ) { 2198 /* 2199 * Send a copy of password change notification to the admin, 2200 * but check to see if it's the admin whose password we're changing, and skip this. 2201 */ 2202 if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) { 2203 2204 $admin_user = get_user_by( 'email', get_option( 'admin_email' ) ); 2205 2206 if ( $admin_user ) { 2207 $switched_locale = switch_to_user_locale( $admin_user->ID ); 2208 } else { 2209 $switched_locale = switch_to_locale( get_locale() ); 2210 } 2211 2212 /* translators: %s: User name. */ 2213 $message = sprintf( __( 'Password changed for user: %s' ), $user->user_login ) . "\r\n"; 2214 /* 2215 * The blogname option is escaped with esc_html() on the way into the database in sanitize_option(). 2216 * We want to reverse this for the plain text arena of emails. 2217 */ 2218 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 2219 2220 $wp_password_change_notification_email = array( 2221 'to' => get_option( 'admin_email' ), 2222 /* translators: Password change notification email subject. %s: Site title. */ 2223 'subject' => __( '[%s] Password Changed' ), 2224 'message' => $message, 2225 'headers' => '', 2226 ); 2227 2228 /** 2229 * Filters the contents of the password change notification email sent to the site admin. 2230 * 2231 * @since 4.9.0 2232 * 2233 * @param array $wp_password_change_notification_email { 2234 * Used to build wp_mail(). 2235 * 2236 * @type string $to The intended recipient - site admin email address. 2237 * @type string $subject The subject of the email. 2238 * @type string $message The body of the email. 2239 * @type string $headers The headers of the email. 2240 * } 2241 * @param WP_User $user User object for user whose password was changed. 2242 * @param string $blogname The site title. 2243 */ 2244 $wp_password_change_notification_email = apply_filters( 'wp_password_change_notification_email', $wp_password_change_notification_email, $user, $blogname ); 2245 2246 wp_mail( 2247 $wp_password_change_notification_email['to'], 2248 wp_specialchars_decode( sprintf( $wp_password_change_notification_email['subject'], $blogname ) ), 2249 $wp_password_change_notification_email['message'], 2250 $wp_password_change_notification_email['headers'] 2251 ); 2252 2253 if ( $switched_locale ) { 2254 restore_previous_locale(); 2255 } 2256 } 2257 } 2258 endif; 2259 2260 if ( ! function_exists( 'wp_new_user_notification' ) ) : 2261 /** 2262 * Emails login credentials to a newly-registered user. 2263 * 2264 * A new user registration notification is also sent to admin email. 2265 * 2266 * @since 2.0.0 2267 * @since 4.3.0 The `$plaintext_pass` parameter was changed to `$notify`. 2268 * @since 4.3.1 The `$plaintext_pass` parameter was deprecated. `$notify` added as a third parameter. 2269 * @since 4.6.0 The `$notify` parameter accepts 'user' for sending notification only to the user created. 2270 * 2271 * @param int $user_id User ID. 2272 * @param mixed $deprecated Not used. 2273 * @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty 2274 * string (admin only), 'user', or 'both' (admin and user). Default empty. 2275 */ 2276 function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) { 2277 if ( null !== $deprecated ) { 2278 _deprecated_argument( __FUNCTION__, '4.3.1' ); 2279 } 2280 2281 // Accepts only 'user', 'admin' , 'both' or default '' as $notify. 2282 if ( ! in_array( $notify, array( 'user', 'admin', 'both', '' ), true ) ) { 2283 return; 2284 } 2285 2286 $user = get_userdata( $user_id ); 2287 2288 /* 2289 * The blogname option is escaped with esc_html() on the way into the database in sanitize_option(). 2290 * We want to reverse this for the plain text arena of emails. 2291 */ 2292 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 2293 2294 /** 2295 * Filters whether the admin is notified of a new user registration. 2296 * 2297 * @since 6.1.0 2298 * 2299 * @param bool $send Whether to send the email. Default true. 2300 * @param WP_User $user User object for new user. 2301 */ 2302 $send_notification_to_admin = apply_filters( 'wp_send_new_user_notification_to_admin', true, $user ); 2303 2304 if ( 'user' !== $notify && true === $send_notification_to_admin ) { 2305 2306 $admin_user = get_user_by( 'email', get_option( 'admin_email' ) ); 2307 2308 if ( $admin_user ) { 2309 $switched_locale = switch_to_user_locale( $admin_user->ID ); 2310 } else { 2311 $switched_locale = switch_to_locale( get_locale() ); 2312 } 2313 2314 /* translators: %s: Site title. */ 2315 $message = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n"; 2316 /* translators: %s: User login. */ 2317 $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n"; 2318 /* translators: %s: User email address. */ 2319 $message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n"; 2320 2321 $wp_new_user_notification_email_admin = array( 2322 'to' => get_option( 'admin_email' ), 2323 /* translators: New user registration notification email subject. %s: Site title. */ 2324 'subject' => __( '[%s] New User Registration' ), 2325 'message' => $message, 2326 'headers' => '', 2327 ); 2328 2329 /** 2330 * Filters the contents of the new user notification email sent to the site admin. 2331 * 2332 * @since 4.9.0 2333 * 2334 * @param array $wp_new_user_notification_email_admin { 2335 * Used to build wp_mail(). 2336 * 2337 * @type string $to The intended recipient - site admin email address. 2338 * @type string $subject The subject of the email. 2339 * @type string $message The body of the email. 2340 * @type string $headers The headers of the email. 2341 * } 2342 * @param WP_User $user User object for new user. 2343 * @param string $blogname The site title. 2344 */ 2345 $wp_new_user_notification_email_admin = apply_filters( 'wp_new_user_notification_email_admin', $wp_new_user_notification_email_admin, $user, $blogname ); 2346 2347 wp_mail( 2348 $wp_new_user_notification_email_admin['to'], 2349 wp_specialchars_decode( sprintf( $wp_new_user_notification_email_admin['subject'], $blogname ) ), 2350 $wp_new_user_notification_email_admin['message'], 2351 $wp_new_user_notification_email_admin['headers'] 2352 ); 2353 2354 if ( $switched_locale ) { 2355 restore_previous_locale(); 2356 } 2357 } 2358 2359 /** 2360 * Filters whether the user is notified of their new user registration. 2361 * 2362 * @since 6.1.0 2363 * 2364 * @param bool $send Whether to send the email. Default true. 2365 * @param WP_User $user User object for new user. 2366 */ 2367 $send_notification_to_user = apply_filters( 'wp_send_new_user_notification_to_user', true, $user ); 2368 2369 // `$deprecated` was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification. 2370 if ( 'admin' === $notify || true !== $send_notification_to_user || ( empty( $deprecated ) && empty( $notify ) ) ) { 2371 return; 2372 } 2373 2374 $key = get_password_reset_key( $user ); 2375 if ( is_wp_error( $key ) ) { 2376 return; 2377 } 2378 2379 $switched_locale = switch_to_user_locale( $user_id ); 2380 2381 $message = __( 'To set your password, visit the following address:' ) . "\r\n\r\n"; 2382 2383 /* 2384 * Since some user login names end in a period, this could produce ambiguous URLs that 2385 * end in a period. To avoid the ambiguity, ensure that the login is not the last query 2386 * arg in the URL. If moving it to the end, a trailing period will need to be escaped. 2387 * 2388 * @see https://core.trac.wordpress.org/tickets/42957 2389 */ 2390 $message .= network_site_url( 'wp-login.php?login=' . rawurlencode( $user->user_login ) . "&key=$key&action=rp", 'login' ) . "\r\n"; 2391 2392 $wp_new_user_notification_email = array( 2393 'to' => $user->user_email, 2394 /* translators: Login details notification email subject. %s: Site title. */ 2395 'subject' => __( '[%s] Login Details' ), 2396 'message' => $message, 2397 'headers' => '', 2398 ); 2399 2400 /** 2401 * Filters the contents of the new user notification email sent to the new user. 2402 * 2403 * @since 4.9.0 2404 * 2405 * @param array $wp_new_user_notification_email { 2406 * Used to build wp_mail(). 2407 * 2408 * @type string $to The intended recipient - New user email address. 2409 * @type string $subject The subject of the email. 2410 * @type string $message The body of the email. 2411 * @type string $headers The headers of the email. 2412 * } 2413 * @param WP_User $user User object for new user. 2414 * @param string $blogname The site title. 2415 */ 2416 $wp_new_user_notification_email = apply_filters( 'wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname ); 2417 2418 wp_mail( 2419 $wp_new_user_notification_email['to'], 2420 wp_specialchars_decode( sprintf( $wp_new_user_notification_email['subject'], $blogname ) ), 2421 $wp_new_user_notification_email['message'], 2422 $wp_new_user_notification_email['headers'] 2423 ); 2424 2425 if ( $switched_locale ) { 2426 restore_previous_locale(); 2427 } 2428 } 2429 endif; 2430 2431 if ( ! function_exists( 'wp_nonce_tick' ) ) : 2432 /** 2433 * Returns the time-dependent variable for nonce creation. 2434 * 2435 * A nonce has a lifespan of two ticks. Nonces in their second tick may be 2436 * updated, e.g. by autosave. 2437 * 2438 * @since 2.5.0 2439 * @since 6.1.0 Added `$action` argument. 2440 * 2441 * @param string|int $action Optional. The nonce action. Default -1. 2442 * @return float Float value rounded up to the next highest integer. 2443 */ 2444 function wp_nonce_tick( $action = -1 ) { 2445 /** 2446 * Filters the lifespan of nonces in seconds. 2447 * 2448 * @since 2.5.0 2449 * @since 6.1.0 Added `$action` argument to allow for more targeted filters. 2450 * 2451 * @param int $lifespan Lifespan of nonces in seconds. Default 86,400 seconds, or one day. 2452 * @param string|int $action The nonce action, or -1 if none was provided. 2453 */ 2454 $nonce_life = apply_filters( 'nonce_life', DAY_IN_SECONDS, $action ); 2455 2456 return ceil( time() / ( $nonce_life / 2 ) ); 2457 } 2458 endif; 2459 2460 if ( ! function_exists( 'wp_verify_nonce' ) ) : 2461 /** 2462 * Verifies that a correct security nonce was used with time limit. 2463 * 2464 * A nonce is valid for between 12 and 24 hours (by default). 2465 * 2466 * @since 2.0.3 2467 * 2468 * @param string $nonce Nonce value that was used for verification, usually via a form field. 2469 * @param string|int $action Should give context to what is taking place and be the same when nonce was created. 2470 * @return int|false 1 if the nonce is valid and generated between 0-12 hours ago, 2471 * 2 if the nonce is valid and generated between 12-24 hours ago. 2472 * False if the nonce is invalid. 2473 */ 2474 function wp_verify_nonce( $nonce, $action = -1 ) { 2475 $nonce = (string) $nonce; 2476 $user = wp_get_current_user(); 2477 $uid = (int) $user->ID; 2478 if ( ! $uid ) { 2479 /** 2480 * Filters whether the user who generated the nonce is logged out. 2481 * 2482 * @since 3.5.0 2483 * 2484 * @param int $uid ID of the nonce-owning user. 2485 * @param string|int $action The nonce action, or -1 if none was provided. 2486 */ 2487 $uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); 2488 } 2489 2490 if ( empty( $nonce ) ) { 2491 return false; 2492 } 2493 2494 $token = wp_get_session_token(); 2495 $i = wp_nonce_tick( $action ); 2496 2497 // Nonce generated 0-12 hours ago. 2498 $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ); 2499 if ( hash_equals( $expected, $nonce ) ) { 2500 return 1; 2501 } 2502 2503 // Nonce generated 12-24 hours ago. 2504 $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ); 2505 if ( hash_equals( $expected, $nonce ) ) { 2506 return 2; 2507 } 2508 2509 /** 2510 * Fires when nonce verification fails. 2511 * 2512 * @since 4.4.0 2513 * 2514 * @param string $nonce The invalid nonce. 2515 * @param string|int $action The nonce action. 2516 * @param WP_User $user The current user object. 2517 * @param string $token The user's session token. 2518 */ 2519 do_action( 'wp_verify_nonce_failed', $nonce, $action, $user, $token ); 2520 2521 // Invalid nonce. 2522 return false; 2523 } 2524 endif; 2525 2526 if ( ! function_exists( 'wp_create_nonce' ) ) : 2527 /** 2528 * Creates a cryptographic token tied to a specific action, user, user session, 2529 * and window of time. 2530 * 2531 * @since 2.0.3 2532 * @since 4.0.0 Session tokens were integrated with nonce creation. 2533 * 2534 * @param string|int $action Scalar value to add context to the nonce. 2535 * @return string The token. 2536 */ 2537 function wp_create_nonce( $action = -1 ) { 2538 $user = wp_get_current_user(); 2539 $uid = (int) $user->ID; 2540 if ( ! $uid ) { 2541 /** This filter is documented in wp-includes/pluggable.php */ 2542 $uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); 2543 } 2544 2545 $token = wp_get_session_token(); 2546 $i = wp_nonce_tick( $action ); 2547 2548 return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ); 2549 } 2550 endif; 2551 2552 if ( ! function_exists( 'wp_salt' ) ) : 2553 /** 2554 * Returns a salt to add to hashes. 2555 * 2556 * Salts are created using secret keys. Secret keys are located in two places: 2557 * in the database and in the wp-config.php file. The secret key in the database 2558 * is randomly generated and will be appended to the secret keys in wp-config.php. 2559 * 2560 * The secret keys in wp-config.php should be updated to strong, random keys to maximize 2561 * security. Below is an example of how the secret key constants are defined. 2562 * Do not paste this example directly into wp-config.php. Instead, have a 2563 * {@link https://api.wordpress.org/secret-key/1.1/salt/ secret key created} just 2564 * for you. 2565 * 2566 * define('AUTH_KEY', ' Xakm<o xQy rw4EMsLKM-?!T+,PFF})H4lzcW57AF0U@N@< >M%G4Yt>f`z]MON'); 2567 * define('SECURE_AUTH_KEY', 'LzJ}op]mr|6+![P}Ak:uNdJCJZd>(Hx.-Mh#Tz)pCIU#uGEnfFz|f ;;eU%/U^O~'); 2568 * define('LOGGED_IN_KEY', '|i|Ux`9<p-h$aFf(qnT:sDO:D1P^wZ$$/Ra@miTJi9G;ddp_<q}6H1)o|a +&JCM'); 2569 * define('NONCE_KEY', '%:R{[P|,s.KuMltH5}cI;/k<Gx~j!f0I)m_sIyu+&NJZ)-iO>z7X>QYR0Z_XnZ@|'); 2570 * define('AUTH_SALT', 'eZyT)-Naw]F8CwA*VaW#q*|.)g@o}||wf~@C-YSt}(dh_r6EbI#A,y|nU2{B#JBW'); 2571 * define('SECURE_AUTH_SALT', '!=oLUTXh,QW=H `}`L|9/^4-3 STz},T(w}W<I`.JjPi)<Bmf1v,HpGe}T1:Xt7n'); 2572 * define('LOGGED_IN_SALT', '+XSqHc;@Q*K_b|Z?NC[3H!!EONbh.n<+=uKR:>*c(u`g~EJBf#8u#R{mUEZrozmm'); 2573 * define('NONCE_SALT', 'h`GXHhD>SLWVfg1(1(N{;.V!MoE(SfbA_ksP@&`+AycHcAV$+?@3q+rxV{%^VyKT'); 2574 * 2575 * Salting passwords helps against tools which has stored hashed values of 2576 * common dictionary strings. The added values makes it harder to crack. 2577 * 2578 * @since 2.5.0 2579 * 2580 * @link https://api.wordpress.org/secret-key/1.1/salt/ Create secrets for wp-config.php 2581 * 2582 * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce). 2583 * @return string Salt value 2584 */ 2585 function wp_salt( $scheme = 'auth' ) { 2586 static $cached_salts = array(); 2587 if ( isset( $cached_salts[ $scheme ] ) ) { 2588 /** 2589 * Filters the WordPress salt. 2590 * 2591 * @since 2.5.0 2592 * 2593 * @param string $cached_salt Cached salt for the given scheme. 2594 * @param string $scheme Authentication scheme. Values include 'auth', 2595 * 'secure_auth', 'logged_in', and 'nonce'. 2596 */ 2597 return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme ); 2598 } 2599 2600 static $duplicated_keys; 2601 if ( null === $duplicated_keys ) { 2602 $duplicated_keys = array(); 2603 2604 foreach ( array( 'AUTH', 'SECURE_AUTH', 'LOGGED_IN', 'NONCE', 'SECRET' ) as $first ) { 2605 foreach ( array( 'KEY', 'SALT' ) as $second ) { 2606 if ( ! defined( "{$first}_{$second}" ) ) { 2607 continue; 2608 } 2609 $value = constant( "{$first}_{$second}" ); 2610 $duplicated_keys[ $value ] = isset( $duplicated_keys[ $value ] ); 2611 } 2612 } 2613 2614 $duplicated_keys['put your unique phrase here'] = true; 2615 2616 /* 2617 * translators: This string should only be translated if wp-config-sample.php is localized. 2618 * You can check the localized release package or 2619 * https://i18n.svn.wordpress.org/<locale code>/branches/<wp version>/dist/wp-config-sample.php 2620 */ 2621 $duplicated_keys[ __( 'put your unique phrase here' ) ] = true; 2622 } 2623 2624 /* 2625 * Determine which options to prime. 2626 * 2627 * If the salt keys are undefined, use a duplicate value or the 2628 * default `put your unique phrase here` value the salt will be 2629 * generated via `wp_generate_password()` and stored as a site 2630 * option. These options will be primed to avoid repeated 2631 * database requests for undefined salts. 2632 */ 2633 $options_to_prime = array(); 2634 foreach ( array( 'auth', 'secure_auth', 'logged_in', 'nonce' ) as $key ) { 2635 foreach ( array( 'key', 'salt' ) as $second ) { 2636 $const = strtoupper( "{$key}_{$second}" ); 2637 if ( ! defined( $const ) || true === $duplicated_keys[ constant( $const ) ] ) { 2638 $options_to_prime[] = "{$key}_{$second}"; 2639 } 2640 } 2641 } 2642 2643 if ( ! empty( $options_to_prime ) ) { 2644 /* 2645 * Also prime `secret_key` used for undefined salting schemes. 2646 * 2647 * If the scheme is unknown, the default value for `secret_key` will be 2648 * used too for the salt. This should rarely happen, so the option is only 2649 * primed if other salts are undefined. 2650 * 2651 * At this point of execution it is known that a database call will be made 2652 * to prime salts, so the `secret_key` option can be primed regardless of the 2653 * constants status. 2654 */ 2655 $options_to_prime[] = 'secret_key'; 2656 wp_prime_site_option_caches( $options_to_prime ); 2657 } 2658 2659 $values = array( 2660 'key' => '', 2661 'salt' => '', 2662 ); 2663 if ( defined( 'SECRET_KEY' ) && SECRET_KEY && empty( $duplicated_keys[ SECRET_KEY ] ) ) { 2664 $values['key'] = SECRET_KEY; 2665 } 2666 if ( 'auth' === $scheme && defined( 'SECRET_SALT' ) && SECRET_SALT && empty( $duplicated_keys[ SECRET_SALT ] ) ) { 2667 $values['salt'] = SECRET_SALT; 2668 } 2669 2670 if ( in_array( $scheme, array( 'auth', 'secure_auth', 'logged_in', 'nonce' ), true ) ) { 2671 foreach ( array( 'key', 'salt' ) as $type ) { 2672 $const = strtoupper( "{$scheme}_{$type}" ); 2673 if ( defined( $const ) && constant( $const ) && empty( $duplicated_keys[ constant( $const ) ] ) ) { 2674 $values[ $type ] = constant( $const ); 2675 } elseif ( ! $values[ $type ] ) { 2676 $values[ $type ] = get_site_option( "{$scheme}_{$type}" ); 2677 if ( ! $values[ $type ] ) { 2678 $values[ $type ] = wp_generate_password( 64, true, true ); 2679 update_site_option( "{$scheme}_{$type}", $values[ $type ] ); 2680 } 2681 } 2682 } 2683 } else { 2684 if ( ! $values['key'] ) { 2685 $values['key'] = get_site_option( 'secret_key' ); 2686 if ( ! $values['key'] ) { 2687 $values['key'] = wp_generate_password( 64, true, true ); 2688 update_site_option( 'secret_key', $values['key'] ); 2689 } 2690 } 2691 $values['salt'] = hash_hmac( 'md5', $scheme, $values['key'] ); 2692 } 2693 2694 $cached_salts[ $scheme ] = $values['key'] . $values['salt']; 2695 2696 /** This filter is documented in wp-includes/pluggable.php */ 2697 return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme ); 2698 } 2699 endif; 2700 2701 if ( ! function_exists( 'wp_hash' ) ) : 2702 /** 2703 * Gets the hash of the given string. 2704 * 2705 * The default algorithm is md5 but can be changed to any algorithm supported by 2706 * `hash_hmac()`. Use the `hash_hmac_algos()` function to check the supported 2707 * algorithms. 2708 * 2709 * @since 2.0.3 2710 * @since 6.8.0 The `$algo` parameter was added. 2711 * 2712 * @throws InvalidArgumentException if the hashing algorithm is not supported. 2713 * 2714 * @param string $data Plain text to hash. 2715 * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce). 2716 * @param string $algo Hashing algorithm to use. Default: 'md5'. 2717 * @return string Hash of $data. 2718 */ 2719 function wp_hash( $data, $scheme = 'auth', $algo = 'md5' ) { 2720 $salt = wp_salt( $scheme ); 2721 2722 // Ensure the algorithm is supported by the hash_hmac function. 2723 if ( ! in_array( $algo, hash_hmac_algos(), true ) ) { 2724 throw new InvalidArgumentException( 2725 sprintf( 2726 /* translators: 1: Name of a cryptographic hash algorithm. 2: List of supported algorithms. */ 2727 __( 'Unsupported hashing algorithm: %1$s. Supported algorithms are: %2$s' ), 2728 $algo, 2729 implode( ', ', hash_hmac_algos() ) 2730 ) 2731 ); 2732 } 2733 2734 return hash_hmac( $algo, $data, $salt ); 2735 } 2736 endif; 2737 2738 if ( ! function_exists( 'wp_hash_password' ) ) : 2739 /** 2740 * Creates a hash of a plain text password. 2741 * 2742 * For integration with other applications, this function can be overwritten to 2743 * instead use the other package password hashing algorithm. 2744 * 2745 * @since 2.5.0 2746 * @since 6.8.0 The password is now hashed using bcrypt by default instead of phpass. 2747 * 2748 * @global PasswordHash $wp_hasher phpass object. 2749 * 2750 * @param string $password Plain text user password to hash. 2751 * @return string The hash string of the password. 2752 */ 2753 function wp_hash_password( 2754 #[\SensitiveParameter] 2755 $password 2756 ) { 2757 global $wp_hasher; 2758 2759 if ( ! empty( $wp_hasher ) ) { 2760 return $wp_hasher->HashPassword( trim( $password ) ); 2761 } 2762 2763 if ( strlen( $password ) > 4096 ) { 2764 return '*'; 2765 } 2766 2767 /** 2768 * Filters the hashing algorithm to use in the password_hash() and password_needs_rehash() functions. 2769 * 2770 * The default is the value of the `PASSWORD_BCRYPT` constant which means bcrypt is used. 2771 * 2772 * **Important:** The only password hashing algorithm that is guaranteed to be available across PHP 2773 * installations is bcrypt. If you use any other algorithm you must make sure that it is available on 2774 * the server. The `password_algos()` function can be used to check which hashing algorithms are available. 2775 * 2776 * The hashing options can be controlled via the {@see 'wp_hash_password_options'} filter. 2777 * 2778 * Other available constants include: 2779 * 2780 * - `PASSWORD_ARGON2I` 2781 * - `PASSWORD_ARGON2ID` 2782 * - `PASSWORD_DEFAULT` 2783 * 2784 * @since 6.8.0 2785 * @since 7.0.0 The `$algorithm` parameter is now always a string. 2786 * 2787 * @param string $algorithm The hashing algorithm. Default is the value of the `PASSWORD_BCRYPT` constant. 2788 */ 2789 $algorithm = apply_filters( 'wp_hash_password_algorithm', PASSWORD_BCRYPT ); 2790 2791 /** 2792 * Filters the options passed to the password_hash() and password_needs_rehash() functions. 2793 * 2794 * The default hashing algorithm is bcrypt, but this can be changed via the {@see 'wp_hash_password_algorithm'} 2795 * filter. You must ensure that the options are appropriate for the algorithm in use. 2796 * 2797 * @since 6.8.0 2798 * @since 7.0.0 The `$algorithm` parameter is now always a string. 2799 * 2800 * @param array $options Array of options to pass to the password hashing functions. 2801 * By default this is an empty array which means the default 2802 * options will be used. 2803 * @param string $algorithm The hashing algorithm in use. 2804 */ 2805 $options = apply_filters( 'wp_hash_password_options', array(), $algorithm ); 2806 2807 // Algorithms other than bcrypt don't need to use pre-hashing. 2808 if ( PASSWORD_BCRYPT !== $algorithm ) { 2809 return password_hash( $password, $algorithm, $options ); 2810 } 2811 2812 // Use SHA-384 to retain entropy from a password that's longer than 72 bytes, and a `wp-sha384` key for domain separation. 2813 $password_to_hash = base64_encode( hash_hmac( 'sha384', trim( $password ), 'wp-sha384', true ) ); 2814 2815 // Add a prefix to facilitate distinguishing vanilla bcrypt hashes. 2816 return '$wp' . password_hash( $password_to_hash, $algorithm, $options ); 2817 } 2818 endif; 2819 2820 if ( ! function_exists( 'wp_check_password' ) ) : 2821 /** 2822 * Checks a plaintext password against a hashed password. 2823 * 2824 * Note that this function may be used to check a value that is not a user password. 2825 * A plugin may use this function to check a password of a different type, and there 2826 * may not always be a user ID associated with the password. 2827 * 2828 * For integration with other applications, this function can be overwritten to 2829 * instead use the other package password hashing algorithm. 2830 * 2831 * @since 2.5.0 2832 * @since 6.8.0 Passwords in WordPress are now hashed with bcrypt by default. A 2833 * password that wasn't hashed with bcrypt will be checked with phpass. 2834 * 2835 * @global PasswordHash $wp_hasher phpass object. Used as a fallback for verifying 2836 * passwords that were hashed with phpass. 2837 * 2838 * @param string $password Plaintext password. 2839 * @param string $hash Hash of the password to check against. 2840 * @param string|int $user_id Optional. ID of a user associated with the password. 2841 * @return bool False, if the $password does not match the hashed password. 2842 */ 2843 function wp_check_password( 2844 #[\SensitiveParameter] 2845 $password, 2846 $hash, 2847 $user_id = '' 2848 ) { 2849 global $wp_hasher; 2850 2851 if ( strlen( $hash ) <= 32 ) { 2852 // Check the hash using md5 regardless of the current hashing mechanism. 2853 $check = hash_equals( $hash, md5( $password ) ); 2854 } elseif ( ! empty( $wp_hasher ) ) { 2855 // Check the password using the overridden hasher. 2856 $check = $wp_hasher->CheckPassword( $password, $hash ); 2857 } elseif ( strlen( $password ) > 4096 ) { 2858 // Passwords longer than 4096 characters are not supported. 2859 $check = false; 2860 } elseif ( str_starts_with( $hash, '$wp' ) ) { 2861 // Check the password using the current prefixed hash. 2862 $password_to_verify = base64_encode( hash_hmac( 'sha384', $password, 'wp-sha384', true ) ); 2863 $check = password_verify( $password_to_verify, substr( $hash, 3 ) ); 2864 } elseif ( str_starts_with( $hash, '$P$' ) ) { 2865 // Check the password using phpass. 2866 require_once ABSPATH . WPINC . '/class-phpass.php'; 2867 $check = ( new PasswordHash( 8, true ) )->CheckPassword( $password, $hash ); 2868 } else { 2869 // Check the password using compat support for any non-prefixed hash. 2870 $check = password_verify( $password, $hash ); 2871 } 2872 2873 /** 2874 * Filters whether the plaintext password matches the hashed password. 2875 * 2876 * @since 2.5.0 2877 * @since 6.8.0 Passwords are now hashed with bcrypt by default. 2878 * Old passwords may still be hashed with phpass or md5. 2879 * 2880 * @param bool $check Whether the passwords match. 2881 * @param string $password The plaintext password. 2882 * @param string $hash The hashed password. 2883 * @param string|int $user_id Optional ID of a user associated with the password. 2884 * Can be empty. 2885 */ 2886 return apply_filters( 'check_password', $check, $password, $hash, $user_id ); 2887 } 2888 endif; 2889 2890 if ( ! function_exists( 'wp_password_needs_rehash' ) ) : 2891 /** 2892 * Checks whether a password hash needs to be rehashed. 2893 * 2894 * Passwords are hashed with bcrypt using the default cost. A password hashed in a prior version 2895 * of WordPress may still be hashed with phpass and will need to be rehashed. If the default cost 2896 * or algorithm is changed in PHP or WordPress then a password hashed in a previous version will 2897 * need to be rehashed. 2898 * 2899 * Note that, just like wp_check_password(), this function may be used to check a value that is 2900 * not a user password. A plugin may use this function to check a password of a different type, 2901 * and there may not always be a user ID associated with the password. 2902 * 2903 * @since 6.8.0 2904 * 2905 * @global PasswordHash $wp_hasher phpass object. 2906 * 2907 * @param string $hash Hash of a password to check. 2908 * @param string|int $user_id Optional. ID of a user associated with the password. 2909 * @return bool Whether the hash needs to be rehashed. 2910 */ 2911 function wp_password_needs_rehash( $hash, $user_id = '' ) { 2912 global $wp_hasher; 2913 2914 if ( ! empty( $wp_hasher ) ) { 2915 return false; 2916 } 2917 2918 /** This filter is documented in wp-includes/pluggable.php */ 2919 $algorithm = apply_filters( 'wp_hash_password_algorithm', PASSWORD_BCRYPT ); 2920 2921 /** This filter is documented in wp-includes/pluggable.php */ 2922 $options = apply_filters( 'wp_hash_password_options', array(), $algorithm ); 2923 2924 $prefixed = str_starts_with( $hash, '$wp' ); 2925 2926 if ( ( PASSWORD_BCRYPT === $algorithm ) && ! $prefixed ) { 2927 // If bcrypt is in use and the hash is not prefixed then it needs to be rehashed. 2928 $needs_rehash = true; 2929 } else { 2930 // Otherwise check the hash minus its prefix if necessary. 2931 $hash_to_check = $prefixed ? substr( $hash, 3 ) : $hash; 2932 $needs_rehash = password_needs_rehash( $hash_to_check, $algorithm, $options ); 2933 } 2934 2935 /** 2936 * Filters whether the password hash needs to be rehashed. 2937 * 2938 * @since 6.8.0 2939 * 2940 * @param bool $needs_rehash Whether the password hash needs to be rehashed. 2941 * @param string $hash The password hash. 2942 * @param string|int $user_id Optional. ID of a user associated with the password. 2943 */ 2944 return apply_filters( 'password_needs_rehash', $needs_rehash, $hash, $user_id ); 2945 } 2946 endif; 2947 2948 if ( ! function_exists( 'wp_generate_password' ) ) : 2949 /** 2950 * Generates a random password drawn from the defined set of characters. 2951 * 2952 * Uses wp_rand() to create passwords with far less predictability 2953 * than similar native PHP functions like `rand()` or `mt_rand()`. 2954 * 2955 * @since 2.5.0 2956 * 2957 * @param int $length Optional. The length of password to generate. Default 12. 2958 * @param bool $special_chars Optional. Whether to include standard special characters. 2959 * Default true. 2960 * @param bool $extra_special_chars Optional. Whether to include other special characters. 2961 * Used when generating secret keys and salts. Default false. 2962 * @return string The random password. 2963 */ 2964 function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) { 2965 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 2966 if ( $special_chars ) { 2967 $chars .= '!@#$%^&*()'; 2968 } 2969 if ( $extra_special_chars ) { 2970 $chars .= '-_ []{}<>~`+=,.;:/?|'; 2971 } 2972 2973 $password = ''; 2974 for ( $i = 0; $i < $length; $i++ ) { 2975 $password .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 ); 2976 } 2977 2978 /** 2979 * Filters the randomly-generated password. 2980 * 2981 * @since 3.0.0 2982 * @since 5.3.0 Added the `$length`, `$special_chars`, and `$extra_special_chars` parameters. 2983 * 2984 * @param string $password The generated password. 2985 * @param int $length The length of password to generate. 2986 * @param bool $special_chars Whether to include standard special characters. 2987 * @param bool $extra_special_chars Whether to include other special characters. 2988 */ 2989 return apply_filters( 'random_password', $password, $length, $special_chars, $extra_special_chars ); 2990 } 2991 endif; 2992 2993 if ( ! function_exists( 'wp_rand' ) ) : 2994 /** 2995 * Generates a random non-negative number. 2996 * 2997 * @since 2.6.2 2998 * @since 4.4.0 Uses PHP7 random_int() or the random_compat library if available. 2999 * @since 6.1.0 Returns zero instead of a random number if both `$min` and `$max` are zero. 3000 * 3001 * @global string $rnd_value 3002 * 3003 * @param int $min Optional. Lower limit for the generated number. 3004 * Accepts positive integers or zero. Defaults to 0. 3005 * @param int $max Optional. Upper limit for the generated number. 3006 * Accepts positive integers. Defaults to 4294967295. 3007 * @return int A random non-negative number between min and max. 3008 */ 3009 function wp_rand( $min = null, $max = null ) { 3010 global $rnd_value; 3011 3012 /* 3013 * Some misconfigured 32-bit environments (Entropy PHP, for example) 3014 * truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats. 3015 */ 3016 $max_random_number = 3000000000 === 2147483647 ? (float) '4294967295' : 4294967295; // 4294967295 = 0xffffffff 3017 3018 if ( null === $min ) { 3019 $min = 0; 3020 } 3021 3022 if ( null === $max ) { 3023 $max = $max_random_number; 3024 } 3025 3026 // We only handle ints, floats are truncated to their integer value. 3027 $min = (int) $min; 3028 $max = (int) $max; 3029 3030 // Use PHP's CSPRNG, or a compatible method. 3031 static $use_random_int_functionality = true; 3032 if ( $use_random_int_functionality ) { 3033 try { 3034 // wp_rand() can accept arguments in either order, PHP cannot. 3035 $_max = max( $min, $max ); 3036 $_min = min( $min, $max ); 3037 $val = random_int( $_min, $_max ); 3038 if ( false !== $val ) { 3039 return absint( $val ); 3040 } else { 3041 $use_random_int_functionality = false; 3042 } 3043 } catch ( Error $e ) { 3044 $use_random_int_functionality = false; 3045 } catch ( Exception $e ) { 3046 $use_random_int_functionality = false; 3047 } 3048 } 3049 3050 /* 3051 * Reset $rnd_value after 14 uses. 3052 * 32 (md5) + 40 (sha1) + 40 (sha1) / 8 = 14 random numbers from $rnd_value. 3053 */ 3054 if ( strlen( $rnd_value ) < 8 ) { 3055 if ( defined( 'WP_SETUP_CONFIG' ) ) { 3056 static $seed = ''; 3057 } else { 3058 $seed = get_transient( 'random_seed' ); 3059 } 3060 $rnd_value = md5( uniqid( microtime() . mt_rand(), true ) . $seed ); 3061 $rnd_value .= sha1( $rnd_value ); 3062 $rnd_value .= sha1( $rnd_value . $seed ); 3063 $seed = md5( $seed . $rnd_value ); 3064 if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) { 3065 set_transient( 'random_seed', $seed ); 3066 } 3067 } 3068 3069 // Take the first 8 digits for our value. 3070 $value = substr( $rnd_value, 0, 8 ); 3071 3072 // Strip the first eight, leaving the remainder for the next call to wp_rand(). 3073 $rnd_value = substr( $rnd_value, 8 ); 3074 3075 $value = abs( hexdec( $value ) ); 3076 3077 // Reduce the value to be within the min - max range. 3078 $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 ); 3079 3080 return abs( (int) $value ); 3081 } 3082 endif; 3083 3084 if ( ! function_exists( 'wp_set_password' ) ) : 3085 /** 3086 * Updates the user's password with a new hashed one. 3087 * 3088 * For integration with other applications, this function can be overwritten to 3089 * instead use the other package password checking algorithm. 3090 * 3091 * Please note: This function should be used sparingly and is really only meant for single-time 3092 * application. Leveraging this improperly in a plugin or theme could result in an endless loop 3093 * of password resets if precautions are not taken to ensure it does not execute on every page load. 3094 * 3095 * @since 2.5.0 3096 * @since 6.8.0 The password is now hashed using bcrypt by default instead of phpass. 3097 * 3098 * @global wpdb $wpdb WordPress database abstraction object. 3099 * 3100 * @param string $password The plaintext new user password. 3101 * @param int $user_id User ID. 3102 */ 3103 function wp_set_password( 3104 #[\SensitiveParameter] 3105 $password, 3106 $user_id 3107 ) { 3108 global $wpdb; 3109 3110 $old_user_data = get_userdata( $user_id ); 3111 3112 $hash = wp_hash_password( $password ); 3113 $wpdb->update( 3114 $wpdb->users, 3115 array( 3116 'user_pass' => $hash, 3117 'user_activation_key' => '', 3118 ), 3119 array( 'ID' => $user_id ) 3120 ); 3121 3122 clean_user_cache( $user_id ); 3123 3124 /** 3125 * Fires after the user password is set. 3126 * 3127 * @since 6.2.0 3128 * @since 6.7.0 The `$old_user_data` parameter was added. 3129 * 3130 * @param string $password The plaintext password just set. 3131 * @param int $user_id The ID of the user whose password was just set. 3132 * @param WP_User $old_user_data Object containing user's data prior to update. 3133 */ 3134 do_action( 'wp_set_password', $password, $user_id, $old_user_data ); 3135 } 3136 endif; 3137 3138 if ( ! function_exists( 'get_avatar' ) ) : 3139 /** 3140 * Retrieves the avatar `<img>` tag for a user, email address, MD5 hash, comment, or post. 3141 * 3142 * @since 2.5.0 3143 * @since 4.2.0 Added the optional `$args` parameter. 3144 * @since 5.5.0 Added the `loading` argument. 3145 * @since 6.1.0 Added the `decoding` argument. 3146 * @since 6.3.0 Added the `fetchpriority` argument. 3147 * 3148 * @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, 3149 * user email, WP_User object, WP_Post object, or WP_Comment object. 3150 * @param int $size Optional. Height and width of the avatar in pixels. Default 96. 3151 * @param string $default_value URL for the default image or a default type. Accepts: 3152 * - '404' (return a 404 instead of a default image) 3153 * - 'retro' (a 8-bit arcade-style pixelated face) 3154 * - 'robohash' (a robot) 3155 * - 'monsterid' (a monster) 3156 * - 'wavatar' (a cartoon face) 3157 * - 'identicon' (the "quilt", a geometric pattern) 3158 * - 'initials' (initials based avatar with background color) 3159 * - 'color' (generated background color) 3160 * - 'mystery', 'mm', or 'mysteryman' (The Oyster Man) 3161 * - 'blank' (transparent GIF) 3162 * - 'gravatar_default' (the Gravatar logo) 3163 * Default is the value of the 'avatar_default' option, 3164 * with a fallback of 'mystery'. 3165 * @param string $alt Optional. Alternative text to use in the avatar image tag. 3166 * Default empty. 3167 * @param array $args { 3168 * Optional. Extra arguments to retrieve the avatar. 3169 * 3170 * @type int $height Display height of the avatar in pixels. Defaults to $size. 3171 * @type int $width Display width of the avatar in pixels. Defaults to $size. 3172 * @type bool $force_default Whether to always show the default image, never the Gravatar. 3173 * Default false. 3174 * @type string $rating What rating to display avatars up to. Accepts: 3175 * - 'G' (suitable for all audiences) 3176 * - 'PG' (possibly offensive, usually for audiences 13 and above) 3177 * - 'R' (intended for adult audiences above 17) 3178 * - 'X' (even more mature than above) 3179 * Default is the value of the 'avatar_rating' option. 3180 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. 3181 * Default null. 3182 * @type array|string $class Array or string of additional classes to add to the img element. 3183 * Default null. 3184 * @type bool $force_display Whether to always show the avatar - ignores the show_avatars option. 3185 * Default false. 3186 * @type string $loading Value for the `loading` attribute. 3187 * Default null. 3188 * @type string $fetchpriority Value for the `fetchpriority` attribute. 3189 * Default null. 3190 * @type string $decoding Value for the `decoding` attribute. 3191 * Default null. 3192 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. 3193 * Default empty. 3194 * } 3195 * @return string|false `<img>` tag for the user's avatar. False on failure. 3196 */ 3197 function get_avatar( $id_or_email, $size = 96, $default_value = '', $alt = '', $args = null ) { 3198 $defaults = array( 3199 // get_avatar_data() args. 3200 'size' => 96, 3201 'height' => null, 3202 'width' => null, 3203 'default' => get_option( 'avatar_default', 'mystery' ), 3204 'force_default' => false, 3205 'rating' => get_option( 'avatar_rating' ), 3206 'scheme' => null, 3207 'alt' => '', 3208 'class' => null, 3209 'force_display' => false, 3210 'loading' => null, 3211 'fetchpriority' => null, 3212 'decoding' => null, 3213 'extra_attr' => '', 3214 ); 3215 3216 if ( empty( $args ) ) { 3217 $args = array(); 3218 } 3219 3220 $args['size'] = (int) $size; 3221 $args['default'] = $default_value; 3222 $args['alt'] = $alt; 3223 3224 $args = wp_parse_args( $args, $defaults ); 3225 3226 if ( empty( $args['height'] ) ) { 3227 $args['height'] = $args['size']; 3228 } 3229 if ( empty( $args['width'] ) ) { 3230 $args['width'] = $args['size']; 3231 } 3232 3233 // Update args with loading optimized attributes. 3234 $loading_optimization_attr = wp_get_loading_optimization_attributes( 'img', $args, 'get_avatar' ); 3235 3236 $args = array_merge( $args, $loading_optimization_attr ); 3237 3238 if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { 3239 $id_or_email = get_comment( $id_or_email ); 3240 } 3241 3242 /** 3243 * Allows the HTML for a user's avatar to be returned early. 3244 * 3245 * Returning a non-null value will effectively short-circuit get_avatar(), passing 3246 * the value through the {@see 'get_avatar'} filter and returning early. 3247 * 3248 * @since 4.2.0 3249 * 3250 * @param string|null $avatar HTML for the user's avatar. Default null. 3251 * @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, 3252 * user email, WP_User object, WP_Post object, or WP_Comment object. 3253 * @param array $args Arguments passed to get_avatar_url(), after processing. 3254 */ 3255 $avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args ); 3256 3257 if ( ! is_null( $avatar ) ) { 3258 /** This filter is documented in wp-includes/pluggable.php */ 3259 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args ); 3260 } 3261 3262 if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) { 3263 return false; 3264 } 3265 3266 $url2x = get_avatar_url( $id_or_email, array_merge( $args, array( 'size' => $args['size'] * 2 ) ) ); 3267 3268 $args = get_avatar_data( $id_or_email, $args ); 3269 3270 $url = $args['url']; 3271 3272 if ( ! $url || is_wp_error( $url ) ) { 3273 return false; 3274 } 3275 3276 $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' ); 3277 3278 if ( ! $args['found_avatar'] || $args['force_default'] ) { 3279 $class[] = 'avatar-default'; 3280 } 3281 3282 if ( $args['class'] ) { 3283 if ( is_array( $args['class'] ) ) { 3284 $class = array_merge( $class, $args['class'] ); 3285 } else { 3286 $class[] = $args['class']; 3287 } 3288 } 3289 3290 // Add `loading`, `fetchpriority`, and `decoding` attributes. 3291 $extra_attr = $args['extra_attr']; 3292 3293 if ( in_array( $args['loading'], array( 'lazy', 'eager' ), true ) 3294 && ! preg_match( '/\bloading\s*=/', $extra_attr ) 3295 ) { 3296 if ( ! empty( $extra_attr ) ) { 3297 $extra_attr .= ' '; 3298 } 3299 3300 $extra_attr .= "loading='{$args['loading']}'"; 3301 } 3302 3303 if ( in_array( $args['fetchpriority'], array( 'high', 'low', 'auto' ), true ) 3304 && ! preg_match( '/\bfetchpriority\s*=/', $extra_attr ) 3305 ) { 3306 if ( ! empty( $extra_attr ) ) { 3307 $extra_attr .= ' '; 3308 } 3309 3310 $extra_attr .= "fetchpriority='{$args['fetchpriority']}'"; 3311 } 3312 3313 if ( in_array( $args['decoding'], array( 'async', 'sync', 'auto' ), true ) 3314 && ! preg_match( '/\bdecoding\s*=/', $extra_attr ) 3315 ) { 3316 if ( ! empty( $extra_attr ) ) { 3317 $extra_attr .= ' '; 3318 } 3319 3320 $extra_attr .= "decoding='{$args['decoding']}'"; 3321 } 3322 3323 $avatar = sprintf( 3324 "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>", 3325 esc_attr( $args['alt'] ), 3326 esc_url( $url ), 3327 esc_url( $url2x ) . ' 2x', 3328 esc_attr( implode( ' ', $class ) ), 3329 (int) $args['height'], 3330 (int) $args['width'], 3331 $extra_attr 3332 ); 3333 3334 /** 3335 * Filters the HTML for a user's avatar. 3336 * 3337 * @since 2.5.0 3338 * @since 4.2.0 Added the `$args` parameter. 3339 * 3340 * @param string $avatar HTML for the user's avatar. 3341 * @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, 3342 * user email, WP_User object, WP_Post object, or WP_Comment object. 3343 * @param int $size Height and width of the avatar in pixels. 3344 * @param string $default_value URL for the default image or a default type. Accepts: 3345 * - '404' (return a 404 instead of a default image) 3346 * - 'retro' (a 8-bit arcade-style pixelated face) 3347 * - 'robohash' (a robot) 3348 * - 'monsterid' (a monster) 3349 * - 'wavatar' (a cartoon face) 3350 * - 'identicon' (the "quilt", a geometric pattern) 3351 * - 'mystery', 'mm', or 'mysteryman' (The Oyster Man) 3352 * - 'blank' (transparent GIF) 3353 * - 'gravatar_default' (the Gravatar logo) 3354 * @param string $alt Alternative text to use in the avatar image tag. 3355 * @param array $args Arguments passed to get_avatar_data(), after processing. 3356 */ 3357 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args ); 3358 } 3359 endif; 3360 3361 if ( ! function_exists( 'wp_text_diff' ) ) : 3362 /** 3363 * Displays a human readable HTML representation of the difference between two strings. 3364 * 3365 * The Diff is available for getting the changes between versions. The output is 3366 * HTML, so the primary use is for displaying the changes. If the two strings 3367 * are equivalent, then an empty string will be returned. 3368 * 3369 * @since 2.6.0 3370 * 3371 * @see wp_parse_args() Used to change defaults to user defined settings. 3372 * @uses Text_Diff 3373 * @uses WP_Text_Diff_Renderer_Table 3374 * 3375 * @param string $left_string "old" (left) version of string. 3376 * @param string $right_string "new" (right) version of string. 3377 * @param string|array $args { 3378 * Associative array of options to pass to WP_Text_Diff_Renderer_Table(). 3379 * 3380 * @type string $title Titles the diff in a manner compatible 3381 * with the output. Default empty. 3382 * @type string $title_left Change the HTML to the left of the title. 3383 * Default empty. 3384 * @type string $title_right Change the HTML to the right of the title. 3385 * Default empty. 3386 * @type bool $show_split_view True for split view (two columns), false for 3387 * un-split view (single column). Default true. 3388 * } 3389 * @return string Empty string if strings are equivalent or HTML with differences. 3390 */ 3391 function wp_text_diff( $left_string, $right_string, $args = null ) { 3392 $defaults = array( 3393 'title' => '', 3394 'title_left' => '', 3395 'title_right' => '', 3396 'show_split_view' => true, 3397 ); 3398 $args = wp_parse_args( $args, $defaults ); 3399 3400 if ( ! class_exists( 'WP_Text_Diff_Renderer_Table', false ) ) { 3401 require ABSPATH . WPINC . '/wp-diff.php'; 3402 } 3403 3404 $left_string = normalize_whitespace( $left_string ); 3405 $right_string = normalize_whitespace( $right_string ); 3406 3407 $left_lines = explode( "\n", $left_string ); 3408 $right_lines = explode( "\n", $right_string ); 3409 $text_diff = new Text_Diff( $left_lines, $right_lines ); 3410 $renderer = new WP_Text_Diff_Renderer_Table( $args ); 3411 $diff = $renderer->render( $text_diff ); 3412 3413 if ( ! $diff ) { 3414 return ''; 3415 } 3416 3417 $is_split_view = ! empty( $args['show_split_view'] ); 3418 $is_split_view_class = $is_split_view ? ' is-split-view' : ''; 3419 3420 $r = "<table class='diff$is_split_view_class'>\n"; 3421 3422 if ( $args['title'] ) { 3423 $r .= "<caption class='diff-title'>$args[title]</caption>\n"; 3424 } 3425 3426 if ( $args['title_left'] || $args['title_right'] ) { 3427 $r .= '<thead>'; 3428 } 3429 3430 if ( $args['title_left'] || $args['title_right'] ) { 3431 $th_or_td_left = empty( $args['title_left'] ) ? 'td' : 'th'; 3432 $th_or_td_right = empty( $args['title_right'] ) ? 'td' : 'th'; 3433 3434 $r .= "<tr class='diff-sub-title'>\n"; 3435 $r .= "\t<$th_or_td_left>$args[title_left]</$th_or_td_left>\n"; 3436 if ( $is_split_view ) { 3437 $r .= "\t<$th_or_td_right>$args[title_right]</$th_or_td_right>\n"; 3438 } 3439 $r .= "</tr>\n"; 3440 } 3441 3442 if ( $args['title_left'] || $args['title_right'] ) { 3443 $r .= "</thead>\n"; 3444 } 3445 3446 $r .= "<tbody>\n$diff\n</tbody>\n"; 3447 $r .= '</table>'; 3448 3449 return $r; 3450 } 3451 endif;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Aug 26 08:20:24 2026 | Cross-referenced by PHPXref |