From 015b0b0fa2aa8e2c91d7406d56d10352fd41e7bc Mon Sep 17 00:00:00 2001
From: Mohammad Jangda <batmoo@gmail.com>
Date: Tue, 17 Dec 2013 05:29:46 +0000
Subject: [PATCH] XML-RPC Push: Prevent duplicate meta on updates.

Because of how XML-RPC works, we can only update existing meta fiels by
specifying IDs we want to override and their new values.

Currently, we don't do this, so updates to pushed posts result in
duplicated meta.

Let's avoid this by first deleting existing meta using IDs, then
re-inserting all the updated meta fields. This way, we can handle adds,
updates, and deletes.

Fixes #35
---
 .../class-syndication-wp-xmlrpc-client.php    | 30 +++++++++++++++----
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/includes/class-syndication-wp-xmlrpc-client.php b/includes/class-syndication-wp-xmlrpc-client.php
index 465e84e..f9534e7 100644
--- a/includes/class-syndication-wp-xmlrpc-client.php
+++ b/includes/class-syndication-wp-xmlrpc-client.php
@@ -143,15 +143,28 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
 
 	public function edit_post( $post_ID, $remote_post_id ) {
 
+		$args = array();
+
 		$post = (array)get_post( $post_ID );
 
 		// This filter can be used to exclude or alter posts during a content push
 		$post = apply_filters( 'syn_xmlrpc_push_filter_edit_post', $post, $post_ID );
 		if ( false === $post )
 			return true;
+
+		$remote_post = $this->get_remote_post( $remote_post_id );
+
+		if ( ! $remote_post ) {
+			return new WP_Error( 'syn-remote-post-not-found', __( 'Remote post doesn\'t exist.', 'syndication' ) );
+		}
+
+		// 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'] );
+		}
 		
 		// rearranging arguments
-		$args = array();
 		$args['post_title']	 = $post['post_title'];
 		$args['post_content']   = $post['post_content'];
 		$args['post_excerpt']   = $post['post_excerpt'];
@@ -162,7 +175,7 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
 
 		$args['terms_names'] = $this->_get_post_terms( $post_ID );
 
-		$args['custom_fields'] = $this->_get_custom_fields( $post_ID );
+		$args['custom_fields'] = array_merge( $args['custom_fields'], $this->_get_custom_fields( $post_ID ) );
 
 		$args = apply_filters( 'syn_xmlrpc_push_edit_post_args', $args, $post );
 
@@ -293,23 +306,28 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
 
 	}
 
-	public function is_post_exists( $ext_ID ) {
+	function get_remote_post( $remote_post_id ) {
 
 		$result = $this->query(
 			'wp.getPost',
 			'1',
 			$this->username,
 			$this->password,
-			$ext_ID
+			$remote_post_id
 		);
 
 		if( !$result )
 			return false;
 
-		$post = $this->getResponse();
+		return $this->getResponse();
+	}
+
+	public function is_post_exists( $remote_post_id ) {
+		$remote_post = $this->get_remote_post( $remote_post_id );
 
-		if( $ext_ID != $post['post_id'] )
+		if( ! $remote_post || $remote_post_id != $remote_post['post_id'] ) {
 			return false;
+		}
 
 		return true;
 
-- 
GitLab