| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 3 |
|
| 4 |
if (!class_exists('WPO_Activation')) : |
| 5 |
|
| 6 |
class WPO_Activation { |
| 7 |
|
| 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 |
/** |
| 16 |
* Actions to be performed upon plugin activation |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public static function actions(): void { |
| 21 |
|
| 22 |
self::check_minimum_requirements(); |
| 23 |
self::check_user_capability(); |
| 24 |
self::handle_activation_type(); |
| 25 |
|
| 26 |
WP_Optimize()->get_options()->set_default_options(); |
| 27 |
WP_Optimize()->get_minify()->plugin_activate(); |
| 28 |
WP_Optimize()->get_gzip_compression()->restore(); |
| 29 |
WP_Optimize()->get_browser_cache()->restore(); |
| 30 |
WP_Optimize()->get_table_management()->create_plugin_tables(); |
| 31 |
WP_Optimize()->get_webp_instance()->plugin_activate(); |
| 32 |
|
| 33 |
WP_Optimize_Page_Cache_Preloader::instance()->maybe_reschedule_preload_on_activation(); |
| 34 |
|
| 35 |
self::init_batch_processing(); |
| 36 |
self::silence_activation_actions(); |
| 37 |
} |
| 38 |
|
| 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; |
| 48 |
} |
| 49 |
|
| 50 |
WP_Optimize()->cron_activate(); |
| 51 |
self::maybe_init_premium(); |
| 52 |
|
| 53 |
self::$silence_activation_actions_done = true; |
| 54 |
} |
| 55 |
|
| 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 |
/** |
| 94 |
* When non network admin tries to activate plugin, deactivate it and die with a message |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
private static function deactivate_and_die(): void { |
| 99 |
deactivate_plugins(plugin_basename(WPO_PLUGIN_MAIN_PATH . 'wp-optimize.php')); |
| 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>'); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Decides whether the plugin is newly installed and activated or already installed and reactivated |
| 106 |
* |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
private static function is_reactivated(): bool { |
| 110 |
return (bool) WP_Optimize()->get_options()->get_option('last-optimized'); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 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 |
| 118 |
*/ |
| 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 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Make use of Task Manager library |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
private static function init_batch_processing(): void { |
| 131 |
if (!class_exists('Updraft_Tasks_Activation')) { |
| 132 |
require_once(WPO_PLUGIN_MAIN_PATH . 'vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-tasks-activation.php'); |
| 133 |
} |
| 134 |
Updraft_Tasks_Activation::init(WPO_PLUGIN_SLUG); |
| 135 |
Updraft_Tasks_Activation::reinstall_if_needed(); |
| 136 |
} |
| 137 |
|
| 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 |
/** |
| 150 |
* Decides whether activate plugin is premium version or not |
| 151 |
* |
| 152 |
* @return bool |
| 153 |
*/ |
| 154 |
private static function is_premium(): bool { |
| 155 |
return file_exists(WPO_PLUGIN_MAIN_PATH . 'premium.php'); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Run premium plugin activation actions |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
private static function init_premium(): void { |
| 164 |
if (!class_exists('WP_Optimize_Premium')) { |
| 165 |
include_once(WPO_PLUGIN_MAIN_PATH . 'premium.php'); |
| 166 |
} |
| 167 |
WP_Optimize_Premium()->plugin_activation_actions(); |
| 168 |
} |
| 169 |
} |
| 170 |
endif; |
| 171 |
|