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 12 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
98 changes: 80 additions & 18 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_patterns_callback() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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' ),
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
),
);
}

/**
* 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,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;
Expand Down Expand Up @@ -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'] );
Expand All @@ -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 ]
);
Expand All @@ -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'] );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we combine the header and footer identification part with this? Seems like we're going through the entire array anyway with array_filter? Also, this function name can be confusing, I think we should just stick to using a single foreach in get_patterns_for_homepage_menu_slugs to populate the header/footer/homepages instead of array_filter and then a foreach on the remaining elements.


/**
* Retrieve Pattern Count.
*
Expand Down
62 changes: 15 additions & 47 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,32 @@ 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;
Object.values( homepagePatternDataResp ).forEach(
( homepagePatternData ) => {
makeHomepagePattern.push( homepagePatternData.content );
}
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved

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

);
return makeHomepagePattern;
}

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

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

setHomepagePattern( refactorPatterns( homepagePatternDataTemp?.body ) );

Object.values( 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(
homepagePatternList?.indexOf(
Expand Down