Welcome.php
224 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Activation; |
| 4 | |
| 5 | |
| 6 | if (!defined("WPINC")) { |
| 7 | die; |
| 8 | } |
| 9 | |
| 10 | use WPStaging\Framework\Analytics\Actions\AnalyticsGenericEvent; |
| 11 | use WPStaging\Framework\Onboarding\FreeOnboarding; |
| 12 | use WPStaging\Framework\Traits\NoticesTrait; |
| 13 | use WPStaging\Framework\Facades\Escape; |
| 14 | use WPStaging\Framework\Notices\Notices; |
| 15 | |
| 16 | class Welcome |
| 17 | { |
| 18 | use NoticesTrait; |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | const QUERY_FROM_ACTIVATION = 'wpstg-activation'; |
| 28 | |
| 29 | |
| 30 | const UTM_CONTENT_ACTIVATION = 'welcome_page_activation'; |
| 31 | |
| 32 | |
| 33 | const UTM_CONTENT_SIDEBAR = 'sidebar_upgrade'; |
| 34 | |
| 35 | |
| 36 | const EVENT_ACTIVATION_REDIRECT = 'activation_redirect'; |
| 37 | |
| 38 | public function __construct() |
| 39 | { |
| 40 | add_action('admin_init', [$this, 'welcome']); |
| 41 | add_action('wp_ajax_wpstg_activate_pro', [$this, 'ajaxActivatePro']); // phpcs:ignore WPStaging.Security.AuthorizationChecked -- Authorization checked in ajaxActivatePro() |
| 42 | |
| 43 | if (wpstgGetProVersionNumberIfInstalled() && $this->isWPStagingAdminPage()) { |
| 44 | add_action(Notices::ACTION_ADMIN_NOTICES, [$this, 'wpstgproActivationNotice']); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | public function wpstgproActivationNotice() |
| 53 | { |
| 54 | if ($this->isFirstRunAwaitingConsent()) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | $nonce = wp_create_nonce('wpstg_activate_pro'); |
| 59 | ?> |
| 60 | <div class="notice notice-warning is-dismissible"> |
| 61 | <p> |
| 62 | <?php |
| 63 | printf( |
| 64 | Escape::escapeHtml(__('WP Staging Pro is installed but not active. %1$sActivate now%2$s to unlock all Pro features.', 'wp-staging')), |
| 65 | '<a href="#" id="wpstg-activate-pro" data-nonce="' . esc_attr($nonce) . '">', |
| 66 | '</a>' |
| 67 | ); |
| 68 | ?> |
| 69 | </p> |
| 70 | </div> |
| 71 | <script> |
| 72 | (function() { |
| 73 | var link = document.getElementById('wpstg-activate-pro'); |
| 74 | if (!link) return; |
| 75 | link.addEventListener('click', function(e) { |
| 76 | e.preventDefault(); |
| 77 | if (link.dataset.busy) return; |
| 78 | link.dataset.busy = '1'; |
| 79 | var originalText = link.textContent; |
| 80 | link.textContent = '<?php echo esc_js(__('Activating...', 'wp-staging')); ?>'; |
| 81 | link.style.pointerEvents = 'none'; |
| 82 | var xhr = new XMLHttpRequest(); |
| 83 | xhr.open('POST', ajaxurl); |
| 84 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
| 85 | xhr.onload = function() { |
| 86 | try { var res = JSON.parse(xhr.responseText); } catch(err) { res = {}; } |
| 87 | if (res.success) { |
| 88 | window.location.reload(); |
| 89 | } else { |
| 90 | link.textContent = res.data || 'Activation failed.'; |
| 91 | link.style.pointerEvents = ''; |
| 92 | delete link.dataset.busy; |
| 93 | } |
| 94 | }; |
| 95 | xhr.onerror = function() { |
| 96 | link.textContent = originalText; |
| 97 | link.style.pointerEvents = ''; |
| 98 | delete link.dataset.busy; |
| 99 | }; |
| 100 | xhr.send('action=wpstg_activate_pro&nonce=' + link.dataset.nonce); |
| 101 | }); |
| 102 | })(); |
| 103 | </script> |
| 104 | <?php |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | private function isFirstRunAwaitingConsent(): bool |
| 115 | { |
| 116 | $onboarding = FreeOnboarding::resolve(); |
| 117 | |
| 118 | return $onboarding !== null && $onboarding->isPreConsentScreen(); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | public function ajaxActivatePro() |
| 125 | { |
| 126 | if (!check_ajax_referer('wpstg_activate_pro', 'nonce', false)) { |
| 127 | wp_send_json_error('Invalid security token.'); |
| 128 | } |
| 129 | |
| 130 | if (!current_user_can('activate_plugins')) { |
| 131 | wp_send_json_error('Insufficient permissions.'); |
| 132 | } |
| 133 | |
| 134 | $slug = wpstgGetPluginSlug(WPSTG_PRO_VERSION_PLUGIN_FILE); |
| 135 | if (!$slug) { |
| 136 | wp_send_json_error('Pro plugin not found.'); |
| 137 | } |
| 138 | |
| 139 | $result = activate_plugin($slug); |
| 140 | if (is_wp_error($result)) { |
| 141 | wp_send_json_error($result->get_error_message()); |
| 142 | } |
| 143 | |
| 144 | wp_send_json_success(); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | public function welcome() |
| 156 | { |
| 157 | |
| 158 | if (wpstgGetProVersionNumberIfInstalled()) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | if (get_transient('wpstg_activation_redirect') === false) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | delete_transient('wpstg_activation_redirect'); |
| 169 | |
| 170 | |
| 171 | if (is_network_admin() || isset($_GET['activate-multi'])) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | wp_safe_redirect($this->getFirstScreenUrl()); |
| 176 | exit; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | public function getFirstScreenUrl(): string |
| 188 | { |
| 189 | $onboarding = FreeOnboarding::resolve(); |
| 190 | |
| 191 | return $this->getScreenUrl($onboarding !== null && $onboarding->isEligible()); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | public function getScreenUrl(bool $isFirstRun): string |
| 201 | { |
| 202 | AnalyticsGenericEvent::logEvent(self::EVENT_ACTIVATION_REDIRECT, FreeOnboarding::ANALYTICS_GROUP, [ |
| 203 | 'destination' => $isFirstRun ? 'task_selector' : 'welcome_page', |
| 204 | ]); |
| 205 | |
| 206 | if ($isFirstRun) { |
| 207 | return admin_url('admin.php?page=wpstg_clone'); |
| 208 | } |
| 209 | |
| 210 | return add_query_arg(self::QUERY_FROM_ACTIVATION, '1', admin_url('admin.php?page=wpstg-welcome')); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | public static function getUpgradeContext(): string |
| 218 | { |
| 219 | |
| 220 | // selects an attribution label and nothing else. phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 221 | return isset($_GET[self::QUERY_FROM_ACTIVATION]) ? self::UTM_CONTENT_ACTIVATION : self::UTM_CONTENT_SIDEBAR; |
| 222 | } |
| 223 | } |
| 224 |