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