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

Use Custom Blog Patterns for Content Websites' Homepages #33

Merged
merged 2 commits into from
Aug 28, 2024
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
55 changes: 24 additions & 31 deletions includes/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,33 @@
* Class Patterns
*/
final class Patterns {

/**
* Retrieve custom content structure.
* Get the custom content structure for a given site_classification_mapping_slug.
*
* @param string $site_classification_mapping_slug The site classification mapping site meta slug.
* @return array
*/
public static function get_hero_custom_content_structure() {
return array( 'header', 'hero-custom', 'footer' );
}
public static function get_custom_content_structure( $site_classification_mapping_slug ) {
$custom_content_structures = array(
'hero-custom' => array( 'header', 'hero-custom', 'footer' ),
'blog-custom' => array( 'header', 'blog-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' );
return isset( $custom_content_structures[ $site_classification_mapping_slug ] ) ? $custom_content_structures[ $site_classification_mapping_slug ] : array();
}

/**
* Check whether custom hero to be used or not.
* Get the custom patterns slugs for a given site_classification_mapping_slug.
*
* @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
* @return array|boolean
* @param string $site_classification_mapping_slug The site classification mapping site meta slug.
* @return array
*/
public static function check_hero_custom_content_structure_needed( $site_classification, $site_classification_mapping ) {
$primary_sitetype = $site_classification['primaryType'];
$secondary_sitetype = $site_classification['slug'];

if ( isset( $site_classification_mapping['hero-custom'][ $primary_sitetype ][ $secondary_sitetype ] ) ) {
return $site_classification_mapping['hero-custom'][ $primary_sitetype ][ $secondary_sitetype ];
}
return false;
public static function get_custom_patterns_slugs( $site_classification_mapping_slug ) {
$custom_patterns_list = array(
'menu-custom' => array( 'menu-8', 'menu-1', 'menu-2', 'menu-7', 'menu-3' ),
'blog-custom' => array( 'blog-10', 'blog-1', 'blog-4' ),
);
return isset( $custom_patterns_list[ $site_classification_mapping_slug ] ) ? $custom_patterns_list[ $site_classification_mapping_slug ] : array();
}

/**
Expand All @@ -54,19 +47,19 @@ public static function get_custom_hero_pattern() {
}

/**
* Checks whether a custom menu pattern is needed for a given page and site classification.
* Checks whether a given site classification site meta needs a custom content structure based on the site classification mapping site meta slug.
*
* @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.
* @param string $site_classification_mapping_slug The site classification mapping site meta slug.
* @param array $site_classification_mapping The site classification mapping site meta.
* @param array $site_classification The site classification site meta.
* @return boolean
*/
public static function check_custom_menu_needed( $site_classification, $site_classification_mapping, $page ) {
public static function needs_custom_content_structure( $site_classification_mapping_slug, $site_classification_mapping, $site_classification ) {
$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 ];
if ( isset( $site_classification_mapping[ $site_classification_mapping_slug ][ $primary_sitetype ][ $secondary_sitetype ] ) ) {
return $site_classification_mapping[ $site_classification_mapping_slug ][ $primary_sitetype ][ $secondary_sitetype ];
}

return false;
Expand Down
43 changes: 39 additions & 4 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,50 @@ public static function get_home_pages( $site_description, $content_style, $targe
}
// check if custom hero patterns needs to be added
$site_classification = self::get_sitegen_from_cache( 'siteclassification' );
if ( Patterns::check_hero_custom_content_structure_needed( $site_classification, $site_classification_mapping ) ) {
if ( Patterns::needs_custom_content_structure( 'hero-custom', $site_classification_mapping, $site_classification ) ) {
// update content structures and generated patterns
$custom_structure = Patterns::get_hero_custom_content_structure();
$custom_structure = Patterns::get_custom_content_structure( 'hero-custom' );
foreach ( $generated_content_structures as $home_slug => $structure ) {
$generated_content_structures[ $home_slug ] = $custom_structure;
}
$generated_patterns['hero-custom'] = array_pad( array(), 3, Patterns::get_custom_hero_pattern() );
}

// Check if a custom blog homepage structure is needed.
if ( Patterns::needs_custom_content_structure( 'blog-custom', $site_classification_mapping, $site_classification ) ) {
// Fetch the blog patterns.
$blog_patterns = self::get_patterns_for_category( 'blog', $site_classification );
if ( ! $blog_patterns['error'] ) {
// Filter out unnecessary blog patterns.
$blog_patterns_slugs = Patterns::get_custom_patterns_slugs( 'blog-custom' );
$blog_patterns_filtered = array_filter(
$blog_patterns,
function ( $key ) use ( $blog_patterns_slugs ) {
return in_array( $key, $blog_patterns_slugs, true );
},
ARRAY_FILTER_USE_KEY
);

// Set the content structure of the generated homepages to match the custom blog content structure.
$custom_structure = Patterns::get_custom_content_structure( 'blog-custom' );
foreach ( $generated_content_structures as $home_slug => $structure ) {
$generated_content_structures[ $home_slug ] = $custom_structure;
}

// Populate the blog-custom content structure patterns in the generated patterns list.
$generated_patterns['blog-custom'] = array();

foreach ( $blog_patterns_filtered as $slug => $pattern ) {
array_push(
$generated_patterns['blog-custom'],
array(
'replacedPattern' => $pattern['content'],
)
);
}
}
}

$random_homepages = array_rand( $generated_content_structures, 3 );
$generated_homepages = array();

Expand Down Expand Up @@ -548,10 +583,10 @@ public static function get_content_for_page(
}

$site_classification = self::get_sitegen_from_cache( 'siteclassification' );
if ( Patterns::check_custom_menu_needed( $site_classification, $site_classification_mapping, $page ) ) {
if ( 'menu' === $page && Patterns::needs_custom_content_structure( 'menu-custom', $site_classification_mapping, $site_classification ) ) {
$menu_patterns = self::get_patterns_for_category( $page, $site_classification );
if ( ! $menu_patterns['error'] ) {
$menu_patterns_slugs = Patterns::get_custom_menu_slugs();
$menu_patterns_slugs = Patterns::get_custom_patterns_slugs( 'menu-custom' );
$menu_patterns_filtered = array_filter(
$menu_patterns,
function ( $key ) use ( $menu_patterns_slugs ) {
Expand Down
Loading