Welcome.php
141 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Activation; |
| 4 | |
| 5 | // No Direct Access |
| 6 | if (!defined("WPINC")) { |
| 7 | die; |
| 8 | } |
| 9 | |
| 10 | use WPStaging\Framework\Traits\NoticesTrait; |
| 11 | use WPStaging\Framework\Facades\Escape; |
| 12 | use WPStaging\Framework\Notices\Notices; |
| 13 | |
| 14 | class Welcome |
| 15 | { |
| 16 | use NoticesTrait; |
| 17 | |
| 18 | public function __construct() |
| 19 | { |
| 20 | add_action('admin_init', [$this, 'welcome']); |
| 21 | add_action('wp_ajax_wpstg_activate_pro', [$this, 'ajaxActivatePro']); // phpcs:ignore WPStaging.Security.AuthorizationChecked -- Authorization checked in ajaxActivatePro() |
| 22 | |
| 23 | if (wpstgGetProVersionNumberIfInstalled() && $this->isWPStagingAdminPage()) { |
| 24 | add_action(Notices::ACTION_ADMIN_NOTICES, [$this, 'wpstgproActivationNotice']); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @return void |
| 30 | * @todo make this message permanently hideable when dismissed. |
| 31 | */ |
| 32 | public function wpstgproActivationNotice() |
| 33 | { |
| 34 | $nonce = wp_create_nonce('wpstg_activate_pro'); |
| 35 | ?> |
| 36 | <div class="notice notice-warning is-dismissible"> |
| 37 | <p> |
| 38 | <?php |
| 39 | printf( |
| 40 | Escape::escapeHtml(__('WP Staging Pro is installed but not active. %1$sActivate now%2$s to unlock all Pro features.', 'wp-staging')), |
| 41 | '<a href="#" id="wpstg-activate-pro" data-nonce="' . esc_attr($nonce) . '">', |
| 42 | '</a>' |
| 43 | ); |
| 44 | ?> |
| 45 | </p> |
| 46 | </div> |
| 47 | <script> |
| 48 | (function() { |
| 49 | var link = document.getElementById('wpstg-activate-pro'); |
| 50 | if (!link) return; |
| 51 | link.addEventListener('click', function(e) { |
| 52 | e.preventDefault(); |
| 53 | if (link.dataset.busy) return; |
| 54 | link.dataset.busy = '1'; |
| 55 | var originalText = link.textContent; |
| 56 | link.textContent = '<?php echo esc_js(__('Activating...', 'wp-staging')); ?>'; |
| 57 | link.style.pointerEvents = 'none'; |
| 58 | var xhr = new XMLHttpRequest(); |
| 59 | xhr.open('POST', ajaxurl); |
| 60 | xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
| 61 | xhr.onload = function() { |
| 62 | try { var res = JSON.parse(xhr.responseText); } catch(err) { res = {}; } |
| 63 | if (res.success) { |
| 64 | window.location.reload(); |
| 65 | } else { |
| 66 | link.textContent = res.data || 'Activation failed.'; |
| 67 | link.style.pointerEvents = ''; |
| 68 | delete link.dataset.busy; |
| 69 | } |
| 70 | }; |
| 71 | xhr.onerror = function() { |
| 72 | link.textContent = originalText; |
| 73 | link.style.pointerEvents = ''; |
| 74 | delete link.dataset.busy; |
| 75 | }; |
| 76 | xhr.send('action=wpstg_activate_pro&nonce=' + link.dataset.nonce); |
| 77 | }); |
| 78 | })(); |
| 79 | </script> |
| 80 | <?php |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return void |
| 85 | */ |
| 86 | public function ajaxActivatePro() |
| 87 | { |
| 88 | if (!check_ajax_referer('wpstg_activate_pro', 'nonce', false)) { |
| 89 | wp_send_json_error('Invalid security token.'); |
| 90 | } |
| 91 | |
| 92 | if (!current_user_can('activate_plugins')) { |
| 93 | wp_send_json_error('Insufficient permissions.'); |
| 94 | } |
| 95 | |
| 96 | $slug = wpstgGetPluginSlug(WPSTG_PRO_VERSION_PLUGIN_FILE); |
| 97 | if (!$slug) { |
| 98 | wp_send_json_error('Pro plugin not found.'); |
| 99 | } |
| 100 | |
| 101 | $result = activate_plugin($slug); |
| 102 | if (is_wp_error($result)) { |
| 103 | wp_send_json_error($result->get_error_message()); |
| 104 | } |
| 105 | |
| 106 | wp_send_json_success(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Sends user to the welcome page on first activation of WPSTG as well as each |
| 111 | * time WPSTG is upgraded to a new version |
| 112 | * |
| 113 | * @access public |
| 114 | * @return void |
| 115 | * @since 1.0.1 |
| 116 | */ |
| 117 | public function welcome() |
| 118 | { |
| 119 | // Bail if pro is installed |
| 120 | if (wpstgGetProVersionNumberIfInstalled()) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // Bail if no activation redirect |
| 125 | if (get_transient('wpstg_activation_redirect') === false) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // Delete the redirect transient |
| 130 | delete_transient('wpstg_activation_redirect'); |
| 131 | |
| 132 | // Bail if activating from network, or bulk |
| 133 | if (is_network_admin() || isset($_GET['activate-multi'])) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | wp_safe_redirect(admin_url('admin.php?page=wpstg-welcome')); |
| 138 | exit; |
| 139 | } |
| 140 | } |
| 141 |