|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_sources WHERE id = %d", $id ), ARRAY_A ); return is_array( $row ) ? $row : null; } /** * Returns a source row by network + external ID, or null. * * @return array|null */ public function findByNetworkAndExternalId( string $network, string $externalId ): ?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_sources WHERE network = %s AND external_id = %s", $network, $externalId ), ARRAY_A ); return is_array( $row ) ? $row : null; } /** * Returns a minimal set of profile columns for multiple sources by ID. * Used by FeedRenderer::renderPreview() to pass source data to templates. * * @param int[] $ids * @return array> */ public function findSourcesByIds( array $ids ): array { global $wpdb; if ( empty( $ids ) ) { return []; } $ints = array_map( 'intval', $ids ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, network, name, username, followers_count, media_count, biography, profile_url, avatar_path FROM {$wpdb->prefix}feednetic_sources WHERE id IN (" . implode( ',', array_fill( 0, count( $ints ), '%d' ) ) . ') ORDER BY FIELD(id, ' . implode( ',', array_fill( 0, count( $ints ), '%d' ) ) . ')', array_merge( $ints, $ints ) ), ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * Returns all source 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_sources ORDER BY id ASC", ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * Returns sources matching the given status. * * @param string $status One of active|expired|error. * @return array> */ public function findByStatus( string $status ): 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_sources WHERE status = %s ORDER BY id ASC", $status ), ARRAY_A ); return is_array( $rows ) ? $rows : []; } /** * Inserts a new source and returns its new ID. * * @param array $data Column → value map (no id/created_at/updated_at). */ 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_sources", $data, $this->formatFor( $data ) ); $id = (int) $wpdb->insert_id; // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores do_action( 'feednetic/source_connected', $id ); return $id; } /** * Updates an existing source 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_sources", $data, [ 'id' => $id ], $this->formatFor( $data ), [ '%d' ] ); } /** * Deletes a source and cascades to its posts, media rows and feed associations. * * File deletion (uploads/feednetic/) is deferred — hook feednetic/before_source_delete * to handle it before the media rows are removed from the database. */ public function delete( int $id ): void { global $wpdb; /** * Fires before a source is deleted. Use this to clean up local media files * before the media rows are removed from the database. * * @param int $id Source ID being deleted. */ // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- slash-namespaced hooks per project convention. do_action( 'feednetic/before_source_delete', $id ); // Delete media rows for all posts belonging to this source via subquery. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->query( $wpdb->prepare( "DELETE FROM `{$wpdb->prefix}feednetic_media` WHERE post_id IN ( SELECT id FROM `{$wpdb->prefix}feednetic_posts` WHERE source_id = %d )", $id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin's own table, no caching layer needed $wpdb->delete( "{$wpdb->prefix}feednetic_posts", [ 'source_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_feed_sources", [ 'source_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_sources", [ 'id' => $id ], [ '%d' ] ); } /** * Merges $updates into the source's meta JSON column. * Keys with a null value are removed from meta. * * @param array $updates */ public function updateMeta( int $id, array $updates ): void { $source = $this->findById( $id ); if ( null === $source ) { return; } $meta = []; if ( ! empty( $source['meta'] ) ) { $decoded = json_decode( (string) $source['meta'], true ); $meta = is_array( $decoded ) ? $decoded : []; } foreach ( $updates as $key => $value ) { if ( null === $value ) { unset( $meta[ $key ] ); } else { $meta[ $key ] = $value; } } $this->update( $id, [ 'meta' => (string) wp_json_encode( $meta ) ] ); } /** * Sets the last_error column to NULL and stamps updated_at. */ public function clearLastError( 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_sources` SET last_error = NULL, updated_at = %s WHERE id = %d", gmdate( 'Y-m-d H:i:s' ), $id ) ); } /** * Updates the status (and optionally stores an error message). */ public function updateStatus( int $id, string $status, ?string $lastError = null ): void { $data = [ 'status' => $status ]; if ( null !== $lastError ) { $data['last_error'] = $lastError; } $this->update( $id, $data ); } /** * Stamps the last_fetched_at column with the current UTC time. */ public function updateLastFetched( int $id ): void { $this->update( $id, [ 'last_fetched_at' => gmdate( 'Y-m-d H:i:s' ) ] ); } /** * Updates profile columns fetched from the network API. * * @param array $profile Keys: name, username, biography, avatar_url, avatar_path, * profile_url, followers_count, media_count. */ public function updateProfile( int $id, array $profile ): void { $allowed = [ 'name', 'username', 'biography', 'avatar_url', 'avatar_path', 'profile_url', 'followers_count', 'media_count', 'profile_fetched_at', ]; $data = []; foreach ( $allowed as $col ) { if ( array_key_exists( $col, $profile ) ) { $data[ $col ] = $profile[ $col ]; } } if ( ! isset( $data['profile_fetched_at'] ) ) { $data['profile_fetched_at'] = gmdate( 'Y-m-d H:i:s' ); } $this->update( $id, $data ); } /** * 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 ); } }