From 56b4b18e687dc108e70a8843d078c2b05896ec19 Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Mon, 16 Oct 2023 13:57:13 +0530 Subject: [PATCH] Add Wonder Blocks Site Pages --- includes/Patterns.php | 46 +++++++----- includes/Services/WonderBlocksService.php | 90 +++++++++++++++++++++++ 2 files changed, 116 insertions(+), 20 deletions(-) diff --git a/includes/Patterns.php b/includes/Patterns.php index b31d701..f386f25 100644 --- a/includes/Patterns.php +++ b/includes/Patterns.php @@ -73,25 +73,28 @@ protected static function get_theme_step_patterns() { ), 'site-pages' => array( 'company-page' => array( - 'active' => true, - 'title' => __( 'About', 'wp-module-onboarding' ), - 'selected' => true, - 'shown' => true, - 'description' => __( 'Explain your company values or the history behind your brand.', 'wp-module-onboarding' ), + 'active' => true, + 'title' => __( 'About', 'wp-module-onboarding' ), + 'selected' => true, + 'shown' => true, + 'description' => __( 'Explain your company values or the history behind your brand.', 'wp-module-onboarding' ), + 'wonder_blocks' => 'about-4', ), 'contact-us' => array( - 'active' => true, - 'selected' => true, - 'title' => __( 'Contact', 'wp-module-onboarding' ), - 'shown' => true, - 'description' => __( 'Offer visitors a single page with a contact form, your street address and social media.', 'wp-module-onboarding' ), + 'active' => true, + 'selected' => true, + 'title' => __( 'Contact', 'wp-module-onboarding' ), + 'shown' => true, + 'description' => __( 'Offer visitors a single page with a contact form, your street address and social media.', 'wp-module-onboarding' ), + 'wonder_blocks' => 'contact-4', ), 'testimonials-page' => array( - 'active' => true, - 'title' => __( 'Testimonials', 'wp-module-onboarding' ), - 'selected' => false, - 'shown' => true, - 'description' => __( 'Highlight your success with testimonials from your fans.', 'wp-module-onboarding' ), + 'active' => true, + 'title' => __( 'Testimonials', 'wp-module-onboarding' ), + 'selected' => false, + 'shown' => true, + 'description' => __( 'Highlight your success with testimonials from your fans.', 'wp-module-onboarding' ), + 'wonder_blocks' => 'testimonials-1', ), 'blog-page' => array( 'active' => true, @@ -138,9 +141,12 @@ protected static function get_theme_step_patterns() { public static function get_fallbacks() { return array( 'wonder-blocks' => array( - 'home-1' => 'yith-wonder/homepage-1', - 'home-2' => 'yith-wonder/homepage-2', - 'home-3' => 'yith-wonder/homepage-3', + 'home-1' => 'yith-wonder/homepage-1', + 'home-2' => 'yith-wonder/homepage-2', + 'home-3' => 'yith-wonder/homepage-3', + 'about-4' => 'yith-wonder/company-page', + 'contact-4' => 'yith-wonder/contact-us', + 'testimonials-1' => 'yith-wonder/testimonials-page', ), ); } @@ -283,7 +289,7 @@ public static function get_pattern_from_block_patterns_registry( $pattern_slug ) */ public static function get_pattern_from_slug( $pattern_slug ) { if ( WonderBlocksService::is_valid_slug( $pattern_slug ) ) { - $pattern = WonderBlocksService::get_template_from_slug( $pattern_slug ); + $pattern = WonderBlocksService::get_data_from_slug( $pattern_slug ); if ( ! $pattern ) { $fallback_pattern_slug = self::get_fallback_from_slug( $pattern_slug ); if ( ! $fallback_pattern_slug ) { @@ -357,7 +363,7 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false return false; } - $pattern_slugs = self::get_theme_step_patterns()[ $active_theme ][ $step ]; + $pattern_slugs = self::get_theme_step_patterns()[ $active_theme ][ $step ]; $block_patterns = $squash ? '' : array(); foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) { diff --git a/includes/Services/WonderBlocksService.php b/includes/Services/WonderBlocksService.php index c78f591..78b8031 100644 --- a/includes/Services/WonderBlocksService.php +++ b/includes/Services/WonderBlocksService.php @@ -16,6 +16,38 @@ */ class WonderBlocksService { + /** + * Determines whether a slug is a pattern. + * + * @param string $slug The slug to evaluate. + * @return boolean + */ + public static function is_pattern( $slug ) { + $patterns = array( + 'testimonials-1' => true, + ); + + return isset( $patterns[ $slug ] ); + } + + /** + * Determines whether a slug is a template. + * + * @param string $slug The slug to evaluate. + * @return boolean + */ + public static function is_template( $slug ) { + $templates = array( + 'home-1' => true, + 'home-2' => true, + 'home-3' => true, + 'about-4' => true, + 'contact-4' => true, + ); + + return isset( $templates[ $slug ] ); + } + /** * Get the slug for a given pattern name. * @@ -68,6 +100,21 @@ public static function is_enabled() { && true === Data::current_brand()['config']['wonder_blocks']; } + /** + * Get wonder blocks data from a given template/pattern slug. + * + * @param string $slug The wonder blocks slug. + * @return array|false + */ + public static function get_data_from_slug( $slug ) { + $wonder_blocks_slug = self::strip_prefix_from_slug( $slug ); + if ( self::is_pattern( $wonder_blocks_slug ) ) { + return self::get_pattern_from_slug( $slug ); + } + + return self::get_template_from_slug( $slug ); + } + /** * Fetches the template from WonderBlocks given the template slug. * @@ -111,6 +158,49 @@ public static function get_template_from_slug( $template_slug ) { return false; } + /** + * Fetches the pattern from WonderBlocks given the pattern slug. + * + * @param string $pattern_slug The pattern slug. + * @return array|false + */ + public static function get_pattern_from_slug( $pattern_slug ) { + $primary_type = PrimaryType::instantiate_from_option(); + if ( ! $primary_type ) { + return false; + } + $secondary_type = SecondaryType::instantiate_from_option(); + if ( ! $secondary_type ) { + return false; + } + + $wonder_blocks_slug = self::strip_prefix_from_slug( $pattern_slug ); + $request = new WonderBlocksFetchRequest( + array( + 'endpoint' => 'patterns', + 'slug' => $wonder_blocks_slug, + 'primary_type' => $primary_type->value, + 'secondary_type' => $secondary_type->value, + ) + ); + $patterns = WonderBlocks::fetch( $request ); + + if ( ! empty( $patterns ) ) { + $patterns['categories'] = array( $patterns['categories'], 'yith-wonder-pages' ); + $patterns['name'] = $patterns['slug']; + return array( + 'slug' => $pattern_slug, + 'title' => $patterns['title'], + 'content' => $patterns['content'], + 'name' => $patterns['name'], + 'meta' => Patterns::get_meta_from_slug( $pattern_slug ), + 'categories' => $patterns['categories'], + ); + } + + return false; + } + /** * Clear the cache for a template slug fetched via get_template_from_slug. *