3 * XML-RPC protocol support for WordPress
9 * WordPress XMLRPC server implementation.
11 * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
12 * pingback. Additional WordPress API for managing comments, pages, posts,
15 * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled
16 * via the xmlrpc_enabled filter found in wp_xmlrpc_server::login().
19 * @subpackage Publishing
22 class wp_xmlrpc_server extends IXR_Server {
25 * Register all of the XMLRPC methods that XMLRPC server understands.
27 * Sets up server and method property. Passes XMLRPC
28 * methods through the 'xmlrpc_methods' filter to allow plugins to extend
29 * or replace XMLRPC methods.
33 * @return wp_xmlrpc_server
35 function __construct() {
36 $this->methods = array(
38 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
39 'wp.newPost' => 'this:wp_newPost',
40 'wp.editPost' => 'this:wp_editPost',
41 'wp.deletePost' => 'this:wp_deletePost',
42 'wp.getPost' => 'this:wp_getPost',
43 'wp.getPosts' => 'this:wp_getPosts',
44 'wp.newTerm' => 'this:wp_newTerm',
45 'wp.editTerm' => 'this:wp_editTerm',
46 'wp.deleteTerm' => 'this:wp_deleteTerm',
47 'wp.getTerm' => 'this:wp_getTerm',
48 'wp.getTerms' => 'this:wp_getTerms',
49 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
50 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
51 'wp.getUser' => 'this:wp_getUser',
52 'wp.getUsers' => 'this:wp_getUsers',
53 'wp.getProfile' => 'this:wp_getProfile',
54 'wp.editProfile' => 'this:wp_editProfile',
55 'wp.getPage' => 'this:wp_getPage',
56 'wp.getPages' => 'this:wp_getPages',
57 'wp.newPage' => 'this:wp_newPage',
58 'wp.deletePage' => 'this:wp_deletePage',
59 'wp.editPage' => 'this:wp_editPage',
60 'wp.getPageList' => 'this:wp_getPageList',
61 'wp.getAuthors' => 'this:wp_getAuthors',
62 'wp.getCategories' => 'this:mw_getCategories', // Alias
63 'wp.getTags' => 'this:wp_getTags',
64 'wp.newCategory' => 'this:wp_newCategory',
65 'wp.deleteCategory' => 'this:wp_deleteCategory',
66 'wp.suggestCategories' => 'this:wp_suggestCategories',
67 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
68 'wp.getCommentCount' => 'this:wp_getCommentCount',
69 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
70 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
71 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
72 'wp.getOptions' => 'this:wp_getOptions',
73 'wp.setOptions' => 'this:wp_setOptions',
74 'wp.getComment' => 'this:wp_getComment',
75 'wp.getComments' => 'this:wp_getComments',
76 'wp.deleteComment' => 'this:wp_deleteComment',
77 'wp.editComment' => 'this:wp_editComment',
78 'wp.newComment' => 'this:wp_newComment',
79 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
80 'wp.getMediaItem' => 'this:wp_getMediaItem',
81 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
82 'wp.getPostFormats' => 'this:wp_getPostFormats',
83 'wp.getPostType' => 'this:wp_getPostType',
84 'wp.getPostTypes' => 'this:wp_getPostTypes',
85 'wp.getRevisions' => 'this:wp_getRevisions',
86 'wp.restoreRevision' => 'this:wp_restoreRevision',
89 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
90 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
91 'blogger.getPost' => 'this:blogger_getPost',
92 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
93 'blogger.newPost' => 'this:blogger_newPost',
94 'blogger.editPost' => 'this:blogger_editPost',
95 'blogger.deletePost' => 'this:blogger_deletePost',
97 // MetaWeblog API (with MT extensions to structs)
98 'metaWeblog.newPost' => 'this:mw_newPost',
99 'metaWeblog.editPost' => 'this:mw_editPost',
100 'metaWeblog.getPost' => 'this:mw_getPost',
101 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
102 'metaWeblog.getCategories' => 'this:mw_getCategories',
103 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
105 // MetaWeblog API aliases for Blogger API
106 // see http://www.xmlrpc.com/stories/storyReader$2460
107 'metaWeblog.deletePost' => 'this:blogger_deletePost',
108 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
111 'mt.getCategoryList' => 'this:mt_getCategoryList',
112 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
113 'mt.getPostCategories' => 'this:mt_getPostCategories',
114 'mt.setPostCategories' => 'this:mt_setPostCategories',
115 'mt.supportedMethods' => 'this:mt_supportedMethods',
116 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
117 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
118 'mt.publishPost' => 'this:mt_publishPost',
121 'pingback.ping' => 'this:pingback_ping',
122 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
124 'demo.sayHello' => 'this:sayHello',
125 'demo.addTwoNumbers' => 'this:addTwoNumbers'
128 $this->initialise_blog_option_info();
129 $this->methods = apply_filters('xmlrpc_methods', $this->methods);
132 function serve_request() {
133 $this->IXR_Server($this->methods);
137 * Test XMLRPC API by saying, "Hello!" to client.
141 * @param array $args Method Parameters.
144 function sayHello($args) {
149 * Test XMLRPC API by adding two numbers for client.
153 * @param array $args Method Parameters.
156 function addTwoNumbers($args) {
159 return $number1 + $number2;
167 * @param string $username User's username.
168 * @param string $password User's password.
169 * @return mixed WP_User object if authentication passed, false otherwise
171 function login( $username, $password ) {
172 // Respect any old filters against get_option() for 'enable_xmlrpc'.
173 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false ); // Deprecated
174 if ( false === $enabled )
175 $enabled = apply_filters( 'option_enable_xmlrpc', true ); // Deprecated
177 // Proper filter for turning off XML-RPC. It is on by default.
178 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
181 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
185 $user = wp_authenticate($username, $password);
187 if (is_wp_error($user)) {
188 $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
189 $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
193 wp_set_current_user( $user->ID );
198 * Check user's credentials. Deprecated.
202 * @deprecated use wp_xmlrpc_server::login
203 * @see wp_xmlrpc_server::login
205 * @param string $username User's username.
206 * @param string $password User's password.
207 * @return bool Whether authentication passed.
209 function login_pass_ok( $username, $password ) {
210 return (bool) $this->login( $username, $password );
214 * Escape string or array of strings for database.
218 * @param string|array $data Escape single string or array of strings.
219 * @return string|array Type matches $data and sanitized for the database.
221 function escape( &$data ) {
222 if ( ! is_array( $data ) )
223 return wp_slash( $data );
225 foreach ( $data as &$v ) {
226 if ( is_array( $v ) )
228 elseif ( ! is_object( $v ) )
234 * Retrieve custom fields for post.
238 * @param int $post_id Post ID.
239 * @return array Custom fields, if exist.
241 function get_custom_fields($post_id) {
242 $post_id = (int) $post_id;
244 $custom_fields = array();
246 foreach ( (array) has_meta($post_id) as $meta ) {
247 // Don't expose protected fields.
248 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
251 $custom_fields[] = array(
252 "id" => $meta['meta_id'],
253 "key" => $meta['meta_key'],
254 "value" => $meta['meta_value']
258 return $custom_fields;
262 * Set custom fields for post.
266 * @param int $post_id Post ID.
267 * @param array $fields Custom fields.
269 function set_custom_fields($post_id, $fields) {
270 $post_id = (int) $post_id;
272 foreach ( (array) $fields as $meta ) {
273 if ( isset($meta['id']) ) {
274 $meta['id'] = (int) $meta['id'];
275 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
276 if ( isset($meta['key']) ) {
277 $meta['key'] = wp_unslash( $meta['key'] );
278 if ( $meta['key'] !== $pmeta->meta_key )
280 $meta['value'] = wp_unslash( $meta['value'] );
281 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
282 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
283 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
284 delete_metadata_by_mid( 'post', $meta['id'] );
286 } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
287 add_post_meta( $post_id, $meta['key'], $meta['value'] );
293 * Set up blog options property.
295 * Passes property through 'xmlrpc_blog_options' filter.
299 function initialise_blog_option_info() {
302 $this->blog_options = array(
304 'software_name' => array(
305 'desc' => __( 'Software Name' ),
307 'value' => 'WordPress'
309 'software_version' => array(
310 'desc' => __( 'Software Version' ),
312 'value' => $wp_version
315 'desc' => __( 'WordPress Address (URL)' ),
317 'option' => 'siteurl'
320 'desc' => __( 'Site Address (URL)' ),
324 'login_url' => array(
325 'desc' => __( 'Login Address (URL)' ),
327 'value' => wp_login_url( )
329 'admin_url' => array(
330 'desc' => __( 'The URL to the admin area' ),
332 'value' => get_admin_url( )
334 'image_default_link_type' => array(
335 'desc' => __( 'Image default link type' ),
337 'option' => 'image_default_link_type'
339 'image_default_size' => array(
340 'desc' => __( 'Image default size' ),
342 'option' => 'image_default_size'
344 'image_default_align' => array(
345 'desc' => __( 'Image default align' ),
347 'option' => 'image_default_align'
350 'desc' => __( 'Template' ),
352 'option' => 'template'
354 'stylesheet' => array(
355 'desc' => __( 'Stylesheet' ),
357 'option' => 'stylesheet'
359 'post_thumbnail' => array(
360 'desc' => __('Post Thumbnail'),
362 'value' => current_theme_supports( 'post-thumbnails' )
366 'time_zone' => array(
367 'desc' => __( 'Time Zone' ),
369 'option' => 'gmt_offset'
371 'blog_title' => array(
372 'desc' => __( 'Site Title' ),
374 'option' => 'blogname'
376 'blog_tagline' => array(
377 'desc' => __( 'Site Tagline' ),
379 'option' => 'blogdescription'
381 'date_format' => array(
382 'desc' => __( 'Date Format' ),
384 'option' => 'date_format'
386 'time_format' => array(
387 'desc' => __( 'Time Format' ),
389 'option' => 'time_format'
391 'users_can_register' => array(
392 'desc' => __( 'Allow new users to sign up' ),
394 'option' => 'users_can_register'
396 'thumbnail_size_w' => array(
397 'desc' => __( 'Thumbnail Width' ),
399 'option' => 'thumbnail_size_w'
401 'thumbnail_size_h' => array(
402 'desc' => __( 'Thumbnail Height' ),
404 'option' => 'thumbnail_size_h'
406 'thumbnail_crop' => array(
407 'desc' => __( 'Crop thumbnail to exact dimensions' ),
409 'option' => 'thumbnail_crop'
411 'medium_size_w' => array(
412 'desc' => __( 'Medium size image width' ),
414 'option' => 'medium_size_w'
416 'medium_size_h' => array(
417 'desc' => __( 'Medium size image height' ),
419 'option' => 'medium_size_h'
421 'large_size_w' => array(
422 'desc' => __( 'Large size image width' ),
424 'option' => 'large_size_w'
426 'large_size_h' => array(
427 'desc' => __( 'Large size image height' ),
429 'option' => 'large_size_h'
431 'default_comment_status' => array(
432 'desc' => __( 'Allow people to post comments on new articles' ),
434 'option' => 'default_comment_status'
436 'default_ping_status' => array(
437 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
439 'option' => 'default_ping_status'
443 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
447 * Retrieve the blogs of the user.
451 * @param array $args Method parameters. Contains:
454 * @return array. Contains:
459 * - 'xmlrpc' - url of xmlrpc endpoint
461 function wp_getUsersBlogs( $args ) {
462 global $current_site;
463 // If this isn't on WPMU then just use blogger_getUsersBlogs
464 if ( !is_multisite() ) {
465 array_unshift( $args, 1 );
466 return $this->blogger_getUsersBlogs( $args );
469 $this->escape( $args );
471 $username = $args[0];
472 $password = $args[1];
474 if ( !$user = $this->login($username, $password) )
477 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
479 $blogs = (array) get_blogs_of_user( $user->ID );
482 foreach ( $blogs as $blog ) {
483 // Don't include blogs that aren't hosted at this site
484 if ( $blog->site_id != $current_site->id )
487 $blog_id = $blog->userblog_id;
489 switch_to_blog( $blog_id );
491 $is_admin = current_user_can( 'manage_options' );
494 'isAdmin' => $is_admin,
495 'url' => home_url( '/' ),
496 'blogid' => (string) $blog_id,
497 'blogName' => get_option( 'blogname' ),
498 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
501 restore_current_blog();
508 * Checks if the method received at least the minimum number of arguments.
512 * @param string|array $args Sanitize single string or array of strings.
513 * @param int $count Minimum number of arguments.
514 * @return boolean if $args contains at least $count arguments.
516 protected function minimum_args( $args, $count ) {
517 if ( count( $args ) < $count ) {
518 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
526 * Prepares taxonomy data for return in an XML-RPC object.
530 * @param object $taxonomy The unprepared taxonomy data
531 * @param array $fields The subset of taxonomy fields to return
532 * @return array The prepared taxonomy data
534 protected function _prepare_taxonomy( $taxonomy, $fields ) {
536 'name' => $taxonomy->name,
537 'label' => $taxonomy->label,
538 'hierarchical' => (bool) $taxonomy->hierarchical,
539 'public' => (bool) $taxonomy->public,
540 'show_ui' => (bool) $taxonomy->show_ui,
541 '_builtin' => (bool) $taxonomy->_builtin,
544 if ( in_array( 'labels', $fields ) )
545 $_taxonomy['labels'] = (array) $taxonomy->labels;
547 if ( in_array( 'cap', $fields ) )
548 $_taxonomy['cap'] = (array) $taxonomy->cap;
550 if ( in_array( 'menu', $fields ) )
551 $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
553 if ( in_array( 'object_type', $fields ) )
554 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
556 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
560 * Prepares term data for return in an XML-RPC object.
564 * @param array|object $term The unprepared term data
565 * @return array The prepared term data
567 protected function _prepare_term( $term ) {
569 if ( ! is_array( $_term) )
570 $_term = get_object_vars( $_term );
572 // For integers which may be larger than XML-RPC supports ensure we return strings.
573 $_term['term_id'] = strval( $_term['term_id'] );
574 $_term['term_group'] = strval( $_term['term_group'] );
575 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
576 $_term['parent'] = strval( $_term['parent'] );
578 // Count we are happy to return as an integer because people really shouldn't use terms that much.
579 $_term['count'] = intval( $_term['count'] );
581 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
585 * Convert a WordPress date string to an IXR_Date object.
589 * @param string $date
592 protected function _convert_date( $date ) {
593 if ( $date === '0000-00-00 00:00:00' ) {
594 return new IXR_Date( '00000000T00:00:00Z' );
596 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
600 * Convert a WordPress GMT date string to an IXR_Date object.
604 * @param string $date_gmt
605 * @param string $date
608 protected function _convert_date_gmt( $date_gmt, $date ) {
609 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
610 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
612 return $this->_convert_date( $date_gmt );
616 * Prepares post data for return in an XML-RPC object.
620 * @param array $post The unprepared post data
621 * @param array $fields The subset of post type fields to return
622 * @return array The prepared post data
624 protected function _prepare_post( $post, $fields ) {
625 // holds the data for this post. built up based on $fields
626 $_post = array( 'post_id' => strval( $post['ID'] ) );
628 // prepare common post fields
629 $post_fields = array(
630 'post_title' => $post['post_title'],
631 'post_date' => $this->_convert_date( $post['post_date'] ),
632 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
633 'post_modified' => $this->_convert_date( $post['post_modified'] ),
634 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
635 'post_status' => $post['post_status'],
636 'post_type' => $post['post_type'],
637 'post_name' => $post['post_name'],
638 'post_author' => $post['post_author'],
639 'post_password' => $post['post_password'],
640 'post_excerpt' => $post['post_excerpt'],
641 'post_content' => $post['post_content'],
642 'post_parent' => strval( $post['post_parent'] ),
643 'post_mime_type' => $post['post_mime_type'],
644 'link' => post_permalink( $post['ID'] ),
645 'guid' => $post['guid'],
646 'menu_order' => intval( $post['menu_order'] ),
647 'comment_status' => $post['comment_status'],
648 'ping_status' => $post['ping_status'],
649 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
653 $post_fields['post_thumbnail'] = array();
654 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
655 if ( $thumbnail_id ) {
656 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
657 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
660 // Consider future posts as published
661 if ( $post_fields['post_status'] === 'future' )
662 $post_fields['post_status'] = 'publish';
664 // Fill in blank post format
665 $post_fields['post_format'] = get_post_format( $post['ID'] );
666 if ( empty( $post_fields['post_format'] ) )
667 $post_fields['post_format'] = 'standard';
669 // Merge requested $post_fields fields into $_post
670 if ( in_array( 'post', $fields ) ) {
671 $_post = array_merge( $_post, $post_fields );
673 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
674 $_post = array_merge( $_post, $requested_fields );
677 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
679 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
680 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
681 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
682 $_post['terms'] = array();
683 foreach ( $terms as $term ) {
684 $_post['terms'][] = $this->_prepare_term( $term );
688 if ( in_array( 'custom_fields', $fields ) )
689 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
691 if ( in_array( 'enclosure', $fields ) ) {
692 $_post['enclosure'] = array();
693 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
694 if ( ! empty( $enclosures ) ) {
695 $encdata = explode( "\n", $enclosures[0] );
696 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
697 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
698 $_post['enclosure']['type'] = trim( $encdata[2] );
702 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
706 * Prepares post data for return in an XML-RPC object.
710 * @param object $post_type Post type object
711 * @param array $fields The subset of post fields to return
712 * @return array The prepared post type data
714 protected function _prepare_post_type( $post_type, $fields ) {
716 'name' => $post_type->name,
717 'label' => $post_type->label,
718 'hierarchical' => (bool) $post_type->hierarchical,
719 'public' => (bool) $post_type->public,
720 'show_ui' => (bool) $post_type->show_ui,
721 '_builtin' => (bool) $post_type->_builtin,
722 'has_archive' => (bool) $post_type->has_archive,
723 'supports' => get_all_post_type_supports( $post_type->name ),
726 if ( in_array( 'labels', $fields ) ) {
727 $_post_type['labels'] = (array) $post_type->labels;
730 if ( in_array( 'cap', $fields ) ) {
731 $_post_type['cap'] = (array) $post_type->cap;
732 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
735 if ( in_array( 'menu', $fields ) ) {
736 $_post_type['menu_position'] = (int) $post_type->menu_position;
737 $_post_type['menu_icon'] = $post_type->menu_icon;
738 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
741 if ( in_array( 'taxonomies', $fields ) )
742 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
744 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
748 * Prepares media item data for return in an XML-RPC object.
752 * @param object $media_item The unprepared media item data
753 * @param string $thumbnail_size The image size to use for the thumbnail URL
754 * @return array The prepared media item data
756 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
757 $_media_item = array(
758 'attachment_id' => strval( $media_item->ID ),
759 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
760 'parent' => $media_item->post_parent,
761 'link' => wp_get_attachment_url( $media_item->ID ),
762 'title' => $media_item->post_title,
763 'caption' => $media_item->post_excerpt,
764 'description' => $media_item->post_content,
765 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
768 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
769 if ( $thumbnail_src )
770 $_media_item['thumbnail'] = $thumbnail_src[0];
772 $_media_item['thumbnail'] = $_media_item['link'];
774 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
778 * Prepares page data for return in an XML-RPC object.
782 * @param object $page The unprepared page data
783 * @return array The prepared page data
785 protected function _prepare_page( $page ) {
786 // Get all of the page content and link.
787 $full_page = get_extended( $page->post_content );
788 $link = post_permalink( $page->ID );
790 // Get info the page parent if there is one.
792 if ( ! empty( $page->post_parent ) ) {
793 $parent = get_post( $page->post_parent );
794 $parent_title = $parent->post_title;
797 // Determine comment and ping settings.
798 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
799 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
802 $page_date = $this->_convert_date( $page->post_date );
803 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
805 // Pull the categories info together.
806 $categories = array();
807 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
808 $categories[] = get_cat_name( $cat_id );
811 // Get the author info.
812 $author = get_userdata( $page->post_author );
814 $page_template = get_page_template_slug( $page->ID );
815 if ( empty( $page_template ) )
816 $page_template = 'default';
819 'dateCreated' => $page_date,
820 'userid' => $page->post_author,
821 'page_id' => $page->ID,
822 'page_status' => $page->post_status,
823 'description' => $full_page['main'],
824 'title' => $page->post_title,
826 'permaLink' => $link,
827 'categories' => $categories,
828 'excerpt' => $page->post_excerpt,
829 'text_more' => $full_page['extended'],
830 'mt_allow_comments' => $allow_comments,
831 'mt_allow_pings' => $allow_pings,
832 'wp_slug' => $page->post_name,
833 'wp_password' => $page->post_password,
834 'wp_author' => $author->display_name,
835 'wp_page_parent_id' => $page->post_parent,
836 'wp_page_parent_title' => $parent_title,
837 'wp_page_order' => $page->menu_order,
838 'wp_author_id' => (string) $author->ID,
839 'wp_author_display_name' => $author->display_name,
840 'date_created_gmt' => $page_date_gmt,
841 'custom_fields' => $this->get_custom_fields( $page->ID ),
842 'wp_page_template' => $page_template
845 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
849 * Prepares comment data for return in an XML-RPC object.
853 * @param object $comment The unprepared comment data
854 * @return array The prepared comment data
856 protected function _prepare_comment( $comment ) {
858 $comment_date = $this->_convert_date( $comment->comment_date );
859 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
861 if ( '0' == $comment->comment_approved )
862 $comment_status = 'hold';
863 else if ( 'spam' == $comment->comment_approved )
864 $comment_status = 'spam';
865 else if ( '1' == $comment->comment_approved )
866 $comment_status = 'approve';
868 $comment_status = $comment->comment_approved;
871 'date_created_gmt' => $comment_date_gmt,
872 'user_id' => $comment->user_id,
873 'comment_id' => $comment->comment_ID,
874 'parent' => $comment->comment_parent,
875 'status' => $comment_status,
876 'content' => $comment->comment_content,
877 'link' => get_comment_link($comment),
878 'post_id' => $comment->comment_post_ID,
879 'post_title' => get_the_title($comment->comment_post_ID),
880 'author' => $comment->comment_author,
881 'author_url' => $comment->comment_author_url,
882 'author_email' => $comment->comment_author_email,
883 'author_ip' => $comment->comment_author_IP,
884 'type' => $comment->comment_type,
887 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
891 * Prepares user data for return in an XML-RPC object.
895 * @param WP_User $user The unprepared user object
896 * @param array $fields The subset of user fields to return
897 * @return array The prepared user data
899 protected function _prepare_user( $user, $fields ) {
900 $_user = array( 'user_id' => strval( $user->ID ) );
902 $user_fields = array(
903 'username' => $user->user_login,
904 'first_name' => $user->user_firstname,
905 'last_name' => $user->user_lastname,
906 'registered' => $this->_convert_date( $user->user_registered ),
907 'bio' => $user->user_description,
908 'email' => $user->user_email,
909 'nickname' => $user->nickname,
910 'nicename' => $user->user_nicename,
911 'url' => $user->user_url,
912 'display_name' => $user->display_name,
913 'roles' => $user->roles,
916 if ( in_array( 'all', $fields ) ) {
917 $_user = array_merge( $_user, $user_fields );
919 if ( in_array( 'basic', $fields ) ) {
920 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
921 $fields = array_merge( $fields, $basic_fields );
923 $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
924 $_user = array_merge( $_user, $requested_fields );
927 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
931 * Create a new post for any registered post type.
935 * @param array $args Method parameters. Contains:
939 * - array $content_struct
940 * $content_struct can contain:
941 * - post_type (default: 'post')
942 * - post_status (default: 'draft')
947 * - post_date_gmt | post_date
950 * - comment_status - can be 'open' | 'closed'
951 * - ping_status - can be 'open' | 'closed'
953 * - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
954 * - custom_fields - array, with each element containing 'key' and 'value'
955 * - terms - array, with taxonomy names as keys and arrays of term IDs as values
956 * - terms_names - array, with taxonomy names as keys and arrays of term names as values
958 * - any other fields supported by wp_insert_post()
959 * @return string post_id
961 function wp_newPost( $args ) {
962 if ( ! $this->minimum_args( $args, 4 ) )
965 $this->escape( $args );
967 $blog_id = (int) $args[0];
968 $username = $args[1];
969 $password = $args[2];
970 $content_struct = $args[3];
972 if ( ! $user = $this->login( $username, $password ) )
975 do_action( 'xmlrpc_call', 'wp.newPost' );
977 unset( $content_struct['ID'] );
979 return $this->_insert_post( $user, $content_struct );
983 * Helper method for filtering out elements from an array.
987 * @param int $count Number to compare to one.
989 private function _is_greater_than_one( $count ) {
994 * Helper method for wp_newPost and wp_editPost, containing shared logic.
997 * @uses wp_insert_post()
999 * @param WP_User $user The post author if post_author isn't set in $content_struct.
1000 * @param array $content_struct Post data to insert.
1002 protected function _insert_post( $user, $content_struct ) {
1003 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1004 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1006 $post_data = wp_parse_args( $content_struct, $defaults );
1008 $post_type = get_post_type_object( $post_data['post_type'] );
1010 return new IXR_Error( 403, __( 'Invalid post type' ) );
1012 $update = ! empty( $post_data['ID'] );
1015 if ( ! get_post( $post_data['ID'] ) )
1016 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1017 if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1018 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1019 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1020 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1022 if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1023 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1026 switch ( $post_data['post_status'] ) {
1031 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1032 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1036 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1037 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1040 if ( ! get_post_status_object( $post_data['post_status'] ) )
1041 $post_data['post_status'] = 'draft';
1045 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1046 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1048 $post_data['post_author'] = absint( $post_data['post_author'] );
1049 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1050 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1051 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1053 $author = get_userdata( $post_data['post_author'] );
1056 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1058 $post_data['post_author'] = $user->ID;
1061 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1062 unset( $post_data['comment_status'] );
1064 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1065 unset( $post_data['ping_status'] );
1067 // Do some timestamp voodoo
1068 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1069 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1070 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1071 } elseif ( ! empty( $post_data['post_date'] ) ) {
1072 $dateCreated = $post_data['post_date']->getIso();
1075 if ( ! empty( $dateCreated ) ) {
1076 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1077 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1080 if ( ! isset( $post_data['ID'] ) )
1081 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1082 $post_ID = $post_data['ID'];
1084 if ( $post_data['post_type'] == 'post' ) {
1085 // Private and password-protected posts cannot be stickied.
1086 if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
1087 // Error if the client tried to stick the post, otherwise, silently unstick.
1088 if ( ! empty( $post_data['sticky'] ) )
1089 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1091 unstick_post( $post_ID );
1092 } elseif ( isset( $post_data['sticky'] ) ) {
1093 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1094 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1095 if ( $post_data['sticky'] )
1096 stick_post( $post_ID );
1098 unstick_post( $post_ID );
1102 if ( isset( $post_data['post_thumbnail'] ) ) {
1103 // empty value deletes, non-empty value adds/updates
1104 if ( ! $post_data['post_thumbnail'] )
1105 delete_post_thumbnail( $post_ID );
1106 elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1107 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1108 set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1109 unset( $content_struct['post_thumbnail'] );
1112 if ( isset( $post_data['custom_fields'] ) )
1113 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1115 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1116 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1118 // accumulate term IDs from terms and terms_names
1121 // first validate the terms specified by ID
1122 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1123 $taxonomies = array_keys( $post_data['terms'] );
1125 // validating term ids
1126 foreach ( $taxonomies as $taxonomy ) {
1127 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1128 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1130 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1131 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1133 $term_ids = $post_data['terms'][$taxonomy];
1134 foreach ( $term_ids as $term_id ) {
1135 $term = get_term_by( 'id', $term_id, $taxonomy );
1138 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1140 $terms[$taxonomy][] = (int) $term_id;
1145 // now validate terms specified by name
1146 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1147 $taxonomies = array_keys( $post_data['terms_names'] );
1149 foreach ( $taxonomies as $taxonomy ) {
1150 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1151 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1153 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1154 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1156 // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
1157 $ambiguous_terms = array();
1158 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1159 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1161 // count the number of terms with the same name
1162 $tax_term_names_count = array_count_values( $tax_term_names );
1164 // filter out non-ambiguous term names
1165 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1167 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1170 $term_names = $post_data['terms_names'][$taxonomy];
1171 foreach ( $term_names as $term_name ) {
1172 if ( in_array( $term_name, $ambiguous_terms ) )
1173 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1175 $term = get_term_by( 'name', $term_name, $taxonomy );
1178 // term doesn't exist, so check that the user is allowed to create new terms
1179 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1180 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1182 // create the new term
1183 $term_info = wp_insert_term( $term_name, $taxonomy );
1184 if ( is_wp_error( $term_info ) )
1185 return new IXR_Error( 500, $term_info->get_error_message() );
1187 $terms[$taxonomy][] = (int) $term_info['term_id'];
1189 $terms[$taxonomy][] = (int) $term->term_id;
1195 $post_data['tax_input'] = $terms;
1196 unset( $post_data['terms'], $post_data['terms_names'] );
1198 // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
1199 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1202 if ( isset( $post_data['post_format'] ) ) {
1203 $format = set_post_format( $post_ID, $post_data['post_format'] );
1205 if ( is_wp_error( $format ) )
1206 return new IXR_Error( 500, $format->get_error_message() );
1208 unset( $post_data['post_format'] );
1211 // Handle enclosures
1212 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1213 $this->add_enclosure_if_new( $post_ID, $enclosure );
1215 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1217 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1219 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1220 if ( is_wp_error( $post_ID ) )
1221 return new IXR_Error( 500, $post_ID->get_error_message() );
1224 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1226 return strval( $post_ID );
1230 * Edit a post for any registered post type.
1232 * The $content_struct parameter only needs to contain fields that
1233 * should be changed. All other fields will retain their existing values.
1237 * @param array $args Method parameters. Contains:
1239 * - string $username
1240 * - string $password
1242 * - array $content_struct
1243 * @return true on success
1245 function wp_editPost( $args ) {
1246 if ( ! $this->minimum_args( $args, 5 ) )
1247 return $this->error;
1249 $this->escape( $args );
1251 $blog_id = (int) $args[0];
1252 $username = $args[1];
1253 $password = $args[2];
1254 $post_id = (int) $args[3];
1255 $content_struct = $args[4];
1257 if ( ! $user = $this->login( $username, $password ) )
1258 return $this->error;
1260 do_action( 'xmlrpc_call', 'wp.editPost' );
1262 $post = get_post( $post_id, ARRAY_A );
1264 if ( empty( $post['ID'] ) )
1265 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1267 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1268 // If the post has been modified since the date provided, return an error.
1269 if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1270 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1274 // convert the date field back to IXR form
1275 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1277 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1278 // since _insert_post will ignore the non-GMT date if the GMT date is set
1279 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1280 unset( $post['post_date_gmt'] );
1282 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1284 $this->escape( $post );
1285 $merged_content_struct = array_merge( $post, $content_struct );
1287 $retval = $this->_insert_post( $user, $merged_content_struct );
1288 if ( $retval instanceof IXR_Error )
1295 * Delete a post for any registered post type.
1299 * @uses wp_delete_post()
1300 * @param array $args Method parameters. Contains:
1302 * - string $username
1303 * - string $password
1305 * @return true on success
1307 function wp_deletePost( $args ) {
1308 if ( ! $this->minimum_args( $args, 4 ) )
1309 return $this->error;
1311 $this->escape( $args );
1313 $blog_id = (int) $args[0];
1314 $username = $args[1];
1315 $password = $args[2];
1316 $post_id = (int) $args[3];
1318 if ( ! $user = $this->login( $username, $password ) )
1319 return $this->error;
1321 do_action( 'xmlrpc_call', 'wp.deletePost' );
1323 $post = get_post( $post_id, ARRAY_A );
1324 if ( empty( $post['ID'] ) )
1325 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1327 if ( ! current_user_can( 'delete_post', $post_id ) )
1328 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1330 $result = wp_delete_post( $post_id );
1333 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1343 * The optional $fields parameter specifies what fields will be included
1344 * in the response array. This should be a list of field names. 'post_id' will
1345 * always be included in the response regardless of the value of $fields.
1347 * Instead of, or in addition to, individual field names, conceptual group
1348 * names can be used to specify multiple fields. The available conceptual
1349 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1353 * @param array $args Method parameters. Contains:
1355 * - string $username
1356 * - string $password
1357 * - array $fields optional
1358 * @return array contains (based on $fields parameter):
1364 * - 'post_modified_gmt'
1373 * - 'comment_status'
1382 function wp_getPost( $args ) {
1383 if ( ! $this->minimum_args( $args, 4 ) )
1384 return $this->error;
1386 $this->escape( $args );
1388 $blog_id = (int) $args[0];
1389 $username = $args[1];
1390 $password = $args[2];
1391 $post_id = (int) $args[3];
1393 if ( isset( $args[4] ) )
1396 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1398 if ( ! $user = $this->login( $username, $password ) )
1399 return $this->error;
1401 do_action( 'xmlrpc_call', 'wp.getPost' );
1403 $post = get_post( $post_id, ARRAY_A );
1405 if ( empty( $post['ID'] ) )
1406 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1408 if ( ! current_user_can( 'edit_post', $post_id ) )
1409 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1411 return $this->_prepare_post( $post, $fields );
1419 * The optional $filter parameter modifies the query used to retrieve posts.
1420 * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
1421 * 'orderby', and 'order'.
1423 * The optional $fields parameter specifies what fields will be included
1424 * in the response array.
1426 * @uses wp_get_recent_posts()
1427 * @see wp_getPost() for more on $fields
1428 * @see get_posts() for more on $filter values
1430 * @param array $args Method parameters. Contains:
1432 * - string $username
1433 * - string $password
1434 * - array $filter optional
1435 * - array $fields optional
1436 * @return array contains a collection of posts.
1438 function wp_getPosts( $args ) {
1439 if ( ! $this->minimum_args( $args, 3 ) )
1440 return $this->error;
1442 $this->escape( $args );
1444 $blog_id = (int) $args[0];
1445 $username = $args[1];
1446 $password = $args[2];
1447 $filter = isset( $args[3] ) ? $args[3] : array();
1449 if ( isset( $args[4] ) )
1452 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1454 if ( ! $user = $this->login( $username, $password ) )
1455 return $this->error;
1457 do_action( 'xmlrpc_call', 'wp.getPosts' );
1461 if ( isset( $filter['post_type'] ) ) {
1462 $post_type = get_post_type_object( $filter['post_type'] );
1463 if ( ! ( (bool) $post_type ) )
1464 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1466 $post_type = get_post_type_object( 'post' );
1469 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1470 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1472 $query['post_type'] = $post_type->name;
1474 if ( isset( $filter['post_status'] ) )
1475 $query['post_status'] = $filter['post_status'];
1477 if ( isset( $filter['number'] ) )
1478 $query['numberposts'] = absint( $filter['number'] );
1480 if ( isset( $filter['offset'] ) )
1481 $query['offset'] = absint( $filter['offset'] );
1483 if ( isset( $filter['orderby'] ) ) {
1484 $query['orderby'] = $filter['orderby'];
1486 if ( isset( $filter['order'] ) )
1487 $query['order'] = $filter['order'];
1490 if ( isset( $filter['s'] ) ) {
1491 $query['s'] = $filter['s'];
1494 $posts_list = wp_get_recent_posts( $query );
1496 if ( ! $posts_list )
1499 // holds all the posts data
1502 foreach ( $posts_list as $post ) {
1503 if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1506 $struct[] = $this->_prepare_post( $post, $fields );
1513 * Create a new term.
1517 * @uses wp_insert_term()
1518 * @param array $args Method parameters. Contains:
1520 * - string $username
1521 * - string $password
1522 * - array $content_struct
1523 * The $content_struct must contain:
1526 * Also, it can optionally contain:
1530 * @return string term_id
1532 function wp_newTerm( $args ) {
1533 if ( ! $this->minimum_args( $args, 4 ) )
1534 return $this->error;
1536 $this->escape( $args );
1538 $blog_id = (int) $args[0];
1539 $username = $args[1];
1540 $password = $args[2];
1541 $content_struct = $args[3];
1543 if ( ! $user = $this->login( $username, $password ) )
1544 return $this->error;
1546 do_action( 'xmlrpc_call', 'wp.newTerm' );
1548 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1549 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1551 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1553 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1554 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1556 $taxonomy = (array) $taxonomy;
1558 // hold the data of the term
1559 $term_data = array();
1561 $term_data['name'] = trim( $content_struct['name'] );
1562 if ( empty( $term_data['name'] ) )
1563 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1565 if ( isset( $content_struct['parent'] ) ) {
1566 if ( ! $taxonomy['hierarchical'] )
1567 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1569 $parent_term_id = (int) $content_struct['parent'];
1570 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1572 if ( is_wp_error( $parent_term ) )
1573 return new IXR_Error( 500, $parent_term->get_error_message() );
1575 if ( ! $parent_term )
1576 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1578 $term_data['parent'] = $content_struct['parent'];
1581 if ( isset( $content_struct['description'] ) )
1582 $term_data['description'] = $content_struct['description'];
1584 if ( isset( $content_struct['slug'] ) )
1585 $term_data['slug'] = $content_struct['slug'];
1587 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1589 if ( is_wp_error( $term ) )
1590 return new IXR_Error( 500, $term->get_error_message() );
1593 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1595 return strval( $term['term_id'] );
1603 * @uses wp_update_term()
1604 * @param array $args Method parameters. Contains:
1606 * - string $username
1607 * - string $password
1609 * - array $content_struct
1610 * The $content_struct must contain:
1612 * Also, it can optionally contain:
1617 * @return bool True, on success.
1619 function wp_editTerm( $args ) {
1620 if ( ! $this->minimum_args( $args, 5 ) )
1621 return $this->error;
1623 $this->escape( $args );
1625 $blog_id = (int) $args[0];
1626 $username = $args[1];
1627 $password = $args[2];
1628 $term_id = (int) $args[3];
1629 $content_struct = $args[4];
1631 if ( ! $user = $this->login( $username, $password ) )
1632 return $this->error;
1634 do_action( 'xmlrpc_call', 'wp.editTerm' );
1636 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1637 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1639 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1641 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1642 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1644 $taxonomy = (array) $taxonomy;
1646 // hold the data of the term
1647 $term_data = array();
1649 $term = get_term( $term_id , $content_struct['taxonomy'] );
1651 if ( is_wp_error( $term ) )
1652 return new IXR_Error( 500, $term->get_error_message() );
1655 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1657 if ( isset( $content_struct['name'] ) ) {
1658 $term_data['name'] = trim( $content_struct['name'] );
1660 if ( empty( $term_data['name'] ) )
1661 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1664 if ( isset( $content_struct['parent'] ) ) {
1665 if ( ! $taxonomy['hierarchical'] )
1666 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1668 $parent_term_id = (int) $content_struct['parent'];
1669 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1671 if ( is_wp_error( $parent_term ) )
1672 return new IXR_Error( 500, $parent_term->get_error_message() );
1674 if ( ! $parent_term )
1675 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1677 $term_data['parent'] = $content_struct['parent'];
1680 if ( isset( $content_struct['description'] ) )
1681 $term_data['description'] = $content_struct['description'];
1683 if ( isset( $content_struct['slug'] ) )
1684 $term_data['slug'] = $content_struct['slug'];
1686 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1688 if ( is_wp_error( $term ) )
1689 return new IXR_Error( 500, $term->get_error_message() );
1692 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1702 * @uses wp_delete_term()
1703 * @param array $args Method parameters. Contains:
1705 * - string $username
1706 * - string $password
1707 * - string $taxnomy_name
1709 * @return boolean|IXR_Error If it suceeded true else a reason why not
1711 function wp_deleteTerm( $args ) {
1712 if ( ! $this->minimum_args( $args, 5 ) )
1713 return $this->error;
1715 $this->escape( $args );
1717 $blog_id = (int) $args[0];
1718 $username = $args[1];
1719 $password = $args[2];
1720 $taxonomy = $args[3];
1721 $term_id = (int) $args[4];
1723 if ( ! $user = $this->login( $username, $password ) )
1724 return $this->error;
1726 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1728 if ( ! taxonomy_exists( $taxonomy ) )
1729 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1731 $taxonomy = get_taxonomy( $taxonomy );
1733 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
1734 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
1736 $term = get_term( $term_id, $taxonomy->name );
1738 if ( is_wp_error( $term ) )
1739 return new IXR_Error( 500, $term->get_error_message() );
1742 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1744 $result = wp_delete_term( $term_id, $taxonomy->name );
1746 if ( is_wp_error( $result ) )
1747 return new IXR_Error( 500, $term->get_error_message() );
1750 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
1761 * @param array $args Method parameters. Contains:
1763 * - string $username
1764 * - string $password
1765 * - string $taxonomy
1767 * @return array contains:
1772 * - 'term_taxonomy_id'
1778 function wp_getTerm( $args ) {
1779 if ( ! $this->minimum_args( $args, 5 ) )
1780 return $this->error;
1782 $this->escape( $args );
1784 $blog_id = (int) $args[0];
1785 $username = $args[1];
1786 $password = $args[2];
1787 $taxonomy = $args[3];
1788 $term_id = (int) $args[4];
1790 if ( ! $user = $this->login( $username, $password ) )
1791 return $this->error;
1793 do_action( 'xmlrpc_call', 'wp.getTerm' );
1795 if ( ! taxonomy_exists( $taxonomy ) )
1796 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1798 $taxonomy = get_taxonomy( $taxonomy );
1800 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1801 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1803 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
1805 if ( is_wp_error( $term ) )
1806 return new IXR_Error( 500, $term->get_error_message() );
1809 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1811 return $this->_prepare_term( $term );
1815 * Retrieve all terms for a taxonomy.
1819 * The optional $filter parameter modifies the query used to retrieve terms.
1820 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
1823 * @param array $args Method parameters. Contains:
1825 * - string $username
1826 * - string $password
1827 * - string $taxonomy
1828 * - array $filter optional
1829 * @return array terms
1831 function wp_getTerms( $args ) {
1832 if ( ! $this->minimum_args( $args, 4 ) )
1833 return $this->error;
1835 $this->escape( $args );
1837 $blog_id = (int) $args[0];
1838 $username = $args[1];
1839 $password = $args[2];
1840 $taxonomy = $args[3];
1841 $filter = isset( $args[4] ) ? $args[4] : array();
1843 if ( ! $user = $this->login( $username, $password ) )
1844 return $this->error;
1846 do_action( 'xmlrpc_call', 'wp.getTerms' );
1848 if ( ! taxonomy_exists( $taxonomy ) )
1849 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1851 $taxonomy = get_taxonomy( $taxonomy );
1853 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1854 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1858 if ( isset( $filter['number'] ) )
1859 $query['number'] = absint( $filter['number'] );
1861 if ( isset( $filter['offset'] ) )
1862 $query['offset'] = absint( $filter['offset'] );
1864 if ( isset( $filter['orderby'] ) ) {
1865 $query['orderby'] = $filter['orderby'];
1867 if ( isset( $filter['order'] ) )
1868 $query['order'] = $filter['order'];
1871 if ( isset( $filter['hide_empty'] ) )
1872 $query['hide_empty'] = $filter['hide_empty'];
1874 $query['get'] = 'all';
1876 if ( isset( $filter['search'] ) )
1877 $query['search'] = $filter['search'];
1879 $terms = get_terms( $taxonomy->name, $query );
1881 if ( is_wp_error( $terms ) )
1882 return new IXR_Error( 500, $terms->get_error_message() );
1886 foreach ( $terms as $term ) {
1887 $struct[] = $this->_prepare_term( $term );
1894 * Retrieve a taxonomy.
1898 * @uses get_taxonomy()
1899 * @param array $args Method parameters. Contains:
1901 * - string $username
1902 * - string $password
1903 * - string $taxonomy
1904 * @return array (@see get_taxonomy())
1906 function wp_getTaxonomy( $args ) {
1907 if ( ! $this->minimum_args( $args, 4 ) )
1908 return $this->error;
1910 $this->escape( $args );
1912 $blog_id = (int) $args[0];
1913 $username = $args[1];
1914 $password = $args[2];
1915 $taxonomy = $args[3];
1917 if ( isset( $args[4] ) )
1920 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
1922 if ( ! $user = $this->login( $username, $password ) )
1923 return $this->error;
1925 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
1927 if ( ! taxonomy_exists( $taxonomy ) )
1928 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1930 $taxonomy = get_taxonomy( $taxonomy );
1932 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1933 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1935 return $this->_prepare_taxonomy( $taxonomy, $fields );
1939 * Retrieve all taxonomies.
1943 * @uses get_taxonomies()
1944 * @param array $args Method parameters. Contains:
1946 * - string $username
1947 * - string $password
1948 * @return array taxonomies
1950 function wp_getTaxonomies( $args ) {
1951 if ( ! $this->minimum_args( $args, 3 ) )
1952 return $this->error;
1954 $this->escape( $args );
1956 $blog_id = (int) $args[0];
1957 $username = $args[1];
1958 $password = $args[2];
1959 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
1961 if ( isset( $args[4] ) )
1964 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
1966 if ( ! $user = $this->login( $username, $password ) )
1967 return $this->error;
1969 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
1971 $taxonomies = get_taxonomies( $filter, 'objects' );
1973 // holds all the taxonomy data
1976 foreach ( $taxonomies as $taxonomy ) {
1977 // capability check for post_types
1978 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1981 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
1990 * The optional $fields parameter specifies what fields will be included
1991 * in the response array. This should be a list of field names. 'user_id' will
1992 * always be included in the response regardless of the value of $fields.
1994 * Instead of, or in addition to, individual field names, conceptual group
1995 * names can be used to specify multiple fields. The available conceptual
1996 * groups are 'basic' and 'all'.
1998 * @uses get_userdata()
1999 * @param array $args Method parameters. Contains:
2001 * - string $username
2002 * - string $password
2004 * - array $fields optional
2005 * @return array contains (based on $fields parameter):
2019 function wp_getUser( $args ) {
2020 if ( ! $this->minimum_args( $args, 4 ) )
2021 return $this->error;
2023 $this->escape( $args );
2025 $blog_id = (int) $args[0];
2026 $username = $args[1];
2027 $password = $args[2];
2028 $user_id = (int) $args[3];
2030 if ( isset( $args[4] ) )
2033 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2035 if ( ! $user = $this->login( $username, $password ) )
2036 return $this->error;
2038 do_action( 'xmlrpc_call', 'wp.getUser' );
2040 if ( ! current_user_can( 'edit_user', $user_id ) )
2041 return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2043 $user_data = get_userdata( $user_id );
2046 return new IXR_Error( 404, __( 'Invalid user ID' ) );
2048 return $this->_prepare_user( $user_data, $fields );
2054 * The optional $filter parameter modifies the query used to retrieve users.
2055 * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2056 * 'who', 'orderby', and 'order'.
2058 * The optional $fields parameter specifies what fields will be included
2059 * in the response array.
2062 * @see wp_getUser() for more on $fields and return values
2064 * @param array $args Method parameters. Contains:
2066 * - string $username
2067 * - string $password
2068 * - array $filter optional
2069 * - array $fields optional
2070 * @return array users data
2072 function wp_getUsers( $args ) {
2073 if ( ! $this->minimum_args( $args, 3 ) )
2074 return $this->error;
2076 $this->escape( $args );
2078 $blog_id = (int) $args[0];
2079 $username = $args[1];
2080 $password = $args[2];
2081 $filter = isset( $args[3] ) ? $args[3] : array();
2083 if ( isset( $args[4] ) )
2086 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2088 if ( ! $user = $this->login( $username, $password ) )
2089 return $this->error;
2091 do_action( 'xmlrpc_call', 'wp.getUsers' );
2093 if ( ! current_user_can( 'list_users' ) )
2094 return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
2096 $query = array( 'fields' => 'all_with_meta' );
2098 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2099 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2101 if ( isset( $filter['orderby'] ) ) {
2102 $query['orderby'] = $filter['orderby'];
2104 if ( isset( $filter['order'] ) )
2105 $query['order'] = $filter['order'];
2108 if ( isset( $filter['role'] ) ) {
2109 if ( get_role( $filter['role'] ) === null )
2110 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2112 $query['role'] = $filter['role'];
2115 if ( isset( $filter['who'] ) ) {
2116 $query['who'] = $filter['who'];
2119 $users = get_users( $query );
2122 foreach ( $users as $user_data ) {
2123 if ( current_user_can( 'edit_user', $user_data->ID ) )
2124 $_users[] = $this->_prepare_user( $user_data, $fields );
2130 * Retrieve information about the requesting user.
2132 * @uses get_userdata()
2133 * @param array $args Method parameters. Contains:
2135 * - string $username
2136 * - string $password
2137 * - array $fields optional
2138 * @return array (@see wp_getUser)
2140 function wp_getProfile( $args ) {
2141 if ( ! $this->minimum_args( $args, 3 ) )
2142 return $this->error;
2144 $this->escape( $args );
2146 $blog_id = (int) $args[0];
2147 $username = $args[1];
2148 $password = $args[2];
2150 if ( isset( $args[3] ) )
2153 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2155 if ( ! $user = $this->login( $username, $password ) )
2156 return $this->error;
2158 do_action( 'xmlrpc_call', 'wp.getProfile' );
2160 if ( ! current_user_can( 'edit_user', $user->ID ) )
2161 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2163 $user_data = get_userdata( $user->ID );
2165 return $this->_prepare_user( $user_data, $fields );
2169 * Edit user's profile.
2171 * @uses wp_update_user()
2172 * @param array $args Method parameters. Contains:
2174 * - string $username
2175 * - string $password
2176 * - array $content_struct
2177 * It can optionally contain:
2185 * @return bool True, on success.
2187 function wp_editProfile( $args ) {
2188 if ( ! $this->minimum_args( $args, 4 ) )
2189 return $this->error;
2191 $this->escape( $args );
2193 $blog_id = (int) $args[0];
2194 $username = $args[1];
2195 $password = $args[2];
2196 $content_struct = $args[3];
2198 if ( ! $user = $this->login( $username, $password ) )
2199 return $this->error;
2201 do_action( 'xmlrpc_call', 'wp.editProfile' );
2203 if ( ! current_user_can( 'edit_user', $user->ID ) )
2204 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2206 // holds data of the user
2207 $user_data = array();
2208 $user_data['ID'] = $user->ID;
2210 // only set the user details if it was given
2211 if ( isset( $content_struct['first_name'] ) )
2212 $user_data['first_name'] = $content_struct['first_name'];
2214 if ( isset( $content_struct['last_name'] ) )
2215 $user_data['last_name'] = $content_struct['last_name'];
2217 if ( isset( $content_struct['url'] ) )
2218 $user_data['user_url'] = $content_struct['url'];
2220 if ( isset( $content_struct['display_name'] ) )
2221 $user_data['display_name'] = $content_struct['display_name'];
2223 if ( isset( $content_struct['nickname'] ) )
2224 $user_data['nickname'] = $content_struct['nickname'];
2226 if ( isset( $content_struct['nicename'] ) )
2227 $user_data['user_nicename'] = $content_struct['nicename'];
2229 if ( isset( $content_struct['bio'] ) )
2230 $user_data['description'] = $content_struct['bio'];
2232 $result = wp_update_user( $user_data );
2234 if ( is_wp_error( $result ) )
2235 return new IXR_Error( 500, $result->get_error_message() );
2238 return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2248 * @param array $args Method parameters. Contains:
2255 function wp_getPage($args) {
2256 $this->escape($args);
2258 $blog_id = (int) $args[0];
2259 $page_id = (int) $args[1];
2260 $username = $args[2];
2261 $password = $args[3];
2263 if ( !$user = $this->login($username, $password) ) {
2264 return $this->error;
2267 $page = get_post($page_id);
2269 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2271 if ( !current_user_can( 'edit_page', $page_id ) )
2272 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2274 do_action('xmlrpc_call', 'wp.getPage');
2276 // If we found the page then format the data.
2277 if ( $page->ID && ($page->post_type == 'page') ) {
2278 return $this->_prepare_page( $page );
2280 // If the page doesn't exist indicate that.
2282 return(new IXR_Error(404, __('Sorry, no such page.')));
2291 * @param array $args Method parameters. Contains:
2298 function wp_getPages($args) {
2299 $this->escape($args);
2301 $blog_id = (int) $args[0];
2302 $username = $args[1];
2303 $password = $args[2];
2304 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
2306 if ( !$user = $this->login($username, $password) )
2307 return $this->error;
2309 if ( !current_user_can( 'edit_pages' ) )
2310 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2312 do_action('xmlrpc_call', 'wp.getPages');
2314 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2315 $num_pages = count($pages);
2317 // If we have pages, put together their info.
2318 if ( $num_pages >= 1 ) {
2319 $pages_struct = array();
2321 foreach ($pages as $page) {
2322 if ( current_user_can( 'edit_page', $page->ID ) )
2323 $pages_struct[] = $this->_prepare_page( $page );
2326 return($pages_struct);
2328 // If no pages were found return an error.
2339 * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
2342 function wp_newPage($args) {
2343 // Items not escaped here will be escaped in newPost.
2344 $username = $this->escape($args[1]);
2345 $password = $this->escape($args[2]);
2347 $publish = $args[4];
2349 if ( !$user = $this->login($username, $password) )
2350 return $this->error;
2352 do_action('xmlrpc_call', 'wp.newPage');
2354 // Mark this as content for a page.
2355 $args[3]["post_type"] = 'page';
2357 // Let mw_newPost do all of the heavy lifting.
2358 return($this->mw_newPost($args));
2366 * @param array $args Method parameters.
2367 * @return bool True, if success.
2369 function wp_deletePage($args) {
2370 $this->escape($args);
2372 $blog_id = (int) $args[0];
2373 $username = $args[1];
2374 $password = $args[2];
2375 $page_id = (int) $args[3];
2377 if ( !$user = $this->login($username, $password) )
2378 return $this->error;
2380 do_action('xmlrpc_call', 'wp.deletePage');
2382 // Get the current page based on the page_id and
2383 // make sure it is a page and not a post.
2384 $actual_page = get_post($page_id, ARRAY_A);
2385 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2386 return(new IXR_Error(404, __('Sorry, no such page.')));
2388 // Make sure the user can delete pages.
2389 if ( !current_user_can('delete_page', $page_id) )
2390 return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
2392 // Attempt to delete the page.
2393 $result = wp_delete_post($page_id);
2395 return(new IXR_Error(500, __('Failed to delete the page.')));
2397 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2407 * @param array $args Method parameters.
2410 function wp_editPage($args) {
2411 // Items not escaped here will be escaped in editPost.
2412 $blog_id = (int) $args[0];
2413 $page_id = (int) $this->escape($args[1]);
2414 $username = $this->escape($args[2]);
2415 $password = $this->escape($args[3]);
2416 $content = $args[4];
2417 $publish = $args[5];
2419 if ( !$user = $this->login($username, $password) )
2420 return $this->error;
2422 do_action('xmlrpc_call', 'wp.editPage');
2424 // Get the page data and make sure it is a page.
2425 $actual_page = get_post($page_id, ARRAY_A);
2426 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2427 return(new IXR_Error(404, __('Sorry, no such page.')));
2429 // Make sure the user is allowed to edit pages.
2430 if ( !current_user_can('edit_page', $page_id) )
2431 return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
2433 // Mark this as content for a page.
2434 $content['post_type'] = 'page';
2436 // Arrange args in the way mw_editPost understands.
2445 // Let mw_editPost do all of the heavy lifting.
2446 return($this->mw_editPost($args));
2450 * Retrieve page list.
2454 * @param array $args Method parameters.
2457 function wp_getPageList($args) {
2460 $this->escape($args);
2462 $blog_id = (int) $args[0];
2463 $username = $args[1];
2464 $password = $args[2];
2466 if ( !$user = $this->login($username, $password) )
2467 return $this->error;
2469 if ( !current_user_can( 'edit_pages' ) )
2470 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2472 do_action('xmlrpc_call', 'wp.getPageList');
2474 // Get list of pages ids and titles
2475 $page_list = $wpdb->get_results("
2477 post_title page_title,
2478 post_parent page_parent_id,
2483 WHERE post_type = 'page'
2487 // The date needs to be formatted properly.
2488 $num_pages = count($page_list);
2489 for ( $i = 0; $i < $num_pages; $i++ ) {
2490 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2491 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2493 unset($page_list[$i]->post_date_gmt);
2494 unset($page_list[$i]->post_date);
2495 unset($page_list[$i]->post_status);
2502 * Retrieve authors list.
2506 * @param array $args Method parameters.
2509 function wp_getAuthors($args) {
2511 $this->escape($args);
2513 $blog_id = (int) $args[0];
2514 $username = $args[1];
2515 $password = $args[2];
2517 if ( !$user = $this->login($username, $password) )
2518 return $this->error;
2520 if ( !current_user_can('edit_posts') )
2521 return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
2523 do_action('xmlrpc_call', 'wp.getAuthors');
2526 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2528 'user_id' => $user->ID,
2529 'user_login' => $user->user_login,
2530 'display_name' => $user->display_name
2538 * Get list of all tags
2542 * @param array $args Method parameters.
2545 function wp_getTags( $args ) {
2546 $this->escape( $args );
2548 $blog_id = (int) $args[0];
2549 $username = $args[1];
2550 $password = $args[2];
2552 if ( !$user = $this->login($username, $password) )
2553 return $this->error;
2555 if ( !current_user_can( 'edit_posts' ) )
2556 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2558 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2562 if ( $all_tags = get_tags() ) {
2563 foreach( (array) $all_tags as $tag ) {
2564 $struct['tag_id'] = $tag->term_id;
2565 $struct['name'] = $tag->name;
2566 $struct['count'] = $tag->count;
2567 $struct['slug'] = $tag->slug;
2568 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2569 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
2579 * Create new category.
2583 * @param array $args Method parameters.
2584 * @return int Category ID.
2586 function wp_newCategory($args) {
2587 $this->escape($args);
2589 $blog_id = (int) $args[0];
2590 $username = $args[1];
2591 $password = $args[2];
2592 $category = $args[3];
2594 if ( !$user = $this->login($username, $password) )
2595 return $this->error;
2597 do_action('xmlrpc_call', 'wp.newCategory');
2599 // Make sure the user is allowed to add a category.
2600 if ( !current_user_can('manage_categories') )
2601 return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
2603 // If no slug was provided make it empty so that
2604 // WordPress will generate one.
2605 if ( empty($category['slug']) )
2606 $category['slug'] = '';
2608 // If no parent_id was provided make it empty
2609 // so that it will be a top level page (no parent).
2610 if ( !isset($category['parent_id']) )
2611 $category['parent_id'] = '';
2613 // If no description was provided make it empty.
2614 if ( empty($category["description"]) )
2615 $category["description"] = "";
2617 $new_category = array(
2618 'cat_name' => $category['name'],
2619 'category_nicename' => $category['slug'],
2620 'category_parent' => $category['parent_id'],
2621 'category_description' => $category['description']
2624 $cat_id = wp_insert_category($new_category, true);
2625 if ( is_wp_error( $cat_id ) ) {
2626 if ( 'term_exists' == $cat_id->get_error_code() )
2627 return (int) $cat_id->get_error_data();
2629 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2630 } elseif ( ! $cat_id ) {
2631 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2634 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
2644 * @param array $args Method parameters.
2645 * @return mixed See {@link wp_delete_term()} for return info.
2647 function wp_deleteCategory($args) {
2648 $this->escape($args);
2650 $blog_id = (int) $args[0];
2651 $username = $args[1];
2652 $password = $args[2];
2653 $category_id = (int) $args[3];
2655 if ( !$user = $this->login($username, $password) )
2656 return $this->error;
2658 do_action('xmlrpc_call', 'wp.deleteCategory');
2660 if ( !current_user_can('manage_categories') )
2661 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
2663 $status = wp_delete_term( $category_id, 'category' );
2665 if( true == $status )
2666 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
2672 * Retrieve category list.
2676 * @param array $args Method parameters.
2679 function wp_suggestCategories($args) {
2680 $this->escape($args);
2682 $blog_id = (int) $args[0];
2683 $username = $args[1];
2684 $password = $args[2];
2685 $category = $args[3];
2686 $max_results = (int) $args[4];
2688 if ( !$user = $this->login($username, $password) )
2689 return $this->error;
2691 if ( !current_user_can( 'edit_posts' ) )
2692 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
2694 do_action('xmlrpc_call', 'wp.suggestCategories');
2696 $category_suggestions = array();
2697 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
2698 foreach ( (array) get_categories($args) as $cat ) {
2699 $category_suggestions[] = array(
2700 'category_id' => $cat->term_id,
2701 'category_name' => $cat->name
2705 return($category_suggestions);
2713 * @param array $args Method parameters.
2716 function wp_getComment($args) {
2717 $this->escape($args);
2719 $blog_id = (int) $args[0];
2720 $username = $args[1];
2721 $password = $args[2];
2722 $comment_id = (int) $args[3];
2724 if ( !$user = $this->login($username, $password) )
2725 return $this->error;
2727 if ( !current_user_can( 'moderate_comments' ) )
2728 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2730 do_action('xmlrpc_call', 'wp.getComment');
2732 if ( ! $comment = get_comment($comment_id) )
2733 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2735 return $this->_prepare_comment( $comment );
2739 * Retrieve comments.
2741 * Besides the common blog_id, username, and password arguments, it takes a filter
2742 * array as last argument.
2744 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
2746 * The defaults are as follows:
2747 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
2748 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
2749 * - 'number' - Default is 10. Total number of media items to retrieve.
2750 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2754 * @param array $args Method parameters.
2755 * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
2757 function wp_getComments($args) {
2758 $this->escape($args);
2760 $blog_id = (int) $args[0];
2761 $username = $args[1];
2762 $password = $args[2];
2763 $struct = isset( $args[3] ) ? $args[3] : array();
2765 if ( !$user = $this->login($username, $password) )
2766 return $this->error;
2768 if ( !current_user_can( 'moderate_comments' ) )
2769 return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
2771 do_action('xmlrpc_call', 'wp.getComments');
2773 if ( isset($struct['status']) )
2774 $status = $struct['status'];
2779 if ( isset($struct['post_id']) )
2780 $post_id = absint($struct['post_id']);
2783 if ( isset($struct['offset']) )
2784 $offset = absint($struct['offset']);
2787 if ( isset($struct['number']) )
2788 $number = absint($struct['number']);
2790 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
2792 $comments_struct = array();
2794 foreach ( $comments as $comment ) {
2795 $comments_struct[] = $this->_prepare_comment( $comment );
2798 return $comments_struct;
2804 * By default, the comment will be moved to the trash instead of deleted.
2805 * See {@link wp_delete_comment()} for more information on
2810 * @param array $args Method parameters. Contains:
2815 * @return mixed {@link wp_delete_comment()}
2817 function wp_deleteComment($args) {
2818 $this->escape($args);
2820 $blog_id = (int) $args[0];
2821 $username = $args[1];
2822 $password = $args[2];
2823 $comment_ID = (int) $args[3];
2825 if ( !$user = $this->login($username, $password) )
2826 return $this->error;
2828 if ( !current_user_can( 'moderate_comments' ) )
2829 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2831 if ( ! get_comment($comment_ID) )
2832 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2834 if ( !current_user_can( 'edit_comment', $comment_ID ) )
2835 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2837 do_action('xmlrpc_call', 'wp.deleteComment');
2839 $status = wp_delete_comment( $comment_ID );
2841 if( true == $status )
2842 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
2850 * Besides the common blog_id, username, and password arguments, it takes a
2851 * comment_id integer and a content_struct array as last argument.
2853 * The allowed keys in the content_struct array are:
2858 * - 'date_created_gmt'
2859 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
2863 * @param array $args. Contains:
2869 * @return bool True, on success.
2871 function wp_editComment($args) {
2872 $this->escape($args);
2874 $blog_id = (int) $args[0];
2875 $username = $args[1];
2876 $password = $args[2];
2877 $comment_ID = (int) $args[3];
2878 $content_struct = $args[4];
2880 if ( !$user = $this->login($username, $password) )
2881 return $this->error;
2883 if ( !current_user_can( 'moderate_comments' ) )
2884 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2886 if ( ! get_comment($comment_ID) )
2887 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2889 if ( !current_user_can( 'edit_comment', $comment_ID ) )
2890 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2892 do_action('xmlrpc_call', 'wp.editComment');
2894 if ( isset($content_struct['status']) ) {
2895 $statuses = get_comment_statuses();
2896 $statuses = array_keys($statuses);
2898 if ( ! in_array($content_struct['status'], $statuses) )
2899 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
2900 $comment_approved = $content_struct['status'];
2903 // Do some timestamp voodoo
2904 if ( !empty( $content_struct['date_created_gmt'] ) ) {
2905 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
2906 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
2907 $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
2908 $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
2911 if ( isset($content_struct['content']) )
2912 $comment_content = $content_struct['content'];
2914 if ( isset($content_struct['author']) )
2915 $comment_author = $content_struct['author'];
2917 if ( isset($content_struct['author_url']) )
2918 $comment_author_url = $content_struct['author_url'];
2920 if ( isset($content_struct['author_email']) )
2921 $comment_author_email = $content_struct['author_email'];
2923 // We've got all the data -- post it:
2924 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
2926 $result = wp_update_comment($comment);
2927 if ( is_wp_error( $result ) )
2928 return new IXR_Error(500, $result->get_error_message());
2931 return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
2933 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
2939 * Create new comment.
2943 * @param array $args Method parameters.
2944 * @return mixed {@link wp_new_comment()}
2946 function wp_newComment($args) {
2949 $this->escape($args);
2951 $blog_id = (int) $args[0];
2952 $username = $args[1];
2953 $password = $args[2];
2955 $content_struct = $args[4];
2957 $allow_anon = apply_filters('xmlrpc_allow_anonymous_comments', false);
2959 $user = $this->login($username, $password);
2963 if ( $allow_anon && get_option('comment_registration') )
2964 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
2965 else if ( !$allow_anon )
2966 return $this->error;
2971 if ( is_numeric($post) )
2972 $post_id = absint($post);
2974 $post_id = url_to_postid($post);
2977 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2979 if ( ! get_post($post_id) )
2980 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2982 $comment['comment_post_ID'] = $post_id;
2985 $comment['comment_author'] = $this->escape( $user->display_name );
2986 $comment['comment_author_email'] = $this->escape( $user->user_email );
2987 $comment['comment_author_url'] = $this->escape( $user->user_url );
2988 $comment['user_ID'] = $user->ID;
2990 $comment['comment_author'] = '';
2991 if ( isset($content_struct['author']) )
2992 $comment['comment_author'] = $content_struct['author'];
2994 $comment['comment_author_email'] = '';
2995 if ( isset($content_struct['author_email']) )
2996 $comment['comment_author_email'] = $content_struct['author_email'];
2998 $comment['comment_author_url'] = '';
2999 if ( isset($content_struct['author_url']) )
3000 $comment['comment_author_url'] = $content_struct['author_url'];
3002 $comment['user_ID'] = 0;
3004 if ( get_option('require_name_email') ) {
3005 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
3006 return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
3007 elseif ( !is_email($comment['comment_author_email']) )
3008 return new IXR_Error( 403, __( 'A valid email address is required' ) );
3012 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
3014 $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
3016 do_action('xmlrpc_call', 'wp.newComment');
3018 $comment_ID = wp_new_comment( $comment );
3020 do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
3026 * Retrieve all of the comment status.
3030 * @param array $args Method parameters.
3033 function wp_getCommentStatusList($args) {
3034 $this->escape( $args );
3036 $blog_id = (int) $args[0];
3037 $username = $args[1];
3038 $password = $args[2];
3040 if ( !$user = $this->login($username, $password) )
3041 return $this->error;
3043 if ( !current_user_can( 'moderate_comments' ) )
3044 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3046 do_action('xmlrpc_call', 'wp.getCommentStatusList');
3048 return get_comment_statuses();
3052 * Retrieve comment count.
3056 * @param array $args Method parameters.
3059 function wp_getCommentCount( $args ) {
3060 $this->escape($args);
3062 $blog_id = (int) $args[0];
3063 $username = $args[1];
3064 $password = $args[2];
3065 $post_id = (int) $args[3];
3067 if ( !$user = $this->login($username, $password) )
3068 return $this->error;
3070 if ( !current_user_can( 'edit_posts' ) )
3071 return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
3073 do_action('xmlrpc_call', 'wp.getCommentCount');
3075 $count = wp_count_comments( $post_id );
3077 'approved' => $count->approved,
3078 'awaiting_moderation' => $count->moderated,
3079 'spam' => $count->spam,
3080 'total_comments' => $count->total_comments
3085 * Retrieve post statuses.
3089 * @param array $args Method parameters.
3092 function wp_getPostStatusList( $args ) {
3093 $this->escape( $args );
3095 $blog_id = (int) $args[0];
3096 $username = $args[1];
3097 $password = $args[2];
3099 if ( !$user = $this->login($username, $password) )
3100 return $this->error;
3102 if ( !current_user_can( 'edit_posts' ) )
3103 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3105 do_action('xmlrpc_call', 'wp.getPostStatusList');
3107 return get_post_statuses();
3111 * Retrieve page statuses.
3115 * @param array $args Method parameters.
3118 function wp_getPageStatusList( $args ) {
3119 $this->escape( $args );
3121 $blog_id = (int) $args[0];
3122 $username = $args[1];
3123 $password = $args[2];
3125 if ( !$user = $this->login($username, $password) )
3126 return $this->error;
3128 if ( !current_user_can( 'edit_pages' ) )
3129 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3131 do_action('xmlrpc_call', 'wp.getPageStatusList');
3133 return get_page_statuses();
3137 * Retrieve page templates.
3141 * @param array $args Method parameters.
3144 function wp_getPageTemplates( $args ) {
3145 $this->escape( $args );
3147 $blog_id = (int) $args[0];
3148 $username = $args[1];
3149 $password = $args[2];
3151 if ( !$user = $this->login($username, $password) )
3152 return $this->error;
3154 if ( !current_user_can( 'edit_pages' ) )
3155 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3157 $templates = get_page_templates();
3158 $templates['Default'] = 'default';
3164 * Retrieve blog options.
3168 * @param array $args Method parameters.
3171 function wp_getOptions( $args ) {
3172 $this->escape( $args );
3174 $blog_id = (int) $args[