3 * XML-RPC protocol support for WordPress
6 * @subpackage Publishing
10 * WordPress XMLRPC server implementation.
12 * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
13 * pingback. Additional WordPress API for managing comments, pages, posts,
16 * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled
17 * via the xmlrpc_enabled filter found in wp_xmlrpc_server::login().
20 * @subpackage Publishing
23 class wp_xmlrpc_server extends IXR_Server {
49 * Flags that the user authentication has failed in this instance of wp_xmlrpc_server.
54 protected $auth_failed = false;
57 * Register all of the XMLRPC methods that XMLRPC server understands.
59 * Sets up server and method property. Passes XMLRPC
60 * methods through the 'xmlrpc_methods' filter to allow plugins to extend
61 * or replace XMLRPC methods.
65 public function __construct() {
66 $this->methods = array(
68 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
69 'wp.newPost' => 'this:wp_newPost',
70 'wp.editPost' => 'this:wp_editPost',
71 'wp.deletePost' => 'this:wp_deletePost',
72 'wp.getPost' => 'this:wp_getPost',
73 'wp.getPosts' => 'this:wp_getPosts',
74 'wp.newTerm' => 'this:wp_newTerm',
75 'wp.editTerm' => 'this:wp_editTerm',
76 'wp.deleteTerm' => 'this:wp_deleteTerm',
77 'wp.getTerm' => 'this:wp_getTerm',
78 'wp.getTerms' => 'this:wp_getTerms',
79 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
80 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
81 'wp.getUser' => 'this:wp_getUser',
82 'wp.getUsers' => 'this:wp_getUsers',
83 'wp.getProfile' => 'this:wp_getProfile',
84 'wp.editProfile' => 'this:wp_editProfile',
85 'wp.getPage' => 'this:wp_getPage',
86 'wp.getPages' => 'this:wp_getPages',
87 'wp.newPage' => 'this:wp_newPage',
88 'wp.deletePage' => 'this:wp_deletePage',
89 'wp.editPage' => 'this:wp_editPage',
90 'wp.getPageList' => 'this:wp_getPageList',
91 'wp.getAuthors' => 'this:wp_getAuthors',
92 'wp.getCategories' => 'this:mw_getCategories', // Alias
93 'wp.getTags' => 'this:wp_getTags',
94 'wp.newCategory' => 'this:wp_newCategory',
95 'wp.deleteCategory' => 'this:wp_deleteCategory',
96 'wp.suggestCategories' => 'this:wp_suggestCategories',
97 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
98 'wp.deleteFile' => 'this:wp_deletePost', // Alias
99 'wp.getCommentCount' => 'this:wp_getCommentCount',
100 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
101 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
102 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
103 'wp.getOptions' => 'this:wp_getOptions',
104 'wp.setOptions' => 'this:wp_setOptions',
105 'wp.getComment' => 'this:wp_getComment',
106 'wp.getComments' => 'this:wp_getComments',
107 'wp.deleteComment' => 'this:wp_deleteComment',
108 'wp.editComment' => 'this:wp_editComment',
109 'wp.newComment' => 'this:wp_newComment',
110 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
111 'wp.getMediaItem' => 'this:wp_getMediaItem',
112 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
113 'wp.getPostFormats' => 'this:wp_getPostFormats',
114 'wp.getPostType' => 'this:wp_getPostType',
115 'wp.getPostTypes' => 'this:wp_getPostTypes',
116 'wp.getRevisions' => 'this:wp_getRevisions',
117 'wp.restoreRevision' => 'this:wp_restoreRevision',
120 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
121 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
122 'blogger.getPost' => 'this:blogger_getPost',
123 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
124 'blogger.newPost' => 'this:blogger_newPost',
125 'blogger.editPost' => 'this:blogger_editPost',
126 'blogger.deletePost' => 'this:blogger_deletePost',
128 // MetaWeblog API (with MT extensions to structs)
129 'metaWeblog.newPost' => 'this:mw_newPost',
130 'metaWeblog.editPost' => 'this:mw_editPost',
131 'metaWeblog.getPost' => 'this:mw_getPost',
132 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
133 'metaWeblog.getCategories' => 'this:mw_getCategories',
134 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
136 // MetaWeblog API aliases for Blogger API
137 // see http://www.xmlrpc.com/stories/storyReader$2460
138 'metaWeblog.deletePost' => 'this:blogger_deletePost',
139 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
142 'mt.getCategoryList' => 'this:mt_getCategoryList',
143 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
144 'mt.getPostCategories' => 'this:mt_getPostCategories',
145 'mt.setPostCategories' => 'this:mt_setPostCategories',
146 'mt.supportedMethods' => 'this:mt_supportedMethods',
147 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
148 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
149 'mt.publishPost' => 'this:mt_publishPost',
152 'pingback.ping' => 'this:pingback_ping',
153 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
155 'demo.sayHello' => 'this:sayHello',
156 'demo.addTwoNumbers' => 'this:addTwoNumbers'
159 $this->initialise_blog_option_info();
162 * Filter the methods exposed by the XML-RPC server.
164 * This filter can be used to add new methods, and remove built-in methods.
168 * @param array $methods An array of XML-RPC methods.
170 $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
174 * Make private/protected methods readable for backwards compatibility.
179 * @param callable $name Method to call.
180 * @param array $arguments Arguments to pass when calling.
181 * @return array|IXR_Error|false Return value of the callback, false otherwise.
183 public function __call( $name, $arguments ) {
184 if ( '_multisite_getUsersBlogs' === $name ) {
185 return call_user_func_array( array( $this, $name ), $arguments );
193 public function serve_request() {
194 $this->IXR_Server($this->methods);
198 * Test XMLRPC API by saying, "Hello!" to client.
202 * @return string Hello string response.
204 public function sayHello() {
209 * Test XMLRPC API by adding two numbers for client.
213 * @param array $args {
214 * Method arguments. Note: arguments must be ordered as documented.
216 * @type int $number1 A number to add.
217 * @type int $number2 A second number to add.
219 * @return int Sum of the two given numbers.
221 public function addTwoNumbers( $args ) {
224 return $number1 + $number2;
232 * @param string $username User's username.
233 * @param string $password User's password.
234 * @return WP_User|bool WP_User object if authentication passed, false otherwise
236 public function login( $username, $password ) {
238 * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
239 * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
241 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
242 if ( false === $enabled ) {
243 $enabled = apply_filters( 'option_enable_xmlrpc', true );
247 * Filter whether XML-RPC is enabled.
249 * This is the proper filter for turning off XML-RPC.
253 * @param bool $enabled Whether XML-RPC is enabled. Default true.
255 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
258 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
262 if ( $this->auth_failed ) {
263 $user = new WP_Error( 'login_prevented' );
265 $user = wp_authenticate( $username, $password );
268 if ( is_wp_error( $user ) ) {
269 $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
271 // Flag that authentication has failed once on this wp_xmlrpc_server instance
272 $this->auth_failed = true;
275 * Filter the XML-RPC user login error message.
279 * @param string $error The XML-RPC error message.
280 * @param WP_User $user WP_User object.
282 $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
286 wp_set_current_user( $user->ID );
291 * Check user's credentials. Deprecated.
294 * @deprecated 2.8.0 Use wp_xmlrpc_server::login()
295 * @see wp_xmlrpc_server::login()
297 * @param string $username User's username.
298 * @param string $password User's password.
299 * @return bool Whether authentication passed.
301 public function login_pass_ok( $username, $password ) {
302 return (bool) $this->login( $username, $password );
306 * Escape string or array of strings for database.
310 * @param string|array $data Escape single string or array of strings.
311 * @return string|void Returns with string is passed, alters by-reference
312 * when array is passed.
314 public function escape( &$data ) {
315 if ( ! is_array( $data ) )
316 return wp_slash( $data );
318 foreach ( $data as &$v ) {
319 if ( is_array( $v ) )
321 elseif ( ! is_object( $v ) )
327 * Retrieve custom fields for post.
331 * @param int $post_id Post ID.
332 * @return array Custom fields, if exist.
334 public function get_custom_fields($post_id) {
335 $post_id = (int) $post_id;
337 $custom_fields = array();
339 foreach ( (array) has_meta($post_id) as $meta ) {
340 // Don't expose protected fields.
341 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
344 $custom_fields[] = array(
345 "id" => $meta['meta_id'],
346 "key" => $meta['meta_key'],
347 "value" => $meta['meta_value']
351 return $custom_fields;
355 * Set custom fields for post.
359 * @param int $post_id Post ID.
360 * @param array $fields Custom fields.
362 public function set_custom_fields($post_id, $fields) {
363 $post_id = (int) $post_id;
365 foreach ( (array) $fields as $meta ) {
366 if ( isset($meta['id']) ) {
367 $meta['id'] = (int) $meta['id'];
368 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
369 if ( isset($meta['key']) ) {
370 $meta['key'] = wp_unslash( $meta['key'] );
371 if ( $meta['key'] !== $pmeta->meta_key )
373 $meta['value'] = wp_unslash( $meta['value'] );
374 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
375 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
376 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
377 delete_metadata_by_mid( 'post', $meta['id'] );
379 } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
380 add_post_meta( $post_id, $meta['key'], $meta['value'] );
386 * Set up blog options property.
388 * Passes property through {@see 'xmlrpc_blog_options'} filter.
392 * @global string $wp_version
394 public function initialise_blog_option_info() {
397 $this->blog_options = array(
399 'software_name' => array(
400 'desc' => __( 'Software Name' ),
402 'value' => 'WordPress'
404 'software_version' => array(
405 'desc' => __( 'Software Version' ),
407 'value' => $wp_version
410 'desc' => __( 'WordPress Address (URL)' ),
412 'option' => 'siteurl'
415 'desc' => __( 'Site Address (URL)' ),
419 'login_url' => array(
420 'desc' => __( 'Login Address (URL)' ),
422 'value' => wp_login_url( )
424 'admin_url' => array(
425 'desc' => __( 'The URL to the admin area' ),
427 'value' => get_admin_url( )
429 'image_default_link_type' => array(
430 'desc' => __( 'Image default link type' ),
432 'option' => 'image_default_link_type'
434 'image_default_size' => array(
435 'desc' => __( 'Image default size' ),
437 'option' => 'image_default_size'
439 'image_default_align' => array(
440 'desc' => __( 'Image default align' ),
442 'option' => 'image_default_align'
445 'desc' => __( 'Template' ),
447 'option' => 'template'
449 'stylesheet' => array(
450 'desc' => __( 'Stylesheet' ),
452 'option' => 'stylesheet'
454 'post_thumbnail' => array(
455 'desc' => __('Post Thumbnail'),
457 'value' => current_theme_supports( 'post-thumbnails' )
461 'time_zone' => array(
462 'desc' => __( 'Time Zone' ),
464 'option' => 'gmt_offset'
466 'blog_title' => array(
467 'desc' => __( 'Site Title' ),
469 'option' => 'blogname'
471 'blog_tagline' => array(
472 'desc' => __( 'Site Tagline' ),
474 'option' => 'blogdescription'
476 'date_format' => array(
477 'desc' => __( 'Date Format' ),
479 'option' => 'date_format'
481 'time_format' => array(
482 'desc' => __( 'Time Format' ),
484 'option' => 'time_format'
486 'users_can_register' => array(
487 'desc' => __( 'Allow new users to sign up' ),
489 'option' => 'users_can_register'
491 'thumbnail_size_w' => array(
492 'desc' => __( 'Thumbnail Width' ),
494 'option' => 'thumbnail_size_w'
496 'thumbnail_size_h' => array(
497 'desc' => __( 'Thumbnail Height' ),
499 'option' => 'thumbnail_size_h'
501 'thumbnail_crop' => array(
502 'desc' => __( 'Crop thumbnail to exact dimensions' ),
504 'option' => 'thumbnail_crop'
506 'medium_size_w' => array(
507 'desc' => __( 'Medium size image width' ),
509 'option' => 'medium_size_w'
511 'medium_size_h' => array(
512 'desc' => __( 'Medium size image height' ),
514 'option' => 'medium_size_h'
516 'medium_large_size_w' => array(
517 'desc' => __( 'Medium-Large size image width' ),
519 'option' => 'medium_large_size_w'
521 'medium_large_size_h' => array(
522 'desc' => __( 'Medium-Large size image height' ),
524 'option' => 'medium_large_size_h'
526 'large_size_w' => array(
527 'desc' => __( 'Large size image width' ),
529 'option' => 'large_size_w'
531 'large_size_h' => array(
532 'desc' => __( 'Large size image height' ),
534 'option' => 'large_size_h'
536 'default_comment_status' => array(
537 'desc' => __( 'Allow people to post comments on new articles' ),
539 'option' => 'default_comment_status'
541 'default_ping_status' => array(
542 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles' ),
544 'option' => 'default_ping_status'
549 * Filter the XML-RPC blog options property.
553 * @param array $blog_options An array of XML-RPC blog options.
555 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
559 * Retrieve the blogs of the user.
563 * @param array $args {
564 * Method arguments. Note: arguments must be ordered as documented.
566 * @type string $username Username.
567 * @type string $password Password.
569 * @return array|IXR_Error Array contains:
571 * - 'isPrimary' - whether the blog is the user's primary blog
575 * - 'xmlrpc' - url of xmlrpc endpoint
577 public function wp_getUsersBlogs( $args ) {
578 // If this isn't on WPMU then just use blogger_getUsersBlogs
579 if ( !is_multisite() ) {
580 array_unshift( $args, 1 );
581 return $this->blogger_getUsersBlogs( $args );
584 $this->escape( $args );
586 $username = $args[0];
587 $password = $args[1];
589 if ( !$user = $this->login($username, $password) )
593 * Fires after the XML-RPC user has been authenticated but before the rest of
594 * the method logic begins.
596 * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
597 * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
601 * @param string $name The method name.
603 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
605 $blogs = (array) get_blogs_of_user( $user->ID );
607 $primary_blog_id = 0;
608 $active_blog = get_active_blog_for_user( $user->ID );
609 if ( $active_blog ) {
610 $primary_blog_id = (int) $active_blog->blog_id;
613 foreach ( $blogs as $blog ) {
614 // Don't include blogs that aren't hosted at this site.
615 if ( $blog->site_id != get_current_site()->id )
618 $blog_id = $blog->userblog_id;
620 switch_to_blog( $blog_id );
622 $is_admin = current_user_can( 'manage_options' );
623 $is_primary = ( (int) $blog_id === $primary_blog_id );
626 'isAdmin' => $is_admin,
627 'isPrimary' => $is_primary,
628 'url' => home_url( '/' ),
629 'blogid' => (string) $blog_id,
630 'blogName' => get_option( 'blogname' ),
631 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
634 restore_current_blog();
641 * Checks if the method received at least the minimum number of arguments.
646 * @param string|array $args Sanitize single string or array of strings.
647 * @param int $count Minimum number of arguments.
648 * @return bool if `$args` contains at least $count arguments.
650 protected function minimum_args( $args, $count ) {
651 if ( count( $args ) < $count ) {
652 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
660 * Prepares taxonomy data for return in an XML-RPC object.
664 * @param object $taxonomy The unprepared taxonomy data.
665 * @param array $fields The subset of taxonomy fields to return.
666 * @return array The prepared taxonomy data.
668 protected function _prepare_taxonomy( $taxonomy, $fields ) {
670 'name' => $taxonomy->name,
671 'label' => $taxonomy->label,
672 'hierarchical' => (bool) $taxonomy->hierarchical,
673 'public' => (bool) $taxonomy->public,
674 'show_ui' => (bool) $taxonomy->show_ui,
675 '_builtin' => (bool) $taxonomy->_builtin,
678 if ( in_array( 'labels', $fields ) )
679 $_taxonomy['labels'] = (array) $taxonomy->labels;
681 if ( in_array( 'cap', $fields ) )
682 $_taxonomy['cap'] = (array) $taxonomy->cap;
684 if ( in_array( 'menu', $fields ) )
685 $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
687 if ( in_array( 'object_type', $fields ) )
688 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
691 * Filter XML-RPC-prepared data for the given taxonomy.
695 * @param array $_taxonomy An array of taxonomy data.
696 * @param object $taxonomy Taxonomy object.
697 * @param array $fields The subset of taxonomy fields to return.
699 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
703 * Prepares term data for return in an XML-RPC object.
707 * @param array|object $term The unprepared term data.
708 * @return array The prepared term data.
710 protected function _prepare_term( $term ) {
712 if ( ! is_array( $_term ) )
713 $_term = get_object_vars( $_term );
715 // For integers which may be larger than XML-RPC supports ensure we return strings.
716 $_term['term_id'] = strval( $_term['term_id'] );
717 $_term['term_group'] = strval( $_term['term_group'] );
718 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
719 $_term['parent'] = strval( $_term['parent'] );
721 // Count we are happy to return as an integer because people really shouldn't use terms that much.
722 $_term['count'] = intval( $_term['count'] );
725 * Filter XML-RPC-prepared data for the given term.
729 * @param array $_term An array of term data.
730 * @param array|object $term Term object or array.
732 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
736 * Convert a WordPress date string to an IXR_Date object.
740 * @param string $date Date string to convert.
741 * @return IXR_Date IXR_Date object.
743 protected function _convert_date( $date ) {
744 if ( $date === '0000-00-00 00:00:00' ) {
745 return new IXR_Date( '00000000T00:00:00Z' );
747 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
751 * Convert a WordPress GMT date string to an IXR_Date object.
755 * @param string $date_gmt WordPress GMT date string.
756 * @param string $date Date string.
757 * @return IXR_Date IXR_Date object.
759 protected function _convert_date_gmt( $date_gmt, $date ) {
760 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
761 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
763 return $this->_convert_date( $date_gmt );
767 * Prepares post data for return in an XML-RPC object.
771 * @param array $post The unprepared post data.
772 * @param array $fields The subset of post type fields to return.
773 * @return array The prepared post data.
775 protected function _prepare_post( $post, $fields ) {
776 // Holds the data for this post. built up based on $fields.
777 $_post = array( 'post_id' => strval( $post['ID'] ) );
779 // Prepare common post fields.
780 $post_fields = array(
781 'post_title' => $post['post_title'],
782 'post_date' => $this->_convert_date( $post['post_date'] ),
783 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
784 'post_modified' => $this->_convert_date( $post['post_modified'] ),
785 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
786 'post_status' => $post['post_status'],
787 'post_type' => $post['post_type'],
788 'post_name' => $post['post_name'],
789 'post_author' => $post['post_author'],
790 'post_password' => $post['post_password'],
791 'post_excerpt' => $post['post_excerpt'],
792 'post_content' => $post['post_content'],
793 'post_parent' => strval( $post['post_parent'] ),
794 'post_mime_type' => $post['post_mime_type'],
795 'link' => get_permalink( $post['ID'] ),
796 'guid' => $post['guid'],
797 'menu_order' => intval( $post['menu_order'] ),
798 'comment_status' => $post['comment_status'],
799 'ping_status' => $post['ping_status'],
800 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
804 $post_fields['post_thumbnail'] = array();
805 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
806 if ( $thumbnail_id ) {
807 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
808 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
811 // Consider future posts as published.
812 if ( $post_fields['post_status'] === 'future' )
813 $post_fields['post_status'] = 'publish';
815 // Fill in blank post format.
816 $post_fields['post_format'] = get_post_format( $post['ID'] );
817 if ( empty( $post_fields['post_format'] ) )
818 $post_fields['post_format'] = 'standard';
820 // Merge requested $post_fields fields into $_post.
821 if ( in_array( 'post', $fields ) ) {
822 $_post = array_merge( $_post, $post_fields );
824 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
825 $_post = array_merge( $_post, $requested_fields );
828 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
830 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
831 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
832 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
833 $_post['terms'] = array();
834 foreach ( $terms as $term ) {
835 $_post['terms'][] = $this->_prepare_term( $term );
839 if ( in_array( 'custom_fields', $fields ) )
840 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
842 if ( in_array( 'enclosure', $fields ) ) {
843 $_post['enclosure'] = array();
844 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
845 if ( ! empty( $enclosures ) ) {
846 $encdata = explode( "\n", $enclosures[0] );
847 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
848 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
849 $_post['enclosure']['type'] = trim( $encdata[2] );
854 * Filter XML-RPC-prepared date for the given post.
858 * @param array $_post An array of modified post data.
859 * @param array $post An array of post data.
860 * @param array $fields An array of post fields.
862 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
866 * Prepares post data for return in an XML-RPC object.
870 * @param object $post_type Post type object.
871 * @param array $fields The subset of post fields to return.
872 * @return array The prepared post type data.
874 protected function _prepare_post_type( $post_type, $fields ) {
876 'name' => $post_type->name,
877 'label' => $post_type->label,
878 'hierarchical' => (bool) $post_type->hierarchical,
879 'public' => (bool) $post_type->public,
880 'show_ui' => (bool) $post_type->show_ui,
881 '_builtin' => (bool) $post_type->_builtin,
882 'has_archive' => (bool) $post_type->has_archive,
883 'supports' => get_all_post_type_supports( $post_type->name ),
886 if ( in_array( 'labels', $fields ) ) {
887 $_post_type['labels'] = (array) $post_type->labels;
890 if ( in_array( 'cap', $fields ) ) {
891 $_post_type['cap'] = (array) $post_type->cap;
892 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
895 if ( in_array( 'menu', $fields ) ) {
896 $_post_type['menu_position'] = (int) $post_type->menu_position;
897 $_post_type['menu_icon'] = $post_type->menu_icon;
898 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
901 if ( in_array( 'taxonomies', $fields ) )
902 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
905 * Filter XML-RPC-prepared date for the given post type.
909 * @param array $_post_type An array of post type data.
910 * @param object $post_type Post type object.
912 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
916 * Prepares media item data for return in an XML-RPC object.
920 * @param object $media_item The unprepared media item data.
921 * @param string $thumbnail_size The image size to use for the thumbnail URL.
922 * @return array The prepared media item data.
924 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
925 $_media_item = array(
926 'attachment_id' => strval( $media_item->ID ),
927 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
928 'parent' => $media_item->post_parent,
929 'link' => wp_get_attachment_url( $media_item->ID ),
930 'title' => $media_item->post_title,
931 'caption' => $media_item->post_excerpt,
932 'description' => $media_item->post_content,
933 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
934 'type' => $media_item->post_mime_type
937 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
938 if ( $thumbnail_src )
939 $_media_item['thumbnail'] = $thumbnail_src[0];
941 $_media_item['thumbnail'] = $_media_item['link'];
944 * Filter XML-RPC-prepared data for the given media item.
948 * @param array $_media_item An array of media item data.
949 * @param object $media_item Media item object.
950 * @param string $thumbnail_size Image size.
952 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
956 * Prepares page data for return in an XML-RPC object.
960 * @param object $page The unprepared page data.
961 * @return array The prepared page data.
963 protected function _prepare_page( $page ) {
964 // Get all of the page content and link.
965 $full_page = get_extended( $page->post_content );
966 $link = get_permalink( $page->ID );
968 // Get info the page parent if there is one.
970 if ( ! empty( $page->post_parent ) ) {
971 $parent = get_post( $page->post_parent );
972 $parent_title = $parent->post_title;
975 // Determine comment and ping settings.
976 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
977 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
980 $page_date = $this->_convert_date( $page->post_date );
981 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
983 // Pull the categories info together.
984 $categories = array();
985 if ( is_object_in_taxonomy( 'page', 'category' ) ) {
986 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
987 $categories[] = get_cat_name( $cat_id );
991 // Get the author info.
992 $author = get_userdata( $page->post_author );
994 $page_template = get_page_template_slug( $page->ID );
995 if ( empty( $page_template ) )
996 $page_template = 'default';
999 'dateCreated' => $page_date,
1000 'userid' => $page->post_author,
1001 'page_id' => $page->ID,
1002 'page_status' => $page->post_status,
1003 'description' => $full_page['main'],
1004 'title' => $page->post_title,
1006 'permaLink' => $link,
1007 'categories' => $categories,
1008 'excerpt' => $page->post_excerpt,
1009 'text_more' => $full_page['extended'],
1010 'mt_allow_comments' => $allow_comments,
1011 'mt_allow_pings' => $allow_pings,
1012 'wp_slug' => $page->post_name,
1013 'wp_password' => $page->post_password,
1014 'wp_author' => $author->display_name,
1015 'wp_page_parent_id' => $page->post_parent,
1016 'wp_page_parent_title' => $parent_title,
1017 'wp_page_order' => $page->menu_order,
1018 'wp_author_id' => (string) $author->ID,
1019 'wp_author_display_name' => $author->display_name,
1020 'date_created_gmt' => $page_date_gmt,
1021 'custom_fields' => $this->get_custom_fields( $page->ID ),
1022 'wp_page_template' => $page_template
1026 * Filter XML-RPC-prepared data for the given page.
1030 * @param array $_page An array of page data.
1031 * @param WP_Post $page Page object.
1033 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
1037 * Prepares comment data for return in an XML-RPC object.
1041 * @param object $comment The unprepared comment data.
1042 * @return array The prepared comment data.
1044 protected function _prepare_comment( $comment ) {
1045 // Format page date.
1046 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
1048 if ( '0' == $comment->comment_approved ) {
1049 $comment_status = 'hold';
1050 } elseif ( 'spam' == $comment->comment_approved ) {
1051 $comment_status = 'spam';
1052 } elseif ( '1' == $comment->comment_approved ) {
1053 $comment_status = 'approve';
1055 $comment_status = $comment->comment_approved;
1058 'date_created_gmt' => $comment_date_gmt,
1059 'user_id' => $comment->user_id,
1060 'comment_id' => $comment->comment_ID,
1061 'parent' => $comment->comment_parent,
1062 'status' => $comment_status,
1063 'content' => $comment->comment_content,
1064 'link' => get_comment_link($comment),
1065 'post_id' => $comment->comment_post_ID,
1066 'post_title' => get_the_title($comment->comment_post_ID),
1067 'author' => $comment->comment_author,
1068 'author_url' => $comment->comment_author_url,
1069 'author_email' => $comment->comment_author_email,
1070 'author_ip' => $comment->comment_author_IP,
1071 'type' => $comment->comment_type,
1075 * Filter XML-RPC-prepared data for the given comment.
1079 * @param array $_comment An array of prepared comment data.
1080 * @param WP_Comment $comment Comment object.
1082 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
1086 * Prepares user data for return in an XML-RPC object.
1090 * @param WP_User $user The unprepared user object.
1091 * @param array $fields The subset of user fields to return.
1092 * @return array The prepared user data.
1094 protected function _prepare_user( $user, $fields ) {
1095 $_user = array( 'user_id' => strval( $user->ID ) );
1097 $user_fields = array(
1098 'username' => $user->user_login,
1099 'first_name' => $user->user_firstname,
1100 'last_name' => $user->user_lastname,
1101 'registered' => $this->_convert_date( $user->user_registered ),
1102 'bio' => $user->user_description,
1103 'email' => $user->user_email,
1104 'nickname' => $user->nickname,
1105 'nicename' => $user->user_nicename,
1106 'url' => $user->user_url,
1107 'display_name' => $user->display_name,
1108 'roles' => $user->roles,
1111 if ( in_array( 'all', $fields ) ) {
1112 $_user = array_merge( $_user, $user_fields );
1114 if ( in_array( 'basic', $fields ) ) {
1115 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
1116 $fields = array_merge( $fields, $basic_fields );
1118 $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
1119 $_user = array_merge( $_user, $requested_fields );
1123 * Filter XML-RPC-prepared data for the given user.
1127 * @param array $_user An array of user data.
1128 * @param WP_User $user User object.
1129 * @param array $fields An array of user fields.
1131 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
1135 * Create a new post for any registered post type.
1139 * @link http://en.wikipedia.org/wiki/RSS_enclosure for information on RSS enclosures.
1141 * @param array $args {
1142 * Method arguments. Note: top-level arguments must be ordered as documented.
1144 * @type int $blog_id Blog ID (unused).
1145 * @type string $username Username.
1146 * @type string $password Password.
1147 * @type array $content_struct {
1148 * Content struct for adding a new post. See wp_insert_post() for information on
1149 * additional post fields
1151 * @type string $post_type Post type. Default 'post'.
1152 * @type string $post_status Post status. Default 'draft'
1153 * @type string $post_title Post title.
1154 * @type int $post_author Post author ID.
1155 * @type string $post_excerpt Post excerpt.
1156 * @type string $post_content Post content.
1157 * @type string $post_date_gmt Post date in GMT.
1158 * @type string $post_date Post date.
1159 * @type string $post_password Post password (20-character limit).
1160 * @type string $comment_status Post comment enabled status. Accepts 'open' or 'closed'.
1161 * @type string $ping_status Post ping status. Accepts 'open' or 'closed'.
1162 * @type bool $sticky Whether the post should be sticky. Automatically false if
1163 * `$post_status` is 'private'.
1164 * @type int $post_thumbnail ID of an image to use as the post thumbnail/featured image.
1165 * @type array $custom_fields Array of meta key/value pairs to add to the post.
1166 * @type array $terms Associative array with taxonomy names as keys and arrays
1167 * of term IDs as values.
1168 * @type array $terms_names Associative array with taxonomy names as keys and arrays
1169 * of term names as values.
1170 * @type array $enclosure {
1171 * Array of feed enclosure data to add to post meta.
1173 * @type string $url URL for the feed enclosure.
1174 * @type int $length Size in bytes of the enclosure.
1175 * @type string $type Mime-type for the enclosure.
1179 * @return int|IXR_Error Post ID on success, IXR_Error instance otherwise.
1181 public function wp_newPost( $args ) {
1182 if ( ! $this->minimum_args( $args, 4 ) )
1183 return $this->error;
1185 $this->escape( $args );
1187 $username = $args[1];
1188 $password = $args[2];
1189 $content_struct = $args[3];
1191 if ( ! $user = $this->login( $username, $password ) )
1192 return $this->error;
1194 // convert the date field back to IXR form
1195 if ( isset( $content_struct['post_date'] ) && ! ( $content_struct['post_date'] instanceof IXR_Date ) ) {
1196 $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
1199 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1200 // since _insert_post will ignore the non-GMT date if the GMT date is set
1201 if ( isset( $content_struct['post_date_gmt'] ) && ! ( $content_struct['post_date_gmt'] instanceof IXR_Date ) ) {
1202 if ( $content_struct['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) {
1203 unset( $content_struct['post_date_gmt'] );
1205 $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
1209 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1210 do_action( 'xmlrpc_call', 'wp.newPost' );
1212 unset( $content_struct['ID'] );
1214 return $this->_insert_post( $user, $content_struct );
1218 * Helper method for filtering out elements from an array.
1222 * @param int $count Number to compare to one.
1224 private function _is_greater_than_one( $count ) {
1229 * Encapsulate the logic for sticking a post
1230 * and determining if the user has permission to do so
1235 * @param array $post_data
1236 * @param bool $update
1237 * @return void|IXR_Error
1239 private function _toggle_sticky( $post_data, $update = false ) {
1240 $post_type = get_post_type_object( $post_data['post_type'] );
1242 // Private and password-protected posts cannot be stickied.
1243 if ( 'private' === $post_data['post_status'] || ! empty( $post_data['post_password'] ) ) {
1244 // Error if the client tried to stick the post, otherwise, silently unstick.
1245 if ( ! empty( $post_data['sticky'] ) ) {
1246 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1250 unstick_post( $post_data['ID'] );
1252 } elseif ( isset( $post_data['sticky'] ) ) {
1253 if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) {
1254 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1257 $sticky = wp_validate_boolean( $post_data['sticky'] );
1259 stick_post( $post_data['ID'] );
1261 unstick_post( $post_data['ID'] );
1267 * Helper method for wp_newPost() and wp_editPost(), containing shared logic.
1272 * @see wp_insert_post()
1274 * @param WP_User $user The post author if post_author isn't set in $content_struct.
1275 * @param array|IXR_Error $content_struct Post data to insert.
1276 * @return IXR_Error|string
1278 protected function _insert_post( $user, $content_struct ) {
1279 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1280 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1282 $post_data = wp_parse_args( $content_struct, $defaults );
1284 $post_type = get_post_type_object( $post_data['post_type'] );
1286 return new IXR_Error( 403, __( 'Invalid post type' ) );
1288 $update = ! empty( $post_data['ID'] );
1291 if ( ! get_post( $post_data['ID'] ) )
1292 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1293 if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1294 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1295 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1296 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1298 if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1299 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1302 switch ( $post_data['post_status'] ) {
1307 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1308 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1312 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1313 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1316 if ( ! get_post_status_object( $post_data['post_status'] ) )
1317 $post_data['post_status'] = 'draft';
1321 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1322 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1324 $post_data['post_author'] = absint( $post_data['post_author'] );
1325 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1326 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1327 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1329 $author = get_userdata( $post_data['post_author'] );
1332 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1334 $post_data['post_author'] = $user->ID;
1337 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1338 unset( $post_data['comment_status'] );
1340 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1341 unset( $post_data['ping_status'] );
1343 // Do some timestamp voodoo.
1344 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1345 // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
1346 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1347 } elseif ( ! empty( $post_data['post_date'] ) ) {
1348 $dateCreated = $post_data['post_date']->getIso();
1351 if ( ! empty( $dateCreated ) ) {
1352 $post_data['post_date'] = iso8601_to_datetime( $dateCreated );
1353 $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
1356 if ( ! isset( $post_data['ID'] ) )
1357 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1358 $post_ID = $post_data['ID'];
1360 if ( $post_data['post_type'] == 'post' ) {
1361 $error = $this->_toggle_sticky( $post_data, $update );
1367 if ( isset( $post_data['post_thumbnail'] ) ) {
1368 // empty value deletes, non-empty value adds/updates.
1369 if ( ! $post_data['post_thumbnail'] )
1370 delete_post_thumbnail( $post_ID );
1371 elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1372 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1373 set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1374 unset( $content_struct['post_thumbnail'] );
1377 if ( isset( $post_data['custom_fields'] ) )
1378 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1380 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1381 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1383 // Accumulate term IDs from terms and terms_names.
1386 // First validate the terms specified by ID.
1387 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1388 $taxonomies = array_keys( $post_data['terms'] );
1390 // Validating term ids.
1391 foreach ( $taxonomies as $taxonomy ) {
1392 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1393 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1395 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1396 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1398 $term_ids = $post_data['terms'][$taxonomy];
1399 $terms[ $taxonomy ] = array();
1400 foreach ( $term_ids as $term_id ) {
1401 $term = get_term_by( 'id', $term_id, $taxonomy );
1404 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1406 $terms[$taxonomy][] = (int) $term_id;
1411 // Now validate terms specified by name.
1412 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1413 $taxonomies = array_keys( $post_data['terms_names'] );
1415 foreach ( $taxonomies as $taxonomy ) {
1416 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1417 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1419 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1420 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1423 * For hierarchical taxonomies, we can't assign a term when multiple terms
1424 * in the hierarchy share the same name.
1426 $ambiguous_terms = array();
1427 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1428 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1430 // Count the number of terms with the same name.
1431 $tax_term_names_count = array_count_values( $tax_term_names );
1433 // Filter out non-ambiguous term names.
1434 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1436 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1439 $term_names = $post_data['terms_names'][$taxonomy];
1440 foreach ( $term_names as $term_name ) {
1441 if ( in_array( $term_name, $ambiguous_terms ) )
1442 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1444 $term = get_term_by( 'name', $term_name, $taxonomy );
1447 // Term doesn't exist, so check that the user is allowed to create new terms.
1448 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1449 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1451 // Create the new term.
1452 $term_info = wp_insert_term( $term_name, $taxonomy );
1453 if ( is_wp_error( $term_info ) )
1454 return new IXR_Error( 500, $term_info->get_error_message() );
1456 $terms[$taxonomy][] = (int) $term_info['term_id'];
1458 $terms[$taxonomy][] = (int) $term->term_id;
1464 $post_data['tax_input'] = $terms;
1465 unset( $post_data['terms'], $post_data['terms_names'] );
1467 // Do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'.
1468 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1471 if ( isset( $post_data['post_format'] ) ) {
1472 $format = set_post_format( $post_ID, $post_data['post_format'] );
1474 if ( is_wp_error( $format ) )
1475 return new IXR_Error( 500, $format->get_error_message() );
1477 unset( $post_data['post_format'] );
1480 // Handle enclosures.
1481 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1482 $this->add_enclosure_if_new( $post_ID, $enclosure );
1484 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1487 * Filter post data array to be inserted via XML-RPC.
1491 * @param array $post_data Parsed array of post data.
1492 * @param array $content_struct Post data array.
1494 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1496 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1497 if ( is_wp_error( $post_ID ) )
1498 return new IXR_Error( 500, $post_ID->get_error_message() );
1501 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1503 return strval( $post_ID );
1507 * Edit a post for any registered post type.
1509 * The $content_struct parameter only needs to contain fields that
1510 * should be changed. All other fields will retain their existing values.
1514 * @param array $args {
1515 * Method arguments. Note: arguments must be ordered as documented.
1517 * @type int $blog_id Blog ID (unused).
1518 * @type string $username Username.
1519 * @type string $password Password.
1520 * @type int $post_id Post ID.
1521 * @type array $content_struct Extra content arguments.
1523 * @return true|IXR_Error True on success, IXR_Error on failure.
1525 public function wp_editPost( $args ) {
1526 if ( ! $this->minimum_args( $args, 5 ) )
1527 return $this->error;
1529 $this->escape( $args );
1531 $username = $args[1];
1532 $password = $args[2];
1533 $post_id = (int) $args[3];
1534 $content_struct = $args[4];
1536 if ( ! $user = $this->login( $username, $password ) )
1537 return $this->error;
1539 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1540 do_action( 'xmlrpc_call', 'wp.editPost' );
1542 $post = get_post( $post_id, ARRAY_A );
1544 if ( empty( $post['ID'] ) )
1545 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1547 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1548 // If the post has been modified since the date provided, return an error.
1549 if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1550 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1554 // Convert the date field back to IXR form.
1555 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1558 * Ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1559 * since _insert_post() will ignore the non-GMT date if the GMT date is set.
1561 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1562 unset( $post['post_date_gmt'] );
1564 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1566 $this->escape( $post );
1567 $merged_content_struct = array_merge( $post, $content_struct );
1569 $retval = $this->_insert_post( $user, $merged_content_struct );
1570 if ( $retval instanceof IXR_Error )
1577 * Delete a post for any registered post type.
1581 * @see wp_delete_post()
1583 * @param array $args {
1584 * Method arguments. Note: arguments must be ordered as documented.
1586 * @type int $blog_id Blog ID (unused).
1587 * @type string $username Username.
1588 * @type string $password Password.
1589 * @type int $post_id Post ID.
1591 * @return true|IXR_Error True on success, IXR_Error instance on failure.
1593 public function wp_deletePost( $args ) {
1594 if ( ! $this->minimum_args( $args, 4 ) )
1595 return $this->error;
1597 $this->escape( $args );
1599 $username = $args[1];
1600 $password = $args[2];
1601 $post_id = (int) $args[3];
1603 if ( ! $user = $this->login( $username, $password ) )
1604 return $this->error;
1606 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1607 do_action( 'xmlrpc_call', 'wp.deletePost' );
1609 $post = get_post( $post_id, ARRAY_A );
1610 if ( empty( $post['ID'] ) ) {
1611 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1614 if ( ! current_user_can( 'delete_post', $post_id ) ) {
1615 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete this post.' ) );
1618 $result = wp_delete_post( $post_id );
1621 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1632 * The optional $fields parameter specifies what fields will be included
1633 * in the response array. This should be a list of field names. 'post_id' will
1634 * always be included in the response regardless of the value of $fields.
1636 * Instead of, or in addition to, individual field names, conceptual group
1637 * names can be used to specify multiple fields. The available conceptual
1638 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1643 * @param array $args {
1644 * Method arguments. Note: arguments must be ordered as documented.
1646 * @type int $blog_id Blog ID (unused).
1647 * @type string $username Username.
1648 * @type string $password Password.
1649 * @type int $post_id Post ID.
1650 * @type array $fields The subset of post type fields to return.
1652 * @return array|IXR_Error Array contains (based on $fields parameter):
1658 * - 'post_modified_gmt'
1667 * - 'comment_status'
1676 public function wp_getPost( $args ) {
1677 if ( ! $this->minimum_args( $args, 4 ) )
1678 return $this->error;
1680 $this->escape( $args );
1682 $username = $args[1];
1683 $password = $args[2];
1684 $post_id = (int) $args[3];
1686 if ( isset( $args[4] ) ) {
1690 * Filter the list of post query fields used by the given XML-RPC method.
1694 * @param array $fields Array of post fields. Default array contains 'post', 'terms', and 'custom_fields'.
1695 * @param string $method Method name.
1697 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1700 if ( ! $user = $this->login( $username, $password ) )
1701 return $this->error;
1703 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1704 do_action( 'xmlrpc_call', 'wp.getPost' );
1706 $post = get_post( $post_id, ARRAY_A );
1708 if ( empty( $post['ID'] ) )
1709 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1711 if ( ! current_user_can( 'edit_post', $post_id ) )
1712 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1714 return $this->_prepare_post( $post, $fields );
1722 * @see wp_get_recent_posts()
1723 * @see wp_getPost() for more on `$fields`
1724 * @see get_posts() for more on `$filter` values
1726 * @param array $args {
1727 * Method arguments. Note: arguments must be ordered as documented.
1729 * @type int $blog_id Blog ID (unused).
1730 * @type string $username Username.
1731 * @type string $password Password.
1732 * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'post_type',
1733 * 'post_status', 'number', 'offset', 'orderby', 's', and 'order'.
1734 * Default empty array.
1735 * @type array $fields Optional. The subset of post type fields to return in the response array.
1737 * @return array|IXR_Error Array contains a collection of posts.
1739 public function wp_getPosts( $args ) {
1740 if ( ! $this->minimum_args( $args, 3 ) )
1741 return $this->error;
1743 $this->escape( $args );
1745 $username = $args[1];
1746 $password = $args[2];
1747 $filter = isset( $args[3] ) ? $args[3] : array();
1749 if ( isset( $args[4] ) ) {
1752 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1753 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1756 if ( ! $user = $this->login( $username, $password ) )
1757 return $this->error;
1759 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1760 do_action( 'xmlrpc_call', 'wp.getPosts' );
1764 if ( isset( $filter['post_type'] ) ) {
1765 $post_type = get_post_type_object( $filter['post_type'] );
1766 if ( ! ( (bool) $post_type ) )
1767 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1769 $post_type = get_post_type_object( 'post' );
1772 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1773 return new IXR_Error( 401, __( 'You are not allowed to edit posts in this post type.' ));
1775 $query['post_type'] = $post_type->name;
1777 if ( isset( $filter['post_status'] ) )
1778 $query['post_status'] = $filter['post_status'];
1780 if ( isset( $filter['number'] ) )
1781 $query['numberposts'] = absint( $filter['number'] );
1783 if ( isset( $filter['offset'] ) )
1784 $query['offset'] = absint( $filter['offset'] );
1786 if ( isset( $filter['orderby'] ) ) {
1787 $query['orderby'] = $filter['orderby'];
1789 if ( isset( $filter['order'] ) )
1790 $query['order'] = $filter['order'];
1793 if ( isset( $filter['s'] ) ) {
1794 $query['s'] = $filter['s'];
1797 $posts_list = wp_get_recent_posts( $query );
1799 if ( ! $posts_list )
1802 // Holds all the posts data.
1805 foreach ( $posts_list as $post ) {
1806 if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1809 $struct[] = $this->_prepare_post( $post, $fields );
1816 * Create a new term.
1820 * @see wp_insert_term()
1822 * @param array $args {
1823 * Method arguments. Note: arguments must be ordered as documented.
1825 * @type int $blog_id Blog ID (unused).
1826 * @type string $username Username.
1827 * @type string $password Password.
1828 * @type array $content_struct Content struct for adding a new term. The struct must contain
1829 * the term 'name' and 'taxonomy'. Optional accepted values include
1830 * 'parent', 'description', and 'slug'.
1832 * @return int|IXR_Error The term ID on success, or an IXR_Error object on failure.
1834 public function wp_newTerm( $args ) {
1835 if ( ! $this->minimum_args( $args, 4 ) )
1836 return $this->error;
1838 $this->escape( $args );
1840 $username = $args[1];
1841 $password = $args[2];
1842 $content_struct = $args[3];
1844 if ( ! $user = $this->login( $username, $password ) )
1845 return $this->error;
1847 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1848 do_action( 'xmlrpc_call', 'wp.newTerm' );
1850 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1851 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1853 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1855 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1856 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1858 $taxonomy = (array) $taxonomy;
1860 // hold the data of the term
1861 $term_data = array();
1863 $term_data['name'] = trim( $content_struct['name'] );
1864 if ( empty( $term_data['name'] ) )
1865 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1867 if ( isset( $content_struct['parent'] ) ) {
1868 if ( ! $taxonomy['hierarchical'] )
1869 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1871 $parent_term_id = (int) $content_struct['parent'];
1872 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1874 if ( is_wp_error( $parent_term ) )
1875 return new IXR_Error( 500, $parent_term->get_error_message() );
1877 if ( ! $parent_term )
1878 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1880 $term_data['parent'] = $content_struct['parent'];
1883 if ( isset( $content_struct['description'] ) )
1884 $term_data['description'] = $content_struct['description'];
1886 if ( isset( $content_struct['slug'] ) )
1887 $term_data['slug'] = $content_struct['slug'];
1889 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1891 if ( is_wp_error( $term ) )
1892 return new IXR_Error( 500, $term->get_error_message() );
1895 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1897 return strval( $term['term_id'] );
1905 * @see wp_update_term()
1907 * @param array $args {
1908 * Method arguments. Note: arguments must be ordered as documented.
1910 * @type int $blog_id Blog ID (unused).
1911 * @type string $username Username.
1912 * @type string $password Password.
1913 * @type int $term_id Term ID.
1914 * @type array $content_struct Content struct for editing a term. The struct must contain the
1915 * term ''taxonomy'. Optional accepted values include 'name', 'parent',
1916 * 'description', and 'slug'.
1918 * @return true|IXR_Error True on success, IXR_Error instance on failure.
1920 public function wp_editTerm( $args ) {
1921 if ( ! $this->minimum_args( $args, 5 ) )
1922 return $this->error;
1924 $this->escape( $args );
1926 $username = $args[1];
1927 $password = $args[2];
1928 $term_id = (int) $args[3];
1929 $content_struct = $args[4];
1931 if ( ! $user = $this->login( $username, $password ) )
1932 return $this->error;
1934 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1935 do_action( 'xmlrpc_call', 'wp.editTerm' );
1937 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1938 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1940 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1942 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1943 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1945 $taxonomy = (array) $taxonomy;
1947 // hold the data of the term
1948 $term_data = array();
1950 $term = get_term( $term_id , $content_struct['taxonomy'] );
1952 if ( is_wp_error( $term ) )
1953 return new IXR_Error( 500, $term->get_error_message() );
1956 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1958 if ( isset( $content_struct['name'] ) ) {
1959 $term_data['name'] = trim( $content_struct['name'] );
1961 if ( empty( $term_data['name'] ) )
1962 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1965 if ( ! empty( $content_struct['parent'] ) ) {
1966 if ( ! $taxonomy['hierarchical'] )
1967 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1969 $parent_term_id = (int) $content_struct['parent'];
1970 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1972 if ( is_wp_error( $parent_term ) )
1973 return new IXR_Error( 500, $parent_term->get_error_message() );
1975 if ( ! $parent_term )
1976 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1978 $term_data['parent'] = $content_struct['parent'];
1981 if ( isset( $content_struct['description'] ) )
1982 $term_data['description'] = $content_struct['description'];
1984 if ( isset( $content_struct['slug'] ) )
1985 $term_data['slug'] = $content_struct['slug'];
1987 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1989 if ( is_wp_error( $term ) )
1990 return new IXR_Error( 500, $term->get_error_message() );
1993 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
2003 * @see wp_delete_term()
2005 * @param array $args {
2006 * Method arguments. Note: arguments must be ordered as documented.
2008 * @type int $blog_id Blog ID (unused).
2009 * @type string $username Username.
2010 * @type string $password Password.
2011 * @type string $taxnomy_name Taxonomy name.
2012 * @type int $term_id Term ID.
2014 * @return bool|IXR_Error True on success, IXR_Error instance on failure.
2016 public function wp_deleteTerm( $args ) {
2017 if ( ! $this->minimum_args( $args, 5 ) )
2018 return $this->error;
2020 $this->escape( $args );
2022 $username = $args[1];
2023 $password = $args[2];
2024 $taxonomy = $args[3];
2025 $term_id = (int) $args[4];
2027 if ( ! $user = $this->login( $username, $password ) )
2028 return $this->error;
2030 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2031 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
2033 if ( ! taxonomy_exists( $taxonomy ) )
2034 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2036 $taxonomy = get_taxonomy( $taxonomy );
2038 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
2039 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
2041 $term = get_term( $term_id, $taxonomy->name );
2043 if ( is_wp_error( $term ) )
2044 return new IXR_Error( 500, $term->get_error_message() );
2047 return new IXR_Error( 404, __( 'Invalid term ID' ) );
2049 $result = wp_delete_term( $term_id, $taxonomy->name );
2051 if ( is_wp_error( $result ) )
2052 return new IXR_Error( 500, $term->get_error_message() );
2055 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
2067 * @param array $args {
2068 * Method arguments. Note: arguments must be ordered as documented.
2070 * @type int $blog_id Blog ID (unused).
2071 * @type string $username Username.
2072 * @type string $password Password.
2073 * @type string $taxnomy Taxonomy name.
2074 * @type string $term_id Term ID.
2076 * @return array|IXR_Error IXR_Error on failure, array on success, containing:
2081 * - 'term_taxonomy_id'
2087 public function wp_getTerm( $args ) {
2088 if ( ! $this->minimum_args( $args, 5 ) )
2089 return $this->error;
2091 $this->escape( $args );
2093 $username = $args[1];
2094 $password = $args[2];
2095 $taxonomy = $args[3];
2096 $term_id = (int) $args[4];
2098 if ( ! $user = $this->login( $username, $password ) )
2099 return $this->error;
2101 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2102 do_action( 'xmlrpc_call', 'wp.getTerm' );
2104 if ( ! taxonomy_exists( $taxonomy ) )
2105 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2107 $taxonomy = get_taxonomy( $taxonomy );
2109 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2110 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2112 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
2114 if ( is_wp_error( $term ) )
2115 return new IXR_Error( 500, $term->get_error_message() );
2118 return new IXR_Error( 404, __( 'Invalid term ID' ) );
2120 return $this->_prepare_term( $term );
2124 * Retrieve all terms for a taxonomy.
2128 * The optional $filter parameter modifies the query used to retrieve terms.
2129 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
2133 * @param array $args {
2134 * Method arguments. Note: arguments must be ordered as documented.
2136 * @type int $blog_id Blog ID (unused).
2137 * @type string $username Username.
2138 * @type string $password Password.
2139 * @type string $taxnomy Taxonomy name.
2140 * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'number',
2141 * 'offset', 'orderby', 'order', 'hide_empty', and 'search'. Default empty array.
2143 * @return array|IXR_Error An associative array of terms data on success, IXR_Error instance otherwise.
2145 public function wp_getTerms( $args ) {
2146 if ( ! $this->minimum_args( $args, 4 ) )
2147 return $this->error;
2149 $this->escape( $args );
2151 $username = $args[1];
2152 $password = $args[2];
2153 $taxonomy = $args[3];
2154 $filter = isset( $args[4] ) ? $args[4] : array();
2156 if ( ! $user = $this->login( $username, $password ) )
2157 return $this->error;
2159 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2160 do_action( 'xmlrpc_call', 'wp.getTerms' );
2162 if ( ! taxonomy_exists( $taxonomy ) )
2163 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2165 $taxonomy = get_taxonomy( $taxonomy );
2167 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2168 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2172 if ( isset( $filter['number'] ) )
2173 $query['number'] = absint( $filter['number'] );
2175 if ( isset( $filter['offset'] ) )
2176 $query['offset'] = absint( $filter['offset'] );
2178 if ( isset( $filter['orderby'] ) ) {
2179 $query['orderby'] = $filter['orderby'];
2181 if ( isset( $filter['order'] ) )
2182 $query['order'] = $filter['order'];
2185 if ( isset( $filter['hide_empty'] ) )
2186 $query['hide_empty'] = $filter['hide_empty'];
2188 $query['get'] = 'all';
2190 if ( isset( $filter['search'] ) )
2191 $query['search'] = $filter['search'];
2193 $terms = get_terms( $taxonomy->name, $query );
2195 if ( is_wp_error( $terms ) )
2196 return new IXR_Error( 500, $terms->get_error_message() );
2200 foreach ( $terms as $term ) {
2201 $struct[] = $this->_prepare_term( $term );
2208 * Retrieve a taxonomy.
2212 * @see get_taxonomy()
2214 * @param array $args {
2215 * Method arguments. Note: arguments must be ordered as documented.
2217 * @type int $blog_id Blog ID (unused).
2218 * @type string $username Username.
2219 * @type string $password Password.
2220 * @type string $taxnomy Taxonomy name.
2221 * @type array $fields Optional. Array of taxonomy fields to limit to in the return.
2222 * Accepts 'labels', 'cap', 'menu', and 'object_type'.
2223 * Default empty array.
2225 * @return array|IXR_Error An array of taxonomy data on success, IXR_Error instance otherwise.
2227 public function wp_getTaxonomy( $args ) {
2228 if ( ! $this->minimum_args( $args, 4 ) )
2229 return $this->error;
2231 $this->escape( $args );
2233 $username = $args[1];
2234 $password = $args[2];
2235 $taxonomy = $args[3];
2237 if ( isset( $args[4] ) ) {
2241 * Filter the taxonomy query fields used by the given XML-RPC method.
2245 * @param array $fields An array of taxonomy fields to retrieve.
2246 * @param string $method The method name.
2248 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
2251 if ( ! $user = $this->login( $username, $password ) )
2252 return $this->error;
2254 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2255 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
2257 if ( ! taxonomy_exists( $taxonomy ) )
2258 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2260 $taxonomy = get_taxonomy( $taxonomy );
2262 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2263 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2265 return $this->_prepare_taxonomy( $taxonomy, $fields );
2269 * Retrieve all taxonomies.
2273 * @see get_taxonomies()
2275 * @param array $args {
2276 * Method arguments. Note: arguments must be ordered as documented.
2278 * @type int $blog_id Blog ID (unused).
2279 * @type string $username Username.
2280 * @type string $password Password.
2281 * @type array $filter Optional. An array of arguments for retrieving taxonomies.
2282 * @type array $fields Optional. The subset of taxonomy fields to return.
2284 * @return array|IXR_Error An associative array of taxonomy data with returned fields determined
2285 * by `$fields`, or an IXR_Error instance on failure.
2287 public function wp_getTaxonomies( $args ) {
2288 if ( ! $this->minimum_args( $args, 3 ) )
2289 return $this->error;
2291 $this->escape( $args );
2293 $username = $args[1];
2294 $password = $args[2];
2295 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2297 if ( isset( $args[4] ) ) {
2300 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2301 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
2304 if ( ! $user = $this->login( $username, $password ) )
2305 return $this->error;
2307 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2308 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
2310 $taxonomies = get_taxonomies( $filter, 'objects' );
2312 // holds all the taxonomy data
2315 foreach ( $taxonomies as $taxonomy ) {
2316 // capability check for post_types
2317 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2320 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
2329 * The optional $fields parameter specifies what fields will be included
2330 * in the response array. This should be a list of field names. 'user_id' will
2331 * always be included in the response regardless of the value of $fields.
2333 * Instead of, or in addition to, individual field names, conceptual group
2334 * names can be used to specify multiple fields. The available conceptual
2335 * groups are 'basic' and 'all'.
2337 * @uses get_userdata()
2339 * @param array $args {
2340 * Method arguments. Note: arguments must be ordered as documented.
2342 * @type int $blog_id (unused)
2343 * @type string $username
2344 * @type string $password
2345 * @type int $user_id
2346 * @type array $fields (optional)
2348 * @return array|IXR_Error Array contains (based on $fields parameter):
2362 public function wp_getUser( $args ) {
2363 if ( ! $this->minimum_args( $args, 4 ) )
2364 return $this->error;
2366 $this->escape( $args );
2368 $username = $args[1];
2369 $password = $args[2];
2370 $user_id = (int) $args[3];
2372 if ( isset( $args[4] ) ) {
2376 * Filter the default user query fields used by the given XML-RPC method.
2380 * @param array $fields User query fields for given method. Default 'all'.
2381 * @param string $method The method name.
2383 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2386 if ( ! $user = $this->login( $username, $password ) )
2387 return $this->error;
2389 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2390 do_action( 'xmlrpc_call', 'wp.getUser' );
2392 if ( ! current_user_can( 'edit_user', $user_id ) )
2393 return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2395 $user_data = get_userdata( $user_id );
2398 return new IXR_Error( 404, __( 'Invalid user ID.' ) );
2400 return $this->_prepare_user( $user_data, $fields );
2406 * The optional $filter parameter modifies the query used to retrieve users.
2407 * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2408 * 'who', 'orderby', and 'order'.
2410 * The optional $fields parameter specifies what fields will be included
2411 * in the response array.
2414 * @see wp_getUser() for more on $fields and return values
2416 * @param array $args {
2417 * Method arguments. Note: arguments must be ordered as documented.
2419 * @type int $blog_id (unused)
2420 * @type string $username
2421 * @type string $password
2422 * @type array $filter (optional)
2423 * @type array $fields (optional)
2425 * @return array|IXR_Error users data
2427 public function wp_getUsers( $args ) {
2428 if ( ! $this->minimum_args( $args, 3 ) )
2429 return $this->error;
2431 $this->escape( $args );
2433 $username = $args[1];
2434 $password = $args[2];
2435 $filter = isset( $args[3] ) ? $args[3] : array();
2437 if ( isset( $args[4] ) ) {
2440 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2441 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2444 if ( ! $user = $this->login( $username, $password ) )
2445 return $this->error;
2447 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2448 do_action( 'xmlrpc_call', 'wp.getUsers' );
2450 if ( ! current_user_can( 'list_users' ) )
2451 return new IXR_Error( 401, __( 'You are not allowed to browse users.' ) );
2453 $query = array( 'fields' => 'all_with_meta' );
2455 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2456 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2458 if ( isset( $filter['orderby'] ) ) {
2459 $query['orderby'] = $filter['orderby'];
2461 if ( isset( $filter['order'] ) )
2462 $query['order'] = $filter['order'];
2465 if ( isset( $filter['role'] ) ) {
2466 if ( get_role( $filter['role'] ) === null )
2467 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2469 $query['role'] = $filter['role'];
2472 if ( isset( $filter['who'] ) ) {
2473 $query['who'] = $filter['who'];
2476 $users = get_users( $query );
2479 foreach ( $users as $user_data ) {
2480 if ( current_user_can( 'edit_user', $user_data->ID ) )
2481 $_users[] = $this->_prepare_user( $user_data, $fields );
2487 * Retrieve information about the requesting user.
2489 * @uses get_userdata()
2491 * @param array $args {
2492 * Method arguments. Note: arguments must be ordered as documented.
2494 * @type int $blog_id (unused)
2495 * @type string $username
2496 * @type string $password
2497 * @type array $fields (optional)
2499 * @return array|IXR_Error (@see wp_getUser)
2501 public function wp_getProfile( $args ) {
2502 if ( ! $this->minimum_args( $args, 3 ) )
2503 return $this->error;
2505 $this->escape( $args );
2507 $username = $args[1];
2508 $password = $args[2];
2510 if ( isset( $args[3] ) ) {
2513 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2514 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2517 if ( ! $user = $this->login( $username, $password ) )
2518 return $this->error;
2520 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2521 do_action( 'xmlrpc_call', 'wp.getProfile' );
2523 if ( ! current_user_can( 'edit_user', $user->ID ) )
2524 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2526 $user_data = get_userdata( $user->ID );
2528 return $this->_prepare_user( $user_data, $fields );
2532 * Edit user's profile.
2534 * @uses wp_update_user()
2536 * @param array $args {
2537 * Method arguments. Note: arguments must be ordered as documented.
2539 * @type int $blog_id (unused)
2540 * @type string $username
2541 * @type string $password
2542 * @type array $content_struct It can optionally contain:
2551 * @return true|IXR_Error True, on success.
2553 public function wp_editProfile( $args ) {
2554 if ( ! $this->minimum_args( $args, 4 ) )
2555 return $this->error;
2557 $this->escape( $args );
2559 $username = $args[1];
2560 $password = $args[2];
2561 $content_struct = $args[3];
2563 if ( ! $user = $this->login( $username, $password ) )
2564 return $this->error;
2566 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2567 do_action( 'xmlrpc_call', 'wp.editProfile' );
2569 if ( ! current_user_can( 'edit_user', $user->ID ) )
2570 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2572 // holds data of the user
2573 $user_data = array();
2574 $user_data['ID'] = $user->ID;
2576 // only set the user details if it was given
2577 if ( isset( $content_struct['first_name'] ) )
2578 $user_data['first_name'] = $content_struct['first_name'];
2580 if ( isset( $content_struct['last_name'] ) )
2581 $user_data['last_name'] = $content_struct['last_name'];
2583 if ( isset( $content_struct['url'] ) )
2584 $user_data['user_url'] = $content_struct['url'];
2586 if ( isset( $content_struct['display_name'] ) )
2587 $user_data['display_name'] = $content_struct['display_name'];
2589 if ( isset( $content_struct['nickname'] ) )
2590 $user_data['nickname'] = $content_struct['nickname'];
2592 if ( isset( $content_struct['nicename'] ) )
2593 $user_data['user_nicename'] = $content_struct['nicename'];
2595 if ( isset( $content_struct['bio'] ) )
2596 $user_data['description'] = $content_struct['bio'];
2598 $result = wp_update_user( $user_data );
2600 if ( is_wp_error( $result ) )
2601 return new IXR_Error( 500, $result->get_error_message() );
2604 return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2614 * @param array $args {
2615 * Method arguments. Note: arguments must be ordered as documented.
2617 * @type int $blog_id (unused)
2618 * @type int $page_id
2619 * @type string $username
2620 * @type string $password
2622 * @return array|IXR_Error
2624 public function wp_getPage( $args ) {
2625 $this->escape( $args );
2627 $page_id = (int) $args[1];
2628 $username = $args[2];
2629 $password = $args[3];
2631 if ( !$user = $this->login($username, $password) ) {
2632 return $this->error;
2635 $page = get_post($page_id);
2637 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2639 if ( !current_user_can( 'edit_page', $page_id ) )
2640 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2642 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2643 do_action( 'xmlrpc_call', 'wp.getPage' );
2645 // If we found the page then format the data.
2646 if ( $page->ID && ($page->post_type == 'page') ) {
2647 return $this->_prepare_page( $page );
2649 // If the page doesn't exist indicate that.
2651 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2660 * @param array $args {
2661 * Method arguments. Note: arguments must be ordered as documented.
2663 * @type int $blog_id (unused)
2664 * @type string $username
2665 * @type string $password
2666 * @type int $num_pages
2668 * @return array|IXR_Error
2670 public function wp_getPages( $args ) {
2671 $this->escape( $args );
2673 $username = $args[1];
2674 $password = $args[2];
2675 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
2677 if ( !$user = $this->login($username, $password) )
2678 return $this->error;
2680 if ( !current_user_can( 'edit_pages' ) )
2681 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2683 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2684 do_action( 'xmlrpc_call', 'wp.getPages' );
2686 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2687 $num_pages = count($pages);
2689 // If we have pages, put together their info.
2690 if ( $num_pages >= 1 ) {
2691 $pages_struct = array();
2693 foreach ($pages as $page) {
2694 if ( current_user_can( 'edit_page', $page->ID ) )
2695 $pages_struct[] = $this->_prepare_page( $page );
2698 return $pages_struct;
2709 * @see wp_xmlrpc_server::mw_newPost()
2711 * @param array $args {
2712 * Method arguments. Note: arguments must be ordered as documented.
2714 * @type int $blog_id (unused)
2715 * @type string $username
2716 * @type string $password
2717 * @type array $content_struct
2719 * @return int|IXR_Error
2721 public function wp_newPage( $args ) {
2722 // Items not escaped here will be escaped in newPost.
2723 $username = $this->escape( $args[1] );
2724 $password = $this->escape( $args[2] );
2726 if ( !$user = $this->login($username, $password) )
2727 return $this->error;
2729 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2730 do_action( 'xmlrpc_call', 'wp.newPage' );
2732 // Mark this as content for a page.
2733 $args[3]["post_type"] = 'page';
2735 // Let mw_newPost do all of the heavy lifting.
2736 return $this->mw_newPost( $args );
2744 * @param array $args {
2745 * Method arguments. Note: arguments must be ordered as documented.
2747 * @type int $blog_id (unused)
2748 * @type string $username
2749 * @type string $password
2750 * @type int $page_id
2752 * @return true|IXR_Error True, if success.
2754 public function wp_deletePage( $args ) {
2755 $this->escape( $args );
2757 $username = $args[1];
2758 $password = $args[2];
2759 $page_id = (int) $args[3];
2761 if ( !$user = $this->login($username, $password) )
2762 return $this->error;
2764 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2765 do_action( 'xmlrpc_call', 'wp.deletePage' );
2767 // Get the current page based on the page_id and
2768 // make sure it is a page and not a post.
2769 $actual_page = get_post($page_id, ARRAY_A);
2770 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2771 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2773 // Make sure the user can delete pages.
2774 if ( !current_user_can('delete_page', $page_id) )
2775 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete this page.' ) );
2777 // Attempt to delete the page.
2778 $result = wp_delete_post($page_id);
2780 return new IXR_Error( 500, __( 'Failed to delete the page.' ) );
2783 * Fires after a page has been successfully deleted via XML-RPC.
2787 * @param int $page_id ID of the deleted page.
2788 * @param array $args An array of arguments to delete the page.
2790 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2800 * @param array $args {
2801 * Method arguments. Note: arguments must be ordered as documented.
2803 * @type int $blog_id (unused)
2804 * @type int $page_id
2805 * @type string $username
2806 * @type string $password
2807 * @type string $content
2808 * @type string $publish
2810 * @return array|IXR_Error
2812 public function wp_editPage( $args ) {
2813 // Items will be escaped in mw_editPost.
2814 $page_id = (int) $args[1];
2815 $username = $args[2];
2816 $password = $args[3];
2817 $content = $args[4];
2818 $publish = $args[5];
2820 $escaped_username = $this->escape( $username );
2821 $escaped_password = $this->escape( $password );
2823 if ( !$user = $this->login( $escaped_username, $escaped_password ) ) {
2824 return $this->error;
2827 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2828 do_action( 'xmlrpc_call', 'wp.editPage' );
2830 // Get the page data and make sure it is a page.
2831 $actual_page = get_post($page_id, ARRAY_A);
2832 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2833 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2835 // Make sure the user is allowed to edit pages.
2836 if ( !current_user_can('edit_page', $page_id) )
2837 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this page.' ) );
2839 // Mark this as content for a page.
2840 $content['post_type'] = 'page';
2842 // Arrange args in the way mw_editPost understands.
2851 // Let mw_editPost do all of the heavy lifting.
2852 return $this->mw_editPost( $args );
2856 * Retrieve page list.
2860 * @global wpdb $wpdb WordPress database abstraction object.
2862 * @param array $args {
2863 * Method arguments. Note: arguments must be ordered as documented.
2865 * @type int $blog_id (unused)
2866 * @type string $username
2867 * @type string $password
2869 * @return array|IXR_Error
2871 public function wp_getPageList( $args ) {
2874 $this->escape( $args );
2876 $username = $args[1];
2877 $password = $args[2];
2879 if ( !$user = $this->login($username, $password) )
2880 return $this->error;
2882 if ( !current_user_can( 'edit_pages' ) )
2883 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2885 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2886 do_action( 'xmlrpc_call', 'wp.getPageList' );
2888 // Get list of pages ids and titles
2889 $page_list = $wpdb->get_results("
2891 post_title page_title,
2892 post_parent page_parent_id,
2897 WHERE post_type = 'page'
2901 // The date needs to be formatted properly.
2902 $num_pages = count($page_list);
2903 for ( $i = 0; $i < $num_pages; $i++ ) {
2904 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2905 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2907 unset($page_list[$i]->post_date_gmt);
2908 unset($page_list[$i]->post_date);
2909 unset($page_list[$i]->post_status);
2916 * Retrieve authors list.
2920 * @param array $args {
2921 * Method arguments. Note: arguments must be ordered as documented.
2923 * @type int $blog_id (unused)
2924 * @type string $username
2925 * @type string $password
2927 * @return array|IXR_Error
2929 public function wp_getAuthors( $args ) {
2930 $this->escape( $args );
2932 $username = $args[1];
2933 $password = $args[2];
2935 if ( !$user = $this->login($username, $password) )
2936 return $this->error;
2938 if ( !current_user_can('edit_posts') )
2939 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
2941 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2942 do_action( 'xmlrpc_call', 'wp.getAuthors' );
2945 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2947 'user_id' => $user->ID,
2948 'user_login' => $user->user_login,
2949 'display_name' => $user->display_name
2957 * Get list of all tags
2961 * @param array $args {
2962 * Method arguments. Note: arguments must be ordered as documented.
2964 * @type int $blog_id (unused)
2965 * @type string $username
2966 * @type string $password
2968 * @return array|IXR_Error
2970 public function wp_getTags( $args ) {
2971 $this->escape( $args );
2973 $username = $args[1];
2974 $password = $args[2];
2976 if ( !$user = $this->login($username, $password) )
2977 return $this->error;
2979 if ( !current_user_can( 'edit_posts' ) )
2980 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2982 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2983 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2987 if ( $all_tags = get_tags() ) {
2988 foreach ( (array) $all_tags as $tag ) {
2990 $struct['tag_id'] = $tag->term_id;
2991 $struct['name'] = $tag->name;
2992 $struct['count'] = $tag->count;
2993 $struct['slug'] = $tag->slug;
2994 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2995 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
3005 * Create new category.
3009 * @param array $args {
3010 * Method arguments. Note: arguments must be ordered as documented.
3012 * @type int $blog_id (unused)
3013 * @type string $username
3014 * @type string $password
3015 * @type array $category
3017 * @return int|IXR_Error Category ID.
3019 public function wp_newCategory( $args ) {
3020 $this->escape( $args );
3022 $username = $args[1];
3023 $password = $args[2];
3024 $category = $args[3];
3026 if ( !$user = $this->login($username, $password) )
3027 return $this->error;
3029 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3030 do_action( 'xmlrpc_call', 'wp.newCategory' );
3032 // Make sure the user is allowed to add a category.
3033 if ( !current_user_can('manage_categories') )
3034 return new IXR_Error(401, __('Sorry, you do not have the right to add a category.'));
3036 // If no slug was provided make it empty so that
3037 // WordPress will generate one.
3038 if ( empty($category['slug']) )
3039 $category['slug'] = '';
3041 // If no parent_id was provided make it empty
3042 // so that it will be a top level page (no parent).
3043 if ( !isset($category['parent_id']) )
3044 $category['parent_id'] = '';
3046 // If no description was provided make it empty.
3047 if ( empty($category["description"]) )
3048 $category["description"] = "";
3050 $new_category = array(
3051 'cat_name' => $category['name'],
3052 'category_nicename' => $category['slug'],
3053 'category_parent' => $category['parent_id'],
3054 'category_description' => $category['description']
3057 $cat_id = wp_insert_category($new_category, true);
3058 if ( is_wp_error( $cat_id ) ) {
3059 if ( 'term_exists' == $cat_id->get_error_code() )
3060 return (int) $cat_id->get_error_data();
3062 return new IXR_Error(500, __('Sorry, the new category failed.'));
3063 } elseif ( ! $cat_id ) {
3064 return new IXR_Error(500, __('Sorry, the new category failed.'));
3068 * Fires after a new category has been successfully created via XML-RPC.
3072 * @param int $cat_id ID of the new category.
3073 * @param array $args An array of new category arguments.
3075 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
3085 * @param array $args {
3086 * Method arguments. Note: arguments must be ordered as documented.
3088 * @type int $blog_id (unused)
3089 * @type string $username
3090 * @type string $password
3091 * @type int $category_id
3093 * @return bool|IXR_Error See {@link wp_delete_term()} for return info.
3095 public function wp_deleteCategory( $args ) {
3096 $this->escape( $args );
3098 $username = $args[1];
3099 $password = $args[2];
3100 $category_id = (int) $args[3];
3102 if ( !$user = $this->login($username, $password) )
3103 return $this->error;
3105 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3106 do_action( 'xmlrpc_call', 'wp.deleteCategory' );
3108 if ( !current_user_can('manage_categories') )
3109 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
3111 $status = wp_delete_term( $category_id, 'category' );
3113 if ( true == $status ) {
3115 * Fires after a category has been successfully deleted via XML-RPC.
3119 * @param int $category_id ID of the deleted category.
3120 * @param array $args An array of arguments to delete the category.
3122 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
3129 * Retrieve category list.
3133 * @param array $args {
3134 * Method arguments. Note: arguments must be ordered as documented.
3136 * @type int $blog_id (unused)
3137 * @type string $username
3138 * @type string $password
3139 * @type array $category
3140 * @type int $max_results
3142 * @return array|IXR_Error
3144 public function wp_suggestCategories( $args ) {
3145 $this->escape( $args );
3147 $username = $args[1];
3148 $password = $args[2];
3149 $category = $args[3];
3150 $max_results = (int) $args[4];
3152 if ( !$user = $this->login($username, $password) )
3153 return $this->error;
3155 if ( !current_user_can( 'edit_posts' ) )
3156 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
3158 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3159 do_action( 'xmlrpc_call', 'wp.suggestCategories' );
3161 $category_suggestions = array();
3162 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
3163 foreach ( (array) get_categories($args) as $cat ) {
3164 $category_suggestions[] = array(
3165 'category_id' => $cat->term_id,
3166 'category_name' => $cat->name
3170 return $category_suggestions;
3178 * @param array $args {
3179 * Method arguments. Note: arguments must be ordered as documented.
3181 * @type int $blog_id (unused)
3182 * @type string $username
3183 * @type string $password
3184 * @type int $comment_id
3186 * @return array|IXR_Error
3188 public function wp_getComment($args) {
3189 $this->escape($args);
3191 $username = $args[1];
3192 $password = $args[2];
3193 $comment_id = (int) $args[3];
3195 if ( ! $user = $this->login( $username, $password ) ) {
3196 return $this->error;
3199 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3200 do_action( 'xmlrpc_call', 'wp.getComment' );
3202 if ( ! $comment = get_comment( $comment_id ) ) {
3203 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3206 if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
3207 return new IXR_Error( 403, __( 'You are not allowed to moderate or edit this comment.' ) );
3210 return $this->_prepare_comment( $comment );
3214 * Retrieve comments.
3216 * Besides the common blog_id (unused), username, and password arguments, it takes a filter
3217 * array as last argument.
3219 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
3221 * The defaults are as follows:
3222 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
3223 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
3224 * - 'number' - Default is 10. Total number of media items to retrieve.
3225 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
3229 * @param array $args {
3230 * Method arguments. Note: arguments must be ordered as documented.
3232 * @type int $blog_id (unused)
3233 * @type string $username
3234 * @type string $password
3235 * @type array $struct
3237 * @return array|IXR_Error Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
3239 public function wp_getComments( $args ) {
3240 $this->escape( $args );
3242 $username = $args[1];
3243 $password = $args[2];
3244 $struct = isset( $args[3] ) ? $args[3] : array();
3246 if ( ! $user = $this->login( $username, $password ) ) {
3247 return $this->error;
3250 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3251 do_action( 'xmlrpc_call', 'wp.getComments' );
3253 if ( isset( $struct['status'] ) ) {
3254 $status = $struct['status'];
3259 if ( ! current_user_can( 'moderate_comments' ) && 'approve' !== $status ) {
3260 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
3264 if ( isset( $struct['post_id'] ) ) {
3265 $post_id = absint( $struct['post_id'] );
3269 if ( isset( $struct['post_type'] ) ) {
3270 $post_type_object = get_post_type_object( $struct['post_type'] );
3271 if ( ! $post_type_object || ! post_type_supports( $post_type_object->name, 'comments' ) ) {
3272 return new IXR_Error( 404, __( 'Invalid post type.' ) );
3274 $post_type = $struct['post_type'];
3278 if ( isset( $struct['offset'] ) ) {
3279 $offset = absint( $struct['offset'] );
3283 if ( isset( $struct['number'] ) ) {
3284 $number = absint( $struct['number'] );
3287 $comments = get_comments( array(
3288 'status' => $status,
3289 'post_id' => $post_id,
3290 'offset' => $offset,
3291 'number' => $number,
3292 'post_type' => $post_type,
3295 $comments_struct = array();
3296 if ( is_array( $comments ) ) {
3297 foreach ( $comments as $comment ) {
3298 $comments_struct[] = $this->_prepare_comment( $comment );
3302 return $comments_struct;
3308 * By default, the comment will be moved to the trash instead of deleted.
3309 * See {@link wp_delete_comment()} for more information on
3314 * @param array $args {
3315 * Method arguments. Note: arguments must be ordered as documented.
3317 * @type int $blog_id (unused)
3318 * @type string $username
3319 * @type string $password
3320 * @type int $comment_ID
3322 * @return bool|IXR_Error {@link wp_delete_comment()}
3324 public function wp_deleteComment( $args ) {
3325 $this->escape($args);
3327 $username = $args[1];
3328 $password = $args[2];
3329 $comment_ID = (int) $args[3];
3331 if ( ! $user = $this->login( $username, $password ) ) {
3332 return $this->error;
3335 if ( ! get_comment( $comment_ID ) ) {
3336 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3339 if ( !current_user_can( 'edit_comment', $comment_ID ) ) {
3340 return new IXR_Error( 403, __( 'You are not allowed to moderate or edit this comment.' ) );
3343 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3344 do_action( 'xmlrpc_call', 'wp.deleteComment' );
3346 $status = wp_delete_comment( $comment_ID );
3350 * Fires after a comment has been successfully deleted via XML-RPC.
3354 * @param int $comment_ID ID of the deleted comment.
3355 * @param array $args An array of arguments to delete the comment.
3357 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
3366 * Besides the common blog_id (unused), username, and password arguments, it takes a
3367 * comment_id integer and a content_struct array as last argument.
3369 * The allowed keys in the content_struct array are:
3374 * - 'date_created_gmt'
3375 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See get_comment_statuses() for more details
3379 * @param array $args {
3380 * Method arguments. Note: arguments must be ordered as documented.
3382 * @type int $blog_id (unused)
3383 * @type string $username
3384 * @type string $password
3385 * @type int $comment_ID
3386 * @type array $content_struct
3388 * @return true|IXR_Error True, on success.
3390 public function wp_editComment( $args ) {
3391 $this->escape( $args );
3393 $username = $args[1];
3394 $password = $args[2];
3395 $comment_ID = (int) $args[3];
3396 $content_struct = $args[4];
3398 if ( !$user = $this->login( $username, $password ) ) {
3399 return $this->error;
3402 if ( ! get_comment( $comment_ID ) ) {
3403 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3406 if ( ! current_user_can( 'edit_comment', $comment_ID ) ) {
3407 return new IXR_Error( 403, __( 'You are not allowed to moderate or edit this comment.' ) );
3410 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3411 do_action( 'xmlrpc_call', 'wp.editComment' );
3413 if ( isset($content_struct['status']) ) {
3414 $statuses = get_comment_statuses();
3415 $statuses = array_keys($statuses);
3417 if ( ! in_array($content_struct['status'], $statuses) )
3418 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
3419 $comment_approved = $content_struct['status'];
3422 // Do some timestamp voodoo
3423 if ( !empty( $content_struct['date_created_gmt'] ) ) {
3424 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
3425 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
3426 $comment_date = iso8601_to_datetime( $dateCreated );
3427 $comment_date_gmt = get_gmt_from_date( $comment_date );
3430 if ( isset($content_struct['content']) )
3431 $comment_content = $content_struct['content'];
3433 if ( isset($content_struct['author']) )
3434 $comment_author = $content_struct['author'];
3436 if ( isset($content_struct['author_url']) )
3437 $comment_author_url = $content_struct['author_url'];
3439 if ( isset($content_struct['author_email']) )
3440 $comment_author_email = $content_struct['author_email'];
3442 // We've got all the data -- post it:
3443 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
3445 $result = wp_update_comment($comment);
3446 if ( is_wp_error( $result ) )
3447 return new IXR_Error(500, $result->get_error_message());
3450 return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
3453 * Fires after a comment has been successfully updated via XML-RPC.
3457 * @param int $comment_ID ID of the updated comment.
3458 * @param array $args An array of arguments to update the comment.
3460 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
3466 * Create new comment.
3470 * @param array $args {
3471 * Method arguments. Note: arguments must be ordered as documented.
3473 * @type int $blog_id (unused)
3474 * @type string $username
3475 * @type string $password
3476 * @type string|int $post
3477 * @type array $content_struct
3479 * @return int|IXR_Error {@link wp_new_comment()}
3481 public function wp_newComment($args) {
3482 $this->escape($args);
3484 $username = $args[1];
3485 $password = $args[2];
3487 $content_struct = $args[4];
3490 * Filter whether to allow anonymous comments over XML-RPC.
3494 * @param bool $allow Whether to allow anonymous commenting via XML-RPC.
3497 $allow_anon = apply_filters( 'xmlrpc_allow_anonymous_comments', false );
3499 $user = $this->login($username, $password);
3503 if ( $allow_anon && get_option('comment_registration') ) {
3504 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
3505 } elseif ( ! $allow_anon ) {
3506 return $this->error;
3512 if ( is_numeric($post) )
3513 $post_id = absint($post);
3515 $post_id = url_to_postid($post);
3518 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3521 if ( ! get_post( $post_id ) ) {
3522 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3525 if ( ! comments_open( $post_id ) ) {
3526 return new IXR_Error( 403, __( 'Sorry, comments are closed for this item.' ) );
3530 $comment['comment_post_ID'] = $post_id;
3533 $display_name = $user->display_name;
3534 $user_email = $user->user_email;
3535 $user_url = $user->user_url;
3537 $comment['comment_author'] = $this->escape( $display_name );
3538 $comment['comment_author_email'] = $this->escape( $user_email );
3539 $comment['comment_author_url'] = $this->escape( $user_url );
3540 $comment['user_ID'] = $user->ID;
3542 $comment['comment_author'] = '';
3543 if ( isset($content_struct['author']) )
3544 $comment['comment_author'] = $content_struct['author'];
3546 $comment['comment_author_email'] = '';
3547 if ( isset($content_struct['author_email']) )
3548 $comment['comment_author_email'] = $content_struct['author_email'];
3550 $comment['comment_author_url'] = '';
3551 if ( isset($content_struct['author_url']) )
3552 $comment['comment_author_url'] = $content_struct['author_url'];
3554 $comment['user_ID'] = 0;
3556 if ( get_option('require_name_email') ) {
3557 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
3558 return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
3559 elseif ( !is_email($comment['comment_author_email']) )
3560 return new IXR_Error( 403, __( 'A valid email address is required' ) );
3564 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
3566 $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
3568 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3569 do_action( 'xmlrpc_call', 'wp.newComment' );
3571 $comment_ID = wp_new_comment( $comment );
3574 * Fires after a new comment has been successfully created via XML-RPC.
3578 * @param int $comment_ID ID of the new comment.
3579 * @param array $args An array of new comment arguments.
3581 do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
3587 * Retrieve all of the comment status.
3591 * @param array $args {
3592 * Method arguments. Note: arguments must be ordered as documented.
3594 * @type int $blog_id (unused)
3595 * @type string $username
3596 * @type string $password
3598 * @return array|IXR_Error
3600 public function wp_getCommentStatusList( $args ) {
3601 $this->escape( $args );
3603 $username = $args[1];
3604 $password = $args[2];
3606 if ( ! $user = $this->login( $username, $password ) ) {
3607 return $this->error;
3610 if ( ! current_user_can( 'publish_posts' ) ) {
3611 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3614 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3615 do_action( 'xmlrpc_call', 'wp.getCommentStatusList' );
3617 return get_comment_statuses();
3621 * Retrieve comment count.
3625 * @param array $args {
3626 * Method arguments. Note: arguments must be ordered as documented.
3628 * @type int $blog_id (unused)
3629 * @type string $username
3630 * @type string $password
3631 * @type int $post_id
3633 * @return array|IXR_Error
3635 public function wp_getCommentCount( $args ) {
3636 $this->escape( $args );
3638 $username = $args[1];
3639 $password = $args[2];
3640 $post_id = (int) $args[3];
3642 if ( ! $user = $this->login( $username, $password ) ) {
3643 return $this->error;
3646 $post = get_post( $post_id, ARRAY_A );
3647 if ( empty( $post['ID'] ) ) {
3648 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3651 if ( ! current_user_can( 'edit_post', $post_id ) ) {
3652 return new IXR_Error( 403, __( 'You are not allowed access to details of this post.' ) );
3655 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3656 do_action( 'xmlrpc_call', 'wp.getCommentCount' );
3658 $count = wp_count_comments( $post_id );
3661 'approved' => $count->approved,
3662 'awaiting_moderation' => $count->moderated,
3663 'spam' => $count->spam,
3664 'total_comments' => $count->total_comments
3669 * Retrieve post statuses.
3673 * @param array $args {
3674 * Method arguments. Note: arguments must be ordered as documented.
3676 * @type int $blog_id (unused)
3677 * @type string $username
3678 * @type string $password
3680 * @return array|IXR_Error
3682 public function wp_getPostStatusList( $args ) {
3683 $this->escape( $args );
3685 $username = $args[1];
3686 $password = $args[2];
3688 if ( !$user = $this->login($username, $password) )
3689 return $this->error;
3691 if ( !current_user_can( 'edit_posts' ) )
3692 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3694 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3695 do_action( 'xmlrpc_call', 'wp.getPostStatusList' );
3697 return get_post_statuses();
3701 * Retrieve page statuses.
3705 * @param array $args {
3706 * Method arguments. Note: arguments must be ordered as documented.
3708 * @type int $blog_id (unused)
3709 * @type string $username
3710 * @type string $password
3712 * @return array|IXR_Error
3714 public function wp_getPageStatusList( $args ) {
3715 $this->escape( $args );
3717 $username = $args[1];
3718 $password = $args[2];
3720 if ( !$user = $this->login($username, $password) )
3721 return $this->error;
3723 if ( !current_user_can( 'edit_pages' ) )
3724 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3726 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3727 do_action( 'xmlrpc_call', 'wp.getPageStatusList' );
3729 return get_page_statuses();
3733 * Retrieve page templates.
3737 * @param array $args {
3738 * Method arguments. Note: arguments must be ordered as documented.
3740 * @type int $blog_id (unused)
3741 * @type string $username
3742 * @type string $password
3744 * @return array|IXR_Error
3746 public function wp_getPageTemplates( $args ) {
3747 $this->escape( $args );
3749 $username = $args[1];
3750 $password = $args[2];
3752 if ( !$user = $this->login($username, $password) )
3753 return $this->error;
3755 if ( !current_user_can( 'edit_pages' ) )
3756 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3758 $templates = get_page_templates();
3759 $templates['Default'] = 'default';
3765 * Retrieve blog options.
3769 * @param array $args {
3770 * Method arguments. Note: arguments must be ordered as documented.
3772 * @type int $blog_id (unused)
3773 * @type string $username
3774 * @type string $password
3775 * @type array $options
3777 * @return array|IXR_Error
3779 public function wp_getOptions( $args ) {
3780 $this->escape( $args );
3782 $username = $args[1];
3783 $password = $args[2];
3784 $options = isset( $args[3] ) ? (array) $args[3] : array();
3786 if ( !$user = $this->login($username, $password) )
3787 return $this->error;
3789 // If no specific options where asked for, return all of them
3790 if ( count( $options ) == 0 )
3791 $options = array_keys($this->blog_options);
3793 return $this->_getOptions($options);
3797 * Retrieve blog options value from list.
3801 * @param array $options Options to retrieve.
3804 public function _getOptions($options) {
3806 $can_manage = current_user_can( 'manage_options' );
3807 foreach ( $options as $option ) {
3808 if ( array_key_exists( $option, $this->blog_options ) ) {
3809 $data[$option] = $this->blog_options[$option];
3810 //Is the value static or dynamic?
3811 if ( isset( $data[$option]['option'] ) ) {
3812 $data[$option]['value'] = get_option( $data[$option]['option'] );
3813 unset($data[$option]['option']);
3816 if ( ! $can_manage )
3817 $data[$option]['readonly'] = true;
3825 * Update blog options.
3829 * @param array $args {
3830 * Method arguments. Note: arguments must be ordered as documented.
3832 * @type int $blog_id (unused)
3833 * @type string $username
3834 * @type string $password
3835 * @type array $options
3837 * @return array|IXR_Error
3839 public function wp_setOptions( $args ) {
3840 $this->escape( $args );
3842 $username = $args[1];
3843 $password = $args[2];
3844 $options = (array) $args[3];
3846 if ( !$user = $this->login($username, $password) )
3847 return $this->error;
3849 if ( !current_user_can( 'manage_options' ) )
3850 return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
3852 $option_names = array();
3853 foreach ( $options as $o_name => $o_value ) {
3854 $option_names[] = $o_name;
3855 if ( !array_key_exists( $o_name, $this->blog_options ) )
3858 if ( $this->blog_options[$o_name]['readonly'] == true )
3861 update_option( $this->blog_options[$o_name]['option'], wp_unslash( $o_value ) );
3864 //Now return the updated values
3865 return $this->_getOptions($option_names);
3869 * Retrieve a media item by ID
3873 * @param array $args {
3874 * Method arguments. Note: arguments must be ordered as documented.
3876 * @type int $blog_id (unused)
3877 * @type string $username
3878 * @type string $password
3879 * @type int $attachment_id
3881 * @return array|IXR_Error Associative array contains:
3882 * - 'date_created_gmt'
3891 public function wp_getMediaItem( $args ) {
3892 $this->escape( $args );
3894 $username = $args[1];
3895 $password = $args[2];
3896 $attachment_id = (int) $args[3];
3898 if ( !$user = $this->login($username, $password) )
3899 return $this->error;
3901 if ( !current_user_can( 'upload_files' ) )
3902 return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
3904 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3905 do_action( 'xmlrpc_call', 'wp.getMediaItem' );
3907 if ( ! $attachment = get_post($attachment_id) )
3908 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
3910 return $this->_prepare_media_item( $attachment );
3914 * Retrieves a collection of media library items (or attachments)
3916 * Besides the common blog_id (unused), username, and password arguments, it takes a filter
3917 * array as last argument.
3919 * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
3921 * The defaults are as follows:
3922 * - 'number' - Default is 5. Total number of media items to retrieve.
3923 * - 'offset' - Default is 0. See WP_Query::query() for more.
3924 * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
3925 * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
3929 * @param array $args {
3930 * Method arguments. Note: arguments must be ordered as documented.
3932 * @type int $blog_id (unused)
3933 * @type string $username
3934 * @type string $password
3935 * @type array $struct
3937 * @return array|IXR_Error Contains a collection of media items. See wp_xmlrpc_server::wp_getMediaItem() for a description of each item contents
3939 public function wp_getMediaLibrary($args) {
3940 $this->escape($args);
3942 $username = $args[1];
3943 $password = $args[2];
3944 $struct = isset( $args[3] ) ? $args[3] : array() ;
3946 if ( !$user = $this->login($username, $password) )
3947 return $this->error;
3949 if ( !current_user_can( 'upload_files' ) )
3950 return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
3952 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3953 do_action( 'xmlrpc_call', 'wp.getMediaLibrary' );
3955 $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
3956 $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
3957 $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
3958 $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
3960 $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
3962 $attachments_struct = array();
3964 foreach ($attachments as $attachment )
3965 $attachments_struct[] = $this->_prepare_media_item( $attachment );
3967 return $attachments_struct;
3971 * Retrieves a list of post formats used by the site.
3975 * @param array $args {
3976 * Method arguments. Note: arguments must be ordered as documented.
3978 * @type int $blog_id (unused)
3979 * @type string $username
3980 * @type string $password
3982 * @return array|IXR_Error List of post formats, otherwise IXR_Error object.
3984 public function wp_getPostFormats( $args ) {
3985 $this->escape( $args );
3987 $username = $args[1];
3988 $password = $args[2];
3990 if ( !$user = $this->login( $username, $password ) )
3991 return $this->error;
3993 if ( !current_user_can( 'edit_posts' ) )
3994 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3996 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3997 do_action( 'xmlrpc_call', 'wp.getPostFormats' );
3999 $formats = get_post_format_strings();
4001 // find out if they want a list of currently supports formats
4002 if ( isset( $args[3] ) && is_array( $args[3] ) ) {
4003 if ( $args[3]['show-supported'] ) {
4004 if ( current_theme_supports( 'post-formats' ) ) {
4005 $supported = get_theme_support( 'post-formats' );
4008 $data['all'] = $formats;
4009 $data['supported'] = $supported[0];
4020 * Retrieves a post type
4024 * @see get_post_type_object()
4026 * @param array $args {
4027 * Method arguments. Note: arguments must be ordered as documented.
4029 * @type int $blog_id (unused)
4030 * @type string $username
4031 * @type string $password
4032 * @type string $post_type_name
4033 * @type array $fields (optional)
4035 * @return array|IXR_Error Array contains:
4038 * - 'capability_type'
4046 public function wp_getPostType( $args ) {
4047 if ( ! $this->minimum_args( $args, 4 ) )
4048 return $this->error;
4050 $this->escape( $args );
4052 $username = $args[1];
4053 $password = $args[2];
4054 $post_type_name = $args[3];
4056 if ( isset( $args[4] ) ) {
4060 * Filter the default query fields used by the given XML-RPC method.
4064 * @param array $fields An array of post type query fields for the given method.
4065 * @param string $method The method name.
4067 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
4070 if ( !$user = $this->login( $username, $password ) )
4071 return $this->error;
4073 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4074 do_action( 'xmlrpc_call', 'wp.getPostType' );
4076 if ( ! post_type_exists( $post_type_name ) )
4077 return new IXR_Error( 403, __( 'Invalid post type' ) );
4079 $post_type = get_post_type_object( $post_type_name );
4081 if ( ! current_user_can( $post_type->cap->edit_posts ) )
4082 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
4084 return $this->_prepare_post_type( $post_type, $fields );
4088 * Retrieves a post types
4092 * @see get_post_types()
4094 * @param array $args {
4095 * Method arguments. Note: arguments must be ordered as documented.
4097 * @type int $blog_id (unused)
4098 * @type string $username
4099 * @type string $password
4100 * @type array $filter (optional)
4101 * @type array $fields (optional)
4103 * @return array|IXR_Error
4105 public function wp_getPostTypes( $args ) {
4106 if ( ! $this->minimum_args( $args, 3 ) )
4107 return $this->error;
4109 $this->escape( $args );
4111 $username = $args[1];
4112 $password = $args[2];
4113 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
4115 if ( isset( $args[4] ) ) {
4118 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4119 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
4122 if ( ! $user = $this->login( $username, $password ) )
4123 return $this->error;
4125 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4126 do_action( 'xmlrpc_call', 'wp.getPostTypes' );
4128 $post_types = get_post_types( $filter, 'objects' );
4132 foreach ( $post_types as $post_type ) {
4133 if ( ! current_user_can( $post_type->cap->edit_posts ) )
4136 $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
4143 * Retrieve revisions for a specific post.
4147 * The optional $fields parameter specifies what fields will be included
4148 * in the response array.
4150 * @uses wp_get_post_revisions()
4151 * @see wp_getPost() for more on $fields
4153 * @param array $args {
4154 * Method arguments. Note: arguments must be ordered as documented.
4156 * @type int $blog_id (unused)
4157 * @type string $username
4158 * @type string $password
4159 * @type int $post_id
4160 * @type array $fields (optional)
4162 * @return array|IXR_Error contains a collection of posts.
4164 public function wp_getRevisions( $args ) {
4165 if ( ! $this->minimum_args( $args, 4 ) )
4166 return $this->error;
4168 $this->escape( $args );
4170 $username = $args[1];
4171 $password = $args[2];
4172 $post_id = (int) $args[3];
4174 if ( isset( $args[4] ) ) {
4178 * Filter the default revision query fields used by the given XML-RPC method.
4182 * @param array $field An array of revision query fields.
4183 * @param string $method The method name.
4185 $fields = apply_filters( 'xmlrpc_default_revision_fields', array( 'post_date', 'post_date_gmt' ), 'wp.getRevisions' );
4188 if ( ! $user = $this->login( $username, $password ) )
4189 return $this->error;
4191 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4192 do_action( 'xmlrpc_call', 'wp.getRevisions' );
4194 if ( ! $post = get_post( $post_id ) )
4195 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4197 if ( ! current_user_can( 'edit_post', $post_id ) )
4198 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
4200 // Check if revisions are enabled.
4201 if ( ! wp_revisions_enabled( $post ) )
4202 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
4204 $revisions = wp_get_post_revisions( $post_id );
4211 foreach ( $revisions as $revision ) {
4212 if ( ! current_user_can( 'read_post', $revision->ID ) )
4216 if ( wp_is_post_autosave( $revision ) )
4219 $struct[] = $this->_prepare_post( get_object_vars( $revision ), $fields );
4226 * Restore a post revision
4230 * @uses wp_restore_post_revision()
4232 * @param array $args {
4233 * Method arguments. Note: arguments must be ordered as documented.
4235 * @type int $blog_id (unused)
4236 * @type string $username
4237 * @type string $password
4238 * @type int $revision_id
4240 * @return bool|IXR_Error false if there was an error restoring, true if success.
4242 public function wp_restoreRevision( $args ) {
4243 if ( ! $this->minimum_args( $args, 3 ) )
4244 return $this->error;
4246 $this->escape( $args );
4248 $username = $args[1];
4249 $password = $args[2];
4250 $revision_id = (int) $args[3];
4252 if ( ! $user = $this->login( $username, $password ) )
4253 return $this->error;
4255 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4256 do_action( 'xmlrpc_call', 'wp.restoreRevision' );
4258 if ( ! $revision = wp_get_post_revision( $revision_id ) )
4259 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4261 if ( wp_is_post_autosave( $revision ) )
4262 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4264 if ( ! $post = get_post( $revision->post_parent ) )
4265 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4267 if ( ! current_user_can( 'edit_post', $revision->post_parent ) )
4268 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4270 // Check if revisions are disabled.
4271 if ( ! wp_revisions_enabled( $post ) )
4272 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
4274 $post = wp_restore_post_revision( $revision_id );
4276 return (bool) $post;
4279 /* Blogger API functions.
4280 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
4284 * Retrieve blogs that user owns.
4286 * Will make more sense once we support multiple blogs.
4290 * @param array $args {
4291 * Method arguments. Note: arguments must be ordered as documented.
4293 * @type int $blog_id (unused)
4294 * @type string $username
4295 * @type string $password
4297 * @return array|IXR_Error
4299 public function blogger_getUsersBlogs($args) {
4300 if ( is_multisite() )
4301 return $this->_multisite_getUsersBlogs($args);
4303 $this->escape($args);
4305 $username = $args[1];
4306 $password = $args[2];
4308 if ( !$user = $this->login($username, $password) )
4309 return $this->error;
4311 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4312 do_action( 'xmlrpc_call', 'blogger.getUsersBlogs' );
4314 $is_admin = current_user_can('manage_options');
4317 'isAdmin' => $is_admin,
4318 'url' => get_option('home') . '/',
4320 'blogName' => get_option('blogname'),
4321 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
4324 return array($struct);
4328 * Private function for retrieving a users blogs for multisite setups
4332 * @return array|IXR_Error
4334 protected function _multisite_getUsersBlogs($args) {
4335 $current_blog = get_blog_details();
4337 $domain = $current_blog->domain;
4338 $path = $current_blog->path . 'xmlrpc.php';
4340 $rpc = new IXR_Client( set_url_scheme( "http://{$domain}{$path}" ) );
4341 $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
4342 $blogs = $rpc->getResponse();
4344 if ( isset($blogs['faultCode']) )
4345 return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
4347 if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
4350 foreach ( (array) $blogs as $blog ) {
4351 if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
4352 return array($blog);
4359 * Retrieve user's data.
4361 * Gives your client some info about you, so you don't have to.
4365 * @param array $args {
4366 * Method arguments. Note: arguments must be ordered as documented.
4368 * @type int $blog_id (unused)
4369 * @type string $username
4370 * @type string $password
4372 * @return array|IXR_Error
4374 public function blogger_getUserInfo( $args ) {
4375 $this->escape( $args );
4377 $username = $args[1];
4378 $password = $args[2];
4380 if ( !$user = $this->login($username, $password) )
4381 return $this->error;
4383 if ( !current_user_can( 'edit_posts' ) )
4384 return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
4386 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4387 do_action( 'xmlrpc_call', 'blogger.getUserInfo' );
4390 'nickname' => $user->nickname,
4391 'userid' => $user->ID,
4392 'url' => $user->user_url,
4393 'lastname' => $user->last_name,
4394 'firstname' => $user->first_name
4405 * @param array $args {
4406 * Method arguments. Note: arguments must be ordered as documented.
4408 * @type int $blog_id (unused)
4409 * @type int $post_ID
4410 * @type string $username
4411 * @type string $password
4413 * @return array|IXR_Error
4415 public function blogger_getPost( $args ) {
4416 $this->escape( $args );
4418 $post_ID = (int) $args[1];
4419 $username = $args[2];
4420 $password = $args[3];
4422 if ( !$user = $this->login($username, $password) )
4423 return $this->error;
4425 $post_data = get_post($post_ID, ARRAY_A);
4427 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4429 if ( !current_user_can( 'edit_post', $post_ID ) )
4430 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4432 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4433 do_action( 'xmlrpc_call', 'blogger.getPost' );
4435 $categories = implode(',', wp_get_post_categories($post_ID));
4437 $content = '<title>'.wp_unslash($post_data['post_title']).'</title>';
4438 $content .= '<category>'.$categories.'</category>';
4439 $content .= wp_unslash($post_data['post_content']);
4442 'userid' => $post_data['post_author'],
4443 'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
4444 'content' => $content,
4445 'postid' => (string) $post_data['ID']
4452 * Retrieve list of recent posts.
4456 * @param array $args {
4457 * Method arguments. Note: arguments must be ordered as documented.
4459 * @type string $appkey (unused)
4460 * @type int $blog_id (unused)
4461 * @type string $username
4462 * @type string $password
4463 * @type int $numberposts (optional)
4465 * @return array|IXR_Error
4467 public function blogger_getRecentPosts( $args ) {
4469 $this->escape($args);
4471 // $args[0] = appkey - ignored
4472 $username = $args[2];
4473 $password = $args[3];
4474 if ( isset( $args[4] ) )
4475 $query = array( 'numberposts' => absint( $args[4] ) );
4479 if ( !$user = $this->login($username, $password) )
4480 return $this->error;
4482 if ( ! current_user_can( 'edit_posts' ) )
4483 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
4485 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4486 do_action( 'xmlrpc_call', 'blogger.getRecentPosts' );
4488 $posts_list = wp_get_recent_posts( $query );
4490 if ( !$posts_list ) {
4491 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
4492 return $this->error;
4495 $recent_posts = array();
4496 foreach ($posts_list as $entry) {
4497 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
4500 $post_date = $this->_convert_date( $entry['post_date'] );
4501 $categories = implode(',', wp_get_post_categories($entry['ID']));
4503 $content = '<title>'.wp_unslash($entry['post_title']).'</title>';
4504 $content .= '<category>'.$categories.'</category>';
4505 $content .= wp_unslash($entry['post_content']);
4507 $recent_posts[] = array(
4508 'userid' => $entry['post_author'],
4509 'dateCreated' => $post_date,
4510 'content' => $content,
4511 'postid' => (string) $entry['ID'],
4515 return $recent_posts;
4525 public function blogger_getTemplate($args) {
4526 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4536 public function blogger_setTemplate($args) {
4537 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4545 * @param array $args {
4546 * Method arguments. Note: arguments must be ordered as documented.
4548 * @type string $appkey (unused)
4549 * @type int $blog_id (unused)
4550 * @type string $username
4551 * @type string $password
4552 * @type string $content
4553 * @type string $publish
4555 * @return int|IXR_Error
4557 public function blogger_newPost( $args ) {
4558 $this->escape( $args );
4560 $username = $args[2];
4561 $password = $args[3];
4562 $content = $args[4];
4563 $publish = $args[5];
4565 if ( !$user = $this->login($username, $password) )
4566 return $this->error;
4568 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4569 do_action( 'xmlrpc_call', 'blogger.newPost' );
4571 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
4572 if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) || !current_user_can($cap) )
4573 return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
4575 $post_status = ($publish) ? 'publish' : 'draft';
4577 $post_author = $user->ID;
4579 $post_title = xmlrpc_getposttitle($content);
4580 $post_category = xmlrpc_getpostcategory($content);
4581 $post_content = xmlrpc_removepostdata($content);
4583 $post_date = current_time('mysql');
4584 $post_date_gmt = current_time('mysql', 1);
4586 $post_data = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
4588 $post_ID = wp_insert_post($post_data);
4589 if ( is_wp_error( $post_ID ) )
4590 return new IXR_Error(500, $post_ID->get_error_message());
4593 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4595 $this->attach_uploads( $post_ID, $post_content );
4598 * Fires after a new post has been successfully created via the XML-RPC Blogger API.
4602 * @param int $post_ID ID of the new post.
4603 * @param array $args An array of new post arguments.
4605 do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
4615 * @param array $args {
4616 * Method arguments. Note: arguments must be ordered as documented.
4618 * @type int $blog_id (unused)
4619 * @type int $post_ID
4620 * @type string $username
4621 * @type string $password
4622 * @type string $content
4623 * @type bool $publish
4625 * @return true|IXR_Error true when done.
4627 public function blogger_editPost( $args ) {
4629 $this->escape($args);
4631 $post_ID = (int) $args[1];
4632 $username = $args[2];
4633 $password = $args[3];
4634 $content = $args[4];
4635 $publish = $args[5];
4637 if ( ! $user = $this->login( $username, $password ) ) {
4638 return $this->error;
4641 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4642 do_action( 'xmlrpc_call', 'blogger.editPost' );
4644 $actual_post = get_post( $post_ID, ARRAY_A );
4646 if ( ! $actual_post || $actual_post['post_type'] != 'post' ) {
4647 return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
4650 $this->escape($actual_post);
4652 if ( ! current_user_can( 'edit_post', $post_ID ) ) {
4653 return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
4655 if ( 'publish' == $actual_post['post_status'] && ! current_user_can( 'publish_posts' ) ) {
4656 return new IXR_Error( 401, __( 'Sorry, you do not have the right to publish this post.' ) );
4659 $postdata = array();
4660 $postdata['ID'] = $actual_post['ID'];
4661 $postdata['post_content'] = xmlrpc_removepostdata( $content );
4662 $postdata['post_title'] = xmlrpc_getposttitle( $content );
4663 $postdata['post_category'] = xmlrpc_getpostcategory( $content );
4664 $postdata['post_status'] = $actual_post['post_status'];
4665 $postdata['post_excerpt'] = $actual_post['post_excerpt'];
4666 $postdata['post_status'] = $publish ? 'publish' : 'draft';
4668 $result = wp_update_post( $postdata );
4671 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
4673 $this->attach_uploads( $actual_post['ID'], $postdata['post_content'] );
4676 * Fires after a post has been successfully updated via the XML-RPC Blogger API.
4680 * @param int $post_ID ID of the updated post.
4681 * @param array $args An array of arguments for the post to edit.
4683 do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
4693 * @param array $args {
4694 * Method arguments. Note: arguments must be ordered as documented.
4696 * @type int $blog_id (unused)
4697 * @type int $post_ID
4698 * @type string $username
4699 * @type string $password
4701 * @return true|IXR_Error True when post is deleted.
4703 public function blogger_deletePost( $args ) {
4704 $this->escape( $args );
4706 $post_ID = (int) $args[1];
4707 $username = $args[2];
4708 $password = $args[3];
4710 if ( !$user = $this->login($username, $password) )
4711 return $this->error;
4713 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4714 do_action( 'xmlrpc_call', 'blogger.deletePost' );
4716 $actual_post = get_post( $post_ID, ARRAY_A );
4718 if ( ! $actual_post || $actual_post['post_type'] != 'post' ) {
4719 return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
4722 if ( ! current_user_can( 'delete_post', $post_ID ) ) {
4723 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete this post.' ) );
4726 $result = wp_delete_post( $post_ID );
4729 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
4733 * Fires after a post has been successfully deleted via the XML-RPC Blogger API.
4737 * @param int $post_ID ID of the deleted post.
4738 * @param array $args An array of arguments to delete the post.
4740 do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
4745 /* MetaWeblog API functions
4746 * specs on wherever Dave Winer wants them to be
4750 * Create a new post.
4752 * The 'content_struct' argument must contain:
4761 * Also, it can optionally contain:
4764 * - wp_page_parent_id
4767 * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
4768 * - mt_allow_comments - can be 'open' or 'closed'
4769 * - mt_allow_pings - can be 'open' or 'closed'
4770 * - date_created_gmt
4772 * - wp_post_thumbnail
4776 * @param array $args {
4777 * Method arguments. Note: arguments must be ordered as documented.
4779 * @type int $blog_id (unused)
4780 * @type string $username
4781 * @type string $password
4782 * @type array $content_struct
4783 * @type int $publish
4785 * @return int|IXR_Error
4787 public function mw_newPost($args) {
4788 $this->escape($args);
4790 $username = $args[1];
4791 $password = $args[2];
4792 $content_struct = $args[3];
4793 $publish = isset( $args[4] ) ? $args[4] : 0;
4795 if ( !$user = $this->login($username, $password) )
4796 return $this->error;
4798 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4799 do_action( 'xmlrpc_call', 'metaWeblog.newPost' );
4801 $page_template = '';
4802 if ( !empty( $content_struct['post_type'] ) ) {
4803 if ( $content_struct['post_type'] == 'page' ) {
4805 $cap = 'publish_pages';
4806 elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
4807 $cap = 'publish_pages';
4809 $cap = 'edit_pages';
4810 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
4811 $post_type = 'page';
4812 if ( !empty( $content_struct['wp_page_template'] ) )
4813 $page_template = $content_struct['wp_page_template'];
4814 } elseif ( $content_struct['post_type'] == 'post' ) {
4816 $cap = 'publish_posts';
4817 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
4818 $cap = 'publish_posts';
4820 $cap = 'edit_posts';
4821 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4822 $post_type = 'post';
4824 // No other post_type values are allowed here
4825 return new IXR_Error( 401, __( 'Invalid post type' ) );
4829 $cap = 'publish_posts';
4830 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
4831 $cap = 'publish_posts';
4833 $cap = 'edit_posts';
4834 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4835 $post_type = 'post';
4838 if ( ! current_user_can( get_post_type_object( $post_type )->cap->create_posts ) )
4839 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts on this site.' ) );
4840 if ( !current_user_can( $cap ) )
4841 return new IXR_Error( 401, $error_message );
4843 // Check for a valid post format if one was given
4844 if ( isset( $content_struct['wp_post_format'] ) ) {
4845 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4846 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4847 return new IXR_Error( 404, __( 'Invalid post format' ) );
4851 // Let WordPress generate the post_name (slug) unless
4852 // one has been provided.
4854 if ( isset($content_struct['wp_slug']) )
4855 $post_name = $content_struct['wp_slug'];
4857 // Only use a password if one was given.
4858 if ( isset($content_struct['wp_password']) )
4859 $post_password = $content_struct['wp_password'];
4861 // Only set a post parent if one was provided.
4862 if ( isset($content_struct['wp_page_parent_id']) )
4863 $post_parent = $content_struct['wp_page_parent_id'];
4865 // Only set the menu_order if it was provided.
4866 if ( isset($content_struct['wp_page_order']) )
4867 $menu_order = $content_struct['wp_page_order'];
4869 $post_author = $user->ID;
4871 // If an author id was provided then use it instead.
4872 if ( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) {
4873 switch ( $post_type ) {
4875 if ( !current_user_can( 'edit_others_posts' ) )
4876 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
4879 if ( !current_user_can( 'edit_others_pages' ) )
4880 return new IXR_Error( 401, __( 'You are not allowed to create pages as this user.' ) );
4883 return new IXR_Error( 401, __( 'Invalid post type' ) );
4885 $author = get_userdata( $content_struct['wp_author_id'] );
4887 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
4888 $post_author = $content_struct['wp_author_id'];
4891 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
4892 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
4894 $post_status = $publish ? 'publish' : 'draft';
4896 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4897 switch ( $content_struct["{$post_type}_status"] ) {
4902 $post_status = $content_struct["{$post_type}_status"];
4905 $post_status = $publish ? 'publish' : 'draft';
4910 $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
4911 $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
4913 $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
4915 if ( isset($content_struct['mt_allow_comments']) ) {
4916 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4917 switch ( $content_struct['mt_allow_comments'] ) {
4919 $comment_status = 'closed';
4922 $comment_status = 'open';
4925 $comment_status = get_default_comment_status( $post_type );
4929 switch ( (int) $content_struct['mt_allow_comments'] ) {
4932 $comment_status = 'closed';
4935 $comment_status = 'open';
4938 $comment_status = get_default_comment_status( $post_type );
4943 $comment_status = get_default_comment_status( $post_type );
4946 if ( isset($content_struct['mt_allow_pings']) ) {
4947 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4948 switch ( $content_struct['mt_allow_pings'] ) {
4950 $ping_status = 'closed';
4953 $ping_status = 'open';
4956 $ping_status = get_default_comment_status( $post_type, 'pingback' );
4960 switch ( (int) $content_struct['mt_allow_pings'] ) {
4962 $ping_status = 'closed';
4965 $ping_status = 'open';
4968 $ping_status = get_default_comment_status( $post_type, 'pingback' );
4973 $ping_status = get_default_comment_status( $post_type, 'pingback' );
4977 $post_content = $post_content . '<!--more-->' . $post_more;
4980 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4981 $to_ping = $content_struct['mt_tb_ping_urls'];
4982 if ( is_array($to_ping) )
4983 $to_ping = implode(' ', $to_ping);
4986 // Do some timestamp voodoo
4987 if ( !empty( $content_struct['date_created_gmt'] ) )
4988 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4989 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4990 elseif ( !empty( $content_struct['dateCreated']) )
4991 $dateCreated = $content_struct['dateCreated']->getIso();
4993 if ( !empty( $dateCreated ) ) {
4994 $post_date = iso8601_to_datetime( $dateCreated );
4995 $post_date_gmt = get_gmt_from_date( $post_date );
4998 $post_date_gmt = '';
5001 $post_category = array();
5002 if ( isset( $content_struct['categories'] ) ) {
5003 $catnames = $content_struct['categories'];
5005 if ( is_array($catnames) ) {
5006 foreach ($catnames as $cat) {
5007 $post_category[] = get_cat_ID($cat);
5012 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'to_ping', 'post_type', 'post_name', 'post_password', 'post_parent', 'menu_order', 'tags_input', 'page_template');
5014 $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
5016 // Only posts can be sticky
5017 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
5019 $data['sticky'] = $content_struct['sticky'];
5020 $error = $this->_toggle_sticky( $data );
5026 if ( isset($content_struct['custom_fields']) )
5027 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
5029 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
5030 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
5031 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
5033 unset( $content_struct['wp_post_thumbnail'] );
5036 // Handle enclosures
5037 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
5038 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
5040 $this->attach_uploads( $post_ID, $post_content );
5042 // Handle post formats if assigned, value is validated earlier
5044 if ( isset( $content_struct['wp_post_format'] ) )
5045 set_post_format( $post_ID, $content_struct['wp_post_format'] );
5047 $post_ID = wp_insert_post( $postdata, true );
5048 if ( is_wp_error( $post_ID ) )
5049 return new IXR_Error(500, $post_ID->get_error_message());
5052 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
5055 * Fires after a new post has been successfully created via the XML-RPC MovableType API.
5059 * @param int $post_ID ID of the new post.
5060 * @param array $args An array of arguments to create the new post.
5062 do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
5064 return strval($post_ID);
5068 * @param integer $post_ID
5069 * @param array $enclosure
5071 public function add_enclosure_if_new( $post_ID, $enclosure ) {
5072 if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
5073 $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'] . "\n";
5075 if ( $enclosures = get_post_meta( $post_ID, 'enclosure' ) ) {
5076 foreach ( $enclosures as $enc ) {
5077 // This method used to omit the trailing new line. #23219
5078 if ( rtrim( $enc, "\n" ) == rtrim( $encstring, "\n" ) ) {
5085 add_post_meta( $post_ID, 'enclosure', $encstring );
5090 * Attach upload to a post.
5094 * @global wpdb $wpdb WordPress database abstraction object.
5096 * @param int $post_ID Post ID.
5097 * @param string $post_content Post Content for attachment.
5099 public function attach_uploads( $post_ID, $post_content ) {
5102 // find any unattached files
5103 $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
5104 if ( is_array( $attachments ) ) {
5105 foreach ( $attachments as $file ) {
5106 if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false )
5107 $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
5117 * @param array $args {
5118 * Method arguments. Note: arguments must be ordered as documented.
5120 * @type int $blog_id (unused)
5121 * @type string $username
5122 * @type string $password
5123 * @type array $content_struct
5124 * @type int $publish
5126 * @return bool|IXR_Error True on success.
5128 public function mw_editPost( $args ) {
5129 $this->escape( $args );
5131 $post_ID = (int) $args[0];
5132 $username = $args[1];
5133 $password = $args[2];
5134 $content_struct = $args[3];
5135 $publish = isset( $args[4] ) ? $args[4] : 0;
5137 if ( ! $user = $this->login($username, $password) )
5138 return $this->error;
5140 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5141 do_action( 'xmlrpc_call', 'metaWeblog.editPost' );
5143 $postdata = get_post( $post_ID, ARRAY_A );
5146 * If there is no post data for the give post id, stop now and return an error.
5147 * Otherwise a new post will be created (which was the old behavior).
5149 if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
5150 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5152 if ( ! current_user_can( 'edit_post', $post_ID ) )
5153 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this post.' ) );
5155 // Use wp.editPost to edit post types other than post and page.
5156 if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
5157 return new IXR_Error( 401, __( 'Invalid post type' ) );
5159 // Thwart attempt to change the post type.
5160 if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
5161 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
5163 // Check for a valid post format if one was given
5164 if ( isset( $content_struct['wp_post_format'] ) ) {
5165 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
5166 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
5167 return new IXR_Error( 404, __( 'Invalid post format' ) );
5171 $this->escape($postdata);
5173 $ID = $postdata['ID'];
5174 $post_content = $postdata['post_content'];
5175 $post_title = $postdata['post_title'];
5176 $post_excerpt = $postdata['post_excerpt'];
5177 $post_password = $postdata['post_password'];
5178 $post_parent = $postdata['post_parent'];
5179 $post_type = $postdata['post_type'];
5180 $menu_order = $postdata['menu_order'];
5182 // Let WordPress manage slug if none was provided.
5183 $post_name = $postdata['post_name'];
5184 if ( isset($content_struct['wp_slug']) )
5185 $post_name = $content_struct['wp_slug'];
5187 // Only use a password if one was given.
5188 if ( isset($content_struct['wp_password']) )
5189 $post_password = $content_struct['wp_password'];
5191 // Only set a post parent if one was given.
5192 if ( isset($content_struct['wp_page_parent_id']) )
5193 $post_parent = $content_struct['wp_page_parent_id'];
5195 // Only set the menu_order if it was given.
5196 if ( isset($content_struct['wp_page_order']) )
5197 $menu_order = $content_struct['wp_page_order'];
5199 $page_template = null;
5200 if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
5201 $page_template = $content_struct['wp_page_template'];
5203 $post_author = $postdata['post_author'];
5205 // Only set the post_author if one is set.
5206 if ( isset( $content_struct['wp_author_id'] ) ) {
5207 // Check permissions if attempting to switch author to or from another user.
5208 if ( $user->ID != $content_struct['wp_author_id'] || $user->ID != $post_author ) {
5209 switch ( $post_type ) {
5211 if ( ! current_user_can( 'edit_others_posts' ) ) {
5212 return new IXR_Error( 401, __( 'You are not allowed to change the post author as this user.' ) );
5216 if ( ! current_user_can( 'edit_others_pages' ) ) {
5217 return new IXR_Error( 401, __( 'You are not allowed to change the page author as this user.' ) );
5221 return new IXR_Error( 401, __( 'Invalid post type' ) );
5223 $post_author = $content_struct['wp_author_id'];
5227 if ( isset($content_struct['mt_allow_comments']) ) {
5228 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
5229 switch ( $content_struct['mt_allow_comments'] ) {
5231 $comment_status = 'closed';
5234 $comment_status = 'open';
5237 $comment_status = get_default_comment_status( $post_type );
5241 switch ( (int) $content_struct['mt_allow_comments'] ) {
5244 $comment_status = 'closed';
5247 $comment_status = 'open';
5250 $comment_status = get_default_comment_status( $post_type );
5256 if ( isset($content_struct['mt_allow_pings']) ) {
5257 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
5258 switch ( $content_struct['mt_allow_pings'] ) {
5260 $ping_status = 'closed';
5263 $ping_status = 'open';
5266 $ping_status = get_default_comment_status( $post_type, 'pingback' );
5270 switch ( (int) $content_struct["mt_allow_pings"] ) {
5272 $ping_status = 'closed';
5275 $ping_status = 'open';
5278 $ping_status = get_default_comment_status( $post_type, 'pingback' );
5284 if ( isset( $content_struct['title'] ) )
5285 $post_title = $content_struct['title'];
5287 if ( isset( $content_struct['description'] ) )
5288 $post_content = $content_struct['description'];
5290 $post_category = array();
5291 if ( isset( $content_struct['categories'] ) ) {
5292 $catnames = $content_struct['categories'];
5293 if ( is_array($catnames) ) {
5294 foreach ($catnames as $cat) {
5295 $post_category[] = get_cat_ID($cat);
5300 if ( isset( $content_struct['mt_excerpt'] ) )
5301 $post_excerpt = $content_struct['mt_excerpt'];
5303 $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
5305 $post_status = $publish ? 'publish' : 'draft';
5306 if ( isset( $content_struct["{$post_type}_status"] ) ) {
5307 switch( $content_struct["{$post_type}_status"] ) {
5312 $post_status = $content_struct["{$post_type}_status"];
5315 $post_status = $publish ? 'publish' : 'draft';
5320 $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
5322 if ( 'publish' == $post_status || 'private' == $post_status ) {
5323 if ( 'page' == $post_type && ! current_user_can( 'publish_pages' ) ) {
5324 return new IXR_Error( 401, __( 'Sorry, you do not have the right to publish this page.' ) );
5325 } elseif ( ! current_user_can( 'publish_posts' ) ) {
5326 return new IXR_Error( 401, __( 'Sorry, you do not have the right to publish this post.' ) );
5331 $post_content = $post_content . "<!--more-->" . $post_more;
5334 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
5335 $to_ping = $content_struct['mt_tb_ping_urls'];
5336 if ( is_array($to_ping) )
5337 $to_ping = implode(' ', $to_ping);
5340 // Do some timestamp voodoo.
5341 if ( !empty( $content_struct['date_created_gmt'] ) )
5342 // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
5343 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
5344 elseif ( !empty( $content_struct['dateCreated']) )
5345 $dateCreated = $content_struct['dateCreated']->getIso();
5347 if ( !empty( $dateCreated ) ) {
5348 $post_date = iso8601_to_datetime( $dateCreated );
5349 $post_date_gmt = get_gmt_from_date( $post_date, 'GMT' );
5351 $post_date = $postdata['post_date'];
5352 $post_date_gmt = $postdata['post_date_gmt'];
5355 // We've got all the data -- post it.
5356 $newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'post_date', 'post_date_gmt', 'to_ping', 'post_name', 'post_password', 'post_parent', 'menu_order', 'post_author', 'tags_input', 'page_template');
5358 $result = wp_update_post($newpost, true);
5359 if ( is_wp_error( $result ) )
5360 return new IXR_Error(500, $result->get_error_message());
5363 return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
5365 // Only posts can be sticky
5366 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
5368 $data['sticky'] = $content_struct['sticky'];
5369 $data['post_type'] = 'post';
5370 $error = $this->_toggle_sticky( $data, true );
5376 if ( isset($content_struct['custom_fields']) )
5377 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
5379 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
5381 // Empty value deletes, non-empty value adds/updates.
5382 if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
5383 delete_post_thumbnail( $post_ID );
5385 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
5386 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
5388 unset( $content_struct['wp_post_thumbnail'] );
5391 // Handle enclosures.
5392 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
5393 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
5395 $this->attach_uploads( $ID, $post_content );
5397 // Handle post formats if assigned, validation is handled earlier in this function.
5398 if ( isset( $content_struct['wp_post_format'] ) )
5399 set_post_format( $post_ID, $content_struct['wp_post_format'] );
5402 * Fires after a post has been successfully updated via the XML-RPC MovableType API.
5406 * @param int $post_ID ID of the updated post.
5407 * @param array $args An array of arguments to update the post.
5409 do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
5419 * @param array $args {
5420 * Method arguments. Note: arguments must be ordered as documented.
5422 * @type int $blog_id (unused)
5423 * @type int $post_ID
5424 * @type string $username
5425 * @type string $password
5427 * @return array|IXR_Error
5429 public function mw_getPost( $args ) {
5430 $this->escape( $args );
5432 $post_ID = (int) $args[0];
5433 $username = $args[1];
5434 $password = $args[2];
5436 if ( !$user = $this->login($username, $password) )
5437 return $this->error;
5439 $postdata = get_post($post_ID, ARRAY_A);
5441 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5443 if ( !current_user_can( 'edit_post', $post_ID ) )
5444 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
5446 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5447 do_action( 'xmlrpc_call', 'metaWeblog.getPost' );
5449 if ($postdata['post_date'] != '') {
5450 $post_date = $this->_convert_date( $postdata['post_date'] );
5451 $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'], $postdata['post_date'] );
5452 $post_modified = $this->_convert_date( $postdata['post_modified'] );
5453 $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
5455 $categories = array();
5456 $catids = wp_get_post_categories($post_ID);
5457 foreach ($catids as $catid)
5458 $categories[] = get_cat_name($catid);
5460 $tagnames = array();
5461 $tags = wp_get_post_tags( $post_ID );
5462 if ( !empty( $tags ) ) {
5463 foreach ( $tags as $tag )
5464 $tagnames[] = $tag->name;
5465 $tagnames = implode( ', ', $tagnames );
5470 $post = get_extended($postdata['post_content']);
5471 $link = get_permalink($postdata['ID']);
5473 // Get the author info.
5474 $author = get_userdata($postdata['post_author']);
5476 $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
5477 $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
5479 // Consider future posts as published
5480 if ( $postdata['post_status'] === 'future' )
5481 $postdata['post_status'] = 'publish';
5484 $post_format = get_post_format( $post_ID );
5485 if ( empty( $post_format ) )
5486 $post_format = 'standard';
5489 if ( is_sticky( $post_ID ) )
5492 $enclosure = array();
5493 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
5494 if ($key == 'enclosure') {
5495 foreach ( (array) $val as $enc ) {
5496 $encdata = explode("\n", $enc);
5497 $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
5498 $enclosure['length'] = (int) trim($encdata[1]);
5499 $enclosure['type'] = trim($encdata[2]);
5506 'dateCreated' => $post_date,
5507 'userid' => $postdata['post_author'],
5508 'postid' => $postdata['ID'],
5509 'description' => $post['main'],
5510 'title' => $postdata['post_title'],
5512 'permaLink' => $link,
5513 // commented out because no other tool seems to use this
5514 // 'content' => $entry['post_content'],
5515 'categories' => $categories,
5516 'mt_excerpt' => $postdata['post_excerpt'],
5517 'mt_text_more' => $post['extended'],
5518 'wp_more_text' => $post['more_text'],
5519 'mt_allow_comments' => $allow_comments,
5520 'mt_allow_pings' => $allow_pings,
5521 'mt_keywords' => $tagnames,
5522 'wp_slug' => $postdata['post_name'],
5523 'wp_password' => $postdata['post_password'],
5524 'wp_author_id' => (string) $author->ID,
5525 'wp_author_display_name' => $author->display_name,
5526 'date_created_gmt' => $post_date_gmt,
5527 'post_status' => $postdata['post_status'],
5528 'custom_fields' => $this->get_custom_fields($post_ID),
5529 'wp_post_format' => $post_format,
5530 'sticky' => $sticky,
5531 'date_modified' => $post_modified,
5532 'date_modified_gmt' => $post_modified_gmt
5535 if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
5537 $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
5541 return new IXR_Error(404, __('Sorry, no such post.'));
5546 * Retrieve list of recent posts.
5550 * @param array $args {
5551 * Method arguments. Note: arguments must be ordered as documented.
5553 * @type int $blog_id (unused)
5554 * @type string $username
5555 * @type string $password
5556 * @type int $numberposts
5558 * @return array|IXR_Error
5560 public function mw_getRecentPosts( $args ) {
5561 $this->escape( $args );
5563 $username = $args[1];
5564 $password = $args[2];
5565 if ( isset( $args[3] ) )
5566 $query = array( 'numberposts' => absint( $args[3] ) );
5570 if ( !$user = $this->login($username, $password) )
5571 return $this->error;
5573 if ( ! current_user_can( 'edit_posts' ) )
5574 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
5576 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5577 do_action( 'xmlrpc_call', 'metaWeblog.getRecentPosts' );
5579 $posts_list = wp_get_recent_posts( $query );
5584 $recent_posts = array();
5585 foreach ($posts_list as $entry) {
5586 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5589 $post_date = $this->_convert_date( $entry['post_date'] );
5590 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5591 $post_modified = $this->_convert_date( $entry['post_modified'] );
5592 $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
5594 $categories = array();
5595 $catids = wp_get_post_categories($entry['ID']);
5596 foreach ( $catids as $catid )
5597 $categories[] = get_cat_name($catid);
5599 $tagnames = array();
5600 $tags = wp_get_post_tags( $entry['ID'] );
5601 if ( !empty( $tags ) ) {
5602 foreach ( $tags as $tag ) {
5603 $tagnames[] = $tag->name;
5605 $tagnames = implode( ', ', $tagnames );
5610 $post = get_extended($entry['post_content']);
5611 $link = get_permalink($entry['ID']);
5613 // Get the post author info.
5614 $author = get_userdata($entry['post_author']);
5616 $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
5617 $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
5619 // Consider future posts as published
5620 if ( $entry['post_status'] === 'future' )
5621 $entry['post_status'] = 'publish';
5624 $post_format = get_post_format( $entry['ID'] );
5625 if ( empty( $post_format ) )
5626 $post_format = 'standard';
5628 $recent_posts[] = array(
5629 'dateCreated' => $post_date,
5630 'userid' => $entry['post_author'],
5631 'postid' => (string) $entry['ID'],
5632 'description' => $post['main'],
5633 'title' => $entry['post_title'],
5635 'permaLink' => $link,
5636 // commented out because no other tool seems to use this
5637 // 'content' => $entry['post_content'],
5638 'categories' => $categories,
5639 'mt_excerpt' => $entry['post_excerpt'],
5640 'mt_text_more' => $post['extended'],
5641 'wp_more_text' => $post['more_text'],
5642 'mt_allow_comments' => $allow_comments,
5643 'mt_allow_pings' => $allow_pings,
5644 'mt_keywords' => $tagnames,
5645 'wp_slug' => $entry['post_name'],
5646 'wp_password' => $entry['post_password'],
5647 'wp_author_id' => (string) $author->ID,
5648 'wp_author_display_name' => $author->display_name,
5649 'date_created_gmt' => $post_date_gmt,
5650 'post_status' => $entry['post_status'],
5651 'custom_fields' => $this->get_custom_fields($entry['ID']),
5652 'wp_post_format' => $post_format,
5653 'date_modified' => $post_modified,
5654 'date_modified_gmt' => $post_modified_gmt,
5655 'sticky' => ( $entry['post_type'] === 'post' && is_sticky( $entry['ID'] ) ),
5656 'wp_post_thumbnail' => get_post_thumbnail_id( $entry['ID'] )
5660 return $recent_posts;
5664 * Retrieve the list of categories on a given blog.
5668 * @param array $args {
5669 * Method arguments. Note: arguments must be ordered as documented.
5671 * @type int $blog_id (unused)
5672 * @type string $username
5673 * @type string $password
5675 * @return array|IXR_Error
5677 public function mw_getCategories( $args ) {
5678 $this->escape( $args );
5680 $username = $args[1];
5681 $password = $args[2];
5683 if ( !$user = $this->login($username, $password) )
5684 return $this->error;
5686 if ( !current_user_can( 'edit_posts' ) )
5687 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5689 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5690 do_action( 'xmlrpc_call', 'metaWeblog.getCategories' );
5692 $categories_struct = array();
5694 if ( $cats = get_categories(array('get' => 'all')) ) {
5695 foreach ( $cats as $cat ) {
5697 $struct['categoryId'] = $cat->term_id;
5698 $struct['parentId'] = $cat->parent;
5699 $struct['description'] = $cat->name;
5700 $struct['categoryDescription'] = $cat->description;
5701 $struct['categoryName'] = $cat->name;
5702 $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
5703 $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
5705 $categories_struct[] = $struct;
5709 return $categories_struct;
5713 * Uploads a file, following your settings.
5715 * Adapted from a patch by Johann Richard.
5717 * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
5721 * @global wpdb $wpdb WordPress database abstraction object.
5723 * @param array $args {
5724 * Method arguments. Note: arguments must be ordered as documented.
5726 * @type int $blog_id (unused)
5727 * @type string $username
5728 * @type string $password
5731 * @return array|IXR_Error
5733 public function mw_newMediaObject( $args ) {
5736 $username = $this->escape( $args[1] );
5737 $password = $this->escape( $args[2] );
5740 $name = sanitize_file_name( $data['name'] );
5741 $type = $data['type'];
5742 $bits = $data['bits'];
5744 if ( !$user = $this->login($username, $password) )
5745 return $this->error;
5747 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5748 do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject' );
5750 if ( !current_user_can('upload_files') ) {
5751 $this->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
5752 return $this->error;
5755 if ( is_multisite() && upload_is_user_over_quota( false ) ) {
5756 $this->error = new IXR_Error( 401, __( 'Sorry, you have used your space allocation.' ) );
5757 return $this->error;
5761 * Filter whether to preempt the XML-RPC media upload.
5763 * Passing a truthy value will effectively short-circuit the media upload,
5764 * returning that value as a 500 error instead.
5768 * @param bool $error Whether to pre-empt the media upload. Default false.
5770 if ( $upload_err = apply_filters( 'pre_upload_error', false ) ) {
5771 return new IXR_Error( 500, $upload_err );
5774 $upload = wp_upload_bits($name, null, $bits);
5775 if ( ! empty($upload['error']) ) {
5776 $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
5777 return new IXR_Error(500, $errorString);
5779 // Construct the attachment array
5781 if ( ! empty( $data['post_id'] ) ) {
5782 $post_id = (int) $data['post_id'];
5784 if ( ! current_user_can( 'edit_post', $post_id ) )
5785 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
5787 $attachment = array(
5788 'post_title' => $name,
5789 'post_content' => '',
5790 'post_type' => 'attachment',
5791 'post_parent' => $post_id,
5792 'post_mime_type' => $type,
5793 'guid' => $upload[ 'url' ]
5797 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
5798 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
5801 * Fires after a new attachment has been added via the XML-RPC MovableType API.
5805 * @param int $id ID of the new attachment.
5806 * @param array $args An array of arguments to add the attachment.
5808 do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
5810 $struct = $this->_prepare_media_item( get_post( $id ) );
5812 // Deprecated values
5813 $struct['id'] = $struct['attachment_id'];
5814 $struct['file'] = $struct['title'];
5815 $struct['url'] = $struct['link'];
5820 /* MovableType API functions
5821 * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
5825 * Retrieve the post titles of recent posts.
5829 * @param array $args {
5830 * Method arguments. Note: arguments must be ordered as documented.
5832 * @type int $blog_id (unused)
5833 * @type string $username
5834 * @type string $password
5835 * @type int $numberposts
5837 * @return array|IXR_Error
5839 public function mt_getRecentPostTitles( $args ) {
5840 $this->escape( $args );
5842 $username = $args[1];
5843 $password = $args[2];
5844 if ( isset( $args[3] ) )
5845 $query = array( 'numberposts' => absint( $args[3] ) );
5849 if ( !$user = $this->login($username, $password) )
5850 return $this->error;
5852 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5853 do_action( 'xmlrpc_call', 'mt.getRecentPostTitles' );
5855 $posts_list = wp_get_recent_posts( $query );
5857 if ( !$posts_list ) {
5858 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
5859 return $this->error;
5862 $recent_posts = array();
5864 foreach ($posts_list as $entry) {
5865 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5868 $post_date = $this->_convert_date( $entry['post_date'] );
5869 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5871 $recent_posts[] = array(
5872 'dateCreated' => $post_date,
5873 'userid' => $entry['post_author'],
5874 'postid' => (string) $entry['ID'],
5875 'title' => $entry['post_title'],
5876 'post_status' => $entry['post_status'],
5877 'date_created_gmt' => $post_date_gmt
5881 return $recent_posts;
5885 * Retrieve list of all categories on blog.
5889 * @param array $args {
5890 * Method arguments. Note: arguments must be ordered as documented.
5892 * @type int $blog_id (unused)
5893 * @type string $username
5894 * @type string $password
5896 * @return array|IXR_Error
5898 public function mt_getCategoryList( $args ) {
5899 $this->escape( $args );
5901 $username = $args[1];
5902 $password = $args[2];
5904 if ( !$user = $this->login($username, $password) )
5905 return $this->error;
5907 if ( !current_user_can( 'edit_posts' ) )
5908 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5910 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5911 do_action( 'xmlrpc_call', 'mt.getCategoryList' );
5913 $categories_struct = array();
5915 if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
5916 foreach ( $cats as $cat ) {
5918 $struct['categoryId'] = $cat->term_id;
5919 $struct['categoryName'] = $cat->name;
5921 $categories_struct[] = $struct;
5925 return $categories_struct;
5929 * Retrieve post categories.
5933 * @param array $args {
5934 * Method arguments. Note: arguments must be ordered as documented.
5936 * @type int $post_ID
5937 * @type string $username
5938 * @type string $password
5940 * @return array|IXR_Error
5942 public function mt_getPostCategories( $args ) {
5943 $this->escape( $args );
5945 $post_ID = (int) $args[0];
5946 $username = $args[1];
5947 $password = $args[2];
5949 if ( !$user = $this->login($username, $password) )
5950 return $this->error;
5952 if ( ! get_post( $post_ID ) )
5953 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5955 if ( !current_user_can( 'edit_post', $post_ID ) )
5956 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
5958 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5959 do_action( 'xmlrpc_call', 'mt.getPostCategories' );
5961 $categories = array();
5962 $catids = wp_get_post_categories(intval($post_ID));
5963 // first listed category will be the primary category
5965 foreach ( $catids as $catid ) {
5966 $categories[] = array(
5967 'categoryName' => get_cat_name($catid),
5968 'categoryId' => (string) $catid,
5969 'isPrimary' => $isPrimary
5978 * Sets categories for a post.
5982 * @param array $args {
5983 * Method arguments. Note: arguments must be ordered as documented.
5985 * @type int $post_ID
5986 * @type string $username
5987 * @type string $password
5988 * @type array $categories
5990 * @return true|IXR_Error True on success.
5992 public function mt_setPostCategories( $args ) {
5993 $this->escape( $args );
5995 $post_ID = (int) $args[0];
5996 $username = $args[1];
5997 $password = $args[2];
5998 $categories = $args[3];
6000 if ( !$user = $this->login($username, $password) )
6001 return $this->error;
6003 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6004 do_action( 'xmlrpc_call', 'mt.setPostCategories' );
6006 if ( ! get_post( $post_ID ) )
6007 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
6009 if ( !current_user_can('edit_post', $post_ID) )
6010 return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
6013 foreach ( $categories as $cat ) {
6014 $catids[] = $cat['categoryId'];
6017 wp_set_post_categories($post_ID, $catids);
6023 * Retrieve an array of methods supported by this server.
6029 public function mt_supportedMethods() {
6030 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6031 do_action( 'xmlrpc_call', 'mt.supportedMethods' );
6033 return array_keys( $this->methods );
6037 * Retrieve an empty array because we don't support per-post text filters.
6041 public function mt_supportedTextFilters() {
6042 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6043 do_action( 'xmlrpc_call', 'mt.supportedTextFilters' );
6046 * Filter the MoveableType text filters list for XML-RPC.
6050 * @param array $filters An array of text filters.
6052 return apply_filters( 'xmlrpc_text_filters', array() );
6056 * Retrieve trackbacks sent to a given post.
6060 * @global wpdb $wpdb WordPress database abstraction object.
6062 * @param int $post_ID
6063 * @return array|IXR_Error
6065 public function mt_getTrackbackPings( $post_ID ) {
6068 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6069 do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
6071 $actual_post = get_post($post_ID, ARRAY_A);
6073 if ( !$actual_post )
6074 return new IXR_Error(404, __('Sorry, no such post.'));
6076 $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
6081 $trackback_pings = array();
6082 foreach ( $comments as $comment ) {
6083 if ( 'trackback' == $comment->comment_type ) {
6084 $content = $comment->comment_content;
6085 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
6086 $trackback_pings[] = array(
6087 'pingTitle' => $title,
6088 'pingURL' => $comment->comment_author_url,
6089 'pingIP' => $comment->comment_author_IP
6094 return $trackback_pings;
6098 * Sets a post's publish status to 'publish'.
6102 * @param array $args {
6103 * Method arguments. Note: arguments must be ordered as documented.
6105 * @type int $post_ID
6106 * @type string $username
6107 * @type string $password
6109 * @return int|IXR_Error
6111 public function mt_publishPost( $args ) {
6112 $this->escape( $args );
6114 $post_ID = (int) $args[0];
6115 $username = $args[1];
6116 $password = $args[2];
6118 if ( !$user = $this->login($username, $password) )
6119 return $this->error;
6121 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6122 do_action( 'xmlrpc_call', 'mt.publishPost' );
6124 $postdata = get_post($post_ID, ARRAY_A);
6126 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
6128 if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
6129 return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
6131 $postdata['post_status'] = 'publish';
6134 $cats = wp_get_post_categories($post_ID);
6135 $postdata['post_category'] = $cats;
6136 $this->escape($postdata);
6138 return wp_update_post( $postdata );
6141 /* PingBack functions
6142 * specs on www.hixie.ch/specs/pingback/pingback
6146 * Retrieves a pingback and registers it.
6150 * @global wpdb $wpdb WordPress database abstraction object.
6151 * @global string $wp_version
6153 * @param array $args {
6154 * Method arguments. Note: arguments must be ordered as documented.
6156 * @type string $pagelinkedfrom
6157 * @type string $pagelinkedto
6159 * @return string|IXR_Error
6161 public function pingback_ping( $args ) {
6162 global $wpdb, $wp_version;
6164 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6165 do_action( 'xmlrpc_call', 'pingback.ping' );
6167 $this->escape( $args );
6169 $pagelinkedfrom = str_replace( '&', '&', $args[0] );
6170 $pagelinkedto = str_replace( '&', '&', $args[1] );
6171 $pagelinkedto = str_replace( '&', '&', $pagelinkedto );
6174 * Filter the pingback source URI.
6178 * @param string $pagelinkedfrom URI of the page linked from.
6179 * @param string $pagelinkedto URI of the page linked to.
6181 $pagelinkedfrom = apply_filters( 'pingback_ping_source_uri', $pagelinkedfrom, $pagelinkedto );
6183 if ( ! $pagelinkedfrom )
6184 return $this->pingback_error( 0, __( 'A valid URL was not provided.' ) );
6186 // Check if the page linked to is in our site
6187 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
6189 return $this->pingback_error( 0, __( 'Is there no link to us?' ) );
6191 // let's find which post is linked to
6192 // FIXME: does url_to_postid() cover all these cases already?
6193 // if so, then let's use it and drop the old code.
6194 $urltest = parse_url($pagelinkedto);
6195 if ( $post_ID = url_to_postid($pagelinkedto) ) {
6197 } elseif ( isset( $urltest['path'] ) && preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
6198 // the path defines the post_ID (archives/p/XXXX)
6199 $blah = explode('/', $match[0]);
6200 $post_ID = (int) $blah[1];
6201 } elseif ( isset( $urltest['query'] ) && preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
6202 // the querystring defines the post_ID (?p=XXXX)
6203 $blah = explode('=', $match[0]);
6204 $post_ID = (int) $blah[1];
6205 } elseif ( isset($urltest['fragment']) ) {
6206 // an #anchor is there, it's either...
6207 if ( intval($urltest['fragment']) ) {
6208 // ...an integer #XXXX (simplest case)
6209 $post_ID = (int) $urltest['fragment'];
6210 } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
6211 // ...a post id in the form 'post-###'
6212 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
6213 } elseif ( is_string($urltest['fragment']) ) {
6214 // ...or a string #title, a little more complicated
6215 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
6216 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
6217 if (! ($post_ID = $wpdb->get_var($sql)) ) {
6218 // returning unknown error '0' is better than die()ing
6219 return $this->pingback_error( 0, '' );
6223 // TODO: Attempt to extract a post ID from the given URL
6224 return $this->pingback_error( 33, __('The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
6226 $post_ID = (int) $post_ID;
6228 $post = get_post($post_ID);
6230 if ( !$post ) // Post_ID not found
6231 return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
6233 if ( $post_ID == url_to_postid($pagelinkedfrom) )
6234 return $this->pingback_error( 0, __( 'The source URL and the target URL cannot both point to the same resource.' ) );
6236 // Check if pings are on
6237 if ( !pings_open($post) )
6238 return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
6240 // Let's check that the remote site didn't already pingback this entry
6241 if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
6242 return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
6244 // very stupid, but gives time to the 'from' server to publish !
6247 $remote_ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
6249 /** This filter is documented in wp-includes/class-http.php */
6250 $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) );
6252 // Let's check the remote site
6253 $http_api_args = array(
6256 'limit_response_size' => 153600, // 150 KB
6257 'user-agent' => "$user_agent; verifying pingback from $remote_ip",
6259 'X-Pingback-Forwarded-For' => $remote_ip,
6262 $request = wp_safe_remote_get( $pagelinkedfrom, $http_api_args );
6263 $linea = wp_remote_retrieve_body( $request );
6266 return $this->pingback_error( 16, __( 'The source URL does not exist.' ) );
6269 * Filter the pingback remote source.
6273 * @param string $linea Response object for the page linked from.
6274 * @param string $pagelinkedto URL of the page linked to.
6276 $linea = apply_filters( 'pre_remote_source', $linea, $pagelinkedto );
6278 // Work around bug in strip_tags():
6279 $linea = str_replace('<!DOC', '<DOC', $linea);
6280 $linea = preg_replace( '/[\r\n\t ]+/', ' ', $linea ); // normalize spaces
6281 $linea = preg_replace( "/<\/*(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
6283 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
6284 $title = $matchtitle[1];
6285 if ( empty( $title ) )
6286 return $this->pingback_error( 32, __('We cannot find a title on that page.' ) );
6288 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
6290 $p = explode( "\n\n", $linea );
6292 $preg_target = preg_quote($pagelinkedto, '|');
6294 foreach ( $p as $para ) {
6295 if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
6296 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
6298 // If the URL isn't in a link context, keep looking
6299 if ( empty($context) )
6302 // We're going to use this fake tag to mark the context in a bit
6303 // the marker is needed in case the link text appears more than once in the paragraph
6304 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
6306 // prevent really long link text
6307 if ( strlen($context[1]) > 100 )
6308 $context[1] = substr($context[1], 0, 100) . '…';
6310 $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
6311 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
6312 $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
6313 $excerpt = trim($excerpt);
6314 $preg_marker = preg_quote($marker, '|');
6315 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
6316 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
6321 if ( empty($context) ) // Link to target not found
6322 return $this->pingback_error( 17, __( 'The source URL does not contain a link to the target URL, and so cannot be used as a source.' ) );
6324 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
6326 $context = '[…] ' . esc_html( $excerpt ) . ' […]';
6327 $pagelinkedfrom = $this->escape( $pagelinkedfrom );
6329 $comment_post_ID = (int) $post_ID;
6330 $comment_author = $title;
6331 $comment_author_email = '';
6332 $this->escape($comment_author);
6333 $comment_author_url = $pagelinkedfrom;
6334 $comment_content = $context;
6335 $this->escape($comment_content);
6336 $comment_type = 'pingback';
6338 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
6340 $comment_ID = wp_new_comment($commentdata);
6343 * Fires after a post pingback has been sent.
6347 * @param int $comment_ID Comment ID.
6349 do_action( 'pingback_post', $comment_ID );
6351 return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
6355 * Retrieve array of URLs that pingbacked the given URL.
6357 * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
6361 * @global wpdb $wpdb WordPress database abstraction object.
6363 * @param string $url
6364 * @return array|IXR_Error
6366 public function pingback_extensions_getPingbacks( $url ) {
6369 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
6370 do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' );
6372 $url = $this->escape( $url );
6374 $post_ID = url_to_postid($url);
6376 // We aren't sure that the resource is available and/or pingback enabled
6377 return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
6380 $actual_post = get_post($post_ID, ARRAY_A);
6382 if ( !$actual_post ) {
6383 // No such post = resource not found
6384 return $this->pingback_error( 32, __('The specified target URL does not exist.' ) );
6387 $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
6392 $pingbacks = array();
6393 foreach ( $comments as $comment ) {
6394 if ( 'pingback' == $comment->comment_type )
6395 $pingbacks[] = $comment->comment_author_url;
6402 * @param integer $code
6403 * @param string $message
6406 protected function pingback_error( $code, $message ) {
6408 * Filter the XML-RPC pingback error return.
6412 * @param IXR_Error $error An IXR_Error object containing the error code and message.
6414 return apply_filters( 'xmlrpc_pingback_error', new IXR_Error( $code, $message ) );