3 * XML-RPC protocol support for WordPress
6 * @subpackage Publishing
10 * WordPress XMLRPC server implementation.
12 * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
13 * pingback. Additional WordPress API for managing comments, pages, posts,
16 * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled
17 * via the xmlrpc_enabled filter found in wp_xmlrpc_server::login().
20 * @subpackage Publishing
23 class wp_xmlrpc_server extends IXR_Server {
37 * Register all of the XMLRPC methods that XMLRPC server understands.
39 * Sets up server and method property. Passes XMLRPC
40 * methods through the 'xmlrpc_methods' filter to allow plugins to extend
41 * or replace XMLRPC methods.
45 * @return wp_xmlrpc_server
47 public function __construct() {
48 $this->methods = array(
50 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
51 'wp.newPost' => 'this:wp_newPost',
52 'wp.editPost' => 'this:wp_editPost',
53 'wp.deletePost' => 'this:wp_deletePost',
54 'wp.getPost' => 'this:wp_getPost',
55 'wp.getPosts' => 'this:wp_getPosts',
56 'wp.newTerm' => 'this:wp_newTerm',
57 'wp.editTerm' => 'this:wp_editTerm',
58 'wp.deleteTerm' => 'this:wp_deleteTerm',
59 'wp.getTerm' => 'this:wp_getTerm',
60 'wp.getTerms' => 'this:wp_getTerms',
61 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
62 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
63 'wp.getUser' => 'this:wp_getUser',
64 'wp.getUsers' => 'this:wp_getUsers',
65 'wp.getProfile' => 'this:wp_getProfile',
66 'wp.editProfile' => 'this:wp_editProfile',
67 'wp.getPage' => 'this:wp_getPage',
68 'wp.getPages' => 'this:wp_getPages',
69 'wp.newPage' => 'this:wp_newPage',
70 'wp.deletePage' => 'this:wp_deletePage',
71 'wp.editPage' => 'this:wp_editPage',
72 'wp.getPageList' => 'this:wp_getPageList',
73 'wp.getAuthors' => 'this:wp_getAuthors',
74 'wp.getCategories' => 'this:mw_getCategories', // Alias
75 'wp.getTags' => 'this:wp_getTags',
76 'wp.newCategory' => 'this:wp_newCategory',
77 'wp.deleteCategory' => 'this:wp_deleteCategory',
78 'wp.suggestCategories' => 'this:wp_suggestCategories',
79 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
80 'wp.deleteFile' => 'this:wp_deletePost', // Alias
81 'wp.getCommentCount' => 'this:wp_getCommentCount',
82 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
83 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
84 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
85 'wp.getOptions' => 'this:wp_getOptions',
86 'wp.setOptions' => 'this:wp_setOptions',
87 'wp.getComment' => 'this:wp_getComment',
88 'wp.getComments' => 'this:wp_getComments',
89 'wp.deleteComment' => 'this:wp_deleteComment',
90 'wp.editComment' => 'this:wp_editComment',
91 'wp.newComment' => 'this:wp_newComment',
92 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
93 'wp.getMediaItem' => 'this:wp_getMediaItem',
94 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
95 'wp.getPostFormats' => 'this:wp_getPostFormats',
96 'wp.getPostType' => 'this:wp_getPostType',
97 'wp.getPostTypes' => 'this:wp_getPostTypes',
98 'wp.getRevisions' => 'this:wp_getRevisions',
99 'wp.restoreRevision' => 'this:wp_restoreRevision',
102 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
103 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
104 'blogger.getPost' => 'this:blogger_getPost',
105 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
106 'blogger.newPost' => 'this:blogger_newPost',
107 'blogger.editPost' => 'this:blogger_editPost',
108 'blogger.deletePost' => 'this:blogger_deletePost',
110 // MetaWeblog API (with MT extensions to structs)
111 'metaWeblog.newPost' => 'this:mw_newPost',
112 'metaWeblog.editPost' => 'this:mw_editPost',
113 'metaWeblog.getPost' => 'this:mw_getPost',
114 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
115 'metaWeblog.getCategories' => 'this:mw_getCategories',
116 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
118 // MetaWeblog API aliases for Blogger API
119 // see http://www.xmlrpc.com/stories/storyReader$2460
120 'metaWeblog.deletePost' => 'this:blogger_deletePost',
121 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
124 'mt.getCategoryList' => 'this:mt_getCategoryList',
125 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
126 'mt.getPostCategories' => 'this:mt_getPostCategories',
127 'mt.setPostCategories' => 'this:mt_setPostCategories',
128 'mt.supportedMethods' => 'this:mt_supportedMethods',
129 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
130 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
131 'mt.publishPost' => 'this:mt_publishPost',
134 'pingback.ping' => 'this:pingback_ping',
135 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
137 'demo.sayHello' => 'this:sayHello',
138 'demo.addTwoNumbers' => 'this:addTwoNumbers'
141 $this->initialise_blog_option_info();
144 * Filter the methods exposed by the XML-RPC server.
146 * This filter can be used to add new methods, and remove built-in methods.
150 * @param array $methods An array of XML-RPC methods.
152 $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
156 * Make private/protected methods readable for backwards compatibility.
161 * @param callable $name Method to call.
162 * @param array $arguments Arguments to pass when calling.
163 * @return mixed|bool Return value of the callback, false otherwise.
165 public function __call( $name, $arguments ) {
166 return call_user_func_array( array( $this, $name ), $arguments );
169 public function serve_request() {
170 $this->IXR_Server($this->methods);
174 * Test XMLRPC API by saying, "Hello!" to client.
178 * @param array $args Method Parameters.
181 public function sayHello($args) {
186 * Test XMLRPC API by adding two numbers for client.
190 * @param array $args Method Parameters.
193 public function addTwoNumbers($args) {
196 return $number1 + $number2;
204 * @param string $username User's username.
205 * @param string $password User's password.
206 * @return WP_User|bool WP_User object if authentication passed, false otherwise
208 public function login( $username, $password ) {
210 * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
211 * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
213 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
214 if ( false === $enabled ) {
215 $enabled = apply_filters( 'option_enable_xmlrpc', true );
219 * Filter whether XML-RPC is enabled.
221 * This is the proper filter for turning off XML-RPC.
225 * @param bool $enabled Whether XML-RPC is enabled. Default true.
227 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
230 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
234 $user = wp_authenticate($username, $password);
236 if (is_wp_error($user)) {
237 $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
240 * Filter the XML-RPC user login error message.
244 * @param string $error The XML-RPC error message.
245 * @param WP_User $user WP_User object.
247 $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
251 wp_set_current_user( $user->ID );
256 * Check user's credentials. Deprecated.
260 * @deprecated use wp_xmlrpc_server::login
261 * @see wp_xmlrpc_server::login
263 * @param string $username User's username.
264 * @param string $password User's password.
265 * @return bool Whether authentication passed.
267 public function login_pass_ok( $username, $password ) {
268 return (bool) $this->login( $username, $password );
272 * Escape string or array of strings for database.
276 * @param string|array $data Escape single string or array of strings.
277 * @return string|array Type matches $data and sanitized for the database.
279 public function escape( &$data ) {
280 if ( ! is_array( $data ) )
281 return wp_slash( $data );
283 foreach ( $data as &$v ) {
284 if ( is_array( $v ) )
286 elseif ( ! is_object( $v ) )
292 * Retrieve custom fields for post.
296 * @param int $post_id Post ID.
297 * @return array Custom fields, if exist.
299 public function get_custom_fields($post_id) {
300 $post_id = (int) $post_id;
302 $custom_fields = array();
304 foreach ( (array) has_meta($post_id) as $meta ) {
305 // Don't expose protected fields.
306 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
309 $custom_fields[] = array(
310 "id" => $meta['meta_id'],
311 "key" => $meta['meta_key'],
312 "value" => $meta['meta_value']
316 return $custom_fields;
320 * Set custom fields for post.
324 * @param int $post_id Post ID.
325 * @param array $fields Custom fields.
327 public function set_custom_fields($post_id, $fields) {
328 $post_id = (int) $post_id;
330 foreach ( (array) $fields as $meta ) {
331 if ( isset($meta['id']) ) {
332 $meta['id'] = (int) $meta['id'];
333 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
334 if ( isset($meta['key']) ) {
335 $meta['key'] = wp_unslash( $meta['key'] );
336 if ( $meta['key'] !== $pmeta->meta_key )
338 $meta['value'] = wp_unslash( $meta['value'] );
339 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
340 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
341 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
342 delete_metadata_by_mid( 'post', $meta['id'] );
344 } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
345 add_post_meta( $post_id, $meta['key'], $meta['value'] );
351 * Set up blog options property.
353 * Passes property through 'xmlrpc_blog_options' filter.
357 public function initialise_blog_option_info() {
360 $this->blog_options = array(
362 'software_name' => array(
363 'desc' => __( 'Software Name' ),
365 'value' => 'WordPress'
367 'software_version' => array(
368 'desc' => __( 'Software Version' ),
370 'value' => $wp_version
373 'desc' => __( 'WordPress Address (URL)' ),
375 'option' => 'siteurl'
378 'desc' => __( 'Site Address (URL)' ),
382 'login_url' => array(
383 'desc' => __( 'Login Address (URL)' ),
385 'value' => wp_login_url( )
387 'admin_url' => array(
388 'desc' => __( 'The URL to the admin area' ),
390 'value' => get_admin_url( )
392 'image_default_link_type' => array(
393 'desc' => __( 'Image default link type' ),
395 'option' => 'image_default_link_type'
397 'image_default_size' => array(
398 'desc' => __( 'Image default size' ),
400 'option' => 'image_default_size'
402 'image_default_align' => array(
403 'desc' => __( 'Image default align' ),
405 'option' => 'image_default_align'
408 'desc' => __( 'Template' ),
410 'option' => 'template'
412 'stylesheet' => array(
413 'desc' => __( 'Stylesheet' ),
415 'option' => 'stylesheet'
417 'post_thumbnail' => array(
418 'desc' => __('Post Thumbnail'),
420 'value' => current_theme_supports( 'post-thumbnails' )
424 'time_zone' => array(
425 'desc' => __( 'Time Zone' ),
427 'option' => 'gmt_offset'
429 'blog_title' => array(
430 'desc' => __( 'Site Title' ),
432 'option' => 'blogname'
434 'blog_tagline' => array(
435 'desc' => __( 'Site Tagline' ),
437 'option' => 'blogdescription'
439 'date_format' => array(
440 'desc' => __( 'Date Format' ),
442 'option' => 'date_format'
444 'time_format' => array(
445 'desc' => __( 'Time Format' ),
447 'option' => 'time_format'
449 'users_can_register' => array(
450 'desc' => __( 'Allow new users to sign up' ),
452 'option' => 'users_can_register'
454 'thumbnail_size_w' => array(
455 'desc' => __( 'Thumbnail Width' ),
457 'option' => 'thumbnail_size_w'
459 'thumbnail_size_h' => array(
460 'desc' => __( 'Thumbnail Height' ),
462 'option' => 'thumbnail_size_h'
464 'thumbnail_crop' => array(
465 'desc' => __( 'Crop thumbnail to exact dimensions' ),
467 'option' => 'thumbnail_crop'
469 'medium_size_w' => array(
470 'desc' => __( 'Medium size image width' ),
472 'option' => 'medium_size_w'
474 'medium_size_h' => array(
475 'desc' => __( 'Medium size image height' ),
477 'option' => 'medium_size_h'
479 'large_size_w' => array(
480 'desc' => __( 'Large size image width' ),
482 'option' => 'large_size_w'
484 'large_size_h' => array(
485 'desc' => __( 'Large size image height' ),
487 'option' => 'large_size_h'
489 'default_comment_status' => array(
490 'desc' => __( 'Allow people to post comments on new articles' ),
492 'option' => 'default_comment_status'
494 'default_ping_status' => array(
495 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
497 'option' => 'default_ping_status'
502 * Filter the XML-RPC blog options property.
506 * @param array $blog_options An array of XML-RPC blog options.
508 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
512 * Retrieve the blogs of the user.
516 * @param array $args Method parameters. Contains:
519 * @return array|IXR_Error Array contains:
524 * - 'xmlrpc' - url of xmlrpc endpoint
526 public function wp_getUsersBlogs( $args ) {
527 // If this isn't on WPMU then just use blogger_getUsersBlogs
528 if ( !is_multisite() ) {
529 array_unshift( $args, 1 );
530 return $this->blogger_getUsersBlogs( $args );
533 $this->escape( $args );
535 $username = $args[0];
536 $password = $args[1];
538 if ( !$user = $this->login($username, $password) )
542 * Fires after the XML-RPC user has been authenticated but before the rest of
543 * the method logic begins.
545 * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
546 * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
550 * @param method $name The method name.
552 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
554 $blogs = (array) get_blogs_of_user( $user->ID );
557 foreach ( $blogs as $blog ) {
558 // Don't include blogs that aren't hosted at this site
559 if ( $blog->site_id != get_current_site()->id )
562 $blog_id = $blog->userblog_id;
564 switch_to_blog( $blog_id );
566 $is_admin = current_user_can( 'manage_options' );
569 'isAdmin' => $is_admin,
570 'url' => home_url( '/' ),
571 'blogid' => (string) $blog_id,
572 'blogName' => get_option( 'blogname' ),
573 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
576 restore_current_blog();
583 * Checks if the method received at least the minimum number of arguments.
587 * @param string|array $args Sanitize single string or array of strings.
588 * @param int $count Minimum number of arguments.
589 * @return boolean if $args contains at least $count arguments.
591 protected function minimum_args( $args, $count ) {
592 if ( count( $args ) < $count ) {
593 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
601 * Prepares taxonomy data for return in an XML-RPC object.
605 * @param object $taxonomy The unprepared taxonomy data
606 * @param array $fields The subset of taxonomy fields to return
607 * @return array The prepared taxonomy data
609 protected function _prepare_taxonomy( $taxonomy, $fields ) {
611 'name' => $taxonomy->name,
612 'label' => $taxonomy->label,
613 'hierarchical' => (bool) $taxonomy->hierarchical,
614 'public' => (bool) $taxonomy->public,
615 'show_ui' => (bool) $taxonomy->show_ui,
616 '_builtin' => (bool) $taxonomy->_builtin,
619 if ( in_array( 'labels', $fields ) )
620 $_taxonomy['labels'] = (array) $taxonomy->labels;
622 if ( in_array( 'cap', $fields ) )
623 $_taxonomy['cap'] = (array) $taxonomy->cap;
625 if ( in_array( 'menu', $fields ) )
626 $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
628 if ( in_array( 'object_type', $fields ) )
629 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
632 * Filter XML-RPC-prepared data for the given taxonomy.
636 * @param array $_taxonomy An array of taxonomy data.
637 * @param object $taxonomy Taxonomy object.
638 * @param array $fields The subset of taxonomy fields to return.
640 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
644 * Prepares term data for return in an XML-RPC object.
648 * @param array|object $term The unprepared term data
649 * @return array The prepared term data
651 protected function _prepare_term( $term ) {
653 if ( ! is_array( $_term) )
654 $_term = get_object_vars( $_term );
656 // For integers which may be larger than XML-RPC supports ensure we return strings.
657 $_term['term_id'] = strval( $_term['term_id'] );
658 $_term['term_group'] = strval( $_term['term_group'] );
659 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
660 $_term['parent'] = strval( $_term['parent'] );
662 // Count we are happy to return as an integer because people really shouldn't use terms that much.
663 $_term['count'] = intval( $_term['count'] );
666 * Filter XML-RPC-prepared data for the given term.
670 * @param array $_term An array of term data.
671 * @param array|object $term Term object or array.
673 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
677 * Convert a WordPress date string to an IXR_Date object.
681 * @param string $date
684 protected function _convert_date( $date ) {
685 if ( $date === '0000-00-00 00:00:00' ) {
686 return new IXR_Date( '00000000T00:00:00Z' );
688 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
692 * Convert a WordPress GMT date string to an IXR_Date object.
696 * @param string $date_gmt
697 * @param string $date
700 protected function _convert_date_gmt( $date_gmt, $date ) {
701 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
702 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
704 return $this->_convert_date( $date_gmt );
708 * Prepares post data for return in an XML-RPC object.
712 * @param array $post The unprepared post data
713 * @param array $fields The subset of post type fields to return
714 * @return array The prepared post data
716 protected function _prepare_post( $post, $fields ) {
717 // holds the data for this post. built up based on $fields
718 $_post = array( 'post_id' => strval( $post['ID'] ) );
720 // prepare common post fields
721 $post_fields = array(
722 'post_title' => $post['post_title'],
723 'post_date' => $this->_convert_date( $post['post_date'] ),
724 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
725 'post_modified' => $this->_convert_date( $post['post_modified'] ),
726 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
727 'post_status' => $post['post_status'],
728 'post_type' => $post['post_type'],
729 'post_name' => $post['post_name'],
730 'post_author' => $post['post_author'],
731 'post_password' => $post['post_password'],
732 'post_excerpt' => $post['post_excerpt'],
733 'post_content' => $post['post_content'],
734 'post_parent' => strval( $post['post_parent'] ),
735 'post_mime_type' => $post['post_mime_type'],
736 'link' => post_permalink( $post['ID'] ),
737 'guid' => $post['guid'],
738 'menu_order' => intval( $post['menu_order'] ),
739 'comment_status' => $post['comment_status'],
740 'ping_status' => $post['ping_status'],
741 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
745 $post_fields['post_thumbnail'] = array();
746 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
747 if ( $thumbnail_id ) {
748 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
749 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
752 // Consider future posts as published
753 if ( $post_fields['post_status'] === 'future' )
754 $post_fields['post_status'] = 'publish';
756 // Fill in blank post format
757 $post_fields['post_format'] = get_post_format( $post['ID'] );
758 if ( empty( $post_fields['post_format'] ) )
759 $post_fields['post_format'] = 'standard';
761 // Merge requested $post_fields fields into $_post
762 if ( in_array( 'post', $fields ) ) {
763 $_post = array_merge( $_post, $post_fields );
765 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
766 $_post = array_merge( $_post, $requested_fields );
769 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
771 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
772 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
773 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
774 $_post['terms'] = array();
775 foreach ( $terms as $term ) {
776 $_post['terms'][] = $this->_prepare_term( $term );
780 if ( in_array( 'custom_fields', $fields ) )
781 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
783 if ( in_array( 'enclosure', $fields ) ) {
784 $_post['enclosure'] = array();
785 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
786 if ( ! empty( $enclosures ) ) {
787 $encdata = explode( "\n", $enclosures[0] );
788 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
789 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
790 $_post['enclosure']['type'] = trim( $encdata[2] );
795 * Filter XML-RPC-prepared date for the given post.
799 * @param array $_post An array of modified post data.
800 * @param array $post An array of post data.
801 * @param array $fields An array of post fields.
803 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
807 * Prepares post data for return in an XML-RPC object.
811 * @param object $post_type Post type object
812 * @param array $fields The subset of post fields to return
813 * @return array The prepared post type data
815 protected function _prepare_post_type( $post_type, $fields ) {
817 'name' => $post_type->name,
818 'label' => $post_type->label,
819 'hierarchical' => (bool) $post_type->hierarchical,
820 'public' => (bool) $post_type->public,
821 'show_ui' => (bool) $post_type->show_ui,
822 '_builtin' => (bool) $post_type->_builtin,
823 'has_archive' => (bool) $post_type->has_archive,
824 'supports' => get_all_post_type_supports( $post_type->name ),
827 if ( in_array( 'labels', $fields ) ) {
828 $_post_type['labels'] = (array) $post_type->labels;
831 if ( in_array( 'cap', $fields ) ) {
832 $_post_type['cap'] = (array) $post_type->cap;
833 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
836 if ( in_array( 'menu', $fields ) ) {
837 $_post_type['menu_position'] = (int) $post_type->menu_position;
838 $_post_type['menu_icon'] = $post_type->menu_icon;
839 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
842 if ( in_array( 'taxonomies', $fields ) )
843 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
846 * Filter XML-RPC-prepared date for the given post type.
850 * @param array $_post_type An array of post type data.
851 * @param object $post_type Post type object.
853 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
857 * Prepares media item data for return in an XML-RPC object.
861 * @param object $media_item The unprepared media item data
862 * @param string $thumbnail_size The image size to use for the thumbnail URL
863 * @return array The prepared media item data
865 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
866 $_media_item = array(
867 'attachment_id' => strval( $media_item->ID ),
868 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
869 'parent' => $media_item->post_parent,
870 'link' => wp_get_attachment_url( $media_item->ID ),
871 'title' => $media_item->post_title,
872 'caption' => $media_item->post_excerpt,
873 'description' => $media_item->post_content,
874 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
877 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
878 if ( $thumbnail_src )
879 $_media_item['thumbnail'] = $thumbnail_src[0];
881 $_media_item['thumbnail'] = $_media_item['link'];
884 * Filter XML-RPC-prepared data for the given media item.
888 * @param array $_media_item An array of media item data.
889 * @param object $media_item Media item object.
890 * @param string $thumbnail_size Image size.
892 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
896 * Prepares page data for return in an XML-RPC object.
900 * @param object $page The unprepared page data
901 * @return array The prepared page data
903 protected function _prepare_page( $page ) {
904 // Get all of the page content and link.
905 $full_page = get_extended( $page->post_content );
906 $link = post_permalink( $page->ID );
908 // Get info the page parent if there is one.
910 if ( ! empty( $page->post_parent ) ) {
911 $parent = get_post( $page->post_parent );
912 $parent_title = $parent->post_title;
915 // Determine comment and ping settings.
916 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
917 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
920 $page_date = $this->_convert_date( $page->post_date );
921 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
923 // Pull the categories info together.
924 $categories = array();
925 if ( is_object_in_taxonomy( 'page', 'category' ) ) {
926 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
927 $categories[] = get_cat_name( $cat_id );
931 // Get the author info.
932 $author = get_userdata( $page->post_author );
934 $page_template = get_page_template_slug( $page->ID );
935 if ( empty( $page_template ) )
936 $page_template = 'default';
939 'dateCreated' => $page_date,
940 'userid' => $page->post_author,
941 'page_id' => $page->ID,
942 'page_status' => $page->post_status,
943 'description' => $full_page['main'],
944 'title' => $page->post_title,
946 'permaLink' => $link,
947 'categories' => $categories,
948 'excerpt' => $page->post_excerpt,
949 'text_more' => $full_page['extended'],
950 'mt_allow_comments' => $allow_comments,
951 'mt_allow_pings' => $allow_pings,
952 'wp_slug' => $page->post_name,
953 'wp_password' => $page->post_password,
954 'wp_author' => $author->display_name,
955 'wp_page_parent_id' => $page->post_parent,
956 'wp_page_parent_title' => $parent_title,
957 'wp_page_order' => $page->menu_order,
958 'wp_author_id' => (string) $author->ID,
959 'wp_author_display_name' => $author->display_name,
960 'date_created_gmt' => $page_date_gmt,
961 'custom_fields' => $this->get_custom_fields( $page->ID ),
962 'wp_page_template' => $page_template
966 * Filter XML-RPC-prepared data for the given page.
970 * @param array $_page An array of page data.
971 * @param WP_Post $page Page object.
973 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
977 * Prepares comment data for return in an XML-RPC object.
981 * @param object $comment The unprepared comment data
982 * @return array The prepared comment data
984 protected function _prepare_comment( $comment ) {
986 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
988 if ( '0' == $comment->comment_approved ) {
989 $comment_status = 'hold';
990 } elseif ( 'spam' == $comment->comment_approved ) {
991 $comment_status = 'spam';
992 } elseif ( '1' == $comment->comment_approved ) {
993 $comment_status = 'approve';
995 $comment_status = $comment->comment_approved;
998 'date_created_gmt' => $comment_date_gmt,
999 'user_id' => $comment->user_id,
1000 'comment_id' => $comment->comment_ID,
1001 'parent' => $comment->comment_parent,
1002 'status' => $comment_status,
1003 'content' => $comment->comment_content,
1004 'link' => get_comment_link($comment),
1005 'post_id' => $comment->comment_post_ID,
1006 'post_title' => get_the_title($comment->comment_post_ID),
1007 'author' => $comment->comment_author,
1008 'author_url' => $comment->comment_author_url,
1009 'author_email' => $comment->comment_author_email,
1010 'author_ip' => $comment->comment_author_IP,
1011 'type' => $comment->comment_type,
1015 * Filter XML-RPC-prepared data for the given comment.
1019 * @param array $_comment An array of prepared comment data.
1020 * @param object $comment Comment object.
1022 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
1026 * Prepares user data for return in an XML-RPC object.
1030 * @param WP_User $user The unprepared user object
1031 * @param array $fields The subset of user fields to return
1032 * @return array The prepared user data
1034 protected function _prepare_user( $user, $fields ) {
1035 $_user = array( 'user_id' => strval( $user->ID ) );
1037 $user_fields = array(
1038 'username' => $user->user_login,
1039 'first_name' => $user->user_firstname,
1040 'last_name' => $user->user_lastname,
1041 'registered' => $this->_convert_date( $user->user_registered ),
1042 'bio' => $user->user_description,
1043 'email' => $user->user_email,
1044 'nickname' => $user->nickname,
1045 'nicename' => $user->user_nicename,
1046 'url' => $user->user_url,
1047 'display_name' => $user->display_name,
1048 'roles' => $user->roles,
1051 if ( in_array( 'all', $fields ) ) {
1052 $_user = array_merge( $_user, $user_fields );
1054 if ( in_array( 'basic', $fields ) ) {
1055 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
1056 $fields = array_merge( $fields, $basic_fields );
1058 $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
1059 $_user = array_merge( $_user, $requested_fields );
1063 * Filter XML-RPC-prepared data for the given user.
1067 * @param array $_user An array of user data.
1068 * @param WP_User $user User object.
1069 * @param array $fields An array of user fields.
1071 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
1075 * Create a new post for any registered post type.
1079 * @param array $args Method parameters. Contains:
1080 * - int $blog_id (unused)
1081 * - string $username
1082 * - string $password
1083 * - array $content_struct
1084 * $content_struct can contain:
1085 * - post_type (default: 'post')
1086 * - post_status (default: 'draft')
1091 * - post_date_gmt | post_date
1094 * - comment_status - can be 'open' | 'closed'
1095 * - ping_status - can be 'open' | 'closed'
1097 * - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
1098 * - custom_fields - array, with each element containing 'key' and 'value'
1099 * - terms - array, with taxonomy names as keys and arrays of term IDs as values
1100 * - terms_names - array, with taxonomy names as keys and arrays of term names as values
1102 * - any other fields supported by wp_insert_post()
1103 * @return string|IXR_Error post_id
1105 public function wp_newPost( $args ) {
1106 if ( ! $this->minimum_args( $args, 4 ) )
1107 return $this->error;
1109 $this->escape( $args );
1111 $username = $args[1];
1112 $password = $args[2];
1113 $content_struct = $args[3];
1115 if ( ! $user = $this->login( $username, $password ) )
1116 return $this->error;
1118 // convert the date field back to IXR form
1119 if ( isset( $content_struct['post_date'] ) && ! is_a( $content_struct['post_date'], 'IXR_Date' ) ) {
1120 $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
1123 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1124 // since _insert_post will ignore the non-GMT date if the GMT date is set
1125 if ( isset( $content_struct['post_date_gmt'] ) && ! is_a( $content_struct['post_date_gmt'], 'IXR_Date' ) ) {
1126 if ( $content_struct['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) {
1127 unset( $content_struct['post_date_gmt'] );
1129 $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
1133 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1134 do_action( 'xmlrpc_call', 'wp.newPost' );
1136 unset( $content_struct['ID'] );
1138 return $this->_insert_post( $user, $content_struct );
1142 * Helper method for filtering out elements from an array.
1146 * @param int $count Number to compare to one.
1148 private function _is_greater_than_one( $count ) {
1153 * Helper method for wp_newPost and wp_editPost, containing shared logic.
1156 * @uses wp_insert_post()
1158 * @param WP_User $user The post author if post_author isn't set in $content_struct.
1159 * @param array|IXR_Error $content_struct Post data to insert.
1161 protected function _insert_post( $user, $content_struct ) {
1162 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1163 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1165 $post_data = wp_parse_args( $content_struct, $defaults );
1167 $post_type = get_post_type_object( $post_data['post_type'] );
1169 return new IXR_Error( 403, __( 'Invalid post type' ) );
1171 $update = ! empty( $post_data['ID'] );
1174 if ( ! get_post( $post_data['ID'] ) )
1175 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1176 if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1177 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1178 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1179 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1181 if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1182 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1185 switch ( $post_data['post_status'] ) {
1190 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1191 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1195 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1196 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1199 if ( ! get_post_status_object( $post_data['post_status'] ) )
1200 $post_data['post_status'] = 'draft';
1204 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1205 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1207 $post_data['post_author'] = absint( $post_data['post_author'] );
1208 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1209 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1210 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1212 $author = get_userdata( $post_data['post_author'] );
1215 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1217 $post_data['post_author'] = $user->ID;
1220 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1221 unset( $post_data['comment_status'] );
1223 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1224 unset( $post_data['ping_status'] );
1226 // Do some timestamp voodoo
1227 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1228 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1229 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1230 } elseif ( ! empty( $post_data['post_date'] ) ) {
1231 $dateCreated = $post_data['post_date']->getIso();
1234 if ( ! empty( $dateCreated ) ) {
1235 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1236 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1239 if ( ! isset( $post_data['ID'] ) )
1240 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1241 $post_ID = $post_data['ID'];
1243 if ( $post_data['post_type'] == 'post' ) {
1244 // Private and password-protected posts cannot be stickied.
1245 if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
1246 // Error if the client tried to stick the post, otherwise, silently unstick.
1247 if ( ! empty( $post_data['sticky'] ) )
1248 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1250 unstick_post( $post_ID );
1251 } elseif ( isset( $post_data['sticky'] ) ) {
1252 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1253 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1254 if ( $post_data['sticky'] )
1255 stick_post( $post_ID );
1257 unstick_post( $post_ID );
1261 if ( isset( $post_data['post_thumbnail'] ) ) {
1262 // empty value deletes, non-empty value adds/updates
1263 if ( ! $post_data['post_thumbnail'] )
1264 delete_post_thumbnail( $post_ID );
1265 elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1266 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1267 set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1268 unset( $content_struct['post_thumbnail'] );
1271 if ( isset( $post_data['custom_fields'] ) )
1272 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1274 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1275 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1277 // accumulate term IDs from terms and terms_names
1280 // first validate the terms specified by ID
1281 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1282 $taxonomies = array_keys( $post_data['terms'] );
1284 // validating term ids
1285 foreach ( $taxonomies as $taxonomy ) {
1286 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1287 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1289 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1290 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1292 $term_ids = $post_data['terms'][$taxonomy];
1293 $terms[ $taxonomy ] = array();
1294 foreach ( $term_ids as $term_id ) {
1295 $term = get_term_by( 'id', $term_id, $taxonomy );
1298 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1300 $terms[$taxonomy][] = (int) $term_id;
1305 // now validate terms specified by name
1306 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1307 $taxonomies = array_keys( $post_data['terms_names'] );
1309 foreach ( $taxonomies as $taxonomy ) {
1310 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1311 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1313 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1314 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1316 // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
1317 $ambiguous_terms = array();
1318 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1319 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1321 // count the number of terms with the same name
1322 $tax_term_names_count = array_count_values( $tax_term_names );
1324 // filter out non-ambiguous term names
1325 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1327 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1330 $term_names = $post_data['terms_names'][$taxonomy];
1331 foreach ( $term_names as $term_name ) {
1332 if ( in_array( $term_name, $ambiguous_terms ) )
1333 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1335 $term = get_term_by( 'name', $term_name, $taxonomy );
1338 // term doesn't exist, so check that the user is allowed to create new terms
1339 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1340 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1342 // create the new term
1343 $term_info = wp_insert_term( $term_name, $taxonomy );
1344 if ( is_wp_error( $term_info ) )
1345 return new IXR_Error( 500, $term_info->get_error_message() );
1347 $terms[$taxonomy][] = (int) $term_info['term_id'];
1349 $terms[$taxonomy][] = (int) $term->term_id;
1355 $post_data['tax_input'] = $terms;
1356 unset( $post_data['terms'], $post_data['terms_names'] );
1358 // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
1359 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1362 if ( isset( $post_data['post_format'] ) ) {
1363 $format = set_post_format( $post_ID, $post_data['post_format'] );
1365 if ( is_wp_error( $format ) )
1366 return new IXR_Error( 500, $format->get_error_message() );
1368 unset( $post_data['post_format'] );
1371 // Handle enclosures
1372 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1373 $this->add_enclosure_if_new( $post_ID, $enclosure );
1375 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1378 * Filter post data array to be inserted via XML-RPC.
1382 * @param array $post_data Parsed array of post data.
1383 * @param array $content_struct Post data array.
1385 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1387 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1388 if ( is_wp_error( $post_ID ) )
1389 return new IXR_Error( 500, $post_ID->get_error_message() );
1392 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1394 return strval( $post_ID );
1398 * Edit a post for any registered post type.
1400 * The $content_struct parameter only needs to contain fields that
1401 * should be changed. All other fields will retain their existing values.
1405 * @param array $args Method parameters. Contains:
1406 * - int $blog_id (unused)
1407 * - string $username
1408 * - string $password
1410 * - array $content_struct
1411 * @return bool|IXR_Error true on success
1413 public function wp_editPost( $args ) {
1414 if ( ! $this->minimum_args( $args, 5 ) )
1415 return $this->error;
1417 $this->escape( $args );
1419 $username = $args[1];
1420 $password = $args[2];
1421 $post_id = (int) $args[3];
1422 $content_struct = $args[4];
1424 if ( ! $user = $this->login( $username, $password ) )
1425 return $this->error;
1427 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1428 do_action( 'xmlrpc_call', 'wp.editPost' );
1430 $post = get_post( $post_id, ARRAY_A );
1432 if ( empty( $post['ID'] ) )
1433 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1435 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1436 // If the post has been modified since the date provided, return an error.
1437 if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1438 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1442 // convert the date field back to IXR form
1443 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1445 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1446 // since _insert_post will ignore the non-GMT date if the GMT date is set
1447 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1448 unset( $post['post_date_gmt'] );
1450 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1452 $this->escape( $post );
1453 $merged_content_struct = array_merge( $post, $content_struct );
1455 $retval = $this->_insert_post( $user, $merged_content_struct );
1456 if ( $retval instanceof IXR_Error )
1463 * Delete a post for any registered post type.
1467 * @uses wp_delete_post()
1468 * @param array $args Method parameters. Contains:
1469 * - int $blog_id (unused)
1470 * - string $username
1471 * - string $password
1473 * @return bool|IXR_Error true on success
1475 public function wp_deletePost( $args ) {
1476 if ( ! $this->minimum_args( $args, 4 ) )
1477 return $this->error;
1479 $this->escape( $args );
1481 $username = $args[1];
1482 $password = $args[2];
1483 $post_id = (int) $args[3];
1485 if ( ! $user = $this->login( $username, $password ) )
1486 return $this->error;
1488 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1489 do_action( 'xmlrpc_call', 'wp.deletePost' );
1491 $post = get_post( $post_id, ARRAY_A );
1492 if ( empty( $post['ID'] ) )
1493 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1495 if ( ! current_user_can( 'delete_post', $post_id ) )
1496 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1498 $result = wp_delete_post( $post_id );
1501 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1511 * The optional $fields parameter specifies what fields will be included
1512 * in the response array. This should be a list of field names. 'post_id' will
1513 * always be included in the response regardless of the value of $fields.
1515 * Instead of, or in addition to, individual field names, conceptual group
1516 * names can be used to specify multiple fields. The available conceptual
1517 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1521 * @param array $args Method parameters. Contains:
1522 * - int $blog_id (unset)
1523 * - string $username
1524 * - string $password
1526 * - array $fields optional
1527 * @return array|IXR_Error Array contains (based on $fields parameter):
1533 * - 'post_modified_gmt'
1542 * - 'comment_status'
1551 public function wp_getPost( $args ) {
1552 if ( ! $this->minimum_args( $args, 4 ) )
1553 return $this->error;
1555 $this->escape( $args );
1557 $username = $args[1];
1558 $password = $args[2];
1559 $post_id = (int) $args[3];
1561 if ( isset( $args[4] ) ) {
1565 * Filter the list of post query fields used by the given XML-RPC method.
1569 * @param array $fields Array of post fields.
1570 * @param string $method Method name.
1572 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1575 if ( ! $user = $this->login( $username, $password ) )
1576 return $this->error;
1578 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1579 do_action( 'xmlrpc_call', 'wp.getPost' );
1581 $post = get_post( $post_id, ARRAY_A );
1583 if ( empty( $post['ID'] ) )
1584 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1586 if ( ! current_user_can( 'edit_post', $post_id ) )
1587 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1589 return $this->_prepare_post( $post, $fields );
1597 * The optional $filter parameter modifies the query used to retrieve posts.
1598 * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
1599 * 'orderby', and 'order'.
1601 * The optional $fields parameter specifies what fields will be included
1602 * in the response array.
1604 * @uses wp_get_recent_posts()
1605 * @see wp_getPost() for more on $fields
1606 * @see get_posts() for more on $filter values
1608 * @param array $args Method parameters. Contains:
1609 * - int $blog_id (unused)
1610 * - string $username
1611 * - string $password
1612 * - array $filter optional
1613 * - array $fields optional
1614 * @return array|IXR_Error Array contains a collection of posts.
1616 public function wp_getPosts( $args ) {
1617 if ( ! $this->minimum_args( $args, 3 ) )
1618 return $this->error;
1620 $this->escape( $args );
1622 $username = $args[1];
1623 $password = $args[2];
1624 $filter = isset( $args[3] ) ? $args[3] : array();
1626 if ( isset( $args[4] ) ) {
1629 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1630 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1633 if ( ! $user = $this->login( $username, $password ) )
1634 return $this->error;
1636 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1637 do_action( 'xmlrpc_call', 'wp.getPosts' );
1641 if ( isset( $filter['post_type'] ) ) {
1642 $post_type = get_post_type_object( $filter['post_type'] );
1643 if ( ! ( (bool) $post_type ) )
1644 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1646 $post_type = get_post_type_object( 'post' );
1649 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1650 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1652 $query['post_type'] = $post_type->name;
1654 if ( isset( $filter['post_status'] ) )
1655 $query['post_status'] = $filter['post_status'];
1657 if ( isset( $filter['number'] ) )
1658 $query['numberposts'] = absint( $filter['number'] );
1660 if ( isset( $filter['offset'] ) )
1661 $query['offset'] = absint( $filter['offset'] );
1663 if ( isset( $filter['orderby'] ) ) {
1664 $query['orderby'] = $filter['orderby'];
1666 if ( isset( $filter['order'] ) )
1667 $query['order'] = $filter['order'];
1670 if ( isset( $filter['s'] ) ) {
1671 $query['s'] = $filter['s'];
1674 $posts_list = wp_get_recent_posts( $query );
1676 if ( ! $posts_list )
1679 // holds all the posts data
1682 foreach ( $posts_list as $post ) {
1683 if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1686 $struct[] = $this->_prepare_post( $post, $fields );
1693 * Create a new term.
1697 * @uses wp_insert_term()
1698 * @param array $args Method parameters. Contains:
1699 * - int $blog_id (unused)
1700 * - string $username
1701 * - string $password
1702 * - array $content_struct
1703 * The $content_struct must contain:
1706 * Also, it can optionally contain:
1710 * @return string|IXR_Error term_id
1712 public function wp_newTerm( $args ) {
1713 if ( ! $this->minimum_args( $args, 4 ) )
1714 return $this->error;
1716 $this->escape( $args );
1718 $username = $args[1];
1719 $password = $args[2];
1720 $content_struct = $args[3];
1722 if ( ! $user = $this->login( $username, $password ) )
1723 return $this->error;
1725 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1726 do_action( 'xmlrpc_call', 'wp.newTerm' );
1728 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1729 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1731 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1733 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1734 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1736 $taxonomy = (array) $taxonomy;
1738 // hold the data of the term
1739 $term_data = array();
1741 $term_data['name'] = trim( $content_struct['name'] );
1742 if ( empty( $term_data['name'] ) )
1743 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1745 if ( isset( $content_struct['parent'] ) ) {
1746 if ( ! $taxonomy['hierarchical'] )
1747 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1749 $parent_term_id = (int) $content_struct['parent'];
1750 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1752 if ( is_wp_error( $parent_term ) )
1753 return new IXR_Error( 500, $parent_term->get_error_message() );
1755 if ( ! $parent_term )
1756 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1758 $term_data['parent'] = $content_struct['parent'];
1761 if ( isset( $content_struct['description'] ) )
1762 $term_data['description'] = $content_struct['description'];
1764 if ( isset( $content_struct['slug'] ) )
1765 $term_data['slug'] = $content_struct['slug'];
1767 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1769 if ( is_wp_error( $term ) )
1770 return new IXR_Error( 500, $term->get_error_message() );
1773 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1775 return strval( $term['term_id'] );
1783 * @uses wp_update_term()
1784 * @param array $args Method parameters. Contains:
1785 * - int $blog_id (unused)
1786 * - string $username
1787 * - string $password
1789 * - array $content_struct
1790 * The $content_struct must contain:
1792 * Also, it can optionally contain:
1797 * @return bool|IXR_Error True, on success.
1799 public function wp_editTerm( $args ) {
1800 if ( ! $this->minimum_args( $args, 5 ) )
1801 return $this->error;
1803 $this->escape( $args );
1805 $username = $args[1];
1806 $password = $args[2];
1807 $term_id = (int) $args[3];
1808 $content_struct = $args[4];
1810 if ( ! $user = $this->login( $username, $password ) )
1811 return $this->error;
1813 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1814 do_action( 'xmlrpc_call', 'wp.editTerm' );
1816 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1817 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1819 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1821 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1822 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1824 $taxonomy = (array) $taxonomy;
1826 // hold the data of the term
1827 $term_data = array();
1829 $term = get_term( $term_id , $content_struct['taxonomy'] );
1831 if ( is_wp_error( $term ) )
1832 return new IXR_Error( 500, $term->get_error_message() );
1835 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1837 if ( isset( $content_struct['name'] ) ) {
1838 $term_data['name'] = trim( $content_struct['name'] );
1840 if ( empty( $term_data['name'] ) )
1841 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1844 if ( isset( $content_struct['parent'] ) ) {
1845 if ( ! $taxonomy['hierarchical'] )
1846 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1848 $parent_term_id = (int) $content_struct['parent'];
1849 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1851 if ( is_wp_error( $parent_term ) )
1852 return new IXR_Error( 500, $parent_term->get_error_message() );
1854 if ( ! $parent_term )
1855 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1857 $term_data['parent'] = $content_struct['parent'];
1860 if ( isset( $content_struct['description'] ) )
1861 $term_data['description'] = $content_struct['description'];
1863 if ( isset( $content_struct['slug'] ) )
1864 $term_data['slug'] = $content_struct['slug'];
1866 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1868 if ( is_wp_error( $term ) )
1869 return new IXR_Error( 500, $term->get_error_message() );
1872 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1882 * @uses wp_delete_term()
1883 * @param array $args Method parameters. Contains:
1884 * - int $blog_id (unused)
1885 * - string $username
1886 * - string $password
1887 * - string $taxnomy_name
1889 * @return boolean|IXR_Error If it suceeded true else a reason why not
1891 public function wp_deleteTerm( $args ) {
1892 if ( ! $this->minimum_args( $args, 5 ) )
1893 return $this->error;
1895 $this->escape( $args );
1897 $username = $args[1];
1898 $password = $args[2];
1899 $taxonomy = $args[3];
1900 $term_id = (int) $args[4];
1902 if ( ! $user = $this->login( $username, $password ) )
1903 return $this->error;
1905 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1906 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1908 if ( ! taxonomy_exists( $taxonomy ) )
1909 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1911 $taxonomy = get_taxonomy( $taxonomy );
1913 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
1914 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
1916 $term = get_term( $term_id, $taxonomy->name );
1918 if ( is_wp_error( $term ) )
1919 return new IXR_Error( 500, $term->get_error_message() );
1922 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1924 $result = wp_delete_term( $term_id, $taxonomy->name );
1926 if ( is_wp_error( $result ) )
1927 return new IXR_Error( 500, $term->get_error_message() );
1930 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
1941 * @param array $args Method parameters. Contains:
1942 * - int $blog_id (unused)
1943 * - string $username
1944 * - string $password
1945 * - string $taxonomy
1947 * @return array|IXR_Error Array contains:
1952 * - 'term_taxonomy_id'
1958 public function wp_getTerm( $args ) {
1959 if ( ! $this->minimum_args( $args, 5 ) )
1960 return $this->error;
1962 $this->escape( $args );
1964 $username = $args[1];
1965 $password = $args[2];
1966 $taxonomy = $args[3];
1967 $term_id = (int) $args[4];
1969 if ( ! $user = $this->login( $username, $password ) )
1970 return $this->error;
1972 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1973 do_action( 'xmlrpc_call', 'wp.getTerm' );
1975 if ( ! taxonomy_exists( $taxonomy ) )
1976 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1978 $taxonomy = get_taxonomy( $taxonomy );
1980 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1981 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1983 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
1985 if ( is_wp_error( $term ) )
1986 return new IXR_Error( 500, $term->get_error_message() );
1989 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1991 return $this->_prepare_term( $term );
1995 * Retrieve all terms for a taxonomy.
1999 * The optional $filter parameter modifies the query used to retrieve terms.
2000 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
2003 * @param array $args Method parameters. Contains:
2004 * - int $blog_id (unused)
2005 * - string $username
2006 * - string $password
2007 * - string $taxonomy
2008 * - array $filter optional
2009 * @return array|IXR_Error terms
2011 public function wp_getTerms( $args ) {
2012 if ( ! $this->minimum_args( $args, 4 ) )
2013 return $this->error;
2015 $this->escape( $args );
2017 $username = $args[1];
2018 $password = $args[2];
2019 $taxonomy = $args[3];
2020 $filter = isset( $args[4] ) ? $args[4] : array();
2022 if ( ! $user = $this->login( $username, $password ) )
2023 return $this->error;
2025 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2026 do_action( 'xmlrpc_call', 'wp.getTerms' );
2028 if ( ! taxonomy_exists( $taxonomy ) )
2029 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2031 $taxonomy = get_taxonomy( $taxonomy );
2033 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2034 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2038 if ( isset( $filter['number'] ) )
2039 $query['number'] = absint( $filter['number'] );
2041 if ( isset( $filter['offset'] ) )
2042 $query['offset'] = absint( $filter['offset'] );
2044 if ( isset( $filter['orderby'] ) ) {
2045 $query['orderby'] = $filter['orderby'];
2047 if ( isset( $filter['order'] ) )
2048 $query['order'] = $filter['order'];
2051 if ( isset( $filter['hide_empty'] ) )
2052 $query['hide_empty'] = $filter['hide_empty'];
2054 $query['get'] = 'all';
2056 if ( isset( $filter['search'] ) )
2057 $query['search'] = $filter['search'];
2059 $terms = get_terms( $taxonomy->name, $query );
2061 if ( is_wp_error( $terms ) )
2062 return new IXR_Error( 500, $terms->get_error_message() );
2066 foreach ( $terms as $term ) {
2067 $struct[] = $this->_prepare_term( $term );
2074 * Retrieve a taxonomy.
2078 * @uses get_taxonomy()
2079 * @param array $args Method parameters. Contains:
2080 * - int $blog_id (unused)
2081 * - string $username
2082 * - string $password
2083 * - string $taxonomy
2084 * @return array|IXR_Error (@see get_taxonomy())
2086 public function wp_getTaxonomy( $args ) {
2087 if ( ! $this->minimum_args( $args, 4 ) )
2088 return $this->error;
2090 $this->escape( $args );
2092 $username = $args[1];
2093 $password = $args[2];
2094 $taxonomy = $args[3];
2096 if ( isset( $args[4] ) ) {
2100 * Filter the taxonomy query fields used by the given XML-RPC method.
2104 * @param array $fields An array of taxonomy fields to retrieve.
2105 * @param string $method The method name.
2107 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
2110 if ( ! $user = $this->login( $username, $password ) )
2111 return $this->error;
2113 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2114 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
2116 if ( ! taxonomy_exists( $taxonomy ) )
2117 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2119 $taxonomy = get_taxonomy( $taxonomy );
2121 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2122 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2124 return $this->_prepare_taxonomy( $taxonomy, $fields );
2128 * Retrieve all taxonomies.
2132 * @uses get_taxonomies()
2133 * @param array $args Method parameters. Contains:
2134 * - int $blog_id (unused)
2135 * - string $username
2136 * - string $password
2137 * @return array taxonomies
2139 public function wp_getTaxonomies( $args ) {
2140 if ( ! $this->minimum_args( $args, 3 ) )
2141 return $this->error;
2143 $this->escape( $args );
2145 $username = $args[1];
2146 $password = $args[2];
2147 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2149 if ( isset( $args[4] ) ) {
2152 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2153 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
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.getTaxonomies' );
2162 $taxonomies = get_taxonomies( $filter, 'objects' );
2164 // holds all the taxonomy data
2167 foreach ( $taxonomies as $taxonomy ) {
2168 // capability check for post_types
2169 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2172 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
2181 * The optional $fields parameter specifies what fields will be included
2182 * in the response array. This should be a list of field names. 'user_id' will
2183 * always be included in the response regardless of the value of $fields.
2185 * Instead of, or in addition to, individual field names, conceptual group
2186 * names can be used to specify multiple fields. The available conceptual
2187 * groups are 'basic' and 'all'.
2189 * @uses get_userdata()
2190 * @param array $args Method parameters. Contains:
2191 * - int $blog_id (unused)
2192 * - string $username
2193 * - string $password
2195 * - array $fields optional
2196 * @return array|IXR_Error Array contains (based on $fields parameter):
2210 public function wp_getUser( $args ) {
2211 if ( ! $this->minimum_args( $args, 4 ) )
2212 return $this->error;
2214 $this->escape( $args );
2216 $username = $args[1];
2217 $password = $args[2];
2218 $user_id = (int) $args[3];
2220 if ( isset( $args[4] ) ) {
2224 * Filter the default user query fields used by the given XML-RPC method.
2228 * @param array $fields User query fields for given method. Default 'all'.
2229 * @param string $method The method name.
2231 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2234 if ( ! $user = $this->login( $username, $password ) )
2235 return $this->error;
2237 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2238 do_action( 'xmlrpc_call', 'wp.getUser' );
2240 if ( ! current_user_can( 'edit_user', $user_id ) )
2241 return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2243 $user_data = get_userdata( $user_id );
2246 return new IXR_Error( 404, __( 'Invalid user ID' ) );
2248 return $this->_prepare_user( $user_data, $fields );
2254 * The optional $filter parameter modifies the query used to retrieve users.
2255 * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2256 * 'who', 'orderby', and 'order'.
2258 * The optional $fields parameter specifies what fields will be included
2259 * in the response array.
2262 * @see wp_getUser() for more on $fields and return values
2264 * @param array $args Method parameters. Contains:
2265 * - int $blog_id (unused)
2266 * - string $username
2267 * - string $password
2268 * - array $filter optional
2269 * - array $fields optional
2270 * @return array|IXR_Error users data
2272 public function wp_getUsers( $args ) {
2273 if ( ! $this->minimum_args( $args, 3 ) )
2274 return $this->error;
2276 $this->escape( $args );
2278 $username = $args[1];
2279 $password = $args[2];
2280 $filter = isset( $args[3] ) ? $args[3] : array();
2282 if ( isset( $args[4] ) ) {
2285 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2286 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2289 if ( ! $user = $this->login( $username, $password ) )
2290 return $this->error;
2292 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2293 do_action( 'xmlrpc_call', 'wp.getUsers' );
2295 if ( ! current_user_can( 'list_users' ) )
2296 return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
2298 $query = array( 'fields' => 'all_with_meta' );
2300 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2301 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2303 if ( isset( $filter['orderby'] ) ) {
2304 $query['orderby'] = $filter['orderby'];
2306 if ( isset( $filter['order'] ) )
2307 $query['order'] = $filter['order'];
2310 if ( isset( $filter['role'] ) ) {
2311 if ( get_role( $filter['role'] ) === null )
2312 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2314 $query['role'] = $filter['role'];
2317 if ( isset( $filter['who'] ) ) {
2318 $query['who'] = $filter['who'];
2321 $users = get_users( $query );
2324 foreach ( $users as $user_data ) {
2325 if ( current_user_can( 'edit_user', $user_data->ID ) )
2326 $_users[] = $this->_prepare_user( $user_data, $fields );
2332 * Retrieve information about the requesting user.
2334 * @uses get_userdata()
2335 * @param array $args Method parameters. Contains:
2336 * - int $blog_id (unused)
2337 * - string $username
2338 * - string $password
2339 * - array $fields optional
2340 * @return array|IXR_Error (@see wp_getUser)
2342 public function wp_getProfile( $args ) {
2343 if ( ! $this->minimum_args( $args, 3 ) )
2344 return $this->error;
2346 $this->escape( $args );
2348 $username = $args[1];
2349 $password = $args[2];
2351 if ( isset( $args[3] ) ) {
2354 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2355 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2358 if ( ! $user = $this->login( $username, $password ) )
2359 return $this->error;
2361 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2362 do_action( 'xmlrpc_call', 'wp.getProfile' );
2364 if ( ! current_user_can( 'edit_user', $user->ID ) )
2365 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2367 $user_data = get_userdata( $user->ID );
2369 return $this->_prepare_user( $user_data, $fields );
2373 * Edit user's profile.
2375 * @uses wp_update_user()
2376 * @param array $args Method parameters. Contains:
2377 * - int $blog_id (unused)
2378 * - string $username
2379 * - string $password
2380 * - array $content_struct
2381 * It can optionally contain:
2389 * @return bool|IXR_Error True, on success.
2391 public function wp_editProfile( $args ) {
2392 if ( ! $this->minimum_args( $args, 4 ) )
2393 return $this->error;
2395 $this->escape( $args );
2397 $username = $args[1];
2398 $password = $args[2];
2399 $content_struct = $args[3];
2401 if ( ! $user = $this->login( $username, $password ) )
2402 return $this->error;
2404 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2405 do_action( 'xmlrpc_call', 'wp.editProfile' );
2407 if ( ! current_user_can( 'edit_user', $user->ID ) )
2408 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2410 // holds data of the user
2411 $user_data = array();
2412 $user_data['ID'] = $user->ID;
2414 // only set the user details if it was given
2415 if ( isset( $content_struct['first_name'] ) )
2416 $user_data['first_name'] = $content_struct['first_name'];
2418 if ( isset( $content_struct['last_name'] ) )
2419 $user_data['last_name'] = $content_struct['last_name'];
2421 if ( isset( $content_struct['url'] ) )
2422 $user_data['user_url'] = $content_struct['url'];
2424 if ( isset( $content_struct['display_name'] ) )
2425 $user_data['display_name'] = $content_struct['display_name'];
2427 if ( isset( $content_struct['nickname'] ) )
2428 $user_data['nickname'] = $content_struct['nickname'];
2430 if ( isset( $content_struct['nicename'] ) )
2431 $user_data['user_nicename'] = $content_struct['nicename'];
2433 if ( isset( $content_struct['bio'] ) )
2434 $user_data['description'] = $content_struct['bio'];
2436 $result = wp_update_user( $user_data );
2438 if ( is_wp_error( $result ) )
2439 return new IXR_Error( 500, $result->get_error_message() );
2442 return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2452 * @param array $args Method parameters. Contains:
2453 * - blog_id (unused)
2457 * @return array|IXR_Error
2459 public function wp_getPage($args) {
2460 $this->escape($args);
2462 $page_id = (int) $args[1];
2463 $username = $args[2];
2464 $password = $args[3];
2466 if ( !$user = $this->login($username, $password) ) {
2467 return $this->error;
2470 $page = get_post($page_id);
2472 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2474 if ( !current_user_can( 'edit_page', $page_id ) )
2475 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2477 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2478 do_action( 'xmlrpc_call', 'wp.getPage' );
2480 // If we found the page then format the data.
2481 if ( $page->ID && ($page->post_type == 'page') ) {
2482 return $this->_prepare_page( $page );
2484 // If the page doesn't exist indicate that.
2486 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2495 * @param array $args Method parameters. Contains:
2496 * - blog_id (unused)
2500 * @return array|IXR_Error
2502 public function wp_getPages($args) {
2503 $this->escape($args);
2505 $username = $args[1];
2506 $password = $args[2];
2507 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
2509 if ( !$user = $this->login($username, $password) )
2510 return $this->error;
2512 if ( !current_user_can( 'edit_pages' ) )
2513 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2515 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2516 do_action( 'xmlrpc_call', 'wp.getPages' );
2518 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2519 $num_pages = count($pages);
2521 // If we have pages, put together their info.
2522 if ( $num_pages >= 1 ) {
2523 $pages_struct = array();
2525 foreach ($pages as $page) {
2526 if ( current_user_can( 'edit_page', $page->ID ) )
2527 $pages_struct[] = $this->_prepare_page( $page );
2530 return $pages_struct;
2541 * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
2542 * @return int|IXR_Error
2544 public function wp_newPage($args) {
2545 // Items not escaped here will be escaped in newPost.
2546 $username = $this->escape($args[1]);
2547 $password = $this->escape($args[2]);
2549 $publish = $args[4];
2551 if ( !$user = $this->login($username, $password) )
2552 return $this->error;
2554 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2555 do_action( 'xmlrpc_call', 'wp.newPage' );
2557 // Mark this as content for a page.
2558 $args[3]["post_type"] = 'page';
2560 // Let mw_newPost do all of the heavy lifting.
2561 return $this->mw_newPost( $args );
2569 * @param array $args Method parameters.
2570 * @return bool|IXR_Error True, if success.
2572 public function wp_deletePage($args) {
2573 $this->escape($args);
2575 $username = $args[1];
2576 $password = $args[2];
2577 $page_id = (int) $args[3];
2579 if ( !$user = $this->login($username, $password) )
2580 return $this->error;
2582 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2583 do_action( 'xmlrpc_call', 'wp.deletePage' );
2585 // Get the current page based on the page_id and
2586 // make sure it is a page and not a post.
2587 $actual_page = get_post($page_id, ARRAY_A);
2588 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2589 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2591 // Make sure the user can delete pages.
2592 if ( !current_user_can('delete_page', $page_id) )
2593 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete this page.' ) );
2595 // Attempt to delete the page.
2596 $result = wp_delete_post($page_id);
2598 return new IXR_Error( 500, __( 'Failed to delete the page.' ) );
2601 * Fires after a page has been successfully deleted via XML-RPC.
2605 * @param int $page_id ID of the deleted page.
2606 * @param array $args An array of arguments to delete the page.
2608 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2618 * @param array $args Method parameters.
2619 * @return array|IXR_Error
2621 public function wp_editPage($args) {
2622 // Items not escaped here will be escaped in editPost.
2623 $page_id = (int) $this->escape($args[1]);
2624 $username = $this->escape($args[2]);
2625 $password = $this->escape($args[3]);
2626 $content = $args[4];
2627 $publish = $args[5];
2629 if ( !$user = $this->login($username, $password) )
2630 return $this->error;
2632 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2633 do_action( 'xmlrpc_call', 'wp.editPage' );
2635 // Get the page data and make sure it is a page.
2636 $actual_page = get_post($page_id, ARRAY_A);
2637 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2638 return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2640 // Make sure the user is allowed to edit pages.
2641 if ( !current_user_can('edit_page', $page_id) )
2642 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this page.' ) );
2644 // Mark this as content for a page.
2645 $content['post_type'] = 'page';
2647 // Arrange args in the way mw_editPost understands.
2656 // Let mw_editPost do all of the heavy lifting.
2657 return $this->mw_editPost( $args );
2661 * Retrieve page list.
2665 * @param array $args Method parameters.
2666 * @return array|IXR_Error
2668 public function wp_getPageList($args) {
2671 $this->escape($args);
2673 $username = $args[1];
2674 $password = $args[2];
2676 if ( !$user = $this->login($username, $password) )
2677 return $this->error;
2679 if ( !current_user_can( 'edit_pages' ) )
2680 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2682 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2683 do_action( 'xmlrpc_call', 'wp.getPageList' );
2685 // Get list of pages ids and titles
2686 $page_list = $wpdb->get_results("
2688 post_title page_title,
2689 post_parent page_parent_id,
2694 WHERE post_type = 'page'
2698 // The date needs to be formatted properly.
2699 $num_pages = count($page_list);
2700 for ( $i = 0; $i < $num_pages; $i++ ) {
2701 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2702 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2704 unset($page_list[$i]->post_date_gmt);
2705 unset($page_list[$i]->post_date);
2706 unset($page_list[$i]->post_status);
2713 * Retrieve authors list.
2717 * @param array $args Method parameters.
2718 * @return array|IXR_Error
2720 public function wp_getAuthors($args) {
2722 $this->escape($args);
2724 $username = $args[1];
2725 $password = $args[2];
2727 if ( !$user = $this->login($username, $password) )
2728 return $this->error;
2730 if ( !current_user_can('edit_posts') )
2731 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
2733 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2734 do_action( 'xmlrpc_call', 'wp.getAuthors' );
2737 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2739 'user_id' => $user->ID,
2740 'user_login' => $user->user_login,
2741 'display_name' => $user->display_name
2749 * Get list of all tags
2753 * @param array $args Method parameters.
2754 * @return array|IXR_Error
2756 public function wp_getTags( $args ) {
2757 $this->escape( $args );
2759 $username = $args[1];
2760 $password = $args[2];
2762 if ( !$user = $this->login($username, $password) )
2763 return $this->error;
2765 if ( !current_user_can( 'edit_posts' ) )
2766 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2768 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2769 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2773 if ( $all_tags = get_tags() ) {
2774 foreach( (array) $all_tags as $tag ) {
2776 $struct['tag_id'] = $tag->term_id;
2777 $struct['name'] = $tag->name;
2778 $struct['count'] = $tag->count;
2779 $struct['slug'] = $tag->slug;
2780 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2781 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
2791 * Create new category.
2795 * @param array $args Method parameters.
2796 * @return int|IXR_Error Category ID.
2798 public function wp_newCategory($args) {
2799 $this->escape($args);
2801 $username = $args[1];
2802 $password = $args[2];
2803 $category = $args[3];
2805 if ( !$user = $this->login($username, $password) )
2806 return $this->error;
2808 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2809 do_action( 'xmlrpc_call', 'wp.newCategory' );
2811 // Make sure the user is allowed to add a category.
2812 if ( !current_user_can('manage_categories') )
2813 return new IXR_Error(401, __('Sorry, you do not have the right to add a category.'));
2815 // If no slug was provided make it empty so that
2816 // WordPress will generate one.
2817 if ( empty($category['slug']) )
2818 $category['slug'] = '';
2820 // If no parent_id was provided make it empty
2821 // so that it will be a top level page (no parent).
2822 if ( !isset($category['parent_id']) )
2823 $category['parent_id'] = '';
2825 // If no description was provided make it empty.
2826 if ( empty($category["description"]) )
2827 $category["description"] = "";
2829 $new_category = array(
2830 'cat_name' => $category['name'],
2831 'category_nicename' => $category['slug'],
2832 'category_parent' => $category['parent_id'],
2833 'category_description' => $category['description']
2836 $cat_id = wp_insert_category($new_category, true);
2837 if ( is_wp_error( $cat_id ) ) {
2838 if ( 'term_exists' == $cat_id->get_error_code() )
2839 return (int) $cat_id->get_error_data();
2841 return new IXR_Error(500, __('Sorry, the new category failed.'));
2842 } elseif ( ! $cat_id ) {
2843 return new IXR_Error(500, __('Sorry, the new category failed.'));
2847 * Fires after a new category has been successfully created via XML-RPC.
2851 * @param int $cat_id ID of the new category.
2852 * @param array $args An array of new category arguments.
2854 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
2864 * @param array $args Method parameters.
2865 * @return bool|IXR_Error See {@link wp_delete_term()} for return info.
2867 public function wp_deleteCategory($args) {
2868 $this->escape($args);
2870 $username = $args[1];
2871 $password = $args[2];
2872 $category_id = (int) $args[3];
2874 if ( !$user = $this->login($username, $password) )
2875 return $this->error;
2877 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2878 do_action( 'xmlrpc_call', 'wp.deleteCategory' );
2880 if ( !current_user_can('manage_categories') )
2881 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
2883 $status = wp_delete_term( $category_id, 'category' );
2885 if ( true == $status ) {
2887 * Fires after a category has been successfully deleted via XML-RPC.
2891 * @param int $category_id ID of the deleted category.
2892 * @param array $args An array of arguments to delete the category.
2894 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
2901 * Retrieve category list.
2905 * @param array $args Method parameters.
2906 * @return array|IXR_Error
2908 public function wp_suggestCategories($args) {
2909 $this->escape($args);
2911 $username = $args[1];
2912 $password = $args[2];
2913 $category = $args[3];
2914 $max_results = (int) $args[4];
2916 if ( !$user = $this->login($username, $password) )
2917 return $this->error;
2919 if ( !current_user_can( 'edit_posts' ) )
2920 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
2922 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2923 do_action( 'xmlrpc_call', 'wp.suggestCategories' );
2925 $category_suggestions = array();
2926 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
2927 foreach ( (array) get_categories($args) as $cat ) {
2928 $category_suggestions[] = array(
2929 'category_id' => $cat->term_id,
2930 'category_name' => $cat->name
2934 return $category_suggestions;
2942 * @param array $args Method parameters.
2943 * @return array|IXR_Error
2945 public function wp_getComment($args) {
2946 $this->escape($args);
2948 $username = $args[1];
2949 $password = $args[2];
2950 $comment_id = (int) $args[3];
2952 if ( !$user = $this->login($username, $password) )
2953 return $this->error;
2955 if ( !current_user_can( 'moderate_comments' ) )
2956 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2958 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2959 do_action( 'xmlrpc_call', 'wp.getComment' );
2961 if ( ! $comment = get_comment($comment_id) )
2962 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2964 return $this->_prepare_comment( $comment );
2968 * Retrieve comments.
2970 * Besides the common blog_id (unused), username, and password arguments, it takes a filter
2971 * array as last argument.
2973 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
2975 * The defaults are as follows:
2976 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
2977 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
2978 * - 'number' - Default is 10. Total number of media items to retrieve.
2979 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2983 * @param array $args Method parameters.
2984 * @return array|IXR_Error Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
2986 public function wp_getComments($args) {
2987 $this->escape($args);
2989 $username = $args[1];
2990 $password = $args[2];
2991 $struct = isset( $args[3] ) ? $args[3] : array();
2993 if ( !$user = $this->login($username, $password) )
2994 return $this->error;
2996 if ( !current_user_can( 'moderate_comments' ) )
2997 return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
2999 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3000 do_action( 'xmlrpc_call', 'wp.getComments' );
3002 if ( isset($struct['status']) )
3003 $status = $struct['status'];
3008 if ( isset($struct['post_id']) )
3009 $post_id = absint($struct['post_id']);
3012 if ( isset($struct['offset']) )
3013 $offset = absint($struct['offset']);
3016 if ( isset($struct['number']) )
3017 $number = absint($struct['number']);
3019 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
3021 $comments_struct = array();
3023 foreach ( $comments as $comment ) {
3024 $comments_struct[] = $this->_prepare_comment( $comment );
3027 return $comments_struct;
3033 * By default, the comment will be moved to the trash instead of deleted.
3034 * See {@link wp_delete_comment()} for more information on
3039 * @param array $args Method parameters. Contains:
3040 * - blog_id (unused)
3044 * @return bool|IXR_Error {@link wp_delete_comment()}
3046 public function wp_deleteComment($args) {
3047 $this->escape($args);
3049 $username = $args[1];
3050 $password = $args[2];
3051 $comment_ID = (int) $args[3];
3053 if ( !$user = $this->login($username, $password) )
3054 return $this->error;
3056 if ( !current_user_can( 'moderate_comments' ) )
3057 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3059 if ( ! get_comment($comment_ID) )
3060 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3062 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3063 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3065 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3066 do_action( 'xmlrpc_call', 'wp.deleteComment' );
3068 $status = wp_delete_comment( $comment_ID );
3070 if ( true == $status ) {
3072 * Fires after a comment has been successfully deleted via XML-RPC.
3076 * @param int $comment_ID ID of the deleted comment.
3077 * @param array $args An array of arguments to delete the comment.
3079 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
3088 * Besides the common blog_id (unused), username, and password arguments, it takes a
3089 * comment_id integer and a content_struct array as last argument.
3091 * The allowed keys in the content_struct array are:
3096 * - 'date_created_gmt'
3097 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
3101 * @param array $args. Contains:
3102 * - blog_id (unused)
3107 * @return bool|IXR_Error True, on success.
3109 public function wp_editComment($args) {
3110 $this->escape($args);
3112 $username = $args[1];
3113 $password = $args[2];
3114 $comment_ID = (int) $args[3];
3115 $content_struct = $args[4];
3117 if ( !$user = $this->login($username, $password) )
3118 return $this->error;
3120 if ( !current_user_can( 'moderate_comments' ) )
3121 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3123 if ( ! get_comment($comment_ID) )
3124 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3126 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3127 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3129 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3130 do_action( 'xmlrpc_call', 'wp.editComment' );
3132 if ( isset($content_struct['status']) ) {
3133 $statuses = get_comment_statuses();
3134 $statuses = array_keys($statuses);
3136 if ( ! in_array($content_struct['status'], $statuses) )
3137 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
3138 $comment_approved = $content_struct['status'];
3141 // Do some timestamp voodoo
3142 if ( !empty( $content_struct['date_created_gmt'] ) ) {
3143 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
3144 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
3145 $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
3146 $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
3149 if ( isset($content_struct['content']) )
3150 $comment_content = $content_struct['content'];
3152 if ( isset($content_struct['author']) )
3153 $comment_author = $content_struct['author'];
3155 if ( isset($content_struct['author_url']) )
3156 $comment_author_url = $content_struct['author_url'];
3158 if ( isset($content_struct['author_email']) )
3159 $comment_author_email = $content_struct['author_email'];
3161 // We've got all the data -- post it:
3162 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
3164 $result = wp_update_comment($comment);
3165 if ( is_wp_error( $result ) )
3166 return new IXR_Error(500, $result->get_error_message());
3169 return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
3172 * Fires after a comment has been successfully updated via XML-RPC.
3176 * @param int $comment_ID ID of the updated comment.
3177 * @param array $args An array of arguments to update the comment.
3179 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
3185 * Create new comment.
3189 * @param array $args Method parameters.
3190 * @return int|IXR_Error {@link wp_new_comment()}
3192 public function wp_newComment($args) {
3193 $this->escape($args);
3195 $username = $args[1];
3196 $password = $args[2];
3198 $content_struct = $args[4];
3201 * Filter whether to allow anonymous comments over XML-RPC.
3205 * @param bool $allow Whether to allow anonymous commenting via XML-RPC.
3208 $allow_anon = apply_filters( 'xmlrpc_allow_anonymous_comments', false );
3210 $user = $this->login($username, $password);
3214 if ( $allow_anon && get_option('comment_registration') )
3215 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
3216 else if ( !$allow_anon )
3217 return $this->error;
3222 if ( is_numeric($post) )
3223 $post_id = absint($post);
3225 $post_id = url_to_postid($post);
3228 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3230 if ( ! get_post($post_id) )
3231 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3234 $comment['comment_post_ID'] = $post_id;
3237 $comment['comment_author'] = $this->escape( $user->display_name );
3238 $comment['comment_author_email'] = $this->escape( $user->user_email );
3239 $comment['comment_author_url'] = $this->escape( $user->user_url );
3240 $comment['user_ID'] = $user->ID;
3242 $comment['comment_author'] = '';
3243 if ( isset($content_struct['author']) )
3244 $comment['comment_author'] = $content_struct['author'];
3246 $comment['comment_author_email'] = '';
3247 if ( isset($content_struct['author_email']) )
3248 $comment['comment_author_email'] = $content_struct['author_email'];
3250 $comment['comment_author_url'] = '';
3251 if ( isset($content_struct['author_url']) )
3252 $comment['comment_author_url'] = $content_struct['author_url'];
3254 $comment['user_ID'] = 0;
3256 if ( get_option('require_name_email') ) {
3257 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
3258 return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
3259 elseif ( !is_email($comment['comment_author_email']) )
3260 return new IXR_Error( 403, __( 'A valid email address is required' ) );
3264 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
3266 $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
3268 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3269 do_action( 'xmlrpc_call', 'wp.newComment' );
3271 $comment_ID = wp_new_comment( $comment );
3274 * Fires after a new comment has been successfully created via XML-RPC.
3278 * @param int $comment_ID ID of the new comment.
3279 * @param array $args An array of new comment arguments.
3281 do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
3287 * Retrieve all of the comment status.
3291 * @param array $args Method parameters.
3292 * @return array|IXR_Error
3294 public function wp_getCommentStatusList($args) {
3295 $this->escape( $args );
3297 $username = $args[1];
3298 $password = $args[2];
3300 if ( !$user = $this->login($username, $password) )
3301 return $this->error;
3303 if ( !current_user_can( 'moderate_comments' ) )
3304 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3306 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3307 do_action( 'xmlrpc_call', 'wp.getCommentStatusList' );
3309 return get_comment_statuses();
3313 * Retrieve comment count.
3317 * @param array $args Method parameters.
3318 * @return array|IXR_Error
3320 public function wp_getCommentCount( $args ) {
3321 $this->escape($args);
3323 $username = $args[1];
3324 $password = $args[2];
3325 $post_id = (int) $args[3];
3327 if ( !$user = $this->login($username, $password) )
3328 return $this->error;
3330 if ( !current_user_can( 'edit_posts' ) )
3331 return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
3333 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3334 do_action( 'xmlrpc_call', 'wp.getCommentCount' );
3336 $count = wp_count_comments( $post_id );
3338 'approved' => $count->approved,
3339 'awaiting_moderation' => $count->moderated,
3340 'spam' => $count->spam,
3341 'total_comments' => $count->total_comments
3346 * Retrieve post statuses.
3350 * @param array $args Method parameters.
3351 * @return array|IXR_Error
3353 public function wp_getPostStatusList( $args ) {
3354 $this->escape( $args );
3356 $username = $args[1];
3357 $password = $args[2];
3359 if ( !$user = $this->login($username, $password) )
3360 return $this->error;
3362 if ( !current_user_can( 'edit_posts' ) )
3363 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3365 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3366 do_action( 'xmlrpc_call', 'wp.getPostStatusList' );
3368 return get_post_statuses();
3372 * Retrieve page statuses.
3376 * @param array $args Method parameters.
3377 * @return array|IXR_Error
3379 public function wp_getPageStatusList( $args ) {
3380 $this->escape( $args );
3382 $username = $args[1];
3383 $password = $args[2];
3385 if ( !$user = $this->login($username, $password) )
3386 return $this->error;
3388 if ( !current_user_can( 'edit_pages' ) )
3389 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3391 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3392 do_action( 'xmlrpc_call', 'wp.getPageStatusList' );
3394 return get_page_statuses();
3398 * Retrieve page templates.
3402 * @param array $args Method parameters.
3403 * @return array|IXR_Error
3405 public function wp_getPageTemplates( $args ) {
3406 $this->escape( $args );
3408 $username = $args[1];
3409 $password = $args[2];
3411 if ( !$user = $this->login($username, $password) )
3412 return $this->error;
3414 if ( !current_user_can( 'edit_pages' ) )
3415 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3417 $templates = get_page_templates();
3418 $templates['Default'] = 'default';
3424 * Retrieve blog options.
3428 * @param array $args Method parameters.
3429 * @return array|IXR_Error
3431 public function wp_getOptions( $args ) {
3432 $this->escape( $args );
3434 $username = $args[1];
3435 $password = $args[2];
3436 $options = isset( $args[3] ) ? (array) $args[3] : array();
3438 if ( !$user = $this->login($username, $password) )
3439 return $this->error;
3441 // If no specific options where asked for, return all of them
3442 if ( count( $options ) == 0 )
3443 $options = array_keys($this->blog_options);
3445 return $this->_getOptions($options);
3449 * Retrieve blog options value from list.
3453 * @param array $options Options to retrieve.
3456 public function _getOptions($options) {
3458 $can_manage = current_user_can( 'manage_options' );
3459 foreach ( $options as $option ) {
3460 if ( array_key_exists( $option, $this->blog_options ) ) {
3461 $data[$option] = $this->blog_options[$option];
3462 //Is the value static or dynamic?
3463 if ( isset( $data[$option]['option'] ) ) {
3464 $data[$option]['value'] = get_option( $data[$option]['option'] );
3465 unset($data[$option]['option']);
3468 if ( ! $can_manage )
3469 $data[$option]['readonly'] = true;
3477 * Update blog options.
3481 * @param array $args Method parameters.
3482 * @return array|IXR_Error
3484 public function wp_setOptions( $args ) {
3485 $this->escape( $args );
3487 $username = $args[1];
3488 $password = $args[2];
3489 $options = (array) $args[3];
3491 if ( !$user = $this->login($username, $password) )
3492 return $this->error;
3494 if ( !current_user_can( 'manage_options' ) )
3495 return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
3497 $option_names = array();
3498 foreach ( $options as $o_name => $o_value ) {
3499 $option_names[] = $o_name;
3500 if ( !array_key_exists( $o_name, $this->blog_options ) )
3503 if ( $this->blog_options[$o_name]['readonly'] == true )
3506 update_option( $this->blog_options[$o_name]['option'], wp_unslash( $o_value ) );
3509 //Now return the updated values
3510 return $this->_getOptions($option_names);
3514 * Retrieve a media item by ID
3518 * @param array $args Method parameters. Contains:
3519 * - blog_id (unused)
3523 * @return array|IXR_Error Associative array contains:
3524 * - 'date_created_gmt'
3533 public function wp_getMediaItem($args) {
3534 $this->escape($args);
3536 $username = $args[1];
3537 $password = $args[2];
3538 $attachment_id = (int) $args[3];
3540 if ( !$user = $this->login($username, $password) )
3541 return $this->error;
3543 if ( !current_user_can( 'upload_files' ) )
3544 return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
3546 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3547 do_action( 'xmlrpc_call', 'wp.getMediaItem' );
3549 if ( ! $attachment = get_post($attachment_id) )
3550 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
3552 return $this->_prepare_media_item( $attachment );
3556 * Retrieves a collection of media library items (or attachments)
3558 * Besides the common blog_id (unused), username, and password arguments, it takes a filter
3559 * array as last argument.
3561 * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
3563 * The defaults are as follows:
3564 * - 'number' - Default is 5. Total number of media items to retrieve.
3565 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
3566 * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
3567 * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
3571 * @param array $args Method parameters. Contains:
3572 * - blog_id (unused)
3576 * @return array|IXR_Error Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
3578 public function wp_getMediaLibrary($args) {
3579 $this->escape($args);
3581 $username = $args[1];
3582 $password = $args[2];
3583 $struct = isset( $args[3] ) ? $args[3] : array() ;
3585 if ( !$user = $this->login($username, $password) )
3586 return $this->error;
3588 if ( !current_user_can( 'upload_files' ) )
3589 return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
3591 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3592 do_action( 'xmlrpc_call', 'wp.getMediaLibrary' );
3594 $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
3595 $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
3596 $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
3597 $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
3599 $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
3601 $attachments_struct = array();
3603 foreach ($attachments as $attachment )
3604 $attachments_struct[] = $this->_prepare_media_item( $attachment );
3606 return $attachments_struct;
3610 * Retrieves a list of post formats used by the site
3614 * @param array $args Method parameters. Contains:
3615 * - blog_id (unused)
3618 * @return array|IXR_Error
3620 public function wp_getPostFormats( $args ) {
3621 $this->escape( $args );
3623 $username = $args[1];
3624 $password = $args[2];
3626 if ( !$user = $this->login( $username, $password ) )
3627 return $this->error;
3629 if ( !current_user_can( 'edit_posts' ) )
3630 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3632 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3633 do_action( 'xmlrpc_call', 'wp.getPostFormats' );
3635 $formats = get_post_format_strings();
3637 # find out if they want a list of currently supports formats
3638 if ( isset( $args[3] ) && is_array( $args[3] ) ) {
3639 if ( $args[3]['show-supported'] ) {
3640 if ( current_theme_supports( 'post-formats' ) ) {
3641 $supported = get_theme_support( 'post-formats' );
3644 $data['all'] = $formats;
3645 $data['supported'] = $supported[0];
3656 * Retrieves a post type
3660 * @uses get_post_type_object()
3661 * @param array $args Method parameters. Contains:
3662 * - int $blog_id (unused)
3663 * - string $username
3664 * - string $password
3665 * - string $post_type_name
3667 * @return array|IXR_Error Array contains:
3670 * - 'capability_type'
3678 public function wp_getPostType( $args ) {
3679 if ( ! $this->minimum_args( $args, 4 ) )
3680 return $this->error;
3682 $this->escape( $args );
3684 $username = $args[1];
3685 $password = $args[2];
3686 $post_type_name = $args[3];
3688 if ( isset( $args[4] ) ) {
3692 * Filter the default query fields used by the given XML-RPC method.
3696 * @param array $fields An array of post type query fields for the given method.
3697 * @param string $method The method name.
3699 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
3702 if ( !$user = $this->login( $username, $password ) )
3703 return $this->error;
3705 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3706 do_action( 'xmlrpc_call', 'wp.getPostType' );
3708 if( ! post_type_exists( $post_type_name ) )
3709 return new IXR_Error( 403, __( 'Invalid post type' ) );
3711 $post_type = get_post_type_object( $post_type_name );
3713 if( ! current_user_can( $post_type->cap->edit_posts ) )
3714 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
3716 return $this->_prepare_post_type( $post_type, $fields );
3720 * Retrieves a post types
3724 * @uses get_post_types()
3725 * @param array $args Method parameters. Contains:
3726 * - int $blog_id (unused)
3727 * - string $username
3728 * - string $password
3731 * @return array|IXR_Error
3733 public function wp_getPostTypes( $args ) {
3734 if ( ! $this->minimum_args( $args, 3 ) )
3735 return $this->error;
3737 $this->escape( $args );
3739 $username = $args[1];
3740 $password = $args[2];
3741 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
3743 if ( isset( $args[4] ) ) {
3746 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3747 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
3750 if ( ! $user = $this->login( $username, $password ) )
3751 return $this->error;
3753 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3754 do_action( 'xmlrpc_call', 'wp.getPostTypes' );
3756 $post_types = get_post_types( $filter, 'objects' );
3760 foreach( $post_types as $post_type ) {
3761 if( ! current_user_can( $post_type->cap->edit_posts ) )
3764 $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
3771 * Retrieve revisions for a specific post.
3775 * The optional $fields parameter specifies what fields will be included
3776 * in the response array.
3778 * @uses wp_get_post_revisions()
3779 * @see wp_getPost() for more on $fields
3781 * @param array $args Method parameters. Contains:
3782 * - int $blog_id (unused)
3783 * - string $username
3784 * - string $password
3787 * @return array|IXR_Error contains a collection of posts.
3789 public function wp_getRevisions( $args ) {
3790 if ( ! $this->minimum_args( $args, 4 ) )
3791 return $this->error;
3793 $this->escape( $args );
3795 $username = $args[1];
3796 $password = $args[2];
3797 $post_id = (int) $args[3];
3799 if ( isset( $args[4] ) ) {
3803 * Filter the default revision query fields used by the given XML-RPC method.
3807 * @param array $field An array of revision query fields.
3808 * @param string $method The method name.
3810 $fields = apply_filters( 'xmlrpc_default_revision_fields', array( 'post_date', 'post_date_gmt' ), 'wp.getRevisions' );
3813 if ( ! $user = $this->login( $username, $password ) )
3814 return $this->error;
3816 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3817 do_action( 'xmlrpc_call', 'wp.getRevisions' );
3819 if ( ! $post = get_post( $post_id ) )
3820 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3822 if ( ! current_user_can( 'edit_post', $post_id ) )
3823 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
3825 // Check if revisions are enabled.
3826 if ( ! wp_revisions_enabled( $post ) )
3827 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
3829 $revisions = wp_get_post_revisions( $post_id );
3836 foreach ( $revisions as $revision ) {
3837 if ( ! current_user_can( 'read_post', $revision->ID ) )
3841 if ( wp_is_post_autosave( $revision ) )
3844 $struct[] = $this->_prepare_post( get_object_vars( $revision ), $fields );
3851 * Restore a post revision
3855 * @uses wp_restore_post_revision()
3857 * @param array $args Method parameters. Contains:
3858 * - int $blog_id (unused)
3859 * - string $username
3860 * - string $password
3862 * @return bool|IXR_Error false if there was an error restoring, true if success.
3864 public function wp_restoreRevision( $args ) {
3865 if ( ! $this->minimum_args( $args, 3 ) )
3866 return $this->error;
3868 $this->escape( $args );
3870 $username = $args[1];
3871 $password = $args[2];
3872 $revision_id = (int) $args[3];
3874 if ( ! $user = $this->login( $username, $password ) )
3875 return $this->error;
3877 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3878 do_action( 'xmlrpc_call', 'wp.restoreRevision' );
3880 if ( ! $revision = wp_get_post_revision( $revision_id ) )
3881 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3883 if ( wp_is_post_autosave( $revision ) )
3884 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3886 if ( ! $post = get_post( $revision->post_parent ) )
3887 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3889 if ( ! current_user_can( 'edit_post', $revision->post_parent ) )
3890 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
3892 // Check if revisions are disabled.
3893 if ( ! wp_revisions_enabled( $post ) )
3894 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
3896 $post = wp_restore_post_revision( $revision_id );
3898 return (bool) $post;
3901 /* Blogger API functions.
3902 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
3906 * Retrieve blogs that user owns.
3908 * Will make more sense once we support multiple blogs.
3912 * @param array $args Method parameters.
3913 * @return array|IXR_Error
3915 public function blogger_getUsersBlogs($args) {
3916 if ( is_multisite() )
3917 return $this->_multisite_getUsersBlogs($args);
3919 $this->escape($args);
3921 $username = $args[1];
3922 $password = $args[2];
3924 if ( !$user = $this->login($username, $password) )
3925 return $this->error;
3927 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3928 do_action( 'xmlrpc_call', 'blogger.getUsersBlogs' );
3930 $is_admin = current_user_can('manage_options');
3933 'isAdmin' => $is_admin,
3934 'url' => get_option('home') . '/',
3936 'blogName' => get_option('blogname'),
3937 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
3940 return array($struct);
3944 * Private function for retrieving a users blogs for multisite setups
3948 * @return array|IXR_Error
3950 protected function _multisite_getUsersBlogs($args) {
3951 $current_blog = get_blog_details();
3953 $domain = $current_blog->domain;
3954 $path = $current_blog->path . 'xmlrpc.php';
3956 $rpc = new IXR_Client( set_url_scheme( "http://{$domain}{$path}" ) );
3957 $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
3958 $blogs = $rpc->getResponse();
3960 if ( isset($blogs['faultCode']) )
3961 return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
3963 if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
3966 foreach ( (array) $blogs as $blog ) {
3967 if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
3968 return array($blog);
3975 * Retrieve user's data.
3977 * Gives your client some info about you, so you don't have to.
3981 * @param array $args Method parameters.
3982 * @return array|IXR_Error
3984 public function blogger_getUserInfo($args) {
3986 $this->escape($args);
3988 $username = $args[1];
3989 $password = $args[2];
3991 if ( !$user = $this->login($username, $password) )
3992 return $this->error;
3994 if ( !current_user_can( 'edit_posts' ) )
3995 return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
3997 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3998 do_action( 'xmlrpc_call', 'blogger.getUserInfo' );
4001 'nickname' => $user->nickname,
4002 'userid' => $user->ID,
4003 'url' => $user->user_url,
4004 'lastname' => $user->last_name,
4005 'firstname' => $user->first_name
4016 * @param array $args Method parameters.
4017 * @return array|IXR_Error
4019 public function blogger_getPost($args) {
4021 $this->escape($args);
4023 $post_ID = (int) $args[1];
4024 $username = $args[2];
4025 $password = $args[3];
4027 if ( !$user = $this->login($username, $password) )
4028 return $this->error;
4030 $post_data = get_post($post_ID, ARRAY_A);
4032 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4034 if ( !current_user_can( 'edit_post', $post_ID ) )
4035 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4037 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4038 do_action( 'xmlrpc_call', 'blogger.getPost' );
4040 $categories = implode(',', wp_get_post_categories($post_ID));
4042 $content = '<title>'.wp_unslash($post_data['post_title']).'</title>';
4043 $content .= '<category>'.$categories.'</category>';
4044 $content .= wp_unslash($post_data['post_content']);
4047 'userid' => $post_data['post_author'],
4048 'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
4049 'content' => $content,
4050 'postid' => (string) $post_data['ID']
4057 * Retrieve list of recent posts.
4061 * @param array $args Method parameters.
4062 * @return array|IXR_Error
4064 public function blogger_getRecentPosts($args) {
4066 $this->escape($args);
4068 // $args[0] = appkey - ignored
4069 $username = $args[2];
4070 $password = $args[3];
4071 if ( isset( $args[4] ) )
4072 $query = array( 'numberposts' => absint( $args[4] ) );
4076 if ( !$user = $this->login($username, $password) )
4077 return $this->error;
4079 if ( ! current_user_can( 'edit_posts' ) )
4080 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
4082 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4083 do_action( 'xmlrpc_call', 'blogger.getRecentPosts' );
4085 $posts_list = wp_get_recent_posts( $query );
4087 if ( !$posts_list ) {
4088 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
4089 return $this->error;
4092 $recent_posts = array();
4093 foreach ($posts_list as $entry) {
4094 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
4097 $post_date = $this->_convert_date( $entry['post_date'] );
4098 $categories = implode(',', wp_get_post_categories($entry['ID']));
4100 $content = '<title>'.wp_unslash($entry['post_title']).'</title>';
4101 $content .= '<category>'.$categories.'</category>';
4102 $content .= wp_unslash($entry['post_content']);
4104 $recent_posts[] = array(
4105 'userid' => $entry['post_author'],
4106 'dateCreated' => $post_date,
4107 'content' => $content,
4108 'postid' => (string) $entry['ID'],
4112 return $recent_posts;
4122 public function blogger_getTemplate($args) {
4123 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4133 public function blogger_setTemplate($args) {
4134 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4142 * @param array $args Method parameters.
4143 * @return int|IXR_Error
4145 public function blogger_newPost($args) {
4147 $this->escape($args);
4149 $username = $args[2];
4150 $password = $args[3];
4151 $content = $args[4];
4152 $publish = $args[5];
4154 if ( !$user = $this->login($username, $password) )
4155 return $this->error;
4157 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4158 do_action( 'xmlrpc_call', 'blogger.newPost' );
4160 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
4161 if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) || !current_user_can($cap) )
4162 return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
4164 $post_status = ($publish) ? 'publish' : 'draft';
4166 $post_author = $user->ID;
4168 $post_title = xmlrpc_getposttitle($content);
4169 $post_category = xmlrpc_getpostcategory($content);
4170 $post_content = xmlrpc_removepostdata($content);
4172 $post_date = current_time('mysql');
4173 $post_date_gmt = current_time('mysql', 1);
4175 $post_data = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
4177 $post_ID = wp_insert_post($post_data);
4178 if ( is_wp_error( $post_ID ) )
4179 return new IXR_Error(500, $post_ID->get_error_message());
4182 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4184 $this->attach_uploads( $post_ID, $post_content );
4187 * Fires after a new post has been successfully created via the XML-RPC Blogger API.
4191 * @param int $post_ID ID of the new post.
4192 * @param array $args An array of new post arguments.
4194 do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
4204 * @param array $args Method parameters.
4205 * @return bool|IXR_Error true when done.
4207 public function blogger_editPost( $args ) {
4209 $this->escape($args);
4211 $post_ID = (int) $args[1];
4212 $username = $args[2];
4213 $password = $args[3];
4214 $content = $args[4];
4215 $publish = $args[5];
4217 if ( ! $user = $this->login( $username, $password ) ) {
4218 return $this->error;
4221 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4222 do_action( 'xmlrpc_call', 'blogger.editPost' );
4224 $actual_post = get_post( $post_ID, ARRAY_A );
4226 if ( ! $actual_post || $actual_post['post_type'] != 'post' ) {
4227 return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
4230 $this->escape($actual_post);
4232 if ( ! current_user_can( 'edit_post', $post_ID ) ) {
4233 return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
4235 if ( 'publish' == $actual_post['post_status'] && ! current_user_can( 'publish_posts' ) ) {
4236 return new IXR_Error( 401, __( 'Sorry, you do not have the right to publish this post.' ) );
4239 $postdata = array();
4240 $postdata['ID'] = $actual_post['ID'];
4241 $postdata['post_content'] = xmlrpc_removepostdata( $content );
4242 $postdata['post_title'] = xmlrpc_getposttitle( $content );
4243 $postdata['post_category'] = xmlrpc_getpostcategory( $content );
4244 $postdata['post_status'] = $actual_post['post_status'];
4245 $postdata['post_excerpt'] = $actual_post['post_excerpt'];
4247 $result = wp_update_post( $postdata );
4250 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
4252 $this->attach_uploads( $actual_post['ID'], $postdata['post_content'] );
4255 * Fires after a post has been successfully updated via the XML-RPC Blogger API.
4259 * @param int $post_ID ID of the updated post.
4260 * @param array $args An array of arguments for the post to edit.
4262 do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
4272 * @param array $args Method parameters.
4273 * @return bool|IXR_Error True when post is deleted.
4275 public function blogger_deletePost($args) {
4276 $this->escape($args);
4278 $post_ID = (int) $args[1];
4279 $username = $args[2];
4280 $password = $args[3];
4281 $publish = $args[4];
4283 if ( !$user = $this->login($username, $password) )
4284 return $this->error;
4286 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4287 do_action( 'xmlrpc_call', 'blogger.deletePost' );
4289 $actual_post = get_post($post_ID,ARRAY_A);
4291 if ( !$actual_post || $actual_post['post_type'] != 'post' )
4292 return new IXR_Error(404, __('Sorry, no such post.'));
4294 if ( !current_user_can('delete_post', $post_ID) )
4295 return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
4297 $result = wp_delete_post($post_ID);
4300 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
4303 * Fires after a post has been successfully deleted via the XML-RPC Blogger API.
4307 * @param int $post_ID ID of the deleted post.
4308 * @param array $args An array of arguments to delete the post.
4310 do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
4315 /* MetaWeblog API functions
4316 * specs on wherever Dave Winer wants them to be
4320 * Create a new post.
4322 * The 'content_struct' argument must contain:
4331 * Also, it can optionally contain:
4334 * - wp_page_parent_id
4337 * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
4338 * - mt_allow_comments - can be 'open' or 'closed'
4339 * - mt_allow_pings - can be 'open' or 'closed'
4340 * - date_created_gmt
4342 * - wp_post_thumbnail
4346 * @param array $args Method parameters. Contains:
4347 * - blog_id (unused)
4352 * @return int|IXR_Error
4354 public function mw_newPost($args) {
4355 $this->escape($args);
4357 $username = $args[1];
4358 $password = $args[2];
4359 $content_struct = $args[3];
4360 $publish = isset( $args[4] ) ? $args[4] : 0;
4362 if ( !$user = $this->login($username, $password) )
4363 return $this->error;
4365 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4366 do_action( 'xmlrpc_call', 'metaWeblog.newPost' );
4368 $page_template = '';
4369 if ( !empty( $content_struct['post_type'] ) ) {
4370 if ( $content_struct['post_type'] == 'page' ) {
4372 $cap = 'publish_pages';
4373 elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
4374 $cap = 'publish_pages';
4376 $cap = 'edit_pages';
4377 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
4378 $post_type = 'page';
4379 if ( !empty( $content_struct['wp_page_template'] ) )
4380 $page_template = $content_struct['wp_page_template'];
4381 } elseif ( $content_struct['post_type'] == 'post' ) {
4383 $cap = 'publish_posts';
4384 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
4385 $cap = 'publish_posts';
4387 $cap = 'edit_posts';
4388 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4389 $post_type = 'post';
4391 // No other post_type values are allowed here
4392 return new IXR_Error( 401, __( 'Invalid post type' ) );
4396 $cap = 'publish_posts';
4397 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
4398 $cap = 'publish_posts';
4400 $cap = 'edit_posts';
4401 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4402 $post_type = 'post';
4405 if ( ! current_user_can( get_post_type_object( $post_type )->cap->create_posts ) )
4406 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts on this site.' ) );
4407 if ( !current_user_can( $cap ) )
4408 return new IXR_Error( 401, $error_message );
4410 // Check for a valid post format if one was given
4411 if ( isset( $content_struct['wp_post_format'] ) ) {
4412 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4413 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4414 return new IXR_Error( 404, __( 'Invalid post format' ) );
4418 // Let WordPress generate the post_name (slug) unless
4419 // one has been provided.
4421 if ( isset($content_struct['wp_slug']) )
4422 $post_name = $content_struct['wp_slug'];
4424 // Only use a password if one was given.
4425 if ( isset($content_struct['wp_password']) )
4426 $post_password = $content_struct['wp_password'];
4428 // Only set a post parent if one was provided.
4429 if ( isset($content_struct['wp_page_parent_id']) )
4430 $post_parent = $content_struct['wp_page_parent_id'];
4432 // Only set the menu_order if it was provided.
4433 if ( isset($content_struct['wp_page_order']) )
4434 $menu_order = $content_struct['wp_page_order'];
4436 $post_author = $user->ID;
4438 // If an author id was provided then use it instead.
4439 if ( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) {
4440 switch ( $post_type ) {
4442 if ( !current_user_can( 'edit_others_posts' ) )
4443 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
4446 if ( !current_user_can( 'edit_others_pages' ) )
4447 return new IXR_Error( 401, __( 'You are not allowed to create pages as this user.' ) );
4450 return new IXR_Error( 401, __( 'Invalid post type' ) );
4453 $author = get_userdata( $content_struct['wp_author_id'] );
4455 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
4456 $post_author = $content_struct['wp_author_id'];
4459 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
4460 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
4462 $post_status = $publish ? 'publish' : 'draft';
4464 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4465 switch ( $content_struct["{$post_type}_status"] ) {
4470 $post_status = $content_struct["{$post_type}_status"];
4473 $post_status = $publish ? 'publish' : 'draft';
4478 $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
4479 $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
4481 $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
4483 if ( isset($content_struct['mt_allow_comments']) ) {
4484 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4485 switch ( $content_struct['mt_allow_comments'] ) {
4487 $comment_status = 'closed';
4490 $comment_status = 'open';
4493 $comment_status = get_option('default_comment_status');
4497 switch ( (int) $content_struct['mt_allow_comments'] ) {
4500 $comment_status = 'closed';
4503 $comment_status = 'open';
4506 $comment_status = get_option('default_comment_status');
4511 $comment_status = get_option('default_comment_status');
4514 if ( isset($content_struct['mt_allow_pings']) ) {
4515 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4516 switch ( $content_struct['mt_allow_pings'] ) {
4518 $ping_status = 'closed';
4521 $ping_status = 'open';
4524 $ping_status = get_option('default_ping_status');
4528 switch ( (int) $content_struct['mt_allow_pings'] ) {
4530 $ping_status = 'closed';
4533 $ping_status = 'open';
4536 $ping_status = get_option('default_ping_status');
4541 $ping_status = get_option('default_ping_status');
4545 $post_content = $post_content . '<!--more-->' . $post_more;
4548 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4549 $to_ping = $content_struct['mt_tb_ping_urls'];
4550 if ( is_array($to_ping) )
4551 $to_ping = implode(' ', $to_ping);
4554 // Do some timestamp voodoo
4555 if ( !empty( $content_struct['date_created_gmt'] ) )
4556 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4557 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4558 elseif ( !empty( $content_struct['dateCreated']) )
4559 $dateCreated = $content_struct['dateCreated']->getIso();
4561 if ( !empty( $dateCreated ) ) {
4562 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4563 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4565 $post_date = current_time('mysql');
4566 $post_date_gmt = current_time('mysql', 1);
4569 $post_category = array();
4570 if ( isset( $content_struct['categories'] ) ) {
4571 $catnames = $content_struct['categories'];
4573 if ( is_array($catnames) ) {
4574 foreach ($catnames as $cat) {
4575 $post_category[] = get_cat_ID($cat);
4580 $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');
4582 $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
4584 // Only posts can be sticky
4585 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4586 if ( $content_struct['sticky'] == true )
4587 stick_post( $post_ID );
4588 elseif ( $content_struct['sticky'] == false )
4589 unstick_post( $post_ID );
4592 if ( isset($content_struct['custom_fields']) )
4593 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4595 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4596 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4597 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4599 unset( $content_struct['wp_post_thumbnail'] );
4602 // Handle enclosures
4603 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4604 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4606 $this->attach_uploads( $post_ID, $post_content );
4608 // Handle post formats if assigned, value is validated earlier
4610 if ( isset( $content_struct['wp_post_format'] ) )
4611 set_post_format( $post_ID, $content_struct['wp_post_format'] );
4613 $post_ID = wp_insert_post( $postdata, true );
4614 if ( is_wp_error( $post_ID ) )
4615 return new IXR_Error(500, $post_ID->get_error_message());
4618 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4621 * Fires after a new post has been successfully created via the XML-RPC MovableType API.
4625 * @param int $post_ID ID of the new post.
4626 * @param array $args An array of arguments to create the new post.
4628 do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
4630 return strval($post_ID);
4634 * @param integer $post_ID
4635 * @param array $enclosure
4637 public function add_enclosure_if_new( $post_ID, $enclosure ) {
4638 if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
4639 $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'] . "\n";
4641 if ( $enclosures = get_post_meta( $post_ID, 'enclosure' ) ) {
4642 foreach ( $enclosures as $enc ) {
4643 // This method used to omit the trailing new line. #23219
4644 if ( rtrim( $enc, "\n" ) == rtrim( $encstring, "\n" ) ) {
4651 add_post_meta( $post_ID, 'enclosure', $encstring );
4656 * Attach upload to a post.
4660 * @param int $post_ID Post ID.
4661 * @param string $post_content Post Content for attachment.
4663 public function attach_uploads( $post_ID, $post_content ) {
4666 // find any unattached files
4667 $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
4668 if ( is_array( $attachments ) ) {
4669 foreach ( $attachments as $file ) {
4670 if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false )
4671 $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
4681 * @param array $args Method parameters.
4682 * @return bool|IXR_Error True on success.
4684 public function mw_editPost($args) {
4686 $this->escape($args);
4688 $post_ID = (int) $args[0];
4689 $username = $args[1];
4690 $password = $args[2];
4691 $content_struct = $args[3];
4692 $publish = isset( $args[4] ) ? $args[4] : 0;
4694 if ( ! $user = $this->login($username, $password) )
4695 return $this->error;
4697 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4698 do_action( 'xmlrpc_call', 'metaWeblog.editPost' );
4700 $postdata = get_post( $post_ID, ARRAY_A );
4702 // If there is no post data for the give post id, stop
4703 // now and return an error. Other wise a new post will be
4704 // created (which was the old behavior).
4705 if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
4706 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4708 if ( ! current_user_can( 'edit_post', $post_ID ) )
4709 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this post.' ) );
4711 // Use wp.editPost to edit post types other than post and page.
4712 if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
4713 return new IXR_Error( 401, __( 'Invalid post type' ) );
4715 // Thwart attempt to change the post type.
4716 if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
4717 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
4719 // Check for a valid post format if one was given
4720 if ( isset( $content_struct['wp_post_format'] ) ) {
4721 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4722 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4723 return new IXR_Error( 404, __( 'Invalid post format' ) );
4727 $this->escape($postdata);
4729 $ID = $postdata['ID'];
4730 $post_content = $postdata['post_content'];
4731 $post_title = $postdata['post_title'];
4732 $post_excerpt = $postdata['post_excerpt'];
4733 $post_password = $postdata['post_password'];
4734 $post_parent = $postdata['post_parent'];
4735 $post_type = $postdata['post_type'];
4736 $menu_order = $postdata['menu_order'];
4738 // Let WordPress manage slug if none was provided.
4740 $post_name = $postdata['post_name'];
4741 if ( isset($content_struct['wp_slug']) )
4742 $post_name = $content_struct['wp_slug'];
4744 // Only use a password if one was given.
4745 if ( isset($content_struct['wp_password']) )
4746 $post_password = $content_struct['wp_password'];
4748 // Only set a post parent if one was given.
4749 if ( isset($content_struct['wp_page_parent_id']) )
4750 $post_parent = $content_struct['wp_page_parent_id'];
4752 // Only set the menu_order if it was given.
4753 if ( isset($content_struct['wp_page_order']) )
4754 $menu_order = $content_struct['wp_page_order'];
4756 $page_template = null;
4757 if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
4758 $page_template = $content_struct['wp_page_template'];
4760 $post_author = $postdata['post_author'];
4762 // Only set the post_author if one is set.
4763 if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
4764 switch ( $post_type ) {
4766 if ( !current_user_can('edit_others_posts') )
4767 return new IXR_Error( 401, __( 'You are not allowed to change the post author as this user.' ) );
4770 if ( !current_user_can('edit_others_pages') )
4771 return new IXR_Error( 401, __( 'You are not allowed to change the page author as this user.' ) );
4774 return new IXR_Error( 401, __( 'Invalid post type' ) );
4777 $post_author = $content_struct['wp_author_id'];
4780 if ( isset($content_struct['mt_allow_comments']) ) {
4781 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4782 switch ( $content_struct['mt_allow_comments'] ) {
4784 $comment_status = 'closed';
4787 $comment_status = 'open';
4790 $comment_status = get_option('default_comment_status');
4794 switch ( (int) $content_struct['mt_allow_comments'] ) {
4797 $comment_status = 'closed';
4800 $comment_status = 'open';
4803 $comment_status = get_option('default_comment_status');
4809 if ( isset($content_struct['mt_allow_pings']) ) {
4810 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4811 switch ( $content_struct['mt_allow_pings'] ) {
4813 $ping_status = 'closed';
4816 $ping_status = 'open';
4819 $ping_status = get_option('default_ping_status');
4823 switch ( (int) $content_struct["mt_allow_pings"] ) {
4825 $ping_status = 'closed';
4828 $ping_status = 'open';
4831 $ping_status = get_option('default_ping_status');
4837 if ( isset( $content_struct['title'] ) )
4838 $post_title = $content_struct['title'];
4840 if ( isset( $content_struct['description'] ) )
4841 $post_content = $content_struct['description'];
4843 $post_category = array();
4844 if ( isset( $content_struct['categories'] ) ) {
4845 $catnames = $content_struct['categories'];
4846 if ( is_array($catnames) ) {
4847 foreach ($catnames as $cat) {
4848 $post_category[] = get_cat_ID($cat);
4853 if ( isset( $content_struct['mt_excerpt'] ) )
4854 $post_excerpt = $content_struct['mt_excerpt'];
4856 $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
4858 $post_status = $publish ? 'publish' : 'draft';
4859 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4860 switch( $content_struct["{$post_type}_status"] ) {
4865 $post_status = $content_struct["{$post_type}_status"];
4868 $post_status = $publish ? 'publish' : 'draft';
4873 $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
4875 if ( ('publish' == $post_status) ) {
4876 if ( ( 'page' == $post_type ) && !current_user_can('publish_pages') )
4877 return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
4878 else if ( !current_user_can('publish_posts') )
4879 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
4883 $post_content = $post_content . "<!--more-->" . $post_more;
4886 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4887 $to_ping = $content_struct['mt_tb_ping_urls'];
4888 if ( is_array($to_ping) )
4889 $to_ping = implode(' ', $to_ping);
4892 // Do some timestamp voodoo
4893 if ( !empty( $content_struct['date_created_gmt'] ) )
4894 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4895 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4896 elseif ( !empty( $content_struct['dateCreated']) )
4897 $dateCreated = $content_struct['dateCreated']->getIso();
4899 if ( !empty( $dateCreated ) ) {
4900 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4901 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4903 $post_date = $postdata['post_date'];
4904 $post_date_gmt = $postdata['post_date_gmt'];
4907 // We've got all the data -- post it:
4908 $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');
4910 $result = wp_update_post($newpost, true);
4911 if ( is_wp_error( $result ) )
4912 return new IXR_Error(500, $result->get_error_message());
4915 return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
4917 // Only posts can be sticky
4918 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4919 if ( $content_struct['sticky'] == true )
4920 stick_post( $post_ID );
4921 elseif ( $content_struct['sticky'] == false )
4922 unstick_post( $post_ID );
4925 if ( isset($content_struct['custom_fields']) )
4926 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4928 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4929 // empty value deletes, non-empty value adds/updates
4930 if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
4931 delete_post_thumbnail( $post_ID );
4933 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4934 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4936 unset( $content_struct['wp_post_thumbnail'] );
4939 // Handle enclosures
4940 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4941 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4943 $this->attach_uploads( $ID, $post_content );
4945 // Handle post formats if assigned, validation is handled
4946 // earlier in this function
4947 if ( isset( $content_struct['wp_post_format'] ) )
4948 set_post_format( $post_ID, $content_struct['wp_post_format'] );
4951 * Fires after a post has been successfully updated via the XML-RPC MovableType API.
4955 * @param int $post_ID ID of the updated post.
4956 * @param array $args An array of arguments to update the post.
4958 do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
4968 * @param array $args Method parameters.
4969 * @return array|IXR_Error
4971 public function mw_getPost($args) {
4973 $this->escape($args);
4975 $post_ID = (int) $args[0];
4976 $username = $args[1];
4977 $password = $args[2];
4979 if ( !$user = $this->login($username, $password) )
4980 return $this->error;
4982 $postdata = get_post($post_ID, ARRAY_A);
4984 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4986 if ( !current_user_can( 'edit_post', $post_ID ) )
4987 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4989 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4990 do_action( 'xmlrpc_call', 'metaWeblog.getPost' );
4992 if ($postdata['post_date'] != '') {
4993 $post_date = $this->_convert_date( $postdata['post_date'] );
4994 $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'], $postdata['post_date'] );
4995 $post_modified = $this->_convert_date( $postdata['post_modified'] );
4996 $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
4998 $categories = array();
4999 $catids = wp_get_post_categories($post_ID);
5000 foreach($catids as $catid)
5001 $categories[] = get_cat_name($catid);
5003 $tagnames = array();
5004 $tags = wp_get_post_tags( $post_ID );
5005 if ( !empty( $tags ) ) {
5006 foreach ( $tags as $tag )
5007 $tagnames[] = $tag->name;
5008 $tagnames = implode( ', ', $tagnames );
5013 $post = get_extended($postdata['post_content']);
5014 $link = post_permalink($postdata['ID']);
5016 // Get the author info.
5017 $author = get_userdata($postdata['post_author']);
5019 $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
5020 $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
5022 // Consider future posts as published
5023 if ( $postdata['post_status'] === 'future' )
5024 $postdata['post_status'] = 'publish';
5027 $post_format = get_post_format( $post_ID );
5028 if ( empty( $post_format ) )
5029 $post_format = 'standard';
5032 if ( is_sticky( $post_ID ) )
5035 $enclosure = array();
5036 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
5037 if ($key == 'enclosure') {
5038 foreach ( (array) $val as $enc ) {
5039 $encdata = explode("\n", $enc);
5040 $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
5041 $enclosure['length'] = (int) trim($encdata[1]);
5042 $enclosure['type'] = trim($encdata[2]);
5049 'dateCreated' => $post_date,
5050 'userid' => $postdata['post_author'],
5051 'postid' => $postdata['ID'],
5052 'description' => $post['main'],
5053 'title' => $postdata['post_title'],
5055 'permaLink' => $link,
5056 // commented out because no other tool seems to use this
5057 // 'content' => $entry['post_content'],
5058 'categories' => $categories,
5059 'mt_excerpt' => $postdata['post_excerpt'],
5060 'mt_text_more' => $post['extended'],
5061 'wp_more_text' => $post['more_text'],
5062 'mt_allow_comments' => $allow_comments,
5063 'mt_allow_pings' => $allow_pings,
5064 'mt_keywords' => $tagnames,
5065 'wp_slug' => $postdata['post_name'],
5066 'wp_password' => $postdata['post_password'],
5067 'wp_author_id' => (string) $author->ID,
5068 'wp_author_display_name' => $author->display_name,
5069 'date_created_gmt' => $post_date_gmt,
5070 'post_status' => $postdata['post_status'],
5071 'custom_fields' => $this->get_custom_fields($post_ID),
5072 'wp_post_format' => $post_format,
5073 'sticky' => $sticky,
5074 'date_modified' => $post_modified,
5075 'date_modified_gmt' => $post_modified_gmt
5078 if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
5080 $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
5084 return new IXR_Error(404, __('Sorry, no such post.'));
5089 * Retrieve list of recent posts.
5093 * @param array $args Method parameters.
5094 * @return array|IXR_Error
5096 public function mw_getRecentPosts($args) {
5098 $this->escape($args);
5100 $username = $args[1];
5101 $password = $args[2];
5102 if ( isset( $args[3] ) )
5103 $query = array( 'numberposts' => absint( $args[3] ) );
5107 if ( !$user = $this->login($username, $password) )
5108 return $this->error;
5110 if ( ! current_user_can( 'edit_posts' ) )
5111 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
5113 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5114 do_action( 'xmlrpc_call', 'metaWeblog.getRecentPosts' );
5116 $posts_list = wp_get_recent_posts( $query );
5121 $recent_posts = array();
5122 foreach ($posts_list as $entry) {
5123 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5126 $post_date = $this->_convert_date( $entry['post_date'] );
5127 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5128 $post_modified = $this->_convert_date( $entry['post_modified'] );
5129 $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
5131 $categories = array();
5132 $catids = wp_get_post_categories($entry['ID']);
5133 foreach( $catids as $catid )
5134 $categories[] = get_cat_name($catid);
5136 $tagnames = array();
5137 $tags = wp_get_post_tags( $entry['ID'] );
5138 if ( !empty( $tags ) ) {
5139 foreach ( $tags as $tag ) {
5140 $tagnames[] = $tag->name;
5142 $tagnames = implode( ', ', $tagnames );
5147 $post = get_extended($entry['post_content']);
5148 $link = post_permalink($entry['ID']);
5150 // Get the post author info.
5151 $author = get_userdata($entry['post_author']);
5153 $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
5154 $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
5156 // Consider future posts as published
5157 if ( $entry['post_status'] === 'future' )
5158 $entry['post_status'] = 'publish';
5161 $post_format = get_post_format( $entry['ID'] );
5162 if ( empty( $post_format ) )
5163 $post_format = 'standard';
5165 $recent_posts[] = array(
5166 'dateCreated' => $post_date,
5167 'userid' => $entry['post_author'],
5168 'postid' => (string) $entry['ID'],
5169 'description' => $post['main'],
5170 'title' => $entry['post_title'],
5172 'permaLink' => $link,
5173 // commented out because no other tool seems to use this
5174 // 'content' => $entry['post_content'],
5175 'categories' => $categories,
5176 'mt_excerpt' => $entry['post_excerpt'],
5177 'mt_text_more' => $post['extended'],
5178 'wp_more_text' => $post['more_text'],
5179 'mt_allow_comments' => $allow_comments,
5180 'mt_allow_pings' => $allow_pings,
5181 'mt_keywords' => $tagnames,
5182 'wp_slug' => $entry['post_name'],
5183 'wp_password' => $entry['post_password'],
5184 'wp_author_id' => (string) $author->ID,
5185 'wp_author_display_name' => $author->display_name,
5186 'date_created_gmt' => $post_date_gmt,
5187 'post_status' => $entry['post_status'],
5188 'custom_fields' => $this->get_custom_fields($entry['ID']),
5189 'wp_post_format' => $post_format,
5190 'date_modified' => $post_modified,
5191 'date_modified_gmt' => $post_modified_gmt,
5192 'sticky' => ( $entry['post_type'] === 'post' && is_sticky( $entry['ID'] ) ),
5193 'wp_post_thumbnail' => get_post_thumbnail_id( $entry['ID'] )
5197 return $recent_posts;
5201 * Retrieve the list of categories on a given blog.
5205 * @param array $args Method parameters.
5206 * @return array|IXR_Error
5208 public function mw_getCategories($args) {
5210 $this->escape($args);
5212 $username = $args[1];
5213 $password = $args[2];
5215 if ( !$user = $this->login($username, $password) )
5216 return $this->error;
5218 if ( !current_user_can( 'edit_posts' ) )
5219 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5221 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5222 do_action( 'xmlrpc_call', 'metaWeblog.getCategories' );
5224 $categories_struct = array();
5226 if ( $cats = get_categories(array('get' => 'all')) ) {
5227 foreach ( $cats as $cat ) {
5229 $struct['categoryId'] = $cat->term_id;
5230 $struct['parentId'] = $cat->parent;
5231 $struct['description'] = $cat->name;
5232 $struct['categoryDescription'] = $cat->description;
5233 $struct['categoryName'] = $cat->name;
5234 $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
5235 $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
5237 $categories_struct[] = $struct;
5241 return $categories_struct;
5245 * Uploads a file, following your settings.
5247 * Adapted from a patch by Johann Richard.
5249 * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
5253 * @param array $args Method parameters.
5254 * @return array|IXR_Error
5256 public function mw_newMediaObject($args) {
5259 $username = $this->escape($args[1]);
5260 $password = $this->escape($args[2]);
5263 $name = sanitize_file_name( $data['name'] );
5264 $type = $data['type'];
5265 $bits = $data['bits'];
5267 if ( !$user = $this->login($username, $password) )
5268 return $this->error;
5270 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5271 do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject' );
5273 if ( !current_user_can('upload_files') ) {
5274 $this->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
5275 return $this->error;
5279 * Filter whether to preempt the XML-RPC media upload.
5281 * Passing a truthy value will effectively short-circuit the media upload,
5282 * returning that value as a 500 error instead.
5286 * @param bool $error Whether to pre-empt the media upload. Default false.
5288 if ( $upload_err = apply_filters( 'pre_upload_error', false ) ) {
5289 return new IXR_Error( 500, $upload_err );
5292 if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) {
5293 // Get postmeta info on the object.
5294 $old_file = $wpdb->get_row("
5297 WHERE post_title = '{$name}'
5298 AND post_type = 'attachment'
5301 // Delete previous file.
5302 wp_delete_attachment($old_file->ID);
5304 // Make sure the new name is different by pre-pending the
5305 // previous post id.
5306 $filename = preg_replace('/^wpid\d+-/', '', $name);
5307 $name = "wpid{$old_file->ID}-{$filename}";
5310 $upload = wp_upload_bits($name, null, $bits);
5311 if ( ! empty($upload['error']) ) {
5312 $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
5313 return new IXR_Error(500, $errorString);
5315 // Construct the attachment array
5317 if ( ! empty( $data['post_id'] ) ) {
5318 $post_id = (int) $data['post_id'];
5320 if ( ! current_user_can( 'edit_post', $post_id ) )
5321 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
5323 $attachment = array(
5324 'post_title' => $name,
5325 'post_content' => '',
5326 'post_type' => 'attachment',
5327 'post_parent' => $post_id,
5328 'post_mime_type' => $type,
5329 'guid' => $upload[ 'url' ]
5333 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
5334 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
5337 * Fires after a new attachment has been added via the XML-RPC MovableType API.
5341 * @param int $id ID of the new attachment.
5342 * @param array $args An array of arguments to add the attachment.
5344 do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
5347 'id' => strval( $id ),
5349 'url' => $upload[ 'url' ],
5353 /** This filter is documented in wp-admin/includes/file.php */
5354 return apply_filters( 'wp_handle_upload', $struct, 'upload' );
5357 /* MovableType API functions
5358 * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
5362 * Retrieve the post titles of recent posts.
5366 * @param array $args Method parameters.
5367 * @return array|IXR_Error
5369 public function mt_getRecentPostTitles($args) {
5371 $this->escape($args);
5373 $username = $args[1];
5374 $password = $args[2];
5375 if ( isset( $args[3] ) )
5376 $query = array( 'numberposts' => absint( $args[3] ) );
5380 if ( !$user = $this->login($username, $password) )
5381 return $this->error;
5383 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5384 do_action( 'xmlrpc_call', 'mt.getRecentPostTitles' );
5386 $posts_list = wp_get_recent_posts( $query );
5388 if ( !$posts_list ) {
5389 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
5390 return $this->error;
5393 $recent_posts = array();
5395 foreach ($posts_list as $entry) {
5396 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5399 $post_date = $this->_convert_date( $entry['post_date'] );
5400 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5402 $recent_posts[] = array(
5403 'dateCreated' => $post_date,
5404 'userid' => $entry['post_author'],
5405 'postid' => (string) $entry['ID'],
5406 'title' => $entry['post_title'],
5407 'post_status' => $entry['post_status'],
5408 'date_created_gmt' => $post_date_gmt
5412 return $recent_posts;
5416 * Retrieve list of all categories on blog.
5420 * @param array $args Method parameters.
5421 * @return array|IXR_Error
5423 public function mt_getCategoryList($args) {
5425 $this->escape($args);
5427 $username = $args[1];
5428 $password = $args[2];
5430 if ( !$user = $this->login($username, $password) )
5431 return $this->error;
5433 if ( !current_user_can( 'edit_posts' ) )
5434 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5436 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5437 do_action( 'xmlrpc_call', 'mt.getCategoryList' );
5439 $categories_struct = array();
5441 if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
5442 foreach ( $cats as $cat ) {
5444 $struct['categoryId'] = $cat->term_id;
5445 $struct['categoryName'] = $cat->name;
5447 $categories_struct[] = $struct;
5451 return $categories_struct;
5455 * Retrieve post categories.
5459 * @param array $args Method parameters.
5460 * @return array|IXR_Error
5462 public function mt_getPostCategories($args) {
5464 $this->escape($args);
5466 $post_ID = (int) $args[0];
5467 $username = $args[1];
5468 $password = $args[2];
5470 if ( !$user = $this->login($username, $password) )
5471 return $this->error;
5473 if ( ! get_post( $post_ID ) )
5474 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5476 if ( !current_user_can( 'edit_post', $post_ID ) )
5477 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
5479 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5480 do_action( 'xmlrpc_call', 'mt.getPostCategories' );
5482 $categories = array();
5483 $catids = wp_get_post_categories(intval($post_ID));
5484 // first listed category will be the primary category
5486 foreach ( $catids as $catid ) {
5487 $categories[] = array(
5488 'categoryName' => get_cat_name($catid),
5489 'categoryId' => (string) $catid,
5490 'isPrimary' => $isPrimary
5499 * Sets categories for a post.
5503 * @param array $args Method parameters.
5504 * @return bool|IXR_Error True on success.
5506 public function mt_setPostCategories($args) {
5508 $this->escape($args);
5510 $post_ID = (int) $args[0];
5511 $username = $args[1];
5512 $password = $args[2];
5513 $categories = $args[3];
5515 if ( !$user = $this->login($username, $password) )
5516 return $this->error;
5518 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5519 do_action( 'xmlrpc_call', 'mt.setPostCategories' );
5521 if ( ! get_post( $post_ID ) )
5522 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5524 if ( !current_user_can('edit_post', $post_ID) )
5525 return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
5528 foreach ( $categories as $cat ) {
5529 $catids[] = $cat['categoryId'];
5532 wp_set_post_categories($post_ID, $catids);
5538 * Retrieve an array of methods supported by this server.
5542 * @param array $args Method parameters.
5545 public function mt_supportedMethods($args) {
5547 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5548 do_action( 'xmlrpc_call', 'mt.supportedMethods' );
5550 $supported_methods = array();
5551 foreach ( $this->methods as $key => $value ) {
5552 $supported_methods[] = $key;
5555 return $supported_methods;
5559 * Retrieve an empty array because we don't support per-post text filters.
5563 * @param array $args Method parameters.
5565 public function mt_supportedTextFilters($args) {
5566 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5567 do_action( 'xmlrpc_call', 'mt.supportedTextFilters' );
5570 * Filter the MoveableType text filters list for XML-RPC.
5574 * @param array $filters An array of text filters.
5576 return apply_filters( 'xmlrpc_text_filters', array() );
5580 * Retrieve trackbacks sent to a given post.
5584 * @param array $args Method parameters.
5585 * @return array|IXR_Error
5587 public function mt_getTrackbackPings($args) {
5591 $post_ID = intval($args);
5593 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5594 do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
5596 $actual_post = get_post($post_ID, ARRAY_A);
5598 if ( !$actual_post )
5599 return new IXR_Error(404, __('Sorry, no such post.'));
5601 $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) );
5606 $trackback_pings = array();
5607 foreach ( $comments as $comment ) {
5608 if ( 'trackback' == $comment->comment_type ) {
5609 $content = $comment->comment_content;
5610 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
5611 $trackback_pings[] = array(
5612 'pingTitle' => $title,
5613 'pingURL' => $comment->comment_author_url,
5614 'pingIP' => $comment->comment_author_IP
5619 return $trackback_pings;
5623 * Sets a post's publish status to 'publish'.
5627 * @param array $args Method parameters.
5628 * @return int|IXR_Error
5630 public function mt_publishPost($args) {
5632 $this->escape($args);
5634 $post_ID = (int) $args[0];
5635 $username = $args[1];
5636 $password = $args[2];
5638 if ( !$user = $this->login($username, $password) )
5639 return $this->error;
5641 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5642 do_action( 'xmlrpc_call', 'mt.publishPost' );
5644 $postdata = get_post($post_ID, ARRAY_A);
5646 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5648 if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
5649 return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
5651 $postdata['post_status'] = 'publish';
5654 $cats = wp_get_post_categories($post_ID);
5655 $postdata['post_category'] = $cats;
5656 $this->escape($postdata);
5658 $result = wp_update_post($postdata);
5663 /* PingBack functions
5664 * specs on www.hixie.ch/specs/pingback/pingback
5668 * Retrieves a pingback and registers it.
5672 * @param array $args Method parameters.
5673 * @return string|IXR_Error
5675 public function pingback_ping($args) {
5678 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5679 do_action( 'xmlrpc_call', 'pingback.ping' );
5681 $this->escape($args);
5683 $pagelinkedfrom = $args[0];
5684 $pagelinkedto = $args[1];
5688 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
5689 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
5690 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
5693 * Filter the pingback source URI.
5697 * @param string $pagelinkedfrom URI of the page linked from.
5698 * @param string $pagelinkedto URI of the page linked to.
5700 $pagelinkedfrom = apply_filters( 'pingback_ping_source_uri', $pagelinkedfrom, $pagelinkedto );
5702 if ( ! $pagelinkedfrom )
5703 return $this->pingback_error( 0, __( 'A valid URL was not provided.' ) );
5705 // Check if the page linked to is in our site
5706 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
5708 return $this->pingback_error( 0, __( 'Is there no link to us?' ) );
5710 // let's find which post is linked to
5711 // FIXME: does url_to_postid() cover all these cases already?
5712 // if so, then let's use it and drop the old code.
5713 $urltest = parse_url($pagelinkedto);
5714 if ( $post_ID = url_to_postid($pagelinkedto) ) {
5716 } elseif ( isset( $urltest['path'] ) && preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
5717 // the path defines the post_ID (archives/p/XXXX)
5718 $blah = explode('/', $match[0]);
5719 $post_ID = (int) $blah[1];
5720 } elseif ( isset( $urltest['query'] ) && preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
5721 // the querystring defines the post_ID (?p=XXXX)
5722 $blah = explode('=', $match[0]);
5723 $post_ID = (int) $blah[1];
5724 } elseif ( isset($urltest['fragment']) ) {
5725 // an #anchor is there, it's either...
5726 if ( intval($urltest['fragment']) ) {
5727 // ...an integer #XXXX (simplest case)
5728 $post_ID = (int) $urltest['fragment'];
5729 } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
5730 // ...a post id in the form 'post-###'
5731 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
5732 } elseif ( is_string($urltest['fragment']) ) {
5733 // ...or a string #title, a little more complicated
5734 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
5735 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
5736 if (! ($post_ID = $wpdb->get_var($sql)) ) {
5737 // returning unknown error '0' is better than die()ing
5738 return $this->pingback_error( 0, '' );
5742 // TODO: Attempt to extract a post ID from the given URL
5743 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.' ) );
5745 $post_ID = (int) $post_ID;
5747 $post = get_post($post_ID);
5749 if ( !$post ) // Post_ID not found
5750 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.' ) );
5752 if ( $post_ID == url_to_postid($pagelinkedfrom) )
5753 return $this->pingback_error( 0, __( 'The source URL and the target URL cannot both point to the same resource.' ) );
5755 // Check if pings are on
5756 if ( !pings_open($post) )
5757 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.' ) );
5759 // Let's check that the remote site didn't already pingback this entry
5760 if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
5761 return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
5763 // very stupid, but gives time to the 'from' server to publish !
5766 $remote_ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
5768 /** This filter is documented in wp-includes/class-http.php */
5769 $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $GLOBALS['wp_version'] . '; ' . get_bloginfo( 'url' ) );
5771 // Let's check the remote site
5772 $http_api_args = array(
5775 'limit_response_size' => 153600, // 150 KB
5776 'user-agent' => "$user_agent; verifying pingback from $remote_ip",
5778 'X-Pingback-Forwarded-For' => $remote_ip,
5781 $request = wp_safe_remote_get( $pagelinkedfrom, $http_api_args );
5782 $linea = wp_remote_retrieve_body( $request );
5785 return $this->pingback_error( 16, __( 'The source URL does not exist.' ) );
5788 * Filter the pingback remote source.
5792 * @param string $linea Response object for the page linked from.
5793 * @param string $pagelinkedto URL of the page linked to.
5795 $linea = apply_filters( 'pre_remote_source', $linea, $pagelinkedto );
5797 // Work around bug in strip_tags():
5798 $linea = str_replace('<!DOC', '<DOC', $linea);
5799 $linea = preg_replace( '/[\r\n\t ]+/', ' ', $linea ); // normalize spaces
5800 $linea = preg_replace( "/<\/*(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
5802 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
5803 $title = $matchtitle[1];
5804 if ( empty( $title ) )
5805 return $this->pingback_error( 32, __('We cannot find a title on that page.' ) );
5807 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
5809 $p = explode( "\n\n", $linea );
5811 $preg_target = preg_quote($pagelinkedto, '|');
5813 foreach ( $p as $para ) {
5814 if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
5815 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
5817 // If the URL isn't in a link context, keep looking
5818 if ( empty($context) )
5821 // We're going to use this fake tag to mark the context in a bit
5822 // the marker is needed in case the link text appears more than once in the paragraph
5823 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
5825 // prevent really long link text
5826 if ( strlen($context[1]) > 100 )
5827 $context[1] = substr($context[1], 0, 100) . '…';
5829 $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
5830 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
5831 $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
5832 $excerpt = trim($excerpt);
5833 $preg_marker = preg_quote($marker, '|');
5834 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
5835 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
5840 if ( empty($context) ) // Link to target not found
5841 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.' ) );
5843 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
5845 $context = '[…] ' . esc_html( $excerpt ) . ' […]';
5846 $pagelinkedfrom = $this->escape( $pagelinkedfrom );
5848 $comment_post_ID = (int) $post_ID;
5849 $comment_author = $title;
5850 $comment_author_email = '';
5851 $this->escape($comment_author);
5852 $comment_author_url = $pagelinkedfrom;
5853 $comment_content = $context;
5854 $this->escape($comment_content);
5855 $comment_type = 'pingback';
5857 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
5859 $comment_ID = wp_new_comment($commentdata);
5862 * Fires after a post pingback has been sent.
5866 * @param int $comment_ID Comment ID.
5868 do_action( 'pingback_post', $comment_ID );
5870 return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
5874 * Retrieve array of URLs that pingbacked the given URL.
5876 * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
5880 * @param array $args Method parameters.
5881 * @return array|IXR_Error
5883 public function pingback_extensions_getPingbacks($args) {
5887 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5888 do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' );
5890 $this->escape($args);
5894 $post_ID = url_to_postid($url);
5896 // We aren't sure that the resource is available and/or pingback enabled
5897 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.' ) );
5900 $actual_post = get_post($post_ID, ARRAY_A);
5902 if ( !$actual_post ) {
5903 // No such post = resource not found
5904 return $this->pingback_error( 32, __('The specified target URL does not exist.' ) );
5907 $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) );
5912 $pingbacks = array();
5913 foreach ( $comments as $comment ) {
5914 if ( 'pingback' == $comment->comment_type )
5915 $pingbacks[] = $comment->comment_author_url;
5922 * @param integer $code
5923 * @param string $message
5925 protected function pingback_error( $code, $message ) {
5927 * Filter the XML-RPC pingback error return.
5931 * @param IXR_Error $error An IXR_Error object containing the error code and message.
5933 return apply_filters( 'xmlrpc_pingback_error', new IXR_Error( $code, $message ) );