| 1 |
<?php |
| 2 |
/** |
| 3 |
* Activator class. |
| 4 |
* |
| 5 |
* @package WebberZone\Top_Ten\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WebberZone\Top_Ten\Admin; |
| 9 |
|
| 10 |
use WebberZone\Top_Ten\Database; |
| 11 |
use WebberZone\Top_Ten\Util\Hook_Registry; |
| 12 |
use WebberZone\Top_Ten\Admin\Cron; |
| 13 |
|
| 14 |
if ( ! defined( 'WPINC' ) ) { |
| 15 |
die; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Handles plugin activation and creation of the Top 10 database tables. |
| 20 |
* |
| 21 |
* @since 3.3.0 |
| 22 |
*/ |
| 23 |
class Activator { |
| 24 |
|
| 25 |
/** |
| 26 |
* Name of the main table. |
| 27 |
* |
| 28 |
* @since 4.1.0 |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public static $table_name = 'top_ten'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Name of the daily table. |
| 35 |
* |
| 36 |
* @since 4.1.0 |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public static $table_name_daily = 'top_ten_daily'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Name of the visits log table. |
| 43 |
* |
| 44 |
* @since 4.3.0 |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
public static $table_name_log = 'top_ten_visits_log'; |
| 48 |
|
| 49 |
/** |
| 50 |
* Name of the visits funnel table. |
| 51 |
* |
| 52 |
* @since 4.3.0 |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
public static $table_name_funnel = 'top_ten_visits_funnel'; |
| 56 |
|
| 57 |
/** |
| 58 |
* Constructor class. |
| 59 |
* |
| 60 |
* @since 3.3.0 |
| 61 |
*/ |
| 62 |
public function __construct() { |
| 63 |
Hook_Registry::add_action( 'admin_init', array( $this, 'update_db_check' ) ); |
| 64 |
Hook_Registry::add_action( 'wp_initialize_site', array( $this, 'activate_new_site' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Fired when the plugin is Network Activated. |
| 69 |
* |
| 70 |
* @since 1.9.10.1 |
| 71 |
* |
| 72 |
* @param boolean $network_wide True if WPMU superadmin uses |
| 73 |
* "Network Activate" action, false if |
| 74 |
* WPMU is disabled or plugin is |
| 75 |
* activated on an individual blog. |
| 76 |
*/ |
| 77 |
public static function activation_hook( $network_wide ) { |
| 78 |
Database::clear_table_installation_cache(); |
| 79 |
|
| 80 |
if ( is_multisite() && $network_wide ) { |
| 81 |
$sites = get_sites( |
| 82 |
array( |
| 83 |
'archived' => 0, |
| 84 |
'spam' => 0, |
| 85 |
'deleted' => 0, |
| 86 |
) |
| 87 |
); |
| 88 |
|
| 89 |
foreach ( $sites as $site ) { |
| 90 |
switch_to_blog( (int) $site->blog_id ); |
| 91 |
self::single_activate(); |
| 92 |
} |
| 93 |
|
| 94 |
// Switch back to the current blog. |
| 95 |
restore_current_blog(); |
| 96 |
|
| 97 |
} else { |
| 98 |
self::single_activate(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Fired for each blog when the plugin is activated. |
| 104 |
* |
| 105 |
* @since 2.0.0 |
| 106 |
*/ |
| 107 |
public static function single_activate() { |
| 108 |
global $wpdb, $tptn_db_version; |
| 109 |
|
| 110 |
$table_name = $wpdb->base_prefix . self::$table_name; |
| 111 |
$table_name_daily = $wpdb->base_prefix . self::$table_name_daily; |
| 112 |
|
| 113 |
$table_name_log = $wpdb->base_prefix . self::$table_name_log; |
| 114 |
$table_name_funnel = $wpdb->base_prefix . self::$table_name_funnel; |
| 115 |
|
| 116 |
// Create tables if not exists. |
| 117 |
self::maybe_create_table( $table_name, self::create_full_table_sql() ); |
| 118 |
self::maybe_create_table( $table_name_daily, self::create_daily_table_sql() ); |
| 119 |
self::maybe_create_table( $table_name_log, Database::create_log_table_sql() ); |
| 120 |
self::maybe_create_table( $table_name_funnel, Database::create_funnel_table_sql() ); |
| 121 |
|
| 122 |
// Upgrade table code. |
| 123 |
$installed_ver = get_site_option( 'tptn_db_version' ); |
| 124 |
|
| 125 |
if ( $installed_ver != $tptn_db_version ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual |
| 126 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 127 |
|
| 128 |
$show_errors = $wpdb->hide_errors(); |
| 129 |
dbDelta( self::create_full_table_sql() ); |
| 130 |
dbDelta( self::create_daily_table_sql() ); |
| 131 |
dbDelta( Database::create_log_table_sql() ); |
| 132 |
dbDelta( Database::create_funnel_table_sql() ); |
| 133 |
$wpdb->show_errors( $show_errors ); |
| 134 |
|
| 135 |
if ( self::is_all_tables_installed() ) { |
| 136 |
update_site_option( 'tptn_db_version', $tptn_db_version ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
Cron::enable_aggregation_run(); |
| 141 |
|
| 142 |
/** |
| 143 |
* Fires after the plugin has been activated. |
| 144 |
* |
| 145 |
* @since 4.1.0 |
| 146 |
*/ |
| 147 |
do_action( 'tptn_activate' ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Check if the Top Ten table is installed. |
| 152 |
* |
| 153 |
* @since 4.1.0 |
| 154 |
* |
| 155 |
* @param string $table_name Table name. |
| 156 |
* @return bool True if the table exists, false otherwise. |
| 157 |
*/ |
| 158 |
public static function is_table_installed( $table_name ) { |
| 159 |
return Database::is_table_installed( $table_name ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Create table if not exists. |
| 164 |
* |
| 165 |
* @since 4.1.0 |
| 166 |
* |
| 167 |
* @param string $table_name Table name. |
| 168 |
* @param string $sql SQL to create the table. |
| 169 |
*/ |
| 170 |
public static function maybe_create_table( $table_name, $sql ) { |
| 171 |
if ( ! self::is_table_installed( $table_name ) ) { |
| 172 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 173 |
|
| 174 |
global $wpdb; |
| 175 |
$show_errors = $wpdb->hide_errors(); |
| 176 |
dbDelta( $sql ); |
| 177 |
$wpdb->show_errors( $show_errors ); |
| 178 |
Database::clear_table_installation_cache(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Create tables. |
| 184 |
* |
| 185 |
* @since 4.1.0 |
| 186 |
*/ |
| 187 |
public static function create_tables() { |
| 188 |
global $wpdb; |
| 189 |
|
| 190 |
$table_name = $wpdb->base_prefix . self::$table_name; |
| 191 |
$table_name_daily = $wpdb->base_prefix . self::$table_name_daily; |
| 192 |
$table_name_log = $wpdb->base_prefix . self::$table_name_log; |
| 193 |
$table_name_funnel = $wpdb->base_prefix . self::$table_name_funnel; |
| 194 |
|
| 195 |
self::maybe_create_table( $table_name, self::create_full_table_sql() ); |
| 196 |
self::maybe_create_table( $table_name_daily, self::create_daily_table_sql() ); |
| 197 |
self::maybe_create_table( $table_name_log, Database::create_log_table_sql() ); |
| 198 |
self::maybe_create_table( $table_name_funnel, Database::create_funnel_table_sql() ); |
| 199 |
Database::clear_table_installation_cache(); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Create full table sql. |
| 204 |
* |
| 205 |
* @since 4.1.0 |
| 206 |
* |
| 207 |
* @return string SQL to create the full table. |
| 208 |
*/ |
| 209 |
public static function create_full_table_sql() { |
| 210 |
return Database::create_full_table_sql(); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Create full daily table sql. |
| 215 |
* |
| 216 |
* @since 4.1.0 |
| 217 |
* |
| 218 |
* @return string SQL to create the daily table. |
| 219 |
*/ |
| 220 |
public static function create_daily_table_sql() { |
| 221 |
return Database::create_daily_table_sql(); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Recreate a table. |
| 226 |
* |
| 227 |
* This method recreates a table by creating a backup, dropping the original table, |
| 228 |
* and then creating a new table with the original name and inserting the data from the backup. |
| 229 |
* |
| 230 |
* @since 4.1.0 |
| 231 |
* |
| 232 |
* @param string $table_name The name of the table to recreate. |
| 233 |
* @param string $create_table_sql The SQL statement to create the new table. |
| 234 |
* @param bool $backup Whether to backup the table or not. |
| 235 |
* @param array $fields The fields to include in the temporary table and on duplicate key code. |
| 236 |
* @param array $group_by_fields The fields to group by in the temporary table. |
| 237 |
* |
| 238 |
* @return bool|\WP_Error True if recreated, error message if failed. |
| 239 |
*/ |
| 240 |
public static function recreate_table( |
| 241 |
$table_name, |
| 242 |
$create_table_sql, |
| 243 |
$backup = true, |
| 244 |
$fields = array( 'postnumber', 'cntaccess', 'blog_id' ), |
| 245 |
$group_by_fields = array( 'postnumber', 'blog_id' ) |
| 246 |
) { |
| 247 |
return Database::recreate_table( $table_name, $create_table_sql, $backup, $fields, $group_by_fields ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Recreate overall table. |
| 252 |
* |
| 253 |
* @since 4.1.0 |
| 254 |
* |
| 255 |
* @param bool $backup Whether to backup the table or not. |
| 256 |
* |
| 257 |
* @return bool|\WP_Error True if recreated, error message if failed. |
| 258 |
*/ |
| 259 |
public static function recreate_overall_table( $backup = true ) { |
| 260 |
return Database::recreate_overall_table( $backup ); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Recreate daily table. |
| 265 |
* |
| 266 |
* @since 4.1.0 |
| 267 |
* |
| 268 |
* @param bool $backup Whether to backup the table or not. |
| 269 |
* |
| 270 |
* @return bool|\WP_Error True if recreated, error message if failed. |
| 271 |
*/ |
| 272 |
public static function recreate_daily_table( $backup = true ) { |
| 273 |
return Database::recreate_daily_table( $backup ); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Fired when a new site is activated with a WPMU environment. |
| 278 |
* |
| 279 |
* @since 2.0.0 |
| 280 |
* |
| 281 |
* @param int|\WP_Site $blog WordPress 5.1 passes a WP_Site object. |
| 282 |
*/ |
| 283 |
public static function activate_new_site( $blog ) { |
| 284 |
|
| 285 |
if ( ! is_plugin_active_for_network( plugin_basename( TOP_TEN_PLUGIN_FILE ) ) ) { |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
if ( ! is_int( $blog ) ) { |
| 290 |
$blog = $blog->id; |
| 291 |
} |
| 292 |
|
| 293 |
switch_to_blog( $blog ); |
| 294 |
Database::clear_table_installation_cache(); |
| 295 |
self::single_activate(); |
| 296 |
restore_current_blog(); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Fired when the plugin is deactivated. |
| 301 |
* |
| 302 |
* @since 4.3.0 |
| 303 |
* |
| 304 |
* @param bool $network_wide True if WPMU superadmin uses "Network Deactivate" action, false otherwise. |
| 305 |
*/ |
| 306 |
public static function deactivation_hook( $network_wide ) { |
| 307 |
if ( is_multisite() && $network_wide ) { |
| 308 |
$sites = get_sites( |
| 309 |
array( |
| 310 |
'archived' => 0, |
| 311 |
'spam' => 0, |
| 312 |
'deleted' => 0, |
| 313 |
) |
| 314 |
); |
| 315 |
|
| 316 |
foreach ( $sites as $site ) { |
| 317 |
switch_to_blog( (int) $site->blog_id ); |
| 318 |
self::single_deactivate(); |
| 319 |
restore_current_blog(); |
| 320 |
} |
| 321 |
} else { |
| 322 |
self::single_deactivate(); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Fired for each blog when the plugin is deactivated. |
| 328 |
* |
| 329 |
* @since 4.3.0 |
| 330 |
*/ |
| 331 |
public static function single_deactivate() { |
| 332 |
Cron::disable_run(); |
| 333 |
Cron::disable_aggregation_run(); |
| 334 |
|
| 335 |
// Stop auto-loading the settings option while the plugin is inactive. |
| 336 |
update_option( 'tptn_settings', get_option( 'tptn_settings' ), false ); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Check and update database version. |
| 341 |
* |
| 342 |
* @since 3.3.0 |
| 343 |
*/ |
| 344 |
public static function update_db_check() { |
| 345 |
global $tptn_db_version, $network_wide; |
| 346 |
|
| 347 |
if ( get_site_option( 'tptn_db_version' ) != $tptn_db_version ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual |
| 348 |
self::activation_hook( $network_wide ); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Check if all required tables are installed. |
| 354 |
* |
| 355 |
* @since 4.1.0 |
| 356 |
* |
| 357 |
* @return bool True if all tables are installed, false if any are missing. |
| 358 |
*/ |
| 359 |
public static function is_all_tables_installed() { |
| 360 |
return Database::are_tables_installed() |
| 361 |
&& Database::is_table_installed( Database::get_log_table() ) |
| 362 |
&& Database::is_table_installed( Database::get_funnel_table() ); |
| 363 |
} |
| 364 |
} |
| 365 |
|