| @@ -5,60 +5,130 @@ | ||
| 5 | 5 | |
| 6 | 6 | class WPO_Activation { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | + * Flag to prevent multiple silence activation actions from running when the plugin is reactivated. | |
| 10 | + * | |
| 11 | + * @var bool | |
| 12 | + */ | |
| 13 | + private static $silence_activation_actions_done = false; | |
| 14 | + | |
| 15 | + /** | |
| 9 | 16 | * Actions to be performed upon plugin activation |
| 17 | + * | |
| 18 | + * @return void | |
| 10 | 19 | */ |
| 11 | - public static function actions() { | |
| 12 | - if (is_multisite() && !is_network_admin()) { | |
| 13 | - self::deactivate_and_die(); | |
| 14 | - } | |
| 20 | + public static function actions(): void { | |
| 15 | 21 | |
| 16 | - if (!self::is_reactivated()) { | |
| 17 | - self::set_as_newly_activated(); | |
| 18 | - } | |
| 22 | + self::check_minimum_requirements(); | |
| 23 | + self::check_user_capability(); | |
| 24 | + self::handle_activation_type(); | |
| 19 | 25 | |
| 20 | 26 | WP_Optimize()->get_options()->set_default_options(); |
| 21 | 27 | WP_Optimize()->get_minify()->plugin_activate(); |
| 22 | 28 | WP_Optimize()->get_gzip_compression()->restore(); |
| 23 | 29 | WP_Optimize()->get_browser_cache()->restore(); |
| 30 | + WP_Optimize()->get_table_management()->create_plugin_tables(); | |
| 31 | + WP_Optimize()->get_webp_instance()->plugin_activate(); | |
| 24 | 32 | |
| 33 | + WP_Optimize_Page_Cache_Preloader::instance()->maybe_reschedule_preload_on_activation(); | |
| 34 | + | |
| 25 | 35 | self::init_batch_processing(); |
| 36 | + self::silence_activation_actions(); | |
| 37 | + } | |
| 26 | 38 | |
| 27 | - if (self::is_premium()) { | |
| 28 | - self::init_premium(); | |
| 39 | + /** | |
| 40 | + * When user uploads a plugin zip file on already activated plugin, WordPress doesn't run the activation hook. | |
| 41 | + * This method is used to perform the necessary activation actions in such cases. | |
| 42 | + * | |
| 43 | + * @return void | |
| 44 | + */ | |
| 45 | + public static function silence_activation_actions(): void { | |
| 46 | + if (self::$silence_activation_actions_done) { | |
| 47 | + return; | |
| 29 | 48 | } |
| 49 | + | |
| 50 | + WP_Optimize()->cron_activate(); | |
| 51 | + self::maybe_init_premium(); | |
| 52 | + | |
| 53 | + self::$silence_activation_actions_done = true; | |
| 30 | 54 | } |
| 31 | 55 | |
| 32 | 56 | /** |
| 57 | + * Check if minimum requirements are met, deactivate and die if not | |
| 58 | + * | |
| 59 | + * @return void | |
| 60 | + */ | |
| 61 | + private static function check_minimum_requirements(): void { | |
| 62 | + if (!WP_Optimize()->is_minimum_requirement_met()) { | |
| 63 | + WP_Optimize()->add_notice_minimum_requirements_not_met(); | |
| 64 | + WP_Optimize()->deactivate_plugin(); | |
| 65 | + WP_Optimize()->die_minimum_requirement_not_met(); | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Check if the current user has permission to activate the plugin on multisite | |
| 71 | + * | |
| 72 | + * @return void | |
| 73 | + */ | |
| 74 | + private static function check_user_capability(): void { | |
| 75 | + if (is_multisite() && !WP_Optimize()->current_user_can('manage_network_options')) { | |
| 76 | + self::deactivate_and_die(); | |
| 77 | + } | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Check if this is a reactivation or a new activation and set up accordingly | |
| 82 | + * | |
| 83 | + * @return void | |
| 84 | + */ | |
| 85 | + private static function handle_activation_type(): void { | |
| 86 | + // Always record first-activation timestamp (no-ops if already set). | |
| 87 | + self::set_as_newly_activated(); | |
| 88 | + if (!self::is_reactivated()) { | |
| 89 | + WP_Optimize()->get_onboarding()->activate_onboarding_wizard(); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 33 | 94 | * When non network admin tries to activate plugin, deactivate it and die with a message |
| 95 | + * | |
| 96 | + * @return void | |
| 34 | 97 | */ |
| 35 | - private static function deactivate_and_die() { | |
| 98 | + private static function deactivate_and_die(): void { | |
| 36 | 99 | deactivate_plugins(plugin_basename(WPO_PLUGIN_MAIN_PATH . 'wp-optimize.php')); |
| 37 | - wp_die(__('Only Network Administrator can activate WP-Optimize plugin.', 'wp-optimize') . | |
| 38 | - ' <a href="' . admin_url('plugins.php') . '">' . __('go back', 'wp-optimize') . '</a>'); | |
| 100 | + wp_die(esc_html__('Only Network Administrator can activate the WP-Optimize plugin.', 'wp-optimize') . | |
| 101 | + ' <a href="' . esc_url(admin_url('plugins.php')) . '">' . esc_html__('go back', 'wp-optimize') . '</a>'); | |
| 39 | 102 | } |
| 40 | 103 | |
| 41 | 104 | /** |
| 42 | 105 | * Decides whether the plugin is newly installed and activated or already installed and reactivated |
| 43 | 106 | * |
| 44 | - * @return mixed | |
| 107 | + * @return bool | |
| 45 | 108 | */ |
| 46 | - private static function is_reactivated() { | |
| 47 | - return WP_Optimize()->get_options()->get_option('last-optimized', false); | |
| 109 | + private static function is_reactivated(): bool { | |
| 110 | + return (bool) WP_Optimize()->get_options()->get_option('last-optimized'); | |
| 48 | 111 | } |
| 49 | 112 | |
| 50 | 113 | /** |
| 51 | - * Set plugin option `newly-activated` as `true` | |
| 114 | + * Records the first-ever activation timestamp. | |
| 115 | + * No-ops if already set, so the original install date is never overwritten on reactivation. | |
| 116 | + * | |
| 117 | + * @return void | |
| 52 | 118 | */ |
| 53 | - private static function set_as_newly_activated() { | |
| 54 | - WP_Optimize()->get_options()->update_option('newly-activated', true); | |
| 119 | + private static function set_as_newly_activated(): void { | |
| 120 | + if (!WP_Optimize()->get_options()->get_option('newly-activated')) { | |
| 121 | + WP_Optimize()->get_options()->update_option('newly-activated', time()); | |
| 122 | + } | |
| 55 | 123 | } |
| 56 | 124 | |
| 57 | 125 | /** |
| 58 | 126 | * Make use of Task Manager library |
| 127 | + * | |
| 128 | + * @return void | |
| 59 | 129 | */ |
| 60 | - private static function init_batch_processing() { | |
| 130 | + private static function init_batch_processing(): void { | |
| 61 | 131 | if (!class_exists('Updraft_Tasks_Activation')) { |
| 62 | 132 | require_once(WPO_PLUGIN_MAIN_PATH . 'vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-tasks-activation.php'); |
| 63 | 133 | } |
| 64 | 134 | Updraft_Tasks_Activation::init(WPO_PLUGIN_SLUG); |
| @@ -65,20 +135,33 @@ | ||
| 65 | 135 | Updraft_Tasks_Activation::reinstall_if_needed(); |
| 66 | 136 | } |
| 67 | 137 | |
| 68 | 138 | /** |
| 139 | + * Initialize premium plugin if the premium version is installed | |
| 140 | + * | |
| 141 | + * @return void | |
| 142 | + */ | |
| 143 | + private static function maybe_init_premium(): void { | |
| 144 | + if (self::is_premium()) { | |
| 145 | + self::init_premium(); | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 69 | 150 | * Decides whether activate plugin is premium version or not |
| 70 | 151 | * |
| 71 | 152 | * @return bool |
| 72 | 153 | */ |
| 73 | - private static function is_premium() { | |
| 154 | + private static function is_premium(): bool { | |
| 74 | 155 | return file_exists(WPO_PLUGIN_MAIN_PATH . 'premium.php'); |
| 75 | 156 | } |
| 76 | 157 | |
| 77 | 158 | /** |
| 78 | 159 | * Run premium plugin activation actions |
| 160 | + * | |
| 161 | + * @return void | |
| 79 | 162 | */ |
| 80 | - private static function init_premium() { | |
| 163 | + private static function init_premium(): void { | |
| 81 | 164 | if (!class_exists('WP_Optimize_Premium')) { |
| 82 | 165 | include_once(WPO_PLUGIN_MAIN_PATH . 'premium.php'); |
| 83 | 166 | } |
| 84 | 167 | WP_Optimize_Premium()->plugin_activation_actions(); |