Skip to content

Commit

Permalink
Merge pull request #187 from newfold-labs/enhance/PRESS2-399-dummy-menu
Browse files Browse the repository at this point in the history
Dummy Header Menu
  • Loading branch information
arunshenoy99 authored Apr 4, 2023
2 parents 515fc18 + 33ada8f commit cab49b2
Show file tree
Hide file tree
Showing 12 changed files with 476 additions and 247 deletions.
9 changes: 8 additions & 1 deletion includes/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@
final class Application {

/**
* Newfold Labs Module Container
*
* @var Container
*/
protected $container;

/**
* Arguments for the container
*
* @var agrs
*/
protected $args;

/**
Expand Down Expand Up @@ -65,4 +72,4 @@ public function __construct( Container $container ) {
\do_action( 'nfd_module_onboarding_post_init' );
}
}
// END \NewfoldLabs\WP\Module\Onboarding\Application
// END /NewfoldLabs/WP/Module/Onboarding/Application
85 changes: 79 additions & 6 deletions includes/Data/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
*/
final class Patterns {

/**
* List of dummy menu page titles.
*
* @return array
*/
public static function get_dummy_menu_items() {
return array(
__( 'Home', 'wp-module-onboarding' ),
__( 'About', 'wp-module-onboarding' ),
__( 'Contact', 'wp-module-onboarding' ),
__( 'News', 'wp-module-onboarding' ),
__( 'Privacy', 'wp-module-onboarding' ),
__( 'Careers', 'wp-module-onboarding' ),
);
}

/**
* Retrieve Patterns for Theme Step.
*
Expand All @@ -16,7 +32,8 @@ protected static function get_theme_step_patterns() {
'yith-wonder' => array(
'theme-styles' => array(
'site-header-left-logo-navigation-inline' => array(
'active' => true,
'active' => true,
'replace' => true,
),
'homepage-1' => array(
'active' => true,
Expand All @@ -29,7 +46,8 @@ protected static function get_theme_step_patterns() {
),
'homepage-styles' => array(
'site-header-left-logo-navigation-inline' => array(
'active' => true,
'active' => true,
'replace' => true,
),
'homepage-1' => array(
'active' => true,
Expand Down Expand Up @@ -148,6 +166,47 @@ public static function get_pattern_from_slug( $pattern_slug ) {
return false;
}

/**
* 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
*/
private static function replace_header_menu_slug( $patterns, $header_menu_slug ) {
foreach ( $patterns as $slug => $slug_details ) {
if ( true === $slug_details['replace'] ) {
unset( $patterns[ $slug ] );
$patterns = array_merge( array( $header_menu_slug => $slug_details ), $patterns );
}
}
return $patterns;
}

/**
* Replace the header menu slug in the patterns array
*
* @param array $pattern_content pattern grammar that is to be modified
*
* @return array
*/
private static function replace_split_menu_items( $pattern_content ) {
$dummy_menu_grammar = '';
$menu_navigation_grammar = '<!-- wp:navigation-link {"isTopLevelLink":true} /-->';

foreach ( self::get_dummy_menu_items() as $item ) {
$dummy_menu_grammar = '<!-- wp:navigation-link {
"isTopLevelLink":true,
"label":"' . strtolower( $item ) . '",
"title":"' . $item . '",
"url":"' . get_site_url() . '/' . strtolower( $item ) . '"
} /-->';
$pattern_content = preg_replace( $menu_navigation_grammar, $dummy_menu_grammar, $pattern_content, 1 );
}
return $pattern_content;
}

/**
* Retrieve Theme Step Patterns from chosen Theme in Previous Step
*
Expand All @@ -163,15 +222,29 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false
return 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 = '';
$styles_to_check_for_header_menu = self::get_theme_step_patterns()[ $active_theme ]['styles-for-header-menu-check'];
$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'] ) {
$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(
Expand Down
19 changes: 17 additions & 2 deletions includes/RestApi/RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace NewfoldLabs\WP\Module\Onboarding\RestApi;

use NewfoldLabs\WP\Module\Onboarding\RestApi\RestApiFilter;

/**
* Instantiate controllers and register routes.
*/
final class RestApi {

/**
* List of custom REST API controllers
*
* @var array
*/
protected $controllers = array(
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\SiteImagesController',
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\Themes\\ThemeGeneratorController',
Expand All @@ -20,13 +27,21 @@ final class RestApi {
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\SitePagesController',
'NewfoldLabs\WP\\Module\\Onboarding\\RestApi\\Themes\\ThemeInstallerController',
'NewfoldLabs\WP\\Module\\Onboarding\\RestApi\\Themes\\ThemeFontsController',
'NewfoldLabs\WP\\Module\\Onboarding\\RestApi\\Themes\\ThemeColorsController'
'NewfoldLabs\WP\\Module\\Onboarding\\RestApi\\Themes\\ThemeColorsController',
);

/**
* Setup the custom REST API
*/
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
// create an instance of the RestApiFilter to filter the responses for header menu navigation
new RestApiFilter();
}

/**
* Register the custom REST API routes
*/
public function register_routes() {
foreach ( $this->controllers as $controller ) {
/**
Expand All @@ -38,4 +53,4 @@ public function register_routes() {
$instance->register_routes();
}
}
} // END \NewfoldLabs\WP\Module\Onboarding\RestApi()
} // END /NewfoldLabs/WP/Module/Onboarding/RestApi()
185 changes: 185 additions & 0 deletions includes/RestApi/RestApiFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

namespace NewfoldLabs\WP\Module\Onboarding\RestApi;

use NewfoldLabs\WP\Module\Onboarding\Data\Patterns;

/**
* Instantiate controllers and register routes.
*/
class RestApiFilter {

/**
* Setup the custom REST API filters
*/
public function __construct() {
\add_filter( 'rest_request_before_callbacks', array( __CLASS__, 'add_appropriate_filters_for_onboarding' ), 10, 3 );
}

/**
* Custom filter to check for pages API call, if true then add more filters for the onboarding flow only.
*
* @param array $response - the API response
* @param array $handler - handler
* @param \WP_REST_Request $request - WP_REST_Request object
*
* @return array
*/
public static function add_appropriate_filters_for_onboarding( $response, array $handler, \WP_REST_Request $request ) {
if ( ! self::is_request_from_onboarding_flow( $request ) ) {
return $response;
}
$request_method = $request->get_method();
switch ( $request_method ) {
case 'GET':
self::get_method_filters( $request );
break;
}
return $response;
}

/**
* Apply the appropriate filters based on the route
*
* @param object $request REST API object
* @return void
*/
private static function get_method_filters( $request ) {
$request_route = $request->get_route();
switch ( $request_route ) {
case '/wp/v2/pages':
\add_filter( 'rest_page_query', array( __CLASS__, 'header_menu_limit_pages' ) );
\add_filter( 'rest_request_after_callbacks', array( __CLASS__, 'header_menu_rename_pages' ), 10, 3 );
break;
case '/wp/v2/navigation':
\add_filter( 'rest_request_after_callbacks', array( __CLASS__, 'wp_onboarding_nav_menu_filter' ), 10, 2 );
break;
}
}

/**
* Function for modifying the navigation menu grammar.
*
* @param object $response - WP_REST_Response object
* @param array $args - An array containing arguments.
*
* @return object
*/
public static function wp_onboarding_nav_menu_filter( $response, $args ) {
$modified_data = array_map(
array( __CLASS__, 'prepare_raw_html_menu' ),
$response->get_data(),
array_keys( $response->get_data() )
);
$response->set_data( $modified_data );
return $response;
}

/**
* Modify the reponse to make sure it has the dummy pages.
*
* @param array $data - array containing navigation menu data
* @param integer $index - array index from the pages list
*
* @return array
*/
public static function prepare_raw_html_menu( $data, $index ) {
// create dummy menu links
$menu_navigation_grammar = '';
foreach ( Patterns::get_dummy_menu_items() as $page_title ) {
$menu_navigation_grammar .= '<!-- wp:navigation-link {"isTopLevelLink":true, "label":"' . $page_title . '", "title":"' . $page_title . '"} /-->';
}
// need to reset ID else the data saved in the DB gets used
$data['id'] = $index;
$data['content']['rendered'] = $menu_navigation_grammar;
return $data;
}

/**
* Custom filter to check for pages API call, if true then add more filters for the onboarding flow only.
*
* @param array $args - the arguments used by the WP_QUERY
*
* @return array
*/
public static function header_menu_limit_pages( $args ) {
$args['posts_per_page'] = 6;
$args['orderby'] = 'id';
$args['no_found_rows'] = true;
return $args;
}

/**
* Custom filter to rename the info for the pages API call.
*
* @param array $response - the api response
* @param array $handler - handler
* @param \WP_REST_Request $request - WP_REST_Request object
*
* @return array
*/
public static function header_menu_rename_pages( $response, array $handler, \WP_REST_Request $request ) {
self::modify_get_pages_response( $response );
return $response;
}

/**
* Check if the API call is being made from the onboarding flow.
*
* @param \WP_REST_Request $request - WP_REST_Request object
*
* @return boolean
*/
public static function is_request_from_onboarding_flow( \WP_REST_Request $request ) {
return false !== stripos( $request->get_header( 'referer' ), 'page=nfd-onboarding' );
}

/**
* Modify the reponse to make sure it has the dummy pages.
*
* @param array $response - response array
*
* @return null
*/
public static function modify_get_pages_response( $response ) {
if ( ! ( $response instanceof \WP_REST_Response ) ) {
return;
}

// make sure we have the number of dummy pages required
$pages = $response->get_data();
if ( count( $pages ) < count( Patterns::get_dummy_menu_items() ) ) {
$pages = array_pad(
$pages,
count( Patterns::get_dummy_menu_items() ),
array_pop( $pages )
);
}

$data = array_map(
array( __CLASS__, 'rename_page' ),
$pages,
array_keys( $pages )
);
$response->set_data( $data );
}

/**
* Modify the reponse to make sure it has the dummy pages.
*
* @param array $page - array containing page attibutes
* @param integer $index - array index from the pages list
*
* @return array
*/
public static function rename_page( array $page, $index ) {
if ( isset( $page['title']['rendered'] ) ) {
// changed id so that while rendering the menu link and name are proper
$page['id'] = $page['id'] + $index;
$page['title']['rendered'] = Patterns::get_dummy_menu_items()[ $index ];
$page['menu_order'] = $index;
}
return $page;
}

} // END /NewfoldLabs/WP/Module/Onboarding/RestApiFilter()
Loading

0 comments on commit cab49b2

Please sign in to comment.