Skip to content

Commit ebb6325

Browse files
authored
Merge pull request #30 from newfold-labs/add/custom-homepage-pattern
Use custom menu patterns for menu pages in the AI Sitegen flow
2 parents 395b5b7 + 3368f3b commit ebb6325

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

includes/Patterns.php

+28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ public static function get_hero_custom_content_structure() {
1515
return array( 'header', 'hero-custom', 'footer' );
1616
}
1717

18+
/**
19+
* Retrieve custom menu pattern slugs.
20+
*
21+
* @return array
22+
*/
23+
public static function get_custom_menu_slugs() {
24+
return array( 'menu-8', 'menu-1', 'menu-2', 'menu-7', 'menu-3' );
25+
}
26+
1827
/**
1928
* Check whether custom hero to be used or not.
2029
*
@@ -43,4 +52,23 @@ public static function get_custom_hero_pattern() {
4352
'dalleImages' => array(),
4453
);
4554
}
55+
56+
/**
57+
* Checks whether a custom menu pattern is needed for a given page and site classification.
58+
*
59+
* @param array $site_classification site classification as determined by AI
60+
* @param array $site_classification_mapping site classification mapping for which the home page pattern needs to be overridden
61+
* @param string $page The slug of the page being generated.
62+
* @return boolean
63+
*/
64+
public static function check_custom_menu_needed( $site_classification, $site_classification_mapping, $page ) {
65+
$primary_sitetype = $site_classification['primaryType'];
66+
$secondary_sitetype = $site_classification['slug'];
67+
68+
if ( 'menu' === $page && isset( $site_classification_mapping['menu-custom'][ $primary_sitetype ][ $secondary_sitetype ] ) ) {
69+
return $site_classification_mapping['menu-custom'][ $primary_sitetype ][ $secondary_sitetype ];
70+
}
71+
72+
return false;
73+
}
4674
}

includes/SiteGen/SiteGen.php

+40-2
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,20 @@ private static function get_prompt_from_info( $site_info ) {
116116
* Get the patterns for a particular category.
117117
*
118118
* @param string $category The category to get patterns for.
119+
* @param array $site_classification site classification as determined by AI.
119120
*/
120-
private static function get_patterns_for_category( $category ) {
121+
private static function get_patterns_for_category( $category, $site_classification = array() ) {
122+
$primary_sitetype = isset( $site_classification['primaryType'] ) ? $site_classification['primaryType'] : null;
123+
$secondary_sitetype = isset( $site_classification['slug'] ) ? $site_classification['slug'] : null;
124+
$args = array(
125+
'category' => $category,
126+
'primary_type' => $primary_sitetype,
127+
'secondary_type' => $secondary_sitetype,
128+
);
129+
$api = NFD_PATTERNS_BASE . 'patterns?' . http_build_query( $args );
130+
121131
$response = wp_remote_get(
122-
NFD_PATTERNS_BASE . 'patterns?category=' . $category,
132+
$api,
123133
array(
124134
'headers' => array(
125135
'Content-Type' => 'application/json',
@@ -527,6 +537,34 @@ public static function get_content_for_page(
527537
$keywords,
528538
$page
529539
) {
540+
$site_classification_mapping = self::get_sitegen_from_cache( 'siteclassificationmapping' );
541+
if ( ! $site_classification_mapping ) {
542+
$site_classification_mapping = self::generate_site_meta(
543+
array(
544+
'site_description' => $site_description,
545+
),
546+
'siteclassificationmapping'
547+
);
548+
}
549+
550+
$site_classification = self::get_sitegen_from_cache( 'siteclassification' );
551+
if ( Patterns::check_custom_menu_needed( $site_classification, $site_classification_mapping, $page ) ) {
552+
$menu_patterns = self::get_patterns_for_category( $page, $site_classification );
553+
if ( ! $menu_patterns['error'] ) {
554+
$menu_patterns_slugs = Patterns::get_custom_menu_slugs();
555+
$menu_patterns_filtered = array_filter(
556+
$menu_patterns,
557+
function ( $key ) use ( $menu_patterns_slugs ) {
558+
return in_array( $key, $menu_patterns_slugs, true );
559+
},
560+
ARRAY_FILTER_USE_KEY
561+
);
562+
$random_menu_pattern_slug = array_rand( $menu_patterns_filtered );
563+
564+
return $menu_patterns_filtered[ $random_menu_pattern_slug ]['content'];
565+
}
566+
}
567+
530568
$response = wp_remote_post(
531569
NFD_AI_BASE . 'generatePageContent',
532570
array(

0 commit comments

Comments
 (0)