Skip to content

Commit

Permalink
Merge pull request #116 from newfold-labs/PRESS-2-359-Call-Site-Featu…
Browse files Browse the repository at this point in the history
…res-API-in-the-Theme-Generation-Interstitial-State

PRESS 2 359 Call Site Features API in the Theme Generation interstitial state
  • Loading branch information
arunshenoy99 authored Dec 12, 2022
2 parents e317bd4 + bd80ffd commit aa9d3c2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 20 deletions.
7 changes: 0 additions & 7 deletions includes/Services/PluginUninstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ class PluginUninstaller {

public static function uninstall( $plugin ) {

$position_in_queue = PluginInstallTaskManager::status( $plugin );
if ( $position_in_queue !== false && $position_in_queue !== 0 ) {
PluginInstallTaskManager::remove_from_queue(
$plugin,
);
}

$plugin_list = Plugins::get_squashed();
// Gets the specified path for the Plugin from the predefined list
$plugin_path = $plugin_list[ $plugin ]['path'];
Expand Down
18 changes: 18 additions & 0 deletions includes/TaskManagers/PluginUninstallTaskManager.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace NewfoldLabs\WP\Module\Onboarding\TaskManagers;

use NewfoldLabs\WP\Module\Onboarding\Data\Plugins;
use NewfoldLabs\WP\Module\Onboarding\Data\Options;
use NewfoldLabs\WP\Module\Onboarding\Models\PriorityQueue;
use NewfoldLabs\WP\Module\Onboarding\Tasks\PluginUninstallTask;
use NewfoldLabs\WP\Module\Onboarding\Services\PluginUninstaller;

/**
* Manages the execution of PluginUninstallTasks.
Expand Down Expand Up @@ -109,6 +111,22 @@ public static function add_to_queue( PluginUninstallTask $plugin_uninstall_task
converted to an associative array before storing it in the option. */
$plugins = \get_option( Options::get_option_name( self::$queue_name ), array() );

$position_in_queue = PluginInstallTaskManager::status( $plugin_uninstall_task->get_slug() );
if ( $position_in_queue !== false && $position_in_queue !== 0 ) {
PluginInstallTaskManager::remove_from_queue(
$plugin_uninstall_task->get_slug(),
);

return true;
}

$plugin_list = Plugins::get_squashed();
// Gets the specified path for the Plugin from the predefined list
$plugin_path = $plugin_list[ $plugin_uninstall_task->get_slug() ]['path'];

if (!PluginUninstaller::is_plugin_installed($plugin_path))
return true;

$queue = new PriorityQueue();
foreach ( $plugins as $queued_plugin ) {

Expand Down
57 changes: 44 additions & 13 deletions src/OnboardingSPA/pages/Steps/Complete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';
import { useNavigate } from 'react-router-dom';

import { StepLoader } from '../../../components/Loaders';
import getContents from './contents';
import { completeFlow } from '../../../utils/api/flow';
import { StepLoader } from '../../../components/Loaders';
import { setSiteFeatures } from '../../../utils/api/plugins';
import { StepErrorState } from '../../../components/ErrorState';
import getContents from './contents';
import { DesignStateHandler } from '../../../components/StateHandlers';

const StepComplete = () => {
Expand All @@ -16,24 +17,54 @@ const StepComplete = () => {
const navigate = useNavigate();
const [ isError, setIsError ] = useState( false );

const { nextStep, brandName } = useSelect( ( select ) => {
return {
nextStep: select( nfdOnboardingStore ).getNextStep(),
brandName: select( nfdOnboardingStore ).getNewfoldBrandName(),
};
}, [] );
const { nextStep, brandName, currentData, pluginInstallHash } = useSelect(
( select ) => {
return {
nextStep: select( nfdOnboardingStore ).getNextStep(),
brandName: select( nfdOnboardingStore ).getNewfoldBrandName(),
currentData:
select( nfdOnboardingStore ).getCurrentOnboardingData(),
pluginInstallHash:
select( nfdOnboardingStore ).getPluginInstallHash(),
};
},
[]
);

const contents = getContents( brandName );

const checkFlowComplete = async () => {
const flowCompletionResponse = await completeFlow();
if ( flowCompletionResponse?.error ) {
setIsHeaderNavigationEnabled( true );
return setIsError( true );
}
await Promise.all( [ completeFlowRequest(), setSiteFeaturesRequest() ] ).then(
( values ) =>
values.forEach( ( value ) => {
// If any Request returns False then Show Error
if ( ! value ) {
setIsHeaderNavigationEnabled( true );
return setIsError( true );
}
} )
);

navigate( nextStep.path );
};

async function completeFlowRequest() {
const flowCompletionResponse = await completeFlow();
if ( flowCompletionResponse?.error ) return false;
return true;
}

async function setSiteFeaturesRequest() {
if ( Array.isArray( currentData?.data?.siteFeatures ) ) return true;

const siteFeaturesResponse = await setSiteFeatures( pluginInstallHash, {
plugins: currentData?.data?.siteFeatures,
} );
if ( siteFeaturesResponse?.error ) return false;

return true;
}

useEffect( () => {
setIsHeaderNavigationEnabled( false );
setIsDrawerSuppressed( true );
Expand Down
10 changes: 10 additions & 0 deletions src/OnboardingSPA/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,13 @@ export function getStepPreviewData( state ) {
export function getHeaderMenuData( state ) {
return state.header.menu;
}

/**
* Gets the Plugin Install Hash for security
*
* @param {*} state
* @return string
*/
export function getPluginInstallHash( state ) {
return state.runtime.pluginInstallHash;
}
13 changes: 13 additions & 0 deletions src/OnboardingSPA/utils/api/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ export const getSiteFeatures = async () => {
} )
);
};

export const setSiteFeatures = async ( pluginInstallHash, data ) => {
return await resolve(
apiFetch( {
url: onboardingRestURL( 'plugins/site-features' ),
method: 'POST',
headers: {
'X-NFD-ONBOARDING': pluginInstallHash,
},
data,
} )
);
};

0 comments on commit aa9d3c2

Please sign in to comment.