prefix . 'srpp_stats'; } /** * Runs on activation. */ public static function activate() { SRPP_Display::register_post_type(); self::create_tables(); self::default_options(); self::maybe_seed(); flush_rewrite_rules(); update_option( 'srpp_db_version', self::DB_VERSION ); } /** * Runs on deactivation. */ public static function deactivate() { wp_clear_scheduled_hook( 'srpp_cleanup_event' ); flush_rewrite_rules(); } /** * Run upgrades when the stored version is behind. */ public static function maybe_upgrade() { if ( get_option( 'srpp_db_version' ) !== self::DB_VERSION ) { self::create_tables(); self::default_options(); update_option( 'srpp_db_version', self::DB_VERSION ); } } /** * Create the aggregated statistics table. */ public static function create_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = self::stats_table(); $collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE {$table} ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, display_id BIGINT UNSIGNED NOT NULL DEFAULT 0, source_id BIGINT UNSIGNED NOT NULL DEFAULT 0, target_id BIGINT UNSIGNED NOT NULL DEFAULT 0, template VARCHAR(32) NOT NULL DEFAULT '', placement VARCHAR(48) NOT NULL DEFAULT '', stat_date DATE NOT NULL, impressions BIGINT UNSIGNED NOT NULL DEFAULT 0, clicks BIGINT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY row_key (display_id,source_id,target_id,stat_date), KEY display_date (display_id,stat_date), KEY tpl (template), KEY plc (placement) ) {$collate};"; dbDelta( $sql ); } /** * Seed default plugin options. */ public static function default_options() { $defaults = array( 'analytics_enabled' => 1, 'analytics_retention' => 180, 'cache_enabled' => 1, 'load_base_css' => 1, 'lazy_images' => 1, 'respect_noindex' => 0, 'uninstall_delete' => 0, ); $current = get_option( 'srpp_settings', array() ); update_option( 'srpp_settings', wp_parse_args( $current, $defaults ) ); } /** * Create one starter Display so the plugin does something out of the box. */ public static function maybe_seed() { if ( get_option( 'srpp_seeded' ) ) { return; } $existing = get_posts( array( 'post_type' => SRPP_CPT, 'post_status' => 'any', 'posts_per_page' => 1, 'fields' => 'ids', ) ); if ( empty( $existing ) ) { $config = srpp_default_config(); $config['heading'] = __( 'You may also like', 'advanced-css3-related-posts-widget' ); SRPP_Display::save( 0, array( 'title' => __( 'Blog recommendations', 'advanced-css3-related-posts-widget' ), 'status' => 'publish', 'config' => $config, ) ); } update_option( 'srpp_seeded', 1 ); } }