wp/wp-includes/pluggable.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    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 object $current_user The current user object which holds the user data.
    21  * @uses do_action() Calls 'set_current_user' hook after setting the current user.
       
    22  *
    21  *
    23  * @param int $id User ID
    22  * @param int $id User ID
    24  * @param string $name User's username
    23  * @param string $name User's username
    25  * @return WP_User Current user User object
    24  * @return WP_User Current user User object
    26  */
    25  */
    32 
    31 
    33 	$current_user = new WP_User( $id, $name );
    32 	$current_user = new WP_User( $id, $name );
    34 
    33 
    35 	setup_userdata( $current_user->ID );
    34 	setup_userdata( $current_user->ID );
    36 
    35 
    37 	do_action('set_current_user');
    36 	/**
       
    37 	 * Fires after the current user is set.
       
    38 	 *
       
    39 	 * @since 2.0.1
       
    40 	 */
       
    41 	do_action( 'set_current_user' );
    38 
    42 
    39 	return $current_user;
    43 	return $current_user;
    40 }
    44 }
    41 endif;
    45 endif;
    42 
    46 
    60 if ( !function_exists('get_currentuserinfo') ) :
    64 if ( !function_exists('get_currentuserinfo') ) :
    61 /**
    65 /**
    62  * Populate global variables with information about the currently logged in user.
    66  * Populate global variables with information about the currently logged in user.
    63  *
    67  *
    64  * Will set the current user, if the current user is not set. The current user
    68  * Will set the current user, if the current user is not set. The current user
    65  * will be set to the logged in person. If no user is logged in, then it will
    69  * will be set to the logged-in person. If no user is logged-in, then it will
    66  * set the current user to 0, which is invalid and won't have any permissions.
    70  * set the current user to 0, which is invalid and won't have any permissions.
    67  *
    71  *
    68  * @since 0.71
    72  * @since 0.71
       
    73  *
    69  * @uses $current_user Checks if the current user is set
    74  * @uses $current_user Checks if the current user is set
    70  * @uses wp_validate_auth_cookie() Retrieves current logged in user.
    75  *
    71  *
    76  * @return null|false False on XML-RPC Request and invalid auth cookie. Null when current user set.
    72  * @return bool|null False on XMLRPC Request and invalid auth cookie. Null when current user set
       
    73  */
    77  */
    74 function get_currentuserinfo() {
    78 function get_currentuserinfo() {
    75 	global $current_user;
    79 	global $current_user;
    76 
    80 
    77 	if ( ! empty( $current_user ) ) {
    81 	if ( ! empty( $current_user ) ) {
    95 	if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
    99 	if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
    96 		wp_set_current_user( 0 );
   100 		wp_set_current_user( 0 );
    97 		return false;
   101 		return false;
    98 	}
   102 	}
    99 
   103 
   100 	if ( ! $user = wp_validate_auth_cookie() ) {
   104 	/**
   101 		 if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[LOGGED_IN_COOKIE] ) || !$user = wp_validate_auth_cookie( $_COOKIE[LOGGED_IN_COOKIE], 'logged_in' ) ) {
   105 	 * Filter the current user.
   102 		 	wp_set_current_user( 0 );
   106 	 *
   103 		 	return false;
   107 	 * The default filters use this to determine the current user from the
   104 		 }
   108 	 * request's cookies, if available.
   105 	}
   109 	 *
   106 
   110 	 * Returning a value of false will effectively short-circuit setting
   107 	wp_set_current_user( $user );
   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 );
   108 }
   124 }
   109 endif;
   125 endif;
   110 
   126 
   111 if ( !function_exists('get_userdata') ) :
   127 if ( !function_exists('get_userdata') ) :
   112 /**
   128 /**
   193  *
   209  *
   194  * The default charset is based on the charset used on the blog. The charset can
   210  * The default charset is based on the charset used on the blog. The charset can
   195  * be set using the 'wp_mail_charset' filter.
   211  * be set using the 'wp_mail_charset' filter.
   196  *
   212  *
   197  * @since 1.2.1
   213  * @since 1.2.1
   198  * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
   214  *
   199  * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
       
   200  * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
       
   201  * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
       
   202  * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
       
   203  * @uses do_action_ref_array() Calls 'phpmailer_init' hook on the reference to
       
   204  *		phpmailer object.
       
   205  * @uses PHPMailer
   215  * @uses PHPMailer
   206  *
   216  *
   207  * @param string|array $to Array or comma-separated list of email addresses to send message.
   217  * @param string|array $to Array or comma-separated list of email addresses to send message.
   208  * @param string $subject Email subject
   218  * @param string $subject Email subject
   209  * @param string $message Message contents
   219  * @param string $message Message contents
   211  * @param string|array $attachments Optional. Files to attach.
   221  * @param string|array $attachments Optional. Files to attach.
   212  * @return bool Whether the email contents were sent successfully.
   222  * @return bool Whether the email contents were sent successfully.
   213  */
   223  */
   214 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
   224 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
   215 	// Compact the input, apply the filters, and extract them back out
   225 	// Compact the input, apply the filters, and extract them back out
   216 	extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
   226 
   217 
   227 	/**
   218 	if ( !is_array($attachments) )
   228 	 * Filter the wp_mail() arguments.
       
   229 	 *
       
   230 	 * @since 2.2.0
       
   231 	 *
       
   232 	 * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
       
   233 	 *                    subject, message, headers, and attachments values.
       
   234 	 */
       
   235 	$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
       
   236 
       
   237 	if ( isset( $atts['to'] ) ) {
       
   238 		$to = $atts['to'];
       
   239 	}
       
   240 
       
   241 	if ( isset( $atts['subject'] ) ) {
       
   242 		$subject = $atts['subject'];
       
   243 	}
       
   244 
       
   245 	if ( isset( $atts['message'] ) ) {
       
   246 		$message = $atts['message'];
       
   247 	}
       
   248 
       
   249 	if ( isset( $atts['headers'] ) ) {
       
   250 		$headers = $atts['headers'];
       
   251 	}
       
   252 
       
   253 	if ( isset( $atts['attachments'] ) ) {
       
   254 		$attachments = $atts['attachments'];
       
   255 	}
       
   256 
       
   257 	if ( ! is_array( $attachments ) ) {
   219 		$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
   258 		$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
   220 
   259 	}
   221 	global $phpmailer;
   260 	global $phpmailer;
   222 
   261 
   223 	// (Re)create it, if it's gone missing
   262 	// (Re)create it, if it's gone missing
   224 	if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
   263 	if ( ! ( $phpmailer instanceof PHPMailer ) ) {
   225 		require_once ABSPATH . WPINC . '/class-phpmailer.php';
   264 		require_once ABSPATH . WPINC . '/class-phpmailer.php';
   226 		require_once ABSPATH . WPINC . '/class-smtp.php';
   265 		require_once ABSPATH . WPINC . '/class-smtp.php';
   227 		$phpmailer = new PHPMailer( true );
   266 		$phpmailer = new PHPMailer( true );
   228 	}
   267 	}
   229 
   268 
   261 				$content = trim( $content );
   300 				$content = trim( $content );
   262 
   301 
   263 				switch ( strtolower( $name ) ) {
   302 				switch ( strtolower( $name ) ) {
   264 					// Mainly for legacy -- process a From: header if it's there
   303 					// Mainly for legacy -- process a From: header if it's there
   265 					case 'from':
   304 					case 'from':
   266 						if ( strpos($content, '<' ) !== false ) {
   305 						$bracket_pos = strpos( $content, '<' );
   267 							// So... making my life hard again?
   306 						if ( $bracket_pos !== false ) {
   268 							$from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
   307 							// Text before the bracketed email is the "From" name.
   269 							$from_name = str_replace( '"', '', $from_name );
   308 							if ( $bracket_pos > 0 ) {
   270 							$from_name = trim( $from_name );
   309 								$from_name = substr( $content, 0, $bracket_pos - 1 );
   271 
   310 								$from_name = str_replace( '"', '', $from_name );
   272 							$from_email = substr( $content, strpos( $content, '<' ) + 1 );
   311 								$from_name = trim( $from_name );
       
   312 							}
       
   313 
       
   314 							$from_email = substr( $content, $bracket_pos + 1 );
   273 							$from_email = str_replace( '>', '', $from_email );
   315 							$from_email = str_replace( '>', '', $from_email );
   274 							$from_email = trim( $from_email );
   316 							$from_email = trim( $from_email );
   275 						} else {
   317 
       
   318 						// Avoid setting an empty $from_email.
       
   319 						} elseif ( '' !== trim( $content ) ) {
   276 							$from_email = trim( $content );
   320 							$from_email = trim( $content );
   277 						}
   321 						}
   278 						break;
   322 						break;
   279 					case 'content-type':
   323 					case 'content-type':
   280 						if ( strpos( $content, ';' ) !== false ) {
   324 						if ( strpos( $content, ';' ) !== false ) {
   281 							list( $type, $charset ) = explode( ';', $content );
   325 							list( $type, $charset_content ) = explode( ';', $content );
   282 							$content_type = trim( $type );
   326 							$content_type = trim( $type );
   283 							if ( false !== stripos( $charset, 'charset=' ) ) {
   327 							if ( false !== stripos( $charset_content, 'charset=' ) ) {
   284 								$charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
   328 								$charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
   285 							} elseif ( false !== stripos( $charset, 'boundary=' ) ) {
   329 							} elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
   286 								$boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
   330 								$boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
   287 								$charset = '';
   331 								$charset = '';
   288 							}
   332 							}
   289 						} else {
   333 
       
   334 						// Avoid setting an empty $content_type.
       
   335 						} elseif ( '' !== trim( $content ) ) {
   290 							$content_type = trim( $content );
   336 							$content_type = trim( $content );
   291 						}
   337 						}
   292 						break;
   338 						break;
   293 					case 'cc':
   339 					case 'cc':
   294 						$cc = array_merge( (array) $cc, explode( ',', $content ) );
   340 						$cc = array_merge( (array) $cc, explode( ',', $content ) );
   304 			}
   350 			}
   305 		}
   351 		}
   306 	}
   352 	}
   307 
   353 
   308 	// Empty out the values that may be set
   354 	// Empty out the values that may be set
   309 	$phpmailer->ClearAddresses();
       
   310 	$phpmailer->ClearAllRecipients();
   355 	$phpmailer->ClearAllRecipients();
   311 	$phpmailer->ClearAttachments();
   356 	$phpmailer->ClearAttachments();
   312 	$phpmailer->ClearBCCs();
       
   313 	$phpmailer->ClearCCs();
       
   314 	$phpmailer->ClearCustomHeaders();
   357 	$phpmailer->ClearCustomHeaders();
   315 	$phpmailer->ClearReplyTos();
   358 	$phpmailer->ClearReplyTos();
   316 
   359 
   317 	// From email and name
   360 	// From email and name
   318 	// If we don't have a name from the input headers
   361 	// If we don't have a name from the input headers
   321 
   364 
   322 	/* If we don't have an email from the input headers default to wordpress@$sitename
   365 	/* If we don't have an email from the input headers default to wordpress@$sitename
   323 	 * Some hosts will block outgoing mail from this address if it doesn't exist but
   366 	 * Some hosts will block outgoing mail from this address if it doesn't exist but
   324 	 * there's no easy alternative. Defaulting to admin_email might appear to be another
   367 	 * there's no easy alternative. Defaulting to admin_email might appear to be another
   325 	 * option but some hosts may refuse to relay mail from an unknown domain. See
   368 	 * option but some hosts may refuse to relay mail from an unknown domain. See
   326 	 * http://trac.wordpress.org/ticket/5007.
   369 	 * https://core.trac.wordpress.org/ticket/5007.
   327 	 */
   370 	 */
   328 
   371 
   329 	if ( !isset( $from_email ) ) {
   372 	if ( !isset( $from_email ) ) {
   330 		// Get the site domain and get rid of www.
   373 		// Get the site domain and get rid of www.
   331 		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
   374 		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
   334 		}
   377 		}
   335 
   378 
   336 		$from_email = 'wordpress@' . $sitename;
   379 		$from_email = 'wordpress@' . $sitename;
   337 	}
   380 	}
   338 
   381 
   339 	// Plugin authors can override the potentially troublesome default
   382 	/**
   340 	$phpmailer->From     = apply_filters( 'wp_mail_from'     , $from_email );
   383 	 * Filter the email address to send from.
   341 	$phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name  );
   384 	 *
       
   385 	 * @since 2.2.0
       
   386 	 *
       
   387 	 * @param string $from_email Email address to send from.
       
   388 	 */
       
   389 	$phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
       
   390 
       
   391 	/**
       
   392 	 * Filter the name to associate with the "from" email address.
       
   393 	 *
       
   394 	 * @since 2.3.0
       
   395 	 *
       
   396 	 * @param string $from_name Name associated with the "from" email address.
       
   397 	 */
       
   398 	$phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
   342 
   399 
   343 	// Set destination addresses
   400 	// Set destination addresses
   344 	if ( !is_array( $to ) )
   401 	if ( !is_array( $to ) )
   345 		$to = explode( ',', $to );
   402 		$to = explode( ',', $to );
   346 
   403 
   407 	// Set Content-Type and charset
   464 	// Set Content-Type and charset
   408 	// If we don't have a content-type from the input headers
   465 	// If we don't have a content-type from the input headers
   409 	if ( !isset( $content_type ) )
   466 	if ( !isset( $content_type ) )
   410 		$content_type = 'text/plain';
   467 		$content_type = 'text/plain';
   411 
   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 	 */
   412 	$content_type = apply_filters( 'wp_mail_content_type', $content_type );
   476 	$content_type = apply_filters( 'wp_mail_content_type', $content_type );
   413 
   477 
   414 	$phpmailer->ContentType = $content_type;
   478 	$phpmailer->ContentType = $content_type;
   415 
   479 
   416 	// Set whether it's plaintext, depending on $content_type
   480 	// Set whether it's plaintext, depending on $content_type
   420 	// If we don't have a charset from the input headers
   484 	// If we don't have a charset from the input headers
   421 	if ( !isset( $charset ) )
   485 	if ( !isset( $charset ) )
   422 		$charset = get_bloginfo( 'charset' );
   486 		$charset = get_bloginfo( 'charset' );
   423 
   487 
   424 	// Set the content-type and charset
   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 	 */
   425 	$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
   497 	$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
   426 
   498 
   427 	// Set custom headers
   499 	// Set custom headers
   428 	if ( !empty( $headers ) ) {
   500 	if ( !empty( $headers ) ) {
   429 		foreach( (array) $headers as $name => $content ) {
   501 		foreach( (array) $headers as $name => $content ) {
   442 				continue;
   514 				continue;
   443 			}
   515 			}
   444 		}
   516 		}
   445 	}
   517 	}
   446 
   518 
       
   519 	/**
       
   520 	 * Fires after PHPMailer is initialized.
       
   521 	 *
       
   522 	 * @since 2.2.0
       
   523 	 *
       
   524 	 * @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference.
       
   525 	 */
   447 	do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
   526 	do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
   448 
   527 
   449 	// Send!
   528 	// Send!
   450 	try {
   529 	try {
   451 		return $phpmailer->Send();
   530 		return $phpmailer->Send();
   467  */
   546  */
   468 function wp_authenticate($username, $password) {
   547 function wp_authenticate($username, $password) {
   469 	$username = sanitize_user($username);
   548 	$username = sanitize_user($username);
   470 	$password = trim($password);
   549 	$password = trim($password);
   471 
   550 
   472 	$user = apply_filters('authenticate', null, $username, $password);
   551 	/**
       
   552 	 * Filter the user to authenticate.
       
   553 	 *
       
   554 	 * If a non-null value is passed, the filter will effectively short-circuit
       
   555 	 * authentication, returning an error instead.
       
   556 	 *
       
   557 	 * @since 2.8.0
       
   558 	 *
       
   559 	 * @param null|WP_User $user     User to authenticate.
       
   560 	 * @param string       $username User login.
       
   561 	 * @param string       $password User password
       
   562 	 */
       
   563 	$user = apply_filters( 'authenticate', null, $username, $password );
   473 
   564 
   474 	if ( $user == null ) {
   565 	if ( $user == null ) {
   475 		// TODO what should the error message be? (Or would these even happen?)
   566 		// TODO what should the error message be? (Or would these even happen?)
   476 		// Only needed if all authentication handlers fail to return anything.
   567 		// Only needed if all authentication handlers fail to return anything.
   477 		$user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
   568 		$user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
   478 	}
   569 	}
   479 
   570 
   480 	$ignore_codes = array('empty_username', 'empty_password');
   571 	$ignore_codes = array('empty_username', 'empty_password');
   481 
   572 
   482 	if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
   573 	if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
   483 		do_action('wp_login_failed', $username);
   574 		/**
       
   575 		 * Fires after a user login has failed.
       
   576 		 *
       
   577 		 * @since 2.5.0
       
   578 		 *
       
   579 		 * @param string $username User login.
       
   580 		 */
       
   581 		do_action( 'wp_login_failed', $username );
   484 	}
   582 	}
   485 
   583 
   486 	return $user;
   584 	return $user;
   487 }
   585 }
   488 endif;
   586 endif;
   492  * Log the current user out.
   590  * Log the current user out.
   493  *
   591  *
   494  * @since 2.5.0
   592  * @since 2.5.0
   495  */
   593  */
   496 function wp_logout() {
   594 function wp_logout() {
       
   595 	wp_destroy_current_session();
   497 	wp_clear_auth_cookie();
   596 	wp_clear_auth_cookie();
   498 	do_action('wp_logout');
   597 
       
   598 	/**
       
   599 	 * Fires after a user is logged-out.
       
   600 	 *
       
   601 	 * @since 1.5.0
       
   602 	 */
       
   603 	do_action( 'wp_logout' );
   499 }
   604 }
   500 endif;
   605 endif;
   501 
   606 
   502 if ( !function_exists('wp_validate_auth_cookie') ) :
   607 if ( !function_exists('wp_validate_auth_cookie') ) :
   503 /**
   608 /**
   507  * pulling in the contents (if $cookie is not used).
   612  * pulling in the contents (if $cookie is not used).
   508  *
   613  *
   509  * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
   614  * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
   510  * should be and compares the two.
   615  * should be and compares the two.
   511  *
   616  *
   512  * @since 2.5
   617  * @since 2.5.0
   513  *
   618  *
   514  * @param string $cookie Optional. If used, will validate contents instead of cookie's
   619  * @param string $cookie Optional. If used, will validate contents instead of cookie's
   515  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   620  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   516  * @return bool|int False if invalid cookie, User ID if valid.
   621  * @return bool|int False if invalid cookie, User ID if valid.
   517  */
   622  */
   518 function wp_validate_auth_cookie($cookie = '', $scheme = '') {
   623 function wp_validate_auth_cookie($cookie = '', $scheme = '') {
   519 	if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
   624 	if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
   520 		do_action('auth_cookie_malformed', $cookie, $scheme);
   625 		/**
       
   626 		 * Fires if an authentication cookie is malformed.
       
   627 		 *
       
   628 		 * @since 2.7.0
       
   629 		 *
       
   630 		 * @param string $cookie Malformed auth cookie.
       
   631 		 * @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth',
       
   632 		 *                       or 'logged_in'.
       
   633 		 */
       
   634 		do_action( 'auth_cookie_malformed', $cookie, $scheme );
   521 		return false;
   635 		return false;
   522 	}
   636 	}
   523 
   637 
   524 	extract($cookie_elements, EXTR_OVERWRITE);
   638 	$scheme = $cookie_elements['scheme'];
   525 
   639 	$username = $cookie_elements['username'];
   526 	$expired = $expiration;
   640 	$hmac = $cookie_elements['hmac'];
       
   641 	$token = $cookie_elements['token'];
       
   642 	$expired = $expiration = $cookie_elements['expiration'];
   527 
   643 
   528 	// Allow a grace period for POST and AJAX requests
   644 	// Allow a grace period for POST and AJAX requests
   529 	if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
   645 	if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
   530 		$expired += HOUR_IN_SECONDS;
   646 		$expired += HOUR_IN_SECONDS;
       
   647 	}
   531 
   648 
   532 	// Quick check to see if an honest cookie has expired
   649 	// Quick check to see if an honest cookie has expired
   533 	if ( $expired < time() ) {
   650 	if ( $expired < time() ) {
   534 		do_action('auth_cookie_expired', $cookie_elements);
   651 		/**
       
   652 		 * Fires once an authentication cookie has expired.
       
   653 		 *
       
   654 		 * @since 2.7.0
       
   655 		 *
       
   656 		 * @param array $cookie_elements An array of data for the authentication cookie.
       
   657 		 */
       
   658 		do_action( 'auth_cookie_expired', $cookie_elements );
   535 		return false;
   659 		return false;
   536 	}
   660 	}
   537 
   661 
   538 	$user = get_user_by('login', $username);
   662 	$user = get_user_by('login', $username);
   539 	if ( ! $user ) {
   663 	if ( ! $user ) {
   540 		do_action('auth_cookie_bad_username', $cookie_elements);
   664 		/**
       
   665 		 * Fires if a bad username is entered in the user authentication process.
       
   666 		 *
       
   667 		 * @since 2.7.0
       
   668 		 *
       
   669 		 * @param array $cookie_elements An array of data for the authentication cookie.
       
   670 		 */
       
   671 		do_action( 'auth_cookie_bad_username', $cookie_elements );
   541 		return false;
   672 		return false;
   542 	}
   673 	}
   543 
   674 
   544 	$pass_frag = substr($user->user_pass, 8, 4);
   675 	$pass_frag = substr($user->user_pass, 8, 4);
   545 
   676 
   546 	$key = wp_hash($username . $pass_frag . '|' . $expiration, $scheme);
   677 	$key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
   547 	$hash = hash_hmac('md5', $username . '|' . $expiration, $key);
   678 
   548 
   679 	// If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
   549 	if ( $hmac != $hash ) {
   680 	$algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
   550 		do_action('auth_cookie_bad_hash', $cookie_elements);
   681 	$hash = hash_hmac( $algo, $username . '|' . $expiration . '|' . $token, $key );
       
   682 
       
   683 	if ( ! hash_equals( $hash, $hmac ) ) {
       
   684 		/**
       
   685 		 * Fires if a bad authentication cookie hash is encountered.
       
   686 		 *
       
   687 		 * @since 2.7.0
       
   688 		 *
       
   689 		 * @param array $cookie_elements An array of data for the authentication cookie.
       
   690 		 */
       
   691 		do_action( 'auth_cookie_bad_hash', $cookie_elements );
   551 		return false;
   692 		return false;
   552 	}
   693 	}
   553 
   694 
   554 	if ( $expiration < time() ) // AJAX/POST grace period set above
   695 	$manager = WP_Session_Tokens::get_instance( $user->ID );
       
   696 	if ( ! $manager->verify( $token ) ) {
       
   697 		do_action( 'auth_cookie_bad_session_token', $cookie_elements );
       
   698 		return false;
       
   699 	}
       
   700 
       
   701 	// AJAX/POST grace period set above
       
   702 	if ( $expiration < time() ) {
   555 		$GLOBALS['login_grace_period'] = 1;
   703 		$GLOBALS['login_grace_period'] = 1;
   556 
   704 	}
   557 	do_action('auth_cookie_valid', $cookie_elements, $user);
   705 
       
   706 	/**
       
   707 	 * Fires once an authentication cookie has been validated.
       
   708 	 *
       
   709 	 * @since 2.7.0
       
   710 	 *
       
   711 	 * @param array   $cookie_elements An array of data for the authentication cookie.
       
   712 	 * @param WP_User $user            User object.
       
   713 	 */
       
   714 	do_action( 'auth_cookie_valid', $cookie_elements, $user );
   558 
   715 
   559 	return $user->ID;
   716 	return $user->ID;
   560 }
   717 }
   561 endif;
   718 endif;
   562 
   719 
   563 if ( !function_exists('wp_generate_auth_cookie') ) :
   720 if ( !function_exists('wp_generate_auth_cookie') ) :
   564 /**
   721 /**
   565  * Generate authentication cookie contents.
   722  * Generate authentication cookie contents.
   566  *
   723  *
   567  * @since 2.5
   724  * @since 2.5.0
   568  * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID
       
   569  *		and expiration of cookie.
       
   570  *
   725  *
   571  * @param int $user_id User ID
   726  * @param int $user_id User ID
   572  * @param int $expiration Cookie expiration in seconds
   727  * @param int $expiration Cookie expiration in seconds
   573  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   728  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   574  * @return string Authentication cookie contents
   729  * @param string $token User's session token to use for this cookie
   575  */
   730  * @return string Authentication cookie contents. Empty string if user does not exist.
   576 function wp_generate_auth_cookie($user_id, $expiration, $scheme = 'auth') {
   731  */
       
   732 function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) {
   577 	$user = get_userdata($user_id);
   733 	$user = get_userdata($user_id);
       
   734 	if ( ! $user ) {
       
   735 		return '';
       
   736 	}
       
   737 
       
   738 	if ( ! $token ) {
       
   739 		$manager = WP_Session_Tokens::get_instance( $user_id );
       
   740 		$token = $manager->create( $expiration );
       
   741 	}
   578 
   742 
   579 	$pass_frag = substr($user->user_pass, 8, 4);
   743 	$pass_frag = substr($user->user_pass, 8, 4);
   580 
   744 
   581 	$key = wp_hash($user->user_login . $pass_frag . '|' . $expiration, $scheme);
   745 	$key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
   582 	$hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key);
   746 
   583 
   747 	// If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
   584 	$cookie = $user->user_login . '|' . $expiration . '|' . $hash;
   748 	$algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
   585 
   749 	$hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key );
   586 	return apply_filters('auth_cookie', $cookie, $user_id, $expiration, $scheme);
   750 
       
   751 	$cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash;
       
   752 
       
   753 	/**
       
   754 	 * Filter the authentication cookie.
       
   755 	 *
       
   756 	 * @since 2.5.0
       
   757 	 *
       
   758 	 * @param string $cookie     Authentication cookie.
       
   759 	 * @param int    $user_id    User ID.
       
   760 	 * @param int    $expiration Authentication cookie expiration in seconds.
       
   761 	 * @param string $scheme     Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'.
       
   762 	 * @param string $token      User's session token used.
       
   763 	 */
       
   764 	return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );
   587 }
   765 }
   588 endif;
   766 endif;
   589 
   767 
   590 if ( !function_exists('wp_parse_auth_cookie') ) :
   768 if ( !function_exists('wp_parse_auth_cookie') ) :
   591 /**
   769 /**
   592  * Parse a cookie into its components
   770  * Parse a cookie into its components
   593  *
   771  *
   594  * @since 2.7
   772  * @since 2.7.0
   595  *
   773  *
   596  * @param string $cookie
   774  * @param string $cookie
   597  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   775  * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
   598  * @return array Authentication cookie components
   776  * @return array Authentication cookie components
   599  */
   777  */
   623 			return false;
   801 			return false;
   624 		$cookie = $_COOKIE[$cookie_name];
   802 		$cookie = $_COOKIE[$cookie_name];
   625 	}
   803 	}
   626 
   804 
   627 	$cookie_elements = explode('|', $cookie);
   805 	$cookie_elements = explode('|', $cookie);
   628 	if ( count($cookie_elements) != 3 )
   806 	if ( count( $cookie_elements ) !== 4 ) {
   629 		return false;
   807 		return false;
   630 
   808 	}
   631 	list($username, $expiration, $hmac) = $cookie_elements;
   809 
   632 
   810 	list( $username, $expiration, $token, $hmac ) = $cookie_elements;
   633 	return compact('username', 'expiration', 'hmac', 'scheme');
   811 
       
   812 	return compact( 'username', 'expiration', 'token', 'hmac', 'scheme' );
   634 }
   813 }
   635 endif;
   814 endif;
   636 
   815 
   637 if ( !function_exists('wp_set_auth_cookie') ) :
   816 if ( !function_exists('wp_set_auth_cookie') ) :
   638 /**
   817 /**
   639  * Sets the authentication cookies based User ID.
   818  * Sets the authentication cookies based on user ID.
   640  *
   819  *
   641  * The $remember parameter increases the time that the cookie will be kept. The
   820  * The $remember parameter increases the time that the cookie will be kept. The
   642  * default the cookie is kept without remembering is two days. When $remember is
   821  * default the cookie is kept without remembering is two days. When $remember is
   643  * set, the cookies will be kept for 14 days or two weeks.
   822  * set, the cookies will be kept for 14 days or two weeks.
   644  *
   823  *
   645  * @since 2.5
   824  * @since 2.5.0
   646  *
   825  *
   647  * @param int $user_id User ID
   826  * @param int $user_id User ID
   648  * @param bool $remember Whether to remember the user
   827  * @param bool $remember Whether to remember the user
       
   828  * @param mixed $secure  Whether the admin cookies should only be sent over HTTPS.
       
   829  *                       Default is_ssl().
   649  */
   830  */
   650 function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
   831 function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
   651 	if ( $remember ) {
   832 	if ( $remember ) {
   652 		$expiration = time() + apply_filters('auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember);
   833 		/**
   653 		// Ensure the browser will continue to send the cookie after the expiration time is reached.
   834 		 * Filter the duration of the authentication cookie expiration period.
   654 		// Needed for the login grace period in wp_validate_auth_cookie().
   835 		 *
       
   836 		 * @since 2.8.0
       
   837 		 *
       
   838 		 * @param int  $length   Duration of the expiration period in seconds.
       
   839 		 * @param int  $user_id  User ID.
       
   840 		 * @param bool $remember Whether to remember the user login. Default false.
       
   841 		 */
       
   842 		$expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember );
       
   843 
       
   844 		/*
       
   845 		 * Ensure the browser will continue to send the cookie after the expiration time is reached.
       
   846 		 * Needed for the login grace period in wp_validate_auth_cookie().
       
   847 		 */
   655 		$expire = $expiration + ( 12 * HOUR_IN_SECONDS );
   848 		$expire = $expiration + ( 12 * HOUR_IN_SECONDS );
   656 	} else {
   849 	} else {
   657 		$expiration = time() + apply_filters('auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember);
   850 		/** This filter is documented in wp-includes/pluggable.php */
       
   851 		$expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember );
   658 		$expire = 0;
   852 		$expire = 0;
   659 	}
   853 	}
   660 
   854 
   661 	if ( '' === $secure )
   855 	if ( '' === $secure ) {
   662 		$secure = is_ssl();
   856 		$secure = is_ssl();
   663 
   857 	}
   664 	$secure = apply_filters('secure_auth_cookie', $secure, $user_id);
   858 
   665 	$secure_logged_in_cookie = apply_filters('secure_logged_in_cookie', false, $user_id, $secure);
   859 	// Frontend 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 );
       
   861 
       
   862 	/**
       
   863 	 * Filter whether the connection is secure.
       
   864 	 *
       
   865 	 * @since 3.1.0
       
   866 	 *
       
   867 	 * @param bool $secure  Whether the connection is secure.
       
   868 	 * @param int  $user_id User ID.
       
   869 	 */
       
   870 	$secure = apply_filters( 'secure_auth_cookie', $secure, $user_id );
       
   871 
       
   872 	/**
       
   873 	 * Filter whether to use a secure cookie when logged-in.
       
   874 	 *
       
   875 	 * @since 3.1.0
       
   876 	 *
       
   877 	 * @param bool $secure_logged_in_cookie Whether to use a secure cookie when logged-in.
       
   878 	 * @param int  $user_id                 User ID.
       
   879 	 * @param bool $secure                  Whether the connection is secure.
       
   880 	 */
       
   881 	$secure_logged_in_cookie = apply_filters( 'secure_logged_in_cookie', $secure_logged_in_cookie, $user_id, $secure );
   666 
   882 
   667 	if ( $secure ) {
   883 	if ( $secure ) {
   668 		$auth_cookie_name = SECURE_AUTH_COOKIE;
   884 		$auth_cookie_name = SECURE_AUTH_COOKIE;
   669 		$scheme = 'secure_auth';
   885 		$scheme = 'secure_auth';
   670 	} else {
   886 	} else {
   671 		$auth_cookie_name = AUTH_COOKIE;
   887 		$auth_cookie_name = AUTH_COOKIE;
   672 		$scheme = 'auth';
   888 		$scheme = 'auth';
   673 	}
   889 	}
   674 
   890 
   675 	$auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme);
   891 	$manager = WP_Session_Tokens::get_instance( $user_id );
   676 	$logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
   892 	$token = $manager->create( $expiration );
   677 
   893 
   678 	do_action('set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme);
   894 	$auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
   679 	do_action('set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in');
   895 	$logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );
       
   896 
       
   897 	/**
       
   898 	 * Fires immediately before the authentication cookie is set.
       
   899 	 *
       
   900 	 * @since 2.5.0
       
   901 	 *
       
   902 	 * @param string $auth_cookie Authentication cookie.
       
   903 	 * @param int    $expire      Login grace period in seconds. Default 43,200 seconds, or 12 hours.
       
   904 	 * @param int    $expiration  Duration in seconds the authentication cookie should be valid.
       
   905 	 *                            Default 1,209,600 seconds, or 14 days.
       
   906 	 * @param int    $user_id     User ID.
       
   907 	 * @param string $scheme      Authentication scheme. Values include 'auth', 'secure_auth', or 'logged_in'.
       
   908 	 */
       
   909 	do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme );
       
   910 
       
   911 	/**
       
   912 	 * Fires immediately before the secure authentication cookie is set.
       
   913 	 *
       
   914 	 * @since 2.6.0
       
   915 	 *
       
   916 	 * @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.
       
   918 	 * @param int    $expiration       Duration in seconds the authentication cookie should be valid.
       
   919 	 *                                 Default 1,209,600 seconds, or 14 days.
       
   920 	 * @param int    $user_id          User ID.
       
   921 	 * @param string $scheme           Authentication scheme. Default 'logged_in'.
       
   922 	 */
       
   923 	do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in' );
   680 
   924 
   681 	setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
   925 	setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
   682 	setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
   926 	setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
   683 	setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);
   927 	setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);
   684 	if ( COOKIEPATH != SITECOOKIEPATH )
   928 	if ( COOKIEPATH != SITECOOKIEPATH )
   688 
   932 
   689 if ( !function_exists('wp_clear_auth_cookie') ) :
   933 if ( !function_exists('wp_clear_auth_cookie') ) :
   690 /**
   934 /**
   691  * Removes all of the cookies associated with authentication.
   935  * Removes all of the cookies associated with authentication.
   692  *
   936  *
   693  * @since 2.5
   937  * @since 2.5.0
   694  */
   938  */
   695 function wp_clear_auth_cookie() {
   939 function wp_clear_auth_cookie() {
   696 	do_action('clear_auth_cookie');
   940 	/**
       
   941 	 * Fires just before the authentication cookies are cleared.
       
   942 	 *
       
   943 	 * @since 2.7.0
       
   944 	 */
       
   945 	do_action( 'clear_auth_cookie' );
   697 
   946 
   698 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
   947 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH,   COOKIE_DOMAIN );
   699 	setcookie( SECURE_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 );
   700 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
   949 	setcookie( AUTH_COOKIE,        ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN );
   701 	setcookie( SECURE_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 );
   736 
   985 
   737 if ( !function_exists('auth_redirect') ) :
   986 if ( !function_exists('auth_redirect') ) :
   738 /**
   987 /**
   739  * Checks if a user is logged in, if not it redirects them to the login page.
   988  * Checks if a user is logged in, if not it redirects them to the login page.
   740  *
   989  *
   741  * @since 1.5
   990  * @since 1.5.0
   742  */
   991  */
   743 function auth_redirect() {
   992 function auth_redirect() {
   744 	// Checks if a user is logged in, if not redirects them to the login page
   993 	// Checks if a user is logged in, if not redirects them to the login page
   745 
   994 
   746 	$secure = ( is_ssl() || force_ssl_admin() );
   995 	$secure = ( is_ssl() || force_ssl_admin() );
   747 
   996 
   748 	$secure = apply_filters('secure_auth_redirect', $secure);
   997 	/**
       
   998 	 * Filter whether to use a secure authentication redirect.
       
   999 	 *
       
  1000 	 * @since 3.1.0
       
  1001 	 *
       
  1002 	 * @param bool $secure Whether to use a secure authentication redirect. Default false.
       
  1003 	 */
       
  1004 	$secure = apply_filters( 'secure_auth_redirect', $secure );
   749 
  1005 
   750 	// If https is required and request is http, redirect
  1006 	// If https is required and request is http, redirect
   751 	if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
  1007 	if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
   752 		if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
  1008 		if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
   753 			wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
  1009 			wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
   756 			wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  1012 			wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
   757 			exit();
  1013 			exit();
   758 		}
  1014 		}
   759 	}
  1015 	}
   760 
  1016 
   761 	if ( is_user_admin() )
  1017 	if ( is_user_admin() ) {
   762 		$scheme = 'logged_in';
  1018 		$scheme = 'logged_in';
   763 	else
  1019 	} else {
       
  1020 		/**
       
  1021 		 * Filter the authentication redirect scheme.
       
  1022 		 *
       
  1023 		 * @since 2.9.0
       
  1024 		 *
       
  1025 		 * @param string $scheme Authentication redirect scheme. Default empty.
       
  1026 		 */
   764 		$scheme = apply_filters( 'auth_redirect_scheme', '' );
  1027 		$scheme = apply_filters( 'auth_redirect_scheme', '' );
       
  1028 	}
   765 
  1029 
   766 	if ( $user_id = wp_validate_auth_cookie( '',  $scheme) ) {
  1030 	if ( $user_id = wp_validate_auth_cookie( '',  $scheme) ) {
   767 		do_action('auth_redirect', $user_id);
  1031 		/**
       
  1032 		 * Fires before the authentication redirect.
       
  1033 		 *
       
  1034 		 * @since 2.8.0
       
  1035 		 *
       
  1036 		 * @param int $user_id User ID.
       
  1037 		 */
       
  1038 		do_action( 'auth_redirect', $user_id );
   768 
  1039 
   769 		// If the user wants ssl but the session is not ssl, redirect.
  1040 		// If the user wants ssl but the session is not ssl, redirect.
   770 		if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
  1041 		if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
   771 			if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
  1042 			if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
   772 				wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
  1043 				wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
   797  * Makes sure that a user was referred from another admin page.
  1068  * Makes sure that a user was referred from another admin page.
   798  *
  1069  *
   799  * To avoid security exploits.
  1070  * To avoid security exploits.
   800  *
  1071  *
   801  * @since 1.2.0
  1072  * @since 1.2.0
   802  * @uses do_action() Calls 'check_admin_referer' on $action.
  1073  *
   803  *
  1074  * @param int|string $action    Action nonce.
   804  * @param string $action Action nonce
  1075  * @param string     $query_arg Optional. Key to check for nonce in `$_REQUEST` (since 2.5).
   805  * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
  1076  *                              Default '_wpnonce'.
   806  */
  1077  * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between
   807 function check_admin_referer($action = -1, $query_arg = '_wpnonce') {
  1078  *                   0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
       
  1079  */
       
  1080 function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) {
   808 	if ( -1 == $action )
  1081 	if ( -1 == $action )
   809 		_doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2' );
  1082 		_doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2' );
   810 
  1083 
   811 	$adminurl = strtolower(admin_url());
  1084 	$adminurl = strtolower(admin_url());
   812 	$referer = strtolower(wp_get_referer());
  1085 	$referer = strtolower(wp_get_referer());
   813 	$result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
  1086 	$result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
   814 	if ( !$result && !(-1 == $action && strpos($referer, $adminurl) === 0) ) {
  1087 	if ( !$result && !(-1 == $action && strpos($referer, $adminurl) === 0) ) {
   815 		wp_nonce_ays($action);
  1088 		wp_nonce_ays($action);
   816 		die();
  1089 		die();
   817 	}
  1090 	}
   818 	do_action('check_admin_referer', $action, $result);
  1091 
       
  1092 	/**
       
  1093 	 * Fires once the admin request has been validated or not.
       
  1094 	 *
       
  1095 	 * @since 1.5.1
       
  1096 	 *
       
  1097 	 * @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
       
  1099 	 *                          0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
       
  1100 	 */
       
  1101 	do_action( 'check_admin_referer', $action, $result );
   819 	return $result;
  1102 	return $result;
   820 }
  1103 }
   821 endif;
  1104 endif;
   822 
  1105 
   823 if ( !function_exists('check_ajax_referer') ) :
  1106 if ( !function_exists('check_ajax_referer') ) :
   824 /**
  1107 /**
   825  * Verifies the AJAX request to prevent processing requests external of the blog.
  1108  * Verifies the AJAX request to prevent processing requests external of the blog.
   826  *
  1109  *
   827  * @since 2.0.3
  1110  * @since 2.0.3
   828  *
  1111  *
   829  * @param string $action Action nonce
  1112  * @param int|string   $action    Action nonce.
   830  * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
  1113  * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false,
       
  1114  *                                `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce'
       
  1115  *                                (in that order). Default false.
       
  1116  * @param bool         $die       Optional. Whether to die early when the nonce cannot be verified.
       
  1117  *                                Default true.
       
  1118  * @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.
   831  */
  1120  */
   832 function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
  1121 function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
   833 	$nonce = '';
  1122 	$nonce = '';
   834 
  1123 
   835 	if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) )
  1124 	if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) )
   846 			wp_die( -1 );
  1135 			wp_die( -1 );
   847 		else
  1136 		else
   848 			die( '-1' );
  1137 			die( '-1' );
   849 	}
  1138 	}
   850 
  1139 
   851 	do_action('check_ajax_referer', $action, $result);
  1140 	/**
       
  1141 	 * Fires once the AJAX request has been validated or not.
       
  1142 	 *
       
  1143 	 * @since 2.1.0
       
  1144 	 *
       
  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
       
  1147 	 *                          0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
       
  1148 	 */
       
  1149 	do_action( 'check_ajax_referer', $action, $result );
   852 
  1150 
   853 	return $result;
  1151 	return $result;
   854 }
  1152 }
   855 endif;
  1153 endif;
   856 
  1154 
   857 if ( !function_exists('wp_redirect') ) :
  1155 if ( !function_exists('wp_redirect') ) :
   858 /**
  1156 /**
   859  * Redirects to another page.
  1157  * Redirects to another page.
   860  *
  1158  *
   861  * @since 1.5.1
  1159  * @since 1.5.1
   862  * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
       
   863  *
  1160  *
   864  * @param string $location The path to redirect to.
  1161  * @param string $location The path to redirect to.
   865  * @param int $status Status code to use.
  1162  * @param int $status Status code to use.
   866  * @return bool False if $location is not provided, true otherwise.
  1163  * @return bool False if $location is not provided, true otherwise.
   867  */
  1164  */
   891 	if ( ! $location )
  1188 	if ( ! $location )
   892 		return false;
  1189 		return false;
   893 
  1190 
   894 	$location = wp_sanitize_redirect($location);
  1191 	$location = wp_sanitize_redirect($location);
   895 
  1192 
   896 	if ( !$is_IIS && php_sapi_name() != 'cgi-fcgi' )
  1193 	if ( !$is_IIS && PHP_SAPI != 'cgi-fcgi' )
   897 		status_header($status); // This causes problems on IIS and some FastCGI setups
  1194 		status_header($status); // This causes problems on IIS and some FastCGI setups
   898 
  1195 
   899 	header("Location: $location", true, $status);
  1196 	header("Location: $location", true, $status);
   900 
  1197 
   901 	return true;
  1198 	return true;
   904 
  1201 
   905 if ( !function_exists('wp_sanitize_redirect') ) :
  1202 if ( !function_exists('wp_sanitize_redirect') ) :
   906 /**
  1203 /**
   907  * Sanitizes a URL for use in a redirect.
  1204  * Sanitizes a URL for use in a redirect.
   908  *
  1205  *
   909  * @since 2.3
  1206  * @since 2.3.0
   910  *
  1207  *
   911  * @return string redirect-sanitized URL
  1208  * @return string redirect-sanitized URL
   912  **/
  1209  **/
   913 function wp_sanitize_redirect($location) {
  1210 function wp_sanitize_redirect($location) {
   914 	$location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location);
  1211 	$regex = '/
       
  1212 		(
       
  1213 			(?: [\xC2-\xDF][\x80-\xBF]        # double-byte sequences   110xxxxx 10xxxxxx
       
  1214 			|   \xE0[\xA0-\xBF][\x80-\xBF]    # triple-byte sequences   1110xxxx 10xxxxxx * 2
       
  1215 			|   [\xE1-\xEC][\x80-\xBF]{2}
       
  1216 			|   \xED[\x80-\x9F][\x80-\xBF]
       
  1217 			|   [\xEE-\xEF][\x80-\xBF]{2}
       
  1218 			|   \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences   11110xxx 10xxxxxx * 3
       
  1219 			|   [\xF1-\xF3][\x80-\xBF]{3}
       
  1220 			|   \xF4[\x80-\x8F][\x80-\xBF]{2}
       
  1221 		){1,40}                              # ...one or more times
       
  1222 		)/x';
       
  1223 	$location = preg_replace_callback( $regex, '_wp_sanitize_utf8_in_redirect', $location );
       
  1224 	$location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!*\[\]()]|i', '', $location);
   915 	$location = wp_kses_no_null($location);
  1225 	$location = wp_kses_no_null($location);
   916 
  1226 
   917 	// remove %0d and %0a from location
  1227 	// remove %0d and %0a from location
   918 	$strip = array('%0d', '%0a', '%0D', '%0A');
  1228 	$strip = array('%0d', '%0a', '%0D', '%0A');
   919 	$location = _deep_replace($strip, $location);
  1229 	$location = _deep_replace($strip, $location);
   920 	return $location;
  1230 	return $location;
   921 }
  1231 }
       
  1232 
       
  1233 /**
       
  1234  * URL encode UTF-8 characters in a URL.
       
  1235  *
       
  1236  * @ignore
       
  1237  * @since 4.2.0
       
  1238  * @access private
       
  1239  *
       
  1240  * @see wp_sanitize_redirect()
       
  1241  */
       
  1242 function _wp_sanitize_utf8_in_redirect( $matches ) {
       
  1243 	return urlencode( $matches[0] );
       
  1244 }
   922 endif;
  1245 endif;
   923 
  1246 
   924 if ( !function_exists('wp_safe_redirect') ) :
  1247 if ( !function_exists('wp_safe_redirect') ) :
   925 /**
  1248 /**
   926  * Performs a safe (local) redirect, using wp_redirect().
  1249  * Performs a safe (local) redirect, using wp_redirect().
   931  *
  1254  *
   932  * If the host is not allowed, then the redirect is to wp-admin on the siteurl
  1255  * If the host is not allowed, then the redirect is to wp-admin on the siteurl
   933  * instead. This prevents malicious redirects which redirect to another host,
  1256  * instead. This prevents malicious redirects which redirect to another host,
   934  * but only used in a few places.
  1257  * but only used in a few places.
   935  *
  1258  *
   936  * @since 2.3
  1259  * @since 2.3.0
   937  * @uses wp_validate_redirect() To validate the redirect is to an allowed host.
       
   938  *
  1260  *
   939  * @return void Does not return anything
  1261  * @return void Does not return anything
   940  **/
  1262  **/
   941 function wp_safe_redirect($location, $status = 302) {
  1263 function wp_safe_redirect($location, $status = 302) {
   942 
  1264 
   958  * list.
  1280  * list.
   959  *
  1281  *
   960  * If the host is not allowed, then the redirect is to $default supplied
  1282  * If the host is not allowed, then the redirect is to $default supplied
   961  *
  1283  *
   962  * @since 2.8.1
  1284  * @since 2.8.1
   963  * @uses apply_filters() Calls 'allowed_redirect_hosts' on an array containing
       
   964  *		WordPress host string and $location host string.
       
   965  *
  1285  *
   966  * @param string $location The redirect to validate
  1286  * @param string $location The redirect to validate
   967  * @param string $default The value to return if $location is not allowed
  1287  * @param string $default The value to return if $location is not allowed
   968  * @return string redirect-sanitized URL
  1288  * @return string redirect-sanitized URL
   969  **/
  1289  **/
   990 	if ( isset($lp['scheme'])  && !isset($lp['host']) )
  1310 	if ( isset($lp['scheme'])  && !isset($lp['host']) )
   991 		return $default;
  1311 		return $default;
   992 
  1312 
   993 	$wpp = parse_url(home_url());
  1313 	$wpp = parse_url(home_url());
   994 
  1314 
   995 	$allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '');
  1315 	/**
       
  1316 	 * Filter the whitelist of hosts to redirect to.
       
  1317 	 *
       
  1318 	 * @since 2.3.0
       
  1319 	 *
       
  1320 	 * @param array       $hosts An array of allowed hosts.
       
  1321 	 * @param bool|string $host  The parsed host; empty if not isset.
       
  1322 	 */
       
  1323 	$allowed_hosts = (array) apply_filters( 'allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '' );
   996 
  1324 
   997 	if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
  1325 	if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
   998 		$location = $default;
  1326 		$location = $default;
   999 
  1327 
  1000 	return $location;
  1328 	return $location;
  1001 }
  1329 }
  1002 endif;
  1330 endif;
  1003 
  1331 
  1004 if ( ! function_exists('wp_notify_postauthor') ) :
  1332 if ( ! function_exists('wp_notify_postauthor') ) :
  1005 /**
  1333 /**
  1006  * Notify an author of a comment/trackback/pingback to one of their posts.
  1334  * Notify an author (and/or others) of a comment/trackback/pingback on a post.
  1007  *
  1335  *
  1008  * @since 1.0.0
  1336  * @since 1.0.0
  1009  *
  1337  *
  1010  * @param int $comment_id Comment ID
  1338  * @param int $comment_id Comment ID
  1011  * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback'
  1339  * @param string $deprecated Not used
  1012  * @return bool False if user email does not exist. True on completion.
  1340  * @return bool True on completion. False if no email addresses were specified.
  1013  */
  1341  */
  1014 function wp_notify_postauthor( $comment_id, $comment_type = '' ) {
  1342 function wp_notify_postauthor( $comment_id, $deprecated = null ) {
       
  1343 	if ( null !== $deprecated ) {
       
  1344 		_deprecated_argument( __FUNCTION__, '3.8' );
       
  1345 	}
       
  1346 
  1015 	$comment = get_comment( $comment_id );
  1347 	$comment = get_comment( $comment_id );
  1016 	if ( empty( $comment ) )
  1348 	if ( empty( $comment ) )
  1017 		return false;
  1349 		return false;
  1018 
  1350 
  1019 	$post    = get_post( $comment->comment_post_ID );
  1351 	$post    = get_post( $comment->comment_post_ID );
  1020 	$author  = get_userdata( $post->post_author );
  1352 	$author  = get_userdata( $post->post_author );
  1021 
  1353 
       
  1354 	// Who to notify? By default, just the post author, but others can be added.
       
  1355 	$emails = array();
       
  1356 	if ( $author ) {
       
  1357 		$emails[] = $author->user_email;
       
  1358 	}
       
  1359 
       
  1360 	/**
       
  1361 	 * Filter the list of email addresses to receive a comment notification.
       
  1362 	 *
       
  1363 	 * By default, only post authors are notified of comments. This filter allows
       
  1364 	 * others to be added.
       
  1365 	 *
       
  1366 	 * @since 3.7.0
       
  1367 	 *
       
  1368 	 * @param array $emails     An array of email addresses to receive a comment notification.
       
  1369 	 * @param int   $comment_id The comment ID.
       
  1370 	 */
       
  1371 	$emails = apply_filters( 'comment_notification_recipients', $emails, $comment_id );
       
  1372 	$emails = array_filter( $emails );
       
  1373 
       
  1374 	// If there are no addresses to send the comment to, bail.
       
  1375 	if ( ! count( $emails ) ) {
       
  1376 		return false;
       
  1377 	}
       
  1378 
       
  1379 	// Facilitate unsetting below without knowing the keys.
       
  1380 	$emails = array_flip( $emails );
       
  1381 
       
  1382 	/**
       
  1383 	 * Filter whether to notify comment authors of their comments on their own posts.
       
  1384 	 *
       
  1385 	 * By default, comment authors aren't notified of their comments on their own
       
  1386 	 * posts. This filter allows you to override that.
       
  1387 	 *
       
  1388 	 * @since 3.8.0
       
  1389 	 *
       
  1390 	 * @param bool $notify     Whether to notify the post author of their own comment.
       
  1391 	 *                         Default false.
       
  1392 	 * @param int  $comment_id The comment ID.
       
  1393 	 */
       
  1394 	$notify_author = apply_filters( 'comment_notification_notify_author', false, $comment_id );
       
  1395 
  1022 	// The comment was left by the author
  1396 	// The comment was left by the author
  1023 	if ( $comment->user_id == $post->post_author )
  1397 	if ( $author && ! $notify_author && $comment->user_id == $post->post_author ) {
       
  1398 		unset( $emails[ $author->user_email ] );
       
  1399 	}
       
  1400 
       
  1401 	// The author moderated a comment on their own post
       
  1402 	if ( $author && ! $notify_author && $post->post_author == get_current_user_id() ) {
       
  1403 		unset( $emails[ $author->user_email ] );
       
  1404 	}
       
  1405 
       
  1406 	// The post author is no longer a member of the blog
       
  1407 	if ( $author && ! $notify_author && ! user_can( $post->post_author, 'read_post', $post->ID ) ) {
       
  1408 		unset( $emails[ $author->user_email ] );
       
  1409 	}
       
  1410 
       
  1411 	// If there's no email to send the comment to, bail, otherwise flip array back around for use below
       
  1412 	if ( ! count( $emails ) ) {
  1024 		return false;
  1413 		return false;
  1025 
  1414 	} else {
  1026 	// The author moderated a comment on his own post
  1415 		$emails = array_flip( $emails );
  1027 	if ( $post->post_author == get_current_user_id() )
  1416 	}
  1028 		return false;
       
  1029 
       
  1030 	// The post author is no longer a member of the blog
       
  1031 	if ( ! user_can( $post->post_author, 'read_post', $post->ID ) )
       
  1032 		return false;
       
  1033 
       
  1034 	// If there's no email to send the comment to
       
  1035 	if ( '' == $author->user_email )
       
  1036 		return false;
       
  1037 
  1417 
  1038 	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  1418 	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  1039 
  1419 
  1040 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1420 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1041 	// we want to reverse this for the plain text arena of emails.
  1421 	// we want to reverse this for the plain text arena of emails.
  1042 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1422 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1043 
  1423 
  1044 	if ( empty( $comment_type ) ) $comment_type = 'comment';
  1424 	switch ( $comment->comment_type ) {
  1045 
  1425 		case 'trackback':
  1046 	if ('comment' == $comment_type) {
  1426 			$notify_message  = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
  1047 		$notify_message  = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
  1427 			/* translators: 1: website name, 2: website IP, 3: website hostname */
  1048 		/* translators: 1: comment author, 2: author IP, 3: author domain */
  1428 			$notify_message .= sprintf( __('Website: %1$s (IP: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1049 		$notify_message .= sprintf( __('Author : %1$s (IP: %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";
  1050 		$notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
  1430 			$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment->comment_content ) . "\r\n\r\n";
  1051 		$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
  1431 			$notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n";
  1052 		$notify_message .= sprintf( __('Whois  : http://whois.arin.net/rest/ip/%s'), $comment->comment_author_IP ) . "\r\n";
  1432 			/* translators: 1: blog name, 2: post title */
  1053 		$notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1433 			$subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
  1054 		$notify_message .= __('You can see all comments on this post here: ') . "\r\n";
  1434 			break;
  1055 		/* translators: 1: blog name, 2: post title */
  1435 		case 'pingback':
  1056 		$subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
  1436 			$notify_message  = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
  1057 	} elseif ('trackback' == $comment_type) {
  1437 			/* translators: 1: website name, 2: website IP, 3: website hostname */
  1058 		$notify_message  = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
  1438 			$notify_message .= sprintf( __('Website: %1$s (IP: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1059 		/* translators: 1: website name, 2: author IP, 3: author domain */
  1439 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1060 		$notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1440 			$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment->comment_content ) . "\r\n\r\n";
  1061 		$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
  1441 			$notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n";
  1062 		$notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1442 			/* translators: 1: blog name, 2: post title */
  1063 		$notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
  1443 			$subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
  1064 		/* translators: 1: blog name, 2: post title */
  1444 			break;
  1065 		$subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
  1445 		default: // Comments
  1066 	} elseif ('pingback' == $comment_type) {
  1446 			$notify_message  = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
  1067 		$notify_message  = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
  1447 			/* translators: 1: comment author, 2: author IP, 3: author domain */
  1068 		/* translators: 1: comment author, 2: author IP, 3: author domain */
  1448 			$notify_message .= sprintf( __( 'Author: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1069 		$notify_message .= sprintf( __('Website: %1$s (IP: %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";
  1070 		$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
  1450 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1071 		$notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
  1451 			$notify_message .= sprintf( __( 'Whois: %s' ), "http://whois.arin.net/rest/ip/{$comment->comment_author_IP}" ) . "\r\n";
  1072 		$notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
  1452 			$notify_message .= sprintf( __('Comment: %s' ), "\r\n" . $comment->comment_content ) . "\r\n\r\n";
  1073 		/* translators: 1: blog name, 2: post title */
  1453 			$notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n";
  1074 		$subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
  1454 			/* translators: 1: blog name, 2: post title */
       
  1455 			$subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
       
  1456 			break;
  1075 	}
  1457 	}
  1076 	$notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
  1458 	$notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
  1077 	$notify_message .= sprintf( __('Permalink: %s'), get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment_id ) . "\r\n";
  1459 	$notify_message .= sprintf( __('Permalink: %s'), get_comment_link( $comment_id ) ) . "\r\n";
  1078 
  1460 
  1079 	if ( user_can( $post->post_author, 'edit_comment', $comment_id ) ) {
  1461 	if ( user_can( $post->post_author, 'edit_comment', $comment_id ) ) {
  1080 		if ( EMPTY_TRASH_DAYS )
  1462 		if ( EMPTY_TRASH_DAYS )
  1081 			$notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
  1463 			$notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
  1082 		else
  1464 		else
  1100 		. "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1482 		. "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1101 
  1483 
  1102 	if ( isset($reply_to) )
  1484 	if ( isset($reply_to) )
  1103 		$message_headers .= $reply_to . "\n";
  1485 		$message_headers .= $reply_to . "\n";
  1104 
  1486 
  1105 	$emails = array( $author->user_email );
  1487 	/**
  1106 
  1488 	 * Filter the comment notification email text.
  1107 	$emails          = apply_filters( 'comment_notification_recipients', $emails,          $comment_id );
  1489 	 *
  1108 	$notify_message  = apply_filters( 'comment_notification_text',       $notify_message,  $comment_id );
  1490 	 * @since 1.5.2
  1109 	$subject         = apply_filters( 'comment_notification_subject',    $subject,         $comment_id );
  1491 	 *
  1110 	$message_headers = apply_filters( 'comment_notification_headers',    $message_headers, $comment_id );
  1492 	 * @param string $notify_message The comment notification email text.
       
  1493 	 * @param int    $comment_id     Comment ID.
       
  1494 	 */
       
  1495 	$notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment_id );
       
  1496 
       
  1497 	/**
       
  1498 	 * Filter the comment notification email subject.
       
  1499 	 *
       
  1500 	 * @since 1.5.2
       
  1501 	 *
       
  1502 	 * @param string $subject    The comment notification email subject.
       
  1503 	 * @param int    $comment_id Comment ID.
       
  1504 	 */
       
  1505 	$subject = apply_filters( 'comment_notification_subject', $subject, $comment_id );
       
  1506 
       
  1507 	/**
       
  1508 	 * Filter the comment notification email headers.
       
  1509 	 *
       
  1510 	 * @since 1.5.2
       
  1511 	 *
       
  1512 	 * @param string $message_headers Headers for the comment notification email.
       
  1513 	 * @param int    $comment_id      Comment ID.
       
  1514 	 */
       
  1515 	$message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment_id );
  1111 
  1516 
  1112 	foreach ( $emails as $email ) {
  1517 	foreach ( $emails as $email ) {
  1113 		@wp_mail( $email, $subject, $notify_message, $message_headers );
  1518 		@wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  1114 	}
  1519 	}
  1115 
  1520 
  1116 	return true;
  1521 	return true;
  1117 }
  1522 }
  1118 endif;
  1523 endif;
  1119 
  1524 
  1120 if ( !function_exists('wp_notify_moderator') ) :
  1525 if ( !function_exists('wp_notify_moderator') ) :
  1121 /**
  1526 /**
  1122  * Notifies the moderator of the blog about a new comment that is awaiting approval.
  1527  * Notifies the moderator of the blog about a new comment that is awaiting approval.
  1123  *
  1528  *
  1124  * @since 1.0
  1529  * @since 1.0.0
  1125  * @uses $wpdb
  1530  *
       
  1531  * @global wpdb $wpdb WordPress database abstraction object.
  1126  *
  1532  *
  1127  * @param int $comment_id Comment ID
  1533  * @param int $comment_id Comment ID
  1128  * @return bool Always returns true
  1534  * @return bool Always returns true
  1129  */
  1535  */
  1130 function wp_notify_moderator($comment_id) {
  1536 function wp_notify_moderator($comment_id) {
  1135 
  1541 
  1136 	$comment = get_comment($comment_id);
  1542 	$comment = get_comment($comment_id);
  1137 	$post = get_post($comment->comment_post_ID);
  1543 	$post = get_post($comment->comment_post_ID);
  1138 	$user = get_userdata( $post->post_author );
  1544 	$user = get_userdata( $post->post_author );
  1139 	// Send to the administration and to the post author if the author can modify the comment.
  1545 	// Send to the administration and to the post author if the author can modify the comment.
  1140 	$emails = array( get_option('admin_email') );
  1546 	$emails = array( get_option( 'admin_email' ) );
  1141 	if ( user_can($user->ID, 'edit_comment', $comment_id) && !empty($user->user_email) && ( get_option('admin_email') != $user->user_email) )
  1547 	if ( user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) {
  1142 		$emails[] = $user->user_email;
  1548 		if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) )
       
  1549 			$emails[] = $user->user_email;
       
  1550 	}
  1143 
  1551 
  1144 	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  1552 	$comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  1145 	$comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
  1553 	$comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
  1146 
  1554 
  1147 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1555 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1148 	// we want to reverse this for the plain text arena of emails.
  1556 	// we want to reverse this for the plain text arena of emails.
  1149 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1557 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1150 
  1558 
  1151 	switch ($comment->comment_type)
  1559 	switch ( $comment->comment_type ) {
  1152 	{
       
  1153 		case 'trackback':
  1560 		case 'trackback':
  1154 			$notify_message  = sprintf( __('A new trackback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1561 			$notify_message  = sprintf( __('A new trackback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1155 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1562 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1156 			$notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1563 			/* translators: 1: website name, 2: website IP, 3: website hostname */
  1157 			$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
  1564 			$notify_message .= sprintf( __( 'Website: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
       
  1565 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1158 			$notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1566 			$notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1159 			break;
  1567 			break;
  1160 		case 'pingback':
  1568 		case 'pingback':
  1161 			$notify_message  = sprintf( __('A new pingback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1569 			$notify_message  = sprintf( __('A new pingback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1162 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1570 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1163 			$notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1571 			/* translators: 1: website name, 2: website IP, 3: website hostname */
  1164 			$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
  1572 			$notify_message .= sprintf( __( 'Website: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
       
  1573 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1165 			$notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1574 			$notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1166 			break;
  1575 			break;
  1167 		default: //Comments
  1576 		default: // Comments
  1168 			$notify_message  = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1577 			$notify_message  = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1169 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1578 			$notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1170 			$notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\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";
  1171 			$notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
  1580 			$notify_message .= sprintf( __( 'E-mail: %s' ), $comment->comment_author_email ) . "\r\n";
  1172 			$notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
  1581 			$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  1173 			$notify_message .= sprintf( __('Whois  : http://whois.arin.net/rest/ip/%s'), $comment->comment_author_IP ) . "\r\n";
  1582 			$notify_message .= sprintf( __( 'Whois: %s' ), "http://whois.arin.net/rest/ip/{$comment->comment_author_IP}" ) . "\r\n";
  1174 			$notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1583 			$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment->comment_content ) . "\r\n\r\n";
  1175 			break;
  1584 			break;
  1176 	}
  1585 	}
  1177 
  1586 
  1178 	$notify_message .= sprintf( __('Approve it: %s'),  admin_url("comment.php?action=approve&c=$comment_id") ) . "\r\n";
  1587 	$notify_message .= sprintf( __('Approve it: %s'),  admin_url("comment.php?action=approve&c=$comment_id") ) . "\r\n";
  1179 	if ( EMPTY_TRASH_DAYS )
  1588 	if ( EMPTY_TRASH_DAYS )
  1187 	$notify_message .= admin_url("edit-comments.php?comment_status=moderated") . "\r\n";
  1596 	$notify_message .= admin_url("edit-comments.php?comment_status=moderated") . "\r\n";
  1188 
  1597 
  1189 	$subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title );
  1598 	$subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title );
  1190 	$message_headers = '';
  1599 	$message_headers = '';
  1191 
  1600 
  1192 	$emails          = apply_filters( 'comment_moderation_recipients', $emails,          $comment_id );
  1601 	/**
  1193 	$notify_message  = apply_filters( 'comment_moderation_text',       $notify_message,  $comment_id );
  1602 	 * Filter the list of recipients for comment moderation emails.
  1194 	$subject         = apply_filters( 'comment_moderation_subject',    $subject,         $comment_id );
  1603 	 *
  1195 	$message_headers = apply_filters( 'comment_moderation_headers',    $message_headers, $comment_id );
  1604 	 * @since 3.7.0
       
  1605 	 *
       
  1606 	 * @param array $emails     List of email addresses to notify for comment moderation.
       
  1607 	 * @param int   $comment_id Comment ID.
       
  1608 	 */
       
  1609 	$emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id );
       
  1610 
       
  1611 	/**
       
  1612 	 * Filter the comment moderation email text.
       
  1613 	 *
       
  1614 	 * @since 1.5.2
       
  1615 	 *
       
  1616 	 * @param string $notify_message Text of the comment moderation email.
       
  1617 	 * @param int    $comment_id     Comment ID.
       
  1618 	 */
       
  1619 	$notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id );
       
  1620 
       
  1621 	/**
       
  1622 	 * Filter the comment moderation email subject.
       
  1623 	 *
       
  1624 	 * @since 1.5.2
       
  1625 	 *
       
  1626 	 * @param string $subject    Subject of the comment moderation email.
       
  1627 	 * @param int    $comment_id Comment ID.
       
  1628 	 */
       
  1629 	$subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id );
       
  1630 
       
  1631 	/**
       
  1632 	 * Filter the comment moderation email headers.
       
  1633 	 *
       
  1634 	 * @since 2.8.0
       
  1635 	 *
       
  1636 	 * @param string $message_headers Headers for the comment moderation email.
       
  1637 	 * @param int    $comment_id      Comment ID.
       
  1638 	 */
       
  1639 	$message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id );
  1196 
  1640 
  1197 	foreach ( $emails as $email ) {
  1641 	foreach ( $emails as $email ) {
  1198 		@wp_mail( $email, $subject, $notify_message, $message_headers );
  1642 		@wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  1199 	}
  1643 	}
  1200 
  1644 
  1201 	return true;
  1645 	return true;
  1202 }
  1646 }
  1203 endif;
  1647 endif;
  1204 
  1648 
  1205 if ( !function_exists('wp_password_change_notification') ) :
  1649 if ( !function_exists('wp_password_change_notification') ) :
  1206 /**
  1650 /**
  1207  * Notify the blog admin of a user changing password, normally via email.
  1651  * Notify the blog admin of a user changing password, normally via email.
  1208  *
  1652  *
  1209  * @since 2.7
  1653  * @since 2.7.0
  1210  *
  1654  *
  1211  * @param object $user User Object
  1655  * @param object $user User Object
  1212  */
  1656  */
  1213 function wp_password_change_notification(&$user) {
  1657 function wp_password_change_notification(&$user) {
  1214 	// send a copy of password change notification to the admin
  1658 	// send a copy of password change notification to the admin
  1215 	// but check to see if it's the admin whose password we're changing, and skip this
  1659 	// but check to see if it's the admin whose password we're changing, and skip this
  1216 	if ( $user->user_email != get_option('admin_email') ) {
  1660 	if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {
  1217 		$message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
  1661 		$message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
  1218 		// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1662 		// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1219 		// we want to reverse this for the plain text arena of emails.
  1663 		// we want to reverse this for the plain text arena of emails.
  1220 		$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1664 		$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1221 		wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), $blogname), $message);
  1665 		wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), $blogname), $message);
  1223 }
  1667 }
  1224 endif;
  1668 endif;
  1225 
  1669 
  1226 if ( !function_exists('wp_new_user_notification') ) :
  1670 if ( !function_exists('wp_new_user_notification') ) :
  1227 /**
  1671 /**
  1228  * Notify the blog admin of a new user, normally via email.
  1672  * Email login credentials to a newly-registered user.
  1229  *
  1673  *
  1230  * @since 2.0
  1674  * A new user registration notification is also sent to admin email.
  1231  *
  1675  *
  1232  * @param int $user_id User ID
  1676  * @since 2.0.0
  1233  * @param string $plaintext_pass Optional. The user's plaintext password
  1677  *
       
  1678  * @param int    $user_id        User ID.
       
  1679  * @param string $plaintext_pass Optional. The user's plaintext password. Default empty.
  1234  */
  1680  */
  1235 function wp_new_user_notification($user_id, $plaintext_pass = '') {
  1681 function wp_new_user_notification($user_id, $plaintext_pass = '') {
  1236 	$user = get_userdata( $user_id );
  1682 	$user = get_userdata( $user_id );
  1237 
  1683 
  1238 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1684 	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1262  * Get the time-dependent variable for nonce creation.
  1708  * Get the time-dependent variable for nonce creation.
  1263  *
  1709  *
  1264  * A nonce has a lifespan of two ticks. Nonces in their second tick may be
  1710  * A nonce has a lifespan of two ticks. Nonces in their second tick may be
  1265  * updated, e.g. by autosave.
  1711  * updated, e.g. by autosave.
  1266  *
  1712  *
  1267  * @since 2.5
  1713  * @since 2.5.0
  1268  *
  1714  *
  1269  * @return int
  1715  * @return float Float value rounded up to the next highest integer.
  1270  */
  1716  */
  1271 function wp_nonce_tick() {
  1717 function wp_nonce_tick() {
       
  1718 	/**
       
  1719 	 * Filter the lifespan of nonces in seconds.
       
  1720 	 *
       
  1721 	 * @since 2.5.0
       
  1722 	 *
       
  1723 	 * @param int $lifespan Lifespan of nonces in seconds. Default 86,400 seconds, or one day.
       
  1724 	 */
  1272 	$nonce_life = apply_filters( 'nonce_life', DAY_IN_SECONDS );
  1725 	$nonce_life = apply_filters( 'nonce_life', DAY_IN_SECONDS );
  1273 
  1726 
  1274 	return ceil(time() / ( $nonce_life / 2 ));
  1727 	return ceil(time() / ( $nonce_life / 2 ));
  1275 }
  1728 }
  1276 endif;
  1729 endif;
  1282  * The user is given an amount of time to use the token, so therefore, since the
  1735  * The user is given an amount of time to use the token, so therefore, since the
  1283  * UID and $action remain the same, the independent variable is the time.
  1736  * UID and $action remain the same, the independent variable is the time.
  1284  *
  1737  *
  1285  * @since 2.0.3
  1738  * @since 2.0.3
  1286  *
  1739  *
  1287  * @param string $nonce Nonce that was used in the form to verify
  1740  * @param string     $nonce  Nonce that was used in the form to verify
  1288  * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
  1741  * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
  1289  * @return bool Whether the nonce check passed or failed.
  1742  * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between
  1290  */
  1743  *                   0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
  1291 function wp_verify_nonce($nonce, $action = -1) {
  1744  */
       
  1745 function wp_verify_nonce( $nonce, $action = -1 ) {
       
  1746 	$nonce = (string) $nonce;
  1292 	$user = wp_get_current_user();
  1747 	$user = wp_get_current_user();
  1293 	$uid = (int) $user->ID;
  1748 	$uid = (int) $user->ID;
  1294 	if ( ! $uid )
  1749 	if ( ! $uid ) {
       
  1750 		/**
       
  1751 		 * Filter whether the user who generated the nonce is logged out.
       
  1752 		 *
       
  1753 		 * @since 3.5.0
       
  1754 		 *
       
  1755 		 * @param int    $uid    ID of the nonce-owning user.
       
  1756 		 * @param string $action The nonce action.
       
  1757 		 */
  1295 		$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
  1758 		$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
  1296 
  1759 	}
       
  1760 
       
  1761 	if ( empty( $nonce ) ) {
       
  1762 		return false;
       
  1763 	}
       
  1764 
       
  1765 	$token = wp_get_session_token();
  1297 	$i = wp_nonce_tick();
  1766 	$i = wp_nonce_tick();
  1298 
  1767 
  1299 	// Nonce generated 0-12 hours ago
  1768 	// Nonce generated 0-12 hours ago
  1300 	if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) === $nonce )
  1769 	$expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce'), -12, 10 );
       
  1770 	if ( hash_equals( $expected, $nonce ) ) {
  1301 		return 1;
  1771 		return 1;
       
  1772 	}
       
  1773 
  1302 	// Nonce generated 12-24 hours ago
  1774 	// Nonce generated 12-24 hours ago
  1303 	if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) === $nonce )
  1775 	$expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
       
  1776 	if ( hash_equals( $expected, $nonce ) ) {
  1304 		return 2;
  1777 		return 2;
       
  1778 	}
       
  1779 
  1305 	// Invalid nonce
  1780 	// Invalid nonce
  1306 	return false;
  1781 	return false;
  1307 }
  1782 }
  1308 endif;
  1783 endif;
  1309 
  1784 
  1310 if ( !function_exists('wp_create_nonce') ) :
  1785 if ( !function_exists('wp_create_nonce') ) :
  1311 /**
  1786 /**
  1312  * Creates a random, one time use token.
  1787  * Creates a cryptographic token tied to a specific action, user, and window of time.
  1313  *
  1788  *
  1314  * @since 2.0.3
  1789  * @since 2.0.3
  1315  *
  1790  *
  1316  * @param string|int $action Scalar value to add context to the nonce.
  1791  * @param string|int $action Scalar value to add context to the nonce.
  1317  * @return string The one use form token
  1792  * @return string The token.
  1318  */
  1793  */
  1319 function wp_create_nonce($action = -1) {
  1794 function wp_create_nonce($action = -1) {
  1320 	$user = wp_get_current_user();
  1795 	$user = wp_get_current_user();
  1321 	$uid = (int) $user->ID;
  1796 	$uid = (int) $user->ID;
  1322 	if ( ! $uid )
  1797 	if ( ! $uid ) {
       
  1798 		/** This filter is documented in wp-includes/pluggable.php */
  1323 		$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
  1799 		$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
  1324 
  1800 	}
       
  1801 
       
  1802 	$token = wp_get_session_token();
  1325 	$i = wp_nonce_tick();
  1803 	$i = wp_nonce_tick();
  1326 
  1804 
  1327 	return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
  1805 	return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
  1328 }
  1806 }
  1329 endif;
  1807 endif;
  1330 
  1808 
  1331 if ( !function_exists('wp_salt') ) :
  1809 if ( !function_exists('wp_salt') ) :
  1332 /**
  1810 /**
  1340  * security. Below is an example of how the secret key constants are defined.
  1818  * security. Below is an example of how the secret key constants are defined.
  1341  * Do not paste this example directly into wp-config.php. Instead, have a
  1819  * Do not paste this example directly into wp-config.php. Instead, have a
  1342  * {@link https://api.wordpress.org/secret-key/1.1/salt/ secret key created} just
  1820  * {@link https://api.wordpress.org/secret-key/1.1/salt/ secret key created} just
  1343  * for you.
  1821  * for you.
  1344  *
  1822  *
  1345  * <code>
  1823  *     define('AUTH_KEY',         ' Xakm<o xQy rw4EMsLKM-?!T+,PFF})H4lzcW57AF0U@N@< >M%G4Yt>f`z]MON');
  1346  * define('AUTH_KEY',         ' Xakm<o xQy rw4EMsLKM-?!T+,PFF})H4lzcW57AF0U@N@< >M%G4Yt>f`z]MON');
  1824  *     define('SECURE_AUTH_KEY',  'LzJ}op]mr|6+![P}Ak:uNdJCJZd>(Hx.-Mh#Tz)pCIU#uGEnfFz|f ;;eU%/U^O~');
  1347  * define('SECURE_AUTH_KEY',  'LzJ}op]mr|6+![P}Ak:uNdJCJZd>(Hx.-Mh#Tz)pCIU#uGEnfFz|f ;;eU%/U^O~');
  1825  *     define('LOGGED_IN_KEY',    '|i|Ux`9<p-h$aFf(qnT:sDO:D1P^wZ$$/Ra@miTJi9G;ddp_<q}6H1)o|a +&JCM');
  1348  * define('LOGGED_IN_KEY',    '|i|Ux`9<p-h$aFf(qnT:sDO:D1P^wZ$$/Ra@miTJi9G;ddp_<q}6H1)o|a +&JCM');
  1826  *     define('NONCE_KEY',        '%:R{[P|,s.KuMltH5}cI;/k<Gx~j!f0I)m_sIyu+&NJZ)-iO>z7X>QYR0Z_XnZ@|');
  1349  * define('NONCE_KEY',        '%:R{[P|,s.KuMltH5}cI;/k<Gx~j!f0I)m_sIyu+&NJZ)-iO>z7X>QYR0Z_XnZ@|');
  1827  *     define('AUTH_SALT',        'eZyT)-Naw]F8CwA*VaW#q*|.)g@o}||wf~@C-YSt}(dh_r6EbI#A,y|nU2{B#JBW');
  1350  * define('AUTH_SALT',        'eZyT)-Naw]F8CwA*VaW#q*|.)g@o}||wf~@C-YSt}(dh_r6EbI#A,y|nU2{B#JBW');
  1828  *     define('SECURE_AUTH_SALT', '!=oLUTXh,QW=H `}`L|9/^4-3 STz},T(w}W<I`.JjPi)<Bmf1v,HpGe}T1:Xt7n');
  1351  * define('SECURE_AUTH_SALT', '!=oLUTXh,QW=H `}`L|9/^4-3 STz},T(w}W<I`.JjPi)<Bmf1v,HpGe}T1:Xt7n');
  1829  *     define('LOGGED_IN_SALT',   '+XSqHc;@Q*K_b|Z?NC[3H!!EONbh.n<+=uKR:>*c(u`g~EJBf#8u#R{mUEZrozmm');
  1352  * define('LOGGED_IN_SALT',   '+XSqHc;@Q*K_b|Z?NC[3H!!EONbh.n<+=uKR:>*c(u`g~EJBf#8u#R{mUEZrozmm');
  1830  *     define('NONCE_SALT',       'h`GXHhD>SLWVfg1(1(N{;.V!MoE(SfbA_ksP@&`+AycHcAV$+?@3q+rxV{%^VyKT');
  1353  * define('NONCE_SALT',       'h`GXHhD>SLWVfg1(1(N{;.V!MoE(SfbA_ksP@&`+AycHcAV$+?@3q+rxV{%^VyKT');
       
  1354  * </code>
       
  1355  *
  1831  *
  1356  * Salting passwords helps against tools which has stored hashed values of
  1832  * Salting passwords helps against tools which has stored hashed values of
  1357  * common dictionary strings. The added values makes it harder to crack.
  1833  * common dictionary strings. The added values makes it harder to crack.
  1358  *
  1834  *
  1359  * @since 2.5
  1835  * @since 2.5.0
  1360  *
  1836  *
  1361  * @link https://api.wordpress.org/secret-key/1.1/salt/ Create secrets for wp-config.php
  1837  * @link https://api.wordpress.org/secret-key/1.1/salt/ Create secrets for wp-config.php
  1362  *
  1838  *
  1363  * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce)
  1839  * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce)
  1364  * @return string Salt value
  1840  * @return string Salt value
  1365  */
  1841  */
  1366 function wp_salt( $scheme = 'auth' ) {
  1842 function wp_salt( $scheme = 'auth' ) {
  1367 	static $cached_salts = array();
  1843 	static $cached_salts = array();
  1368 	if ( isset( $cached_salts[ $scheme ] ) )
  1844 	if ( isset( $cached_salts[ $scheme ] ) ) {
       
  1845 		/**
       
  1846 		 * Filter the WordPress salt.
       
  1847 		 *
       
  1848 		 * @since 2.5.0
       
  1849 		 *
       
  1850 		 * @param string $cached_salt Cached salt for the given scheme.
       
  1851 		 * @param string $scheme      Authentication scheme. Values include 'auth',
       
  1852 		 *                            'secure_auth', 'logged_in', and 'nonce'.
       
  1853 		 */
  1369 		return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
  1854 		return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
       
  1855 	}
  1370 
  1856 
  1371 	static $duplicated_keys;
  1857 	static $duplicated_keys;
  1372 	if ( null === $duplicated_keys ) {
  1858 	if ( null === $duplicated_keys ) {
  1373 		$duplicated_keys = array( 'put your unique phrase here' => true );
  1859 		$duplicated_keys = array( 'put your unique phrase here' => true );
  1374 		foreach ( array( 'AUTH', 'SECURE_AUTH', 'LOGGED_IN', 'NONCE', 'SECRET' ) as $first ) {
  1860 		foreach ( array( 'AUTH', 'SECURE_AUTH', 'LOGGED_IN', 'NONCE', 'SECRET' ) as $first ) {
  1375 			foreach ( array( 'KEY', 'SALT' ) as $second ) {
  1861 			foreach ( array( 'KEY', 'SALT' ) as $second ) {
  1376 				if ( ! defined( "{$first}_{$second}" ) )
  1862 				if ( ! defined( "{$first}_{$second}" ) ) {
  1377 					continue;
  1863 					continue;
       
  1864 				}
  1378 				$value = constant( "{$first}_{$second}" );
  1865 				$value = constant( "{$first}_{$second}" );
  1379 				$duplicated_keys[ $value ] = isset( $duplicated_keys[ $value ] );
  1866 				$duplicated_keys[ $value ] = isset( $duplicated_keys[ $value ] );
  1380 			}
  1867 			}
  1381 		}
  1868 		}
  1382 	}
  1869 	}
  1383 
  1870 
  1384 	$key = $salt = '';
  1871 	$values = array(
  1385 	if ( defined( 'SECRET_KEY' ) && SECRET_KEY && empty( $duplicated_keys[ SECRET_KEY ] ) )
  1872 		'key' => '',
  1386 		$key = SECRET_KEY;
  1873 		'salt' => ''
  1387 	if ( 'auth' == $scheme && defined( 'SECRET_SALT' ) && SECRET_SALT && empty( $duplicated_keys[ SECRET_SALT ] ) )
  1874 	);
  1388 		$salt = SECRET_SALT;
  1875 	if ( defined( 'SECRET_KEY' ) && SECRET_KEY && empty( $duplicated_keys[ SECRET_KEY ] ) ) {
       
  1876 		$values['key'] = SECRET_KEY;
       
  1877 	}
       
  1878 	if ( 'auth' == $scheme && defined( 'SECRET_SALT' ) && SECRET_SALT && empty( $duplicated_keys[ SECRET_SALT ] ) ) {
       
  1879 		$values['salt'] = SECRET_SALT;
       
  1880 	}
  1389 
  1881 
  1390 	if ( in_array( $scheme, array( 'auth', 'secure_auth', 'logged_in', 'nonce' ) ) ) {
  1882 	if ( in_array( $scheme, array( 'auth', 'secure_auth', 'logged_in', 'nonce' ) ) ) {
  1391 		foreach ( array( 'key', 'salt' ) as $type ) {
  1883 		foreach ( array( 'key', 'salt' ) as $type ) {
  1392 			$const = strtoupper( "{$scheme}_{$type}" );
  1884 			$const = strtoupper( "{$scheme}_{$type}" );
  1393 			if ( defined( $const ) && constant( $const ) && empty( $duplicated_keys[ constant( $const ) ] ) ) {
  1885 			if ( defined( $const ) && constant( $const ) && empty( $duplicated_keys[ constant( $const ) ] ) ) {
  1394 				$$type = constant( $const );
  1886 				$values[ $type ] = constant( $const );
  1395 			} elseif ( ! $$type ) {
  1887 			} elseif ( ! $values[ $type ] ) {
  1396 				$$type = get_site_option( "{$scheme}_{$type}" );
  1888 				$values[ $type ] = get_site_option( "{$scheme}_{$type}" );
  1397 				if ( ! $$type ) {
  1889 				if ( ! $values[ $type ] ) {
  1398 					$$type = wp_generate_password( 64, true, true );
  1890 					$values[ $type ] = wp_generate_password( 64, true, true );
  1399 					update_site_option( "{$scheme}_{$type}", $$type );
  1891 					update_site_option( "{$scheme}_{$type}", $values[ $type ] );
  1400 				}
  1892 				}
  1401 			}
  1893 			}
  1402 		}
  1894 		}
  1403 	} else {
  1895 	} else {
  1404 		if ( ! $key ) {
  1896 		if ( ! $values['key'] ) {
  1405 			$key = get_site_option( 'secret_key' );
  1897 			$values['key'] = get_site_option( 'secret_key' );
  1406 			if ( ! $key ) {
  1898 			if ( ! $values['key'] ) {
  1407 				$key = wp_generate_password( 64, true, true );
  1899 				$values['key'] = wp_generate_password( 64, true, true );
  1408 				update_site_option( 'secret_key', $key );
  1900 				update_site_option( 'secret_key', $values['key'] );
  1409 			}
  1901 			}
  1410 		}
  1902 		}
  1411 		$salt = hash_hmac( 'md5', $scheme, $key );
  1903 		$values['salt'] = hash_hmac( 'md5', $scheme, $values['key'] );
  1412 	}
  1904 	}
  1413 
  1905 
  1414 	$cached_salts[ $scheme ] = $key . $salt;
  1906 	$cached_salts[ $scheme ] = $values['key'] . $values['salt'];
       
  1907 
       
  1908 	/** This filter is documented in wp-includes/pluggable.php */
  1415 	return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
  1909 	return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
  1416 }
  1910 }
  1417 endif;
  1911 endif;
  1418 
  1912 
  1419 if ( !function_exists('wp_hash') ) :
  1913 if ( !function_exists('wp_hash') ) :
  1420 /**
  1914 /**
  1421  * Get hash of given string.
  1915  * Get hash of given string.
  1422  *
  1916  *
  1423  * @since 2.0.3
  1917  * @since 2.0.3
  1424  * @uses wp_salt() Get WordPress salt
       
  1425  *
  1918  *
  1426  * @param string $data Plain text to hash
  1919  * @param string $data Plain text to hash
  1427  * @return string Hash of $data
  1920  * @return string Hash of $data
  1428  */
  1921  */
  1429 function wp_hash($data, $scheme = 'auth') {
  1922 function wp_hash($data, $scheme = 'auth') {
  1438  * Create a hash (encrypt) of a plain text password.
  1931  * Create a hash (encrypt) of a plain text password.
  1439  *
  1932  *
  1440  * For integration with other applications, this function can be overwritten to
  1933  * For integration with other applications, this function can be overwritten to
  1441  * instead use the other package password checking algorithm.
  1934  * instead use the other package password checking algorithm.
  1442  *
  1935  *
  1443  * @since 2.5
  1936  * @since 2.5.0
       
  1937  *
  1444  * @global object $wp_hasher PHPass object
  1938  * @global object $wp_hasher PHPass object
  1445  * @uses PasswordHash::HashPassword
  1939  * @uses PasswordHash::HashPassword
  1446  *
  1940  *
  1447  * @param string $password Plain text user password to hash
  1941  * @param string $password Plain text user password to hash
  1448  * @return string The hash string of the password
  1942  * @return string The hash string of the password
  1449  */
  1943  */
  1450 function wp_hash_password($password) {
  1944 function wp_hash_password($password) {
  1451 	global $wp_hasher;
  1945 	global $wp_hasher;
  1452 
  1946 
  1453 	if ( empty($wp_hasher) ) {
  1947 	if ( empty($wp_hasher) ) {
  1454 		require_once( ABSPATH . 'wp-includes/class-phpass.php');
  1948 		require_once( ABSPATH . WPINC . '/class-phpass.php');
  1455 		// By default, use the portable hash from phpass
  1949 		// By default, use the portable hash from phpass
  1456 		$wp_hasher = new PasswordHash(8, true);
  1950 		$wp_hasher = new PasswordHash(8, true);
  1457 	}
  1951 	}
  1458 
  1952 
  1459 	return $wp_hasher->HashPassword( trim( $password ) );
  1953 	return $wp_hasher->HashPassword( trim( $password ) );
  1470  * against the already encrypted password to see if they match.
  1964  * against the already encrypted password to see if they match.
  1471  *
  1965  *
  1472  * For integration with other applications, this function can be overwritten to
  1966  * For integration with other applications, this function can be overwritten to
  1473  * instead use the other package password checking algorithm.
  1967  * instead use the other package password checking algorithm.
  1474  *
  1968  *
  1475  * @since 2.5
  1969  * @since 2.5.0
       
  1970  *
  1476  * @global object $wp_hasher PHPass object used for checking the password
  1971  * @global object $wp_hasher PHPass object used for checking the password
  1477  *	against the $hash + $password
  1972  *	against the $hash + $password
  1478  * @uses PasswordHash::CheckPassword
  1973  * @uses PasswordHash::CheckPassword
  1479  *
  1974  *
  1480  * @param string $password Plaintext user's password
  1975  * @param string $password Plaintext user's password
  1484 function wp_check_password($password, $hash, $user_id = '') {
  1979 function wp_check_password($password, $hash, $user_id = '') {
  1485 	global $wp_hasher;
  1980 	global $wp_hasher;
  1486 
  1981 
  1487 	// If the hash is still md5...
  1982 	// If the hash is still md5...
  1488 	if ( strlen($hash) <= 32 ) {
  1983 	if ( strlen($hash) <= 32 ) {
  1489 		$check = ( $hash == md5($password) );
  1984 		$check = hash_equals( $hash, md5( $password ) );
  1490 		if ( $check && $user_id ) {
  1985 		if ( $check && $user_id ) {
  1491 			// Rehash using new hash.
  1986 			// Rehash using new hash.
  1492 			wp_set_password($password, $user_id);
  1987 			wp_set_password($password, $user_id);
  1493 			$hash = wp_hash_password($password);
  1988 			$hash = wp_hash_password($password);
  1494 		}
  1989 		}
  1495 
  1990 
  1496 		return apply_filters('check_password', $check, $password, $hash, $user_id);
  1991 		/**
       
  1992 		 * Filter whether the plaintext password matches the encrypted password.
       
  1993 		 *
       
  1994 		 * @since 2.5.0
       
  1995 		 *
       
  1996 		 * @param bool   $check    Whether the passwords match.
       
  1997 		 * @param string $password The plaintext password.
       
  1998 		 * @param string $hash     The hashed password.
       
  1999 		 * @param int    $user_id  User ID.
       
  2000 		 */
       
  2001 		return apply_filters( 'check_password', $check, $password, $hash, $user_id );
  1497 	}
  2002 	}
  1498 
  2003 
  1499 	// If the stored hash is longer than an MD5, presume the
  2004 	// If the stored hash is longer than an MD5, presume the
  1500 	// new style phpass portable hash.
  2005 	// new style phpass portable hash.
  1501 	if ( empty($wp_hasher) ) {
  2006 	if ( empty($wp_hasher) ) {
  1502 		require_once( ABSPATH . 'wp-includes/class-phpass.php');
  2007 		require_once( ABSPATH . WPINC . '/class-phpass.php');
  1503 		// By default, use the portable hash from phpass
  2008 		// By default, use the portable hash from phpass
  1504 		$wp_hasher = new PasswordHash(8, true);
  2009 		$wp_hasher = new PasswordHash(8, true);
  1505 	}
  2010 	}
  1506 
  2011 
  1507 	$check = $wp_hasher->CheckPassword($password, $hash);
  2012 	$check = $wp_hasher->CheckPassword($password, $hash);
  1508 
  2013 
  1509 	return apply_filters('check_password', $check, $password, $hash, $user_id);
  2014 	/** This filter is documented in wp-includes/pluggable.php */
       
  2015 	return apply_filters( 'check_password', $check, $password, $hash, $user_id );
  1510 }
  2016 }
  1511 endif;
  2017 endif;
  1512 
  2018 
  1513 if ( !function_exists('wp_generate_password') ) :
  2019 if ( !function_exists('wp_generate_password') ) :
  1514 /**
  2020 /**
  1515  * Generates a random password drawn from the defined set of characters.
  2021  * Generates a random password drawn from the defined set of characters.
  1516  *
  2022  *
  1517  * @since 2.5
  2023  * @since 2.5.0
  1518  *
  2024  *
  1519  * @param int $length The length of password to generate
  2025  * @param int  $length              Optional. The length of password to generate. Default 12.
  1520  * @param bool $special_chars Whether to include standard special characters. Default true.
  2026  * @param bool $special_chars       Optional. Whether to include standard special characters.
  1521  * @param bool $extra_special_chars Whether to include other special characters. Used when
  2027  *                                  Default true.
  1522  *   generating secret keys and salts. Default false.
  2028  * @param bool $extra_special_chars Optional. Whether to include other special characters.
  1523  * @return string The random password
  2029  *                                  Used when generating secret keys and salts. Default false.
  1524  **/
  2030  * @return string The random password.
       
  2031  */
  1525 function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
  2032 function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
  1526 	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  2033 	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  1527 	if ( $special_chars )
  2034 	if ( $special_chars )
  1528 		$chars .= '!@#$%^&*()';
  2035 		$chars .= '!@#$%^&*()';
  1529 	if ( $extra_special_chars )
  2036 	if ( $extra_special_chars )
  1532 	$password = '';
  2039 	$password = '';
  1533 	for ( $i = 0; $i < $length; $i++ ) {
  2040 	for ( $i = 0; $i < $length; $i++ ) {
  1534 		$password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
  2041 		$password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
  1535 	}
  2042 	}
  1536 
  2043 
  1537 	// random_password filter was previously in random_password function which was deprecated
  2044 	/**
  1538 	return apply_filters('random_password', $password);
  2045 	 * Filter the randomly-generated password.
       
  2046 	 *
       
  2047 	 * @since 3.0.0
       
  2048 	 *
       
  2049 	 * @param string $password The generated password.
       
  2050 	 */
       
  2051 	return apply_filters( 'random_password', $password );
  1539 }
  2052 }
  1540 endif;
  2053 endif;
  1541 
  2054 
  1542 if ( !function_exists('wp_rand') ) :
  2055 if ( !function_exists('wp_rand') ) :
  1543 /**
  2056 /**
  1591  * Updates the user's password with a new encrypted one.
  2104  * Updates the user's password with a new encrypted one.
  1592  *
  2105  *
  1593  * For integration with other applications, this function can be overwritten to
  2106  * For integration with other applications, this function can be overwritten to
  1594  * instead use the other package password checking algorithm.
  2107  * instead use the other package password checking algorithm.
  1595  *
  2108  *
  1596  * @since 2.5
  2109  * Please note: This function should be used sparingly and is really only meant for single-time
  1597  * @uses $wpdb WordPress database object for queries
  2110  * application. Leveraging this improperly in a plugin or theme could result in an endless loop
  1598  * @uses wp_hash_password() Used to encrypt the user's password before passing to the database
  2111  * of password resets if precautions are not taken to ensure it does not execute on every page load.
       
  2112  *
       
  2113  * @since 2.5.0
       
  2114  *
       
  2115  * @global wpdb $wpdb WordPress database abstraction object.
  1599  *
  2116  *
  1600  * @param string $password The plaintext new user password
  2117  * @param string $password The plaintext new user password
  1601  * @param int $user_id User ID
  2118  * @param int $user_id User ID
  1602  */
  2119  */
  1603 function wp_set_password( $password, $user_id ) {
  2120 function wp_set_password( $password, $user_id ) {
  1610 }
  2127 }
  1611 endif;
  2128 endif;
  1612 
  2129 
  1613 if ( !function_exists( 'get_avatar' ) ) :
  2130 if ( !function_exists( 'get_avatar' ) ) :
  1614 /**
  2131 /**
  1615  * Retrieve the avatar for a user who provided a user ID or email address.
  2132  * Retrieve the avatar `<img>` tag for a user, email address, MD5 hash, comment, or post.
  1616  *
  2133  *
  1617  * @since 2.5
  2134  * @since 2.5.0
  1618  * @param int|string|object $id_or_email A user ID,  email address, or comment object
  2135  * @since 4.2.0 Optional `$args` parameter added.
  1619  * @param int $size Size of the avatar image
  2136  *
  1620  * @param string $default URL to a default image to use if no avatar is available
  2137  * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
  1621  * @param string $alt Alternative text to use in image tag. Defaults to blank
  2138  *                           user email, WP_User object, WP_Post object, or comment object.
  1622  * @return string <img> tag for the user's avatar
  2139  * @param int    $size       Optional. Height and width of the avatar image file in pixels. Default 96.
  1623 */
  2140  * @param string $default    Optional. URL for the default image or a default type. Accepts '404'
  1624 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
  2141  *                           (return a 404 instead of a default image), 'retro' (8bit), 'monsterid'
  1625 	if ( ! get_option('show_avatars') )
  2142  *                           (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"),
       
  2143  *                           'mystery', 'mm', or 'mysterman' (The Oyster Man), 'blank' (transparent GIF),
       
  2144  *                           or 'gravatar_default' (the Gravatar logo). Default is the value of the
       
  2145  *                           'avatar_default' option, with a fallback of 'mystery'.
       
  2146  * @param string $alt        Optional. Alternative text to use in &lt;img&gt; tag. Default empty.
       
  2147  * @param array  $args       {
       
  2148  *     Optional. Extra arguments to retrieve the avatar.
       
  2149  *
       
  2150  *     @type int          $height        Display height of the avatar in pixels. Defaults to $size.
       
  2151  *     @type int          $width         Display width of the avatar in pixels. Defaults to $size.
       
  2152  *     @type bool         $force_default Whether to always show the default image, never the Gravatar. Default false.
       
  2153  *     @type string       $rating        What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
       
  2154  *                                       judged in that order. Default is the value of the 'avatar_rating' option.
       
  2155  *     @type string       $scheme        URL scheme to use. See set_url_scheme() for accepted values.
       
  2156  *                                       Default null.
       
  2157  *     @type array|string $class         Array or string of additional classes to add to the &lt;img&gt; element.
       
  2158  *                                       Default null.
       
  2159  *     @type bool         $force_display Whether to always show the avatar - ignores the show_avatars option.
       
  2160  *                                       Default false.
       
  2161  *     @type string       $extra_attr    HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
       
  2162  * }
       
  2163  * @return false|string `<img>` tag for the user's avatar. False on failure.
       
  2164  */
       
  2165 function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) {
       
  2166 	$defaults = array(
       
  2167 		// get_avatar_data() args.
       
  2168 		'size'          => 96,
       
  2169 		'height'        => null,
       
  2170 		'width'         => null,
       
  2171 		'default'       => get_option( 'avatar_default', 'mystery' ),
       
  2172 		'force_default' => false,
       
  2173 		'rating'        => get_option( 'avatar_rating' ),
       
  2174 		'scheme'        => null,
       
  2175 		'alt'           => '',
       
  2176 		'class'         => null,
       
  2177 		'force_display' => false,
       
  2178 		'extra_attr'    => '',
       
  2179 	);
       
  2180 
       
  2181 	if ( empty( $args ) ) {
       
  2182 		$args = array();
       
  2183 	}
       
  2184 
       
  2185 	$args['size']    = (int) $size;
       
  2186 	$args['default'] = $default;
       
  2187 	$args['alt']     = $alt;
       
  2188 
       
  2189 	$args = wp_parse_args( $args, $defaults );
       
  2190 
       
  2191 	if ( empty( $args['height'] ) ) {
       
  2192 		$args['height'] = $args['size'];
       
  2193 	}
       
  2194 	if ( empty( $args['width'] ) ) {
       
  2195 		$args['width'] = $args['size'];
       
  2196 	}
       
  2197 
       
  2198 	/**
       
  2199 	 * Filter whether to retrieve the avatar URL early.
       
  2200 	 *
       
  2201 	 * 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.
       
  2203 	 *
       
  2204 	 * @since 4.2.0
       
  2205 	 *
       
  2206 	 * @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.
       
  2208 	 * @param array             $args        Arguments passed to get_avatar_url(), after processing.
       
  2209 	 */
       
  2210 	$avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args );
       
  2211 
       
  2212 	if ( ! is_null( $avatar ) ) {
       
  2213 		/** This filter is documented in wp-includes/pluggable.php */
       
  2214 		return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );
       
  2215 	}
       
  2216 
       
  2217 	if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) {
  1626 		return false;
  2218 		return false;
  1627 
  2219 	}
  1628 	if ( false === $alt)
  2220 
  1629 		$safe_alt = '';
  2221 	$url2x = get_avatar_url( $id_or_email, array_merge( $args, array( 'size' => $args['size'] * 2 ) ) );
  1630 	else
  2222 
  1631 		$safe_alt = esc_attr( $alt );
  2223 	$args = get_avatar_data( $id_or_email, $args );
  1632 
  2224 
  1633 	if ( !is_numeric($size) )
  2225 	$url = $args['url'];
  1634 		$size = '96';
  2226 
  1635 
  2227 	if ( ! $url || is_wp_error( $url ) ) {
  1636 	$email = '';
  2228         return false;
  1637 	if ( is_numeric($id_or_email) ) {
  2229 	}
  1638 		$id = (int) $id_or_email;
  2230 
  1639 		$user = get_userdata($id);
  2231 	$class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
  1640 		if ( $user )
  2232 
  1641 			$email = $user->user_email;
  2233 	if ( ! $args['found_avatar'] || $args['force_default'] ) {
  1642 	} elseif ( is_object($id_or_email) ) {
  2234         $class[] = 'avatar-default';
  1643 		// No avatar for pingbacks or trackbacks
  2235 	}
  1644 		$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
  2236 
  1645 		if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
  2237 	if ( $args['class'] ) {
  1646 			return false;
  2238 		if ( is_array( $args['class'] ) ) {
  1647 
  2239 			$class = array_merge( $class, $args['class'] );
  1648 		if ( !empty($id_or_email->user_id) ) {
  2240 		} else {
  1649 			$id = (int) $id_or_email->user_id;
  2241 			$class[] = $args['class'];
  1650 			$user = get_userdata($id);
       
  1651 			if ( $user)
       
  1652 				$email = $user->user_email;
       
  1653 		} elseif ( !empty($id_or_email->comment_author_email) ) {
       
  1654 			$email = $id_or_email->comment_author_email;
       
  1655 		}
  2242 		}
  1656 	} else {
  2243 	}
  1657 		$email = $id_or_email;
  2244 
  1658 	}
  2245 	$avatar = sprintf(
  1659 
  2246 		"<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>",
  1660 	if ( empty($default) ) {
  2247 		esc_attr( $args['alt'] ),
  1661 		$avatar_default = get_option('avatar_default');
  2248 		esc_url( $url ),
  1662 		if ( empty($avatar_default) )
  2249 		esc_attr( "$url2x 2x" ),
  1663 			$default = 'mystery';
  2250 		esc_attr( join( ' ', $class ) ),
  1664 		else
  2251 		(int) $args['height'],
  1665 			$default = $avatar_default;
  2252 		(int) $args['width'],
  1666 	}
  2253 		$args['extra_attr']
  1667 
  2254 	);
  1668 	if ( !empty($email) )
  2255 
  1669 		$email_hash = md5( strtolower( trim( $email ) ) );
  2256 	/**
  1670 
  2257 	 * Filter the avatar to retrieve.
  1671 	if ( is_ssl() ) {
  2258 	 *
  1672 		$host = 'https://secure.gravatar.com';
  2259 	 * @since 2.5.0
  1673 	} else {
  2260 	 * @since 4.2.0 The `$args` parameter was added.
  1674 		if ( !empty($email) )
  2261 	 *
  1675 			$host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
  2262 	 * @param string            $avatar      &lt;img&gt; tag for the user's avatar.
  1676 		else
  2263 	 * @param int|object|string $id_or_email A user ID, email address, or comment object.
  1677 			$host = 'http://0.gravatar.com';
  2264 	 * @param int               $size        Square avatar width and height in pixels to retrieve.
  1678 	}
  2265 	 * @param string            $alt         Alternative text to use in the avatar image tag.
  1679 
  2266 	 *                                       Default empty.
  1680 	if ( 'mystery' == $default )
  2267 	 * @param array             $args        Arguments passed to get_avatar_data(), after processing.
  1681 		$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
  2268 	 */
  1682 	elseif ( 'blank' == $default )
  2269 	return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );
  1683 		$default = $email ? 'blank' : includes_url( 'images/blank.gif' );
       
  1684 	elseif ( !empty($email) && 'gravatar_default' == $default )
       
  1685 		$default = '';
       
  1686 	elseif ( 'gravatar_default' == $default )
       
  1687 		$default = "$host/avatar/?s={$size}";
       
  1688 	elseif ( empty($email) )
       
  1689 		$default = "$host/avatar/?d=$default&amp;s={$size}";
       
  1690 	elseif ( strpos($default, 'http://') === 0 )
       
  1691 		$default = add_query_arg( 's', $size, $default );
       
  1692 
       
  1693 	if ( !empty($email) ) {
       
  1694 		$out = "$host/avatar/";
       
  1695 		$out .= $email_hash;
       
  1696 		$out .= '?s='.$size;
       
  1697 		$out .= '&amp;d=' . urlencode( $default );
       
  1698 
       
  1699 		$rating = get_option('avatar_rating');
       
  1700 		if ( !empty( $rating ) )
       
  1701 			$out .= "&amp;r={$rating}";
       
  1702 
       
  1703 		$out = str_replace( '&#038;', '&amp;', esc_url( $out ) );
       
  1704 		$avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
       
  1705 	} else {
       
  1706 		$avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
       
  1707 	}
       
  1708 
       
  1709 	return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
       
  1710 }
  2270 }
  1711 endif;
  2271 endif;
  1712 
  2272 
  1713 if ( !function_exists( 'wp_text_diff' ) ) :
  2273 if ( !function_exists( 'wp_text_diff' ) ) :
  1714 /**
  2274 /**
  1725  * 'title_left' : Default is an empty string. Change the HTML to the left of the
  2285  * 'title_left' : Default is an empty string. Change the HTML to the left of the
  1726  *		title.
  2286  *		title.
  1727  * 'title_right' : Default is an empty string. Change the HTML to the right of
  2287  * 'title_right' : Default is an empty string. Change the HTML to the right of
  1728  *		the title.
  2288  *		the title.
  1729  *
  2289  *
  1730  * @since 2.6
  2290  * @since 2.6.0
       
  2291  *
  1731  * @see wp_parse_args() Used to change defaults to user defined settings.
  2292  * @see wp_parse_args() Used to change defaults to user defined settings.
  1732  * @uses Text_Diff
  2293  * @uses Text_Diff
  1733  * @uses WP_Text_Diff_Renderer_Table
  2294  * @uses WP_Text_Diff_Renderer_Table
  1734  *
  2295  *
  1735  * @param string $left_string "old" (left) version of string
  2296  * @param string $left_string "old" (left) version of string