'ids', 'number' => 0, ) ) as $site_id ) { switch_to_blog( $site_id ); self::install(); restore_current_blog(); } return; } self::install(); } /** * Deactivation callback. * * @param bool $network_wide Whether the plugin is being network deactivated. */ public static function deactivate( $network_wide = false ) { if ( is_multisite() && $network_wide ) { foreach ( get_sites( array( 'fields' => 'ids', 'number' => 0, ) ) as $site_id ) { switch_to_blog( $site_id ); wp_clear_scheduled_hook( self::CRON_HOOK ); restore_current_blog(); } return; } wp_clear_scheduled_hook( self::CRON_HOOK ); } /** * Installs Toolvane 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( TOOLVANE_BASENAME ) ) { return; } switch_to_blog( $site->blog_id ); self::install(); restore_current_blog(); } /** * Adds the Toolvane tables to the list of tables dropped when a site is deleted. * * @param string[] $tables Table names. * @return string[] */ public static function drop_tables( $tables ) { $tables[] = self::table_404(); return $tables; } /** * Runs the installer if the stored database version is outdated. */ public static function maybe_upgrade() { if ( get_option( self::DB_VERSION_OPTION ) !== TOOLVANE_DB_VERSION ) { self::install(); } } /** * Creates tables, default options and cron events for the current site. */ public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = self::table_404(); $charset = $wpdb->get_charset_collate(); dbDelta( "CREATE TABLE {$table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, url text NOT NULL, url_hash char(32) NOT NULL, referrer text NULL, user_agent varchar(255) NOT NULL DEFAULT '', hits int(10) unsigned NOT NULL DEFAULT 1, first_seen datetime NOT NULL, last_seen datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY url_hash (url_hash), KEY last_seen (last_seen) ) {$charset};" ); add_option( Plugin::MODULES_OPTION, array(), '', true ); update_option( self::DB_VERSION_OPTION, TOOLVANE_DB_VERSION, true ); if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_HOOK ); } } /** * 404 log table name. * * @return string */ public static function table_404() { global $wpdb; return $wpdb->prefix . 'toolvane_404'; } }