Skip to content

Commit 9af2577

Browse files
committed
feat: add event tracking and various changes
1 parent 7afae81 commit 9af2577

13 files changed

+504
-195
lines changed

inc/class-api.php

+189-58
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ public function register_routes() {
6262
'send' => array(
6363
'methods' => \WP_REST_Server::CREATABLE,
6464
'args' => array(
65-
'step' => array(
65+
'step' => array(
6666
'required' => true,
6767
'type' => 'string',
6868
),
69-
'message' => array(
69+
'message' => array(
70+
'required' => false,
71+
'type' => 'string',
72+
),
73+
'template' => array(
7074
'required' => false,
7175
'type' => 'string',
7276
),
@@ -114,9 +118,27 @@ public function register_routes() {
114118
'required' => true,
115119
'type' => 'string',
116120
),
121+
'images' => array(
122+
'required' => false,
123+
'type' => 'array',
124+
),
117125
),
118126
'callback' => array( $this, 'templates' ),
119127
),
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+
),
120142
);
121143

122144
foreach ( $routes as $route => $args ) {
@@ -138,14 +160,20 @@ public function register_routes() {
138160
public function send( \WP_REST_Request $request ) {
139161
$data = $request->get_params();
140162

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+
141172
$request = wp_remote_post(
142173
QUICKWP_APP_API . 'wizard/send',
143174
array(
144175
'timeout' => 20, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
145-
'body' => array(
146-
'step' => $data['step'],
147-
'message' => $data['message'],
148-
),
176+
'body' => $params,
149177
)
150178
);
151179

@@ -254,6 +282,101 @@ public function get( \WP_REST_Request $request ) {
254282
return new \WP_REST_Response( $response, 200 );
255283
}
256284

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+
257380
/**
258381
* Get templates.
259382
*
@@ -300,16 +423,65 @@ public function templates( \WP_REST_Request $request ) {
300423
return new \WP_REST_Response( array( 'error' => __( 'Error Parsing JSON', 'quickwp' ) ), 500 );
301424
}
302425

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+
303436
$result = array();
304437

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
307442

308443
if ( ! $pattern ) {
309444
continue;
310445
}
311446

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+
);
313485
}
314486

315487
return new \WP_REST_Response(
@@ -369,6 +541,8 @@ public function images( \WP_REST_Request $request ) {
369541
*
370542
* @param array<object> $data Response.
371543
*
544+
* @throws \Exception Exception in case of invalid JSON.
545+
*
372546
* @return array<object>|false
373547
*/
374548
private static function process_json_from_response( $data ) {
@@ -392,8 +566,8 @@ private static function process_json_from_response( $data ) {
392566
throw new \Exception( 'Invalid JSON' );
393567
} catch ( \Exception $e ) {
394568
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 );
397571

398572
if ( is_array( $json_object ) ) {
399573
return $json_object;
@@ -405,29 +579,18 @@ private static function process_json_from_response( $data ) {
405579
}
406580

407581
/**
408-
* Extract Patterns.
582+
* Extract Data.
409583
*
410584
* @param array<object> $items Items.
411585
*
412-
* @return array
586+
* @return void
413587
*/
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 ) {
422590
if ( ! isset( $item['slug'] ) || ! isset( $item['order'] ) || ! isset( $item['strings'] ) ) {
423591
continue;
424592
}
425593

426-
$patterns_used[] = array(
427-
'order' => $item['order'],
428-
'slug' => $item['slug'],
429-
);
430-
431594
$strings = $item['strings'];
432595

433596
foreach ( $strings as $string ) {
@@ -456,37 +619,5 @@ function ( $value ) use( $image ) {
456619
}
457620
}
458621
}
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-
);
491622
}
492623
}

inc/class-main.php

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function enqueue_assets() {
8787
array(
8888
'api' => $this->api->get_endpoint(),
8989
'siteUrl' => get_site_url(),
90+
'themeSlug' => get_template(),
9091
'isGuidedMode' => defined( 'QUICKWP_APP_GUIDED_MODE' ) && QUICKWP_APP_GUIDED_MODE,
9192
)
9293
);

quickwp.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
define( 'QUICKWP_APP_VERSION', '1.0.0' );
2424

2525
if ( ! defined( 'QUICKWP_APP_API' ) ) {
26-
define( 'QUICKWP_APP_API', 'https://aaf0-103-217-244-105.ngrok-free.app/api/' );
26+
define( 'QUICKWP_APP_API', 'https://quickwp.com/api/' );
2727
}
2828

2929
if ( ! defined( 'QUICKWP_APP_GUIDED_MODE' ) ) {

src/App.js

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useEffect } from '@wordpress/element';
1616
import { useIsSiteEditorLoading } from './hooks';
1717
import Loader from './components/Loader';
1818
import Header from './parts/Header';
19+
import { recordEvent } from './utils';
1920

2021
const App = () => {
2122
const isEditorLoading = useIsSiteEditorLoading();
@@ -45,6 +46,8 @@ const App = () => {
4546
if ( hasWelcome ) {
4647
toggle( 'core/edit-site', 'welcomeGuide' );
4748
}
49+
50+
recordEvent();
4851
}, [ hasWelcome ]);
4952

5053
const StepControls = currentStep?.view || null;

0 commit comments

Comments
 (0)