feat: normalize content records
This commit is contained in:
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Normalizes package content records for handlers.
|
||||||
|
*
|
||||||
|
* @package WPContentSync
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace WPContentSync\Content;
|
||||||
|
|
||||||
|
final class ContentRecordNormalizer {
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $record Raw post record.
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function post( array $record ): array {
|
||||||
|
return array(
|
||||||
|
'id' => (int) ( $record['id'] ?? 0 ),
|
||||||
|
'post_type' => sanitize_text_field( (string) ( $record['post_type'] ?? 'post' ) ),
|
||||||
|
'post_title' => sanitize_text_field( (string) ( $record['post_title'] ?? '' ) ),
|
||||||
|
'post_content' => (string) ( $record['post_content'] ?? '' ),
|
||||||
|
'post_excerpt' => sanitize_text_field( (string) ( $record['post_excerpt'] ?? '' ) ),
|
||||||
|
'post_status' => sanitize_text_field( (string) ( $record['post_status'] ?? 'draft' ) ),
|
||||||
|
'post_name' => sanitize_text_field( (string) ( $record['post_name'] ?? '' ) ),
|
||||||
|
'post_parent' => (int) ( $record['post_parent'] ?? 0 ),
|
||||||
|
'menu_order' => (int) ( $record['menu_order'] ?? 0 ),
|
||||||
|
'meta' => $this->arrayValue( $record['meta'] ?? array() ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $record Raw term record.
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function term( array $record ): array {
|
||||||
|
return array(
|
||||||
|
'id' => (int) ( $record['id'] ?? 0 ),
|
||||||
|
'taxonomy' => sanitize_text_field( (string) ( $record['taxonomy'] ?? '' ) ),
|
||||||
|
'name' => sanitize_text_field( (string) ( $record['name'] ?? '' ) ),
|
||||||
|
'slug' => sanitize_text_field( (string) ( $record['slug'] ?? '' ) ),
|
||||||
|
'description' => (string) ( $record['description'] ?? '' ),
|
||||||
|
'parent' => (int) ( $record['parent'] ?? 0 ),
|
||||||
|
'meta' => $this->arrayValue( $record['meta'] ?? array() ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $record Raw media record.
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function media( array $record ): array {
|
||||||
|
return array(
|
||||||
|
'id' => (int) ( $record['id'] ?? 0 ),
|
||||||
|
'post_title' => sanitize_text_field( (string) ( $record['post_title'] ?? '' ) ),
|
||||||
|
'post_mime_type' => sanitize_text_field( (string) ( $record['post_mime_type'] ?? '' ) ),
|
||||||
|
'source_url' => esc_url_raw( (string) ( $record['source_url'] ?? '' ) ),
|
||||||
|
'metadata' => $this->arrayValue( $record['metadata'] ?? array() ),
|
||||||
|
'meta' => $this->arrayValue( $record['meta'] ?? array() ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value Value to normalize.
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function arrayValue( $value ): array {
|
||||||
|
return is_array( $value ) ? $value : array();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Tests for content record normalization.
|
||||||
|
*
|
||||||
|
* @package WPContentSync
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace WPContentSync\Tests\Unit\Content;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use WPContentSync\Content\ContentRecordNormalizer;
|
||||||
|
|
||||||
|
class ContentRecordNormalizerTest extends TestCase {
|
||||||
|
public function test_it_normalizes_post_records(): void {
|
||||||
|
$normalizer = new ContentRecordNormalizer();
|
||||||
|
|
||||||
|
$record = $normalizer->post(
|
||||||
|
array(
|
||||||
|
'id' => '42',
|
||||||
|
'post_type' => '<b>post</b>',
|
||||||
|
'post_title' => "Hello\nWorld",
|
||||||
|
'post_content' => '<p>Keep HTML</p>',
|
||||||
|
'post_excerpt' => '<em>Excerpt</em>',
|
||||||
|
'post_status' => 'publish',
|
||||||
|
'post_name' => 'hello-world',
|
||||||
|
'post_parent' => '7',
|
||||||
|
'menu_order' => '3',
|
||||||
|
'meta' => array(
|
||||||
|
'_source_url' => 'https://source.test/page',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame(
|
||||||
|
array(
|
||||||
|
'id' => 42,
|
||||||
|
'post_type' => 'post',
|
||||||
|
'post_title' => 'Hello World',
|
||||||
|
'post_content' => '<p>Keep HTML</p>',
|
||||||
|
'post_excerpt' => 'Excerpt',
|
||||||
|
'post_status' => 'publish',
|
||||||
|
'post_name' => 'hello-world',
|
||||||
|
'post_parent' => 7,
|
||||||
|
'menu_order' => 3,
|
||||||
|
'meta' => array(
|
||||||
|
'_source_url' => 'https://source.test/page',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
$record
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_normalizes_term_records(): void {
|
||||||
|
$normalizer = new ContentRecordNormalizer();
|
||||||
|
|
||||||
|
$record = $normalizer->term(
|
||||||
|
array(
|
||||||
|
'id' => '9',
|
||||||
|
'taxonomy' => '<b>category</b>',
|
||||||
|
'name' => "News\nUpdates",
|
||||||
|
'slug' => 'news-updates',
|
||||||
|
'description' => '<p>Keep description HTML</p>',
|
||||||
|
'parent' => '2',
|
||||||
|
'meta' => array(
|
||||||
|
'landing_url' => 'https://source.test/news',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame(
|
||||||
|
array(
|
||||||
|
'id' => 9,
|
||||||
|
'taxonomy' => 'category',
|
||||||
|
'name' => 'News Updates',
|
||||||
|
'slug' => 'news-updates',
|
||||||
|
'description' => '<p>Keep description HTML</p>',
|
||||||
|
'parent' => 2,
|
||||||
|
'meta' => array(
|
||||||
|
'landing_url' => 'https://source.test/news',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
$record
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_normalizes_media_records(): void {
|
||||||
|
$normalizer = new ContentRecordNormalizer();
|
||||||
|
|
||||||
|
$record = $normalizer->media(
|
||||||
|
array(
|
||||||
|
'id' => '12',
|
||||||
|
'post_title' => "Hero\nImage",
|
||||||
|
'post_mime_type' => 'image/jpeg',
|
||||||
|
'source_url' => 'https://source.test/uploads/hero.jpg',
|
||||||
|
'metadata' => array(
|
||||||
|
'width' => 1200,
|
||||||
|
),
|
||||||
|
'meta' => array(
|
||||||
|
'_wp_attachment_image_alt' => 'Hero',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame(
|
||||||
|
array(
|
||||||
|
'id' => 12,
|
||||||
|
'post_title' => 'Hero Image',
|
||||||
|
'post_mime_type' => 'image/jpeg',
|
||||||
|
'source_url' => 'https://source.test/uploads/hero.jpg',
|
||||||
|
'metadata' => array(
|
||||||
|
'width' => 1200,
|
||||||
|
),
|
||||||
|
'meta' => array(
|
||||||
|
'_wp_attachment_image_alt' => 'Hero',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
$record
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user