Skip to content

Commit

Permalink
[WiP] Save current progress
Browse files Browse the repository at this point in the history
  • Loading branch information
HardeepAsrani committed Jan 22, 2024
1 parent 266ac0b commit ad39726
Show file tree
Hide file tree
Showing 13 changed files with 623 additions and 161 deletions.
184 changes: 172 additions & 12 deletions inc/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function register_routes() {
$namespace = $this->namespace . '/' . $this->version;

$routes = array(
'send' => array(
'send' => array(
'methods' => \WP_REST_Server::CREATABLE,
'args' => array(
'step' => array(
Expand All @@ -73,7 +73,7 @@ public function register_routes() {
),
'callback' => array( $this, 'send' ),
),
'status' => array(
'status' => array(
'methods' => \WP_REST_Server::READABLE,
'args' => array(
'thread_id' => array(
Expand All @@ -87,11 +87,17 @@ public function register_routes() {
),
'callback' => array( $this, 'status' ),
),
'get' => array(
'get' => array(
'methods' => \WP_REST_Server::READABLE,
'args' => array(
'thread_id' => array(
'required' => true,
'type' => 'string',
),
),
'callback' => array( $this, 'get' ),
),
'images' => array(
'images' => array(
'methods' => \WP_REST_Server::READABLE,
'args' => array(
'query' => array(
Expand All @@ -101,6 +107,16 @@ public function register_routes() {
),
'callback' => array( $this, 'images' ),
),
'templates' => array(
'methods' => \WP_REST_Server::READABLE,
'args' => array(
'thread_id' => array(
'required' => true,
'type' => 'string',
),
),
'callback' => array( $this, 'templates' ),
),
);

foreach ( $routes as $route => $args ) {
Expand All @@ -123,9 +139,10 @@ public function send( \WP_REST_Request $request ) {
$data = $request->get_params();

$request = wp_remote_post(
QUICKWP_API . 'wizard/send',
QUICKWP_APP_API . 'wizard/send',
array(
'body' => array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'step' => $data['step'],
'message' => $data['message'],
),
Expand Down Expand Up @@ -161,9 +178,10 @@ public function status( \WP_REST_Request $request ) {
$data = $request->get_params();

$request = wp_remote_get( // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
QUICKWP_API . 'wizard/status',
QUICKWP_APP_API . 'wizard/status',
array(
'body' => array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'thread_id' => $data['thread_id'],
'run_id' => $data['run_id'],
),
Expand Down Expand Up @@ -199,9 +217,10 @@ public function get( \WP_REST_Request $request ) {
$data = $request->get_params();

$request = wp_remote_get(// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
QUICKWP_API . 'wizard/get',
QUICKWP_APP_API . 'wizard/get',
array(
'body' => array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'thread_id' => $data['thread_id'],
),
)
Expand All @@ -225,6 +244,107 @@ public function get( \WP_REST_Request $request ) {
return new \WP_REST_Response( $response, 200 );
}

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

$request = wp_remote_get(// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
QUICKWP_APP_API . 'wizard/get',
array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'thread_id' => $data['thread_id'],
),
)
);

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', 'quickwp' ) ), 500 );
}

$patterns_used = array();

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 ) {
add_filter(
'quickwp/' . $string['slug'],
function () use( $string ) {
return $string['value'];
}
);
}
}

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 new \WP_REST_Response(
array(
'status' => 'success',
'data' => implode( '', $filtered_patterns ),
),
200
);
}

/**
* Get images.
*
Expand All @@ -236,9 +356,10 @@ public function images( \WP_REST_Request $request ) {
$data = $request->get_params();

$request = wp_remote_get(// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
QUICKWP_API . 'wizard/images',
QUICKWP_APP_API . 'wizard/images',
array(
'body' => array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'query' => $data['query'],
),
)
Expand All @@ -259,6 +380,45 @@ public function images( \WP_REST_Request $request ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
}



return new \WP_REST_Response( $response, 200 );
}

/**
* Get JSON from response.
*
* @param array $data Response.
*
* @return array|bool
*/
private static function process_json_from_response( $data ) {
// Find the target item.
$target = current( $data );

if ( false === $target ) {
// Handle the case where the target is not found.
return false;
}

// Extract the JSON string.
$json_string = $target->content[0]->text->value;

try {
$json_object = json_decode( $json_string, true );
return $json_object;
} catch ( \Exception $e) {
// If parsing failed, try to find a JSON array in the string.
preg_match( '/\[(.|\n)*\]/', $json_string, $matches );

if ( ! empty( $matches ) ) {
$json_array_string = $matches[0];
$json_object = json_decode( $json_array_string, true );

return $json_object;
}
}

return false;
}
}
6 changes: 3 additions & 3 deletions inc/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ public function enqueue_assets() {
return;
}

$asset_file = include QUICKWP_PATH . '/build/index.asset.php';
$asset_file = include QUICKWP_APP_PATH . '/build/index.asset.php';

wp_enqueue_style(
'quickwp',
QUICKWP_URL . 'build/style-index.css',
QUICKWP_APP_URL . 'build/style-index.css',
array( 'wp-components' ),
$asset_file['version']
);

wp_enqueue_script(
'quickwp',
QUICKWP_URL . 'build/index.js',
QUICKWP_APP_URL . 'build/index.js',
$asset_file['dependencies'],
$asset_file['version'],
true
Expand Down
12 changes: 6 additions & 6 deletions quickwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
die;
}

define( 'QUICKWP_BASEFILE', __FILE__ );
define( 'QUICKWP_URL', plugins_url( '/', __FILE__ ) );
define( 'QUICKWP_PATH', __DIR__ );
define( 'QUICKWP_VERSION', '1.0.0' );
define( 'QUICKWP_API', 'https://4ab6-103-217-244-109.ngrok-free.app/api/' );
define( 'QUICKWP_APP_BASEFILE', __FILE__ );
define( 'QUICKWP_APP_URL', plugins_url( '/', __FILE__ ) );
define( 'QUICKWP_APP_PATH', __DIR__ );
define( 'QUICKWP_APP_VERSION', '1.0.0' );
define( 'QUICKWP_APP_API', 'https://aaf0-103-217-244-105.ngrok-free.app/api/' );

$vendor_file = QUICKWP_PATH . '/vendor/autoload.php';
$vendor_file = QUICKWP_APP_PATH . '/vendor/autoload.php';

if ( is_readable( $vendor_file ) ) {
require_once $vendor_file;
Expand Down
17 changes: 15 additions & 2 deletions src/components/TemplatePreview.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
/**
* External dependencies.
*/
import classNames from 'classnames';

/**
* WordPress dependencies.
*/
import { BlockPreview } from '@wordpress/block-editor';

const TemplatePreview = ({ template }) => {
const TemplatePreview = ({
template,
canScroll = false
}) => {
return (
<div className="block basis-full rounded-xl overflow-hidden max-h-80vh">
<div className={ classNames(
'block basis-full rounded-xl overflow-hidden max-h-80vh',
{
'overflow-y-auto': canScroll
}
) }>
<BlockPreview
blocks={ template }
viewportWidth={ 1400 }
Expand Down
Loading

0 comments on commit ad39726

Please sign in to comment.