abstract-wc-stripe-connect-rest-controller.php
3.31 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// phpcs:disable WordPress.Files.FileName
/**
* Stripe Connect base REST controller class.
*/
abstract class WC_Stripe_Connect_REST_Controller extends WP_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v1';
/**
* Stripe connect api.
*
* @var object $api
*/
private $api;
/**
* Constructor.
*
* @param WC_Stripe_Connect_API $api stripe connect api.
*/
public function __construct( WC_Stripe_Connect_API $api ) {
$this->api = $api;
}
/**
* Registers rest routes for stripe connect functionality
*/
public function register_routes() {
if ( method_exists( $this, 'get' ) ) {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
[
'methods' => 'GET',
'callback' => [ $this, 'get_internal' ],
'permission_callback' => [ $this, 'check_permission' ],
],
]
);
}
if ( method_exists( $this, 'post' ) ) {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
[
'methods' => 'POST',
'callback' => [ $this, 'post_internal' ],
'permission_callback' => [ $this, 'check_permission' ],
],
]
);
}
if ( method_exists( $this, 'delete' ) ) {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
[
'methods' => 'DELETE',
'callback' => [ $this, 'delete_internal' ],
'permission_callback' => [ $this, 'check_permission' ],
],
]
);
}
}
/**
* Send get request.
*
* @param array $request request.
*
* @return array
*/
public function get_internal( $request ) {
$this->prevent_route_caching();
return $this->get( $request );
}
/**
* Send post request.
*
* @param array $request request.
*
* @return array
*/
public function post_internal( $request ) {
$this->prevent_route_caching();
return $this->post( $request );
}
/**
* Sends delete request.
*
* @param array $request request.
*
* @return array
*/
public function delete_internal( $request ) {
$this->prevent_route_caching();
return $this->delete( $request );
}
/**
* Validate the requester's permissions
*
* @param array $request request.
*
* @return bool
*/
public function check_permission( $request ) {
return current_user_can( 'manage_woocommerce' );
}
/**
* Consolidate cache prevention mechanisms.
*/
public function prevent_route_caching() {
if ( ! defined( 'DONOTCACHEPAGE' ) ) {
define( 'DONOTCACHEPAGE', true ); // Play nice with WP-Super-Cache.
}
// Prevent our REST API endpoint responses from being added to browser cache.
add_filter( 'rest_post_dispatch', [ $this, 'send_nocache_header' ], PHP_INT_MAX, 2 );
}
/**
* Send a no-cache header for WCS REST API responses. Prompted by cache issues
* on the Pantheon hosting platform.
*
* See: https://pantheon.io/docs/cache-control/
*
* @param WP_REST_Response $response REST API response.
* @param WP_REST_Server $server server.
*
* @return WP_REST_Response passthrough $response parameter
*/
public function send_nocache_header( $response, $server ) {
$server->send_header( 'Cache-Control', 'no-cache, must-revalidate, max-age=0' );
return $response;
}
}