Completed
Push — content-options-blog-display-7... ( 930de1 )
by
unknown
12:25
created

Jetpack_JSON_API_User_Connect_Endpoint   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A result() 0 4 1
A validate_input() 0 15 4
1
<?php
2
3
class Jetpack_JSON_API_User_Connect_Endpoint extends Jetpack_JSON_API_Endpoint {
4
5
	protected $needed_capabilities = 'create_users';
6
7
	private $user_id;
8
	private $user_token;
9
10
	function result() {
11
		Jetpack::update_user_token( $this->user_id, sprintf( '%s.%d', $this->user_token, $this->user_id ), false );
12
		return array( 'success' => Jetpack::is_user_connected( $this->user_id ) );
13
	}
14
15
	function validate_input( $user_id ) {
16
		$input = $this->input();
17
		if ( ! isset( $user_id ) ) {
18
			return new WP_Error( 'input_error', __( 'user_id is required', 'jetpack' ) );
19
		}
20
		$this->user_id = $user_id;
21
		if ( Jetpack::is_user_connected( $this->user_id ) ) {
22
			return new WP_Error( 'user_already_connected', __( 'The user is already connected', 'jetpack' ) );
23
		}
24
		if ( ! isset( $input['user_token'] ) ) {
25
			return new WP_Error( 'input_error', __( 'user_token is required', 'jetpack' ) );
26
		}
27
		$this->user_token = sanitize_text_field( $input[ 'user_token'] );
28
		return parent::validate_input( $user_id );
29
	}
30
31
}
32