Skip to content

Commit

Permalink
Add homepages to Wonder Blocks templates
Browse files Browse the repository at this point in the history
  • Loading branch information
arunshenoy99 committed Jan 15, 2024
1 parent 048b3c4 commit 189d868
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"require": {
"newfold-labs/wp-module-installer": "^1.1",
"newfold-labs/wp-module-data": "^2.4.3",
"newfold-labs/wp-module-patterns": "^0.1.12",
"newfold-labs/wp-module-ai": "^1.0.4",
"wp-forge/wp-upgrade-handler": "^1.0",
"mustache/mustache": "^2.14"
Expand Down
52 changes: 51 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions includes/Services/FlowService.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,14 @@ public static function get_experience_level() {
$data = self::read_data_from_wp_option( false );
return isset( $data['data']['wpComfortLevel'] ) ? $data['data']['wpComfortLevel'] : false;
}

/**
* Fetches the homepages generated in the Sitegen flow.
*
* @return false|array
*/
public static function get_sitegen_homepages() {
$data = self::read_data_from_wp_option( false );
return ! empty( $data['sitegen']['homepages']['data'] ) ? $data['sitegen']['homepages']['data'] : false;
}
}
83 changes: 79 additions & 4 deletions includes/Services/SiteGenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use NewfoldLabs\WP\Module\Onboarding\Data\Themes;
use NewfoldLabs\WP\Module\Onboarding\Data\Themes\Colors;
use NewfoldLabs\WP\Module\Onboarding\Data\Themes\Fonts;
use NewfoldLabs\WP\Module\Patterns\SiteClassification as PatternsSiteClassification;

/**
* Class SiteGenService
Expand Down Expand Up @@ -252,7 +253,6 @@ public static function generate_child_theme( $data ) {
}

return true;

}

/**
Expand Down Expand Up @@ -332,7 +332,7 @@ public static function get_customize_sidebar_data() {
),
'color_palette'
);
$font_pair = self::instantiate_site_meta(
$font_pair = self::instantiate_site_meta(
array(
'site_description' => $prompt,
),
Expand All @@ -350,10 +350,85 @@ public static function get_customize_sidebar_data() {
$default_design = Fonts::get_sitegen_default_design_data();

return array(
'design' => $default_design,
'design' => $default_design,
'colorPalettes' => $color_palette,
'designStyles' => $font_pair,
'designStyles' => $font_pair,
);
}

/**
* Filters Wonder Blocks transients before they are set.
*
* @return void
*/
public static function pre_set_filter_wonder_blocks_transients() {
$args = wp_parse_args(
array(
'primary_type' => PatternsSiteClassification::get_primary_type(),
'secondary_type' => PatternsSiteClassification::get_secondary_type(),
)
);
$id = md5( serialize( $args ) );

\add_action( "pre_set_transient_wba_templates_{$id}", array( __CLASS__, 'filter_wonder_blocks_templates_transient' ), 10, 1 );
\add_action( 'pre_set_transient_wba_templates_categories', array( __CLASS__, 'filter_wonder_blocks_categories_transient' ), 10, 1 );
}

/**
* Filters the Wonder Blocks templates transient.
*
* @param array $value The original value of the transient.
* @return array
*/
public static function filter_wonder_blocks_templates_transient( $value ) {
if ( empty( $value ) || ! is_array( $value ) ) {
return $value;
}

$homepages = FlowService::get_sitegen_homepages();
if ( ! $homepages ) {
return $value;
}

foreach ( $homepages as $slug => $data ) {
array_push(
$value,
array(
'id' => $slug,
'slug' => $slug,
'description' => $slug,
'content' => $data['content'],
'categories' => array( 'home', 'featured' ),
)
);
}

return $value;
}

/**
* Filters the Wonder Blocks categories transient.
*
* @param array $value The original value of the transient.
* @return array
*/
public static function filter_wonder_blocks_categories_transient( $value ) {
if ( empty( $value ) || ! is_array( $value ) ) {
return $value;
}

$homepages = FlowService::get_sitegen_homepages();
if ( ! $homepages ) {
return $value;
}

foreach ( $value as $index => $category ) {
if ( 'home' === $category['title'] ) {
$category['count'] = $category['count'] + count( $homepages );
$value[ $index ] = $category;
}
}

return $value;
}
}

0 comments on commit 189d868

Please sign in to comment.