Skip to content

Commit

Permalink
Merge pull request #24 from newfold-labs/ensure-dalle-used
Browse files Browse the repository at this point in the history
ensure that dalle image pattern is always used, always add hero to th…
  • Loading branch information
amartya-dev authored Apr 8, 2024
2 parents b5c0716 + 821e262 commit ffc1c06
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ class SiteGen {
* Function to check capabilities
*/
private static function check_capabilities() {
$capability = new SiteCapabilities();

$capability = new SiteCapabilities();
$sitegen_enabled = $capability->get( 'hasAISiteGen' );

return $sitegen_enabled;
}

Expand Down Expand Up @@ -426,8 +424,15 @@ public static function get_home_pages( $site_description, $content_style, $targe
}
$parsed_response = json_decode( wp_remote_retrieve_body( $response ), true );
$generated_content_structures = $parsed_response['contentStructures'];
$generated_patterns = $parsed_response['generatedPatterns'];
$generated_homepages = $parsed_response['pages'];
// Ensure all content structures should have hero
foreach ( $generated_content_structures as $home_slug => $structure ) {
if ( ! in_array( 'hero', $structure, true ) ) {
array_splice( $structure, 1, 0, 'hero' );
$generated_content_structures[ $home_slug ] = $structure;
}
}
$generated_patterns = $parsed_response['generatedPatterns'];
$generated_homepages = $parsed_response['pages'];
self::cache_sitegen_response( 'contentStructures', $generated_content_structures );
self::cache_sitegen_response( 'generatedPatterns', $generated_patterns );
self::cache_sitegen_response( 'homepages', $generated_homepages );
Expand All @@ -437,6 +442,7 @@ public static function get_home_pages( $site_description, $content_style, $targe
$generated_homepages = array();
$generated_patterns = self::get_sitegen_from_cache( 'generatedPatterns' );

$dalle_used = false;
$categories_to_separate = array( 'header', 'footer' );
// Choose random categories for the generated patterns and return
foreach ( $random_homepages as $homepage_index => $slug ) {
Expand All @@ -452,6 +458,16 @@ public static function get_home_pages( $site_description, $content_style, $targe
$pattern_index = ( $regenerate ) ? array_rand( $generated_patterns[ $pattern_category ] ) : $homepage_index;
$random_pattern = $generated_patterns[ $pattern_category ][ $pattern_index ];

// Check if this is a hero pattern and we are at end of homepages without ever using dalle
if ( ! $dalle_used && count( $random_homepages ) === $homepage_index && 'hero' === $pattern_category ) {
// Chose the dalle hero only
foreach ( $generated_patterns[ $pattern_category ] as $gen_hero ) {
if ( ! empty( $gen_hero['dalleImages'] ) ) {
$random_pattern = $gen_hero;
}
}
}

if ( in_array( $pattern_category, $categories_to_separate, true ) ) {
$homepage_patterns[ $pattern_category ] = $random_pattern['replacedPattern'];
} else {
Expand All @@ -460,6 +476,7 @@ public static function get_home_pages( $site_description, $content_style, $targe

if ( ! empty( $random_pattern['dalleImages'] ) ) {
$homepage_patterns['generatedImages'] = $random_pattern['dalleImages'];
$dalle_used = true;
}
}
$generated_homepages[ $slug ] = $homepage_patterns;
Expand Down

0 comments on commit ffc1c06

Please sign in to comment.