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 {@see 'xmlrpc_enabled'} filter found in wp_xmlrpc_server::login().
20 * @subpackage Publishing
23 class wp_xmlrpc_server extends IXR_Server {
49 * Flags that the user authentication has failed in this instance of wp_xmlrpc_server.
54 protected $auth_failed = false;
57 * Registers all of the XMLRPC methods that XMLRPC server understands.
59 * Sets up server and method property. Passes XMLRPC
60 * methods through the {@see 'xmlrpc_methods'} filter to allow plugins to extend
61 * or replace XML-RPC methods.
65 public function __construct() {
66 $this->methods = array(
68 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
69 'wp.newPost' => 'this:wp_newPost',
70 'wp.editPost' => 'this:wp_editPost',
71 'wp.deletePost' => 'this:wp_deletePost',
72 'wp.getPost' => 'this:wp_getPost',
73 'wp.getPosts' => 'this:wp_getPosts',
74 'wp.newTerm' => 'this:wp_newTerm',
75 'wp.editTerm' => 'this:wp_editTerm',
76 'wp.deleteTerm' => 'this:wp_deleteTerm',
77 'wp.getTerm' => 'this:wp_getTerm',
78 'wp.getTerms' => 'this:wp_getTerms',
79 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
80 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
81 'wp.getUser' => 'this:wp_getUser',
82 'wp.getUsers' => 'this:wp_getUsers',
83 'wp.getProfile' => 'this:wp_getProfile',
84 'wp.editProfile' => 'this:wp_editProfile',
85 'wp.getPage' => 'this:wp_getPage',
86 'wp.getPages' => 'this:wp_getPages',
87 'wp.newPage' => 'this:wp_newPage',
88 'wp.deletePage' => 'this:wp_deletePage',
89 'wp.editPage' => 'this:wp_editPage',
90 'wp.getPageList' => 'this:wp_getPageList',
91 'wp.getAuthors' => 'this:wp_getAuthors',
92 'wp.getCategories' => 'this:mw_getCategories', // Alias
93 'wp.getTags' => 'this:wp_getTags',
94 'wp.newCategory' => 'this:wp_newCategory',
95 'wp.deleteCategory' => 'this:wp_deleteCategory',
96 'wp.suggestCategories' => 'this:wp_suggestCategories',
97 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
98 'wp.deleteFile' => 'this:wp_deletePost', // Alias
99 'wp.getCommentCount' => 'this:wp_getCommentCount',
100 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
101 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
102 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
103 'wp.getOptions' => 'this:wp_getOptions',
104 'wp.setOptions' => 'this:wp_setOptions',
105 'wp.getComment' => 'this:wp_getComment',
106 'wp.getComments' => 'this:wp_getComments',
107 'wp.deleteComment' => 'this:wp_deleteComment',
108 'wp.editComment' => 'this:wp_editComment',
109 'wp.newComment' => 'this:wp_newComment',
110 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
111 'wp.getMediaItem' => 'this:wp_getMediaItem',
112 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
113 'wp.getPostFormats' => 'this:wp_getPostFormats',
114 'wp.getPostType' => 'this:wp_getPostType',
115 'wp.getPostTypes' => 'this:wp_getPostTypes',
116 'wp.getRevisions' => 'this:wp_getRevisions',
117 'wp.restoreRevision' => 'this:wp_restoreRevision',
120 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
121 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
122 'blogger.getPost' => 'this:blogger_getPost',
123 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
124 'blogger.newPost' => 'this:blogger_newPost',
125 'blogger.editPost' => 'this:blogger_editPost',
126 'blogger.deletePost' => 'this:blogger_deletePost',
128 // MetaWeblog API (with MT extensions to structs)
129 'metaWeblog.newPost' => 'this:mw_newPost',
130 'metaWeblog.editPost' => 'this:mw_editPost',
131 'metaWeblog.getPost' => 'this:mw_getPost',
132 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
133 'metaWeblog.getCategories' => 'this:mw_getCategories',
134 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
136 // MetaWeblog API aliases for Blogger API
137 // see http://www.xmlrpc.com/stories/storyReader$2460
138 'metaWeblog.deletePost' => 'this:blogger_deletePost',
139 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
142 'mt.getCategoryList' => 'this:mt_getCategoryList',
143 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
144 'mt.getPostCategories' => 'this:mt_getPostCategories',
145 'mt.setPostCategories' => 'this:mt_setPostCategories',
146 'mt.supportedMethods' => 'this:mt_supportedMethods',
147 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
148 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
149 'mt.publishPost' => 'this:mt_publishPost',
152 'pingback.ping' => 'this:pingback_ping',
153 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
155 'demo.sayHello' => 'this:sayHello',
156 'demo.addTwoNumbers' => 'this:addTwoNumbers'
159 $this->initialise_blog_option_info();
162 * Filters the methods exposed by the XML-RPC server.
164 * This filter can be used to add new methods, and remove built-in methods.
168 * @param array $methods An array of XML-RPC methods.
170 $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
174 * Make private/protected methods readable for backward compatibility.
179 * @param callable $name Method to call.
180 * @param array $arguments Arguments to pass when calling.
181 * @return array|IXR_Error|false Return value of the callback, false otherwise.
183 public function __call( $name, $arguments ) {
184 if ( '_multisite_getUsersBlogs' === $name ) {
185 return call_user_func_array( array( $this, $name ), $arguments );
191 * Serves the XML-RPC request.
196 public function serve_request() {
197 $this->IXR_Server($this->methods);
201 * Test XMLRPC API by saying, "Hello!" to client.
205 * @return string Hello string response.
207 public function sayHello() {
212 * Test XMLRPC API by adding two numbers for client.
216 * @param array $args {
217 * Method arguments. Note: arguments must be ordered as documented.
219 * @type int $number1 A number to add.
220 * @type int $number2 A second number to add.
222 * @return int Sum of the two given numbers.
224 public function addTwoNumbers( $args ) {
227 return $number1 + $number2;
235 * @param string $username User's username.
236 * @param string $password User's password.
237 * @return WP_User|bool WP_User object if authentication passed, false otherwise
239 public function login( $username, $password ) {
241 * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
242 * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
244 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
245 if ( false === $enabled ) {
246 $enabled = apply_filters( 'option_enable_xmlrpc', true );
250 * Filters whether XML-RPC methods requiring authentication are enabled.
252 * Contrary to the way it's named, this filter does not control whether XML-RPC is *fully*
253 * enabled, rather, it only controls whether XML-RPC methods requiring authentication - such
254 * as for publishing purposes - are enabled.
256 * Further, the filter does not control whether pingbacks or other custom endpoints that don't
257 * require authentication are enabled. This behavior is expected, and due to how parity was matched
258 * with the `enable_xmlrpc` UI option the filter replaced when it was introduced in 3.5.
260 * To disable XML-RPC methods that require authentication, use:
262 * add_filter( 'xmlrpc_enabled', '__return_false' );
264 * For more granular control over all XML-RPC methods and requests, see the {@see 'xmlrpc_methods'}
265 * and {@see 'xmlrpc_element_limit'} hooks.
269 * @param bool $enabled Whether XML-RPC is enabled. Default true.
271 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
274 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
278 if ( $this->auth_failed ) {
279 $user = new WP_Error( 'login_prevented' );
281 $user = wp_authenticate( $username, $password );
284 if ( is_wp_error( $user ) ) {
285 $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
287 // Flag that authentication has failed once on this wp_xmlrpc_server instance
288 $this->auth_failed = true;
291 * Filters the XML-RPC user login error message.
295 * @param string $error The XML-RPC error message.
296 * @param WP_User $user WP_User object.
298 $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
302 wp_set_current_user( $user->ID );
307 * Check user's credentials. Deprecated.
310 * @deprecated 2.8.0 Use wp_xmlrpc_server::login()
311 * @see wp_xmlrpc_server::login()
313 * @param string $username User's username.
314 * @param string $password User's password.
315 * @return bool Whether authentication passed.
317 public function login_pass_ok( $username, $password ) {
318 return (bool) $this->login( $username, $password );
322 * Escape string or array of strings for database.
326 * @param string|array $data Escape single string or array of strings.
327 * @return string|void Returns with string is passed, alters by-reference
328 * when array is passed.
330 public function escape( &$data ) {
331 if ( ! is_array( $data ) )
332 return wp_slash( $data );
334 foreach ( $data as &$v ) {
335 if ( is_array( $v ) )
337 elseif ( ! is_object( $v ) )
343 * Retrieve custom fields for post.
347 * @param int $post_id Post ID.
348 * @return array Custom fields, if exist.
350 public function get_custom_fields($post_id) {
351 $post_id = (int) $post_id;
353 $custom_fields = array();
355 foreach ( (array) has_meta($post_id) as $meta ) {
356 // Don't expose protected fields.
357 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
360 $custom_fields[] = array(
361 "id" => $meta['meta_id'],
362 "key" => $meta['meta_key'],
363 "value" => $meta['meta_value']
367 return $custom_fields;
371 * Set custom fields for post.
375 * @param int $post_id Post ID.
376 * @param array $fields Custom fields.
378 public function set_custom_fields($post_id, $fields) {
379 $post_id = (int) $post_id;
381 foreach ( (array) $fields as $meta ) {
382 if ( isset($meta['id']) ) {
383 $meta['id'] = (int) $meta['id'];
384 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
385 if ( isset($meta['key']) ) {
386 $meta['key'] = wp_unslash( $meta['key'] );
387 if ( $meta['key'] !== $pmeta->meta_key )
389 $meta['value'] = wp_unslash( $meta['value'] );
390 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
391 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
392 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
393 delete_metadata_by_mid( 'post', $meta['id'] );
395 } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
396 add_post_meta( $post_id, $meta['key'], $meta['value'] );
402 * Set up blog options property.
404 * Passes property through {@see 'xmlrpc_blog_options'} filter.
408 public function initialise_blog_option_info() {
409 $this->blog_options = array(
411 'software_name' => array(
412 'desc' => __( 'Software Name' ),
414 'value' => 'WordPress'
416 'software_version' => array(
417 'desc' => __( 'Software Version' ),
419 'value' => get_bloginfo( 'version' )
422 'desc' => __( 'WordPress Address (URL)' ),
424 'option' => 'siteurl'
427 'desc' => __( 'Site Address (URL)' ),
431 'login_url' => array(
432 'desc' => __( 'Login Address (URL)' ),
434 'value' => wp_login_url( )
436 'admin_url' => array(
437 'desc' => __( 'The URL to the admin area' ),
439 'value' => get_admin_url( )
441 'image_default_link_type' => array(
442 'desc' => __( 'Image default link type' ),
444 'option' => 'image_default_link_type'
446 'image_default_size' => array(
447 'desc' => __( 'Image default size' ),
449 'option' => 'image_default_size'
451 'image_default_align' => array(
452 'desc' => __( 'Image default align' ),
454 'option' => 'image_default_align'
457 'desc' => __( 'Template' ),
459 'option' => 'template'
461 'stylesheet' => array(
462 'desc' => __( 'Stylesheet' ),
464 'option' => 'stylesheet'
466 'post_thumbnail' => array(
467 'desc' => __('Post Thumbnail'),
469 'value' => current_theme_supports( 'post-thumbnails' )
473 'time_zone' => array(
474 'desc' => __( 'Time Zone' ),
476 'option' => 'gmt_offset'
478 'blog_title' => array(
479 'desc' => __( 'Site Title' ),
481 'option' => 'blogname'
483 'blog_tagline' => array(
484 'desc' => __( 'Site Tagline' ),
486 'option' => 'blogdescription'
488 'date_format' => array(
489 'desc' => __( 'Date Format' ),
491 'option' => 'date_format'
493 'time_format' => array(
494 'desc' => __( 'Time Format' ),
496 'option' => 'time_format'
498 'users_can_register' => array(
499 'desc' => __( 'Allow new users to sign up' ),
501 'option' => 'users_can_register'
503 'thumbnail_size_w' => array(
504 'desc' => __( 'Thumbnail Width' ),
506 'option' => 'thumbnail_size_w'
508 'thumbnail_size_h' => array(
509 'desc' => __( 'Thumbnail Height' ),
511 'option' => 'thumbnail_size_h'
513 'thumbnail_crop' => array(
514 'desc' => __( 'Crop thumbnail to exact dimensions' ),
516 'option' => 'thumbnail_crop'
518 'medium_size_w' => array(
519 'desc' => __( 'Medium size image width' ),
521 'option' => 'medium_size_w'
523 'medium_size_h' => array(
524 'desc' => __( 'Medium size image height' ),
526 'option' => 'medium_size_h'
528 'medium_large_size_w' => array(
529 'desc' => __( 'Medium-Large size image width' ),
531 'option' => 'medium_large_size_w'
533 'medium_large_size_h' => array(
534 'desc' => __( 'Medium-Large size image height' ),
536 'option' => 'medium_large_size_h'
538 'large_size_w' => array(
539 'desc' => __( 'Large size image width' ),
541 'option' => 'large_size_w'
543 'large_size_h' => array(
544 'desc' => __( 'Large size image height' ),
546 'option' => 'large_size_h'
548 'default_comment_status' => array(
549 'desc' => __( 'Allow people to post comments on new articles' ),
551 'option' => 'default_comment_status'
553 'default_ping_status' => array(
554 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles' ),
556 'option' => 'default_ping_status'
561 * Filters the XML-RPC blog options property.
565 * @param array $blog_options An array of XML-RPC blog options.
567 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
571 * Retrieve the blogs of the user.
575 * @param array $args {
576 * Method arguments. Note: arguments must be ordered as documented.
578 * @type string $username Username.
579 * @type string $password Password.
581 * @return array|IXR_Error Array contains:
583 * - 'isPrimary' - whether the blog is the user's primary blog
587 * - 'xmlrpc' - url of xmlrpc endpoint
589 public function wp_getUsersBlogs( $args ) {
590 if ( ! $this->minimum_args( $args, 2 ) ) {
594 // If this isn't on WPMU then just use blogger_getUsersBlogs
595 if ( !is_multisite() ) {
596 array_unshift( $args, 1 );
597 return $this->blogger_getUsersBlogs( $args );
600 $this->escape( $args );
602 $username = $args[0];
603 $password = $args[1];
605 if ( !$user = $this->login($username, $password) )
609 * Fires after the XML-RPC user has been authenticated but before the rest of
610 * the method logic begins.
612 * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
613 * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
617 * @param string $name The method name.
619 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
621 $blogs = (array) get_blogs_of_user( $user->ID );
623 $primary_blog_id = 0;
624 $active_blog = get_active_blog_for_user( $user->ID );
625 if ( $active_blog ) {
626 $primary_blog_id = (int) $active_blog->blog_id;
629 foreach ( $blogs as $blog ) {
630 // Don't include blogs that aren't hosted at this site.
631 if ( $blog->site_id != get_current_network_id() )
634 $blog_id = $blog->userblog_id;
636 switch_to_blog( $blog_id );
638 $is_admin = current_user_can( 'manage_options' );
639 $is_primary = ( (int) $blog_id === $primary_blog_id );
642 'isAdmin' => $is_admin,
643 'isPrimary' => $is_primary,
644 'url' => home_url( '/' ),
645 'blogid' => (string) $blog_id,
646 'blogName' => get_option( 'blogname' ),
647 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
650 restore_current_blog();
657 * Checks if the method received at least the minimum number of arguments.
662 * @param string|array $args Sanitize single string or array of strings.
663 * @param int $count Minimum number of arguments.
664 * @return bool if `$args` contains at least $count arguments.
666 protected function minimum_args( $args, $count ) {
667 if ( count( $args ) < $count ) {
668 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
676 * Prepares taxonomy data for return in an XML-RPC object.
680 * @param object $taxonomy The unprepared taxonomy data.
681 * @param array $fields The subset of taxonomy fields to return.
682 * @return array The prepared taxonomy data.
684 protected function _prepare_taxonomy( $taxonomy, $fields ) {
686 'name' => $taxonomy->name,
687 'label' => $taxonomy->label,
688 'hierarchical' => (bool) $taxonomy->hierarchical,
689 'public' => (bool) $taxonomy->public,
690 'show_ui' => (bool) $taxonomy->show_ui,
691 '_builtin' => (bool) $taxonomy->_builtin,
694 if ( in_array( 'labels', $fields ) )
695 $_taxonomy['labels'] = (array) $taxonomy->labels;
697 if ( in_array( 'cap', $fields ) )
698 $_taxonomy['cap'] = (array) $taxonomy->cap;
700 if ( in_array( 'menu', $fields ) )
701 $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
703 if ( in_array( 'object_type', $fields ) )
704 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
707 * Filters XML-RPC-prepared data for the given taxonomy.
711 * @param array $_taxonomy An array of taxonomy data.
712 * @param WP_Taxonomy $taxonomy Taxonomy object.
713 * @param array $fields The subset of taxonomy fields to return.
715 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
719 * Prepares term data for return in an XML-RPC object.
723 * @param array|object $term The unprepared term data.
724 * @return array The prepared term data.
726 protected function _prepare_term( $term ) {
728 if ( ! is_array( $_term ) )
729 $_term = get_object_vars( $_term );
731 // For integers which may be larger than XML-RPC supports ensure we return strings.
732 $_term['term_id'] = strval( $_term['term_id'] );
733 $_term['term_group'] = strval( $_term['term_group'] );
734 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
735 $_term['parent'] = strval( $_term['parent'] );
737 // Count we are happy to return as an integer because people really shouldn't use terms that much.
738 $_term['count'] = intval( $_term['count'] );
741 * Filters XML-RPC-prepared data for the given term.
745 * @param array $_term An array of term data.
746 * @param array|object $term Term object or array.
748 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
752 * Convert a WordPress date string to an IXR_Date object.
756 * @param string $date Date string to convert.
757 * @return IXR_Date IXR_Date object.
759 protected function _convert_date( $date ) {
760 if ( $date === '0000-00-00 00:00:00' ) {
761 return new IXR_Date( '00000000T00:00:00Z' );
763 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
767 * Convert a WordPress GMT date string to an IXR_Date object.
771 * @param string $date_gmt WordPress GMT date string.
772 * @param string $date Date string.
773 * @return IXR_Date IXR_Date object.
775 protected function _convert_date_gmt( $date_gmt, $date ) {
776 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
777 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
779 return $this->_convert_date( $date_gmt );
783 * Prepares post data for return in an XML-RPC object.
787 * @param array $post The unprepared post data.
788 * @param array $fields The subset of post type fields to return.
789 * @return array The prepared post data.
791 protected function _prepare_post( $post, $fields ) {
792 // Holds the data for this post. built up based on $fields.
793 $_post = array( 'post_id' => strval( $post['ID'] ) );
795 // Prepare common post fields.
796 $post_fields = array(
797 'post_title' => $post['post_title'],
798 'post_date' => $this->_convert_date( $post['post_date'] ),
799 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
800 'post_modified' => $this->_convert_date( $post['post_modified'] ),
801 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
802 'post_status' => $post['post_status'],
803 'post_type' => $post['post_type'],
804 'post_name' => $post['post_name'],
805 'post_author' => $post['post_author'],
806 'post_password' => $post['post_password'],
807 'post_excerpt' => $post['post_excerpt'],
808 'post_content' => $post['post_content'],
809 'post_parent' => strval( $post['post_parent'] ),
810 'post_mime_type' => $post['post_mime_type'],
811 'link' => get_permalink( $post['ID'] ),
812 'guid' => $post['guid'],
813 'menu_order' => intval( $post['menu_order'] ),
814 'comment_status' => $post['comment_status'],
815 'ping_status' => $post['ping_status'],
816 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
820 $post_fields['post_thumbnail'] = array();
821 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
822 if ( $thumbnail_id ) {
823 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
824 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
827 // Consider future posts as published.
828 if ( $post_fields['post_status'] === 'future' )
829 $post_fields['post_status'] = 'publish';
831 // Fill in blank post format.
832 $post_fields['post_format'] = get_post_format( $post['ID'] );
833 if ( empty( $post_fields['post_format'] ) )
834 $post_fields['post_format'] = 'standard';
836 // Merge requested $post_fields fields into $_post.
837 if ( in_array( 'post', $fields ) ) {
838 $_post = array_merge( $_post, $post_fields );
840 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
841 $_post = array_merge( $_post, $requested_fields );
844 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
846 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
847 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
848 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
849 $_post['terms'] = array();
850 foreach ( $terms as $term ) {
851 $_post['terms'][] = $this->_prepare_term( $term );
855 if ( in_array( 'custom_fields', $fields ) )
856 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
858 if ( in_array( 'enclosure', $fields ) ) {
859 $_post['enclosure'] = array();
860 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
861 if ( ! empty( $enclosures ) ) {
862 $encdata = explode( "\n", $enclosures[0] );
863 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
864 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
865 $_post['enclosure']['type'] = trim( $encdata[2] );
870 * Filters XML-RPC-prepared date for the given post.
874 * @param array $_post An array of modified post data.
875 * @param array $post An array of post data.
876 * @param array $fields An array of post fields.
878 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
882 * Prepares post data for return in an XML-RPC object.
885 * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
888 * @param WP_Post_Type $post_type Post type object.
889 * @param array $fields The subset of post fields to return.
890 * @return array The prepared post type data.
892 protected function _prepare_post_type( $post_type, $fields ) {
894 'name' => $post_type->name,
895 'label' => $post_type->label,
896 'hierarchical' => (bool) $post_type->hierarchical,
897 'public' => (bool) $post_type->public,
898 'show_ui' => (bool) $post_type->show_ui,
899 '_builtin' => (bool) $post_type->_builtin,
900 'has_archive' => (bool) $post_type->has_archive,
901 'supports' => get_all_post_type_supports( $post_type->name ),
904 if ( in_array( 'labels', $fields ) ) {
905 $_post_type['labels'] = (array) $post_type->labels;
908 if ( in_array( 'cap', $fields ) ) {
909 $_post_type['cap'] = (array) $post_type->cap;
910 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
913 if ( in_array( 'menu', $fields ) ) {
914 $_post_type['menu_position'] = (int) $post_type->menu_position;
915 $_post_type['menu_icon'] = $post_type->menu_icon;
916 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
919 if ( in_array( 'taxonomies', $fields ) )
920 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
923 * Filters XML-RPC-prepared date for the given post type.
926 * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
928 * @param array $_post_type An array of post type data.
929 * @param WP_Post_Type $post_type Post type object.
931 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
935 * Prepares media item data for return in an XML-RPC object.
939 * @param object $media_item The unprepared media item data.
940 * @param string $thumbnail_size The image size to use for the thumbnail URL.
941 * @return array The prepared media item data.
943 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
944 $_media_item = array(
945 'attachment_id' => strval( $media_item->ID ),
946 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
947 'parent' => $media_item->post_parent,
948 'link' => wp_get_attachment_url( $media_item->ID ),
949 'title' => $media_item->post_title,
950 'caption' => $media_item->post_excerpt,
951 'description' => $media_item->post_content,
952 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
953 'type' => $media_item->post_mime_type
956 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
957 if ( $thumbnail_src )
958 $_media_item['thumbnail'] = $thumbnail_src[0];
960 $_media_item['thumbnail'] = $_media_item['link'];
963 * Filters XML-RPC-prepared data for the given media item.
967 * @param array $_media_item An array of media item data.
968 * @param object $media_item Media item object.
969 * @param string $thumbnail_size Image size.
971 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
975 * Prepares page data for return in an XML-RPC object.
979 * @param object $page The unprepared page data.
980 * @return array The prepared page data.
982 protected function _prepare_page( $page ) {
983 // Get all of the page content and link.
984 $full_page = get_extended( $page->post_content );
985 $link = get_permalink( $page->ID );
987 // Get info the page parent if there is one.
989 if ( ! empty( $page->post_parent ) ) {
990 $parent = get_post( $page->post_parent );
991 $parent_title = $parent->post_title;
994 // Determine comment and ping settings.
995 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
996 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
999 $page_date = $this->_convert_date( $page->post_date );
1000 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
1002 // Pull the categories info together.
1003 $categories = array();
1004 if ( is_object_in_taxonomy( 'page', 'category' ) ) {
1005 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
1006 $categories[] = get_cat_name( $cat_id );
1010 // Get the author info.
1011 $author = get_userdata( $page->post_author );
1013 $page_template = get_page_template_slug( $page->ID );
1014 if ( empty( $page_template ) )
1015 $page_template = 'default';
1018 'dateCreated' => $page_date,
1019 'userid' => $page->post_author,
1020 'page_id' => $page->ID,
1021 'page_status' => $page->post_status,
1022 'description' => $full_page['main'],
1023 'title' => $page->post_title,
1025 'permaLink' => $link,
1026 'categories' => $categories,
1027 'excerpt' => $page->post_excerpt,
1028 'text_more' => $full_page['extended'],
1029 'mt_allow_comments' => $allow_comments,
1030 'mt_allow_pings' => $allow_pings,
1031 'wp_slug' => $page->post_name,
1032 'wp_password' => $page->post_password,
1033 'wp_author' => $author->display_name,
1034 'wp_page_parent_id' => $page->post_parent,
1035 'wp_page_parent_title' => $parent_title,
1036 'wp_page_order' => $page->menu_order,
1037 'wp_author_id' => (string) $author->ID,
1038 'wp_author_display_name' => $author->display_name,
1039 'date_created_gmt' => $page_date_gmt,
1040 'custom_fields' => $this->get_custom_fields( $page->ID ),
1041 'wp_page_template' => $page_template
1045 * Filters XML-RPC-prepared data for the given page.
1049 * @param array $_page An array of page data.
1050 * @param WP_Post $page Page object.
1052 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
1056 * Prepares comment data for return in an XML-RPC object.
1060 * @param object $comment The unprepared comment data.
1061 * @return array The prepared comment data.
1063 protected function _prepare_comment( $comment ) {
1064 // Format page date.
1065 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
1067 if ( '0' == $comment->comment_approved ) {
1068 $comment_status = 'hold';
1069 } elseif ( 'spam' == $comment->comment_approved ) {
1070 $comment_status = 'spam';
1071 } elseif ( '1' == $comment->comment_approved ) {
1072 $comment_status = 'approve';
1074 $comment_status = $comment->comment_approved;
1077 'date_created_gmt' => $comment_date_gmt,
1078 'user_id' => $comment->user_id,
1079 'comment_id' => $comment->comment_ID,
1080 'parent' => $comment->comment_parent,
1081 'status' => $comment_status,
1082 'content' => $comment->comment_content,
1083 'link' => get_comment_link($comment),
1084 'post_id' => $comment->comment_post_ID,
1085 'post_title' => get_the_title($comment->comment_post_ID),
1086 'author' => $comment->comment_author,
1087 'author_url' => $comment->comment_author_url,
1088 'author_email' => $comment->comment_author_email,
1089 'author_ip' => $comment->comment_author_IP,
1090 'type' => $comment->comment_type,
1094 * Filters XML-RPC-prepared data for the given comment.
1098 * @param array $_comment An array of prepared comment data.
1099 * @param WP_Comment $comment Comment object.
1101 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
1105 * Prepares user data for return in an XML-RPC object.
1109 * @param WP_User $user The unprepared user object.
1110 * @param array $fields The subset of user fields to return.
1111 * @return array The prepared user data.
1113 protected function _prepare_user( $user, $fields ) {
1114 $_user = array( 'user_id' => strval( $user->ID ) );
1116 $user_fields = array(
1117 'username' => $user->user_login,
1118 'first_name' => $user->user_firstname,
1119 'last_name' => $user->user_lastname,
1120 'registered' => $this->_convert_date( $user->user_registered ),
1121 'bio' => $user->user_description,
1122 'email' => $user->user_email,
1123 'nickname' => $user->nickname,
1124 'nicename' => $user->user_nicename,
1125 'url' => $user->user_url,
1126 'display_name' => $user->display_name,
1127 'roles' => $user->roles,
1130 if ( in_array( 'all', $fields ) ) {
1131 $_user = array_merge( $_user, $user_fields );
1133 if ( in_array( 'basic', $fields ) ) {
1134 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
1135 $fields = array_merge( $fields, $basic_fields );
1137 $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
1138 $_user = array_merge( $_user, $requested_fields );
1142 * Filters XML-RPC-prepared data for the given user.
1146 * @param array $_user An array of user data.
1147 * @param WP_User $user User object.
1148 * @param array $fields An array of user fields.
1150 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
1154 * Create a new post for any registered post type.
1158 * @link https://en.wikipedia.org/wiki/RSS_enclosure for information on RSS enclosures.
1160 * @param array $args {
1161 * Method arguments. Note: top-level arguments must be ordered as documented.
1163 * @type int $blog_id Blog ID (unused).
1164 * @type string $username Username.
1165 * @type string $password Password.
1166 * @type array $content_struct {
1167 * Content struct for adding a new post. See wp_insert_post() for information on
1168 * additional post fields
1170 * @type string $post_type Post type. Default 'post'.
1171 * @type string $post_status Post status. Default 'draft'
1172 * @type string $post_title Post title.
1173 * @type int $post_author Post author ID.
1174 * @type string $post_excerpt Post excerpt.
1175 * @type string $post_content Post content.
1176 * @type string $post_date_gmt Post date in GMT.
1177 * @type string $post_date Post date.
1178 * @type string $post_password Post password (20-character limit).
1179 * @type string $comment_status Post comment enabled status. Accepts 'open' or 'closed'.
1180 * @type string $ping_status Post ping status. Accepts 'open' or 'closed'.
1181 * @type bool $sticky Whether the post should be sticky. Automatically false if
1182 * `$post_status` is 'private'.
1183 * @type int $post_thumbnail ID of an image to use as the post thumbnail/featured image.
1184 * @type array $custom_fields Array of meta key/value pairs to add to the post.
1185 * @type array $terms Associative array with taxonomy names as keys and arrays
1186 * of term IDs as values.
1187 * @type array $terms_names Associative array with taxonomy names as keys and arrays
1188 * of term names as values.
1189 * @type array $enclosure {
1190 * Array of feed enclosure data to add to post meta.
1192 * @type string $url URL for the feed enclosure.
1193 * @type int $length Size in bytes of the enclosure.
1194 * @type string $type Mime-type for the enclosure.
1198 * @return int|IXR_Error Post ID on success, IXR_Error instance otherwise.
1200 public function wp_newPost( $args ) {
1201 if ( ! $this->minimum_args( $args, 4 ) )
1202 return $this->error;
1204 $this->escape( $args );
1206 $username = $args[1];
1207 $password = $args[2];
1208 $content_struct = $args[3];
1210 if ( ! $user = $this->login( $username, $password ) )
1211 return $this->error;
1213 // convert the date field back to IXR form
1214 if ( isset( $content_struct['post_date'] ) && ! ( $content_struct['post_date'] instanceof IXR_Date ) ) {
1215 $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
1218 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1219 // since _insert_post will ignore the non-GMT date if the GMT date is set
1220 if ( isset( $content_struct['post_date_gmt'] ) && ! ( $content_struct['post_date_gmt'] instanceof IXR_Date ) ) {
1221 if ( $content_struct['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) {
1222 unset( $content_struct['post_date_gmt'] );
1224 $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
1228 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1229 do_action( 'xmlrpc_call', 'wp.newPost' );
1231 unset( $content_struct['ID'] );
1233 return $this->_insert_post( $user, $content_struct );
1237 * Helper method for filtering out elements from an array.
1241 * @param int $count Number to compare to one.
1243 private function _is_greater_than_one( $count ) {
1248 * Encapsulate the logic for sticking a post
1249 * and determining if the user has permission to do so
1254 * @param array $post_data
1255 * @param bool $update
1256 * @return void|IXR_Error
1258 private function _toggle_sticky( $post_data, $update = false ) {
1259 $post_type = get_post_type_object( $post_data['post_type'] );
1261 // Private and password-protected posts cannot be stickied.
1262 if ( 'private' === $post_data['post_status'] || ! empty( $post_data['post_password'] ) ) {
1263 // Error if the client tried to stick the post, otherwise, silently unstick.
1264 if ( ! empty( $post_data['sticky'] ) ) {
1265 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1269 unstick_post( $post_data['ID'] );
1271 } elseif ( isset( $post_data['sticky'] ) ) {
1272 if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) {
1273 return new IXR_Error( 401, __( 'Sorry, you are not allowed to make posts sticky.' ) );
1276 $sticky = wp_validate_boolean( $post_data['sticky'] );
1278 stick_post( $post_data['ID'] );
1280 unstick_post( $post_data['ID'] );
1286 * Helper method for wp_newPost() and wp_editPost(), containing shared logic.
1291 * @see wp_insert_post()
1293 * @param WP_User $user The post author if post_author isn't set in $content_struct.
1294 * @param array|IXR_Error $content_struct Post data to insert.
1295 * @return IXR_Error|string
1297 protected function _insert_post( $user, $content_struct ) {
1298 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1299 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1301 $post_data = wp_parse_args( $content_struct, $defaults );
1303 $post_type = get_post_type_object( $post_data['post_type'] );
1305 return new IXR_Error( 403, __( 'Invalid post type.' ) );
1307 $update = ! empty( $post_data['ID'] );
1310 if ( ! get_post( $post_data['ID'] ) )
1311 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1312 if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1313 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1314 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1315 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1317 if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1318 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1321 switch ( $post_data['post_status'] ) {
1326 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1327 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type.' ) );
1331 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1332 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type.' ) );
1335 if ( ! get_post_status_object( $post_data['post_status'] ) )
1336 $post_data['post_status'] = 'draft';
1340 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1341 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type.' ) );
1343 $post_data['post_author'] = absint( $post_data['post_author'] );
1344 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1345 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1346 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create posts as this user.' ) );
1348 $author = get_userdata( $post_data['post_author'] );
1351 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1353 $post_data['post_author'] = $user->ID;
1356 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1357 unset( $post_data['comment_status'] );
1359 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1360 unset( $post_data['ping_status'] );
1362 // Do some timestamp voodoo.
1363 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1364 // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
1365 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1366 } elseif ( ! empty( $post_data['post_date'] ) ) {
1367 $dateCreated = $post_data['post_date']->getIso();
1370 // Default to not flagging the post date to be edited unless it's intentional.
1371 $post_data['edit_date'] = false;
1373 if ( ! empty( $dateCreated ) ) {
1374 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1375 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1377 // Flag the post date to be edited.
1378 $post_data['edit_date'] = true;
1381 if ( ! isset( $post_data['ID'] ) )
1382 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1383 $post_ID = $post_data['ID'];
1385 if ( $post_data['post_type'] == 'post' ) {
1386 $error = $this->_toggle_sticky( $post_data, $update );
1392 if ( isset( $post_data['post_thumbnail'] ) ) {
1393 // empty value deletes, non-empty value adds/updates.
1394 if ( ! $post_data['post_thumbnail'] )
1395 delete_post_thumbnail( $post_ID );
1396 elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1397 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1398 set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1399 unset( $content_struct['post_thumbnail'] );
1402 if ( isset( $post_data['custom_fields'] ) )
1403 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1405 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1406 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1408 // Accumulate term IDs from terms and terms_names.
1411 // First validate the terms specified by ID.
1412 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1413 $taxonomies = array_keys( $post_data['terms'] );
1415 // Validating term ids.
1416 foreach ( $taxonomies as $taxonomy ) {
1417 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1418 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1420 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1421 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1423 $term_ids = $post_data['terms'][$taxonomy];
1424 $terms[ $taxonomy ] = array();
1425 foreach ( $term_ids as $term_id ) {
1426 $term = get_term_by( 'id', $term_id, $taxonomy );
1429 return new IXR_Error( 403, __( 'Invalid term ID.' ) );
1431 $terms[$taxonomy][] = (int) $term_id;
1436 // Now validate terms specified by name.
1437 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1438 $taxonomies = array_keys( $post_data['terms_names'] );
1440 foreach ( $taxonomies as $taxonomy ) {
1441 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1442 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1444 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1445 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1448 * For hierarchical taxonomies, we can't assign a term when multiple terms
1449 * in the hierarchy share the same name.
1451 $ambiguous_terms = array();
1452 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1453 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1455 // Count the number of terms with the same name.
1456 $tax_term_names_count = array_count_values( $tax_term_names );
1458 // Filter out non-ambiguous term names.
1459 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1461 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1464 $term_names = $post_data['terms_names'][$taxonomy];
1465 foreach ( $term_names as $term_name ) {
1466 if ( in_array( $term_name, $ambiguous_terms ) )
1467 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1469 $term = get_term_by( 'name', $term_name, $taxonomy );
1472 // Term doesn't exist, so check that the user is allowed to create new terms.
1473 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1474 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1476 // Create the new term.
1477 $term_info = wp_insert_term( $term_name, $taxonomy );
1478 if ( is_wp_error( $term_info ) )
1479 return new IXR_Error( 500, $term_info->get_error_message() );
1481 $terms[$taxonomy][] = (int) $term_info['term_id'];
1483 $terms[$taxonomy][] = (int) $term->term_id;
1489 $post_data['tax_input'] = $terms;
1490 unset( $post_data['terms'], $post_data['terms_names'] );
1492 // Do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'.
1493 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1496 if ( isset( $post_data['post_format'] ) ) {
1497 $format = set_post_format( $post_ID, $post_data['post_format'] );
1499 if ( is_wp_error( $format ) )
1500 return new IXR_Error( 500, $format->get_error_message() );
1502 unset( $post_data['post_format'] );
1505 // Handle enclosures.
1506 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1507 $this->add_enclosure_if_new( $post_ID, $enclosure );
1509 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1512 * Filters post data array to be inserted via XML-RPC.
1516 * @param array $post_data Parsed array of post data.
1517 * @param array $content_struct Post data array.
1519 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1521 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1522 if ( is_wp_error( $post_ID ) )
1523 return new IXR_Error( 500, $post_ID->get_error_message() );
1526 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted.' ) );
1528 return strval( $post_ID );
1532 * Edit a post for any registered post type.
1534 * The $content_struct parameter only needs to contain fields that
1535 * should be changed. All other fields will retain their existing values.
1539 * @param array $args {
1540 * Method arguments. Note: arguments must be ordered as documented.
1542 * @type int $blog_id Blog ID (unused).
1543 * @type string $username Username.
1544 * @type string $password Password.
1545 * @type int $post_id Post ID.
1546 * @type array $content_struct Extra content arguments.
1548 * @return true|IXR_Error True on success, IXR_Error on failure.
1550 public function wp_editPost( $args ) {
1551 if ( ! $this->minimum_args( $args, 5 ) )
1552 return $this->error;
1554 $this->escape( $args );
1556 $username = $args[1];
1557 $password = $args[2];
1558 $post_id = (int) $args[3];
1559 $content_struct = $args[4];
1561 if ( ! $user = $this->login( $username, $password ) )
1562 return $this->error;
1564 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1565 do_action( 'xmlrpc_call', 'wp.editPost' );
1567 $post = get_post( $post_id, ARRAY_A );
1569 if ( empty( $post['ID'] ) )
1570 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1572 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1573 // If the post has been modified since the date provided, return an error.
1574 if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1575 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1579 // Convert the date field back to IXR form.
1580 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1583 * Ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1584 * since _insert_post() will ignore the non-GMT date if the GMT date is set.
1586 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1587 unset( $post['post_date_gmt'] );
1589 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1591 $this->escape( $post );
1592 $merged_content_struct = array_merge( $post, $content_struct );
1594 $retval = $this->_insert_post( $user, $merged_content_struct );
1595 if ( $retval instanceof IXR_Error )
1602 * Delete a post for any registered post type.
1606 * @see wp_delete_post()
1608 * @param array $args {
1609 * Method arguments. Note: arguments must be ordered as documented.
1611 * @type int $blog_id Blog ID (unused).
1612 * @type string $username Username.
1613 * @type string $password Password.
1614 * @type int $post_id Post ID.
1616 * @return true|IXR_Error True on success, IXR_Error instance on failure.
1618 public function wp_deletePost( $args ) {
1619 if ( ! $this->minimum_args( $args, 4 ) )
1620 return $this->error;
1622 $this->escape( $args );
1624 $username = $args[1];
1625 $password = $args[2];
1626 $post_id = (int) $args[3];
1628 if ( ! $user = $this->login( $username, $password ) )
1629 return $this->error;
1631 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1632 do_action( 'xmlrpc_call', 'wp.deletePost' );
1634 $post = get_post( $post_id, ARRAY_A );
1635 if ( empty( $post['ID'] ) ) {
1636 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1639 if ( ! current_user_can( 'delete_post', $post_id ) ) {
1640 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1643 $result = wp_delete_post( $post_id );
1646 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1657 * The optional $fields parameter specifies what fields will be included
1658 * in the response array. This should be a list of field names. 'post_id' will
1659 * always be included in the response regardless of the value of $fields.
1661 * Instead of, or in addition to, individual field names, conceptual group
1662 * names can be used to specify multiple fields. The available conceptual
1663 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1668 * @param array $args {
1669 * Method arguments. Note: arguments must be ordered as documented.
1671 * @type int $blog_id Blog ID (unused).
1672 * @type string $username Username.
1673 * @type string $password Password.
1674 * @type int $post_id Post ID.
1675 * @type array $fields The subset of post type fields to return.
1677 * @return array|IXR_Error Array contains (based on $fields parameter):
1683 * - 'post_modified_gmt'
1692 * - 'comment_status'
1701 public function wp_getPost( $args ) {
1702 if ( ! $this->minimum_args( $args, 4 ) )
1703 return $this->error;
1705 $this->escape( $args );
1707 $username = $args[1];
1708 $password = $args[2];
1709 $post_id = (int) $args[3];
1711 if ( isset( $args[4] ) ) {
1715 * Filters the list of post query fields used by the given XML-RPC method.
1719 * @param array $fields Array of post fields. Default array contains 'post', 'terms', and 'custom_fields'.
1720 * @param string $method Method name.
1722 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1725 if ( ! $user = $this->login( $username, $password ) )
1726 return $this->error;
1728 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1729 do_action( 'xmlrpc_call', 'wp.getPost' );
1731 $post = get_post( $post_id, ARRAY_A );
1733 if ( empty( $post['ID'] ) )
1734 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1736 if ( ! current_user_can( 'edit_post', $post_id ) )
1737 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1739 return $this->_prepare_post( $post, $fields );
1747 * @see wp_get_recent_posts()
1748 * @see wp_getPost() for more on `$fields`
1749 * @see get_posts() for more on `$filter` values
1751 * @param array $args {
1752 * Method arguments. Note: arguments must be ordered as documented.
1754 * @type int $blog_id Blog ID (unused).
1755 * @type string $username Username.
1756 * @type string $password Password.
1757 * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'post_type',
1758 * 'post_status', 'number', 'offset', 'orderby', 's', and 'order'.
1759 * Default empty array.
1760 * @type array $fields Optional. The subset of post type fields to return in the response array.
1762 * @return array|IXR_Error Array contains a collection of posts.
1764 public function wp_getPosts( $args ) {
1765 if ( ! $this->minimum_args( $args, 3 ) )
1766 return $this->error;
1768 $this->escape( $args );
1770 $username = $args[1];
1771 $password = $args[2];
1772 $filter = isset( $args[3] ) ? $args[3] : array();
1774 if ( isset( $args[4] ) ) {
1777 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1778 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1781 if ( ! $user = $this->login( $username, $password ) )
1782 return $this->error;
1784 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1785 do_action( 'xmlrpc_call', 'wp.getPosts' );
1789 if ( isset( $filter['post_type'] ) ) {
1790 $post_type = get_post_type_object( $filter['post_type'] );
1791 if ( ! ( (bool) $post_type ) )
1792 return new IXR_Error( 403, __( 'Invalid post type.' ) );
1794 $post_type = get_post_type_object( 'post' );
1797 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1798 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
1800 $query['post_type'] = $post_type->name;
1802 if ( isset( $filter['post_status'] ) )
1803 $query['post_status'] = $filter['post_status'];
1805 if ( isset( $filter['number'] ) )
1806 $query['numberposts'] = absint( $filter['number'] );
1808 if ( isset( $filter['offset'] ) )
1809 $query['offset'] = absint( $filter['offset'] );
1811 if ( isset( $filter['orderby'] ) ) {
1812 $query['orderby'] = $filter['orderby'];
1814 if ( isset( $filter['order'] ) )
1815 $query['order'] = $filter['order'];
1818 if ( isset( $filter['s'] ) ) {
1819 $query['s'] = $filter['s'];
1822 $posts_list = wp_get_recent_posts( $query );
1824 if ( ! $posts_list )
1827 // Holds all the posts data.
1830 foreach ( $posts_list as $post ) {
1831 if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1834 $struct[] = $this->_prepare_post( $post, $fields );
1841 * Create a new term.
1845 * @see wp_insert_term()
1847 * @param array $args {
1848 * Method arguments. Note: arguments must be ordered as documented.
1850 * @type int $blog_id Blog ID (unused).
1851 * @type string $username Username.
1852 * @type string $password Password.
1853 * @type array $content_struct Content struct for adding a new term. The struct must contain
1854 * the term 'name' and 'taxonomy'. Optional accepted values include
1855 * 'parent', 'description', and 'slug'.
1857 * @return int|IXR_Error The term ID on success, or an IXR_Error object on failure.
1859 public function wp_newTerm( $args ) {
1860 if ( ! $this->minimum_args( $args, 4 ) )
1861 return $this->error;
1863 $this->escape( $args );
1865 $username = $args[1];
1866 $password = $args[2];
1867 $content_struct = $args[3];
1869 if ( ! $user = $this->login( $username, $password ) )
1870 return $this->error;
1872 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1873 do_action( 'xmlrpc_call', 'wp.newTerm' );
1875 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1876 return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
1878 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1880 if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) {
1881 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create terms in this taxonomy.' ) );
1884 $taxonomy = (array) $taxonomy;
1886 // hold the data of the term
1887 $term_data = array();
1889 $term_data['name'] = trim( $content_struct['name'] );
1890 if ( empty( $term_data['name'] ) )
1891 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1893 if ( isset( $content_struct['parent'] ) ) {
1894 if ( ! $taxonomy['hierarchical'] )
1895 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1897 $parent_term_id = (int) $content_struct['parent'];
1898 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1900 if ( is_wp_error( $parent_term ) )
1901 return new IXR_Error( 500, $parent_term->get_error_message() );
1903 if ( ! $parent_term )
1904 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1906 $term_data['parent'] = $content_struct['parent'];
1909 if ( isset( $content_struct['description'] ) )
1910 $term_data['description'] = $content_struct['description'];
1912 if ( isset( $content_struct['slug'] ) )
1913 $term_data['slug'] = $content_struct['slug'];
1915 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1917 if ( is_wp_error( $term ) )
1918 return new IXR_Error( 500, $term->get_error_message() );
1921 return new IXR_Error( 500, __( 'Sorry, your term could not be created.' ) );
1923 return strval( $term['term_id'] );
1931 * @see wp_update_term()
1933 * @param array $args {
1934 * Method arguments. Note: arguments must be ordered as documented.
1936 * @type int $blog_id Blog ID (unused).
1937 * @type string $username Username.
1938 * @type string $password Password.
1939 * @type int $term_id Term ID.
1940 * @type array $content_struct Content struct for editing a term. The struct must contain the
1941 * term ''taxonomy'. Optional accepted values include 'name', 'parent',
1942 * 'description', and 'slug'.
1944 * @return true|IXR_Error True on success, IXR_Error instance on failure.
1946 public function wp_editTerm( $args ) {
1947 if ( ! $this->minimum_args( $args, 5 ) )
1948 return $this->error;
1950 $this->escape( $args );
1952 $username = $args[1];
1953 $password = $args[2];
1954 $term_id = (int) $args[3];
1955 $content_struct = $args[4];
1957 if ( ! $user = $this->login( $username, $password ) )
1958 return $this->error;
1960 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1961 do_action( 'xmlrpc_call', 'wp.editTerm' );
1963 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1964 return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
1966 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1968 $taxonomy = (array) $taxonomy;
1970 // hold the data of the term
1971 $term_data = array();
1973 $term = get_term( $term_id , $content_struct['taxonomy'] );
1975 if ( is_wp_error( $term ) )
1976 return new IXR_Error( 500, $term->get_error_message() );
1979 return new IXR_Error( 404, __( 'Invalid term ID.' ) );
1981 if ( ! current_user_can( 'edit_term', $term_id ) ) {
1982 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this term.' ) );
1985 if ( isset( $content_struct['name'] ) ) {
1986 $term_data['name'] = trim( $content_struct['name'] );
1988 if ( empty( $term_data['name'] ) )
1989 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1992 if ( ! empty( $content_struct['parent'] ) ) {
1993 if ( ! $taxonomy['hierarchical'] )
1994 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1996 $parent_term_id = (int) $content_struct['parent'];
1997 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1999 if ( is_wp_error( $parent_term ) )
2000 return new IXR_Error( 500, $parent_term->get_error_message() );
2002 if ( ! $parent_term )
2003 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
2005 $term_data['parent'] = $content_struct['parent'];
2008 if ( isset( $content_struct['description'] ) )
2009 $term_data['description'] = $content_struct['description'];
2011 if ( isset( $content_struct['slug'] ) )
2012 $term_data['slug'] = $content_struct['slug'];
2014 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
2016 if ( is_wp_error( $term ) )
2017 return new IXR_Error( 500, $term->get_error_message() );
2020 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
2030 * @see wp_delete_term()
2032 * @param array $args {
2033 * Method arguments. Note: arguments must be ordered as documented.
2035 * @type int $blog_id Blog ID (unused).
2036 * @type string $username Username.
2037 * @type string $password Password.
2038 * @type string $taxnomy_name Taxonomy name.
2039 * @type int $term_id Term ID.
2041 * @return bool|IXR_Error True on success, IXR_Error instance on failure.
2043 public function wp_deleteTerm( $args ) {
2044 if ( ! $this->minimum_args( $args, 5 ) )
2045 return $this->error;
2047 $this->escape( $args );
2049 $username = $args[1];
2050 $password = $args[2];
2051 $taxonomy = $args[3];
2052 $term_id = (int) $args[4];
2054 if ( ! $user = $this->login( $username, $password ) )
2055 return $this->error;
2057 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2058 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
2060 if ( ! taxonomy_exists( $taxonomy ) )
2061 return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
2063 $taxonomy = get_taxonomy( $taxonomy );
2064 $term = get_term( $term_id, $taxonomy->name );
2066 if ( is_wp_error( $term ) )
2067 return new IXR_Error( 500, $term->get_error_message() );
2070 return new IXR_Error( 404, __( 'Invalid term ID.' ) );
2072 if ( ! current_user_can( 'delete_term', $term_id ) ) {
2073 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this term.' ) );
2076 $result = wp_delete_term( $term_id, $taxonomy->name );
2078 if ( is_wp_error( $result ) )
2079 return new IXR_Error( 500, $term->get_error_message() );
2082 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
2094 * @param array $args {
2095 * Method arguments. Note: arguments must be ordered as documented.
2097 * @type int $blog_id Blog ID (unused).
2098 * @type string $username Username.
2099 * @type string $password Password.
2100 * @type string $taxnomy Taxonomy name.
2101 * @type string $term_id Term ID.
2103 * @return array|IXR_Error IXR_Error on failure, array on success, containing:
2108 * - 'term_taxonomy_id'
2114 public function wp_getTerm( $args ) {
2115 if ( ! $this->minimum_args( $args, 5 ) )
2116 return $this->error;
2118 $this->escape( $args );
2120 $username = $args[1];
2121 $password = $args[2];
2122 $taxonomy = $args[3];
2123 $term_id = (int) $args[4];
2125 if ( ! $user = $this->login( $username, $password ) )
2126 return $this->error;
2128 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2129 do_action( 'xmlrpc_call', 'wp.getTerm' );
2131 if ( ! taxonomy_exists( $taxonomy ) )
2132 return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
2134 $taxonomy = get_taxonomy( $taxonomy );
2136 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
2138 if ( is_wp_error( $term ) )
2139 return new IXR_Error( 500, $term->get_error_message() );
2142 return new IXR_Error( 404, __( 'Invalid term ID.' ) );
2144 if ( ! current_user_can( 'assign_term', $term_id ) ) {
2145 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign this term.' ) );
2148 return $this->_prepare_term( $term );
2152 * Retrieve all terms for a taxonomy.
2156 * The optional $filter parameter modifies the query used to retrieve terms.
2157 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
2161 * @param array $args {
2162 * Method arguments. Note: arguments must be ordered as documented.
2164 * @type int $blog_id Blog ID (unused).
2165 * @type string $username Username.
2166 * @type string $password Password.
2167 * @type string $taxnomy Taxonomy name.
2168 * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'number',
2169 * 'offset', 'orderby', 'order', 'hide_empty', and 'search'. Default empty array.
2171 * @return array|IXR_Error An associative array of terms data on success, IXR_Error instance otherwise.
2173 public function wp_getTerms( $args ) {
2174 if ( ! $this->minimum_args( $args, 4 ) )
2175 return $this->error;
2177 $this->escape( $args );
2179 $username = $args[1];
2180 $password = $args[2];
2181 $taxonomy = $args[3];
2182 $filter = isset( $args[4] ) ? $args[4] : array();
2184 if ( ! $user = $this->login( $username, $password ) )
2185 return $this->error;
2187 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2188 do_action( 'xmlrpc_call', 'wp.getTerms' );
2190 if ( ! taxonomy_exists( $taxonomy ) )
2191 return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
2193 $taxonomy = get_taxonomy( $taxonomy );
2195 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2196 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign terms in this taxonomy.' ) );
2200 if ( isset( $filter['number'] ) )
2201 $query['number'] = absint( $filter['number'] );
2203 if ( isset( $filter['offset'] ) )
2204 $query['offset'] = absint( $filter['offset'] );
2206 if ( isset( $filter['orderby'] ) ) {
2207 $query['orderby'] = $filter['orderby'];
2209 if ( isset( $filter['order'] ) )
2210 $query['order'] = $filter['order'];
2213 if ( isset( $filter['hide_empty'] ) )
2214 $query['hide_empty'] = $filter['hide_empty'];
2216 $query['get'] = 'all';
2218 if ( isset( $filter['search'] ) )
2219 $query['search'] = $filter['search'];
2221 $terms = get_terms( $taxonomy->name, $query );
2223 if ( is_wp_error( $terms ) )
2224 return new IXR_Error( 500, $terms->get_error_message() );
2228 foreach ( $terms as $term ) {
2229 $struct[] = $this->_prepare_term( $term );
2236 * Retrieve a taxonomy.
2240 * @see get_taxonomy()
2242 * @param array $args {
2243 * Method arguments. Note: arguments must be ordered as documented.
2245 * @type int $blog_id Blog ID (unused).
2246 * @type string $username Username.
2247 * @type string $password Password.
2248 * @type string $taxnomy Taxonomy name.
2249 * @type array $fields Optional. Array of taxonomy fields to limit to in the return.
2250 * Accepts 'labels', 'cap', 'menu', and 'object_type'.
2251 * Default empty array.
2253 * @return array|IXR_Error An array of taxonomy data on success, IXR_Error instance otherwise.
2255 public function wp_getTaxonomy( $args ) {
2256 if ( ! $this->minimum_args( $args, 4 ) )
2257 return $this->error;
2259 $this->escape( $args );
2261 $username = $args[1];
2262 $password = $args[2];
2263 $taxonomy = $args[3];
2265 if ( isset( $args[4] ) ) {
2269 * Filters the taxonomy query fields used by the given XML-RPC method.
2273 * @param array $fields An array of taxonomy fields to retrieve.
2274 * @param string $method The method name.
2276 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
2279 if ( ! $user = $this->login( $username, $password ) )
2280 return $this->error;
2282 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2283 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
2285 if ( ! taxonomy_exists( $taxonomy ) )
2286 return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
2288 $taxonomy = get_taxonomy( $taxonomy );
2290 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2291 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign terms in this taxonomy.' ) );
2293 return $this->_prepare_taxonomy( $taxonomy, $fields );
2297 * Retrieve all taxonomies.
2301 * @see get_taxonomies()
2303 * @param array $args {
2304 * Method arguments. Note: arguments must be ordered as documented.
2306 * @type int $blog_id Blog ID (unused).
2307 * @type string $username Username.
2308 * @type string $password Password.
2309 * @type array $filter Optional. An array of arguments for retrieving taxonomies.
2310 * @type array $fields Optional. The subset of taxonomy fields to return.
2312 * @return array|IXR_Error An associative array of taxonomy data with returned fields determined
2313 * by `$fields`, or an IXR_Error instance on failure.
2315 public function wp_getTaxonomies( $args ) {
2316 if ( ! $this->minimum_args( $args, 3 ) )
2317 return $this->error;
2319 $this->escape( $args );
2321 $username = $args[1];
2322 $password = $args[2];
2323 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2325 if ( isset( $args[4] ) ) {
2328 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2329 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
2332 if ( ! $user = $this->login( $username, $password ) )
2333 return $this->error;
2335 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2336 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
2338 $taxonomies = get_taxonomies( $filter, 'objects' );
2340 // holds all the taxonomy data
2343 foreach ( $taxonomies as $taxonomy ) {
2344 // capability check for post_types
2345 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2348 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
2357 * The optional $fields parameter specifies what fields will be included
2358 * in the response array. This should be a list of field names. 'user_id' will
2359 * always be included in the response regardless of the value of $fields.
2361 * Instead of, or in addition to, individual field names, conceptual group
2362 * names can be used to specify multiple fields. The available conceptual
2363 * groups are 'basic' and 'all'.
2365 * @uses get_userdata()
2367 * @param array $args {
2368 * Method arguments. Note: arguments must be ordered as documented.
2370 * @type int $blog_id (unused)
2371 * @type string $username
2372 * @type string $password
2373 * @type int $user_id
2374 * @type array $fields (optional)
2376 * @return array|IXR_Error Array contains (based on $fields parameter):
2390 public function wp_getUser( $args ) {
2391 if ( ! $this->minimum_args( $args, 4 ) )
2392 return $this->error;
2394 $this->escape( $args );
2396 $username = $args[1];
2397 $password = $args[2];
2398 $user_id = (int) $args[3];
2400 if ( isset( $args[4] ) ) {
2404 * Filters the default user query fields used by the given XML-RPC method.
2408 * @param array $fields User query fields for given method. Default 'all'.
2409 * @param string $method The method name.
2411 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2414 if ( ! $user = $this->login( $username, $password ) )
2415 return $this->error;
2417 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2418 do_action( 'xmlrpc_call', 'wp.getUser' );
2420 if ( ! current_user_can( 'edit_user', $user_id ) )
2421 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this user.' ) );
2423 $user_data = get_userdata( $user_id );
2426 return new IXR_Error( 404, __( 'Invalid user ID.' ) );
2428 return $this->_prepare_user( $user_data, $fields );
2434 * The optional $filter parameter modifies the query used to retrieve users.
2435 * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2436 * 'who', 'orderby', and 'order'.
2438 * The optional $fields parameter specifies what fields will be included
2439 * in the response array.
2442 * @see wp_getUser() for more on $fields and return values
2444 * @param array $args {
2445 * Method arguments. Note: arguments must be ordered as documented.
2447 * @type int $blog_id (unused)
2448 * @type string $username
2449 * @type string $password
2450 * @type array $filter (optional)
2451 * @type array $fields (optional)
2453 * @return array|IXR_Error users data
2455 public function wp_getUsers( $args ) {
2456 if ( ! $this->minimum_args( $args, 3 ) )
2457 return $this->error;
2459 $this->escape( $args );
2461 $username = $args[1];
2462 $password = $args[2];
2463 $filter = isset( $args[3] ) ? $args[3] : array();
2465 if ( isset( $args[4] ) ) {
2468 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2469 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2472 if ( ! $user = $this->login( $username, $password ) )
2473 return $this->error;
2475 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2476 do_action( 'xmlrpc_call', 'wp.getUsers' );
2478 if ( ! current_user_can( 'list_users' ) )
2479 return new IXR_Error( 401, __( 'Sorry, you are not allowed to list users.' ) );
2481 $query = array( 'fields' => 'all_with_meta' );
2483 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2484 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2486 if ( isset( $filter['orderby'] ) ) {
2487 $query['orderby'] = $filter['orderby'];
2489 if ( isset( $filter['order'] ) )
2490 $query['order'] = $filter['order'];
2493 if ( isset( $filter['role'] ) ) {
2494 if ( get_role( $filter['role'] ) === null )
2495 return new IXR_Error( 403, __( 'Invalid role.' ) );
2497 $query['role'] = $filter['role'];
2500 if ( isset( $filter['who'] ) ) {
2501 $query['who'] = $filter['who'];
2504 $users = get_users( $query );
2507 foreach ( $users as $user_data ) {
2508 if ( current_user_can( 'edit_user', $user_data->ID ) )
2509 $_users[] = $this->_prepare_user( $user_data, $fields );
2515 * Retrieve information about the requesting user.
2517 * @uses get_userdata()
2519 * @param array $args {
2520 * Method arguments. Note: arguments must be ordered as documented.
2522 * @type int $blog_id (unused)
2523 * @type string $username
2524 * @type string $password
2525 * @type array $fields (optional)
2527 * @return array|IXR_Error (@see wp_getUser)
2529 public function wp_getProfile( $args ) {
2530 if ( ! $this->minimum_args( $args, 3 ) )
2531 return $this->error;
2533 $this->escape( $args );
2535 $username = $args[1];
2536 $password = $args[2];
2538 if ( isset( $args[3] ) ) {
2541 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2542 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2545 if ( ! $user = $this->login( $username, $password ) )
2546 return $this->error;
2548 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2549 do_action( 'xmlrpc_call', 'wp.getProfile' );
2551 if ( ! current_user_can( 'edit_user', $user->ID ) )
2552 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit your profile.' ) );
2554 $user_data = get_userdata( $user->ID );
2556 return $this->_prepare_user( $user_data, $fields );
2560 * Edit user's profile.
2562 * @uses wp_update_user()
2564 * @param array $args {
2565 * Method arguments. Note: arguments must be ordered as documented.
2567 * @type int $blog_id (unused)
2568 * @type string $username
2569 * @type string $password
2570 * @type array $content_struct It can optionally contain:
2579 * @return true|IXR_Error True, on success.
2581 public function wp_editProfile( $args ) {
2582 if ( ! $this->minimum_args( $args, 4 ) )
2583 return $this->error;
2585 $this->escape( $args );
2587 $username = $args[1];
2588 $password = $args[2];
2589 $content_struct = $args[3];
2591 if ( ! $user = $this->login( $username, $password ) )
2592 return $this->error;
2594 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2595 do_action( 'xmlrpc_call', 'wp.editProfile' );
2597 if ( ! current_user_can( 'edit_user', $user->ID ) )
2598 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit your profile.' ) );
2600 // holds data of the user
2601 $user_data = array();
2602 $user_data['ID'] = $user->ID;
2604 // only set the user details if it was given
2605 if ( isset( $content_struct['first_name'] ) )
2606 $user_data['first_name'] = $content_struct['first_name'];
2608 if ( isset( $content_struct['last_name'] ) )
2609 $user_data['last_name'] = $content_struct['last_name'];
2611 if ( isset( $content_struct['url'] ) )
2612 $user_data['user_url'] = $content_struct['url'];
2614 if ( isset( $content_struct['display_name'] ) )
2615 $user_data['display_name'] = $content_struct['display_name'];
2617 if ( isset( $content_struct['nickname'] ) )
2618 $user_data['nickname'] = $content_struct['nickname'];
2620 if ( isset( $content_struct['nicename'] ) )
2621 $user_data['user_nicename'] = $content_struct['nicename'];
2623 if ( isset( $content_struct['bio'] ) )
2624 $user_data['description'] = $content_struct['bio'];
2626 $result = wp_update_user( $user_data );
2628 if ( is_wp_error( $result ) )
2629 return new IXR_Error( 500, $result->get_error_message() );
2632 return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2642 * @param array $args {
2643 * Method arguments. Note: arguments must be ordered as documented.
2645 * @type int $blog_id (unused)
2646 * @type int $page_id
2647 * @type string $username
2648 * @type string $password
2650 * @return array|IXR_Error
2652 public function wp_getPage( $args ) {
2653 $this->escape( $args );
2655 $page_id = (int) $args[1];
2656 $username = $args[2];
2657 $password = $args[3];
2659 if ( !$user = $this->login($username, $password) ) {
2660 return $this->error;
2663 $page = get_post($page_id);
2665 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2667 if ( !current_user_can( 'edit_page', $page_id ) )
2668 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this page.' ) );
2670 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2671 do_action( 'xmlrpc_call', 'wp.getPage' );
2673 // If we found the page then format the data.
2674 if ( $page->ID && ($page->post_type == 'page') ) {
2675 return $this->_prepare_page( $page );
2677 // If the page doesn't exist indicate that.
2679 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2688 * @param array $args {
2689 * Method arguments. Note: arguments must be ordered as documented.
2691 * @type int $blog_id (unused)
2692 * @type string $username
2693 * @type string $password
2694 * @type int $num_pages
2696 * @return array|IXR_Error
2698 public function wp_getPages( $args ) {
2699 $this->escape( $args );
2701 $username = $args[1];
2702 $password = $args[2];
2703 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
2705 if ( !$user = $this->login($username, $password) )
2706 return $this->error;
2708 if ( !current_user_can( 'edit_pages' ) )
2709 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit pages.' ) );
2711 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2712 do_action( 'xmlrpc_call', 'wp.getPages' );
2714 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2715 $num_pages = count($pages);
2717 // If we have pages, put together their info.
2718 if ( $num_pages >= 1 ) {
2719 $pages_struct = array();
2721 foreach ($pages as $page) {
2722 if ( current_user_can( 'edit_page', $page->ID ) )
2723 $pages_struct[] = $this->_prepare_page( $page );
2726 return $pages_struct;
2737 * @see wp_xmlrpc_server::mw_newPost()
2739 * @param array $args {
2740 * Method arguments. Note: arguments must be ordered as documented.
2742 * @type int $blog_id (unused)
2743 * @type string $username
2744 * @type string $password
2745 * @type array $content_struct
2747 * @return int|IXR_Error
2749 public function wp_newPage( $args ) {
2750 // Items not escaped here will be escaped in newPost.
2751 $username = $this->escape( $args[1] );
2752 $password = $this->escape( $args[2] );
2754 if ( !$user = $this->login($username, $password) )
2755 return $this->error;
2757 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2758 do_action( 'xmlrpc_call', 'wp.newPage' );
2760 // Mark this as content for a page.
2761 $args[3]["post_type"] = 'page';
2763 // Let mw_newPost do all of the heavy lifting.
2764 return $this->mw_newPost( $args );
2772 * @param array $args {
2773 * Method arguments. Note: arguments must be ordered as documented.
2775 * @type int $blog_id (unused)
2776 * @type string $username
2777 * @type string $password
2778 * @type int $page_id
2780 * @return true|IXR_Error True, if success.
2782 public function wp_deletePage( $args ) {
2783 $this->escape( $args );
2785 $username = $args[1];
2786 $password = $args[2];
2787 $page_id = (int) $args[3];
2789 if ( !$user = $this->login($username, $password) )
2790 return $this->error;
2792 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2793 do_action( 'xmlrpc_call', 'wp.deletePage' );
2795 // Get the current page based on the page_id and
2796 // make sure it is a page and not a post.
2797 $actual_page = get_post($page_id, ARRAY_A);
2798 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2799 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2801 // Make sure the user can delete pages.
2802 if ( !current_user_can('delete_page', $page_id) )
2803 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this page.' ) );
2805 // Attempt to delete the page.
2806 $result = wp_delete_post($page_id);
2808 return new IXR_Error( 500, __( 'Failed to delete the page.' ) );
2811 * Fires after a page has been successfully deleted via XML-RPC.
2815 * @param int $page_id ID of the deleted page.
2816 * @param array $args An array of arguments to delete the page.
2818 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2828 * @param array $args {
2829 * Method arguments. Note: arguments must be ordered as documented.
2831 * @type int $blog_id (unused)
2832 * @type int $page_id
2833 * @type string $username
2834 * @type string $password
2835 * @type string $content
2836 * @type string $publish
2838 * @return array|IXR_Error
2840 public function wp_editPage( $args ) {
2841 // Items will be escaped in mw_editPost.
2842 $page_id = (int) $args[1];
2843 $username = $args[2];
2844 $password = $args[3];
2845 $content = $args[4];
2846 $publish = $args[5];
2848 $escaped_username = $this->escape( $username );
2849 $escaped_password = $this->escape( $password );
2851 if ( !$user = $this->login( $escaped_username, $escaped_password ) ) {
2852 return $this->error;
2855 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2856 do_action( 'xmlrpc_call', 'wp.editPage' );
2858 // Get the page data and make sure it is a page.
2859 $actual_page = get_post($page_id, ARRAY_A);
2860 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2861 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2863 // Make sure the user is allowed to edit pages.
2864 if ( !current_user_can('edit_page', $page_id) )
2865 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this page.' ) );
2867 // Mark this as content for a page.
2868 $content['post_type'] = 'page';
2870 // Arrange args in the way mw_editPost understands.
2879 // Let mw_editPost do all of the heavy lifting.
2880 return $this->mw_editPost( $args );
2884 * Retrieve page list.
2888 * @global wpdb $wpdb WordPress database abstraction object.
2890 * @param array $args {
2891 * Method arguments. Note: arguments must be ordered as documented.
2893 * @type int $blog_id (unused)
2894 * @type string $username
2895 * @type string $password
2897 * @return array|IXR_Error
2899 public function wp_getPageList( $args ) {
2902 $this->escape( $args );
2904 $username = $args[1];
2905 $password = $args[2];
2907 if ( !$user = $this->login($username, $password) )
2908 return $this->error;
2910 if ( !current_user_can( 'edit_pages' ) )
2911 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit pages.' ) );
2913 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2914 do_action( 'xmlrpc_call', 'wp.getPageList' );
2916 // Get list of pages ids and titles
2917 $page_list = $wpdb->get_results("
2919 post_title page_title,
2920 post_parent page_parent_id,
2925 WHERE post_type = 'page'
2929 // The date needs to be formatted properly.
2930 $num_pages = count($page_list);
2931 for ( $i = 0; $i < $num_pages; $i++ ) {
2932 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2933 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2935 unset($page_list[$i]->post_date_gmt);
2936 unset($page_list[$i]->post_date);
2937 unset($page_list[$i]->post_status);
2944 * Retrieve authors list.
2948 * @param array $args {
2949 * Method arguments. Note: arguments must be ordered as documented.
2951 * @type int $blog_id (unused)
2952 * @type string $username
2953 * @type string $password
2955 * @return array|IXR_Error
2957 public function wp_getAuthors( $args ) {
2958 $this->escape( $args );
2960 $username = $args[1];
2961 $password = $args[2];
2963 if ( !$user = $this->login($username, $password) )
2964 return $this->error;
2966 if ( !current_user_can('edit_posts') )
2967 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
2969 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2970 do_action( 'xmlrpc_call', 'wp.getAuthors' );
2973 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2975 'user_id' => $user->ID,
2976 'user_login' => $user->user_login,
2977 'display_name' => $user->display_name
2985 * Get list of all tags
2989 * @param array $args {
2990 * Method arguments. Note: arguments must be ordered as documented.
2992 * @type int $blog_id (unused)
2993 * @type string $username
2994 * @type string $password
2996 * @return array|IXR_Error
2998 public function wp_getTags( $args ) {
2999 $this->escape( $args );
3001 $username = $args[1];
3002 $password = $args[2];
3004 if ( !$user = $this->login($username, $password) )
3005 return $this->error;
3007 if ( !current_user_can( 'edit_posts' ) )
3008 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
3010 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3011 do_action( 'xmlrpc_call', 'wp.getKeywords' );
3015 if ( $all_tags = get_tags() ) {
3016 foreach ( (array) $all_tags as $tag ) {
3018 $struct['tag_id'] = $tag->term_id;
3019 $struct['name'] = $tag->name;
3020 $struct['count'] = $tag->count;
3021 $struct['slug'] = $tag->slug;
3022 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
3023 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
3033 * Create new category.
3037 * @param array $args {
3038 * Method arguments. Note: arguments must be ordered as documented.
3040 * @type int $blog_id (unused)
3041 * @type string $username
3042 * @type string $password
3043 * @type array $category
3045 * @return int|IXR_Error Category ID.
3047 public function wp_newCategory( $args ) {
3048 $this->escape( $args );
3050 $username = $args[1];
3051 $password = $args[2];
3052 $category = $args[3];
3054 if ( !$user = $this->login($username, $password) )
3055 return $this->error;
3057 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3058 do_action( 'xmlrpc_call', 'wp.newCategory' );
3060 // Make sure the user is allowed to add a category.
3061 if ( !current_user_can('manage_categories') )
3062 return new IXR_Error(401, __('Sorry, you are not allowed to add a category.'));
3064 // If no slug was provided make it empty so that
3065 // WordPress will generate one.
3066 if ( empty($category['slug']) )
3067 $category['slug'] = '';
3069 // If no parent_id was provided make it empty
3070 // so that it will be a top level page (no parent).
3071 if ( !isset($category['parent_id']) )
3072 $category['parent_id'] = '';
3074 // If no description was provided make it empty.
3075 if ( empty($category["description"]) )
3076 $category["description"] = "";
3078 $new_category = array(
3079 'cat_name' => $category['name'],
3080 'category_nicename' => $category['slug'],
3081 'category_parent' => $category['parent_id'],
3082 'category_description' => $category['description']
3085 $cat_id = wp_insert_category($new_category, true);
3086 if ( is_wp_error( $cat_id ) ) {
3087 if ( 'term_exists' == $cat_id->get_error_code() )
3088 return (int) $cat_id->get_error_data();
3090 return new IXR_Error(500, __('Sorry, the new category failed.'));
3091 } elseif ( ! $cat_id ) {
3092 return new IXR_Error(500, __('Sorry, the new category failed.'));
3096 * Fires after a new category has been successfully created via XML-RPC.
3100 * @param int $cat_id ID of the new category.
3101 * @param array $args An array of new category arguments.
3103 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
3113 * @param array $args {
3114 * Method arguments. Note: arguments must be ordered as documented.
3116 * @type int $blog_id (unused)
3117 * @type string $username
3118 * @type string $password
3119 * @type int $category_id
3121 * @return bool|IXR_Error See wp_delete_term() for return info.
3123 public function wp_deleteCategory( $args ) {
3124 $this->escape( $args );
3126 $username = $args[1];
3127 $password = $args[2];
3128 $category_id = (int) $args[3];
3130 if ( !$user = $this->login($username, $password) )
3131 return $this->error;
3133 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3134 do_action( 'xmlrpc_call', 'wp.deleteCategory' );
3136 if ( !current_user_can('manage_categories') )
3137 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete a category.' ) );
3139 $status = wp_delete_term( $category_id, 'category' );
3141 if ( true == $status ) {
3143 * Fires after a category has been successfully deleted via XML-RPC.
3147 * @param int $category_id ID of the deleted category.
3148 * @param array $args An array of arguments to delete the category.
3150 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
3157 * Retrieve category list.
3161 * @param array $args {
3162 * Method arguments. Note: arguments must be ordered as documented.
3164 * @type int $blog_id (unused)
3165 * @type string $username
3166 * @type string $password
3167 * @type array $category
3168 * @type int $max_results
3170 * @return array|IXR_Error
3172 public function wp_suggestCategories( $args ) {
3173 $this->escape( $args );
3175 $username = $args[1];
3176 $password = $args[2];
3177 $category = $args[3];
3178 $max_results = (int) $args[4];
3180 if ( !$user = $this->login($username, $password) )
3181 return $this->error;
3183 if ( !current_user_can( 'edit_posts' ) )
3184 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
3186 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3187 do_action( 'xmlrpc_call', 'wp.suggestCategories' );
3189 $category_suggestions = array();
3190 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
3191 foreach ( (array) get_categories($args) as $cat ) {
3192 $category_suggestions[] = array(
3193 'category_id' => $cat->term_id,
3194 'category_name' => $cat->name
3198 return $category_suggestions;
3206 * @param array $args {
3207 * Method arguments. Note: arguments must be ordered as documented.
3209 * @type int $blog_id (unused)
3210 * @type string $username
3211 * @type string $password
3212 * @type int $comment_id
3214 * @return array|IXR_Error
3216 public function wp_getComment($args) {
3217 $this->escape($args);
3219 $username = $args[1];
3220 $password = $args[2];
3221 $comment_id = (int) $args[3];
3223 if ( ! $user = $this->login( $username, $password ) ) {
3224 return $this->error;
3227 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3228 do_action( 'xmlrpc_call', 'wp.getComment' );
3230 if ( ! $comment = get_comment( $comment_id ) ) {
3231 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3234 if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
3235 return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) );
3238 return $this->_prepare_comment( $comment );
3242 * Retrieve comments.
3244 * Besides the common blog_id (unused), username, and password arguments, it takes a filter
3245 * array as last argument.
3247 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
3249 * The defaults are as follows:
3250 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
3251 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
3252 * - 'number' - Default is 10. Total number of media items to retrieve.
3253 * - 'offset' - Default is 0. See WP_Query::query() for more.
3257 * @param array $args {
3258 * Method arguments. Note: arguments must be ordered as documented.
3260 * @type int $blog_id (unused)
3261 * @type string $username
3262 * @type string $password
3263 * @type array $struct
3265 * @return array|IXR_Error Contains a collection of comments. See wp_xmlrpc_server::wp_getComment() for a description of each item contents
3267 public function wp_getComments( $args ) {
3268 $this->escape( $args );
3270 $username = $args[1];
3271 $password = $args[2];
3272 $struct = isset( $args[3] ) ? $args[3] : array();
3274 if ( ! $user = $this->login( $username, $password ) ) {
3275 return $this->error;
3278 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3279 do_action( 'xmlrpc_call', 'wp.getComments' );
3281 if ( isset( $struct['status'] ) ) {
3282 $status = $struct['status'];
3287 if ( ! current_user_can( 'moderate_comments' ) && 'approve' !== $status ) {
3288 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
3292 if ( isset( $struct['post_id'] ) ) {
3293 $post_id = absint( $struct['post_id'] );
3297 if ( isset( $struct['post_type'] ) ) {
3298 $post_type_object = get_post_type_object( $struct['post_type'] );
3299 if ( ! $post_type_object || ! post_type_supports( $post_type_object->name, 'comments' ) ) {
3300 return new IXR_Error( 404, __( 'Invalid post type.' ) );
3302 $post_type = $struct['post_type'];
3306 if ( isset( $struct['offset'] ) ) {
3307 $offset = absint( $struct['offset'] );
3311 if ( isset( $struct['number'] ) ) {
3312 $number = absint( $struct['number'] );
3315 $comments = get_comments( array(
3316 'status' => $status,
3317 'post_id' => $post_id,
3318 'offset' => $offset,
3319 'number' => $number,
3320 'post_type' => $post_type,
3323 $comments_struct = array();
3324 if ( is_array( $comments ) ) {
3325 foreach ( $comments as $comment ) {
3326 $comments_struct[] = $this->_prepare_comment( $comment );
3330 return $comments_struct;
3336 * By default, the comment will be moved to the trash instead of deleted.
3337 * See wp_delete_comment() for more information on this behavior.
3341 * @param array $args {
3342 * Method arguments. Note: arguments must be ordered as documented.
3344 * @type int $blog_id (unused)
3345 * @type string $username
3346 * @type string $password
3347 * @type int $comment_ID
3349 * @return bool|IXR_Error See wp_delete_comment().
3351 public function wp_deleteComment( $args ) {
3352 $this->escape($args);
3354 $username = $args[1];
3355 $password = $args[2];
3356 $comment_ID = (int) $args[3];
3358 if ( ! $user = $this->login( $username, $password ) ) {
3359 return $this->error;
3362 if ( ! get_comment( $comment_ID ) ) {
3363 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3366 if ( !current_user_can( 'edit_comment', $comment_ID ) ) {
3367 return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) );
3370 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3371 do_action( 'xmlrpc_call', 'wp.deleteComment' );
3373 $status = wp_delete_comment( $comment_ID );
3377 * Fires after a comment has been successfully deleted via XML-RPC.
3381 * @param int $comment_ID ID of the deleted comment.
3382 * @param array $args An array of arguments to delete the comment.
3384 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
3393 * Besides the common blog_id (unused), username, and password arguments, it takes a
3394 * comment_id integer and a content_struct array as last argument.
3396 * The allowed keys in the content_struct array are:
3401 * - 'date_created_gmt'
3402 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See get_comment_statuses() for more details
3406 * @param array $args {
3407 * Method arguments. Note: arguments must be ordered as documented.
3409 * @type int $blog_id (unused)
3410 * @type string $username
3411 * @type string $password
3412 * @type int $comment_ID
3413 * @type array $content_struct
3415 * @return true|IXR_Error True, on success.
3417 public function wp_editComment( $args ) {
3418 $this->escape( $args );
3420 $username = $args[1];
3421 $password = $args[2];
3422 $comment_ID = (int) $args[3];
3423 $content_struct = $args[4];
3425 if ( !$user = $this->login( $username, $password ) ) {
3426 return $this->error;
3429 if ( ! get_comment( $comment_ID ) ) {
3430 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3433 if ( ! current_user_can( 'edit_comment', $comment_ID ) ) {
3434 return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) );
3437 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3438 do_action( 'xmlrpc_call', 'wp.editComment' );
3440 if ( isset($content_struct['status']) ) {
3441 $statuses = get_comment_statuses();
3442 $statuses = array_keys($statuses);
3444 if ( ! in_array($content_struct['status'], $statuses) )
3445 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
3446 $comment_approved = $content_struct['status'];
3449 // Do some timestamp voodoo
3450 if ( !empty( $content_struct['date_created_gmt'] ) ) {
3451 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
3452 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
3453 $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
3454 $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
3457 if ( isset($content_struct['content']) )
3458 $comment_content = $content_struct['content'];
3460 if ( isset($content_struct['author']) )
3461 $comment_author = $content_struct['author'];
3463 if ( isset($content_struct['author_url']) )
3464 $comment_author_url = $content_struct['author_url'];
3466 if ( isset($content_struct['author_email']) )
3467 $comment_author_email = $content_struct['author_email'];
3469 // We've got all the data -- post it:
3470 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
3472 $result = wp_update_comment($comment);
3473 if ( is_wp_error( $result ) )
3474 return new IXR_Error(500, $result->get_error_message());
3477 return new IXR_Error(500, __('Sorry, the comment could not be edited.'));
3480 * Fires after a comment has been successfully updated via XML-RPC.
3484 * @param int $comment_ID ID of the updated comment.
3485 * @param array $args An array of arguments to update the comment.
3487 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
3493 * Create new comment.
3497 * @param array $args {
3498 * Method arguments. Note: arguments must be ordered as documented.
3500 * @type int $blog_id (unused)
3501 * @type string $username
3502 * @type string $password
3503 * @type string|int $post
3504 * @type array $content_struct
3506 * @return int|IXR_Error See wp_new_comment().
3508 public function wp_newComment($args) {
3509 $this->escape($args);
3511 $username = $args[1];
3512 $password = $args[2];
3514 $content_struct = $args[4];
3517 * Filters whether to allow anonymous comments over XML-RPC.
3521 * @param bool $allow Whether to allow anonymous commenting via