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