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

Syndicate thumbnails for XMLRPC Push

* Remove old thumbnail cruft
* Replace with new hotness to properly syndicate thumbnails attached to posts (requires Syndication plugin be active on both sites as it uses a custom endpoint)
* Remove internal success and error tracking for clients (this was causing problems with the ability to do subsequent XMLRPC calls: edit/new post then send thumbnail)
* Client actions should now return an ID, bool, or WP_Error

fixes #33
parent 149c62fe
No related branches found
No related tags found
No related merge requests found
......@@ -8,10 +8,6 @@ class Syndication_WP_REST_Client implements Syndication_Client {
private $access_token;
private $blog_ID;
private $response;
private $error_message;
private $error_code;
private $port;
private $useragent;
private $timeout;
......@@ -60,18 +56,15 @@ class Syndication_WP_REST_Client implements Syndication_Client {
) );
if ( is_wp_error( $response ) ) {
$this->error_message = 'HTTP connection error!!';
return false;
return $response;
}
$response = json_decode( wp_remote_retrieve_body( $response ) );
if( empty($response->error) ) {
$this->response = $response->ID;
return true;
if( empty( $response->error ) ) {
return $response->ID;
} else {
$this->error_message = $response->message;
return false;
return new WP_Error( 'rest-push-new-fail', $response->message );
}
}
......@@ -106,17 +99,15 @@ class Syndication_WP_REST_Client implements Syndication_Client {
) );
if ( is_wp_error( $response ) ) {
$this->error_message = 'HTTP connection error!!';
return false;
return $response;
}
$response = json_decode( wp_remote_retrieve_body( $response ) );
if( empty($response->error) ) {
return true;
if( empty( $response->error ) ) {
return $post_ID;
} else {
$this->error_message = $response->message;
return false;
return new WP_Error( 'rest-push-edit-fail', $response->message );
}
}
......@@ -146,17 +137,15 @@ class Syndication_WP_REST_Client implements Syndication_Client {
) );
if ( is_wp_error( $response ) ) {
$this->error_message = 'HTTP connection error!!';
return false;
return $response;
}
$response = json_decode( wp_remote_retrieve_body( $response ) );
if( empty($response->error) ) {
if( empty( $response->error ) ) {
return true;
} else {
$this->error_message = $response->message;
return false;
return new WP_Error( 'rest-push-delete-fail', $response->message );
}
}
......@@ -172,9 +161,8 @@ class Syndication_WP_REST_Client implements Syndication_Client {
),
) );
// TODO: return WP_Error
if ( is_wp_error( $response ) ) {
$this->error_message = 'HTTP connection error!!';
// @TODO error validation and error messages
return false;
}
......@@ -183,7 +171,6 @@ class Syndication_WP_REST_Client implements Syndication_Client {
if( empty( $response->error ) ) {
return true;
} else {
$this->error_message = $response->message;
return false;
}
......@@ -201,7 +188,6 @@ class Syndication_WP_REST_Client implements Syndication_Client {
) );
if ( is_wp_error( $response ) ) {
$this->error_message = 'HTTP connection error!!';
return false;
}
......@@ -210,24 +196,11 @@ class Syndication_WP_REST_Client implements Syndication_Client {
if( empty($response->error) ) {
return true;
} else {
$this->error_message = $response->message;
return false;
}
}
public function get_response() {
return $this->response;
}
public function get_error_code() {
return $this->error_code;
}
public function get_error_message() {
return $this->error_message;
}
public static function display_settings( $site ) {
$site_token = push_syndicate_decrypt( get_post_meta( $site->ID, 'syn_site_token', true) );
......
......@@ -10,10 +10,6 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
private $default_comment_status;
private $default_ping_status;
private $response;
private $error_message;
private $error_code;
function __construct( $site_ID ) {
switch( SIMPLEPIE_VERSION ) {
......@@ -68,18 +64,6 @@ class Syndication_WP_RSS_Client extends SimplePie implements Syndication_Client
return false;
}
public function get_response() {
return $this->response;
}
public function get_error_code() {
return $this->error_code;
}
public function get_error_message() {
return $this->error_message;
}
public static function display_settings($site) {
$feed_url = get_post_meta( $site->ID, 'syn_feed_url', true );
......
......@@ -15,10 +15,6 @@ class Syndication_WP_XML_Client implements Syndication_Client {
private $enc_field;
private $enc_is_photo;
private $response;
private $error_message;
private $error_code;
private $feed_url;
function __construct( $site_ID ) {
......@@ -56,7 +52,6 @@ class Syndication_WP_XML_Client implements Syndication_Client {
if ( parse_url( $url ) ) {
$this->feed_url = $url;
} else {
$this->error_code = 0;
$this->error_message = sprintf( __( 'Feed url not set for this feed: %s', 'push-syndication' ), $site_ID );
}
}
......@@ -317,33 +312,6 @@ class Syndication_WP_XML_Client implements Syndication_Client {
return false; // Not supported
}
/**
* Get the response message sent from the slave site.
*
* @return string response message.
*/
public function get_response() {
return $this->response;
}
/**
* Get the error code.
*
* @return int error code.
*/
public function get_error_code() {
return $this->error_code;
}
/**
* Get the error message sent from the slave site.
*
* @return string error message.
*/
public function get_error_message() {
return $this->error_message;
}
/**
* Display the client settings for the slave site.
*
......
......@@ -10,7 +10,6 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
private $username;
private $password;
private $ext_thumbnail_ids;
private $site_ID;
function __construct( $site_ID ) {
......@@ -26,17 +25,52 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
$this->password = push_syndicate_decrypt( get_post_meta( $site_ID, 'syn_site_password', true) );
$this->site_ID = $site_ID;
// get the thumbnail ids
$this->ext_thumbnail_ids = get_option( 'syn_post_thumbnail_ids' );
$this->ext_thumbnail_ids = !empty( $this->ext_thumbnail_ids ) ? $this->ext_thumbnail_ids : array() ;
$this->ext_thumbnail_ids[ $this->site_ID ] = !empty( $this->ext_thumbnail_ids[ $this->site_ID ] ) ? $this->ext_thumbnail_ids[ $this->site_ID ] : array() ;
parent::__construct( $server );
if ( true === apply_filters( 'syn_xmlrpc_push_send_thumbnail', true, $site_ID, $this ) ) {
add_action( 'syn_xmlrpc_push_new_post_success', array( $this, 'post_push_send_thumbnail' ), 10, 6 );
add_action( 'syn_xmlrpc_push_edit_post_success', array( $this, 'post_push_send_thumbnail' ), 10, 6 );
// TODO: on delete post, delete thumbnail
}
}
function __destruct() {
update_option( 'syn_post_thumbnail_ids', $this->ext_thumbnail_ids );
function post_push_send_thumbnail( $remote_post_id, $post_id ) {
$thumbnail_id = get_post_thumbnail_id( $post_id );
$syndicated_thumbnail_id = get_post_meta( $post_id, '_syn_push_syndicated_thumbnail', true );
if ( ! $thumbnail_id ) {
if ( $syndicated_thumbnail_id ) {
$result = $this->query(
'syndication.deleteThumbnail',
'1',
$this->username,
$this->password,
$remote_post_id
);
delete_post_meta( $post_id, '_syn_push_syndicated_thumbnail' );
}
return;
}
if ( $syndicated_thumbnail_id == $thumbnail_id )
return;
list( $thumbnail_url ) = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail' );
$result = $this->query(
'syndication.addThumbnail',
'1',
$this->username,
$this->password,
$remote_post_id,
$thumbnail_url
);
if ( $result ) {
update_post_meta( $post_id, '_syn_push_syndicated_thumbnail', $thumbnail_id );
}
}
public static function get_client_data() {
......@@ -76,14 +110,19 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
$args
);
if( !$result )
return false;
if ( ! $result ) {
return new WP_Error( $this->getErrorCode(), $this->getErrorMessage() );
}
$remote_post_id = (int) $this->get_response();
return $result;
do_action( 'syn_xmlrpc_push_new_post_success', $remote_post_id, $post_ID );
return $remote_post_id;
}
public function edit_post( $post_ID, $ext_ID ) {
public function edit_post( $post_ID, $remote_post_id ) {
$post = (array)get_post( $post_ID );
......@@ -113,99 +152,34 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
'1',
$this->username,
$this->password,
$ext_ID,
$remote_post_id,
$args
);
if( ! $result ) {
return false;
if ( ! $result ) {
return new WP_Error( $this->getErrorCode(), $this->getErrorMessage() );
}
return true;
do_action( 'syn_xmlrpc_push_edit_post_success', $remote_post_id, $post_ID );
return $remote_post_id;
}
public function delete_post( $ext_ID ) {
public function delete_post( $remote_post_id ) {
$result = $this->query(
'wp.deletePost',
'1',
$this->username,
$this->password,
$ext_ID
$remote_post_id
);
if( !$result )
return false;
return true;
}
public function manage_thumbnails( $post_ID ) {
// TODO: check if post thumbnails are supported
$post_thumbnail_id = get_post_thumbnail_id( $post_ID );
if( empty( $post_thumbnail_id ) )
return $this->remove_post_thumbnail( $post_ID );
if( !empty( $this->ext_thumbnail_ids[ $this->site_ID ][ $post_thumbnail_id ] ) )
return true;
if( $this->insert_post_thumbnail( $post_thumbnail_id ) ) {
$this->ext_thumbnail_ids[ $this->site_ID ][ $post_thumbnail_id ] = (int)$this->get_response();
return true;
if ( ! $result ) {
return new WP_Error( $this->getErrorCode(), $this->getErrorMessage() );
}
return false;
}
public function insert_post_thumbnail( $post_ID ) {
$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_insert_thumbnail', $post, $post_ID );
if ( false === $post )
return true;
// rearranging arguments
$args = array();
$args['post_title'] = $post['post_title'];
$args['post_content'] = $post['post_content'];
$args['guid'] = $post['guid'];
// TODO: check that method is supported
$result = $this->query(
'pushSyndicateInsertThumbnail',
'1',
$this->username,
$this->password,
$args
);
if( !$result )
return false;
return true;
}
public function remove_post_thumbnail( $post_ID ) {
$result = $this->query(
'pushSyndicateRemoveThumbnail',
'1',
$this->username,
$this->password,
$post_ID
);
if( !$result )
return false;
return true;
}
private function _get_custom_fields( $post_id ) {
......@@ -268,7 +242,7 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
if( !$result ) {
$error_code = absint($this->get_error_code());
$error_code = absint($this->getErrorCode());
switch( $error_code ) {
case 32301:
......@@ -332,18 +306,6 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
}
public function get_response() {
return parent::getResponse();
}
public function get_error_code() {
return parent::getErrorCode();
}
public function get_error_message() {
return parent::getErrorMessage();
}
public static function display_settings( $site ) {
$site_url = get_post_meta( $site->ID, 'syn_site_url', true);
......@@ -408,56 +370,59 @@ class Syndication_WP_XMLRPC_Client_Extensions {
public static function init() {
add_filter( 'xmlrpc_methods' , array( __CLASS__, 'push_syndicate_methods' ) );
add_filter( 'wp_get_attachment_url' , array( __CLASS__, 'push_syndicate_attachment_url' ), 10, 2 );
}
public static function push_syndicate_methods( $methods ) {
$methods['syndication.AddThumbnail'] = array( __CLASS__, 'xmlrpc_add_thumbnail' );
$methods['syndication.DeleteThumbnail'] = array( __CLASS__, 'xmlrpc_delete_thumbnail' );
$methods['syndication.addThumbnail'] = array( __CLASS__, 'xmlrpc_add_thumbnail' );
$methods['syndication.deleteThumbnail'] = array( __CLASS__, 'xmlrpc_delete_thumbnail' );
return $methods;
}
public static function xmlrpc_add_thumbnail( $args ) {
global $wp_xmlrpc_server;
global $wp_xmlrpc_server, $wpdb;
$wp_xmlrpc_server->escape( $args );
$blog_id = (int) $args[0];
$username = $args[1];
$password = $args[2];
$post_ID = (int)$args[3];
$content_struct = $args[4];
if ( !$user = $wp_xmlrpc_server->login( $username, $password ) )
return $wp_xmlrpc_server->error;
if ( ! current_user_can( 'edit_posts' ) )
return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
if ( isset( $content_struct['post_title'] ) )
$data['post_title'] = $content_struct['post_title'];
if ( isset( $content_struct['post_content'] ) )
$data['post_content'] = $content_struct['post_content'];
$data['post_type'] = 'attachment';
if ( empty( $content_struct['guid'] ) )
return new IXR_Error( 403, __( 'Please provide a thumbnail URL.' ) );
$data['guid'] = $content_struct['guid'];
$thumbnail_url = esc_url_raw( $args[4] );
if ( ! $post_ID )
return new IXR_Error( 500, __( 'Please specify a valid post_ID.', 'syndication' ) );
$thumbnail_raw = wp_remote_retrieve_body( wp_remote_get( $thumbnail_url ) );
if ( ! $thumbnail_raw )
return new IXR_Error( 500, __( 'Sorry, the image URL provided was incorrect.', 'syndication' ) );
$thumbnail_filename = basename( $thumbnail_url );
$thumbnail_type = wp_check_filetype( $thumbnail_filename );
$args = array(
$blog_id,
$username,
$password,
array(
'name' => $thumbnail_filename,
'type' => $thumbnail_type['type'],
'bits' => $thumbnail_raw,
'overwrite' => false,
),
);
$wp_filetype = wp_check_filetype( $content_struct['guid'], null );
if ( empty( $wp_filetype['type'] ) )
return new IXR_Error( 403, __( 'Invalid thumbnail URL.' ) );
// Note: Leting mw_newMediaObject handle our auth and cap checks
$image = $wp_xmlrpc_server->mw_newMediaObject( $args );
$data['post_mime_type'] = $wp_filetype['type'];
if ( ! is_array( $image ) || empty( $image['url'] ) )
return $image;
$thumbnail_id = wp_insert_attachment( $data, $content_struct['guid'], $post_ID );
$attachment_meta_data = wp_generate_attachment_metadata( $thumbnail_id, $content_struct['guid'] );
$attachment_meta_data['push_syndicate_featured_image'] = 'yes';
wp_update_attachment_metadata( $thumbnail_id, $attachment_meta_data );
$thumbnail_id = (int) $image['id'];
if( empty( $thumbnail_id ) )
return new IXR_Error( 500, __( 'Sorry, looks like the image upload failed.', 'syndication' ) );
if ( set_post_thumbnail( $post_ID, $thumbnail_id ) )
$thumbnail_set = set_post_thumbnail( $post_ID, $thumbnail_id );
if ( ! $thumbnail_set )
return new IXR_Error( 403, __( 'Could not attach post thumbnail.' ) );
return $thumbnail_id;
......@@ -479,7 +444,8 @@ class Syndication_WP_XMLRPC_Client_Extensions {
if ( ! current_user_can( 'edit_post', $post_ID ) )
return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
if ( ! delete_post_thumbnail( $post_ID ) )
$result = delete_post_thumbnail( $post_ID );
if ( ! $result )
return new IXR_Error( 403, __( 'Could not remove post thumbnail.' ) );
return true;
......
......@@ -656,7 +656,7 @@ class WP_Push_Syndication_Server {
return;
$client = Syndication_Client_Factory::get_client( $transport_type, $post->ID );
if( $client->test_connection() ) {
if ( $client->test_connection() ) {
add_filter('redirect_post_location', create_function( '$location', 'return add_query_arg("message", 251, $location);' ) );
} else {
$site_enabled = 'off';
......@@ -849,9 +849,10 @@ class WP_Push_Syndication_Server {
$result = $client->new_post( $post_ID );
$this->validate_result_new_post( $result, $slave_post_states, $site->ID, $client );
do_action( 'syn_post_push_new_post', $result, $post_ID, $site, $transport_type, $client, $info );
$this->validate_result_new_post( $result, $slave_post_states, $site->ID, $client );
} else { // states 'success', 'edit-error' and 'remove-error'
$push_edit_shortcircuit = apply_filters( 'syn_pre_push_edit_post_shortcircuit', false, $post_ID, $site, $transport_type, $client, $info );
if ( true === $push_edit_shortcircuit )
......@@ -859,13 +860,10 @@ class WP_Push_Syndication_Server {
$result = $client->edit_post( $post_ID, $info['ext_ID'] );
do_action( 'syn_post_push_edit_post', $result, $post_ID, $site, $transport_type, $client, $info );
$this->validate_result_edit_post( $result, $slave_post_states, $site->ID, $client );
$this->validate_result_edit_post( $result, $info, $slave_post_states, $site->ID, $client );
do_action( 'syn_post_push_edit_post', $result, $post_ID, $site, $transport_type, $client, $info );
}
// TODO: manage thumbnails
}
}
......@@ -882,11 +880,8 @@ class WP_Push_Syndication_Server {
if( $info['state'] == 'success' || $info['state'] == 'edit-error' || $info['state'] == 'remove-error' ) {
$result = $client->delete_post( $info['ext_ID'] );
if( !$result ) {
$slave_post_states[ 'remove-error' ][ $site->ID ] = array(
'error_code' => $client->get_error_code(),
'error_message' => $client->get_error_message()
);
if ( is_wp_error( $result ) ) {
$slave_post_states[ 'remove-error' ][ $site->ID ] = $result;
}
}
......@@ -1026,17 +1021,15 @@ class WP_Push_Syndication_Server {
*/
public function validate_result_new_post( $result, &$slave_post_states, $site_ID, $client ) {
if( $result ) {
$slave_post_states[ 'success' ][ $site_ID ] = array(
'ext_ID' => (int)$client->get_response()
);
if ( is_wp_error( $result ) ) {
$slave_post_states[ 'new-error' ][ $site_ID ] = $result;
} else {
$slave_post_states[ 'new-error' ][ $site_ID ] = array(
'error_code' => $client->get_error_code(),
'error_message' => $client->get_error_message()
$slave_post_states[ 'success' ][ $site_ID ] = array(
'ext_ID' => (int) $result
);
}
return $result;
}
/**
......@@ -1044,20 +1037,16 @@ class WP_Push_Syndication_Server {
* edit-error -> edit-error
* success -> edit-error
*/
public function validate_result_edit_post( $result, $info, &$slave_post_states, $site_ID, $client ) {
if( $result ) {
$slave_post_states[ 'success' ][ $site_ID ] = array(
'ext_ID' => $info[ 'ext_ID' ]
);
public function validate_result_edit_post( $result, &$slave_post_states, $site_ID, $client ) {
if ( is_wp_error( $result ) ) {
$slave_post_states[ 'edit-error' ][ $site_ID ] = $result;
} else {
$slave_post_states[ 'edit-error' ][ $site_ID ] = array(
'ext_ID' => $info[ 'ext_ID' ],
'error_code' => $client->get_error_code(),
'error_message' => $client->get_error_message()
$slave_post_states[ 'success' ][ $site_ID ] = array(
'ext_ID' => (int) $result
);
}
return $result;
}
public function pre_schedule_delete_content( $post_id ) {
......@@ -1352,4 +1341,6 @@ class WP_Push_Syndication_Server {
update_option( 'syn_version', SYNDICATION_VERSION );
}
}
......@@ -70,27 +70,6 @@ interface Syndication_Client {
*/
public function is_post_exists( $ext_ID );
/**
* Get the response message sent from the slave site.
*
* @return string response message.
*/
public function get_response();
/**
* Get the error code.
*
* @return int error code.
*/
public function get_error_code();
/**
* Get the error message sent from the slave site.
*
* @return string error message.
*/
public function get_error_message();
/**
* Display the client settings for the slave site.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment