/wp-includes/pluggable.php
PHP | 2833 lines | 1895 code | 201 blank | 737 comment | 184 complexity | bf8a90823b313325120795eb6445d7af MD5 | raw file
Possible License(s): 0BSD
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * These functions can be replaced via plugins. If plugins do not redefine these
- * functions, then these will be used instead.
- *
- * @package WordPress
- */
- if ( ! function_exists( 'wp_set_current_user' ) ) :
- /**
- * Changes the current user by ID or name.
- *
- * Set $id to null and specify a name if you do not know a user's ID.
- *
- * Some WordPress functionality is based on the current user and not based on
- * the signed in user. Therefore, it opens the ability to edit and perform
- * actions on users who aren't signed in.
- *
- * @since 2.0.3
- * @global WP_User $current_user The current user object which holds the user data.
- *
- * @param int $id User ID
- * @param string $name User's username
- * @return WP_User Current user User object
- */
- function wp_set_current_user( $id, $name = '' ) {
- global $current_user;
- // If `$id` matches the current user, there is nothing to do.
- if ( isset( $current_user )
- && ( $current_user instanceof WP_User )
- && ( $id == $current_user->ID )
- && ( null !== $id )
- ) {
- return $current_user;
- }
- $current_user = new WP_User( $id, $name );
- setup_userdata( $current_user->ID );
- /**
- * Fires after the current user is set.
- *
- * @since 2.0.1
- */
- do_action( 'set_current_user' );
- return $current_user;
- }
- endif;
- if ( ! function_exists( 'wp_get_current_user' ) ) :
- /**
- * Retrieve the current user object.
- *
- * Will set the current user, if the current user is not set. The current user
- * will be set to the logged-in person. If no user is logged-in, then it will
- * set the current user to 0, which is invalid and won't have any permissions.
- *
- * @since 2.0.3
- *
- * @see _wp_get_current_user()
- * @global WP_User $current_user Checks if the current user is set.
- *
- * @return WP_User Current WP_User instance.
- */
- function wp_get_current_user() {
- return _wp_get_current_user();
- }
- endif;
- if ( ! function_exists( 'get_userdata' ) ) :
- /**
- * Retrieve user info by user ID.
- *
- * @since 0.71
- *
- * @param int $user_id User ID
- * @return WP_User|false WP_User object on success, false on failure.
- */
- function get_userdata( $user_id ) {
- return get_user_by( 'id', $user_id );
- }
- endif;
- if ( ! function_exists( 'get_user_by' ) ) :
- /**
- * Retrieve user info by a given field
- *
- * @since 2.8.0
- * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
- *
- * @param string $field The field to retrieve the user with. id | ID | slug | email | login.
- * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
- * @return WP_User|false WP_User object on success, false on failure.
- */
- function get_user_by( $field, $value ) {
- $userdata = WP_User::get_data_by( $field, $value );
- if ( ! $userdata ) {
- return false;
- }
- $user = new WP_User;
- $user->init( $userdata );
- return $user;
- }
- endif;
- if ( ! function_exists( 'cache_users' ) ) :
- /**
- * Retrieve info for user lists to prevent multiple queries by get_userdata()
- *
- * @since 3.0.0
- *
- * @global wpdb $wpdb WordPress database abstraction object.
- *
- * @param array $user_ids User ID numbers list
- */
- function cache_users( $user_ids ) {
- global $wpdb;
- $clean = _get_non_cached_ids( $user_ids, 'users' );
- if ( empty( $clean ) ) {
- return;
- }
- $list = implode( ',', $clean );
- $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" );
- $ids = array();
- foreach ( $users as $user ) {
- update_user_caches( $user );
- $ids[] = $user->ID;
- }
- update_meta_cache( 'user', $ids );
- }
- endif;
- if ( ! function_exists( 'wp_mail' ) ) :
- /**
- * Sends an email, similar to PHP's mail function.
- *
- * A true return value does not automatically mean that the user received the
- * email successfully. It just only means that the method used was able to
- * process the request without any errors.
- *
- * The default content type is `text/plain` which does not allow using HTML.
- * However, you can set the content type of the email by using the
- * {@see 'wp_mail_content_type'} filter.
- *
- * The default charset is based on the charset used on the blog. The charset can
- * be set using the {@see 'wp_mail_charset'} filter.
- *
- * @since 1.2.1
- *
- * @global PHPMailer $phpmailer
- *
- * @param string|array $to Array or comma-separated list of email addresses to send message.
- * @param string $subject Email subject
- * @param string $message Message contents
- * @param string|array $headers Optional. Additional headers.
- * @param string|array $attachments Optional. Files to attach.
- * @return bool Whether the email contents were sent successfully.
- */
- function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
- // Compact the input, apply the filters, and extract them back out.
- /**
- * Filters the wp_mail() arguments.
- *
- * @since 2.2.0
- *
- * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
- * subject, message, headers, and attachments values.
- */
- $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
- if ( isset( $atts['to'] ) ) {
- $to = $atts['to'];
- }
- if ( ! is_array( $to ) ) {
- $to = explode( ',', $to );
- }
- if ( isset( $atts['subject'] ) ) {
- $subject = $atts['subject'];
- }
- if ( isset( $atts['message'] ) ) {
- $message = $atts['message'];
- }
- if ( isset( $atts['headers'] ) ) {
- $headers = $atts['headers'];
- }
- if ( isset( $atts['attachments'] ) ) {
- $attachments = $atts['attachments'];
- }
- if ( ! is_array( $attachments ) ) {
- $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
- }
- global $phpmailer;
- // (Re)create it, if it's gone missing.
- if ( ! ( $phpmailer instanceof PHPMailer ) ) {
- require_once ABSPATH . WPINC . '/class-phpmailer.php';
- require_once ABSPATH . WPINC . '/class-smtp.php';
- $phpmailer = new PHPMailer( true );
- }
- // Headers.
- $cc = array();
- $bcc = array();
- $reply_to = array();
- if ( empty( $headers ) ) {
- $headers = array();
- } else {
- if ( ! is_array( $headers ) ) {
- // Explode the headers out, so this function can take
- // both string headers and an array of headers.
- $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
- } else {
- $tempheaders = $headers;
- }
- $headers = array();
- // If it's actually got contents.
- if ( ! empty( $tempheaders ) ) {
- // Iterate through the raw headers.
- foreach ( (array) $tempheaders as $header ) {
- if ( strpos( $header, ':' ) === false ) {
- if ( false !== stripos( $header, 'boundary=' ) ) {
- $parts = preg_split( '/boundary=/i', trim( $header ) );
- $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
- }
- continue;
- }
- // Explode them out.
- list( $name, $content ) = explode( ':', trim( $header ), 2 );
- // Cleanup crew.
- $name = trim( $name );
- $content = trim( $content );
- switch ( strtolower( $name ) ) {
- // Mainly for legacy -- process a "From:" header if it's there.
- case 'from':
- $bracket_pos = strpos( $content, '<' );
- if ( false !== $bracket_pos ) {
- // Text before the bracketed email is the "From" name.
- if ( $bracket_pos > 0 ) {
- $from_name = substr( $content, 0, $bracket_pos - 1 );
- $from_name = str_replace( '"', '', $from_name );
- $from_name = trim( $from_name );
- }
- $from_email = substr( $content, $bracket_pos + 1 );
- $from_email = str_replace( '>', '', $from_email );
- $from_email = trim( $from_email );
- // Avoid setting an empty $from_email.
- } elseif ( '' !== trim( $content ) ) {
- $from_email = trim( $content );
- }
- break;
- case 'content-type':
- if ( strpos( $content, ';' ) !== false ) {
- list( $type, $charset_content ) = explode( ';', $content );
- $content_type = trim( $type );
- if ( false !== stripos( $charset_content, 'charset=' ) ) {
- $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
- } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
- $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
- $charset = '';
- }
- // Avoid setting an empty $content_type.
- } elseif ( '' !== trim( $content ) ) {
- $content_type = trim( $content );
- }
- break;
- case 'cc':
- $cc = array_merge( (array) $cc, explode( ',', $content ) );
- break;
- case 'bcc':
- $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
- break;
- case 'reply-to':
- $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
- break;
- default:
- // Add it to our grand headers array.
- $headers[ trim( $name ) ] = trim( $content );
- break;
- }
- }
- }
- }
- // Empty out the values that may be set.
- $phpmailer->clearAllRecipients();
- $phpmailer->clearAttachments();
- $phpmailer->clearCustomHeaders();
- $phpmailer->clearReplyTos();
- // Set "From" name and email.
- // If we don't have a name from the input headers.
- if ( ! isset( $from_name ) ) {
- $from_name = 'WordPress';
- }
- /*
- * If we don't have an email from the input headers, default to wordpress@$sitename
- * Some hosts will block outgoing mail from this address if it doesn't exist,
- * but there's no easy alternative. Defaulting to admin_email might appear to be
- * another option, but some hosts may refuse to relay mail from an unknown domain.
- * See https://core.trac.wordpress.org/ticket/5007.
- */
- if ( ! isset( $from_email ) ) {
- // Get the site domain and get rid of www.
- $sitename = strtolower( $_SERVER['SERVER_NAME'] );
- if ( substr( $sitename, 0, 4 ) == 'www.' ) {
- $sitename = substr( $sitename, 4 );
- }
- $from_email = 'wordpress@' . $sitename;
- }
- /**
- * Filters the email address to send from.
- *
- * @since 2.2.0
- *
- * @param string $from_email Email address to send from.
- */
- $from_email = apply_filters( 'wp_mail_from', $from_email );
- /**
- * Filters the name to associate with the "from" email address.
- *
- * @since 2.3.0
- *
- * @param string $from_name Name associated with the "from" email address.
- */
- $from_name = apply_filters( 'wp_mail_from_name', $from_name );
- try {
- $phpmailer->setFrom( $from_email, $from_name, false );
- } catch ( phpmailerException $e ) {
- $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
- $mail_error_data['phpmailer_exception_code'] = $e->getCode();
- /** This filter is documented in wp-includes/pluggable.php */
- do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
- return false;
- }
- // Set mail's subject and body.
- $phpmailer->Subject = $subject;
- $phpmailer->Body = $message;
- // Set destination addresses, using appropriate methods for handling addresses.
- $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' );
- foreach ( $address_headers as $address_header => $addresses ) {
- if ( empty( $addresses ) ) {
- continue;
- }
- foreach ( (array) $addresses as $address ) {
- try {
- // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>".
- $recipient_name = '';
- if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) {
- if ( count( $matches ) == 3 ) {
- $recipient_name = $matches[1];
- $address = $matches[2];
- }
- }
- switch ( $address_header ) {
- case 'to':
- $phpmailer->addAddress( $address, $recipient_name );
- break;
- case 'cc':
- $phpmailer->addCc( $address, $recipient_name );
- break;
- case 'bcc':
- $phpmailer->addBcc( $address, $recipient_name );
- break;
- case 'reply_to':
- $phpmailer->addReplyTo( $address, $recipient_name );
- break;
- }
- } catch ( phpmailerException $e ) {
- continue;
- }
- }
- }
- // Set to use PHP's mail().
- $phpmailer->isMail();
- // Set Content-Type and charset.
- // If we don't have a content-type from the input headers.
- if ( ! isset( $content_type ) ) {
- $content_type = 'text/plain';
- }
- /**
- * Filters the wp_mail() content type.
- *
- * @since 2.3.0
- *
- * @param string $content_type Default wp_mail() content type.
- */
- $content_type = apply_filters( 'wp_mail_content_type', $content_type );
- $phpmailer->ContentType = $content_type;
- // Set whether it's plaintext, depending on $content_type.
- if ( 'text/html' == $content_type ) {
- $phpmailer->isHTML( true );
- }
- // If we don't have a charset from the input headers.
- if ( ! isset( $charset ) ) {
- $charset = get_bloginfo( 'charset' );
- }
- /**
- * Filters the default wp_mail() charset.
- *
- * @since 2.3.0
- *
- * @param string $charset Default email charset.
- */
- $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
- // Set custom headers.
- if ( ! empty( $headers ) ) {
- foreach ( (array) $headers as $name => $content ) {
- // Only add custom headers not added automatically by PHPMailer.
- if ( ! in_array( $name, array( 'MIME-Version', 'X-Mailer' ), true ) ) {
- $phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
- }
- }
- if ( false !== stripos( $content_type, 'multipart' ) && ! empty( $boundary ) ) {
- $phpmailer->addCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
- }
- }
- if ( ! empty( $attachments ) ) {
- foreach ( $attachments as $attachment ) {
- try {
- $phpmailer->addAttachment( $attachment );
- } catch ( phpmailerException $e ) {
- continue;
- }
- }
- }
- /**
- * Fires after PHPMailer is initialized.
- *
- * @since 2.2.0
- *
- * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
- */
- do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
- // Send!
- try {
- return $phpmailer->send();
- } catch ( phpmailerException $e ) {
- $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
- $mail_error_data['phpmailer_exception_code'] = $e->getCode();
- /**
- * Fires after a phpmailerException is caught.
- *
- * @since 4.4.0
- *
- * @param WP_Error $error A WP_Error object with the phpmailerException message, and an array
- * containing the mail recipient, subject, message, headers, and attachments.
- */
- do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
- return false;
- }
- }
- endif;
- if ( ! function_exists( 'wp_authenticate' ) ) :
- /**
- * Authenticate a user, confirming the login credentials are valid.
- *
- * @since 2.5.0
- * @since 4.5.0 `$username` now accepts an email address.
- *
- * @param string $username User's username or email address.
- * @param string $password User's password.
- * @return WP_User|WP_Error WP_User object if the credentials are valid,
- * otherwise WP_Error.
- */
- function wp_authenticate( $username, $password ) {
- $username = sanitize_user( $username );
- $password = trim( $password );
- /**
- * Filters whether a set of user login credentials are valid.
- *
- * A WP_User object is returned if the credentials authenticate a user.
- * WP_Error or null otherwise.
- *
- * @since 2.8.0
- * @since 4.5.0 `$username` now accepts an email address.
- *
- * @param null|WP_User|WP_Error $user WP_User if the user is authenticated.
- * WP_Error or null otherwise.
- * @param string $username Username or email address.
- * @param string $password User password
- */
- $user = apply_filters( 'authenticate', null, $username, $password );
- if ( null == $user ) {
- // TODO: What should the error message be? (Or would these even happen?)
- // Only needed if all authentication handlers fail to return anything.
- $user = new WP_Error( 'authentication_failed', __( '<strong>Error</strong>: Invalid username, email address or incorrect password.' ) );
- }
- $ignore_codes = array( 'empty_username', 'empty_password' );
- if ( is_wp_error( $user ) && ! in_array( $user->get_error_code(), $ignore_codes, true ) ) {
- $error = $user;
- /**
- * Fires after a user login has failed.
- *
- * @since 2.5.0
- * @since 4.5.0 The value of `$username` can now be an email address.
- * @since 5.4.0 The `$error` parameter was added.
- *
- * @param string $username Username or email address.
- * @param WP_Error $error A WP_Error object with the authentication failure details.
- */
- do_action( 'wp_login_failed', $username, $error );
- }
- return $user;
- }
- endif;
- if ( ! function_exists( 'wp_logout' ) ) :
- /**
- * Log the current user out.
- *
- * @since 2.5.0
- */
- function wp_logout() {
- $user_id = get_current_user_id();
- wp_destroy_current_session();
- wp_clear_auth_cookie();
- wp_set_current_user( 0 );
- /**
- * Fires after a user is logged out.
- *
- * @since 1.5.0
- * @since 5.5.0 Added the `$user_id` parameter.
- *
- * @param int $user_id ID of the user that was logged out.
- */
- do_action( 'wp_logout', $user_id );
- }
- endif;
- if ( ! function_exists( 'wp_validate_auth_cookie' ) ) :
- /**
- * Validates authentication cookie.
- *
- * The checks include making sure that the authentication cookie is set and
- * pulling in the contents (if $cookie is not used).
- *
- * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
- * should be and compares the two.
- *
- * @since 2.5.0
- *
- * @global int $login_grace_period
- *
- * @param string $cookie Optional. If used, will validate contents instead of cookie's.
- * @param string $scheme Optional. The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'.
- * @return int|false User ID if valid cookie, false if invalid.
- */
- function wp_validate_auth_cookie( $cookie = '', $scheme = '' ) {
- $cookie_elements = wp_parse_auth_cookie( $cookie, $scheme );
- if ( ! $cookie_elements ) {
- /**
- * Fires if an authentication cookie is malformed.
- *
- * @since 2.7.0
- *
- * @param string $cookie Malformed auth cookie.
- * @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth',
- * or 'logged_in'.
- */
- do_action( 'auth_cookie_malformed', $cookie, $scheme );
- return false;
- }
- $scheme = $cookie_elements['scheme'];
- $username = $cookie_elements['username'];
- $hmac = $cookie_elements['hmac'];
- $token = $cookie_elements['token'];
- $expired = $cookie_elements['expiration'];
- $expiration = $cookie_elements['expiration'];
- // Allow a grace period for POST and Ajax requests.
- if ( wp_doing_ajax() || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
- $expired += HOUR_IN_SECONDS;
- }
- // Quick check to see if an honest cookie has expired.
- if ( $expired < time() ) {
- /**
- * Fires once an authentication cookie has expired.
- *
- * @since 2.7.0
- *
- * @param string[] $cookie_elements An array of data for the authentication cookie.
- */
- do_action( 'auth_cookie_expired', $cookie_elements );
- return false;
- }
- $user = get_user_by( 'login', $username );
- if ( ! $user ) {
- /**
- * Fires if a bad username is entered in the user authentication process.
- *
- * @since 2.7.0
- *
- * @param string[] $cookie_elements An array of data for the authentication cookie.
- */
- do_action( 'auth_cookie_bad_username', $cookie_elements );
- return false;
- }
- $pass_frag = substr( $user->user_pass, 8, 4 );
- $key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
- // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
- $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
- $hash = hash_hmac( $algo, $username . '|' . $expiration . '|' . $token, $key );
- if ( ! hash_equals( $hash, $hmac ) ) {
- /**
- * Fires if a bad authentication cookie hash is encountered.
- *
- * @since 2.7.0
- *
- * @param string[] $cookie_elements An array of data for the authentication cookie.
- */
- do_action( 'auth_cookie_bad_hash', $cookie_elements );
- return false;
- }
- $manager = WP_Session_Tokens::get_instance( $user->ID );
- if ( ! $manager->verify( $token ) ) {
- /**
- * Fires if a bad session token is encountered.
- *
- * @since 4.0.0
- *
- * @param string[] $cookie_elements An array of data for the authentication cookie.
- */
- do_action( 'auth_cookie_bad_session_token', $cookie_elements );
- return false;
- }
- // Ajax/POST grace period set above.
- if ( $expiration < time() ) {
- $GLOBALS['login_grace_period'] = 1;
- }
- /**
- * Fires once an authentication cookie has been validated.
- *
- * @since 2.7.0
- *
- * @param string[] $cookie_elements An array of data for the authentication cookie.
- * @param WP_User $user User object.
- */
- do_action( 'auth_cookie_valid', $cookie_elements, $user );
- return $user->ID;
- }
- endif;
- if ( ! function_exists( 'wp_generate_auth_cookie' ) ) :
- /**
- * Generates authentication cookie contents.
- *
- * @since 2.5.0
- * @since 4.0.0 The `$token` parameter was added.
- *
- * @param int $user_id User ID.
- * @param int $expiration The time the cookie expires as a UNIX timestamp.
- * @param string $scheme Optional. The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'.
- * Default 'auth'.
- * @param string $token User's session token to use for this cookie.
- * @return string Authentication cookie contents. Empty string if user does not exist.
- */
- function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) {
- $user = get_userdata( $user_id );
- if ( ! $user ) {
- return '';
- }
- if ( ! $token ) {
- $manager = WP_Session_Tokens::get_instance( $user_id );
- $token = $manager->create( $expiration );
- }
- $pass_frag = substr( $user->user_pass, 8, 4 );
- $key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
- // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
- $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
- $hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key );
- $cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash;
- /**
- * Filters the authentication cookie.
- *
- * @since 2.5.0
- * @since 4.0.0 The `$token` parameter was added.
- *
- * @param string $cookie Authentication cookie.
- * @param int $user_id User ID.
- * @param int $expiration The time the cookie expires as a UNIX timestamp.
- * @param string $scheme Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'.
- * @param string $token User's session token used.
- */
- return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );
- }
- endif;
- if ( ! function_exists( 'wp_parse_auth_cookie' ) ) :
- /**
- * Parses a cookie into its components.
- *
- * @since 2.7.0
- *
- * @param string $cookie Authentication cookie.
- * @param string $scheme Optional. The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'.
- * @return string[]|false Authentication cookie components.
- */
- function wp_parse_auth_cookie( $cookie = '', $scheme = '' ) {
- if ( empty( $cookie ) ) {
- switch ( $scheme ) {
- case 'auth':
- $cookie_name = AUTH_COOKIE;
- break;
- case 'secure_auth':
- $cookie_name = SECURE_AUTH_COOKIE;
- break;
- case 'logged_in':
- $cookie_name = LOGGED_IN_COOKIE;
- break;
- default:
- if ( is_ssl() ) {
- $cookie_name = SECURE_AUTH_COOKIE;
- $scheme = 'secure_auth';
- } else {
- $cookie_name = AUTH_COOKIE;
- $scheme = 'auth';
- }
- }
- if ( empty( $_COOKIE[ $cookie_name ] ) ) {
- return false;
- }
- $cookie = $_COOKIE[ $cookie_name ];
- }
- $cookie_elements = explode( '|', $cookie );
- if ( count( $cookie_elements ) !== 4 ) {
- return false;
- }
- list( $username, $expiration, $token, $hmac ) = $cookie_elements;
- return compact( 'username', 'expiration', 'token', 'hmac', 'scheme' );
- }
- endif;
- if ( ! function_exists( 'wp_set_auth_cookie' ) ) :
- /**
- * Sets the authentication cookies based on user ID.
- *
- * The $remember parameter increases the time that the cookie will be kept. The
- * default the cookie is kept without remembering is two days. When $remember is
- * set, the cookies will be kept for 14 days or two weeks.
- *
- * @since 2.5.0
- * @since 4.3.0 Added the `$token` parameter.
- *
- * @param int $user_id User ID.
- * @param bool $remember Whether to remember the user.
- * @param bool|string $secure Whether the auth cookie should only be sent over HTTPS. Default is an empty
- * string which means the value of `is_ssl()` will be used.
- * @param string $token Optional. User's session token to use for this cookie.
- */
- function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) {
- if ( $remember ) {
- /**
- * Filters the duration of the authentication cookie expiration period.
- *
- * @since 2.8.0
- *
- * @param int $length Duration of the expiration period in seconds.
- * @param int $user_id User ID.
- * @param bool $remember Whether to remember the user login. Default false.
- */
- $expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember );
- /*
- * Ensure the browser will continue to send the cookie after the expiration time is reached.
- * Needed for the login grace period in wp_validate_auth_cookie().
- */
- $expire = $expiration + ( 12 * HOUR_IN_SECONDS );
- } else {
- /** This filter is documented in wp-includes/pluggable.php */
- $expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember );
- $expire = 0;
- }
- if ( '' === $secure ) {
- $secure = is_ssl();
- }
- // Front-end cookie is secure when the auth cookie is secure and the site's home URL uses HTTPS.
- $secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME );
- /**
- * Filters whether the auth cookie should only be sent over HTTPS.
- *
- * @since 3.1.0
- *
- * @param bool $secure Whether the cookie should only be sent over HTTPS.
- * @param int $user_id User ID.
- */
- $secure = apply_filters( 'secure_auth_cookie', $secure, $user_id );
- /**
- * Filters whether the logged in cookie should only be sent over HTTPS.
- *
- * @since 3.1.0
- *
- * @param bool $secure_logged_in_cookie Whether the logged in cookie should only be sent over HTTPS.
- * @param int $user_id User ID.
- * @param bool $secure Whether the auth cookie should only be sent over HTTPS.
- */
- $secure_logged_in_cookie = apply_filters( 'secure_logged_in_cookie', $secure_logged_in_cookie, $user_id, $secure );
- if ( $secure ) {
- $auth_cookie_name = SECURE_AUTH_COOKIE;
- $scheme = 'secure_auth';
- } else {
- $auth_cookie_name = AUTH_COOKIE;
- $scheme = 'auth';
- }
- if ( '' === $token ) {
- $manager = WP_Session_Tokens::get_instance( $user_id );
- $token = $manager->create( $expiration );
- }
- $auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
- $logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );
- /**
- * Fires immediately before the authentication cookie is set.
- *
- * @since 2.5.0
- * @since 4.9.0 The `$token` parameter was added.
- *
- * @param string $auth_cookie Authentication cookie value.
- * @param int $expire The time the login grace period expires as a UNIX timestamp.
- * Default is 12 hours past the cookie's expiration time.
- * @param int $expiration The time when the authentication cookie expires as a UNIX timestamp.
- * Default is 14 days from now.
- * @param int $user_id User ID.
- * @param string $scheme Authentication scheme. Values include 'auth' or 'secure_auth'.
- * @param string $token User's session token to use for this cookie.
- */
- do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme, $token );
- /**
- * Fires immediately before the logged-in authentication cookie is set.
- *
- * @since 2.6.0
- * @since 4.9.0 The `$token` parameter was added.
- *
- * @param string $logged_in_cookie The logged-in cookie value.
- * @param int $expire The time the login grace period expires as a UNIX timestamp.
- * Default is 12 hours past the cookie's expiration time.
- * @param int $expiration The time when the logged-in authentication cookie expires as a UNIX timestamp.
- * Default is 14 days from now.
- * @param int $user_id User ID.
- * @param string $scheme Authentication scheme. Default 'logged_in'.
- * @param string $token User's session token to use for this cookie.
- */
- do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in', $token );
- /**
- * Allows preventing auth cookies from actually being sent to the client.
- *
- * @since 4.7.4
- *
- * @param bool $send Whether to send auth cookies to the client.
- */
- if ( ! apply_filters( 'send_auth_cookies', true ) ) {
- return;
- }
- setcookie( $auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true );
- setcookie( $auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true );
- setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true );
- if ( COOKIEPATH != SITECOOKIEPATH ) {
- setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true );
- }
- }
- endif;
- if ( ! function_exists( 'wp_clear_auth_cookie' ) ) :
- /**
- * Removes all of the cookies associated with authentication.
- *
- * @since 2.5.0
- */
- function wp_clear_auth_cookie() {
- /**
- * Fires just before the authentication cookies are cleared.
- *
- * @since 2.7.0
- */
- do_action( 'clear_auth_cookie' );
- /** This filter is documented in wp-includes/pluggable.php */
- if ( ! apply_filters( 'send_auth_cookies', true ) ) {
- return;
- }
- // Auth cookies.
- setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN );
- setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN );
- setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
- setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
- setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
- setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
- // Settings cookies.
- setcookie( 'wp-settings-' . get_current_user_id(), ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
- setcookie( 'wp-settings-time-' . get_current_user_id(), ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
- // Old cookies.
- setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
- setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
- setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
- setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
- // Even older cookies.
- setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
- setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
- setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
- setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
- // Post password cookie.
- setcookie( 'wp-postpass_' . COOKIEHASH, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
- }
- endif;
- if ( ! function_exists( 'is_user_logged_in' ) ) :
- /**
- * Determines whether the current visitor is a logged in user.
- *
- * For more information on this and similar theme functions, check out
- * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
- * Conditional Tags} article in the Theme Developer Handbook.
- *
- * @since 2.0.0
- *
- * @return bool True if user is logged in, false if not logged in.
- */
- function is_user_logged_in() {
- $user = wp_get_current_user();
- return $user->exists();
- }
- endif;
- if ( ! function_exists( 'auth_redirect' ) ) :
- /**
- * Checks if a user is logged in, if not it redirects them to the login page.
- *
- * When this code is called from a page, it checks to see if the user viewing the page is logged in.
- * If the user is not logged in, they are redirected to the login page. The user is redirected
- * in such a way that, upon logging in, they will be sent directly to the page they were originally
- * trying to access.
- *
- * @since 1.5.0
- */
- function auth_redirect() {
- $secure = ( is_ssl() || force_ssl_admin() );
- /**
- * Filters whether to use a secure authentication redirect.
- *
- * @since 3.1.0
- *
- * @param bool $secure Whether to use a secure authentication redirect. Default false.
- */
- $secure = apply_filters( 'secure_auth_redirect', $secure );
- // If https is required and request is http, redirect.
- if ( $secure && ! is_ssl() && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
- if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
- wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
- exit();
- } else {
- wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
- exit();
- }
- }
- /**
- * Filters the authentication redirect scheme.
- *
- * @since 2.9.0
- *
- * @param string $scheme Authentication redirect scheme. Default empty.
- */
- $scheme = apply_filters( 'auth_redirect_scheme', '' );
- $user_id = wp_validate_auth_cookie( '', $scheme );
- if ( $user_id ) {
- /**
- * Fires before the authentication redirect.
- *
- * @since 2.8.0
- *
- * @param int $user_id User ID.
- */
- do_action( 'auth_redirect', $user_id );
- // If the user wants ssl but the session is not ssl, redirect.
- if ( ! $secure && get_user_option( 'use_ssl', $user_id ) && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
- if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
- wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
- exit();
- } else {
- wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
- exit();
- }
- }
- return; // The cookie is good, so we're done.
- }
- // The cookie is no good, so force login.
- nocache_headers();
- $redirect = ( strpos( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
- $login_url = wp_login_url( $redirect, true );
- wp_redirect( $login_url );
- exit();
- }
- endif;
- if ( ! function_exists( 'check_admin_referer' ) ) :
- /**
- * Ensures intent by verifying that a user was referred from another admin page with the correct security nonce.
- *
- * This function ensures the user intends to perform a given action, which helps protect against clickjacking style
- * attacks. It verifies intent, not authorisation, therefore it does not verify the user's capabilities. This should
- * be performed with `current_user_can()` or similar.
- *
- * If the nonce value is invalid, the function will exit with an "Are You Sure?" style message.
- *
- * @since 1.2.0
- * @since 2.5.0 The `$query_arg` parameter was added.
- *
- * @param int|string $action The nonce action.
- * @param string $query_arg Optional. Key to check for nonce in `$_REQUEST`. Default '_wpnonce'.
- * @return int|false 1 if the nonce is valid and generated between 0-12 hours ago,
- * 2 if the nonce is valid and generated between 12-24 hours ago.
- * False if the nonce is invalid.
- */
- function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) {
- if ( -1 === $action ) {
- _doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2.0' );
- }
- $adminurl = strtolower( admin_url() );
- $referer = strtolower( wp_get_referer() );
- $result = isset( $_REQUEST[ $query_arg ] ) ? wp_verify_nonce( $_REQUEST[ $query_arg ], $action ) : false;
- /**
- * Fires once the admin request has been validated or not.
- *
- * @since 1.5.1
- *
- * @param string $action The nonce action.
- * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
- * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
- */
- do_action( 'check_admin_referer', $action, $result );
- if ( ! $result && ! ( -1 === $action && strpos( $referer, $adminurl ) === 0 ) ) {
- wp_nonce_ays( $action );
- die();
- }
- return $result;
- }
- endif;
- if ( ! function_exists( 'check_ajax_referer' ) ) :
- /**
- * Verifies the Ajax request to prevent processing requests external of the blog.
- *
- * @since 2.0.3
- *
- * @param int|string $action Action nonce.
- * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false,
- * `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce'
- * (in that order). Default false.
- * @param bool $die Optional. Whether to die early when the nonce cannot be verified.
- * Default true.
- * @return int|false 1 if the nonce is valid and generated between 0-12 hours ago,
- * 2 if the nonce is valid and generated between 12-24 hours ago.
- * False if the nonce is invalid.
- */
- function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
- if ( -1 == $action ) {
- _doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '4.7' );
- }
- $nonce = '';
- if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) ) {
- $nonce = $_REQUEST[ $query_arg ];
- } elseif ( isset( $_REQUEST['_ajax_nonce'] ) ) {
- $nonce = $_REQUEST['_ajax_nonce'];
- } elseif ( isset( $_REQUEST['_wpnonce'] ) ) {
- $nonce = $_REQUEST['_wpnonce'];
- }
- $result = wp_verify_nonce( $nonce, $action );
- /**
- * Fires once the Ajax request has been validated or not.
- *
- * @since 2.1.0
- *
- * @param string $action The Ajax nonce action.
- * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
- * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
- */
- do_action( 'check_ajax_referer', $action, $result );
- if ( $die && false === $result ) {
- if ( wp_doing_ajax() ) {
- wp_die( -1, 403 );
- } else {
- die( '-1' );
- }
- }
- return $result;
- }
- endif;
- if ( ! function_exists( 'wp_redirect' ) ) :
- /**
- * Redirects to another page.
- *
- * Note: wp_redirect() does not exit automatically, and should almost always be
- * followed by a call to `exit;`:
- *
- * wp_redirect( $url );
- * exit;
- *
- * Exiting can also be selectively manipulated by using wp_redirect() as a conditional
- * in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_location'} filters:
- *
- * if ( wp_redirect( $url ) ) {
- * exit;
- * }
- *
- * @since 1.5.1
- * @since 5.1.0 The `$x_redirect_by` parameter was added.
- * @since 5.4.0 On invalid status codes, wp_die() is called.
- *
- * @global bool $is_IIS
- *
- * @param string $location The path or URL to redirect to.
- * @param int $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily).
- * @param string $x_redirect_by Optional. The application doing the redirect. Default 'WordPress'.
- * @return bool False if the redirect was cancelled, true otherwise.
- */
- function wp_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) {
- global $is_IIS;
- /**
- * Filters the redirect location.
- *
- * @since 2.1.0
- *
- * @param string $location The path or URL to redirect to.
- * @param int $status The HTTP response status code to use.
- */
- $location = apply_filters( 'wp_redirect', $location, $status );
- /**
- * Filters the redirect HTTP response status code to use.
- *
- * @since 2.3.0
- *
- * @param int $status The HTTP response status code to use.
- * @param string $location The path or URL to redirect to.
- */
- $status = apply_filters( 'wp_redirect_status', $status, $location );
- if ( ! $location ) {
- return false;
- }
- if ( $status < 300 || 399 < $status ) {
- wp_die( __( 'HTTP redirect status code must be a redirection code, 3xx.' ) );
- }
- $location = wp_sanitize_redirect( $location );
- if ( ! $is_IIS && PHP_SAPI != 'cgi-fcgi' ) {
- status_header( $status ); // This causes problems on IIS and some FastCGI setups.
- }
- /**
- * Filters the X-Redirect-By header.
- *
- * Allows applications to identify themselves when they're doing a redirect.
- *
- * @since 5.1.0
- *
- * @param string $x_redirect_by The application doing the redirect.
- * @param int $status Status code to use.
- * @param string $location The path to redirect to.
- */
- $x_redirect_by = apply_filters( 'x_redirect_by', $x_redirect_by, $status, $location );
- if ( is_string( $x_redirect_by ) ) {
- header( "X-Redirect-By: $x_redirect_by" );
- }
- header( "Location: $location", true, $status );
- return true;
- }
- endif;
- if ( ! function_exists( 'wp_sanitize_redirect' ) ) :
- /**
- * Sanitizes a URL for use in a redirect.
- *
- * @since 2.3.0
- *
- * @param string $location The path to redirect to.
- * @return string Redirect-sanitized URL.
- */
- function wp_sanitize_redirect( $location ) {
- // Encode spaces.
- $location = str_replace( ' ', '%20', $location );
- $regex = '/
- (
- (?: [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
- | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
- | [\xE1-\xEC][\x80-\xBF]{2}
- | \xED[\x80-\x9F][\x80-\xBF]
- | [\xEE-\xEF][\x80-\xBF]{2}
- | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
- | [\xF1-\xF3][\x80-\xBF]{3}
- | \xF4[\x80-\x8F][\x80-\xBF]{2}
- ){1,40} # ...one or more times
- )/x';
- $location = preg_replace_callback( $regex, '_wp_sanitize_utf8_in_redirect', $location );
- $location = preg_replace( '|[^a-z0-9-~+_.?#=&;,/:%!*\[\]()@]|i', '', $location );
- $location = wp_kses_no_null( $location );
- // Remove %0D and %0A from location.
- $strip = array( '%0d', '%0a', '%0D', '%0A' );
- return _deep_replace( $strip, $location );
- }
- /**
- * URL encode UTF-8 characters in a URL.
- *
- * @ignore
- * @since 4.2.0
- * @access private
- *
- * @see wp_sanitize_redirect()
- *
- * @param array $matches RegEx matches against the redirect location.
- * @return string URL-encoded version of the first RegEx match.
- */
- function _wp_sanitize_utf8_in_redirect( $matches ) {
- return urlencode( $matches[0] );
- }
- endif;
- if ( ! function_exists( 'wp_safe_redirect' ) ) :
- /**
- * Performs a safe (local) redirect, using wp_redirect().
- *
- * Checks whether the $location is using an allowed host, if it has an absolute
- * path. A plugin can therefore set or remove allowed host(s) to or from the
- * list.
- *
- * If the host is not allowed, then the redirect defaults to wp-admin on the siteurl
- * instead. This prevents malicious redirects which redirect to another host,
- * but only used in a few places.
- *
- * Note: wp_safe_redirect() does not exit automatically, and should almost always be
- * followed by a call to `exit;`:
- *
- * wp_safe_redirect( $url );
- * exit;
- *
- * Exiting can also be selectively manipulated by using wp_safe_redirect() as a conditional
- * in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_location'} filters:
- *
- * if ( wp_safe_redirect( $url ) ) {
- * exit;
- * }
- *
- * @since 2.3.0
- * @since 5.1.0 The return value from wp_redirect() is now passed on, and the `$x_redirect_by` parameter was added.
- *
- * @param string $location The path or URL to redirect to.
- * @param int $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily).
- * @param string $x_redirect_by Optional. The application doing the redirect. Default 'WordPress'.
- * @return bool $redirect False if the redirect was cancelled, true otherwise.
- */
- function wp_safe_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) {
- // Need to look at the URL the way it will end up in wp_redirect().
- $location = wp_sanitize_redirect( $location );
- /**
- * Filters the redirect fallback URL for when the provided redirect is not safe (local).
- *
- * @since 4.3.0
- *
- * @param string $fallback_url The fallback URL to use by default.
- * @param int $status The HTTP response status code to use.
- */
- $location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) );
- return wp_redirect( $location, $status, $x_redirect_by );
- }
- endif;
- if ( ! function_exists( 'wp_validate_redirect' ) ) :
- /**
- * Validates a URL for use in a redirect.
- *
- * Checks whether the $location is using an allowed host, if it has an absolute
- * path. A plugin can therefore set or remove allowed host(s) to or from the
- * list.
- *
- * If the host is not allowed, then the redirect is to $default supplied
- *
- * @since 2.8.1
- *
- * @param string $location The redirect to validate
- * @param string $default The value to return if $location is not allowed
- * @return string redirect-sanitized URL
- */
- function wp_validate_redirect( $location, $default = '' ) {
- $location = trim( $location, " \t\n\r\0\x08\x0B" );
- // Browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'.
- if ( substr( $location, 0, 2 ) == '//' ) {
- $location = 'http:' . $location;
- }
- // In PHP 5 parse_url() may fail if the URL query part contains 'http://'.
- // See https://bugs.php.net/bug.php?id=38143
- $cut = strpos( $location, '?' );
- $test = $cut ? substr( $location, 0, $cut ) : $location;
- $lp = parse_url( $test );
- // Give up if malformed URL.
- if ( false === $lp ) {
- return $default;
- }
- // Allow only 'http' and 'https' schemes. No 'data:', etc.
- if ( isset( $lp['scheme'] ) && ! ( 'http' == $lp['scheme'] || 'https' == $lp['scheme'] ) ) {
- return $default;
- }
- if ( ! isset( $lp['host'] ) && ! empty( $lp['path'] ) && '/' !== $lp['path'][0] ) {
- $path = '';
- if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
- $path = dirname( parse_url( 'http://placeholder' . $_SERVER['REQUEST_URI'], PHP_URL_PATH ) . '?' );
- $path = wp_normalize_path( $path );
- }
- $location = '/' . ltrim( $path . '/', '/' ) . $location;
- }
- // Reject if certain components are set but host is not.
- // This catches URLs like https:host.com for which parse_url() does not set the host field.
- if ( ! isset( $lp['host'] ) && ( isset( $lp['scheme'] ) || isset( $lp['user'] ) || isset( $lp['pass'] ) || isset( $lp['port'] ) ) ) {
- return $default;
- }
- // Reject malformed components parse_url() can return on odd inputs.
- foreach ( array( 'user', 'pass', 'host' ) as $component ) {
- if ( isset( $lp[ $component ] ) && strpbrk( $lp[ $component ], ':/?#@' ) ) {
- return $default;
- }
- }
- $wpp = parse_url( home_url() );
- /**
- * Filters the whitelist of hosts to redirect to.
- *
- * @since 2.3.0
- *
- * @param string[] $hosts An array of allowed host names.
- * @param string $host The host name of the redirect destination; empty string if not set.
- */
- $allowed_hosts = (array) apply_filters( 'allowed_redirect_hosts', array( $wpp['host'] ), isset( $lp['host'] ) ? $lp['host'] : '' );
- if ( isset( $lp['host'] ) && ( ! in_array( $lp['host'], $allowed_hosts, true ) && strtolower( $wpp['host'] ) !== $lp['host'] ) ) {
- $location = $default;
- }
- return $location;
- }
- endif;
- if ( ! function_exists( 'wp_notify_postauthor' ) ) :
- /**
- * Notify an author (and/or others) of a comment/trackback/pingback on a post.
- *
- * @since 1.0.0
- *
- * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
- * @param string $deprecated Not used
- * @return bool True on completion. False if no email addresses were specified.
- */
- function wp_notify_postauthor( $comment_id, $deprecated = null ) {
- if ( null !== $deprecated ) {
- _deprecated_argument( __FUNCTION__, '3.8.0' );
- }
- $comment = get_comment( $comment_id );
- if ( empty( $comment ) || empty( $comment->comment_post_ID ) ) {
- return false;
- }
- $post = get_post( $comment->comment_post_ID );
- $author = get_userdata( $post->post_author );
- // Who to notify? By default, just the post author, but others can be added.
- $emails = array();
- if ( $author ) {
- $emails[] = $author->user_email;
- }
- /**
- * Filters the list of …
Large files files are truncated, but you can click here to view the full file