diff --git a/includes/Data/Data.php b/includes/Data/Data.php
index ea316b139..bba53189e 100644
--- a/includes/Data/Data.php
+++ b/includes/Data/Data.php
@@ -80,6 +80,15 @@ public static function current_plan() {
}
}
+ $current_flow = Flows::get_flow_from_top_priority();
+ if ( false !== $current_flow ) {
+ return array(
+ 'flow' => 'ecommerce',
+ 'subtype' => 'wc_priority',
+ 'type' => null,
+ );
+ }
+
return array(
'flow' => Flows::get_default_flow(),
'subtype' => null,
@@ -93,24 +102,8 @@ public static function current_plan() {
* @return string
*/
public static function current_flow() {
-
- $current_flow = Flows::get_flow_from_params();
- if ( false !== $current_flow ) {
- return $current_flow;
- }
-
- $current_flow = Flows::get_flow_from_plugins();
- if ( false !== $current_flow ) {
- return $current_flow;
- }
-
- $customer_data = self::customer_data();
- $current_flow = Flows::get_flow_from_customer_data( $customer_data );
- if ( false !== $current_flow ) {
- return $current_flow;
- }
-
- return Flows::get_default_flow();
+ $current_plan = self::current_plan();
+ return $current_plan['flow'];
}
/**
diff --git a/includes/Data/Flows.php b/includes/Data/Flows.php
index 128b78bce..7a531aabf 100644
--- a/includes/Data/Flows.php
+++ b/includes/Data/Flows.php
@@ -1,6 +1,7 @@
'jetpack/jetpack.php',
),
'woocommerce' => array(
- 'approved' => true,
- 'path' => 'woocommerce/woocommerce.php',
+ 'approved' => true,
+ 'path' => 'woocommerce/woocommerce.php',
+ 'post_install_callback' => array( __CLASS__, 'wc_prevent_redirect_on_activation' ),
),
'wordpress-seo' => array(
'approved' => true,
@@ -420,4 +421,13 @@ public static function get_init() {
return $init_list;
}
+ /**
+ * Prevent redirect to woo wizard after activation of woocommerce.
+ *
+ * @return void
+ */
+ public static function wc_prevent_redirect_on_activation() {
+ \delete_transient( '_wc_activation_redirect' );
+ }
+
}
diff --git a/includes/Data/Preview.php b/includes/Data/Preview.php
index c4f7930b8..886994f35 100644
--- a/includes/Data/Preview.php
+++ b/includes/Data/Preview.php
@@ -4,12 +4,25 @@
use NewfoldLabs\WP\Module\Onboarding\Services\PluginInstaller;
use NewfoldLabs\WP\Module\Onboarding\Services\ThemeInstaller;
+/**
+ * Class Preview
+ */
final class Preview {
-
+ /**
+ * Convert boolean to plugin/theme status.
+ *
+ * @param boolean $boolean The boolean value.
+ * @return string
+ */
private static function boolean_to_status( $boolean ) {
- return $boolean ? 'activated' : 'init';
+ return $boolean ? 'activated' : 'init';
}
+ /**
+ * Map of pre requisites to show the live preview successfully for a flow.
+ *
+ * @return array
+ */
private static function pre_requisites() {
$theme_map = Themes::get();
return array(
@@ -30,21 +43,35 @@ private static function pre_requisites() {
);
}
- public static function get_pre_requisites() {
- $pre_requisites = self::pre_requisites();
- return isset( $pre_requisites[ Data::current_flow() ] ) ? $pre_requisites[ Data::current_flow() ] : array();
+ /**
+ * Get the pre requisites for a given flow.
+ *
+ * @param string $flow A valid Onboarding flow.
+ * @return array
+ */
+ public static function get_pre_requisites( $flow = null ) {
+ $pre_requisites = self::pre_requisites();
+ if ( ! isset( $flow ) ) {
+ $flow = Data::current_flow();
+ }
+ return isset( $pre_requisites[ $flow ] ) ? $pre_requisites[ $flow ] : array();
}
+ /**
+ * Get all the settings necessary to load the live preview
+ *
+ * @return array
+ */
public static function get_settings() {
- $block_editor_context = new \WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
- $custom_settings = array(
- 'siteUrl' => \site_url(),
- );
+ $block_editor_context = new \WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
+ $custom_settings = array(
+ 'siteUrl' => \site_url(),
+ );
- return array(
- 'settings' => \get_block_editor_settings( $custom_settings, $block_editor_context ),
- 'globalStyles' => \wp_get_global_styles(),
- 'preRequisites' => self::get_pre_requisites(),
- );
+ return array(
+ 'settings' => \get_block_editor_settings( $custom_settings, $block_editor_context ),
+ 'globalStyles' => \wp_get_global_styles(),
+ 'preRequisites' => self::get_pre_requisites(),
+ );
}
}
diff --git a/includes/RestApi/FlowController.php b/includes/RestApi/FlowController.php
index 363ebd113..34fed044b 100644
--- a/includes/RestApi/FlowController.php
+++ b/includes/RestApi/FlowController.php
@@ -1,11 +1,8 @@
namespace,
+ $this->rest_base . '/switch',
+ array(
+ array(
+ 'methods' => \WP_REST_Server::CREATABLE,
+ 'callback' => array( $this, 'switch' ),
+ 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
+ 'args' => $this->get_switch_args(),
+ ),
+ )
+ );
}
/**
- * Fetch onboarding flow details from database.
+ * Get the valid request params for the switch endpoint.
*
- * @param \WP_REST_Request $request Request model.
+ * @return array
+ */
+ public function get_switch_args() {
+ return array(
+ 'flow' => array(
+ 'required' => true,
+ 'type' => 'string',
+ 'sanitize_callback' => 'sanitize_text_field',
+ ),
+ );
+ }
+
+ /**
+ * Get Onboarding flow data.
*
* @return \WP_REST_Response
*/
- public function get_onboarding_flow_data( \WP_REST_Request $request ) {
- // check if data is available in the database if not then fetch the default dataset
- $result = $this->read_details_from_wp_options();
- if ( ! $result ) {
- $result = Flows::get_data();
- $result['createdAt'] = time();
- // update default data if flow type is ecommerce
- $result = $this->update_default_data_for_ecommerce( $result );
- $this->save_details_to_wp_options( $result );
- }
-
+ public function get_onboarding_flow_data() {
return new \WP_REST_Response(
- $result,
+ FlowService::get_flow_data(),
200
);
}
/**
- * Save / Update onboarding flow details to database.
+ * Update the Onboarding flow data.
*
- * @param \WP_REST_Request $request Request model.
+ * @param \WP_REST_Request $request The incoming request.
*
- * @return \WP_REST_Response|\WP_Error
+ * @return \WP_Error|\WP_REST_Response
*/
public function save_onboarding_flow_data( \WP_REST_Request $request ) {
- $flow_data = array();
- $params = json_decode( $request->get_body(), true );
-
- if ( is_null( $params ) ) {
- return new \WP_Error(
- 'no_post_data',
- 'No Data Provided',
- array( 'status' => 404 )
- );
- }
-
- $flow_data = $this->read_details_from_wp_options();
- if ( ! $flow_data ) {
- $flow_data = Flows::get_data();
- $flow_data['createdAt'] = time();
- // update default data if flow type is ecommerce
- $flow_data = $this->update_default_data_for_ecommerce( $flow_data );
- $this->save_details_to_wp_options( $flow_data );
- }
-
- foreach ( $params as $key => $param ) {
- $value = $this->array_search_key( $key, $flow_data );
- if ( false === $value ) {
- return new \WP_Error(
- 'wrong_param_provided',
- "Wrong Parameter Provided : $key",
- array( 'status' => 404 )
- );
- }
- }
-
- $flow_data = array_replace_recursive( $flow_data, $params );
-
- // update timestamp once data is updated
- $flow_data['updatedAt'] = time();
-
- // Update Blog Information from Basic Info
- if ( ( ! empty( $flow_data['data']['blogName'] ) ) ) {
- \update_option( Options::get_option_name( 'blog_name', false ), $flow_data['data']['blogName'] );
- }
-
- if ( ( ! empty( $flow_data['data']['blogDescription'] ) ) ) {
- \update_option( Options::get_option_name( 'blog_description', false ), $flow_data['data']['blogDescription'] );
- }
-
- if ( ( ! empty( $flow_data['data']['siteLogo'] ) ) && ! empty( $flow_data['data']['siteLogo']['id'] ) ) {
- \update_option( Options::get_option_name( 'site_icon', false ), $flow_data['data']['siteLogo']['id'] );
- \update_option( Options::get_option_name( 'site_logo', false ), $flow_data['data']['siteLogo']['id'] );
- } else {
- \update_option( Options::get_option_name( 'site_icon', false ), 0 );
- \delete_option( Options::get_option_name( 'site_logo', false ) );
- }
+ $params = json_decode( $request->get_body(), true );
- // save data to database
- if ( ! $this->update_wp_options_data_in_database( $flow_data ) ) {
- return new \WP_Error(
- 'database_update_failed',
- 'There was an error saving the data',
- array( 'status' => 404 )
- );
+ $flow_data = FlowService::update_flow_data( $params );
+ if ( \is_wp_error( $flow_data ) ) {
+ return $flow_data;
}
return new \WP_REST_Response(
@@ -162,82 +120,7 @@ public function save_onboarding_flow_data( \WP_REST_Request $request ) {
}
/**
- * Check the current flow type and update default data if flowtype is ecommerce.
- *
- * @param array $data default blueprint flow data.
- *
- * @return array
- */
- private function update_default_data_for_ecommerce( $data ) {
- // get current flow type
- $flow_type = Data::current_flow();
- if ( 'ecommerce' === $flow_type ) {
- // update default data with ecommerce data
- $data['data']['topPriority']['priority1'] = 'selling';
- $data['data']['siteType'] = array(
- 'label' => '',
- 'referTo' => 'business',
- );
- }
- return $data;
- }
-
- /**
- * Read onboarding flow options from database
- *
- * @return array
- */
- public function read_details_from_wp_options() {
- return \get_option( Options::get_option_name( 'flow' ) );
- }
-
- /**
- * Add onboarding flow options
- *
- * @param array $data default blueprint flow data.
- *
- * @return array
- */
- private function save_details_to_wp_options( $data ) {
- return \add_option( Options::get_option_name( 'flow' ), $data );
- }
-
- /**
- * Update onboarding flow options
- *
- * @param array $data default blueprint flow data.
- *
- * @return array
- */
- private function update_wp_options_data_in_database( $data ) {
- return \update_option( Options::get_option_name( 'flow' ), $data );
- }
-
- /**
- * Function to search for key in array recursively with case sensitive exact match
- *
- * @param array $needle_key specific key in flow data.
- * @param array $array WP Options Data.
- *
- * @return boolean
- */
- private function array_search_key( $needle_key, $array ) {
- foreach ( $array as $key => $value ) {
- if ( strcmp( $key, $needle_key ) === 0 ) {
- return true;
- }
- if ( is_array( $value ) ) {
- $result = $this->array_search_key( $needle_key, $value );
- if ( false !== $result ) {
- return $result;
- }
- }
- }
- return false;
- }
-
- /**
- * Flow completion API for child theme generation, verify child theme and publish site pages
+ * Flow completion API for child theme generation, verify child theme and publish site pages.
*
* @return \WP_REST_Response
*/
@@ -264,4 +147,23 @@ public function complete() {
201
);
}
+
+ /**
+ * Switch the Onboarding flow.
+ *
+ * @param \WP_REST_Request $request The incoming switch request.
+ * @return \WP_Error|\WP_REST_Response
+ */
+ public function switch( \WP_REST_Request $request ) {
+ $flow = $request->get_param( 'flow' );
+ $status = FlowService::switch_flow( $flow );
+ if ( \is_wp_error( $status ) ) {
+ return $status;
+ }
+
+ return new \WP_REST_Response(
+ array(),
+ 200
+ );
+ }
}
diff --git a/includes/Services/FlowService.php b/includes/Services/FlowService.php
new file mode 100644
index 000000000..e5edc09c5
--- /dev/null
+++ b/includes/Services/FlowService.php
@@ -0,0 +1,202 @@
+ 404 )
+ );
+ }
+
+ $flow_data = self::get_flow_data();
+
+ foreach ( $params as $key => $param ) {
+ $value = self::array_search_key( $key, $flow_data );
+ if ( false === $value ) {
+ return new \WP_Error(
+ 'wrong_param_provided',
+ "Wrong Parameter Provided : $key",
+ array( 'status' => 404 )
+ );
+ }
+ }
+
+ $flow_data = array_replace_recursive( $flow_data, $params );
+
+ // Update timestamp everytime the Onboarding flow data is updated.
+ $flow_data['updatedAt'] = time();
+
+ // Update Blog Information from Basic Info
+ if ( ( ! empty( $flow_data['data']['blogName'] ) ) ) {
+ \update_option( Options::get_option_name( 'blog_name', false ), $flow_data['data']['blogName'] );
+ }
+
+ if ( ( ! empty( $flow_data['data']['blogDescription'] ) ) ) {
+ \update_option( Options::get_option_name( 'blog_description', false ), $flow_data['data']['blogDescription'] );
+ }
+
+ if ( ( ! empty( $flow_data['data']['siteLogo'] ) ) && ! empty( $flow_data['data']['siteLogo']['id'] ) ) {
+ \update_option( Options::get_option_name( 'site_icon', false ), $flow_data['data']['siteLogo']['id'] );
+ \update_option( Options::get_option_name( 'site_logo', false ), $flow_data['data']['siteLogo']['id'] );
+ } else {
+ \update_option( Options::get_option_name( 'site_icon', false ), 0 );
+ \delete_option( Options::get_option_name( 'site_logo', false ) );
+ }
+
+ if ( ! self::update_data_in_wp_option( $flow_data ) ) {
+ return new \WP_Error(
+ 'database_update_failed',
+ 'There was an error saving the data',
+ array( 'status' => 404 )
+ );
+ }
+
+ return $flow_data;
+ }
+
+ /**
+ * Switch the Onboarding flow.
+ *
+ * @param string $flow A valid Onboarding flow for a brand.
+ * @return \WP_Error|boolean
+ */
+ public static function switch_flow( $flow ) {
+ // Get all the enabled flows for a brand.
+ $enabled_flows = Flows::get_flows();
+ // If the request flow does not exist or is not enabled then return an error.
+ if ( ! isset( $enabled_flows[ $flow ] ) || true !== $enabled_flows[ $flow ] ) {
+ return new \WP_Error(
+ 'nfd_onboarding_error',
+ 'Flow not enabled.',
+ array( 'status' => 400 )
+ );
+ }
+
+ // Reset the Plugin Install Status and Queue.
+ PluginInstallTaskManager::reset_install_status();
+
+ // Get the pre requisites for a flow.
+ $pre_requisites = Preview::get_pre_requisites( $flow );
+ if ( ! isset( $pre_requisites ) || ! isset( $pre_requisites['plugins'] ) ) {
+ return true;
+ }
+
+ // Install and activate all the required plugins.
+ foreach ( $pre_requisites['plugins'] as $plugin => $active ) {
+ // Skip if the plugin installation if it is already active.
+ if ( 'activated' === $active ) {
+ continue;
+ }
+
+ $plugin_install_task = new PluginInstallTask( $plugin, true );
+ $status = $plugin_install_task->execute();
+
+ if ( \is_wp_error( $status ) ) {
+ return $status;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Read Onboarding flow data from the wp_option.
+ *
+ * @return array
+ */
+ public static function read_data_from_wp_option() {
+ return \get_option( Options::get_option_name( 'flow' ), false );
+ }
+
+ /**
+ * Update flow data params if the current flow is ecommerce.
+ *
+ * @param array $data The flow data.
+ *
+ * @return array
+ */
+ private static function update_data_for_ecommerce( $data ) {
+ // get current flow type
+ $flow_type = Data::current_flow();
+ if ( 'ecommerce' === $flow_type ) {
+ // update default data with ecommerce data
+ $data['data']['topPriority']['priority1'] = 'selling';
+ $data['data']['siteType'] = array(
+ 'label' => '',
+ 'referTo' => 'business',
+ );
+ }
+ return $data;
+ }
+
+ /**
+ * Update Onboarding flow data in the wp_option.
+ *
+ * @param array $data default blueprint flow data.
+ *
+ * @return array
+ */
+ private static function update_data_in_wp_option( $data ) {
+ return \update_option( Options::get_option_name( 'flow' ), $data );
+ }
+
+ /**
+ * Search for $needle_key in $array recursively.
+ *
+ * @param string $needle_key The key to be searched for.
+ * @param array $array The array in which the search occurs.
+ *
+ * @return boolean
+ */
+ private static function array_search_key( $needle_key, $array ) {
+ foreach ( $array as $key => $value ) {
+ if ( strcmp( $key, $needle_key ) === 0 ) {
+ return true;
+ }
+ if ( is_array( $value ) ) {
+ $result = self::array_search_key( $needle_key, $value );
+ if ( false !== $result ) {
+ return $result;
+ }
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/includes/Services/PluginInstaller.php b/includes/Services/PluginInstaller.php
index 5dc155e39..89d2eb497 100644
--- a/includes/Services/PluginInstaller.php
+++ b/includes/Services/PluginInstaller.php
@@ -2,10 +2,20 @@
namespace NewfoldLabs\WP\Module\Onboarding\Services;
use NewfoldLabs\WP\Module\Onboarding\Data\Plugins;
-use NewfoldLabs\WP\Module\Onboarding\Data\Options;
+/**
+ * Class PluginInstaller
+ */
class PluginInstaller {
+ /**
+ * Install a whitelisted plugin.
+ *
+ * @param string $plugin The plugin slug from Plugins.php.
+ * @param boolean $activate Whether to activate the plugin after install.
+ *
+ * @return \WP_Error|\WP_REST_Response
+ */
public static function install( $plugin, $activate ) {
$plugins_list = Plugins::get();
@@ -36,23 +46,23 @@ public static function install( $plugin, $activate ) {
// If it is not a zip URL then check if it is an approved slug.
$plugin = \sanitize_text_field( $plugin );
if ( self::is_nfd_slug( $plugin ) ) {
- // [TODO] Better handle mu-plugins and direct file downloads.
- if ( $plugin === 'nfd_slug_endurance_page_cache' ) {
+ // [TODO] Better handle mu-plugins and direct file downloads.
+ if ( 'nfd_slug_endurance_page_cache' === $plugin ) {
return self::install_endurance_page_cache();
}
- $plugin_path = $plugins_list['nfd_slugs'][ $plugin ]['path'];
+ $plugin_path = $plugins_list['nfd_slugs'][ $plugin ]['path'];
if ( ! self::is_plugin_installed( $plugin_path ) ) {
- $status = self::install_from_zip( $plugins_list['nfd_slugs'][ $plugin ]['url'], $activate );
+ $status = self::install_from_zip( $plugins_list['nfd_slugs'][ $plugin ]['url'], $activate );
if ( \is_wp_error( $status ) ) {
return $status;
}
}
if ( $activate && ! \is_plugin_active( $plugin_path ) ) {
- $status = \activate_plugin( $plugin_path );
+ $status = \activate_plugin( $plugin_path );
if ( \is_wp_error( $status ) ) {
- $status->add_data( array( 'status' => 500 ) );
+ $status->add_data( array( 'status' => 500 ) );
- return $status;
+ return $status;
}
}
return new \WP_REST_Response(
@@ -69,20 +79,26 @@ public static function install( $plugin, $activate ) {
);
}
- $plugin_path = $plugins_list['wp_slugs'][ $plugin ]['path'];
+ $plugin_path = $plugins_list['wp_slugs'][ $plugin ]['path'];
+ $plugin_post_install_callback = isset( $plugins_list['wp_slugs'][ $plugin ]['post_install_callback'] )
+ ? $plugins_list['wp_slugs'][ $plugin ]['post_install_callback']
+ : false;
if ( ! self::is_plugin_installed( $plugin_path ) ) {
- $status = self::install_from_wordpress( $plugin, $activate );
+ $status = self::install_from_wordpress( $plugin, $activate );
if ( \is_wp_error( $status ) ) {
- return $status;
+ return $status;
+ }
+ if ( is_callable( $plugin_post_install_callback ) ) {
+ $plugin_post_install_callback();
}
}
if ( $activate && ! \is_plugin_active( $plugin_path ) ) {
- $status = \activate_plugin( $plugin_path );
+ $status = \activate_plugin( $plugin_path );
if ( \is_wp_error( $status ) ) {
- $status->add_data( array( 'status' => 500 ) );
+ $status->add_data( array( 'status' => 500 ) );
- return $status;
+ return $status;
}
}
@@ -93,12 +109,14 @@ public static function install( $plugin, $activate ) {
}
/**
- * @param string $slug Representing the wordpress.org slug.
+ * Install a plugin from wordpress.org.
*
+ * @param string $plugin The wp_slug to install.
+ * @param boolean $activate Whether to activate the plugin after install.
* @return \WP_REST_Response|\WP_Error
*/
public static function install_from_wordpress( $plugin, $activate ) {
- require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
+ require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
$api = \plugins_api(
'plugin_information',
@@ -121,9 +139,9 @@ public static function install_from_wordpress( $plugin, $activate ) {
return $api;
}
- $status = self::install_from_zip( $api->download_link, $activate );
+ $status = self::install_from_zip( $api->download_link, $activate );
if ( \is_wp_error( $status ) ) {
- return $status;
+ return $status;
}
return new \WP_REST_Response(
@@ -133,8 +151,10 @@ public static function install_from_wordpress( $plugin, $activate ) {
}
/**
- * @param string $url URL to the zip for the plugin.
+ * Install the plugin from a custom ZIP.
*
+ * @param string $url The ZIP URL to install from.
+ * @param boolean $activate Whether to activate the plugin after install.
* @return \WP_REST_Response|\WP_Error
*/
public static function install_from_zip( $url, $activate ) {
@@ -192,11 +212,11 @@ public static function install_from_zip( $url, $activate ) {
}
if ( $activate && ! \is_plugin_active( $plugin_file ) ) {
- $status = \activate_plugin( $plugin_file );
+ $status = \activate_plugin( $plugin_file );
if ( \is_wp_error( $status ) ) {
- $status->add_data( array( 'status' => 500 ) );
+ $status->add_data( array( 'status' => 500 ) );
- return $status;
+ return $status;
}
}
@@ -207,25 +227,23 @@ public static function install_from_zip( $url, $activate ) {
}
/**
- * @param string $plugin Slug of the plugin.
- *
* Checks if a given slug is a valid nfd_slug. Ref: includes/Data/Plugins.php for nfd_slug.
*
+ * @param string $plugin Slug of the plugin.
* @return boolean
*/
public static function is_nfd_slug( $plugin ) {
- $plugins_list = Plugins::get();
+ $plugins_list = Plugins::get();
if ( isset( $plugins_list['nfd_slugs'][ $plugin ]['approved'] ) ) {
- return true;
+ return true;
}
- return false;
+ return false;
}
/**
- * @param string $plugin_path Path to the plugin's header file.
- *
* Determines if a plugin has already been installed.
*
+ * @param string $plugin_path Path to the plugin's header file.
* @return boolean
*/
public static function is_plugin_installed( $plugin_path ) {
@@ -241,50 +259,51 @@ public static function is_plugin_installed( $plugin_path ) {
}
/**
- * @param string $plugin
+ * Get the type of plugin slug. Ref: includes/Data/Plugins.php for the different types.
*
- * @return string Type of plugin. Ref: includes/Data/Plugins.php for the different types.
+ * @param string $plugin The plugin slug to retrieve the type.
+ * @return string
*/
public static function get_plugin_type( $plugin ) {
if ( \wp_http_validate_url( $plugin ) ) {
- return 'urls';
+ return 'urls';
}
if ( self::is_nfd_slug( $plugin ) ) {
- return 'nfd_slugs';
+ return 'nfd_slugs';
}
- return 'wp_slugs';
+ return 'wp_slugs';
}
/**
- * @param string $plugin
- * @param string $plugin_type
+ * Get the path to the Plugin's header file.
*
- * @return string Path to the Plugin's header file.
+ * @param string $plugin The slug of the plugin.
+ * @param string $plugin_type The type of plugin.
+ * @return string
*/
public static function get_plugin_path( $plugin, $plugin_type ) {
- $plugin_list = Plugins::get();
- return $plugin_list[ $plugin_type ][ $plugin ]['path'];
+ $plugin_list = Plugins::get();
+ return $plugin_list[ $plugin_type ][ $plugin ]['path'];
}
/**
- * @param string $plugin
- * @param string $activate
- *
* Checks if a plugin with the given slug and activation criteria already exists.
*
+ * @param string $plugin The slug of the plugin to check for
+ * @param boolean $activate The activation criteria.
* @return boolean
*/
public static function exists( $plugin, $activate ) {
- $plugin_type = self::get_plugin_type( $plugin );
- $plugin_path = self::get_plugin_path( $plugin, $plugin_type );
+ $plugin_type = self::get_plugin_type( $plugin );
+ $plugin_path = self::get_plugin_path( $plugin, $plugin_type );
if ( ! self::is_plugin_installed( $plugin_path ) ) {
- return false;
+ return false;
}
if ( $activate && ! \is_plugin_active( $plugin_path ) ) {
- return false;
+ return false;
}
- return true;
+ return true;
}
/**
@@ -303,11 +322,11 @@ public static function install_endurance_page_cache() {
);
}
- global $wp_filesystem;
+ global $wp_filesystem;
- $plugin_list = Plugins::get();
- $plugin_url = $plugin_list['nfd_slugs']['nfd_slug_endurance_page_cache']['url'];
- $plugin_path = $plugin_list['nfd_slugs']['nfd_slug_endurance_page_cache']['path'];
+ $plugin_list = Plugins::get();
+ $plugin_url = $plugin_list['nfd_slugs']['nfd_slug_endurance_page_cache']['url'];
+ $plugin_path = $plugin_list['nfd_slugs']['nfd_slug_endurance_page_cache']['path'];
if ( $wp_filesystem->exists( $plugin_path ) ) {
return new \WP_REST_Response(
@@ -317,15 +336,15 @@ public static function install_endurance_page_cache() {
}
if ( ! $wp_filesystem->is_dir( WP_CONTENT_DIR . '/mu-plugins' ) ) {
- $wp_filesystem->mkdir( WP_CONTENT_DIR . '/mu-plugins' );
+ $wp_filesystem->mkdir( WP_CONTENT_DIR . '/mu-plugins' );
}
- $request = \wp_remote_get( $plugin_url );
+ $request = \wp_remote_get( $plugin_url );
if ( \is_wp_error( $request ) ) {
- return $request;
+ return $request;
}
- $wp_filesystem->put_contents( $plugin_path, $request['body'], FS_CHMOD_FILE );
+ $wp_filesystem->put_contents( $plugin_path, $request['body'], FS_CHMOD_FILE );
return new \WP_REST_Response(
array(),
@@ -343,7 +362,7 @@ protected static function connect_to_filesystem() {
// We want to ensure that the user has direct access to the filesystem.
$access_type = \get_filesystem_method();
- if ( $access_type !== 'direct' ) {
+ if ( 'direct' !== $access_type ) {
return false;
}
diff --git a/includes/TaskManagers/PluginInstallTaskManager.php b/includes/TaskManagers/PluginInstallTaskManager.php
index c03014281..f5e1d7e4f 100644
--- a/includes/TaskManagers/PluginInstallTaskManager.php
+++ b/includes/TaskManagers/PluginInstallTaskManager.php
@@ -12,16 +12,24 @@
*/
class PluginInstallTaskManager {
- /**
- * The number of times a PluginInstallTask can be retried.
- *
- * @var int
- */
+ /**
+ * The number of times a PluginInstallTask can be retried.
+ *
+ * @var int
+ */
private static $retry_limit = 1;
+ /**
+ * The name of the queue, might be prefixed.
+ *
+ * @var string
+ */
private static $queue_name = 'plugin_install_queue';
- function __construct() {
+ /**
+ * Schedules the crons.
+ */
+ public function __construct() {
// Ensure there is a thirty second option in the cron schedules
add_filter( 'cron_schedules', array( $this, 'add_thirty_seconds_schedule' ) );
@@ -34,10 +42,21 @@ function __construct() {
}
}
+ /**
+ * Returns the queue name, might be prefixed.
+ *
+ * @return string
+ */
public static function get_queue_name() {
- return self::$queue_name;
+ return self::$queue_name;
}
+ /**
+ * Adds a 30 second cron schedule.
+ *
+ * @param array $schedules The existing cron schedule.
+ * @return array
+ */
public function add_thirty_seconds_schedule( $schedules ) {
if ( ! array_key_exists( 'thirty_seconds', $schedules ) || 30 !== $schedules['thirty_seconds']['interval'] ) {
$schedules['thirty_seconds'] = array(
@@ -46,9 +65,14 @@ public function add_thirty_seconds_schedule( $schedules ) {
);
}
- return $schedules;
+ return $schedules;
}
+ /**
+ * Queues the initial list of Plugin Installs for a flow.
+ *
+ * @return boolean
+ */
public static function queue_initial_installs() {
// Checks if the init_list of plugins have already been queued.
@@ -57,7 +81,7 @@ public static function queue_initial_installs() {
}
// Set option to installing to prevent re-queueing the init_list again on page load.
- \update_option( Options::get_option_name( 'plugins_init_status' ), 'installing' );
+ \update_option( Options::get_option_name( 'plugins_init_status' ), 'installing' );
// Get the initial list of plugins to be installed based on the plan.
$init_plugins = Plugins::get_init();
@@ -86,17 +110,19 @@ public static function queue_initial_installs() {
*/
public function install() {
/*
- Get the plugins queued up to be installed, the PluginInstall task gets
- converted to an associative array before storing it in the option. */
+ Get the plugins queued up to be installed, the PluginInstall task gets
+ converted to an associative array before storing it in the option.
+ */
$plugins = \get_option( Options::get_option_name( self::$queue_name ), array() );
/*
- Conversion of the max heap to an array will always place the PluginInstallTask with the highest
- priority at the beginning of the array */
+ Conversion of the max heap to an array will always place the PluginInstallTask with the highest
+ priority at the beginning of the array
+ */
$plugin_to_install = array_shift( $plugins );
// Update the plugin install queue.
- \update_option( Options::get_option_name( self::$queue_name ), $plugins );
+ \update_option( Options::get_option_name( self::$queue_name ), $plugins );
// Recreate the PluginInstall task from the associative array.
$plugin_install_task = new PluginInstallTask(
@@ -113,15 +139,16 @@ public function install() {
$status = $plugin_install_task->execute();
if ( \is_wp_error( $status ) ) {
- // If there is an error, then increase the retry count for the task.
- $plugin_install_task->increment_retries();
+ // If there is an error, then increase the retry count for the task.
+ $plugin_install_task->increment_retries();
- // Get Latest Value of the install queue
- $plugins = \get_option( Options::get_option_name( self::$queue_name ), array() );
+ // Get Latest Value of the install queue
+ $plugins = \get_option( Options::get_option_name( self::$queue_name ), array() );
- /*
- If the number of retries have not exceeded the limit
- then re-queue the task at the end of the queue to be retried. */
+ /*
+ If the number of retries have not exceeded the limit
+ then re-queue the task at the end of the queue to be retried.
+ */
if ( $plugin_install_task->get_retries() <= self::$retry_limit ) {
array_push( $plugins, $plugin_install_task->to_array() );
@@ -139,30 +166,30 @@ public function install() {
}
/**
- * @param PluginInstallTask $plugin_install_task
- *
* Adds a new PluginInstallTask to the Plugin Install queue.
* The Task will be inserted at an appropriate position in the queue based on it's priority.
*
+ * @param PluginInstallTask $plugin_install_task The task to be inserted.
* @return array|false
*/
public static function add_to_queue( PluginInstallTask $plugin_install_task ) {
/*
- Get the plugins queued up to be installed, the PluginInstall task gets
- converted to an associative array before storing it in the option. */
+ Get the plugins queued up to be installed, the PluginInstall task gets
+ converted to an associative array before storing it in the option.
+ */
$plugins = \get_option( Options::get_option_name( self::$queue_name ), array() );
$queue = new PriorityQueue();
foreach ( $plugins as $queued_plugin ) {
-
/*
- Check if there is an already existing PluginInstallTask in the queue
- for a given slug and activation criteria. */
+ Check if there is an already existing PluginInstallTask in the queue
+ for a given slug and activation criteria.
+ */
if ( $queued_plugin['slug'] === $plugin_install_task->get_slug()
- && $queued_plugin['activate'] === $plugin_install_task->get_activate() ) {
- return false;
+ && $queued_plugin['activate'] === $plugin_install_task->get_activate() ) {
+ return false;
}
- $queue->insert( $queued_plugin, $queued_plugin['priority'] );
+ $queue->insert( $queued_plugin, $queued_plugin['priority'] );
}
// Insert a new PluginInstallTask at the appropriate position in the queue.
@@ -171,29 +198,53 @@ public static function add_to_queue( PluginInstallTask $plugin_install_task ) {
$plugin_install_task->get_priority()
);
- return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() );
+ return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() );
}
+ /**
+ * Removes a PluginInstallTask from the queue.
+ *
+ * @param string $plugin The slug of the task to remove.
+ * @return array
+ */
public static function remove_from_queue( $plugin ) {
/*
- Get the plugins queued up to be installed, the PluginInstall task gets
- converted to an associative array before storing it in the option. */
+ Get the plugins queued up to be installed, the PluginInstall task gets
+ converted to an associative array before storing it in the option.
+ */
$plugins = \get_option( Options::get_option_name( self::$queue_name ), array() );
$queue = new PriorityQueue();
foreach ( $plugins as $queued_plugin ) {
/*
- If the Plugin slug does not match add it back to the queue. */
+ If the Plugin slug does not match add it back to the queue.
+ */
if ( $queued_plugin['slug'] !== $plugin ) {
- $queue->insert( $queued_plugin, $queued_plugin['priority'] );
+ $queue->insert( $queued_plugin, $queued_plugin['priority'] );
}
}
- return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() );
+ return \update_option( Options::get_option_name( self::$queue_name ), $queue->to_array() );
}
+ /**
+ * Get the status of a given plugin slug from the queue.
+ *
+ * @param string $plugin The slug of the plugin.
+ * @return boolean
+ */
public static function status( $plugin ) {
$plugins = \get_option( Options::get_option_name( self::$queue_name ), array() );
- return array_search( $plugin, array_column( $plugins, 'slug' ) );
+ return array_search( $plugin, array_column( $plugins, 'slug' ), true );
+ }
+
+ /**
+ * Reset the Plugin install status and the queue.
+ *
+ * @return void
+ */
+ public static function reset_install_status() {
+ \delete_option( Options::get_option_name( 'plugins_init_status' ) );
+ \delete_option( Options::get_option_name( 'plugin_install_queue' ) );
}
}
diff --git a/src/OnboardingSPA/components/Content/index.js b/src/OnboardingSPA/components/Content/index.js
index f111868d9..46a050b79 100644
--- a/src/OnboardingSPA/components/Content/index.js
+++ b/src/OnboardingSPA/components/Content/index.js
@@ -3,6 +3,7 @@ import { Fragment, memo, Suspense, useCallback } from '@wordpress/element';
import { store as nfdOnboardingStore } from '../../store';
import { useSelect } from '@wordpress/data';
+import FlowStateHandler from '../StateHandlers/Flow';
/**
* Primary content area within the