1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Stripe Connect base REST controller class. |
9
|
|
|
*/ |
10
|
|
|
abstract class WC_Stripe_Connect_REST_Controller extends WP_REST_Controller { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Endpoint namespace. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $namespace = 'wc/v1'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Stripe connect api. |
21
|
|
|
* |
22
|
|
|
* @var object $api |
23
|
|
|
*/ |
24
|
|
|
private $api; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor. |
28
|
|
|
* |
29
|
|
|
* @param WC_Stripe_Connect_API $api stripe connect api. |
30
|
|
|
*/ |
31
|
|
|
public function __construct( WC_Stripe_Connect_API $api ) { |
32
|
|
|
|
33
|
|
|
$this->api = $api; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Registers rest routes for stripe connect functionality |
38
|
|
|
*/ |
39
|
|
|
public function register_routes() { |
40
|
|
|
|
41
|
|
View Code Duplication |
if ( method_exists( $this, 'get' ) ) { |
|
|
|
|
42
|
|
|
register_rest_route( |
43
|
|
|
$this->namespace, |
44
|
|
|
'/' . $this->rest_base, |
45
|
|
|
array( |
46
|
|
|
array( |
47
|
|
|
'methods' => 'GET', |
48
|
|
|
'callback' => array( $this, 'get_internal' ), |
49
|
|
|
'permission_callback' => array( $this, 'check_permission' ), |
50
|
|
|
), |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
if ( method_exists( $this, 'post' ) ) { |
|
|
|
|
56
|
|
|
register_rest_route( |
57
|
|
|
$this->namespace, |
58
|
|
|
'/' . $this->rest_base, |
59
|
|
|
array( |
60
|
|
|
array( |
61
|
|
|
'methods' => 'POST', |
62
|
|
|
'callback' => array( $this, 'post_internal' ), |
63
|
|
|
'permission_callback' => array( $this, 'check_permission' ), |
64
|
|
|
), |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
if ( method_exists( $this, 'delete' ) ) { |
|
|
|
|
70
|
|
|
register_rest_route( |
71
|
|
|
$this->namespace, |
72
|
|
|
'/' . $this->rest_base, |
73
|
|
|
array( |
74
|
|
|
array( |
75
|
|
|
'methods' => 'DELETE', |
76
|
|
|
'callback' => array( $this, 'delete_internal' ), |
77
|
|
|
'permission_callback' => array( $this, 'check_permission' ), |
78
|
|
|
), |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Send get request. |
86
|
|
|
* |
87
|
|
|
* @param array $request request. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function get_internal( $request ) { |
92
|
|
|
|
93
|
|
|
$this->prevent_route_caching(); |
94
|
|
|
|
95
|
|
|
return $this->get( $request ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Send post request. |
100
|
|
|
* |
101
|
|
|
* @param array $request request. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function post_internal( $request ) { |
106
|
|
|
|
107
|
|
|
$this->prevent_route_caching(); |
108
|
|
|
|
109
|
|
|
return $this->post( $request ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sends delete request. |
114
|
|
|
* |
115
|
|
|
* @param array $request request. |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function delete_internal( $request ) { |
120
|
|
|
|
121
|
|
|
$this->prevent_route_caching(); |
122
|
|
|
|
123
|
|
|
return $this->delete( $request ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Validate the requester's permissions |
128
|
|
|
* |
129
|
|
|
* @param array $request request. |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function check_permission( $request ) { |
134
|
|
|
|
135
|
|
|
return current_user_can( 'manage_woocommerce' ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Consolidate cache prevention mechanisms. |
140
|
|
|
*/ |
141
|
|
|
public function prevent_route_caching() { |
142
|
|
|
|
143
|
|
|
if ( ! defined( 'DONOTCACHEPAGE' ) ) { |
144
|
|
|
define( 'DONOTCACHEPAGE', true ); // Play nice with WP-Super-Cache. |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Prevent our REST API endpoint responses from being added to browser cache. |
148
|
|
|
add_filter( 'rest_post_dispatch', array( $this, 'send_nocache_header' ), PHP_INT_MAX, 2 ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Send a no-cache header for WCS REST API responses. Prompted by cache issues |
153
|
|
|
* on the Pantheon hosting platform. |
154
|
|
|
* |
155
|
|
|
* See: https://pantheon.io/docs/cache-control/ |
156
|
|
|
* |
157
|
|
|
* @param WP_REST_Response $response REST API response. |
158
|
|
|
* @param WP_REST_Server $server server. |
159
|
|
|
* |
160
|
|
|
* @return WP_REST_Response passthrough $response parameter |
161
|
|
|
*/ |
162
|
|
|
public function send_nocache_header( $response, $server ) { |
163
|
|
|
|
164
|
|
|
$server->send_header( 'Cache-Control', 'no-cache, must-revalidate, max-age=0' ); |
165
|
|
|
|
166
|
|
|
return $response; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.