get_charset_collate(); $sql = "CREATE TABLE {$wpdb->prefix}aidf_crawler_log ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, bot_name varchar(50) NOT NULL, bot_ua varchar(500) NOT NULL DEFAULT '', url_path varchar(500) NOT NULL DEFAULT '', status_code smallint(5) unsigned NOT NULL DEFAULT 200, created_at datetime NOT NULL, PRIMARY KEY (id), KEY bot_date (bot_name, created_at), KEY date_idx (created_at) ) $charset_collate; CREATE TABLE {$wpdb->prefix}aidf_crawler_summary ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, bot_name varchar(50) NOT NULL, log_date date NOT NULL, hit_count int(10) unsigned NOT NULL DEFAULT 0, top_page varchar(500) NOT NULL DEFAULT '', status_200 int(10) unsigned NOT NULL DEFAULT 0, status_403 int(10) unsigned NOT NULL DEFAULT 0, status_other int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY bot_day (bot_name, log_date), KEY date_idx (log_date) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); update_option( 'aidf_db_version', AIDF_VERSION ); } /** * Activation hook. */ function aidf_activate() { AIDF_Server::register_rewrite_rules(); flush_rewrite_rules(); if ( false === get_option( 'aidf_settings' ) ) { update_option( 'aidf_settings', AIDF_Plugin::get_defaults() ); } if ( false === get_option( 'aidf_activated_at' ) ) { update_option( 'aidf_activated_at', time(), false ); } set_transient( 'aidf_activation_redirect', true, 30 ); aidf_create_crawler_tables(); } register_activation_hook( __FILE__, 'aidf_activate' ); /** * Deactivation hook. */ function aidf_deactivate() { flush_rewrite_rules(); AIDF_Crawler_Logger::clear_cron(); } register_deactivation_hook( __FILE__, 'aidf_deactivate' ); /** * Boot the plugin. */ function aidf_init() { AIDF_Plugin::get_instance(); } add_action( 'plugins_loaded', 'aidf_init' ); /** * Cron: prune old crawler log entries. */ add_action( 'aidf_prune_crawler_log', array( 'AIDF_Crawler_Logger', 'prune_log' ) ); /** * Cron: rebuild crawler summary table from raw log data. */ add_action( 'aidf_rebuild_crawler_summary', array( 'AIDF_Crawler_Analytics', 'rebuild_summary' ) ); /** * Run DB migrations when the plugin version has advanced. * * Hooked to admin_init so it runs on every admin page load, but only * performs work when the stored version is behind the current version. */ function aidf_maybe_upgrade_db() { $installed_version = get_option( 'aidf_db_version', '0' ); if ( version_compare( $installed_version, AIDF_VERSION, '<' ) ) { aidf_create_crawler_tables(); } } add_action( 'admin_init', 'aidf_maybe_upgrade_db' );