From b92ac1b172802b38d0b820e2595d64be8db9931d Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Tue, 4 Apr 2023 14:37:02 +0530 Subject: [PATCH 1/5] add WordPress version shield to Onboarding --- bootstrap.php | 11 ++ includes/Application.php | 12 +- includes/Compatibility/Safe_Mode.php | 233 +++++++++++++++++++++++++++ includes/Compatibility/Scan.php | 115 +++++++++++++ includes/Compatibility/Status.php | 64 ++++++++ includes/Data/Options.php | 1 + 6 files changed, 435 insertions(+), 1 deletion(-) create mode 100644 includes/Compatibility/Safe_Mode.php create mode 100644 includes/Compatibility/Scan.php create mode 100644 includes/Compatibility/Status.php diff --git a/bootstrap.php b/bootstrap.php index f14f799b6..10a99a5c7 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -3,6 +3,9 @@ use NewfoldLabs\WP\Module\Onboarding\Application; use function NewfoldLabs\WP\ModuleLoader\register; use NewfoldLabs\WP\Module\Onboarding\ModuleController; +use NewfoldLabs\WP\Module\Onboarding\Compatibility\Scan; +use NewfoldLabs\WP\Module\Onboarding\Compatibility\Safe_Mode; +use NewfoldLabs\WP\Module\Onboarding\Compatibility\Status; /** * Register Onboarding with Newfold Module Loader @@ -33,6 +36,14 @@ function nfd_wp_module_onboarding_register() { if ( ! defined( 'NFD_ONBOARDING_BUILD_URL' && defined( 'NFD_ONBOARDING_VERSION' ) ) ) { define( 'NFD_ONBOARDING_BUILD_URL', $container->plugin()->url . '/vendor/newfold-labs/wp-module-onboarding/build/' . NFD_ONBOARDING_VERSION ); } + + if ( 'compatible' !== Status::get() ) { + $compatibility_scan = new Scan(); + Status::set( $compatibility_scan ); + if ( 'compatible' !== $compatibility_scan->result ) { + return new Safe_Mode( $compatibility_scan ); + } + } // Instantiate Onboarding Module Application new Application( $container ); }, diff --git a/includes/Application.php b/includes/Application.php index 5982f65be..9a98be82c 100644 --- a/includes/Application.php +++ b/includes/Application.php @@ -1,6 +1,7 @@ scan = $scan; + \add_action( 'admin_menu', array( $this, 'core_update_page' ) ); + } + + /** + * Display WP core update page when in safe mode. + */ + public function core_update_page() { + \add_submenu_page( + null, + \__( 'Onboarding', 'wp-module-onboarding' ), + \__( 'Onboarding', 'wp-module-onboarding' ), + Permissions::ADMIN, + WP_Admin::$slug, + array( $this, 'render' ), + 100 + ); + } + + /** + * Render WP core update page. + */ + public function render() { ?> + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +
+
+

+

+

+ scan->data['wp_version'], + $this->scan->config['min_wp'] + ) + ) + ?> +

+
+
+
+
+ +

+ + + + +

+
+
+

