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 17 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
20 changes: 16 additions & 4 deletions includes/Data/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,32 @@ protected static function get_theme_step_patterns() {
'site-header-left-logo-navigation-inline' => array(
'active' => true,
),
'homepage-1' => array(
'homepage-1' => array(
'active' => true,
'shown' => true,
),
'homepage-2' => array(
'homepage-2' => array(
'active' => true,
'shown' => true,
),
'homepage-3' => array(
'homepage-3' => array(
'active' => true,
'shown' => true,
),
'site-footer' => array(
'site-footer' => array(
'active' => true,
),
'site-header-left-logo-navigation-below' => array(
'active' => true,
abhijitb marked this conversation as resolved.
Show resolved Hide resolved
'shown' => false,
),
'site-header-centered' => array(
'active' => true,
'shown' => false,
),
'site-header-splitted-menu' => array(
'active' => true,
'shown' => false,
),
),
'site-pages' => array(
Expand Down
195 changes: 187 additions & 8 deletions includes/WP_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ final class WP_Admin {
*/
public static $slug = 'nfd-onboarding';

/**
abhijitb marked this conversation as resolved.
Show resolved Hide resolved
* Default set of dummy pages.
*
* @var array
*/
public static $dummy_pages = array( 'Home', 'About', 'Contact', 'News', 'Privacy', 'Careers' );
abhijitb marked this conversation as resolved.
Show resolved Hide resolved

/**
* Tap WordPress Hooks
*
Expand All @@ -26,7 +33,7 @@ final class WP_Admin {
public function __construct() {
\add_action( 'admin_menu', array( __CLASS__, 'register_page' ) );
\add_action( 'load-dashboard_page_' . self::$slug, array( __CLASS__, 'initialize' ) );
// \add_action( 'wp_dashboard_setup', array( __CLASS__, 'register_widget' ) );
\add_filter( 'rest_request_before_callbacks', array( __CLASS__, 'check_for_pages_api_call_from_onboarding_flow' ), 10, 3 );
}

/**
Expand Down Expand Up @@ -100,26 +107,198 @@ public static function register_assets() {
$asset['version']
);

wp_add_inline_script(
'wp-blocks',
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
);
wp_add_inline_script(
'wp-blocks',
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
);

\wp_enqueue_script( self::$slug );
\wp_enqueue_style( self::$slug );
}
}

/**
* Initialise Plugins and Themes if necessary.
*
* @return void
*/
public static function initialize() {
if ( isset( $_GET['nfd_plugins'] ) && $_GET['nfd_plugins'] === 'true' ) {
if ( ! empty( get_query_var( 'nfd_plugins' ) ) && 'true' === get_query_var( 'nfd_plugins' ) ) {
PluginInstallTaskManager::queue_initial_installs();
}

if ( isset( $_GET['nfd_themes'] ) && $_GET['nfd_themes'] === 'true' ) {
if ( ! empty( get_query_var( 'nfd_themes' ) ) && 'true' === get_query_var( 'nfd_themes' ) ) {
abhijitb marked this conversation as resolved.
Show resolved Hide resolved
ThemeInstallTaskManager::queue_initial_installs();
}

self::register_assets();
}

} // END \NewfoldLabs\WP\Module\Onboarding\Admin()
/**
* 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 check_for_pages_api_call_from_onboarding_flow( $response, array $handler, \WP_REST_Request $request ) {
if ( self::is_wp_pages_request( $request ) && self::is_request_from_onboarding_flow( $request ) ) {
\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 );
} elseif ( self::is_nav_menu_request( $request ) && self::is_request_from_onboarding_flow( $request ) ) {
\add_filter( 'rest_request_after_callbacks', array( __CLASS__, 'wp_onboarding_nav_menu_filter' ), 10, 2 );
}
return $response;
}
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved

abhijitb marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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;
}

/**
* Check if the CORE navigation API call is being made.
*
* @param \WP_REST_Request $request - WP_REST_Request object
*
* @return boolean
*/
public static function is_nav_menu_request( \WP_REST_Request $request ) {
return '/wp/v2/navigation' === $request->get_route() && 'GET' === $request->get_method();
}
abhijitb marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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 ) {
if ( self::is_wp_pages_request( $request ) && self::is_request_from_onboarding_flow( $request ) ) {
abhijitb marked this conversation as resolved.
Show resolved Hide resolved
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' );
}

/**
* Check if the CORE pages API call is being made.
*
* @param \WP_REST_Request $request - WP_REST_Request object
*
* @return boolean
*/
public static function is_wp_pages_request( \WP_REST_Request $request ) {
return '/wp/v2/pages' === $request->get_route() && 'GET' === $request->get_method();
}
abhijitb marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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/Admin()
Loading