Skip to content

Commit

Permalink
feat: add event tracking and various changes
Browse files Browse the repository at this point in the history
  • Loading branch information
HardeepAsrani committed Feb 12, 2024
1 parent 7afae81 commit 9af2577
Show file tree
Hide file tree
Showing 13 changed files with 504 additions and 195 deletions.
247 changes: 189 additions & 58 deletions inc/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ public function register_routes() {
'send' => array(
'methods' => \WP_REST_Server::CREATABLE,
'args' => array(
'step' => array(
'step' => array(
'required' => true,
'type' => 'string',
),
'message' => array(
'message' => array(
'required' => false,
'type' => 'string',
),
'template' => array(
'required' => false,
'type' => 'string',
),
Expand Down Expand Up @@ -114,9 +118,27 @@ public function register_routes() {
'required' => true,
'type' => 'string',
),
'images' => array(
'required' => false,
'type' => 'array',
),
),
'callback' => array( $this, 'templates' ),
),
'homepage' => array(
'methods' => \WP_REST_Server::READABLE,
'args' => array(
'thread_id' => array(
'required' => true,
'type' => 'string',
),
'template' => array(
'required' => true,
'type' => 'string',
),
),
'callback' => array( $this, 'homepage' ),
),
);

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

$params = array(
'step' => $data['step'],
'message' => $data['message'],
);

if ( isset( $data['template'] ) ) {
$params['template'] = $data['template'];
}

$request = wp_remote_post(
QUICKWP_APP_API . 'wizard/send',
array(
'timeout' => 20, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'step' => $data['step'],
'message' => $data['message'],
),
'body' => $params,
)
);

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

/**
* Get homepage.
*
* @param \WP_REST_Request $request Request.
*
* @return \WP_REST_Response
*/
public function homepage( \WP_REST_Request $request ) {
$data = $request->get_params();

$api_url = QUICKWP_APP_API . 'wizard/get';

$query_params = array(
'thread_id' => $data['thread_id'],
);

$request_url = add_query_arg( $query_params, $api_url );

$request = wp_safe_remote_get(
$request_url,
array(
'timeout' => 20, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
)
);

if ( is_wp_error( $request ) ) {
return new \WP_REST_Response( array( 'error' => $request->get_error_message() ), 500 );
}

/**
* Holds the response as a standard class object
*
* @var \stdClass $response
*/
$response = json_decode( wp_remote_retrieve_body( $request ) );

if ( ! isset( $response->data ) || ! $response->data ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
}

$items = self::process_json_from_response( $response->data );

if ( ! $items ) {
return new \WP_REST_Response( array( 'error' => __( 'Error Parsing JSON', 'quickwp' ) ), 500 );
}

self::extract_data( $items );

$templates = apply_filters( 'quickwp_templates', array() );

if ( empty( $templates ) || ! isset( $templates['homepage'] ) ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
}

$template = $templates['homepage'][ $data['template'] ];

$result = array();

$theme_path = get_stylesheet_directory();

$patterns = file_get_contents( $template ); //phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown

if ( ! $patterns ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
}

preg_match_all( '/"slug":"(.*?)"/', $patterns, $matches );
$slugs = $matches[1];

$filtered_patterns = array();

foreach ( $slugs as $slug ) {
$slug = str_replace( 'quickwp/', '', $slug );
$pattern_path = $theme_path . '/patterns/' . $slug . '.php';

if ( ! file_exists( $pattern_path ) ) {
continue;
}

ob_start();
include $pattern_path;
$pattern_content = ob_get_clean();

$filtered_patterns[] = $pattern_content;
}

return new \WP_REST_Response(
array(
'status' => 'success',
'data' => implode( '', $filtered_patterns ),
),
200
);
}

/**
* Get templates.
*
Expand Down Expand Up @@ -300,16 +423,65 @@ public function templates( \WP_REST_Request $request ) {
return new \WP_REST_Response( array( 'error' => __( 'Error Parsing JSON', 'quickwp' ) ), 500 );
}

self::extract_data( $items );

$templates = apply_filters( 'quickwp_templates', array() );

if ( empty( $templates ) || ! isset( $templates['homepage'] ) ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
}

$items = $templates['homepage'];

$result = array();

foreach( $items as $item ) {
$pattern = self::extract_patterns( $item );
$theme_path = get_stylesheet_directory();

foreach ( $items as $item => $path ) {
$pattern = file_get_contents( $path ); //phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown

if ( ! $pattern ) {
continue;
}

$result[] = $pattern;
preg_match_all( '/"slug":"(.*?)"/', $pattern, $matches );
$slugs = $matches[1];

$filtered_patterns = array();

foreach ( $slugs as $slug ) {
$slug = str_replace( 'quickwp/', '', $slug );
$pattern_path = $theme_path . '/patterns/' . $slug . '.php';

if ( ! file_exists( $pattern_path ) ) {
continue;
}

// Check if $data param has images and it counts more than 0.
if ( isset( $data['images'] ) && count( $data['images'] ) > 0 ) {
$images = $data['images'];

add_filter(
'quickwp/image',
function () use( $images ) {
// Get a random image from the array.
$image = $images[ array_rand( $images ) ];
return esc_url( $image['src'] );
}
);
}

ob_start();
include $pattern_path;
$pattern_content = ob_get_clean();

$filtered_patterns[] = $pattern_content;
}

$result[] = array(
'slug' => $item,
'patterns' => implode( '', $filtered_patterns ),
);
}

return new \WP_REST_Response(
Expand Down Expand Up @@ -369,6 +541,8 @@ public function images( \WP_REST_Request $request ) {
*
* @param array<object> $data Response.
*
* @throws \Exception Exception in case of invalid JSON.
*
* @return array<object>|false
*/
private static function process_json_from_response( $data ) {
Expand All @@ -392,8 +566,8 @@ private static function process_json_from_response( $data ) {
throw new \Exception( 'Invalid JSON' );
} catch ( \Exception $e ) {
if ( substr( $json_string, 0, 7 ) === '```json' && substr( trim( $json_string ), -3 ) === '```' ) {
$cleaned_json = trim( str_replace( [ '```json', '```' ], '', $json_string ) );
$json_object = json_decode( $cleaned_json, true );
$cleaned_json = trim( str_replace( array( '```json', '```' ), '', $json_string ) );
$json_object = json_decode( $cleaned_json, true );

if ( is_array( $json_object ) ) {
return $json_object;
Expand All @@ -405,29 +579,18 @@ private static function process_json_from_response( $data ) {
}

/**
* Extract Patterns.
* Extract Data.
*
* @param array<object> $items Items.
*
* @return array
* @return void
*/
private static function extract_patterns( $items ) {
if ( ! isset( $items['slug'] ) || ! isset( $items['data'] ) ) {
return;
}

$patterns_used = array();

foreach ( $items['data'] as $item ) {
private static function extract_data( $items ) {
foreach ( $items as $item ) {
if ( ! isset( $item['slug'] ) || ! isset( $item['order'] ) || ! isset( $item['strings'] ) ) {
continue;
}

$patterns_used[] = array(
'order' => $item['order'],
'slug' => $item['slug'],
);

$strings = $item['strings'];

foreach ( $strings as $string ) {
Expand Down Expand Up @@ -456,37 +619,5 @@ function ( $value ) use( $image ) {
}
}
}

usort(
$patterns_used,
function ( $a, $b ) {
return $a['order'] <=> $b['order'];
}
);

$theme_path = get_stylesheet_directory();

$filtered_patterns = array();

foreach ( $patterns_used as $pattern ) {
$pattern['slug'] = str_replace( 'quickwp/', '', $pattern['slug'] );

$pattern_path = $theme_path . '/patterns/' . $pattern['slug'] . '.php';

if ( ! file_exists( $pattern_path ) ) {
continue;
}

ob_start();
include $pattern_path;
$pattern_content = ob_get_clean();

$filtered_patterns[] = $pattern_content;
}

return array(
'slug' => $items['slug'],
'patterns' => implode( '', $filtered_patterns ),
);
}
}
1 change: 1 addition & 0 deletions inc/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function enqueue_assets() {
array(
'api' => $this->api->get_endpoint(),
'siteUrl' => get_site_url(),
'themeSlug' => get_template(),
'isGuidedMode' => defined( 'QUICKWP_APP_GUIDED_MODE' ) && QUICKWP_APP_GUIDED_MODE,
)
);
Expand Down
2 changes: 1 addition & 1 deletion quickwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
define( 'QUICKWP_APP_VERSION', '1.0.0' );

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

if ( ! defined( 'QUICKWP_APP_GUIDED_MODE' ) ) {
Expand Down
3 changes: 3 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useEffect } from '@wordpress/element';
import { useIsSiteEditorLoading } from './hooks';
import Loader from './components/Loader';
import Header from './parts/Header';
import { recordEvent } from './utils';

const App = () => {
const isEditorLoading = useIsSiteEditorLoading();
Expand Down Expand Up @@ -45,6 +46,8 @@ const App = () => {
if ( hasWelcome ) {
toggle( 'core/edit-site', 'welcomeGuide' );
}

recordEvent();
}, [ hasWelcome ]);

const StepControls = currentStep?.view || null;
Expand Down
Loading

0 comments on commit 9af2577

Please sign in to comment.