Skip to content

Commit c93084d

Browse files
authored
fix(content-distribution): handling multiple post meta (#199)
1 parent 2149a62 commit c93084d

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

includes/content-distribution/class-incoming-post.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public function is_linked(): bool {
272272
*
273273
* @return void
274274
*/
275-
protected function update_post_meta() {
275+
protected function update_meta() {
276276
$data = $this->payload['post_data']['post_meta'];
277277

278278
$reserved_keys = Content_Distribution::get_reserved_post_meta_keys();
@@ -297,8 +297,12 @@ protected function update_post_meta() {
297297
if ( 1 === count( $meta_value ) ) {
298298
update_post_meta( $this->ID, $meta_key, $meta_value[0] );
299299
} else {
300-
foreach ( $meta_value as $value ) {
301-
add_post_meta( $this->ID, $meta_key, $value );
300+
$value = get_post_meta( $this->ID, $meta_key, false );
301+
if ( $value !== $meta_value ) {
302+
delete_post_meta( $this->ID, $meta_key );
303+
foreach ( $meta_value as $item ) {
304+
add_post_meta( $this->ID, $meta_key, $item );
305+
}
302306
}
303307
}
304308
}
@@ -497,7 +501,7 @@ public function insert( $payload = [] ) {
497501
$this->post = get_post( $this->ID );
498502

499503
// Handle post meta.
500-
$this->update_post_meta();
504+
$this->update_meta();
501505

502506
// Handle thumbnail.
503507
$thumbnail_url = $post_data['thumbnail_url'];

tests/unit-tests/content-distribution/test-incoming-post.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,23 @@ public function test_post_meta_sync() {
310310
$this->assertEmpty( get_post_meta( $post_id, 'custom', true ) );
311311
}
312312

313+
/**
314+
* Test adding and deleting post meta.
315+
*/
316+
public function test_add_and_delete_multiple_post_meta() {
317+
$post_id = $this->incoming_post->insert();
318+
319+
$payload = $this->get_sample_payload();
320+
321+
$payload['post_data']['post_meta']['multiple'] = [ 'value 2', 'value 3' ];
322+
$this->incoming_post->insert( $payload );
323+
$this->assertSame( [ 'value 2', 'value 3' ], get_post_meta( $post_id, 'multiple' ) );
324+
325+
$payload['post_data']['post_meta']['multiple'] = [ 'value 3', 'value 3' ];
326+
$this->incoming_post->insert( $payload );
327+
$this->assertSame( [ 'value 3', 'value 3' ], get_post_meta( $post_id, 'multiple' ) );
328+
}
329+
313330
/**
314331
* Test status changes.
315332
*/

0 commit comments

Comments
 (0)