feat: import media metadata records

This commit is contained in:
Keith Solomon
2026-05-01 14:57:10 -05:00
parent 592e6e7403
commit 425d42e4bb
3 changed files with 518 additions and 0 deletions
+238
View File
@@ -0,0 +1,238 @@
<?php
/**
* Imports media attachment metadata records.
*
* @package WPContentSync
*/
namespace WPContentSync\Content;
use WPContentSync\Logging\LoggerInterface;
use WPContentSync\Sync\SyncContext;
use WPContentSync\Sync\SyncResult;
use WPContentSync\Url\MetadataUrlTransformer;
use WPContentSync\Url\UrlMapping;
use WPContentSync\Url\UrlMappingCollection;
use WPContentSync\Url\UrlTransformer;
final class MediaContentHandler implements ContentHandlerInterface {
private ContentRecordNormalizer $normalizer;
private UrlTransformer $url_transformer;
private MetadataUrlTransformer $metadata_transformer;
private LoggerInterface $logger;
public function __construct(
ContentRecordNormalizer $normalizer,
UrlTransformer $url_transformer,
MetadataUrlTransformer $metadata_transformer,
LoggerInterface $logger
) {
$this->normalizer = $normalizer;
$this->url_transformer = $url_transformer;
$this->metadata_transformer = $metadata_transformer;
$this->logger = $logger;
}
public function bucket(): string {
return 'media';
}
/**
* @param array<int, array<string, mixed>> $records Package records.
*/
public function importRecords( array $records, SyncContext $context ): SyncResult {
$created = 0;
$updated = 0;
$skipped = 0;
$conflicts = 0;
$errors = array();
$mappings = $this->mappings( $context );
foreach ( $records as $record ) {
$normalized = $this->normalizer->media( $record );
$existing = $this->findExistingAttachmentId( (int) $normalized['id'], $context->sourceUrl() );
if ( $existing > 0 && 'manual_review' === $context->conflictStrategy() ) {
++$skipped;
++$conflicts;
$this->logger->warning(
'Skipped media import because manual review is required.',
array(
'source_id' => $normalized['id'],
'attachment_id' => $existing,
)
);
continue;
}
try {
$attachment_id = $this->saveAttachment( $normalized, $existing, $mappings );
} catch ( ContentImportException $exception ) {
$errors[] = $exception->getMessage();
$this->logger->error(
$exception->getMessage(),
array(
'bucket' => $exception->bucket(),
'record' => $exception->record(),
)
);
continue;
}
if ( $existing > 0 ) {
++$updated;
} else {
++$created;
}
$this->saveMetadata( $attachment_id, $normalized, $context, $mappings );
}
if ( array() !== $errors ) {
return SyncResult::merge(
array(
SyncResult::success(
array(
'created' => $created,
'updated' => $updated,
'skipped' => $skipped,
'conflicts' => $conflicts,
)
),
SyncResult::failure( $errors ),
)
);
}
$this->logger->info(
'Imported media metadata records.',
array(
'created' => $created,
'updated' => $updated,
'skipped' => $skipped,
'conflicts' => $conflicts,
)
);
return SyncResult::success(
array(
'created' => $created,
'updated' => $updated,
'skipped' => $skipped,
'conflicts' => $conflicts,
)
);
}
private function findExistingAttachmentId( int $source_id, string $source_site ): int {
if ( $source_id <= 0 ) {
return 0;
}
$attachments = get_posts(
array(
'post_type' => 'attachment',
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Source ID lookup is the handler's stable import identity.
'meta_key' => '_wpcs_source_id',
'meta_value' => (string) $source_id,
// phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value
)
);
if ( array() === $attachments ) {
return 0;
}
foreach ( $attachments as $attachment ) {
$attachment_id = (int) $attachment->ID;
if ( get_post_meta( $attachment_id, '_wpcs_source_site', true ) === $source_site ) {
return $attachment_id;
}
}
return 0;
}
/**
* @param array<string, mixed> $record Normalized media record.
* @param int $existing Existing attachment ID.
*/
private function saveAttachment( array $record, int $existing, UrlMappingCollection $mappings ): int {
$attachment_data = array(
'post_title' => $record['post_title'],
'post_mime_type' => $record['post_mime_type'],
'post_type' => 'attachment',
);
if ( '' !== $record['source_url'] ) {
$this->logger->warning(
'Skipped media binary download; importing attachment metadata only.',
array(
'source_id' => $record['id'],
'source_url' => $this->url_transformer->transformString( (string) $record['source_url'], $mappings ),
)
);
}
if ( $existing > 0 ) {
$attachment_data['ID'] = $existing;
return $this->attachmentIdFromResult( wp_update_post( $attachment_data, true ), $record );
}
return $this->attachmentIdFromResult( wp_insert_attachment( $attachment_data, false, 0, true ), $record );
}
/**
* @param int|\WP_Error $result Attachment save result.
* @param array<string, mixed> $record Normalized media record.
*/
private function attachmentIdFromResult( $result, array $record ): int {
if ( is_wp_error( $result ) || (int) $result <= 0 ) {
throw new ContentImportException(
$this->bucket(),
$record,
sprintf( 'Media import failed for source ID %d.', (int) $record['id'] )
);
}
return (int) $result;
}
/**
* @param array<string, mixed> $record Normalized media record.
*/
private function saveMetadata( int $attachment_id, array $record, SyncContext $context, UrlMappingCollection $mappings ): void {
update_post_meta( $attachment_id, '_wpcs_source_id', (int) $record['id'] );
update_post_meta( $attachment_id, '_wpcs_source_site', $context->sourceUrl() );
update_post_meta(
$attachment_id,
'_wpcs_source_url',
$this->url_transformer->transformString( (string) $record['source_url'], $mappings )
);
wp_update_attachment_metadata(
$attachment_id,
$this->metadata_transformer->transformValue( $record['metadata'], $mappings )
);
foreach ( $record['meta'] as $key => $value ) {
update_post_meta(
$attachment_id,
(string) $key,
$this->metadata_transformer->transformValue( $value, $mappings )
);
}
}
private function mappings( SyncContext $context ): UrlMappingCollection {
$mappings = array();
foreach ( $context->urlMappings() as $source => $destination ) {
$mappings[] = new UrlMapping( $source, $destination );
}
return new UrlMappingCollection( $mappings );
}
}