3 * XML-RPC protocol support for WordPress
6 * @subpackage Publishing
10 * WordPress XMLRPC server implementation.
12 * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
13 * pingback. Additional WordPress API for managing comments, pages, posts,
16 * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled
17 * via the xmlrpc_enabled filter found in wp_xmlrpc_server::login().
20 * @subpackage Publishing
23 class wp_xmlrpc_server extends IXR_Server {
49 * Register all of the XMLRPC methods that XMLRPC server understands.
51 * Sets up server and method property. Passes XMLRPC
52 * methods through the 'xmlrpc_methods' filter to allow plugins to extend
53 * or replace XMLRPC methods.
57 public function __construct() {
58 $this->methods = array(
60 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
61 'wp.newPost' => 'this:wp_newPost',
62 'wp.editPost' => 'this:wp_editPost',
63 'wp.deletePost' => 'this:wp_deletePost',
64 'wp.getPost' => 'this:wp_getPost',
65 'wp.getPosts' => 'this:wp_getPosts',
66 'wp.newTerm' => 'this:wp_newTerm',
67 'wp.editTerm' => 'this:wp_editTerm',
68 'wp.deleteTerm' => 'this:wp_deleteTerm',
69 'wp.getTerm' => 'this:wp_getTerm',
70 'wp.getTerms' => 'this:wp_getTerms',
71 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
72 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
73 'wp.getUser' => 'this:wp_getUser',
74 'wp.getUsers' => 'this:wp_getUsers',
75 'wp.getProfile' => 'this:wp_getProfile',
76 'wp.editProfile' => 'this:wp_editProfile',
77 'wp.getPage' => 'this:wp_getPage',
78 'wp.getPages' => 'this:wp_getPages',
79 'wp.newPage' => 'this:wp_newPage',
80 'wp.deletePage' => 'this:wp_deletePage',
81 'wp.editPage' => 'this:wp_editPage',
82 'wp.getPageList' => 'this:wp_getPageList',
83 'wp.getAuthors' => 'this:wp_getAuthors',
84 'wp.getCategories' => 'this:mw_getCategories', // Alias
85 'wp.getTags' => 'this:wp_getTags',
86 'wp.newCategory' => 'this:wp_newCategory',
87 'wp.deleteCategory' => 'this:wp_deleteCategory',
88 'wp.suggestCategories' => 'this:wp_suggestCategories',
89 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
90 'wp.deleteFile' => 'this:wp_deletePost', // Alias
91 'wp.getCommentCount' => 'this:wp_getCommentCount',
92 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
93 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
94 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
95 'wp.getOptions' => 'this:wp_getOptions',
96 'wp.setOptions' => 'this:wp_setOptions',
97 'wp.getComment' => 'this:wp_getComment',
98 'wp.getComments' => 'this:wp_getComments',
99 'wp.deleteComment' => 'this:wp_deleteComment',
100 'wp.editComment' => 'this:wp_editComment',
101 'wp.newComment' => 'this:wp_newComment',
102 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
103 'wp.getMediaItem' => 'this:wp_getMediaItem',
104 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
105 'wp.getPostFormats' => 'this:wp_getPostFormats',
106 'wp.getPostType' => 'this:wp_getPostType',
107 'wp.getPostTypes' => 'this:wp_getPostTypes',
108 'wp.getRevisions' => 'this:wp_getRevisions',
109 'wp.restoreRevision' => 'this:wp_restoreRevision',
112 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
113 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
114 'blogger.getPost' => 'this:blogger_getPost',
115 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
116 'blogger.newPost' => 'this:blogger_newPost',
117 'blogger.editPost' => 'this:blogger_editPost',
118 'blogger.deletePost' => 'this:blogger_deletePost',
120 // MetaWeblog API (with MT extensions to structs)
121 'metaWeblog.newPost' => 'this:mw_newPost',
122 'metaWeblog.editPost' => 'this:mw_editPost',
123 'metaWeblog.getPost' => 'this:mw_getPost',
124 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
125 'metaWeblog.getCategories' => 'this:mw_getCategories',
126 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
128 // MetaWeblog API aliases for Blogger API
129 // see http://www.xmlrpc.com/stories/storyReader$2460
130 'metaWeblog.deletePost' => 'this:blogger_deletePost',
131 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
134 'mt.getCategoryList' => 'this:mt_getCategoryList',
135 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
136 'mt.getPostCategories' => 'this:mt_getPostCategories',
137 'mt.setPostCategories' => 'this:mt_setPostCategories',
138 'mt.supportedMethods' => 'this:mt_supportedMethods',
139 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
140 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
141 'mt.publishPost' => 'this:mt_publishPost',
144 'pingback.ping' => 'this:pingback_ping',
145 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
147 'demo.sayHello' => 'this:sayHello',
148 'demo.addTwoNumbers' => 'this:addTwoNumbers'
151 $this->initialise_blog_option_info();
154 * Filter the methods exposed by the XML-RPC server.
156 * This filter can be used to add new methods, and remove built-in methods.
160 * @param array $methods An array of XML-RPC methods.
162 $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
166 * Make private/protected methods readable for backwards compatibility.
171 * @param callable $name Method to call.
172 * @param array $arguments Arguments to pass when calling.
173 * @return array|IXR_Error|false Return value of the callback, false otherwise.
175 public function __call( $name, $arguments ) {
176 if ( '_multisite_getUsersBlogs' === $name ) {
177 return call_user_func_array( array( $this, $name ), $arguments );
185 public function serve_request() {
186 $this->IXR_Server($this->methods);
190 * Test XMLRPC API by saying, "Hello!" to client.
194 * @return string Hello string response.
196 public function sayHello() {
201 * Test XMLRPC API by adding two numbers for client.
205 * @param array $args {
206 * Method arguments. Note: arguments must be ordered as documented.
208 * @type int $number1 A number to add.
209 * @type int $number2 A second number to add.
211 * @return int Sum of the two given numbers.
213 public function addTwoNumbers( $args ) {
216 return $number1 + $number2;
224 * @param string $username User's username.
225 * @param string $password User's password.
226 * @return WP_User|bool WP_User object if authentication passed, false otherwise
228 public function login( $username, $password ) {
230 * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
231 * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
233 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
234 if ( false === $enabled ) {
235 $enabled = apply_filters( 'option_enable_xmlrpc', true );
239 * Filter whether XML-RPC is enabled.
241 * This is the proper filter for turning off XML-RPC.
245 * @param bool $enabled Whether XML-RPC is enabled. Default true.
247 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
250 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
254 $user = wp_authenticate($username, $password);
256 if (is_wp_error($user)) {
257 $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
260 * Filter the XML-RPC user login error message.
264 * @param string $error The XML-RPC error message.
265 * @param WP_User $user WP_User object.
267 $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
271 wp_set_current_user( $user->ID );
276 * Check user's credentials. Deprecated.
280 * @deprecated use wp_xmlrpc_server::login
281 * @see wp_xmlrpc_server::login
283 * @param string $username User's username.
284 * @param string $password User's password.
285 * @return bool Whether authentication passed.
287 public function login_pass_ok( $username, $password ) {
288 return (bool) $this->login( $username, $password );
292 * Escape string or array of strings for database.
296 * @param string|array $data Escape single string or array of strings.
297 * @return string|void Returns with string is passed, alters by-reference
298 * when array is passed.
300 public function escape( &$data ) {
301 if ( ! is_array( $data ) )
302 return wp_slash( $data );
304 foreach ( $data as &$v ) {
305 if ( is_array( $v ) )
307 elseif ( ! is_object( $v ) )
313 * Retrieve custom fields for post.
317 * @param int $post_id Post ID.
318 * @return array Custom fields, if exist.
320 public function get_custom_fields($post_id) {
321 $post_id = (int) $post_id;
323 $custom_fields = array();
325 foreach ( (array) has_meta($post_id) as $meta ) {
326 // Don't expose protected fields.
327 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
330 $custom_fields[] = array(
331 "id" => $meta['meta_id'],
332 "key" => $meta['meta_key'],
333 "value" => $meta['meta_value']
337 return $custom_fields;
341 * Set custom fields for post.
345 * @param int $post_id Post ID.
346 * @param array $fields Custom fields.
348 public function set_custom_fields($post_id, $fields) {
349 $post_id = (int) $post_id;
351 foreach ( (array) $fields as $meta ) {
352 if ( isset($meta['id']) ) {
353 $meta['id'] = (int) $meta['id'];
354 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
355 if ( isset($meta['key']) ) {
356 $meta['key'] = wp_unslash( $meta['key'] );
357 if ( $meta['key'] !== $pmeta->meta_key )
359 $meta['value'] = wp_unslash( $meta['value'] );
360 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
361 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
362 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
363 delete_metadata_by_mid( 'post', $meta['id'] );
365 } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
366 add_post_meta( $post_id, $meta['key'], $meta['value'] );
372 * Set up blog options property.
374 * Passes property through {@see 'xmlrpc_blog_options'} filter.
378 * @global string $wp_version
380 public function initialise_blog_option_info() {
383 $this->blog_options = array(
385 'software_name' => array(
386 'desc' => __( 'Software Name' ),
388 'value' => 'WordPress'
390 'software_version' => array(
391 'desc' => __( 'Software Version' ),
393 'value' => $wp_version
396 'desc' => __( 'WordPress Address (URL)' ),
398 'option' => 'siteurl'
401 'desc' => __( 'Site Address (URL)' ),
405 'login_url' => array(
406 'desc' => __( 'Login Address (URL)' ),
408 'value' => wp_login_url( )
410 'admin_url' => array(
411 'desc' => __( 'The URL to the admin area' ),
413 'value' => get_admin_url( )
415 'image_default_link_type' => array(
416 'desc' => __( 'Image default link type' ),
418 'option' => 'image_default_link_type'
420 'image_default_size' => array(
421 'desc' => __( 'Image default size' ),
423 'option' => 'image_default_size'
425 'image_default_align' => array(
426 'desc' => __( 'Image default align' ),
428 'option' => 'image_default_align'
431 'desc' => __( 'Template' ),
433 'option' => 'template'
435 'stylesheet' => array(
436 'desc' => __( 'Stylesheet' ),
438 'option' => 'stylesheet'
440 'post_thumbnail' => array(
441 'desc' => __('Post Thumbnail'),
443 'value' => current_theme_supports( 'post-thumbnails' )
447 'time_zone' => array(
448 'desc' => __( 'Time Zone' ),
450 'option' => 'gmt_offset'
452 'blog_title' => array(
453 'desc' => __( 'Site Title' ),
455 'option' => 'blogname'
457 'blog_tagline' => array(
458 'desc' => __( 'Site Tagline' ),
460 'option' => 'blogdescription'
462 'date_format' => array(
463 'desc' => __( 'Date Format' ),
465 'option' => 'date_format'
467 'time_format' => array(
468 'desc' => __( 'Time Format' ),
470 'option' => 'time_format'
472 'users_can_register' => array(
473 'desc' => __( 'Allow new users to sign up' ),
475 'option' => 'users_can_register'
477 'thumbnail_size_w' => array(
478 'desc' => __( 'Thumbnail Width' ),
480 'option' => 'thumbnail_size_w'
482 'thumbnail_size_h' => array(
483 'desc' => __( 'Thumbnail Height' ),
485 'option' => 'thumbnail_size_h'
487 'thumbnail_crop' => array(
488 'desc' => __( 'Crop thumbnail to exact dimensions' ),
490 'option' => 'thumbnail_crop'
492 'medium_size_w' => array(
493 'desc' => __( 'Medium size image width' ),
495 'option' => 'medium_size_w'
497 'medium_size_h' => array(
498 'desc' => __( 'Medium size image height' ),
500 'option' => 'medium_size_h'
502 'large_size_w' => array(
503 'desc' => __( 'Large size image width' ),
505 'option' => 'large_size_w'
507 'large_size_h' => array(
508 'desc' => __( 'Large size image height' ),
510 'option' => 'large_size_h'
512 'default_comment_status' => array(
513 'desc' => __( 'Allow people to post comments on new articles' ),
515 'option' => 'default_comment_status'
517 'default_ping_status' => array(
518 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles' ),
520 'option' => 'default_ping_status'
525 * Filter the XML-RPC blog options property.
529 * @param array $blog_options An array of XML-RPC blog options.
531 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
535 * Retrieve the blogs of the user.
539 * @param array $args {
540 * Method arguments. Note: arguments must be ordered as documented.
542 * @type string $username Username.
543 * @type string $password Password.
545 * @return array|IXR_Error Array contains:
550 * - 'xmlrpc' - url of xmlrpc endpoint
552 public function wp_getUsersBlogs( $args ) {
553 // If this isn't on WPMU then just use blogger_getUsersBlogs
554 if ( !is_multisite() ) {
555 array_unshift( $args, 1 );
556 return $this->blogger_getUsersBlogs( $args );
559 $this->escape( $args );
561 $username = $args[0];
562 $password = $args[1];
564 if ( !$user = $this->login($username, $password) )
568 * Fires after the XML-RPC user has been authenticated but before the rest of
569 * the method logic begins.
571 * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
572 * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
576 * @param string $name The method name.
578 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
580 $blogs = (array) get_blogs_of_user( $user->ID );
583 foreach ( $blogs as $blog ) {
584 // Don't include blogs that aren't hosted at this site.
585 if ( $blog->site_id != get_current_site()->id )
588 $blog_id = $blog->userblog_id;
590 switch_to_blog( $blog_id );
592 $is_admin = current_user_can( 'manage_options' );
595 'isAdmin' => $is_admin,
596 'url' => home_url( '/' ),
597 'blogid' => (string) $blog_id,
598 'blogName' => get_option( 'blogname' ),
599 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
602 restore_current_blog();
609 * Checks if the method received at least the minimum number of arguments.
614 * @param string|array $args Sanitize single string or array of strings.
615 * @param int $count Minimum number of arguments.
616 * @return bool if `$args` contains at least $count arguments.
618 protected function minimum_args( $args, $count ) {
619 if ( count( $args ) < $count ) {
620 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
628 * Prepares taxonomy data for return in an XML-RPC object.
632 * @param object $taxonomy The unprepared taxonomy data.
633 * @param array $fields The subset of taxonomy fields to return.
634 * @return array The prepared taxonomy data.
636 protected function _prepare_taxonomy( $taxonomy, $fields ) {
638 'name' => $taxonomy->name,
639 'label' => $taxonomy->label,
640 'hierarchical' => (bool) $taxonomy->hierarchical,
641 'public' => (bool) $taxonomy->public,
642 'show_ui' => (bool) $taxonomy->show_ui,
643 '_builtin' => (bool) $taxonomy->_builtin,
646 if ( in_array( 'labels', $fields ) )
647 $_taxonomy['labels'] = (array) $taxonomy->labels;
649 if ( in_array( 'cap', $fields ) )
650 $_taxonomy['cap'] = (array) $taxonomy->cap;
652 if ( in_array( 'menu', $fields ) )
653 $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
655 if ( in_array( 'object_type', $fields ) )
656 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
659 * Filter XML-RPC-prepared data for the given taxonomy.
663 * @param array $_taxonomy An array of taxonomy data.
664 * @param object $taxonomy Taxonomy object.
665 * @param array $fields The subset of taxonomy fields to return.
667 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
671 * Prepares term data for return in an XML-RPC object.
675 * @param array|object $term The unprepared term data.
676 * @return array The prepared term data.
678 protected function _prepare_term( $term ) {
680 if ( ! is_array( $_term ) )
681 $_term = get_object_vars( $_term );
683 // For integers which may be larger than XML-RPC supports ensure we return strings.
684 $_term['term_id'] = strval( $_term['term_id'] );
685 $_term['term_group'] = strval( $_term['term_group'] );
686 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
687 $_term['parent'] = strval( $_term['parent'] );
689 // Count we are happy to return as an integer because people really shouldn't use terms that much.
690 $_term['count'] = intval( $_term['count'] );
693 * Filter XML-RPC-prepared data for the given term.
697 * @param array $_term An array of term data.
698 * @param array|object $term Term object or array.
700 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
704 * Convert a WordPress date string to an IXR_Date object.
708 * @param string $date Date string to convert.
709 * @return IXR_Date IXR_Date object.
711 protected function _convert_date( $date ) {
712 if ( $date === '0000-00-00 00:00:00' ) {
713 return new IXR_Date( '00000000T00:00:00Z' );
715 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
719 * Convert a WordPress GMT date string to an IXR_Date object.
723 * @param string $date_gmt WordPress GMT date string.
724 * @param string $date Date string.
725 * @return IXR_Date IXR_Date object.
727 protected function _convert_date_gmt( $date_gmt, $date ) {
728 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
729 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
731 return $this->_convert_date( $date_gmt );
735 * Prepares post data for return in an XML-RPC object.
739 * @param array $post The unprepared post data.
740 * @param array $fields The subset of post type fields to return.
741 * @return array The prepared post data.
743 protected function _prepare_post( $post, $fields ) {
744 // Holds the data for this post. built up based on $fields.
745 $_post = array( 'post_id' => strval( $post['ID'] ) );
747 // Prepare common post fields.
748 $post_fields = array(
749 'post_title' => $post['post_title'],
750 'post_date' => $this->_convert_date( $post['post_date'] ),
751 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
752 'post_modified' => $this->_convert_date( $post['post_modified'] ),
753 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
754 'post_status' => $post['post_status'],
755 'post_type' => $post['post_type'],
756 'post_name' => $post['post_name'],
757 'post_author' => $post['post_author'],
758 'post_password' => $post['post_password'],
759 'post_excerpt' => $post['post_excerpt'],
760 'post_content' => $post['post_content'],
761 'post_parent' => strval( $post['post_parent'] ),
762 'post_mime_type' => $post['post_mime_type'],
763 'link' => post_permalink( $post['ID'] ),
764 'guid' => $post['guid'],
765 'menu_order' => intval( $post['menu_order'] ),
766 'comment_status' => $post['comment_status'],
767 'ping_status' => $post['ping_status'],
768 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
772 $post_fields['post_thumbnail'] = array();
773 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
774 if ( $thumbnail_id ) {
775 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
776 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
779 // Consider future posts as published.
780 if ( $post_fields['post_status'] === 'future' )
781 $post_fields['post_status'] = 'publish';
783 // Fill in blank post format.
784 $post_fields['post_format'] = get_post_format( $post['ID'] );
785 if ( empty( $post_fields['post_format'] ) )
786 $post_fields['post_format'] = 'standard';
788 // Merge requested $post_fields fields into $_post.
789 if ( in_array( 'post', $fields ) ) {
790 $_post = array_merge( $_post, $post_fields );
792 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
793 $_post = array_merge( $_post, $requested_fields );
796 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
798 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
799 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
800 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
801 $_post['terms'] = array();
802 foreach ( $terms as $term ) {
803 $_post['terms'][] = $this->_prepare_term( $term );
807 if ( in_array( 'custom_fields', $fields ) )
808 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
810 if ( in_array( 'enclosure', $fields ) ) {
811 $_post['enclosure'] = array();
812 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
813 if ( ! empty( $enclosures ) ) {
814 $encdata = explode( "\n", $enclosures[0] );
815 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
816 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
817 $_post['enclosure']['type'] = trim( $encdata[2] );
822 * Filter XML-RPC-prepared date for the given post.
826 * @param array $_post An array of modified post data.
827 * @param array $post An array of post data.
828 * @param array $fields An array of post fields.
830 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
834 * Prepares post data for return in an XML-RPC object.
838 * @param object $post_type Post type object.
839 * @param array $fields The subset of post fields to return.
840 * @return array The prepared post type data.
842 protected function _prepare_post_type( $post_type, $fields ) {
844 'name' => $post_type->name,
845 'label' => $post_type->label,
846 'hierarchical' => (bool) $post_type->hierarchical,
847 'public' => (bool) $post_type->public,
848 'show_ui' => (bool) $post_type->show_ui,
849 '_builtin' => (bool) $post_type->_builtin,
850 'has_archive' => (bool) $post_type->has_archive,
851 'supports' => get_all_post_type_supports( $post_type->name ),
854 if ( in_array( 'labels', $fields ) ) {
855 $_post_type['labels'] = (array) $post_type->labels;
858 if ( in_array( 'cap', $fields ) ) {
859 $_post_type['cap'] = (array) $post_type->cap;
860 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
863 if ( in_array( 'menu', $fields ) ) {
864 $_post_type['menu_position'] = (int) $post_type->menu_position;
865 $_post_type['menu_icon'] = $post_type->menu_icon;
866 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
869 if ( in_array( 'taxonomies', $fields ) )
870 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
873 * Filter XML-RPC-prepared date for the given post type.
877 * @param array $_post_type An array of post type data.
878 * @param object $post_type Post type object.
880 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
884 * Prepares media item data for return in an XML-RPC object.
888 * @param object $media_item The unprepared media item data.
889 * @param string $thumbnail_size The image size to use for the thumbnail URL.
890 * @return array The prepared media item data.
892 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
893 $_media_item = array(
894 'attachment_id' => strval( $media_item->ID ),
895 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
896 'parent' => $media_item->post_parent,
897 'link' => wp_get_attachment_url( $media_item->ID ),
898 'title' => $media_item->post_title,
899 'caption' => $media_item->post_excerpt,
900 'description' => $media_item->post_content,
901 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
904 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
905 if ( $thumbnail_src )
906 $_media_item['thumbnail'] = $thumbnail_src[0];
908 $_media_item['thumbnail'] = $_media_item['link'];
911 * Filter XML-RPC-prepared data for the given media item.
915 * @param array $_media_item An array of media item data.
916 * @param object $media_item Media item object.
917 * @param string $thumbnail_size Image size.
919 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
923 * Prepares page data for return in an XML-RPC object.
927 * @param object $page The unprepared page data.
928 * @return array The prepared page data.
930 protected function _prepare_page( $page ) {
931 // Get all of the page content and link.
932 $full_page = get_extended( $page->post_content );
933 $link = post_permalink( $page->ID );
935 // Get info the page parent if there is one.
937 if ( ! empty( $page->post_parent ) ) {
938 $parent = get_post( $page->post_parent );
939 $parent_title = $parent->post_title;
942 // Determine comment and ping settings.
943 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
944 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
947 $page_date = $this->_convert_date( $page->post_date );
948 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
950 // Pull the categories info together.
951 $categories = array();
952 if ( is_object_in_taxonomy( 'page', 'category' ) ) {
953 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
954 $categories[] = get_cat_name( $cat_id );
958 // Get the author info.
959 $author = get_userdata( $page->post_author );
961 $page_template = get_page_template_slug( $page->ID );
962 if ( empty( $page_template ) )
963 $page_template = 'default';
966 'dateCreated' => $page_date,
967 'userid' => $page->post_author,
968 'page_id' => $page->ID,
969 'page_status' => $page->post_status,
970 'description' => $full_page['main'],
971 'title' => $page->post_title,
973 'permaLink' => $link,
974 'categories' => $categories,
975 'excerpt' => $page->post_excerpt,
976 'text_more' => $full_page['extended'],
977 'mt_allow_comments' => $allow_comments,
978 'mt_allow_pings' => $allow_pings,
979 'wp_slug' => $page->post_name,
980 'wp_password' => $page->post_password,
981 'wp_author' => $author->display_name,
982 'wp_page_parent_id' => $page->post_parent,
983 'wp_page_parent_title' => $parent_title,
984 'wp_page_order' => $page->menu_order,
985 'wp_author_id' => (string) $author->ID,
986 'wp_author_display_name' => $author->display_name,
987 'date_created_gmt' => $page_date_gmt,
988 'custom_fields' => $this->get_custom_fields( $page->ID ),
989 'wp_page_template' => $page_template
993 * Filter XML-RPC-prepared data for the given page.
997 * @param array $_page An array of page data.
998 * @param WP_Post $page Page object.
1000 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
1004 * Prepares comment data for return in an XML-RPC object.
1008 * @param object $comment The unprepared comment data.
1009 * @return array The prepared comment data.
1011 protected function _prepare_comment( $comment ) {
1012 // Format page date.
1013 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
1015 if ( '0' == $comment->comment_approved ) {
1016 $comment_status = 'hold';
1017 } elseif ( 'spam' == $comment->comment_approved ) {
1018 $comment_status = 'spam';
1019 } elseif ( '1' == $comment->comment_approved ) {
1020 $comment_status = 'approve';
1022 $comment_status = $comment->comment_approved;
1025 'date_created_gmt' => $comment_date_gmt,
1026 'user_id' => $comment->user_id,
1027 'comment_id' => $comment->comment_ID,
1028 'parent' => $comment->comment_parent,
1029 'status' => $comment_status,
1030 'content' => $comment->comment_content,
1031 'link' => get_comment_link($comment),
1032 'post_id' => $comment->comment_post_ID,
1033 'post_title' => get_the_title($comment->comment_post_ID),
1034 'author' => $comment->comment_author,
1035 'author_url' => $comment->comment_author_url,
1036 'author_email' => $comment->comment_author_email,
1037 'author_ip' => $comment->comment_author_IP,
1038 'type' => $comment->comment_type,
1042 * Filter XML-RPC-prepared data for the given comment.
1046 * @param array $_comment An array of prepared comment data.
1047 * @param object $comment Comment object.
1049 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
1053 * Prepares user data for return in an XML-RPC object.
1057 * @param WP_User $user The unprepared user object.
1058 * @param array $fields The subset of user fields to return.
1059 * @return array The prepared user data.
1061 protected function _prepare_user( $user, $fields ) {
1062 $_user = array( 'user_id' => strval( $user->ID ) );
1064 $user_fields = array(
1065 'username' => $user->user_login,
1066 'first_name' => $user->user_firstname,
1067 'last_name' => $user->user_lastname,
1068 'registered' => $this->_convert_date( $user->user_registered ),
1069 'bio' => $user->user_description,
1070 'email' => $user->user_email,
1071 'nickname' => $user->nickname,
1072 'nicename' => $user->user_nicename,
1073 'url' => $user->user_url,
1074 'display_name' => $user->display_name,
1075 'roles' => $user->roles,
1078 if ( in_array( 'all', $fields ) ) {
1079 $_user = array_merge( $_user, $user_fields );
1081 if ( in_array( 'basic', $fields ) ) {
1082 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
1083 $fields = array_merge( $fields, $basic_fields );
1085 $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
1086 $_user = array_merge( $_user, $requested_fields );
1090 * Filter XML-RPC-prepared data for the given user.
1094 * @param array $_user An array of user data.
1095 * @param WP_User $user User object.
1096 * @param array $fields An array of user fields.
1098 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
1102 * Create a new post for any registered post type.
1106 * @link http://en.wikipedia.org/wiki/RSS_enclosure for information on RSS enclosures.
1108 * @param array $args {
1109 * Method arguments. Note: top-level arguments must be ordered as documented.
1111 * @type int $blog_id Blog ID (unused).
1112 * @type string $username Username.
1113 * @type string $password Password.
1114 * @type array $content_struct {
1115 * Content struct for adding a new post. See wp_insert_post() for information on
1116 * additional post fields
1118 * @type string $post_type Post type. Default 'post'.
1119 * @type string $post_status Post status. Default 'draft'
1120 * @type string $post_title Post title.
1121 * @type int $post_author Post author ID.
1122 * @type string $post_excerpt Post excerpt.
1123 * @type string $post_content Post content.
1124 * @type string $post_date_gmt Post date in GMT.
1125 * @type string $post_date Post date.
1126 * @type string $post_password Post password (20-character limit).
1127 * @type string $comment_status Post comment enabled status. Accepts 'open' or 'closed'.
1128 * @type string $ping_status Post ping status. Accepts 'open' or 'closed'.
1129 * @type bool $sticky Whether the post should be sticky. Automatically false if
1130 * `$post_status` is 'private'.
1131 * @type int $post_thumbnail ID of an image to use as the post thumbnail/featured image.
1132 * @type array $custom_fields Array of meta key/value pairs to add to the post.
1133 * @type array $terms Associative array with taxonomy names as keys and arrays
1134 * of term IDs as values.
1135 * @type array $terms_names Associative array with taxonomy names as keys and arrays
1136 * of term names as values.
1137 * @type array $enclosure {
1138 * Array of feed enclosure data to add to post meta.
1140 * @type string $url URL for the feed enclosure.
1141 * @type int $length Size in bytes of the enclosure.
1142 * @type string $type Mime-type for the enclosure.
1146 * @return int|IXR_Error Post ID on success, IXR_Error instance otherwise.
1148 public function wp_newPost( $args ) {
1149 if ( ! $this->minimum_args( $args, 4 ) )
1150 return $this->error;
1152 $this->escape( $args );
1154 $username = $args[1];
1155 $password = $args[2];
1156 $content_struct = $args[3];
1158 if ( ! $user = $this->login( $username, $password ) )
1159 return $this->error;
1161 // convert the date field back to IXR form
1162 if ( isset( $content_struct['post_date'] ) && ! ( $content_struct['post_date'] instanceof IXR_Date ) ) {
1163 $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
1166 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1167 // since _insert_post will ignore the non-GMT date if the GMT date is set
1168 if ( isset( $content_struct['post_date_gmt'] ) && ! ( $content_struct['post_date_gmt'] instanceof IXR_Date ) ) {
1169 if ( $content_struct['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) {
1170 unset( $content_struct['post_date_gmt'] );
1172 $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
1176 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1177 do_action( 'xmlrpc_call', 'wp.newPost' );
1179 unset( $content_struct['ID'] );
1181 return $this->_insert_post( $user, $content_struct );
1185 * Helper method for filtering out elements from an array.
1189 * @param int $count Number to compare to one.
1191 private function _is_greater_than_one( $count ) {
1196 * Encapsulate the logic for sticking a post
1197 * and determining if the user has permission to do so
1202 * @param array $post_data
1203 * @param bool $update
1204 * @return void|IXR_Error
1206 private function _toggle_sticky( $post_data, $update = false ) {
1207 $post_type = get_post_type_object( $post_data['post_type'] );
1209 // Private and password-protected posts cannot be stickied.
1210 if ( 'private' === $post_data['post_status'] || ! empty( $post_data['post_password'] ) ) {
1211 // Error if the client tried to stick the post, otherwise, silently unstick.
1212 if ( ! empty( $post_data['sticky'] ) ) {
1213 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1217 unstick_post( $post_data['ID'] );
1219 } elseif ( isset( $post_data['sticky'] ) ) {
1220 if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) {
1221 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1224 $sticky = wp_validate_boolean( $post_data['sticky'] );
1226 stick_post( $post_data['ID'] );
1228 unstick_post( $post_data['ID'] );
1234 * Helper method for wp_newPost() and wp_editPost(), containing shared logic.
1239 * @see wp_insert_post()
1241 * @param WP_User $user The post author if post_author isn't set in $content_struct.
1242 * @param array|IXR_Error $content_struct Post data to insert.
1243 * @return IXR_Error|string
1245 protected function _insert_post( $user, $content_struct ) {
1246 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1247 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1249 $post_data = wp_parse_args( $content_struct, $defaults );
1251 $post_type = get_post_type_object( $post_data['post_type'] );
1253 return new IXR_Error( 403, __( 'Invalid post type' ) );
1255 $update = ! empty( $post_data['ID'] );
1258 if ( ! get_post( $post_data['ID'] ) )
1259 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1260 if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1261 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1262 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1263 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1265 if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1266 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1269 switch ( $post_data['post_status'] ) {
1274 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1275 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1279 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1280 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1283 if ( ! get_post_status_object( $post_data['post_status'] ) )
1284 $post_data['post_status'] = 'draft';
1288 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1289 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1291 $post_data['post_author'] = absint( $post_data['post_author'] );
1292 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1293 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1294 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1296 $author = get_userdata( $post_data['post_author'] );
1299 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1301 $post_data['post_author'] = $user->ID;
1304 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1305 unset( $post_data['comment_status'] );
1307 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1308 unset( $post_data['ping_status'] );
1310 // Do some timestamp voodoo.
1311 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1312 // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
1313 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1314 } elseif ( ! empty( $post_data['post_date'] ) ) {
1315 $dateCreated = $post_data['post_date']->getIso();
1318 if ( ! empty( $dateCreated ) ) {
1319 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1320 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1323 if ( ! isset( $post_data['ID'] ) )
1324 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1325 $post_ID = $post_data['ID'];
1327 if ( $post_data['post_type'] == 'post' ) {
1328 $error = $this->_toggle_sticky( $post_data, $update );
1334 if ( isset( $post_data['post_thumbnail'] ) ) {
1335 // empty value deletes, non-empty value adds/updates.
1336 if ( ! $post_data['post_thumbnail'] )
1337 delete_post_thumbnail( $post_ID );
1338 elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1339 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1340 set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1341 unset( $content_struct['post_thumbnail'] );
1344 if ( isset( $post_data['custom_fields'] ) )
1345 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1347 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1348 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1350 // Accumulate term IDs from terms and terms_names.
1353 // First validate the terms specified by ID.
1354 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1355 $taxonomies = array_keys( $post_data['terms'] );
1357 // Validating term ids.
1358 foreach ( $taxonomies as $taxonomy ) {
1359 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1360 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1362 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1363 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1365 $term_ids = $post_data['terms'][$taxonomy];
1366 $terms[ $taxonomy ] = array();
1367 foreach ( $term_ids as $term_id ) {
1368 $term = get_term_by( 'id', $term_id, $taxonomy );
1371 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1373 $terms[$taxonomy][] = (int) $term_id;
1378 // Now validate terms specified by name.
1379 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1380 $taxonomies = array_keys( $post_data['terms_names'] );
1382 foreach ( $taxonomies as $taxonomy ) {
1383 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1384 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1386 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1387 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1390 * For hierarchical taxonomies, we can't assign a term when multiple terms
1391 * in the hierarchy share the same name.
1393 $ambiguous_terms = array();
1394 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1395 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1397 // Count the number of terms with the same name.
1398 $tax_term_names_count = array_count_values( $tax_term_names );
1400 // Filter out non-ambiguous term names.
1401 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1403 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1406 $term_names = $post_data['terms_names'][$taxonomy];
1407 foreach ( $term_names as $term_name ) {
1408 if ( in_array( $term_name, $ambiguous_terms ) )
1409 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1411 $term = get_term_by( 'name', $term_name, $taxonomy );
1414 // Term doesn't exist, so check that the user is allowed to create new terms.
1415 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1416 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1418 // Create the new term.
1419 $term_info = wp_insert_term( $term_name, $taxonomy );
1420 if ( is_wp_error( $term_info ) )
1421 return new IXR_Error( 500, $term_info->get_error_message() );
1423 $terms[$taxonomy][] = (int) $term_info['term_id'];
1425 $terms[$taxonomy][] = (int) $term->term_id;
1431 $post_data['tax_input'] = $terms;
1432 unset( $post_data['terms'], $post_data['terms_names'] );
1434 // Do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'.
1435 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1438 if ( isset( $post_data['post_format'] ) ) {
1439 $format = set_post_format( $post_ID, $post_data['post_format'] );
1441 if ( is_wp_error( $format ) )
1442 return new IXR_Error( 500, $format->get_error_message() );
1444 unset( $post_data['post_format'] );
1447 // Handle enclosures.
1448 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1449 $this->add_enclosure_if_new( $post_ID, $enclosure );
1451 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1454 * Filter post data array to be inserted via XML-RPC.
1458 * @param array $post_data Parsed array of post data.
1459 * @param array $content_struct Post data array.
1461 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1463 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1464 if ( is_wp_error( $post_ID ) )
1465 return new IXR_Error( 500, $post_ID->get_error_message() );
1468 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1470 return strval( $post_ID );
1474 * Edit a post for any registered post type.
1476 * The $content_struct parameter only needs to contain fields that
1477 * should be changed. All other fields will retain their existing values.
1481 * @param array $args {
1482 * Method arguments. Note: arguments must be ordered as documented.
1484 * @type int $blog_id Blog ID (unused).
1485 * @type string $username Username.
1486 * @type string $password Password.
1487 * @type int $post_id Post ID.
1488 * @type array $content_struct Extra content arguments.
1490 * @return true|IXR_Error True on success, IXR_Error on failure.
1492 public function wp_editPost( $args ) {
1493 if ( ! $this->minimum_args( $args, 5 ) )
1494 return $this->error;
1496 $this->escape( $args );
1498 $username = $args[1];
1499 $password = $args[2];
1500 $post_id = (int) $args[3];
1501 $content_struct = $args[4];
1503 if ( ! $user = $this->login( $username, $password ) )
1504 return $this->error;
1506 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1507 do_action( 'xmlrpc_call', 'wp.editPost' );
1509 $post = get_post( $post_id, ARRAY_A );
1511 if ( empty( $post['ID'] ) )
1512 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1514 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1515 // If the post has been modified since the date provided, return an error.
1516 if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1517 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1521 // Convert the date field back to IXR form.
1522 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1525 * Ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1526 * since _insert_post() will ignore the non-GMT date if the GMT date is set.
1528 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1529 unset( $post['post_date_gmt'] );
1531 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1533 $this->escape( $post );
1534 $merged_content_struct = array_merge( $post, $content_struct );
1536 $retval = $this->_insert_post( $user, $merged_content_struct );
1537 if ( $retval instanceof IXR_Error )
1544 * Delete a post for any registered post type.
1548 * @see wp_delete_post()
1550 * @param array $args {
1551 * Method arguments. Note: arguments must be ordered as documented.
1553 * @type int $blog_id Blog ID (unused).
1554 * @type string $username Username.
1555 * @type string $password Password.
1556 * @type int $post_id Post ID.
1558 * @return true|IXR_Error True on success, IXR_Error instance on failure.
1560 public function wp_deletePost( $args ) {
1561 if ( ! $this->minimum_args( $args, 4 ) )
1562 return $this->error;
1564 $this->escape( $args );
1566 $username = $args[1];
1567 $password = $args[2];
1568 $post_id = (int) $args[3];
1570 if ( ! $user = $this->login( $username, $password ) )
1571 return $this->error;
1573 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1574 do_action( 'xmlrpc_call', 'wp.deletePost' );
1576 $post = get_post( $post_id, ARRAY_A );
1577 if ( empty( $post['ID'] ) )
1578 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1580 if ( ! current_user_can( 'delete_post', $post_id ) )
1581 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1583 $result = wp_delete_post( $post_id );
1586 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1596 * The optional $fields parameter specifies what fields will be included
1597 * in the response array. This should be a list of field names. 'post_id' will
1598 * always be included in the response regardless of the value of $fields.
1600 * Instead of, or in addition to, individual field names, conceptual group
1601 * names can be used to specify multiple fields. The available conceptual
1602 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1607 * @param array $args {
1608 * Method arguments. Note: arguments must be ordered as documented.
1610 * @type int $blog_id Blog ID (unused).
1611 * @type string $username Username.
1612 * @type string $password Password.
1613 * @type int $post_id Post ID.
1614 * @type array $fields The subset of post type fields to return.
1616 * @return array|IXR_Error Array contains (based on $fields parameter):
1622 * - 'post_modified_gmt'
1631 * - 'comment_status'
1640 public function wp_getPost( $args ) {
1641 if ( ! $this->minimum_args( $args, 4 ) )
1642 return $this->error;
1644 $this->escape( $args );
1646 $username = $args[1];
1647 $password = $args[2];
1648 $post_id = (int) $args[3];
1650 if ( isset( $args[4] ) ) {
1654 * Filter the list of post query fields used by the given XML-RPC method.
1658 * @param array $fields Array of post fields. Default array contains 'post', 'terms', and 'custom_fields'.
1659 * @param string $method Method name.
1661 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1664 if ( ! $user = $this->login( $username, $password ) )
1665 return $this->error;
1667 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1668 do_action( 'xmlrpc_call', 'wp.getPost' );
1670 $post = get_post( $post_id, ARRAY_A );
1672 if ( empty( $post['ID'] ) )
1673 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1675 if ( ! current_user_can( 'edit_post', $post_id ) )
1676 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1678 return $this->_prepare_post( $post, $fields );
1686 * @see wp_get_recent_posts()
1687 * @see wp_getPost() for more on `$fields`
1688 * @see get_posts() for more on `$filter` values
1690 * @param array $args {
1691 * Method arguments. Note: arguments must be ordered as documented.
1693 * @type int $blog_id Blog ID (unused).
1694 * @type string $username Username.
1695 * @type string $password Password.
1696 * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'post_type',
1697 * 'post_status', 'number', 'offset', 'orderby', and 'order'.
1698 * Default empty array.
1699 * @type array $fields Optional. The subset of post type fields to return in the response array.
1701 * @return array|IXR_Error Array contains a collection of posts.
1703 public function wp_getPosts( $args ) {
1704 if ( ! $this->minimum_args( $args, 3 ) )
1705 return $this->error;
1707 $this->escape( $args );
1709 $username = $args[1];
1710 $password = $args[2];
1711 $filter = isset( $args[3] ) ? $args[3] : array();
1713 if ( isset( $args[4] ) ) {
1716 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1717 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1720 if ( ! $user = $this->login( $username, $password ) )
1721 return $this->error;
1723 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1724 do_action( 'xmlrpc_call', 'wp.getPosts' );
1728 if ( isset( $filter['post_type'] ) ) {
1729 $post_type = get_post_type_object( $filter['post_type'] );
1730 if ( ! ( (bool) $post_type ) )
1731 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1733 $post_type = get_post_type_object( 'post' );
1736 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1737 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1739 $query['post_type'] = $post_type->name;
1741 if ( isset( $filter['post_status'] ) )
1742 $query['post_status'] = $filter['post_status'];
1744 if ( isset( $filter['number'] ) )
1745 $query['numberposts'] = absint( $filter['number'] );
1747 if ( isset( $filter['offset'] ) )
1748 $query['offset'] = absint( $filter['offset'] );
1750 if ( isset( $filter['orderby'] ) ) {
1751 $query['orderby'] = $filter['orderby'];
1753 if ( isset( $filter['order'] ) )
1754 $query['order'] = $filter['order'];
1757 if ( isset( $filter['s'] ) ) {
1758 $query['s'] = $filter['s'];
1761 $posts_list = wp_get_recent_posts( $query );
1763 if ( ! $posts_list )
1766 // Holds all the posts data.
1769 foreach ( $posts_list as $post ) {
1770 if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1773 $struct[] = $this->_prepare_post( $post, $fields );
1780 * Create a new term.
1784 * @see wp_insert_term()
1786 * @param array $args {
1787 * Method arguments. Note: arguments must be ordered as documented.
1789 * @type int $blog_id Blog ID (unused).
1790 * @type string $username Username.
1791 * @type string $password Password.
1792 * @type array $content_struct Content struct for adding a new term. The struct must contain
1793 * the term 'name' and 'taxonomy'. Optional accepted values include
1794 * 'parent', 'description', and 'slug'.
1796 * @return int|IXR_Error The term ID on success, or an IXR_Error object on failure.
1798 public function wp_newTerm( $args ) {
1799 if ( ! $this->minimum_args( $args, 4 ) )
1800 return $this->error;
1802 $this->escape( $args );
1804 $username = $args[1];
1805 $password = $args[2];
1806 $content_struct = $args[3];
1808 if ( ! $user = $this->login( $username, $password ) )
1809 return $this->error;
1811 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1812 do_action( 'xmlrpc_call', 'wp.newTerm' );
1814 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1815 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1817 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1819 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1820 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1822 $taxonomy = (array) $taxonomy;
1824 // hold the data of the term
1825 $term_data = array();
1827 $term_data['name'] = trim( $content_struct['name'] );
1828 if ( empty( $term_data['name'] ) )
1829 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1831 if ( isset( $content_struct['parent'] ) ) {
1832 if ( ! $taxonomy['hierarchical'] )
1833 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1835 $parent_term_id = (int) $content_struct['parent'];
1836 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1838 if ( is_wp_error( $parent_term ) )
1839 return new IXR_Error( 500, $parent_term->get_error_message() );
1841 if ( ! $parent_term )
1842 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1844 $term_data['parent'] = $content_struct['parent'];
1847 if ( isset( $content_struct['description'] ) )
1848 $term_data['description'] = $content_struct['description'];
1850 if ( isset( $content_struct['slug'] ) )
1851 $term_data['slug'] = $content_struct['slug'];
1853 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1855 if ( is_wp_error( $term ) )
1856 return new IXR_Error( 500, $term->get_error_message() );
1859 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1861 return strval( $term['term_id'] );
1869 * @see wp_update_term()
1871 * @param array $args {
1872 * Method arguments. Note: arguments must be ordered as documented.
1874 * @type int $blog_id Blog ID (unused).
1875 * @type string $username Username.
1876 * @type string $password Password.
1877 * @type int $term_id Term ID.
1878 * @type array $content_struct Content struct for editing a term. The struct must contain the
1879 * term ''taxonomy'. Optional accepted values include 'name', 'parent',
1880 * 'description', and 'slug'.
1882 * @return true|IXR_Error True on success, IXR_Error instance on failure.
1884 public function wp_editTerm( $args ) {
1885 if ( ! $this->minimum_args( $args, 5 ) )
1886 return $this->error;
1888 $this->escape( $args );
1890 $username = $args[1];
1891 $password = $args[2];
1892 $term_id = (int) $args[3];
1893 $content_struct = $args[4];
1895 if ( ! $user = $this->login( $username, $password ) )
1896 return $this->error;
1898 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1899 do_action( 'xmlrpc_call', 'wp.editTerm' );
1901 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1902 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1904 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1906 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1907 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1909 $taxonomy = (array) $taxonomy;
1911 // hold the data of the term
1912 $term_data = array();
1914 $term = get_term( $term_id , $content_struct['taxonomy'] );
1916 if ( is_wp_error( $term ) )
1917 return new IXR_Error( 500, $term->get_error_message() );
1920 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1922 if ( isset( $content_struct['name'] ) ) {
1923 $term_data['name'] = trim( $content_struct['name'] );
1925 if ( empty( $term_data['name'] ) )
1926 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1929 if ( isset( $content_struct['parent'] ) ) {
1930 if ( ! $taxonomy['hierarchical'] )
1931 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1933 $parent_term_id = (int) $content_struct['parent'];
1934 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1936 if ( is_wp_error( $parent_term ) )
1937 return new IXR_Error( 500, $parent_term->get_error_message() );
1939 if ( ! $parent_term )
1940 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1942 $term_data['parent'] = $content_struct['parent'];
1945 if ( isset( $content_struct['description'] ) )
1946 $term_data['description'] = $content_struct['description'];
1948 if ( isset( $content_struct['slug'] ) )
1949 $term_data['slug'] = $content_struct['slug'];
1951 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1953 if ( is_wp_error( $term ) )
1954 return new IXR_Error( 500, $term->get_error_message() );
1957 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1967 * @see wp_delete_term()
1969 * @param array $args {
1970 * Method arguments. Note: arguments must be ordered as documented.
1972 * @type int $blog_id Blog ID (unused).
1973 * @type string $username Username.
1974 * @type string $password Password.
1975 * @type string $taxnomy_name Taxonomy name.
1976 * @type int $term_id Term ID.
1978 * @return bool|IXR_Error True on success, IXR_Error instance on failure.
1980 public function wp_deleteTerm( $args ) {
1981 if ( ! $this->minimum_args( $args, 5 ) )
1982 return $this->error;
1984 $this->escape( $args );
1986 $username = $args[1];
1987 $password = $args[2];
1988 $taxonomy = $args[3];
1989 $term_id = (int) $args[4];
1991 if ( ! $user = $this->login( $username, $password ) )
1992 return $this->error;
1994 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1995 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1997 if ( ! taxonomy_exists( $taxonomy ) )
1998 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2000 $taxonomy = get_taxonomy( $taxonomy );
2002 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
2003 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
2005 $term = get_term( $term_id, $taxonomy->name );
2007 if ( is_wp_error( $term ) )
2008 return new IXR_Error( 500, $term->get_error_message() );
2011 return new IXR_Error( 404, __( 'Invalid term ID' ) );
2013 $result = wp_delete_term( $term_id, $taxonomy->name );
2015 if ( is_wp_error( $result ) )
2016 return new IXR_Error( 500, $term->get_error_message() );
2019 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
2031 * @param array $args {
2032 * Method arguments. Note: arguments must be ordered as documented.
2034 * @type int $blog_id Blog ID (unused).
2035 * @type string $username Username.
2036 * @type string $password Password.
2037 * @type string $taxnomy Taxonomy name.
2038 * @type string $term_id Term ID.
2040 * @return array|IXR_Error IXR_Error on failure, array on success, containing:
2045 * - 'term_taxonomy_id'
2051 public function wp_getTerm( $args ) {
2052 if ( ! $this->minimum_args( $args, 5 ) )
2053 return $this->error;
2055 $this->escape( $args );
2057 $username = $args[1];
2058 $password = $args[2];
2059 $taxonomy = $args[3];
2060 $term_id = (int) $args[4];
2062 if ( ! $user = $this->login( $username, $password ) )
2063 return $this->error;
2065 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2066 do_action( 'xmlrpc_call', 'wp.getTerm' );
2068 if ( ! taxonomy_exists( $taxonomy ) )
2069 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2071 $taxonomy = get_taxonomy( $taxonomy );
2073 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2074 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2076 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
2078 if ( is_wp_error( $term ) )
2079 return new IXR_Error( 500, $term->get_error_message() );
2082 return new IXR_Error( 404, __( 'Invalid term ID' ) );
2084 return $this->_prepare_term( $term );
2088 * Retrieve all terms for a taxonomy.
2092 * The optional $filter parameter modifies the query used to retrieve terms.
2093 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
2097 * @param array $args {
2098 * Method arguments. Note: arguments must be ordered as documented.
2100 * @type int $blog_id Blog ID (unused).
2101 * @type string $username Username.
2102 * @type string $password Password.
2103 * @type string $taxnomy Taxonomy name.
2104 * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'number',
2105 * 'offset', 'orderby', 'order', 'hide_empty', and 'search'. Default empty array.
2107 * @return array|IXR_Error An associative array of terms data on success, IXR_Error instance otherwise.
2109 public function wp_getTerms( $args ) {
2110 if ( ! $this->minimum_args( $args, 4 ) )
2111 return $this->error;
2113 $this->escape( $args );
2115 $username = $args[1];
2116 $password = $args[2];
2117 $taxonomy = $args[3];
2118 $filter = isset( $args[4] ) ? $args[4] : array();
2120 if ( ! $user = $this->login( $username, $password ) )
2121 return $this->error;
2123 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2124 do_action( 'xmlrpc_call', 'wp.getTerms' );
2126 if ( ! taxonomy_exists( $taxonomy ) )
2127 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2129 $taxonomy = get_taxonomy( $taxonomy );
2131 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2132 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2136 if ( isset( $filter['number'] ) )
2137 $query['number'] = absint( $filter['number'] );
2139 if ( isset( $filter['offset'] ) )
2140 $query['offset'] = absint( $filter['offset'] );
2142 if ( isset( $filter['orderby'] ) ) {
2143 $query['orderby'] = $filter['orderby'];
2145 if ( isset( $filter['order'] ) )
2146 $query['order'] = $filter['order'];
2149 if ( isset( $filter['hide_empty'] ) )
2150 $query['hide_empty'] = $filter['hide_empty'];
2152 $query['get'] = 'all';
2154 if ( isset( $filter['search'] ) )
2155 $query['search'] = $filter['search'];
2157 $terms = get_terms( $taxonomy->name, $query );
2159 if ( is_wp_error( $terms ) )
2160 return new IXR_Error( 500, $terms->get_error_message() );
2164 foreach ( $terms as $term ) {
2165 $struct[] = $this->_prepare_term( $term );
2172 * Retrieve a taxonomy.
2176 * @see get_taxonomy()
2178 * @param array $args {
2179 * Method arguments. Note: arguments must be ordered as documented.
2181 * @type int $blog_id Blog ID (unused).
2182 * @type string $username Username.
2183 * @type string $password Password.
2184 * @type string $taxnomy Taxonomy name.
2185 * @type array $fields Optional. Array of taxonomy fields to limit to in the return.
2186 * Accepts 'labels', 'cap', 'menu', and 'object_type'.
2187 * Default empty array.
2189 * @return array|IXR_Error An array of taxonomy data on success, IXR_Error instance otherwise.
2191 public function wp_getTaxonomy( $args ) {
2192 if ( ! $this->minimum_args( $args, 4 ) )
2193 return $this->error;
2195 $this->escape( $args );
2197 $username = $args[1];
2198 $password = $args[2];
2199 $taxonomy = $args[3];
2201 if ( isset( $args[4] ) ) {
2205 * Filter the taxonomy query fields used by the given XML-RPC method.
2209 * @param array $fields An array of taxonomy fields to retrieve.
2210 * @param string $method The method name.
2212 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
2215 if ( ! $user = $this->login( $username, $password ) )
2216 return $this->error;
2218 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2219 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
2221 if ( ! taxonomy_exists( $taxonomy ) )
2222 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2224 $taxonomy = get_taxonomy( $taxonomy );
2226 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2227 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2229 return $this->_prepare_taxonomy( $taxonomy, $fields );
2233 * Retrieve all taxonomies.
2237 * @see get_taxonomies()
2239 * @param array $args {
2240 * Method arguments. Note: arguments must be ordered as documented.
2242 * @type int $blog_id Blog ID (unused).
2243 * @type string $username Username.
2244 * @type string $password Password.
2245 * @type array $filter Optional. An array of arguments for retrieving taxonomies.
2246 * @type array $fields Optional. The subset of taxonomy fields to return.
2248 * @return array|IXR_Error An associative array of taxonomy data with returned fields determined
2249 * by `$fields`, or an IXR_Error instance on failure.
2251 public function wp_getTaxonomies( $args ) {
2252 if ( ! $this->minimum_args( $args, 3 ) )
2253 return $this->error;
2255 $this->escape( $args );
2257 $username = $args[1];
2258 $password = $args[2];
2259 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2261 if ( isset( $args[4] ) ) {
2264 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2265 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
2268 if ( ! $user = $this->login( $username, $password ) )
2269 return $this->error;
2271 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2272 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
2274 $taxonomies = get_taxonomies( $filter, 'objects' );
2276 // holds all the taxonomy data
2279 foreach ( $taxonomies as $taxonomy ) {
2280 // capability check for post_types
2281 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2284 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
2293 * The optional $fields parameter specifies what fields will be included
2294 * in the response array. This should be a list of field names. 'user_id' will
2295 * always be included in the response regardless of the value of $fields.
2297 * Instead of, or in addition to, individual field names, conceptual group
2298 * names can be used to specify multiple fields. The available conceptual
2299 * groups are 'basic' and 'all'.
2301 * @uses get_userdata()
2303 * @param array $args {
2304 * Method arguments. Note: arguments must be ordered as documented.
2306 * @type int $blog_id (unused)
2307 * @type string $username
2308 * @type string $password
2309 * @type int $user_id
2310 * @type array $fields (optional)
2312 * @return array|IXR_Error Array contains (based on $fields parameter):
2326 public function wp_getUser( $args ) {
2327 if ( ! $this->minimum_args( $args, 4 ) )
2328 return $this->error;
2330 $this->escape( $args );
2332 $username = $args[1];
2333 $password = $args[2];
2334 $user_id = (int) $args[3];
2336 if ( isset( $args[4] ) ) {
2340 * Filter the default user query fields used by the given XML-RPC method.
2344 * @param array $fields User query fields for given method. Default 'all'.
2345 * @param string $method The method name.
2347 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2350 if ( ! $user = $this->login( $username, $password ) )
2351 return $this->error;
2353 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2354 do_action( 'xmlrpc_call', 'wp.getUser' );
2356 if ( ! current_user_can( 'edit_user', $user_id ) )
2357 return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2359 $user_data = get_userdata( $user_id );
2362 return new IXR_Error( 404, __( 'Invalid user ID.' ) );
2364 return $this->_prepare_user( $user_data, $fields );
2370 * The optional $filter parameter modifies the query used to retrieve users.
2371 * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2372 * 'who', 'orderby', and 'order'.
2374 * The optional $fields parameter specifies what fields will be included
2375 * in the response array.
2378 * @see wp_getUser() for more on $fields and return values
2380 * @param array $args {
2381 * Method arguments. Note: arguments must be ordered as documented.
2383 * @type int $blog_id (unused)
2384 * @type string $username
2385 * @type string $password
2386 * @type array $filter (optional)
2387 * @type array $fields (optional)
2389 * @return array|IXR_Error users data
2391 public function wp_getUsers( $args ) {
2392 if ( ! $this->minimum_args( $args, 3 ) )
2393 return $this->error;
2395 $this->escape( $args );
2397 $username = $args[1];
2398 $password = $args[2];
2399 $filter = isset( $args[3] ) ? $args[3] : array();
2401 if ( isset( $args[4] ) ) {
2404 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2405 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2408 if ( ! $user = $this->login( $username, $password ) )
2409 return $this->error;
2411 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2412 do_action( 'xmlrpc_call', 'wp.getUsers' );
2414 if ( ! current_user_can( 'list_users' ) )
2415 return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
2417 $query = array( 'fields' => 'all_with_meta' );
2419 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2420 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2422 if ( isset( $filter['orderby'] ) ) {
2423 $query['orderby'] = $filter['orderby'];
2425 if ( isset( $filter['order'] ) )
2426 $query['order'] = $filter['order'];
2429 if ( isset( $filter['role'] ) ) {
2430 if ( get_role( $filter['role'] ) === null )
2431 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2433 $query['role'] = $filter['role'];
2436 if ( isset( $filter['who'] ) ) {
2437 $query['who'] = $filter['who'];
2440 $users = get_users( $query );
2443 foreach ( $users as $user_data ) {
2444 if ( current_user_can( 'edit_user', $user_data->ID ) )
2445 $_users[] = $this->_prepare_user( $user_data, $fields );
2451 * Retrieve information about the requesting user.
2453 * @uses get_userdata()
2455 * @param array $args {
2456 * Method arguments. Note: arguments must be ordered as documented.
2458 * @type int $blog_id (unused)
2459 * @type string $username
2460 * @type string $password
2461 * @type array $fields (optional)
2463 * @return array|IXR_Error (@see wp_getUser)
2465 public function wp_getProfile( $args ) {
2466 if ( ! $this->minimum_args( $args, 3 ) )
2467 return $this->error;
2469 $this->escape( $args );
2471 $username = $args[1];
2472 $password = $args[2];
2474 if ( isset( $args[3] ) ) {
2477 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2478 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2481 if ( ! $user = $this->login( $username, $password ) )
2482 return $this->error;
2484 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2485 do_action( 'xmlrpc_call', 'wp.getProfile' );
2487 if ( ! current_user_can( 'edit_user', $user->ID ) )
2488 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2490 $user_data = get_userdata( $user->ID );
2492 return $this->_prepare_user( $user_data, $fields );
2496 * Edit user's profile.
2498 * @uses wp_update_user()
2500 * @param array $args {
2501 * Method arguments. Note: arguments must be ordered as documented.
2503 * @type int $blog_id (unused)
2504 * @type string $username
2505 * @type string $password
2506 * @type array $content_struct It can optionally contain:
2515 * @return true|IXR_Error True, on success.
2517 public function wp_editProfile( $args ) {
2518 if ( ! $this->minimum_args( $args, 4 ) )
2519 return $this->error;
2521 $this->escape( $args );
2523 $username = $args[1];
2524 $password = $args[2];
2525 $content_struct = $args[3];
2527 if ( ! $user = $this->login( $username, $password ) )
2528 return $this->error;
2530 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2531 do_action( 'xmlrpc_call', 'wp.editProfile' );
2533 if ( ! current_user_can( 'edit_user', $user->ID ) )
2534 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2536 // holds data of the user
2537 $user_data = array();
2538 $user_data['ID'] = $user->ID;
2540 // only set the user details if it was given
2541 if ( isset( $content_struct['first_name'] ) )
2542 $user_data['first_name'] = $content_struct['first_name'];
2544 if ( isset( $content_struct['last_name'] ) )
2545 $user_data['last_name'] = $content_struct['last_name'];
2547 if ( isset( $content_struct['url'] ) )
2548 $user_data['user_url'] = $content_struct['url'];
2550 if ( isset( $content_struct['display_name'] ) )
2551 $user_data['display_name'] = $content_struct['display_name'];
2553 if ( isset( $content_struct['nickname'] ) )
2554 $user_data['nickname'] = $content_struct['nickname'];
2556 if ( isset( $content_struct['nicename'] ) )
2557 $user_data['user_nicename'] = $content_struct['nicename'];
2559 if ( isset( $content_struct['bio'] ) )
2560 $user_data['description'] = $content_struct['bio'];
2562 $result = wp_update_user( $user_data );
2564 if ( is_wp_error( $result ) )
2565 return new IXR_Error( 500, $result->get_error_message() );
2568 return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2578 * @param array $args {
2579 * Method arguments. Note: arguments must be ordered as documented.
2581 * @type int $blog_id (unused)
2582 * @type int $page_id
2583 * @type string $username
2584 * @type string $password
2586 * @return array|IXR_Error
2588 public function wp_getPage( $args ) {
2589 $this->escape( $args );
2591 $page_id = (int) $args[1];
2592 $username = $args[2];
2593 $password = $args[3];
2595 if ( !$user = $this->login($username, $password) ) {
2596 return $this->error;
2599 $page = get_post($page_id);
2601 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2603 if ( !current_user_can( 'edit_page', $page_id ) )
2604 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2606 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2607 do_action( 'xmlrpc_call', 'wp.getPage' );
2609 // If we found the page then format the data.
2610 if ( $page->ID && ($page->post_type == 'page') ) {
2611 return $this->_prepare_page( $page );
2613 // If the page doesn't exist indicate that.
2615 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2624 * @param array $args {
2625 * Method arguments. Note: arguments must be ordered as documented.
2627 * @type int $blog_id (unused)
2628 * @type string $username
2629 * @type string $password
2630 * @type int $num_pages
2632 * @return array|IXR_Error
2634 public function wp_getPages( $args ) {
2635 $this->escape( $args );
2637 $username = $args[1];
2638 $password = $args[2];
2639 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
2641 if ( !$user = $this->login($username, $password) )
2642 return $this->error;
2644 if ( !current_user_can( 'edit_pages' ) )
2645 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2647 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2648 do_action( 'xmlrpc_call', 'wp.getPages' );
2650 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2651 $num_pages = count($pages);
2653 // If we have pages, put together their info.
2654 if ( $num_pages >= 1 ) {
2655 $pages_struct = array();
2657 foreach ($pages as $page) {
2658 if ( current_user_can( 'edit_page', $page->ID ) )
2659 $pages_struct[] = $this->_prepare_page( $page );
2662 return $pages_struct;
2673 * @see wp_xmlrpc_server::mw_newPost()
2675 * @param array $args {
2676 * Method arguments. Note: arguments must be ordered as documented.
2678 * @type int $blog_id (unused)
2679 * @type string $username
2680 * @type string $password
2681 * @type array $content_struct
2683 * @return int|IXR_Error
2685 public function wp_newPage( $args ) {
2686 // Items not escaped here will be escaped in newPost.
2687 $username = $this->escape( $args[1] );
2688 $password = $this->escape( $args[2] );
2690 if ( !$user = $this->login($username, $password) )
2691 return $this->error;
2693 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2694 do_action( 'xmlrpc_call', 'wp.newPage' );
2696 // Mark this as content for a page.
2697 $args[3]["post_type"] = 'page';
2699 // Let mw_newPost do all of the heavy lifting.
2700 return $this->mw_newPost( $args );
2708 * @param array $args {
2709 * Method arguments. Note: arguments must be ordered as documented.
2711 * @type int $blog_id (unused)
2712 * @type string $username
2713 * @type string $password
2714 * @type int $page_id
2716 * @return true|IXR_Error True, if success.
2718 public function wp_deletePage( $args ) {
2719 $this->escape( $args );
2721 $username = $args[1];
2722 $password = $args[2];
2723 $page_id = (int) $args[3];
2725 if ( !$user = $this->login($username, $password) )
2726 return $this->error;
2728 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2729 do_action( 'xmlrpc_call', 'wp.deletePage' );
2731 // Get the current page based on the page_id and
2732 // make sure it is a page and not a post.
2733 $actual_page = get_post($page_id, ARRAY_A);
2734 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2735 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2737 // Make sure the user can delete pages.
2738 if ( !current_user_can('delete_page', $page_id) )
2739 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete this page.' ) );
2741 // Attempt to delete the page.
2742 $result = wp_delete_post($page_id);
2744 return new IXR_Error( 500, __( 'Failed to delete the page.' ) );
2747 * Fires after a page has been successfully deleted via XML-RPC.
2751 * @param int $page_id ID of the deleted page.
2752 * @param array $args An array of arguments to delete the page.
2754 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2764 * @param array $args {
2765 * Method arguments. Note: arguments must be ordered as documented.
2767 * @type int $blog_id (unused)
2768 * @type int $page_id
2769 * @type string $username
2770 * @type string $password
2771 * @type string $content
2772 * @type string $publish
2774 * @return array|IXR_Error
2776 public function wp_editPage( $args ) {
2777 // Items will be escaped in mw_editPost.
2778 $page_id = (int) $args[1];
2779 $username = $args[2];
2780 $password = $args[3];
2781 $content = $args[4];
2782 $publish = $args[5];
2784 $escaped_username = $this->escape( $username );
2785 $escaped_password = $this->escape( $password );
2787 if ( !$user = $this->login( $escaped_username, $escaped_password ) ) {
2788 return $this->error;
2791 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2792 do_action( 'xmlrpc_call', 'wp.editPage' );
2794 // Get the page data and make sure it is a page.
2795 $actual_page = get_post($page_id, ARRAY_A);
2796 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2797 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2799 // Make sure the user is allowed to edit pages.
2800 if ( !current_user_can('edit_page', $page_id) )
2801 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this page.' ) );
2803 // Mark this as content for a page.
2804 $content['post_type'] = 'page';
2806 // Arrange args in the way mw_editPost understands.
2815 // Let mw_editPost do all of the heavy lifting.
2816 return $this->mw_editPost( $args );
2820 * Retrieve page list.
2824 * @global wpdb $wpdb
2826 * @param array $args {
2827 * Method arguments. Note: arguments must be ordered as documented.
2829 * @type int $blog_id (unused)
2830 * @type string $username
2831 * @type string $password
2833 * @return array|IXR_Error
2835 public function wp_getPageList( $args ) {
2838 $this->escape( $args );
2840 $username = $args[1];
2841 $password = $args[2];
2843 if ( !$user = $this->login($username, $password) )
2844 return $this->error;
2846 if ( !current_user_can( 'edit_pages' ) )
2847 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2849 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2850 do_action( 'xmlrpc_call', 'wp.getPageList' );
2852 // Get list of pages ids and titles
2853 $page_list = $wpdb->get_results("
2855 post_title page_title,
2856 post_parent page_parent_id,
2861 WHERE post_type = 'page'
2865 // The date needs to be formatted properly.
2866 $num_pages = count($page_list);
2867 for ( $i = 0; $i < $num_pages; $i++ ) {
2868 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2869 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2871 unset($page_list[$i]->post_date_gmt);
2872 unset($page_list[$i]->post_date);
2873 unset($page_list[$i]->post_status);
2880 * Retrieve authors list.
2884 * @param array $args {
2885 * Method arguments. Note: arguments must be ordered as documented.
2887 * @type int $blog_id (unused)
2888 * @type string $username
2889 * @type string $password
2891 * @return array|IXR_Error
2893 public function wp_getAuthors( $args ) {
2894 $this->escape( $args );
2896 $username = $args[1];
2897 $password = $args[2];
2899 if ( !$user = $this->login($username, $password) )
2900 return $this->error;
2902 if ( !current_user_can('edit_posts') )
2903 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
2905 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2906 do_action( 'xmlrpc_call', 'wp.getAuthors' );
2909 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2911 'user_id' => $user->ID,
2912 'user_login' => $user->user_login,
2913 'display_name' => $user->display_name
2921 * Get list of all tags
2925 * @param array $args {
2926 * Method arguments. Note: arguments must be ordered as documented.
2928 * @type int $blog_id (unused)
2929 * @type string $username
2930 * @type string $password
2932 * @return array|IXR_Error
2934 public function wp_getTags( $args ) {
2935 $this->escape( $args );
2937 $username = $args[1];
2938 $password = $args[2];
2940 if ( !$user = $this->login($username, $password) )
2941 return $this->error;
2943 if ( !current_user_can( 'edit_posts' ) )
2944 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2946 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2947 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2951 if ( $all_tags = get_tags() ) {
2952 foreach( (array) $all_tags as $tag ) {
2954 $struct['tag_id'] = $tag->term_id;
2955 $struct['name'] = $tag->name;
2956 $struct['count'] = $tag->count;
2957 $struct['slug'] = $tag->slug;
2958 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2959 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
2969 * Create new category.
2973 * @param array $args {
2974 * Method arguments. Note: arguments must be ordered as documented.
2976 * @type int $blog_id (unused)
2977 * @type string $username
2978 * @type string $password
2979 * @type array $category
2981 * @return int|IXR_Error Category ID.
2983 public function wp_newCategory( $args ) {
2984 $this->escape( $args );
2986 $username = $args[1];
2987 $password = $args[2];
2988 $category = $args[3];
2990 if ( !$user = $this->login($username, $password) )
2991 return $this->error;
2993 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2994 do_action( 'xmlrpc_call', 'wp.newCategory' );
2996 // Make sure the user is allowed to add a category.
2997 if ( !current_user_can('manage_categories') )
2998 return new IXR_Error(401, __('Sorry, you do not have the right to add a category.'));
3000 // If no slug was provided make it empty so that
3001 // WordPress will generate one.
3002 if ( empty($category['slug']) )
3003 $category['slug'] = '';
3005 // If no parent_id was provided make it empty
3006 // so that it will be a top level page (no parent).
3007 if ( !isset($category['parent_id']) )
3008 $category['parent_id'] = '';
3010 // If no description was provided make it empty.
3011 if ( empty($category["description"]) )
3012 $category["description"] = "";
3014 $new_category = array(
3015 'cat_name' => $category['name'],
3016 'category_nicename' => $category['slug'],
3017 'category_parent' => $category['parent_id'],
3018 'category_description' => $category['description']
3021 $cat_id = wp_insert_category($new_category, true);
3022 if ( is_wp_error( $cat_id ) ) {
3023 if ( 'term_exists' == $cat_id->get_error_code() )
3024 return (int) $cat_id->get_error_data();
3026 return new IXR_Error(500, __('Sorry, the new category failed.'));
3027 } elseif ( ! $cat_id ) {
3028 return new IXR_Error(500, __('Sorry, the new category failed.'));
3032 * Fires after a new category has been successfully created via XML-RPC.
3036 * @param int $cat_id ID of the new category.
3037 * @param array $args An array of new category arguments.
3039 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
3049 * @param array $args {
3050 * Method arguments. Note: arguments must be ordered as documented.
3052 * @type int $blog_id (unused)
3053 * @type string $username
3054 * @type string $password
3055 * @type int $category_id
3057 * @return bool|IXR_Error See {@link wp_delete_term()} for return info.
3059 public function wp_deleteCategory( $args ) {
3060 $this->escape( $args );
3062 $username = $args[1];
3063 $password = $args[2];
3064 $category_id = (int) $args[3];
3066 if ( !$user = $this->login($username, $password) )
3067 return $this->error;
3069 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3070 do_action( 'xmlrpc_call', 'wp.deleteCategory' );
3072 if ( !current_user_can('manage_categories') )
3073 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
3075 $status = wp_delete_term( $category_id, 'category' );
3077 if ( true == $status ) {
3079 * Fires after a category has been successfully deleted via XML-RPC.
3083 * @param int $category_id ID of the deleted category.
3084 * @param array $args An array of arguments to delete the category.
3086 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
3093 * Retrieve category list.
3097 * @param array $args {
3098 * Method arguments. Note: arguments must be ordered as documented.
3100 * @type int $blog_id (unused)
3101 * @type string $username
3102 * @type string $password
3103 * @type array $category
3104 * @type int $max_results
3106 * @return array|IXR_Error
3108 public function wp_suggestCategories( $args ) {
3109 $this->escape( $args );
3111 $username = $args[1];
3112 $password = $args[2];
3113 $category = $args[3];
3114 $max_results = (int) $args[4];
3116 if ( !$user = $this->login($username, $password) )
3117 return $this->error;
3119 if ( !current_user_can( 'edit_posts' ) )
3120 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
3122 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3123 do_action( 'xmlrpc_call', 'wp.suggestCategories' );
3125 $category_suggestions = array();
3126 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
3127 foreach ( (array) get_categories($args) as $cat ) {
3128 $category_suggestions[] = array(
3129 'category_id' => $cat->term_id,