Completed
Push — try/add-jetpack-purchase-token ( dd3973...a226ea )
by
unknown
37:20 queued 21:00
created

WPCOM_REST_API_V2_Endpoint_Purchase_Token   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A register_routes() 0 21 1
A permission_callback() 0 9 2
A get_purchase_token() 0 13 2
A delete_purchase_token() 0 8 2
1
<?php
2
/**
3
 * REST API endpoint to manage purchase tokens for logged out users.
4
 *
5
 * @package automattic/jetpack
6
 * @since 9.8.0
7
 */
8
9
/**
10
 * PurchaseToken wpcom api v2 endpoint
11
 */
12
class WPCOM_REST_API_V2_Endpoint_Purchase_Token extends WP_REST_Controller {
13
	/**
14
	 * Constructor.
15
	 */
16
	public function __construct() {
17
		$this->namespace = 'wpcom/v2';
18
		$this->rest_base = 'purchase-token';
19
20
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
21
	}
22
23
	/**
24
	 * Register the route.
25
	 */
26
	public function register_routes() {
27
		register_rest_route(
28
			$this->namespace,
29
			$this->rest_base,
30
			array(
31
				'methods'             => WP_REST_Server::READABLE,
32
				'callback'            => array( $this, 'get_purchase_token' ),
33
				'permission_callback' => array( $this, 'permission_callback' ),
34
			)
35
		);
36
37
		register_rest_route(
38
			$this->namespace,
39
			$this->rest_base,
40
			array(
41
				'methods'             => \WP_REST_Server::DELETABLE,
42
				'callback'            => array( $this, 'delete_purchase_token' ),
43
				'permission_callback' => array( $this, 'permission_callback' ),
44
			)
45
		);
46
	}
47
48
	/**
49
	 * Verify if site can request or delete a purchase token.
50
	 *
51
	 * @return bool True if user is able to request or delete a purchase token.
52
	 */
53
	public function permission_callback() {
54
		$is_site_level_auth = apply_filters( 'jetpack_site_level_auth', false );
55
56
		if ( $is_site_level_auth ) {
57
			return true;
58
		}
59
60
		return current_user_can( 'manage_options' );
61
	}
62
63
	/**
64
	 * Returns a purchase token used for site-connected (non user-authenticated) checkout.
65
	 *
66
	 * @return array|WP_Error The current purchase token or WP_Error with error details.
67
	 */
68
	public function get_purchase_token() {
69
		$blog_id = Jetpack_Options::get_option( 'id' );
70
		if ( ! $blog_id ) {
71
			return new WP_Error( 'site_not_registered', esc_html__( 'Site not registered.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'site_not_registered'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
72
		}
73
74
		$purchase_token = Jetpack_Options::get_option( 'purchase_token', false );
75
		$response       = array(
76
			'purchaseToken' => $purchase_token,
77
		);
78
79
		return $response;
80
	}
81
82
	/**
83
	 * Deletes the purchaseToken Jetpack_Option
84
	 *
85
	 * @return boolean|WP_Error Whether the token was deleted or WP_Error with error details.
86
	 */
87
	public static function delete_purchase_token() {
88
		$blog_id = Jetpack_Options::get_option( 'id' );
89
		if ( ! $blog_id ) {
90
			return new WP_Error( 'site_not_registered', esc_html__( 'Site not registered.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'site_not_registered'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
91
		}
92
93
		return Jetpack_Options::delete_option( 'purchase_token' );
94
	}
95
}
96
97
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Purchase_Token' );
98