Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading