PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.2
WebberZone Top 10 — Popular Posts v4.3.2
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / includes / admin / class-activator.php

class-activator.php in WebberZone Top 10 — Popular Posts 4.3.2, at includes/admin/class-activator.php

361 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Admin Columns Class.
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
79 if ( is_multisite() && $network_wide ) {
80 $sites = get_sites(
81 array(
82 'archived' => 0,
83 'spam' => 0,
84 'deleted' => 0,
85 )
86 );
87
88 foreach ( $sites as $site ) {
89 switch_to_blog( (int) $site->blog_id );
90 self::single_activate();
91 }
92
93 // Switch back to the current blog.
94 restore_current_blog();
95
96 } else {
97 self::single_activate();
98 }
99 }
100
101 /**
102 * Fired for each blog when the plugin is activated.
103 *
104 * @since 2.0.0
105 */
106 public static function single_activate() {
107 global $wpdb, $tptn_db_version;
108
109 $table_name = $wpdb->base_prefix . self::$table_name;
110 $table_name_daily = $wpdb->base_prefix . self::$table_name_daily;
111
112 $table_name_log = $wpdb->base_prefix . self::$table_name_log;
113 $table_name_funnel = $wpdb->base_prefix . self::$table_name_funnel;
114
115 // Create tables if not exists.
116 self::maybe_create_table( $table_name, self::create_full_table_sql() );
117 self::maybe_create_table( $table_name_daily, self::create_daily_table_sql() );
118 self::maybe_create_table( $table_name_log, Database::create_log_table_sql() );
119 self::maybe_create_table( $table_name_funnel, Database::create_funnel_table_sql() );
120
121 // Upgrade table code.
122 $installed_ver = get_site_option( 'tptn_db_version' );
123
124 if ( $installed_ver != $tptn_db_version ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
125 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
126
127 $wpdb->hide_errors();
128 dbDelta( self::create_full_table_sql() );
129 dbDelta( self::create_daily_table_sql() );
130 dbDelta( Database::create_log_table_sql() );
131 dbDelta( Database::create_funnel_table_sql() );
132 $wpdb->show_errors();
133
134 if ( self::is_all_tables_installed() ) {
135 update_site_option( 'tptn_db_version', $tptn_db_version );
136 }
137 }
138
139 Cron::enable_aggregation_run();
140
141 /**
142 * Fires after the plugin has been activated.
143 *
144 * @since 4.1.0
145 */
146 do_action( 'tptn_activate' );
147 }
148
149 /**
150 * Check if the Top Ten table is installed.
151 *
152 * @since 4.1.0
153 *
154 * @param string $table_name Table name.
155 * @return bool True if the table exists, false otherwise.
156 */
157 public static function is_table_installed( $table_name ) {
158 return Database::is_table_installed( $table_name );
159 }
160
161 /**
162 * Create table if not exists.
163 *
164 * @since 4.1.0
165 *
166 * @param string $table_name Table name.
167 * @param string $sql SQL to create the table.
168 */
169 public static function maybe_create_table( $table_name, $sql ) {
170 if ( ! self::is_table_installed( $table_name ) ) {
171 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
172
173 global $wpdb;
174 $wpdb->hide_errors();
175 dbDelta( $sql );
176 $wpdb->show_errors();
177 }
178 }
179
180 /**
181 * Create tables.
182 *
183 * @since 4.1.0
184 */
185 public static function create_tables() {
186 global $wpdb;
187
188 $table_name = $wpdb->base_prefix . self::$table_name;
189 $table_name_daily = $wpdb->base_prefix . self::$table_name_daily;
190 $table_name_log = $wpdb->base_prefix . self::$table_name_log;
191 $table_name_funnel = $wpdb->base_prefix . self::$table_name_funnel;
192
193 self::maybe_create_table( $table_name, self::create_full_table_sql() );
194 self::maybe_create_table( $table_name_daily, self::create_daily_table_sql() );
195 self::maybe_create_table( $table_name_log, Database::create_log_table_sql() );
196 self::maybe_create_table( $table_name_funnel, Database::create_funnel_table_sql() );
197 }
198
199 /**
200 * Create full table sql.
201 *
202 * @since 4.1.0
203 *
204 * @return string SQL to create the full table.
205 */
206 public static function create_full_table_sql() {
207 return Database::create_full_table_sql();
208 }
209
210 /**
211 * Create full daily table sql.
212 *
213 * @since 4.1.0
214 *
215 * @return string SQL to create the daily table.
216 */
217 public static function create_daily_table_sql() {
218 return Database::create_daily_table_sql();
219 }
220
221 /**
222 * Recreate a table.
223 *
224 * This method recreates a table by creating a backup, dropping the original table,
225 * and then creating a new table with the original name and inserting the data from the backup.
226 *
227 * @since 4.1.0
228 *
229 * @param string $table_name The name of the table to recreate.
230 * @param string $create_table_sql The SQL statement to create the new table.
231 * @param bool $backup Whether to backup the table or not.
232 * @param array $fields The fields to include in the temporary table and on duplicate key code.
233 * @param array $group_by_fields The fields to group by in the temporary table.
234 *
235 * @return bool|\WP_Error True if recreated, error message if failed.
236 */
237 public static function recreate_table(
238 $table_name,
239 $create_table_sql,
240 $backup = true,
241 $fields = array( 'postnumber', 'cntaccess', 'blog_id' ),
242 $group_by_fields = array( 'postnumber', 'blog_id' )
243 ) {
244 return Database::recreate_table( $table_name, $create_table_sql, $backup, $fields, $group_by_fields );
245 }
246
247 /**
248 * Recreate overall table.
249 *
250 * @since 4.1.0
251 *
252 * @param bool $backup Whether to backup the table or not.
253 *
254 * @return bool|\WP_Error True if recreated, error message if failed.
255 */
256 public static function recreate_overall_table( $backup = true ) {
257 return Database::recreate_overall_table( $backup );
258 }
259
260 /**
261 * Recreate daily table.
262 *
263 * @since 4.1.0
264 *
265 * @param bool $backup Whether to backup the table or not.
266 *
267 * @return bool|\WP_Error True if recreated, error message if failed.
268 */
269 public static function recreate_daily_table( $backup = true ) {
270 return Database::recreate_daily_table( $backup );
271 }
272
273 /**
274 * Fired when a new site is activated with a WPMU environment.
275 *
276 * @since 2.0.0
277 *
278 * @param int|\WP_Site $blog WordPress 5.1 passes a WP_Site object.
279 */
280 public static function activate_new_site( $blog ) {
281
282 if ( ! is_plugin_active_for_network( plugin_basename( TOP_TEN_PLUGIN_FILE ) ) ) {
283 return;
284 }
285
286 if ( ! is_int( $blog ) ) {
287 $blog = $blog->id;
288 }
289
290 switch_to_blog( $blog );
291 self::single_activate();
292 restore_current_blog();
293 }
294
295 /**
296 * Fired when the plugin is deactivated.
297 *
298 * @since 4.3.0
299 *
300 * @param bool $network_wide True if WPMU superadmin uses "Network Deactivate" action, false otherwise.
301 */
302 public static function deactivation_hook( $network_wide ) {
303 if ( is_multisite() && $network_wide ) {
304 $sites = get_sites(
305 array(
306 'archived' => 0,
307 'spam' => 0,
308 'deleted' => 0,
309 )
310 );
311
312 foreach ( $sites as $site ) {
313 switch_to_blog( (int) $site->blog_id );
314 self::single_deactivate();
315 restore_current_blog();
316 }
317 } else {
318 self::single_deactivate();
319 }
320 }
321
322 /**
323 * Fired for each blog when the plugin is deactivated.
324 *
325 * @since 4.3.0
326 */
327 public static function single_deactivate() {
328 Cron::disable_run();
329 Cron::disable_aggregation_run();
330
331 // Stop auto-loading the settings option while the plugin is inactive.
332 update_option( 'tptn_settings', get_option( 'tptn_settings' ), false );
333 }
334
335 /**
336 * Check and update database version.
337 *
338 * @since 3.3.0
339 */
340 public static function update_db_check() {
341 global $tptn_db_version, $network_wide;
342
343 if ( get_site_option( 'tptn_db_version' ) != $tptn_db_version ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
344 self::activation_hook( $network_wide );
345 }
346 }
347
348 /**
349 * Check if all required tables are installed.
350 *
351 * @since 4.1.0
352 *
353 * @return bool True if all tables are installed, false if any are missing.
354 */
355 public static function is_all_tables_installed() {
356 return Database::are_tables_installed()
357 && Database::is_table_installed( Database::get_log_table() )
358 && Database::is_table_installed( Database::get_funnel_table() );
359 }
360 }
361