Skip to content

Commit

Permalink
fix(content-distribution): handling multiple post meta (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe authored Jan 22, 2025
1 parent 2149a62 commit c93084d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 8 additions & 4 deletions includes/content-distribution/class-incoming-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function is_linked(): bool {
*
* @return void
*/
protected function update_post_meta() {
protected function update_meta() {
$data = $this->payload['post_data']['post_meta'];

$reserved_keys = Content_Distribution::get_reserved_post_meta_keys();
Expand All @@ -297,8 +297,12 @@ protected function update_post_meta() {
if ( 1 === count( $meta_value ) ) {
update_post_meta( $this->ID, $meta_key, $meta_value[0] );
} else {
foreach ( $meta_value as $value ) {
add_post_meta( $this->ID, $meta_key, $value );
$value = get_post_meta( $this->ID, $meta_key, false );
if ( $value !== $meta_value ) {
delete_post_meta( $this->ID, $meta_key );
foreach ( $meta_value as $item ) {
add_post_meta( $this->ID, $meta_key, $item );
}
}
}
}
Expand Down Expand Up @@ -497,7 +501,7 @@ public function insert( $payload = [] ) {
$this->post = get_post( $this->ID );

// Handle post meta.
$this->update_post_meta();
$this->update_meta();

// Handle thumbnail.
$thumbnail_url = $post_data['thumbnail_url'];
Expand Down
17 changes: 17 additions & 0 deletions tests/unit-tests/content-distribution/test-incoming-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ public function test_post_meta_sync() {
$this->assertEmpty( get_post_meta( $post_id, 'custom', true ) );
}

/**
* Test adding and deleting post meta.
*/
public function test_add_and_delete_multiple_post_meta() {
$post_id = $this->incoming_post->insert();

$payload = $this->get_sample_payload();

$payload['post_data']['post_meta']['multiple'] = [ 'value 2', 'value 3' ];
$this->incoming_post->insert( $payload );
$this->assertSame( [ 'value 2', 'value 3' ], get_post_meta( $post_id, 'multiple' ) );

$payload['post_data']['post_meta']['multiple'] = [ 'value 3', 'value 3' ];
$this->incoming_post->insert( $payload );
$this->assertSame( [ 'value 3', 'value 3' ], get_post_meta( $post_id, 'multiple' ) );
}

/**
* Test status changes.
*/
Expand Down

0 comments on commit c93084d

Please sign in to comment.