Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_JSON_API_User_Connect_Endpoint | |
0.00% |
0 / 12 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| result | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| validate_input | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | |
| 3 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 4 | use Automattic\Jetpack\Connection\Tokens; |
| 5 | |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit( 0 ); |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * User connect endpoint class. |
| 12 | * |
| 13 | * @phan-constructor-used-for-side-effects |
| 14 | */ |
| 15 | class Jetpack_JSON_API_User_Connect_Endpoint extends Jetpack_JSON_API_Endpoint { |
| 16 | |
| 17 | /** |
| 18 | * Needed capabilities. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $needed_capabilities = 'create_users'; |
| 23 | |
| 24 | /** |
| 25 | * The user ID. |
| 26 | * |
| 27 | * @var int |
| 28 | */ |
| 29 | private $user_id; |
| 30 | |
| 31 | /** |
| 32 | * The user token. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | private $user_token; |
| 37 | |
| 38 | /** |
| 39 | * The endpoint callback. |
| 40 | * |
| 41 | * @return array |
| 42 | */ |
| 43 | public function result() { |
| 44 | ( new Tokens() )->update_user_token( $this->user_id, sprintf( '%s.%d', $this->user_token, $this->user_id ), false ); |
| 45 | return array( 'success' => ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $this->user_id ) ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Validate input. |
| 50 | * |
| 51 | * @param int $user_id - the User ID. |
| 52 | * |
| 53 | * @return bool|WP_Error |
| 54 | */ |
| 55 | public function validate_input( $user_id ) { |
| 56 | $input = $this->input(); |
| 57 | if ( ! isset( $user_id ) ) { |
| 58 | return new WP_Error( 'input_error', __( 'user_id is required', 'jetpack' ) ); |
| 59 | } |
| 60 | $this->user_id = $user_id; |
| 61 | if ( ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $this->user_id ) ) { |
| 62 | return new WP_Error( 'user_already_connected', __( 'The user is already connected', 'jetpack' ) ); |
| 63 | } |
| 64 | if ( ! isset( $input['user_token'] ) ) { |
| 65 | return new WP_Error( 'input_error', __( 'user_token is required', 'jetpack' ) ); |
| 66 | } |
| 67 | $this->user_token = sanitize_text_field( $input['user_token'] ); |
| 68 | return parent::validate_input( $user_id ); |
| 69 | } |
| 70 | } |