class-wc-rest-connect-base-controller.php
3.56 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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( class_exists( 'WC_REST_Connect_Base_Controller' ) ) {
return;
}
abstract class WC_REST_Connect_Base_Controller extends WP_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v1';
/**
* @var WC_Connect_API_Client
*/
protected $api_client;
/**
* @var WC_Connect_Service_Settings_Store
*/
protected $settings_store;
/**
* @var WC_Connect_Logger
*/
protected $logger;
public function __construct( WC_Connect_API_Client $api_client, WC_Connect_Service_Settings_Store $settings_store, WC_Connect_Logger $logger ) {
$this->api_client = $api_client;
$this->settings_store = $settings_store;
$this->logger = $logger;
}
public function register_routes() {
if ( method_exists( $this, 'get' ) ) {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'get_internal' ),
'permission_callback' => array( $this, 'check_permission' ),
),
)
);
}
if ( method_exists( $this, 'post' ) ) {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => 'POST',
'callback' => array( $this, 'post_internal' ),
'permission_callback' => array( $this, 'check_permission' ),
),
)
);
}
if ( method_exists( $this, 'put' ) ) {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => 'PUT',
'callback' => array( $this, 'put_internal' ),
'permission_callback' => array( $this, 'check_permission' ),
),
)
);
}
if ( method_exists( $this, 'delete' ) ) {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => 'DELETE',
'callback' => array( $this, 'delete_internal' ),
'permission_callback' => array( $this, 'check_permission' ),
),
)
);
}
}
/**
* 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', array( $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
* @param WP_REST_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;
}
public function get_internal( $request ) {
$this->prevent_route_caching();
return $this->get( $request );
}
public function post_internal( $request ) {
$this->prevent_route_caching();
return $this->post( $request );
}
public function put_internal( $request ) {
$this->prevent_route_caching();
return $this->put( $request );
}
public function delete_internal( $request ) {
$this->prevent_route_caching();
return $this->delete( $request );
}
/**
* Validate the requester's permissions
*/
public function check_permission( $request ) {
return WC_Connect_Functions::user_can_manage_labels();
}
}