Skip to content

Commit

Permalink
Add Wonder Blocks Site Pages
Browse files Browse the repository at this point in the history
  • Loading branch information
arunshenoy99 committed Oct 16, 2023
1 parent 6c8cef5 commit 56b4b18
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 20 deletions.
46 changes: 26 additions & 20 deletions includes/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
),
);
}
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down
90 changes: 90 additions & 0 deletions includes/Services/WonderBlocksService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 56b4b18

Please sign in to comment.