| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Prevents plugin activation redirects from interfering with Launch. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Shared\Services; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
/** |
| 12 |
* Disables automatic setup wizard redirects from plugins we install. |
| 13 |
* |
| 14 |
* Uses four layers of defense: |
| 15 |
* 1. Filters that plugins check before redirecting |
| 16 |
* 2. Transient/option cleanup right after activation (priority 0) |
| 17 |
* 3. Hook removal for plugins that call exit() during activated_plugin |
| 18 |
* 4. wp_redirect interception as safety net |
| 19 |
*/ |
| 20 |
class PluginRedirectDisabler |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Per-plugin cleanup actions keyed by plugin slug. |
| 24 |
* |
| 25 |
* @var array<string, array<string, mixed>> |
| 26 |
*/ |
| 27 |
private static $pluginCleanup = [ |
| 28 |
'wpforms-lite' => [ |
| 29 |
'transients' => ['wpforms_activation_redirect'], |
| 30 |
// WPForms 2.0's wizard arms after activation, so the transient never reaches it. |
| 31 |
'setOptions' => ['wpforms_setup_wizard_disabled' => true], |
| 32 |
], |
| 33 |
'wp-mail-smtp' => [ |
| 34 |
'transients' => ['wp_mail_smtp_activation_redirect'], |
| 35 |
'setOptions' => ['wp_mail_smtp_activation_prevent_redirect' => true], |
| 36 |
], |
| 37 |
'google-analytics-for-wordpress' => [ |
| 38 |
'transients' => ['_monsterinsights_activation_redirect'], |
| 39 |
], |
| 40 |
'woocommerce' => [ |
| 41 |
'transients' => ['_wc_activation_redirect'], |
| 42 |
], |
| 43 |
'all-in-one-seo-pack' => [ |
| 44 |
'setOptions' => ['aioseo_activation_redirect' => true], |
| 45 |
], |
| 46 |
'really-simple-ssl' => [ |
| 47 |
'transients' => ['rsssl_redirect_to_settings_page'], |
| 48 |
], |
| 49 |
'imagify' => [ |
| 50 |
'transients' => ['imagify_activation'], |
| 51 |
], |
| 52 |
'complianz-gdpr' => [ |
| 53 |
'transients' => ['cmplz_redirect_to_settings_page'], |
| 54 |
'setOptions' => ['cmplz_onboarding_dismissed' => true], |
| 55 |
], |
| 56 |
'charitable' => [ |
| 57 |
'transients' => ['charitable_activation_redirect'], |
| 58 |
], |
| 59 |
'give' => [ |
| 60 |
'deleteOptions' => ['give_cache__give_activation_redirect'], |
| 61 |
], |
| 62 |
'seo-by-rank-math' => [ |
| 63 |
'transients' => ['_rank_math_activation_redirect'], |
| 64 |
], |
| 65 |
'sugar-calendar-lite' => [ |
| 66 |
'transients' => ['sugar_calendar_activation_redirect'], |
| 67 |
'setOptions' => ['sugar_calendar_prevent_redirect' => true], |
| 68 |
], |
| 69 |
'the-events-calendar' => [ |
| 70 |
'transients' => ['_tribe_events_activation_redirect'], |
| 71 |
'setOptions' => ['tribe_skip_welcome' => true], |
| 72 |
], |
| 73 |
'complianz-terms-conditions' => [ |
| 74 |
'transients' => ['cmplz_tc_redirect_to_settings'], |
| 75 |
], |
| 76 |
'translatepress-multilingual' => [ |
| 77 |
'setOptions' => ['trp_onboarding_started' => 'yes'], |
| 78 |
], |
| 79 |
]; |
| 80 |
|
| 81 |
/** |
| 82 |
* URL patterns to intercept via wp_redirect filter. |
| 83 |
* |
| 84 |
* @var string[] |
| 85 |
*/ |
| 86 |
private static $redirectUrlPatterns = [ |
| 87 |
'page=wpseo_installation_successful', |
| 88 |
'page=wc-gzd-setup', |
| 89 |
]; |
| 90 |
|
| 91 |
/** |
| 92 |
* Sets up all redirect prevention hooks. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function __construct() |
| 97 |
{ |
| 98 |
\add_filter('woocommerce_enable_setup_wizard', '__return_false'); |
| 99 |
\add_filter('woocommerce_prevent_automatic_wizard_redirect', '__return_true'); |
| 100 |
\add_filter('monsterinsights_enable_onboarding_wizard', '__return_false'); |
| 101 |
\add_filter('charitable_disable_activation_redirect', '__return_true'); |
| 102 |
\add_filter('tec_admin_update_page_bypass', '__return_true'); |
| 103 |
|
| 104 |
\add_action('activated_plugin', [$this, 'cleanupRedirectTriggers'], 0); |
| 105 |
|
| 106 |
\add_action('activate_ecwid-shopping-cart/ecwid-shopping-cart.php', function () { |
| 107 |
\remove_action('activated_plugin', 'ecwid_plugin_activation_redirect'); |
| 108 |
}); |
| 109 |
|
| 110 |
\add_action('activate_chatbot/qcld-wpwbot.php', function () { |
| 111 |
\remove_action('activated_plugin', 'qc_wpbotfree_activation_redirect'); |
| 112 |
}); |
| 113 |
|
| 114 |
\add_filter('wp_redirect', [$this, 'interceptRedirects'], 9998); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Cleans up redirect triggers for the specific plugin being activated. |
| 119 |
* |
| 120 |
* @param string $plugin The plugin basename (e.g. 'wpforms-lite/wpforms.php'). |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public function cleanupRedirectTriggers($plugin) |
| 124 |
{ |
| 125 |
$slug = \dirname($plugin); |
| 126 |
if (!in_array($slug, SupportedPlugins::SLUGS, true)) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
if (!isset(self::$pluginCleanup[$slug])) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
$cleanup = self::$pluginCleanup[$slug]; |
| 135 |
|
| 136 |
foreach ($cleanup['transients'] ?? [] as $transient) { |
| 137 |
\delete_transient($transient); |
| 138 |
} |
| 139 |
|
| 140 |
foreach ($cleanup['deleteOptions'] ?? [] as $option) { |
| 141 |
\delete_option($option); |
| 142 |
} |
| 143 |
|
| 144 |
foreach ($cleanup['setOptions'] ?? [] as $option => $value) { |
| 145 |
\update_option($option, $value); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Intercepts known plugin redirect URLs. |
| 151 |
* |
| 152 |
* @param string $url The redirect URL. |
| 153 |
* @return string The original URL or admin dashboard URL to block the redirect. |
| 154 |
*/ |
| 155 |
public function interceptRedirects($url) |
| 156 |
{ |
| 157 |
foreach (self::$redirectUrlPatterns as $pattern) { |
| 158 |
if (str_contains($url, $pattern)) { |
| 159 |
return \admin_url(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return $url; |
| 164 |
} |
| 165 |
} |
| 166 |
|