|null */ public function findById( int $id ): ?array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}feednetic_feeds WHERE id = %d", $id ), ARRAY_A ); return is_array( $row ) ? $row : null; } /** * Returns all feed rows. * * @return array> */ public function findAll(): 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( "SELECT * FROM {$wpdb->prefix}feednetic_feeds ORDER BY id ASC", ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * One page of feeds, newest first, optionally filtered by a name search and * a status ("publish" or "draft"; anything else means all). * * @return array{rows:array>,total:int} */ public function findPage( string $search, int $page, int $perPage, string $status = '' ): array { global $wpdb; $search = trim( $search ); $page = max( 1, $page ); $perPage = max( 1, min( 100, $perPage ) ); $offset = ( $page - 1 ) * $perPage; $like = '' === $search ? '' : '%' . $wpdb->esc_like( $search ) . '%'; $status = in_array( $status, [ 'publish', 'draft' ], true ) ? $status : ''; // Fixed SQL: an empty parameter switches its condition off. // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}feednetic_feeds WHERE ( %s = '' OR name LIKE %s ) AND ( %s = '' OR status = %s )", $like, $like, $status, $status ) ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}feednetic_feeds WHERE ( %s = '' OR name LIKE %s ) AND ( %s = '' OR status = %s ) ORDER BY updated_at DESC, id DESC LIMIT %d OFFSET %d", $like, $like, $status, $status, $perPage, $offset ), ARRAY_A ); // phpcs:enable return [ 'rows' => is_array( $rows ) ? $rows : [], 'total' => $total, ]; } /** * Number of feeds per status (no search applied), for the list's filter tabs. * * @return array{all:int,publish:int,draft:int} */ public function countByStatus(): array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table $rows = $wpdb->get_results( "SELECT status, COUNT(*) AS n FROM {$wpdb->prefix}feednetic_feeds GROUP BY status", ARRAY_A ); $counts = [ 'all' => 0, 'publish' => 0, 'draft' => 0, ]; foreach ( is_array( $rows ) ? $rows : [] as $row ) { $n = (int) $row['n']; $counts['all'] += $n; if ( 'publish' === $row['status'] || 'draft' === $row['status'] ) { $counts[ $row['status'] ] += $n; } } return $counts; } /** * Inserts a new feed and returns its new ID. * * @param array $data Column → value map. */ public function insert( array $data ): int { global $wpdb; $now = gmdate( 'Y-m-d H:i:s' ); $data['created_at'] = $now; $data['updated_at'] = $now; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->insert( "{$wpdb->prefix}feednetic_feeds", $data, $this->formatFor( $data ) ); $id = (int) $wpdb->insert_id; // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores do_action( 'feednetic/feed_saved', $id ); return $id; } /** * Updates an existing feed row. * * @param array $data Column → value map. */ public function update( int $id, array $data ): void { global $wpdb; $data['updated_at'] = 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->update( "{$wpdb->prefix}feednetic_feeds", $data, [ 'id' => $id ], $this->formatFor( $data ), [ '%d' ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores do_action( 'feednetic/feed_saved', $id ); } /** * Deletes a feed and its source associations. */ public 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_feed_sources", [ 'feed_id' => $id ], [ '%d' ] ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->delete( "{$wpdb->prefix}feednetic_feeds", [ 'id' => $id ], [ '%d' ] ); } /** * Returns the source rows associated with a feed, ordered by position. * * @return array> */ public function getSources( int $feedId ): 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 fs.*, s.network, s.name, s.username, s.status, s.followers_count, s.media_count, s.biography, s.profile_url, s.avatar_path FROM {$wpdb->prefix}feednetic_feed_sources fs JOIN {$wpdb->prefix}feednetic_sources s ON s.id = fs.source_id WHERE fs.feed_id = %d ORDER BY fs.position ASC, fs.source_id ASC", $feedId ), ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * The feed's account: its first-position feed_sources row (joined with the * source), or null when the feed has none. A feed shows one account; extra * rows written by an add-on are never read here. * * @return array|null */ public function getPrimarySource( int $feedId ): ?array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $row = $wpdb->get_row( $wpdb->prepare( "SELECT fs.*, s.network, s.name, s.username, s.status, s.followers_count, s.media_count, s.biography, s.profile_url, s.avatar_path FROM {$wpdb->prefix}feednetic_feed_sources fs JOIN {$wpdb->prefix}feednetic_sources s ON s.id = fs.source_id WHERE fs.feed_id = %d ORDER BY fs.position ASC, fs.source_id ASC LIMIT 1", $feedId ), ARRAY_A ); return is_array( $row ) ? $row : null; } /** * Sets the feed's account. Only the first-position row changes (or is * created); every other feed_sources row of the feed is left as it is, so * data an add-on stored there survives a save. */ public function setPrimarySource( int $feedId, int $sourceId ): void { global $wpdb; $table = "{$wpdb->prefix}feednetic_feed_sources"; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $first = $wpdb->get_row( $wpdb->prepare( "SELECT source_id, stream, position FROM {$table} WHERE feed_id = %d ORDER BY position ASC, source_id ASC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix $feedId ), ARRAY_A ); if ( is_array( $first ) && (int) $first['source_id'] === $sourceId && 'timeline' === (string) $first['stream'] ) { return; } // The chosen account may already be a later row (added by an add-on): // it moves to the first position instead of being stored twice. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->delete( $table, [ 'feed_id' => $feedId, 'source_id' => $sourceId, 'stream' => 'timeline', ], [ '%d', '%d', '%s' ] ); if ( is_array( $first ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->update( $table, [ 'source_id' => $sourceId, 'stream' => 'timeline', ], [ 'feed_id' => $feedId, 'source_id' => (int) $first['source_id'], 'stream' => (string) $first['stream'], ], [ '%d', '%s' ], [ '%d', '%d', '%s' ] ); return; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->insert( $table, [ 'feed_id' => $feedId, 'source_id' => $sourceId, 'stream' => 'timeline', 'position' => 0, ], [ '%d', '%d', '%s', '%d' ] ); } /** * Replaces all source associations for a feed. * * @param array $sources */ public function setSources( int $feedId, array $sources ): 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_feed_sources", [ 'feed_id' => $feedId ], [ '%d' ] ); foreach ( $sources as $row ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->insert( "{$wpdb->prefix}feednetic_feed_sources", [ 'feed_id' => $feedId, 'source_id' => $row['source_id'], 'stream' => $row['stream'], 'position' => $row['position'], ], [ '%d', '%d', '%s', '%d' ] ); } } /** * Returns all feed rows that contain a given source. * * @return array> */ public function findBySourceId( 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 f.* FROM {$wpdb->prefix}feednetic_feeds f JOIN {$wpdb->prefix}feednetic_feed_sources fs ON fs.feed_id = f.id WHERE fs.source_id = %d", $sourceId ), ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * Returns the $limit most recently updated feeds, ordered by updated_at DESC. * * @return array> */ public function findRecent( int $limit = 5 ): 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_feeds ORDER BY updated_at DESC LIMIT %d", $limit ), ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * Duplicates a feed and returns the new feed's ID. * The copy is named "{original name} (copy)" and set to draft status. */ public function duplicate( int $feedId ): ?int { $original = $this->findById( $feedId ); if ( null === $original ) { return null; } $baseName = trim( (string) $original['name'] ); if ( '' === $baseName ) { /* translators: %d: feed id */ $baseName = sprintf( __( 'Feed #%d', 'feednetic' ), $feedId ); } $newId = $this->insert( [ 'name' => $baseName . ' (copy)', 'status' => 'draft', 'settings' => (string) $original['settings'], ] ); // Copy the feed's account (add-ons copy their own rows on // feednetic/rest/duplicated). $primary = $this->getPrimarySource( $feedId ); if ( null !== $primary ) { $this->setPrimarySource( $newId, (int) $primary['source_id'] ); } return $newId; } /** * Marks a feed as having been rendered in a headless context (block-theme * template part where get_the_ID() = 0). Stored durably in the feed record * so object-cache eviction never loses the flag. */ public function markHeadless( int $feedId ): 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_feeds", [ 'is_headless' => 1 ], [ 'id' => $feedId ], [ '%d' ], [ '%d' ] ); } /** * Derives a wpdb format array from a data array. * * @param array $data * @return string[] */ private function formatFor( array $data ): array { return array_map( static function ( $value ): string { return is_int( $value ) ? '%d' : '%s'; }, $data ); } }