ProcessorService.php
10.1 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
namespace WP_Rocket\Engine\CriticalPath;
use WP_Error;
class ProcessorService {
/**
* Responsible for dealing with data/database.
*
* @var DataManager datamanager instance.
*/
private $data_manager;
/**
* Responsible for dealing with CPCSS APIs.
*
* @var APIClient api_client instance.
*/
private $api_client;
/**
* RESTWP constructor.
*
* @since 3.6
*
* @param DataManager $data_manager Data manager instance, responsible for dealing with data/database.
* @param APIClient $api_client API Client instance to deal with CPCSS APIs.
*/
public function __construct( DataManager $data_manager, APIClient $api_client ) {
$this->data_manager = $data_manager;
$this->api_client = $api_client;
}
/**
* Process CPCSS generation, Check timeout and send the generation request.
*
* @since 3.6
*
* @param string $item_url URL for item to be used in error messages.
* @param string $item_path Path for item to be processed.
* @param array $additional_parameters additional parameters for generation.
*
* @return array|WP_Error
*/
public function process_generate( $item_url, $item_path, $additional_parameters = [] ) {
$defaults = [
'timeout' => false,
'is_mobile' => false,
'item_type' => 'custom',
];
$args = array_merge( $defaults, $additional_parameters );
// Ajax call requested a timeout.
if ( $args['timeout'] ) {
return $this->process_timeout( $item_url, $args['is_mobile'], $args['item_type'] );
}
$cpcss_job_id = $this->data_manager->get_cache_job_id( $item_url, $args['is_mobile'] );
if ( false === $cpcss_job_id ) {
return $this->send_generation_request( $item_url, $item_path, $args['is_mobile'], $args['item_type'] );
}
// job_id is found and we need to check status for it.
return $this->check_cpcss_job_status( $cpcss_job_id, $item_path, $item_url, $args['is_mobile'], $args['item_type'] );
}
/**
* Send Generation first request.
*
* @since 3.6
*
* @param string $item_url Url for item to send the generation request for.
* @param string $item_path Path for item to send the generation request for.
* @param bool $is_mobile If this request is for mobile cpcss.
* @param string $item_type Optional. Type for this item if it's custom or specific type. Default: custom.
*
* @return array
*/
private function send_generation_request( $item_url, $item_path, $is_mobile = false, $item_type = 'custom' ) {
// call send generation request from APIClient for the first time.
$params = [
'mobile' => (int) $is_mobile,
'nofontface' => false,
];
$generated_job = $this->api_client->send_generation_request( $item_url, $params, $item_type );
// validate generate response.
if ( is_wp_error( $generated_job ) ) {
// Failed so return back the data.
return $generated_job;
}
// Send generation request succeeded.
// Save job_id into cache.
$this->data_manager->set_cache_job_id( $item_url, $generated_job->data->id, $is_mobile );
return $this->check_cpcss_job_status( $generated_job->data->id, $item_path, $item_url, $is_mobile, $item_type );
}
/**
* Get job details by job_id.
*
* @since 3.6
*
* @param string $job_id ID for the job to get details.
* @param string $item_url URL for item to be used in error messages.
* @param string $item_type Optional. Type for this item if it's custom or specific type. Default: custom.
*
* @return array|mixed|WP_Error
*/
private function get_cpcss_job_details( $job_id, $item_url, $item_type = 'custom' ) {
$job_details = $this->api_client->get_job_details( $job_id, $item_url, $item_type );
if ( is_wp_error( $job_details ) ) {
return $job_details;
}
return $job_details;
}
/**
* Check status and process the output for a job.
*
* @since 3.6
*
* @param string $job_id ID for the job to get details.
* @param string $item_path Path for this item to be validated.
* @param string $item_url URL for item to be used in error messages.
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
* @param string $item_type Optional. Type for this item if it's custom or specific type. Default: custom.
*
* @return array|WP_Error Response in case of success, failure or pending.
*/
private function check_cpcss_job_status( $job_id, $item_path, $item_url, $is_mobile = false, $item_type = 'custom' ) {
$job_details = $this->api_client->get_job_details( $job_id, $item_url, $is_mobile, $item_type );
if ( is_wp_error( $job_details ) ) {
$this->data_manager->delete_cache_job_id( $item_url, $is_mobile );
return $job_details;
}
if ( 200 !== $job_details->status ) {
// On job error.
return $this->on_job_error( $job_details, $item_url, $is_mobile, $item_type );
}
// On job status 200.
$job_state = $job_details->data->state;
// For pending job status.
if ( isset( $job_state ) && 'complete' !== $job_state ) {
return $this->on_job_pending( $item_url, $item_type );
}
// For successful job status.
if (
isset( $job_state, $job_details->data->critical_path )
&&
'complete' === $job_state
) {
return $this->on_job_success( $item_path, $item_url, $job_details->data->critical_path, $is_mobile, $item_type );
}
}
/**
* Process logic for job error.
*
* @since 3.6
*
* @param array $job_details Job details array.
* @param string $item_url Url for web page to be processed, used for error messages.
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
* @param string $item_type Optional. Type for this item if it's custom or specific type. Default: custom.
*
* @return WP_Error
*/
private function on_job_error( $job_details, $item_url, $is_mobile = false, $item_type = 'custom' ) {
$this->data_manager->delete_cache_job_id( $item_url, $is_mobile );
if ( $is_mobile ) {
$error = sprintf(
// translators: %1$s = item URL or item type.
__( 'Mobile Critical CSS for %1$s not generated.', 'rocket' ),
( 'custom' === $item_type ) ? $item_url : $item_type
);
} else {
$error = sprintf(
// translators: %1$s = item URL or item type.
__( 'Critical CSS for %1$s not generated.', 'rocket' ),
( 'custom' === $item_type ) ? $item_url : $item_type
);
}
if ( isset( $job_details->message ) ) {
// translators: %1$s = error message.
$error .= ' ' . sprintf( __( 'Error: %1$s', 'rocket' ), $job_details->message );
}
return new WP_Error(
'cpcss_generation_failed',
$error,
[
'status' => 400,
]
);
}
/**
* Process logic for job pending status.
*
* @since 3.6
*
* @param string $item_url Url for web page to be processed, used for error messages.
* @param string $item_type Optional. Type for this item if it's custom or specific type. Default: custom.
*
* @return array
*/
private function on_job_pending( $item_url, $item_type = 'custom' ) {
return [
'code' => 'cpcss_generation_pending',
'message' => sprintf(
// translators: %1$s = Item URL or item type.
__( 'Critical CSS for %s in progress.', 'rocket' ),
( 'custom' === $item_type ) ? $item_url : $item_type
),
];
}
/**
* Process logic for job success status.
*
* @since 3.6
*
* @param string $item_path Item Path for web page to be processed.
* @param string $item_url Item Url for web page to be processed.
* @param string $cpcss_code CPCSS Code to be saved.
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
* @param string $item_type Optional. Type for this item if it's custom or specific type. Default: custom.
*
* @return array|WP_Error
*/
private function on_job_success( $item_path, $item_url, $cpcss_code, $is_mobile = false, $item_type = 'custom' ) {
// delete cache job_id for this item.
$this->data_manager->delete_cache_job_id( $item_url, $is_mobile );
// save the generated CPCSS code into file.
$saved = $this->data_manager->save_cpcss( $item_path, $cpcss_code, $item_url, $is_mobile, $item_type );
if ( is_wp_error( $saved ) ) {
return $saved;
}
if ( $is_mobile ) {
return [
'code' => 'cpcss_generation_successful',
'message' => sprintf(
// translators: %1$s = Item URL or item type.
__( 'Mobile Critical CSS for %s generated.', 'rocket' ),
( 'custom' === $item_type ) ? $item_url : $item_type
),
];
}
// Send the current status of job.
return [
'code' => 'cpcss_generation_successful',
'message' => sprintf(
// translators: %1$s = Item URL or item type.
__( 'Critical CSS for %s generated.', 'rocket' ),
( 'custom' === $item_type ) ? $item_url : $item_type
),
];
}
/**
* Process the login for CPCSS deletion.
*
* @param string $item_path Path for item to delete CPCSS code.
*
* @return array|WP_Error
*/
public function process_delete( $item_path ) {
$deleted = $this->data_manager->delete_cpcss( $item_path );
if ( is_wp_error( $deleted ) ) {
return $deleted;
}
return [
'code' => 'success',
'message' => __( 'Critical CSS file deleted successfully.', 'rocket' ),
];
}
/**
* Process timeout action for CPCSS generation.
*
* @since 3.6
*
* @param string $item_url URL for item to be used in error messages.
* @param bool $is_mobile Bool identifier for is_mobile CPCSS generation.
* @param string $item_type Optional. Type for this item if it's custom or specific type. Default: custom.
* @return WP_Error
*/
private function process_timeout( $item_url, $is_mobile = false, $item_type = 'custom' ) {
$this->data_manager->delete_cache_job_id( $item_url, $is_mobile );
if ( $is_mobile ) {
return new WP_Error(
'cpcss_generation_timeout',
sprintf(
// translators: %1$s = Item URL or item type.
__( 'Mobile Critical CSS for %1$s timeout. Please retry a little later.', 'rocket' ),
( 'custom' === $item_type ) ? $item_url : $item_type
),
[
'status' => 400,
]
);
}
return new WP_Error(
'cpcss_generation_timeout',
sprintf(
// translators: %1$s = Item URL or item type.
__( 'Critical CSS for %1$s timeout. Please retry a little later.', 'rocket' ),
( 'custom' === $item_type ) ? $item_url : $item_type
),
[
'status' => 400,
]
);
}
}