feat: import taxonomy term records
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
<?php
|
||||
/**
|
||||
* Imports taxonomy term 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 TermContentHandler 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 'terms';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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->term( $record );
|
||||
$existing = $this->findExistingTermId( $normalized );
|
||||
|
||||
if ( $existing > 0 && 'manual_review' === $context->conflictStrategy() ) {
|
||||
++$skipped;
|
||||
++$conflicts;
|
||||
$this->logger->warning(
|
||||
'Skipped term import because manual review is required.',
|
||||
array(
|
||||
'source_id' => $normalized['id'],
|
||||
'term_id' => $existing,
|
||||
'taxonomy' => $normalized['taxonomy'],
|
||||
)
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$term_id = $this->saveTerm( $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->saveMeta( $term_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 taxonomy term records.',
|
||||
array(
|
||||
'created' => $created,
|
||||
'updated' => $updated,
|
||||
'skipped' => $skipped,
|
||||
'conflicts' => $conflicts,
|
||||
)
|
||||
);
|
||||
|
||||
return SyncResult::success(
|
||||
array(
|
||||
'created' => $created,
|
||||
'updated' => $updated,
|
||||
'skipped' => $skipped,
|
||||
'conflicts' => $conflicts,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $record Normalized term record.
|
||||
*/
|
||||
private function findExistingTermId( array $record ): int {
|
||||
$source_id = (int) $record['id'];
|
||||
|
||||
if ( $source_id > 0 ) {
|
||||
$terms = get_terms(
|
||||
array(
|
||||
'taxonomy' => (string) $record['taxonomy'],
|
||||
'hide_empty' => false,
|
||||
'number' => 1,
|
||||
// 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 ( ! is_wp_error( $terms ) && array() !== $terms ) {
|
||||
return (int) $terms[0]->term_id;
|
||||
}
|
||||
}
|
||||
|
||||
$term = get_term_by( 'slug', (string) $record['slug'], (string) $record['taxonomy'] );
|
||||
|
||||
return false === $term ? 0 : (int) $term->term_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $record Normalized term record.
|
||||
* @param int $existing Existing term ID.
|
||||
*/
|
||||
private function saveTerm( array $record, int $existing, UrlMappingCollection $mappings ): int {
|
||||
$args = array(
|
||||
'slug' => $record['slug'],
|
||||
'description' => $this->url_transformer->transformString( (string) $record['description'], $mappings ),
|
||||
'parent' => $record['parent'],
|
||||
);
|
||||
|
||||
if ( $existing > 0 ) {
|
||||
$args['name'] = $record['name'];
|
||||
|
||||
return $this->termIdFromResult(
|
||||
wp_update_term( $existing, (string) $record['taxonomy'], $args ),
|
||||
$record
|
||||
);
|
||||
}
|
||||
|
||||
return $this->termIdFromResult(
|
||||
wp_insert_term( (string) $record['name'], (string) $record['taxonomy'], $args ),
|
||||
$record
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, int>|\WP_Error $result Term save result.
|
||||
* @param array<string, mixed> $record Normalized term record.
|
||||
*/
|
||||
private function termIdFromResult( $result, array $record ): int {
|
||||
if ( is_wp_error( $result ) || ! is_array( $result ) || (int) ( $result['term_id'] ?? 0 ) <= 0 ) {
|
||||
throw new ContentImportException(
|
||||
$this->bucket(),
|
||||
$record,
|
||||
sprintf( 'Term import failed for source ID %d.', (int) $record['id'] )
|
||||
);
|
||||
}
|
||||
|
||||
return (int) $result['term_id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $record Normalized term record.
|
||||
*/
|
||||
private function saveMeta( int $term_id, array $record, SyncContext $context, UrlMappingCollection $mappings ): void {
|
||||
update_term_meta( $term_id, '_wpcs_source_id', (int) $record['id'] );
|
||||
update_term_meta( $term_id, '_wpcs_source_site', $context->sourceUrl() );
|
||||
|
||||
foreach ( $record['meta'] as $key => $value ) {
|
||||
update_term_meta(
|
||||
$term_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 );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user