| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_WPLR_Sync_RPC extends Meow_WPLR_Sync_Core { |
| 4 |
|
| 5 |
public function __construct() { |
| 6 |
parent::__construct(); |
| 7 |
add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' )); |
| 8 |
} |
| 9 |
|
| 10 |
// Authenticate and share the useful arguments. |
| 11 |
function rpc_init_with( &$args ) { |
| 12 |
global $wp_xmlrpc_server; |
| 13 |
$wp_xmlrpc_server->escape( $args ); |
| 14 |
$blog_id = array_shift( $args ); |
| 15 |
$username = array_shift( $args ); |
| 16 |
$password = array_shift( $args ); |
| 17 |
$user = $wp_xmlrpc_server->login( $username, $password ); |
| 18 |
if ( !$user ) { |
| 19 |
$this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) ); |
| 20 |
return false; |
| 21 |
} |
| 22 |
|
| 23 |
if ( !current_user_can( 'upload_files' ) ) { |
| 24 |
$this->error = new IXR_Error( 403, __( 'You do not have permission to upload files.' ) ); |
| 25 |
return false; |
| 26 |
} |
| 27 |
return $user; |
| 28 |
} |
| 29 |
|
| 30 |
// Ping for the client. Should probably send back the version of the plugin. |
| 31 |
function rpc_ping( $args ) { |
| 32 |
if ( !$user = $this->rpc_init_with( $args ) ) { |
| 33 |
return $this->error; |
| 34 |
} |
| 35 |
return $user; |
| 36 |
return $user->data->display_name; |
| 37 |
} |
| 38 |
|
| 39 |
// Sync file |
| 40 |
function rpc_sync( $args ) { |
| 41 |
if ( !$user = $this->rpc_init_with( $args ) ) { |
| 42 |
return $this->error; |
| 43 |
} |
| 44 |
$fileinfo = $args[0]; |
| 45 |
$lrinfo = new Meow_WPLR_LRInfo(); |
| 46 |
$lrinfo->lr_id = $fileinfo["id"]; |
| 47 |
$lrinfo->lr_file = $fileinfo["file"]; |
| 48 |
$lrinfo->lr_title = $fileinfo["title"]; |
| 49 |
$lrinfo->lr_caption = $fileinfo["caption"]; |
| 50 |
$lrinfo->lr_desc = $fileinfo["desc"]; |
| 51 |
$lrinfo->lr_alt_text = $fileinfo["altText"]; |
| 52 |
$lrinfo->sync_title = $fileinfo["syncTitle"]; |
| 53 |
$lrinfo->sync_caption = $fileinfo["syncCaption"]; |
| 54 |
$lrinfo->sync_desc = $fileinfo["syncDesc"]; |
| 55 |
$lrinfo->sync_alt_text = $fileinfo["syncAltText"]; |
| 56 |
|
| 57 |
$lrinfo->type = $fileinfo["type"]; |
| 58 |
$file = $this->b64_to_file( $fileinfo["data"] ); |
| 59 |
if ( !$sync = $this->sync_media( $lrinfo, $file ) ) |
| 60 |
return $this->error; |
| 61 |
return $sync; |
| 62 |
} |
| 63 |
|
| 64 |
// Delete file |
| 65 |
function rpc_delete( $args ) { |
| 66 |
if ( !$user = $this->rpc_init_with( $args ) ) { |
| 67 |
return $this->error; |
| 68 |
} |
| 69 |
$list = $this->delete_media( $args[0] ); |
| 70 |
return $list; |
| 71 |
} |
| 72 |
|
| 73 |
// List synced files |
| 74 |
function rpc_list( $args ) { |
| 75 |
if ( !$user = $this->rpc_init_with( $args ) ) { |
| 76 |
return $this->error; |
| 77 |
} |
| 78 |
$list = $this->list_sync_media(); |
| 79 |
return $list; |
| 80 |
} |
| 81 |
|
| 82 |
// List files (the Media IDs) that are not linked |
| 83 |
function rpc_list_unlinks( $args ) { |
| 84 |
if ( !$user = $this->rpc_init_with( $args ) ) { |
| 85 |
return $this->error; |
| 86 |
} |
| 87 |
$list = $this->list_unlinks(); |
| 88 |
return $list; |
| 89 |
} |
| 90 |
|
| 91 |
// Get LinkInfo for the Media ID |
| 92 |
function rpc_linkinfo( $args ) { |
| 93 |
if ( !$user = $this->rpc_init_with( $args ) ) { |
| 94 |
return $this->error; |
| 95 |
} |
| 96 |
$inkinfo = $this->linkinfo_media( $args[0] ); |
| 97 |
return $inkinfo; |
| 98 |
} |
| 99 |
|
| 100 |
// Get LinkInfo for the upload |
| 101 |
function rpc_linkinfo_upload( $args ) { |
| 102 |
if ( !$user = $this->rpc_init_with( $args ) ) { |
| 103 |
return $this->error; |
| 104 |
} |
| 105 |
$file = $this->b64_to_file( $args[0] ); |
| 106 |
$inkinfo = $this->linkinfo_upload( $file, null ); |
| 107 |
return $inkinfo; |
| 108 |
} |
| 109 |
|
| 110 |
// Get LinkInfo for the Media ID |
| 111 |
function rpc_userinfo( $args ) { |
| 112 |
if ( !$user = $this->rpc_init_with( $args ) ) { |
| 113 |
return $this->error; |
| 114 |
} |
| 115 |
return $user->data; |
| 116 |
} |
| 117 |
|
| 118 |
// Get LinkInfo for the Media ID |
| 119 |
function rpc_link( $args ) { |
| 120 |
if ( !$user = $this->rpc_init_with( $args ) ) { |
| 121 |
return $this->error; |
| 122 |
} |
| 123 |
$lr_id = $args[0]; |
| 124 |
$wp_id = $args[1]; |
| 125 |
return $this->link_media( $lr_id, $wp_id ); |
| 126 |
} |
| 127 |
|
| 128 |
function xmlrpc_methods( $methods ) { |
| 129 |
$methods['lrsync.ping'] = array( $this, 'rpc_ping' ); |
| 130 |
$methods['lrsync.sync'] = array( $this, 'rpc_sync' ); |
| 131 |
$methods['lrsync.list'] = array( $this, 'rpc_list' ); |
| 132 |
$methods['lrsync.delete'] = array( $this, 'rpc_delete' ); |
| 133 |
$methods['lrsync.list_unlinks'] = array( $this, 'rpc_list_unlinks' ); |
| 134 |
$methods['lrsync.link'] = array( $this, 'rpc_link' ); |
| 135 |
$methods['lrsync.linkinfo'] = array( $this, 'rpc_linkinfo' ); |
| 136 |
$methods['lrsync.linkinfo_upload'] = array( $this, 'rpc_linkinfo_upload' ); |
| 137 |
$methods['lrsync.userinfo'] = array( $this, 'rpc_userinfo' ); |
| 138 |
return $methods; |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|