Skip to content
Snippets Groups Projects
Commit 015b0b0f authored by Mohammad Jangda's avatar Mohammad Jangda
Browse files

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
parent 091fb6d0
No related branches found
No related tags found
No related merge requests found
...@@ -143,15 +143,28 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica ...@@ -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 ) { public function edit_post( $post_ID, $remote_post_id ) {
$args = array();
$post = (array)get_post( $post_ID ); $post = (array)get_post( $post_ID );
// This filter can be used to exclude or alter posts during a content push // 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 ); $post = apply_filters( 'syn_xmlrpc_push_filter_edit_post', $post, $post_ID );
if ( false === $post ) if ( false === $post )
return true; 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 // rearranging arguments
$args = array();
$args['post_title'] = $post['post_title']; $args['post_title'] = $post['post_title'];
$args['post_content'] = $post['post_content']; $args['post_content'] = $post['post_content'];
$args['post_excerpt'] = $post['post_excerpt']; $args['post_excerpt'] = $post['post_excerpt'];
...@@ -162,7 +175,7 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica ...@@ -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['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 ); $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 ...@@ -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( $result = $this->query(
'wp.getPost', 'wp.getPost',
'1', '1',
$this->username, $this->username,
$this->password, $this->password,
$ext_ID $remote_post_id
); );
if( !$result ) if( !$result )
return false; 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 false;
}
return true; return true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment