From 980efd84ef3559774b621d760b9809a87a1b47ec Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 11 Oct 2024 14:47:01 -0300 Subject: [PATCH 1/7] Try to fix issue 5411 / set a separate multi value to onboarding transient --- .../FrmOnboardingWizardController.php | 52 ++++++++++++++----- formidable.php | 3 +- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/classes/controllers/FrmOnboardingWizardController.php b/classes/controllers/FrmOnboardingWizardController.php index 320f317124..d5986654e7 100644 --- a/classes/controllers/FrmOnboardingWizardController.php +++ b/classes/controllers/FrmOnboardingWizardController.php @@ -46,11 +46,20 @@ class FrmOnboardingWizardController { /** * Transient value associated with the redirection to the Onboarding Wizard page. + * Used when activating a single plugin. * * @var string */ const TRANSIENT_VALUE = 'formidable-welcome'; + /** + * Transient value associated with the redirection to the Onboarding Wizard page. + * Used when activating multiple plugins at once. + * + * @var string + */ + const TRANSIENT_MULTI_VALUE = 'formidable-welcome-multi'; + /** * Option name for storing the redirect status for the Onboarding Wizard page. * @@ -143,11 +152,26 @@ public static function do_admin_redirects() { return; } - // Check if we should consider redirection. - if ( ! FrmAppHelper::is_formidable_admin() || ! self::is_onboarding_wizard_displayed() || self::has_onboarding_been_skipped() || FrmAppHelper::pro_is_connected() ) { + if ( self::has_onboarding_been_skipped() || FrmAppHelper::pro_is_connected() ) { return; } + switch ( get_transient( self::TRANSIENT_NAME ) ) { + case self::TRANSIENT_VALUE: + // For single activations we want to always redirect. + break; + case self::TRANSIENT_MULTI_VALUE: + // For multi-activations we want to only redirect when a user loads a Formidable page. + if ( ! FrmAppHelper::is_formidable_admin() ) { + return; + } + break; + default: + // If the transient is any other value, we do not want to redirect. + // The value is likely false, or 'no'. + return; + } + set_transient( self::TRANSIENT_NAME, 'no', 60 ); // Prevent redirect with every activation. @@ -462,17 +486,6 @@ public static function is_onboarding_wizard_page() { return FrmAppHelper::is_admin_page( self::PAGE_SLUG ); } - /** - * Validates if the Onboarding Wizard page is being displayed. - * - * @since 6.9 - * - * @return bool True if the Onboarding Wizard page is displayed, false otherwise. - */ - public static function is_onboarding_wizard_displayed() { - return get_transient( self::TRANSIENT_NAME ) === self::TRANSIENT_VALUE; - } - /** * Checks if the plugin has already performed a redirect to avoid repeated redirections. * @@ -663,4 +676,17 @@ public static function get_upgrade_link() { public static function get_usage_data() { return get_option( self::USAGE_DATA_OPTION, array() ); } + + /** + * Validates if the Onboarding Wizard page is being displayed. + * + * @since 6.9 + * @deprecated x.x + * + * @return bool True if the Onboarding Wizard page is displayed, false otherwise. + */ + public static function is_onboarding_wizard_displayed() { + // TODO deprecate this. + return get_transient( self::TRANSIENT_NAME ) === self::TRANSIENT_VALUE; + } } diff --git a/formidable.php b/formidable.php index 5a2a07efeb..6d17b65a90 100644 --- a/formidable.php +++ b/formidable.php @@ -138,9 +138,10 @@ function frm_class_autoloader( $class_name, $filepath ) { */ function frm_maybe_install() { if ( get_transient( FrmOnboardingWizardController::TRANSIENT_NAME ) !== 'no' ) { + $transient_value = isset( $_GET['activate-multi'] ) ? FrmOnboardingWizardController::TRANSIENT_MULTI_VALUE : FrmOnboardingWizardController::TRANSIENT_VALUE; set_transient( FrmOnboardingWizardController::TRANSIENT_NAME, - FrmOnboardingWizardController::TRANSIENT_VALUE, + $transient_value, 60 ); } From 64f73fb9491393feca86456e59c991b254ef5ab1 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 11 Oct 2024 14:58:47 -0300 Subject: [PATCH 2/7] Reduce code --- .../FrmOnboardingWizardController.php | 25 ++++++++----------- formidable.php | 3 +-- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/classes/controllers/FrmOnboardingWizardController.php b/classes/controllers/FrmOnboardingWizardController.php index d5986654e7..d1035738ae 100644 --- a/classes/controllers/FrmOnboardingWizardController.php +++ b/classes/controllers/FrmOnboardingWizardController.php @@ -156,20 +156,15 @@ public static function do_admin_redirects() { return; } - switch ( get_transient( self::TRANSIENT_NAME ) ) { - case self::TRANSIENT_VALUE: - // For single activations we want to always redirect. - break; - case self::TRANSIENT_MULTI_VALUE: - // For multi-activations we want to only redirect when a user loads a Formidable page. - if ( ! FrmAppHelper::is_formidable_admin() ) { - return; - } - break; - default: - // If the transient is any other value, we do not want to redirect. - // The value is likely false, or 'no'. - return; + $transient_value = get_transient( self::TRANSIENT_NAME ); + if ( ! in_array( $transient_value, array( self::TRANSIENT_VALUE, self::TRANSIENT_MULTI_VALUE ), true ) ) { + self::mark_onboarding_as_skipped(); + return; + } + + if ( self::TRANSIENT_MULTI_VALUE === $transient_value && ! FrmAppHelper::is_formidable_admin() ) { + // For multi-activations we want to only redirect when a user loads a Formidable page. + return; } set_transient( self::TRANSIENT_NAME, 'no', 60 ); @@ -686,7 +681,7 @@ public static function get_usage_data() { * @return bool True if the Onboarding Wizard page is displayed, false otherwise. */ public static function is_onboarding_wizard_displayed() { - // TODO deprecate this. + _deprecated_function( __METHOD__, 'x.x' ); return get_transient( self::TRANSIENT_NAME ) === self::TRANSIENT_VALUE; } } diff --git a/formidable.php b/formidable.php index 6d17b65a90..ca6e684ac4 100644 --- a/formidable.php +++ b/formidable.php @@ -138,10 +138,9 @@ function frm_class_autoloader( $class_name, $filepath ) { */ function frm_maybe_install() { if ( get_transient( FrmOnboardingWizardController::TRANSIENT_NAME ) !== 'no' ) { - $transient_value = isset( $_GET['activate-multi'] ) ? FrmOnboardingWizardController::TRANSIENT_MULTI_VALUE : FrmOnboardingWizardController::TRANSIENT_VALUE; set_transient( FrmOnboardingWizardController::TRANSIENT_NAME, - $transient_value, + isset( $_GET['activate-multi'] ) ? FrmOnboardingWizardController::TRANSIENT_MULTI_VALUE : FrmOnboardingWizardController::TRANSIENT_VALUE, 60 ); } From f08dc9fd2c65c6fdac0a1ad0516840a405eaf243 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 11 Oct 2024 15:09:59 -0300 Subject: [PATCH 3/7] The GET param is only available after the redirect, so check later --- classes/controllers/FrmOnboardingWizardController.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/classes/controllers/FrmOnboardingWizardController.php b/classes/controllers/FrmOnboardingWizardController.php index d1035738ae..9f6e27328d 100644 --- a/classes/controllers/FrmOnboardingWizardController.php +++ b/classes/controllers/FrmOnboardingWizardController.php @@ -162,6 +162,16 @@ public static function do_admin_redirects() { return; } + $is_multi_activate = isset( $_GET['activate-multi'] ); + if ( $is_multi_activate ) { + set_transient( + FrmOnboardingWizardController::TRANSIENT_NAME, + FrmOnboardingWizardController::TRANSIENT_MULTI_VALUE, + 60 + ); + return; + } + if ( self::TRANSIENT_MULTI_VALUE === $transient_value && ! FrmAppHelper::is_formidable_admin() ) { // For multi-activations we want to only redirect when a user loads a Formidable page. return; From e26fbfa336a6296b409a795980f3f7a8e3f96f80 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 11 Oct 2024 15:10:16 -0300 Subject: [PATCH 4/7] The GET param is only available after the redirect, so check later --- formidable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formidable.php b/formidable.php index ca6e684ac4..5a2a07efeb 100644 --- a/formidable.php +++ b/formidable.php @@ -140,7 +140,7 @@ function frm_maybe_install() { if ( get_transient( FrmOnboardingWizardController::TRANSIENT_NAME ) !== 'no' ) { set_transient( FrmOnboardingWizardController::TRANSIENT_NAME, - isset( $_GET['activate-multi'] ) ? FrmOnboardingWizardController::TRANSIENT_MULTI_VALUE : FrmOnboardingWizardController::TRANSIENT_VALUE, + FrmOnboardingWizardController::TRANSIENT_VALUE, 60 ); } From 7e4de4c545a752e19f3c9b5e12a356d2ab7d43a0 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 11 Oct 2024 15:16:42 -0300 Subject: [PATCH 5/7] Add more comments --- classes/controllers/FrmOnboardingWizardController.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/classes/controllers/FrmOnboardingWizardController.php b/classes/controllers/FrmOnboardingWizardController.php index 9f6e27328d..9a7a6d1367 100644 --- a/classes/controllers/FrmOnboardingWizardController.php +++ b/classes/controllers/FrmOnboardingWizardController.php @@ -135,6 +135,8 @@ public static function load_admin_hooks() { /** * Performs a safe redirect to the welcome screen when the plugin is activated. + * On single activation, we will redirect immediately. + * When activating multiple plugins, the redirect is delayed until a Formidable page is loaded. * * @return void */ @@ -162,8 +164,11 @@ public static function do_admin_redirects() { return; } - $is_multi_activate = isset( $_GET['activate-multi'] ); - if ( $is_multi_activate ) { + if ( isset( $_GET['activate-multi'] ) ) { + /** + * $_GET['activate-multi'] is set after activating multiple plugins. + * In this case, change the transient value so we know for future checks. + */ set_transient( FrmOnboardingWizardController::TRANSIENT_NAME, FrmOnboardingWizardController::TRANSIENT_MULTI_VALUE, From d4689121b77c8651d49d645d22bb9926112a54bf Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 11 Oct 2024 15:20:12 -0300 Subject: [PATCH 6/7] Use self (phpcs) --- classes/controllers/FrmOnboardingWizardController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/controllers/FrmOnboardingWizardController.php b/classes/controllers/FrmOnboardingWizardController.php index 9a7a6d1367..7a1d3470f5 100644 --- a/classes/controllers/FrmOnboardingWizardController.php +++ b/classes/controllers/FrmOnboardingWizardController.php @@ -170,8 +170,8 @@ public static function do_admin_redirects() { * In this case, change the transient value so we know for future checks. */ set_transient( - FrmOnboardingWizardController::TRANSIENT_NAME, - FrmOnboardingWizardController::TRANSIENT_MULTI_VALUE, + self::TRANSIENT_NAME, + self::TRANSIENT_MULTI_VALUE, 60 ); return; From 2a97188fbe56ee48150e98c545456be92eeebf15 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 11 Oct 2024 15:21:57 -0300 Subject: [PATCH 7/7] Reduce to a single line --- classes/controllers/FrmOnboardingWizardController.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/classes/controllers/FrmOnboardingWizardController.php b/classes/controllers/FrmOnboardingWizardController.php index 7a1d3470f5..1ca23dbf38 100644 --- a/classes/controllers/FrmOnboardingWizardController.php +++ b/classes/controllers/FrmOnboardingWizardController.php @@ -169,11 +169,7 @@ public static function do_admin_redirects() { * $_GET['activate-multi'] is set after activating multiple plugins. * In this case, change the transient value so we know for future checks. */ - set_transient( - self::TRANSIENT_NAME, - self::TRANSIENT_MULTI_VALUE, - 60 - ); + set_transient( self::TRANSIENT_NAME, self::TRANSIENT_MULTI_VALUE, 60 ); return; }