Skip to content

Commit

Permalink
Merge pull request #30 from newfold-labs/add/custom-homepage-pattern
Browse files Browse the repository at this point in the history
Use custom menu patterns for menu pages in the AI Sitegen flow
  • Loading branch information
abhijitb authored Aug 7, 2024
2 parents 395b5b7 + 3368f3b commit ebb6325
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
28 changes: 28 additions & 0 deletions includes/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public static function get_hero_custom_content_structure() {
return array( 'header', 'hero-custom', 'footer' );
}

/**
* Retrieve custom menu pattern slugs.
*
* @return array
*/
public static function get_custom_menu_slugs() {
return array( 'menu-8', 'menu-1', 'menu-2', 'menu-7', 'menu-3' );
}

/**
* Check whether custom hero to be used or not.
*
Expand Down Expand Up @@ -43,4 +52,23 @@ public static function get_custom_hero_pattern() {
'dalleImages' => array(),
);
}

/**
* Checks whether a custom menu pattern is needed for a given page and site classification.
*
* @param array $site_classification site classification as determined by AI
* @param array $site_classification_mapping site classification mapping for which the home page pattern needs to be overridden
* @param string $page The slug of the page being generated.
* @return boolean
*/
public static function check_custom_menu_needed( $site_classification, $site_classification_mapping, $page ) {
$primary_sitetype = $site_classification['primaryType'];
$secondary_sitetype = $site_classification['slug'];

if ( 'menu' === $page && isset( $site_classification_mapping['menu-custom'][ $primary_sitetype ][ $secondary_sitetype ] ) ) {
return $site_classification_mapping['menu-custom'][ $primary_sitetype ][ $secondary_sitetype ];
}

return false;
}
}
42 changes: 40 additions & 2 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,20 @@ private static function get_prompt_from_info( $site_info ) {
* Get the patterns for a particular category.
*
* @param string $category The category to get patterns for.
* @param array $site_classification site classification as determined by AI.
*/
private static function get_patterns_for_category( $category ) {
private static function get_patterns_for_category( $category, $site_classification = array() ) {
$primary_sitetype = isset( $site_classification['primaryType'] ) ? $site_classification['primaryType'] : null;
$secondary_sitetype = isset( $site_classification['slug'] ) ? $site_classification['slug'] : null;
$args = array(
'category' => $category,
'primary_type' => $primary_sitetype,
'secondary_type' => $secondary_sitetype,
);
$api = NFD_PATTERNS_BASE . 'patterns?' . http_build_query( $args );

$response = wp_remote_get(
NFD_PATTERNS_BASE . 'patterns?category=' . $category,
$api,
array(
'headers' => array(
'Content-Type' => 'application/json',
Expand Down Expand Up @@ -527,6 +537,34 @@ public static function get_content_for_page(
$keywords,
$page
) {
$site_classification_mapping = self::get_sitegen_from_cache( 'siteclassificationmapping' );
if ( ! $site_classification_mapping ) {
$site_classification_mapping = self::generate_site_meta(
array(
'site_description' => $site_description,
),
'siteclassificationmapping'
);
}

$site_classification = self::get_sitegen_from_cache( 'siteclassification' );
if ( Patterns::check_custom_menu_needed( $site_classification, $site_classification_mapping, $page ) ) {
$menu_patterns = self::get_patterns_for_category( $page, $site_classification );
if ( ! $menu_patterns['error'] ) {
$menu_patterns_slugs = Patterns::get_custom_menu_slugs();
$menu_patterns_filtered = array_filter(
$menu_patterns,
function ( $key ) use ( $menu_patterns_slugs ) {
return in_array( $key, $menu_patterns_slugs, true );
},
ARRAY_FILTER_USE_KEY
);
$random_menu_pattern_slug = array_rand( $menu_patterns_filtered );

return $menu_patterns_filtered[ $random_menu_pattern_slug ]['content'];
}
}

$response = wp_remote_post(
NFD_AI_BASE . 'generatePageContent',
array(
Expand Down

0 comments on commit ebb6325

Please sign in to comment.