|null $metadata Attachment metadata already produced by WordPress. */ public function register_attachment( int $attachment_id, ?array $metadata = null ): int { $attachment_id = (int) $attachment_id; if ( $attachment_id <= 0 || 'attachment' !== get_post_type( $attachment_id ) ) { return 0; } $meta = is_array( $metadata ) ? $metadata : wp_get_attachment_metadata( $attachment_id ); $meta = is_array( $meta ) ? $meta : array(); $file = get_attached_file( $attachment_id ); $file_exists = is_string( $file ) && $file !== '' && is_readable( $file ); $mime = get_post_mime_type( $attachment_id ) ?: ''; $width = isset( $meta['width'] ) ? (int) $meta['width'] : 0; $height = isset( $meta['height'] ) ? (int) $meta['height'] : 0; $data = array( 'mime_type' => $mime ); if ( $width > 0 ) { $data['width'] = $width; } if ( $height > 0 ) { $data['height'] = $height; } if ( $width > 0 && $height > 0 ) { $data['orientation'] = $width > $height ? 'landscape' : ( $height > $width ? 'portrait' : 'square' ); } if ( $file_exists ) { $data['file_size'] = (int) filesize( $file ); } $this->persist_file_status( $attachment_id, $file_exists ); return ( new AssetRepository() )->upsert( $attachment_id, $data ); } public function queue_index( int $attachment_id ): void { $attachment_id = (int) $attachment_id; if ( $attachment_id <= 0 ) { return; } global $wpdb; $wpdb->update( \FoldRocket\Infrastructure\Database\TableNames::assets(), array( 'hash_fingerprint' => null ), array( 'attachment_id' => $attachment_id, 'site_id' => \FoldRocket\Infrastructure\Database\TableNames::site_id() ) ); $jobs = new JobRepository(); if ( $jobs->has_open_for_attachment( 'index_asset', $attachment_id ) ) { return; } $id = $jobs->create( 'index_asset', array( 'attachment_id' => $attachment_id ), 'index:' . $attachment_id ); ( new JobScheduler() )->dispatch( $id ); } public function index_now( int $attachment_id, bool $force = false ): int { $meta = wp_get_attachment_metadata( $attachment_id ); $file = get_attached_file( $attachment_id ); $file_exists = is_string( $file ) && $file !== '' && is_readable( $file ); $mime = get_post_mime_type( $attachment_id ) ?: ''; $width = isset( $meta['width'] ) ? (int) $meta['width'] : null; $height = isset( $meta['height'] ) ? (int) $meta['height'] : null; $size = $file_exists ? (int) filesize( $file ) : null; $this->persist_file_status( $attachment_id, $file_exists ); $orientation = 'unknown'; if ( $width && $height ) { if ( $width > $height ) { $orientation = 'landscape'; } elseif ( $height > $width ) { $orientation = 'portrait'; } else { $orientation = 'square'; } } // Prefer a small, uncropped WP intermediate image for the perceptual hash. $hash_source = $file; if ( $file && $width && $height && is_array( $meta ) ) { $candidates = array_values( $meta['sizes'] ?? array() ); usort( $candidates, static fn( $a, $b ) => ( (int) ( $a['width'] ?? 0 ) ) <=> ( (int) ( $b['width'] ?? 0 ) ) ); foreach ( $candidates as $candidate ) { $cw = (int) ( $candidate['width'] ?? 0 ); $ch = (int) ( $candidate['height'] ?? 0 ); if ( $cw < 64 || $ch < 64 || $cw > 1024 || $ch > 1024 || abs( ( $cw / $ch ) / ( $width / $height ) - 1 ) > 0.02 ) { continue; } $path = dirname( $file ) . '/' . basename( (string) $candidate['file'] ); if ( is_readable( $path ) ) { $hash_source = $path; break; } } } $fingerprint = $file && is_readable( $file ) ? sha1( 'v2|' . ( function_exists( 'imagecreatefromstring' ) ? 'gd|' : 'no-gd|' ) . $file . '|' . (string) filemtime( $file ) . '|' . $size . '|' . $hash_source . '|' . (string) filemtime( $hash_source ) ) : null; global $wpdb; $cached = $wpdb->get_row( $wpdb->prepare( 'SELECT sha256,perceptual_hash,hash_fingerprint FROM %i WHERE site_id = %d AND attachment_id = %d', \FoldRocket\Infrastructure\Database\TableNames::assets(), \FoldRocket\Infrastructure\Database\TableNames::site_id(), $attachment_id ), ARRAY_A ); $reusable = ! $force && $fingerprint !== null && $cached && $fingerprint === $cached['hash_fingerprint']; $sha256 = $reusable ? $cached['sha256'] : null; $phash = $reusable ? $cached['perceptual_hash'] : null; if ( ! $reusable && $file && is_readable( $file ) && $size && $size < 50 * 1024 * 1024 ) { $hash = hash_file( 'sha256', $file ); $sha256 = $hash !== false ? $hash : null; if ( str_starts_with( $mime, 'image/' ) && class_exists( PerceptualHasher::class ) ) { $phash = PerceptualHasher::hash_file( $hash_source ); } } $repo = new AssetRepository(); return $repo->upsert( $attachment_id, array( 'hash_fingerprint' => $fingerprint, 'sha256' => $sha256, 'perceptual_hash' => $phash, 'width' => $width, 'height' => $height, 'file_size' => $size, 'mime_type' => $mime, 'orientation' => $orientation, ) ); } private function persist_file_status( int $attachment_id, bool $file_exists ): void { if ( $file_exists ) { delete_post_meta( $attachment_id, self::MISSING_FILE_META ); return; } update_post_meta( $attachment_id, self::MISSING_FILE_META, '1' ); } /** * Refresh the physical-file status for every live attachment. * * This is intentionally run when the Missing file filter is first opened so * files removed outside WordPress are discovered without requiring a full * library reindex. The query is lightweight and metadata is only written * when the stored status actually changes. * * @return array{checked: int, missing: int, changed: int} */ public function refresh_file_statuses(): array { global $wpdb; $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT ID FROM %i WHERE post_type = %s AND post_status = %s ORDER BY ID ASC', $wpdb->posts, 'attachment', 'inherit' ) ) ?: array(); $attachment_ids = array_values( array_filter( array_map( 'intval', $ids ) ) ); if ( $attachment_ids ) { update_meta_cache( 'post', $attachment_ids ); } $missing = 0; $changed = 0; foreach ( $attachment_ids as $attachment_id ) { $file = get_attached_file( $attachment_id ); $file_exists = is_string( $file ) && $file !== '' && is_readable( $file ); $was_missing = '1' === (string) get_post_meta( $attachment_id, self::MISSING_FILE_META, true ); if ( ! $file_exists ) { ++$missing; } if ( $was_missing === ! $file_exists ) { continue; } $this->persist_file_status( $attachment_id, $file_exists ); ++$changed; } return array( 'checked' => count( $attachment_ids ), 'missing' => $missing, 'changed' => $changed, ); } /** * Attachment IDs that exist in the media library but are missing from frkt_assets. * * @return list */ public function unindexed_attachment_ids( int $limit = 0 ): array { global $wpdb; $assets = \FoldRocket\Infrastructure\Database\TableNames::assert_plugin_table( \FoldRocket\Infrastructure\Database\TableNames::assets() ); $site_id = \FoldRocket\Infrastructure\Database\TableNames::site_id(); $posts = $wpdb->posts; $limit = max( 0, $limit ); if ( $limit > 0 ) { $ids = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM %i p LEFT JOIN %i a ON a.attachment_id = p.ID AND a.site_id = %d WHERE p.post_type = 'attachment' AND p.post_status = 'inherit' AND a.id IS NULL ORDER BY p.ID ASC LIMIT %d", $posts, $assets, $site_id, $limit ) ) ?: array(); } else { $ids = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM %i p LEFT JOIN %i a ON a.attachment_id = p.ID AND a.site_id = %d WHERE p.post_type = 'attachment' AND p.post_status = 'inherit' AND a.id IS NULL ORDER BY p.ID ASC", $posts, $assets, $site_id ) ) ?: array(); } return array_map( 'intval', $ids ); } /** * Index up to $limit missing attachments synchronously. * * @return array{processed: int, indexed: int, attachments: int, remaining: int, pruned: int} */ public function index_missing_batch( int $limit = 40, bool $prune = true ): array { $limit = min( 100, max( 1, $limit ) ); $pruned = $prune ? $this->prune_orphans() : 0; $ids = $this->unindexed_attachment_ids( $limit ); foreach ( $ids as $id ) { $this->index_now( $id ); } $counts = $this->coverage_counts(); return array( 'processed' => count( $ids ), 'indexed' => $counts['indexed'], 'attachments' => $counts['attachments'], 'remaining' => $counts['remaining'], 'pruned' => $pruned, ); } /** * Queue background index jobs for missing attachments (capped per call). * * @return array{queued: int, indexed: int, attachments: int, remaining: int, pruned: int} */ public function queue_missing( int $limit = 200 ): array { $limit = min( 500, max( 1, $limit ) ); $pruned = $this->prune_orphans(); $ids = $this->unindexed_attachment_ids( $limit ); foreach ( $ids as $id ) { $this->queue_index( $id ); } $counts = $this->coverage_counts(); return array( 'queued' => count( $ids ), 'indexed' => $counts['indexed'], 'attachments' => $counts['attachments'], 'remaining' => $counts['remaining'], 'pruned' => $pruned, ); } /** * @return array{indexed: int, attachments: int, remaining: int} */ public function coverage_counts(): array { global $wpdb; $assets = \FoldRocket\Infrastructure\Database\TableNames::assert_plugin_table( \FoldRocket\Infrastructure\Database\TableNames::assets() ); $site_id = \FoldRocket\Infrastructure\Database\TableNames::site_id(); $posts = $wpdb->posts; $attachments = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE post_type = 'attachment' AND post_status = 'inherit'", $posts ) ); $indexed = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT a.attachment_id) FROM %i a INNER JOIN %i p ON p.ID = a.attachment_id WHERE a.site_id = %d AND p.post_type = 'attachment' AND p.post_status = 'inherit'", $assets, $posts, $site_id ) ); return array( 'indexed' => $indexed, 'attachments' => $attachments, 'remaining' => max( 0, $attachments - $indexed ), ); } public function delete_by_attachment( int $attachment_id ): void { ( new AssetRepository() )->delete_by_attachment( $attachment_id ); } /** * Remove index rows whose attachment no longer exists as a live inherit attachment. * * @return int Number of orphaned asset rows removed. */ public function prune_orphans(): int { global $wpdb; $assets = \FoldRocket\Infrastructure\Database\TableNames::assert_plugin_table( \FoldRocket\Infrastructure\Database\TableNames::assets() ); $site_id = \FoldRocket\Infrastructure\Database\TableNames::site_id(); $posts = $wpdb->posts; $ids = $wpdb->get_col( $wpdb->prepare( "SELECT a.attachment_id FROM %i a LEFT JOIN %i p ON p.ID = a.attachment_id AND p.post_type = 'attachment' AND p.post_status = 'inherit' WHERE a.site_id = %d AND p.ID IS NULL", $assets, $posts, $site_id ) ) ?: array(); $repo = new AssetRepository(); foreach ( $ids as $attachment_id ) { $repo->delete_by_attachment( (int) $attachment_id ); } return count( $ids ); } }