prefix . self::TABLE_SUFFIX; $charset = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS {$table} ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, keyword VARCHAR(255) NOT NULL, position SMALLINT NOT NULL DEFAULT 0, url VARCHAR(512) NOT NULL, checked_at DATE NOT NULL, PRIMARY KEY (id), KEY keyword (keyword), KEY checked_at (checked_at) ) {$charset};"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } public static function get_keywords() { return get_option( self::OPTION_KEYWORDS, [] ); } public static function add_keyword( $keyword, $target_url = '' ) { $keywords = self::get_keywords(); $keyword = sanitize_text_field( strtolower( trim( $keyword ) ) ); if ( ! $keyword || isset( $keywords[ $keyword ] ) ) return false; $keywords[ $keyword ] = [ 'url' => esc_url_raw( $target_url ), 'added' => current_time( 'mysql' ), 'alert_drop' => 5, // alert if drops more than 5 positions ]; update_option( self::OPTION_KEYWORDS, $keywords ); return true; } public static function remove_keyword( $keyword ) { $keywords = self::get_keywords(); unset( $keywords[ sanitize_text_field( $keyword ) ] ); update_option( self::OPTION_KEYWORDS, $keywords ); } public static function get_history( $keyword, $days = 30 ) { global $wpdb; $table = $wpdb->prefix . self::TABLE_SUFFIX; return $wpdb->get_results( $wpdb->prepare( "SELECT position, url, checked_at FROM {$table} WHERE keyword = %s AND checked_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY) ORDER BY checked_at ASC", $keyword, $days ) ); } /** * Fetches the current GSC position for a single keyword and stores it. * Called immediately when a keyword is added so the first data point is * available without waiting for the next daily cron run. * Returns the position (int) or null if unavailable. */ public static function fetch_position( $keyword ) { $gsc = AWBSEO_GSC::get_instance(); $keywords = self::get_keywords(); $config = $keywords[ $keyword ] ?? []; $position = $gsc->is_connected() ? $gsc->get_keyword_position( $keyword ) : self::serp_check( $keyword ); if ( $position === null ) return null; self::store_position( $keyword, (int) round( $position ), $config['url'] ?? '' ); return (int) round( $position ); } /** * Runs daily - checks positions via Google Search Console API. * Falls back to a basic SERP check if GSC isn't connected. */ public static function run_checks() { $keywords = self::get_keywords(); if ( empty( $keywords ) ) return; $gsc = AWBSEO_GSC::get_instance(); foreach ( $keywords as $keyword => $config ) { $position = $gsc->is_connected() ? $gsc->get_keyword_position( $keyword ) : self::serp_check( $keyword ); if ( $position === null ) continue; self::store_position( $keyword, $position, $config['url'] ?? '' ); self::maybe_send_alert( $keyword, $position, $config ); } } private static function store_position( $keyword, $position, $url ) { global $wpdb; $wpdb->replace( $wpdb->prefix . self::TABLE_SUFFIX, [ 'keyword' => $keyword, 'position' => $position, 'url' => $url, 'checked_at' => current_time( 'Y-m-d' ), ], [ '%s', '%d', '%s', '%s' ] ); } private static function maybe_send_alert( $keyword, $current_pos, $config ) { global $wpdb; $table = $wpdb->prefix . self::TABLE_SUFFIX; // Get yesterday's position. $yesterday = $wpdb->get_var( $wpdb->prepare( "SELECT position FROM {$table} WHERE keyword = %s AND checked_at = DATE_SUB(CURDATE(), INTERVAL 1 DAY)", $keyword ) ); if ( $yesterday === null ) return; $drop = (int) $current_pos - (int) $yesterday; if ( $drop < ( $config['alert_drop'] ?? 5 ) ) return; $admin_email = get_option( 'admin_email' ); wp_mail( $admin_email, sprintf( '[AWBSEO] Ranking drop alert: "%s"', $keyword ), sprintf( "Your keyword \"%s\" dropped %d positions (from #%d to #%d).\n\nLog in to CitedSEO → Rank Tracker to view the full history.\n\n%s", $keyword, $drop, $yesterday, $current_pos, admin_url( 'admin.php?page=awb-seo-rank-tracker' ) ) ); } /** * Basic SERP position check via Google search (fallback when GSC not connected). * Note: for production use, integrate a proper SERP API (ValueSERP, DataForSEO, etc.) */ private static function serp_check( $keyword ) { // Placeholder - integrate a SERP API in production. return null; } }