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

Introduce XML-RPC extensions

Custom XML-RPC endpoints to handle thumbnail upload and management so we can start syndicating thumbnails alongside posts.

see #33
parent 2bfbac19
Branches
No related tags found
No related merge requests found
......@@ -403,3 +403,90 @@ class Syndication_WP_XMLRPC_Client extends WP_HTTP_IXR_Client implements Syndica
}
}
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' );
return $methods;
}
public static function xmlrpc_add_thumbnail( $args ) {
global $wp_xmlrpc_server;
$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'];
$wp_filetype = wp_check_filetype( $content_struct['guid'], null );
if ( empty( $wp_filetype['type'] ) )
return new IXR_Error( 403, __( 'Invalid thumbnail URL.' ) );
$data['post_mime_type'] = $wp_filetype['type'];
$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 );
if ( set_post_thumbnail( $post_ID, $thumbnail_id ) )
return new IXR_Error( 403, __( 'Could not attach post thumbnail.' ) );
return $thumbnail_id;
}
public static function xmlrpc_delete_thumbnail( $args ) {
global $wp_xmlrpc_server;
$wp_xmlrpc_server->escape( $args );
$blog_id = (int) $args[0];
$username = $args[1];
$password = $args[2];
$post_ID = (int)$args[3];
if ( !$user = $wp_xmlrpc_server->login( $username, $password ) )
return $wp_xmlrpc_server->error;
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 ) )
return new IXR_Error( 403, __( 'Could not remove post thumbnail.' ) );
return true;
}
}
Syndication_WP_XMLRPC_Client_Extensions::init();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment