From 52fc532fc81dcc1526e4b5eac78762b786df3a6f Mon Sep 17 00:00:00 2001 From: ashfame Date: Wed, 18 Dec 2024 20:05:13 +0400 Subject: [PATCH] have all fields of subjects be dynamic on backend --- src/plugin/class-subjects-controller.php | 55 ++++++++++------------- tests/plugin/test-blogpost-controller.php | 27 ++++++----- tests/plugin/test-liberate-controller.php | 30 +++++++------ tests/plugin/test-page-controller.php | 26 ++++------- 4 files changed, 60 insertions(+), 78 deletions(-) diff --git a/src/plugin/class-subjects-controller.php b/src/plugin/class-subjects-controller.php index b2332495..033f37e9 100644 --- a/src/plugin/class-subjects-controller.php +++ b/src/plugin/class-subjects-controller.php @@ -288,9 +288,11 @@ public function create_item( $request ): WP_REST_Response|WP_Error { foreach ( $item_meta as $key => $value ) { update_post_meta( $item['ID'], $key, $value ); } - update_post_meta( $item['ID'], 'subject_type', 'page' ); - return $this->prepare_item_for_response( $item, $request ); + $subject_type = $this->get_subject_type( $request ); + update_post_meta( $item['ID'], 'subject_type', $subject_type ); + + return $this->prepare_item_for_response( $item, $request, $subject_type ); } public function update_item( $request ): WP_REST_Response|WP_Error { @@ -369,60 +371,44 @@ public function prepare_item_for_response( $item, $request ): WP_REST_Response|W ); } + $subject_type = $this->get_subject_type( $request ); + $response = array( 'id' => $item['ID'], 'authorId' => $item['post_author'] ?? '', 'sourceUrl' => $item['guid'] ?? '', 'sourceHtml' => $item['post_content_filtered'] ?? '', - 'rawTitle' => get_post_meta( $item['ID'], 'raw_title', true ), - 'parsedTitle' => $item['post_title'] ?? '', - 'rawDate' => get_post_meta( $item['ID'], 'raw_date', true ), - 'parsedDate' => $item['post_date'] ?? '', - 'rawContent' => get_post_meta( $item['ID'], 'raw_content', true ), - 'parsedContent' => $item['post_content'] ?? '', 'transformedId' => absint( get_post_meta( $item['ID'], '_dl_transformed', true ) ), ); + foreach ( array_keys( Schema::get()[ $subject_type ]['fields'] ) as $field_name ) { + $response[ 'raw' . ucfirst( $field_name ) ] = get_post_meta( $item['ID'], 'raw_' . $field_name, true ); + $response[ 'parsed' . ucfirst( $field_name ) ] = get_post_meta( $item['ID'], 'parsed_' . $field_name, true ); + } + $response['previewUrl'] = get_permalink( $response['transformedId'] ); return new WP_REST_Response( $response ); } - public function prepare_item_for_database( $request ): WP_Error|array { + public function prepare_item_for_database( $request ): array { $prepared_post = array(); $request_data = json_decode( $request->get_body(), true ); - if ( isset( $request_data['parsedDate'] ) ) { - try { - $datetime = new DateTime( $request_data['parsedDate'], new DateTimeZone( 'UTC' ) ); - $post_date = $datetime->format( 'Y-m-d H:i:s' ); - $post_date_gmt = get_gmt_from_date( $post_date ); - } catch ( \Exception $e ) { - return new WP_Error( - 'invalid_date_format', - // translators: %s: Error message describing the invalid date format. - sprintf( __( 'Invalid date format: %s', 'try_wordpress' ), $e->getMessage() ), - array( 'status' => 400 ) - ); - } - } + $subject_type = $this->get_subject_type( $request ); // Prepare $postarr that can be passed to wp_insert_post() $prepared_post['ID'] = $request['id']; $prepared_post['post_type'] = $this->storage_post_type; - $prepared_post['post_title'] = $request_data['parsedTitle'] ?? ''; - $prepared_post['post_date'] = $post_date ?? ''; - $prepared_post['post_date_gmt'] = $post_date_gmt ?? ''; - $prepared_post['post_content'] = $request_data['parsedContent'] ?? ''; $prepared_post['post_content_filtered'] = $request_data['sourceHtml'] ?? ''; $prepared_post['guid'] = $request_data['sourceUrl'] ?? ''; $prepared_post['post_author'] = $request_data['authorId'] ?? ''; - $prepared_post['meta'] = array( - 'raw_title' => $request_data['rawTitle'] ?? '', - 'raw_date' => $request_data['rawDate'] ?? '', - 'raw_content' => $request_data['rawContent'] ?? '', - ); + $prepared_post['meta'] = array(); + foreach ( array_keys( Schema::get()[ $subject_type ]['fields'] ) as $field_name ) { + $prepared_post['meta'][ 'raw_' . $field_name ] = $request_data[ 'raw' . ucfirst( $field_name ) ] ?? ''; + $prepared_post['meta'][ 'parsed_' . $field_name ] = $request_data[ 'parsed' . ucfirst( $field_name ) ] ?? ''; + } return $prepared_post; } @@ -461,4 +447,9 @@ public function get_post_id_by_guid( string $guid ): ?int { return null; } + + private function get_subject_type( $request ): string { + preg_match( '/\/subjects\/([^\/]+)/', $request->get_route(), $matches ); + return $matches[1]; + } } diff --git a/tests/plugin/test-blogpost-controller.php b/tests/plugin/test-blogpost-controller.php index 5fa2f094..605a8d6c 100644 --- a/tests/plugin/test-blogpost-controller.php +++ b/tests/plugin/test-blogpost-controller.php @@ -12,13 +12,12 @@ class Blogpost_Controller_Test extends TestCase { private string $storage_post_type = 'lib_1'; private string $source_html; - private string $raw_title = '

This is the test title

'; - private string $parsed_title = 'This is the test title'; - private string $raw_date = ''; - private string $parsed_date = '2024-10-25 18:39:20'; - private string $date_iso_string = '2024-10-25T18:39:20.000Z'; - private string $raw_content = '

This is the test content.

'; - private string $parsed_content = '

This is the test content.

'; + private string $raw_title = '

This is the test title

'; + private string $parsed_title = 'This is the test title'; + private string $raw_date = ''; + private string $parsed_date = '2024-10-25T18:39:20.000Z'; + private string $raw_content = '

This is the test content.

'; + private string $parsed_content = '

This is the test content.

'; protected function setUp(): void { parent::setUp(); @@ -113,7 +112,7 @@ public function testCreateItemFullBody() { 'rawContent' => $this->raw_content, 'parsedContent' => $this->parsed_content, 'rawDate' => $this->raw_date, - 'parsedDate' => $this->date_iso_string, + 'parsedDate' => $this->parsed_date, 'authorId' => $author_id, ) ) @@ -139,10 +138,10 @@ public function testCreateItemFullBody() { // read from db $post = get_post( $response_data['id'] ); $this->assertEquals( $source_url, $post->guid ); - $this->assertEquals( $this->parsed_title, $post->post_title ); - $this->assertEquals( $this->parsed_content, $post->post_content ); + $this->assertEquals( $this->parsed_title, get_post_meta( $response_data['id'], 'parsed_title', true ) ); + $this->assertEquals( $this->parsed_content, get_post_meta( $response_data['id'], 'parsed_content', true ) ); $this->assertEquals( $author_id, $post->post_author ); - $this->assertEquals( $this->parsed_date, $post->post_date ); + $this->assertEquals( $this->parsed_date, get_post_meta( $response_data['id'], 'parsed_date', true ) ); // assert types $this->assertIsInt( $response_data['id'] ); @@ -163,7 +162,7 @@ public function testCreateItemMissingSourceUrl() { 'rawContent' => $this->raw_content, 'parsedContent' => $this->parsed_content, 'rawDate' => $this->raw_date, - 'parsedDate' => $this->date_iso_string, + 'parsedDate' => $this->parsed_date, 'authorId' => $author_id, ) ) @@ -219,8 +218,8 @@ public function testUpdateItem() { // Verify database update $post = get_post( $post_id ); - $this->assertEquals( $new_title, $post->post_title ); - $this->assertEquals( $new_content, $post->post_content ); + $this->assertEquals( $new_title, get_post_meta( $post_id, 'parsed_title', true ) ); + $this->assertEquals( $new_content, get_post_meta( $post_id, 'parsed_content', true ) ); $this->assertEquals( $source_url, $post->guid ); } diff --git a/tests/plugin/test-liberate-controller.php b/tests/plugin/test-liberate-controller.php index 8be0f0ac..ec577b7e 100644 --- a/tests/plugin/test-liberate-controller.php +++ b/tests/plugin/test-liberate-controller.php @@ -11,16 +11,15 @@ class Liberate_Controller_Test extends TestCase { private string $endpoint = '/try-wp/v1/subjects/blog-post'; private string $source_html; - private string $raw_title = '

This is the test title

'; - private string $parsed_title = 'This is the test title'; - private string $raw_date = ''; - private string $parsed_date = '2024-10-25 18:39:20'; - private string $date_iso_string = '2024-10-25T18:39:20.000Z'; - private string $raw_content = '

This is the test content.

'; - private string $parsed_content = '

This is the test content.

'; + private string $raw_title = '

This is the test title

'; + private string $parsed_title = 'This is the test title'; + private string $raw_date = ''; + private string $parsed_date = '2024-10-25T18:39:20.000Z'; + private string $raw_content = '

This is the test content.

'; + private string $parsed_content = '

This is the test content.

'; private string $inserted_post_id; - private string $transformed_post_id; + private int $transformed_post_id; protected function setUp(): void { parent::setUp(); @@ -60,8 +59,11 @@ protected function setUp(): void { update_post_meta( $this->inserted_post_id, 'raw_date', $this->raw_date ); update_post_meta( $this->inserted_post_id, 'raw_title', $this->raw_title ); update_post_meta( $this->inserted_post_id, 'raw_content', $this->raw_content ); + update_post_meta( $this->inserted_post_id, 'parsed_date', $this->parsed_date ); + update_post_meta( $this->inserted_post_id, 'parsed_title', $this->parsed_title ); + update_post_meta( $this->inserted_post_id, 'parsed_content', $this->parsed_content ); - $this->transformed_post_id = get_post_meta( $this->inserted_post_id, '_dl_transformed', true ); + $this->transformed_post_id = absint( get_post_meta( $this->inserted_post_id, '_dl_transformed', true ) ); } protected function tearDown(): void { @@ -189,7 +191,7 @@ public function testPrepareItemForResponse() { $response = $this->liberate_controller->prepare_item_for_response( $post_array, - new WP_REST_Request() + new WP_REST_Request( 'GET', $this->endpoint ) ); $this->assertEquals( @@ -226,7 +228,7 @@ public function testPrepareItemForDatabase() { 'rawTitle' => $this->raw_title, 'parsedTitle' => $this->parsed_title, 'rawDate' => $this->raw_date, - 'parsedDate' => $this->date_iso_string, + 'parsedDate' => $this->parsed_date, 'rawContent' => $this->raw_content, 'parsedContent' => $this->parsed_content, ) @@ -246,7 +248,7 @@ public function testPrepareItemForDatabase() { ); $this->assertEquals( $this->parsed_title, - $prepared_post['post_title'] + $prepared_post['meta']['parsed_title'] ); $this->assertEquals( $this->raw_title, @@ -254,13 +256,13 @@ public function testPrepareItemForDatabase() { ); $this->assertEquals( $this->parsed_content, - $prepared_post['post_content'] + $prepared_post['meta']['parsed_content'] ); $this->assertEquals( $this->source_html, $prepared_post['post_content_filtered'] ); - $this->assertEquals( $this->parsed_date, $prepared_post['post_date'] ); + $this->assertEquals( $this->parsed_date, $prepared_post['meta']['parsed_date'] ); $this->assertEquals( $this->raw_date, $prepared_post['meta']['raw_date'] ); $this->assertEquals( $this->raw_content, $prepared_post['meta']['raw_content'] ); } diff --git a/tests/plugin/test-page-controller.php b/tests/plugin/test-page-controller.php index ce61ddd7..9e7dda08 100644 --- a/tests/plugin/test-page-controller.php +++ b/tests/plugin/test-page-controller.php @@ -12,13 +12,10 @@ class Page_Controller_Test extends TestCase { private string $storage_post_type = 'lib_3'; private string $source_html; - private string $raw_title = '

This is the test title

'; - private string $parsed_title = 'This is the test title'; - private string $raw_date = ''; - private string $parsed_date = '2024-10-25 18:39:20'; - private string $date_iso_string = '2024-10-25T18:39:20.000Z'; - private string $raw_content = '

This is the test content.

'; - private string $parsed_content = '

This is the test content.

'; + private string $raw_title = '

This is the test title

'; + private string $parsed_title = 'This is the test title'; + private string $raw_content = '

This is the test content.

'; + private string $parsed_content = '

This is the test content.

'; protected function setUp(): void { parent::setUp(); @@ -112,8 +109,6 @@ public function testCreateItemFullBody() { 'parsedTitle' => $this->parsed_title, 'rawContent' => $this->raw_content, 'parsedContent' => $this->parsed_content, - 'rawDate' => $this->raw_date, - 'parsedDate' => $this->date_iso_string, 'authorId' => $author_id, ) ) @@ -129,8 +124,6 @@ public function testCreateItemFullBody() { $this->assertEquals( $this->parsed_title, $response_data['parsedTitle'] ); $this->assertEquals( $this->raw_content, $response_data['rawContent'] ); $this->assertEquals( $this->parsed_content, $response_data['parsedContent'] ); - $this->assertEquals( $this->raw_date, $response_data['rawDate'] ); - $this->assertEquals( $this->parsed_date, $response_data['parsedDate'] ); $this->assertEquals( $source_url, $response_data['sourceUrl'] ); $this->assertEquals( $this->source_html, $response_data['sourceHtml'] ); @@ -139,10 +132,9 @@ public function testCreateItemFullBody() { // read from db $post = get_post( $response_data['id'] ); $this->assertEquals( $source_url, $post->guid ); - $this->assertEquals( $this->parsed_title, $post->post_title ); - $this->assertEquals( $this->parsed_content, $post->post_content ); + $this->assertEquals( $this->parsed_title, get_post_meta( $response_data['id'], 'parsed_title', true ) ); + $this->assertEquals( $this->parsed_content, get_post_meta( $response_data['id'], 'parsed_content', true ) ); $this->assertEquals( $author_id, $post->post_author ); - $this->assertEquals( $this->parsed_date, $post->post_date ); } public function testCreateItemMissingSourceUrl() { @@ -157,8 +149,6 @@ public function testCreateItemMissingSourceUrl() { 'parsedTitle' => $this->parsed_title, 'rawContent' => $this->raw_content, 'parsedContent' => $this->parsed_content, - 'rawDate' => $this->raw_date, - 'parsedDate' => $this->date_iso_string, 'authorId' => $author_id, ) ) @@ -214,8 +204,8 @@ public function testUpdateItem() { // Verify database update $post = get_post( $post_id ); - $this->assertEquals( $new_title, $post->post_title ); - $this->assertEquals( $new_content, $post->post_content ); + $this->assertEquals( $new_title, get_post_meta( $post_id, 'parsed_title', true ) ); + $this->assertEquals( $new_content, get_post_meta( $post_id, 'parsed_content', true ) ); $this->assertEquals( $source_url, $post->guid ); }