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 16 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
101 changes: 74 additions & 27 deletions includes/Data/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ protected static function get_theme_step_patterns() {
);
}

/**
* Callback Functions for Theme Step.
*
* @return array
*/
protected static function get_theme_step_filters() {
return array(
'yith-wonder' => array(
'homepage-styles' => array( __CLASS__, 'filter_homepage_patterns' ),
),
);
}

/**
* Get post metadata for a pattern. Ref: SitePagesController.php
*
Expand Down Expand Up @@ -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
*/
Expand All @@ -213,19 +226,17 @@ 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
* @return array|boolean
*/
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() {
// 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;
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
}
return $patterns;
return false;
}

/**
Expand Down Expand Up @@ -268,18 +279,15 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false

$pattern_slugs = self::get_theme_step_patterns()[ $active_theme ][ $step ];
$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 );
}

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;
}
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
$pattern_name = $active_theme . '/' . $pattern_slug;
if ( $block_patterns_registry->is_registered( $pattern_name ) ) {
$pattern = $block_patterns_registry->get_registered( $pattern_name );
Expand All @@ -291,21 +299,60 @@ 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 ]
);
continue;
}
$block_patterns_squashed .= self::cleanup_wp_grammar( $pattern['content'] );
$block_patterns .= self::cleanup_wp_grammar( $pattern['content'] );
}
}
}

return $squash ? $block_patterns_squashed : $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 $block_patterns;
}

/**
* Retrieve Homepage Menu Step Patterns
*
* @param array $patterns Step Patterns Data
* @return array
*/
private static function filter_homepage_patterns( $patterns ) {
$header_content = '';
$homepage_style_slugs = array();
$footer_content = '';

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

foreach ( $homepage_style_slugs as $key => $homepage_style ) {
$homepage_style_slugs[ $key ]['content'] = $header_content . $homepage_style['content'] . $footer_content;
}

return $homepage_style_slugs;
}

/**
Expand Down
59 changes: 10 additions & 49 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 { currentStep, currentData, themeStatus, themeVariations } =
Expand Down Expand Up @@ -76,41 +53,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;
homepagePatternDataResp.forEach(
( homepagePatternData ) => {
makeHomepagePattern.push( homepagePatternData.content );
homepagePatternList.push( homepagePatternData.slug );
}

let patternData = '';
homepagePatterns.forEach( ( patternName ) => {
homepagePatternDataResp?.body.forEach(
( homepagePatternData ) => {
if ( homepagePatternData.slug === patternName ) {
patternData += homepagePatternData.content;
}
}
);
} );
makeHomepagePattern.push( patternData );
}

);
setHomepagePatternList( homepagePatternList );
return makeHomepagePattern;
}

async function getHomepagePatternsData() {
const homepagePatternDataTemp = await getPatterns(
currentStep.patternId
);

if ( homepagePatternDataTemp?.error ) {
return updateThemeStatus( THEME_STATUS_INIT );
}

setHomepagePattern( refactorPatterns( homepagePatternDataTemp?.body ) );

if ( currentData?.data.sitePages.homepage !== '' ) {
setSelectedHomepage(
homepagePatternList?.indexOf(
Expand All @@ -124,8 +87,6 @@ const StepDesignHomepageMenu = () => {
};
setCurrentOnboardingData( currentData );
}

setHomepagePattern( refactorPatterns( homepagePatternDataTemp ) );
}

function saveDataForHomepage( idx ) {
Expand Down