+
+
+ + '6.2', + ); + + /** + * Environment data. + * + * @var array + */ + public $data = array( + 'wp_version' => '', + ); + + /** + * Active Configuration. + * + * @var array + */ + public $config = array(); + + /** + * Test statuses. + * + * @var array + */ + public $test_statuses = array( + 'compatible', + 'unsupported-wp', + ); + + /** + * Result of the compatibility scan. + * + * @var string + */ + public $result = ''; + + /** + * Scan constructor. + * + * @param array $config Config data. + */ + public function __construct( $config = array() ) { + $this->setup( $config ); + // Fetch Relevant Data + $this->fetch(); + // Evaluate Using Configuration & Data + $this->evaluate(); + + return array( + 'status' => $this->result, + 'data' => $this->data, + ); + } + + /** + * Register configuration. + * + * @param array $config Config data. + */ + protected function setup( $config ) { + $this->config = array_merge( $this->default_config, $config ); + } + + /** + * Set up environment data to be checked. + */ + protected function fetch() { + global $wp_version; + + $this->data = array_merge( + $this->data, + array( + 'wp_version' => $wp_version, + ) + ); + + $previous = Status::get(); + if ( ! empty( $previous ) && is_array( $previous ) ) { + $this->data['previous_result'] = $previous; + } + } + + /** + * Run the compatibility check. + */ + protected function evaluate() { + + $this->result = 'scan-initiated'; + + if ( version_compare( $this->data['wp_version'], $this->config['min_wp'], '<' ) ) { + $this->result = 'unsupported-wp'; + } + + if ( 'scan-initiated' === $this->result ) { + $this->result = 'compatible'; + } + + } + +} diff --git a/includes/Compatibility/Status.php b/includes/Compatibility/Status.php new file mode 100644 index 000000000..e43a453f5 --- /dev/null +++ b/includes/Compatibility/Status.php @@ -0,0 +1,64 @@ +result; + $data['timestamp'] = \current_time( 'mysql' ); + } + + \update_option( Options::get_option_name( 'compatibility_results' ), $data ); + + return false; + } + + /** + * Reset/Delete the stored scan results + */ + public static function reset() { + \delete_option( Options::get_option_name( 'compatibility_results' ) ); + } + +} diff --git a/includes/Data/Options.php b/includes/Data/Options.php index d8ccdb2ee..4938ab213 100644 --- a/includes/Data/Options.php +++ b/includes/Data/Options.php @@ -50,6 +50,7 @@ final class Options { 'theme_settings' => 'theme_settings', 'flow_preset' => 'flow_preset', 'wpseo_social' => 'wpseo_social', + 'compatibility_results' => 'compatibility_results', ); /** From dbd192b5101f8483b95c955a911f35cd7bb95efd Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 4 Apr 2023 21:49:43 +0530 Subject: [PATCH 2/5] change minimum wp version to 6.1 --- includes/Compatibility/Scan.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Compatibility/Scan.php b/includes/Compatibility/Scan.php index 1633866d0..a35e2d08d 100644 --- a/includes/Compatibility/Scan.php +++ b/includes/Compatibility/Scan.php @@ -13,7 +13,7 @@ class Scan { * @var array */ public $default_config = array( - 'min_wp' => '6.2', + 'min_wp' => '6.1', ); /** From 4f71d07538a29bcb6511cb2318eb5f57b94d15b3 Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Wed, 5 Apr 2023 19:19:24 +0530 Subject: [PATCH 3/5] use a better core update function --- includes/Compatibility/Safe_Mode.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Compatibility/Safe_Mode.php b/includes/Compatibility/Safe_Mode.php index 79a31b343..0eaaae8a8 100644 --- a/includes/Compatibility/Safe_Mode.php +++ b/includes/Compatibility/Safe_Mode.php @@ -188,7 +188,7 @@ public function render() { ?>

- + From cb814e614a92f5a631ae8ef3827135850722dd16 Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Thu, 6 Apr 2023 03:01:18 +0530 Subject: [PATCH 4/5] increase safety and reliability of core update and redirect --- includes/Compatibility/Safe_Mode.php | 58 +++++++++++++++++++--------- includes/Data/Options.php | 1 + 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/includes/Compatibility/Safe_Mode.php b/includes/Compatibility/Safe_Mode.php index 0eaaae8a8..ba7eac489 100644 --- a/includes/Compatibility/Safe_Mode.php +++ b/includes/Compatibility/Safe_Mode.php @@ -2,6 +2,7 @@ namespace NewfoldLabs\WP\Module\Onboarding\Compatibility; +use NewfoldLabs\WP\Module\Onboarding\Data\Options; use NewfoldLabs\WP\Module\Onboarding\Permissions; use NewfoldLabs\WP\Module\Onboarding\WP_Admin; @@ -25,6 +26,13 @@ class Safe_Mode { public function __construct( Scan $scan ) { $this->scan = $scan; \add_action( 'admin_menu', array( $this, 'core_update_page' ) ); + + // Cleanup and Redirect to Onboarding once core has updated successfully. + \add_action( '_core_updated_successfully', array( self::class, 'handle_redirect' ) ); + + // Cleanup and Redirect to Onboarding once core has updated successfully via manual DB upgrade. + \add_action( 'load-about.php', array( self::class, 'handle_redirect' ) ); + } /** @@ -45,7 +53,10 @@ public function core_update_page() { /** * Render WP core update page. */ - public function render() { ?> + public function render() { + $request_url = \remove_query_arg( '_wp_http_referer' ); + \set_transient( Options::get_option_name( 'core_update_referrer' ), $request_url, 259200 ); + ?>