-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from newfold-labs/Integrate-APIs-to-AI-Onboar…
…ding Integrate APIs to AI Onboarding
- Loading branch information
Showing
8 changed files
with
275 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace NewfoldLabs\WP\Module\Onboarding\RestApi; | ||
|
||
use NewfoldLabs\WP\Module\Onboarding\Permissions; | ||
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService; | ||
|
||
/** | ||
* Class SiteGenController | ||
*/ | ||
class SiteGenController { | ||
|
||
/** | ||
* The namespace of this controller's route. | ||
* | ||
* @var string | ||
*/ | ||
protected $namespace = 'newfold-onboarding/v1'; | ||
|
||
/** | ||
* The endpoint base | ||
* | ||
* @var string | ||
*/ | ||
protected $rest_base = '/sitegen'; | ||
|
||
/** | ||
* Registers rest routes for SiteGenController class. | ||
* | ||
* @return void | ||
*/ | ||
public function register_routes() { | ||
\register_rest_route( | ||
$this->namespace, | ||
$this->rest_base . '/get-identifiers', | ||
array( | ||
'methods' => \WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'get_valid_identifiers' ), | ||
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), | ||
) | ||
); | ||
\register_rest_route( | ||
$this->namespace, | ||
$this->rest_base . '/generate', | ||
array( | ||
'methods' => \WP_REST_Server::CREATABLE, | ||
'callback' => array( $this, 'generate_sitegen_meta' ), | ||
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), | ||
'args' => $this->sitegen_meta_args(), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Required Args for Generating Site Gen Meta. | ||
* | ||
* @return array | ||
*/ | ||
public function sitegen_meta_args() { | ||
return array( | ||
'site_info' => array( | ||
'required' => true, | ||
'type' => 'object', | ||
), | ||
'identifier' => array( | ||
'required' => true, | ||
'type' => 'string', | ||
), | ||
'skip_cache' => array( | ||
'required' => false, | ||
'type' => 'boolean', | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Gets all the valid Identifiers | ||
* | ||
* @return array | ||
*/ | ||
public function get_valid_identifiers() { | ||
return array_keys( array_filter( SiteGenService::get_identifiers() ) ); | ||
} | ||
|
||
/** | ||
* Generate Sitegen meta data. | ||
* | ||
* @param \WP_REST_Request $request Request model. | ||
* | ||
* @return array|WP_Error | ||
*/ | ||
public function generate_sitegen_meta( \WP_REST_Request $request ) { | ||
|
||
$site_info = $request->get_param( 'site_info' ); | ||
$identifier = $request->get_param( 'identifier' ); | ||
$skip_cache = $request->get_param( 'skip_cache' ); | ||
|
||
if ( SiteGenService::is_enabled() ) { | ||
// TODO Implement the main function and do computations if required. | ||
return SiteGenService::instantiate_site_meta( $site_info, $identifier, $skip_cache ); | ||
} | ||
|
||
return new \WP_Error( | ||
'sitegen-error', | ||
'SiteGen is Disabled.', | ||
array( 'status' => 404 ) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
import { resolve } from './resolve.js'; | ||
import { onboardingRestURL } from './common'; | ||
|
||
export async function getSiteGenIdentifiers() { | ||
return await resolve( | ||
apiFetch( { | ||
url: onboardingRestURL( 'sitegen/get-identifiers' ), | ||
} ).then() | ||
); | ||
} | ||
|
||
export async function generateSiteGenMeta( | ||
siteInfo, | ||
identifier, | ||
skipCache = false | ||
) { | ||
return await resolve( | ||
apiFetch( { | ||
url: onboardingRestURL( 'sitegen/generate' ), | ||
method: 'POST', | ||
data: { | ||
site_info: siteInfo, | ||
identifier, | ||
skip_cache: skipCache, | ||
}, | ||
} ).then() | ||
); | ||
} |