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 * Since WordPress 2.6.0, WordPress XMLRPC server can be disabled in the
16 * administration panels.
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.getPage' => 'this:wp_getPage',
52 'wp.getPages' => 'this:wp_getPages',
53 'wp.newPage' => 'this:wp_newPage',
54 'wp.deletePage' => 'this:wp_deletePage',
55 'wp.editPage' => 'this:wp_editPage',
56 'wp.getPageList' => 'this:wp_getPageList',
57 'wp.getAuthors' => 'this:wp_getAuthors',
58 'wp.getCategories' => 'this:mw_getCategories', // Alias
59 'wp.getTags' => 'this:wp_getTags',
60 'wp.newCategory' => 'this:wp_newCategory',
61 'wp.deleteCategory' => 'this:wp_deleteCategory',
62 'wp.suggestCategories' => 'this:wp_suggestCategories',
63 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
64 'wp.getCommentCount' => 'this:wp_getCommentCount',
65 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
66 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
67 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
68 'wp.getOptions' => 'this:wp_getOptions',
69 'wp.setOptions' => 'this:wp_setOptions',
70 'wp.getComment' => 'this:wp_getComment',
71 'wp.getComments' => 'this:wp_getComments',
72 'wp.deleteComment' => 'this:wp_deleteComment',
73 'wp.editComment' => 'this:wp_editComment',
74 'wp.newComment' => 'this:wp_newComment',
75 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
76 'wp.getMediaItem' => 'this:wp_getMediaItem',
77 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
78 'wp.getPostFormats' => 'this:wp_getPostFormats',
79 'wp.getPostType' => 'this:wp_getPostType',
80 'wp.getPostTypes' => 'this:wp_getPostTypes',
83 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
84 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
85 'blogger.getPost' => 'this:blogger_getPost',
86 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
87 'blogger.getTemplate' => 'this:blogger_getTemplate',
88 'blogger.setTemplate' => 'this:blogger_setTemplate',
89 'blogger.newPost' => 'this:blogger_newPost',
90 'blogger.editPost' => 'this:blogger_editPost',
91 'blogger.deletePost' => 'this:blogger_deletePost',
93 // MetaWeblog API (with MT extensions to structs)
94 'metaWeblog.newPost' => 'this:mw_newPost',
95 'metaWeblog.editPost' => 'this:mw_editPost',
96 'metaWeblog.getPost' => 'this:mw_getPost',
97 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
98 'metaWeblog.getCategories' => 'this:mw_getCategories',
99 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
101 // MetaWeblog API aliases for Blogger API
102 // see http://www.xmlrpc.com/stories/storyReader$2460
103 'metaWeblog.deletePost' => 'this:blogger_deletePost',
104 'metaWeblog.getTemplate' => 'this:blogger_getTemplate',
105 'metaWeblog.setTemplate' => 'this:blogger_setTemplate',
106 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
109 'mt.getCategoryList' => 'this:mt_getCategoryList',
110 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
111 'mt.getPostCategories' => 'this:mt_getPostCategories',
112 'mt.setPostCategories' => 'this:mt_setPostCategories',
113 'mt.supportedMethods' => 'this:mt_supportedMethods',
114 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
115 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
116 'mt.publishPost' => 'this:mt_publishPost',
119 'pingback.ping' => 'this:pingback_ping',
120 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
122 'demo.sayHello' => 'this:sayHello',
123 'demo.addTwoNumbers' => 'this:addTwoNumbers'
126 $this->initialise_blog_option_info();
127 $this->methods = apply_filters('xmlrpc_methods', $this->methods);
130 function serve_request() {
131 $this->IXR_Server($this->methods);
135 * Test XMLRPC API by saying, "Hello!" to client.
139 * @param array $args Method Parameters.
142 function sayHello($args) {
147 * Test XMLRPC API by adding two numbers for client.
151 * @param array $args Method Parameters.
154 function addTwoNumbers($args) {
157 return $number1 + $number2;
161 * Check user's credentials.
165 * @param string $user_login User's username.
166 * @param string $user_pass User's password.
167 * @return bool Whether authentication passed.
168 * @deprecated use wp_xmlrpc_server::login
169 * @see wp_xmlrpc_server::login
171 function login_pass_ok($user_login, $user_pass) {
172 if ( !get_option( 'enable_xmlrpc' ) ) {
173 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'), admin_url('options-writing.php') ) );
177 if (!user_pass_ok($user_login, $user_pass)) {
178 $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
189 * @param string $username User's username.
190 * @param string $password User's password.
191 * @return mixed WP_User object if authentication passed, false otherwise
193 function login($username, $password) {
194 if ( !get_option( 'enable_xmlrpc' ) ) {
195 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'), admin_url('options-writing.php') ) );
199 $user = wp_authenticate($username, $password);
201 if (is_wp_error($user)) {
202 $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
206 wp_set_current_user( $user->ID );
211 * Sanitize string or array of strings for database.
215 * @param string|array $array Sanitize single string or array of strings.
216 * @return string|array Type matches $array and sanitized for the database.
218 function escape(&$array) {
221 if (!is_array($array)) {
222 return($wpdb->escape($array));
224 foreach ( (array) $array as $k => $v ) {
225 if ( is_array($v) ) {
226 $this->escape($array[$k]);
227 } else if ( is_object($v) ) {
230 $array[$k] = $wpdb->escape($v);
237 * Retrieve custom fields for post.
241 * @param int $post_id Post ID.
242 * @return array Custom fields, if exist.
244 function get_custom_fields($post_id) {
245 $post_id = (int) $post_id;
247 $custom_fields = array();
249 foreach ( (array) has_meta($post_id) as $meta ) {
250 // Don't expose protected fields.
251 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
254 $custom_fields[] = array(
255 "id" => $meta['meta_id'],
256 "key" => $meta['meta_key'],
257 "value" => $meta['meta_value']
261 return $custom_fields;
265 * Set custom fields for post.
269 * @param int $post_id Post ID.
270 * @param array $fields Custom fields.
272 function set_custom_fields($post_id, $fields) {
273 $post_id = (int) $post_id;
275 foreach ( (array) $fields as $meta ) {
276 if ( isset($meta['id']) ) {
277 $meta['id'] = (int) $meta['id'];
278 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
279 if ( isset($meta['key']) ) {
280 $meta['key'] = stripslashes( $meta['key'] );
281 if ( $meta['key'] != $pmeta->meta_key )
283 $meta['value'] = stripslashes_deep( $meta['value'] );
284 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
285 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
286 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
287 delete_metadata_by_mid( 'post', $meta['id'] );
289 } elseif ( current_user_can( 'add_post_meta', $post_id, stripslashes( $meta['key'] ) ) ) {
290 add_post_meta( $post_id, $meta['key'], $meta['value'] );
296 * Set up blog options property.
298 * Passes property through 'xmlrpc_blog_options' filter.
302 function initialise_blog_option_info() {
305 $this->blog_options = array(
307 'software_name' => array(
308 'desc' => __( 'Software Name' ),
310 'value' => 'WordPress'
312 'software_version' => array(
313 'desc' => __( 'Software Version' ),
315 'value' => $wp_version
318 'desc' => __( 'Site URL' ),
320 'option' => 'siteurl'
322 'image_default_link_type' => array(
323 'desc' => __( 'Image default link type' ),
325 'option' => 'image_default_link_type'
327 'image_default_size' => array(
328 'desc' => __( 'Image default size' ),
330 'option' => 'image_default_size'
332 'image_default_align' => array(
333 'desc' => __( 'Image default align' ),
335 'option' => 'image_default_align'
338 'desc' => __( 'Template' ),
340 'option' => 'template'
342 'stylesheet' => array(
343 'desc' => __( 'Stylesheet' ),
345 'option' => 'stylesheet'
347 'post_thumbnail' => array(
348 'desc' => __('Post Thumbnail'),
350 'value' => current_theme_supports( 'post-thumbnails' )
354 'time_zone' => array(
355 'desc' => __( 'Time Zone' ),
357 'option' => 'gmt_offset'
359 'blog_title' => array(
360 'desc' => __( 'Site Title' ),
362 'option' => 'blogname'
364 'blog_tagline' => array(
365 'desc' => __( 'Site Tagline' ),
367 'option' => 'blogdescription'
369 'date_format' => array(
370 'desc' => __( 'Date Format' ),
372 'option' => 'date_format'
374 'time_format' => array(
375 'desc' => __( 'Time Format' ),
377 'option' => 'time_format'
379 'users_can_register' => array(
380 'desc' => __( 'Allow new users to sign up' ),
382 'option' => 'users_can_register'
384 'thumbnail_size_w' => array(
385 'desc' => __( 'Thumbnail Width' ),
387 'option' => 'thumbnail_size_w'
389 'thumbnail_size_h' => array(
390 'desc' => __( 'Thumbnail Height' ),
392 'option' => 'thumbnail_size_h'
394 'thumbnail_crop' => array(
395 'desc' => __( 'Crop thumbnail to exact dimensions' ),
397 'option' => 'thumbnail_crop'
399 'medium_size_w' => array(
400 'desc' => __( 'Medium size image width' ),
402 'option' => 'medium_size_w'
404 'medium_size_h' => array(
405 'desc' => __( 'Medium size image height' ),
407 'option' => 'medium_size_h'
409 'large_size_w' => array(
410 'desc' => __( 'Large size image width' ),
412 'option' => 'large_size_w'
414 'large_size_h' => array(
415 'desc' => __( 'Large size image height' ),
417 'option' => 'large_size_h'
419 'default_comment_status' => array(
420 'desc' => __( 'Allow people to post comments on new articles' ),
422 'option' => 'default_comment_status'
424 'default_ping_status' => array(
425 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
427 'option' => 'default_ping_status'
431 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
435 * Retrieve the blogs of the user.
439 * @param array $args Method parameters. Contains:
442 * @return array. Contains:
447 * - 'xmlrpc' - url of xmlrpc endpoint
449 function wp_getUsersBlogs( $args ) {
450 global $current_site;
451 // If this isn't on WPMU then just use blogger_getUsersBlogs
452 if ( !is_multisite() ) {
453 array_unshift( $args, 1 );
454 return $this->blogger_getUsersBlogs( $args );
457 $this->escape( $args );
459 $username = $args[0];
460 $password = $args[1];
462 if ( !$user = $this->login($username, $password) )
465 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
467 $blogs = (array) get_blogs_of_user( $user->ID );
470 foreach ( $blogs as $blog ) {
471 // Don't include blogs that aren't hosted at this site
472 if ( $blog->site_id != $current_site->id )
475 $blog_id = $blog->userblog_id;
476 switch_to_blog($blog_id);
477 $is_admin = current_user_can('manage_options');
480 'isAdmin' => $is_admin,
481 'url' => get_option( 'home' ) . '/',
482 'blogid' => (string) $blog_id,
483 'blogName' => get_option( 'blogname' ),
484 'xmlrpc' => site_url( 'xmlrpc.php' )
487 restore_current_blog();
494 * Checks if the method received at least the minimum number of arguments.
498 * @param string|array $args Sanitize single string or array of strings.
499 * @param int $count Minimum number of arguments.
500 * @return boolean if $args contains at least $count arguments.
502 protected function minimum_args( $args, $count ) {
503 if ( count( $args ) < $count ) {
504 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
512 * Prepares taxonomy data for return in an XML-RPC object.
516 * @param object $taxonomy The unprepared taxonomy data
517 * @param array $fields The subset of taxonomy fields to return
518 * @return array The prepared taxonomy data
520 protected function _prepare_taxonomy( $taxonomy, $fields ) {
522 'name' => $taxonomy->name,
523 'label' => $taxonomy->label,
524 'hierarchical' => (bool) $taxonomy->hierarchical,
525 'public' => (bool) $taxonomy->public,
526 'show_ui' => (bool) $taxonomy->show_ui,
527 '_builtin' => (bool) $taxonomy->_builtin,
530 if ( in_array( 'labels', $fields ) )
531 $_taxonomy['labels'] = (array) $taxonomy->labels;
533 if ( in_array( 'cap', $fields ) )
534 $_taxonomy['cap'] = (array) $taxonomy->cap;
536 if ( in_array( 'object_type', $fields ) )
537 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
539 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
543 * Prepares term data for return in an XML-RPC object.
547 * @param array|object $term The unprepared term data
548 * @return array The prepared term data
550 protected function _prepare_term( $term ) {
552 if ( ! is_array( $_term) )
553 $_term = get_object_vars( $_term );
555 // For Intergers which may be largeer than XMLRPC supports ensure we return strings.
556 $_term['term_id'] = strval( $_term['term_id'] );
557 $_term['term_group'] = strval( $_term['term_group'] );
558 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
559 $_term['parent'] = strval( $_term['parent'] );
561 // Count we are happy to return as an Integer because people really shouldn't use Terms that much.
562 $_term['count'] = intval( $_term['count'] );
564 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
568 * Convert a WordPress date string to an IXR_Date object.
572 * @param string $date
575 protected function _convert_date( $date ) {
576 if ( $date === '0000-00-00 00:00:00' ) {
577 return new IXR_Date( '00000000T00:00:00Z' );
579 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
583 * Convert a WordPress GMT date string to an IXR_Date object.
587 * @param string $date_gmt
588 * @param string $date
591 protected function _convert_date_gmt( $date_gmt, $date ) {
592 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
593 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
595 return $this->_convert_date( $date_gmt );
599 * Prepares post data for return in an XML-RPC object.
603 * @param array $post The unprepared post data
604 * @param array $fields The subset of post type fields to return
605 * @return array The prepared post data
607 protected function _prepare_post( $post, $fields ) {
608 // holds the data for this post. built up based on $fields
609 $_post = array( 'post_id' => strval( $post['ID'] ) );
611 // prepare common post fields
612 $post_fields = array(
613 'post_title' => $post['post_title'],
614 'post_date' => $this->_convert_date( $post['post_date'] ),
615 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
616 'post_modified' => $this->_convert_date( $post['post_modified'] ),
617 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
618 'post_status' => $post['post_status'],
619 'post_type' => $post['post_type'],
620 'post_name' => $post['post_name'],
621 'post_author' => $post['post_author'],
622 'post_password' => $post['post_password'],
623 'post_excerpt' => $post['post_excerpt'],
624 'post_content' => $post['post_content'],
625 'post_parent' => strval( $post['post_parent'] ),
626 'post_mime_type' => $post['post_mime_type'],
627 'link' => post_permalink( $post['ID'] ),
628 'guid' => $post['guid'],
629 'menu_order' => intval( $post['menu_order'] ),
630 'comment_status' => $post['comment_status'],
631 'ping_status' => $post['ping_status'],
632 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
636 $post_fields['post_thumbnail'] = array();
637 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
638 if ( $thumbnail_id ) {
639 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
640 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
643 // Consider future posts as published
644 if ( $post_fields['post_status'] === 'future' )
645 $post_fields['post_status'] = 'publish';
647 // Fill in blank post format
648 $post_fields['post_format'] = get_post_format( $post['ID'] );
649 if ( empty( $post_fields['post_format'] ) )
650 $post_fields['post_format'] = 'standard';
652 // Merge requested $post_fields fields into $_post
653 if ( in_array( 'post', $fields ) ) {
654 $_post = array_merge( $_post, $post_fields );
656 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
657 $_post = array_merge( $_post, $requested_fields );
660 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
662 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
663 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
664 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
665 $_post['terms'] = array();
666 foreach ( $terms as $term ) {
667 $_post['terms'][] = $this->_prepare_term( $term );
671 if ( in_array( 'custom_fields', $fields ) )
672 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
674 if ( in_array( 'enclosure', $fields ) ) {
675 $_post['enclosure'] = array();
676 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
677 if ( ! empty( $enclosures ) ) {
678 $encdata = explode( "\n", $enclosures[0] );
679 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
680 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
681 $_post['enclosure']['type'] = trim( $encdata[2] );
685 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
689 * Prepares post data for return in an XML-RPC object.
693 * @param object $post_type Post type object
694 * @param array $fields The subset of post fields to return
695 * @return array The prepared post type data
697 protected function _prepare_post_type( $post_type, $fields ) {
699 'name' => $post_type->name,
700 'label' => $post_type->label,
701 'hierarchical' => (bool) $post_type->hierarchical,
702 'public' => (bool) $post_type->public,
703 'show_ui' => (bool) $post_type->show_ui,
704 '_builtin' => (bool) $post_type->_builtin,
705 'has_archive' => (bool) $post_type->has_archive,
706 'supports' => get_all_post_type_supports( $post_type->name ),
709 if ( in_array( 'labels', $fields ) ) {
710 $_post_type['labels'] = (array) $post_type->labels;
713 if ( in_array( 'cap', $fields ) ) {
714 $_post_type['cap'] = (array) $post_type->cap;
715 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
718 if ( in_array( 'menu', $fields ) ) {
719 $_post_type['menu_position'] = (int) $post_type->menu_position;
720 $_post_type['menu_icon'] = $post_type->menu_icon;
721 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
724 if ( in_array( 'taxonomies', $fields ) )
725 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
727 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
731 * Prepares media item data for return in an XML-RPC object.
735 * @param object $media_item The unprepared media item data
736 * @param string $thumbnail_size The image size to use for the thumbnail URL
737 * @return array The prepared media item data
739 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
740 $_media_item = array(
741 'attachment_id' => strval( $media_item->ID ),
742 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
743 'parent' => $media_item->post_parent,
744 'link' => wp_get_attachment_url( $media_item->ID ),
745 'title' => $media_item->post_title,
746 'caption' => $media_item->post_excerpt,
747 'description' => $media_item->post_content,
748 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
751 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
752 if ( $thumbnail_src )
753 $_media_item['thumbnail'] = $thumbnail_src[0];
755 $_media_item['thumbnail'] = $_media_item['link'];
757 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
761 * Prepares page data for return in an XML-RPC object.
765 * @param object $page The unprepared page data
766 * @return array The prepared page data
768 protected function _prepare_page( $page ) {
769 // Get all of the page content and link.
770 $full_page = get_extended( $page->post_content );
771 $link = post_permalink( $page->ID );
773 // Get info the page parent if there is one.
775 if ( ! empty( $page->post_parent ) ) {
776 $parent = get_page( $page->post_parent );
777 $parent_title = $parent->post_title;
780 // Determine comment and ping settings.
781 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
782 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
785 $page_date = $this->_convert_date( $page->post_date );
786 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
788 // Pull the categories info together.
789 $categories = array();
790 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
791 $categories[] = get_cat_name( $cat_id );
794 // Get the author info.
795 $author = get_userdata( $page->post_author );
797 $page_template = get_page_template_slug( $page->ID );
798 if ( empty( $page_template ) )
799 $page_template = 'default';
802 'dateCreated' => $page_date,
803 'userid' => $page->post_author,
804 'page_id' => $page->ID,
805 'page_status' => $page->post_status,
806 'description' => $full_page['main'],
807 'title' => $page->post_title,
809 'permaLink' => $link,
810 'categories' => $categories,
811 'excerpt' => $page->post_excerpt,
812 'text_more' => $full_page['extended'],
813 'mt_allow_comments' => $allow_comments,
814 'mt_allow_pings' => $allow_pings,
815 'wp_slug' => $page->post_name,
816 'wp_password' => $page->post_password,
817 'wp_author' => $author->display_name,
818 'wp_page_parent_id' => $page->post_parent,
819 'wp_page_parent_title' => $parent_title,
820 'wp_page_order' => $page->menu_order,
821 'wp_author_id' => (string) $author->ID,
822 'wp_author_display_name' => $author->display_name,
823 'date_created_gmt' => $page_date_gmt,
824 'custom_fields' => $this->get_custom_fields( $page->ID ),
825 'wp_page_template' => $page_template
828 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
832 * Prepares comment data for return in an XML-RPC object.
836 * @param object $comment The unprepared comment data
837 * @return array The prepared comment data
839 protected function _prepare_comment( $comment ) {
841 $comment_date = $this->_convert_date( $comment->comment_date );
842 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
844 if ( '0' == $comment->comment_approved )
845 $comment_status = 'hold';
846 else if ( 'spam' == $comment->comment_approved )
847 $comment_status = 'spam';
848 else if ( '1' == $comment->comment_approved )
849 $comment_status = 'approve';
851 $comment_status = $comment->comment_approved;
854 'date_created_gmt' => $comment_date_gmt,
855 'user_id' => $comment->user_id,
856 'comment_id' => $comment->comment_ID,
857 'parent' => $comment->comment_parent,
858 'status' => $comment_status,
859 'content' => $comment->comment_content,
860 'link' => get_comment_link($comment),
861 'post_id' => $comment->comment_post_ID,
862 'post_title' => get_the_title($comment->comment_post_ID),
863 'author' => $comment->comment_author,
864 'author_url' => $comment->comment_author_url,
865 'author_email' => $comment->comment_author_email,
866 'author_ip' => $comment->comment_author_IP,
867 'type' => $comment->comment_type,
870 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
874 * Create a new post for any registered post type.
878 * @param array $args Method parameters. Contains:
882 * - array $content_struct
883 * $content_struct can contain:
884 * - post_type (default: 'post')
885 * - post_status (default: 'draft')
890 * - post_date_gmt | post_date
893 * - comment_status - can be 'open' | 'closed'
894 * - ping_status - can be 'open' | 'closed'
896 * - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
897 * - custom_fields - array, with each element containing 'key' and 'value'
898 * - terms - array, with taxonomy names as keys and arrays of term IDs as values
899 * - terms_names - array, with taxonomy names as keys and arrays of term names as values
901 * - any other fields supported by wp_insert_post()
902 * @return string post_id
904 function wp_newPost( $args ) {
905 if ( ! $this->minimum_args( $args, 4 ) )
908 $this->escape( $args );
910 $blog_id = (int) $args[0];
911 $username = $args[1];
912 $password = $args[2];
913 $content_struct = $args[3];
915 if ( ! $user = $this->login( $username, $password ) )
918 do_action( 'xmlrpc_call', 'wp.newPost' );
920 unset( $content_struct['ID'] );
922 return $this->_insert_post( $user, $content_struct );
926 * Helper method for filtering out elements from an array.
930 * @param int $count Number to compare to one.
932 private function _is_greater_than_one( $count ) {
937 * Helper method for wp_newPost and wp_editPost, containing shared logic.
940 * @uses wp_insert_post()
942 * @param WP_User $user The post author if post_author isn't set in $content_struct.
943 * @param array $content_struct Post data to insert.
945 protected function _insert_post( $user, $content_struct ) {
946 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
947 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
949 $post_data = wp_parse_args( $content_struct, $defaults );
951 $post_type = get_post_type_object( $post_data['post_type'] );
953 return new IXR_Error( 403, __( 'Invalid post type' ) );
955 $update = ! empty( $post_data['ID'] );
958 if ( ! get_post( $post_data['ID'] ) )
959 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
960 if ( ! current_user_can( $post_type->cap->edit_post, $post_data['ID'] ) )
961 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
962 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
963 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
965 if ( ! current_user_can( $post_type->cap->edit_posts ) )
966 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
969 switch ( $post_data['post_status'] ) {
974 if ( ! current_user_can( $post_type->cap->publish_posts ) )
975 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
979 if ( ! current_user_can( $post_type->cap->publish_posts ) )
980 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
983 $post_data['post_status'] = 'draft';
987 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
988 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
990 $post_data['post_author'] = absint( $post_data['post_author'] );
991 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
992 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
993 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
995 $author = get_userdata( $post_data['post_author'] );
998 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1000 $post_data['post_author'] = $user->ID;
1003 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1004 unset( $post_data['comment_status'] );
1006 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1007 unset( $post_data['ping_status'] );
1009 // Do some timestamp voodoo
1010 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1011 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1012 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1013 } elseif ( ! empty( $post_data['post_date'] ) ) {
1014 $dateCreated = $post_data['post_date']->getIso();
1017 if ( ! empty( $dateCreated ) ) {
1018 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1019 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1022 if ( ! isset( $post_data['ID'] ) )
1023 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1024 $post_ID = $post_data['ID'];
1026 if ( $post_data['post_type'] == 'post' ) {
1027 // Private and password-protected posts cannot be stickied.
1028 if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
1029 // Error if the client tried to stick the post, otherwise, silently unstick.
1030 if ( ! empty( $post_data['sticky'] ) )
1031 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1033 unstick_post( $post_ID );
1034 } elseif ( isset( $post_data['sticky'] ) ) {
1035 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1036 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1037 if ( $post_data['sticky'] )
1038 stick_post( $post_ID );
1040 unstick_post( $post_ID );
1044 if ( isset( $post_data['post_thumbnail'] ) ) {
1045 // empty value deletes, non-empty value adds/updates
1046 if ( ! $post_data['post_thumbnail'] )
1047 delete_post_thumbnail( $post_ID );
1048 elseif ( ! set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] ) )
1049 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1050 unset( $content_struct['post_thumbnail'] );
1053 if ( isset( $post_data['custom_fields'] ) )
1054 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1056 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1057 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1059 // accumulate term IDs from terms and terms_names
1062 // first validate the terms specified by ID
1063 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1064 $taxonomies = array_keys( $post_data['terms'] );
1066 // validating term ids
1067 foreach ( $taxonomies as $taxonomy ) {
1068 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1069 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1071 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1072 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1074 $term_ids = $post_data['terms'][$taxonomy];
1075 foreach ( $term_ids as $term_id ) {
1076 $term = get_term_by( 'id', $term_id, $taxonomy );
1079 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1081 $terms[$taxonomy][] = (int) $term_id;
1086 // now validate terms specified by name
1087 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1088 $taxonomies = array_keys( $post_data['terms_names'] );
1090 foreach ( $taxonomies as $taxonomy ) {
1091 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1092 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1094 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1095 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1097 // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
1098 $ambiguous_terms = array();
1099 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1100 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1102 // count the number of terms with the same name
1103 $tax_term_names_count = array_count_values( $tax_term_names );
1105 // filter out non-ambiguous term names
1106 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1108 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1111 $term_names = $post_data['terms_names'][$taxonomy];
1112 foreach ( $term_names as $term_name ) {
1113 if ( in_array( $term_name, $ambiguous_terms ) )
1114 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1116 $term = get_term_by( 'name', $term_name, $taxonomy );
1119 // term doesn't exist, so check that the user is allowed to create new terms
1120 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1121 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1123 // create the new term
1124 $term_info = wp_insert_term( $term_name, $taxonomy );
1125 if ( is_wp_error( $term_info ) )
1126 return new IXR_Error( 500, $term_info->get_error_message() );
1128 $terms[$taxonomy][] = (int) $term_info['term_id'];
1130 $terms[$taxonomy][] = (int) $term->term_id;
1136 $post_data['tax_input'] = $terms;
1137 unset( $post_data['terms'], $post_data['terms_names'] );
1139 // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
1140 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1143 if ( isset( $post_data['post_format'] ) ) {
1144 $format = set_post_format( $post_ID, $post_data['post_format'] );
1146 if ( is_wp_error( $format ) )
1147 return new IXR_Error( 500, $format->get_error_message() );
1149 unset( $post_data['post_format'] );
1152 // Handle enclosures
1153 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1154 $this->add_enclosure_if_new( $post_ID, $enclosure );
1156 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1158 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1160 $post_ID = wp_insert_post( $post_data, true );
1161 if ( is_wp_error( $post_ID ) )
1162 return new IXR_Error( 500, $post_ID->get_error_message() );
1165 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1167 return strval( $post_ID );
1171 * Edit a post for any registered post type.
1173 * The $content_struct parameter only needs to contain fields that
1174 * should be changed. All other fields will retain their existing values.
1178 * @param array $args Method parameters. Contains:
1180 * - string $username
1181 * - string $password
1183 * - array $content_struct
1184 * @return true on success
1186 function wp_editPost( $args ) {
1187 if ( ! $this->minimum_args( $args, 5 ) )
1188 return $this->error;
1190 $this->escape( $args );
1192 $blog_id = (int) $args[0];
1193 $username = $args[1];
1194 $password = $args[2];
1195 $post_id = (int) $args[3];
1196 $content_struct = $args[4];
1198 if ( ! $user = $this->login( $username, $password ) )
1199 return $this->error;
1201 do_action( 'xmlrpc_call', 'wp.editPost' );
1203 $post = get_post( $post_id, ARRAY_A );
1205 if ( empty( $post['ID'] ) )
1206 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1208 // convert the date field back to IXR form
1209 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1211 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1212 // since _insert_post will ignore the non-GMT date if the GMT date is set
1213 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1214 unset( $post['post_date_gmt'] );
1216 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1218 $this->escape( $post );
1219 $merged_content_struct = array_merge( $post, $content_struct );
1221 $retval = $this->_insert_post( $user, $merged_content_struct );
1222 if ( $retval instanceof IXR_Error )
1229 * Delete a post for any registered post type.
1233 * @uses wp_delete_post()
1234 * @param array $args Method parameters. Contains:
1236 * - string $username
1237 * - string $password
1239 * @return true on success
1241 function wp_deletePost( $args ) {
1242 if ( ! $this->minimum_args( $args, 4 ) )
1243 return $this->error;
1245 $this->escape( $args );
1247 $blog_id = (int) $args[0];
1248 $username = $args[1];
1249 $password = $args[2];
1250 $post_id = (int) $args[3];
1252 if ( ! $user = $this->login( $username, $password ) )
1253 return $this->error;
1255 do_action( 'xmlrpc_call', 'wp.deletePost' );
1257 $post = wp_get_single_post( $post_id, ARRAY_A );
1258 if ( empty( $post['ID'] ) )
1259 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1261 $post_type = get_post_type_object( $post['post_type'] );
1262 if ( ! current_user_can( $post_type->cap->delete_post, $post_id ) )
1263 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1265 $result = wp_delete_post( $post_id );
1268 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1278 * The optional $fields parameter specifies what fields will be included
1279 * in the response array. This should be a list of field names. 'post_id' will
1280 * always be included in the response regardless of the value of $fields.
1282 * Instead of, or in addition to, individual field names, conceptual group
1283 * names can be used to specify multiple fields. The available conceptual
1284 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1287 * @uses wp_get_single_post()
1288 * @param array $args Method parameters. Contains:
1290 * - string $username
1291 * - string $password
1292 * - array $fields optional
1293 * @return array contains (based on $fields parameter):
1299 * - 'post_modified_gmt'
1308 * - 'comment_status'
1317 function wp_getPost( $args ) {
1318 if ( ! $this->minimum_args( $args, 4 ) )
1319 return $this->error;
1321 $this->escape( $args );
1323 $blog_id = (int) $args[0];
1324 $username = $args[1];
1325 $password = $args[2];
1326 $post_id = (int) $args[3];
1328 if ( isset( $args[4] ) )
1331 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1333 if ( ! $user = $this->login( $username, $password ) )
1334 return $this->error;
1336 do_action( 'xmlrpc_call', 'wp.getPost' );
1338 $post = wp_get_single_post( $post_id, ARRAY_A );
1340 if ( empty( $post['ID'] ) )
1341 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1343 $post_type = get_post_type_object( $post['post_type'] );
1344 if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) )
1345 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1347 return $this->_prepare_post( $post, $fields );
1355 * The optional $filter parameter modifies the query used to retrieve posts.
1356 * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
1357 * 'orderby', and 'order'.
1359 * The optional $fields parameter specifies what fields will be included
1360 * in the response array.
1362 * @uses wp_get_recent_posts()
1363 * @see wp_getPost() for more on $fields
1364 * @see get_posts() for more on $filter values
1366 * @param array $args Method parameters. Contains:
1368 * - string $username
1369 * - string $password
1370 * - array $filter optional
1371 * - array $fields optional
1372 * @return array contains a collection of posts.
1374 function wp_getPosts( $args ) {
1375 if ( ! $this->minimum_args( $args, 3 ) )
1376 return $this->error;
1378 $this->escape( $args );
1380 $blog_id = (int) $args[0];
1381 $username = $args[1];
1382 $password = $args[2];
1383 $filter = isset( $args[3] ) ? $args[3] : array();
1385 if ( isset( $args[4] ) )
1388 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1390 if ( ! $user = $this->login( $username, $password ) )
1391 return $this->error;
1393 do_action( 'xmlrpc_call', 'wp.getPosts' );
1397 if ( isset( $filter['post_type'] ) ) {
1398 $post_type = get_post_type_object( $filter['post_type'] );
1399 if ( ! ( (bool) $post_type ) )
1400 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1402 $post_type = get_post_type_object( 'post' );
1405 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1406 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1408 $query['post_type'] = $post_type->name;
1410 if ( isset( $filter['post_status'] ) )
1411 $query['post_status'] = $filter['post_status'];
1413 if ( isset( $filter['number'] ) )
1414 $query['numberposts'] = absint( $filter['number'] );
1416 if ( isset( $filter['offset'] ) )
1417 $query['offset'] = absint( $filter['offset'] );
1419 if ( isset( $filter['orderby'] ) ) {
1420 $query['orderby'] = $filter['orderby'];
1422 if ( isset( $filter['order'] ) )
1423 $query['order'] = $filter['order'];
1426 $posts_list = wp_get_recent_posts( $query );
1428 if ( ! $posts_list )
1431 // holds all the posts data
1434 foreach ( $posts_list as $post ) {
1435 $post_type = get_post_type_object( $post['post_type'] );
1436 if ( ! current_user_can( $post_type->cap->edit_post, $post['ID'] ) )
1439 $struct[] = $this->_prepare_post( $post, $fields );
1446 * Create a new term.
1450 * @uses wp_insert_term()
1451 * @param array $args Method parameters. Contains:
1453 * - string $username
1454 * - string $password
1455 * - array $content_struct
1456 * The $content_struct must contain:
1459 * Also, it can optionally contain:
1463 * @return string term_id
1465 function wp_newTerm( $args ) {
1466 if ( ! $this->minimum_args( $args, 4 ) )
1467 return $this->error;
1469 $this->escape( $args );
1471 $blog_id = (int) $args[0];
1472 $username = $args[1];
1473 $password = $args[2];
1474 $content_struct = $args[3];
1476 if ( ! $user = $this->login( $username, $password ) )
1477 return $this->error;
1479 do_action( 'xmlrpc_call', 'wp.newTerm' );
1481 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1482 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1484 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1486 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1487 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1489 $taxonomy = (array) $taxonomy;
1491 // hold the data of the term
1492 $term_data = array();
1494 $term_data['name'] = trim( $content_struct['name'] );
1495 if ( empty( $term_data['name'] ) )
1496 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1498 if ( isset( $content_struct['parent'] ) ) {
1499 if ( ! $taxonomy['hierarchical'] )
1500 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1502 $parent_term_id = (int) $content_struct['parent'];
1503 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1505 if ( is_wp_error( $parent_term ) )
1506 return new IXR_Error( 500, $parent_term->get_error_message() );
1508 if ( ! $parent_term )
1509 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1511 $term_data['parent'] = $content_struct['parent'];
1514 if ( isset( $content_struct['description'] ) )
1515 $term_data['description'] = $content_struct['description'];
1517 if ( isset( $content_struct['slug'] ) )
1518 $term_data['slug'] = $content_struct['slug'];
1520 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1522 if ( is_wp_error( $term ) )
1523 return new IXR_Error( 500, $term->get_error_message() );
1526 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1528 return strval( $term['term_id'] );
1536 * @uses wp_update_term()
1537 * @param array $args Method parameters. Contains:
1539 * - string $username
1540 * - string $password
1542 * - array $content_struct
1543 * The $content_struct must contain:
1545 * Also, it can optionally contain:
1550 * @return bool True, on success.
1552 function wp_editTerm( $args ) {
1553 if ( ! $this->minimum_args( $args, 5 ) )
1554 return $this->error;
1556 $this->escape( $args );
1558 $blog_id = (int) $args[0];
1559 $username = $args[1];
1560 $password = $args[2];
1561 $term_id = (int) $args[3];
1562 $content_struct = $args[4];
1564 if ( ! $user = $this->login( $username, $password ) )
1565 return $this->error;
1567 do_action( 'xmlrpc_call', 'wp.editTerm' );
1569 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1570 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1572 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1574 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1575 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1577 $taxonomy = (array) $taxonomy;
1579 // hold the data of the term
1580 $term_data = array();
1582 $term = get_term( $term_id , $content_struct['taxonomy'] );
1584 if ( is_wp_error( $term ) )
1585 return new IXR_Error( 500, $term->get_error_message() );
1588 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1590 if ( isset( $content_struct['name'] ) ) {
1591 $term_data['name'] = trim( $content_struct['name'] );
1593 if ( empty( $term_data['name'] ) )
1594 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1597 if ( isset( $content_struct['parent'] ) ) {
1598 if ( ! $taxonomy['hierarchical'] )
1599 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1601 $parent_term_id = (int) $content_struct['parent'];
1602 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1604 if ( is_wp_error( $parent_term ) )
1605 return new IXR_Error( 500, $parent_term->get_error_message() );
1607 if ( ! $parent_term )
1608 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1610 $term_data['parent'] = $content_struct['parent'];
1613 if ( isset( $content_struct['description'] ) )
1614 $term_data['description'] = $content_struct['description'];
1616 if ( isset( $content_struct['slug'] ) )
1617 $term_data['slug'] = $content_struct['slug'];
1619 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1621 if ( is_wp_error( $term ) )
1622 return new IXR_Error( 500, $term->get_error_message() );
1625 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1635 * @uses wp_delete_term()
1636 * @param array $args Method parameters. Contains:
1638 * - string $username
1639 * - string $password
1640 * - string $taxnomy_name
1642 * @return boolean|IXR_Error If it suceeded true else a reason why not
1644 function wp_deleteTerm( $args ) {
1645 if ( ! $this->minimum_args( $args, 5 ) )
1646 return $this->error;
1648 $this->escape( $args );
1650 $blog_id = (int) $args[0];
1651 $username = $args[1];
1652 $password = $args[2];
1653 $taxonomy = $args[3];
1654 $term_id = (int) $args[4];
1656 if ( ! $user = $this->login( $username, $password ) )
1657 return $this->error;
1659 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1661 if ( ! taxonomy_exists( $taxonomy ) )
1662 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1664 $taxonomy = get_taxonomy( $taxonomy );
1666 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
1667 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
1669 $term = get_term( $term_id, $taxonomy->name );
1671 if ( is_wp_error( $term ) )
1672 return new IXR_Error( 500, $term->get_error_message() );
1675 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1677 $result = wp_delete_term( $term_id, $taxonomy->name );
1679 if ( is_wp_error( $result ) )
1680 return new IXR_Error( 500, $term->get_error_message() );
1683 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
1694 * @param array $args Method parameters. Contains:
1696 * - string $username
1697 * - string $password
1698 * - string $taxonomy
1700 * @return array contains:
1705 * - 'term_taxonomy_id'
1711 function wp_getTerm( $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.getTerm' );
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->assign_terms ) )
1734 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1736 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
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 return $this->_prepare_term( $term );
1748 * Retrieve all terms for a taxonomy.
1752 * The optional $filter parameter modifies the query used to retrieve terms.
1753 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
1756 * @param array $args Method parameters. Contains:
1758 * - string $username
1759 * - string $password
1760 * - string $taxonomy
1761 * - array $filter optional
1762 * @return array terms
1764 function wp_getTerms( $args ) {
1765 if ( ! $this->minimum_args( $args, 4 ) )
1766 return $this->error;
1768 $this->escape( $args );
1770 $blog_id = (int) $args[0];
1771 $username = $args[1];
1772 $password = $args[2];
1773 $taxonomy = $args[3];
1774 $filter = isset( $args[4] ) ? $args[4] : array();
1776 if ( ! $user = $this->login( $username, $password ) )
1777 return $this->error;
1779 do_action( 'xmlrpc_call', 'wp.getTerms' );
1781 if ( ! taxonomy_exists( $taxonomy ) )
1782 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1784 $taxonomy = get_taxonomy( $taxonomy );
1786 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1787 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1791 if ( isset( $filter['number'] ) )
1792 $query['number'] = absint( $filter['number'] );
1794 if ( isset( $filter['offset'] ) )
1795 $query['offset'] = absint( $filter['offset'] );
1797 if ( isset( $filter['orderby'] ) ) {
1798 $query['orderby'] = $filter['orderby'];
1800 if ( isset( $filter['order'] ) )
1801 $query['order'] = $filter['order'];
1804 if ( isset( $filter['hide_empty'] ) )
1805 $query['hide_empty'] = $filter['hide_empty'];
1807 $query['get'] = 'all';
1809 if ( isset( $filter['search'] ) )
1810 $query['search'] = $filter['search'];
1812 $terms = get_terms( $taxonomy->name, $query );
1814 if ( is_wp_error( $terms ) )
1815 return new IXR_Error( 500, $terms->get_error_message() );
1819 foreach ( $terms as $term ) {
1820 $struct[] = $this->_prepare_term( $term );
1827 * Retrieve a taxonomy.
1831 * @uses get_taxonomy()
1832 * @param array $args Method parameters. Contains:
1834 * - string $username
1835 * - string $password
1836 * - string $taxonomy
1837 * @return array (@see get_taxonomy())
1839 function wp_getTaxonomy( $args ) {
1840 if ( ! $this->minimum_args( $args, 4 ) )
1841 return $this->error;
1843 $this->escape( $args );
1845 $blog_id = (int) $args[0];
1846 $username = $args[1];
1847 $password = $args[2];
1848 $taxonomy = $args[3];
1850 if ( isset( $args[4] ) )
1853 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
1855 if ( ! $user = $this->login( $username, $password ) )
1856 return $this->error;
1858 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
1860 if ( ! taxonomy_exists( $taxonomy ) )
1861 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1863 $taxonomy = get_taxonomy( $taxonomy );
1865 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1866 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1868 return $this->_prepare_taxonomy( $taxonomy, $fields );
1872 * Retrieve all taxonomies.
1876 * @uses get_taxonomies()
1877 * @param array $args Method parameters. Contains:
1879 * - string $username
1880 * - string $password
1881 * @return array taxonomies
1883 function wp_getTaxonomies( $args ) {
1884 if ( ! $this->minimum_args( $args, 3 ) )
1885 return $this->error;
1887 $this->escape( $args );
1889 $blog_id = (int) $args[0];
1890 $username = $args[1];
1891 $password = $args[2];
1892 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
1894 if ( isset( $args[4] ) )
1897 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
1899 if ( ! $user = $this->login( $username, $password ) )
1900 return $this->error;
1902 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
1904 $taxonomies = get_taxonomies( $filter, 'objects' );
1906 // holds all the taxonomy data
1909 foreach ( $taxonomies as $taxonomy ) {
1910 // capability check for post_types
1911 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1914 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
1925 * @param array $args Method parameters. Contains:
1932 function wp_getPage($args) {
1933 $this->escape($args);
1935 $blog_id = (int) $args[0];
1936 $page_id = (int) $args[1];
1937 $username = $args[2];
1938 $password = $args[3];
1940 if ( !$user = $this->login($username, $password) ) {
1941 return $this->error;
1944 $page = get_page($page_id);
1946 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1948 if ( !current_user_can( 'edit_page', $page_id ) )
1949 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
1951 do_action('xmlrpc_call', 'wp.getPage');
1953 // If we found the page then format the data.
1954 if ( $page->ID && ($page->post_type == 'page') ) {
1955 return $this->_prepare_page( $page );
1957 // If the page doesn't exist indicate that.
1959 return(new IXR_Error(404, __('Sorry, no such page.')));
1968 * @param array $args Method parameters. Contains:
1975 function wp_getPages($args) {
1976 $this->escape($args);
1978 $blog_id = (int) $args[0];
1979 $username = $args[1];
1980 $password = $args[2];
1981 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
1983 if ( !$user = $this->login($username, $password) )
1984 return $this->error;
1986 if ( !current_user_can( 'edit_pages' ) )
1987 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
1989 do_action('xmlrpc_call', 'wp.getPages');
1991 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
1992 $num_pages = count($pages);
1994 // If we have pages, put together their info.
1995 if ( $num_pages >= 1 ) {
1996 $pages_struct = array();
1998 foreach ($pages as $page) {
1999 if ( current_user_can( 'edit_page', $page->ID ) )
2000 $pages_struct[] = $this->_prepare_page( $page );
2003 return($pages_struct);
2005 // If no pages were found return an error.
2016 * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
2019 function wp_newPage($args) {
2020 // Items not escaped here will be escaped in newPost.
2021 $username = $this->escape($args[1]);
2022 $password = $this->escape($args[2]);
2024 $publish = $args[4];
2026 if ( !$user = $this->login($username, $password) )
2027 return $this->error;
2029 do_action('xmlrpc_call', 'wp.newPage');
2031 // Mark this as content for a page.
2032 $args[3]["post_type"] = 'page';
2034 // Let mw_newPost do all of the heavy lifting.
2035 return($this->mw_newPost($args));
2043 * @param array $args Method parameters.
2044 * @return bool True, if success.
2046 function wp_deletePage($args) {
2047 $this->escape($args);
2049 $blog_id = (int) $args[0];
2050 $username = $args[1];
2051 $password = $args[2];
2052 $page_id = (int) $args[3];
2054 if ( !$user = $this->login($username, $password) )
2055 return $this->error;
2057 do_action('xmlrpc_call', 'wp.deletePage');
2059 // Get the current page based on the page_id and
2060 // make sure it is a page and not a post.
2061 $actual_page = wp_get_single_post($page_id, ARRAY_A);
2062 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2063 return(new IXR_Error(404, __('Sorry, no such page.')));
2065 // Make sure the user can delete pages.
2066 if ( !current_user_can('delete_page', $page_id) )
2067 return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
2069 // Attempt to delete the page.
2070 $result = wp_delete_post($page_id);
2072 return(new IXR_Error(500, __('Failed to delete the page.')));
2074 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2084 * @param array $args Method parameters.
2087 function wp_editPage($args) {
2088 // Items not escaped here will be escaped in editPost.
2089 $blog_id = (int) $args[0];
2090 $page_id = (int) $this->escape($args[1]);
2091 $username = $this->escape($args[2]);
2092 $password = $this->escape($args[3]);
2093 $content = $args[4];
2094 $publish = $args[5];
2096 if ( !$user = $this->login($username, $password) )
2097 return $this->error;
2099 do_action('xmlrpc_call', 'wp.editPage');
2101 // Get the page data and make sure it is a page.
2102 $actual_page = wp_get_single_post($page_id, ARRAY_A);
2103 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2104 return(new IXR_Error(404, __('Sorry, no such page.')));
2106 // Make sure the user is allowed to edit pages.
2107 if ( !current_user_can('edit_page', $page_id) )
2108 return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
2110 // Mark this as content for a page.
2111 $content['post_type'] = 'page';
2113 // Arrange args in the way mw_editPost understands.
2122 // Let mw_editPost do all of the heavy lifting.
2123 return($this->mw_editPost($args));
2127 * Retrieve page list.
2131 * @param array $args Method parameters.
2134 function wp_getPageList($args) {
2137 $this->escape($args);
2139 $blog_id = (int) $args[0];
2140 $username = $args[1];
2141 $password = $args[2];
2143 if ( !$user = $this->login($username, $password) )
2144 return $this->error;
2146 if ( !current_user_can( 'edit_pages' ) )
2147 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2149 do_action('xmlrpc_call', 'wp.getPageList');
2151 // Get list of pages ids and titles
2152 $page_list = $wpdb->get_results("
2154 post_title page_title,
2155 post_parent page_parent_id,
2160 WHERE post_type = 'page'
2164 // The date needs to be formatted properly.
2165 $num_pages = count($page_list);
2166 for ( $i = 0; $i < $num_pages; $i++ ) {
2167 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2168 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2170 unset($page_list[$i]->post_date_gmt);
2171 unset($page_list[$i]->post_date);
2172 unset($page_list[$i]->post_status);
2179 * Retrieve authors list.
2183 * @param array $args Method parameters.
2186 function wp_getAuthors($args) {
2188 $this->escape($args);
2190 $blog_id = (int) $args[0];
2191 $username = $args[1];
2192 $password = $args[2];
2194 if ( !$user = $this->login($username, $password) )
2195 return $this->error;
2197 if ( !current_user_can('edit_posts') )
2198 return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
2200 do_action('xmlrpc_call', 'wp.getAuthors');
2203 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2205 'user_id' => $user->ID,
2206 'user_login' => $user->user_login,
2207 'display_name' => $user->display_name
2215 * Get list of all tags
2219 * @param array $args Method parameters.
2222 function wp_getTags( $args ) {
2223 $this->escape( $args );
2225 $blog_id = (int) $args[0];
2226 $username = $args[1];
2227 $password = $args[2];
2229 if ( !$user = $this->login($username, $password) )
2230 return $this->error;
2232 if ( !current_user_can( 'edit_posts' ) )
2233 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2235 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2239 if ( $all_tags = get_tags() ) {
2240 foreach( (array) $all_tags as $tag ) {
2241 $struct['tag_id'] = $tag->term_id;
2242 $struct['name'] = $tag->name;
2243 $struct['count'] = $tag->count;
2244 $struct['slug'] = $tag->slug;
2245 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2246 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
2256 * Create new category.
2260 * @param array $args Method parameters.
2261 * @return int Category ID.
2263 function wp_newCategory($args) {
2264 $this->escape($args);
2266 $blog_id = (int) $args[0];
2267 $username = $args[1];
2268 $password = $args[2];
2269 $category = $args[3];
2271 if ( !$user = $this->login($username, $password) )
2272 return $this->error;
2274 do_action('xmlrpc_call', 'wp.newCategory');
2276 // Make sure the user is allowed to add a category.
2277 if ( !current_user_can('manage_categories') )
2278 return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
2280 // If no slug was provided make it empty so that
2281 // WordPress will generate one.
2282 if ( empty($category['slug']) )
2283 $category['slug'] = '';
2285 // If no parent_id was provided make it empty
2286 // so that it will be a top level page (no parent).
2287 if ( !isset($category['parent_id']) )
2288 $category['parent_id'] = '';
2290 // If no description was provided make it empty.
2291 if ( empty($category["description"]) )
2292 $category["description"] = "";
2294 $new_category = array(
2295 'cat_name' => $category['name'],
2296 'category_nicename' => $category['slug'],
2297 'category_parent' => $category['parent_id'],
2298 'category_description' => $category['description']
2301 $cat_id = wp_insert_category($new_category, true);
2302 if ( is_wp_error( $cat_id ) ) {
2303 if ( 'term_exists' == $cat_id->get_error_code() )
2304 return (int) $cat_id->get_error_data();
2306 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2307 } elseif ( ! $cat_id ) {
2308 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2311 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
2321 * @param array $args Method parameters.
2322 * @return mixed See {@link wp_delete_term()} for return info.
2324 function wp_deleteCategory($args) {
2325 $this->escape($args);
2327 $blog_id = (int) $args[0];
2328 $username = $args[1];
2329 $password = $args[2];
2330 $category_id = (int) $args[3];
2332 if ( !$user = $this->login($username, $password) )
2333 return $this->error;
2335 do_action('xmlrpc_call', 'wp.deleteCategory');
2337 if ( !current_user_can('manage_categories') )
2338 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
2340 $status = wp_delete_term( $category_id, 'category' );
2342 if( true == $status )
2343 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
2349 * Retrieve category list.
2353 * @param array $args Method parameters.
2356 function wp_suggestCategories($args) {
2357 $this->escape($args);
2359 $blog_id = (int) $args[0];
2360 $username = $args[1];
2361 $password = $args[2];
2362 $category = $args[3];
2363 $max_results = (int) $args[4];
2365 if ( !$user = $this->login($username, $password) )
2366 return $this->error;
2368 if ( !current_user_can( 'edit_posts' ) )
2369 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
2371 do_action('xmlrpc_call', 'wp.suggestCategories');
2373 $category_suggestions = array();
2374 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
2375 foreach ( (array) get_categories($args) as $cat ) {
2376 $category_suggestions[] = array(
2377 'category_id' => $cat->term_id,
2378 'category_name' => $cat->name
2382 return($category_suggestions);
2390 * @param array $args Method parameters.
2393 function wp_getComment($args) {
2394 $this->escape($args);
2396 $blog_id = (int) $args[0];
2397 $username = $args[1];
2398 $password = $args[2];
2399 $comment_id = (int) $args[3];
2401 if ( !$user = $this->login($username, $password) )
2402 return $this->error;
2404 if ( !current_user_can( 'moderate_comments' ) )
2405 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2407 do_action('xmlrpc_call', 'wp.getComment');
2409 if ( ! $comment = get_comment($comment_id) )
2410 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2412 return $this->_prepare_comment( $comment );
2416 * Retrieve comments.
2418 * Besides the common blog_id, username, and password arguments, it takes a filter
2419 * array as last argument.
2421 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
2423 * The defaults are as follows:
2424 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
2425 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
2426 * - 'number' - Default is 10. Total number of media items to retrieve.
2427 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2431 * @param array $args Method parameters.
2432 * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
2434 function wp_getComments($args) {
2435 $this->escape($args);
2437 $blog_id = (int) $args[0];
2438 $username = $args[1];
2439 $password = $args[2];
2440 $struct = isset( $args[3] ) ? $args[3] : array();
2442 if ( !$user = $this->login($username, $password) )
2443 return $this->error;
2445 if ( !current_user_can( 'moderate_comments' ) )
2446 return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
2448 do_action('xmlrpc_call', 'wp.getComments');
2450 if ( isset($struct['status']) )
2451 $status = $struct['status'];
2456 if ( isset($struct['post_id']) )
2457 $post_id = absint($struct['post_id']);
2460 if ( isset($struct['offset']) )
2461 $offset = absint($struct['offset']);
2464 if ( isset($struct['number']) )
2465 $number = absint($struct['number']);
2467 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
2469 $comments_struct = array();
2471 foreach ( $comments as $comment ) {
2472 $comments_struct[] = $this->_prepare_comment( $comment );
2475 return $comments_struct;
2481 * By default, the comment will be moved to the trash instead of deleted.
2482 * See {@link wp_delete_comment()} for more information on
2487 * @param array $args Method parameters. Contains:
2492 * @return mixed {@link wp_delete_comment()}
2494 function wp_deleteComment($args) {
2495 $this->escape($args);
2497 $blog_id = (int) $args[0];
2498 $username = $args[1];
2499 $password = $args[2];
2500 $comment_ID = (int) $args[3];
2502 if ( !$user = $this->login($username, $password) )
2503 return $this->error;
2505 if ( !current_user_can( 'moderate_comments' ) )
2506 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2508 if ( ! get_comment($comment_ID) )
2509 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2511 if ( !current_user_can( 'edit_comment', $comment_ID ) )
2512 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2514 do_action('xmlrpc_call', 'wp.deleteComment');
2516 $status = wp_delete_comment( $comment_ID );
2518 if( true == $status )
2519 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
2527 * Besides the common blog_id, username, and password arguments, it takes a
2528 * comment_id integer and a content_struct array as last argument.
2530 * The allowed keys in the content_struct array are:
2535 * - 'date_created_gmt'
2536 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
2540 * @param array $args. Contains:
2546 * @return bool True, on success.
2548 function wp_editComment($args) {
2549 $this->escape($args);
2551 $blog_id = (int) $args[0];
2552 $username = $args[1];
2553 $password = $args[2];
2554 $comment_ID = (int) $args[3];
2555 $content_struct = $args[4];
2557 if ( !$user = $this->login($username, $password) )
2558 return $this->error;
2560 if ( !current_user_can( 'moderate_comments' ) )
2561 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2563 if ( ! get_comment($comment_ID) )
2564 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2566 if ( !current_user_can( 'edit_comment', $comment_ID ) )
2567 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2569 do_action('xmlrpc_call', 'wp.editComment');
2571 if ( isset($content_struct['status']) ) {
2572 $statuses = get_comment_statuses();
2573 $statuses = array_keys($statuses);
2575 if ( ! in_array($content_struct['status'], $statuses) )
2576 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
2577 $comment_approved = $content_struct['status'];
2580 // Do some timestamp voodoo
2581 if ( !empty( $content_struct['date_created_gmt'] ) ) {
2582 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
2583 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
2584 $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
2585 $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
2588 if ( isset($content_struct['content']) )
2589 $comment_content = $content_struct['content'];
2591 if ( isset($content_struct['author']) )
2592 $comment_author = $content_struct['author'];
2594 if ( isset($content_struct['author_url']) )
2595 $comment_author_url = $content_struct['author_url'];
2597 if ( isset($content_struct['author_email']) )
2598 $comment_author_email = $content_struct['author_email'];
2600 // We've got all the data -- post it:
2601 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
2603 $result = wp_update_comment($comment);
2604 if ( is_wp_error( $result ) )
2605 return new IXR_Error(500, $result->get_error_message());
2608 return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
2610 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
2616 * Create new comment.
2620 * @param array $args Method parameters.
2621 * @return mixed {@link wp_new_comment()}
2623 function wp_newComment($args) {
2626 $this->escape($args);
2628 $blog_id = (int) $args[0];
2629 $username = $args[1];
2630 $password = $args[2];
2632 $content_struct = $args[4];
2634 $allow_anon = apply_filters('xmlrpc_allow_anonymous_comments', false);
2636 $user = $this->login($username, $password);
2640 if ( $allow_anon && get_option('comment_registration') )
2641 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
2642 else if ( !$allow_anon )
2643 return $this->error;
2648 if ( is_numeric($post) )
2649 $post_id = absint($post);
2651 $post_id = url_to_postid($post);
2654 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2656 if ( ! get_post($post_id) )
2657 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2659 $comment['comment_post_ID'] = $post_id;
2662 $comment['comment_author'] = $wpdb->escape( $user->display_name );
2663 $comment['comment_author_email'] = $wpdb->escape( $user->user_email );
2664 $comment['comment_author_url'] = $wpdb->escape( $user->user_url );
2665 $comment['user_ID'] = $user->ID;
2667 $comment['comment_author'] = '';
2668 if ( isset($content_struct['author']) )
2669 $comment['comment_author'] = $content_struct['author'];
2671 $comment['comment_author_email'] = '';
2672 if ( isset($content_struct['author_email']) )
2673 $comment['comment_author_email'] = $content_struct['author_email'];
2675 $comment['comment_author_url'] = '';
2676 if ( isset($content_struct['author_url']) )
2677 $comment['comment_author_url'] = $content_struct['author_url'];
2679 $comment['user_ID'] = 0;
2681 if ( get_option('require_name_email') ) {
2682 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
2683 return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
2684 elseif ( !is_email($comment['comment_author_email']) )
2685 return new IXR_Error( 403, __( 'A valid email address is required' ) );
2689 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
2691 $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
2693 do_action('xmlrpc_call', 'wp.newComment');
2695 $comment_ID = wp_new_comment( $comment );
2697 do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
2703 * Retrieve all of the comment status.
2707 * @param array $args Method parameters.
2710 function wp_getCommentStatusList($args) {
2711 $this->escape( $args );
2713 $blog_id = (int) $args[0];
2714 $username = $args[1];
2715 $password = $args[2];
2717 if ( !$user = $this->login($username, $password) )
2718 return $this->error;
2720 if ( !current_user_can( 'moderate_comments' ) )
2721 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
2723 do_action('xmlrpc_call', 'wp.getCommentStatusList');
2725 return get_comment_statuses();
2729 * Retrieve comment count.
2733 * @param array $args Method parameters.
2736 function wp_getCommentCount( $args ) {
2737 $this->escape($args);
2739 $blog_id = (int) $args[0];
2740 $username = $args[1];
2741 $password = $args[2];
2742 $post_id = (int) $args[3];
2744 if ( !$user = $this->login($username, $password) )
2745 return $this->error;
2747 if ( !current_user_can( 'edit_posts' ) )
2748 return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
2750 do_action('xmlrpc_call', 'wp.getCommentCount');
2752 $count = wp_count_comments( $post_id );
2754 'approved' => $count->approved,
2755 'awaiting_moderation' => $count->moderated,
2756 'spam' => $count->spam,
2757 'total_comments' => $count->total_comments
2762 * Retrieve post statuses.
2766 * @param array $args Method parameters.
2769 function wp_getPostStatusList( $args ) {
2770 $this->escape( $args );
2772 $blog_id = (int) $args[0];
2773 $username = $args[1];
2774 $password = $args[2];
2776 if ( !$user = $this->login($username, $password) )
2777 return $this->error;
2779 if ( !current_user_can( 'edit_posts' ) )
2780 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
2782 do_action('xmlrpc_call', 'wp.getPostStatusList');
2784 return get_post_statuses();
2788 * Retrieve page statuses.
2792 * @param array $args Method parameters.
2795 function wp_getPageStatusList( $args ) {
2796 $this->escape( $args );
2798 $blog_id = (int) $args[0];
2799 $username = $args[1];
2800 $password = $args[2];
2802 if ( !$user = $this->login($username, $password) )
2803 return $this->error;
2805 if ( !current_user_can( 'edit_pages' ) )
2806 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
2808 do_action('xmlrpc_call', 'wp.getPageStatusList');
2810 return get_page_statuses();
2814 * Retrieve page templates.
2818 * @param array $args Method parameters.
2821 function wp_getPageTemplates( $args ) {
2822 $this->escape( $args );
2824 $blog_id = (int) $args[0];
2825 $username = $args[1];
2826 $password = $args[2];
2828 if ( !$user = $this->login($username, $password) )
2829 return $this->error;
2831 if ( !current_user_can( 'edit_pages' ) )
2832 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
2834 $templates = get_page_templates();
2835 $templates['Default'] = 'default';
2841 * Retrieve blog options.
2845 * @param array $args Method parameters.
2848 function wp_getOptions( $args ) {
2849 $this->escape( $args );
2851 $blog_id = (int) $args[0];
2852 $username = $args[1];
2853 $password = $args[2];
2854 $options = isset( $args[3] ) ? (array) $args[3] : array();
2856 if ( !$user = $this->login($username, $password) )
2857 return $this->error;
2859 // If no specific options where asked for, return all of them
2860 if ( count( $options ) == 0 )
2861 $options = array_keys($this->blog_options);
2863 return $this->_getOptions($options);
2867 * Retrieve blog options value from list.
2871 * @param array $options Options to retrieve.
2874 function _getOptions($options) {
2876 foreach ( $options as $option ) {
2877 if ( array_key_exists( $option, $this->blog_options ) ) {
2878 $data[$option] = $this->blog_options[$option];
2879 //Is the value static or dynamic?
2880 if ( isset( $data[$option]['option'] ) ) {
2881 $data[$option]['value'] = get_option( $data[$option]['option'] );
2882 unset($data[$option]['option']);
2891 * Update blog options.
2895 * @param array $args Method parameters.
2898 function wp_setOptions( $args ) {
2899 $this->escape( $args );
2901 $blog_id = (int) $args[0];
2902 $username = $args[1];
2903 $password = $args[2];
2904 $options = (array) $args[3];
2906 if ( !$user = $this->login($username, $password) )
2907 return $this->error;
2909 if ( !current_user_can( 'manage_options' ) )
2910 return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
2912 foreach ( $options as $o_name => $o_value ) {
2913 $option_names[] = $o_name;
2914 if ( !array_key_exists( $o_name, $this->blog_options ) )
2917 if ( $this->blog_options[$o_name]['readonly'] == true )
2920 update_option( $this->blog_options[$o_name]['option'], $o_value );
2923 //Now return the updated values
2924 return $this->_getOptions($option_names);
2928 * Retrieve a media item by ID
2932 * @param array $args Method parameters. Contains:
2937 * @return array. Associative array containing:
2938 * - 'date_created_gmt'
2947 function wp_getMediaItem($args) {
2948 $this->escape($args);
2950 $blog_id = (int) $args[0];
2951 $username = $args[1];
2952 $password = $args[2];
2953 $attachment_id = (int) $args[3];
2955 if ( !$user = $this->login($username, $password) )
2956 return $this->error;
2958 if ( !current_user_can( 'upload_files' ) )
2959 return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
2961 do_action('xmlrpc_call', 'wp.getMediaItem');
2963 if ( ! $attachment = get_post($attachment_id) )
2964 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
2966 return $this->_prepare_media_item( $attachment );
2970 * Retrieves a collection of media library items (or attachments)
2972 * Besides the common blog_id, username, and password arguments, it takes a filter
2973 * array as last argument.
2975 * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
2977 * The defaults are as follows:
2978 * - 'number' - Default is 5. Total number of media items to retrieve.
2979 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2980 * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
2981 * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
2985 * @param array $args Method parameters. Contains:
2990 * @return array. Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
2992 function wp_getMediaLibrary($args) {
2993 $this->escape($args);
2995 $blog_id = (int) $args[0];
2996 $username = $args[1];
2997 $password = $args[2];
2998 $struct = isset( $args[3] ) ? $args[3] : array() ;
3000 if ( !$user = $this->login($username, $password) )
3001 return $this->error;
3003 if ( !current_user_can( 'upload_files' ) )
3004 return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
3006 do_action('xmlrpc_call', 'wp.getMediaLibrary');
3008 $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
3009 $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
3010 $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
3011 $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
3013 $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
3015 $attachments_struct = array();
3017 foreach ($attachments as $attachment )
3018 $attachments_struct[] = $this->_prepare_media_item( $attachment );
3020 return $attachments_struct;
3024 * Retrieves a list of post formats used by the site
3028 * @param array $args Method parameters. Contains:
3034 function wp_getPostFormats( $args ) {
3035 $this->escape( $args );
3037 $blog_id = (int) $args[0];
3038 $username = $args[1];
3039 $password = $args[2];
3041 if ( !$user = $this->login( $username, $password ) )
3042 return $this->error;
3044 if ( !current_user_can( 'edit_posts' ) )
3045 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3047 do_action( 'xmlrpc_call', 'wp.getPostFormats' );
3049 $formats = get_post_format_strings();
3051 # find out if they want a list of currently supports formats
3052 if ( isset( $args[3] ) && is_array( $args[3] ) ) {
3053 if ( $args[3]['show-supported'] ) {
3054 if ( current_theme_supports( 'post-formats' ) ) {
3055 $supported = get_theme_support( 'post-formats' );
3057 $data['all'] = $formats;
3058 $data['supported'] = $supported[0];
3069 * Retrieves a post type
3073 * @uses get_post_type_object()
3074 * @param array $args Method parameters. Contains:
3076 * - string $username
3077 * - string $password
3078 * - string $post_type_name
3080 * @return array contains:
3083 * - 'capability_type'
3091 function wp_getPostType( $args ) {
3092 if ( ! $this->minimum_args( $args, 4 ) )
3093 return $this->error;
3095 $this->escape( $args );
3097 $blog_id = (int) $args[0];
3098 $username = $args[1];
3099 $password = $args[2];
3100 $post_type_name = $args[3];
3102 if ( isset( $args[4] ) )
3105 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
3107 if ( !$user = $this->login( $username, $password ) )
3108 return $this->error;
3110 do_action( 'xmlrpc_call', 'wp.getPostType' );
3112 if( ! post_type_exists( $post_type_name ) )
3113 return new IXR_Error( 403, __( 'Invalid post type' ) );
3115 $post_type = get_post_type_object( $post_type_name );
3117 if( ! current_user_can( $post_type->cap->edit_posts ) )
3118 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
3120 return $this->_prepare_post_type( $post_type, $fields );
3124 * Retrieves a post types
3128 * @uses get_post_types()
3129 * @param array $args Method parameters. Contains:
3131 * - string $username
3132 * - string $password
3137 function wp_getPostTypes( $args ) {
3138 if ( ! $this->minimum_args( $args, 3 ) )
3139 return $this->error;
3141 $this->escape( $args );
3143 $blog_id = (int) $args[0];
3144 $username = $args[1];
3145 $password = $args[2];
3146 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
3148 if ( isset( $args[4] ) )
3151 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
3153 if ( ! $user = $this->login( $username, $password ) )
3154 return $this->error;
3156 do_action( 'xmlrpc_call', 'wp.getPostTypes' );
3158 $post_types = get_post_types( $filter, 'objects' );
3162 foreach( $post_types as $post_type ) {
3163 if( ! current_user_can( $post_type->cap->edit_posts ) )
3166 $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
3172 /* Blogger API functions.
3173 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
3177 * Retrieve blogs that user owns.
3179 * Will make more sense once we support multiple blogs.
3183 * @param array $args Method parameters.
3186 function blogger_getUsersBlogs($args) {
3187 if ( is_multisite() )