RESTWP.php
7.38 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
namespace WP_Rocket\Engine\CriticalPath;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use WP_Rocket\Admin\Options_Data;
/**
* Class RESTWP
*
* @package WP_Rocket\Engine\CriticalPath
*/
abstract class RESTWP implements RESTWPInterface {
/**
* Namespace for REST Route.
*/
const ROUTE_NAMESPACE = 'wp-rocket/v1';
/**
* Part of route namespace for this inherited class item type.
*
* @var string $route_namespace to be set with like post, term.
*/
protected $route_namespace;
/**
* CPCSS generation and deletion service.
*
* @var ProcessorService instance for this service.
*/
private $cpcss_service;
/**
* WP Rocket options instance.
*
* @var Options_Data
*/
private $options;
/**
* RESTWP constructor.
*
* @since 3.6
*
* @param ProcessorService $cpcss_service Has the logic for cpcss generation and deletion.
* @param Options_Data $options Instance of options data handler.
*/
public function __construct( ProcessorService $cpcss_service, Options_Data $options ) {
$this->cpcss_service = $cpcss_service;
$this->options = $options;
}
/**
* Registers the generate route in the WP REST API
*
* @since 3.6
*
* @return void
*/
public function register_generate_route() {
register_rest_route(
self::ROUTE_NAMESPACE,
'cpcss/' . $this->route_namespace . '/(?P<id>[\d]+)',
[
'methods' => 'POST',
'callback' => [ $this, 'generate' ],
'permission_callback' => [ $this, 'check_permissions' ],
]
);
}
/**
* Register Delete CPCSS route in the WP REST API.
*
* @since 3.6
*/
public function register_delete_route() {
register_rest_route(
self::ROUTE_NAMESPACE,
'cpcss/' . $this->route_namespace . '/(?P<id>[\d]+)',
[
'methods' => 'DELETE',
'callback' => [ $this, 'delete' ],
'permission_callback' => [ $this, 'check_permissions' ],
]
);
}
/**
* Checks user's permissions. This is a callback registered to REST route's "permission_callback" parameter.
*
* @since 3.6
*
* @return bool true if the user has permission; else false.
*/
public function check_permissions() {
return current_user_can( 'rocket_regenerate_critical_css' );
}
/**
* Clean post cache files on CPCSS generation or deletion.
*
* @since 3.6.1
*
* @param int $item_id ID for this item to get Url for.
*/
private function clean_post_cache( $item_id ) {
rocket_clean_files( $this->get_url( $item_id ) );
}
/**
* Generates the CPCSS for the requested post ID.
*
* @since 3.6
*
* @param WP_REST_Request $request WP REST request response.
*
* @return WP_REST_Response
*/
public function generate( WP_REST_Request $request ) {
$item_id = (int) $request->get_param( 'id' );
$is_mobile = (bool) $request->get_param( 'is_mobile' );
// Bailout in case mobile CPCSS generation is called but this option is disabled.
if (
$is_mobile
&&
(
! $this->options->get( 'async_css_mobile', 0 )
||
! $this->options->get( 'do_caching_mobile_files', 0 )
)
) {
return rest_ensure_response(
$this->return_error(
new WP_Error(
'mobile_cpcss_not_enabled',
__( 'Mobile CPCSS generation not enabled.', 'rocket' ),
[
'status' => 400,
]
)
)
);
}
// validate item.
$validated = $this->validate_item_for_generate( $item_id );
if ( is_wp_error( $validated ) ) {
return rest_ensure_response( $this->return_error( $validated ) );
}
// get item url.
$item_url = $this->get_url( $item_id );
$timeout = ( isset( $request['timeout'] ) && ! empty( $request['timeout'] ) );
$item_path = $this->get_path( $item_id, $is_mobile );
$additional_params = [
'timeout' => $timeout,
'is_mobile' => $is_mobile,
'item_type' => 'custom',
];
$generated = $this->cpcss_service->process_generate( $item_url, $item_path, $additional_params );
if ( is_wp_error( $generated ) ) {
return rest_ensure_response(
$this->return_error( $generated )
);
}
$this->clean_post_cache( $item_id );
return rest_ensure_response(
$this->return_success( $generated )
);
}
/**
* Validate the item to be sent to generate CPCSS.
*
* @since 3.6
*
* @param int $item_id ID for this item to be validated.
*
* @return true|WP_Error
*/
abstract protected function validate_item_for_generate( $item_id );
/**
* Validate the item to be sent to Delete CPCSS.
*
* @since 3.6
*
* @param int $item_id ID for this item to be validated.
*
* @return true|WP_Error
*/
abstract protected function validate_item_for_delete( $item_id );
/**
* Get url for this item.
*
* @since 3.6
*
* @param int $item_id ID for this item to get Url for.
*
* @return false|string
*/
abstract protected function get_url( $item_id );
/**
* Get CPCSS file path to save CPCSS code into.
*
* @since 3.6
*
* @param int $item_id ID for this item to get the path for.
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
*
* @return string
*/
abstract protected function get_path( $item_id, $is_mobile = false );
/**
* Delete Post ID CPCSS file.
*
* @since 3.6
*
* @param WP_REST_Request $request the WP Rest Request object.
*
* @return WP_REST_Response
*/
public function delete( WP_REST_Request $request ) {
$item_id = (int) $request->get_param( 'id' );
// validate item.
$validated = $this->validate_item_for_delete( $item_id );
if ( is_wp_error( $validated ) ) {
return rest_ensure_response( $this->return_error( $validated ) );
}
if ( $this->options->get( 'async_css_mobile', 0 ) ) {
$mobile_item_path = $this->get_path( $item_id, true );
$this->cpcss_service->process_delete( $mobile_item_path );
}
$item_path = $this->get_path( $item_id );
$deleted = $this->cpcss_service->process_delete( $item_path );
if ( is_wp_error( $deleted ) ) {
return rest_ensure_response( $this->return_error( $deleted ) );
}
$this->clean_post_cache( $item_id );
return rest_ensure_response( $this->return_success( $deleted ) );
}
/**
* Returns the formatted array response
*
* @since 3.6
*
* @param bool $success True for success, false otherwise.
* @param string $code The code to use for the response.
* @param string $message The message to send in the response.
* @param int $status The status code to send for the response.
*
* @return array
*/
protected function return_array_response( $success = false, $code = '', $message = '', $status = 200 ) {
return [
'success' => $success,
'code' => $code,
'message' => $message,
'data' => [
'status' => $status,
],
];
}
/**
* Convert WP_Error into array to be used in response.
*
* @since 3.6
*
* @param WP_Error $error Error that will be converted to array.
*
* @return array
*/
protected function return_error( $error ) {
$error_data = $error->get_error_data();
return $this->return_array_response(
false,
$error->get_error_code(),
$error->get_error_message(),
isset( $error_data['status'] ) ? $error_data['status'] : 400
);
}
/**
* Return success to be used in response.
*
* @since 3.6
*
* @param array $data which has success parameters with two keys: code and message.
*
* @return array
*/
protected function return_success( $data ) {
return $this->return_array_response(
true,
$data['code'],
$data['message'],
200
);
}
}