| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
use Automattic\Jetpack\Constants; |
| 4 |
|
| 5 |
/** |
| 6 |
* User create endpoint class. |
| 7 |
*/ |
| 8 |
class Jetpack_JSON_API_User_Create_Endpoint extends Jetpack_JSON_API_Endpoint { |
| 9 |
|
| 10 |
/** |
| 11 |
* Needed capabilities. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $needed_capabilities = 'create_users'; |
| 16 |
|
| 17 |
/** |
| 18 |
* User data. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private $user_data; |
| 23 |
|
| 24 |
/** |
| 25 |
* Endpoint callback. |
| 26 |
* |
| 27 |
* @return object|false |
| 28 |
*/ |
| 29 |
public function result() { |
| 30 |
return $this->create_or_get_user(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Validate the input. |
| 35 |
* |
| 36 |
* @param object $object - the object. |
| 37 |
* |
| 38 |
* @return bool|WP_Error |
| 39 |
*/ |
| 40 |
public function validate_input( $object ) { |
| 41 |
$this->user_data = $this->input(); |
| 42 |
|
| 43 |
if ( empty( $this->user_data ) ) { |
| 44 |
return new WP_Error( 'input_error', __( 'user_data is required', 'jetpack' ) ); |
| 45 |
} |
| 46 |
if ( ! isset( $this->user_data['email'] ) ) { |
| 47 |
return new WP_Error( 'input_error', __( 'user email is required', 'jetpack' ) ); |
| 48 |
} |
| 49 |
if ( ! isset( $this->user_data['login'] ) ) { |
| 50 |
return new WP_Error( 'input_error', __( 'user login is required', 'jetpack' ) ); |
| 51 |
} |
| 52 |
return parent::validate_input( $object ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Create or get the user. |
| 57 |
* |
| 58 |
* @return object|false |
| 59 |
*/ |
| 60 |
public function create_or_get_user() { |
| 61 |
require_once JETPACK__PLUGIN_DIR . 'modules/sso/class.jetpack-sso-helpers.php'; |
| 62 |
// Check for an existing user |
| 63 |
$user = get_user_by( 'email', $this->user_data['email'] ); |
| 64 |
$roles = (array) $this->user_data['roles']; |
| 65 |
$role = array_pop( $roles ); |
| 66 |
|
| 67 |
$query_args = $this->query_args(); |
| 68 |
if ( isset( $query_args['invite_accepted'] ) && $query_args['invite_accepted'] ) { |
| 69 |
Constants::set_constant( 'JETPACK_INVITE_ACCEPTED', true ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! $user ) { |
| 73 |
// We modify the input here to mimick the same call structure of the update user endpoint. |
| 74 |
$this->user_data = (object) $this->user_data; |
| 75 |
$this->user_data->role = $role; |
| 76 |
$this->user_data->url = isset( $this->user_data->URL ) ? $this->user_data->URL : ''; |
| 77 |
$this->user_data->display_name = $this->user_data->name; |
| 78 |
$this->user_data->description = ''; |
| 79 |
$user = Jetpack_SSO_Helpers::generate_user( $this->user_data ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( is_multisite() ) { |
| 83 |
add_user_to_blog( get_current_blog_id(), $user->ID, $role ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! $user ) { |
| 87 |
|
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
return $this->get_user( $user->ID ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get the user. |
| 96 |
* |
| 97 |
* @param int $user_id - the user ID. |
| 98 |
* |
| 99 |
* @return object|WP_Error |
| 100 |
*/ |
| 101 |
public function get_user( $user_id ) { |
| 102 |
$the_user = $this->get_author( $user_id, true ); |
| 103 |
if ( $the_user && ! is_wp_error( $the_user ) ) { |
| 104 |
$userdata = get_userdata( $user_id ); |
| 105 |
$the_user->roles = ! is_wp_error( $userdata ) ? $userdata->roles : array(); |
| 106 |
} |
| 107 |
|
| 108 |
return $the_user; |
| 109 |
} |
| 110 |
} |
| 111 |
|