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 {
37 * Register all of the XMLRPC methods that XMLRPC server understands.
39 * Sets up server and method property. Passes XMLRPC
40 * methods through the 'xmlrpc_methods' filter to allow plugins to extend
41 * or replace XMLRPC methods.
45 public function __construct() {
46 $this->methods = array(
48 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
49 'wp.newPost' => 'this:wp_newPost',
50 'wp.editPost' => 'this:wp_editPost',
51 'wp.deletePost' => 'this:wp_deletePost',
52 'wp.getPost' => 'this:wp_getPost',
53 'wp.getPosts' => 'this:wp_getPosts',
54 'wp.newTerm' => 'this:wp_newTerm',
55 'wp.editTerm' => 'this:wp_editTerm',
56 'wp.deleteTerm' => 'this:wp_deleteTerm',
57 'wp.getTerm' => 'this:wp_getTerm',
58 'wp.getTerms' => 'this:wp_getTerms',
59 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
60 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
61 'wp.getUser' => 'this:wp_getUser',
62 'wp.getUsers' => 'this:wp_getUsers',
63 'wp.getProfile' => 'this:wp_getProfile',
64 'wp.editProfile' => 'this:wp_editProfile',
65 'wp.getPage' => 'this:wp_getPage',
66 'wp.getPages' => 'this:wp_getPages',
67 'wp.newPage' => 'this:wp_newPage',
68 'wp.deletePage' => 'this:wp_deletePage',
69 'wp.editPage' => 'this:wp_editPage',
70 'wp.getPageList' => 'this:wp_getPageList',
71 'wp.getAuthors' => 'this:wp_getAuthors',
72 'wp.getCategories' => 'this:mw_getCategories', // Alias
73 'wp.getTags' => 'this:wp_getTags',
74 'wp.newCategory' => 'this:wp_newCategory',
75 'wp.deleteCategory' => 'this:wp_deleteCategory',
76 'wp.suggestCategories' => 'this:wp_suggestCategories',
77 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
78 'wp.deleteFile' => 'this:wp_deletePost', // Alias
79 'wp.getCommentCount' => 'this:wp_getCommentCount',
80 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
81 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
82 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
83 'wp.getOptions' => 'this:wp_getOptions',
84 'wp.setOptions' => 'this:wp_setOptions',
85 'wp.getComment' => 'this:wp_getComment',
86 'wp.getComments' => 'this:wp_getComments',
87 'wp.deleteComment' => 'this:wp_deleteComment',
88 'wp.editComment' => 'this:wp_editComment',
89 'wp.newComment' => 'this:wp_newComment',
90 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
91 'wp.getMediaItem' => 'this:wp_getMediaItem',
92 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
93 'wp.getPostFormats' => 'this:wp_getPostFormats',
94 'wp.getPostType' => 'this:wp_getPostType',
95 'wp.getPostTypes' => 'this:wp_getPostTypes',
96 'wp.getRevisions' => 'this:wp_getRevisions',
97 'wp.restoreRevision' => 'this:wp_restoreRevision',
100 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
101 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
102 'blogger.getPost' => 'this:blogger_getPost',
103 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
104 'blogger.newPost' => 'this:blogger_newPost',
105 'blogger.editPost' => 'this:blogger_editPost',
106 'blogger.deletePost' => 'this:blogger_deletePost',
108 // MetaWeblog API (with MT extensions to structs)
109 'metaWeblog.newPost' => 'this:mw_newPost',
110 'metaWeblog.editPost' => 'this:mw_editPost',
111 'metaWeblog.getPost' => 'this:mw_getPost',
112 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
113 'metaWeblog.getCategories' => 'this:mw_getCategories',
114 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
116 // MetaWeblog API aliases for Blogger API
117 // see http://www.xmlrpc.com/stories/storyReader$2460
118 'metaWeblog.deletePost' => 'this:blogger_deletePost',
119 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
122 'mt.getCategoryList' => 'this:mt_getCategoryList',
123 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
124 'mt.getPostCategories' => 'this:mt_getPostCategories',
125 'mt.setPostCategories' => 'this:mt_setPostCategories',
126 'mt.supportedMethods' => 'this:mt_supportedMethods',
127 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
128 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
129 'mt.publishPost' => 'this:mt_publishPost',
132 'pingback.ping' => 'this:pingback_ping',
133 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
135 'demo.sayHello' => 'this:sayHello',
136 'demo.addTwoNumbers' => 'this:addTwoNumbers'
139 $this->initialise_blog_option_info();
142 * Filter the methods exposed by the XML-RPC server.
144 * This filter can be used to add new methods, and remove built-in methods.
148 * @param array $methods An array of XML-RPC methods.
150 $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
154 * Make private/protected methods readable for backwards compatibility.
159 * @param callable $name Method to call.
160 * @param array $arguments Arguments to pass when calling.
161 * @return mixed|bool Return value of the callback, false otherwise.
163 public function __call( $name, $arguments ) {
164 if ( '_multisite_getUsersBlogs' === $name ) {
165 return call_user_func_array( array( $this, $name ), $arguments );
170 public function serve_request() {
171 $this->IXR_Server($this->methods);
175 * Test XMLRPC API by saying, "Hello!" to client.
179 * @param array $args Method Parameters.
182 public function sayHello($args) {
187 * Test XMLRPC API by adding two numbers for client.
191 * @param array $args Method Parameters.
194 public function addTwoNumbers($args) {
197 return $number1 + $number2;
205 * @param string $username User's username.
206 * @param string $password User's password.
207 * @return WP_User|bool WP_User object if authentication passed, false otherwise
209 public function login( $username, $password ) {
211 * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
212 * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
214 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
215 if ( false === $enabled ) {
216 $enabled = apply_filters( 'option_enable_xmlrpc', true );
220 * Filter whether XML-RPC is enabled.
222 * This is the proper filter for turning off XML-RPC.
226 * @param bool $enabled Whether XML-RPC is enabled. Default true.
228 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
231 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
235 $user = wp_authenticate($username, $password);
237 if (is_wp_error($user)) {
238 $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
241 * Filter the XML-RPC user login error message.
245 * @param string $error The XML-RPC error message.
246 * @param WP_User $user WP_User object.
248 $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
252 wp_set_current_user( $user->ID );
257 * Check user's credentials. Deprecated.
261 * @deprecated use wp_xmlrpc_server::login
262 * @see wp_xmlrpc_server::login
264 * @param string $username User's username.
265 * @param string $password User's password.
266 * @return bool Whether authentication passed.
268 public function login_pass_ok( $username, $password ) {
269 return (bool) $this->login( $username, $password );
273 * Escape string or array of strings for database.
277 * @param string|array $data Escape single string or array of strings.
278 * @return string|array Type matches $data and sanitized for the database.
280 public function escape( &$data ) {
281 if ( ! is_array( $data ) )
282 return wp_slash( $data );
284 foreach ( $data as &$v ) {
285 if ( is_array( $v ) )
287 elseif ( ! is_object( $v ) )
293 * Retrieve custom fields for post.
297 * @param int $post_id Post ID.
298 * @return array Custom fields, if exist.
300 public function get_custom_fields($post_id) {
301 $post_id = (int) $post_id;
303 $custom_fields = array();
305 foreach ( (array) has_meta($post_id) as $meta ) {
306 // Don't expose protected fields.
307 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
310 $custom_fields[] = array(
311 "id" => $meta['meta_id'],
312 "key" => $meta['meta_key'],
313 "value" => $meta['meta_value']
317 return $custom_fields;
321 * Set custom fields for post.
325 * @param int $post_id Post ID.
326 * @param array $fields Custom fields.
328 public function set_custom_fields($post_id, $fields) {
329 $post_id = (int) $post_id;
331 foreach ( (array) $fields as $meta ) {
332 if ( isset($meta['id']) ) {
333 $meta['id'] = (int) $meta['id'];
334 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
335 if ( isset($meta['key']) ) {
336 $meta['key'] = wp_unslash( $meta['key'] );
337 if ( $meta['key'] !== $pmeta->meta_key )
339 $meta['value'] = wp_unslash( $meta['value'] );
340 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
341 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
342 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
343 delete_metadata_by_mid( 'post', $meta['id'] );
345 } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
346 add_post_meta( $post_id, $meta['key'], $meta['value'] );
352 * Set up blog options property.
354 * Passes property through 'xmlrpc_blog_options' filter.
358 public function initialise_blog_option_info() {
361 $this->blog_options = array(
363 'software_name' => array(
364 'desc' => __( 'Software Name' ),
366 'value' => 'WordPress'
368 'software_version' => array(
369 'desc' => __( 'Software Version' ),
371 'value' => $wp_version
374 'desc' => __( 'WordPress Address (URL)' ),
376 'option' => 'siteurl'
379 'desc' => __( 'Site Address (URL)' ),
383 'login_url' => array(
384 'desc' => __( 'Login Address (URL)' ),
386 'value' => wp_login_url( )
388 'admin_url' => array(
389 'desc' => __( 'The URL to the admin area' ),
391 'value' => get_admin_url( )
393 'image_default_link_type' => array(
394 'desc' => __( 'Image default link type' ),
396 'option' => 'image_default_link_type'
398 'image_default_size' => array(
399 'desc' => __( 'Image default size' ),
401 'option' => 'image_default_size'
403 'image_default_align' => array(
404 'desc' => __( 'Image default align' ),
406 'option' => 'image_default_align'
409 'desc' => __( 'Template' ),
411 'option' => 'template'
413 'stylesheet' => array(
414 'desc' => __( 'Stylesheet' ),
416 'option' => 'stylesheet'
418 'post_thumbnail' => array(
419 'desc' => __('Post Thumbnail'),
421 'value' => current_theme_supports( 'post-thumbnails' )
425 'time_zone' => array(
426 'desc' => __( 'Time Zone' ),
428 'option' => 'gmt_offset'
430 'blog_title' => array(
431 'desc' => __( 'Site Title' ),
433 'option' => 'blogname'
435 'blog_tagline' => array(
436 'desc' => __( 'Site Tagline' ),
438 'option' => 'blogdescription'
440 'date_format' => array(
441 'desc' => __( 'Date Format' ),
443 'option' => 'date_format'
445 'time_format' => array(
446 'desc' => __( 'Time Format' ),
448 'option' => 'time_format'
450 'users_can_register' => array(
451 'desc' => __( 'Allow new users to sign up' ),
453 'option' => 'users_can_register'
455 'thumbnail_size_w' => array(
456 'desc' => __( 'Thumbnail Width' ),
458 'option' => 'thumbnail_size_w'
460 'thumbnail_size_h' => array(
461 'desc' => __( 'Thumbnail Height' ),
463 'option' => 'thumbnail_size_h'
465 'thumbnail_crop' => array(
466 'desc' => __( 'Crop thumbnail to exact dimensions' ),
468 'option' => 'thumbnail_crop'
470 'medium_size_w' => array(
471 'desc' => __( 'Medium size image width' ),
473 'option' => 'medium_size_w'
475 'medium_size_h' => array(
476 'desc' => __( 'Medium size image height' ),
478 'option' => 'medium_size_h'
480 'large_size_w' => array(
481 'desc' => __( 'Large size image width' ),
483 'option' => 'large_size_w'
485 'large_size_h' => array(
486 'desc' => __( 'Large size image height' ),
488 'option' => 'large_size_h'
490 'default_comment_status' => array(
491 'desc' => __( 'Allow people to post comments on new articles' ),
493 'option' => 'default_comment_status'
495 'default_ping_status' => array(
496 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
498 'option' => 'default_ping_status'
503 * Filter the XML-RPC blog options property.
507 * @param array $blog_options An array of XML-RPC blog options.
509 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
513 * Retrieve the blogs of the user.
517 * @param array $args Method parameters. Contains:
520 * @return array|IXR_Error Array contains:
525 * - 'xmlrpc' - url of xmlrpc endpoint
527 public function wp_getUsersBlogs( $args ) {
528 // If this isn't on WPMU then just use blogger_getUsersBlogs
529 if ( !is_multisite() ) {
530 array_unshift( $args, 1 );
531 return $this->blogger_getUsersBlogs( $args );
534 $this->escape( $args );
536 $username = $args[0];
537 $password = $args[1];
539 if ( !$user = $this->login($username, $password) )
543 * Fires after the XML-RPC user has been authenticated but before the rest of
544 * the method logic begins.
546 * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
547 * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
551 * @param method $name The method name.
553 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
555 $blogs = (array) get_blogs_of_user( $user->ID );
558 foreach ( $blogs as $blog ) {
559 // Don't include blogs that aren't hosted at this site
560 if ( $blog->site_id != get_current_site()->id )
563 $blog_id = $blog->userblog_id;
565 switch_to_blog( $blog_id );
567 $is_admin = current_user_can( 'manage_options' );
570 'isAdmin' => $is_admin,
571 'url' => home_url( '/' ),
572 'blogid' => (string) $blog_id,
573 'blogName' => get_option( 'blogname' ),
574 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
577 restore_current_blog();
584 * Checks if the method received at least the minimum number of arguments.
588 * @param string|array $args Sanitize single string or array of strings.
589 * @param int $count Minimum number of arguments.
590 * @return boolean if $args contains at least $count arguments.
592 protected function minimum_args( $args, $count ) {
593 if ( count( $args ) < $count ) {
594 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
602 * Prepares taxonomy data for return in an XML-RPC object.
606 * @param object $taxonomy The unprepared taxonomy data
607 * @param array $fields The subset of taxonomy fields to return
608 * @return array The prepared taxonomy data
610 protected function _prepare_taxonomy( $taxonomy, $fields ) {
612 'name' => $taxonomy->name,
613 'label' => $taxonomy->label,
614 'hierarchical' => (bool) $taxonomy->hierarchical,
615 'public' => (bool) $taxonomy->public,
616 'show_ui' => (bool) $taxonomy->show_ui,
617 '_builtin' => (bool) $taxonomy->_builtin,
620 if ( in_array( 'labels', $fields ) )
621 $_taxonomy['labels'] = (array) $taxonomy->labels;
623 if ( in_array( 'cap', $fields ) )
624 $_taxonomy['cap'] = (array) $taxonomy->cap;
626 if ( in_array( 'menu', $fields ) )
627 $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
629 if ( in_array( 'object_type', $fields ) )
630 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
633 * Filter XML-RPC-prepared data for the given taxonomy.
637 * @param array $_taxonomy An array of taxonomy data.
638 * @param object $taxonomy Taxonomy object.
639 * @param array $fields The subset of taxonomy fields to return.
641 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
645 * Prepares term data for return in an XML-RPC object.
649 * @param array|object $term The unprepared term data
650 * @return array The prepared term data
652 protected function _prepare_term( $term ) {
654 if ( ! is_array( $_term) )
655 $_term = get_object_vars( $_term );
657 // For integers which may be larger than XML-RPC supports ensure we return strings.
658 $_term['term_id'] = strval( $_term['term_id'] );
659 $_term['term_group'] = strval( $_term['term_group'] );
660 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
661 $_term['parent'] = strval( $_term['parent'] );
663 // Count we are happy to return as an integer because people really shouldn't use terms that much.
664 $_term['count'] = intval( $_term['count'] );
667 * Filter XML-RPC-prepared data for the given term.
671 * @param array $_term An array of term data.
672 * @param array|object $term Term object or array.
674 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
678 * Convert a WordPress date string to an IXR_Date object.
682 * @param string $date
685 protected function _convert_date( $date ) {
686 if ( $date === '0000-00-00 00:00:00' ) {
687 return new IXR_Date( '00000000T00:00:00Z' );
689 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
693 * Convert a WordPress GMT date string to an IXR_Date object.
697 * @param string $date_gmt
698 * @param string $date
701 protected function _convert_date_gmt( $date_gmt, $date ) {
702 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
703 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
705 return $this->_convert_date( $date_gmt );
709 * Prepares post data for return in an XML-RPC object.
713 * @param array $post The unprepared post data
714 * @param array $fields The subset of post type fields to return
715 * @return array The prepared post data
717 protected function _prepare_post( $post, $fields ) {
718 // holds the data for this post. built up based on $fields
719 $_post = array( 'post_id' => strval( $post['ID'] ) );
721 // prepare common post fields
722 $post_fields = array(
723 'post_title' => $post['post_title'],
724 'post_date' => $this->_convert_date( $post['post_date'] ),
725 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
726 'post_modified' => $this->_convert_date( $post['post_modified'] ),
727 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
728 'post_status' => $post['post_status'],
729 'post_type' => $post['post_type'],
730 'post_name' => $post['post_name'],
731 'post_author' => $post['post_author'],
732 'post_password' => $post['post_password'],
733 'post_excerpt' => $post['post_excerpt'],
734 'post_content' => $post['post_content'],
735 'post_parent' => strval( $post['post_parent'] ),
736 'post_mime_type' => $post['post_mime_type'],
737 'link' => post_permalink( $post['ID'] ),
738 'guid' => $post['guid'],
739 'menu_order' => intval( $post['menu_order'] ),
740 'comment_status' => $post['comment_status'],
741 'ping_status' => $post['ping_status'],
742 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
746 $post_fields['post_thumbnail'] = array();
747 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
748 if ( $thumbnail_id ) {
749 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
750 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
753 // Consider future posts as published
754 if ( $post_fields['post_status'] === 'future' )
755 $post_fields['post_status'] = 'publish';
757 // Fill in blank post format
758 $post_fields['post_format'] = get_post_format( $post['ID'] );
759 if ( empty( $post_fields['post_format'] ) )
760 $post_fields['post_format'] = 'standard';
762 // Merge requested $post_fields fields into $_post
763 if ( in_array( 'post', $fields ) ) {
764 $_post = array_merge( $_post, $post_fields );
766 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
767 $_post = array_merge( $_post, $requested_fields );
770 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
772 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
773 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
774 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
775 $_post['terms'] = array();
776 foreach ( $terms as $term ) {
777 $_post['terms'][] = $this->_prepare_term( $term );
781 if ( in_array( 'custom_fields', $fields ) )
782 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
784 if ( in_array( 'enclosure', $fields ) ) {
785 $_post['enclosure'] = array();
786 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
787 if ( ! empty( $enclosures ) ) {
788 $encdata = explode( "\n", $enclosures[0] );
789 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
790 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
791 $_post['enclosure']['type'] = trim( $encdata[2] );
796 * Filter XML-RPC-prepared date for the given post.
800 * @param array $_post An array of modified post data.
801 * @param array $post An array of post data.
802 * @param array $fields An array of post fields.
804 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
808 * Prepares post data for return in an XML-RPC object.
812 * @param object $post_type Post type object
813 * @param array $fields The subset of post fields to return
814 * @return array The prepared post type data
816 protected function _prepare_post_type( $post_type, $fields ) {
818 'name' => $post_type->name,
819 'label' => $post_type->label,
820 'hierarchical' => (bool) $post_type->hierarchical,
821 'public' => (bool) $post_type->public,
822 'show_ui' => (bool) $post_type->show_ui,
823 '_builtin' => (bool) $post_type->_builtin,
824 'has_archive' => (bool) $post_type->has_archive,
825 'supports' => get_all_post_type_supports( $post_type->name ),
828 if ( in_array( 'labels', $fields ) ) {
829 $_post_type['labels'] = (array) $post_type->labels;
832 if ( in_array( 'cap', $fields ) ) {
833 $_post_type['cap'] = (array) $post_type->cap;
834 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
837 if ( in_array( 'menu', $fields ) ) {
838 $_post_type['menu_position'] = (int) $post_type->menu_position;
839 $_post_type['menu_icon'] = $post_type->menu_icon;
840 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
843 if ( in_array( 'taxonomies', $fields ) )
844 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
847 * Filter XML-RPC-prepared date for the given post type.
851 * @param array $_post_type An array of post type data.
852 * @param object $post_type Post type object.
854 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
858 * Prepares media item data for return in an XML-RPC object.
862 * @param object $media_item The unprepared media item data
863 * @param string $thumbnail_size The image size to use for the thumbnail URL
864 * @return array The prepared media item data
866 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
867 $_media_item = array(
868 'attachment_id' => strval( $media_item->ID ),
869 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
870 'parent' => $media_item->post_parent,
871 'link' => wp_get_attachment_url( $media_item->ID ),
872 'title' => $media_item->post_title,
873 'caption' => $media_item->post_excerpt,
874 'description' => $media_item->post_content,
875 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
878 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
879 if ( $thumbnail_src )
880 $_media_item['thumbnail'] = $thumbnail_src[0];
882 $_media_item['thumbnail'] = $_media_item['link'];
885 * Filter XML-RPC-prepared data for the given media item.
889 * @param array $_media_item An array of media item data.
890 * @param object $media_item Media item object.
891 * @param string $thumbnail_size Image size.
893 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
897 * Prepares page data for return in an XML-RPC object.
901 * @param object $page The unprepared page data
902 * @return array The prepared page data
904 protected function _prepare_page( $page ) {
905 // Get all of the page content and link.
906 $full_page = get_extended( $page->post_content );
907 $link = post_permalink( $page->ID );
909 // Get info the page parent if there is one.
911 if ( ! empty( $page->post_parent ) ) {
912 $parent = get_post( $page->post_parent );
913 $parent_title = $parent->post_title;
916 // Determine comment and ping settings.
917 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
918 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
921 $page_date = $this->_convert_date( $page->post_date );
922 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
924 // Pull the categories info together.
925 $categories = array();
926 if ( is_object_in_taxonomy( 'page', 'category' ) ) {
927 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
928 $categories[] = get_cat_name( $cat_id );
932 // Get the author info.
933 $author = get_userdata( $page->post_author );
935 $page_template = get_page_template_slug( $page->ID );
936 if ( empty( $page_template ) )
937 $page_template = 'default';
940 'dateCreated' => $page_date,
941 'userid' => $page->post_author,
942 'page_id' => $page->ID,
943 'page_status' => $page->post_status,
944 'description' => $full_page['main'],
945 'title' => $page->post_title,
947 'permaLink' => $link,
948 'categories' => $categories,
949 'excerpt' => $page->post_excerpt,
950 'text_more' => $full_page['extended'],
951 'mt_allow_comments' => $allow_comments,
952 'mt_allow_pings' => $allow_pings,
953 'wp_slug' => $page->post_name,
954 'wp_password' => $page->post_password,
955 'wp_author' => $author->display_name,
956 'wp_page_parent_id' => $page->post_parent,
957 'wp_page_parent_title' => $parent_title,
958 'wp_page_order' => $page->menu_order,
959 'wp_author_id' => (string) $author->ID,
960 'wp_author_display_name' => $author->display_name,
961 'date_created_gmt' => $page_date_gmt,
962 'custom_fields' => $this->get_custom_fields( $page->ID ),
963 'wp_page_template' => $page_template
967 * Filter XML-RPC-prepared data for the given page.
971 * @param array $_page An array of page data.
972 * @param WP_Post $page Page object.
974 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
978 * Prepares comment data for return in an XML-RPC object.
982 * @param object $comment The unprepared comment data
983 * @return array The prepared comment data
985 protected function _prepare_comment( $comment ) {
987 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
989 if ( '0' == $comment->comment_approved ) {
990 $comment_status = 'hold';
991 } elseif ( 'spam' == $comment->comment_approved ) {
992 $comment_status = 'spam';
993 } elseif ( '1' == $comment->comment_approved ) {
994 $comment_status = 'approve';
996 $comment_status = $comment->comment_approved;
999 'date_created_gmt' => $comment_date_gmt,
1000 'user_id' => $comment->user_id,
1001 'comment_id' => $comment->comment_ID,
1002 'parent' => $comment->comment_parent,
1003 'status' => $comment_status,
1004 'content' => $comment->comment_content,
1005 'link' => get_comment_link($comment),
1006 'post_id' => $comment->comment_post_ID,
1007 'post_title' => get_the_title($comment->comment_post_ID),
1008 'author' => $comment->comment_author,
1009 'author_url' => $comment->comment_author_url,
1010 'author_email' => $comment->comment_author_email,
1011 'author_ip' => $comment->comment_author_IP,
1012 'type' => $comment->comment_type,
1016 * Filter XML-RPC-prepared data for the given comment.
1020 * @param array $_comment An array of prepared comment data.
1021 * @param object $comment Comment object.
1023 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
1027 * Prepares user data for return in an XML-RPC object.
1031 * @param WP_User $user The unprepared user object
1032 * @param array $fields The subset of user fields to return
1033 * @return array The prepared user data
1035 protected function _prepare_user( $user, $fields ) {
1036 $_user = array( 'user_id' => strval( $user->ID ) );
1038 $user_fields = array(
1039 'username' => $user->user_login,
1040 'first_name' => $user->user_firstname,
1041 'last_name' => $user->user_lastname,
1042 'registered' => $this->_convert_date( $user->user_registered ),
1043 'bio' => $user->user_description,
1044 'email' => $user->user_email,
1045 'nickname' => $user->nickname,
1046 'nicename' => $user->user_nicename,
1047 'url' => $user->user_url,
1048 'display_name' => $user->display_name,
1049 'roles' => $user->roles,
1052 if ( in_array( 'all', $fields ) ) {
1053 $_user = array_merge( $_user, $user_fields );
1055 if ( in_array( 'basic', $fields ) ) {
1056 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
1057 $fields = array_merge( $fields, $basic_fields );
1059 $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
1060 $_user = array_merge( $_user, $requested_fields );
1064 * Filter XML-RPC-prepared data for the given user.
1068 * @param array $_user An array of user data.
1069 * @param WP_User $user User object.
1070 * @param array $fields An array of user fields.
1072 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
1076 * Create a new post for any registered post type.
1080 * @param array $args Method parameters. Contains:
1081 * - int $blog_id (unused)
1082 * - string $username
1083 * - string $password
1084 * - array $content_struct
1085 * $content_struct can contain:
1086 * - post_type (default: 'post')
1087 * - post_status (default: 'draft')
1092 * - post_date_gmt | post_date
1095 * - comment_status - can be 'open' | 'closed'
1096 * - ping_status - can be 'open' | 'closed'
1098 * - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
1099 * - custom_fields - array, with each element containing 'key' and 'value'
1100 * - terms - array, with taxonomy names as keys and arrays of term IDs as values
1101 * - terms_names - array, with taxonomy names as keys and arrays of term names as values
1103 * - any other fields supported by wp_insert_post()
1104 * @return string|IXR_Error post_id
1106 public function wp_newPost( $args ) {
1107 if ( ! $this->minimum_args( $args, 4 ) )
1108 return $this->error;
1110 $this->escape( $args );
1112 $username = $args[1];
1113 $password = $args[2];
1114 $content_struct = $args[3];
1116 if ( ! $user = $this->login( $username, $password ) )
1117 return $this->error;
1119 // convert the date field back to IXR form
1120 if ( isset( $content_struct['post_date'] ) && ! ( $content_struct['post_date'] instanceof IXR_Date ) ) {
1121 $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
1124 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1125 // since _insert_post will ignore the non-GMT date if the GMT date is set
1126 if ( isset( $content_struct['post_date_gmt'] ) && ! ( $content_struct['post_date_gmt'] instanceof IXR_Date ) ) {
1127 if ( $content_struct['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) {
1128 unset( $content_struct['post_date_gmt'] );
1130 $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
1134 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1135 do_action( 'xmlrpc_call', 'wp.newPost' );
1137 unset( $content_struct['ID'] );
1139 return $this->_insert_post( $user, $content_struct );
1143 * Helper method for filtering out elements from an array.
1147 * @param int $count Number to compare to one.
1149 private function _is_greater_than_one( $count ) {
1154 * Helper method for wp_newPost and wp_editPost, containing shared logic.
1157 * @uses wp_insert_post()
1159 * @param WP_User $user The post author if post_author isn't set in $content_struct.
1160 * @param array|IXR_Error $content_struct Post data to insert.
1162 protected function _insert_post( $user, $content_struct ) {
1163 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1164 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1166 $post_data = wp_parse_args( $content_struct, $defaults );
1168 $post_type = get_post_type_object( $post_data['post_type'] );
1170 return new IXR_Error( 403, __( 'Invalid post type' ) );
1172 $update = ! empty( $post_data['ID'] );
1175 if ( ! get_post( $post_data['ID'] ) )
1176 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1177 if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1178 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1179 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1180 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1182 if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1183 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1186 switch ( $post_data['post_status'] ) {
1191 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1192 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1196 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1197 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1200 if ( ! get_post_status_object( $post_data['post_status'] ) )
1201 $post_data['post_status'] = 'draft';
1205 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1206 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1208 $post_data['post_author'] = absint( $post_data['post_author'] );
1209 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1210 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1211 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1213 $author = get_userdata( $post_data['post_author'] );
1216 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1218 $post_data['post_author'] = $user->ID;
1221 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1222 unset( $post_data['comment_status'] );
1224 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1225 unset( $post_data['ping_status'] );
1227 // Do some timestamp voodoo
1228 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1229 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1230 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1231 } elseif ( ! empty( $post_data['post_date'] ) ) {
1232 $dateCreated = $post_data['post_date']->getIso();
1235 if ( ! empty( $dateCreated ) ) {
1236 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1237 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1240 if ( ! isset( $post_data['ID'] ) )
1241 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1242 $post_ID = $post_data['ID'];
1244 if ( $post_data['post_type'] == 'post' ) {
1245 // Private and password-protected posts cannot be stickied.
1246 if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
1247 // Error if the client tried to stick the post, otherwise, silently unstick.
1248 if ( ! empty( $post_data['sticky'] ) )
1249 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1251 unstick_post( $post_ID );
1252 } elseif ( isset( $post_data['sticky'] ) ) {
1253 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1254 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1255 if ( $post_data['sticky'] )
1256 stick_post( $post_ID );
1258 unstick_post( $post_ID );
1262 if ( isset( $post_data['post_thumbnail'] ) ) {
1263 // empty value deletes, non-empty value adds/updates
1264 if ( ! $post_data['post_thumbnail'] )
1265 delete_post_thumbnail( $post_ID );
1266 elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1267 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1268 set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1269 unset( $content_struct['post_thumbnail'] );
1272 if ( isset( $post_data['custom_fields'] ) )
1273 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1275 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1276 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1278 // accumulate term IDs from terms and terms_names
1281 // first validate the terms specified by ID
1282 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1283 $taxonomies = array_keys( $post_data['terms'] );
1285 // validating term ids
1286 foreach ( $taxonomies as $taxonomy ) {
1287 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1288 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1290 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1291 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1293 $term_ids = $post_data['terms'][$taxonomy];
1294 $terms[ $taxonomy ] = array();
1295 foreach ( $term_ids as $term_id ) {
1296 $term = get_term_by( 'id', $term_id, $taxonomy );
1299 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1301 $terms[$taxonomy][] = (int) $term_id;
1306 // now validate terms specified by name
1307 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1308 $taxonomies = array_keys( $post_data['terms_names'] );
1310 foreach ( $taxonomies as $taxonomy ) {
1311 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1312 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1314 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1315 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1317 // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
1318 $ambiguous_terms = array();
1319 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1320 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1322 // count the number of terms with the same name
1323 $tax_term_names_count = array_count_values( $tax_term_names );
1325 // filter out non-ambiguous term names
1326 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1328 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1331 $term_names = $post_data['terms_names'][$taxonomy];
1332 foreach ( $term_names as $term_name ) {
1333 if ( in_array( $term_name, $ambiguous_terms ) )
1334 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1336 $term = get_term_by( 'name', $term_name, $taxonomy );
1339 // term doesn't exist, so check that the user is allowed to create new terms
1340 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1341 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1343 // create the new term
1344 $term_info = wp_insert_term( $term_name, $taxonomy );
1345 if ( is_wp_error( $term_info ) )
1346 return new IXR_Error( 500, $term_info->get_error_message() );
1348 $terms[$taxonomy][] = (int) $term_info['term_id'];
1350 $terms[$taxonomy][] = (int) $term->term_id;
1356 $post_data['tax_input'] = $terms;
1357 unset( $post_data['terms'], $post_data['terms_names'] );
1359 // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
1360 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1363 if ( isset( $post_data['post_format'] ) ) {
1364 $format = set_post_format( $post_ID, $post_data['post_format'] );
1366 if ( is_wp_error( $format ) )
1367 return new IXR_Error( 500, $format->get_error_message() );
1369 unset( $post_data['post_format'] );
1372 // Handle enclosures
1373 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1374 $this->add_enclosure_if_new( $post_ID, $enclosure );
1376 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1379 * Filter post data array to be inserted via XML-RPC.
1383 * @param array $post_data Parsed array of post data.
1384 * @param array $content_struct Post data array.
1386 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1388 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1389 if ( is_wp_error( $post_ID ) )
1390 return new IXR_Error( 500, $post_ID->get_error_message() );
1393 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1395 return strval( $post_ID );
1399 * Edit a post for any registered post type.
1401 * The $content_struct parameter only needs to contain fields that
1402 * should be changed. All other fields will retain their existing values.
1406 * @param array $args Method parameters. Contains:
1407 * - int $blog_id (unused)
1408 * - string $username
1409 * - string $password
1411 * - array $content_struct
1412 * @return bool|IXR_Error true on success
1414 public function wp_editPost( $args ) {
1415 if ( ! $this->minimum_args( $args, 5 ) )
1416 return $this->error;
1418 $this->escape( $args );
1420 $username = $args[1];
1421 $password = $args[2];
1422 $post_id = (int) $args[3];
1423 $content_struct = $args[4];
1425 if ( ! $user = $this->login( $username, $password ) )
1426 return $this->error;
1428 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1429 do_action( 'xmlrpc_call', 'wp.editPost' );
1431 $post = get_post( $post_id, ARRAY_A );
1433 if ( empty( $post['ID'] ) )
1434 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1436 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1437 // If the post has been modified since the date provided, return an error.
1438 if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1439 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1443 // convert the date field back to IXR form
1444 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1446 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1447 // since _insert_post will ignore the non-GMT date if the GMT date is set
1448 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1449 unset( $post['post_date_gmt'] );
1451 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1453 $this->escape( $post );
1454 $merged_content_struct = array_merge( $post, $content_struct );
1456 $retval = $this->_insert_post( $user, $merged_content_struct );
1457 if ( $retval instanceof IXR_Error )
1464 * Delete a post for any registered post type.
1468 * @uses wp_delete_post()
1469 * @param array $args Method parameters. Contains:
1470 * - int $blog_id (unused)
1471 * - string $username
1472 * - string $password
1474 * @return bool|IXR_Error true on success
1476 public function wp_deletePost( $args ) {
1477 if ( ! $this->minimum_args( $args, 4 ) )
1478 return $this->error;
1480 $this->escape( $args );
1482 $username = $args[1];
1483 $password = $args[2];
1484 $post_id = (int) $args[3];
1486 if ( ! $user = $this->login( $username, $password ) )
1487 return $this->error;
1489 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1490 do_action( 'xmlrpc_call', 'wp.deletePost' );
1492 $post = get_post( $post_id, ARRAY_A );
1493 if ( empty( $post['ID'] ) )
1494 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1496 if ( ! current_user_can( 'delete_post', $post_id ) )
1497 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1499 $result = wp_delete_post( $post_id );
1502 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1512 * The optional $fields parameter specifies what fields will be included
1513 * in the response array. This should be a list of field names. 'post_id' will
1514 * always be included in the response regardless of the value of $fields.
1516 * Instead of, or in addition to, individual field names, conceptual group
1517 * names can be used to specify multiple fields. The available conceptual
1518 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1522 * @param array $args Method parameters. Contains:
1523 * - int $blog_id (unused)
1524 * - string $username
1525 * - string $password
1527 * - array $fields optional
1528 * @return array|IXR_Error Array contains (based on $fields parameter):
1534 * - 'post_modified_gmt'
1543 * - 'comment_status'
1552 public function wp_getPost( $args ) {
1553 if ( ! $this->minimum_args( $args, 4 ) )
1554 return $this->error;
1556 $this->escape( $args );
1558 $username = $args[1];
1559 $password = $args[2];
1560 $post_id = (int) $args[3];
1562 if ( isset( $args[4] ) ) {
1566 * Filter the list of post query fields used by the given XML-RPC method.
1570 * @param array $fields Array of post fields.
1571 * @param string $method Method name.
1573 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1576 if ( ! $user = $this->login( $username, $password ) )
1577 return $this->error;
1579 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1580 do_action( 'xmlrpc_call', 'wp.getPost' );
1582 $post = get_post( $post_id, ARRAY_A );
1584 if ( empty( $post['ID'] ) )
1585 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1587 if ( ! current_user_can( 'edit_post', $post_id ) )
1588 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1590 return $this->_prepare_post( $post, $fields );
1598 * The optional $filter parameter modifies the query used to retrieve posts.
1599 * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
1600 * 'orderby', and 'order'.
1602 * The optional $fields parameter specifies what fields will be included
1603 * in the response array.
1605 * @uses wp_get_recent_posts()
1606 * @see wp_getPost() for more on $fields
1607 * @see get_posts() for more on $filter values
1609 * @param array $args Method parameters. Contains:
1610 * - int $blog_id (unused)
1611 * - string $username
1612 * - string $password
1613 * - array $filter optional
1614 * - array $fields optional
1615 * @return array|IXR_Error Array contains a collection of posts.
1617 public function wp_getPosts( $args ) {
1618 if ( ! $this->minimum_args( $args, 3 ) )
1619 return $this->error;
1621 $this->escape( $args );
1623 $username = $args[1];
1624 $password = $args[2];
1625 $filter = isset( $args[3] ) ? $args[3] : array();
1627 if ( isset( $args[4] ) ) {
1630 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1631 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1634 if ( ! $user = $this->login( $username, $password ) )
1635 return $this->error;
1637 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1638 do_action( 'xmlrpc_call', 'wp.getPosts' );
1642 if ( isset( $filter['post_type'] ) ) {
1643 $post_type = get_post_type_object( $filter['post_type'] );
1644 if ( ! ( (bool) $post_type ) )
1645 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1647 $post_type = get_post_type_object( 'post' );
1650 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1651 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1653 $query['post_type'] = $post_type->name;
1655 if ( isset( $filter['post_status'] ) )
1656 $query['post_status'] = $filter['post_status'];
1658 if ( isset( $filter['number'] ) )
1659 $query['numberposts'] = absint( $filter['number'] );
1661 if ( isset( $filter['offset'] ) )
1662 $query['offset'] = absint( $filter['offset'] );
1664 if ( isset( $filter['orderby'] ) ) {
1665 $query['orderby'] = $filter['orderby'];
1667 if ( isset( $filter['order'] ) )
1668 $query['order'] = $filter['order'];
1671 if ( isset( $filter['s'] ) ) {
1672 $query['s'] = $filter['s'];
1675 $posts_list = wp_get_recent_posts( $query );
1677 if ( ! $posts_list )
1680 // holds all the posts data
1683 foreach ( $posts_list as $post ) {
1684 if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1687 $struct[] = $this->_prepare_post( $post, $fields );
1694 * Create a new term.
1698 * @uses wp_insert_term()
1699 * @param array $args Method parameters. Contains:
1700 * - int $blog_id (unused)
1701 * - string $username
1702 * - string $password
1703 * - array $content_struct
1704 * The $content_struct must contain:
1707 * Also, it can optionally contain:
1711 * @return string|IXR_Error term_id
1713 public function wp_newTerm( $args ) {
1714 if ( ! $this->minimum_args( $args, 4 ) )
1715 return $this->error;
1717 $this->escape( $args );
1719 $username = $args[1];
1720 $password = $args[2];
1721 $content_struct = $args[3];
1723 if ( ! $user = $this->login( $username, $password ) )
1724 return $this->error;
1726 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1727 do_action( 'xmlrpc_call', 'wp.newTerm' );
1729 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1730 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1732 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1734 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1735 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1737 $taxonomy = (array) $taxonomy;
1739 // hold the data of the term
1740 $term_data = array();
1742 $term_data['name'] = trim( $content_struct['name'] );
1743 if ( empty( $term_data['name'] ) )
1744 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1746 if ( isset( $content_struct['parent'] ) ) {
1747 if ( ! $taxonomy['hierarchical'] )
1748 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1750 $parent_term_id = (int) $content_struct['parent'];
1751 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1753 if ( is_wp_error( $parent_term ) )
1754 return new IXR_Error( 500, $parent_term->get_error_message() );
1756 if ( ! $parent_term )
1757 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1759 $term_data['parent'] = $content_struct['parent'];
1762 if ( isset( $content_struct['description'] ) )
1763 $term_data['description'] = $content_struct['description'];
1765 if ( isset( $content_struct['slug'] ) )
1766 $term_data['slug'] = $content_struct['slug'];
1768 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1770 if ( is_wp_error( $term ) )
1771 return new IXR_Error( 500, $term->get_error_message() );
1774 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1776 return strval( $term['term_id'] );
1784 * @uses wp_update_term()
1785 * @param array $args Method parameters. Contains:
1786 * - int $blog_id (unused)
1787 * - string $username
1788 * - string $password
1790 * - array $content_struct
1791 * The $content_struct must contain:
1793 * Also, it can optionally contain:
1798 * @return bool|IXR_Error True, on success.
1800 public function wp_editTerm( $args ) {
1801 if ( ! $this->minimum_args( $args, 5 ) )
1802 return $this->error;
1804 $this->escape( $args );
1806 $username = $args[1];
1807 $password = $args[2];
1808 $term_id = (int) $args[3];
1809 $content_struct = $args[4];
1811 if ( ! $user = $this->login( $username, $password ) )
1812 return $this->error;
1814 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1815 do_action( 'xmlrpc_call', 'wp.editTerm' );
1817 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1818 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1820 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1822 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1823 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1825 $taxonomy = (array) $taxonomy;
1827 // hold the data of the term
1828 $term_data = array();
1830 $term = get_term( $term_id , $content_struct['taxonomy'] );
1832 if ( is_wp_error( $term ) )
1833 return new IXR_Error( 500, $term->get_error_message() );
1836 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1838 if ( isset( $content_struct['name'] ) ) {
1839 $term_data['name'] = trim( $content_struct['name'] );
1841 if ( empty( $term_data['name'] ) )
1842 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1845 if ( isset( $content_struct['parent'] ) ) {
1846 if ( ! $taxonomy['hierarchical'] )
1847 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1849 $parent_term_id = (int) $content_struct['parent'];
1850 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1852 if ( is_wp_error( $parent_term ) )
1853 return new IXR_Error( 500, $parent_term->get_error_message() );
1855 if ( ! $parent_term )
1856 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1858 $term_data['parent'] = $content_struct['parent'];
1861 if ( isset( $content_struct['description'] ) )
1862 $term_data['description'] = $content_struct['description'];
1864 if ( isset( $content_struct['slug'] ) )
1865 $term_data['slug'] = $content_struct['slug'];
1867 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1869 if ( is_wp_error( $term ) )
1870 return new IXR_Error( 500, $term->get_error_message() );
1873 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1883 * @uses wp_delete_term()
1884 * @param array $args Method parameters. Contains:
1885 * - int $blog_id (unused)
1886 * - string $username
1887 * - string $password
1888 * - string $taxnomy_name
1890 * @return boolean|IXR_Error If it suceeded true else a reason why not
1892 public function wp_deleteTerm( $args ) {
1893 if ( ! $this->minimum_args( $args, 5 ) )
1894 return $this->error;
1896 $this->escape( $args );
1898 $username = $args[1];
1899 $password = $args[2];
1900 $taxonomy = $args[3];
1901 $term_id = (int) $args[4];
1903 if ( ! $user = $this->login( $username, $password ) )
1904 return $this->error;
1906 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1907 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1909 if ( ! taxonomy_exists( $taxonomy ) )
1910 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1912 $taxonomy = get_taxonomy( $taxonomy );
1914 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
1915 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
1917 $term = get_term( $term_id, $taxonomy->name );
1919 if ( is_wp_error( $term ) )
1920 return new IXR_Error( 500, $term->get_error_message() );
1923 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1925 $result = wp_delete_term( $term_id, $taxonomy->name );
1927 if ( is_wp_error( $result ) )
1928 return new IXR_Error( 500, $term->get_error_message() );
1931 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
1942 * @param array $args Method parameters. Contains:
1943 * - int $blog_id (unused)
1944 * - string $username
1945 * - string $password
1946 * - string $taxonomy
1948 * @return array|IXR_Error Array contains:
1953 * - 'term_taxonomy_id'
1959 public function wp_getTerm( $args ) {
1960 if ( ! $this->minimum_args( $args, 5 ) )
1961 return $this->error;
1963 $this->escape( $args );
1965 $username = $args[1];
1966 $password = $args[2];
1967 $taxonomy = $args[3];
1968 $term_id = (int) $args[4];
1970 if ( ! $user = $this->login( $username, $password ) )
1971 return $this->error;
1973 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1974 do_action( 'xmlrpc_call', 'wp.getTerm' );
1976 if ( ! taxonomy_exists( $taxonomy ) )
1977 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1979 $taxonomy = get_taxonomy( $taxonomy );
1981 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1982 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1984 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
1986 if ( is_wp_error( $term ) )
1987 return new IXR_Error( 500, $term->get_error_message() );
1990 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1992 return $this->_prepare_term( $term );
1996 * Retrieve all terms for a taxonomy.
2000 * The optional $filter parameter modifies the query used to retrieve terms.
2001 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
2004 * @param array $args Method parameters. Contains:
2005 * - int $blog_id (unused)
2006 * - string $username
2007 * - string $password
2008 * - string $taxonomy
2009 * - array $filter optional
2010 * @return array|IXR_Error terms
2012 public function wp_getTerms( $args ) {
2013 if ( ! $this->minimum_args( $args, 4 ) )
2014 return $this->error;
2016 $this->escape( $args );
2018 $username = $args[1];
2019 $password = $args[2];
2020 $taxonomy = $args[3];
2021 $filter = isset( $args[4] ) ? $args[4] : array();
2023 if ( ! $user = $this->login( $username, $password ) )
2024 return $this->error;
2026 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2027 do_action( 'xmlrpc_call', 'wp.getTerms' );
2029 if ( ! taxonomy_exists( $taxonomy ) )
2030 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2032 $taxonomy = get_taxonomy( $taxonomy );
2034 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2035 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2039 if ( isset( $filter['number'] ) )
2040 $query['number'] = absint( $filter['number'] );
2042 if ( isset( $filter['offset'] ) )
2043 $query['offset'] = absint( $filter['offset'] );
2045 if ( isset( $filter['orderby'] ) ) {
2046 $query['orderby'] = $filter['orderby'];
2048 if ( isset( $filter['order'] ) )
2049 $query['order'] = $filter['order'];
2052 if ( isset( $filter['hide_empty'] ) )
2053 $query['hide_empty'] = $filter['hide_empty'];
2055 $query['get'] = 'all';
2057 if ( isset( $filter['search'] ) )
2058 $query['search'] = $filter['search'];
2060 $terms = get_terms( $taxonomy->name, $query );
2062 if ( is_wp_error( $terms ) )
2063 return new IXR_Error( 500, $terms->get_error_message() );
2067 foreach ( $terms as $term ) {
2068 $struct[] = $this->_prepare_term( $term );
2075 * Retrieve a taxonomy.
2079 * @uses get_taxonomy()
2080 * @param array $args Method parameters. Contains:
2081 * - int $blog_id (unused)
2082 * - string $username
2083 * - string $password
2084 * - string $taxonomy
2085 * @return array|IXR_Error (@see get_taxonomy())
2087 public function wp_getTaxonomy( $args ) {
2088 if ( ! $this->minimum_args( $args, 4 ) )
2089 return $this->error;
2091 $this->escape( $args );
2093 $username = $args[1];
2094 $password = $args[2];
2095 $taxonomy = $args[3];
2097 if ( isset( $args[4] ) ) {
2101 * Filter the taxonomy query fields used by the given XML-RPC method.
2105 * @param array $fields An array of taxonomy fields to retrieve.
2106 * @param string $method The method name.
2108 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
2111 if ( ! $user = $this->login( $username, $password ) )
2112 return $this->error;
2114 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2115 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
2117 if ( ! taxonomy_exists( $taxonomy ) )
2118 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2120 $taxonomy = get_taxonomy( $taxonomy );
2122 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2123 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2125 return $this->_prepare_taxonomy( $taxonomy, $fields );
2129 * Retrieve all taxonomies.
2133 * @uses get_taxonomies()
2134 * @param array $args Method parameters. Contains:
2135 * - int $blog_id (unused)
2136 * - string $username
2137 * - string $password
2138 * @return array taxonomies
2140 public function wp_getTaxonomies( $args ) {
2141 if ( ! $this->minimum_args( $args, 3 ) )
2142 return $this->error;
2144 $this->escape( $args );
2146 $username = $args[1];
2147 $password = $args[2];
2148 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2150 if ( isset( $args[4] ) ) {
2153 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2154 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
2157 if ( ! $user = $this->login( $username, $password ) )
2158 return $this->error;
2160 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2161 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
2163 $taxonomies = get_taxonomies( $filter, 'objects' );
2165 // holds all the taxonomy data
2168 foreach ( $taxonomies as $taxonomy ) {
2169 // capability check for post_types
2170 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2173 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
2182 * The optional $fields parameter specifies what fields will be included
2183 * in the response array. This should be a list of field names. 'user_id' will
2184 * always be included in the response regardless of the value of $fields.
2186 * Instead of, or in addition to, individual field names, conceptual group
2187 * names can be used to specify multiple fields. The available conceptual
2188 * groups are 'basic' and 'all'.
2190 * @uses get_userdata()
2191 * @param array $args Method parameters. Contains:
2192 * - int $blog_id (unused)
2193 * - string $username
2194 * - string $password
2196 * - array $fields optional
2197 * @return array|IXR_Error Array contains (based on $fields parameter):
2211 public function wp_getUser( $args ) {
2212 if ( ! $this->minimum_args( $args, 4 ) )
2213 return $this->error;
2215 $this->escape( $args );
2217 $username = $args[1];
2218 $password = $args[2];
2219 $user_id = (int) $args[3];
2221 if ( isset( $args[4] ) ) {
2225 * Filter the default user query fields used by the given XML-RPC method.
2229 * @param array $fields User query fields for given method. Default 'all'.
2230 * @param string $method The method name.
2232 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2235 if ( ! $user = $this->login( $username, $password ) )
2236 return $this->error;
2238 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2239 do_action( 'xmlrpc_call', 'wp.getUser' );
2241 if ( ! current_user_can( 'edit_user', $user_id ) )
2242 return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2244 $user_data = get_userdata( $user_id );
2247 return new IXR_Error( 404, __( 'Invalid user ID' ) );
2249 return $this->_prepare_user( $user_data, $fields );
2255 * The optional $filter parameter modifies the query used to retrieve users.
2256 * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2257 * 'who', 'orderby', and 'order'.
2259 * The optional $fields parameter specifies what fields will be included
2260 * in the response array.
2263 * @see wp_getUser() for more on $fields and return values
2265 * @param array $args Method parameters. Contains:
2266 * - int $blog_id (unused)
2267 * - string $username
2268 * - string $password
2269 * - array $filter optional
2270 * - array $fields optional
2271 * @return array|IXR_Error users data
2273 public function wp_getUsers( $args ) {
2274 if ( ! $this->minimum_args( $args, 3 ) )
2275 return $this->error;
2277 $this->escape( $args );
2279 $username = $args[1];
2280 $password = $args[2];
2281 $filter = isset( $args[3] ) ? $args[3] : array();
2283 if ( isset( $args[4] ) ) {
2286 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2287 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2290 if ( ! $user = $this->login( $username, $password ) )
2291 return $this->error;
2293 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2294 do_action( 'xmlrpc_call', 'wp.getUsers' );
2296 if ( ! current_user_can( 'list_users' ) )
2297 return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
2299 $query = array( 'fields' => 'all_with_meta' );
2301 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2302 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2304 if ( isset( $filter['orderby'] ) ) {
2305 $query['orderby'] = $filter['orderby'];
2307 if ( isset( $filter['order'] ) )
2308 $query['order'] = $filter['order'];
2311 if ( isset( $filter['role'] ) ) {
2312 if ( get_role( $filter['role'] ) === null )
2313 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2315 $query['role'] = $filter['role'];
2318 if ( isset( $filter['who'] ) ) {
2319 $query['who'] = $filter['who'];
2322 $users = get_users( $query );
2325 foreach ( $users as $user_data ) {
2326 if ( current_user_can( 'edit_user', $user_data->ID ) )
2327 $_users[] = $this->_prepare_user( $user_data, $fields );
2333 * Retrieve information about the requesting user.
2335 * @uses get_userdata()
2336 * @param array $args Method parameters. Contains:
2337 * - int $blog_id (unused)
2338 * - string $username
2339 * - string $password
2340 * - array $fields optional
2341 * @return array|IXR_Error (@see wp_getUser)
2343 public function wp_getProfile( $args ) {
2344 if ( ! $this->minimum_args( $args, 3 ) )
2345 return $this->error;
2347 $this->escape( $args );
2349 $username = $args[1];
2350 $password = $args[2];
2352 if ( isset( $args[3] ) ) {
2355 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2356 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2359 if ( ! $user = $this->login( $username, $password ) )
2360 return $this->error;
2362 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2363 do_action( 'xmlrpc_call', 'wp.getProfile' );
2365 if ( ! current_user_can( 'edit_user', $user->ID ) )
2366 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2368 $user_data = get_userdata( $user->ID );
2370 return $this->_prepare_user( $user_data, $fields );
2374 * Edit user's profile.
2376 * @uses wp_update_user()
2377 * @param array $args Method parameters. Contains:
2378 * - int $blog_id (unused)
2379 * - string $username
2380 * - string $password
2381 * - array $content_struct
2382 * It can optionally contain:
2390 * @return bool|IXR_Error True, on success.
2392 public function wp_editProfile( $args ) {
2393 if ( ! $this->minimum_args( $args, 4 ) )
2394 return $this->error;
2396 $this->escape( $args );
2398 $username = $args[1];
2399 $password = $args[2];
2400 $content_struct = $args[3];
2402 if ( ! $user = $this->login( $username, $password ) )
2403 return $this->error;
2405 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2406 do_action( 'xmlrpc_call', 'wp.editProfile' );
2408 if ( ! current_user_can( 'edit_user', $user->ID ) )
2409 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2411 // holds data of the user
2412 $user_data = array();
2413 $user_data['ID'] = $user->ID;
2415 // only set the user details if it was given
2416 if ( isset( $content_struct['first_name'] ) )
2417 $user_data['first_name'] = $content_struct['first_name'];
2419 if ( isset( $content_struct['last_name'] ) )
2420 $user_data['last_name'] = $content_struct['last_name'];
2422 if ( isset( $content_struct['url'] ) )
2423 $user_data['user_url'] = $content_struct['url'];
2425 if ( isset( $content_struct['display_name'] ) )
2426 $user_data['display_name'] = $content_struct['display_name'];
2428 if ( isset( $content_struct['nickname'] ) )
2429 $user_data['nickname'] = $content_struct['nickname'];
2431 if ( isset( $content_struct['nicename'] ) )
2432 $user_data['user_nicename'] = $content_struct['nicename'];
2434 if ( isset( $content_struct['bio'] ) )
2435 $user_data['description'] = $content_struct['bio'];
2437 $result = wp_update_user( $user_data );
2439 if ( is_wp_error( $result ) )
2440 return new IXR_Error( 500, $result->get_error_message() );
2443 return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2453 * @param array $args Method parameters. Contains:
2454 * - blog_id (unused)
2458 * @return array|IXR_Error
2460 public function wp_getPage($args) {
2461 $this->escape($args);
2463 $page_id = (int) $args[1];
2464 $username = $args[2];
2465 $password = $args[3];
2467 if ( !$user = $this->login($username, $password) ) {
2468 return $this->error;
2471 $page = get_post($page_id);
2473 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2475 if ( !current_user_can( 'edit_page', $page_id ) )
2476 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2478 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2479 do_action( 'xmlrpc_call', 'wp.getPage' );
2481 // If we found the page then format the data.
2482 if ( $page->ID && ($page->post_type == 'page') ) {
2483 return $this->_prepare_page( $page );
2485 // If the page doesn't exist indicate that.
2487 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2496 * @param array $args Method parameters. Contains:
2497 * - blog_id (unused)
2501 * @return array|IXR_Error
2503 public function wp_getPages($args) {
2504 $this->escape($args);
2506 $username = $args[1];
2507 $password = $args[2];
2508 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
2510 if ( !$user = $this->login($username, $password) )
2511 return $this->error;
2513 if ( !current_user_can( 'edit_pages' ) )
2514 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2516 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2517 do_action( 'xmlrpc_call', 'wp.getPages' );
2519 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2520 $num_pages = count($pages);
2522 // If we have pages, put together their info.
2523 if ( $num_pages >= 1 ) {
2524 $pages_struct = array();
2526 foreach ($pages as $page) {
2527 if ( current_user_can( 'edit_page', $page->ID ) )
2528 $pages_struct[] = $this->_prepare_page( $page );
2531 return $pages_struct;
2542 * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
2543 * @return int|IXR_Error
2545 public function wp_newPage($args) {
2546 // Items not escaped here will be escaped in newPost.
2547 $username = $this->escape($args[1]);
2548 $password = $this->escape($args[2]);
2550 if ( !$user = $this->login($username, $password) )
2551 return $this->error;
2553 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2554 do_action( 'xmlrpc_call', 'wp.newPage' );
2556 // Mark this as content for a page.
2557 $args[3]["post_type"] = 'page';
2559 // Let mw_newPost do all of the heavy lifting.
2560 return $this->mw_newPost( $args );
2568 * @param array $args Method parameters.
2569 * @return bool|IXR_Error True, if success.
2571 public function wp_deletePage($args) {
2572 $this->escape($args);
2574 $username = $args[1];
2575 $password = $args[2];
2576 $page_id = (int) $args[3];
2578 if ( !$user = $this->login($username, $password) )
2579 return $this->error;
2581 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2582 do_action( 'xmlrpc_call', 'wp.deletePage' );
2584 // Get the current page based on the page_id and
2585 // make sure it is a page and not a post.
2586 $actual_page = get_post($page_id, ARRAY_A);
2587 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2588 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2590 // Make sure the user can delete pages.
2591 if ( !current_user_can('delete_page', $page_id) )
2592 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete this page.' ) );
2594 // Attempt to delete the page.
2595 $result = wp_delete_post($page_id);
2597 return new IXR_Error( 500, __( 'Failed to delete the page.' ) );
2600 * Fires after a page has been successfully deleted via XML-RPC.
2604 * @param int $page_id ID of the deleted page.
2605 * @param array $args An array of arguments to delete the page.
2607 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2617 * @param array $args Method parameters.
2618 * @return array|IXR_Error
2620 public function wp_editPage($args) {
2621 // Items not escaped here will be escaped in editPost.
2622 $page_id = (int) $this->escape($args[1]);
2623 $username = $this->escape($args[2]);
2624 $password = $this->escape($args[3]);
2625 $content = $args[4];
2626 $publish = $args[5];
2628 if ( !$user = $this->login($username, $password) )
2629 return $this->error;
2631 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2632 do_action( 'xmlrpc_call', 'wp.editPage' );
2634 // Get the page data and make sure it is a page.
2635 $actual_page = get_post($page_id, ARRAY_A);
2636 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2637 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2639 // Make sure the user is allowed to edit pages.
2640 if ( !current_user_can('edit_page', $page_id) )
2641 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this page.' ) );
2643 // Mark this as content for a page.
2644 $content['post_type'] = 'page';
2646 // Arrange args in the way mw_editPost understands.
2655 // Let mw_editPost do all of the heavy lifting.
2656 return $this->mw_editPost( $args );
2660 * Retrieve page list.
2664 * @param array $args Method parameters.
2665 * @return array|IXR_Error
2667 public function wp_getPageList($args) {
2670 $this->escape($args);
2672 $username = $args[1];
2673 $password = $args[2];
2675 if ( !$user = $this->login($username, $password) )
2676 return $this->error;
2678 if ( !current_user_can( 'edit_pages' ) )
2679 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2681 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2682 do_action( 'xmlrpc_call', 'wp.getPageList' );
2684 // Get list of pages ids and titles
2685 $page_list = $wpdb->get_results("
2687 post_title page_title,
2688 post_parent page_parent_id,
2693 WHERE post_type = 'page'
2697 // The date needs to be formatted properly.
2698 $num_pages = count($page_list);
2699 for ( $i = 0; $i < $num_pages; $i++ ) {
2700 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2701 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2703 unset($page_list[$i]->post_date_gmt);
2704 unset($page_list[$i]->post_date);
2705 unset($page_list[$i]->post_status);
2712 * Retrieve authors list.
2716 * @param array $args Method parameters.
2717 * @return array|IXR_Error
2719 public function wp_getAuthors($args) {
2721 $this->escape($args);
2723 $username = $args[1];
2724 $password = $args[2];
2726 if ( !$user = $this->login($username, $password) )
2727 return $this->error;
2729 if ( !current_user_can('edit_posts') )
2730 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
2732 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2733 do_action( 'xmlrpc_call', 'wp.getAuthors' );
2736 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2738 'user_id' => $user->ID,
2739 'user_login' => $user->user_login,
2740 'display_name' => $user->display_name
2748 * Get list of all tags
2752 * @param array $args Method parameters.
2753 * @return array|IXR_Error
2755 public function wp_getTags( $args ) {
2756 $this->escape( $args );
2758 $username = $args[1];
2759 $password = $args[2];
2761 if ( !$user = $this->login($username, $password) )
2762 return $this->error;
2764 if ( !current_user_can( 'edit_posts' ) )
2765 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2767 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2768 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2772 if ( $all_tags = get_tags() ) {
2773 foreach( (array) $all_tags as $tag ) {
2775 $struct['tag_id'] = $tag->term_id;
2776 $struct['name'] = $tag->name;
2777 $struct['count'] = $tag->count;
2778 $struct['slug'] = $tag->slug;
2779 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2780 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
2790 * Create new category.
2794 * @param array $args Method parameters.
2795 * @return int|IXR_Error Category ID.
2797 public function wp_newCategory($args) {
2798 $this->escape($args);
2800 $username = $args[1];
2801 $password = $args[2];
2802 $category = $args[3];
2804 if ( !$user = $this->login($username, $password) )
2805 return $this->error;
2807 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2808 do_action( 'xmlrpc_call', 'wp.newCategory' );
2810 // Make sure the user is allowed to add a category.
2811 if ( !current_user_can('manage_categories') )
2812 return new IXR_Error(401, __('Sorry, you do not have the right to add a category.'));
2814 // If no slug was provided make it empty so that
2815 // WordPress will generate one.
2816 if ( empty($category['slug']) )
2817 $category['slug'] = '';
2819 // If no parent_id was provided make it empty
2820 // so that it will be a top level page (no parent).
2821 if ( !isset($category['parent_id']) )
2822 $category['parent_id'] = '';
2824 // If no description was provided make it empty.
2825 if ( empty($category["description"]) )
2826 $category["description"] = "";
2828 $new_category = array(
2829 'cat_name' => $category['name'],
2830 'category_nicename' => $category['slug'],
2831 'category_parent' => $category['parent_id'],
2832 'category_description' => $category['description']
2835 $cat_id = wp_insert_category($new_category, true);
2836 if ( is_wp_error( $cat_id ) ) {
2837 if ( 'term_exists' == $cat_id->get_error_code() )
2838 return (int) $cat_id->get_error_data();
2840 return new IXR_Error(500, __('Sorry, the new category failed.'));
2841 } elseif ( ! $cat_id ) {
2842 return new IXR_Error(500, __('Sorry, the new category failed.'));
2846 * Fires after a new category has been successfully created via XML-RPC.
2850 * @param int $cat_id ID of the new category.
2851 * @param array $args An array of new category arguments.
2853 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
2863 * @param array $args Method parameters.
2864 * @return bool|IXR_Error See {@link wp_delete_term()} for return info.
2866 public function wp_deleteCategory($args) {
2867 $this->escape($args);
2869 $username = $args[1];
2870 $password = $args[2];
2871 $category_id = (int) $args[3];
2873 if ( !$user = $this->login($username, $password) )
2874 return $this->error;
2876 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2877 do_action( 'xmlrpc_call', 'wp.deleteCategory' );
2879 if ( !current_user_can('manage_categories') )
2880 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
2882 $status = wp_delete_term( $category_id, 'category' );
2884 if ( true == $status ) {
2886 * Fires after a category has been successfully deleted via XML-RPC.
2890 * @param int $category_id ID of the deleted category.
2891 * @param array $args An array of arguments to delete the category.
2893 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
2900 * Retrieve category list.
2904 * @param array $args Method parameters.
2905 * @return array|IXR_Error
2907 public function wp_suggestCategories($args) {
2908 $this->escape($args);
2910 $username = $args[1];
2911 $password = $args[2];
2912 $category = $args[3];
2913 $max_results = (int) $args[4];
2915 if ( !$user = $this->login($username, $password) )
2916 return $this->error;
2918 if ( !current_user_can( 'edit_posts' ) )
2919 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
2921 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2922 do_action( 'xmlrpc_call', 'wp.suggestCategories' );
2924 $category_suggestions = array();
2925 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
2926 foreach ( (array) get_categories($args) as $cat ) {
2927 $category_suggestions[] = array(
2928 'category_id' => $cat->term_id,
2929 'category_name' => $cat->name
2933 return $category_suggestions;
2941 * @param array $args Method parameters.
2942 * @return array|IXR_Error
2944 public function wp_getComment($args) {
2945 $this->escape($args);
2947 $username = $args[1];
2948 $password = $args[2];
2949 $comment_id = (int) $args[3];
2951 if ( !$user = $this->login($username, $password) )
2952 return $this->error;
2954 if ( !current_user_can( 'moderate_comments' ) )
2955 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2957 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2958 do_action( 'xmlrpc_call', 'wp.getComment' );
2960 if ( ! $comment = get_comment($comment_id) )
2961 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2963 return $this->_prepare_comment( $comment );
2967 * Retrieve comments.
2969 * Besides the common blog_id (unused), username, and password arguments, it takes a filter
2970 * array as last argument.
2972 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
2974 * The defaults are as follows:
2975 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
2976 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
2977 * - 'number' - Default is 10. Total number of media items to retrieve.
2978 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2982 * @param array $args Method parameters.
2983 * @return array|IXR_Error Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
2985 public function wp_getComments($args) {
2986 $this->escape($args);
2988 $username = $args[1];
2989 $password = $args[2];
2990 $struct = isset( $args[3] ) ? $args[3] : array();
2992 if ( !$user = $this->login($username, $password) )
2993 return $this->error;
2995 if ( !current_user_can( 'moderate_comments' ) )
2996 return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
2998 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2999 do_action( 'xmlrpc_call', 'wp.getComments' );
3001 if ( isset($struct['status']) )
3002 $status = $struct['status'];
3007 if ( isset($struct['post_id']) )
3008 $post_id = absint($struct['post_id']);
3011 if ( isset($struct['offset']) )
3012 $offset = absint($struct['offset']);
3015 if ( isset($struct['number']) )
3016 $number = absint($struct['number']);
3018 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
3020 $comments_struct = array();
3022 foreach ( $comments as $comment ) {
3023 $comments_struct[] = $this->_prepare_comment( $comment );
3026 return $comments_struct;
3032 * By default, the comment will be moved to the trash instead of deleted.
3033 * See {@link wp_delete_comment()} for more information on
3038 * @param array $args Method parameters. Contains:
3039 * - blog_id (unused)
3043 * @return bool|IXR_Error {@link wp_delete_comment()}
3045 public function wp_deleteComment($args) {
3046 $this->escape($args);
3048 $username = $args[1];
3049 $password = $args[2];
3050 $comment_ID = (int) $args[3];
3052 if ( !$user = $this->login($username, $password) )
3053 return $this->error;
3055 if ( !current_user_can( 'moderate_comments' ) )
3056 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3058 if ( ! get_comment($comment_ID) )
3059 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3061 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3062 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3064 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3065 do_action( 'xmlrpc_call', 'wp.deleteComment' );
3067 $status = wp_delete_comment( $comment_ID );
3069 if ( true == $status ) {
3071 * Fires after a comment has been successfully deleted via XML-RPC.
3075 * @param int $comment_ID ID of the deleted comment.
3076 * @param array $args An array of arguments to delete the comment.
3078 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
3087 * Besides the common blog_id (unused), username, and password arguments, it takes a
3088 * comment_id integer and a content_struct array as last argument.
3090 * The allowed keys in the content_struct array are:
3095 * - 'date_created_gmt'
3096 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
3100 * @param array $args Contains:
3101 * - blog_id (unused)
3106 * @return bool|IXR_Error True, on success.
3108 public function wp_editComment($args) {
3109 $this->escape($args);
3111 $username = $args[1];
3112 $password = $args[2];
3113 $comment_ID = (int) $args[3];
3114 $content_struct = $args[4];
3116 if ( !$user = $this->login($username, $password) )
3117 return $this->error;
3119 if ( !current_user_can( 'moderate_comments' ) )
3120 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3122 if ( ! get_comment($comment_ID) )
3123 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3125 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3126 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3128 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3129 do_action( 'xmlrpc_call', 'wp.editComment' );
3131 if ( isset($content_struct['status']) ) {
3132 $statuses = get_comment_statuses();
3133 $statuses = array_keys($statuses);
3135 if ( ! in_array($content_struct['status'], $statuses) )
3136 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
3137 $comment_approved = $content_struct['status'];