> */ public function findByPostId( int $postId ): array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}feednetic_media WHERE post_id = %d ORDER BY position ASC", $postId ), ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * Returns pending media rows for a post. * * @return array> */ public function findPendingByPostId( int $postId ): array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}feednetic_media WHERE post_id = %d AND status = 'pending' ORDER BY position ASC", $postId ), ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * Returns all pending media rows for posts belonging to a given source. * * Used to fire process_media jobs immediately after the first fetch so that * images appear without waiting for the next WP-Cron cycle. * * @return array> */ public function findPendingBySource( int $sourceId ): array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $rows = $wpdb->get_results( $wpdb->prepare( "SELECT m.* FROM {$wpdb->prefix}feednetic_media m INNER JOIN {$wpdb->prefix}feednetic_posts p ON p.id = m.post_id WHERE p.source_id = %d AND m.status = 'pending' ORDER BY p.published_at DESC, m.position ASC", $sourceId ), ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * Syncs media rows for a post from a set of Media models. * * Media is identified by (post_id, position). Instagram CDN URLs change on * every fetch (signed, expiring params), so URL equality is NOT a reliable * identity signal. A row whose status is 'done' has already been downloaded * to local storage; the CDN URL is irrelevant and the row is left untouched. * A row that is still 'pending' or 'failed' gets its URL refreshed (latest * signed URL) so the download job can use it. * * @param Media[] $mediaItems */ public function syncForPost( int $postId, array $mediaItems ): void { $existing = []; foreach ( $this->findByPostId( $postId ) as $row ) { $existing[ (int) $row['position'] ] = $row; } $newPositions = []; foreach ( $mediaItems as $media ) { $newPositions[] = $media->position; $existingRow = $existing[ $media->position ] ?? null; if ( null === $existingRow ) { $this->insert( $postId, $media ); continue; } if ( 'done' === $existingRow['status'] ) { // Already stored locally; nothing to do regardless of URL change. continue; } // Still waiting for download: refresh URL and reset for another attempt. $this->refreshForPending( (int) $existingRow['id'], $media ); } foreach ( array_keys( $existing ) as $position ) { if ( ! in_array( $position, $newPositions, true ) ) { $this->delete( (int) $existing[ $position ]['id'] ); } } } /** * Marks a media row as successfully downloaded with variant paths. * * @param array $variants Variant map { "320": { "webp": path, "jpg": path }, … }. */ public function markDone( int $id, array $variants, ?int $width, ?int $height ): void { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->update( "{$wpdb->prefix}feednetic_media", [ 'status' => 'done', 'variants' => (string) wp_json_encode( $variants ), 'width' => $width, 'height' => $height, 'updated_at' => gmdate( 'Y-m-d H:i:s' ), ], [ 'id' => $id ], [ '%s', '%s', '%d', '%d', '%s' ], [ '%d' ] ); } /** * Marks a media row as permanently failed. */ public function markFailed( int $id ): void { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->update( "{$wpdb->prefix}feednetic_media", [ 'status' => 'failed', 'updated_at' => gmdate( 'Y-m-d H:i:s' ), ], [ 'id' => $id ], [ '%s', '%s' ], [ '%d' ] ); } /** * Increments the download attempt counter. */ public function incrementAttempts( int $id ): void { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}feednetic_media SET attempts = attempts + 1, updated_at = %s WHERE id = %d", gmdate( 'Y-m-d H:i:s' ), $id ) ); } /** * Returns the position-0, status=done media row for each requested post ID. * One SQL query for the whole batch; result is keyed by post_id. * * @param int[] $postIds * @return array> post_id → media row */ public function findFirstDoneByPostIds( array $postIds ): array { if ( empty( $postIds ) ) { return []; } global $wpdb; $ints = array_map( 'intval', $postIds ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}feednetic_media WHERE post_id IN (" . implode( ',', array_fill( 0, count( $ints ), '%d' ) ) . ") AND position = 0 AND status = 'done'", $ints ), ARRAY_A ); $map = []; foreach ( is_array( $rows ) ? $rows : [] as $row ) { $map[ (int) $row['post_id'] ] = $row; } return $map; } /** * Returns the position-0 media row (any status) for each requested post ID. * When multiple rows exist at position 0, the 'done' row takes precedence. * One SQL query for the whole batch; result is keyed by post_id. * * @param int[] $postIds * @return array> post_id → media row */ public function findFirstAnyByPostIds( array $postIds ): array { if ( empty( $postIds ) ) { return []; } global $wpdb; $ints = array_map( 'intval', $postIds ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}feednetic_media WHERE post_id IN (" . implode( ',', array_fill( 0, count( $ints ), '%d' ) ) . ') AND position = 0', $ints ), ARRAY_A ); $map = []; foreach ( is_array( $rows ) ? $rows : [] as $row ) { $pid = (int) $row['post_id']; if ( ! isset( $map[ $pid ] ) || 'done' === $row['status'] ) { $map[ $pid ] = $row; } } return $map; } /** * Returns the number of media rows per post (all statuses) for a batch of posts. * One SQL query; posts without media are absent from the result. * * @param int[] $postIds * @return array post_id → media count */ public function countByPostIds( array $postIds ): array { if ( empty( $postIds ) ) { return []; } global $wpdb; $ints = array_map( 'intval', $postIds ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table $rows = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, COUNT(*) AS n FROM {$wpdb->prefix}feednetic_media WHERE post_id IN (" . implode( ',', array_fill( 0, count( $ints ), '%d' ) ) . ') GROUP BY post_id', $ints ), ARRAY_A ); $map = []; foreach ( is_array( $rows ) ? $rows : [] as $row ) { $map[ (int) $row['post_id'] ] = (int) $row['n']; } return $map; } /** * Deletes all media rows for a post. */ /** * Returns the count of media rows with status = 'failed'. */ public function countFailed(): int { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}feednetic_media WHERE status = 'failed'" // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared ); } /** * Resets all failed media rows back to pending (zero attempts) and returns * the number of rows reset. ProcessMediaJob will pick them up on next run. */ public function resetFailed(): int { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $count = (int) $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}feednetic_media SET status = 'pending', attempts = 0, updated_at = %s WHERE status = 'failed'", gmdate( 'Y-m-d H:i:s' ) ) ); return $count; } /** * Resets ALL done and failed media rows back to pending so they are * re-downloaded from scratch. Returns the number of rows reset. */ public function resetAll(): int { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $count = (int) $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}feednetic_media SET status = 'pending', attempts = 0, updated_at = %s WHERE status IN ('done', 'failed')", gmdate( 'Y-m-d H:i:s' ) ) ); return $count; } public function deleteByPostId( int $postId ): void { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->delete( "{$wpdb->prefix}feednetic_media", [ 'post_id' => $postId ], [ '%d' ] ); } /** * Inserts a new media row as pending. */ private function insert( int $postId, Media $media ): void { global $wpdb; $now = gmdate( 'Y-m-d H:i:s' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->insert( "{$wpdb->prefix}feednetic_media", [ 'post_id' => $postId, 'position' => $media->position, 'kind' => $media->kind, 'original_url' => $media->url, 'width' => $media->width, 'height' => $media->height, 'status' => 'pending', 'created_at' => $now, 'updated_at' => $now, ], [ '%d', '%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s' ] ); } /** * Updates the URL and kind on a not-yet-downloaded row and resets it to pending. * * Called when syncForPost sees a new URL at an existing position that hasn't * been downloaded yet (status pending or failed). */ private function refreshForPending( int $id, Media $media ): void { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->update( "{$wpdb->prefix}feednetic_media", [ 'kind' => $media->kind, 'original_url' => $media->url, 'status' => 'pending', 'attempts' => 0, 'updated_at' => gmdate( 'Y-m-d H:i:s' ), ], [ 'id' => $id ], [ '%s', '%s', '%s', '%d', '%s' ], [ '%d' ] ); } /** * Deletes a single media row by primary key. */ private function delete( int $id ): void { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->delete( "{$wpdb->prefix}feednetic_media", [ 'id' => $id ], [ '%d' ] ); } }