| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Jetpack Connection package Utils class file. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-connection |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack\Connection; |
| 9 |
|
| 10 |
use Automattic\Jetpack\Tracking; |
| 11 |
|
| 12 |
/** |
| 13 |
* Provides utility methods for the Connection package. |
| 14 |
*/ |
| 15 |
class Utils { |
| 16 |
|
| 17 |
const DEFAULT_JETPACK__API_VERSION = 1; |
| 18 |
const DEFAULT_JETPACK__API_BASE = 'https://jetpack.wordpress.com/jetpack.'; |
| 19 |
const DEFAULT_JETPACK__WPCOM_JSON_API_BASE = 'https://public-api.wordpress.com'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Enters a user token into the user_tokens option |
| 23 |
* |
| 24 |
* @deprecated 1.24.0 Use Automattic\Jetpack\Connection\Tokens->update_user_token() instead. |
| 25 |
* |
| 26 |
* @param int $user_id The user id. |
| 27 |
* @param string $token The user token. |
| 28 |
* @param bool $is_master_user Whether the user is the master user. |
| 29 |
* @return bool |
| 30 |
*/ |
| 31 |
public static function update_user_token( $user_id, $token, $is_master_user ) { |
| 32 |
_deprecated_function( __METHOD__, '1.24.0', 'Automattic\\Jetpack\\Connection\\Tokens->update_user_token' ); |
| 33 |
return ( new Tokens() )->update_user_token( $user_id, $token, $is_master_user ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Filters the value of the api constant. |
| 38 |
* |
| 39 |
* @param String $constant_value The constant value. |
| 40 |
* @param String $constant_name The constant name. |
| 41 |
* @return mixed | null |
| 42 |
*/ |
| 43 |
public static function jetpack_api_constant_filter( $constant_value, $constant_name ) { |
| 44 |
if ( $constant_value !== null ) { |
| 45 |
// If the constant value was already set elsewhere, use that value. |
| 46 |
return $constant_value; |
| 47 |
} |
| 48 |
|
| 49 |
if ( defined( "self::DEFAULT_$constant_name" ) ) { |
| 50 |
return constant( "self::DEFAULT_$constant_name" ); |
| 51 |
} |
| 52 |
|
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Add a filter to initialize default values of the constants. |
| 58 |
*/ |
| 59 |
public static function init_default_constants() { |
| 60 |
add_filter( |
| 61 |
'jetpack_constant_default_value', |
| 62 |
array( __CLASS__, 'jetpack_api_constant_filter' ), |
| 63 |
10, |
| 64 |
2 |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Filters the registration request body to include tracking properties. |
| 70 |
* |
| 71 |
* @param array $properties Already prepared tracking properties. |
| 72 |
* @return array amended properties. |
| 73 |
*/ |
| 74 |
public static function filter_register_request_body( $properties ) { |
| 75 |
$tracking = new Tracking(); |
| 76 |
$tracks_identity = $tracking->tracks_get_identity( get_current_user_id() ); |
| 77 |
|
| 78 |
return array_merge( |
| 79 |
$properties, |
| 80 |
array( |
| 81 |
'_ui' => $tracks_identity['_ui'], |
| 82 |
'_ut' => $tracks_identity['_ut'], |
| 83 |
) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Generate a new user from a SSO attempt. |
| 89 |
* |
| 90 |
* @param object $user_data WordPress.com user information. |
| 91 |
*/ |
| 92 |
public static function generate_user( $user_data ) { |
| 93 |
$username = $user_data->login; |
| 94 |
/** |
| 95 |
* Determines how many times the SSO module can attempt to randomly generate a user. |
| 96 |
* |
| 97 |
* @module sso |
| 98 |
* |
| 99 |
* @since jetpack-4.3.2 |
| 100 |
* |
| 101 |
* @param int 5 By default, SSO will attempt to random generate a user up to 5 times. |
| 102 |
*/ |
| 103 |
$num_tries = (int) apply_filters( 'jetpack_sso_allowed_username_generate_retries', 5 ); |
| 104 |
|
| 105 |
$exists = username_exists( $username ); |
| 106 |
$tries = 0; |
| 107 |
while ( $exists && $tries++ < $num_tries ) { |
| 108 |
$username = $user_data->login . '_' . $user_data->ID . '_' . wp_rand(); |
| 109 |
$exists = username_exists( $username ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( $exists ) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
$user = (object) array(); |
| 117 |
$user->user_pass = wp_generate_password( 20 ); |
| 118 |
$user->user_login = wp_slash( $username ); |
| 119 |
$user->user_email = wp_slash( $user_data->email ); |
| 120 |
$user->display_name = $user_data->display_name; |
| 121 |
$user->first_name = $user_data->first_name; |
| 122 |
$user->last_name = $user_data->last_name; |
| 123 |
$user->url = $user_data->url; |
| 124 |
$user->description = $user_data->description; |
| 125 |
|
| 126 |
if ( isset( $user_data->role ) && $user_data->role ) { |
| 127 |
$user->role = $user_data->role; |
| 128 |
} |
| 129 |
|
| 130 |
$created_user_id = wp_insert_user( $user ); |
| 131 |
|
| 132 |
update_user_meta( $created_user_id, 'wpcom_user_id', $user_data->ID ); |
| 133 |
return get_userdata( $created_user_id ); |
| 134 |
} |
| 135 |
} |
| 136 |
|