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