Skip to content
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

Merged
merged 20 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 53 additions & 24 deletions includes/Data/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
'site-pages' => array(
'company-page' => array(
'active' => true,
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
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'] );
Expand All @@ -304,10 +289,54 @@ 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';
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
$footer_slug = 'site-footer';

$homepage_style_slugs = array(
'homepage-1' => array(
'active' => true,
'shown' => true,
'content' => '',
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
),
'homepage-2' => array(
'active' => true,
'shown' => true,
'content' => '',
),
'homepage-3' => array(
'active' => true,
'shown' => true,
'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( $header_slug, 'split' ) ) {
$header_block_grammar = self::replace_split_menu_items( $header_block_grammar );
}

$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;
}

return $homepage_style_slugs;
}

/**
* Retrieve Pattern Count.
*
Expand Down
60 changes: 12 additions & 48 deletions src/OnboardingSPA/pages/Steps/DesignHomepageMenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 );
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved

if ( currentData?.data.sitePages.homepage !== '' ) {
setSelectedHomepage(
Expand Down