'ids', 'number' => 500, ) ); foreach ( $sites as $site_id ) { switch_to_blog( $site_id ); self::install(); restore_current_blog(); } return; } self::install(); } /** * Runs on plugin deactivation. */ public static function deactivate() { wp_clear_scheduled_hook( self::CRON_HOOK ); } /** * Installs on a site created after network activation. * * @param \WP_Site $site New site. */ public static function install_new_site( $site ) { if ( ! function_exists( 'is_plugin_active_for_network' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( is_plugin_active_for_network( BUILDIFY_BASENAME ) ) { switch_to_blog( $site->blog_id ); self::install(); restore_current_blog(); } } /** * Upgrades the database schema when the version changed. */ public static function maybe_upgrade() { add_action( 'wp_initialize_site', array( __CLASS__, 'install_new_site' ), 200 ); add_action( self::CRON_HOOK, array( __CLASS__, 'daily_maintenance' ) ); if ( get_option( 'buildify_db_version' ) !== BUILDIFY_DB_VERSION ) { self::install(); } } /** * Creates the table, default options and the cron event. */ public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = $wpdb->prefix . 'buildify_popup_stats'; $charset = $wpdb->get_charset_collate(); dbDelta( "CREATE TABLE {$table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, popup_id bigint(20) unsigned NOT NULL, stat_date date NOT NULL, views int(10) unsigned NOT NULL DEFAULT 0, closes int(10) unsigned NOT NULL DEFAULT 0, conversions int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY popup_date (popup_id,stat_date), KEY stat_date (stat_date) ) {$charset};" ); add_option( Settings::OPTION, Settings::defaults() ); update_option( 'buildify_db_version', BUILDIFY_DB_VERSION ); if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_HOOK ); } // Conditions are rebuilt on the next request. delete_option( Conditions::INDEX_OPTION ); } /** * Removes old statistics rows. */ public static function daily_maintenance() { global $wpdb; $days = (int) Settings::get( 'stats_retention', 180 ); $cutoff = gmdate( 'Y-m-d', time() - ( $days * DAY_IN_SECONDS ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table cleanup, nothing to cache. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}buildify_popup_stats WHERE stat_date < %s", $cutoff ) ); } }