diff options
Diffstat (limited to 'srcs/wordpress/wp-includes/pluggable.php')
| -rw-r--r-- | srcs/wordpress/wp-includes/pluggable.php | 2773 |
1 files changed, 2773 insertions, 0 deletions
diff --git a/srcs/wordpress/wp-includes/pluggable.php b/srcs/wordpress/wp-includes/pluggable.php new file mode 100644 index 0000000..fe6822d --- /dev/null +++ b/srcs/wordpress/wp-includes/pluggable.php @@ -0,0 +1,2773 @@ +<?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 ( $bracket_pos !== false ) { + // 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(); + + // From email and name + // 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' ); + } + + // Set the content-type and 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' ) ) ) { + $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 ( $user == null ) { + // 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 ) ) { + /** + * 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. + * + * @param string $username Username or email address. + */ + do_action( 'wp_login_failed', $username ); + } + + return $user; + } +endif; + +if ( ! function_exists( 'wp_logout' ) ) : + /** + * Log the current user out. + * + * @since 2.5.0 + */ + function wp_logout() { + wp_destroy_current_session(); + wp_clear_auth_cookie(); + wp_set_current_user( 0 ); + + /** + * Fires after a user is logged-out. + * + * @since 1.5.0 + */ + do_action( 'wp_logout' ); + } +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 false|int False if invalid cookie, user ID if valid. + */ + 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 array $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 array $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 array $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 ) ) { + 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 array $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 array|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 mixed $secure Whether the admin cookies should only be sent over HTTPS. + * Default is the value of is_ssl(). + * @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 is forced HTTPS. + $secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME ); + + /** + * Filters whether the connection is secure. + * + * @since 3.1.0 + * + * @param bool $secure Whether the connection is secure. + * @param int $user_id User ID. + */ + $secure = apply_filters( 'secure_auth_cookie', $secure, $user_id ); + + /** + * Filters whether to use a secure cookie when logged-in. + * + * @since 3.1.0 + * + * @param bool $secure_logged_in_cookie Whether to use a secure cookie when logged-in. + * @param int $user_id User ID. + * @param bool $secure Whether the connection is secure. + */ + $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() { + // Checks if a user is logged in, if not redirects them to the login page + + $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', $u |
