wp/wp-includes/pluggable.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    15  * Some WordPress functionality is based on the current user and not based on
    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
    16  * the signed in user. Therefore, it opens the ability to edit and perform
    17  * actions on users who aren't signed in.
    17  * actions on users who aren't signed in.
    18  *
    18  *
    19  * @since 2.0.3
    19  * @since 2.0.3
    20  * @global object $current_user The current user object which holds the user data.
    20  * @global WP_User $current_user The current user object which holds the user data.
    21  *
    21  *
    22  * @param int $id User ID
    22  * @param int    $id   User ID
    23  * @param string $name User's username
    23  * @param string $name User's username
    24  * @return WP_User Current user User object
    24  * @return WP_User Current user User object
    25  */
    25  */
    26 function wp_set_current_user($id, $name = '') {
    26 function wp_set_current_user($id, $name = '') {
    27 	global $current_user;
    27 	global $current_user;
    28 
    28 
    29 	if ( isset( $current_user ) && ( $current_user instanceof WP_User ) && ( $id == $current_user->ID ) )
    29 	// If `$id` matches the user who's already current, there's nothing to do.
       
    30 	if ( isset( $current_user )
       
    31 		&& ( $current_user instanceof WP_User )
       
    32 		&& ( $id == $current_user->ID )
       
    33 		&& ( null !== $id )
       
    34 	) {
    30 		return $current_user;
    35 		return $current_user;
       
    36 	}
    31 
    37 
    32 	$current_user = new WP_User( $id, $name );
    38 	$current_user = new WP_User( $id, $name );
    33 
    39 
    34 	setup_userdata( $current_user->ID );
    40 	setup_userdata( $current_user->ID );
    35 
    41 
    45 endif;
    51 endif;
    46 
    52 
    47 if ( !function_exists('wp_get_current_user') ) :
    53 if ( !function_exists('wp_get_current_user') ) :
    48 /**
    54 /**
    49  * Retrieve the current user object.
    55  * Retrieve the current user object.
    50  *
       
    51  * @since 2.0.3
       
    52  *
       
    53  * @return WP_User Current user WP_User object
       
    54  */
       
    55 function wp_get_current_user() {
       
    56 	global $current_user;
       
    57 
       
    58 	get_currentuserinfo();
       
    59 
       
    60 	return $current_user;
       
    61 }
       
    62 endif;
       
    63 
       
    64 if ( !function_exists('get_currentuserinfo') ) :
       
    65 /**
       
    66  * Populate global variables with information about the currently logged in user.
       
    67  *
    56  *
    68  * Will set the current user, if the current user is not set. The current user
    57  * Will set the current user, if the current user is not set. The current user
    69  * will be set to the logged-in person. If no user is logged-in, then it will
    58  * will be set to the logged-in person. If no user is logged-in, then it will
    70  * set the current user to 0, which is invalid and won't have any permissions.
    59  * set the current user to 0, which is invalid and won't have any permissions.
    71  *
    60  *
       
    61  * @since 2.0.3
       
    62  *
       
    63  * @see _wp_get_current_user()
       
    64  * @global WP_User $current_user Checks if the current user is set.
       
    65  *
       
    66  * @return WP_User Current WP_User instance.
       
    67  */
       
    68 function wp_get_current_user() {
       
    69 	return _wp_get_current_user();
       
    70 }
       
    71 endif;
       
    72 
       
    73 if ( !function_exists('get_userdata') ) :
       
    74 /**
       
    75  * Retrieve user info by user ID.
       
    76  *
    72  * @since 0.71
    77  * @since 0.71
    73  *
    78  *
    74  * @uses $current_user Checks if the current user is set
       
    75  *
       
    76  * @return null|false False on XML-RPC Request and invalid auth cookie. Null when current user set.
       
    77  */
       
    78 function get_currentuserinfo() {
       
    79 	global $current_user;
       
    80 
       
    81 	if ( ! empty( $current_user ) ) {
       
    82 		if ( $current_user instanceof WP_User )
       
    83 			return;
       
    84 
       
    85 		// Upgrade stdClass to WP_User
       
    86 		if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
       
    87 			$cur_id = $current_user->ID;
       
    88 			$current_user = null;
       
    89 			wp_set_current_user( $cur_id );
       
    90 			return;
       
    91 		}
       
    92 
       
    93 		// $current_user has a junk value. Force to WP_User with ID 0.
       
    94 		$current_user = null;
       
    95 		wp_set_current_user( 0 );
       
    96 		return false;
       
    97 	}
       
    98 
       
    99 	if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
       
   100 		wp_set_current_user( 0 );
       
   101 		return false;
       
   102 	}
       
   103 
       
   104 	/**
       
   105 	 * Filter the current user.
       
   106 	 *
       
   107 	 * The default filters use this to determine the current user from the
       
   108 	 * request's cookies, if available.
       
   109 	 *
       
   110 	 * Returning a value of false will effectively short-circuit setting
       
   111 	 * the current user.
       
   112 	 *
       
   113 	 * @since 3.9.0
       
   114 	 *
       
   115 	 * @param int|bool $user_id User ID if one has been determined, false otherwise.
       
   116 	 */
       
   117 	$user_id = apply_filters( 'determine_current_user', false );
       
   118 	if ( ! $user_id ) {
       
   119 		wp_set_current_user( 0 );
       
   120 		return false;
       
   121 	}
       
   122 
       
   123 	wp_set_current_user( $user_id );
       
   124 }
       
   125 endif;
       
   126 
       
   127 if ( !function_exists('get_userdata') ) :
       
   128 /**
       
   129  * Retrieve user info by user ID.
       
   130  *
       
   131  * @since 0.71
       
   132  *
       
   133  * @param int $user_id User ID
    79  * @param int $user_id User ID
   134  * @return WP_User|bool WP_User object on success, false on failure.
    80  * @return WP_User|false WP_User object on success, false on failure.
   135  */
    81  */
   136 function get_userdata( $user_id ) {
    82 function get_userdata( $user_id ) {
   137 	return get_user_by( 'id', $user_id );
    83 	return get_user_by( 'id', $user_id );
   138 }
    84 }
   139 endif;
    85 endif;
   141 if ( !function_exists('get_user_by') ) :
    87 if ( !function_exists('get_user_by') ) :
   142 /**
    88 /**
   143  * Retrieve user info by a given field
    89  * Retrieve user info by a given field
   144  *
    90  *
   145  * @since 2.8.0
    91  * @since 2.8.0
   146  *
    92  * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
   147  * @param string $field The field to retrieve the user with. id | slug | email | login
    93  *
       
    94  * @param string     $field The field to retrieve the user with. id | ID | slug | email | login.
   148  * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
    95  * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
   149  * @return WP_User|bool WP_User object on success, false on failure.
    96  * @return WP_User|false WP_User object on success, false on failure.
   150  */
    97  */
   151 function get_user_by( $field, $value ) {
    98 function get_user_by( $field, $value ) {
   152 	$userdata = WP_User::get_data_by( $field, $value );
    99 	$userdata = WP_User::get_data_by( $field, $value );
   153 
   100 
   154 	if ( !$userdata )
   101 	if ( !$userdata )
   164 if ( !function_exists('cache_users') ) :
   111 if ( !function_exists('cache_users') ) :
   165 /**
   112 /**
   166  * Retrieve info for user lists to prevent multiple queries by get_userdata()
   113  * Retrieve info for user lists to prevent multiple queries by get_userdata()
   167  *
   114  *
   168  * @since 3.0.0
   115  * @since 3.0.0
       
   116  *
       
   117  * @global wpdb $wpdb WordPress database abstraction object.
   169  *
   118  *
   170  * @param array $user_ids User ID numbers list
   119  * @param array $user_ids User ID numbers list
   171  */
   120  */
   172 function cache_users( $user_ids ) {
   121 function cache_users( $user_ids ) {
   173 	global $wpdb;
   122 	global $wpdb;
   203  * just 'wp_mail_from' is set, then just the email address will be used with no
   152  * just 'wp_mail_from' is set, then just the email address will be used with no
   204  * name.
   153  * name.
   205  *
   154  *
   206  * The default content type is 'text/plain' which does not allow using HTML.
   155  * The default content type is 'text/plain' which does not allow using HTML.
   207  * However, you can set the content type of the email by using the
   156  * However, you can set the content type of the email by using the
   208  * 'wp_mail_content_type' filter.
   157  * {@see 'wp_mail_content_type'} filter.
   209  *
   158  *
   210  * The default charset is based on the charset used on the blog. The charset can
   159  * The default charset is based on the charset used on the blog. The charset can
   211  * be set using the 'wp_mail_charset' filter.
   160  * be set using the {@see 'wp_mail_charset'} filter.
   212  *
   161  *
   213  * @since 1.2.1
   162  * @since 1.2.1
   214  *
   163  *
   215  * @uses PHPMailer
   164  * @global PHPMailer $phpmailer
   216  *
   165  *
   217  * @param string|array $to Array or comma-separated list of email addresses to send message.
   166  * @param string|array $to          Array or comma-separated list of email addresses to send message.
   218  * @param string $subject Email subject
   167  * @param string       $subject     Email subject
   219  * @param string $message Message contents
   168  * @param string       $message     Message contents
   220  * @param string|array $headers Optional. Additional headers.
   169  * @param string|array $headers     Optional. Additional headers.
   221  * @param string|array $attachments Optional. Files to attach.
   170  * @param string|array $attachments Optional. Files to attach.
   222  * @return bool Whether the email contents were sent successfully.
   171  * @return bool Whether the email contents were sent successfully.
   223  */
   172  */
   224 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
   173 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
   225 	// Compact the input, apply the filters, and extract them back out
   174 	// Compact the input, apply the filters, and extract them back out
   226 
   175 
   227 	/**
   176 	/**
   228 	 * Filter the wp_mail() arguments.
   177 	 * Filters the wp_mail() arguments.
   229 	 *
   178 	 *
   230 	 * @since 2.2.0
   179 	 * @since 2.2.0
   231 	 *
   180 	 *
   232 	 * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
   181 	 * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
   233 	 *                    subject, message, headers, and attachments values.
   182 	 *                    subject, message, headers, and attachments values.
   234 	 */
   183 	 */
   235 	$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
   184 	$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
   236 
   185 
   237 	if ( isset( $atts['to'] ) ) {
   186 	if ( isset( $atts['to'] ) ) {
   238 		$to = $atts['to'];
   187 		$to = $atts['to'];
       
   188 	}
       
   189 
       
   190 	if ( !is_array( $to ) ) {
       
   191 		$to = explode( ',', $to );
   239 	}
   192 	}
   240 
   193 
   241 	if ( isset( $atts['subject'] ) ) {
   194 	if ( isset( $atts['subject'] ) ) {
   242 		$subject = $atts['subject'];
   195 		$subject = $atts['subject'];
   243 	}
   196 	}
   265 		require_once ABSPATH . WPINC . '/class-smtp.php';
   218 		require_once ABSPATH . WPINC . '/class-smtp.php';
   266 		$phpmailer = new PHPMailer( true );
   219 		$phpmailer = new PHPMailer( true );
   267 	}
   220 	}
   268 
   221 
   269 	// Headers
   222 	// Headers
       
   223 	$cc = $bcc = $reply_to = array();
       
   224 
   270 	if ( empty( $headers ) ) {
   225 	if ( empty( $headers ) ) {
   271 		$headers = array();
   226 		$headers = array();
   272 	} else {
   227 	} else {
   273 		if ( !is_array( $headers ) ) {
   228 		if ( !is_array( $headers ) ) {
   274 			// Explode the headers out, so this function can take both
   229 			// Explode the headers out, so this function can take both
   276 			$tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
   231 			$tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
   277 		} else {
   232 		} else {
   278 			$tempheaders = $headers;
   233 			$tempheaders = $headers;
   279 		}
   234 		}
   280 		$headers = array();
   235 		$headers = array();
   281 		$cc = array();
       
   282 		$bcc = array();
       
   283 
   236 
   284 		// If it's actually got contents
   237 		// If it's actually got contents
   285 		if ( !empty( $tempheaders ) ) {
   238 		if ( !empty( $tempheaders ) ) {
   286 			// Iterate through the raw headers
   239 			// Iterate through the raw headers
   287 			foreach ( (array) $tempheaders as $header ) {
   240 			foreach ( (array) $tempheaders as $header ) {
   340 						$cc = array_merge( (array) $cc, explode( ',', $content ) );
   293 						$cc = array_merge( (array) $cc, explode( ',', $content ) );
   341 						break;
   294 						break;
   342 					case 'bcc':
   295 					case 'bcc':
   343 						$bcc = array_merge( (array) $bcc, explode( ',', $content ) );
   296 						$bcc = array_merge( (array) $bcc, explode( ',', $content ) );
   344 						break;
   297 						break;
       
   298 					case 'reply-to':
       
   299 						$reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
       
   300 						break;
   345 					default:
   301 					default:
   346 						// Add it to our grand headers array
   302 						// Add it to our grand headers array
   347 						$headers[trim( $name )] = trim( $content );
   303 						$headers[trim( $name )] = trim( $content );
   348 						break;
   304 						break;
   349 				}
   305 				}
   350 			}
   306 			}
   351 		}
   307 		}
   352 	}
   308 	}
   353 
   309 
   354 	// Empty out the values that may be set
   310 	// Empty out the values that may be set
   355 	$phpmailer->ClearAllRecipients();
   311 	$phpmailer->clearAllRecipients();
   356 	$phpmailer->ClearAttachments();
   312 	$phpmailer->clearAttachments();
   357 	$phpmailer->ClearCustomHeaders();
   313 	$phpmailer->clearCustomHeaders();
   358 	$phpmailer->ClearReplyTos();
   314 	$phpmailer->clearReplyTos();
   359 
   315 
   360 	// From email and name
   316 	// From email and name
   361 	// If we don't have a name from the input headers
   317 	// If we don't have a name from the input headers
   362 	if ( !isset( $from_name ) )
   318 	if ( !isset( $from_name ) )
   363 		$from_name = 'WordPress';
   319 		$from_name = 'WordPress';
   378 
   334 
   379 		$from_email = 'wordpress@' . $sitename;
   335 		$from_email = 'wordpress@' . $sitename;
   380 	}
   336 	}
   381 
   337 
   382 	/**
   338 	/**
   383 	 * Filter the email address to send from.
   339 	 * Filters the email address to send from.
   384 	 *
   340 	 *
   385 	 * @since 2.2.0
   341 	 * @since 2.2.0
   386 	 *
   342 	 *
   387 	 * @param string $from_email Email address to send from.
   343 	 * @param string $from_email Email address to send from.
   388 	 */
   344 	 */
   389 	$phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
   345 	$from_email = apply_filters( 'wp_mail_from', $from_email );
   390 
   346 
   391 	/**
   347 	/**
   392 	 * Filter the name to associate with the "from" email address.
   348 	 * Filters the name to associate with the "from" email address.
   393 	 *
   349 	 *
   394 	 * @since 2.3.0
   350 	 * @since 2.3.0
   395 	 *
   351 	 *
   396 	 * @param string $from_name Name associated with the "from" email address.
   352 	 * @param string $from_name Name associated with the "from" email address.
   397 	 */
   353 	 */
   398 	$phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
   354 	$from_name = apply_filters( 'wp_mail_from_name', $from_name );
   399 
   355 
   400 	// Set destination addresses
   356 	try {
   401 	if ( !is_array( $to ) )
   357 		$phpmailer->setFrom( $from_email, $from_name, false );
   402 		$to = explode( ',', $to );
   358 	} catch ( phpmailerException $e ) {
   403 
   359 		$mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
   404 	foreach ( (array) $to as $recipient ) {
   360 		$mail_error_data['phpmailer_exception_code'] = $e->getCode();
   405 		try {
   361 
   406 			// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
   362 		/** This filter is documented in wp-includes/pluggable.php */
   407 			$recipient_name = '';
   363 		do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
   408 			if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
   364 
   409 				if ( count( $matches ) == 3 ) {
   365 		return false;
   410 					$recipient_name = $matches[1];
       
   411 					$recipient = $matches[2];
       
   412 				}
       
   413 			}
       
   414 			$phpmailer->AddAddress( $recipient, $recipient_name);
       
   415 		} catch ( phpmailerException $e ) {
       
   416 			continue;
       
   417 		}
       
   418 	}
   366 	}
   419 
   367 
   420 	// Set mail's subject and body
   368 	// Set mail's subject and body
   421 	$phpmailer->Subject = $subject;
   369 	$phpmailer->Subject = $subject;
   422 	$phpmailer->Body    = $message;
   370 	$phpmailer->Body    = $message;
   423 
   371 
   424 	// Add any CC and BCC recipients
   372 	// Set destination addresses, using appropriate methods for handling addresses
   425 	if ( !empty( $cc ) ) {
   373 	$address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' );
   426 		foreach ( (array) $cc as $recipient ) {
   374 
       
   375 	foreach ( $address_headers as $address_header => $addresses ) {
       
   376 		if ( empty( $addresses ) ) {
       
   377 			continue;
       
   378 		}
       
   379 
       
   380 		foreach ( (array) $addresses as $address ) {
   427 			try {
   381 			try {
   428 				// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
   382 				// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
   429 				$recipient_name = '';
   383 				$recipient_name = '';
   430 				if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
   384 
       
   385 				if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) {
   431 					if ( count( $matches ) == 3 ) {
   386 					if ( count( $matches ) == 3 ) {
   432 						$recipient_name = $matches[1];
   387 						$recipient_name = $matches[1];
   433 						$recipient = $matches[2];
   388 						$address        = $matches[2];
   434 					}
   389 					}
   435 				}
   390 				}
   436 				$phpmailer->AddCc( $recipient, $recipient_name );
   391 
       
   392 				switch ( $address_header ) {
       
   393 					case 'to':
       
   394 						$phpmailer->addAddress( $address, $recipient_name );
       
   395 						break;
       
   396 					case 'cc':
       
   397 						$phpmailer->addCc( $address, $recipient_name );
       
   398 						break;
       
   399 					case 'bcc':
       
   400 						$phpmailer->addBcc( $address, $recipient_name );
       
   401 						break;
       
   402 					case 'reply_to':
       
   403 						$phpmailer->addReplyTo( $address, $recipient_name );
       
   404 						break;
       
   405 				}
   437 			} catch ( phpmailerException $e ) {
   406 			} catch ( phpmailerException $e ) {
   438 				continue;
   407 				continue;
   439 			}
   408 			}
   440 		}
   409 		}
   441 	}
   410 	}
   442 
   411 
   443 	if ( !empty( $bcc ) ) {
   412 	// Set to use PHP's mail()
   444 		foreach ( (array) $bcc as $recipient) {
   413 	$phpmailer->isMail();
       
   414 
       
   415 	// Set Content-Type and charset
       
   416 	// If we don't have a content-type from the input headers
       
   417 	if ( !isset( $content_type ) )
       
   418 		$content_type = 'text/plain';
       
   419 
       
   420 	/**
       
   421 	 * Filters the wp_mail() content type.
       
   422 	 *
       
   423 	 * @since 2.3.0
       
   424 	 *
       
   425 	 * @param string $content_type Default wp_mail() content type.
       
   426 	 */
       
   427 	$content_type = apply_filters( 'wp_mail_content_type', $content_type );
       
   428 
       
   429 	$phpmailer->ContentType = $content_type;
       
   430 
       
   431 	// Set whether it's plaintext, depending on $content_type
       
   432 	if ( 'text/html' == $content_type )
       
   433 		$phpmailer->isHTML( true );
       
   434 
       
   435 	// If we don't have a charset from the input headers
       
   436 	if ( !isset( $charset ) )
       
   437 		$charset = get_bloginfo( 'charset' );
       
   438 
       
   439 	// Set the content-type and charset
       
   440 
       
   441 	/**
       
   442 	 * Filters the default wp_mail() charset.
       
   443 	 *
       
   444 	 * @since 2.3.0
       
   445 	 *
       
   446 	 * @param string $charset Default email charset.
       
   447 	 */
       
   448 	$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
       
   449 
       
   450 	// Set custom headers
       
   451 	if ( !empty( $headers ) ) {
       
   452 		foreach ( (array) $headers as $name => $content ) {
       
   453 			$phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
       
   454 		}
       
   455 
       
   456 		if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
       
   457 			$phpmailer->addCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
       
   458 	}
       
   459 
       
   460 	if ( !empty( $attachments ) ) {
       
   461 		foreach ( $attachments as $attachment ) {
   445 			try {
   462 			try {
   446 				// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
   463 				$phpmailer->addAttachment($attachment);
   447 				$recipient_name = '';
       
   448 				if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
       
   449 					if ( count( $matches ) == 3 ) {
       
   450 						$recipient_name = $matches[1];
       
   451 						$recipient = $matches[2];
       
   452 					}
       
   453 				}
       
   454 				$phpmailer->AddBcc( $recipient, $recipient_name );
       
   455 			} catch ( phpmailerException $e ) {
   464 			} catch ( phpmailerException $e ) {
   456 				continue;
   465 				continue;
   457 			}
   466 			}
   458 		}
   467 		}
   459 	}
   468 	}
   460 
   469 
   461 	// Set to use PHP's mail()
       
   462 	$phpmailer->IsMail();
       
   463 
       
   464 	// Set Content-Type and charset
       
   465 	// If we don't have a content-type from the input headers
       
   466 	if ( !isset( $content_type ) )
       
   467 		$content_type = 'text/plain';
       
   468 
       
   469 	/**
       
   470 	 * Filter the wp_mail() content type.
       
   471 	 *
       
   472 	 * @since 2.3.0
       
   473 	 *
       
   474 	 * @param string $content_type Default wp_mail() content type.
       
   475 	 */
       
   476 	$content_type = apply_filters( 'wp_mail_content_type', $content_type );
       
   477 
       
   478 	$phpmailer->ContentType = $content_type;
       
   479 
       
   480 	// Set whether it's plaintext, depending on $content_type
       
   481 	if ( 'text/html' == $content_type )
       
   482 		$phpmailer->IsHTML( true );
       
   483 
       
   484 	// If we don't have a charset from the input headers
       
   485 	if ( !isset( $charset ) )
       
   486 		$charset = get_bloginfo( 'charset' );
       
   487 
       
   488 	// Set the content-type and charset
       
   489 
       
   490 	/**
       
   491 	 * Filter the default wp_mail() charset.
       
   492 	 *
       
   493 	 * @since 2.3.0
       
   494 	 *
       
   495 	 * @param string $charset Default email charset.
       
   496 	 */
       
   497 	$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
       
   498 
       
   499 	// Set custom headers
       
   500 	if ( !empty( $headers ) ) {
       
   501 		foreach( (array) $headers as $name => $content ) {
       
   502 			$phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
       
   503 		}
       
   504 
       
   505 		if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
       
   506 			$phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
       
   507 	}
       
   508 
       
   509 	if ( !empty( $attachments ) ) {
       
   510 		foreach ( $attachments as $attachment ) {
       
   511 			try {
       
   512 				$phpmailer->AddAttachment($attachment);
       
   513 			} catch ( phpmailerException $e ) {
       
   514 				continue;
       
   515 			}
       
   516 		}
       
   517 	}
       
   518 
       
   519 	/**
   470 	/**
   520 	 * Fires after PHPMailer is initialized.
   471 	 * Fires after PHPMailer is initialized.
   521 	 *
   472 	 *
   522 	 * @since 2.2.0
   473 	 * @since 2.2.0
   523 	 *
   474 	 *
   524 	 * @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference.
   475 	 * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
   525 	 */
   476 	 */
   526 	do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
   477 	do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
   527 
   478 
   528 	// Send!
   479 	// Send!
   529 	try {
   480 	try {
   530 		return $phpmailer->Send();
   481 		return $phpmailer->send();
   531 	} catch ( phpmailerException $e ) {
   482 	} catch ( phpmailerException $e ) {
       
   483 
       
   484 		$mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
       
   485 		$mail_error_data['phpmailer_exception_code'] = $e->getCode();
       
   486 
       
   487 		/**
       
   488 		 * Fires after a phpmailerException is caught.
       
   489 		 *
       
   490 		 * @since 4.4.0
       
   491 		 *
       
   492 		 * @param WP_Error $error A WP_Error object with the phpmailerException message, and an array
       
   493 		 *                        containing the mail recipient, subject, message, headers, and attachments.
       
   494 		 */
       
   495 		do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
       
   496 
   532 		return false;
   497 		return false;
   533 	}
   498 	}
   534 }
   499 }
   535 endif;
   500 endif;
   536 
   501 
   537 if ( !function_exists('wp_authenticate') ) :
   502 if ( !function_exists('wp_authenticate') ) :
   538 /**
   503 /**
   539  * Checks a user's login information and logs them in if it checks out.
   504  * Authenticate a user, confirming the login credentials are valid.
   540  *
   505  *
   541  * @since 2.5.0
   506  * @since 2.5.0
   542  *
   507  * @since 4.5.0 `$username` now accepts an email address.
   543  * @param string $username User's username
   508  *
   544  * @param string $password User's password
   509  * @param string $username User's username or email address.
   545  * @return WP_User|WP_Error WP_User object if login successful, otherwise WP_Error object.
   510  * @param string $password User's password.
       
   511  * @return WP_User|WP_Error WP_User object if the credentials are valid,
       
   512  *                          otherwise WP_Error.
   546  */
   513  */
   547 function wp_authenticate($username, $password) {
   514 function wp_authenticate($username, $password) {
   548 	$username = sanitize_user($username);
   515 	$username = sanitize_user($username);
   549 	$password = trim($password);
   516 	$password = trim($password);
   550 
   517 
   551 	/**
   518 	/**
   552 	 * Filter the user to authenticate.
   519 	 * Filters whether a set of user login credentials are valid.
   553 	 *
   520 	 *
   554 	 * If a non-null value is passed, the filter will effectively short-circuit
   521 	 * A WP_User object is returned if the credentials authenticate a user.
   555 	 * authentication, returning an error instead.
   522 	 * WP_Error or null otherwise.
   556 	 *
   523 	 *
   557 	 * @since 2.8.0
   524 	 * @since 2.8.0
   558 	 *
   525 	 * @since 4.5.0 `$username` now accepts an email address.
   559 	 * @param null|WP_User $user     User to authenticate.
   526 	 *
   560 	 * @param string       $username User login.
   527 	 * @param null|WP_User|WP_Error $user     WP_User if the user is authenticated.
   561 	 * @param string       $password User password
   528 	 *                                        WP_Error or null otherwise.
       
   529 	 * @param string                $username Username or email address.
       
   530 	 * @param string                $password User password
   562 	 */
   531 	 */
   563 	$user = apply_filters( 'authenticate', null, $username, $password );
   532 	$user = apply_filters( 'authenticate', null, $username, $password );
   564 
   533 
   565 	if ( $user == null ) {
   534 	if ( $user == null ) {
   566 		// TODO what should the error message be? (Or would these even happen?)
   535 		// TODO what should the error message be? (Or would these even happen?)
   567 		// Only needed if all authentication handlers fail to return anything.
   536 		// Only needed if all authentication handlers fail to return anything.
   568 		$user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
   537 		$user = new WP_Error( 'authentication_failed', __( '<strong>ERROR</strong>: Invalid username, email address or incorrect password.' ) );
   569 	}
   538 	}
   570 
   539 
   571 	$ignore_codes = array('empty_username', 'empty_password');
   540 	$ignore_codes = array('empty_username', 'empty_password');
   572 
   541 
   573 	if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
   542 	if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
   574 		/**
   543 		/**
   575 		 * Fires after a user login has failed.
   544 		 * Fires after a user login has failed.
   576 		 *
   545 		 *
   577 		 * @since 2.5.0
   546 		 * @since 2.5.0
   578 		 *
   547 		 * @since 4.5.0 The value of `$username` can now be an email address.
   579 		 * @param string $username User login.
   548 		 *
       
   549 		 * @param string $username Username or email address.
   580 		 */
   550 		 */
   581 		do_action( 'wp_login_failed', $username );
   551 		do_action( 'wp_login_failed', $username );
   582 	}
   552 	}
   583 
   553 
   584 	return $user;
   554 	return $user;
   614  * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
   584  * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
   615  * should be and compares the two.
   585  * should be and compares the two.
   616  *
   586  *
   617  * @since 2.5.0
   587  * @since 2.5.0
   618  *
   588  *
       
   589  * @global int $login_grace_period
       
   590  *
   619  * @param string $cookie Optional. If used, will validate contents instead of cookie's
   591  * @param string $cookie Optional. If used, will validate contents instead of cookie's
   620  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   592  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   621  * @return bool|int False if invalid cookie, User ID if valid.
   593  * @return false|int False if invalid cookie, User ID if valid.
   622  */
   594  */
   623 function wp_validate_auth_cookie($cookie = '', $scheme = '') {
   595 function wp_validate_auth_cookie($cookie = '', $scheme = '') {
   624 	if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
   596 	if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
   625 		/**
   597 		/**
   626 		 * Fires if an authentication cookie is malformed.
   598 		 * Fires if an authentication cookie is malformed.
   639 	$username = $cookie_elements['username'];
   611 	$username = $cookie_elements['username'];
   640 	$hmac = $cookie_elements['hmac'];
   612 	$hmac = $cookie_elements['hmac'];
   641 	$token = $cookie_elements['token'];
   613 	$token = $cookie_elements['token'];
   642 	$expired = $expiration = $cookie_elements['expiration'];
   614 	$expired = $expiration = $cookie_elements['expiration'];
   643 
   615 
   644 	// Allow a grace period for POST and AJAX requests
   616 	// Allow a grace period for POST and Ajax requests
   645 	if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
   617 	if ( wp_doing_ajax() || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
   646 		$expired += HOUR_IN_SECONDS;
   618 		$expired += HOUR_IN_SECONDS;
   647 	}
   619 	}
   648 
   620 
   649 	// Quick check to see if an honest cookie has expired
   621 	// Quick check to see if an honest cookie has expired
   650 	if ( $expired < time() ) {
   622 	if ( $expired < time() ) {
   696 	if ( ! $manager->verify( $token ) ) {
   668 	if ( ! $manager->verify( $token ) ) {
   697 		do_action( 'auth_cookie_bad_session_token', $cookie_elements );
   669 		do_action( 'auth_cookie_bad_session_token', $cookie_elements );
   698 		return false;
   670 		return false;
   699 	}
   671 	}
   700 
   672 
   701 	// AJAX/POST grace period set above
   673 	// Ajax/POST grace period set above
   702 	if ( $expiration < time() ) {
   674 	if ( $expiration < time() ) {
   703 		$GLOBALS['login_grace_period'] = 1;
   675 		$GLOBALS['login_grace_period'] = 1;
   704 	}
   676 	}
   705 
   677 
   706 	/**
   678 	/**
   720 if ( !function_exists('wp_generate_auth_cookie') ) :
   692 if ( !function_exists('wp_generate_auth_cookie') ) :
   721 /**
   693 /**
   722  * Generate authentication cookie contents.
   694  * Generate authentication cookie contents.
   723  *
   695  *
   724  * @since 2.5.0
   696  * @since 2.5.0
   725  *
   697  * @since 4.0.0 The `$token` parameter was added.
   726  * @param int $user_id User ID
   698  *
   727  * @param int $expiration Cookie expiration in seconds
   699  * @param int    $user_id    User ID
   728  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   700  * @param int    $expiration The time the cookie expires as a UNIX timestamp.
   729  * @param string $token User's session token to use for this cookie
   701  * @param string $scheme     Optional. The cookie scheme to use: auth, secure_auth, or logged_in
       
   702  * @param string $token      User's session token to use for this cookie
   730  * @return string Authentication cookie contents. Empty string if user does not exist.
   703  * @return string Authentication cookie contents. Empty string if user does not exist.
   731  */
   704  */
   732 function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) {
   705 function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) {
   733 	$user = get_userdata($user_id);
   706 	$user = get_userdata($user_id);
   734 	if ( ! $user ) {
   707 	if ( ! $user ) {
   749 	$hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key );
   722 	$hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key );
   750 
   723 
   751 	$cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash;
   724 	$cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash;
   752 
   725 
   753 	/**
   726 	/**
   754 	 * Filter the authentication cookie.
   727 	 * Filters the authentication cookie.
   755 	 *
   728 	 *
   756 	 * @since 2.5.0
   729 	 * @since 2.5.0
       
   730 	 * @since 4.0.0 The `$token` parameter was added.
   757 	 *
   731 	 *
   758 	 * @param string $cookie     Authentication cookie.
   732 	 * @param string $cookie     Authentication cookie.
   759 	 * @param int    $user_id    User ID.
   733 	 * @param int    $user_id    User ID.
   760 	 * @param int    $expiration Authentication cookie expiration in seconds.
   734 	 * @param int    $expiration The time the cookie expires as a UNIX timestamp.
   761 	 * @param string $scheme     Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'.
   735 	 * @param string $scheme     Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'.
   762 	 * @param string $token      User's session token used.
   736 	 * @param string $token      User's session token used.
   763 	 */
   737 	 */
   764 	return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );
   738 	return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );
   765 }
   739 }
   771  *
   745  *
   772  * @since 2.7.0
   746  * @since 2.7.0
   773  *
   747  *
   774  * @param string $cookie
   748  * @param string $cookie
   775  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   749  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   776  * @return array Authentication cookie components
   750  * @return array|false Authentication cookie components
   777  */
   751  */
   778 function wp_parse_auth_cookie($cookie = '', $scheme = '') {
   752 function wp_parse_auth_cookie($cookie = '', $scheme = '') {
   779 	if ( empty($cookie) ) {
   753 	if ( empty($cookie) ) {
   780 		switch ($scheme){
   754 		switch ($scheme){
   781 			case 'auth':
   755 			case 'auth':
   813 }
   787 }
   814 endif;
   788 endif;
   815 
   789 
   816 if ( !function_exists('wp_set_auth_cookie') ) :
   790 if ( !function_exists('wp_set_auth_cookie') ) :
   817 /**
   791 /**
   818  * Sets the authentication cookies based on user ID.
   792  * Log in a user by setting authentication cookies.
   819  *
   793  *
   820  * The $remember parameter increases the time that the cookie will be kept. The
   794  * The $remember parameter increases the time that the cookie will be kept. The
   821  * default the cookie is kept without remembering is two days. When $remember is
   795  * default the cookie is kept without remembering is two days. When $remember is
   822  * set, the cookies will be kept for 14 days or two weeks.
   796  * set, the cookies will be kept for 14 days or two weeks.
   823  *
   797  *
   824  * @since 2.5.0
   798  * @since 2.5.0
   825  *
   799  * @since 4.3.0 Added the `$token` parameter.
   826  * @param int $user_id User ID
   800  *
   827  * @param bool $remember Whether to remember the user
   801  * @param int    $user_id  User ID
   828  * @param mixed $secure  Whether the admin cookies should only be sent over HTTPS.
   802  * @param bool   $remember Whether to remember the user
   829  *                       Default is_ssl().
   803  * @param mixed  $secure   Whether the admin cookies should only be sent over HTTPS.
   830  */
   804  *                         Default is_ssl().
   831 function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
   805  * @param string $token    Optional. User's session token to use for this cookie.
       
   806  */
       
   807 function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) {
   832 	if ( $remember ) {
   808 	if ( $remember ) {
   833 		/**
   809 		/**
   834 		 * Filter the duration of the authentication cookie expiration period.
   810 		 * Filters the duration of the authentication cookie expiration period.
   835 		 *
   811 		 *
   836 		 * @since 2.8.0
   812 		 * @since 2.8.0
   837 		 *
   813 		 *
   838 		 * @param int  $length   Duration of the expiration period in seconds.
   814 		 * @param int  $length   Duration of the expiration period in seconds.
   839 		 * @param int  $user_id  User ID.
   815 		 * @param int  $user_id  User ID.
   854 
   830 
   855 	if ( '' === $secure ) {
   831 	if ( '' === $secure ) {
   856 		$secure = is_ssl();
   832 		$secure = is_ssl();
   857 	}
   833 	}
   858 
   834 
   859 	// Frontend cookie is secure when the auth cookie is secure and the site's home URL is forced HTTPS.
   835 	// Front-end cookie is secure when the auth cookie is secure and the site's home URL is forced HTTPS.
   860 	$secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME );
   836 	$secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME );
   861 
   837 
   862 	/**
   838 	/**
   863 	 * Filter whether the connection is secure.
   839 	 * Filters whether the connection is secure.
   864 	 *
   840 	 *
   865 	 * @since 3.1.0
   841 	 * @since 3.1.0
   866 	 *
   842 	 *
   867 	 * @param bool $secure  Whether the connection is secure.
   843 	 * @param bool $secure  Whether the connection is secure.
   868 	 * @param int  $user_id User ID.
   844 	 * @param int  $user_id User ID.
   869 	 */
   845 	 */
   870 	$secure = apply_filters( 'secure_auth_cookie', $secure, $user_id );
   846 	$secure = apply_filters( 'secure_auth_cookie', $secure, $user_id );
   871 
   847 
   872 	/**
   848 	/**
   873 	 * Filter whether to use a secure cookie when logged-in.
   849 	 * Filters whether to use a secure cookie when logged-in.
   874 	 *
   850 	 *
   875 	 * @since 3.1.0
   851 	 * @since 3.1.0
   876 	 *
   852 	 *
   877 	 * @param bool $secure_logged_in_cookie Whether to use a secure cookie when logged-in.
   853 	 * @param bool $secure_logged_in_cookie Whether to use a secure cookie when logged-in.
   878 	 * @param int  $user_id                 User ID.
   854 	 * @param int  $user_id                 User ID.
   886 	} else {
   862 	} else {
   887 		$auth_cookie_name = AUTH_COOKIE;
   863 		$auth_cookie_name = AUTH_COOKIE;
   888 		$scheme = 'auth';
   864 		$scheme = 'auth';
   889 	}
   865 	}
   890 
   866 
   891 	$manager = WP_Session_Tokens::get_instance( $user_id );
   867 	if ( '' === $token ) {
   892 	$token = $manager->create( $expiration );
   868 		$manager = WP_Session_Tokens::get_instance( $user_id );
       
   869 		$token   = $manager->create( $expiration );
       
   870 	}
   893 
   871 
   894 	$auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
   872 	$auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
   895 	$logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );
   873 	$logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );
   896 
   874 
   897 	/**
   875 	/**
   898 	 * Fires immediately before the authentication cookie is set.
   876 	 * Fires immediately before the authentication cookie is set.
   899 	 *
   877 	 *
   900 	 * @since 2.5.0
   878 	 * @since 2.5.0
       
   879 	 * @since 4.9.0 The `$token` parameter was added.
   901 	 *
   880 	 *
   902 	 * @param string $auth_cookie Authentication cookie.
   881 	 * @param string $auth_cookie Authentication cookie.
   903 	 * @param int    $expire      Login grace period in seconds. Default 43,200 seconds, or 12 hours.
   882 	 * @param int    $expire      The time the login grace period expires as a UNIX timestamp.
   904 	 * @param int    $expiration  Duration in seconds the authentication cookie should be valid.
   883 	 *                            Default is 12 hours past the cookie's expiration time.
   905 	 *                            Default 1,209,600 seconds, or 14 days.
   884 	 * @param int    $expiration  The time when the authentication cookie expires as a UNIX timestamp.
       
   885 	 *                            Default is 14 days from now.
   906 	 * @param int    $user_id     User ID.
   886 	 * @param int    $user_id     User ID.
   907 	 * @param string $scheme      Authentication scheme. Values include 'auth', 'secure_auth', or 'logged_in'.
   887 	 * @param string $scheme      Authentication scheme. Values include 'auth', 'secure_auth', or 'logged_in'.
   908 	 */
   888 	 * @param string $token       User's session token to use for this cookie.
   909 	do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme );
   889 	 */
   910 
   890 	do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme, $token );
   911 	/**
   891 
   912 	 * Fires immediately before the secure authentication cookie is set.
   892 	/**
       
   893 	 * Fires immediately before the logged-in authentication cookie is set.
   913 	 *
   894 	 *
   914 	 * @since 2.6.0
   895 	 * @since 2.6.0
       
   896 	 * @since 4.9.0 The `$token` parameter was added.
   915 	 *
   897 	 *
   916 	 * @param string $logged_in_cookie The logged-in cookie.
   898 	 * @param string $logged_in_cookie The logged-in cookie.
   917 	 * @param int    $expire           Login grace period in seconds. Default 43,200 seconds, or 12 hours.
   899 	 * @param int    $expire           The time the login grace period expires as a UNIX timestamp.
   918 	 * @param int    $expiration       Duration in seconds the authentication cookie should be valid.
   900 	 *                                 Default is 12 hours past the cookie's expiration time.
   919 	 *                                 Default 1,209,600 seconds, or 14 days.
   901 	 * @param int    $expiration       The time when the logged-in authentication cookie expires as a UNIX timestamp.
       
   902 	 *                                 Default is 14 days from now.
   920 	 * @param int    $user_id          User ID.
   903 	 * @param int    $user_id          User ID.
   921 	 * @param string $scheme           Authentication scheme. Default 'logged_in'.
   904 	 * @param string $scheme           Authentication scheme. Default 'logged_in'.
   922 	 */
   905 	 * @param string $token            User's session token to use for this cookie.
   923 	do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in' );
   906 	 */
       
   907 	do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in', $token );
       
   908 
       
   909 	/**
       
   910 	 * Allows preventing auth cookies from actually being sent to the client.
       
   911 	 *
       
   912 	 * @since 4.7.4
       
   913 	 *
       
   914 	 * @param bool $send Whether to send auth cookies to the client.
       
   915 	 */
       
   916 	if ( ! apply_filters( 'send_auth_cookies', true ) ) {
       
   917 		return;
       
   918 	}
   924 
   919 
   925 	setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
   920 	setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
   926 	setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
   921 	setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
   927 	setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);
   922 	setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);
   928 	if ( COOKIEPATH != SITECOOKIEPATH )
   923 	if ( COOKIEPATH != SITECOOKIEPATH )
   942 	 *
   937 	 *
   943 	 * @since 2.7.0
   938 	 * @since 2.7.0
   944 	 */
   939 	 */
   945 	do_action( 'clear_auth_cookie' );
   940 	do_action( 'clear_auth_cookie' );
   946 
   941 
       
   942 	/** This filter is documented in wp-includes/pluggable.php */
       
   943 	if ( ! apply_filters( 'send_auth_cookies', true ) ) {
       
   944 		return;
       
   945 	}
       
   946 
       
   947 	// Auth cookies
   947 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
   948 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
   948 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
   949 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
   949 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
   950 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
   950 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
   951 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
   951 	setcookie( LOGGED_IN_COOKIE,   ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,          COOKIE_DOMAIN );
   952 	setcookie( LOGGED_IN_COOKIE,   ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,          COOKIE_DOMAIN );
   952 	setcookie( LOGGED_IN_COOKIE,   ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH,      COOKIE_DOMAIN );
   953 	setcookie( LOGGED_IN_COOKIE,   ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH,      COOKIE_DOMAIN );
   953 
   954 
       
   955 	// Settings cookies
       
   956 	setcookie( 'wp-settings-' . get_current_user_id(),      ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
       
   957 	setcookie( 'wp-settings-time-' . get_current_user_id(), ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
       
   958 
   954 	// Old cookies
   959 	// Old cookies
   955 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   960 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   956 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   961 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   957 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   962 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   958 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   963 	setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   960 	// Even older cookies
   965 	// Even older cookies
   961 	setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   966 	setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   962 	setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   967 	setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH,     COOKIE_DOMAIN );
   963 	setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   968 	setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   964 	setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
   969 	setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
       
   970 
       
   971 	// Post password cookie
       
   972 	setcookie( 'wp-postpass_' . COOKIEHASH, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
   965 }
   973 }
   966 endif;
   974 endif;
   967 
   975 
   968 if ( !function_exists('is_user_logged_in') ) :
   976 if ( !function_exists('is_user_logged_in') ) :
   969 /**
   977 /**
   974  * @return bool True if user is logged in, false if not logged in.
   982  * @return bool True if user is logged in, false if not logged in.
   975  */
   983  */
   976 function is_user_logged_in() {
   984 function is_user_logged_in() {
   977 	$user = wp_get_current_user();
   985 	$user = wp_get_current_user();
   978 
   986 
   979 	if ( ! $user->exists() )
   987 	return $user->exists();
   980 		return false;
       
   981 
       
   982 	return true;
       
   983 }
   988 }
   984 endif;
   989 endif;
   985 
   990 
   986 if ( !function_exists('auth_redirect') ) :
   991 if ( !function_exists('auth_redirect') ) :
   987 /**
   992 /**
   993 	// Checks if a user is logged in, if not redirects them to the login page
   998 	// Checks if a user is logged in, if not redirects them to the login page
   994 
   999 
   995 	$secure = ( is_ssl() || force_ssl_admin() );
  1000 	$secure = ( is_ssl() || force_ssl_admin() );
   996 
  1001 
   997 	/**
  1002 	/**
   998 	 * Filter whether to use a secure authentication redirect.
  1003 	 * Filters whether to use a secure authentication redirect.
   999 	 *
  1004 	 *
  1000 	 * @since 3.1.0
  1005 	 * @since 3.1.0
  1001 	 *
  1006 	 *
  1002 	 * @param bool $secure Whether to use a secure authentication redirect. Default false.
  1007 	 * @param bool $secure Whether to use a secure authentication redirect. Default false.
  1003 	 */
  1008 	 */
  1012 			wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  1017 			wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  1013 			exit();
  1018 			exit();
  1014 		}
  1019 		}
  1015 	}
  1020 	}
  1016 
  1021 
  1017 	if ( is_user_admin() ) {
  1022 	/**
  1018 		$scheme = 'logged_in';
  1023 	 * Filters the authentication redirect scheme.
  1019 	} else {
  1024 	 *
  1020 		/**
  1025 	 * @since 2.9.0
  1021 		 * Filter the authentication redirect scheme.
  1026 	 *
  1022 		 *
  1027 	 * @param string $scheme Authentication redirect scheme. Default empty.
  1023 		 * @since 2.9.0
  1028 	 */
  1024 		 *
  1029 	$scheme = apply_filters( 'auth_redirect_scheme', '' );
  1025 		 * @param string $scheme Authentication redirect scheme. Default empty.
       
  1026 		 */
       
  1027 		$scheme = apply_filters( 'auth_redirect_scheme', '' );
       
  1028 	}
       
  1029 
  1030 
  1030 	if ( $user_id = wp_validate_auth_cookie( '',  $scheme) ) {
  1031 	if ( $user_id = wp_validate_auth_cookie( '',  $scheme) ) {
  1031 		/**
  1032 		/**
  1032 		 * Fires before the authentication redirect.
  1033 		 * Fires before the authentication redirect.
  1033 		 *
  1034 		 *
  1077  * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between
  1078  * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between
  1078  *                   0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1079  *                   0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1079  */
  1080  */
  1080 function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) {
  1081 function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) {
  1081 	if ( -1 == $action )
  1082 	if ( -1 == $action )
  1082 		_doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2' );
  1083 		_doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2.0' );
  1083 
  1084 
  1084 	$adminurl = strtolower(admin_url());
  1085 	$adminurl = strtolower(admin_url());
  1085 	$referer = strtolower(wp_get_referer());
  1086 	$referer = strtolower(wp_get_referer());
  1086 	$result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
  1087 	$result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
  1087 	if ( !$result && !(-1 == $action && strpos($referer, $adminurl) === 0) ) {
       
  1088 		wp_nonce_ays($action);
       
  1089 		die();
       
  1090 	}
       
  1091 
  1088 
  1092 	/**
  1089 	/**
  1093 	 * Fires once the admin request has been validated or not.
  1090 	 * Fires once the admin request has been validated or not.
  1094 	 *
  1091 	 *
  1095 	 * @since 1.5.1
  1092 	 * @since 1.5.1
  1097 	 * @param string    $action The nonce action.
  1094 	 * @param string    $action The nonce action.
  1098 	 * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
  1095 	 * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
  1099 	 *                          0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1096 	 *                          0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1100 	 */
  1097 	 */
  1101 	do_action( 'check_admin_referer', $action, $result );
  1098 	do_action( 'check_admin_referer', $action, $result );
       
  1099 
       
  1100 	if ( ! $result && ! ( -1 == $action && strpos( $referer, $adminurl ) === 0 ) ) {
       
  1101 		wp_nonce_ays( $action );
       
  1102 		die();
       
  1103 	}
       
  1104 
  1102 	return $result;
  1105 	return $result;
  1103 }
  1106 }
  1104 endif;
  1107 endif;
  1105 
  1108 
  1106 if ( !function_exists('check_ajax_referer') ) :
  1109 if ( !function_exists('check_ajax_referer') ) :
  1107 /**
  1110 /**
  1108  * Verifies the AJAX request to prevent processing requests external of the blog.
  1111  * Verifies the Ajax request to prevent processing requests external of the blog.
  1109  *
  1112  *
  1110  * @since 2.0.3
  1113  * @since 2.0.3
  1111  *
  1114  *
  1112  * @param int|string   $action    Action nonce.
  1115  * @param int|string   $action    Action nonce.
  1113  * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false,
  1116  * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false,
  1117  *                                Default true.
  1120  *                                Default true.
  1118  * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between
  1121  * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between
  1119  *                   0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1122  *                   0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1120  */
  1123  */
  1121 function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
  1124 function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
       
  1125 	if ( -1 == $action ) {
       
  1126 		_doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '4.7' );
       
  1127 	}
       
  1128 
  1122 	$nonce = '';
  1129 	$nonce = '';
  1123 
  1130 
  1124 	if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) )
  1131 	if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) )
  1125 		$nonce = $_REQUEST[ $query_arg ];
  1132 		$nonce = $_REQUEST[ $query_arg ];
  1126 	elseif ( isset( $_REQUEST['_ajax_nonce'] ) )
  1133 	elseif ( isset( $_REQUEST['_ajax_nonce'] ) )
  1128 	elseif ( isset( $_REQUEST['_wpnonce'] ) )
  1135 	elseif ( isset( $_REQUEST['_wpnonce'] ) )
  1129 		$nonce = $_REQUEST['_wpnonce'];
  1136 		$nonce = $_REQUEST['_wpnonce'];
  1130 
  1137 
  1131 	$result = wp_verify_nonce( $nonce, $action );
  1138 	$result = wp_verify_nonce( $nonce, $action );
  1132 
  1139 
  1133 	if ( $die && false == $result ) {
  1140 	/**
  1134 		if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  1141 	 * Fires once the Ajax request has been validated or not.
  1135 			wp_die( -1 );
       
  1136 		else
       
  1137 			die( '-1' );
       
  1138 	}
       
  1139 
       
  1140 	/**
       
  1141 	 * Fires once the AJAX request has been validated or not.
       
  1142 	 *
  1142 	 *
  1143 	 * @since 2.1.0
  1143 	 * @since 2.1.0
  1144 	 *
  1144 	 *
  1145 	 * @param string    $action The AJAX nonce action.
  1145 	 * @param string    $action The Ajax nonce action.
  1146 	 * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
  1146 	 * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between
  1147 	 *                          0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1147 	 *                          0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1148 	 */
  1148 	 */
  1149 	do_action( 'check_ajax_referer', $action, $result );
  1149 	do_action( 'check_ajax_referer', $action, $result );
  1150 
  1150 
       
  1151 	if ( $die && false === $result ) {
       
  1152 		if ( wp_doing_ajax() ) {
       
  1153 			wp_die( -1, 403 );
       
  1154 		} else {
       
  1155 			die( '-1' );
       
  1156 		}
       
  1157 	}
       
  1158 
  1151 	return $result;
  1159 	return $result;
  1152 }
  1160 }
  1153 endif;
  1161 endif;
  1154 
  1162 
  1155 if ( !function_exists('wp_redirect') ) :
  1163 if ( !function_exists('wp_redirect') ) :
  1156 /**
  1164 /**
  1157  * Redirects to another page.
  1165  * Redirects to another page.
  1158  *
  1166  *
       
  1167  * Note: wp_redirect() does not exit automatically, and should almost always be
       
  1168  * followed by a call to `exit;`:
       
  1169  *
       
  1170  *     wp_redirect( $url );
       
  1171  *     exit;
       
  1172  *
       
  1173  * Exiting can also be selectively manipulated by using wp_redirect() as a conditional
       
  1174  * in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_location'} hooks:
       
  1175  *
       
  1176  *     if ( wp_redirect( $url ) ) {
       
  1177  *         exit;
       
  1178  *     }
       
  1179  *
  1159  * @since 1.5.1
  1180  * @since 1.5.1
  1160  *
  1181  *
       
  1182  * @global bool $is_IIS
       
  1183  *
  1161  * @param string $location The path to redirect to.
  1184  * @param string $location The path to redirect to.
  1162  * @param int $status Status code to use.
  1185  * @param int    $status   Status code to use.
  1163  * @return bool False if $location is not provided, true otherwise.
  1186  * @return bool False if $location is not provided, true otherwise.
  1164  */
  1187  */
  1165 function wp_redirect($location, $status = 302) {
  1188 function wp_redirect($location, $status = 302) {
  1166 	global $is_IIS;
  1189 	global $is_IIS;
  1167 
  1190 
  1168 	/**
  1191 	/**
  1169 	 * Filter the redirect location.
  1192 	 * Filters the redirect location.
  1170 	 *
  1193 	 *
  1171 	 * @since 2.1.0
  1194 	 * @since 2.1.0
  1172 	 *
  1195 	 *
  1173 	 * @param string $location The path to redirect to.
  1196 	 * @param string $location The path to redirect to.
  1174 	 * @param int    $status   Status code to use.
  1197 	 * @param int    $status   Status code to use.
  1175 	 */
  1198 	 */
  1176 	$location = apply_filters( 'wp_redirect', $location, $status );
  1199 	$location = apply_filters( 'wp_redirect', $location, $status );
  1177 
  1200 
  1178 	/**
  1201 	/**
  1179 	 * Filter the redirect status code.
  1202 	 * Filters the redirect status code.
  1180 	 *
  1203 	 *
  1181 	 * @since 2.3.0
  1204 	 * @since 2.3.0
  1182 	 *
  1205 	 *
  1183 	 * @param int    $status   Status code to use.
  1206 	 * @param int    $status   Status code to use.
  1184 	 * @param string $location The path to redirect to.
  1207 	 * @param string $location The path to redirect to.
  1203 /**
  1226 /**
  1204  * Sanitizes a URL for use in a redirect.
  1227  * Sanitizes a URL for use in a redirect.
  1205  *
  1228  *
  1206  * @since 2.3.0
  1229  * @since 2.3.0
  1207  *
  1230  *
  1208  * @return string redirect-sanitized URL
  1231  * @param string $location The path to redirect to.
       
  1232  * @return string Redirect-sanitized URL.
  1209  **/
  1233  **/
  1210 function wp_sanitize_redirect($location) {
  1234 function wp_sanitize_redirect($location) {
  1211 	$regex = '/
  1235 	$regex = '/
  1212 		(
  1236 		(
  1213 			(?: [\xC2-\xDF][\x80-\xBF]        # double-byte sequences   110xxxxx 10xxxxxx
  1237 			(?: [\xC2-\xDF][\x80-\xBF]        # double-byte sequences   110xxxxx 10xxxxxx
  1219 			|   [\xF1-\xF3][\x80-\xBF]{3}
  1243 			|   [\xF1-\xF3][\x80-\xBF]{3}
  1220 			|   \xF4[\x80-\x8F][\x80-\xBF]{2}
  1244 			|   \xF4[\x80-\x8F][\x80-\xBF]{2}
  1221 		){1,40}                              # ...one or more times
  1245 		){1,40}                              # ...one or more times
  1222 		)/x';
  1246 		)/x';
  1223 	$location = preg_replace_callback( $regex, '_wp_sanitize_utf8_in_redirect', $location );
  1247 	$location = preg_replace_callback( $regex, '_wp_sanitize_utf8_in_redirect', $location );
  1224 	$location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!*\[\]()]|i', '', $location);
  1248 	$location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!*\[\]()@]|i', '', $location);
  1225 	$location = wp_kses_no_null($location);
  1249 	$location = wp_kses_no_null($location);
  1226 
  1250 
  1227 	// remove %0d and %0a from location
  1251 	// remove %0d and %0a from location
  1228 	$strip = array('%0d', '%0a', '%0D', '%0A');
  1252 	$strip = array('%0d', '%0a', '%0D', '%0A');
  1229 	$location = _deep_replace($strip, $location);
  1253 	return _deep_replace( $strip, $location );
  1230 	return $location;
       
  1231 }
  1254 }
  1232 
  1255 
  1233 /**
  1256 /**
  1234  * URL encode UTF-8 characters in a URL.
  1257  * URL encode UTF-8 characters in a URL.
  1235  *
  1258  *
  1236  * @ignore
  1259  * @ignore
  1237  * @since 4.2.0
  1260  * @since 4.2.0
  1238  * @access private
  1261  * @access private
  1239  *
  1262  *
  1240  * @see wp_sanitize_redirect()
  1263  * @see wp_sanitize_redirect()
       
  1264  *
       
  1265  * @param array $matches RegEx matches against the redirect location.
       
  1266  * @return string URL-encoded version of the first RegEx match.
  1241  */
  1267  */
  1242 function _wp_sanitize_utf8_in_redirect( $matches ) {
  1268 function _wp_sanitize_utf8_in_redirect( $matches ) {
  1243 	return urlencode( $matches[0] );
  1269 	return urlencode( $matches[0] );
  1244 }
  1270 }
  1245 endif;
  1271 endif;
  1250  *
  1276  *
  1251  * Checks whether the $location is using an allowed host, if it has an absolute
  1277  * Checks whether the $location is using an allowed host, if it has an absolute
  1252  * path. A plugin can therefore set or remove allowed host(s) to or from the
  1278  * path. A plugin can therefore set or remove allowed host(s) to or from the
  1253  * list.
  1279  * list.
  1254  *
  1280  *
  1255  * If the host is not allowed, then the redirect is to wp-admin on the siteurl
  1281  * If the host is not allowed, then the redirect defaults to wp-admin on the siteurl
  1256  * instead. This prevents malicious redirects which redirect to another host,
  1282  * instead. This prevents malicious redirects which redirect to another host,
  1257  * but only used in a few places.
  1283  * but only used in a few places.
  1258  *
  1284  *
  1259  * @since 2.3.0
  1285  * @since 2.3.0
  1260  *
  1286  *
  1261  * @return void Does not return anything
  1287  * @param string $location The path to redirect to.
  1262  **/
  1288  * @param int    $status   Status code to use.
       
  1289  */
  1263 function wp_safe_redirect($location, $status = 302) {
  1290 function wp_safe_redirect($location, $status = 302) {
  1264 
  1291 
  1265 	// Need to look at the URL the way it will end up in wp_redirect()
  1292 	// Need to look at the URL the way it will end up in wp_redirect()
  1266 	$location = wp_sanitize_redirect($location);
  1293 	$location = wp_sanitize_redirect($location);
  1267 
  1294 
  1268 	$location = wp_validate_redirect($location, admin_url());
  1295 	/**
       
  1296 	 * Filters the redirect fallback URL for when the provided redirect is not safe (local).
       
  1297 	 *
       
  1298 	 * @since 4.3.0
       
  1299 	 *
       
  1300 	 * @param string $fallback_url The fallback URL to use by default.
       
  1301 	 * @param int    $status       The redirect status.
       
  1302 	 */
       
  1303 	$location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) );
  1269 
  1304 
  1270 	wp_redirect($location, $status);
  1305 	wp_redirect($location, $status);
  1271 }
  1306 }
  1272 endif;
  1307 endif;
  1273 
  1308 
  1282  * If the host is not allowed, then the redirect is to $default supplied
  1317  * If the host is not allowed, then the redirect is to $default supplied
  1283  *
  1318  *
  1284  * @since 2.8.1
  1319  * @since 2.8.1
  1285  *
  1320  *
  1286  * @param string $location The redirect to validate
  1321  * @param string $location The redirect to validate
  1287  * @param string $default The value to return if $location is not allowed
  1322  * @param string $default  The value to return if $location is not allowed
  1288  * @return string redirect-sanitized URL
  1323  * @return string redirect-sanitized URL
  1289  **/
  1324  **/
  1290 function wp_validate_redirect($location, $default = '') {
  1325 function wp_validate_redirect($location, $default = '') {
  1291 	$location = trim( $location );
  1326 	$location = trim( $location, " \t\n\r\0\x08\x0B" );
  1292 	// browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
  1327 	// browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
  1293 	if ( substr($location, 0, 2) == '//' )
  1328 	if ( substr($location, 0, 2) == '//' )
  1294 		$location = 'http:' . $location;
  1329 		$location = 'http:' . $location;
  1295 
  1330 
  1296 	// In php 5 parse_url may fail if the URL query part contains http://, bug #38143
  1331 	// In php 5 parse_url may fail if the URL query part contains http://, bug #38143
  1297 	$test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location;
  1332 	$test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location;
  1298 
  1333 
  1299 	$lp  = parse_url($test);
  1334 	// @-operator is used to prevent possible warnings in PHP < 5.3.3.
       
  1335 	$lp = @parse_url($test);
  1300 
  1336 
  1301 	// Give up if malformed URL
  1337 	// Give up if malformed URL
  1302 	if ( false === $lp )
  1338 	if ( false === $lp )
  1303 		return $default;
  1339 		return $default;
  1304 
  1340 
  1305 	// Allow only http and https schemes. No data:, etc.
  1341 	// Allow only http and https schemes. No data:, etc.
  1306 	if ( isset($lp['scheme']) && !('http' == $lp['scheme'] || 'https' == $lp['scheme']) )
  1342 	if ( isset($lp['scheme']) && !('http' == $lp['scheme'] || 'https' == $lp['scheme']) )
  1307 		return $default;
  1343 		return $default;
  1308 
  1344 
  1309 	// Reject if scheme is set but host is not. This catches urls like https:host.com for which parse_url does not set the host field.
  1345 	// 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.
  1310 	if ( isset($lp['scheme'])  && !isset($lp['host']) )
  1346 	if ( ! isset( $lp['host'] ) && ( isset( $lp['scheme'] ) || isset( $lp['user'] ) || isset( $lp['pass'] ) || isset( $lp['port'] ) ) ) {
  1311 		return $default;
  1347 		return $default;
       
  1348 	}
       
  1349 
       
  1350 	// Reject malformed components parse_url() can return on odd inputs.
       
  1351 	foreach ( array( 'user', 'pass', 'host' ) as $component ) {
       
  1352 		if ( isset( $lp[ $component ] ) && strpbrk( $lp[ $component ], ':/?#@' ) ) {
       
  1353 			return $default;
       
  1354 		}
       
  1355 	}
  1312 
  1356 
  1313 	$wpp = parse_url(home_url());
  1357 	$wpp = parse_url(home_url());
  1314 
  1358 
  1315 	/**
  1359 	/**
  1316 	 * Filter the whitelist of hosts to redirect to.
  1360 	 * Filters the whitelist of hosts to redirect to.
  1317 	 *
  1361 	 *
  1318 	 * @since 2.3.0
  1362 	 * @since 2.3.0
  1319 	 *
  1363 	 *
  1320 	 * @param array       $hosts An array of allowed hosts.
  1364 	 * @param array       $hosts An array of allowed hosts.
  1321 	 * @param bool|string $host  The parsed host; empty if not isset.
  1365 	 * @param bool|string $host  The parsed host; empty if not isset.
  1333 /**
  1377 /**
  1334  * Notify an author (and/or others) of a comment/trackback/pingback on a post.
  1378  * Notify an author (and/or others) of a comment/trackback/pingback on a post.
  1335  *
  1379  *
  1336  * @since 1.0.0
  1380  * @since 1.0.0
  1337  *
  1381  *
  1338  * @param int $comment_id Comment ID
  1382  * @param int|WP_Comment  $comment_id Comment ID or WP_Comment object.
  1339  * @param string $deprecated Not used
  1383  * @param string          $deprecated Not used
  1340  * @return bool True on completion. False if no email addresses were specified.
  1384  * @return bool True on completion. False if no email addresses were specified.
  1341  */
  1385  */
  1342 function wp_notify_postauthor( $comment_id, $deprecated = null ) {
  1386 function wp_notify_postauthor( $comment_id, $deprecated = null ) {
  1343 	if ( null !== $deprecated ) {
  1387 	if ( null !== $deprecated ) {
  1344 		_deprecated_argument( __FUNCTION__, '3.8' );
  1388 		_deprecated_argument( __FUNCTION__, '3.8.0' );
  1345 	}
  1389 	}
  1346 
  1390 
  1347 	$comment = get_comment( $comment_id );
  1391 	$comment = get_comment( $comment_id );
  1348 	if ( empty( $comment ) )
  1392 	if ( empty( $comment ) || empty( $comment->comment_post_ID ) )
  1349 		return false;
  1393 		return false;
  1350 
  1394 
  1351 	$post    = get_post( $comment->comment_post_ID );
  1395 	$post    = get_post( $comment->comment_post_ID );
  1352 	$author  = get_userdata( $post->post_author );
  1396 	$author  = get_userdata( $post->post_author );
  1353 
  1397 
  1356 	if ( $author ) {
  1400 	if ( $author ) {
  1357 		$emails[] = $author->user_email;
  1401 		$emails[] = $author->user_email;
  1358 	}
  1402 	}
  1359 
  1403 
  1360 	/**
  1404 	/**
  1361 	 * Filter the list of email addresses to receive a comment notification.
  1405 	 * Filters the list of email addresses to receive a comment notification.
  1362 	 *
  1406 	 *
  1363 	 * By default, only post authors are notified of comments. This filter allows
  1407 	 * By default, only post authors are notified of comments. This filter allows
  1364 	 * others to be added.
  1408 	 * others to be added.
  1365 	 *
  1409 	 *
  1366 	 * @since 3.7.0
  1410 	 * @since 3.7.0
  1367 	 *
  1411 	 *
  1368 	 * @param array $emails     An array of email addresses to receive a comment notification.
  1412 	 * @param array $emails     An array of email addresses to receive a comment notification.
  1369 	 * @param int   $comment_id The comment ID.
  1413 	 * @param int   $comment_id The comment ID.
  1370 	 */
  1414 	 */
  1371 	$emails = apply_filters( 'comment_notification_recipients', $emails, $comment_id );
  1415 	$emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID );
  1372 	$emails = array_filter( $emails );
  1416 	$emails = array_filter( $emails );
  1373 
  1417 
  1374 	// If there are no addresses to send the comment to, bail.
  1418 	// If there are no addresses to send the comment to, bail.
  1375 	if ( ! count( $emails ) ) {
  1419 	if ( ! count( $emails ) ) {
  1376 		return false;
  1420 		return false;
  1378 
  1422 
  1379 	// Facilitate unsetting below without knowing the keys.
  1423 	// Facilitate unsetting below without knowing the keys.
  1380 	$emails = array_flip( $emails );
  1424 	$emails = array_flip( $emails );
  1381 
  1425 
  1382 	/**
  1426 	/**
  1383 	 * Filter whether to notify comment authors of their comments on their own posts.
  1427 	 * Filters whether to notify comment authors of their comments on their own posts.
  1384 	 *
  1428 	 *
  1385 	 * By default, comment authors aren't notified of their comments on their own
  1429 	 * By default, comment authors aren't notified of their comments on their own
  1386 	 * posts. This filter allows you to override that.
  1430 	 * posts. This filter allows you to override that.
  1387 	 *
  1431 	 *
  1388 	 * @since 3.8.0
  1432 	 * @since 3.8.0
  1389 	 *
  1433 	 *
  1390 	 * @param bool $notify     Whether to notify the post author of their own comment.
  1434 	 * @param bool $notify     Whether to notify the post author of their own comment.
  1391 	 *                         Default false.
  1435 	 *                         Default false.
  1392 	 * @param int  $comment_id The comment ID.
  1436 	 * @param int  $comment_id The comment ID.
  1393 	 */
  1437 	 */
  1394 	$notify_author = apply_filters( 'comment_notification_notify_author', false, $comment_id );
  1438 	$notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID );
  1395 
  1439 
  1396 	// The comment was left by the author
  1440 	// The comment was left by the author
  1397 	if ( $author && ! $notify_author && $comment->user_id == $post->post_author ) {
  1441 	if ( $author && ! $notify_author && $comment->user_id == $post->post_author ) {
  1398 		unset( $emails[ $author->user_email ] );
  1442 		unset( $emails[ $author->user_email ] );
  1399 	}
  1443 	}
  1413 		return false;
  1457 		return false;
  1414 	} else {
  1458 	} else {
  1415 		$emails = array_flip( $emails );
  1459 		$emails = array_flip( $emails );
  1416 	}
  1460 	}
  1417 
  1461 
       
  1462 	$switched_locale = switch_to_locale( get_locale() );
       
  1463 
  1418 	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  1464 	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  1419 
  1465 
  1420 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1466 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1421 	// we want to reverse this for the plain text arena of emails.
  1467 	// we want to reverse this for the plain text arena of emails.
  1422 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1468 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
       
  1469 	$comment_content = wp_specialchars_decode( $comment->comment_content );
  1423 
  1470 
  1424 	switch ( $comment->comment_type ) {
  1471 	switch ( $comment->comment_type ) {
  1425 		case 'trackback':
  1472 		case 'trackback':
       
  1473 			/* translators: 1: Post title */
  1426 			$notify_message  = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
  1474 			$notify_message  = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
  1427 			/* translators: 1: website name, 2: website IP, 3: website hostname */
  1475 			/* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */
  1428 			$notify_message .= sprintf( __('Website: %1$s (IP: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1476 			$notify_message .= sprintf( __('Website: %1$s (IP address: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1429 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1477 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1430 			$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment->comment_content ) . "\r\n\r\n";
  1478 			$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  1431 			$notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n";
  1479 			$notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n";
  1432 			/* translators: 1: blog name, 2: post title */
  1480 			/* translators: 1: blog name, 2: post title */
  1433 			$subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
  1481 			$subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
  1434 			break;
  1482 			break;
  1435 		case 'pingback':
  1483 		case 'pingback':
       
  1484 			/* translators: 1: Post title */
  1436 			$notify_message  = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
  1485 			$notify_message  = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
  1437 			/* translators: 1: website name, 2: website IP, 3: website hostname */
  1486 			/* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */
  1438 			$notify_message .= sprintf( __('Website: %1$s (IP: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1487 			$notify_message .= sprintf( __('Website: %1$s (IP address: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1439 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1488 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1440 			$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment->comment_content ) . "\r\n\r\n";
  1489 			$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  1441 			$notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n";
  1490 			$notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n";
  1442 			/* translators: 1: blog name, 2: post title */
  1491 			/* translators: 1: blog name, 2: post title */
  1443 			$subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
  1492 			$subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
  1444 			break;
  1493 			break;
  1445 		default: // Comments
  1494 		default: // Comments
  1446 			$notify_message  = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
  1495 			$notify_message  = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
  1447 			/* translators: 1: comment author, 2: author IP, 3: author domain */
  1496 			/* translators: 1: comment author, 2: comment author's IP address, 3: comment author's hostname */
  1448 			$notify_message .= sprintf( __( 'Author: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1497 			$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1449 			$notify_message .= sprintf( __( 'E-mail: %s' ), $comment->comment_author_email ) . "\r\n";
  1498 			$notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
  1450 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1499 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1451 			$notify_message .= sprintf( __( 'Whois: %s' ), "http://whois.arin.net/rest/ip/{$comment->comment_author_IP}" ) . "\r\n";
  1500 			$notify_message .= sprintf( __('Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  1452 			$notify_message .= sprintf( __('Comment: %s' ), "\r\n" . $comment->comment_content ) . "\r\n\r\n";
       
  1453 			$notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n";
  1501 			$notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n";
  1454 			/* translators: 1: blog name, 2: post title */
  1502 			/* translators: 1: blog name, 2: post title */
  1455 			$subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
  1503 			$subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
  1456 			break;
  1504 			break;
  1457 	}
  1505 	}
  1458 	$notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
  1506 	$notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
  1459 	$notify_message .= sprintf( __('Permalink: %s'), get_comment_link( $comment_id ) ) . "\r\n";
  1507 	$notify_message .= sprintf( __('Permalink: %s'), get_comment_link( $comment ) ) . "\r\n";
  1460 
  1508 
  1461 	if ( user_can( $post->post_author, 'edit_comment', $comment_id ) ) {
  1509 	if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) {
  1462 		if ( EMPTY_TRASH_DAYS )
  1510 		if ( EMPTY_TRASH_DAYS ) {
  1463 			$notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
  1511 			$notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
  1464 		else
  1512 		} else {
  1465 			$notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
  1513 			$notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
  1466 		$notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
  1514 		}
       
  1515 		$notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
  1467 	}
  1516 	}
  1468 
  1517 
  1469 	$wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
  1518 	$wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
  1470 
  1519 
  1471 	if ( '' == $comment->comment_author ) {
  1520 	if ( '' == $comment->comment_author ) {
  1483 
  1532 
  1484 	if ( isset($reply_to) )
  1533 	if ( isset($reply_to) )
  1485 		$message_headers .= $reply_to . "\n";
  1534 		$message_headers .= $reply_to . "\n";
  1486 
  1535 
  1487 	/**
  1536 	/**
  1488 	 * Filter the comment notification email text.
  1537 	 * Filters the comment notification email text.
  1489 	 *
  1538 	 *
  1490 	 * @since 1.5.2
  1539 	 * @since 1.5.2
  1491 	 *
  1540 	 *
  1492 	 * @param string $notify_message The comment notification email text.
  1541 	 * @param string $notify_message The comment notification email text.
  1493 	 * @param int    $comment_id     Comment ID.
  1542 	 * @param int    $comment_id     Comment ID.
  1494 	 */
  1543 	 */
  1495 	$notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment_id );
  1544 	$notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID );
  1496 
  1545 
  1497 	/**
  1546 	/**
  1498 	 * Filter the comment notification email subject.
  1547 	 * Filters the comment notification email subject.
  1499 	 *
  1548 	 *
  1500 	 * @since 1.5.2
  1549 	 * @since 1.5.2
  1501 	 *
  1550 	 *
  1502 	 * @param string $subject    The comment notification email subject.
  1551 	 * @param string $subject    The comment notification email subject.
  1503 	 * @param int    $comment_id Comment ID.
  1552 	 * @param int    $comment_id Comment ID.
  1504 	 */
  1553 	 */
  1505 	$subject = apply_filters( 'comment_notification_subject', $subject, $comment_id );
  1554 	$subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID );
  1506 
  1555 
  1507 	/**
  1556 	/**
  1508 	 * Filter the comment notification email headers.
  1557 	 * Filters the comment notification email headers.
  1509 	 *
  1558 	 *
  1510 	 * @since 1.5.2
  1559 	 * @since 1.5.2
  1511 	 *
  1560 	 *
  1512 	 * @param string $message_headers Headers for the comment notification email.
  1561 	 * @param string $message_headers Headers for the comment notification email.
  1513 	 * @param int    $comment_id      Comment ID.
  1562 	 * @param int    $comment_id      Comment ID.
  1514 	 */
  1563 	 */
  1515 	$message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment_id );
  1564 	$message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID );
  1516 
  1565 
  1517 	foreach ( $emails as $email ) {
  1566 	foreach ( $emails as $email ) {
  1518 		@wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  1567 		@wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  1519 	}
  1568 	}
  1520 
  1569 
       
  1570 	if ( $switched_locale ) {
       
  1571 		restore_previous_locale();
       
  1572 	}
       
  1573 
  1521 	return true;
  1574 	return true;
  1522 }
  1575 }
  1523 endif;
  1576 endif;
  1524 
  1577 
  1525 if ( !function_exists('wp_notify_moderator') ) :
  1578 if ( !function_exists('wp_notify_moderator') ) :
  1526 /**
  1579 /**
  1527  * Notifies the moderator of the blog about a new comment that is awaiting approval.
  1580  * Notifies the moderator of the site about a new comment that is awaiting approval.
  1528  *
  1581  *
  1529  * @since 1.0.0
  1582  * @since 1.0.0
  1530  *
  1583  *
  1531  * @global wpdb $wpdb WordPress database abstraction object.
  1584  * @global wpdb $wpdb WordPress database abstraction object.
  1532  *
  1585  *
  1533  * @param int $comment_id Comment ID
  1586  * Uses the {@see 'notify_moderator'} filter to determine whether the site moderator
  1534  * @return bool Always returns true
  1587  * should be notified, overriding the site setting.
       
  1588  *
       
  1589  * @param int $comment_id Comment ID.
       
  1590  * @return true Always returns true.
  1535  */
  1591  */
  1536 function wp_notify_moderator($comment_id) {
  1592 function wp_notify_moderator($comment_id) {
  1537 	global $wpdb;
  1593 	global $wpdb;
  1538 
  1594 
  1539 	if ( 0 == get_option( 'moderation_notify' ) )
  1595 	$maybe_notify = get_option( 'moderation_notify' );
       
  1596 
       
  1597 	/**
       
  1598 	 * Filters whether to send the site moderator email notifications, overriding the site setting.
       
  1599 	 *
       
  1600 	 * @since 4.4.0
       
  1601 	 *
       
  1602 	 * @param bool $maybe_notify Whether to notify blog moderator.
       
  1603 	 * @param int  $comment_ID   The id of the comment for the notification.
       
  1604 	 */
       
  1605 	$maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id );
       
  1606 
       
  1607 	if ( ! $maybe_notify ) {
  1540 		return true;
  1608 		return true;
       
  1609 	}
  1541 
  1610 
  1542 	$comment = get_comment($comment_id);
  1611 	$comment = get_comment($comment_id);
  1543 	$post = get_post($comment->comment_post_ID);
  1612 	$post = get_post($comment->comment_post_ID);
  1544 	$user = get_userdata( $post->post_author );
  1613 	$user = get_userdata( $post->post_author );
  1545 	// Send to the administration and to the post author if the author can modify the comment.
  1614 	// Send to the administration and to the post author if the author can modify the comment.
  1546 	$emails = array( get_option( 'admin_email' ) );
  1615 	$emails = array( get_option( 'admin_email' ) );
  1547 	if ( user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) {
  1616 	if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) {
  1548 		if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) )
  1617 		if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) )
  1549 			$emails[] = $user->user_email;
  1618 			$emails[] = $user->user_email;
  1550 	}
  1619 	}
       
  1620 
       
  1621 	$switched_locale = switch_to_locale( get_locale() );
  1551 
  1622 
  1552 	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  1623 	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  1553 	$comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
  1624 	$comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
  1554 
  1625 
  1555 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1626 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1556 	// we want to reverse this for the plain text arena of emails.
  1627 	// we want to reverse this for the plain text arena of emails.
  1557 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1628 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
       
  1629 	$comment_content = wp_specialchars_decode( $comment->comment_content );
  1558 
  1630 
  1559 	switch ( $comment->comment_type ) {
  1631 	switch ( $comment->comment_type ) {
  1560 		case 'trackback':
  1632 		case 'trackback':
       
  1633 			/* translators: 1: Post title */
  1561 			$notify_message  = sprintf( __('A new trackback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1634 			$notify_message  = sprintf( __('A new trackback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1562 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1635 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1563 			/* translators: 1: website name, 2: website IP, 3: website hostname */
  1636 			/* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */
  1564 			$notify_message .= sprintf( __( 'Website: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1637 			$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
       
  1638 			/* translators: 1: Trackback/pingback/comment author URL */
  1565 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1639 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1566 			$notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1640 			$notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment_content . "\r\n\r\n";
  1567 			break;
  1641 			break;
  1568 		case 'pingback':
  1642 		case 'pingback':
       
  1643 			/* translators: 1: Post title */
  1569 			$notify_message  = sprintf( __('A new pingback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1644 			$notify_message  = sprintf( __('A new pingback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1570 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1645 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1571 			/* translators: 1: website name, 2: website IP, 3: website hostname */
  1646 			/* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */
  1572 			$notify_message .= sprintf( __( 'Website: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1647 			$notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
       
  1648 			/* translators: 1: Trackback/pingback/comment author URL */
  1573 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1649 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1574 			$notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1650 			$notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment_content . "\r\n\r\n";
  1575 			break;
  1651 			break;
  1576 		default: // Comments
  1652 		default: // Comments
       
  1653 			/* translators: 1: Post title */
  1577 			$notify_message  = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1654 			$notify_message  = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1578 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1655 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1579 			$notify_message .= sprintf( __( 'Author: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1656 			/* translators: 1: Comment author name, 2: comment author's IP address, 3: comment author's hostname */
  1580 			$notify_message .= sprintf( __( 'E-mail: %s' ), $comment->comment_author_email ) . "\r\n";
  1657 			$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
       
  1658 			/* translators: 1: Comment author URL */
       
  1659 			$notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
       
  1660 			/* translators: 1: Trackback/pingback/comment author URL */
  1581 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1661 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1582 			$notify_message .= sprintf( __( 'Whois: %s' ), "http://whois.arin.net/rest/ip/{$comment->comment_author_IP}" ) . "\r\n";
  1662 			/* translators: 1: Comment text */
  1583 			$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment->comment_content ) . "\r\n\r\n";
  1663 			$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  1584 			break;
  1664 			break;
  1585 	}
  1665 	}
  1586 
  1666 
  1587 	$notify_message .= sprintf( __('Approve it: %s'),  admin_url("comment.php?action=approve&c=$comment_id") ) . "\r\n";
  1667 	/* translators: Comment moderation. 1: Comment action URL */
  1588 	if ( EMPTY_TRASH_DAYS )
  1668 	$notify_message .= sprintf( __( 'Approve it: %s' ), admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) ) . "\r\n";
  1589 		$notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
  1669 
  1590 	else
  1670 	if ( EMPTY_TRASH_DAYS ) {
  1591 		$notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
  1671 		/* translators: Comment moderation. 1: Comment action URL */
  1592 	$notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
  1672 		$notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) ) . "\r\n";
  1593 
  1673 	} else {
       
  1674 		/* translators: Comment moderation. 1: Comment action URL */
       
  1675 		$notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) ) . "\r\n";
       
  1676 	}
       
  1677 
       
  1678 	/* translators: Comment moderation. 1: Comment action URL */
       
  1679 	$notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) ) . "\r\n";
       
  1680 
       
  1681 	/* translators: Comment moderation. 1: Number of comments awaiting approval */
  1594 	$notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:',
  1682 	$notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:',
  1595  		'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n";
  1683  		'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n";
  1596 	$notify_message .= admin_url("edit-comments.php?comment_status=moderated") . "\r\n";
  1684 	$notify_message .= admin_url( "edit-comments.php?comment_status=moderated#wpbody-content" ) . "\r\n";
  1597 
  1685 
       
  1686 	/* translators: Comment moderation notification email subject. 1: Site name, 2: Post title */
  1598 	$subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title );
  1687 	$subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title );
  1599 	$message_headers = '';
  1688 	$message_headers = '';
  1600 
  1689 
  1601 	/**
  1690 	/**
  1602 	 * Filter the list of recipients for comment moderation emails.
  1691 	 * Filters the list of recipients for comment moderation emails.
  1603 	 *
  1692 	 *
  1604 	 * @since 3.7.0
  1693 	 * @since 3.7.0
  1605 	 *
  1694 	 *
  1606 	 * @param array $emails     List of email addresses to notify for comment moderation.
  1695 	 * @param array $emails     List of email addresses to notify for comment moderation.
  1607 	 * @param int   $comment_id Comment ID.
  1696 	 * @param int   $comment_id Comment ID.
  1608 	 */
  1697 	 */
  1609 	$emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id );
  1698 	$emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id );
  1610 
  1699 
  1611 	/**
  1700 	/**
  1612 	 * Filter the comment moderation email text.
  1701 	 * Filters the comment moderation email text.
  1613 	 *
  1702 	 *
  1614 	 * @since 1.5.2
  1703 	 * @since 1.5.2
  1615 	 *
  1704 	 *
  1616 	 * @param string $notify_message Text of the comment moderation email.
  1705 	 * @param string $notify_message Text of the comment moderation email.
  1617 	 * @param int    $comment_id     Comment ID.
  1706 	 * @param int    $comment_id     Comment ID.
  1618 	 */
  1707 	 */
  1619 	$notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id );
  1708 	$notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id );
  1620 
  1709 
  1621 	/**
  1710 	/**
  1622 	 * Filter the comment moderation email subject.
  1711 	 * Filters the comment moderation email subject.
  1623 	 *
  1712 	 *
  1624 	 * @since 1.5.2
  1713 	 * @since 1.5.2
  1625 	 *
  1714 	 *
  1626 	 * @param string $subject    Subject of the comment moderation email.
  1715 	 * @param string $subject    Subject of the comment moderation email.
  1627 	 * @param int    $comment_id Comment ID.
  1716 	 * @param int    $comment_id Comment ID.
  1628 	 */
  1717 	 */
  1629 	$subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id );
  1718 	$subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id );
  1630 
  1719 
  1631 	/**
  1720 	/**
  1632 	 * Filter the comment moderation email headers.
  1721 	 * Filters the comment moderation email headers.
  1633 	 *
  1722 	 *
  1634 	 * @since 2.8.0
  1723 	 * @since 2.8.0
  1635 	 *
  1724 	 *
  1636 	 * @param string $message_headers Headers for the comment moderation email.
  1725 	 * @param string $message_headers Headers for the comment moderation email.
  1637 	 * @param int    $comment_id      Comment ID.
  1726 	 * @param int    $comment_id      Comment ID.
  1640 
  1729 
  1641 	foreach ( $emails as $email ) {
  1730 	foreach ( $emails as $email ) {
  1642 		@wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  1731 		@wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  1643 	}
  1732 	}
  1644 
  1733 
       
  1734 	if ( $switched_locale ) {
       
  1735 		restore_previous_locale();
       
  1736 	}
       
  1737 
  1645 	return true;
  1738 	return true;
  1646 }
  1739 }
  1647 endif;
  1740 endif;
  1648 
  1741 
  1649 if ( !function_exists('wp_password_change_notification') ) :
  1742 if ( !function_exists('wp_password_change_notification') ) :
  1650 /**
  1743 /**
  1651  * Notify the blog admin of a user changing password, normally via email.
  1744  * Notify the blog admin of a user changing password, normally via email.
  1652  *
  1745  *
  1653  * @since 2.7.0
  1746  * @since 2.7.0
  1654  *
  1747  *
  1655  * @param object $user User Object
  1748  * @param WP_User $user User object.
  1656  */
  1749  */
  1657 function wp_password_change_notification(&$user) {
  1750 function wp_password_change_notification( $user ) {
  1658 	// send a copy of password change notification to the admin
  1751 	// send a copy of password change notification to the admin
  1659 	// but check to see if it's the admin whose password we're changing, and skip this
  1752 	// but check to see if it's the admin whose password we're changing, and skip this
  1660 	if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {
  1753 	if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {
  1661 		$message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
  1754 		/* translators: %s: user name */
       
  1755 		$message = sprintf( __( 'Password changed for user: %s' ), $user->user_login ) . "\r\n";
  1662 		// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1756 		// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1663 		// we want to reverse this for the plain text arena of emails.
  1757 		// we want to reverse this for the plain text arena of emails.
  1664 		$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1758 		$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1665 		wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), $blogname), $message);
  1759 
       
  1760 		$wp_password_change_notification_email = array(
       
  1761 			'to'      => get_option( 'admin_email' ),
       
  1762 			/* translators: Password change notification email subject. %s: Site title */
       
  1763 			'subject' => __( '[%s] Password Changed' ),
       
  1764 			'message' => $message,
       
  1765 			'headers' => '',
       
  1766 		);
       
  1767 
       
  1768 		/**
       
  1769 		 * Filters the contents of the password change notification email sent to the site admin.
       
  1770 		 *
       
  1771 		 * @since 4.9.0
       
  1772 		 *
       
  1773 		 * @param array   $wp_password_change_notification_email {
       
  1774 		 *     Used to build wp_mail().
       
  1775 		 *
       
  1776 		 *     @type string $to      The intended recipient - site admin email address.
       
  1777 		 *     @type string $subject The subject of the email.
       
  1778 		 *     @type string $message The body of the email.
       
  1779 		 *     @type string $headers The headers of the email.
       
  1780 		 * }
       
  1781 		 * @param WP_User $user     User object for user whose password was changed.
       
  1782 		 * @param string  $blogname The site title.
       
  1783 		 */
       
  1784 		$wp_password_change_notification_email = apply_filters( 'wp_password_change_notification_email', $wp_password_change_notification_email, $user, $blogname );
       
  1785 
       
  1786 		wp_mail(
       
  1787 			$wp_password_change_notification_email['to'],
       
  1788 			wp_specialchars_decode( sprintf( $wp_password_change_notification_email['subject'], $blogname ) ),
       
  1789 			$wp_password_change_notification_email['message'],
       
  1790 			$wp_password_change_notification_email['headers']
       
  1791 		);
  1666 	}
  1792 	}
  1667 }
  1793 }
  1668 endif;
  1794 endif;
  1669 
  1795 
  1670 if ( !function_exists('wp_new_user_notification') ) :
  1796 if ( !function_exists('wp_new_user_notification') ) :
  1672  * Email login credentials to a newly-registered user.
  1798  * Email login credentials to a newly-registered user.
  1673  *
  1799  *
  1674  * A new user registration notification is also sent to admin email.
  1800  * A new user registration notification is also sent to admin email.
  1675  *
  1801  *
  1676  * @since 2.0.0
  1802  * @since 2.0.0
  1677  *
  1803  * @since 4.3.0 The `$plaintext_pass` parameter was changed to `$notify`.
  1678  * @param int    $user_id        User ID.
  1804  * @since 4.3.1 The `$plaintext_pass` parameter was deprecated. `$notify` added as a third parameter.
  1679  * @param string $plaintext_pass Optional. The user's plaintext password. Default empty.
  1805  * @since 4.6.0 The `$notify` parameter accepts 'user' for sending notification only to the user created.
  1680  */
  1806  *
  1681 function wp_new_user_notification($user_id, $plaintext_pass = '') {
  1807  * @global wpdb         $wpdb      WordPress database object for queries.
       
  1808  * @global PasswordHash $wp_hasher Portable PHP password hashing framework instance.
       
  1809  *
       
  1810  * @param int    $user_id    User ID.
       
  1811  * @param null   $deprecated Not used (argument deprecated).
       
  1812  * @param string $notify     Optional. Type of notification that should happen. Accepts 'admin' or an empty
       
  1813  *                           string (admin only), 'user', or 'both' (admin and user). Default empty.
       
  1814  */
       
  1815 function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {
       
  1816 	if ( $deprecated !== null ) {
       
  1817 		_deprecated_argument( __FUNCTION__, '4.3.1' );
       
  1818 	}
       
  1819 
       
  1820 	global $wpdb, $wp_hasher;
  1682 	$user = get_userdata( $user_id );
  1821 	$user = get_userdata( $user_id );
  1683 
  1822 
  1684 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1823 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1685 	// we want to reverse this for the plain text arena of emails.
  1824 	// we want to reverse this for the plain text arena of emails.
  1686 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1825 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1687 
  1826 
  1688 	$message  = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
  1827 	if ( 'user' !== $notify ) {
  1689 	$message .= sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
  1828 		$switched_locale = switch_to_locale( get_locale() );
  1690 	$message .= sprintf(__('E-mail: %s'), $user->user_email) . "\r\n";
  1829 
  1691 
  1830 		/* translators: %s: site title */
  1692 	@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);
  1831 		$message  = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
  1693 
  1832 		/* translators: %s: user login */
  1694 	if ( empty($plaintext_pass) )
  1833 		$message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
       
  1834 		/* translators: %s: user email address */
       
  1835 		$message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n";
       
  1836 
       
  1837 		$wp_new_user_notification_email_admin = array(
       
  1838 			'to'      => get_option( 'admin_email' ),
       
  1839 			/* translators: Password change notification email subject. %s: Site title */
       
  1840 			'subject' => __( '[%s] New User Registration' ),
       
  1841 			'message' => $message,
       
  1842 			'headers' => '',
       
  1843 		);
       
  1844 
       
  1845 		/**
       
  1846 		 * Filters the contents of the new user notification email sent to the site admin.
       
  1847 		 *
       
  1848 		 * @since 4.9.0
       
  1849 		 *
       
  1850 		 * @param array   $wp_new_user_notification_email {
       
  1851 		 *     Used to build wp_mail().
       
  1852 		 *
       
  1853 		 *     @type string $to      The intended recipient - site admin email address.
       
  1854 		 *     @type string $subject The subject of the email.
       
  1855 		 *     @type string $message The body of the email.
       
  1856 		 *     @type string $headers The headers of the email.
       
  1857 		 * }
       
  1858 		 * @param WP_User $user     User object for new user.
       
  1859 		 * @param string  $blogname The site title.
       
  1860 		 */
       
  1861 		$wp_new_user_notification_email_admin = apply_filters( 'wp_new_user_notification_email_admin', $wp_new_user_notification_email_admin, $user, $blogname );
       
  1862 
       
  1863 		@wp_mail(
       
  1864 			$wp_new_user_notification_email_admin['to'],
       
  1865 			wp_specialchars_decode( sprintf( $wp_new_user_notification_email_admin['subject'], $blogname ) ),
       
  1866 			$wp_new_user_notification_email_admin['message'],
       
  1867 			$wp_new_user_notification_email_admin['headers']
       
  1868 		);
       
  1869 
       
  1870 		if ( $switched_locale ) {
       
  1871 			restore_previous_locale();
       
  1872 		}
       
  1873 	}
       
  1874 
       
  1875 	// `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification.
       
  1876 	if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) {
  1695 		return;
  1877 		return;
  1696 
  1878 	}
  1697 	$message  = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
  1879 
  1698 	$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
  1880 	// Generate something random for a password reset key.
       
  1881 	$key = wp_generate_password( 20, false );
       
  1882 
       
  1883 	/** This action is documented in wp-login.php */
       
  1884 	do_action( 'retrieve_password_key', $user->user_login, $key );
       
  1885 
       
  1886 	// Now insert the key, hashed, into the DB.
       
  1887 	if ( empty( $wp_hasher ) ) {
       
  1888 		require_once ABSPATH . WPINC . '/class-phpass.php';
       
  1889 		$wp_hasher = new PasswordHash( 8, true );
       
  1890 	}
       
  1891 	$hashed = time() . ':' . $wp_hasher->HashPassword( $key );
       
  1892 	$wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
       
  1893 
       
  1894 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
       
  1895 
       
  1896 	/* translators: %s: user login */
       
  1897 	$message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
       
  1898 	$message .= __('To set your password, visit the following address:') . "\r\n\r\n";
       
  1899 	$message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n\r\n";
       
  1900 
  1699 	$message .= wp_login_url() . "\r\n";
  1901 	$message .= wp_login_url() . "\r\n";
  1700 
  1902 
  1701 	wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
  1903 	$wp_new_user_notification_email = array(
  1702 
  1904 		'to'      => $user->user_email,
       
  1905 		/* translators: Password change notification email subject. %s: Site title */
       
  1906 		'subject' => __( '[%s] Your username and password info' ),
       
  1907 		'message' => $message,
       
  1908 		'headers' => '',
       
  1909 	);
       
  1910 
       
  1911 	/**
       
  1912 	 * Filters the contents of the new user notification email sent to the new user.
       
  1913 	 *
       
  1914 	 * @since 4.9.0
       
  1915 	 *
       
  1916 	 * @param array   $wp_new_user_notification_email {
       
  1917 	 *     Used to build wp_mail().
       
  1918 	 *
       
  1919 	 *     @type string $to      The intended recipient - New user email address.
       
  1920 	 *     @type string $subject The subject of the email.
       
  1921 	 *     @type string $message The body of the email.
       
  1922 	 *     @type string $headers The headers of the email.
       
  1923 	 * }
       
  1924 	 * @param WP_User $user     User object for new user.
       
  1925 	 * @param string  $blogname The site title.
       
  1926 	 */
       
  1927 	$wp_new_user_notification_email = apply_filters( 'wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname );
       
  1928 
       
  1929 	wp_mail(
       
  1930 		$wp_new_user_notification_email['to'],
       
  1931 		wp_specialchars_decode( sprintf( $wp_new_user_notification_email['subject'], $blogname ) ),
       
  1932 		$wp_new_user_notification_email['message'],
       
  1933 		$wp_new_user_notification_email['headers']
       
  1934 	);
       
  1935 
       
  1936 	if ( $switched_locale ) {
       
  1937 		restore_previous_locale();
       
  1938 	}
  1703 }
  1939 }
  1704 endif;
  1940 endif;
  1705 
  1941 
  1706 if ( !function_exists('wp_nonce_tick') ) :
  1942 if ( !function_exists('wp_nonce_tick') ) :
  1707 /**
  1943 /**
  1714  *
  1950  *
  1715  * @return float Float value rounded up to the next highest integer.
  1951  * @return float Float value rounded up to the next highest integer.
  1716  */
  1952  */
  1717 function wp_nonce_tick() {
  1953 function wp_nonce_tick() {
  1718 	/**
  1954 	/**
  1719 	 * Filter the lifespan of nonces in seconds.
  1955 	 * Filters the lifespan of nonces in seconds.
  1720 	 *
  1956 	 *
  1721 	 * @since 2.5.0
  1957 	 * @since 2.5.0
  1722 	 *
  1958 	 *
  1723 	 * @param int $lifespan Lifespan of nonces in seconds. Default 86,400 seconds, or one day.
  1959 	 * @param int $lifespan Lifespan of nonces in seconds. Default 86,400 seconds, or one day.
  1724 	 */
  1960 	 */
  1746 	$nonce = (string) $nonce;
  1982 	$nonce = (string) $nonce;
  1747 	$user = wp_get_current_user();
  1983 	$user = wp_get_current_user();
  1748 	$uid = (int) $user->ID;
  1984 	$uid = (int) $user->ID;
  1749 	if ( ! $uid ) {
  1985 	if ( ! $uid ) {
  1750 		/**
  1986 		/**
  1751 		 * Filter whether the user who generated the nonce is logged out.
  1987 		 * Filters whether the user who generated the nonce is logged out.
  1752 		 *
  1988 		 *
  1753 		 * @since 3.5.0
  1989 		 * @since 3.5.0
  1754 		 *
  1990 		 *
  1755 		 * @param int    $uid    ID of the nonce-owning user.
  1991 		 * @param int    $uid    ID of the nonce-owning user.
  1756 		 * @param string $action The nonce action.
  1992 		 * @param string $action The nonce action.
  1775 	$expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
  2011 	$expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
  1776 	if ( hash_equals( $expected, $nonce ) ) {
  2012 	if ( hash_equals( $expected, $nonce ) ) {
  1777 		return 2;
  2013 		return 2;
  1778 	}
  2014 	}
  1779 
  2015 
       
  2016 	/**
       
  2017 	 * Fires when nonce verification fails.
       
  2018 	 *
       
  2019 	 * @since 4.4.0
       
  2020 	 *
       
  2021 	 * @param string     $nonce  The invalid nonce.
       
  2022 	 * @param string|int $action The nonce action.
       
  2023 	 * @param WP_User    $user   The current user object.
       
  2024 	 * @param string     $token  The user's session token.
       
  2025 	 */
       
  2026 	do_action( 'wp_verify_nonce_failed', $nonce, $action, $user, $token );
       
  2027 
  1780 	// Invalid nonce
  2028 	// Invalid nonce
  1781 	return false;
  2029 	return false;
  1782 }
  2030 }
  1783 endif;
  2031 endif;
  1784 
  2032 
  1785 if ( !function_exists('wp_create_nonce') ) :
  2033 if ( !function_exists('wp_create_nonce') ) :
  1786 /**
  2034 /**
  1787  * Creates a cryptographic token tied to a specific action, user, and window of time.
  2035  * Creates a cryptographic token tied to a specific action, user, user session,
       
  2036  * and window of time.
  1788  *
  2037  *
  1789  * @since 2.0.3
  2038  * @since 2.0.3
       
  2039  * @since 4.0.0 Session tokens were integrated with nonce creation
  1790  *
  2040  *
  1791  * @param string|int $action Scalar value to add context to the nonce.
  2041  * @param string|int $action Scalar value to add context to the nonce.
  1792  * @return string The token.
  2042  * @return string The token.
  1793  */
  2043  */
  1794 function wp_create_nonce($action = -1) {
  2044 function wp_create_nonce($action = -1) {
  1834  *
  2084  *
  1835  * @since 2.5.0
  2085  * @since 2.5.0
  1836  *
  2086  *
  1837  * @link https://api.wordpress.org/secret-key/1.1/salt/ Create secrets for wp-config.php
  2087  * @link https://api.wordpress.org/secret-key/1.1/salt/ Create secrets for wp-config.php
  1838  *
  2088  *
       
  2089  * @staticvar array $cached_salts
       
  2090  * @staticvar array $duplicated_keys
       
  2091  *
  1839  * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce)
  2092  * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce)
  1840  * @return string Salt value
  2093  * @return string Salt value
  1841  */
  2094  */
  1842 function wp_salt( $scheme = 'auth' ) {
  2095 function wp_salt( $scheme = 'auth' ) {
  1843 	static $cached_salts = array();
  2096 	static $cached_salts = array();
  1844 	if ( isset( $cached_salts[ $scheme ] ) ) {
  2097 	if ( isset( $cached_salts[ $scheme ] ) ) {
  1845 		/**
  2098 		/**
  1846 		 * Filter the WordPress salt.
  2099 		 * Filters the WordPress salt.
  1847 		 *
  2100 		 *
  1848 		 * @since 2.5.0
  2101 		 * @since 2.5.0
  1849 		 *
  2102 		 *
  1850 		 * @param string $cached_salt Cached salt for the given scheme.
  2103 		 * @param string $cached_salt Cached salt for the given scheme.
  1851 		 * @param string $scheme      Authentication scheme. Values include 'auth',
  2104 		 * @param string $scheme      Authentication scheme. Values include 'auth',
  1914 /**
  2167 /**
  1915  * Get hash of given string.
  2168  * Get hash of given string.
  1916  *
  2169  *
  1917  * @since 2.0.3
  2170  * @since 2.0.3
  1918  *
  2171  *
  1919  * @param string $data Plain text to hash
  2172  * @param string $data   Plain text to hash
       
  2173  * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce)
  1920  * @return string Hash of $data
  2174  * @return string Hash of $data
  1921  */
  2175  */
  1922 function wp_hash($data, $scheme = 'auth') {
  2176 function wp_hash($data, $scheme = 'auth') {
  1923 	$salt = wp_salt($scheme);
  2177 	$salt = wp_salt($scheme);
  1924 
  2178 
  1933  * For integration with other applications, this function can be overwritten to
  2187  * For integration with other applications, this function can be overwritten to
  1934  * instead use the other package password checking algorithm.
  2188  * instead use the other package password checking algorithm.
  1935  *
  2189  *
  1936  * @since 2.5.0
  2190  * @since 2.5.0
  1937  *
  2191  *
  1938  * @global object $wp_hasher PHPass object
  2192  * @global PasswordHash $wp_hasher PHPass object
  1939  * @uses PasswordHash::HashPassword
       
  1940  *
  2193  *
  1941  * @param string $password Plain text user password to hash
  2194  * @param string $password Plain text user password to hash
  1942  * @return string The hash string of the password
  2195  * @return string The hash string of the password
  1943  */
  2196  */
  1944 function wp_hash_password($password) {
  2197 function wp_hash_password($password) {
  1966  * For integration with other applications, this function can be overwritten to
  2219  * For integration with other applications, this function can be overwritten to
  1967  * instead use the other package password checking algorithm.
  2220  * instead use the other package password checking algorithm.
  1968  *
  2221  *
  1969  * @since 2.5.0
  2222  * @since 2.5.0
  1970  *
  2223  *
  1971  * @global object $wp_hasher PHPass object used for checking the password
  2224  * @global PasswordHash $wp_hasher PHPass object used for checking the password
  1972  *	against the $hash + $password
  2225  *	against the $hash + $password
  1973  * @uses PasswordHash::CheckPassword
  2226  * @uses PasswordHash::CheckPassword
  1974  *
  2227  *
  1975  * @param string $password Plaintext user's password
  2228  * @param string     $password Plaintext user's password
  1976  * @param string $hash Hash of the user's password to check against.
  2229  * @param string     $hash     Hash of the user's password to check against.
       
  2230  * @param string|int $user_id  Optional. User ID.
  1977  * @return bool False, if the $password does not match the hashed password
  2231  * @return bool False, if the $password does not match the hashed password
  1978  */
  2232  */
  1979 function wp_check_password($password, $hash, $user_id = '') {
  2233 function wp_check_password($password, $hash, $user_id = '') {
  1980 	global $wp_hasher;
  2234 	global $wp_hasher;
  1981 
  2235 
  1987 			wp_set_password($password, $user_id);
  2241 			wp_set_password($password, $user_id);
  1988 			$hash = wp_hash_password($password);
  2242 			$hash = wp_hash_password($password);
  1989 		}
  2243 		}
  1990 
  2244 
  1991 		/**
  2245 		/**
  1992 		 * Filter whether the plaintext password matches the encrypted password.
  2246 		 * Filters whether the plaintext password matches the encrypted password.
  1993 		 *
  2247 		 *
  1994 		 * @since 2.5.0
  2248 		 * @since 2.5.0
  1995 		 *
  2249 		 *
  1996 		 * @param bool   $check    Whether the passwords match.
  2250 		 * @param bool       $check    Whether the passwords match.
  1997 		 * @param string $password The plaintext password.
  2251 		 * @param string     $password The plaintext password.
  1998 		 * @param string $hash     The hashed password.
  2252 		 * @param string     $hash     The hashed password.
  1999 		 * @param int    $user_id  User ID.
  2253 		 * @param string|int $user_id  User ID. Can be empty.
  2000 		 */
  2254 		 */
  2001 		return apply_filters( 'check_password', $check, $password, $hash, $user_id );
  2255 		return apply_filters( 'check_password', $check, $password, $hash, $user_id );
  2002 	}
  2256 	}
  2003 
  2257 
  2004 	// If the stored hash is longer than an MD5, presume the
  2258 	// If the stored hash is longer than an MD5, presume the
  2040 	for ( $i = 0; $i < $length; $i++ ) {
  2294 	for ( $i = 0; $i < $length; $i++ ) {
  2041 		$password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
  2295 		$password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
  2042 	}
  2296 	}
  2043 
  2297 
  2044 	/**
  2298 	/**
  2045 	 * Filter the randomly-generated password.
  2299 	 * Filters the randomly-generated password.
  2046 	 *
  2300 	 *
  2047 	 * @since 3.0.0
  2301 	 * @since 3.0.0
  2048 	 *
  2302 	 *
  2049 	 * @param string $password The generated password.
  2303 	 * @param string $password The generated password.
  2050 	 */
  2304 	 */
  2055 if ( !function_exists('wp_rand') ) :
  2309 if ( !function_exists('wp_rand') ) :
  2056 /**
  2310 /**
  2057  * Generates a random number
  2311  * Generates a random number
  2058  *
  2312  *
  2059  * @since 2.6.2
  2313  * @since 2.6.2
       
  2314  * @since 4.4.0 Uses PHP7 random_int() or the random_compat library if available.
       
  2315  *
       
  2316  * @global string $rnd_value
       
  2317  * @staticvar string $seed
       
  2318  * @staticvar bool $external_rand_source_available
  2060  *
  2319  *
  2061  * @param int $min Lower limit for the generated number
  2320  * @param int $min Lower limit for the generated number
  2062  * @param int $max Upper limit for the generated number
  2321  * @param int $max Upper limit for the generated number
  2063  * @return int A random number between min and max
  2322  * @return int A random number between min and max
  2064  */
  2323  */
  2065 function wp_rand( $min = 0, $max = 0 ) {
  2324 function wp_rand( $min = 0, $max = 0 ) {
  2066 	global $rnd_value;
  2325 	global $rnd_value;
       
  2326 
       
  2327 	// Some misconfigured 32bit environments (Entropy PHP, for example) truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats.
       
  2328 	$max_random_number = 3000000000 === 2147483647 ? (float) "4294967295" : 4294967295; // 4294967295 = 0xffffffff
       
  2329 
       
  2330 	// We only handle Ints, floats are truncated to their integer value.
       
  2331 	$min = (int) $min;
       
  2332 	$max = (int) $max;
       
  2333 
       
  2334 	// Use PHP's CSPRNG, or a compatible method
       
  2335 	static $use_random_int_functionality = true;
       
  2336 	if ( $use_random_int_functionality ) {
       
  2337 		try {
       
  2338 			$_max = ( 0 != $max ) ? $max : $max_random_number;
       
  2339 			// wp_rand() can accept arguments in either order, PHP cannot.
       
  2340 			$_max = max( $min, $_max );
       
  2341 			$_min = min( $min, $_max );
       
  2342 			$val = random_int( $_min, $_max );
       
  2343 			if ( false !== $val ) {
       
  2344 				return absint( $val );
       
  2345 			} else {
       
  2346 				$use_random_int_functionality = false;
       
  2347 			}
       
  2348 		} catch ( Error $e ) {
       
  2349 			$use_random_int_functionality = false;
       
  2350 		} catch ( Exception $e ) {
       
  2351 			$use_random_int_functionality = false;
       
  2352 		}
       
  2353 	}
  2067 
  2354 
  2068 	// Reset $rnd_value after 14 uses
  2355 	// Reset $rnd_value after 14 uses
  2069 	// 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value
  2356 	// 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value
  2070 	if ( strlen($rnd_value) < 8 ) {
  2357 	if ( strlen($rnd_value) < 8 ) {
  2071 		if ( defined( 'WP_SETUP_CONFIG' ) )
  2358 		if ( defined( 'WP_SETUP_CONFIG' ) )
  2074 			$seed = get_transient('random_seed');
  2361 			$seed = get_transient('random_seed');
  2075 		$rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed );
  2362 		$rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed );
  2076 		$rnd_value .= sha1($rnd_value);
  2363 		$rnd_value .= sha1($rnd_value);
  2077 		$rnd_value .= sha1($rnd_value . $seed);
  2364 		$rnd_value .= sha1($rnd_value . $seed);
  2078 		$seed = md5($seed . $rnd_value);
  2365 		$seed = md5($seed . $rnd_value);
  2079 		if ( ! defined( 'WP_SETUP_CONFIG' ) )
  2366 		if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) {
  2080 			set_transient('random_seed', $seed);
  2367 			set_transient( 'random_seed', $seed );
       
  2368 		}
  2081 	}
  2369 	}
  2082 
  2370 
  2083 	// Take the first 8 digits for our value
  2371 	// Take the first 8 digits for our value
  2084 	$value = substr($rnd_value, 0, 8);
  2372 	$value = substr($rnd_value, 0, 8);
  2085 
  2373 
  2086 	// Strip the first eight, leaving the remainder for the next call to wp_rand().
  2374 	// Strip the first eight, leaving the remainder for the next call to wp_rand().
  2087 	$rnd_value = substr($rnd_value, 8);
  2375 	$rnd_value = substr($rnd_value, 8);
  2088 
  2376 
  2089 	$value = abs(hexdec($value));
  2377 	$value = abs(hexdec($value));
  2090 
       
  2091 	// Some misconfigured 32bit environments (Entropy PHP, for example) truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats.
       
  2092 	$max_random_number = 3000000000 === 2147483647 ? (float) "4294967295" : 4294967295; // 4294967295 = 0xffffffff
       
  2093 
  2378 
  2094 	// Reduce the value to be within the min - max range
  2379 	// Reduce the value to be within the min - max range
  2095 	if ( $max != 0 )
  2380 	if ( $max != 0 )
  2096 		$value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 );
  2381 		$value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 );
  2097 
  2382 
  2113  * @since 2.5.0
  2398  * @since 2.5.0
  2114  *
  2399  *
  2115  * @global wpdb $wpdb WordPress database abstraction object.
  2400  * @global wpdb $wpdb WordPress database abstraction object.
  2116  *
  2401  *
  2117  * @param string $password The plaintext new user password
  2402  * @param string $password The plaintext new user password
  2118  * @param int $user_id User ID
  2403  * @param int    $user_id  User ID
  2119  */
  2404  */
  2120 function wp_set_password( $password, $user_id ) {
  2405 function wp_set_password( $password, $user_id ) {
  2121 	global $wpdb;
  2406 	global $wpdb;
  2122 
  2407 
  2123 	$hash = wp_hash_password( $password );
  2408 	$hash = wp_hash_password( $password );
  2133  *
  2418  *
  2134  * @since 2.5.0
  2419  * @since 2.5.0
  2135  * @since 4.2.0 Optional `$args` parameter added.
  2420  * @since 4.2.0 Optional `$args` parameter added.
  2136  *
  2421  *
  2137  * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
  2422  * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
  2138  *                           user email, WP_User object, WP_Post object, or comment object.
  2423  *                           user email, WP_User object, WP_Post object, or WP_Comment object.
  2139  * @param int    $size       Optional. Height and width of the avatar image file in pixels. Default 96.
  2424  * @param int    $size       Optional. Height and width of the avatar image file in pixels. Default 96.
  2140  * @param string $default    Optional. URL for the default image or a default type. Accepts '404'
  2425  * @param string $default    Optional. URL for the default image or a default type. Accepts '404'
  2141  *                           (return a 404 instead of a default image), 'retro' (8bit), 'monsterid'
  2426  *                           (return a 404 instead of a default image), 'retro' (8bit), 'monsterid'
  2142  *                           (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"),
  2427  *                           (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"),
  2143  *                           'mystery', 'mm', or 'mysterman' (The Oyster Man), 'blank' (transparent GIF),
  2428  *                           'mystery', 'mm', or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF),
  2144  *                           or 'gravatar_default' (the Gravatar logo). Default is the value of the
  2429  *                           or 'gravatar_default' (the Gravatar logo). Default is the value of the
  2145  *                           'avatar_default' option, with a fallback of 'mystery'.
  2430  *                           'avatar_default' option, with a fallback of 'mystery'.
  2146  * @param string $alt        Optional. Alternative text to use in &lt;img&gt; tag. Default empty.
  2431  * @param string $alt        Optional. Alternative text to use in &lt;img&gt; tag. Default empty.
  2147  * @param array  $args       {
  2432  * @param array  $args       {
  2148  *     Optional. Extra arguments to retrieve the avatar.
  2433  *     Optional. Extra arguments to retrieve the avatar.
  2193 	}
  2478 	}
  2194 	if ( empty( $args['width'] ) ) {
  2479 	if ( empty( $args['width'] ) ) {
  2195 		$args['width'] = $args['size'];
  2480 		$args['width'] = $args['size'];
  2196 	}
  2481 	}
  2197 
  2482 
  2198 	/**
  2483 	if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
  2199 	 * Filter whether to retrieve the avatar URL early.
  2484 		$id_or_email = get_comment( $id_or_email );
       
  2485 	}
       
  2486 
       
  2487 	/**
       
  2488 	 * Filters whether to retrieve the avatar URL early.
  2200 	 *
  2489 	 *
  2201 	 * Passing a non-null value will effectively short-circuit get_avatar(), passing
  2490 	 * Passing a non-null value will effectively short-circuit get_avatar(), passing
  2202 	 * the value through the {@see 'pre_get_avatar'} filter and returning early.
  2491 	 * the value through the {@see 'get_avatar'} filter and returning early.
  2203 	 *
  2492 	 *
  2204 	 * @since 4.2.0
  2493 	 * @since 4.2.0
  2205 	 *
  2494 	 *
  2206 	 * @param string            $avatar      HTML for the user's avatar. Default null.
  2495 	 * @param string $avatar      HTML for the user's avatar. Default null.
  2207 	 * @param int|object|string $id_or_email A user ID, email address, or comment object.
  2496 	 * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
  2208 	 * @param array             $args        Arguments passed to get_avatar_url(), after processing.
  2497 	 *                            user email, WP_User object, WP_Post object, or WP_Comment object.
       
  2498 	 * @param array  $args        Arguments passed to get_avatar_url(), after processing.
  2209 	 */
  2499 	 */
  2210 	$avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args );
  2500 	$avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args );
  2211 
  2501 
  2212 	if ( ! is_null( $avatar ) ) {
  2502 	if ( ! is_null( $avatar ) ) {
  2213 		/** This filter is documented in wp-includes/pluggable.php */
  2503 		/** This filter is documented in wp-includes/pluggable.php */
  2223 	$args = get_avatar_data( $id_or_email, $args );
  2513 	$args = get_avatar_data( $id_or_email, $args );
  2224 
  2514 
  2225 	$url = $args['url'];
  2515 	$url = $args['url'];
  2226 
  2516 
  2227 	if ( ! $url || is_wp_error( $url ) ) {
  2517 	if ( ! $url || is_wp_error( $url ) ) {
  2228         return false;
  2518 		return false;
  2229 	}
  2519 	}
  2230 
  2520 
  2231 	$class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
  2521 	$class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
  2232 
  2522 
  2233 	if ( ! $args['found_avatar'] || $args['force_default'] ) {
  2523 	if ( ! $args['found_avatar'] || $args['force_default'] ) {
  2234         $class[] = 'avatar-default';
  2524 		$class[] = 'avatar-default';
  2235 	}
  2525 	}
  2236 
  2526 
  2237 	if ( $args['class'] ) {
  2527 	if ( $args['class'] ) {
  2238 		if ( is_array( $args['class'] ) ) {
  2528 		if ( is_array( $args['class'] ) ) {
  2239 			$class = array_merge( $class, $args['class'] );
  2529 			$class = array_merge( $class, $args['class'] );
  2244 
  2534 
  2245 	$avatar = sprintf(
  2535 	$avatar = sprintf(
  2246 		"<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>",
  2536 		"<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>",
  2247 		esc_attr( $args['alt'] ),
  2537 		esc_attr( $args['alt'] ),
  2248 		esc_url( $url ),
  2538 		esc_url( $url ),
  2249 		esc_attr( "$url2x 2x" ),
  2539 		esc_url( $url2x ) . ' 2x',
  2250 		esc_attr( join( ' ', $class ) ),
  2540 		esc_attr( join( ' ', $class ) ),
  2251 		(int) $args['height'],
  2541 		(int) $args['height'],
  2252 		(int) $args['width'],
  2542 		(int) $args['width'],
  2253 		$args['extra_attr']
  2543 		$args['extra_attr']
  2254 	);
  2544 	);
  2255 
  2545 
  2256 	/**
  2546 	/**
  2257 	 * Filter the avatar to retrieve.
  2547 	 * Filters the avatar to retrieve.
  2258 	 *
  2548 	 *
  2259 	 * @since 2.5.0
  2549 	 * @since 2.5.0
  2260 	 * @since 4.2.0 The `$args` parameter was added.
  2550 	 * @since 4.2.0 The `$args` parameter was added.
  2261 	 *
  2551 	 *
  2262 	 * @param string            $avatar      &lt;img&gt; tag for the user's avatar.
  2552 	 * @param string $avatar      &lt;img&gt; tag for the user's avatar.
  2263 	 * @param int|object|string $id_or_email A user ID, email address, or comment object.
  2553 	 * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
  2264 	 * @param int               $size        Square avatar width and height in pixels to retrieve.
  2554 	 *                            user email, WP_User object, WP_Post object, or WP_Comment object.
  2265 	 * @param string            $alt         Alternative text to use in the avatar image tag.
  2555 	 * @param int    $size        Square avatar width and height in pixels to retrieve.
  2266 	 *                                       Default empty.
  2556 	 * @param string $default     URL for the default image or a default type. Accepts '404', 'retro', 'monsterid',
  2267 	 * @param array             $args        Arguments passed to get_avatar_data(), after processing.
  2557 	 *                            'wavatar', 'indenticon','mystery' (or 'mm', or 'mysteryman'), 'blank', or 'gravatar_default'.
       
  2558 	 *                            Default is the value of the 'avatar_default' option, with a fallback of 'mystery'.
       
  2559 	 * @param string $alt         Alternative text to use in the avatar image tag. Default empty.
       
  2560 	 * @param array  $args        Arguments passed to get_avatar_data(), after processing.
  2268 	 */
  2561 	 */
  2269 	return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );
  2562 	return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );
  2270 }
  2563 }
  2271 endif;
  2564 endif;
  2272 
  2565 
  2291  *
  2584  *
  2292  * @see wp_parse_args() Used to change defaults to user defined settings.
  2585  * @see wp_parse_args() Used to change defaults to user defined settings.
  2293  * @uses Text_Diff
  2586  * @uses Text_Diff
  2294  * @uses WP_Text_Diff_Renderer_Table
  2587  * @uses WP_Text_Diff_Renderer_Table
  2295  *
  2588  *
  2296  * @param string $left_string "old" (left) version of string
  2589  * @param string       $left_string  "old" (left) version of string
  2297  * @param string $right_string "new" (right) version of string
  2590  * @param string       $right_string "new" (right) version of string
  2298  * @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults.
  2591  * @param string|array $args         Optional. Change 'title', 'title_left', and 'title_right' defaults.
  2299  * @return string Empty string if strings are equivalent or HTML with differences.
  2592  * @return string Empty string if strings are equivalent or HTML with differences.
  2300  */
  2593  */
  2301 function wp_text_diff( $left_string, $right_string, $args = null ) {
  2594 function wp_text_diff( $left_string, $right_string, $args = null ) {
  2302 	$defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
  2595 	$defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
  2303 	$args = wp_parse_args( $args, $defaults );
  2596 	$args = wp_parse_args( $args, $defaults );
  2304 
  2597 
  2305 	if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
  2598 	if ( ! class_exists( 'WP_Text_Diff_Renderer_Table', false ) )
  2306 		require( ABSPATH . WPINC . '/wp-diff.php' );
  2599 		require( ABSPATH . WPINC . '/wp-diff.php' );
  2307 
  2600 
  2308 	$left_string  = normalize_whitespace($left_string);
  2601 	$left_string  = normalize_whitespace($left_string);
  2309 	$right_string = normalize_whitespace($right_string);
  2602 	$right_string = normalize_whitespace($right_string);
  2310 
  2603