Completed
Push — add/connect-user-endpoint ( 4e5b9e...eae093 )
by
unknown
28:36 queued 18:20
created

Jetpack_JSON_API_User_Create_Endpoint::get_user()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 9
loc 9
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_JSON_API_User_Create_Endpoint extends Jetpack_JSON_API_Endpoint {
4
5
	protected $needed_capabilities = 'create_users';
6
7
	private $user_data;
8
9
	function result() {
10
		return $this->create_or_get_user();
11
	}
12
13
	function validate_input( $object ) {
14
		$this->user_data = $this->input();
15
		if ( empty( $this->user_data ) ) {
16
			return new WP_Error( 'input_error', __( 'user_data is required', 'jetpack' ) );
17
		}
18
		if ( ! isset( $this->user_data[ 'email' ] ) ) {
19
			return new WP_Error( 'input_error', __( 'user email is required', 'jetpack' ) );
20
		}
21
		if ( ! isset( $this->user_data[ 'login' ] ) ) {
22
			return new WP_Error( 'input_error', __( 'user login is required', 'jetpack' ) );
23
		}
24
		return parent::validate_input( $object );
25
	}
26
27
	function create_or_get_user() {
28
		require_once JETPACK__PLUGIN_DIR . 'modules/sso/class.jetpack-sso-helpers.php';
29
30
		// Check for an existing user
31
		$user = get_user_by( 'email', $this->user_date['email'] );
0 ignored issues
show
Bug introduced by
The property user_date does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
33
		if ( ! $user ) {
34
			$user = Jetpack_SSO_Helpers::generate_user( (object) $this->user_data );
35
		}
36
37
		if ( ! $user ) {
38
			return false;
39
		}
40
41
		return $this->get_user( $user->ID );
42
	}
43
44 View Code Duplication
	public function get_user( $user_id ) {
45
		$the_user = $this->get_author( $user_id, true );
46
		if ( $the_user && ! is_wp_error( $the_user ) ) {
47
			$userdata = get_userdata( $user_id );
48
			$the_user->roles = ! is_wp_error( $userdata ) ? $userdata->roles : array();
49
		}
50
51
		return $the_user;
52
	}
53
54
}
55