query( $wpdb->prepare( "INSERT INTO `{$wpdb->prefix}feednetic_source_stats` (source_id, stat_date, followers_count, media_count) VALUES (%d, CURDATE(), %s, %s) ON DUPLICATE KEY UPDATE followers_count = VALUES(followers_count), media_count = VALUES(media_count)", $sourceId, null !== $followersCount ? (string) $followersCount : null, null !== $mediaCount ? (string) $mediaCount : null ) ); } /** * Returns the followers_count recorded $days ago for a source, or null. * * @param int $sourceId Source primary key. * @param int $days How many days back to look (default 7). * @return int|null */ public function getFollowersDeltaDays( int $sourceId, int $days = 7 ): ?int { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $value = $wpdb->get_var( $wpdb->prepare( "SELECT followers_count FROM `{$wpdb->prefix}feednetic_source_stats` WHERE source_id = %d AND stat_date = DATE_SUB(CURDATE(), INTERVAL %d DAY)", $sourceId, $days ) ); return null !== $value ? (int) $value : null; } /** * Upserts a stats row for a specific date (used by FakeDevSeeder). * * @param int $sourceId Source primary key. * @param string $date Date string in Y-m-d format. * @param int|null $followersCount Follower count to store, or null. * @param int|null $mediaCount Post/media count to store, or null. */ public function upsertByDate( int $sourceId, string $date, ?int $followersCount, ?int $mediaCount ): void { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->query( $wpdb->prepare( "INSERT INTO `{$wpdb->prefix}feednetic_source_stats` (source_id, stat_date, followers_count, media_count) VALUES (%d, %s, %s, %s) ON DUPLICATE KEY UPDATE followers_count = VALUES(followers_count), media_count = VALUES(media_count)", $sourceId, $date, null !== $followersCount ? (string) $followersCount : null, null !== $mediaCount ? (string) $mediaCount : null ) ); } /** * Deletes rows older than 90 days. Called by CleanupJob. */ public function pruneOld(): int { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $deleted = (int) $wpdb->query( "DELETE FROM `{$wpdb->prefix}feednetic_source_stats` WHERE stat_date < DATE_SUB(CURDATE(), INTERVAL 90 DAY)" ); return $deleted; } }