Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…oarding into enhance/auto-advance-on-custom-design-selection
  • Loading branch information
arunshenoy99 committed May 4, 2023
2 parents fc06437 + ee2b421 commit 1248748
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 270 deletions.
3 changes: 2 additions & 1 deletion includes/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function __construct( Container $container ) {
// Reset the stored Compatibility Status every time WP Core is updated.
\add_action( '_core_updated_successfully', array( Status::class, 'reset' ) );

\add_action( 'login_redirect', array( LoginRedirect::class, 'handle_redirect' ), 10, 3 );
\add_filter( 'login_redirect', array( LoginRedirect::class, 'handle_redirect' ), 10 );
\add_filter( 'newfold_sso_success_url', array( LoginRedirect::class, 'handle_redirect' ), 10 );
\add_filter(
Options::get_option_name( 'redirect' ) . '_disable',
array( LoginRedirect::class, 'remove_handle_redirect_action' )
Expand Down
88 changes: 36 additions & 52 deletions includes/LoginRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,75 +12,59 @@ class LoginRedirect {
/**
* Handles the redirect to onboarding
*
* @param [type] $redirect The requested redirect URL.
* @param [type] $redirect_to The requested redirect URL via param.
* @param [type] $user The logged in user object.
* @param string $original_redirect The requested redirect URL
* @return string
*/
public static function handle_redirect( $redirect, $redirect_to, $user ) {
public static function handle_redirect( $original_redirect ) {
// Don't redirect if user is not an admin
if ( ! current_user_can( 'manage_options' ) ) {
return $original_redirect;
}

$redirect_option_name = Options::get_option_name( 'redirect' );
// If request has ?nfd_module_onboarding_redirect=false then temporarily disable the redirect
if ( isset( $_GET[ $redirect_option_name ] )
&& 'false' === $_GET[ $redirect_option_name ] ) {
self::disable_redirect();
&& 'false' === $_GET[ $redirect_option_name ] ) {
self::disable_redirect();
}

$flow_exited = false;
$flow_completed = false;
$flow_data = \get_option( Options::get_option_name( 'flow' ), false );
if ( ! empty( $flow_data ) ) {
$flow_exited = ( ! empty( $flow_data['hasExited'] ) );
$flow_completed = ( ! empty( $flow_data['isComplete'] ) );
// Redirect was temporarily disabled via transient
if ( \get_transient( Options::get_option_name( 'redirect_param' ) ) === '1' ) {
return $original_redirect;
}

if ( \get_transient( Options::get_option_name( 'redirect_param' ) ) === '1'
|| $flow_exited
|| $flow_completed
) {
return $redirect;
// Don't redirect if coming soon is off. User has launched their site
if ( \get_option( Options::get_option_name( 'coming_soon', false ), 'true' ) !== 'true' ) {
return $original_redirect;
}

// Don't redirect if they have intentionally exited or completed onboarding
$flow_data = \get_option( Options::get_option_name( 'flow' ), false );
if ( data_get( $flow_data, 'hasExited' ) || data_get( $flow_data, 'isComplete' ) ) {
return $original_redirect;
}

// Check for disabled redirect database option: nfd_module_onboarding_redirect
$redirect_option = \get_option( $redirect_option_name, null );
// If not set, then set it to true
if ( empty( $redirect_option ) ) {
$redirect_option = \update_option( $redirect_option_name, true );
}
if ( ! $redirect_option ) {
return $original_redirect;
}

// If site was created more than 72 hours ago, don't redirect to onboarding
$install_date = new DateTime( \get_option( Options::get_option_name( 'install_date', false ), gmdate( 'M d, Y' ) ) );
$current_date = new DateTime( gmdate( 'M d, Y' ) );
$interval = $current_date->diff( $install_date );
$interval_in_hours = ( $interval->days * 24 ) + $interval->h;

if ( ! ( $redirect_option
&& \get_option( Options::get_option_name( 'coming_soon', false ), 'true' ) === 'true'
&& ( $interval_in_hours <= 72 ) ) ) {
return $redirect;
}

if ( self::is_administrator( $user ) ) {
return \admin_url( '/index.php?page=nfd-onboarding' );
if ( $interval_in_hours >= 72 ) {
return $original_redirect;
}

return $redirect;
}

/**
* Check if we have a valid user.
*
* @param \WP_User $user The WordPress user object.
*
* @return bool
*/
public static function is_user( $user ) {
return $user && is_object( $user ) && is_a( $user, 'WP_User' );
}

/**
* Check if a user is an administrator.
*
* @param \WP_User $user WordPress user.
*
* @return bool
*/
public static function is_administrator( $user ) {
return self::is_user( $user ) && $user->has_cap( Permissions::ADMIN );
// Finally, if we made it this far, then set the redirect URL to point to onboarding
return \admin_url( '/index.php?page=nfd-onboarding' );
}

/**
Expand All @@ -89,7 +73,7 @@ public static function is_administrator( $user ) {
* @return void
*/
public static function disable_redirect() {
\set_transient( Options::get_option_name( 'redirect_param' ), '1', 30 );
\set_transient( Options::get_option_name( 'redirect_param' ), '1', 30 );
}

/**
Expand All @@ -98,7 +82,7 @@ public static function disable_redirect() {
* @return void
*/
public static function enable_redirect() {
\set_transient( Options::get_option_name( 'redirect_param' ), '0', 30 );
\set_transient( Options::get_option_name( 'redirect_param' ), '0', 30 );
}

/**
Expand All @@ -107,6 +91,6 @@ public static function enable_redirect() {
* @return bool
*/
public static function remove_handle_redirect_action() {
return \remove_action( 'login_redirect', array( __CLASS__, 'handle_redirect' ) );
return \remove_action( 'login_redirect', array( __CLASS__, 'handle_redirect' ) );
}
}
3 changes: 0 additions & 3 deletions includes/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ public static function module_switcher() {
// Check if the Module Does Exist
if ( ModuleRegistry::get( $module_name ) ) {

// Disable the Redirect for Onboarding SPA
LoginRedirect::disable_redirect();

// Deactivate the Module
deactivate( $module_name );
}
Expand Down
1 change: 1 addition & 0 deletions includes/RestApi/RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class RestApi {
'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\\SiteClassificationController',
);

/**
Expand Down
56 changes: 56 additions & 0 deletions includes/RestApi/SiteClassificationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace NewfoldLabs\WP\Module\Onboarding\RestApi;

use NewfoldLabs\WP\Module\Onboarding\Permissions;
use NewfoldLabs\WP\Module\Data\SiteClassification;

/**
* Class SiteClassificationController
*/
class SiteClassificationController {

/**
* The namespace of this controller's route.
*
* @var string
*/
protected $namespace = 'newfold-onboarding/v1';

/**
* The endpoint base
*
* @var string
*/
protected $rest_base = '/site-classification';

/**
* Registers rest routes for SiteClassificationController class.
*
* @return void
*/
public function register_routes() {
\register_rest_route(
$this->namespace,
$this->rest_base,
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
)
);
}

/**
* Get site classification data.
*
* @return array
*/
public function get() {
if ( ! class_exists( 'NewfoldLabs\WP\Module\Data\SiteClassification' ) ) {
return array();
}
$classification = new SiteClassification();
return $classification->get();
}
}
105 changes: 28 additions & 77 deletions src/OnboardingSPA/components/Drawer/DrawerPanel/DesignTypography.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useEffect, useRef } from '@wordpress/element';

import { store as nfdOnboardingStore } from '../../../store';
import { getGlobalStyles, getThemeFonts } from '../../../utils/api/themes';
import { getThemeFonts } from '../../../utils/api/themes';
import { useGlobalStylesOutput } from '../../../utils/global-styles/use-global-styles-output';
import { THEME_STATUS_ACTIVE, THEME_STATUS_INIT } from '../../../../constants';

Expand All @@ -12,7 +12,6 @@ const DesignTypography = () => {
const [ isLoaded, setIsLoaded ] = useState( false );
const [ selectedFont, setSelectedFont ] = useState();
const [ fontPalettes, setFontPalettes ] = useState();
const [ isAccordionClosed, setIsAccordionClosed ] = useState( true );

const { storedPreviewSettings, currentData, themeStatus } = useSelect(
( select ) => {
Expand All @@ -34,19 +33,12 @@ const DesignTypography = () => {
} = useDispatch( nfdOnboardingStore );

const getFontStylesAndPatterns = async () => {
const fontPalettes = await getThemeFonts();
if ( fontPalettes?.error ) {
const fonts = await getThemeFonts();
if ( fonts?.error ) {
return updateThemeStatus( THEME_STATUS_INIT );
}
setFontPalettes( fontPalettes?.body );
setFontPalettes( fonts?.body );

if ( currentData?.data?.typography?.slug !== '' ) {
handleClick(
currentData?.data?.typography?.slug,
storedPreviewSettings,
fontPalettes?.body
);
}
const stylesCustom = storedPreviewSettings?.settings?.styles[ 0 ]?.css;
if ( stylesCustom ) {
// Loads in all CSS variables related to fontFamily
Expand All @@ -59,20 +51,27 @@ const DesignTypography = () => {
setIsLoaded( true );
};

useEffect( () => {
if (
currentData?.data?.typography?.slug !== '' &&
fontPalettes !== undefined
) {
setSelectedFont( currentData?.data?.typography?.slug );
handleClick( currentData?.data?.typography?.slug );
}
}, [ fontPalettes, storedPreviewSettings ] );

useEffect( () => {
if ( ! isLoaded && THEME_STATUS_ACTIVE === themeStatus )
getFontStylesAndPatterns();
}, [ isLoaded, themeStatus ] );

const handleClick = async (
fontStyle,
selectedGlobalStyle = storedPreviewSettings,
fontPalettesCopy = fontPalettes
) => {
const handleClick = async ( fontStyle ) => {
setSelectedFont( fontStyle );

// Changes the Global Styles to Recompute css properties
const globalStylesCopy = selectedGlobalStyle;
const globalStylesCopy = storedPreviewSettings;
const fontPalettesCopy = fontPalettes;

if (
globalStylesCopy?.styles?.typography?.fontFamily &&
Expand Down Expand Up @@ -118,45 +117,28 @@ const DesignTypography = () => {
currentData.data.typography.data = fontPalettesCopy[ fontStyle ];

updatePreviewSettings(
// eslint-disable-next-line react-hooks/rules-of-hooks
useGlobalStylesOutput( globalStylesCopy, storedPreviewSettings )
);
setCurrentOnboardingData( currentData );
};

async function resetFonts() {
setSelectedFont( '' );
const globalStyles = await getGlobalStyles();
let selectedGlobalStyle;
if ( currentData?.data?.theme?.variation ) {
selectedGlobalStyle = globalStyles.body.filter(
( globalStyle ) =>
globalStyle.title === currentData.data.theme.variation
)[ 0 ];
} else if ( globalStyles.body[ 0 ]?.id === 0 ) {
selectedGlobalStyle = globalStyles.body[ 0 ];
}
updatePreviewSettings(
useGlobalStylesOutput( selectedGlobalStyle, storedPreviewSettings )
);

currentData.data.typography.slug = '';
currentData.data.typography.data = [];
setCurrentOnboardingData( currentData );
}

function buildPalettes() {
const paletteRenderedList = [];
for ( const fontStyle in fontPalettes ) {
return Object.keys( fontPalettes ).map( ( fontStyle, idx ) => {
const splitLabel = fontPalettes[ fontStyle ]?.label.split( '&', 2 );
if ( splitLabel.length === 0 ) continue;
paletteRenderedList.push(
if ( splitLabel.length === 0 ) return null;
return (
<div
key={ fontStyle }
tabIndex={ idx + 1 }
role="button"
className={ `font-palette drawer-palette--button ${
selectedFont == fontStyle
selectedFont === fontStyle
? 'font-palette-selected drawer-palette--button--selected'
: ''
} ` }
onClick={ ( e ) => handleClick( fontStyle ) }
onClick={ () => handleClick( fontStyle ) }
onKeyDown={ () => handleClick( fontStyle ) }
>
<div
className="font-palette__icon drawer-palette--button__text"
Expand Down Expand Up @@ -192,44 +174,13 @@ const DesignTypography = () => {
</div>
</div>
);
}

return paletteRenderedList;
}

function buildCustomPalette() {
return (
<div className="custom-font-palette">
<div
className="custom-font-palette__top"
onClick={ ( e ) =>
setIsAccordionClosed( ! isAccordionClosed )
}
>
<div className="custom-font-palette__top-text">
SELECT CUSTOM FONTS
</div>
{ isAccordionClosed && (
<div className="custom-font-palette__top-icon">+</div>
) }
{ ! isAccordionClosed && (
<div className="custom-font-palette__top-icon">-</div>
) }
</div>
</div>
);
} );
}

return (
<div ref={ drawerFontOptions } className="theme-fonts--drawer">
<h2>{ __( 'Font Palettes', 'wp-module-onboarding' ) }</h2>
{ /* { selectedFont &&
<div className='theme-fonts--drawer--reset' onClick={resetFonts}>
<div>Reset Button</div>
</div>
} */ }
{ fontPalettes && buildPalettes() }
{ /* { fontPalettes && buildCustomPalette() } */ }
</div>
);
};
Expand Down
Loading

0 comments on commit 1248748

Please sign in to comment.