0, 'spam' => 0, 'deleted' => 0, ) ); foreach ( $sites as $site ) { switch_to_blog( (int) $site->blog_id ); self::single_activate(); } // Switch back to the current blog. restore_current_blog(); } else { self::single_activate(); } } /** * Fired for each blog when the plugin is activated. * * @since 2.0.0 */ public static function single_activate() { global $wpdb, $tptn_db_version; $table_name = $wpdb->base_prefix . self::$table_name; $table_name_daily = $wpdb->base_prefix . self::$table_name_daily; $table_name_log = $wpdb->base_prefix . self::$table_name_log; $table_name_funnel = $wpdb->base_prefix . self::$table_name_funnel; // Create tables if not exists. self::maybe_create_table( $table_name, self::create_full_table_sql() ); self::maybe_create_table( $table_name_daily, self::create_daily_table_sql() ); self::maybe_create_table( $table_name_log, Database::create_log_table_sql() ); self::maybe_create_table( $table_name_funnel, Database::create_funnel_table_sql() ); // Upgrade table code. $installed_ver = get_site_option( 'tptn_db_version' ); if ( $installed_ver != $tptn_db_version ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $show_errors = $wpdb->hide_errors(); dbDelta( self::create_full_table_sql() ); dbDelta( self::create_daily_table_sql() ); dbDelta( Database::create_log_table_sql() ); dbDelta( Database::create_funnel_table_sql() ); $wpdb->show_errors( $show_errors ); if ( self::is_all_tables_installed() ) { update_site_option( 'tptn_db_version', $tptn_db_version ); } } Cron::enable_aggregation_run(); /** * Fires after the plugin has been activated. * * @since 4.1.0 */ do_action( 'tptn_activate' ); } /** * Check if the Top Ten table is installed. * * @since 4.1.0 * * @param string $table_name Table name. * @return bool True if the table exists, false otherwise. */ public static function is_table_installed( $table_name ) { return Database::is_table_installed( $table_name ); } /** * Create table if not exists. * * @since 4.1.0 * * @param string $table_name Table name. * @param string $sql SQL to create the table. */ public static function maybe_create_table( $table_name, $sql ) { if ( ! self::is_table_installed( $table_name ) ) { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; global $wpdb; $show_errors = $wpdb->hide_errors(); dbDelta( $sql ); $wpdb->show_errors( $show_errors ); Database::clear_table_installation_cache(); } } /** * Create tables. * * @since 4.1.0 */ public static function create_tables() { global $wpdb; $table_name = $wpdb->base_prefix . self::$table_name; $table_name_daily = $wpdb->base_prefix . self::$table_name_daily; $table_name_log = $wpdb->base_prefix . self::$table_name_log; $table_name_funnel = $wpdb->base_prefix . self::$table_name_funnel; self::maybe_create_table( $table_name, self::create_full_table_sql() ); self::maybe_create_table( $table_name_daily, self::create_daily_table_sql() ); self::maybe_create_table( $table_name_log, Database::create_log_table_sql() ); self::maybe_create_table( $table_name_funnel, Database::create_funnel_table_sql() ); Database::clear_table_installation_cache(); } /** * Create full table sql. * * @since 4.1.0 * * @return string SQL to create the full table. */ public static function create_full_table_sql() { return Database::create_full_table_sql(); } /** * Create full daily table sql. * * @since 4.1.0 * * @return string SQL to create the daily table. */ public static function create_daily_table_sql() { return Database::create_daily_table_sql(); } /** * Recreate a table. * * This method recreates a table by creating a backup, dropping the original table, * and then creating a new table with the original name and inserting the data from the backup. * * @since 4.1.0 * * @param string $table_name The name of the table to recreate. * @param string $create_table_sql The SQL statement to create the new table. * @param bool $backup Whether to backup the table or not. * @param array $fields The fields to include in the temporary table and on duplicate key code. * @param array $group_by_fields The fields to group by in the temporary table. * * @return bool|\WP_Error True if recreated, error message if failed. */ public static function recreate_table( $table_name, $create_table_sql, $backup = true, $fields = array( 'postnumber', 'cntaccess', 'blog_id' ), $group_by_fields = array( 'postnumber', 'blog_id' ) ) { return Database::recreate_table( $table_name, $create_table_sql, $backup, $fields, $group_by_fields ); } /** * Recreate overall table. * * @since 4.1.0 * * @param bool $backup Whether to backup the table or not. * * @return bool|\WP_Error True if recreated, error message if failed. */ public static function recreate_overall_table( $backup = true ) { return Database::recreate_overall_table( $backup ); } /** * Recreate daily table. * * @since 4.1.0 * * @param bool $backup Whether to backup the table or not. * * @return bool|\WP_Error True if recreated, error message if failed. */ public static function recreate_daily_table( $backup = true ) { return Database::recreate_daily_table( $backup ); } /** * Fired when a new site is activated with a WPMU environment. * * @since 2.0.0 * * @param int|\WP_Site $blog WordPress 5.1 passes a WP_Site object. */ public static function activate_new_site( $blog ) { if ( ! is_plugin_active_for_network( plugin_basename( TOP_TEN_PLUGIN_FILE ) ) ) { return; } if ( ! is_int( $blog ) ) { $blog = $blog->id; } switch_to_blog( $blog ); Database::clear_table_installation_cache(); self::single_activate(); restore_current_blog(); } /** * Fired when the plugin is deactivated. * * @since 4.3.0 * * @param bool $network_wide True if WPMU superadmin uses "Network Deactivate" action, false otherwise. */ public static function deactivation_hook( $network_wide ) { if ( is_multisite() && $network_wide ) { $sites = get_sites( array( 'archived' => 0, 'spam' => 0, 'deleted' => 0, ) ); foreach ( $sites as $site ) { switch_to_blog( (int) $site->blog_id ); self::single_deactivate(); restore_current_blog(); } } else { self::single_deactivate(); } } /** * Fired for each blog when the plugin is deactivated. * * @since 4.3.0 */ public static function single_deactivate() { Cron::disable_run(); Cron::disable_aggregation_run(); // Stop auto-loading the settings option while the plugin is inactive. update_option( 'tptn_settings', get_option( 'tptn_settings' ), false ); } /** * Check and update database version. * * @since 3.3.0 */ public static function update_db_check() { global $tptn_db_version, $network_wide; if ( get_site_option( 'tptn_db_version' ) != $tptn_db_version ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual self::activation_hook( $network_wide ); } } /** * Check if all required tables are installed. * * @since 4.1.0 * * @return bool True if all tables are installed, false if any are missing. */ public static function is_all_tables_installed() { return Database::are_tables_installed() && Database::is_table_installed( Database::get_log_table() ) && Database::is_table_installed( Database::get_funnel_table() ); } }