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

Dummy Header Menu #187

Merged
merged 37 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b8a25df
added scroll to top and useLayoutEffect for faster rerender
abhijitb Dec 19, 2022
3aead27
added entity edit functionality WIP
abhijitb Dec 20, 2022
8f4d9d2
split menu item update done
abhijitb Jan 23, 2023
b6486d9
added dummy menu links for non split menus
abhijitb Jan 24, 2023
0a42e49
Merge branch 'trunk' into enhance/PRESS2-399-dummy-menu
abhijitb Jan 24, 2023
f774861
WIP : editing pages entities
abhijitb Feb 14, 2023
f5611ce
added filters to modify the w2/json/pages API call
abhijitb Feb 27, 2023
4c48606
removed the entity framework code
abhijitb Feb 27, 2023
bf7cf39
handled the last edge case
abhijitb Feb 28, 2023
80ea624
using LivePreviewSkeleton for showing header menu patterns
abhijitb Mar 1, 2023
3910950
Merge branch 'trunk' into enhance/PRESS2-399-dummy-menu
abhijitb Mar 1, 2023
ecd76af
made the headermenu preview count dynamic
abhijitb Mar 1, 2023
1d021ad
added selected headermenu to the homepages step
abhijitb Mar 1, 2023
51a1efa
fixed Js linting issues
abhijitb Mar 1, 2023
ba36c3e
fixed JS linting issues again
abhijitb Mar 1, 2023
160b236
fixing JS lint issues
abhijitb Mar 2, 2023
1f3f06a
fixed SCSS linting
abhijitb Mar 2, 2023
af21fbd
Merge branch 'trunk' into enhance/PRESS2-399-dummy-menu
abhijitb Mar 9, 2023
3e39e37
added error handling for header menu response
abhijitb Mar 9, 2023
7f053bc
code review fixes and code refactoring
abhijitb Mar 15, 2023
6e8e143
PHP lint and JS lint
abhijitb Mar 15, 2023
c405cbe
JS lint fixes
abhijitb Mar 15, 2023
1b5cbc8
fixed JS lint again
abhijitb Mar 15, 2023
1b2914e
refactored code to fetch data from DB
abhijitb Mar 22, 2023
4bff9b8
reveted JS changes
abhijitb Mar 22, 2023
deb01c6
fixing JS lint issues
abhijitb Mar 22, 2023
0261455
syncing DB with the store
abhijitb Mar 22, 2023
1694990
fixing JS lint
abhijitb Mar 22, 2023
9ecbfb2
error handling for the API call
abhijitb Mar 22, 2023
a79d856
Merge branch 'trunk' into enhance/PRESS2-399-dummy-menu
abhijitb Mar 23, 2023
1934e62
reverted changes as no longer needed
abhijitb Mar 28, 2023
da188b6
refactored code as suggested
abhijitb Mar 29, 2023
202ab98
removed unused isLoaded
abhijitb Mar 29, 2023
72ebdd7
forgot to remove function call
abhijitb Mar 29, 2023
e636a64
Merge branch 'trunk' into enhance/PRESS2-399-dummy-menu
abhijitb Apr 3, 2023
15d5be3
ignoring eslint for react-hooks
abhijitb Apr 3, 2023
33ada8f
disabled eslint rule of hooks for our custom function
abhijitb Apr 3, 2023
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
11 changes: 9 additions & 2 deletions 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 @@ -58,11 +65,11 @@ public function __construct( Container $container ) {
new WP_CLI();
}

if ( Permissions::is_authorized_admin() ) {
if ( Permissions::is_authorized_admin() || Permissions::rest_is_authorized_admin() ) {
abhijitb marked this conversation as resolved.
Show resolved Hide resolved
new WP_Admin();
}

\do_action( 'nfd_module_onboarding_post_init' );
}
}
// END \NewfoldLabs\WP\Module\Onboarding\Application
// END /NewfoldLabs/WP/Module/Onboarding/Application
80 changes: 79 additions & 1 deletion includes/Data/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
*/
final class Patterns {

/**
* List of styles to check for header menu slugs and replace with user selected header menu slug.
*
* @var array
*/
private static $styles_to_check_for_header_menu = array(
'theme-styles',
'homepage-styles',
);
abhijitb marked this conversation as resolved.
Show resolved Hide resolved

/**
* List of dummy menu page titles.
*
* @return array
*/
protected static function default_menu_items() {
abhijitb marked this conversation as resolved.
Show resolved Hide resolved
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 Down Expand Up @@ -148,15 +174,57 @@ 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 ( false !== stripos( $slug, '-header-' ) ) {
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::default_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
*
* @param string $step Step from which Theme Step Pattern is required
* @param boolean $squash Flag set to retrieve the block pattern
* @param string $header_menu_slug header menu slug for user selected menu
*
* @return array|string
*/
public static function get_theme_step_patterns_from_step( $step, $squash = false ) {
public static function get_theme_step_patterns_from_step( $step, $squash = false, $header_menu_slug = '' ) {
$active_theme = ( \wp_get_theme() )->get( 'TextDomain' );

if ( ! isset( self::get_theme_step_patterns()[ $active_theme ][ $step ] ) ) {
Expand All @@ -167,11 +235,21 @@ public static function get_theme_step_patterns_from_step( $step, $squash = false
$block_patterns_registry = \WP_Block_Patterns_Registry::get_instance();
$block_patterns = array();
$block_patterns_squashed = '';

if ( ! empty( $header_menu_slug ) && in_array( $step, self::$styles_to_check_for_header_menu, true ) ) {
$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()
199 changes: 199 additions & 0 deletions includes/RestApi/RestApiFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

namespace NewfoldLabs\WP\Module\Onboarding\RestApi;

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

/**
* Default set of dummy pages.
*
* @var array
*/
private static function dummy_pages() {
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' ),
);
}

/**
* 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 ( self::dummy_pages() 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( self::dummy_pages() ) ) {
$pages = array_pad(
$pages,
count( self::dummy_pages() ),
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'] = self::dummy_pages()[ $index ];
$page['menu_order'] = $index;
}
return $page;
}

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