@@ -62,11 +62,15 @@ public function register_routes() {
62
62
'send ' => array (
63
63
'methods ' => \WP_REST_Server::CREATABLE ,
64
64
'args ' => array (
65
- 'step ' => array (
65
+ 'step ' => array (
66
66
'required ' => true ,
67
67
'type ' => 'string ' ,
68
68
),
69
- 'message ' => array (
69
+ 'message ' => array (
70
+ 'required ' => false ,
71
+ 'type ' => 'string ' ,
72
+ ),
73
+ 'template ' => array (
70
74
'required ' => false ,
71
75
'type ' => 'string ' ,
72
76
),
@@ -114,9 +118,27 @@ public function register_routes() {
114
118
'required ' => true ,
115
119
'type ' => 'string ' ,
116
120
),
121
+ 'images ' => array (
122
+ 'required ' => false ,
123
+ 'type ' => 'array ' ,
124
+ ),
117
125
),
118
126
'callback ' => array ( $ this , 'templates ' ),
119
127
),
128
+ 'homepage ' => array (
129
+ 'methods ' => \WP_REST_Server::READABLE ,
130
+ 'args ' => array (
131
+ 'thread_id ' => array (
132
+ 'required ' => true ,
133
+ 'type ' => 'string ' ,
134
+ ),
135
+ 'template ' => array (
136
+ 'required ' => true ,
137
+ 'type ' => 'string ' ,
138
+ ),
139
+ ),
140
+ 'callback ' => array ( $ this , 'homepage ' ),
141
+ ),
120
142
);
121
143
122
144
foreach ( $ routes as $ route => $ args ) {
@@ -138,14 +160,20 @@ public function register_routes() {
138
160
public function send ( \WP_REST_Request $ request ) {
139
161
$ data = $ request ->get_params ();
140
162
163
+ $ params = array (
164
+ 'step ' => $ data ['step ' ],
165
+ 'message ' => $ data ['message ' ],
166
+ );
167
+
168
+ if ( isset ( $ data ['template ' ] ) ) {
169
+ $ params ['template ' ] = $ data ['template ' ];
170
+ }
171
+
141
172
$ request = wp_remote_post (
142
173
QUICKWP_APP_API . 'wizard/send ' ,
143
174
array (
144
175
'timeout ' => 20 , // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
145
- 'body ' => array (
146
- 'step ' => $ data ['step ' ],
147
- 'message ' => $ data ['message ' ],
148
- ),
176
+ 'body ' => $ params ,
149
177
)
150
178
);
151
179
@@ -254,6 +282,101 @@ public function get( \WP_REST_Request $request ) {
254
282
return new \WP_REST_Response ( $ response , 200 );
255
283
}
256
284
285
+ /**
286
+ * Get homepage.
287
+ *
288
+ * @param \WP_REST_Request $request Request.
289
+ *
290
+ * @return \WP_REST_Response
291
+ */
292
+ public function homepage ( \WP_REST_Request $ request ) {
293
+ $ data = $ request ->get_params ();
294
+
295
+ $ api_url = QUICKWP_APP_API . 'wizard/get ' ;
296
+
297
+ $ query_params = array (
298
+ 'thread_id ' => $ data ['thread_id ' ],
299
+ );
300
+
301
+ $ request_url = add_query_arg ( $ query_params , $ api_url );
302
+
303
+ $ request = wp_safe_remote_get (
304
+ $ request_url ,
305
+ array (
306
+ 'timeout ' => 20 , // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
307
+ )
308
+ );
309
+
310
+ if ( is_wp_error ( $ request ) ) {
311
+ return new \WP_REST_Response ( array ( 'error ' => $ request ->get_error_message () ), 500 );
312
+ }
313
+
314
+ /**
315
+ * Holds the response as a standard class object
316
+ *
317
+ * @var \stdClass $response
318
+ */
319
+ $ response = json_decode ( wp_remote_retrieve_body ( $ request ) );
320
+
321
+ if ( ! isset ( $ response ->data ) || ! $ response ->data ) {
322
+ return new \WP_REST_Response ( array ( 'error ' => __ ( 'Error ' , 'quickwp ' ) ), 500 );
323
+ }
324
+
325
+ $ items = self ::process_json_from_response ( $ response ->data );
326
+
327
+ if ( ! $ items ) {
328
+ return new \WP_REST_Response ( array ( 'error ' => __ ( 'Error Parsing JSON ' , 'quickwp ' ) ), 500 );
329
+ }
330
+
331
+ self ::extract_data ( $ items );
332
+
333
+ $ templates = apply_filters ( 'quickwp_templates ' , array () );
334
+
335
+ if ( empty ( $ templates ) || ! isset ( $ templates ['homepage ' ] ) ) {
336
+ return new \WP_REST_Response ( array ( 'error ' => __ ( 'Error ' , 'quickwp ' ) ), 500 );
337
+ }
338
+
339
+ $ template = $ templates ['homepage ' ][ $ data ['template ' ] ];
340
+
341
+ $ result = array ();
342
+
343
+ $ theme_path = get_stylesheet_directory ();
344
+
345
+ $ patterns = file_get_contents ( $ template ); //phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
346
+
347
+ if ( ! $ patterns ) {
348
+ return new \WP_REST_Response ( array ( 'error ' => __ ( 'Error ' , 'quickwp ' ) ), 500 );
349
+ }
350
+
351
+ preg_match_all ( '/"slug":"(.*?)"/ ' , $ patterns , $ matches );
352
+ $ slugs = $ matches [1 ];
353
+
354
+ $ filtered_patterns = array ();
355
+
356
+ foreach ( $ slugs as $ slug ) {
357
+ $ slug = str_replace ( 'quickwp/ ' , '' , $ slug );
358
+ $ pattern_path = $ theme_path . '/patterns/ ' . $ slug . '.php ' ;
359
+
360
+ if ( ! file_exists ( $ pattern_path ) ) {
361
+ continue ;
362
+ }
363
+
364
+ ob_start ();
365
+ include $ pattern_path ;
366
+ $ pattern_content = ob_get_clean ();
367
+
368
+ $ filtered_patterns [] = $ pattern_content ;
369
+ }
370
+
371
+ return new \WP_REST_Response (
372
+ array (
373
+ 'status ' => 'success ' ,
374
+ 'data ' => implode ( '' , $ filtered_patterns ),
375
+ ),
376
+ 200
377
+ );
378
+ }
379
+
257
380
/**
258
381
* Get templates.
259
382
*
@@ -300,16 +423,65 @@ public function templates( \WP_REST_Request $request ) {
300
423
return new \WP_REST_Response ( array ( 'error ' => __ ( 'Error Parsing JSON ' , 'quickwp ' ) ), 500 );
301
424
}
302
425
426
+ self ::extract_data ( $ items );
427
+
428
+ $ templates = apply_filters ( 'quickwp_templates ' , array () );
429
+
430
+ if ( empty ( $ templates ) || ! isset ( $ templates ['homepage ' ] ) ) {
431
+ return new \WP_REST_Response ( array ( 'error ' => __ ( 'Error ' , 'quickwp ' ) ), 500 );
432
+ }
433
+
434
+ $ items = $ templates ['homepage ' ];
435
+
303
436
$ result = array ();
304
437
305
- foreach ( $ items as $ item ) {
306
- $ pattern = self ::extract_patterns ( $ item );
438
+ $ theme_path = get_stylesheet_directory ();
439
+
440
+ foreach ( $ items as $ item => $ path ) {
441
+ $ pattern = file_get_contents ( $ path ); //phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
307
442
308
443
if ( ! $ pattern ) {
309
444
continue ;
310
445
}
311
446
312
- $ result [] = $ pattern ;
447
+ preg_match_all ( '/"slug":"(.*?)"/ ' , $ pattern , $ matches );
448
+ $ slugs = $ matches [1 ];
449
+
450
+ $ filtered_patterns = array ();
451
+
452
+ foreach ( $ slugs as $ slug ) {
453
+ $ slug = str_replace ( 'quickwp/ ' , '' , $ slug );
454
+ $ pattern_path = $ theme_path . '/patterns/ ' . $ slug . '.php ' ;
455
+
456
+ if ( ! file_exists ( $ pattern_path ) ) {
457
+ continue ;
458
+ }
459
+
460
+ // Check if $data param has images and it counts more than 0.
461
+ if ( isset ( $ data ['images ' ] ) && count ( $ data ['images ' ] ) > 0 ) {
462
+ $ images = $ data ['images ' ];
463
+
464
+ add_filter (
465
+ 'quickwp/image ' ,
466
+ function () use ( $ images ) {
467
+ // Get a random image from the array.
468
+ $ image = $ images [ array_rand ( $ images ) ];
469
+ return esc_url ( $ image ['src ' ] );
470
+ }
471
+ );
472
+ }
473
+
474
+ ob_start ();
475
+ include $ pattern_path ;
476
+ $ pattern_content = ob_get_clean ();
477
+
478
+ $ filtered_patterns [] = $ pattern_content ;
479
+ }
480
+
481
+ $ result [] = array (
482
+ 'slug ' => $ item ,
483
+ 'patterns ' => implode ( '' , $ filtered_patterns ),
484
+ );
313
485
}
314
486
315
487
return new \WP_REST_Response (
@@ -369,6 +541,8 @@ public function images( \WP_REST_Request $request ) {
369
541
*
370
542
* @param array<object> $data Response.
371
543
*
544
+ * @throws \Exception Exception in case of invalid JSON.
545
+ *
372
546
* @return array<object>|false
373
547
*/
374
548
private static function process_json_from_response ( $ data ) {
@@ -392,8 +566,8 @@ private static function process_json_from_response( $data ) {
392
566
throw new \Exception ( 'Invalid JSON ' );
393
567
} catch ( \Exception $ e ) {
394
568
if ( substr ( $ json_string , 0 , 7 ) === '```json ' && substr ( trim ( $ json_string ), -3 ) === '``` ' ) {
395
- $ cleaned_json = trim ( str_replace ( [ '```json ' , '``` ' ] , '' , $ json_string ) );
396
- $ json_object = json_decode ( $ cleaned_json , true );
569
+ $ cleaned_json = trim ( str_replace ( array ( '```json ' , '``` ' ) , '' , $ json_string ) );
570
+ $ json_object = json_decode ( $ cleaned_json , true );
397
571
398
572
if ( is_array ( $ json_object ) ) {
399
573
return $ json_object ;
@@ -405,29 +579,18 @@ private static function process_json_from_response( $data ) {
405
579
}
406
580
407
581
/**
408
- * Extract Patterns .
582
+ * Extract Data .
409
583
*
410
584
* @param array<object> $items Items.
411
585
*
412
- * @return array
586
+ * @return void
413
587
*/
414
- private static function extract_patterns ( $ items ) {
415
- if ( ! isset ( $ items ['slug ' ] ) || ! isset ( $ items ['data ' ] ) ) {
416
- return ;
417
- }
418
-
419
- $ patterns_used = array ();
420
-
421
- foreach ( $ items ['data ' ] as $ item ) {
588
+ private static function extract_data ( $ items ) {
589
+ foreach ( $ items as $ item ) {
422
590
if ( ! isset ( $ item ['slug ' ] ) || ! isset ( $ item ['order ' ] ) || ! isset ( $ item ['strings ' ] ) ) {
423
591
continue ;
424
592
}
425
593
426
- $ patterns_used [] = array (
427
- 'order ' => $ item ['order ' ],
428
- 'slug ' => $ item ['slug ' ],
429
- );
430
-
431
594
$ strings = $ item ['strings ' ];
432
595
433
596
foreach ( $ strings as $ string ) {
@@ -456,37 +619,5 @@ function ( $value ) use( $image ) {
456
619
}
457
620
}
458
621
}
459
-
460
- usort (
461
- $ patterns_used ,
462
- function ( $ a , $ b ) {
463
- return $ a ['order ' ] <=> $ b ['order ' ];
464
- }
465
- );
466
-
467
- $ theme_path = get_stylesheet_directory ();
468
-
469
- $ filtered_patterns = array ();
470
-
471
- foreach ( $ patterns_used as $ pattern ) {
472
- $ pattern ['slug ' ] = str_replace ( 'quickwp/ ' , '' , $ pattern ['slug ' ] );
473
-
474
- $ pattern_path = $ theme_path . '/patterns/ ' . $ pattern ['slug ' ] . '.php ' ;
475
-
476
- if ( ! file_exists ( $ pattern_path ) ) {
477
- continue ;
478
- }
479
-
480
- ob_start ();
481
- include $ pattern_path ;
482
- $ pattern_content = ob_get_clean ();
483
-
484
- $ filtered_patterns [] = $ pattern_content ;
485
- }
486
-
487
- return array (
488
- 'slug ' => $ items ['slug ' ],
489
- 'patterns ' => implode ( '' , $ filtered_patterns ),
490
- );
491
622
}
492
623
}
0 commit comments