From e883c8130d6fe14afb44e9f4a9646a14a92c99d2 Mon Sep 17 00:00:00 2001 From: Yashita Date: Tue, 9 May 2023 18:50:25 +0530 Subject: [PATCH 01/15] refactored hard coded logic from homepage menu --- includes/Data/Patterns.php | 81 +++++++++++++------ .../pages/Steps/DesignHomepageMenu/index.js | 60 +++----------- 2 files changed, 69 insertions(+), 72 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index 59789e946..4ce61f6b0 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -44,27 +44,7 @@ protected static function get_theme_step_patterns() { 'active' => true, ), ), - 'homepage-styles' => array( - 'site-header-left-logo-navigation-inline' => array( - 'active' => true, - 'replace' => true, - ), - 'homepage-1' => array( - 'active' => true, - 'shown' => true, - ), - 'homepage-2' => array( - 'active' => true, - 'shown' => true, - ), - 'homepage-3' => array( - 'active' => true, - 'shown' => true, - ), - 'site-footer' => array( - 'active' => true, - ), - ), + 'homepage-styles' => self::get_patterns_for_homepage_menu_slugs(), 'site-pages' => array( 'company-page' => array( 'active' => true, @@ -191,11 +171,13 @@ 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 */ public static function get_pattern_from_slug( $pattern_slug ) { + $active_theme = ( \wp_get_theme() )->get( 'TextDomain' ); + $pattern_slug = $active_theme . '/' . $pattern_slug; $block_patterns_registry = \WP_Block_Patterns_Registry::get_instance(); if ( $block_patterns_registry->is_registered( $pattern_slug ) ) { $pattern = $block_patterns_registry->get_registered( $pattern_slug ); @@ -281,8 +263,11 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) { if ( true === $pattern_slugs[ $pattern_slug ]['active'] ) { $pattern_name = $active_theme . '/' . $pattern_slug; + $pattern = $block_patterns_registry->get_registered( $pattern_name ); 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']; + } // 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'] ); @@ -304,10 +289,58 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false } } } - return $squash ? $block_patterns_squashed : $block_patterns; } + /** + * Retrieve Homepage Menu Step Patterns + * + * @return array + */ + public static function get_patterns_for_homepage_menu_slugs() { + // 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]; + $header_slug = ! empty( $header_menu_slug ) ? $header_menu_slug : 'site-header-left-logo-navigation-inline'; + + $homepage_slugs = array( + $header_slug, + 'site-footer', + ); + + $homepage_style_slugs = array( + 'homepage-1' => array( + 'active' => true, + 'shown' => true, + 'content' => '', + ), + 'homepage-2' => array( + 'active' => true, + 'shown' => true, + 'content' => '', + ), + 'homepage-3' => array( + 'active' => true, + 'shown' => true, + 'content' => '', + ), + ); + + $header_block_grammar = self::get_pattern_from_slug( $homepage_slugs[0] )['content']; + // if header menu slug contains "split" replace the menu links with dummy links + if ( false !== stripos( $homepage_slugs[0], 'split' ) ) { + $header_block_grammar = self::replace_split_menu_items( $header_block_grammar ); + } + + $footer_block_grammar = self::get_pattern_from_slug( $homepage_slugs[1] )['content']; + + foreach ( array_keys( $homepage_style_slugs ) as $homepage_style ) { + $homepage_style_slugs[ $homepage_style ]['content'] .= $header_block_grammar . self::get_pattern_from_slug( $homepage_style )['content'] . $footer_block_grammar; + } + + return $homepage_style_slugs; + } + /** * Retrieve Pattern Count. * diff --git a/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js b/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js index 847aee2a8..6bd6ae9aa 100644 --- a/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js +++ b/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js @@ -20,32 +20,9 @@ import { } from '../../../components/LivePreview'; const StepDesignHomepageMenu = () => { - const homepagePatternList = [ - 'yith-wonder/homepage-1', - 'yith-wonder/homepage-2', - 'yith-wonder/homepage-3', - ]; - - const homepagesList = { - 'homepage-1': [ - 'yith-wonder/site-header-left-logo-navigation-inline', - 'yith-wonder/homepage-1', - 'yith-wonder/site-footer', - ], - 'homepage-2': [ - 'yith-wonder/site-header-left-logo-navigation-inline', - 'yith-wonder/homepage-2', - 'yith-wonder/site-footer', - ], - 'homepage-3': [ - 'yith-wonder/site-header-left-logo-navigation-inline', - 'yith-wonder/homepage-3', - 'yith-wonder/site-footer', - ], - }; - const location = useLocation(); const [ homepagePattern, setHomepagePattern ] = useState(); + const [ homepagePatternList, setHomepagePatternList ] = useState( [] ); const [ selectedHomepage, setSelectedHomepage ] = useState( 0 ); const { @@ -83,40 +60,27 @@ const StepDesignHomepageMenu = () => { function refactorPatterns( homepagePatternDataResp ) { const makeHomepagePattern = []; - for ( const key in homepagesList ) { - const homepagePatterns = homepagesList[ key ]; - // update the header menu pattern if already selected - if ( - currentData.data.partHeader || - currentData.data.partHeader !== '' - ) { - homepagePatterns[ 0 ] = currentData.data.partHeader; - } - - let patternData = ''; - homepagePatterns.forEach( ( patternName ) => { - homepagePatternDataResp?.body.forEach( - ( homepagePatternData ) => { - if ( homepagePatternData.slug === patternName ) { - patternData += homepagePatternData.content; - } - } - ); - } ); - makeHomepagePattern.push( patternData ); - } + homepagePatternDataResp.forEach( ( homepagePatternData ) => { + makeHomepagePattern.push( homepagePatternData.content ); + } ); return makeHomepagePattern; } async function getHomepagePatternsData() { const homepagePatternDataTemp = await getPatterns( - currentStep.patternId, + currentStep.patternId ); + if ( homepagePatternDataTemp?.error ) { return updateThemeStatus( THEME_STATUS_INIT ); } - setHomepagePattern( refactorPatterns( homepagePatternDataTemp ) ); + setHomepagePattern( refactorPatterns( homepagePatternDataTemp?.body ) ); + + homepagePatternDataTemp?.body.forEach( ( homepagePatternData ) => { + homepagePatternList.push( homepagePatternData.slug ); + } ); + setHomepagePatternList( homepagePatternList ); if ( currentData?.data.sitePages.homepage !== '' ) { setSelectedHomepage( From c15b355292c5316b3548dc711ac01193c8a5b32a Mon Sep 17 00:00:00 2001 From: Yashita Date: Tue, 9 May 2023 22:03:54 +0530 Subject: [PATCH 02/15] refactored header and footer slugs --- includes/Data/Patterns.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index 4ce61f6b0..211cdeda7 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -302,11 +302,7 @@ public static function get_patterns_for_homepage_menu_slugs() { $flow_data = \get_option( Options::get_option_name( 'flow' ) ); $header_menu_slug = explode( '/', $flow_data['data']['partHeader'] )[1]; $header_slug = ! empty( $header_menu_slug ) ? $header_menu_slug : 'site-header-left-logo-navigation-inline'; - - $homepage_slugs = array( - $header_slug, - 'site-footer', - ); + $footer_slug = 'site-footer'; $homepage_style_slugs = array( 'homepage-1' => array( @@ -326,13 +322,13 @@ public static function get_patterns_for_homepage_menu_slugs() { ), ); - $header_block_grammar = self::get_pattern_from_slug( $homepage_slugs[0] )['content']; + $header_block_grammar = self::get_pattern_from_slug( $header_slug )['content']; // if header menu slug contains "split" replace the menu links with dummy links - if ( false !== stripos( $homepage_slugs[0], 'split' ) ) { + if ( false !== stripos( $header_slug, 'split' ) ) { $header_block_grammar = self::replace_split_menu_items( $header_block_grammar ); } - $footer_block_grammar = self::get_pattern_from_slug( $homepage_slugs[1] )['content']; + $footer_block_grammar = self::get_pattern_from_slug( $footer_slug )['content']; foreach ( array_keys( $homepage_style_slugs ) as $homepage_style ) { $homepage_style_slugs[ $homepage_style ]['content'] .= $header_block_grammar . self::get_pattern_from_slug( $homepage_style )['content'] . $footer_block_grammar; From 0272da7e71eb01eeb5c99e0f1b98b1f222e0941c Mon Sep 17 00:00:00 2001 From: Yashita Date: Thu, 11 May 2023 16:03:59 +0530 Subject: [PATCH 03/15] Update Patterns.php --- includes/Data/Patterns.php | 83 ++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index 211cdeda7..8640e8284 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -44,7 +44,9 @@ protected static function get_theme_step_patterns() { 'active' => true, ), ), - 'homepage-styles' => self::get_patterns_for_homepage_menu_slugs(), + 'homepage-styles' => array( + 'callable' => array( __CLASS__, 'get_patterns_for_homepage_menu_slugs' ), + ), 'site-pages' => array( 'company-page' => array( 'active' => true, @@ -200,11 +202,16 @@ public static function get_pattern_from_slug( $pattern_slug ) { * * @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]; + if ( ! empty( $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 ); + } } } return $patterns; @@ -253,18 +260,18 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false $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 ); + + $pattern_slugs_callback = isset( $pattern_slugs['callable'] ) ? $pattern_slugs['callable'] : false; + if ( is_callable( $pattern_slugs_callback ) ) { + $pattern_slugs = $pattern_slugs_callback(); } foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) { if ( true === $pattern_slugs[ $pattern_slug ]['active'] ) { $pattern_name = $active_theme . '/' . $pattern_slug; - $pattern = $block_patterns_registry->get_registered( $pattern_name ); 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']; } @@ -297,41 +304,49 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false * * @return array */ - public static function get_patterns_for_homepage_menu_slugs() { - // 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]; - $header_slug = ! empty( $header_menu_slug ) ? $header_menu_slug : 'site-header-left-logo-navigation-inline'; - $footer_slug = 'site-footer'; + private static function get_patterns_for_homepage_menu_slugs() { + $header_footer_slugs = array( + 'site-header-left-logo-navigation-inline' => array( + 'active' => true, + 'replace' => true, + ), + 'site-footer' => array( + 'active' => true, + ), + ); $homepage_style_slugs = array( 'homepage-1' => array( - 'active' => true, - 'shown' => true, - 'content' => '', + 'active' => true, + 'shown' => true, ), 'homepage-2' => array( - 'active' => true, - 'shown' => true, - 'content' => '', + 'active' => true, + 'shown' => true, ), 'homepage-3' => array( - 'active' => true, - 'shown' => true, - 'content' => '', + 'active' => true, + 'shown' => true, ), ); - $header_block_grammar = self::get_pattern_from_slug( $header_slug )['content']; - // if header menu slug contains "split" replace the menu links with dummy links - if ( false !== stripos( $header_slug, 'split' ) ) { - $header_block_grammar = self::replace_split_menu_items( $header_block_grammar ); - } + $header_footer_slugs = self::replace_header_menu_slug( $header_footer_slugs ); + + $header_footer_slug_keys = array( + 'header' => 'site-header-left-logo-navigation-inline', + 'footer' => 'site-footer', + ); - $footer_block_grammar = self::get_pattern_from_slug( $footer_slug )['content']; + foreach ( $header_footer_slug_keys as $slug_tag => $slug ) { + $header_footer_slugs[ $slug ]['content'] = self::get_pattern_from_slug( $slug )['content']; + // if header menu slug contains "split" replace the menu links with dummy links + if ( false !== stripos( $slug, 'split' ) ) { + $header_footer_slugs[ $slug ]['content'] = self::replace_split_menu_items( $header_footer_slugs[ $slug ]['content'] ); + } + } foreach ( array_keys( $homepage_style_slugs ) as $homepage_style ) { - $homepage_style_slugs[ $homepage_style ]['content'] .= $header_block_grammar . self::get_pattern_from_slug( $homepage_style )['content'] . $footer_block_grammar; + $homepage_style_slugs[ $homepage_style ]['content'] .= $header_footer_slugs[ $header_footer_slug_keys['header'] ]['content'] . self::get_pattern_from_slug( $homepage_style )['content'] . $header_footer_slugs[ $header_footer_slug_keys['footer'] ]['content']; } return $homepage_style_slugs; From d34374c9bc78d2f0683ce216ea8d0834161d91d2 Mon Sep 17 00:00:00 2001 From: Yashita Date: Thu, 11 May 2023 16:14:02 +0530 Subject: [PATCH 04/15] Update Patterns.php --- includes/Data/Patterns.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index 8640e8284..fce281457 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -45,7 +45,7 @@ protected static function get_theme_step_patterns() { ), ), 'homepage-styles' => array( - 'callable' => array( __CLASS__, 'get_patterns_for_homepage_menu_slugs' ), + 'pattern_slug_callback' => array( __CLASS__, 'get_patterns_for_homepage_menu_slugs' ), ), 'site-pages' => array( 'company-page' => array( @@ -198,7 +198,6 @@ 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 * * @return array */ @@ -262,9 +261,9 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false $pattern_slugs = self::replace_header_menu_slug( $pattern_slugs ); - $pattern_slugs_callback = isset( $pattern_slugs['callable'] ) ? $pattern_slugs['callable'] : false; - if ( is_callable( $pattern_slugs_callback ) ) { - $pattern_slugs = $pattern_slugs_callback(); + $pattern_slug_callback = isset( $pattern_slugs['pattern_slug_callback'] ) ? $pattern_slugs['pattern_slug_callback'] : false; + if ( is_callable( $pattern_slug_callback ) ) { + $pattern_slugs = $pattern_slug_callback(); } foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) { From 622434479f183c693113859ef4c09bd30579a226 Mon Sep 17 00:00:00 2001 From: Yashita Date: Thu, 11 May 2023 16:15:40 +0530 Subject: [PATCH 05/15] lint fix --- includes/Data/Patterns.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index fce281457..cdb724d97 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -197,7 +197,7 @@ 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 array $patterns Patterns for the specific step * * @return array */ From b0ea5e94af319a2615f17bc7c9b80b0a2abdf856 Mon Sep 17 00:00:00 2001 From: Yashita Date: Tue, 16 May 2023 23:14:34 +0530 Subject: [PATCH 06/15] added callback function for patterns --- includes/Data/Patterns.php | 123 +++++++++++------- .../pages/Steps/DesignHomepageMenu/index.js | 19 +-- 2 files changed, 84 insertions(+), 58 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index cdb724d97..ed00241f1 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -45,7 +45,25 @@ protected static function get_theme_step_patterns() { ), ), 'homepage-styles' => array( - 'pattern_slug_callback' => array( __CLASS__, 'get_patterns_for_homepage_menu_slugs' ), + 'site-header-left-logo-navigation-inline' => array( + 'active' => true, + 'replace' => true, + ), + 'homepage-1' => array( + 'active' => true, + 'shown' => true, + ), + 'homepage-2' => array( + 'active' => true, + 'shown' => true, + ), + 'homepage-3' => array( + 'active' => true, + 'shown' => true, + ), + 'site-footer' => array( + 'active' => true, + ), ), 'site-pages' => array( 'company-page' => array( @@ -105,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' ), + ), + ); + } + /** * Get post metadata for a pattern. Ref: SitePagesController.php * @@ -254,18 +285,18 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false return false; } + if ( ! isset( self::get_theme_step_patterns_callback()[ $active_theme ][ $step ] ) ) { + return false; + } + $pattern_slugs = self::get_theme_step_patterns()[ $active_theme ][ $step ]; + $pattern_slugs_callback = self::get_theme_step_patterns_callback()[ $active_theme ][ $step ]; $block_patterns_registry = \WP_Block_Patterns_Registry::get_instance(); $block_patterns = array(); $block_patterns_squashed = ''; $pattern_slugs = self::replace_header_menu_slug( $pattern_slugs ); - $pattern_slug_callback = isset( $pattern_slugs['pattern_slug_callback'] ) ? $pattern_slugs['pattern_slug_callback'] : false; - if ( is_callable( $pattern_slug_callback ) ) { - $pattern_slugs = $pattern_slug_callback(); - } - foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) { if ( true === $pattern_slugs[ $pattern_slug ]['active'] ) { $pattern_name = $active_theme . '/' . $pattern_slug; @@ -282,10 +313,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 ] ); @@ -295,62 +327,53 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false } } } + + $pattern_slug_callback = isset( $pattern_slugs_callback ) ? $pattern_slugs_callback : false; + if ( is_callable( $pattern_slug_callback ) ) { + return $pattern_slug_callback( $block_patterns ); + } + 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() { - $header_footer_slugs = array( - 'site-header-left-logo-navigation-inline' => array( - 'active' => true, - 'replace' => true, - ), - 'site-footer' => array( - 'active' => true, - ), - ); - - $homepage_style_slugs = array( - 'homepage-1' => array( - 'active' => true, - 'shown' => true, - ), - 'homepage-2' => array( - 'active' => true, - 'shown' => true, - ), - 'homepage-3' => array( - 'active' => true, - 'shown' => true, - ), - ); - - $header_footer_slugs = self::replace_header_menu_slug( $header_footer_slugs ); - - $header_footer_slug_keys = array( - 'header' => 'site-header-left-logo-navigation-inline', - 'footer' => 'site-footer', - ); - - foreach ( $header_footer_slug_keys as $slug_tag => $slug ) { - $header_footer_slugs[ $slug ]['content'] = self::get_pattern_from_slug( $slug )['content']; - // if header menu slug contains "split" replace the menu links with dummy links - if ( false !== stripos( $slug, 'split' ) ) { - $header_footer_slugs[ $slug ]['content'] = self::replace_split_menu_items( $header_footer_slugs[ $slug ]['content'] ); + private static function get_patterns_for_homepage_menu_slugs( $pattern_slugs ) { + $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']; + } + if ( in_array( 'yith-wonder-site-footer', $slug['categories'] ) ) { + $footer_content = $slug['content']; } } - foreach ( array_keys( $homepage_style_slugs ) as $homepage_style ) { - $homepage_style_slugs[ $homepage_style ]['content'] .= $header_footer_slugs[ $header_footer_slug_keys['header'] ]['content'] . self::get_pattern_from_slug( $homepage_style )['content'] . $header_footer_slugs[ $header_footer_slug_keys['footer'] ]['content']; + 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'] ); + } + /** * Retrieve Pattern Count. * diff --git a/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js b/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js index 6bd6ae9aa..a42088787 100644 --- a/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js +++ b/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js @@ -59,11 +59,11 @@ const StepDesignHomepageMenu = () => { function refactorPatterns( homepagePatternDataResp ) { const makeHomepagePattern = []; - - homepagePatternDataResp.forEach( ( homepagePatternData ) => { - makeHomepagePattern.push( homepagePatternData.content ); - } ); - + Object.values( homepagePatternDataResp ).forEach( + ( homepagePatternData ) => { + makeHomepagePattern.push( homepagePatternData.content ); + } + ); return makeHomepagePattern; } @@ -75,11 +75,14 @@ const StepDesignHomepageMenu = () => { if ( homepagePatternDataTemp?.error ) { return updateThemeStatus( THEME_STATUS_INIT ); } + setHomepagePattern( refactorPatterns( homepagePatternDataTemp?.body ) ); - homepagePatternDataTemp?.body.forEach( ( homepagePatternData ) => { - homepagePatternList.push( homepagePatternData.slug ); - } ); + Object.values( homepagePatternDataTemp?.body ).forEach( + ( homepagePatternData ) => { + homepagePatternList.push( homepagePatternData.slug ); + } + ); setHomepagePatternList( homepagePatternList ); if ( currentData?.data.sitePages.homepage !== '' ) { From fe4bc8f064912c5f9ca310e5f2e3b91bb45f2111 Mon Sep 17 00:00:00 2001 From: Yashita Date: Wed, 17 May 2023 11:44:39 +0530 Subject: [PATCH 07/15] Update Patterns.php --- includes/Data/Patterns.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index ed00241f1..b5474c741 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -351,9 +351,11 @@ private static function get_patterns_for_homepage_menu_slugs( $pattern_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; } } From 84aebc6f60c56e84c5f219604726f2e4e06b075f Mon Sep 17 00:00:00 2001 From: Yashita Date: Wed, 17 May 2023 16:41:27 +0530 Subject: [PATCH 08/15] Update Patterns.php --- includes/Data/Patterns.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index b5474c741..53d01d840 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -209,8 +209,6 @@ public static function get_meta_from_pattern_slug( $pattern_slug ) { * @return array|boolean */ public static function get_pattern_from_slug( $pattern_slug ) { - $active_theme = ( \wp_get_theme() )->get( 'TextDomain' ); - $pattern_slug = $active_theme . '/' . $pattern_slug; $block_patterns_registry = \WP_Block_Patterns_Registry::get_instance(); if ( $block_patterns_registry->is_registered( $pattern_slug ) ) { $pattern = $block_patterns_registry->get_registered( $pattern_slug ); @@ -285,10 +283,6 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false return false; } - if ( ! isset( self::get_theme_step_patterns_callback()[ $active_theme ][ $step ] ) ) { - return false; - } - $pattern_slugs = self::get_theme_step_patterns()[ $active_theme ][ $step ]; $pattern_slugs_callback = self::get_theme_step_patterns_callback()[ $active_theme ][ $step ]; $block_patterns_registry = \WP_Block_Patterns_Registry::get_instance(); From be9df0799589da6254bffd646b8bc098e79ac246 Mon Sep 17 00:00:00 2001 From: Yashita Date: Fri, 19 May 2023 19:39:20 +0530 Subject: [PATCH 09/15] Update Patterns.php --- includes/Data/Patterns.php | 65 +++++++++++++++----------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index 53d01d840..c63911175 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -128,10 +128,10 @@ protected static function get_theme_step_patterns() { * * @return array */ - protected static function get_theme_step_patterns_callback() { + protected static function get_theme_step_filters() { return array( 'yith-wonder' => array( - 'homepage-styles' => array( __CLASS__, 'get_patterns_for_homepage_menu_slugs' ), + 'homepage-styles' => array( __CLASS__, 'filter_homepage_patterns' ), ), ); } @@ -230,19 +230,15 @@ public static function get_pattern_from_slug( $pattern_slug ) { * * @return array */ - private static function replace_header_menu_slug( $patterns ) { + private static function replace_header_menu_slug() { // 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]; + $header_menu_slug = $flow_data['data']['partHeader']; + $header_menu_slug = ! empty( $header_menu_slug ) ? explode( '/', $header_menu_slug )[1] : ''; if ( ! empty( $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 ); - } - } + return $header_menu_slug; } - return $patterns; + return false; } /** @@ -267,7 +263,7 @@ private static function replace_split_menu_items( $pattern_content ) { } return $pattern_content; } - + /** * Retrieve Theme Step Patterns from chosen Theme in Previous Step * @@ -284,21 +280,19 @@ 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 ]; $block_patterns_registry = \WP_Block_Patterns_Registry::get_instance(); - $block_patterns = array(); - $block_patterns_squashed = ''; - - $pattern_slugs = self::replace_header_menu_slug( $pattern_slugs ); foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) { if ( true === $pattern_slugs[ $pattern_slug ]['active'] ) { + if( isset( $pattern_slugs[ $pattern_slug ]['replace'] ) && true === $pattern_slugs[ $pattern_slug ]['replace'] ) { + $pattern_slug_data = $pattern_slugs[ $pattern_slug ]; + $header_menu_slug = self::replace_header_menu_slug(); + $pattern_slug = ( ! empty( $header_menu_slug ) ) ? $header_menu_slug : $pattern_slug; + $pattern_slugs[ $pattern_slug ] = $pattern_slug_data; + } $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']; - } // 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'] ); @@ -317,17 +311,18 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false ); continue; } - $block_patterns_squashed .= self::cleanup_wp_grammar( $pattern['content'] ); + $block_patterns .= self::cleanup_wp_grammar( $pattern['content'] ); } } } - $pattern_slug_callback = isset( $pattern_slugs_callback ) ? $pattern_slugs_callback : false; - if ( is_callable( $pattern_slug_callback ) ) { - return $pattern_slug_callback( $block_patterns ); + $step_filter = self::get_theme_step_filters()[ $active_theme ][ $step ]; + $theme_step_callback = isset( $step_filter ) ? $step_filter : false; + if ( is_callable( $theme_step_callback ) ) { + return $theme_step_callback( $block_patterns ); } - return $squash ? $block_patterns_squashed : $block_patterns; + return $block_patterns; } /** @@ -336,17 +331,19 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false * @param array $pattern_slugs Step Patterns Data * @return array */ - private static function get_patterns_for_homepage_menu_slugs( $pattern_slugs ) { + private static function filter_homepage_patterns( $patterns ) { $header_content = ''; + $homepage_style_slugs = array(); $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 ) { + foreach ( $patterns as $index_key => $slug ) { if ( in_array( 'yith-wonder-site-header', $slug['categories'] ) ) { $header_content = $slug['content']; continue; } + if( in_array( 'yith-wonder-pages', $slug['categories'] ) ) { + array_push($homepage_style_slugs, $slug); + } if ( in_array( 'yith-wonder-site-footer', $slug['categories'] ) ) { $footer_content = $slug['content']; continue; @@ -360,16 +357,6 @@ private static function get_patterns_for_homepage_menu_slugs( $pattern_slugs ) { 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'] ); - } - /** * Retrieve Pattern Count. * From 9f7d329e2e2b49c99d67b45ce3da605152013e91 Mon Sep 17 00:00:00 2001 From: Yashita Date: Fri, 19 May 2023 19:42:32 +0530 Subject: [PATCH 10/15] lint fixes --- includes/Data/Patterns.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index c63911175..401e8657d 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -226,9 +226,7 @@ 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 - * - * @return array + * @return array|boolean */ private static function replace_header_menu_slug() { // fetch the selected header menu slug from DB @@ -284,7 +282,7 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) { if ( true === $pattern_slugs[ $pattern_slug ]['active'] ) { - if( isset( $pattern_slugs[ $pattern_slug ]['replace'] ) && true === $pattern_slugs[ $pattern_slug ]['replace'] ) { + if ( isset( $pattern_slugs[ $pattern_slug ]['replace'] ) && true === $pattern_slugs[ $pattern_slug ]['replace'] ) { $pattern_slug_data = $pattern_slugs[ $pattern_slug ]; $header_menu_slug = self::replace_header_menu_slug(); $pattern_slug = ( ! empty( $header_menu_slug ) ) ? $header_menu_slug : $pattern_slug; @@ -328,7 +326,7 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false /** * Retrieve Homepage Menu Step Patterns * - * @param array $pattern_slugs Step Patterns Data + * @param array $patterns Step Patterns Data * @return array */ private static function filter_homepage_patterns( $patterns ) { @@ -341,7 +339,7 @@ private static function filter_homepage_patterns( $patterns ) { $header_content = $slug['content']; continue; } - if( in_array( 'yith-wonder-pages', $slug['categories'] ) ) { + if ( in_array( 'yith-wonder-pages', $slug['categories'] ) ) { array_push($homepage_style_slugs, $slug); } if ( in_array( 'yith-wonder-site-footer', $slug['categories'] ) ) { From 76067658fc5848c5a2f7d5ab4ce8e929da3649de Mon Sep 17 00:00:00 2001 From: Yashita Date: Fri, 19 May 2023 19:44:08 +0530 Subject: [PATCH 11/15] lint fix --- includes/Data/Patterns.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index 401e8657d..27dcd5cc4 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -261,7 +261,7 @@ private static function replace_split_menu_items( $pattern_content ) { } return $pattern_content; } - + /** * Retrieve Theme Step Patterns from chosen Theme in Previous Step * @@ -340,7 +340,7 @@ private static function filter_homepage_patterns( $patterns ) { continue; } if ( in_array( 'yith-wonder-pages', $slug['categories'] ) ) { - array_push($homepage_style_slugs, $slug); + array_push( $homepage_style_slugs, $slug ); } if ( in_array( 'yith-wonder-site-footer', $slug['categories'] ) ) { $footer_content = $slug['content']; From 83f80525d1197c51885c5526dc5e790c0f42ff1f Mon Sep 17 00:00:00 2001 From: Yashita Date: Sat, 20 May 2023 10:52:44 +0530 Subject: [PATCH 12/15] Update index.js --- .../pages/Steps/DesignHomepageMenu/index.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js b/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js index 7026bde92..a4e4fab69 100644 --- a/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js +++ b/src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js @@ -53,11 +53,13 @@ const StepDesignHomepageMenu = () => { function refactorPatterns( homepagePatternDataResp ) { const makeHomepagePattern = []; - Object.values( homepagePatternDataResp ).forEach( + homepagePatternDataResp.forEach( ( homepagePatternData ) => { makeHomepagePattern.push( homepagePatternData.content ); + homepagePatternList.push( homepagePatternData.slug ); } ); + setHomepagePatternList( homepagePatternList ); return makeHomepagePattern; } @@ -72,13 +74,6 @@ const StepDesignHomepageMenu = () => { setHomepagePattern( refactorPatterns( homepagePatternDataTemp?.body ) ); - Object.values( homepagePatternDataTemp?.body ).forEach( - ( homepagePatternData ) => { - homepagePatternList.push( homepagePatternData.slug ); - } - ); - setHomepagePatternList( homepagePatternList ); - if ( currentData?.data.sitePages.homepage !== '' ) { setSelectedHomepage( homepagePatternList?.indexOf( @@ -92,8 +87,6 @@ const StepDesignHomepageMenu = () => { }; setCurrentOnboardingData( currentData ); } - - setHomepagePattern( refactorPatterns( homepagePatternDataTemp ) ); } function saveDataForHomepage( idx ) { From e37cc294139feb780789f7eb1589ccd8fbdf0f8b Mon Sep 17 00:00:00 2001 From: Yashita Date: Mon, 22 May 2023 13:40:17 +0530 Subject: [PATCH 13/15] refactored selected header menu slug --- includes/Data/Patterns.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index 27dcd5cc4..d8f21292a 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -224,19 +224,23 @@ public static function get_pattern_from_slug( $pattern_slug ) { } /** - * Replace the header menu slug in the patterns array + * Retrieve the header menu slug from flow data. * - * @return array|boolean + * @return string|boolean */ - private static function replace_header_menu_slug() { + private static function get_selected_header_from_flow_data() { // fetch the selected header menu slug from DB - $flow_data = \get_option( Options::get_option_name( 'flow' ) ); - $header_menu_slug = $flow_data['data']['partHeader']; - $header_menu_slug = ! empty( $header_menu_slug ) ? explode( '/', $header_menu_slug )[1] : ''; - if ( ! empty( $header_menu_slug ) ) { - return $header_menu_slug; + $flow_data = \get_option( Options::get_option_name( 'flow' ), false ); + if ( ! $flow_data ) { + return false; } + + if ( ! empty( $flow_data['data']['partHeader'] ) ) { + return explode( '/', $flow_data['data']['partHeader'] )[1]; + } + return false; + } /** @@ -284,7 +288,7 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false if ( true === $pattern_slugs[ $pattern_slug ]['active'] ) { if ( isset( $pattern_slugs[ $pattern_slug ]['replace'] ) && true === $pattern_slugs[ $pattern_slug ]['replace'] ) { $pattern_slug_data = $pattern_slugs[ $pattern_slug ]; - $header_menu_slug = self::replace_header_menu_slug(); + $header_menu_slug = self::get_selected_header_from_flow_data(); $pattern_slug = ( ! empty( $header_menu_slug ) ) ? $header_menu_slug : $pattern_slug; $pattern_slugs[ $pattern_slug ] = $pattern_slug_data; } From 7342a9d75c3a567a7fe9e50ca31716385330f018 Mon Sep 17 00:00:00 2001 From: Yashita Date: Mon, 22 May 2023 15:56:37 +0530 Subject: [PATCH 14/15] all code paths taken care based on conditions in get_theme_step_patterns_from_step --- includes/Data/Patterns.php | 61 +++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index d8f21292a..9ed297404 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -285,37 +285,38 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false $block_patterns_registry = \WP_Block_Patterns_Registry::get_instance(); foreach ( array_keys( $pattern_slugs ) as $pattern_slug ) { - if ( true === $pattern_slugs[ $pattern_slug ]['active'] ) { - if ( isset( $pattern_slugs[ $pattern_slug ]['replace'] ) && true === $pattern_slugs[ $pattern_slug ]['replace'] ) { - $pattern_slug_data = $pattern_slugs[ $pattern_slug ]; - $header_menu_slug = self::get_selected_header_from_flow_data(); - $pattern_slug = ( ! empty( $header_menu_slug ) ) ? $header_menu_slug : $pattern_slug; - $pattern_slugs[ $pattern_slug ] = $pattern_slug_data; - } - $pattern_name = $active_theme . '/' . $pattern_slug; - if ( $block_patterns_registry->is_registered( $pattern_name ) ) { - $pattern = $block_patterns_registry->get_registered( $pattern_name ); - // 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'] ); - } - - if ( ! $squash ) { - $block_patterns[] = array_merge( - array( - 'slug' => $pattern_name, - 'title' => $pattern['title'], - 'content' => self::cleanup_wp_grammar( $pattern['content'] ), - 'name' => $pattern['name'], - 'categories' => $pattern['categories'], - ), - $pattern_slugs[ $pattern_slug ] - ); - continue; - } - $block_patterns .= self::cleanup_wp_grammar( $pattern['content'] ); - } + if ( true !== $pattern_slugs[ $pattern_slug ]['active'] ) { + continue; + } + if ( isset( $pattern_slugs[ $pattern_slug ]['replace'] ) && true === $pattern_slugs[ $pattern_slug ]['replace'] ) { + $pattern_slug_data = $pattern_slugs[ $pattern_slug ]; + $header_menu_slug = self::get_selected_header_from_flow_data(); + $pattern_slug = ( ! empty( $header_menu_slug ) ) ? $header_menu_slug : $pattern_slug; + $pattern_slugs[ $pattern_slug ] = $pattern_slug_data; + } + $pattern_name = $active_theme . '/' . $pattern_slug; + if ( ! $block_patterns_registry->is_registered( $pattern_name ) ) { + continue; + } + $pattern = $block_patterns_registry->get_registered( $pattern_name ); + // 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'] ); + } + if ( ! $squash ) { + $block_patterns[] = array_merge( + array( + 'slug' => $pattern_name, + 'title' => $pattern['title'], + 'content' => self::cleanup_wp_grammar( $pattern['content'] ), + 'name' => $pattern['name'], + 'categories' => $pattern['categories'], + ), + $pattern_slugs[ $pattern_slug ] + ); + continue; } + $block_patterns .= self::cleanup_wp_grammar( $pattern['content'] ); } $step_filter = self::get_theme_step_filters()[ $active_theme ][ $step ]; From 2ff1f17ba0cc6f882c5b86796490cae7665a194e Mon Sep 17 00:00:00 2001 From: Yashita Date: Tue, 23 May 2023 12:20:31 +0530 Subject: [PATCH 15/15] renamed homepage pattern specific function --- includes/Data/Patterns.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Data/Patterns.php b/includes/Data/Patterns.php index 9ed297404..81ba7bcf6 100644 --- a/includes/Data/Patterns.php +++ b/includes/Data/Patterns.php @@ -131,7 +131,7 @@ protected static function get_theme_step_patterns() { protected static function get_theme_step_filters() { return array( 'yith-wonder' => array( - 'homepage-styles' => array( __CLASS__, 'filter_homepage_patterns' ), + 'homepage-styles' => array( __CLASS__, 'filter_yith_wonder_homepage_patterns' ), ), ); } @@ -334,7 +334,7 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false * @param array $patterns Step Patterns Data * @return array */ - private static function filter_homepage_patterns( $patterns ) { + private static function filter_yith_wonder_homepage_patterns( $patterns ) { $header_content = ''; $homepage_style_slugs = array(); $footer_content = '';