PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.6.4
Jetpack – WP Security, Backup, Speed, & Growth v12.6.4
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / json-endpoints / jetpack / class.jetpack-json-api-user-create-endpoint.php

class.jetpack-json-api-user-create-endpoint.php in Jetpack – WP Security, Backup, Speed, & Growth 12.6.4, at json-endpoints/jetpack/class.jetpack-json-api-user-create-endpoint.php

111 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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