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

SiteGen: Plugin Installation #395

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 13 additions & 13 deletions includes/RestApi/SiteGenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class SiteGenController {
public function register_routes() {
\register_rest_route(
$this->namespace,
$this->rest_base . '/get-identifiers',
$this->rest_base . '/identifiers',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_valid_identifiers' ),
'callback' => array( $this, 'get_enabled_identifiers' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
)
);
Expand Down Expand Up @@ -78,8 +78,8 @@ public function sitegen_meta_args() {
*
* @return array
*/
public function get_valid_identifiers() {
return array_keys( array_filter( SiteGenService::get_identifiers() ) );
public function get_enabled_identifiers() {
return array_keys( array_filter( SiteGenService::enabled_identifiers() ) );
}

/**
Expand All @@ -90,20 +90,20 @@ public function get_valid_identifiers() {
* @return array|WP_Error
*/
public function generate_sitegen_meta( \WP_REST_Request $request ) {
if ( ! SiteGenService::is_enabled() ) {
return new \WP_Error(
'nfd_onboarding_error',
'SiteGen is Disabled.',
array( 'status' => 404 )
);
}

$site_info = $request->get_param( 'site_info' );
$identifier = $request->get_param( 'identifier' );
$skip_cache = $request->get_param( 'skip_cache' );

if ( SiteGenService::is_enabled() ) {
// TODO Implement the main function and do computations if required.
return SiteGenService::instantiate_site_meta( $site_info, $identifier, $skip_cache );
}
// TODO Implement the main function and do computations if required.
return SiteGenService::instantiate_site_meta( $site_info, $identifier, $skip_cache );

return new \WP_Error(
'sitegen-error',
'SiteGen is Disabled.',
array( 'status' => 404 )
);
}
}
34 changes: 25 additions & 9 deletions includes/Services/PluginService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use NewfoldLabs\WP\Module\Installer\Tasks\PluginActivationTask;
use NewfoldLabs\WP\Module\Installer\Tasks\PluginDeactivationTask;
use NewfoldLabs\WP\Module\Installer\Tasks\PluginInstallTask;
use NewfoldLabs\WP\Module\Onboarding\Data\Data;
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService;
use NewfoldLabs\WP\Module\Onboarding\Data\Plugins;
use NewfoldLabs\WP\Module\Onboarding\Data\SiteFeatures;

Expand All @@ -25,8 +27,18 @@ class PluginService {
*/
public static function initialize() {

// Get the initial list of plugins to be installed based on the plan.
$init_plugins = array_merge( Plugins::get_init(), SiteFeatures::get_init() );
$init_plugins = array();

$flow = Data::current_flow();
if ( 'sitegen' === $flow && SiteGenService::is_enabled() ) {
$init_plugins = SiteGenService::get_plugin_recommendations();
if ( is_wp_error( $init_plugins ) ) {
return $init_plugins;
}
} else {
// Get the initial list of plugins to be installed based on the plan.
$init_plugins = array_merge( Plugins::get_init(), SiteFeatures::get_init() );
}

foreach ( $init_plugins as $init_plugin ) {
$init_plugin_type = PluginInstaller::get_plugin_type( $init_plugin['slug'] );
Expand All @@ -37,14 +49,14 @@ public static function initialize() {
PluginInstallTaskManager::add_to_queue(
new PluginInstallTask(
$init_plugin['slug'],
true,
$init_plugin['activate'],
$init_plugin['priority']
)
);
continue;
}

if ( PluginInstaller::is_active( $init_plugin_path ) ) {
if ( ! $init_plugin['activate'] && PluginInstaller::is_active( $init_plugin_path ) ) {
PluginDeactivationTaskManager::add_to_queue(
new PluginDeactivationTask(
$init_plugin['slug']
Expand Down Expand Up @@ -120,24 +132,28 @@ public static function activate_init_plugins() {
/**
* Sets up a Transient to activate plugins and filter_active_plugins
*
* @return boolean
* @return void
*/
public static function configure_activation_transient() {
global $pagenow;

switch ( $pagenow ) {
case 'index.php':
// If the page is nfd-onboarding
if ( isset( $_GET['page'] ) && WP_Admin::$slug === \sanitize_text_field( $_GET['page'] ) ) {
// If the page is nfd-onboarding. Ignore lint since WP_Admin::$slug is a static string.
// phpcs:ignore
if ( isset( $_GET['page'] ) && ( WP_Admin::$slug === \sanitize_text_field( $_GET['page'] ) ) ) {
if ( '1' !== get_transient( Options::get_option_name( 'filter_active_plugins' ) ) ) {
set_transient( Options::get_option_name( 'filter_active_plugins' ), '1', 20 * MINUTE_IN_SECONDS );
}
}
break;
default:
if ( '1' === get_transient( Options::get_option_name( 'filter_active_plugins' ) ) ) {
delete_transient( Options::get_option_name( 'filter_active_plugins' ) );
self::activate_init_plugins();
$flow = Data::current_flow();
if ( 'sitegen' !== $flow ) {
delete_transient( Options::get_option_name( 'filter_active_plugins' ) );
self::activate_init_plugins();
}
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
getSiteGenIdentifiers,
} from '../../../utils/api/siteGen';
import Footer from '../../Footer';
import { initialize as initializeSettings } from '../../../utils/api/settings';
import { init as initializePlugins } from '../../../utils/api/plugins';
import { init as initializeThemes } from '../../../utils/api/themes';
import { trigger as cronTrigger } from '../../../utils/api/cronTrigger';

// Wrapping the NewfoldInterfaceSkeleton with the HOC to make theme available
const ThemedNewfoldInterfaceSkeleton = themeToggleHOC(
Expand All @@ -36,12 +40,17 @@
}, [ newfoldBrand ] );
const location = useLocation();

const { currentData } = useSelect( ( select ) => {
return {
currentData:
select( nfdOnboardingStore ).getCurrentOnboardingData(),
};
} );
const { currentData, initialize, pluginInstallHash } = useSelect(
( select ) => {
return {
currentData:
select( nfdOnboardingStore ).getCurrentOnboardingData(),
initialize: select( nfdOnboardingStore ).getInitialize(),
pluginInstallHash:
select( nfdOnboardingStore ).getPluginInstallHash(),
};
}
);

const { setCurrentOnboardingData } = useDispatch( nfdOnboardingStore );

Expand Down Expand Up @@ -117,12 +126,24 @@
};
};

useEffect( () => {
if ( initialize ) {
initializePlugins( pluginInstallHash );
setInterval( cronTrigger, 45000 );
}
}, [ initialize ] );

Check warning on line 134 in src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has a missing dependency: 'pluginInstallHash'. Either include it or remove the dependency array

useEffect( () => {
syncStoreToDB();
generateSiteGenData();
handlePreviousStepTracking();
}, [ location.pathname ] );

Check warning on line 140 in src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'generateSiteGenData', 'handlePreviousStepTracking', and 'syncStoreToDB'. Either include them or remove the dependency array

useEffect( () => {
initializeThemes();
initializeSettings();
}, [] );

return (
<ThemeProvider>
<ThemedNewfoldInterfaceSkeleton
Expand Down
25 changes: 18 additions & 7 deletions src/OnboardingSPA/components/StartOptions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import { store as nfdOnboardingStore } from '../../store';

const StartOptions = ( { questionnaire, oldFlow, options } ) => {
const navigate = useNavigate();
const { brandConfig, migrationUrl } = useSelect( ( select ) => {
return {
brandConfig: select( nfdOnboardingStore ).getNewfoldBrandConfig(),
migrationUrl: select( nfdOnboardingStore ).getMigrationUrl(),
};
} );
const { brandConfig, migrationUrl, currentData } = useSelect(
( select ) => {
return {
brandConfig:
select( nfdOnboardingStore ).getNewfoldBrandConfig(),
migrationUrl: select( nfdOnboardingStore ).getMigrationUrl(),
currentData:
select( nfdOnboardingStore ).getCurrentOnboardingData(),
};
}
);
const {
updateAllSteps,
updateTopSteps,
updateRoutes,
updateDesignRoutes,
updateInitialize,
setCurrentOnboardingData,
} = useDispatch( nfdOnboardingStore );

const switchFlow = ( newFlow ) => {
Expand All @@ -35,8 +41,13 @@ const StartOptions = ( { questionnaire, oldFlow, options } ) => {
if ( SITEGEN_FLOW !== currentFlow ) {
window.nfdOnboarding.oldFlow = currentFlow;
}

window.nfdOnboarding.currentFlow = newFlow;
updateInitialize( true );
currentData.activeFlow = newFlow;
setCurrentOnboardingData( currentData );
if ( SITEGEN_FLOW !== newFlow ) {
updateInitialize( true );
}
navigate( data.steps[ 1 ].path );
};
const selectFlow = ( flow ) => {
Expand Down
2 changes: 2 additions & 0 deletions src/OnboardingSPA/steps/SiteGen/Preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
setHeaderActiveView,
setDrawerActiveView,
setCurrentOnboardingData,
updateInitialize,
} = useDispatch( nfdOnboardingStore );

const { currentData, nextStep } = useSelect( ( select ) => {
Expand Down Expand Up @@ -77,8 +78,9 @@
setSidebarActiveView( false );
setHeaderActiveView( HEADER_SITEGEN );
setDrawerActiveView( false );
updateInitialize( true );
loadData();
}, [] );

Check warning on line 83 in src/OnboardingSPA/steps/SiteGen/Preview/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'loadData', 'setDrawerActiveView', 'setHeaderActiveView', 'setIsHeaderEnabled', 'setSidebarActiveView', and 'updateInitialize'. Either include them or remove the dependency array

const handleFavorite = ( slug ) => {
if ( ! ( slug in homepages ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/OnboardingSPA/utils/api/siteGen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { onboardingRestURL } from './common';
export async function getSiteGenIdentifiers() {
return await resolve(
apiFetch( {
url: onboardingRestURL( 'sitegen/get-identifiers' ),
url: onboardingRestURL( 'sitegen/identifiers' ),
} ).then()
);
}
Expand Down