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