-
Notifications
You must be signed in to change notification settings - Fork 8
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
refactored hard coded logic from homepage menu #236
Changes from 12 commits
e883c81
c15b355
052ce80
0272da7
d34374c
6224344
e0478ab
b0ea5e9
fe4bc8f
9d4eb5c
84aebc6
5422c01
be9df07
9f7d329
7606765
83f8052
e37cc29
5df54a7
7342a9d
2ff1f17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,19 @@ protected static function get_theme_step_patterns() { | |
); | ||
} | ||
|
||
/** | ||
* Callback Functions for Theme Step. | ||
* | ||
* @return array | ||
*/ | ||
protected static function get_theme_step_patterns_callback() { | ||
return array( | ||
'yith-wonder' => array( | ||
'homepage-styles' => array( __CLASS__, 'get_patterns_for_homepage_menu_slugs' ), | ||
arunshenoy99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
); | ||
} | ||
|
||
/** | ||
* Get post metadata for a pattern. Ref: SitePagesController.php | ||
* | ||
|
@@ -191,7 +204,7 @@ public static function get_meta_from_pattern_slug( $pattern_slug ) { | |
/** | ||
* Retrieve pattern from slug. | ||
* | ||
* @param array $pattern_slug Pattern Slug Data | ||
* @param string $pattern_slug Pattern Slug Data | ||
* | ||
* @return array|boolean | ||
*/ | ||
|
@@ -213,16 +226,20 @@ public static function get_pattern_from_slug( $pattern_slug ) { | |
/** | ||
* Replace the header menu slug in the patterns array | ||
* | ||
* @param array $patterns Patterns for the specific step | ||
* @param string $header_menu_slug header menu slug choosen by the user | ||
* @param array $patterns Patterns for the specific step | ||
* | ||
* @return array | ||
*/ | ||
private static function replace_header_menu_slug( $patterns, $header_menu_slug ) { | ||
foreach ( $patterns as $slug => $slug_details ) { | ||
if ( isset( $slug_details['replace'] ) && true === $slug_details['replace'] ) { | ||
unset( $patterns[ $slug ] ); | ||
$patterns = array_merge( array( $header_menu_slug => $slug_details ), $patterns ); | ||
private static function replace_header_menu_slug( $patterns ) { | ||
// fetch the selected header menu slug from DB | ||
$flow_data = \get_option( Options::get_option_name( 'flow' ) ); | ||
$header_menu_slug = explode( '/', $flow_data['data']['partHeader'] )[1]; | ||
arunshenoy99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( ! empty( $header_menu_slug ) ) { | ||
foreach ( $patterns as $slug => $slug_details ) { | ||
if ( isset( $slug_details['replace'] ) && true === $slug_details['replace'] ) { | ||
arunshenoy99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unset( $patterns[ $slug ] ); | ||
$patterns = array_merge( array( $header_menu_slug => $slug_details ), $patterns ); | ||
} | ||
} | ||
} | ||
return $patterns; | ||
|
@@ -267,22 +284,21 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false | |
} | ||
|
||
$pattern_slugs = self::get_theme_step_patterns()[ $active_theme ][ $step ]; | ||
$pattern_slugs_callback = self::get_theme_step_patterns_callback()[ $active_theme ][ $step ]; | ||
arunshenoy99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$block_patterns_registry = \WP_Block_Patterns_Registry::get_instance(); | ||
$block_patterns = array(); | ||
$block_patterns_squashed = ''; | ||
|
||
// fetch the selected header menu slug from DB | ||
$flow_data = \get_option( Options::get_option_name( 'flow' ) ); | ||
$header_menu_slug = explode( '/', $flow_data['data']['partHeader'] )[1]; | ||
if ( ! empty( $header_menu_slug ) ) { | ||
$pattern_slugs = self::replace_header_menu_slug( $pattern_slugs, $header_menu_slug ); | ||
} | ||
$pattern_slugs = self::replace_header_menu_slug( $pattern_slugs ); | ||
|
||
foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) { | ||
if ( true === $pattern_slugs[ $pattern_slug ]['active'] ) { | ||
$pattern_name = $active_theme . '/' . $pattern_slug; | ||
if ( $block_patterns_registry->is_registered( $pattern_name ) ) { | ||
$pattern = $block_patterns_registry->get_registered( $pattern_name ); | ||
if ( array_key_exists( 'content', $pattern_slugs[ $pattern_slug ] ) && ! empty( $pattern_slugs[ $pattern_slug ]['content'] ) ) { | ||
$pattern['content'] = $pattern_slugs[ $pattern_slug ]['content']; | ||
} | ||
arunshenoy99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// if header menu slug contains "split" replace the menu links with dummy links | ||
if ( false !== stripos( $pattern_slug, 'split' ) ) { | ||
$pattern['content'] = self::replace_split_menu_items( $pattern['content'] ); | ||
|
@@ -291,10 +307,11 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false | |
if ( ! $squash ) { | ||
$block_patterns[] = array_merge( | ||
array( | ||
'slug' => $pattern_name, | ||
'title' => $pattern['title'], | ||
'content' => self::cleanup_wp_grammar( $pattern['content'] ), | ||
'name' => $pattern['name'], | ||
'slug' => $pattern_name, | ||
'title' => $pattern['title'], | ||
'content' => self::cleanup_wp_grammar( $pattern['content'] ), | ||
'name' => $pattern['name'], | ||
'categories' => $pattern['categories'], | ||
), | ||
$pattern_slugs[ $pattern_slug ] | ||
); | ||
|
@@ -305,9 +322,54 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false | |
} | ||
} | ||
|
||
$pattern_slug_callback = isset( $pattern_slugs_callback ) ? $pattern_slugs_callback : false; | ||
arunshenoy99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( is_callable( $pattern_slug_callback ) ) { | ||
return $pattern_slug_callback( $block_patterns ); | ||
} | ||
arunshenoy99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return $squash ? $block_patterns_squashed : $block_patterns; | ||
} | ||
|
||
/** | ||
* Retrieve Homepage Menu Step Patterns | ||
* | ||
* @param array $pattern_slugs Step Patterns Data | ||
* @return array | ||
*/ | ||
private static function get_patterns_for_homepage_menu_slugs( $pattern_slugs ) { | ||
arunshenoy99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$header_content = ''; | ||
$footer_content = ''; | ||
$homepage_style_slugs = array_filter( $pattern_slugs, array( __CLASS__, 'filter_pattern_data' ) ); | ||
|
||
$header_footer_slugs = array_diff_assoc( $pattern_slugs, $homepage_style_slugs ); | ||
foreach ( $header_footer_slugs as $key => $slug ) { | ||
if ( in_array( 'yith-wonder-site-header', $slug['categories'] ) ) { | ||
$header_content = $slug['content']; | ||
continue; | ||
} | ||
if ( in_array( 'yith-wonder-site-footer', $slug['categories'] ) ) { | ||
$footer_content = $slug['content']; | ||
continue; | ||
} | ||
} | ||
|
||
foreach ( $homepage_style_slugs as $key => $homepage_style ) { | ||
$homepage_style_slugs[ $key ]['content'] = $header_content . $homepage_style['content'] . $footer_content; | ||
} | ||
|
||
return $homepage_style_slugs; | ||
} | ||
|
||
/** | ||
* Filter out Homepage Menu Slug Patterns Data | ||
* | ||
* @param array $pattern_slugs Slug Data | ||
* @return boolean | ||
*/ | ||
public static function filter_pattern_data( $pattern_slugs ) { | ||
return in_array( 'yith-wonder-pages', $pattern_slugs['categories'] ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we combine the |
||
|
||
/** | ||
* Retrieve Pattern Count. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.