$refs */ public function replace_for_asset( int $asset_id, array $refs ): int { global $wpdb; $table = TableNames::assert_plugin_table( TableNames::references() ); $wpdb->query( 'START TRANSACTION' ); try { if ( false === $wpdb->delete( $table, array( 'asset_id' => $asset_id ) ) ) { throw new \RuntimeException( __( 'Could not save usage references.', 'foldrocket' ) ); } $now = current_time( 'mysql' ); $count = 0; foreach ( $refs as $ref ) { $wpdb->insert( $table, array( 'asset_id' => $asset_id, 'source_type' => (string) $ref['source_type'], 'source_id' => $ref['source_id'], 'source_subtype' => $ref['source_subtype'], 'location_key' => $ref['location_key'], 'context' => $ref['context'], 'confidence' => (string) ( $ref['confidence'] ?? 'unknown' ), 'url' => $ref['url'], 'last_scanned_at' => $now, ) ); if ( $wpdb->last_error !== '' ) { throw new \RuntimeException( __( 'Could not save usage references.', 'foldrocket' ) ); } ++$count; } $assets = TableNames::assert_plugin_table( TableNames::assets() ); $wpdb->update( $assets, array( 'usage_count' => $count, 'updated_at' => $now, ), array( 'id' => $asset_id ) ); if ( $wpdb->last_error !== '' ) { throw new \RuntimeException( __( 'Could not save usage references.', 'foldrocket' ) ); } $wpdb->query( 'COMMIT' ); return $count; } catch ( \Throwable $e ) { $wpdb->query( 'ROLLBACK' ); throw $e; } } /** @return list> */ public function list_for_asset( int $asset_id ): array { global $wpdb; $table = TableNames::assert_plugin_table( TableNames::references() ); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE asset_id = %d ORDER BY confidence ASC, id ASC', $table, $asset_id ), ARRAY_A ) ?: array(); $mapped = array_map( static function ( array $row ): array { $source_id = $row['source_id'] !== null ? (int) $row['source_id'] : null; $title = ''; $edit_url = null; $view_url = $row['url'] ? (string) $row['url'] : null; $post = null; if ( $source_id ) { $post = get_post( $source_id ); if ( $post ) { $title = \FoldRocket\Infrastructure\Support\Text::plain( $post->post_title ); if ( $title === '' ) { $title = sprintf( '#%d', $source_id ); } $edit_url = get_edit_post_link( $source_id, 'raw' ) ?: null; if ( (string) $row['source_subtype'] === 'bricks' ) { $builder = get_permalink( $source_id ); if ( is_string( $builder ) && $builder !== '' ) { $edit_url = add_query_arg( 'bricks', 'run', $builder ); } } if ( ! $view_url ) { $permalink = get_permalink( $source_id ); $view_url = $permalink ? (string) $permalink : null; } } } if ( $title === '' && ! empty( $row['context'] ) ) { $title = \FoldRocket\Infrastructure\Support\Text::plain( (string) $row['context'] ); } $subtype = $row['source_subtype'] ? (string) $row['source_subtype'] : ''; $loc = $row['location_key'] ? (string) $row['location_key'] : ''; $detail = match ( $subtype ) { 'featured_image' => __( 'Featured image', 'foldrocket' ), 'gutenberg' => self::block_label( $loc !== '' && $loc !== 'post_content' ? $loc : '' ), 'content' => __( 'In post content', 'foldrocket' ), 'attached' => __( 'Uploaded to this post (media library parent)', 'foldrocket' ), 'post_meta' => $loc !== '' /* translators: Placeholder in user-facing message. */ ? sprintf( __( 'Post meta: %s', 'foldrocket' ), $loc ) : __( 'Post meta', 'foldrocket' ), 'acf' => $loc !== '' /* translators: Placeholder in user-facing message. */ ? sprintf( __( 'ACF field: %s', 'foldrocket' ), $loc ) : __( 'ACF field', 'foldrocket' ), 'option' => $loc !== '' /* translators: Placeholder in user-facing message. */ ? sprintf( __( 'Option: %s', 'foldrocket' ), $loc ) : __( 'Site option', 'foldrocket' ), 'elementor' => __( 'Elementor widget', 'foldrocket' ), 'bricks' => self::bricks_detail( $loc ), 'woocommerce_featured' => __( 'Product featured image', 'foldrocket' ), 'woocommerce_gallery' => __( 'Product gallery', 'foldrocket' ), default => $subtype !== '' ? $subtype : (string) $row['source_type'], }; $post_type_label = ''; if ( $post instanceof \WP_Post ) { $pto = get_post_type_object( $post->post_type ); $post_type_label = $pto && ! empty( $pto->labels->singular_name ) ? (string) $pto->labels->singular_name : (string) $post->post_type; } if ( $title === '' ) { $title = $post_type_label !== '' ? sprintf( '%s #%d', $post_type_label, (int) $source_id ) : (string) ( $row['source_type'] ?: __( 'Unknown location', 'foldrocket' ) ); } return array( 'id' => (int) $row['id'], 'title' => $title, 'detail' => $detail, 'postType' => $post_type_label, 'sourceType' => (string) $row['source_type'], 'sourceId' => $source_id, 'sourceSubtype' => $row['source_subtype'], 'locationKey' => $row['location_key'], 'context' => $row['context'], 'confidence' => (string) $row['confidence'], 'url' => $view_url, 'editUrl' => $edit_url, 'lastScannedAt' => $row['last_scanned_at'], ); }, $rows ); return $this->unique_pages( $mapped ); } /** * One row per page/post — keeps the strongest match when scanners overlap. * * @param list> $refs * @return list> */ private function unique_pages( array $refs ): array { $best = array(); foreach ( $refs as $ref ) { $source_id = isset( $ref['sourceId'] ) ? (int) $ref['sourceId'] : 0; $key = $source_id > 0 ? 'post:' . $source_id : 'other:' . (string) ( $ref['id'] ?? '' ); if ( ! isset( $best[ $key ] ) ) { $best[ $key ] = $ref; continue; } if ( self::display_score( $ref ) > self::display_score( $best[ $key ] ) ) { $best[ $key ] = $ref; } } return array_values( $best ); } /** * @param array $ref */ private static function display_score( array $ref ): int { $confidence = match ( (string) ( $ref['confidence'] ?? '' ) ) { 'confirmed' => 300, 'probable' => 200, 'possible' => 100, default => 0, }; $subtype = match ( (string) ( $ref['sourceSubtype'] ?? '' ) ) { 'featured_image', 'woocommerce_featured' => 50, 'gutenberg', 'elementor', 'bricks', 'woocommerce_gallery' => 40, 'content' => 30, 'acf', 'post_meta' => 20, 'attached' => 10, default => 15, }; return $confidence + $subtype; } private static function bricks_detail( string $key ): string { return match ( true ) { $key === 'bricks_global_settings' => __( 'Bricks global settings', 'foldrocket' ), $key === 'bricks_theme_styles' => __( 'Bricks theme styles', 'foldrocket' ), $key === 'bricks_global_classes' => __( 'Bricks global classes', 'foldrocket' ), $key === 'bricks_components' => __( 'Bricks components', 'foldrocket' ), $key === 'bricks_global_elements' => __( 'Bricks global elements', 'foldrocket' ), str_contains( $key, 'header' ) => __( 'Bricks header', 'foldrocket' ), str_contains( $key, 'footer' ) => __( 'Bricks footer', 'foldrocket' ), str_contains( $key, 'settings' ) => __( 'Bricks settings', 'foldrocket' ), default => __( 'Bricks', 'foldrocket' ), }; } private static function block_label( string $block_name ): string { if ( $block_name === '' ) { return __( 'Block editor', 'foldrocket' ); } $map = array( 'core/image' => __( 'Image block', 'foldrocket' ), 'core/cover' => __( 'Cover block', 'foldrocket' ), 'core/gallery' => __( 'Gallery block', 'foldrocket' ), 'core/media-text' => __( 'Media & Text block', 'foldrocket' ), 'core/video' => __( 'Video block', 'foldrocket' ), 'core/audio' => __( 'Audio block', 'foldrocket' ), 'core/file' => __( 'File block', 'foldrocket' ), ); if ( isset( $map[ $block_name ] ) ) { return $map[ $block_name ]; } $short = str_contains( $block_name, '/' ) ? substr( $block_name, strrpos( $block_name, '/' ) + 1 ) : $block_name; $short = str_replace( array( '-', '_' ), ' ', $short ); return sprintf( /* translators: %s: block name */ __( 'Block: %s', 'foldrocket' ), ucwords( $short ) ); } }