|
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' ) ); |
|
|
|
|
|
|
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' ) ); |
|
|
|
|
|
|
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
|
|
|
|
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
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.