diff --git a/includes/RestApi/SiteGenController.php b/includes/RestApi/SiteGenController.php index 78f28e39d..4db54136f 100644 --- a/includes/RestApi/SiteGenController.php +++ b/includes/RestApi/SiteGenController.php @@ -88,6 +88,17 @@ public function register_routes() { 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), ) ); + + \register_rest_route( + $this->namespace, + $this->rest_base . '/pages/sitemap', + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'publish_sitemap_pages' ), + 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), + 'args' => $this->get_publish_sitemap_pages_args(), + ) + ); } /** @@ -113,7 +124,7 @@ public function sitegen_meta_args() { } /** - * Gets the arguments for the 'get-homepages' endpoint. + * Gets the arguments for the homepages endpoint. * * @return array The array of arguments. */ @@ -127,6 +138,21 @@ public function get_homepages_args() { ); } + /** + * Gets the arguments for the '/pages/sitemap' endpoint. + * + * @return array The array of arguments. + */ + public function get_publish_sitemap_pages_args() { + return array( + 'site_description' => array( + 'required' => true, + 'type' => 'string', + 'sanitize_callback' => 'sanitize_text_field', + ), + ); + } + /** * Gets the arguments for the 'get-homepages' endpoint. * @@ -264,24 +290,50 @@ public function regenerate_homepage( \WP_REST_Request $request ) { } /** - * Generate Sitegen Site Details meta data. + * Publish the pages in the sitemap. * - * @param \WP_REST_Request $request Request model. + * @param \WP_REST_Request $request The incoming request + * @return \WP_REST_Response|\WP_Error + */ + public function publish_sitemap_pages( \WP_REST_Request $request ) { + $site_description = $request->get_param( 'site_description' ); + $site_info = array( 'site_description' => $site_description ); + + $target_audience = SiteGenService::instantiate_site_meta( $site_info, 'target_audience' ); + if ( is_wp_error( $target_audience ) ) { + return $target_audience; + } + + $content_style = SiteGenService::instantiate_site_meta( $site_info, 'content_tones' ); + if ( is_wp_error( $content_style ) ) { + return $content_style; + } + + $sitemap = SiteGenService::instantiate_site_meta( $site_info, 'sitemap' ); + if ( is_wp_error( $sitemap ) ) { + return $sitemap; + } + + SiteGenService::publish_sitemap_pages( $site_description, $content_style, $target_audience, $sitemap ); + + return new \WP_REST_Response( array(), 201 ); + } + + /** + * Generate Sitegen Site Details meta data. * * @return array|WP_Error */ - public function get_site_details_meta( \WP_REST_Request $request ) { + public function get_site_details_meta() { return SiteGenData::get_site_details_questionnaire(); } /** * Get Sitegen Customize sidebar data. * - * @param \WP_REST_Request $request Request model. - * * @return array|WP_Error */ - public function get_customize_sidebar_data( \WP_REST_Request $request ) { + public function get_customize_sidebar_data() { return SiteGenService::get_customize_sidebar_data(); } } diff --git a/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js b/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js index 412a9ea2a..8b0b3f8c1 100644 --- a/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js +++ b/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js @@ -65,10 +65,11 @@ const SiteGen = () => { async function performSiteGenMetaGeneration( siteInfo, identifier, + skipCache, retryCount = 1 ) { return new Promise( () => - generateSiteGenMeta( siteInfo, identifier ) + generateSiteGenMeta( siteInfo, identifier, skipCache ) .then( ( data ) => { if ( data.body !== null ) { currentData.sitegen.siteGenMetaStatus.currentStatus += 1; @@ -77,6 +78,7 @@ const SiteGen = () => { performSiteGenMetaGeneration( siteInfo, identifier, + skipCache, retryCount + 1 ); } @@ -115,9 +117,11 @@ const SiteGen = () => { site_description: currentData.sitegen?.siteDetails?.prompt, }; + const skipCache = currentData.sitegen?.skipCache; + // Iterate over Identifiers and fire Requests! identifiers.forEach( ( identifier ) => { - performSiteGenMetaGeneration( siteInfo, identifier ); + performSiteGenMetaGeneration( siteInfo, identifier, skipCache ); } ); } diff --git a/src/OnboardingSPA/steps/SiteGen/Editor/index.js b/src/OnboardingSPA/steps/SiteGen/Editor/index.js index e1dc70210..9ddbba8bd 100644 --- a/src/OnboardingSPA/steps/SiteGen/Editor/index.js +++ b/src/OnboardingSPA/steps/SiteGen/Editor/index.js @@ -11,6 +11,7 @@ import { getGlobalStyles } from '../../../utils/api/themes'; // eslint-disable-next-line import/no-extraneous-dependencies import { cloneDeep } from 'lodash'; +import { publishSitemapPages } from '../../../utils/api/siteGen'; const StepSiteGenEditor = () => { const [ homepage, setHomepage ] = useState( false ); @@ -20,6 +21,7 @@ const StepSiteGenEditor = () => { setHeaderActiveView, setDrawerActiveView, setHideFooterNav, + setCurrentOnboardingData, } = useDispatch( nfdOnboardingStore ); const { currentData } = useSelect( ( select ) => { @@ -29,6 +31,18 @@ const StepSiteGenEditor = () => { }; } ); + const handleSitemapPagesGeneration = async () => { + if ( false === currentData?.sitegen?.sitemapPagesGenerated ) { + const sitemapPagesPublished = await publishSitemapPages( + currentData.sitegen.siteDetails.prompt + ); + if ( ! sitemapPagesPublished.error ) { + currentData.sitegen.sitemapPagesGenerated = true; + setCurrentOnboardingData( currentData ); + } + } + }; + const loadData = async () => { setHideFooterNav( true ); setIsHeaderEnabled( true ); @@ -45,6 +59,7 @@ const StepSiteGenEditor = () => { setHeaderActiveView( HEADER_SITEGEN ); setDrawerActiveView( false ); loadData(); + handleSitemapPagesGeneration(); }, [] ); useEffect( () => { diff --git a/src/OnboardingSPA/steps/SiteGen/Preview/index.js b/src/OnboardingSPA/steps/SiteGen/Preview/index.js index f2bc52d59..ad8aac174 100644 --- a/src/OnboardingSPA/steps/SiteGen/Preview/index.js +++ b/src/OnboardingSPA/steps/SiteGen/Preview/index.js @@ -62,8 +62,7 @@ const SiteGenPreview = () => { } const response = await getHomepages( - currentData.sitegen.siteDetails.prompt, - false + currentData.sitegen.siteDetails.prompt ); if ( response.error ) { @@ -96,6 +95,7 @@ const SiteGenPreview = () => { return false; } currentData.sitegen.homepages.active = homepages[ slug ]; + currentData.sitegen.skipCache = false; setCurrentOnboardingData( currentData ); navigate( nextStep.path ); }; diff --git a/src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js b/src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js index 660d9bb91..cbf730246 100644 --- a/src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js +++ b/src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js @@ -49,12 +49,13 @@ const SiteGenSiteDetails = () => { useEffect( () => { if ( - currentData.sitegen.siteDetails.prompt !== undefined && - customerInput !== undefined && + customerInput !== '' && customerInput !== currentData.sitegen.siteDetails.prompt ) { currentData.sitegen.siteDetails.prompt = customerInput?.trim(); currentData.sitegen.siteDetails.mode = 'simple'; + currentData.sitegen.skipCache = true; + currentData.sitegen.sitemapPagesGenerated = false; setCurrentOnboardingData( currentData ); } setIsFooterNavAllowed( isValidInput ); diff --git a/src/OnboardingSPA/utils/api/siteGen.js b/src/OnboardingSPA/utils/api/siteGen.js index 1477aeb1e..942d603c1 100644 --- a/src/OnboardingSPA/utils/api/siteGen.js +++ b/src/OnboardingSPA/utils/api/siteGen.js @@ -76,3 +76,15 @@ export async function getCustomizeSidebarData() { } ).then() ); } + +export async function publishSitemapPages( siteDescription ) { + return await resolve( + apiFetch( { + url: onboardingRestURL( 'sitegen/pages/sitemap' ), + method: 'POST', + data: { + site_description: siteDescription, + }, + } ).then() + ); +}