diff --git a/includes/RestApi/AISearchController.php b/includes/RestApi/AISearchController.php index 4120b99..05d4b03 100644 --- a/includes/RestApi/AISearchController.php +++ b/includes/RestApi/AISearchController.php @@ -145,6 +145,10 @@ public function homepages( \WP_REST_Request $request ) { $response = SiteGen::get_home_pages( $site_description, $content_style, $target_audience ); + if ( array_key_exists( 'error', $response ) ) { + return new \WP_Error( 'bad_request', $response['error'], 400 ); + } + return new \WP_REST_Response( $response, 200 ); } diff --git a/includes/SiteGen/Parser.php b/includes/SiteGen/Parser.php new file mode 100644 index 0000000..e69de29 diff --git a/includes/SiteGen/SiteGen.php b/includes/SiteGen/SiteGen.php index 37798ae..7295db4 100644 --- a/includes/SiteGen/SiteGen.php +++ b/includes/SiteGen/SiteGen.php @@ -153,14 +153,14 @@ private static function get_patterns_for_category( $category ) { * @param string $site_description The site description (the user prompt) * @param array $content_style The generated content style. * @param array $target_audience The generated target audience. - * @param array $content_structure The content structures generated / cached + * @param array $content_structures The content structures generated / cached * @param boolean $skip_cache If we need to skip cache. */ private static function generate_pattern_content( $site_description, $content_style, $target_audience, - $content_structure, + $content_structures, $skip_cache = false ) { if ( ! $skip_cache ) { @@ -179,7 +179,7 @@ private static function generate_pattern_content( ); $unique_categories = array(); - foreach ( $content_structure as $homepage => $structure ) { + foreach ( $content_structures as $homepage => $structure ) { foreach ( $structure as $category ) { if ( ! in_array( $category, $unique_categories, true ) ) { array_push( $unique_categories, $category ); @@ -200,8 +200,7 @@ private static function generate_pattern_content( $category_pattern_map[ $category ] = array(); foreach ( $random_selected_patterns as $pattern_slug ) { - $pattern = $patterns_for_category[ $pattern_slug ]; - // Generate content for these patterns + $pattern = $patterns_for_category[ $pattern_slug ]; $response = wp_remote_post( NFD_AI_BASE . 'generateSiteMeta', array( @@ -325,6 +324,12 @@ public static function get_home_pages( $site_description, $content_style, $targe array( 'site_description' => $site_description ), 'contentstructure' ); + + // If we got an error, return that right away + if ( array_key_exists( 'error', $generated_content_structures ) ) { + return $generated_content_structures; + } + // Check if we have the response in cache already if ( ! $regenerate ) { $generated_homepages = self::get_sitegen_from_cache( 'homepages' ); @@ -350,7 +355,7 @@ public static function get_home_pages( $site_description, $content_style, $targe } // Get a random pattern for the category. $random_pattern = array_rand( $generated_patterns[ $pattern_category ] ); - $random_pattern = $generated_patterns[ $random_pattern ]; + $random_pattern = $generated_patterns[ $pattern_category ][ $random_pattern ]; array_push( $generated_homepages[ $slug ], $random_pattern ); } }