Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sitegen homepages api #9

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions includes/RestApi/AISearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
Empty file added includes/SiteGen/Parser.php
Empty file.
17 changes: 11 additions & 6 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 );
Expand All @@ -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(
Expand Down Expand Up @@ -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' );
Expand All @@ -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 );
}
}
Expand Down
Loading