3 * XML-RPC protocol support for WordPress
6 * @subpackage Publishing
10 * WordPress XMLRPC server implementation.
12 * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
13 * pingback. Additional WordPress API for managing comments, pages, posts,
16 * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled
17 * via the xmlrpc_enabled filter found in wp_xmlrpc_server::login().
20 * @subpackage Publishing
23 class wp_xmlrpc_server extends IXR_Server {
49 * Register all of the XMLRPC methods that XMLRPC server understands.
51 * Sets up server and method property. Passes XMLRPC
52 * methods through the 'xmlrpc_methods' filter to allow plugins to extend
53 * or replace XMLRPC methods.
57 public function __construct() {
58 $this->methods = array(
60 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
61 'wp.newPost' => 'this:wp_newPost',
62 'wp.editPost' => 'this:wp_editPost',
63 'wp.deletePost' => 'this:wp_deletePost',
64 'wp.getPost' => 'this:wp_getPost',
65 'wp.getPosts' => 'this:wp_getPosts',
66 'wp.newTerm' => 'this:wp_newTerm',
67 'wp.editTerm' => 'this:wp_editTerm',
68 'wp.deleteTerm' => 'this:wp_deleteTerm',
69 'wp.getTerm' => 'this:wp_getTerm',
70 'wp.getTerms' => 'this:wp_getTerms',
71 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
72 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
73 'wp.getUser' => 'this:wp_getUser',
74 'wp.getUsers' => 'this:wp_getUsers',
75 'wp.getProfile' => 'this:wp_getProfile',
76 'wp.editProfile' => 'this:wp_editProfile',
77 'wp.getPage' => 'this:wp_getPage',
78 'wp.getPages' => 'this:wp_getPages',
79 'wp.newPage' => 'this:wp_newPage',
80 'wp.deletePage' => 'this:wp_deletePage',
81 'wp.editPage' => 'this:wp_editPage',
82 'wp.getPageList' => 'this:wp_getPageList',
83 'wp.getAuthors' => 'this:wp_getAuthors',
84 'wp.getCategories' => 'this:mw_getCategories', // Alias
85 'wp.getTags' => 'this:wp_getTags',
86 'wp.newCategory' => 'this:wp_newCategory',
87 'wp.deleteCategory' => 'this:wp_deleteCategory',
88 'wp.suggestCategories' => 'this:wp_suggestCategories',
89 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
90 'wp.deleteFile' => 'this:wp_deletePost', // Alias
91 'wp.getCommentCount' => 'this:wp_getCommentCount',
92 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
93 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
94 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
95 'wp.getOptions' => 'this:wp_getOptions',
96 'wp.setOptions' => 'this:wp_setOptions',
97 'wp.getComment' => 'this:wp_getComment',
98 'wp.getComments' => 'this:wp_getComments',
99 'wp.deleteComment' => 'this:wp_deleteComment',
100 'wp.editComment' => 'this:wp_editComment',
101 'wp.newComment' => 'this:wp_newComment',
102 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
103 'wp.getMediaItem' => 'this:wp_getMediaItem',
104 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
105 'wp.getPostFormats' => 'this:wp_getPostFormats',
106 'wp.getPostType' => 'this:wp_getPostType',
107 'wp.getPostTypes' => 'this:wp_getPostTypes',
108 'wp.getRevisions' => 'this:wp_getRevisions',
109 'wp.restoreRevision' => 'this:wp_restoreRevision',
112 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
113 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
114 'blogger.getPost' => 'this:blogger_getPost',
115 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
116 'blogger.newPost' => 'this:blogger_newPost',
117 'blogger.editPost' => 'this:blogger_editPost',
118 'blogger.deletePost' => 'this:blogger_deletePost',
120 // MetaWeblog API (with MT extensions to structs)
121 'metaWeblog.newPost' => 'this:mw_newPost',
122 'metaWeblog.editPost' => 'this:mw_editPost',
123 'metaWeblog.getPost' => 'this:mw_getPost',
124 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
125 'metaWeblog.getCategories' => 'this:mw_getCategories',
126 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
128 // MetaWeblog API aliases for Blogger API
129 // see http://www.xmlrpc.com/stories/storyReader$2460
130 'metaWeblog.deletePost' => 'this:blogger_deletePost',
131 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
134 'mt.getCategoryList' => 'this:mt_getCategoryList',
135 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
136 'mt.getPostCategories' => 'this:mt_getPostCategories',
137 'mt.setPostCategories' => 'this:mt_setPostCategories',
138 'mt.supportedMethods' => 'this:mt_supportedMethods',
139 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
140 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
141 'mt.publishPost' => 'this:mt_publishPost',
144 'pingback.ping' => 'this:pingback_ping',
145 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
147 'demo.sayHello' => 'this:sayHello',
148 'demo.addTwoNumbers' => 'this:addTwoNumbers'
151 $this->initialise_blog_option_info();
154 * Filter the methods exposed by the XML-RPC server.
156 * This filter can be used to add new methods, and remove built-in methods.
160 * @param array $methods An array of XML-RPC methods.
162 $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
166 * Make private/protected methods readable for backwards compatibility.
171 * @param callable $name Method to call.
172 * @param array $arguments Arguments to pass when calling.
173 * @return array|IXR_Error|false Return value of the callback, false otherwise.
175 public function __call( $name, $arguments ) {
176 if ( '_multisite_getUsersBlogs' === $name ) {
177 return call_user_func_array( array( $this, $name ), $arguments );
185 public function serve_request() {
186 $this->IXR_Server($this->methods);
190 * Test XMLRPC API by saying, "Hello!" to client.
194 * @return string Hello string response.
196 public function sayHello() {
201 * Test XMLRPC API by adding two numbers for client.
205 * @param array $args {
206 * Method arguments. Note: arguments must be ordered as documented.
208 * @type int $number1 A number to add.
209 * @type int $number2 A second number to add.
211 * @return int Sum of the two given numbers.
213 public function addTwoNumbers( $args ) {
216 return $number1 + $number2;
224 * @param string $username User's username.
225 * @param string $password User's password.
226 * @return WP_User|bool WP_User object if authentication passed, false otherwise
228 public function login( $username, $password ) {
230 * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
231 * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
233 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
234 if ( false === $enabled ) {
235 $enabled = apply_filters( 'option_enable_xmlrpc', true );
239 * Filter whether XML-RPC is enabled.
241 * This is the proper filter for turning off XML-RPC.
245 * @param bool $enabled Whether XML-RPC is enabled. Default true.
247 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
250 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
254 $user = wp_authenticate($username, $password);
256 if (is_wp_error($user)) {
257 $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
260 * Filter the XML-RPC user login error message.
264 * @param string $error The XML-RPC error message.
265 * @param WP_User $user WP_User object.
267 $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
271 wp_set_current_user( $user->ID );
276 * Check user's credentials. Deprecated.
280 * @deprecated use wp_xmlrpc_server::login
281 * @see wp_xmlrpc_server::login
283 * @param string $username User's username.
284 * @param string $password User's password.
285 * @return bool Whether authentication passed.
287 public function login_pass_ok( $username, $password ) {
288 return (bool) $this->login( $username, $password );
292 * Escape string or array of strings for database.
296 * @param string|array $data Escape single string or array of strings.
297 * @return string|void Returns with string is passed, alters by-reference
298 * when array is passed.
300 public function escape( &$data ) {
301 if ( ! is_array( $data ) )
302 return wp_slash( $data );
304 foreach ( $data as &$v ) {
305 if ( is_array( $v ) )
307 elseif ( ! is_object( $v ) )
313 * Retrieve custom fields for post.
317 * @param int $post_id Post ID.
318 * @return array Custom fields, if exist.
320 public function get_custom_fields($post_id) {
321 $post_id = (int) $post_id;
323 $custom_fields = array();
325 foreach ( (array) has_meta($post_id) as $meta ) {
326 // Don't expose protected fields.
327 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
330 $custom_fields[] = array(
331 "id" => $meta['meta_id'],
332 "key" => $meta['meta_key'],
333 "value" => $meta['meta_value']
337 return $custom_fields;
341 * Set custom fields for post.
345 * @param int $post_id Post ID.
346 * @param array $fields Custom fields.
348 public function set_custom_fields($post_id, $fields) {
349 $post_id = (int) $post_id;
351 foreach ( (array) $fields as $meta ) {
352 if ( isset($meta['id']) ) {
353 $meta['id'] = (int) $meta['id'];
354 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
355 if ( isset($meta['key']) ) {
356 $meta['key'] = wp_unslash( $meta['key'] );
357 if ( $meta['key'] !== $pmeta->meta_key )
359 $meta['value'] = wp_unslash( $meta['value'] );
360 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
361 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
362 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
363 delete_metadata_by_mid( 'post', $meta['id'] );
365 } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
366 add_post_meta( $post_id, $meta['key'], $meta['value'] );
372 * Set up blog options property.
374 * Passes property through {@see 'xmlrpc_blog_options'} filter.
378 * @global string $wp_version
380 public function initialise_blog_option_info() {
383 $this->blog_options = array(
385 'software_name' => array(
386 'desc' => __( 'Software Name' ),
388 'value' => 'WordPress'
390 'software_version' => array(
391 'desc' => __( 'Software Version' ),
393 'value' => $wp_version
396 'desc' => __( 'WordPress Address (URL)' ),
398 'option' => 'siteurl'
401 'desc' => __( 'Site Address (URL)' ),
405 'login_url' => array(
406 'desc' => __( 'Login Address (URL)' ),
408 'value' => wp_login_url( )
410 'admin_url' => array(
411 'desc' => __( 'The URL to the admin area' ),
413 'value' => get_admin_url( )
415 'image_default_link_type' => array(
416 'desc' => __( 'Image default link type' ),
418 'option' => 'image_default_link_type'
420 'image_default_size' => array(
421 'desc' => __( 'Image default size' ),
423 'option' => 'image_default_size'
425 'image_default_align' => array(
426 'desc' => __( 'Image default align' ),
428 'option' => 'image_default_align'
431 'desc' => __( 'Template' ),
433 'option' => 'template'
435 'stylesheet' => array(
436 'desc' => __( 'Stylesheet' ),
438 'option' => 'stylesheet'
440 'post_thumbnail' => array(
441 'desc' => __('Post Thumbnail'),
443 'value' => current_theme_supports( 'post-thumbnails' )
447 'time_zone' => array(
448 'desc' => __( 'Time Zone' ),
450 'option' => 'gmt_offset'
452 'blog_title' => array(
453 'desc' => __( 'Site Title' ),
455 'option' => 'blogname'
457 'blog_tagline' => array(
458 'desc' => __( 'Site Tagline' ),
460 'option' => 'blogdescription'
462 'date_format' => array(
463 'desc' => __( 'Date Format' ),
465 'option' => 'date_format'
467 'time_format' => array(
468 'desc' => __( 'Time Format' ),
470 'option' => 'time_format'
472 'users_can_register' => array(
473 'desc' => __( 'Allow new users to sign up' ),
475 'option' => 'users_can_register'
477 'thumbnail_size_w' => array(
478 'desc' => __( 'Thumbnail Width' ),
480 'option' => 'thumbnail_size_w'
482 'thumbnail_size_h' => array(
483 'desc' => __( 'Thumbnail Height' ),
485 'option' => 'thumbnail_size_h'
487 'thumbnail_crop' => array(
488 'desc' => __( 'Crop thumbnail to exact dimensions' ),
490 'option' => 'thumbnail_crop'
492 'medium_size_w' => array(
493 'desc' => __( 'Medium size image width' ),
495 'option' => 'medium_size_w'
497 'medium_size_h' => array(
498 'desc' => __( 'Medium size image height' ),
500 'option' => 'medium_size_h'
502 'large_size_w' => array(
503 'desc' => __( 'Large size image width' ),
505 'option' => 'large_size_w'
507 'large_size_h' => array(
508 'desc' => __( 'Large size image height' ),
510 'option' => 'large_size_h'
512 'default_comment_status' => array(
513 'desc' => __( 'Allow people to post comments on new articles' ),
515 'option' => 'default_comment_status'
517 'default_ping_status' => array(
518 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles' ),
520 'option' => 'default_ping_status'
525 * Filter the XML-RPC blog options property.
529 * @param array $blog_options An array of XML-RPC blog options.
531 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
535 * Retrieve the blogs of the user.
539 * @param array $args {
540 * Method arguments. Note: arguments must be ordered as documented.
542 * @type string $username Username.
543 * @type string $password Password.
545 * @return array|IXR_Error Array contains:
550 * - 'xmlrpc' - url of xmlrpc endpoint
552 public function wp_getUsersBlogs( $args ) {
553 // If this isn't on WPMU then just use blogger_getUsersBlogs
554 if ( !is_multisite() ) {
555 array_unshift( $args, 1 );
556 return $this->blogger_getUsersBlogs( $args );
559 $this->escape( $args );
561 $username = $args[0];
562 $password = $args[1];
564 if ( !$user = $this->login($username, $password) )
568 * Fires after the XML-RPC user has been authenticated but before the rest of
569 * the method logic begins.
571 * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
572 * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
576 * @param string $name The method name.
578 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
580 $blogs = (array) get_blogs_of_user( $user->ID );
583 foreach ( $blogs as $blog ) {
584 // Don't include blogs that aren't hosted at this site.
585 if ( $blog->site_id != get_current_site()->id )
588 $blog_id = $blog->userblog_id;
590 switch_to_blog( $blog_id );
592 $is_admin = current_user_can( 'manage_options' );
595 'isAdmin' => $is_admin,
596 'url' => home_url( '/' ),
597 'blogid' => (string) $blog_id,
598 'blogName' => get_option( 'blogname' ),
599 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
602 restore_current_blog();
609 * Checks if the method received at least the minimum number of arguments.
614 * @param string|array $args Sanitize single string or array of strings.
615 * @param int $count Minimum number of arguments.
616 * @return bool if `$args` contains at least $count arguments.
618 protected function minimum_args( $args, $count ) {
619 if ( count( $args ) < $count ) {
620 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
628 * Prepares taxonomy data for return in an XML-RPC object.
632 * @param object $taxonomy The unprepared taxonomy data.
633 * @param array $fields The subset of taxonomy fields to return.
634 * @return array The prepared taxonomy data.
636 protected function _prepare_taxonomy( $taxonomy, $fields ) {
638 'name' => $taxonomy->name,
639 'label' => $taxonomy->label,
640 'hierarchical' => (bool) $taxonomy->hierarchical,
641 'public' => (bool) $taxonomy->public,
642 'show_ui' => (bool) $taxonomy->show_ui,
643 '_builtin' => (bool) $taxonomy->_builtin,
646 if ( in_array( 'labels', $fields ) )
647 $_taxonomy['labels'] = (array) $taxonomy->labels;
649 if ( in_array( 'cap', $fields ) )
650 $_taxonomy['cap'] = (array) $taxonomy->cap;
652 if ( in_array( 'menu', $fields ) )
653 $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
655 if ( in_array( 'object_type', $fields ) )
656 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
659 * Filter XML-RPC-prepared data for the given taxonomy.
663 * @param array $_taxonomy An array of taxonomy data.
664 * @param object $taxonomy Taxonomy object.
665 * @param array $fields The subset of taxonomy fields to return.
667 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
671 * Prepares term data for return in an XML-RPC object.
675 * @param array|object $term The unprepared term data.
676 * @return array The prepared term data.
678 protected function _prepare_term( $term ) {
680 if ( ! is_array( $_term ) )
681 $_term = get_object_vars( $_term );
683 // For integers which may be larger than XML-RPC supports ensure we return strings.
684 $_term['term_id'] = strval( $_term['term_id'] );
685 $_term['term_group'] = strval( $_term['term_group'] );
686 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
687 $_term['parent'] = strval( $_term['parent'] );
689 // Count we are happy to return as an integer because people really shouldn't use terms that much.
690 $_term['count'] = intval( $_term['count'] );
693 * Filter XML-RPC-prepared data for the given term.
697 * @param array $_term An array of term data.
698 * @param array|object $term Term object or array.
700 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
704 * Convert a WordPress date string to an IXR_Date object.
708 * @param string $date Date string to convert.
709 * @return IXR_Date IXR_Date object.
711 protected function _convert_date( $date ) {
712 if ( $date === '0000-00-00 00:00:00' ) {
713 return new IXR_Date( '00000000T00:00:00Z' );
715 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
719 * Convert a WordPress GMT date string to an IXR_Date object.
723 * @param string $date_gmt WordPress GMT date string.
724 * @param string $date Date string.
725 * @return IXR_Date IXR_Date object.
727 protected function _convert_date_gmt( $date_gmt, $date ) {
728 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
729 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
731 return $this->_convert_date( $date_gmt );
735 * Prepares post data for return in an XML-RPC object.
739 * @param array $post The unprepared post data.
740 * @param array $fields The subset of post type fields to return.
741 * @return array The prepared post data.
743 protected function _prepare_post( $post, $fields ) {
744 // Holds the data for this post. built up based on $fields.
745 $_post = array( 'post_id' => strval( $post['ID'] ) );
747 // Prepare common post fields.
748 $post_fields = array(
749 'post_title' => $post['post_title'],
750 'post_date' => $this->_convert_date( $post['post_date'] ),
751 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
752 'post_modified' => $this->_convert_date( $post['post_modified'] ),
753 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
754 'post_status' => $post['post_status'],
755 'post_type' => $post['post_type'],
756 'post_name' => $post['post_name'],
757 'post_author' => $post['post_author'],
758 'post_password' => $post['post_password'],
759 'post_excerpt' => $post['post_excerpt'],
760 'post_content' => $post['post_content'],
761 'post_parent' => strval( $post['post_parent'] ),
762 'post_mime_type' => $post['post_mime_type'],
763 'link' => post_permalink( $post['ID'] ),
764 'guid' => $post['guid'],
765 'menu_order' => intval( $post['menu_order'] ),
766 'comment_status' => $post['comment_status'],
767 'ping_status' => $post['ping_status'],
768 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
772 $post_fields['post_thumbnail'] = array();
773 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
774 if ( $thumbnail_id ) {
775 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
776 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
779 // Consider future posts as published.
780 if ( $post_fields['post_status'] === 'future' )
781 $post_fields['post_status'] = 'publish';
783 // Fill in blank post format.
784 $post_fields['post_format'] = get_post_format( $post['ID'] );
785 if ( empty( $post_fields['post_format'] ) )
786 $post_fields['post_format'] = 'standard';
788 // Merge requested $post_fields fields into $_post.
789 if ( in_array( 'post', $fields ) ) {
790 $_post = array_merge( $_post, $post_fields );
792 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
793 $_post = array_merge( $_post, $requested_fields );
796 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
798 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
799 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
800 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
801 $_post['terms'] = array();
802 foreach ( $terms as $term ) {
803 $_post['terms'][] = $this->_prepare_term( $term );
807 if ( in_array( 'custom_fields', $fields ) )
808 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
810 if ( in_array( 'enclosure', $fields ) ) {
811 $_post['enclosure'] = array();
812 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
813 if ( ! empty( $enclosures ) ) {
814 $encdata = explode( "\n", $enclosures[0] );
815 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
816 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
817 $_post['enclosure']['type'] = trim( $encdata[2] );
822 * Filter XML-RPC-prepared date for the given post.
826 * @param array $_post An array of modified post data.
827 * @param array $post An array of post data.
828 * @param array $fields An array of post fields.
830 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
834 * Prepares post data for return in an XML-RPC object.
838 * @param object $post_type Post type object.
839 * @param array $fields The subset of post fields to return.
840 * @return array The prepared post type data.
842 protected function _prepare_post_type( $post_type, $fields ) {
844 'name' => $post_type->name,
845 'label' => $post_type->label,
846 'hierarchical' => (bool) $post_type->hierarchical,
847 'public' => (bool) $post_type->public,
848 'show_ui' => (bool) $post_type->show_ui,
849 '_builtin' => (bool) $post_type->_builtin,
850 'has_archive' => (bool) $post_type->has_archive,
851 'supports' => get_all_post_type_supports( $post_type->name ),
854 if ( in_array( 'labels', $fields ) ) {
855 $_post_type['labels'] = (array) $post_type->labels;
858 if ( in_array( 'cap', $fields ) ) {
859 $_post_type['cap'] = (array) $post_type->cap;
860 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
863 if ( in_array( 'menu', $fields ) ) {
864 $_post_type['menu_position'] = (int) $post_type->menu_position;
865 $_post_type['menu_icon'] = $post_type->menu_icon;
866 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
869 if ( in_array( 'taxonomies', $fields ) )
870 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
873 * Filter XML-RPC-prepared date for the given post type.
877 * @param array $_post_type An array of post type data.
878 * @param object $post_type Post type object.
880 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
884 * Prepares media item data for return in an XML-RPC object.
888 * @param object $media_item The unprepared media item data.
889 * @param string $thumbnail_size The image size to use for the thumbnail URL.
890 * @return array The prepared media item data.
892 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
893 $_media_item = array(
894 'attachment_id' => strval( $media_item->ID ),
895 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
896 'parent' => $media_item->post_parent,
897 'link' => wp_get_attachment_url( $media_item->ID ),
898 'title' => $media_item->post_title,
899 'caption' => $media_item->post_excerpt,
900 'description' => $media_item->post_content,
901 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
904 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
905 if ( $thumbnail_src )
906 $_media_item['thumbnail'] = $thumbnail_src[0];
908 $_media_item['thumbnail'] = $_media_item['link'];
911 * Filter XML-RPC-prepared data for the given media item.
915 * @param array $_media_item An array of media item data.
916 * @param object $media_item Media item object.
917 * @param string $thumbnail_size Image size.
919 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
923 * Prepares page data for return in an XML-RPC object.
927 * @param object $page The unprepared page data.
928 * @return array The prepared page data.
930 protected function _prepare_page( $page ) {
931 // Get all of the page content and link.
932 $full_page = get_extended( $page->post_content );
933 $link = post_permalink( $page->ID );
935 // Get info the page parent if there is one.
937 if ( ! empty( $page->post_parent ) ) {
938 $parent = get_post( $page->post_parent );
939 $parent_title = $parent->post_title;
942 // Determine comment and ping settings.
943 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
944 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
947 $page_date = $this->_convert_date( $page->post_date );
948 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
950 // Pull the categories info together.
951 $categories = array();
952 if ( is_object_in_taxonomy( 'page', 'category' ) ) {
953 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
954 $categories[] = get_cat_name( $cat_id );
958 // Get the author info.
959 $author = get_userdata( $page->post_author );
961 $page_template = get_page_template_slug( $page->ID );
962 if ( empty( $page_template ) )
963 $page_template = 'default';
966 'dateCreated' => $page_date,
967 'userid' => $page->post_author,
968 'page_id' => $page->ID,
969 'page_status' => $page->post_status,
970 'description' => $full_page['main'],
971 'title' => $page->post_title,
973 'permaLink' => $link,
974 'categories' => $categories,
975 'excerpt' => $page->post_excerpt,
976 'text_more' => $full_page['extended'],
977 'mt_allow_comments' => $allow_comments,
978 'mt_allow_pings' => $allow_pings,
979 'wp_slug' => $page->post_name,
980 'wp_password' => $page->post_password,
981 'wp_author' => $author->display_name,
982 'wp_page_parent_id' => $page->post_parent,
983 'wp_page_parent_title' => $parent_title,
984 'wp_page_order' => $page->menu_order,
985 'wp_author_id' => (string) $author->ID,
986 'wp_author_display_name' => $author->display_name,
987 'date_created_gmt' => $page_date_gmt,
988 'custom_fields' => $this->get_custom_fields( $page->ID ),
989 'wp_page_template' => $page_template
993 * Filter XML-RPC-prepared data for the given page.
997 * @param array $_page An array of page data.
998 * @param WP_Post $page Page object.
1000 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
1004 * Prepares comment data for return in an XML-RPC object.
1008 * @param object $comment The unprepared comment data.
1009 * @return array The prepared comment data.
1011 protected function _prepare_comment( $comment ) {
1012 // Format page date.
1013 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
1015 if ( '0' == $comment->comment_approved ) {
1016 $comment_status = 'hold';
1017 } elseif ( 'spam' == $comment->comment_approved ) {
1018 $comment_status = 'spam';
1019 } elseif ( '1' == $comment->comment_approved ) {
1020 $comment_status = 'approve';
1022 $comment_status = $comment->comment_approved;
1025 'date_created_gmt' => $comment_date_gmt,
1026 'user_id' => $comment->user_id,
1027 'comment_id' => $comment->comment_ID,
1028 'parent' => $comment->comment_parent,
1029 'status' => $comment_status,
1030 'content' => $comment->comment_content,
1031 'link' => get_comment_link($comment),
1032 'post_id' => $comment->comment_post_ID,
1033 'post_title' => get_the_title($comment->comment_post_ID),
1034 'author' => $comment->comment_author,
1035 'author_url' => $comment->comment_author_url,
1036 'author_email' => $comment->comment_author_email,
1037 'author_ip' => $comment->comment_author_IP,
1038 'type' => $comment->comment_type,
1042 * Filter XML-RPC-prepared data for the given comment.
1046 * @param array $_comment An array of prepared comment data.
1047 * @param object $comment Comment object.
1049 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
1053 * Prepares user data for return in an XML-RPC object.
1057 * @param WP_User $user The unprepared user object.
1058 * @param array $fields The subset of user fields to return.
1059 * @return array The prepared user data.
1061 protected function _prepare_user( $user, $fields ) {
1062 $_user = array( 'user_id' => strval( $user->ID ) );
1064 $user_fields = array(
1065 'username' => $user->user_login,
1066 'first_name' => $user->user_firstname,
1067 'last_name' => $user->user_lastname,
1068 'registered' => $this->_convert_date( $user->user_registered ),
1069 'bio' => $user->user_description,
1070 'email' => $user->user_email,
1071 'nickname' => $user->nickname,
1072 'nicename' => $user->user_nicename,
1073 'url' => $user->user_url,
1074 'display_name' => $user->display_name,
1075 'roles' => $user->roles,
1078 if ( in_array( 'all', $fields ) ) {
1079 $_user = array_merge( $_user, $user_fields );
1081 if ( in_array( 'basic', $fields ) ) {
1082 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
1083 $fields = array_merge( $fields, $basic_fields );
1085 $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
1086 $_user = array_merge( $_user, $requested_fields );
1090 * Filter XML-RPC-prepared data for the given user.
1094 * @param array $_user An array of user data.
1095 * @param WP_User $user User object.
1096 * @param array $fields An array of user fields.
1098 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
1102 * Create a new post for any registered post type.
1106 * @link http://en.wikipedia.org/wiki/RSS_enclosure for information on RSS enclosures.
1108 * @param array $args {
1109 * Method arguments. Note: top-level arguments must be ordered as documented.
1111 * @type int $blog_id Blog ID (unused).
1112 * @type string $username Username.
1113 * @type string $password Password.
1114 * @type array $content_struct {
1115 * Content struct for adding a new post. See wp_insert_post() for information on
1116 * additional post fields
1118 * @type string $post_type Post type. Default 'post'.
1119 * @type string $post_status Post status. Default 'draft'
1120 * @type string $post_title Post title.
1121 * @type int $post_author Post author ID.
1122 * @type string $post_excerpt Post excerpt.
1123 * @type string $post_content Post content.
1124 * @type string $post_date_gmt Post date in GMT.
1125 * @type string $post_date Post date.
1126 * @type string $post_password Post password (20-character limit).
1127 * @type string $comment_status Post comment enabled status. Accepts 'open' or 'closed'.
1128 * @type string $ping_status Post ping status. Accepts 'open' or 'closed'.
1129 * @type bool $sticky Whether the post should be sticky. Automatically false if
1130 * `$post_status` is 'private'.
1131 * @type int $post_thumbnail ID of an image to use as the post thumbnail/featured image.
1132 * @type array $custom_fields Array of meta key/value pairs to add to the post.
1133 * @type array $terms Associative array with taxonomy names as keys and arrays
1134 * of term IDs as values.
1135 * @type array $terms_names Associative array with taxonomy names as keys and arrays
1136 * of term names as values.
1137 * @type array $enclosure {
1138 * Array of feed enclosure data to add to post meta.
1140 * @type string $url URL for the feed enclosure.
1141 * @type int $length Size in bytes of the enclosure.
1142 * @type string $type Mime-type for the enclosure.
1146 * @return int|IXR_Error Post ID on success, IXR_Error instance otherwise.
1148 public function wp_newPost( $args ) {
1149 if ( ! $this->minimum_args( $args, 4 ) )
1150 return $this->error;
1152 $this->escape( $args );
1154 $username = $args[1];
1155 $password = $args[2];
1156 $content_struct = $args[3];
1158 if ( ! $user = $this->login( $username, $password ) )
1159 return $this->error;
1161 // convert the date field back to IXR form
1162 if ( isset( $content_struct['post_date'] ) && ! ( $content_struct['post_date'] instanceof IXR_Date ) ) {
1163 $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
1166 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1167 // since _insert_post will ignore the non-GMT date if the GMT date is set
1168 if ( isset( $content_struct['post_date_gmt'] ) && ! ( $content_struct['post_date_gmt'] instanceof IXR_Date ) ) {
1169 if ( $content_struct['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) {
1170 unset( $content_struct['post_date_gmt'] );
1172 $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
1176 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1177 do_action( 'xmlrpc_call', 'wp.newPost' );
1179 unset( $content_struct['ID'] );
1181 return $this->_insert_post( $user, $content_struct );
1185 * Helper method for filtering out elements from an array.
1189 * @param int $count Number to compare to one.
1191 private function _is_greater_than_one( $count ) {
1196 * Encapsulate the logic for sticking a post
1197 * and determining if the user has permission to do so
1202 * @param array $post_data
1203 * @param bool $update
1204 * @return void|IXR_Error
1206 private function _toggle_sticky( $post_data, $update = false ) {
1207 $post_type = get_post_type_object( $post_data['post_type'] );
1209 // Private and password-protected posts cannot be stickied.
1210 if ( 'private' === $post_data['post_status'] || ! empty( $post_data['post_password'] ) ) {
1211 // Error if the client tried to stick the post, otherwise, silently unstick.
1212 if ( ! empty( $post_data['sticky'] ) ) {
1213 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1217 unstick_post( $post_data['ID'] );
1219 } elseif ( isset( $post_data['sticky'] ) ) {
1220 if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) {
1221 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1224 $sticky = wp_validate_boolean( $post_data['sticky'] );
1226 stick_post( $post_data['ID'] );
1228 unstick_post( $post_data['ID'] );
1234 * Helper method for wp_newPost() and wp_editPost(), containing shared logic.
1239 * @see wp_insert_post()
1241 * @param WP_User $user The post author if post_author isn't set in $content_struct.
1242 * @param array|IXR_Error $content_struct Post data to insert.
1243 * @return IXR_Error|string
1245 protected function _insert_post( $user, $content_struct ) {
1246 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1247 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1249 $post_data = wp_parse_args( $content_struct, $defaults );
1251 $post_type = get_post_type_object( $post_data['post_type'] );
1253 return new IXR_Error( 403, __( 'Invalid post type' ) );
1255 $update = ! empty( $post_data['ID'] );
1258 if ( ! get_post( $post_data['ID'] ) )
1259 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1260 if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1261 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1262 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1263 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1265 if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1266 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1269 switch ( $post_data['post_status'] ) {
1274 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1275 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1279 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1280 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1283 if ( ! get_post_status_object( $post_data['post_status'] ) )
1284 $post_data['post_status'] = 'draft';
1288 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1289 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1291 $post_data['post_author'] = absint( $post_data['post_author'] );
1292 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1293 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1294 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1296 $author = get_userdata( $post_data['post_author'] );
1299 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1301 $post_data['post_author'] = $user->ID;
1304 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1305 unset( $post_data['comment_status'] );
1307 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1308 unset( $post_data['ping_status'] );
1310 // Do some timestamp voodoo.
1311 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1312 // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
1313 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1314 } elseif ( ! empty( $post_data['post_date'] ) ) {
1315 $dateCreated = $post_data['post_date']->getIso();
1318 if ( ! empty( $dateCreated ) ) {
1319 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1320 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1323 if ( ! isset( $post_data['ID'] ) )
1324 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1325 $post_ID = $post_data['ID'];
1327 if ( $post_data['post_type'] == 'post' ) {
1328 $error = $this->_toggle_sticky( $post_data, $update );
1334 if ( isset( $post_data['post_thumbnail'] ) ) {
1335 // empty value deletes, non-empty value adds/updates.
1336 if ( ! $post_data['post_thumbnail'] )
1337 delete_post_thumbnail( $post_ID );
1338 elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1339 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1340 set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1341 unset( $content_struct['post_thumbnail'] );
1344 if ( isset( $post_data['custom_fields'] ) )
1345 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1347 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1348 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1350 // Accumulate term IDs from terms and terms_names.
1353 // First validate the terms specified by ID.
1354 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1355 $taxonomies = array_keys( $post_data['terms'] );
1357 // Validating term ids.
1358 foreach ( $taxonomies as $taxonomy ) {
1359 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1360 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1362 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1363 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1365 $term_ids = $post_data['terms'][$taxonomy];
1366 $terms[ $taxonomy ] = array();
1367 foreach ( $term_ids as $term_id ) {
1368 $term = get_term_by( 'id', $term_id, $taxonomy );
1371 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1373 $terms[$taxonomy][] = (int) $term_id;
1378 // Now validate terms specified by name.
1379 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1380 $taxonomies = array_keys( $post_data['terms_names'] );
1382 foreach ( $taxonomies as $taxonomy ) {
1383 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1384 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1386 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1387 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1390 * For hierarchical taxonomies, we can't assign a term when multiple terms
1391 * in the hierarchy share the same name.
1393 $ambiguous_terms = array();
1394 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1395 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1397 // Count the number of terms with the same name.
1398 $tax_term_names_count = array_count_values( $tax_term_names );
1400 // Filter out non-ambiguous term names.
1401 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1403 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1406 $term_names = $post_data['terms_names'][$taxonomy];
1407 foreach ( $term_names as $term_name ) {
1408 if ( in_array( $term_name, $ambiguous_terms ) )
1409 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1411 $term = get_term_by( 'name', $term_name, $taxonomy );
1414 // Term doesn't exist, so check that the user is allowed to create new terms.
1415 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1416 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1418 // Create the new term.
1419 $term_info = wp_insert_term( $term_name, $taxonomy );
1420 if ( is_wp_error( $term_info ) )
1421 return new IXR_Error( 500, $term_info->get_error_message() );
1423 $terms[$taxonomy][] = (int) $term_info['term_id'];
1425 $terms[$taxonomy][] = (int) $term->term_id;
1431 $post_data['tax_input'] = $terms;
1432 unset( $post_data['terms'], $post_data['terms_names'] );
1434 // Do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'.
1435 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1438 if ( isset( $post_data['post_format'] ) ) {
1439 $format = set_post_format( $post_ID, $post_data['post_format'] );
1441 if ( is_wp_error( $format ) )
1442 return new IXR_Error( 500, $format->get_error_message() );
1444 unset( $post_data['post_format'] );
1447 // Handle enclosures.
1448 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1449 $this->add_enclosure_if_new( $post_ID, $enclosure );
1451 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1454 * Filter post data array to be inserted via XML-RPC.
1458 * @param array $post_data Parsed array of post data.
1459 * @param array $content_struct Post data array.
1461 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1463 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1464 if ( is_wp_error( $post_ID ) )
1465 return new IXR_Error( 500, $post_ID->get_error_message() );
1468 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1470 return strval( $post_ID );
1474 * Edit a post for any registered post type.
1476 * The $content_struct parameter only needs to contain fields that
1477 * should be changed. All other fields will retain their existing values.
1481 * @param array $args {
1482 * Method arguments. Note: arguments must be ordered as documented.
1484 * @type int $blog_id Blog ID (unused).
1485 * @type string $username Username.
1486 * @type string $password Password.
1487 * @type int $post_id Post ID.
1488 * @type array $content_struct Extra content arguments.
1490 * @return true|IXR_Error True on success, IXR_Error on failure.
1492 public function wp_editPost( $args ) {
1493 if ( ! $this->minimum_args( $args, 5 ) )
1494 return $this->error;
1496 $this->escape( $args );
1498 $username = $args[1];
1499 $password = $args[2];
1500 $post_id = (int) $args[3];
1501 $content_struct = $args[4];
1503 if ( ! $user = $this->login( $username, $password ) )
1504 return $this->error;
1506 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1507 do_action( 'xmlrpc_call', 'wp.editPost' );
1509 $post = get_post( $post_id, ARRAY_A );
1511 if ( empty( $post['ID'] ) )
1512 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1514 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1515 // If the post has been modified since the date provided, return an error.
1516 if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1517 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1521 // Convert the date field back to IXR form.
1522 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1525 * Ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1526 * since _insert_post() will ignore the non-GMT date if the GMT date is set.
1528 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1529 unset( $post['post_date_gmt'] );
1531 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1533 $this->escape( $post );
1534 $merged_content_struct = array_merge( $post, $content_struct );
1536 $retval = $this->_insert_post( $user, $merged_content_struct );
1537 if ( $retval instanceof IXR_Error )
1544 * Delete a post for any registered post type.
1548 * @see wp_delete_post()
1550 * @param array $args {
1551 * Method arguments. Note: arguments must be ordered as documented.
1553 * @type int $blog_id Blog ID (unused).
1554 * @type string $username Username.
1555 * @type string $password Password.
1556 * @type int $post_id Post ID.
1558 * @return true|IXR_Error True on success, IXR_Error instance on failure.
1560 public function wp_deletePost( $args ) {
1561 if ( ! $this->minimum_args( $args, 4 ) )
1562 return $this->error;
1564 $this->escape( $args );
1566 $username = $args[1];
1567 $password = $args[2];
1568 $post_id = (int) $args[3];
1570 if ( ! $user = $this->login( $username, $password ) )
1571 return $this->error;
1573 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1574 do_action( 'xmlrpc_call', 'wp.deletePost' );
1576 $post = get_post( $post_id, ARRAY_A );
1577 if ( empty( $post['ID'] ) )
1578 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1580 if ( ! current_user_can( 'delete_post', $post_id ) )
1581 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1583 $result = wp_delete_post( $post_id );
1586 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1596 * The optional $fields parameter specifies what fields will be included
1597 * in the response array. This should be a list of field names. 'post_id' will
1598 * always be included in the response regardless of the value of $fields.
1600 * Instead of, or in addition to, individual field names, conceptual group
1601 * names can be used to specify multiple fields. The available conceptual
1602 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1607 * @param array $args {
1608 * Method arguments. Note: arguments must be ordered as documented.
1610 * @type int $blog_id Blog ID (unused).
1611 * @type string $username Username.
1612 * @type string $password Password.
1613 * @type int $post_id Post ID.
1614 * @type array $fields The subset of post type fields to return.
1616 * @return array|IXR_Error Array contains (based on $fields parameter):
1622 * - 'post_modified_gmt'
1631 * - 'comment_status'
1640 public function wp_getPost( $args ) {
1641 if ( ! $this->minimum_args( $args, 4 ) )
1642 return $this->error;
1644 $this->escape( $args );
1646 $username = $args[1];
1647 $password = $args[2];
1648 $post_id = (int) $args[3];
1650 if ( isset( $args[4] ) ) {
1654 * Filter the list of post query fields used by the given XML-RPC method.
1658 * @param array $fields Array of post fields. Default array contains 'post', 'terms', and 'custom_fields'.
1659 * @param string $method Method name.
1661 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1664 if ( ! $user = $this->login( $username, $password ) )
1665 return $this->error;
1667 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1668 do_action( 'xmlrpc_call', 'wp.getPost' );
1670 $post = get_post( $post_id, ARRAY_A );
1672 if ( empty( $post['ID'] ) )
1673 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1675 if ( ! current_user_can( 'edit_post', $post_id ) )
1676 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1678 return $this->_prepare_post( $post, $fields );
1686 * @see wp_get_recent_posts()
1687 * @see wp_getPost() for more on `$fields`
1688 * @see get_posts() for more on `$filter` values
1690 * @param array $args {
1691 * Method arguments. Note: arguments must be ordered as documented.
1693 * @type int $blog_id Blog ID (unused).
1694 * @type string $username Username.
1695 * @type string $password Password.
1696 * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'post_type',
1697 * 'post_status', 'number', 'offset', 'orderby', and 'order'.
1698 * Default empty array.
1699 * @type array $fields Optional. The subset of post type fields to return in the response array.
1701 * @return array|IXR_Error Array contains a collection of posts.
1703 public function wp_getPosts( $args ) {
1704 if ( ! $this->minimum_args( $args, 3 ) )
1705 return $this->error;
1707 $this->escape( $args );
1709 $username = $args[1];
1710 $password = $args[2];
1711 $filter = isset( $args[3] ) ? $args[3] : array();
1713 if ( isset( $args[4] ) ) {
1716 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1717 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1720 if ( ! $user = $this->login( $username, $password ) )
1721 return $this->error;
1723 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1724 do_action( 'xmlrpc_call', 'wp.getPosts' );
1728 if ( isset( $filter['post_type'] ) ) {
1729 $post_type = get_post_type_object( $filter['post_type'] );
1730 if ( ! ( (bool) $post_type ) )
1731 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1733 $post_type = get_post_type_object( 'post' );
1736 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1737 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1739 $query['post_type'] = $post_type->name;
1741 if ( isset( $filter['post_status'] ) )
1742 $query['post_status'] = $filter['post_status'];
1744 if ( isset( $filter['number'] ) )
1745 $query['numberposts'] = absint( $filter['number'] );
1747 if ( isset( $filter['offset'] ) )
1748 $query['offset'] = absint( $filter['offset'] );
1750 if ( isset( $filter['orderby'] ) ) {
1751 $query['orderby'] = $filter['orderby'];
1753 if ( isset( $filter['order'] ) )
1754 $query['order'] = $filter['order'];
1757 if ( isset( $filter['s'] ) ) {
1758 $query['s'] = $filter['s'];
1761 $posts_list = wp_get_recent_posts( $query );
1763 if ( ! $posts_list )
1766 // Holds all the posts data.
1769 foreach ( $posts_list as $post ) {
1770 if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1773 $struct[] = $this->_prepare_post( $post, $fields );
1780 * Create a new term.
1784 * @see wp_insert_term()
1786 * @param array $args {
1787 * Method arguments. Note: arguments must be ordered as documented.
1789 * @type int $blog_id Blog ID (unused).
1790 * @type string $username Username.
1791 * @type string $password Password.
1792 * @type array $content_struct Content struct for adding a new term. The struct must contain
1793 * the term 'name' and 'taxonomy'. Optional accepted values include
1794 * 'parent', 'description', and 'slug'.
1796 * @return int|IXR_Error The term ID on success, or an IXR_Error object on failure.
1798 public function wp_newTerm( $args ) {
1799 if ( ! $this->minimum_args( $args, 4 ) )
1800 return $this->error;
1802 $this->escape( $args );
1804 $username = $args[1];
1805 $password = $args[2];
1806 $content_struct = $args[3];
1808 if ( ! $user = $this->login( $username, $password ) )
1809 return $this->error;
1811 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1812 do_action( 'xmlrpc_call', 'wp.newTerm' );
1814 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1815 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1817 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1819 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1820 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1822 $taxonomy = (array) $taxonomy;
1824 // hold the data of the term
1825 $term_data = array();
1827 $term_data['name'] = trim( $content_struct['name'] );
1828 if ( empty( $term_data['name'] ) )
1829 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1831 if ( isset( $content_struct['parent'] ) ) {
1832 if ( ! $taxonomy['hierarchical'] )
1833 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1835 $parent_term_id = (int) $content_struct['parent'];
1836 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1838 if ( is_wp_error( $parent_term ) )
1839 return new IXR_Error( 500, $parent_term->get_error_message() );
1841 if ( ! $parent_term )
1842 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1844 $term_data['parent'] = $content_struct['parent'];
1847 if ( isset( $content_struct['description'] ) )
1848 $term_data['description'] = $content_struct['description'];
1850 if ( isset( $content_struct['slug'] ) )
1851 $term_data['slug'] = $content_struct['slug'];
1853 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1855 if ( is_wp_error( $term ) )
1856 return new IXR_Error( 500, $term->get_error_message() );
1859 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1861 return strval( $term['term_id'] );
1869 * @see wp_update_term()
1871 * @param array $args {
1872 * Method arguments. Note: arguments must be ordered as documented.
1874 * @type int $blog_id Blog ID (unused).
1875 * @type string $username Username.
1876 * @type string $password Password.
1877 * @type int $term_id Term ID.
1878 * @type array $content_struct Content struct for editing a term. The struct must contain the
1879 * term ''taxonomy'. Optional accepted values include 'name', 'parent',
1880 * 'description', and 'slug'.
1882 * @return true|IXR_Error True on success, IXR_Error instance on failure.
1884 public function wp_editTerm( $args ) {
1885 if ( ! $this->minimum_args( $args, 5 ) )
1886 return $this->error;
1888 $this->escape( $args );
1890 $username = $args[1];
1891 $password = $args[2];
1892 $term_id = (int) $args[3];
1893 $content_struct = $args[4];
1895 if ( ! $user = $this->login( $username, $password ) )
1896 return $this->error;
1898 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1899 do_action( 'xmlrpc_call', 'wp.editTerm' );
1901 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1902 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1904 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1906 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1907 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1909 $taxonomy = (array) $taxonomy;
1911 // hold the data of the term
1912 $term_data = array();
1914 $term = get_term( $term_id , $content_struct['taxonomy'] );
1916 if ( is_wp_error( $term ) )
1917 return new IXR_Error( 500, $term->get_error_message() );
1920 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1922 if ( isset( $content_struct['name'] ) ) {
1923 $term_data['name'] = trim( $content_struct['name'] );
1925 if ( empty( $term_data['name'] ) )
1926 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1929 if ( isset( $content_struct['parent'] ) ) {
1930 if ( ! $taxonomy['hierarchical'] )
1931 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1933 $parent_term_id = (int) $content_struct['parent'];
1934 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1936 if ( is_wp_error( $parent_term ) )
1937 return new IXR_Error( 500, $parent_term->get_error_message() );
1939 if ( ! $parent_term )
1940 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1942 $term_data['parent'] = $content_struct['parent'];
1945 if ( isset( $content_struct['description'] ) )
1946 $term_data['description'] = $content_struct['description'];
1948 if ( isset( $content_struct['slug'] ) )
1949 $term_data['slug'] = $content_struct['slug'];
1951 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1953 if ( is_wp_error( $term ) )
1954 return new IXR_Error( 500, $term->get_error_message() );
1957 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1967 * @see wp_delete_term()
1969 * @param array $args {
1970 * Method arguments. Note: arguments must be ordered as documented.
1972 * @type int $blog_id Blog ID (unused).
1973 * @type string $username Username.
1974 * @type string $password Password.
1975 * @type string $taxnomy_name Taxonomy name.
1976 * @type int $term_id Term ID.
1978 * @return bool|IXR_Error True on success, IXR_Error instance on failure.
1980 public function wp_deleteTerm( $args ) {
1981 if ( ! $this->minimum_args( $args, 5 ) )
1982 return $this->error;
1984 $this->escape( $args );
1986 $username = $args[1];
1987 $password = $args[2];
1988 $taxonomy = $args[3];
1989 $term_id = (int) $args[4];
1991 if ( ! $user = $this->login( $username, $password ) )
1992 return $this->error;
1994 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1995 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1997 if ( ! taxonomy_exists( $taxonomy ) )
1998 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2000 $taxonomy = get_taxonomy( $taxonomy );
2002 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
2003 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
2005 $term = get_term( $term_id, $taxonomy->name );
2007 if ( is_wp_error( $term ) )
2008 return new IXR_Error( 500, $term->get_error_message() );
2011 return new IXR_Error( 404, __( 'Invalid term ID' ) );
2013 $result = wp_delete_term( $term_id, $taxonomy->name );
2015 if ( is_wp_error( $result ) )
2016 return new IXR_Error( 500, $term->get_error_message() );
2019 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
2031 * @param array $args {
2032 * Method arguments. Note: arguments must be ordered as documented.
2034 * @type int $blog_id Blog ID (unused).
2035 * @type string $username Username.
2036 * @type string $password Password.
2037 * @type string $taxnomy Taxonomy name.
2038 * @type string $term_id Term ID.
2040 * @return array|IXR_Error IXR_Error on failure, array on success, containing:
2045 * - 'term_taxonomy_id'
2051 public function wp_getTerm( $args ) {
2052 if ( ! $this->minimum_args( $args, 5 ) )
2053 return $this->error;
2055 $this->escape( $args );
2057 $username = $args[1];
2058 $password = $args[2];
2059 $taxonomy = $args[3];
2060 $term_id = (int) $args[4];
2062 if ( ! $user = $this->login( $username, $password ) )
2063 return $this->error;
2065 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2066 do_action( 'xmlrpc_call', 'wp.getTerm' );
2068 if ( ! taxonomy_exists( $taxonomy ) )
2069 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2071 $taxonomy = get_taxonomy( $taxonomy );
2073 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2074 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2076 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
2078 if ( is_wp_error( $term ) )
2079 return new IXR_Error( 500, $term->get_error_message() );
2082 return new IXR_Error( 404, __( 'Invalid term ID' ) );
2084 return $this->_prepare_term( $term );
2088 * Retrieve all terms for a taxonomy.
2092 * The optional $filter parameter modifies the query used to retrieve terms.
2093 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
2097 * @param array $args {
2098 * Method arguments. Note: arguments must be ordered as documented.
2100 * @type int $blog_id Blog ID (unused).
2101 * @type string $username Username.
2102 * @type string $password Password.
2103 * @type string $taxnomy Taxonomy name.
2104 * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'number',
2105 * 'offset', 'orderby', 'order', 'hide_empty', and 'search'. Default empty array.
2107 * @return array|IXR_Error An associative array of terms data on success, IXR_Error instance otherwise.
2109 public function wp_getTerms( $args ) {
2110 if ( ! $this->minimum_args( $args, 4 ) )
2111 return $this->error;
2113 $this->escape( $args );
2115 $username = $args[1];
2116 $password = $args[2];
2117 $taxonomy = $args[3];
2118 $filter = isset( $args[4] ) ? $args[4] : array();
2120 if ( ! $user = $this->login( $username, $password ) )
2121 return $this->error;
2123 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2124 do_action( 'xmlrpc_call', 'wp.getTerms' );
2126 if ( ! taxonomy_exists( $taxonomy ) )
2127 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2129 $taxonomy = get_taxonomy( $taxonomy );
2131 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2132 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2136 if ( isset( $filter['number'] ) )
2137 $query['number'] = absint( $filter['number'] );
2139 if ( isset( $filter['offset'] ) )
2140 $query['offset'] = absint( $filter['offset'] );
2142 if ( isset( $filter['orderby'] ) ) {
2143 $query['orderby'] = $filter['orderby'];
2145 if ( isset( $filter['order'] ) )
2146 $query['order'] = $filter['order'];
2149 if ( isset( $filter['hide_empty'] ) )
2150 $query['hide_empty'] = $filter['hide_empty'];
2152 $query['get'] = 'all';
2154 if ( isset( $filter['search'] ) )
2155 $query['search'] = $filter['search'];
2157 $terms = get_terms( $taxonomy->name, $query );
2159 if ( is_wp_error( $terms ) )
2160 return new IXR_Error( 500, $terms->get_error_message() );
2164 foreach ( $terms as $term ) {
2165 $struct[] = $this->_prepare_term( $term );
2172 * Retrieve a taxonomy.
2176 * @see get_taxonomy()
2178 * @param array $args {
2179 * Method arguments. Note: arguments must be ordered as documented.
2181 * @type int $blog_id Blog ID (unused).
2182 * @type string $username Username.
2183 * @type string $password Password.
2184 * @type string $taxnomy Taxonomy name.
2185 * @type array $fields Optional. Array of taxonomy fields to limit to in the return.
2186 * Accepts 'labels', 'cap', 'menu', and 'object_type'.
2187 * Default empty array.
2189 * @return array|IXR_Error An array of taxonomy data on success, IXR_Error instance otherwise.
2191 public function wp_getTaxonomy( $args ) {
2192 if ( ! $this->minimum_args( $args, 4 ) )
2193 return $this->error;
2195 $this->escape( $args );
2197 $username = $args[1];
2198 $password = $args[2];
2199 $taxonomy = $args[3];
2201 if ( isset( $args[4] ) ) {
2205 * Filter the taxonomy query fields used by the given XML-RPC method.
2209 * @param array $fields An array of taxonomy fields to retrieve.
2210 * @param string $method The method name.
2212 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
2215 if ( ! $user = $this->login( $username, $password ) )
2216 return $this->error;
2218 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2219 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
2221 if ( ! taxonomy_exists( $taxonomy ) )
2222 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2224 $taxonomy = get_taxonomy( $taxonomy );
2226 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2227 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2229 return $this->_prepare_taxonomy( $taxonomy, $fields );
2233 * Retrieve all taxonomies.
2237 * @see get_taxonomies()
2239 * @param array $args {
2240 * Method arguments. Note: arguments must be ordered as documented.
2242 * @type int $blog_id Blog ID (unused).
2243 * @type string $username Username.
2244 * @type string $password Password.
2245 * @type array $filter Optional. An array of arguments for retrieving taxonomies.
2246 * @type array $fields Optional. The subset of taxonomy fields to return.
2248 * @return array|IXR_Error An associative array of taxonomy data with returned fields determined
2249 * by `$fields`, or an IXR_Error instance on failure.
2251 public function wp_getTaxonomies( $args ) {
2252 if ( ! $this->minimum_args( $args, 3 ) )
2253 return $this->error;
2255 $this->escape( $args );
2257 $username = $args[1];
2258 $password = $args[2];
2259 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2261 if ( isset( $args[4] ) ) {
2264 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2265 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
2268 if ( ! $user = $this->login( $username, $password ) )
2269 return $this->error;
2271 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2272 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
2274 $taxonomies = get_taxonomies( $filter, 'objects' );
2276 // holds all the taxonomy data
2279 foreach ( $taxonomies as $taxonomy ) {
2280 // capability check for post_types
2281 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2284 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
2293 * The optional $fields parameter specifies what fields will be included
2294 * in the response array. This should be a list of field names. 'user_id' will
2295 * always be included in the response regardless of the value of $fields.
2297 * Instead of, or in addition to, individual field names, conceptual group
2298 * names can be used to specify multiple fields. The available conceptual
2299 * groups are 'basic' and 'all'.
2301 * @uses get_userdata()
2303 * @param array $args {
2304 * Method arguments. Note: arguments must be ordered as documented.
2306 * @type int $blog_id (unused)
2307 * @type string $username
2308 * @type string $password
2309 * @type int $user_id
2310 * @type array $fields (optional)
2312 * @return array|IXR_Error Array contains (based on $fields parameter):
2326 public function wp_getUser( $args ) {
2327 if ( ! $this->minimum_args( $args, 4 ) )
2328 return $this->error;
2330 $this->escape( $args );
2332 $username = $args[1];
2333 $password = $args[2];
2334 $user_id = (int) $args[3];
2336 if ( isset( $args[4] ) ) {
2340 * Filter the default user query fields used by the given XML-RPC method.
2344 * @param array $fields User query fields for given method. Default 'all'.
2345 * @param string $method The method name.
2347 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2350 if ( ! $user = $this->login( $username, $password ) )
2351 return $this->error;
2353 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2354 do_action( 'xmlrpc_call', 'wp.getUser' );
2356 if ( ! current_user_can( 'edit_user', $user_id ) )
2357 return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2359 $user_data = get_userdata( $user_id );
2362 return new IXR_Error( 404, __( 'Invalid user ID.' ) );
2364 return $this->_prepare_user( $user_data, $fields );
2370 * The optional $filter parameter modifies the query used to retrieve users.
2371 * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2372 * 'who', 'orderby', and 'order'.
2374 * The optional $fields parameter specifies what fields will be included
2375 * in the response array.
2378 * @see wp_getUser() for more on $fields and return values
2380 * @param array $args {
2381 * Method arguments. Note: arguments must be ordered as documented.
2383 * @type int $blog_id (unused)
2384 * @type string $username
2385 * @type string $password
2386 * @type array $filter (optional)
2387 * @type array $fields (optional)
2389 * @return array|IXR_Error users data
2391 public function wp_getUsers( $args ) {
2392 if ( ! $this->minimum_args( $args, 3 ) )
2393 return $this->error;
2395 $this->escape( $args );
2397 $username = $args[1];
2398 $password = $args[2];
2399 $filter = isset( $args[3] ) ? $args[3] : array();
2401 if ( isset( $args[4] ) ) {
2404 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2405 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2408 if ( ! $user = $this->login( $username, $password ) )
2409 return $this->error;
2411 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2412 do_action( 'xmlrpc_call', 'wp.getUsers' );
2414 if ( ! current_user_can( 'list_users' ) )
2415 return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
2417 $query = array( 'fields' => 'all_with_meta' );
2419 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2420 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2422 if ( isset( $filter['orderby'] ) ) {
2423 $query['orderby'] = $filter['orderby'];
2425 if ( isset( $filter['order'] ) )
2426 $query['order'] = $filter['order'];
2429 if ( isset( $filter['role'] ) ) {
2430 if ( get_role( $filter['role'] ) === null )
2431 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2433 $query['role'] = $filter['role'];
2436 if ( isset( $filter['who'] ) ) {
2437 $query['who'] = $filter['who'];
2440 $users = get_users( $query );
2443 foreach ( $users as $user_data ) {
2444 if ( current_user_can( 'edit_user', $user_data->ID ) )
2445 $_users[] = $this->_prepare_user( $user_data, $fields );
2451 * Retrieve information about the requesting user.
2453 * @uses get_userdata()
2455 * @param array $args {
2456 * Method arguments. Note: arguments must be ordered as documented.
2458 * @type int $blog_id (unused)
2459 * @type string $username
2460 * @type string $password
2461 * @type array $fields (optional)
2463 * @return array|IXR_Error (@see wp_getUser)
2465 public function wp_getProfile( $args ) {
2466 if ( ! $this->minimum_args( $args, 3 ) )
2467 return $this->error;
2469 $this->escape( $args );
2471 $username = $args[1];
2472 $password = $args[2];
2474 if ( isset( $args[3] ) ) {
2477 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2478 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2481 if ( ! $user = $this->login( $username, $password ) )
2482 return $this->error;
2484 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2485 do_action( 'xmlrpc_call', 'wp.getProfile' );
2487 if ( ! current_user_can( 'edit_user', $user->ID ) )
2488 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2490 $user_data = get_userdata( $user->ID );
2492 return $this->_prepare_user( $user_data, $fields );
2496 * Edit user's profile.
2498 * @uses wp_update_user()
2500 * @param array $args {
2501 * Method arguments. Note: arguments must be ordered as documented.
2503 * @type int $blog_id (unused)
2504 * @type string $username
2505 * @type string $password
2506 * @type array $content_struct It can optionally contain:
2515 * @return true|IXR_Error True, on success.
2517 public function wp_editProfile( $args ) {
2518 if ( ! $this->minimum_args( $args, 4 ) )
2519 return $this->error;
2521 $this->escape( $args );
2523 $username = $args[1];
2524 $password = $args[2];
2525 $content_struct = $args[3];
2527 if ( ! $user = $this->login( $username, $password ) )
2528 return $this->error;
2530 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2531 do_action( 'xmlrpc_call', 'wp.editProfile' );
2533 if ( ! current_user_can( 'edit_user', $user->ID ) )
2534 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2536 // holds data of the user
2537 $user_data = array();
2538 $user_data['ID'] = $user->ID;
2540 // only set the user details if it was given
2541 if ( isset( $content_struct['first_name'] ) )
2542 $user_data['first_name'] = $content_struct['first_name'];
2544 if ( isset( $content_struct['last_name'] ) )
2545 $user_data['last_name'] = $content_struct['last_name'];
2547 if ( isset( $content_struct['url'] ) )
2548 $user_data['user_url'] = $content_struct['url'];
2550 if ( isset( $content_struct['display_name'] ) )
2551 $user_data['display_name'] = $content_struct['display_name'];
2553 if ( isset( $content_struct['nickname'] ) )
2554 $user_data['nickname'] = $content_struct['nickname'];
2556 if ( isset( $content_struct['nicename'] ) )
2557 $user_data['user_nicename'] = $content_struct['nicename'];
2559 if ( isset( $content_struct['bio'] ) )
2560 $user_data['description'] = $content_struct['bio'];
2562 $result = wp_update_user( $user_data );
2564 if ( is_wp_error( $result ) )
2565 return new IXR_Error( 500, $result->get_error_message() );
2568 return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2578 * @param array $args {
2579 * Method arguments. Note: arguments must be ordered as documented.
2581 * @type int $blog_id (unused)
2582 * @type int $page_id
2583 * @type string $username
2584 * @type string $password
2586 * @return array|IXR_Error
2588 public function wp_getPage( $args ) {
2589 $this->escape( $args );
2591 $page_id = (int) $args[1];
2592 $username = $args[2];
2593 $password = $args[3];
2595 if ( !$user = $this->login($username, $password) ) {
2596 return $this->error;
2599 $page = get_post($page_id);
2601 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2603 if ( !current_user_can( 'edit_page', $page_id ) )
2604 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2606 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2607 do_action( 'xmlrpc_call', 'wp.getPage' );
2609 // If we found the page then format the data.
2610 if ( $page->ID && ($page->post_type == 'page') ) {
2611 return $this->_prepare_page( $page );
2613 // If the page doesn't exist indicate that.
2615 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2624 * @param array $args {
2625 * Method arguments. Note: arguments must be ordered as documented.
2627 * @type int $blog_id (unused)
2628 * @type string $username
2629 * @type string $password
2630 * @type int $num_pages
2632 * @return array|IXR_Error
2634 public function wp_getPages( $args ) {
2635 $this->escape( $args );
2637 $username = $args[1];
2638 $password = $args[2];
2639 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
2641 if ( !$user = $this->login($username, $password) )
2642 return $this->error;
2644 if ( !current_user_can( 'edit_pages' ) )
2645 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2647 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2648 do_action( 'xmlrpc_call', 'wp.getPages' );
2650 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2651 $num_pages = count($pages);
2653 // If we have pages, put together their info.
2654 if ( $num_pages >= 1 ) {
2655 $pages_struct = array();
2657 foreach ($pages as $page) {
2658 if ( current_user_can( 'edit_page', $page->ID ) )
2659 $pages_struct[] = $this->_prepare_page( $page );
2662 return $pages_struct;
2673 * @see wp_xmlrpc_server::mw_newPost()
2675 * @param array $args {
2676 * Method arguments. Note: arguments must be ordered as documented.
2678 * @type int $blog_id (unused)
2679 * @type string $username
2680 * @type string $password
2681 * @type array $content_struct
2683 * @return int|IXR_Error
2685 public function wp_newPage( $args ) {
2686 // Items not escaped here will be escaped in newPost.
2687 $username = $this->escape( $args[1] );
2688 $password = $this->escape( $args[2] );
2690 if ( !$user = $this->login($username, $password) )
2691 return $this->error;
2693 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2694 do_action( 'xmlrpc_call', 'wp.newPage' );
2696 // Mark this as content for a page.
2697 $args[3]["post_type"] = 'page';
2699 // Let mw_newPost do all of the heavy lifting.
2700 return $this->mw_newPost( $args );
2708 * @param array $args {
2709 * Method arguments. Note: arguments must be ordered as documented.
2711 * @type int $blog_id (unused)
2712 * @type string $username
2713 * @type string $password
2714 * @type int $page_id
2716 * @return true|IXR_Error True, if success.
2718 public function wp_deletePage( $args ) {
2719 $this->escape( $args );
2721 $username = $args[1];
2722 $password = $args[2];
2723 $page_id = (int) $args[3];
2725 if ( !$user = $this->login($username, $password) )
2726 return $this->error;
2728 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2729 do_action( 'xmlrpc_call', 'wp.deletePage' );
2731 // Get the current page based on the page_id and
2732 // make sure it is a page and not a post.
2733 $actual_page = get_post($page_id, ARRAY_A);
2734 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2735 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2737 // Make sure the user can delete pages.
2738 if ( !current_user_can('delete_page', $page_id) )
2739 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete this page.' ) );
2741 // Attempt to delete the page.
2742 $result = wp_delete_post($page_id);
2744 return new IXR_Error( 500, __( 'Failed to delete the page.' ) );
2747 * Fires after a page has been successfully deleted via XML-RPC.
2751 * @param int $page_id ID of the deleted page.
2752 * @param array $args An array of arguments to delete the page.
2754 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2764 * @param array $args {
2765 * Method arguments. Note: arguments must be ordered as documented.
2767 * @type int $blog_id (unused)
2768 * @type int $page_id
2769 * @type string $username
2770 * @type string $password
2771 * @type string $content
2772 * @type string $publish
2774 * @return array|IXR_Error
2776 public function wp_editPage( $args ) {
2777 // Items will be escaped in mw_editPost.
2778 $page_id = (int) $args[1];
2779 $username = $args[2];
2780 $password = $args[3];
2781 $content = $args[4];
2782 $publish = $args[5];
2784 $escaped_username = $this->escape( $username );
2785 $escaped_password = $this->escape( $password );
2787 if ( !$user = $this->login( $escaped_username, $escaped_password ) ) {
2788 return $this->error;
2791 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2792 do_action( 'xmlrpc_call', 'wp.editPage' );
2794 // Get the page data and make sure it is a page.
2795 $actual_page = get_post($page_id, ARRAY_A);
2796 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2797 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2799 // Make sure the user is allowed to edit pages.
2800 if ( !current_user_can('edit_page', $page_id) )
2801 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this page.' ) );
2803 // Mark this as content for a page.
2804 $content['post_type'] = 'page';
2806 // Arrange args in the way mw_editPost understands.
2815 // Let mw_editPost do all of the heavy lifting.
2816 return $this->mw_editPost( $args );
2820 * Retrieve page list.
2824 * @global wpdb $wpdb
2826 * @param array $args {
2827 * Method arguments. Note: arguments must be ordered as documented.
2829 * @type int $blog_id (unused)
2830 * @type string $username
2831 * @type string $password
2833 * @return array|IXR_Error
2835 public function wp_getPageList( $args ) {
2838 $this->escape( $args );
2840 $username = $args[1];
2841 $password = $args[2];
2843 if ( !$user = $this->login($username, $password) )
2844 return $this->error;
2846 if ( !current_user_can( 'edit_pages' ) )
2847 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2849 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2850 do_action( 'xmlrpc_call', 'wp.getPageList' );
2852 // Get list of pages ids and titles
2853 $page_list = $wpdb->get_results("
2855 post_title page_title,
2856 post_parent page_parent_id,
2861 WHERE post_type = 'page'
2865 // The date needs to be formatted properly.
2866 $num_pages = count($page_list);
2867 for ( $i = 0; $i < $num_pages; $i++ ) {
2868 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2869 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2871 unset($page_list[$i]->post_date_gmt);
2872 unset($page_list[$i]->post_date);
2873 unset($page_list[$i]->post_status);
2880 * Retrieve authors list.
2884 * @param array $args {
2885 * Method arguments. Note: arguments must be ordered as documented.
2887 * @type int $blog_id (unused)
2888 * @type string $username
2889 * @type string $password
2891 * @return array|IXR_Error
2893 public function wp_getAuthors( $args ) {
2894 $this->escape( $args );
2896 $username = $args[1];
2897 $password = $args[2];
2899 if ( !$user = $this->login($username, $password) )
2900 return $this->error;
2902 if ( !current_user_can('edit_posts') )
2903 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
2905 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2906 do_action( 'xmlrpc_call', 'wp.getAuthors' );
2909 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2911 'user_id' => $user->ID,
2912 'user_login' => $user->user_login,
2913 'display_name' => $user->display_name
2921 * Get list of all tags
2925 * @param array $args {
2926 * Method arguments. Note: arguments must be ordered as documented.
2928 * @type int $blog_id (unused)
2929 * @type string $username
2930 * @type string $password
2932 * @return array|IXR_Error
2934 public function wp_getTags( $args ) {
2935 $this->escape( $args );
2937 $username = $args[1];
2938 $password = $args[2];
2940 if ( !$user = $this->login($username, $password) )
2941 return $this->error;
2943 if ( !current_user_can( 'edit_posts' ) )
2944 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2946 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2947 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2951 if ( $all_tags = get_tags() ) {
2952 foreach( (array) $all_tags as $tag ) {
2954 $struct['tag_id'] = $tag->term_id;
2955 $struct['name'] = $tag->name;
2956 $struct['count'] = $tag->count;
2957 $struct['slug'] = $tag->slug;
2958 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2959 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
2969 * Create new category.
2973 * @param array $args {
2974 * Method arguments. Note: arguments must be ordered as documented.
2976 * @type int $blog_id (unused)
2977 * @type string $username
2978 * @type string $password
2979 * @type array $category
2981 * @return int|IXR_Error Category ID.
2983 public function wp_newCategory( $args ) {
2984 $this->escape( $args );
2986 $username = $args[1];
2987 $password = $args[2];
2988 $category = $args[3];
2990 if ( !$user = $this->login($username, $password) )
2991 return $this->error;
2993 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2994 do_action( 'xmlrpc_call', 'wp.newCategory' );
2996 // Make sure the user is allowed to add a category.
2997 if ( !current_user_can('manage_categories') )
2998 return new IXR_Error(401, __('Sorry, you do not have the right to add a category.'));
3000 // If no slug was provided make it empty so that
3001 // WordPress will generate one.
3002 if ( empty($category['slug']) )
3003 $category['slug'] = '';
3005 // If no parent_id was provided make it empty
3006 // so that it will be a top level page (no parent).
3007 if ( !isset($category['parent_id']) )
3008 $category['parent_id'] = '';
3010 // If no description was provided make it empty.
3011 if ( empty($category["description"]) )
3012 $category["description"] = "";
3014 $new_category = array(
3015 'cat_name' => $category['name'],
3016 'category_nicename' => $category['slug'],
3017 'category_parent' => $category['parent_id'],
3018 'category_description' => $category['description']
3021 $cat_id = wp_insert_category($new_category, true);
3022 if ( is_wp_error( $cat_id ) ) {
3023 if ( 'term_exists' == $cat_id->get_error_code() )
3024 return (int) $cat_id->get_error_data();
3026 return new IXR_Error(500, __('Sorry, the new category failed.'));
3027 } elseif ( ! $cat_id ) {
3028 return new IXR_Error(500, __('Sorry, the new category failed.'));
3032 * Fires after a new category has been successfully created via XML-RPC.
3036 * @param int $cat_id ID of the new category.
3037 * @param array $args An array of new category arguments.
3039 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
3049 * @param array $args {
3050 * Method arguments. Note: arguments must be ordered as documented.
3052 * @type int $blog_id (unused)
3053 * @type string $username
3054 * @type string $password
3055 * @type int $category_id
3057 * @return bool|IXR_Error See {@link wp_delete_term()} for return info.
3059 public function wp_deleteCategory( $args ) {
3060 $this->escape( $args );
3062 $username = $args[1];
3063 $password = $args[2];
3064 $category_id = (int) $args[3];
3066 if ( !$user = $this->login($username, $password) )
3067 return $this->error;
3069 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3070 do_action( 'xmlrpc_call', 'wp.deleteCategory' );
3072 if ( !current_user_can('manage_categories') )
3073 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
3075 $status = wp_delete_term( $category_id, 'category' );
3077 if ( true == $status ) {
3079 * Fires after a category has been successfully deleted via XML-RPC.
3083 * @param int $category_id ID of the deleted category.
3084 * @param array $args An array of arguments to delete the category.
3086 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
3093 * Retrieve category list.
3097 * @param array $args {
3098 * Method arguments. Note: arguments must be ordered as documented.
3100 * @type int $blog_id (unused)
3101 * @type string $username
3102 * @type string $password
3103 * @type array $category
3104 * @type int $max_results
3106 * @return array|IXR_Error
3108 public function wp_suggestCategories( $args ) {
3109 $this->escape( $args );
3111 $username = $args[1];
3112 $password = $args[2];
3113 $category = $args[3];
3114 $max_results = (int) $args[4];
3116 if ( !$user = $this->login($username, $password) )
3117 return $this->error;
3119 if ( !current_user_can( 'edit_posts' ) )
3120 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
3122 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3123 do_action( 'xmlrpc_call', 'wp.suggestCategories' );
3125 $category_suggestions = array();
3126 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
3127 foreach ( (array) get_categories($args) as $cat ) {
3128 $category_suggestions[] = array(
3129 'category_id' => $cat->term_id,
3130 'category_name' => $cat->name
3134 return $category_suggestions;
3142 * @param array $args {
3143 * Method arguments. Note: arguments must be ordered as documented.
3145 * @type int $blog_id (unused)
3146 * @type string $username
3147 * @type string $password
3148 * @type int $comment_id
3150 * @return array|IXR_Error
3152 public function wp_getComment($args) {
3153 $this->escape($args);
3155 $username = $args[1];
3156 $password = $args[2];
3157 $comment_id = (int) $args[3];
3159 if ( !$user = $this->login($username, $password) )
3160 return $this->error;
3162 if ( !current_user_can( 'moderate_comments' ) )
3163 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3165 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3166 do_action( 'xmlrpc_call', 'wp.getComment' );
3168 if ( ! $comment = get_comment($comment_id) )
3169 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3171 return $this->_prepare_comment( $comment );
3175 * Retrieve comments.
3177 * Besides the common blog_id (unused), username, and password arguments, it takes a filter
3178 * array as last argument.
3180 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
3182 * The defaults are as follows:
3183 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
3184 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
3185 * - 'number' - Default is 10. Total number of media items to retrieve.
3186 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
3190 * @param array $args {
3191 * Method arguments. Note: arguments must be ordered as documented.
3193 * @type int $blog_id (unused)
3194 * @type string $username
3195 * @type string $password
3196 * @type array $struct
3198 * @return array|IXR_Error Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
3200 public function wp_getComments( $args ) {
3201 $this->escape( $args );
3203 $username = $args[1];
3204 $password = $args[2];
3205 $struct = isset( $args[3] ) ? $args[3] : array();
3207 if ( !$user = $this->login($username, $password) )
3208 return $this->error;
3210 if ( !current_user_can( 'moderate_comments' ) )
3211 return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
3213 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3214 do_action( 'xmlrpc_call', 'wp.getComments' );
3216 if ( isset($struct['status']) )
3217 $status = $struct['status'];
3222 if ( isset($struct['post_id']) )
3223 $post_id = absint($struct['post_id']);
3226 if ( isset($struct['offset']) )
3227 $offset = absint($struct['offset']);
3230 if ( isset($struct['number']) )
3231 $number = absint($struct['number']);
3233 $comments = get_comments( array( 'status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
3235 $comments_struct = array();
3236 if ( is_array( $comments ) ) {
3237 foreach ( $comments as $comment ) {
3238 $comments_struct[] = $this->_prepare_comment( $comment );
3242 return $comments_struct;
3248 * By default, the comment will be moved to the trash instead of deleted.
3249 * See {@link wp_delete_comment()} for more information on
3254 * @param array $args {
3255 * Method arguments. Note: arguments must be ordered as documented.
3257 * @type int $blog_id (unused)
3258 * @type string $username
3259 * @type string $password
3260 * @type int $comment_ID
3262 * @return bool|IXR_Error {@link wp_delete_comment()}
3264 public function wp_deleteComment($args) {
3265 $this->escape($args);
3267 $username = $args[1];
3268 $password = $args[2];
3269 $comment_ID = (int) $args[3];
3271 if ( !$user = $this->login($username, $password) )
3272 return $this->error;
3274 if ( !current_user_can( 'moderate_comments' ) )
3275 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3277 if ( ! get_comment($comment_ID) )
3278 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3280 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3281 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3283 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3284 do_action( 'xmlrpc_call', 'wp.deleteComment' );
3286 $status = wp_delete_comment( $comment_ID );
3290 * Fires after a comment has been successfully deleted via XML-RPC.
3294 * @param int $comment_ID ID of the deleted comment.
3295 * @param array $args An array of arguments to delete the comment.
3297 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
3306 * Besides the common blog_id (unused), username, and password arguments, it takes a
3307 * comment_id integer and a content_struct array as last argument.
3309 * The allowed keys in the content_struct array are:
3314 * - 'date_created_gmt'
3315 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See get_comment_statuses() for more details
3319 * @param array $args {
3320 * Method arguments. Note: arguments must be ordered as documented.
3322 * @type int $blog_id (unused)
3323 * @type string $username
3324 * @type string $password
3325 * @type int $comment_ID
3326 * @type array $content_struct
3328 * @return true|IXR_Error True, on success.
3330 public function wp_editComment( $args ) {
3331 $this->escape( $args );
3333 $username = $args[1];
3334 $password = $args[2];
3335 $comment_ID = (int) $args[3];
3336 $content_struct = $args[4];
3338 if ( !$user = $this->login($username, $password) )
3339 return $this->error;
3341 if ( !current_user_can( 'moderate_comments' ) )
3342 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3344 if ( ! get_comment($comment_ID) )
3345 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3347 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3348 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3350 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3351 do_action( 'xmlrpc_call', 'wp.editComment' );
3353 if ( isset($content_struct['status']) ) {
3354 $statuses = get_comment_statuses();
3355 $statuses = array_keys($statuses);
3357 if ( ! in_array($content_struct['status'], $statuses) )
3358 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
3359 $comment_approved = $content_struct['status'];
3362 // Do some timestamp voodoo
3363 if ( !empty( $content_struct['date_created_gmt'] ) ) {
3364 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
3365 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
3366 $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
3367 $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
3370 if ( isset($content_struct['content']) )
3371 $comment_content = $content_struct['content'];
3373 if ( isset($content_struct['author']) )
3374 $comment_author = $content_struct['author'];
3376 if ( isset($content_struct['author_url']) )
3377 $comment_author_url = $content_struct['author_url'];
3379 if ( isset($content_struct['author_email']) )
3380 $comment_author_email = $content_struct['author_email'];
3382 // We've got all the data -- post it:
3383 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
3385 $result = wp_update_comment($comment);
3386 if ( is_wp_error( $result ) )
3387 return new IXR_Error(500, $result->get_error_message());
3390 return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
3393 * Fires after a comment has been successfully updated via XML-RPC.
3397 * @param int $comment_ID ID of the updated comment.
3398 * @param array $args An array of arguments to update the comment.
3400 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
3406 * Create new comment.
3410 * @param array $args {
3411 * Method arguments. Note: arguments must be ordered as documented.
3413 * @type int $blog_id (unused)
3414 * @type string $username
3415 * @type string $password
3416 * @type string|int $post
3417 * @type array $content_struct
3419 * @return int|IXR_Error {@link wp_new_comment()}
3421 public function wp_newComment($args) {
3422 $this->escape($args);
3424 $username = $args[1];
3425 $password = $args[2];
3427 $content_struct = $args[4];
3430 * Filter whether to allow anonymous comments over XML-RPC.
3434 * @param bool $allow Whether to allow anonymous commenting via XML-RPC.
3437 $allow_anon = apply_filters( 'xmlrpc_allow_anonymous_comments', false );
3439 $user = $this->login($username, $password);
3443 if ( $allow_anon && get_option('comment_registration') ) {
3444 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
3445 } elseif ( ! $allow_anon ) {
3446 return $this->error;
3452 if ( is_numeric($post) )
3453 $post_id = absint($post);
3455 $post_id = url_to_postid($post);
3458 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3460 if ( ! get_post($post_id) )
3461 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3464 $comment['comment_post_ID'] = $post_id;
3467 $comment['comment_author'] = $this->escape( $user->display_name );
3468 $comment['comment_author_email'] = $this->escape( $user->user_email );
3469 $comment['comment_author_url'] = $this->escape( $user->user_url );
3470 $comment['user_ID'] = $user->ID;
3472 $comment['comment_author'] = '';
3473 if ( isset($content_struct['author']) )
3474 $comment['comment_author'] = $content_struct['author'];
3476 $comment['comment_author_email'] = '';
3477 if ( isset($content_struct['author_email']) )
3478 $comment['comment_author_email'] = $content_struct['author_email'];
3480 $comment['comment_author_url'] = '';
3481 if ( isset($content_struct['author_url']) )
3482 $comment['comment_author_url'] = $content_struct['author_url'];
3484 $comment['user_ID'] = 0;
3486 if ( get_option('require_name_email') ) {
3487 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
3488 return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
3489 elseif ( !is_email($comment['comment_author_email']) )
3490 return new IXR_Error( 403, __( 'A valid email address is required' ) );
3494 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
3496 $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
3498 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3499 do_action( 'xmlrpc_call', 'wp.newComment' );
3501 $comment_ID = wp_new_comment( $comment );
3504 * Fires after a new comment has been successfully created via XML-RPC.
3508 * @param int $comment_ID ID of the new comment.
3509 * @param array $args An array of new comment arguments.
3511 do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
3517 * Retrieve all of the comment status.
3521 * @param array $args {
3522 * Method arguments. Note: arguments must be ordered as documented.
3524 * @type int $blog_id (unused)
3525 * @type string $username
3526 * @type string $password
3528 * @return array|IXR_Error
3530 public function wp_getCommentStatusList($args) {
3531 $this->escape( $args );
3533 $username = $args[1];
3534 $password = $args[2];
3536 if ( !$user = $this->login($username, $password) )
3537 return $this->error;
3539 if ( !current_user_can( 'moderate_comments' ) )
3540 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3542 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3543 do_action( 'xmlrpc_call', 'wp.getCommentStatusList' );
3545 return get_comment_statuses();
3549 * Retrieve comment count.
3553 * @param array $args {
3554 * Method arguments. Note: arguments must be ordered as documented.
3556 * @type int $blog_id (unused)
3557 * @type string $username
3558 * @type string $password
3559 * @type int $post_id
3561 * @return array|IXR_Error
3563 public function wp_getCommentCount( $args ) {
3564 $this->escape( $args );
3566 $username = $args[1];
3567 $password = $args[2];
3568 $post_id = (int) $args[3];
3570 if ( !$user = $this->login($username, $password) )
3571 return $this->error;
3573 if ( !current_user_can( 'edit_posts' ) )
3574 return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
3576 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3577 do_action( 'xmlrpc_call', 'wp.getCommentCount' );
3579 $count = wp_count_comments( $post_id );
3581 'approved' => $count->approved,
3582 'awaiting_moderation' => $count->moderated,
3583 'spam' => $count->spam,
3584 'total_comments' => $count->total_comments
3589 * Retrieve post statuses.
3593 * @param array $args {
3594 * Method arguments. Note: arguments must be ordered as documented.
3596 * @type int $blog_id (unused)
3597 * @type string $username
3598 * @type string $password
3600 * @return array|IXR_Error
3602 public function wp_getPostStatusList( $args ) {
3603 $this->escape( $args );
3605 $username = $args[1];
3606 $password = $args[2];
3608 if ( !$user = $this->login($username, $password) )
3609 return $this->error;
3611 if ( !current_user_can( 'edit_posts' ) )
3612 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3614 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3615 do_action( 'xmlrpc_call', 'wp.getPostStatusList' );
3617 return get_post_statuses();
3621 * Retrieve page statuses.
3625 * @param array $args {
3626 * Method arguments. Note: arguments must be ordered as documented.
3628 * @type int $blog_id (unused)
3629 * @type string $username
3630 * @type string $password
3632 * @return array|IXR_Error
3634 public function wp_getPageStatusList( $args ) {
3635 $this->escape( $args );
3637 $username = $args[1];
3638 $password = $args[2];
3640 if ( !$user = $this->login($username, $password) )
3641 return $this->error;
3643 if ( !current_user_can( 'edit_pages' ) )
3644 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3646 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3647 do_action( 'xmlrpc_call', 'wp.getPageStatusList' );
3649 return get_page_statuses();
3653 * Retrieve page templates.
3657 * @param array $args {
3658 * Method arguments. Note: arguments must be ordered as documented.
3660 * @type int $blog_id (unused)
3661 * @type string $username
3662 * @type string $password
3664 * @return array|IXR_Error
3666 public function wp_getPageTemplates( $args ) {
3667 $this->escape( $args );
3669 $username = $args[1];
3670 $password = $args[2];
3672 if ( !$user = $this->login($username, $password) )
3673 return $this->error;
3675 if ( !current_user_can( 'edit_pages' ) )
3676 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3678 $templates = get_page_templates();
3679 $templates['Default'] = 'default';
3685 * Retrieve blog options.
3689 * @param array $args {
3690 * Method arguments. Note: arguments must be ordered as documented.
3692 * @type int $blog_id (unused)
3693 * @type string $username
3694 * @type string $password
3695 * @type array $options
3697 * @return array|IXR_Error
3699 public function wp_getOptions( $args ) {
3700 $this->escape( $args );
3702 $username = $args[1];
3703 $password = $args[2];
3704 $options = isset( $args[3] ) ? (array) $args[3] : array();
3706 if ( !$user = $this->login($username, $password) )
3707 return $this->error;
3709 // If no specific options where asked for, return all of them
3710 if ( count( $options ) == 0 )
3711 $options = array_keys($this->blog_options);
3713 return $this->_getOptions($options);
3717 * Retrieve blog options value from list.
3721 * @param array $options Options to retrieve.
3724 public function _getOptions($options) {
3726 $can_manage = current_user_can( 'manage_options' );
3727 foreach ( $options as $option ) {
3728 if ( array_key_exists( $option, $this->blog_options ) ) {
3729 $data[$option] = $this->blog_options[$option];
3730 //Is the value static or dynamic?
3731 if ( isset( $data[$option]['option'] ) ) {
3732 $data[$option]['value'] = get_option( $data[$option]['option'] );
3733 unset($data[$option]['option']);
3736 if ( ! $can_manage )
3737 $data[$option]['readonly'] = true;
3745 * Update blog options.
3749 * @param array $args {
3750 * Method arguments. Note: arguments must be ordered as documented.
3752 * @type int $blog_id (unused)
3753 * @type string $username
3754 * @type string $password
3755 * @type array $options
3757 * @return array|IXR_Error
3759 public function wp_setOptions( $args ) {
3760 $this->escape( $args );
3762 $username = $args[1];
3763 $password = $args[2];
3764 $options = (array) $args[3];
3766 if ( !$user = $this->login($username, $password) )
3767 return $this->error;
3769 if ( !current_user_can( 'manage_options' ) )
3770 return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
3772 $option_names = array();
3773 foreach ( $options as $o_name => $o_value ) {
3774 $option_names[] = $o_name;
3775 if ( !array_key_exists( $o_name, $this->blog_options ) )
3778 if ( $this->blog_options[$o_name]['readonly'] == true )
3781 update_option( $this->blog_options[$o_name]['option'], wp_unslash( $o_value ) );
3784 //Now return the updated values
3785 return $this->_getOptions($option_names);
3789 * Retrieve a media item by ID
3793 * @param array $args {
3794 * Method arguments. Note: arguments must be ordered as documented.
3796 * @type int $blog_id (unused)
3797 * @type string $username
3798 * @type string $password
3799 * @type int $attachment_id
3801 * @return array|IXR_Error Associative array contains:
3802 * - 'date_created_gmt'
3811 public function wp_getMediaItem( $args ) {
3812 $this->escape( $args );
3814 $username = $args[1];
3815 $password = $args[2];
3816 $attachment_id = (int) $args[3];
3818 if ( !$user = $this->login($username, $password) )
3819 return $this->error;
3821 if ( !current_user_can( 'upload_files' ) )
3822 return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
3824 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3825 do_action( 'xmlrpc_call', 'wp.getMediaItem' );
3827 if ( ! $attachment = get_post($attachment_id) )
3828 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
3830 return $this->_prepare_media_item( $attachment );
3834 * Retrieves a collection of media library items (or attachments)
3836 * Besides the common blog_id (unused), username, and password arguments, it takes a filter
3837 * array as last argument.
3839 * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
3841 * The defaults are as follows:
3842 * - 'number' - Default is 5. Total number of media items to retrieve.
3843 * - 'offset' - Default is 0. See WP_Query::query() for more.
3844 * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
3845 * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
3849 * @param array $args {
3850 * Method arguments. Note: arguments must be ordered as documented.
3852 * @type int $blog_id (unused)
3853 * @type string $username
3854 * @type string $password
3855 * @type array $struct
3857 * @return array|IXR_Error Contains a collection of media items. See wp_xmlrpc_server::wp_getMediaItem() for a description of each item contents
3859 public function wp_getMediaLibrary($args) {
3860 $this->escape($args);
3862 $username = $args[1];
3863 $password = $args[2];
3864 $struct = isset( $args[3] ) ? $args[3] : array() ;
3866 if ( !$user = $this->login($username, $password) )
3867 return $this->error;
3869 if ( !current_user_can( 'upload_files' ) )
3870 return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
3872 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3873 do_action( 'xmlrpc_call', 'wp.getMediaLibrary' );
3875 $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
3876 $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
3877 $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
3878 $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
3880 $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
3882 $attachments_struct = array();
3884 foreach ($attachments as $attachment )
3885 $attachments_struct[] = $this->_prepare_media_item( $attachment );
3887 return $attachments_struct;
3891 * Retrieves a list of post formats used by the site.
3895 * @param array $args {
3896 * Method arguments. Note: arguments must be ordered as documented.
3898 * @type int $blog_id (unused)
3899 * @type string $username
3900 * @type string $password
3902 * @return array|IXR_Error List of post formats, otherwise IXR_Error object.
3904 public function wp_getPostFormats( $args ) {
3905 $this->escape( $args );
3907 $username = $args[1];
3908 $password = $args[2];
3910 if ( !$user = $this->login( $username, $password ) )
3911 return $this->error;
3913 if ( !current_user_can( 'edit_posts' ) )
3914 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3916 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3917 do_action( 'xmlrpc_call', 'wp.getPostFormats' );
3919 $formats = get_post_format_strings();
3921 // find out if they want a list of currently supports formats
3922 if ( isset( $args[3] ) && is_array( $args[3] ) ) {
3923 if ( $args[3]['show-supported'] ) {
3924 if ( current_theme_supports( 'post-formats' ) ) {
3925 $supported = get_theme_support( 'post-formats' );
3928 $data['all'] = $formats;
3929 $data['supported'] = $supported[0];
3940 * Retrieves a post type
3944 * @see get_post_type_object()
3946 * @param array $args {
3947 * Method arguments. Note: arguments must be ordered as documented.
3949 * @type int $blog_id (unused)
3950 * @type string $username
3951 * @type string $password
3952 * @type string $post_type_name
3953 * @type array $fields (optional)
3955 * @return array|IXR_Error Array contains:
3958 * - 'capability_type'
3966 public function wp_getPostType( $args ) {
3967 if ( ! $this->minimum_args( $args, 4 ) )
3968 return $this->error;
3970 $this->escape( $args );
3972 $username = $args[1];
3973 $password = $args[2];
3974 $post_type_name = $args[3];
3976 if ( isset( $args[4] ) ) {
3980 * Filter the default query fields used by the given XML-RPC method.
3984 * @param array $fields An array of post type query fields for the given method.
3985 * @param string $method The method name.
3987 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
3990 if ( !$user = $this->login( $username, $password ) )
3991 return $this->error;
3993 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3994 do_action( 'xmlrpc_call', 'wp.getPostType' );
3996 if ( ! post_type_exists( $post_type_name ) )
3997 return new IXR_Error( 403, __( 'Invalid post type' ) );
3999 $post_type = get_post_type_object( $post_type_name );
4001 if ( ! current_user_can( $post_type->cap->edit_posts ) )
4002 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
4004 return $this->_prepare_post_type( $post_type, $fields );
4008 * Retrieves a post types
4012 * @see get_post_types()
4014 * @param array $args {
4015 * Method arguments. Note: arguments must be ordered as documented.
4017 * @type int $blog_id (unused)
4018 * @type string $username
4019 * @type string $password
4020 * @type array $filter (optional)
4021 * @type array $fields (optional)
4023 * @return array|IXR_Error
4025 public function wp_getPostTypes( $args ) {
4026 if ( ! $this->minimum_args( $args, 3 ) )
4027 return $this->error;
4029 $this->escape( $args );
4031 $username = $args[1];
4032 $password = $args[2];
4033 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
4035 if ( isset( $args[4] ) ) {
4038 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4039 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
4042 if ( ! $user = $this->login( $username, $password ) )
4043 return $this->error;
4045 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4046 do_action( 'xmlrpc_call', 'wp.getPostTypes' );
4048 $post_types = get_post_types( $filter, 'objects' );
4052 foreach( $post_types as $post_type ) {
4053 if ( ! current_user_can( $post_type->cap->edit_posts ) )
4056 $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
4063 * Retrieve revisions for a specific post.
4067 * The optional $fields parameter specifies what fields will be included
4068 * in the response array.
4070 * @uses wp_get_post_revisions()
4071 * @see wp_getPost() for more on $fields
4073 * @param array $args {
4074 * Method arguments. Note: arguments must be ordered as documented.
4076 * @type int $blog_id (unused)
4077 * @type string $username
4078 * @type string $password
4079 * @type int $post_id
4080 * @type array $fields (optional)
4082 * @return array|IXR_Error contains a collection of posts.
4084 public function wp_getRevisions( $args ) {
4085 if ( ! $this->minimum_args( $args, 4 ) )
4086 return $this->error;
4088 $this->escape( $args );
4090 $username = $args[1];
4091 $password = $args[2];
4092 $post_id = (int) $args[3];
4094 if ( isset( $args[4] ) ) {
4098 * Filter the default revision query fields used by the given XML-RPC method.
4102 * @param array $field An array of revision query fields.
4103 * @param string $method The method name.
4105 $fields = apply_filters( 'xmlrpc_default_revision_fields', array( 'post_date', 'post_date_gmt' ), 'wp.getRevisions' );
4108 if ( ! $user = $this->login( $username, $password ) )
4109 return $this->error;
4111 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4112 do_action( 'xmlrpc_call', 'wp.getRevisions' );
4114 if ( ! $post = get_post( $post_id ) )
4115 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4117 if ( ! current_user_can( 'edit_post', $post_id ) )
4118 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
4120 // Check if revisions are enabled.
4121 if ( ! wp_revisions_enabled( $post ) )
4122 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
4124 $revisions = wp_get_post_revisions( $post_id );
4131 foreach ( $revisions as $revision ) {
4132 if ( ! current_user_can( 'read_post', $revision->ID ) )
4136 if ( wp_is_post_autosave( $revision ) )
4139 $struct[] = $this->_prepare_post( get_object_vars( $revision ), $fields );
4146 * Restore a post revision
4150 * @uses wp_restore_post_revision()
4152 * @param array $args {
4153 * Method arguments. Note: arguments must be ordered as documented.
4155 * @type int $blog_id (unused)
4156 * @type string $username
4157 * @type string $password
4158 * @type int $revision_id
4160 * @return bool|IXR_Error false if there was an error restoring, true if success.
4162 public function wp_restoreRevision( $args ) {
4163 if ( ! $this->minimum_args( $args, 3 ) )
4164 return $this->error;
4166 $this->escape( $args );
4168 $username = $args[1];
4169 $password = $args[2];
4170 $revision_id = (int) $args[3];
4172 if ( ! $user = $this->login( $username, $password ) )
4173 return $this->error;
4175 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4176 do_action( 'xmlrpc_call', 'wp.restoreRevision' );
4178 if ( ! $revision = wp_get_post_revision( $revision_id ) )
4179 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4181 if ( wp_is_post_autosave( $revision ) )
4182 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4184 if ( ! $post = get_post( $revision->post_parent ) )
4185 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4187 if ( ! current_user_can( 'edit_post', $revision->post_parent ) )
4188 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4190 // Check if revisions are disabled.
4191 if ( ! wp_revisions_enabled( $post ) )
4192 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
4194 $post = wp_restore_post_revision( $revision_id );
4196 return (bool) $post;
4199 /* Blogger API functions.
4200 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
4204 * Retrieve blogs that user owns.
4206 * Will make more sense once we support multiple blogs.
4210 * @param array $args {
4211 * Method arguments. Note: arguments must be ordered as documented.
4213 * @type int $blog_id (unused)
4214 * @type string $username
4215 * @type string $password
4217 * @return array|IXR_Error
4219 public function blogger_getUsersBlogs($args) {
4220 if ( is_multisite() )
4221 return $this->_multisite_getUsersBlogs($args);
4223 $this->escape($args);
4225 $username = $args[1];
4226 $password = $args[2];
4228 if ( !$user = $this->login($username, $password) )
4229 return $this->error;
4231 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4232 do_action( 'xmlrpc_call', 'blogger.getUsersBlogs' );
4234 $is_admin = current_user_can('manage_options');
4237 'isAdmin' => $is_admin,
4238 'url' => get_option('home') . '/',
4240 'blogName' => get_option('blogname'),
4241 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
4244 return array($struct);
4248 * Private function for retrieving a users blogs for multisite setups
4252 * @return array|IXR_Error
4254 protected function _multisite_getUsersBlogs($args) {
4255 $current_blog = get_blog_details();
4257 $domain = $current_blog->domain;
4258 $path = $current_blog->path . 'xmlrpc.php';
4260 $rpc = new IXR_Client( set_url_scheme( "http://{$domain}{$path}" ) );
4261 $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
4262 $blogs = $rpc->getResponse();
4264 if ( isset($blogs['faultCode']) )
4265 return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
4267 if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
4270 foreach ( (array) $blogs as $blog ) {
4271 if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
4272 return array($blog);
4279 * Retrieve user's data.
4281 * Gives your client some info about you, so you don't have to.
4285 * @param array $args {
4286 * Method arguments. Note: arguments must be ordered as documented.
4288 * @type int $blog_id (unused)
4289 * @type string $username
4290 * @type string $password
4292 * @return array|IXR_Error
4294 public function blogger_getUserInfo( $args ) {
4295 $this->escape( $args );
4297 $username = $args[1];
4298 $password = $args[2];
4300 if ( !$user = $this->login($username, $password) )
4301 return $this->error;
4303 if ( !current_user_can( 'edit_posts' ) )
4304 return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
4306 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4307 do_action( 'xmlrpc_call', 'blogger.getUserInfo' );
4310 'nickname' => $user->nickname,
4311 'userid' => $user->ID,
4312 'url' => $user->user_url,
4313 'lastname' => $user->last_name,
4314 'firstname' => $user->first_name
4325 * @param array $args {
4326 * Method arguments. Note: arguments must be ordered as documented.
4328 * @type int $blog_id (unused)
4329 * @type int $post_ID
4330 * @type string $username
4331 * @type string $password
4333 * @return array|IXR_Error
4335 public function blogger_getPost( $args ) {
4336 $this->escape( $args );
4338 $post_ID = (int) $args[1];
4339 $username = $args[2];
4340 $password = $args[3];
4342 if ( !$user = $this->login($username, $password) )
4343 return $this->error;
4345 $post_data = get_post($post_ID, ARRAY_A);
4347 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4349 if ( !current_user_can( 'edit_post', $post_ID ) )
4350 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4352 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4353 do_action( 'xmlrpc_call', 'blogger.getPost' );
4355 $categories = implode(',', wp_get_post_categories($post_ID));
4357 $content = '<title>'.wp_unslash($post_data['post_title']).'</title>';
4358 $content .= '<category>'.$categories.'</category>';
4359 $content .= wp_unslash($post_data['post_content']);
4362 'userid' => $post_data['post_author'],
4363 'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
4364 'content' => $content,
4365 'postid' => (string) $post_data['ID']
4372 * Retrieve list of recent posts.
4376 * @param array $args {
4377 * Method arguments. Note: arguments must be ordered as documented.
4379 * @type string $appkey (unused)
4380 * @type int $blog_id (unused)
4381 * @type string $username
4382 * @type string $password
4383 * @type int $numberposts (optional)
4385 * @return array|IXR_Error
4387 public function blogger_getRecentPosts( $args ) {
4389 $this->escape($args);
4391 // $args[0] = appkey - ignored
4392 $username = $args[2];
4393 $password = $args[3];
4394 if ( isset( $args[4] ) )
4395 $query = array( 'numberposts' => absint( $args[4] ) );
4399 if ( !$user = $this->login($username, $password) )
4400 return $this->error;
4402 if ( ! current_user_can( 'edit_posts' ) )
4403 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
4405 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4406 do_action( 'xmlrpc_call', 'blogger.getRecentPosts' );
4408 $posts_list = wp_get_recent_posts( $query );
4410 if ( !$posts_list ) {
4411 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
4412 return $this->error;
4415 $recent_posts = array();
4416 foreach ($posts_list as $entry) {
4417 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
4420 $post_date = $this->_convert_date( $entry['post_date'] );
4421 $categories = implode(',', wp_get_post_categories($entry['ID']));
4423 $content = '<title>'.wp_unslash($entry['post_title']).'</title>';
4424 $content .= '<category>'.$categories.'</category>';
4425 $content .= wp_unslash($entry['post_content']);
4427 $recent_posts[] = array(
4428 'userid' => $entry['post_author'],
4429 'dateCreated' => $post_date,
4430 'content' => $content,
4431 'postid' => (string) $entry['ID'],
4435 return $recent_posts;
4445 public function blogger_getTemplate($args) {
4446 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4456 public function blogger_setTemplate($args) {
4457 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4465 * @param array $args {
4466 * Method arguments. Note: arguments must be ordered as documented.
4468 * @type string $appkey (unused)
4469 * @type int $blog_id (unused)
4470 * @type string $username
4471 * @type string $password
4472 * @type string $content
4473 * @type string $publish
4475 * @return int|IXR_Error
4477 public function blogger_newPost( $args ) {
4478 $this->escape( $args );
4480 $username = $args[2];
4481 $password = $args[3];
4482 $content = $args[4];
4483 $publish = $args[5];
4485 if ( !$user = $this->login($username, $password) )
4486 return $this->error;
4488 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4489 do_action( 'xmlrpc_call', 'blogger.newPost' );
4491 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
4492 if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) || !current_user_can($cap) )
4493 return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
4495 $post_status = ($publish) ? 'publish' : 'draft';
4497 $post_author = $user->ID;
4499 $post_title = xmlrpc_getposttitle($content);
4500 $post_category = xmlrpc_getpostcategory($content);
4501 $post_content = xmlrpc_removepostdata($content);
4503 $post_date = current_time('mysql');
4504 $post_date_gmt = current_time('mysql', 1);
4506 $post_data = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
4508 $post_ID = wp_insert_post($post_data);
4509 if ( is_wp_error( $post_ID ) )
4510 return new IXR_Error(500, $post_ID->get_error_message());
4513 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4515 $this->attach_uploads( $post_ID, $post_content );
4518 * Fires after a new post has been successfully created via the XML-RPC Blogger API.
4522 * @param int $post_ID ID of the new post.
4523 * @param array $args An array of new post arguments.
4525 do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
4535 * @param array $args {
4536 * Method arguments. Note: arguments must be ordered as documented.
4538 * @type int $blog_id (unused)
4539 * @type int $post_ID
4540 * @type string $username
4541 * @type string $password
4542 * @type string $content
4544 * @return true|IXR_Error true when done.
4546 public function blogger_editPost( $args ) {
4548 $this->escape($args);
4550 $post_ID = (int) $args[1];
4551 $username = $args[2];
4552 $password = $args[3];
4553 $content = $args[4];
4555 if ( ! $user = $this->login( $username, $password ) ) {
4556 return $this->error;
4559 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4560 do_action( 'xmlrpc_call', 'blogger.editPost' );
4562 $actual_post = get_post( $post_ID, ARRAY_A );
4564 if ( ! $actual_post || $actual_post['post_type'] != 'post' ) {
4565 return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
4568 $this->escape($actual_post);
4570 if ( ! current_user_can( 'edit_post', $post_ID ) ) {
4571 return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
4573 if ( 'publish' == $actual_post['post_status'] && ! current_user_can( 'publish_posts' ) ) {
4574 return new IXR_Error( 401, __( 'Sorry, you do not have the right to publish this post.' ) );
4577 $postdata = array();
4578 $postdata['ID'] = $actual_post['ID'];
4579 $postdata['post_content'] = xmlrpc_removepostdata( $content );
4580 $postdata['post_title'] = xmlrpc_getposttitle( $content );
4581 $postdata['post_category'] = xmlrpc_getpostcategory( $content );
4582 $postdata['post_status'] = $actual_post['post_status'];
4583 $postdata['post_excerpt'] = $actual_post['post_excerpt'];
4585 $result = wp_update_post( $postdata );
4588 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
4590 $this->attach_uploads( $actual_post['ID'], $postdata['post_content'] );
4593 * Fires after a post has been successfully updated via the XML-RPC Blogger API.
4597 * @param int $post_ID ID of the updated post.
4598 * @param array $args An array of arguments for the post to edit.
4600 do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
4610 * @param array $args {
4611 * Method arguments. Note: arguments must be ordered as documented.
4613 * @type int $blog_id (unused)
4614 * @type int $post_ID
4615 * @type string $username
4616 * @type string $password
4618 * @return true|IXR_Error True when post is deleted.
4620 public function blogger_deletePost( $args ) {
4621 $this->escape( $args );
4623 $post_ID = (int) $args[1];
4624 $username = $args[2];
4625 $password = $args[3];
4627 if ( !$user = $this->login($username, $password) )
4628 return $this->error;
4630 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4631 do_action( 'xmlrpc_call', 'blogger.deletePost' );
4633 $actual_post = get_post($post_ID,ARRAY_A);
4635 if ( !$actual_post || $actual_post['post_type'] != 'post' )
4636 return new IXR_Error(404, __('Sorry, no such post.'));
4638 if ( !current_user_can('delete_post', $post_ID) )
4639 return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
4641 $result = wp_delete_post($post_ID);
4644 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
4647 * Fires after a post has been successfully deleted via the XML-RPC Blogger API.
4651 * @param int $post_ID ID of the deleted post.
4652 * @param array $args An array of arguments to delete the post.
4654 do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
4659 /* MetaWeblog API functions
4660 * specs on wherever Dave Winer wants them to be
4664 * Create a new post.
4666 * The 'content_struct' argument must contain:
4675 * Also, it can optionally contain:
4678 * - wp_page_parent_id
4681 * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
4682 * - mt_allow_comments - can be 'open' or 'closed'
4683 * - mt_allow_pings - can be 'open' or 'closed'
4684 * - date_created_gmt
4686 * - wp_post_thumbnail
4690 * @param array $args {
4691 * Method arguments. Note: arguments must be ordered as documented.
4693 * @type int $blog_id (unused)
4694 * @type string $username
4695 * @type string $password
4696 * @type array $content_struct
4697 * @type int $publish
4699 * @return int|IXR_Error
4701 public function mw_newPost($args) {
4702 $this->escape($args);
4704 $username = $args[1];
4705 $password = $args[2];
4706 $content_struct = $args[3];
4707 $publish = isset( $args[4] ) ? $args[4] : 0;
4709 if ( !$user = $this->login($username, $password) )
4710 return $this->error;
4712 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4713 do_action( 'xmlrpc_call', 'metaWeblog.newPost' );
4715 $page_template = '';
4716 if ( !empty( $content_struct['post_type'] ) ) {
4717 if ( $content_struct['post_type'] == 'page' ) {
4719 $cap = 'publish_pages';
4720 elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
4721 $cap = 'publish_pages';
4723 $cap = 'edit_pages';
4724 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
4725 $post_type = 'page';
4726 if ( !empty( $content_struct['wp_page_template'] ) )
4727 $page_template = $content_struct['wp_page_template'];
4728 } elseif ( $content_struct['post_type'] == 'post' ) {
4730 $cap = 'publish_posts';
4731 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
4732 $cap = 'publish_posts';
4734 $cap = 'edit_posts';
4735 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4736 $post_type = 'post';
4738 // No other post_type values are allowed here
4739 return new IXR_Error( 401, __( 'Invalid post type' ) );
4743 $cap = 'publish_posts';
4744 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
4745 $cap = 'publish_posts';
4747 $cap = 'edit_posts';
4748 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4749 $post_type = 'post';
4752 if ( ! current_user_can( get_post_type_object( $post_type )->cap->create_posts ) )
4753 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts on this site.' ) );
4754 if ( !current_user_can( $cap ) )
4755 return new IXR_Error( 401, $error_message );
4757 // Check for a valid post format if one was given
4758 if ( isset( $content_struct['wp_post_format'] ) ) {
4759 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4760 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4761 return new IXR_Error( 404, __( 'Invalid post format' ) );
4765 // Let WordPress generate the post_name (slug) unless
4766 // one has been provided.
4768 if ( isset($content_struct['wp_slug']) )
4769 $post_name = $content_struct['wp_slug'];
4771 // Only use a password if one was given.
4772 if ( isset($content_struct['wp_password']) )
4773 $post_password = $content_struct['wp_password'];
4775 // Only set a post parent if one was provided.
4776 if ( isset($content_struct['wp_page_parent_id']) )
4777 $post_parent = $content_struct['wp_page_parent_id'];
4779 // Only set the menu_order if it was provided.
4780 if ( isset($content_struct['wp_page_order']) )
4781 $menu_order = $content_struct['wp_page_order'];
4783 $post_author = $user->ID;
4785 // If an author id was provided then use it instead.
4786 if ( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) {
4787 switch ( $post_type ) {
4789 if ( !current_user_can( 'edit_others_posts' ) )
4790 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
4793 if ( !current_user_can( 'edit_others_pages' ) )
4794 return new IXR_Error( 401, __( 'You are not allowed to create pages as this user.' ) );
4797 return new IXR_Error( 401, __( 'Invalid post type' ) );
4799 $author = get_userdata( $content_struct['wp_author_id'] );
4801 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
4802 $post_author = $content_struct['wp_author_id'];
4805 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
4806 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
4808 $post_status = $publish ? 'publish' : 'draft';
4810 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4811 switch ( $content_struct["{$post_type}_status"] ) {
4816 $post_status = $content_struct["{$post_type}_status"];
4819 $post_status = $publish ? 'publish' : 'draft';
4824 $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
4825 $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
4827 $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
4829 if ( isset($content_struct['mt_allow_comments']) ) {
4830 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4831 switch ( $content_struct['mt_allow_comments'] ) {
4833 $comment_status = 'closed';
4836 $comment_status = 'open';
4839 $comment_status = get_default_comment_status( $post_type );
4843 switch ( (int) $content_struct['mt_allow_comments'] ) {
4846 $comment_status = 'closed';
4849 $comment_status = 'open';
4852 $comment_status = get_default_comment_status( $post_type );
4857 $comment_status = get_default_comment_status( $post_type );
4860 if ( isset($content_struct['mt_allow_pings']) ) {
4861 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4862 switch ( $content_struct['mt_allow_pings'] ) {
4864 $ping_status = 'closed';
4867 $ping_status = 'open';
4870 $ping_status = get_default_comment_status( $post_type, 'pingback' );
4874 switch ( (int) $content_struct['mt_allow_pings'] ) {
4876 $ping_status = 'closed';
4879 $ping_status = 'open';
4882 $ping_status = get_default_comment_status( $post_type, 'pingback' );
4887 $ping_status = get_default_comment_status( $post_type, 'pingback' );
4891 $post_content = $post_content . '<!--more-->' . $post_more;
4894 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4895 $to_ping = $content_struct['mt_tb_ping_urls'];
4896 if ( is_array($to_ping) )
4897 $to_ping = implode(' ', $to_ping);
4900 // Do some timestamp voodoo
4901 if ( !empty( $content_struct['date_created_gmt'] ) )
4902 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4903 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4904 elseif ( !empty( $content_struct['dateCreated']) )
4905 $dateCreated = $content_struct['dateCreated']->getIso();
4907 if ( !empty( $dateCreated ) ) {
4908 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4909 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4911 $post_date = current_time('mysql');
4912 $post_date_gmt = current_time('mysql', 1);
4915 $post_category = array();
4916 if ( isset( $content_struct['categories'] ) ) {
4917 $catnames = $content_struct['categories'];
4919 if ( is_array($catnames) ) {
4920 foreach ($catnames as $cat) {
4921 $post_category[] = get_cat_ID($cat);
4926 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'to_ping', 'post_type', 'post_name', 'post_password', 'post_parent', 'menu_order', 'tags_input', 'page_template');
4928 $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
4930 // Only posts can be sticky
4931 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4933 $data['sticky'] = $content_struct['sticky'];
4934 $error = $this->_toggle_sticky( $data );
4940 if ( isset($content_struct['custom_fields']) )
4941 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4943 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4944 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4945 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4947 unset( $content_struct['wp_post_thumbnail'] );
4950 // Handle enclosures
4951 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4952 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4954 $this->attach_uploads( $post_ID, $post_content );
4956 // Handle post formats if assigned, value is validated earlier
4958 if ( isset( $content_struct['wp_post_format'] ) )
4959 set_post_format( $post_ID, $content_struct['wp_post_format'] );
4961 $post_ID = wp_insert_post( $postdata, true );
4962 if ( is_wp_error( $post_ID ) )
4963 return new IXR_Error(500, $post_ID->get_error_message());
4966 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4969 * Fires after a new post has been successfully created via the XML-RPC MovableType API.
4973 * @param int $post_ID ID of the new post.
4974 * @param array $args An array of arguments to create the new post.
4976 do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
4978 return strval($post_ID);
4982 * @param integer $post_ID
4983 * @param array $enclosure
4985 public function add_enclosure_if_new( $post_ID, $enclosure ) {
4986 if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
4987 $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'] . "\n";
4989 if ( $enclosures = get_post_meta( $post_ID, 'enclosure' ) ) {
4990 foreach ( $enclosures as $enc ) {
4991 // This method used to omit the trailing new line. #23219
4992 if ( rtrim( $enc, "\n" ) == rtrim( $encstring, "\n" ) ) {
4999 add_post_meta( $post_ID, 'enclosure', $encstring );
5004 * Attach upload to a post.
5008 * @global wpdb $wpdb
5010 * @param int $post_ID Post ID.
5011 * @param string $post_content Post Content for attachment.
5013 public function attach_uploads( $post_ID, $post_content ) {
5016 // find any unattached files
5017 $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
5018 if ( is_array( $attachments ) ) {
5019 foreach ( $attachments as $file ) {
5020 if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false )
5021 $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
5031 * @param array $args {
5032 * Method arguments. Note: arguments must be ordered as documented.
5034 * @type int $blog_id (unused)
5035 * @type string $username
5036 * @type string $password
5037 * @type array $content_struct
5038 * @type int $publish
5040 * @return bool|IXR_Error True on success.
5042 public function mw_editPost( $args ) {
5043 $this->escape( $args );
5045 $post_ID = (int) $args[0];
5046 $username = $args[1];
5047 $password = $args[2];
5048 $content_struct = $args[3];
5049 $publish = isset( $args[4] ) ? $args[4] : 0;
5051 if ( ! $user = $this->login($username, $password) )
5052 return $this->error;
5054 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5055 do_action( 'xmlrpc_call', 'metaWeblog.editPost' );
5057 $postdata = get_post( $post_ID, ARRAY_A );
5060 * If there is no post data for the give post id, stop now and return an error.
5061 * Otherwise a new post will be created (which was the old behavior).
5063 if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
5064 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5066 if ( ! current_user_can( 'edit_post', $post_ID ) )
5067 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this post.' ) );
5069 // Use wp.editPost to edit post types other than post and page.
5070 if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
5071 return new IXR_Error( 401, __( 'Invalid post type' ) );
5073 // Thwart attempt to change the post type.
5074 if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
5075 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
5077 // Check for a valid post format if one was given
5078 if ( isset( $content_struct['wp_post_format'] ) ) {
5079 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
5080 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
5081 return new IXR_Error( 404, __( 'Invalid post format' ) );
5085 $this->escape($postdata);
5087 $ID = $postdata['ID'];
5088 $post_content = $postdata['post_content'];
5089 $post_title = $postdata['post_title'];
5090 $post_excerpt = $postdata['post_excerpt'];
5091 $post_password = $postdata['post_password'];
5092 $post_parent = $postdata['post_parent'];
5093 $post_type = $postdata['post_type'];
5094 $menu_order = $postdata['menu_order'];
5096 // Let WordPress manage slug if none was provided.
5097 $post_name = $postdata['post_name'];
5098 if ( isset($content_struct['wp_slug']) )
5099 $post_name = $content_struct['wp_slug'];
5101 // Only use a password if one was given.
5102 if ( isset($content_struct['wp_password']) )
5103 $post_password = $content_struct['wp_password'];
5105 // Only set a post parent if one was given.
5106 if ( isset($content_struct['wp_page_parent_id']) )
5107 $post_parent = $content_struct['wp_page_parent_id'];
5109 // Only set the menu_order if it was given.
5110 if ( isset($content_struct['wp_page_order']) )
5111 $menu_order = $content_struct['wp_page_order'];
5113 $page_template = null;
5114 if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
5115 $page_template = $content_struct['wp_page_template'];
5117 $post_author = $postdata['post_author'];
5119 // Only set the post_author if one is set.
5120 if ( isset( $content_struct['wp_author_id'] ) ) {
5121 // Check permissions if attempting to switch author to or from another user.
5122 if ( $user->ID != $content_struct['wp_author_id'] || $user->ID != $post_author ) {
5123 switch ( $post_type ) {
5125 if ( ! current_user_can( 'edit_others_posts' ) ) {
5126 return new IXR_Error( 401, __( 'You are not allowed to change the post author as this user.' ) );
5130 if ( ! current_user_can( 'edit_others_pages' ) ) {
5131 return new IXR_Error( 401, __( 'You are not allowed to change the page author as this user.' ) );
5135 return new IXR_Error( 401, __( 'Invalid post type' ) );
5138 $post_author = $content_struct['wp_author_id'];
5142 if ( isset($content_struct['mt_allow_comments']) ) {
5143 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
5144 switch ( $content_struct['mt_allow_comments'] ) {
5146 $comment_status = 'closed';
5149 $comment_status = 'open';
5152 $comment_status = get_default_comment_status( $post_type );
5156 switch ( (int) $content_struct['mt_allow_comments'] ) {
5159 $comment_status = 'closed';
5162 $comment_status = 'open';
5165 $comment_status = get_default_comment_status( $post_type );
5171 if ( isset($content_struct['mt_allow_pings']) ) {
5172 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
5173 switch ( $content_struct['mt_allow_pings'] ) {
5175 $ping_status = 'closed';
5178 $ping_status = 'open';
5181 $ping_status = get_default_comment_status( $post_type, 'pingback' );
5185 switch ( (int) $content_struct["mt_allow_pings"] ) {
5187 $ping_status = 'closed';
5190 $ping_status = 'open';
5193 $ping_status = get_default_comment_status( $post_type, 'pingback' );
5199 if ( isset( $content_struct['title'] ) )
5200 $post_title = $content_struct['title'];
5202 if ( isset( $content_struct['description'] ) )
5203 $post_content = $content_struct['description'];
5205 $post_category = array();
5206 if ( isset( $content_struct['categories'] ) ) {
5207 $catnames = $content_struct['categories'];
5208 if ( is_array($catnames) ) {
5209 foreach ($catnames as $cat) {
5210 $post_category[] = get_cat_ID($cat);
5215 if ( isset( $content_struct['mt_excerpt'] ) )
5216 $post_excerpt = $content_struct['mt_excerpt'];
5218 $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
5220 $post_status = $publish ? 'publish' : 'draft';
5221 if ( isset( $content_struct["{$post_type}_status"] ) ) {
5222 switch( $content_struct["{$post_type}_status"] ) {
5227 $post_status = $content_struct["{$post_type}_status"];
5230 $post_status = $publish ? 'publish' : 'draft';
5235 $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
5237 if ( ('publish' == $post_status) ) {
5238 if ( ( 'page' == $post_type ) && ! current_user_can( 'publish_pages' ) ) {
5239 return new IXR_Error( 401, __( 'Sorry, you do not have the right to publish this page.' ) );
5240 } elseif ( ! current_user_can( 'publish_posts' ) ) {
5241 return new IXR_Error( 401, __( 'Sorry, you do not have the right to publish this post.' ) );
5246 $post_content = $post_content . "<!--more-->" . $post_more;
5249 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
5250 $to_ping = $content_struct['mt_tb_ping_urls'];
5251 if ( is_array($to_ping) )
5252 $to_ping = implode(' ', $to_ping);
5255 // Do some timestamp voodoo.
5256 if ( !empty( $content_struct['date_created_gmt'] ) )
5257 // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
5258 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
5259 elseif ( !empty( $content_struct['dateCreated']) )
5260 $dateCreated = $content_struct['dateCreated']->getIso();
5262 if ( !empty( $dateCreated ) ) {
5263 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
5264 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
5266 $post_date = $postdata['post_date'];
5267 $post_date_gmt = $postdata['post_date_gmt'];
5270 // We've got all the data -- post it.
5271 $newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'post_date', 'post_date_gmt', 'to_ping', 'post_name', 'post_password', 'post_parent', 'menu_order', 'post_author', 'tags_input', 'page_template');
5273 $result = wp_update_post($newpost, true);
5274 if ( is_wp_error( $result ) )
5275 return new IXR_Error(500, $result->get_error_message());
5278 return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
5280 // Only posts can be sticky
5281 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
5283 $data['sticky'] = $content_struct['sticky'];
5284 $data['post_type'] = 'post';
5285 $error = $this->_toggle_sticky( $data, true );
5291 if ( isset($content_struct['custom_fields']) )
5292 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
5294 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
5296 // Empty value deletes, non-empty value adds/updates.
5297 if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
5298 delete_post_thumbnail( $post_ID );
5300 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
5301 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
5303 unset( $content_struct['wp_post_thumbnail'] );
5306 // Handle enclosures.
5307 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
5308 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
5310 $this->attach_uploads( $ID, $post_content );
5312 // Handle post formats if assigned, validation is handled earlier in this function.
5313 if ( isset( $content_struct['wp_post_format'] ) )
5314 set_post_format( $post_ID, $content_struct['wp_post_format'] );
5317 * Fires after a post has been successfully updated via the XML-RPC MovableType API.
5321 * @param int $post_ID ID of the updated post.
5322 * @param array $args An array of arguments to update the post.
5324 do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
5334 * @param array $args {
5335 * Method arguments. Note: arguments must be ordered as documented.
5337 * @type int $blog_id (unused)
5338 * @type int $post_ID
5339 * @type string $username
5340 * @type string $password
5342 * @return array|IXR_Error
5344 public function mw_getPost( $args ) {
5345 $this->escape( $args );
5347 $post_ID = (int) $args[0];
5348 $username = $args[1];
5349 $password = $args[2];
5351 if ( !$user = $this->login($username, $password) )
5352 return $this->error;
5354 $postdata = get_post($post_ID, ARRAY_A);
5356 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5358 if ( !current_user_can( 'edit_post', $post_ID ) )
5359 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
5361 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5362 do_action( 'xmlrpc_call', 'metaWeblog.getPost' );
5364 if ($postdata['post_date'] != '') {
5365 $post_date = $this->_convert_date( $postdata['post_date'] );
5366 $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'], $postdata['post_date'] );
5367 $post_modified = $this->_convert_date( $postdata['post_modified'] );
5368 $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
5370 $categories = array();
5371 $catids = wp_get_post_categories($post_ID);
5372 foreach($catids as $catid)
5373 $categories[] = get_cat_name($catid);
5375 $tagnames = array();
5376 $tags = wp_get_post_tags( $post_ID );
5377 if ( !empty( $tags ) ) {
5378 foreach ( $tags as $tag )
5379 $tagnames[] = $tag->name;
5380 $tagnames = implode( ', ', $tagnames );
5385 $post = get_extended($postdata['post_content']);
5386 $link = post_permalink($postdata['ID']);
5388 // Get the author info.
5389 $author = get_userdata($postdata['post_author']);
5391 $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
5392 $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
5394 // Consider future posts as published
5395 if ( $postdata['post_status'] === 'future' )
5396 $postdata['post_status'] = 'publish';
5399 $post_format = get_post_format( $post_ID );
5400 if ( empty( $post_format ) )
5401 $post_format = 'standard';
5404 if ( is_sticky( $post_ID ) )
5407 $enclosure = array();
5408 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
5409 if ($key == 'enclosure') {
5410 foreach ( (array) $val as $enc ) {
5411 $encdata = explode("\n", $enc);
5412 $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
5413 $enclosure['length'] = (int) trim($encdata[1]);
5414 $enclosure['type'] = trim($encdata[2]);
5421 'dateCreated' => $post_date,
5422 'userid' => $postdata['post_author'],
5423 'postid' => $postdata['ID'],
5424 'description' => $post['main'],
5425 'title' => $postdata['post_title'],
5427 'permaLink' => $link,
5428 // commented out because no other tool seems to use this
5429 // 'content' => $entry['post_content'],
5430 'categories' => $categories,
5431 'mt_excerpt' => $postdata['post_excerpt'],
5432 'mt_text_more' => $post['extended'],
5433 'wp_more_text' => $post['more_text'],
5434 'mt_allow_comments' => $allow_comments,
5435 'mt_allow_pings' => $allow_pings,
5436 'mt_keywords' => $tagnames,
5437 'wp_slug' => $postdata['post_name'],
5438 'wp_password' => $postdata['post_password'],
5439 'wp_author_id' => (string) $author->ID,
5440 'wp_author_display_name' => $author->display_name,
5441 'date_created_gmt' => $post_date_gmt,
5442 'post_status' => $postdata['post_status'],
5443 'custom_fields' => $this->get_custom_fields($post_ID),
5444 'wp_post_format' => $post_format,
5445 'sticky' => $sticky,
5446 'date_modified' => $post_modified,
5447 'date_modified_gmt' => $post_modified_gmt
5450 if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
5452 $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
5456 return new IXR_Error(404, __('Sorry, no such post.'));
5461 * Retrieve list of recent posts.
5465 * @param array $args {
5466 * Method arguments. Note: arguments must be ordered as documented.
5468 * @type int $blog_id (unused)
5469 * @type string $username
5470 * @type string $password
5471 * @type int $numberposts
5473 * @return array|IXR_Error
5475 public function mw_getRecentPosts( $args ) {
5476 $this->escape( $args );
5478 $username = $args[1];
5479 $password = $args[2];
5480 if ( isset( $args[3] ) )
5481 $query = array( 'numberposts' => absint( $args[3] ) );
5485 if ( !$user = $this->login($username, $password) )
5486 return $this->error;
5488 if ( ! current_user_can( 'edit_posts' ) )
5489 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
5491 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5492 do_action( 'xmlrpc_call', 'metaWeblog.getRecentPosts' );
5494 $posts_list = wp_get_recent_posts( $query );
5499 $recent_posts = array();
5500 foreach ($posts_list as $entry) {
5501 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5504 $post_date = $this->_convert_date( $entry['post_date'] );
5505 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5506 $post_modified = $this->_convert_date( $entry['post_modified'] );
5507 $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
5509 $categories = array();
5510 $catids = wp_get_post_categories($entry['ID']);
5511 foreach( $catids as $catid )
5512 $categories[] = get_cat_name($catid);
5514 $tagnames = array();
5515 $tags = wp_get_post_tags( $entry['ID'] );
5516 if ( !empty( $tags ) ) {
5517 foreach ( $tags as $tag ) {
5518 $tagnames[] = $tag->name;
5520 $tagnames = implode( ', ', $tagnames );
5525 $post = get_extended($entry['post_content']);
5526 $link = post_permalink($entry['ID']);
5528 // Get the post author info.
5529 $author = get_userdata($entry['post_author']);
5531 $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
5532 $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
5534 // Consider future posts as published
5535 if ( $entry['post_status'] === 'future' )
5536 $entry['post_status'] = 'publish';
5539 $post_format = get_post_format( $entry['ID'] );
5540 if ( empty( $post_format ) )
5541 $post_format = 'standard';
5543 $recent_posts[] = array(
5544 'dateCreated' => $post_date,
5545 'userid' => $entry['post_author'],
5546 'postid' => (string) $entry['ID'],
5547 'description' => $post['main'],
5548 'title' => $entry['post_title'],
5550 'permaLink' => $link,
5551 // commented out because no other tool seems to use this
5552 // 'content' => $entry['post_content'],
5553 'categories' => $categories,
5554 'mt_excerpt' => $entry['post_excerpt'],
5555 'mt_text_more' => $post['extended'],
5556 'wp_more_text' => $post['more_text'],
5557 'mt_allow_comments' => $allow_comments,
5558 'mt_allow_pings' => $allow_pings,
5559 'mt_keywords' => $tagnames,
5560 'wp_slug' => $entry['post_name'],
5561 'wp_password' => $entry['post_password'],
5562 'wp_author_id' => (string) $author->ID,
5563 'wp_author_display_name' => $author->display_name,
5564 'date_created_gmt' => $post_date_gmt,
5565 'post_status' => $entry['post_status'],
5566 'custom_fields' => $this->get_custom_fields($entry['ID']),
5567 'wp_post_format' => $post_format,
5568 'date_modified' => $post_modified,
5569 'date_modified_gmt' => $post_modified_gmt,
5570 'sticky' => ( $entry['post_type'] === 'post' && is_sticky( $entry['ID'] ) ),
5571 'wp_post_thumbnail' => get_post_thumbnail_id( $entry['ID'] )
5575 return $recent_posts;
5579 * Retrieve the list of categories on a given blog.
5583 * @param array $args {
5584 * Method arguments. Note: arguments must be ordered as documented.
5586 * @type int $blog_id (unused)
5587 * @type string $username
5588 * @type string $password
5590 * @return array|IXR_Error
5592 public function mw_getCategories( $args ) {
5593 $this->escape( $args );
5595 $username = $args[1];
5596 $password = $args[2];
5598 if ( !$user = $this->login($username, $password) )
5599 return $this->error;
5601 if ( !current_user_can( 'edit_posts' ) )
5602 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5604 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5605 do_action( 'xmlrpc_call', 'metaWeblog.getCategories' );
5607 $categories_struct = array();
5609 if ( $cats = get_categories(array('get' => 'all')) ) {
5610 foreach ( $cats as $cat ) {
5612 $struct['categoryId'] = $cat->term_id;
5613 $struct['parentId'] = $cat->parent;
5614 $struct['description'] = $cat->name;
5615 $struct['categoryDescription'] = $cat->description;
5616 $struct['categoryName'] = $cat->name;
5617 $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
5618 $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
5620 $categories_struct[] = $struct;
5624 return $categories_struct;
5628 * Uploads a file, following your settings.
5630 * Adapted from a patch by Johann Richard.
5632 * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
5636 * @global wpdb $wpdb
5638 * @param array $args {
5639 * Method arguments. Note: arguments must be ordered as documented.
5641 * @type int $blog_id (unused)
5642 * @type string $username
5643 * @type string $password
5646 * @return array|IXR_Error
5648 public function mw_newMediaObject( $args ) {
5651 $username = $this->escape( $args[1] );
5652 $password = $this->escape( $args[2] );
5655 $name = sanitize_file_name( $data['name'] );
5656 $type = $data['type'];
5657 $bits = $data['bits'];
5659 if ( !$user = $this->login($username, $password) )
5660 return $this->error;
5662 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5663 do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject' );
5665 if ( !current_user_can('upload_files') ) {
5666 $this->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
5667 return $this->error;
5671 * Filter whether to preempt the XML-RPC media upload.
5673 * Passing a truthy value will effectively short-circuit the media upload,
5674 * returning that value as a 500 error instead.
5678 * @param bool $error Whether to pre-empt the media upload. Default false.
5680 if ( $upload_err = apply_filters( 'pre_upload_error', false ) ) {
5681 return new IXR_Error( 500, $upload_err );
5684 if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) {
5685 // Get postmeta info on the object.
5686 $old_file = $wpdb->get_row("
5689 WHERE post_title = '{$name}'
5690 AND post_type = 'attachment'
5693 // Delete previous file.
5694 wp_delete_attachment($old_file->ID);
5696 // Make sure the new name is different by pre-pending the
5697 // previous post id.
5698 $filename = preg_replace('/^wpid\d+-/', '', $name);
5699 $name = "wpid{$old_file->ID}-{$filename}";
5702 $upload = wp_upload_bits($name, null, $bits);
5703 if ( ! empty($upload['error']) ) {
5704 $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
5705 return new IXR_Error(500, $errorString);
5707 // Construct the attachment array
5709 if ( ! empty( $data['post_id'] ) ) {
5710 $post_id = (int) $data['post_id'];
5712 if ( ! current_user_can( 'edit_post', $post_id ) )
5713 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
5715 $attachment = array(
5716 'post_title' => $name,
5717 'post_content' => '',
5718 'post_type' => 'attachment',
5719 'post_parent' => $post_id,
5720 'post_mime_type' => $type,
5721 'guid' => $upload[ 'url' ]
5725 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
5726 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
5729 * Fires after a new attachment has been added via the XML-RPC MovableType API.
5733 * @param int $id ID of the new attachment.
5734 * @param array $args An array of arguments to add the attachment.
5736 do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
5739 'id' => strval( $id ),
5741 'url' => $upload[ 'url' ],
5745 /** This filter is documented in wp-admin/includes/file.php */
5746 return apply_filters( 'wp_handle_upload', $struct, 'upload' );
5749 /* MovableType API functions
5750 * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
5754 * Retrieve the post titles of recent posts.
5758 * @param array $args {
5759 * Method arguments. Note: arguments must be ordered as documented.
5761 * @type int $blog_id (unused)
5762 * @type string $username
5763 * @type string $password
5764 * @type int $numberposts
5766 * @return array|IXR_Error
5768 public function mt_getRecentPostTitles( $args ) {
5769 $this->escape( $args );
5771 $username = $args[1];
5772 $password = $args[2];
5773 if ( isset( $args[3] ) )
5774 $query = array( 'numberposts' => absint( $args[3] ) );
5778 if ( !$user = $this->login($username, $password) )
5779 return $this->error;
5781 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5782 do_action( 'xmlrpc_call', 'mt.getRecentPostTitles' );
5784 $posts_list = wp_get_recent_posts( $query );
5786 if ( !$posts_list ) {
5787 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
5788 return $this->error;
5791 $recent_posts = array();
5793 foreach ($posts_list as $entry) {
5794 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5797 $post_date = $this->_convert_date( $entry['post_date'] );
5798 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5800 $recent_posts[] = array(
5801 'dateCreated' => $post_date,
5802 'userid' => $entry['post_author'],
5803 'postid' => (string) $entry['ID'],
5804 'title' => $entry['post_title'],
5805 'post_status' => $entry['post_status'],
5806 'date_created_gmt' => $post_date_gmt
5810 return $recent_posts;
5814 * Retrieve list of all categories on blog.
5818 * @param array $args {
5819 * Method arguments. Note: arguments must be ordered as documented.
5821 * @type int $blog_id (unused)
5822 * @type string $username
5823 * @type string $password
5825 * @return array|IXR_Error
5827 public function mt_getCategoryList( $args ) {
5828 $this->escape( $args );
5830 $username = $args[1];
5831 $password = $args[2];
5833 if ( !$user = $this->login($username, $password) )
5834 return $this->error;
5836 if ( !current_user_can( 'edit_posts' ) )
5837 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5839 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5840 do_action( 'xmlrpc_call', 'mt.getCategoryList' );
5842 $categories_struct = array();
5844 if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
5845 foreach ( $cats as $cat ) {
5847 $struct['categoryId'] = $cat->term_id;
5848 $struct['categoryName'] = $cat->name;
5850 $categories_struct[] = $struct;
5854 return $categories_struct;
5858 * Retrieve post categories.
5862 * @param array $args {
5863 * Method arguments. Note: arguments must be ordered as documented.
5865 * @type int $post_ID
5866 * @type string $username
5867 * @type string $password
5869 * @return array|IXR_Error
5871 public function mt_getPostCategories( $args ) {
5872 $this->escape( $args );
5874 $post_ID = (int) $args[0];
5875 $username = $args[1];
5876 $password = $args[2];
5878 if ( !$user = $this->login($username, $password) )
5879 return $this->error;
5881 if ( ! get_post( $post_ID ) )
5882 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5884 if ( !current_user_can( 'edit_post', $post_ID ) )
5885 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
5887 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5888 do_action( 'xmlrpc_call', 'mt.getPostCategories' );
5890 $categories = array();
5891 $catids = wp_get_post_categories(intval($post_ID));
5892 // first listed category will be the primary category
5894 foreach ( $catids as $catid ) {
5895 $categories[] = array(
5896 'categoryName' => get_cat_name($catid),
5897 'categoryId' => (string) $catid,
5898 'isPrimary' => $isPrimary
5907 * Sets categories for a post.
5911 * @param array $args {
5912 * Method arguments. Note: arguments must be ordered as documented.
5914 * @type int $post_ID
5915 * @type string $username
5916 * @type string $password
5917 * @type array $categories
5919 * @return true|IXR_Error True on success.
5921 public function mt_setPostCategories( $args ) {
5922 $this->escape( $args );
5924 $post_ID = (int) $args[0];
5925 $username = $args[1];
5926 $password = $args[2];
5927 $categories = $args[3];
5929 if ( !$user = $this->login($username, $password) )
5930 return $this->error;
5932 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5933 do_action( 'xmlrpc_call', 'mt.setPostCategories' );
5935 if ( ! get_post( $post_ID ) )
5936 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5938 if ( !current_user_can('edit_post', $post_ID) )
5939 return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
5942 foreach ( $categories as $cat ) {
5943 $catids[] = $cat['categoryId'];
5946 wp_set_post_categories($post_ID, $catids);
5952 * Retrieve an array of methods supported by this server.
5958 public function mt_supportedMethods() {
5959 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5960 do_action( 'xmlrpc_call', 'mt.supportedMethods' );
5962 return array_keys( $this->methods );
5966 * Retrieve an empty array because we don't support per-post text filters.
5970 public function mt_supportedTextFilters() {
5971 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5972 do_action( 'xmlrpc_call', 'mt.supportedTextFilters' );
5975 * Filter the MoveableType text filters list for XML-RPC.
5979 * @param array $filters An array of text filters.
5981 return apply_filters( 'xmlrpc_text_filters', array() );
5985 * Retrieve trackbacks sent to a given post.
5989 * @global wpdb $wpdb
5991 * @param int $post_ID
5992 * @return array|IXR_Error
5994 public function mt_getTrackbackPings( $post_ID ) {
5997 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5998 do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
6000 $actual_post = get_post($post_ID, ARRAY_A);
6002 if ( !$actual_post )
6003 return new IXR_Error(404, __('Sorry, no such post.'));
6005 $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
6010 $trackback_pings = array();
6011 foreach ( $comments as $comment ) {
6012 if ( 'trackback' == $comment->comment_type ) {
6013 $content = $comment->comment_content;
6014 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
6015 $trackback_pings[] = array(
6016 'pingTitle' => $title,
6017 'pingURL' => $comment->comment_author_url,
6018 'pingIP' => $comment->comment_author_IP
6023 return $trackback_pings;
6027 * Sets a post's publish status to 'publish'.
6031 * @param array $args {
6032 * Method arguments. Note: arguments must be ordered as documented.
6034 * @type int $post_ID
6035 * @type string $username
6036 * @type string $password
6038 * @return int|IXR_Error
6040 public function mt_publishPost( $args ) {
6041 $this->escape( $args );
6043 $post_ID = (int) $args[0];
6044 $username = $args[1];
6045 $password = $args[2];
6047 if ( !$user = $this->login($username, $password) )
6048 return $this->error;
6050 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6051 do_action( 'xmlrpc_call', 'mt.publishPost' );
6053 $postdata = get_post($post_ID, ARRAY_A);
6055 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
6057 if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
6058 return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
6060 $postdata['post_status'] = 'publish';
6063 $cats = wp_get_post_categories($post_ID);
6064 $postdata['post_category'] = $cats;
6065 $this->escape($postdata);
6067 return wp_update_post( $postdata );
6070 /* PingBack functions
6071 * specs on www.hixie.ch/specs/pingback/pingback
6075 * Retrieves a pingback and registers it.
6079 * @global wpdb $wpdb
6080 * @global string $wp_version
6082 * @param array $args {
6083 * Method arguments. Note: arguments must be ordered as documented.
6085 * @type string $pagelinkedfrom
6086 * @type string $pagelinkedto
6088 * @return string|IXR_Error
6090 public function pingback_ping( $args ) {
6091 global $wpdb, $wp_version;
6093 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6094 do_action( 'xmlrpc_call', 'pingback.ping' );
6096 $this->escape( $args );
6098 $pagelinkedfrom = str_replace( '&', '&', $args[0] );
6099 $pagelinkedto = str_replace( '&', '&', $args[1] );
6100 $pagelinkedto = str_replace( '&', '&', $pagelinkedto );
6103 * Filter the pingback source URI.
6107 * @param string $pagelinkedfrom URI of the page linked from.
6108 * @param string $pagelinkedto URI of the page linked to.
6110 $pagelinkedfrom = apply_filters( 'pingback_ping_source_uri', $pagelinkedfrom, $pagelinkedto );
6112 if ( ! $pagelinkedfrom )
6113 return $this->pingback_error( 0, __( 'A valid URL was not provided.' ) );
6115 // Check if the page linked to is in our site
6116 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
6118 return $this->pingback_error( 0, __( 'Is there no link to us?' ) );
6120 // let's find which post is linked to
6121 // FIXME: does url_to_postid() cover all these cases already?
6122 // if so, then let's use it and drop the old code.
6123 $urltest = parse_url($pagelinkedto);
6124 if ( $post_ID = url_to_postid($pagelinkedto) ) {
6126 } elseif ( isset( $urltest['path'] ) && preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
6127 // the path defines the post_ID (archives/p/XXXX)
6128 $blah = explode('/', $match[0]);
6129 $post_ID = (int) $blah[1];
6130 } elseif ( isset( $urltest['query'] ) && preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
6131 // the querystring defines the post_ID (?p=XXXX)
6132 $blah = explode('=', $match[0]);
6133 $post_ID = (int) $blah[1];
6134 } elseif ( isset($urltest['fragment']) ) {
6135 // an #anchor is there, it's either...
6136 if ( intval($urltest['fragment']) ) {
6137 // ...an integer #XXXX (simplest case)
6138 $post_ID = (int) $urltest['fragment'];
6139 } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
6140 // ...a post id in the form 'post-###'
6141 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
6142 } elseif ( is_string($urltest['fragment']) ) {
6143 // ...or a string #title, a little more complicated
6144 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
6145 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
6146 if (! ($post_ID = $wpdb->get_var($sql)) ) {
6147 // returning unknown error '0' is better than die()ing
6148 return $this->pingback_error( 0, '' );
6152 // TODO: Attempt to extract a post ID from the given URL
6153 return $this->pingback_error( 33, __('The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
6155 $post_ID = (int) $post_ID;
6157 $post = get_post($post_ID);
6159 if ( !$post ) // Post_ID not found
6160 return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
6162 if ( $post_ID == url_to_postid($pagelinkedfrom) )
6163 return $this->pingback_error( 0, __( 'The source URL and the target URL cannot both point to the same resource.' ) );
6165 // Check if pings are on
6166 if ( !pings_open($post) )
6167 return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
6169 // Let's check that the remote site didn't already pingback this entry
6170 if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
6171 return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
6173 // very stupid, but gives time to the 'from' server to publish !
6176 $remote_ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
6178 /** This filter is documented in wp-includes/class-http.php */
6179 $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) );
6181 // Let's check the remote site
6182 $http_api_args = array(
6185 'limit_response_size' => 153600, // 150 KB
6186 'user-agent' => "$user_agent; verifying pingback from $remote_ip",
6188 'X-Pingback-Forwarded-For' => $remote_ip,
6191 $request = wp_safe_remote_get( $pagelinkedfrom, $http_api_args );
6192 $linea = wp_remote_retrieve_body( $request );
6195 return $this->pingback_error( 16, __( 'The source URL does not exist.' ) );
6198 * Filter the pingback remote source.
6202 * @param string $linea Response object for the page linked from.
6203 * @param string $pagelinkedto URL of the page linked to.
6205 $linea = apply_filters( 'pre_remote_source', $linea, $pagelinkedto );
6207 // Work around bug in strip_tags():
6208 $linea = str_replace('<!DOC', '<DOC', $linea);
6209 $linea = preg_replace( '/[\r\n\t ]+/', ' ', $linea ); // normalize spaces
6210 $linea = preg_replace( "/<\/*(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
6212 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
6213 $title = $matchtitle[1];
6214 if ( empty( $title ) )
6215 return $this->pingback_error( 32, __('We cannot find a title on that page.' ) );
6217 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
6219 $p = explode( "\n\n", $linea );
6221 $preg_target = preg_quote($pagelinkedto, '|');
6223 foreach ( $p as $para ) {
6224 if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
6225 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
6227 // If the URL isn't in a link context, keep looking
6228 if ( empty($context) )
6231 // We're going to use this fake tag to mark the context in a bit
6232 // the marker is needed in case the link text appears more than once in the paragraph
6233 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
6235 // prevent really long link text
6236 if ( strlen($context[1]) > 100 )
6237 $context[1] = substr($context[1], 0, 100) . '…';
6239 $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
6240 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
6241 $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
6242 $excerpt = trim($excerpt);
6243 $preg_marker = preg_quote($marker, '|');
6244 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
6245 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
6250 if ( empty($context) ) // Link to target not found
6251 return $this->pingback_error( 17, __( 'The source URL does not contain a link to the target URL, and so cannot be used as a source.' ) );
6253 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
6255 $context = '[…] ' . esc_html( $excerpt ) . ' […]';
6256 $pagelinkedfrom = $this->escape( $pagelinkedfrom );
6258 $comment_post_ID = (int) $post_ID;
6259 $comment_author = $title;
6260 $comment_author_email = '';
6261 $this->escape($comment_author);
6262 $comment_author_url = $pagelinkedfrom;
6263 $comment_content = $context;
6264 $this->escape($comment_content);
6265 $comment_type = 'pingback';
6267 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
6269 $comment_ID = wp_new_comment($commentdata);
6272 * Fires after a post pingback has been sent.
6276 * @param int $comment_ID Comment ID.
6278 do_action( 'pingback_post', $comment_ID );
6280 return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
6284 * Retrieve array of URLs that pingbacked the given URL.
6286 * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
6290 * @global wpdb $wpdb
6292 * @param string $url
6293 * @return array|IXR_Error
6295 public function pingback_extensions_getPingbacks( $url ) {
6298 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6299 do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' );
6301 $url = $this->escape( $url );
6303 $post_ID = url_to_postid($url);
6305 // We aren't sure that the resource is available and/or pingback enabled
6306 return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
6309 $actual_post = get_post($post_ID, ARRAY_A);
6311 if ( !$actual_post ) {
6312 // No such post = resource not found
6313 return $this->pingback_error( 32, __('The specified target URL does not exist.' ) );
6316 $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
6321 $pingbacks = array();
6322 foreach ( $comments as $comment ) {
6323 if ( 'pingback' == $comment->comment_type )
6324 $pingbacks[] = $comment->comment_author_url;
6331 * @param integer $code
6332 * @param string $message
6335 protected function pingback_error( $code, $message ) {
6337 * Filter the XML-RPC pingback error return.
6341 * @param IXR_Error $error An IXR_Error object containing the error code and message.
6343 return apply_filters( 'xmlrpc_pingback_error', new IXR_Error( $code, $message ) );