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 9, 2024
1 parent 619dcc7 commit d9726a7
Show file tree
Hide file tree
Showing 14 changed files with 751 additions and 115 deletions.
264 changes: 264 additions & 0 deletions inc/class-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
<?php
/**
* API class.
*
* @package Codeinwp/QuickWP
*/

namespace ThemeIsle\QuickWP;

/**
* API class.
*/
class API {
/**
* API namespace.
*
* @var string
*/
private $namespace = 'quickwp';

/**
* API version.
*
* @var string
*/
private $version = 'v1';

/**
* Constructor.
*/
public function __construct() {
$this->register_route();
}

/**
* Get endpoint.
*
* @return string
*/
public function get_endpoint() {
return $this->namespace . '/' . $this->version;
}

/**
* Register hooks and actions.
*
* @return void
*/
private function register_route() {
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}

/**
* Register REST API route
*
* @return void
*/
public function register_routes() {
$namespace = $this->namespace . '/' . $this->version;

$routes = array(
'send' => array(
'methods' => \WP_REST_Server::CREATABLE,
'args' => array(
'step' => array(
'required' => true,
'type' => 'string',
),
'message' => array(
'required' => false,
'type' => 'string',
),
),
'callback' => array( $this, 'send' ),
),
'status' => array(
'methods' => \WP_REST_Server::READABLE,
'args' => array(
'thread_id' => array(
'required' => true,
'type' => 'string',
),
'run_id' => array(
'required' => true,
'type' => 'string',
),
),
'callback' => array( $this, 'status' ),
),
'get' => array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get' ),
),
'images' => array(
'methods' => \WP_REST_Server::READABLE,
'args' => array(
'query' => array(
'required' => true,
'type' => 'string',
),
),
'callback' => array( $this, 'images' ),
),
);

foreach ( $routes as $route => $args ) {
$args['permission_callback'] = function () {
return current_user_can( 'manage_options' );
};

register_rest_route( $namespace, '/' . $route, $args );
}
}

/**
* Send data to the API.
*
* @param \WP_REST_Request $request Request.
*
* @return \WP_REST_Response
*/
public function send( \WP_REST_Request $request ) {
$data = $request->get_params();

$request = wp_remote_post(
QUICKWP_API . 'wizard/send',
array(
'body' => array(
'step' => $data['step'],
'message' => $data['message'],
),
)
);

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->id ) || ! $response->id ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
}

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

/**
* Get status.
*
* @param \WP_REST_Request $request Request.
*
* @return \WP_REST_Response
*/
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',
array(
'body' => array(
'thread_id' => $data['thread_id'],
'run_id' => $data['run_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->id ) || ! $response->id ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
}

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

/**
* Get data.
*
* @param \WP_REST_Request $request Request.
*
* @return \WP_REST_Response
*/
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',
array(
'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 );
}

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

/**
* Get images.
*
* @param \WP_REST_Request $request Request.
*
* @return \WP_REST_Response
*/
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',
array(
'body' => array(
'query' => $data['query'],
),
)
);

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->photos ) || count( $response->photos ) === 0 ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
}

return new \WP_REST_Response( $response, 200 );
}
}
18 changes: 18 additions & 0 deletions inc/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@

namespace ThemeIsle\QuickWP;

use ThemeIsle\QuickWP\API;

/**
* Main class.
*/
class Main {
/**
* API instance.
*
* @var API
*/
private $api;

/**
* Constructor.
*/
public function __construct() {
$this->register_hooks();

$this->api = new API();
}

/**
Expand Down Expand Up @@ -62,5 +72,13 @@ public function enqueue_assets() {
);

wp_set_script_translations( 'quickwp', 'quickwp' );

wp_localize_script(
'quickwp',
'quickwp',
array(
'api' => $this->api->get_endpoint(),
)
);
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ parameters:
- %currentWorkingDirectory%/inc
bootstrapFiles:
- %currentWorkingDirectory%/tests/php/static-analysis-stubs/quickwp.php
checkGenericClassInNonGenericObjectType: false
includes:
- %currentWorkingDirectory%/vendor/szepeviktor/phpstan-wordpress/extension.neon
1 change: 1 addition & 0 deletions quickwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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/' );

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

Expand Down
34 changes: 31 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ import Header from './parts/Header';
const App = () => {
const isEditorLoading = useIsSiteEditorLoading();

const currentStep = useSelect( ( select ) =>
select( 'quickwp/data' ).getStep()
);
const {
currentStep,
hasError
} = useSelect( select => {
const {
getStep,
hasError
} = select( 'quickwp/data' );

return {
currentStep: getStep(),
hasError: hasError()
};
});

const StepControls = currentStep?.view || null;

if ( isEditorLoading ) {
Expand All @@ -31,6 +43,22 @@ const App = () => {
);
}

if ( hasError ) {
return (
<div
id="quickwp"
className="flex flex-col items-center justify-center fixed py-12 px-14 z-50 bg-bg overflow-auto inset-0"
>
<h2 className="text-fg text-xl not-italic font-medium leading-10">
{ __(
'There has been an error. Please refresh the page and try again.',
'quickwp'
) }
</h2>
</div>
);
}

return (
<div
id="quickwp"
Expand Down
5 changes: 2 additions & 3 deletions src/components/PageControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { __ } from '@wordpress/i18n';

import { Button } from '@wordpress/components';

import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies.
Expand All @@ -19,7 +19,6 @@ const PageControl = () => {
const currentStep = useSelect( ( select ) =>
select( 'quickwp/data' ).getStep()
);
const { setStep } = useDispatch( 'quickwp/data' );

const currentPage = STEPS.findIndex(
( step ) => step.value === currentStep.value
Expand All @@ -43,7 +42,7 @@ const PageControl = () => {
}
aria-label={ STEPS[ page ]?.label }
className="text-fg h-6 w-6 !min-w-0 !min-h-0"
onClick={ () => setStep( STEPS[ page ]?.value ) }
onClick={ () => {} }
/>
</li>
) ) }
Expand Down
Loading

0 comments on commit d9726a7

Please sign in to comment.