From 14066e57df346cfe68e8bb2618e6fca024b0832c Mon Sep 17 00:00:00 2001
From: andreybrokeit <andrey.cherneha@cbs.com>
Date: Thu, 13 Mar 2014 23:41:04 -0400
Subject: [PATCH] Merge Duplicate remote site removal bug (pull req 40)

---
 .../class-syndication-wp-xmlrpc-client.php    | 32 +++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/includes/class-syndication-wp-xmlrpc-client.php b/includes/class-syndication-wp-xmlrpc-client.php
index dccde25..f6bf828 100644
--- a/includes/class-syndication-wp-xmlrpc-client.php
+++ b/includes/class-syndication-wp-xmlrpc-client.php
@@ -175,9 +175,37 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
 		// Delete existing metadata to avoid duplicates
 		$args['custom_fields'] = array();
 		foreach ( $remote_post['custom_fields'] as $custom_field ) {
-			$args['custom_fields'][] = array( 'id' => $custom_field['id'] );
+			$args['custom_fields'][] = array( 
+				'id' => $custom_field['id'],
+				'meta_key_lookup' => $custom_field['key'],
+			);
+
 		}
-		
+
+		$thumbnail_meta_keys = $this->get_thumbnail_meta_keys( $post_ID );
+
+		foreach ( $thumbnail_meta_keys as $thumbnail_meta_key ) {
+			$thumbnail_id = get_post_meta( $post_ID, $thumbnail_meta_key, true );
+			$syn_local_meta_key = '_syn_push_thumb_' . $thumbnail_meta_key;
+			$syndicated_thumbnails_by_site = get_post_meta( $post_ID, $syn_local_meta_key, true );
+
+			if ( ! is_array( $syndicated_thumbnails_by_site ) ) {
+				$syndicated_thumbnails_by_site = array();
+			}
+
+			$syndicated_thumbnail_id = isset( $syndicated_thumbnails_by_site[ $this->site_ID ] ) ? $syndicated_thumbnails_by_site[ $this->site_ID ] : false;
+
+			if ( $syndicated_thumbnail_id == $thumbnail_id ) {
+				//need to preserve old meta custom_type_thumbnail_id if it wasn't changed during update
+				//hence we remove ID from the custom_fileds list in order to avoid its deletion
+				foreach ( $args['custom_fields'] as $index => $value ) {
+					if ( $value['meta_key_lookup'] == $thumbnail_meta_key ) {
+						unset( $args['custom_fields'][$index] );
+					}
+				}
+			}
+		}
+
 		//Uploads all gallery images to the remote site and replaces [gallery] tags with new IDs
 		$post['post_content'] = $this->syndicate_gallery_images( $post['post_content'] );
 
-- 
GitLab