diff --git a/includes/RestApi/FlowController.php b/includes/RestApi/FlowController.php index 47cf206a3..085007127 100644 --- a/includes/RestApi/FlowController.php +++ b/includes/RestApi/FlowController.php @@ -137,6 +137,18 @@ public function save_onboarding_flow_data( \WP_REST_Request $request ) { public function complete( $request ) { $flow = $request->get_param( 'flow' ); if ( 'sitegen' === $flow ) { + + // Publish Site Pages before saving Child Theme. + $sitegen_site_pages_request = new \WP_REST_Request( + 'POST', + '/newfold-onboarding/v1/site-pages/sitegen-publish' + ); + $sitegen_site_pages_request = \rest_do_request( $sitegen_site_pages_request ); + if ( $sitegen_site_pages_request->is_error() ) { + return $sitegen_site_pages_request->as_error(); + } + + // Generate Child Theme $flow_data_option = \get_option( Options::get_option_name( 'flow' ), false ); if ( false === $flow_data_option || ! isset( $flow_data_option['data'] ) ) { return new \WP_Error( diff --git a/includes/RestApi/SitePagesController.php b/includes/RestApi/SitePagesController.php index 2d51db639..1236d28e8 100644 --- a/includes/RestApi/SitePagesController.php +++ b/includes/RestApi/SitePagesController.php @@ -5,6 +5,7 @@ use NewfoldLabs\WP\Module\Onboarding\Permissions; use NewfoldLabs\WP\Module\Onboarding\Data\Options; use NewfoldLabs\WP\Module\Onboarding\Data\Patterns; +use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService; use NewfoldLabs\WP\Module\Onboarding\Data\Services\WonderBlocksService; /** @@ -40,6 +41,16 @@ public function register_routes() { 'permission_callback' => array( Permissions::class, 'custom_post_authorized_admin' ), ) ); + + \register_rest_route( + $this->namespace, + $this->rest_base . '/sitegen-publish', + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'publish_sitegen_site_pages' ), + 'permission_callback' => array( Permissions::class, 'custom_post_authorized_admin' ), + ) + ); } /** @@ -151,6 +162,45 @@ private function set_site_pages( $site_pages_pattern_slugs ) { return true; } + + /** + * Publishes all the Site Pages for Sitegen Pages + * + * @return \WP_REST_Response|\WP_Error + */ + public function publish_sitegen_site_pages() { + // Make an AI Call, Iterate over it and publish all the pages that are returned. + $site_description = SiteGenService::get_prompt(); + $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; + } + + $site_pages = SiteGenService::generate_site_pages( $site_description, $content_style, $target_audience, $sitemap ); + + foreach ( $site_pages as $key => $site_page ) { + + $page_data = $this->publish_page( $key, $site_page['content'], false ); + if ( is_wp_error( $page_data ) ) { + return $page_data; + } + } + + return true; + } + /** * Set the Publish Page *