diff --git a/composer.json b/composer.json index be07ecd2f..1bb90cc70 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ "newfold-labs/wp-module-onboarding-data": "^0.1.0", "newfold-labs/wp-module-patterns": "^0.1.12", "newfold-labs/wp-module-install-checker": "^1.0", - "newfold-labs/wp-module-facebook": "1.0.0" + "newfold-labs/wp-module-facebook": "^1.0.2" }, "require-dev": { "wp-phpunit/wp-phpunit": "^6.2", diff --git a/composer.lock b/composer.lock index 216d4ccad..821c6877f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e9414ea34a5260c76cff581c26a6856d", + "content-hash": "ecc1023e4ef1e6b2bd33c3597d78c06e", "packages": [ { "name": "mustache/mustache", @@ -195,16 +195,16 @@ }, { "name": "newfold-labs/wp-module-facebook", - "version": "v1.0.0", + "version": "v1.0.2", "source": { "type": "git", "url": "git@github.com:newfold-labs/wp-module-facebook.git", - "reference": "9b0caa8311bd13b72cb8c03075e7832302109cf4" + "reference": "21ad4a3db764633307b680b3bb693d6ed8390a3b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/newfold-labs/wp-module-facebook/zipball/9b0caa8311bd13b72cb8c03075e7832302109cf4", - "reference": "9b0caa8311bd13b72cb8c03075e7832302109cf4", + "url": "https://api.github.com/repos/newfold-labs/wp-module-facebook/zipball/21ad4a3db764633307b680b3bb693d6ed8390a3b", + "reference": "21ad4a3db764633307b680b3bb693d6ed8390a3b", "shasum": "" }, "require": { @@ -230,10 +230,10 @@ } ], "support": { - "source": "https://github.com/newfold-labs/wp-module-facebook/tree/v1.0.0", + "source": "https://github.com/newfold-labs/wp-module-facebook/tree/v1.0.2", "issues": "https://github.com/newfold-labs/wp-module-facebook/issues" }, - "time": "2024-01-24T10:00:53+00:00" + "time": "2024-01-29T09:15:30+00:00" }, { "name": "newfold-labs/wp-module-install-checker", 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 02d588571..e6be25ff6 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 185cff91a..3f1915f3a 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() + ); +}