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 {
26 * Register all of the XMLRPC methods that XMLRPC server understands.
28 * Sets up server and method property. Passes XMLRPC
29 * methods through the 'xmlrpc_methods' filter to allow plugins to extend
30 * or replace XMLRPC methods.
34 * @return wp_xmlrpc_server
36 function __construct() {
37 $this->methods = array(
39 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
40 'wp.newPost' => 'this:wp_newPost',
41 'wp.editPost' => 'this:wp_editPost',
42 'wp.deletePost' => 'this:wp_deletePost',
43 'wp.getPost' => 'this:wp_getPost',
44 'wp.getPosts' => 'this:wp_getPosts',
45 'wp.newTerm' => 'this:wp_newTerm',
46 'wp.editTerm' => 'this:wp_editTerm',
47 'wp.deleteTerm' => 'this:wp_deleteTerm',
48 'wp.getTerm' => 'this:wp_getTerm',
49 'wp.getTerms' => 'this:wp_getTerms',
50 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
51 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
52 'wp.getUser' => 'this:wp_getUser',
53 'wp.getUsers' => 'this:wp_getUsers',
54 'wp.getProfile' => 'this:wp_getProfile',
55 'wp.editProfile' => 'this:wp_editProfile',
56 'wp.getPage' => 'this:wp_getPage',
57 'wp.getPages' => 'this:wp_getPages',
58 'wp.newPage' => 'this:wp_newPage',
59 'wp.deletePage' => 'this:wp_deletePage',
60 'wp.editPage' => 'this:wp_editPage',
61 'wp.getPageList' => 'this:wp_getPageList',
62 'wp.getAuthors' => 'this:wp_getAuthors',
63 'wp.getCategories' => 'this:mw_getCategories', // Alias
64 'wp.getTags' => 'this:wp_getTags',
65 'wp.newCategory' => 'this:wp_newCategory',
66 'wp.deleteCategory' => 'this:wp_deleteCategory',
67 'wp.suggestCategories' => 'this:wp_suggestCategories',
68 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
69 'wp.getCommentCount' => 'this:wp_getCommentCount',
70 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
71 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
72 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
73 'wp.getOptions' => 'this:wp_getOptions',
74 'wp.setOptions' => 'this:wp_setOptions',
75 'wp.getComment' => 'this:wp_getComment',
76 'wp.getComments' => 'this:wp_getComments',
77 'wp.deleteComment' => 'this:wp_deleteComment',
78 'wp.editComment' => 'this:wp_editComment',
79 'wp.newComment' => 'this:wp_newComment',
80 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
81 'wp.getMediaItem' => 'this:wp_getMediaItem',
82 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
83 'wp.getPostFormats' => 'this:wp_getPostFormats',
84 'wp.getPostType' => 'this:wp_getPostType',
85 'wp.getPostTypes' => 'this:wp_getPostTypes',
86 'wp.getRevisions' => 'this:wp_getRevisions',
87 'wp.restoreRevision' => 'this:wp_restoreRevision',
90 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
91 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
92 'blogger.getPost' => 'this:blogger_getPost',
93 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
94 'blogger.newPost' => 'this:blogger_newPost',
95 'blogger.editPost' => 'this:blogger_editPost',
96 'blogger.deletePost' => 'this:blogger_deletePost',
98 // MetaWeblog API (with MT extensions to structs)
99 'metaWeblog.newPost' => 'this:mw_newPost',
100 'metaWeblog.editPost' => 'this:mw_editPost',
101 'metaWeblog.getPost' => 'this:mw_getPost',
102 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
103 'metaWeblog.getCategories' => 'this:mw_getCategories',
104 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
106 // MetaWeblog API aliases for Blogger API
107 // see http://www.xmlrpc.com/stories/storyReader$2460
108 'metaWeblog.deletePost' => 'this:blogger_deletePost',
109 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
112 'mt.getCategoryList' => 'this:mt_getCategoryList',
113 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
114 'mt.getPostCategories' => 'this:mt_getPostCategories',
115 'mt.setPostCategories' => 'this:mt_setPostCategories',
116 'mt.supportedMethods' => 'this:mt_supportedMethods',
117 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
118 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
119 'mt.publishPost' => 'this:mt_publishPost',
122 'pingback.ping' => 'this:pingback_ping',
123 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
125 'demo.sayHello' => 'this:sayHello',
126 'demo.addTwoNumbers' => 'this:addTwoNumbers'
129 $this->initialise_blog_option_info();
132 * Filter the methods exposed by the XML-RPC server.
134 * This filter can be used to add new methods, and remove built-in methods.
138 * @param array $methods An array of XML-RPC methods.
140 $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
143 function serve_request() {
144 $this->IXR_Server($this->methods);
148 * Test XMLRPC API by saying, "Hello!" to client.
152 * @param array $args Method Parameters.
155 function sayHello($args) {
160 * Test XMLRPC API by adding two numbers for client.
164 * @param array $args Method Parameters.
167 function addTwoNumbers($args) {
170 return $number1 + $number2;
178 * @param string $username User's username.
179 * @param string $password User's password.
180 * @return mixed WP_User object if authentication passed, false otherwise
182 function login( $username, $password ) {
184 * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
185 * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
187 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
188 if ( false === $enabled ) {
189 $enabled = apply_filters( 'option_enable_xmlrpc', true );
193 * Filter whether XML-RPC is enabled.
195 * This is the proper filter for turning off XML-RPC.
199 * @param bool $enabled Whether XML-RPC is enabled. Default true.
201 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
204 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
208 $user = wp_authenticate($username, $password);
210 if (is_wp_error($user)) {
211 $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
214 * Filter the XML-RPC user login error message.
218 * @param string $error The XML-RPC error message.
219 * @param WP_User $user WP_User object.
221 $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
225 wp_set_current_user( $user->ID );
230 * Check user's credentials. Deprecated.
234 * @deprecated use wp_xmlrpc_server::login
235 * @see wp_xmlrpc_server::login
237 * @param string $username User's username.
238 * @param string $password User's password.
239 * @return bool Whether authentication passed.
241 function login_pass_ok( $username, $password ) {
242 return (bool) $this->login( $username, $password );
246 * Escape string or array of strings for database.
250 * @param string|array $data Escape single string or array of strings.
251 * @return string|array Type matches $data and sanitized for the database.
253 function escape( &$data ) {
254 if ( ! is_array( $data ) )
255 return wp_slash( $data );
257 foreach ( $data as &$v ) {
258 if ( is_array( $v ) )
260 elseif ( ! is_object( $v ) )
266 * Retrieve custom fields for post.
270 * @param int $post_id Post ID.
271 * @return array Custom fields, if exist.
273 function get_custom_fields($post_id) {
274 $post_id = (int) $post_id;
276 $custom_fields = array();
278 foreach ( (array) has_meta($post_id) as $meta ) {
279 // Don't expose protected fields.
280 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
283 $custom_fields[] = array(
284 "id" => $meta['meta_id'],
285 "key" => $meta['meta_key'],
286 "value" => $meta['meta_value']
290 return $custom_fields;
294 * Set custom fields for post.
298 * @param int $post_id Post ID.
299 * @param array $fields Custom fields.
301 function set_custom_fields($post_id, $fields) {
302 $post_id = (int) $post_id;
304 foreach ( (array) $fields as $meta ) {
305 if ( isset($meta['id']) ) {
306 $meta['id'] = (int) $meta['id'];
307 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
308 if ( isset($meta['key']) ) {
309 $meta['key'] = wp_unslash( $meta['key'] );
310 if ( $meta['key'] !== $pmeta->meta_key )
312 $meta['value'] = wp_unslash( $meta['value'] );
313 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
314 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
315 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
316 delete_metadata_by_mid( 'post', $meta['id'] );
318 } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
319 add_post_meta( $post_id, $meta['key'], $meta['value'] );
325 * Set up blog options property.
327 * Passes property through 'xmlrpc_blog_options' filter.
331 function initialise_blog_option_info() {
334 $this->blog_options = array(
336 'software_name' => array(
337 'desc' => __( 'Software Name' ),
339 'value' => 'WordPress'
341 'software_version' => array(
342 'desc' => __( 'Software Version' ),
344 'value' => $wp_version
347 'desc' => __( 'WordPress Address (URL)' ),
349 'option' => 'siteurl'
352 'desc' => __( 'Site Address (URL)' ),
356 'login_url' => array(
357 'desc' => __( 'Login Address (URL)' ),
359 'value' => wp_login_url( )
361 'admin_url' => array(
362 'desc' => __( 'The URL to the admin area' ),
364 'value' => get_admin_url( )
366 'image_default_link_type' => array(
367 'desc' => __( 'Image default link type' ),
369 'option' => 'image_default_link_type'
371 'image_default_size' => array(
372 'desc' => __( 'Image default size' ),
374 'option' => 'image_default_size'
376 'image_default_align' => array(
377 'desc' => __( 'Image default align' ),
379 'option' => 'image_default_align'
382 'desc' => __( 'Template' ),
384 'option' => 'template'
386 'stylesheet' => array(
387 'desc' => __( 'Stylesheet' ),
389 'option' => 'stylesheet'
391 'post_thumbnail' => array(
392 'desc' => __('Post Thumbnail'),
394 'value' => current_theme_supports( 'post-thumbnails' )
398 'time_zone' => array(
399 'desc' => __( 'Time Zone' ),
401 'option' => 'gmt_offset'
403 'blog_title' => array(
404 'desc' => __( 'Site Title' ),
406 'option' => 'blogname'
408 'blog_tagline' => array(
409 'desc' => __( 'Site Tagline' ),
411 'option' => 'blogdescription'
413 'date_format' => array(
414 'desc' => __( 'Date Format' ),
416 'option' => 'date_format'
418 'time_format' => array(
419 'desc' => __( 'Time Format' ),
421 'option' => 'time_format'
423 'users_can_register' => array(
424 'desc' => __( 'Allow new users to sign up' ),
426 'option' => 'users_can_register'
428 'thumbnail_size_w' => array(
429 'desc' => __( 'Thumbnail Width' ),
431 'option' => 'thumbnail_size_w'
433 'thumbnail_size_h' => array(
434 'desc' => __( 'Thumbnail Height' ),
436 'option' => 'thumbnail_size_h'
438 'thumbnail_crop' => array(
439 'desc' => __( 'Crop thumbnail to exact dimensions' ),
441 'option' => 'thumbnail_crop'
443 'medium_size_w' => array(
444 'desc' => __( 'Medium size image width' ),
446 'option' => 'medium_size_w'
448 'medium_size_h' => array(
449 'desc' => __( 'Medium size image height' ),
451 'option' => 'medium_size_h'
453 'large_size_w' => array(
454 'desc' => __( 'Large size image width' ),
456 'option' => 'large_size_w'
458 'large_size_h' => array(
459 'desc' => __( 'Large size image height' ),
461 'option' => 'large_size_h'
463 'default_comment_status' => array(
464 'desc' => __( 'Allow people to post comments on new articles' ),
466 'option' => 'default_comment_status'
468 'default_ping_status' => array(
469 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
471 'option' => 'default_ping_status'
476 * Filter the XML-RPC blog options property.
480 * @param array $blog_options An array of XML-RPC blog options.
482 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
486 * Retrieve the blogs of the user.
490 * @param array $args Method parameters. Contains:
493 * @return array. Contains:
498 * - 'xmlrpc' - url of xmlrpc endpoint
500 function wp_getUsersBlogs( $args ) {
501 // If this isn't on WPMU then just use blogger_getUsersBlogs
502 if ( !is_multisite() ) {
503 array_unshift( $args, 1 );
504 return $this->blogger_getUsersBlogs( $args );
507 $this->escape( $args );
509 $username = $args[0];
510 $password = $args[1];
512 if ( !$user = $this->login($username, $password) )
516 * Fires after the XML-RPC user has been authenticated but before the rest of
517 * the method logic begins.
519 * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
520 * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
524 * @param method $name The method name.
526 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
528 $blogs = (array) get_blogs_of_user( $user->ID );
531 foreach ( $blogs as $blog ) {
532 // Don't include blogs that aren't hosted at this site
533 if ( $blog->site_id != get_current_site()->id )
536 $blog_id = $blog->userblog_id;
538 switch_to_blog( $blog_id );
540 $is_admin = current_user_can( 'manage_options' );
543 'isAdmin' => $is_admin,
544 'url' => home_url( '/' ),
545 'blogid' => (string) $blog_id,
546 'blogName' => get_option( 'blogname' ),
547 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
550 restore_current_blog();
557 * Checks if the method received at least the minimum number of arguments.
561 * @param string|array $args Sanitize single string or array of strings.
562 * @param int $count Minimum number of arguments.
563 * @return boolean if $args contains at least $count arguments.
565 protected function minimum_args( $args, $count ) {
566 if ( count( $args ) < $count ) {
567 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
575 * Prepares taxonomy data for return in an XML-RPC object.
579 * @param object $taxonomy The unprepared taxonomy data
580 * @param array $fields The subset of taxonomy fields to return
581 * @return array The prepared taxonomy data
583 protected function _prepare_taxonomy( $taxonomy, $fields ) {
585 'name' => $taxonomy->name,
586 'label' => $taxonomy->label,
587 'hierarchical' => (bool) $taxonomy->hierarchical,
588 'public' => (bool) $taxonomy->public,
589 'show_ui' => (bool) $taxonomy->show_ui,
590 '_builtin' => (bool) $taxonomy->_builtin,
593 if ( in_array( 'labels', $fields ) )
594 $_taxonomy['labels'] = (array) $taxonomy->labels;
596 if ( in_array( 'cap', $fields ) )
597 $_taxonomy['cap'] = (array) $taxonomy->cap;
599 if ( in_array( 'menu', $fields ) )
600 $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
602 if ( in_array( 'object_type', $fields ) )
603 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
606 * Filter XML-RPC-prepared data for the given taxonomy.
610 * @param array $_taxonomy An array of taxonomy data.
611 * @param object $taxonomy Taxonomy object.
612 * @param array $fields The subset of taxonomy fields to return.
614 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
618 * Prepares term data for return in an XML-RPC object.
622 * @param array|object $term The unprepared term data
623 * @return array The prepared term data
625 protected function _prepare_term( $term ) {
627 if ( ! is_array( $_term) )
628 $_term = get_object_vars( $_term );
630 // For integers which may be larger than XML-RPC supports ensure we return strings.
631 $_term['term_id'] = strval( $_term['term_id'] );
632 $_term['term_group'] = strval( $_term['term_group'] );
633 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
634 $_term['parent'] = strval( $_term['parent'] );
636 // Count we are happy to return as an integer because people really shouldn't use terms that much.
637 $_term['count'] = intval( $_term['count'] );
640 * Filter XML-RPC-prepared data for the given term.
644 * @param array $_term An array of term data.
645 * @param array|object $term Term object or array.
647 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
651 * Convert a WordPress date string to an IXR_Date object.
655 * @param string $date
658 protected function _convert_date( $date ) {
659 if ( $date === '0000-00-00 00:00:00' ) {
660 return new IXR_Date( '00000000T00:00:00Z' );
662 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
666 * Convert a WordPress GMT date string to an IXR_Date object.
670 * @param string $date_gmt
671 * @param string $date
674 protected function _convert_date_gmt( $date_gmt, $date ) {
675 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
676 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
678 return $this->_convert_date( $date_gmt );
682 * Prepares post data for return in an XML-RPC object.
686 * @param array $post The unprepared post data
687 * @param array $fields The subset of post type fields to return
688 * @return array The prepared post data
690 protected function _prepare_post( $post, $fields ) {
691 // holds the data for this post. built up based on $fields
692 $_post = array( 'post_id' => strval( $post['ID'] ) );
694 // prepare common post fields
695 $post_fields = array(
696 'post_title' => $post['post_title'],
697 'post_date' => $this->_convert_date( $post['post_date'] ),
698 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
699 'post_modified' => $this->_convert_date( $post['post_modified'] ),
700 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
701 'post_status' => $post['post_status'],
702 'post_type' => $post['post_type'],
703 'post_name' => $post['post_name'],
704 'post_author' => $post['post_author'],
705 'post_password' => $post['post_password'],
706 'post_excerpt' => $post['post_excerpt'],
707 'post_content' => $post['post_content'],
708 'post_parent' => strval( $post['post_parent'] ),
709 'post_mime_type' => $post['post_mime_type'],
710 'link' => post_permalink( $post['ID'] ),
711 'guid' => $post['guid'],
712 'menu_order' => intval( $post['menu_order'] ),
713 'comment_status' => $post['comment_status'],
714 'ping_status' => $post['ping_status'],
715 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
719 $post_fields['post_thumbnail'] = array();
720 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
721 if ( $thumbnail_id ) {
722 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
723 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
726 // Consider future posts as published
727 if ( $post_fields['post_status'] === 'future' )
728 $post_fields['post_status'] = 'publish';
730 // Fill in blank post format
731 $post_fields['post_format'] = get_post_format( $post['ID'] );
732 if ( empty( $post_fields['post_format'] ) )
733 $post_fields['post_format'] = 'standard';
735 // Merge requested $post_fields fields into $_post
736 if ( in_array( 'post', $fields ) ) {
737 $_post = array_merge( $_post, $post_fields );
739 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
740 $_post = array_merge( $_post, $requested_fields );
743 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
745 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
746 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
747 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
748 $_post['terms'] = array();
749 foreach ( $terms as $term ) {
750 $_post['terms'][] = $this->_prepare_term( $term );
754 if ( in_array( 'custom_fields', $fields ) )
755 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
757 if ( in_array( 'enclosure', $fields ) ) {
758 $_post['enclosure'] = array();
759 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
760 if ( ! empty( $enclosures ) ) {
761 $encdata = explode( "\n", $enclosures[0] );
762 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
763 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
764 $_post['enclosure']['type'] = trim( $encdata[2] );
769 * Filter XML-RPC-prepared date for the given post.
773 * @param array $_post An array of modified post data.
774 * @param array $post An array of post data.
775 * @param array $fields An array of post fields.
777 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
781 * Prepares post data for return in an XML-RPC object.
785 * @param object $post_type Post type object
786 * @param array $fields The subset of post fields to return
787 * @return array The prepared post type data
789 protected function _prepare_post_type( $post_type, $fields ) {
791 'name' => $post_type->name,
792 'label' => $post_type->label,
793 'hierarchical' => (bool) $post_type->hierarchical,
794 'public' => (bool) $post_type->public,
795 'show_ui' => (bool) $post_type->show_ui,
796 '_builtin' => (bool) $post_type->_builtin,
797 'has_archive' => (bool) $post_type->has_archive,
798 'supports' => get_all_post_type_supports( $post_type->name ),
801 if ( in_array( 'labels', $fields ) ) {
802 $_post_type['labels'] = (array) $post_type->labels;
805 if ( in_array( 'cap', $fields ) ) {
806 $_post_type['cap'] = (array) $post_type->cap;
807 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
810 if ( in_array( 'menu', $fields ) ) {
811 $_post_type['menu_position'] = (int) $post_type->menu_position;
812 $_post_type['menu_icon'] = $post_type->menu_icon;
813 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
816 if ( in_array( 'taxonomies', $fields ) )
817 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
820 * Filter XML-RPC-prepared date for the given post type.
824 * @param array $_post_type An array of post type data.
825 * @param object $post_type Post type object.
827 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
831 * Prepares media item data for return in an XML-RPC object.
835 * @param object $media_item The unprepared media item data
836 * @param string $thumbnail_size The image size to use for the thumbnail URL
837 * @return array The prepared media item data
839 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
840 $_media_item = array(
841 'attachment_id' => strval( $media_item->ID ),
842 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
843 'parent' => $media_item->post_parent,
844 'link' => wp_get_attachment_url( $media_item->ID ),
845 'title' => $media_item->post_title,
846 'caption' => $media_item->post_excerpt,
847 'description' => $media_item->post_content,
848 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
851 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
852 if ( $thumbnail_src )
853 $_media_item['thumbnail'] = $thumbnail_src[0];
855 $_media_item['thumbnail'] = $_media_item['link'];
858 * Filter XML-RPC-prepared data for the given media item.
862 * @param array $_media_item An array of media item data.
863 * @param object $media_item Media item object.
864 * @param string $thumbnail_size Image size.
866 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
870 * Prepares page data for return in an XML-RPC object.
874 * @param object $page The unprepared page data
875 * @return array The prepared page data
877 protected function _prepare_page( $page ) {
878 // Get all of the page content and link.
879 $full_page = get_extended( $page->post_content );
880 $link = post_permalink( $page->ID );
882 // Get info the page parent if there is one.
884 if ( ! empty( $page->post_parent ) ) {
885 $parent = get_post( $page->post_parent );
886 $parent_title = $parent->post_title;
889 // Determine comment and ping settings.
890 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
891 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
894 $page_date = $this->_convert_date( $page->post_date );
895 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
897 // Pull the categories info together.
898 $categories = array();
899 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
900 $categories[] = get_cat_name( $cat_id );
903 // Get the author info.
904 $author = get_userdata( $page->post_author );
906 $page_template = get_page_template_slug( $page->ID );
907 if ( empty( $page_template ) )
908 $page_template = 'default';
911 'dateCreated' => $page_date,
912 'userid' => $page->post_author,
913 'page_id' => $page->ID,
914 'page_status' => $page->post_status,
915 'description' => $full_page['main'],
916 'title' => $page->post_title,
918 'permaLink' => $link,
919 'categories' => $categories,
920 'excerpt' => $page->post_excerpt,
921 'text_more' => $full_page['extended'],
922 'mt_allow_comments' => $allow_comments,
923 'mt_allow_pings' => $allow_pings,
924 'wp_slug' => $page->post_name,
925 'wp_password' => $page->post_password,
926 'wp_author' => $author->display_name,
927 'wp_page_parent_id' => $page->post_parent,
928 'wp_page_parent_title' => $parent_title,
929 'wp_page_order' => $page->menu_order,
930 'wp_author_id' => (string) $author->ID,
931 'wp_author_display_name' => $author->display_name,
932 'date_created_gmt' => $page_date_gmt,
933 'custom_fields' => $this->get_custom_fields( $page->ID ),
934 'wp_page_template' => $page_template
938 * Filter XML-RPC-prepared data for the given page.
942 * @param array $_page An array of page data.
943 * @param WP_Post $page Page object.
945 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
949 * Prepares comment data for return in an XML-RPC object.
953 * @param object $comment The unprepared comment data
954 * @return array The prepared comment data
956 protected function _prepare_comment( $comment ) {
958 $comment_date = $this->_convert_date( $comment->comment_date );
959 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
961 if ( '0' == $comment->comment_approved )
962 $comment_status = 'hold';
963 else if ( 'spam' == $comment->comment_approved )
964 $comment_status = 'spam';
965 else if ( '1' == $comment->comment_approved )
966 $comment_status = 'approve';
968 $comment_status = $comment->comment_approved;
971 'date_created_gmt' => $comment_date_gmt,
972 'user_id' => $comment->user_id,
973 'comment_id' => $comment->comment_ID,
974 'parent' => $comment->comment_parent,
975 'status' => $comment_status,
976 'content' => $comment->comment_content,
977 'link' => get_comment_link($comment),
978 'post_id' => $comment->comment_post_ID,
979 'post_title' => get_the_title($comment->comment_post_ID),
980 'author' => $comment->comment_author,
981 'author_url' => $comment->comment_author_url,
982 'author_email' => $comment->comment_author_email,
983 'author_ip' => $comment->comment_author_IP,
984 'type' => $comment->comment_type,
988 * Filter XML-RPC-prepared data for the given comment.
992 * @param array $_comment An array of prepared comment data.
993 * @param object $comment Comment object.
995 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
999 * Prepares user data for return in an XML-RPC object.
1003 * @param WP_User $user The unprepared user object
1004 * @param array $fields The subset of user fields to return
1005 * @return array The prepared user data
1007 protected function _prepare_user( $user, $fields ) {
1008 $_user = array( 'user_id' => strval( $user->ID ) );
1010 $user_fields = array(
1011 'username' => $user->user_login,
1012 'first_name' => $user->user_firstname,
1013 'last_name' => $user->user_lastname,
1014 'registered' => $this->_convert_date( $user->user_registered ),
1015 'bio' => $user->user_description,
1016 'email' => $user->user_email,
1017 'nickname' => $user->nickname,
1018 'nicename' => $user->user_nicename,
1019 'url' => $user->user_url,
1020 'display_name' => $user->display_name,
1021 'roles' => $user->roles,
1024 if ( in_array( 'all', $fields ) ) {
1025 $_user = array_merge( $_user, $user_fields );
1027 if ( in_array( 'basic', $fields ) ) {
1028 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
1029 $fields = array_merge( $fields, $basic_fields );
1031 $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
1032 $_user = array_merge( $_user, $requested_fields );
1036 * Filter XML-RPC-prepared data for the given user.
1040 * @param array $_user An array of user data.
1041 * @param WP_User $user User object.
1042 * @param array $fields An array of user fields.
1044 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
1048 * Create a new post for any registered post type.
1052 * @param array $args Method parameters. Contains:
1054 * - string $username
1055 * - string $password
1056 * - array $content_struct
1057 * $content_struct can contain:
1058 * - post_type (default: 'post')
1059 * - post_status (default: 'draft')
1064 * - post_date_gmt | post_date
1067 * - comment_status - can be 'open' | 'closed'
1068 * - ping_status - can be 'open' | 'closed'
1070 * - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
1071 * - custom_fields - array, with each element containing 'key' and 'value'
1072 * - terms - array, with taxonomy names as keys and arrays of term IDs as values
1073 * - terms_names - array, with taxonomy names as keys and arrays of term names as values
1075 * - any other fields supported by wp_insert_post()
1076 * @return string post_id
1078 function wp_newPost( $args ) {
1079 if ( ! $this->minimum_args( $args, 4 ) )
1080 return $this->error;
1082 $this->escape( $args );
1084 $blog_id = (int) $args[0];
1085 $username = $args[1];
1086 $password = $args[2];
1087 $content_struct = $args[3];
1089 if ( ! $user = $this->login( $username, $password ) )
1090 return $this->error;
1092 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1093 do_action( 'xmlrpc_call', 'wp.newPost' );
1095 unset( $content_struct['ID'] );
1097 return $this->_insert_post( $user, $content_struct );
1101 * Helper method for filtering out elements from an array.
1105 * @param int $count Number to compare to one.
1107 private function _is_greater_than_one( $count ) {
1112 * Helper method for wp_newPost and wp_editPost, containing shared logic.
1115 * @uses wp_insert_post()
1117 * @param WP_User $user The post author if post_author isn't set in $content_struct.
1118 * @param array $content_struct Post data to insert.
1120 protected function _insert_post( $user, $content_struct ) {
1121 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1122 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1124 $post_data = wp_parse_args( $content_struct, $defaults );
1126 $post_type = get_post_type_object( $post_data['post_type'] );
1128 return new IXR_Error( 403, __( 'Invalid post type' ) );
1130 $update = ! empty( $post_data['ID'] );
1133 if ( ! get_post( $post_data['ID'] ) )
1134 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1135 if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1136 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1137 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1138 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1140 if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1141 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1144 switch ( $post_data['post_status'] ) {
1149 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1150 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1154 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1155 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1158 if ( ! get_post_status_object( $post_data['post_status'] ) )
1159 $post_data['post_status'] = 'draft';
1163 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1164 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1166 $post_data['post_author'] = absint( $post_data['post_author'] );
1167 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1168 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1169 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1171 $author = get_userdata( $post_data['post_author'] );
1174 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1176 $post_data['post_author'] = $user->ID;
1179 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1180 unset( $post_data['comment_status'] );
1182 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1183 unset( $post_data['ping_status'] );
1185 // Do some timestamp voodoo
1186 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1187 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1188 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1189 } elseif ( ! empty( $post_data['post_date'] ) ) {
1190 $dateCreated = $post_data['post_date']->getIso();
1193 if ( ! empty( $dateCreated ) ) {
1194 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1195 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1198 if ( ! isset( $post_data['ID'] ) )
1199 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1200 $post_ID = $post_data['ID'];
1202 if ( $post_data['post_type'] == 'post' ) {
1203 // Private and password-protected posts cannot be stickied.
1204 if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
1205 // Error if the client tried to stick the post, otherwise, silently unstick.
1206 if ( ! empty( $post_data['sticky'] ) )
1207 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1209 unstick_post( $post_ID );
1210 } elseif ( isset( $post_data['sticky'] ) ) {
1211 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1212 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1213 if ( $post_data['sticky'] )
1214 stick_post( $post_ID );
1216 unstick_post( $post_ID );
1220 if ( isset( $post_data['post_thumbnail'] ) ) {
1221 // empty value deletes, non-empty value adds/updates
1222 if ( ! $post_data['post_thumbnail'] )
1223 delete_post_thumbnail( $post_ID );
1224 elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1225 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1226 set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1227 unset( $content_struct['post_thumbnail'] );
1230 if ( isset( $post_data['custom_fields'] ) )
1231 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1233 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1234 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1236 // accumulate term IDs from terms and terms_names
1239 // first validate the terms specified by ID
1240 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1241 $taxonomies = array_keys( $post_data['terms'] );
1243 // validating term ids
1244 foreach ( $taxonomies as $taxonomy ) {
1245 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1246 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1248 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1249 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1251 $term_ids = $post_data['terms'][$taxonomy];
1252 $terms[ $taxonomy ] = array();
1253 foreach ( $term_ids as $term_id ) {
1254 $term = get_term_by( 'id', $term_id, $taxonomy );
1257 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1259 $terms[$taxonomy][] = (int) $term_id;
1264 // now validate terms specified by name
1265 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1266 $taxonomies = array_keys( $post_data['terms_names'] );
1268 foreach ( $taxonomies as $taxonomy ) {
1269 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1270 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1272 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1273 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1275 // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
1276 $ambiguous_terms = array();
1277 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1278 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1280 // count the number of terms with the same name
1281 $tax_term_names_count = array_count_values( $tax_term_names );
1283 // filter out non-ambiguous term names
1284 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1286 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1289 $term_names = $post_data['terms_names'][$taxonomy];
1290 foreach ( $term_names as $term_name ) {
1291 if ( in_array( $term_name, $ambiguous_terms ) )
1292 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1294 $term = get_term_by( 'name', $term_name, $taxonomy );
1297 // term doesn't exist, so check that the user is allowed to create new terms
1298 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1299 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1301 // create the new term
1302 $term_info = wp_insert_term( $term_name, $taxonomy );
1303 if ( is_wp_error( $term_info ) )
1304 return new IXR_Error( 500, $term_info->get_error_message() );
1306 $terms[$taxonomy][] = (int) $term_info['term_id'];
1308 $terms[$taxonomy][] = (int) $term->term_id;
1314 $post_data['tax_input'] = $terms;
1315 unset( $post_data['terms'], $post_data['terms_names'] );
1317 // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
1318 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1321 if ( isset( $post_data['post_format'] ) ) {
1322 $format = set_post_format( $post_ID, $post_data['post_format'] );
1324 if ( is_wp_error( $format ) )
1325 return new IXR_Error( 500, $format->get_error_message() );
1327 unset( $post_data['post_format'] );
1330 // Handle enclosures
1331 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1332 $this->add_enclosure_if_new( $post_ID, $enclosure );
1334 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1337 * Filter post data array to be inserted via XML-RPC.
1341 * @param array $post_data Parsed array of post data.
1342 * @param array $content_struct Post data array.
1344 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1346 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1347 if ( is_wp_error( $post_ID ) )
1348 return new IXR_Error( 500, $post_ID->get_error_message() );
1351 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1353 return strval( $post_ID );
1357 * Edit a post for any registered post type.
1359 * The $content_struct parameter only needs to contain fields that
1360 * should be changed. All other fields will retain their existing values.
1364 * @param array $args Method parameters. Contains:
1366 * - string $username
1367 * - string $password
1369 * - array $content_struct
1370 * @return true on success
1372 function wp_editPost( $args ) {
1373 if ( ! $this->minimum_args( $args, 5 ) )
1374 return $this->error;
1376 $this->escape( $args );
1378 $blog_id = (int) $args[0];
1379 $username = $args[1];
1380 $password = $args[2];
1381 $post_id = (int) $args[3];
1382 $content_struct = $args[4];
1384 if ( ! $user = $this->login( $username, $password ) )
1385 return $this->error;
1387 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1388 do_action( 'xmlrpc_call', 'wp.editPost' );
1390 $post = get_post( $post_id, ARRAY_A );
1392 if ( empty( $post['ID'] ) )
1393 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1395 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1396 // If the post has been modified since the date provided, return an error.
1397 if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1398 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1402 // convert the date field back to IXR form
1403 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1405 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1406 // since _insert_post will ignore the non-GMT date if the GMT date is set
1407 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1408 unset( $post['post_date_gmt'] );
1410 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1412 $this->escape( $post );
1413 $merged_content_struct = array_merge( $post, $content_struct );
1415 $retval = $this->_insert_post( $user, $merged_content_struct );
1416 if ( $retval instanceof IXR_Error )
1423 * Delete a post for any registered post type.
1427 * @uses wp_delete_post()
1428 * @param array $args Method parameters. Contains:
1430 * - string $username
1431 * - string $password
1433 * @return true on success
1435 function wp_deletePost( $args ) {
1436 if ( ! $this->minimum_args( $args, 4 ) )
1437 return $this->error;
1439 $this->escape( $args );
1441 $blog_id = (int) $args[0];
1442 $username = $args[1];
1443 $password = $args[2];
1444 $post_id = (int) $args[3];
1446 if ( ! $user = $this->login( $username, $password ) )
1447 return $this->error;
1449 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1450 do_action( 'xmlrpc_call', 'wp.deletePost' );
1452 $post = get_post( $post_id, ARRAY_A );
1453 if ( empty( $post['ID'] ) )
1454 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1456 if ( ! current_user_can( 'delete_post', $post_id ) )
1457 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1459 $result = wp_delete_post( $post_id );
1462 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1472 * The optional $fields parameter specifies what fields will be included
1473 * in the response array. This should be a list of field names. 'post_id' will
1474 * always be included in the response regardless of the value of $fields.
1476 * Instead of, or in addition to, individual field names, conceptual group
1477 * names can be used to specify multiple fields. The available conceptual
1478 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1482 * @param array $args Method parameters. Contains:
1484 * - string $username
1485 * - string $password
1486 * - array $fields optional
1487 * @return array contains (based on $fields parameter):
1493 * - 'post_modified_gmt'
1502 * - 'comment_status'
1511 function wp_getPost( $args ) {
1512 if ( ! $this->minimum_args( $args, 4 ) )
1513 return $this->error;
1515 $this->escape( $args );
1517 $blog_id = (int) $args[0];
1518 $username = $args[1];
1519 $password = $args[2];
1520 $post_id = (int) $args[3];
1522 if ( isset( $args[4] ) ) {
1526 * Filter the list of post query fields used by the given XML-RPC method.
1530 * @param array $fields Array of post fields.
1531 * @param string $method Method name.
1533 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1536 if ( ! $user = $this->login( $username, $password ) )
1537 return $this->error;
1539 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1540 do_action( 'xmlrpc_call', 'wp.getPost' );
1542 $post = get_post( $post_id, ARRAY_A );
1544 if ( empty( $post['ID'] ) )
1545 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1547 if ( ! current_user_can( 'edit_post', $post_id ) )
1548 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1550 return $this->_prepare_post( $post, $fields );
1558 * The optional $filter parameter modifies the query used to retrieve posts.
1559 * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
1560 * 'orderby', and 'order'.
1562 * The optional $fields parameter specifies what fields will be included
1563 * in the response array.
1565 * @uses wp_get_recent_posts()
1566 * @see wp_getPost() for more on $fields
1567 * @see get_posts() for more on $filter values
1569 * @param array $args Method parameters. Contains:
1571 * - string $username
1572 * - string $password
1573 * - array $filter optional
1574 * - array $fields optional
1575 * @return array contains a collection of posts.
1577 function wp_getPosts( $args ) {
1578 if ( ! $this->minimum_args( $args, 3 ) )
1579 return $this->error;
1581 $this->escape( $args );
1583 $blog_id = (int) $args[0];
1584 $username = $args[1];
1585 $password = $args[2];
1586 $filter = isset( $args[3] ) ? $args[3] : array();
1588 if ( isset( $args[4] ) ) {
1591 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1592 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1595 if ( ! $user = $this->login( $username, $password ) )
1596 return $this->error;
1598 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1599 do_action( 'xmlrpc_call', 'wp.getPosts' );
1603 if ( isset( $filter['post_type'] ) ) {
1604 $post_type = get_post_type_object( $filter['post_type'] );
1605 if ( ! ( (bool) $post_type ) )
1606 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1608 $post_type = get_post_type_object( 'post' );
1611 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1612 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1614 $query['post_type'] = $post_type->name;
1616 if ( isset( $filter['post_status'] ) )
1617 $query['post_status'] = $filter['post_status'];
1619 if ( isset( $filter['number'] ) )
1620 $query['numberposts'] = absint( $filter['number'] );
1622 if ( isset( $filter['offset'] ) )
1623 $query['offset'] = absint( $filter['offset'] );
1625 if ( isset( $filter['orderby'] ) ) {
1626 $query['orderby'] = $filter['orderby'];
1628 if ( isset( $filter['order'] ) )
1629 $query['order'] = $filter['order'];
1632 if ( isset( $filter['s'] ) ) {
1633 $query['s'] = $filter['s'];
1636 $posts_list = wp_get_recent_posts( $query );
1638 if ( ! $posts_list )
1641 // holds all the posts data
1644 foreach ( $posts_list as $post ) {
1645 if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1648 $struct[] = $this->_prepare_post( $post, $fields );
1655 * Create a new term.
1659 * @uses wp_insert_term()
1660 * @param array $args Method parameters. Contains:
1662 * - string $username
1663 * - string $password
1664 * - array $content_struct
1665 * The $content_struct must contain:
1668 * Also, it can optionally contain:
1672 * @return string term_id
1674 function wp_newTerm( $args ) {
1675 if ( ! $this->minimum_args( $args, 4 ) )
1676 return $this->error;
1678 $this->escape( $args );
1680 $blog_id = (int) $args[0];
1681 $username = $args[1];
1682 $password = $args[2];
1683 $content_struct = $args[3];
1685 if ( ! $user = $this->login( $username, $password ) )
1686 return $this->error;
1688 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1689 do_action( 'xmlrpc_call', 'wp.newTerm' );
1691 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1692 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1694 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1696 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1697 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1699 $taxonomy = (array) $taxonomy;
1701 // hold the data of the term
1702 $term_data = array();
1704 $term_data['name'] = trim( $content_struct['name'] );
1705 if ( empty( $term_data['name'] ) )
1706 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1708 if ( isset( $content_struct['parent'] ) ) {
1709 if ( ! $taxonomy['hierarchical'] )
1710 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1712 $parent_term_id = (int) $content_struct['parent'];
1713 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1715 if ( is_wp_error( $parent_term ) )
1716 return new IXR_Error( 500, $parent_term->get_error_message() );
1718 if ( ! $parent_term )
1719 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1721 $term_data['parent'] = $content_struct['parent'];
1724 if ( isset( $content_struct['description'] ) )
1725 $term_data['description'] = $content_struct['description'];
1727 if ( isset( $content_struct['slug'] ) )
1728 $term_data['slug'] = $content_struct['slug'];
1730 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1732 if ( is_wp_error( $term ) )
1733 return new IXR_Error( 500, $term->get_error_message() );
1736 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1738 return strval( $term['term_id'] );
1746 * @uses wp_update_term()
1747 * @param array $args Method parameters. Contains:
1749 * - string $username
1750 * - string $password
1752 * - array $content_struct
1753 * The $content_struct must contain:
1755 * Also, it can optionally contain:
1760 * @return bool True, on success.
1762 function wp_editTerm( $args ) {
1763 if ( ! $this->minimum_args( $args, 5 ) )
1764 return $this->error;
1766 $this->escape( $args );
1768 $blog_id = (int) $args[0];
1769 $username = $args[1];
1770 $password = $args[2];
1771 $term_id = (int) $args[3];
1772 $content_struct = $args[4];
1774 if ( ! $user = $this->login( $username, $password ) )
1775 return $this->error;
1777 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1778 do_action( 'xmlrpc_call', 'wp.editTerm' );
1780 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1781 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1783 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1785 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1786 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1788 $taxonomy = (array) $taxonomy;
1790 // hold the data of the term
1791 $term_data = array();
1793 $term = get_term( $term_id , $content_struct['taxonomy'] );
1795 if ( is_wp_error( $term ) )
1796 return new IXR_Error( 500, $term->get_error_message() );
1799 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1801 if ( isset( $content_struct['name'] ) ) {
1802 $term_data['name'] = trim( $content_struct['name'] );
1804 if ( empty( $term_data['name'] ) )
1805 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1808 if ( isset( $content_struct['parent'] ) ) {
1809 if ( ! $taxonomy['hierarchical'] )
1810 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1812 $parent_term_id = (int) $content_struct['parent'];
1813 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1815 if ( is_wp_error( $parent_term ) )
1816 return new IXR_Error( 500, $parent_term->get_error_message() );
1818 if ( ! $parent_term )
1819 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1821 $term_data['parent'] = $content_struct['parent'];
1824 if ( isset( $content_struct['description'] ) )
1825 $term_data['description'] = $content_struct['description'];
1827 if ( isset( $content_struct['slug'] ) )
1828 $term_data['slug'] = $content_struct['slug'];
1830 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1832 if ( is_wp_error( $term ) )
1833 return new IXR_Error( 500, $term->get_error_message() );
1836 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1846 * @uses wp_delete_term()
1847 * @param array $args Method parameters. Contains:
1849 * - string $username
1850 * - string $password
1851 * - string $taxnomy_name
1853 * @return boolean|IXR_Error If it suceeded true else a reason why not
1855 function wp_deleteTerm( $args ) {
1856 if ( ! $this->minimum_args( $args, 5 ) )
1857 return $this->error;
1859 $this->escape( $args );
1861 $blog_id = (int) $args[0];
1862 $username = $args[1];
1863 $password = $args[2];
1864 $taxonomy = $args[3];
1865 $term_id = (int) $args[4];
1867 if ( ! $user = $this->login( $username, $password ) )
1868 return $this->error;
1870 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1871 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1873 if ( ! taxonomy_exists( $taxonomy ) )
1874 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1876 $taxonomy = get_taxonomy( $taxonomy );
1878 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
1879 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
1881 $term = get_term( $term_id, $taxonomy->name );
1883 if ( is_wp_error( $term ) )
1884 return new IXR_Error( 500, $term->get_error_message() );
1887 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1889 $result = wp_delete_term( $term_id, $taxonomy->name );
1891 if ( is_wp_error( $result ) )
1892 return new IXR_Error( 500, $term->get_error_message() );
1895 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
1906 * @param array $args Method parameters. Contains:
1908 * - string $username
1909 * - string $password
1910 * - string $taxonomy
1912 * @return array contains:
1917 * - 'term_taxonomy_id'
1923 function wp_getTerm( $args ) {
1924 if ( ! $this->minimum_args( $args, 5 ) )
1925 return $this->error;
1927 $this->escape( $args );
1929 $blog_id = (int) $args[0];
1930 $username = $args[1];
1931 $password = $args[2];
1932 $taxonomy = $args[3];
1933 $term_id = (int) $args[4];
1935 if ( ! $user = $this->login( $username, $password ) )
1936 return $this->error;
1938 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1939 do_action( 'xmlrpc_call', 'wp.getTerm' );
1941 if ( ! taxonomy_exists( $taxonomy ) )
1942 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1944 $taxonomy = get_taxonomy( $taxonomy );
1946 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1947 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1949 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
1951 if ( is_wp_error( $term ) )
1952 return new IXR_Error( 500, $term->get_error_message() );
1955 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1957 return $this->_prepare_term( $term );
1961 * Retrieve all terms for a taxonomy.
1965 * The optional $filter parameter modifies the query used to retrieve terms.
1966 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
1969 * @param array $args Method parameters. Contains:
1971 * - string $username
1972 * - string $password
1973 * - string $taxonomy
1974 * - array $filter optional
1975 * @return array terms
1977 function wp_getTerms( $args ) {
1978 if ( ! $this->minimum_args( $args, 4 ) )
1979 return $this->error;
1981 $this->escape( $args );
1983 $blog_id = (int) $args[0];
1984 $username = $args[1];
1985 $password = $args[2];
1986 $taxonomy = $args[3];
1987 $filter = isset( $args[4] ) ? $args[4] : array();
1989 if ( ! $user = $this->login( $username, $password ) )
1990 return $this->error;
1992 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1993 do_action( 'xmlrpc_call', 'wp.getTerms' );
1995 if ( ! taxonomy_exists( $taxonomy ) )
1996 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1998 $taxonomy = get_taxonomy( $taxonomy );
2000 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2001 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2005 if ( isset( $filter['number'] ) )
2006 $query['number'] = absint( $filter['number'] );
2008 if ( isset( $filter['offset'] ) )
2009 $query['offset'] = absint( $filter['offset'] );
2011 if ( isset( $filter['orderby'] ) ) {
2012 $query['orderby'] = $filter['orderby'];
2014 if ( isset( $filter['order'] ) )
2015 $query['order'] = $filter['order'];
2018 if ( isset( $filter['hide_empty'] ) )
2019 $query['hide_empty'] = $filter['hide_empty'];
2021 $query['get'] = 'all';
2023 if ( isset( $filter['search'] ) )
2024 $query['search'] = $filter['search'];
2026 $terms = get_terms( $taxonomy->name, $query );
2028 if ( is_wp_error( $terms ) )
2029 return new IXR_Error( 500, $terms->get_error_message() );
2033 foreach ( $terms as $term ) {
2034 $struct[] = $this->_prepare_term( $term );
2041 * Retrieve a taxonomy.
2045 * @uses get_taxonomy()
2046 * @param array $args Method parameters. Contains:
2048 * - string $username
2049 * - string $password
2050 * - string $taxonomy
2051 * @return array (@see get_taxonomy())
2053 function wp_getTaxonomy( $args ) {
2054 if ( ! $this->minimum_args( $args, 4 ) )
2055 return $this->error;
2057 $this->escape( $args );
2059 $blog_id = (int) $args[0];
2060 $username = $args[1];
2061 $password = $args[2];
2062 $taxonomy = $args[3];
2064 if ( isset( $args[4] ) ) {
2068 * Filter the taxonomy query fields used by the given XML-RPC method.
2072 * @param array $fields An array of taxonomy fields to retrieve.
2073 * @param string $method The method name.
2075 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
2078 if ( ! $user = $this->login( $username, $password ) )
2079 return $this->error;
2081 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2082 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
2084 if ( ! taxonomy_exists( $taxonomy ) )
2085 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2087 $taxonomy = get_taxonomy( $taxonomy );
2089 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2090 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2092 return $this->_prepare_taxonomy( $taxonomy, $fields );
2096 * Retrieve all taxonomies.
2100 * @uses get_taxonomies()
2101 * @param array $args Method parameters. Contains:
2103 * - string $username
2104 * - string $password
2105 * @return array taxonomies
2107 function wp_getTaxonomies( $args ) {
2108 if ( ! $this->minimum_args( $args, 3 ) )
2109 return $this->error;
2111 $this->escape( $args );
2113 $blog_id = (int) $args[0];
2114 $username = $args[1];
2115 $password = $args[2];
2116 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2118 if ( isset( $args[4] ) ) {
2121 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2122 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
2125 if ( ! $user = $this->login( $username, $password ) )
2126 return $this->error;
2128 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2129 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
2131 $taxonomies = get_taxonomies( $filter, 'objects' );
2133 // holds all the taxonomy data
2136 foreach ( $taxonomies as $taxonomy ) {
2137 // capability check for post_types
2138 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2141 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
2150 * The optional $fields parameter specifies what fields will be included
2151 * in the response array. This should be a list of field names. 'user_id' will
2152 * always be included in the response regardless of the value of $fields.
2154 * Instead of, or in addition to, individual field names, conceptual group
2155 * names can be used to specify multiple fields. The available conceptual
2156 * groups are 'basic' and 'all'.
2158 * @uses get_userdata()
2159 * @param array $args Method parameters. Contains:
2161 * - string $username
2162 * - string $password
2164 * - array $fields optional
2165 * @return array contains (based on $fields parameter):
2179 function wp_getUser( $args ) {
2180 if ( ! $this->minimum_args( $args, 4 ) )
2181 return $this->error;
2183 $this->escape( $args );
2185 $blog_id = (int) $args[0];
2186 $username = $args[1];
2187 $password = $args[2];
2188 $user_id = (int) $args[3];
2190 if ( isset( $args[4] ) ) {
2194 * Filter the default user query fields used by the given XML-RPC method.
2198 * @param array $fields User query fields for given method. Default 'all'.
2199 * @param string $method The method name.
2201 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2204 if ( ! $user = $this->login( $username, $password ) )
2205 return $this->error;
2207 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2208 do_action( 'xmlrpc_call', 'wp.getUser' );
2210 if ( ! current_user_can( 'edit_user', $user_id ) )
2211 return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2213 $user_data = get_userdata( $user_id );
2216 return new IXR_Error( 404, __( 'Invalid user ID' ) );
2218 return $this->_prepare_user( $user_data, $fields );
2224 * The optional $filter parameter modifies the query used to retrieve users.
2225 * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2226 * 'who', 'orderby', and 'order'.
2228 * The optional $fields parameter specifies what fields will be included
2229 * in the response array.
2232 * @see wp_getUser() for more on $fields and return values
2234 * @param array $args Method parameters. Contains:
2236 * - string $username
2237 * - string $password
2238 * - array $filter optional
2239 * - array $fields optional
2240 * @return array users data
2242 function wp_getUsers( $args ) {
2243 if ( ! $this->minimum_args( $args, 3 ) )
2244 return $this->error;
2246 $this->escape( $args );
2248 $blog_id = (int) $args[0];
2249 $username = $args[1];
2250 $password = $args[2];
2251 $filter = isset( $args[3] ) ? $args[3] : array();
2253 if ( isset( $args[4] ) ) {
2256 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2257 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2260 if ( ! $user = $this->login( $username, $password ) )
2261 return $this->error;
2263 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2264 do_action( 'xmlrpc_call', 'wp.getUsers' );
2266 if ( ! current_user_can( 'list_users' ) )
2267 return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
2269 $query = array( 'fields' => 'all_with_meta' );
2271 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2272 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2274 if ( isset( $filter['orderby'] ) ) {
2275 $query['orderby'] = $filter['orderby'];
2277 if ( isset( $filter['order'] ) )
2278 $query['order'] = $filter['order'];
2281 if ( isset( $filter['role'] ) ) {
2282 if ( get_role( $filter['role'] ) === null )
2283 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2285 $query['role'] = $filter['role'];
2288 if ( isset( $filter['who'] ) ) {
2289 $query['who'] = $filter['who'];
2292 $users = get_users( $query );
2295 foreach ( $users as $user_data ) {
2296 if ( current_user_can( 'edit_user', $user_data->ID ) )
2297 $_users[] = $this->_prepare_user( $user_data, $fields );
2303 * Retrieve information about the requesting user.
2305 * @uses get_userdata()
2306 * @param array $args Method parameters. Contains:
2308 * - string $username
2309 * - string $password
2310 * - array $fields optional
2311 * @return array (@see wp_getUser)
2313 function wp_getProfile( $args ) {
2314 if ( ! $this->minimum_args( $args, 3 ) )
2315 return $this->error;
2317 $this->escape( $args );
2319 $blog_id = (int) $args[0];
2320 $username = $args[1];
2321 $password = $args[2];
2323 if ( isset( $args[3] ) ) {
2326 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2327 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2330 if ( ! $user = $this->login( $username, $password ) )
2331 return $this->error;
2333 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2334 do_action( 'xmlrpc_call', 'wp.getProfile' );
2336 if ( ! current_user_can( 'edit_user', $user->ID ) )
2337 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2339 $user_data = get_userdata( $user->ID );
2341 return $this->_prepare_user( $user_data, $fields );
2345 * Edit user's profile.
2347 * @uses wp_update_user()
2348 * @param array $args Method parameters. Contains:
2350 * - string $username
2351 * - string $password
2352 * - array $content_struct
2353 * It can optionally contain:
2361 * @return bool True, on success.
2363 function wp_editProfile( $args ) {
2364 if ( ! $this->minimum_args( $args, 4 ) )
2365 return $this->error;
2367 $this->escape( $args );
2369 $blog_id = (int) $args[0];
2370 $username = $args[1];
2371 $password = $args[2];
2372 $content_struct = $args[3];
2374 if ( ! $user = $this->login( $username, $password ) )
2375 return $this->error;
2377 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2378 do_action( 'xmlrpc_call', 'wp.editProfile' );
2380 if ( ! current_user_can( 'edit_user', $user->ID ) )
2381 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2383 // holds data of the user
2384 $user_data = array();
2385 $user_data['ID'] = $user->ID;
2387 // only set the user details if it was given
2388 if ( isset( $content_struct['first_name'] ) )
2389 $user_data['first_name'] = $content_struct['first_name'];
2391 if ( isset( $content_struct['last_name'] ) )
2392 $user_data['last_name'] = $content_struct['last_name'];
2394 if ( isset( $content_struct['url'] ) )
2395 $user_data['user_url'] = $content_struct['url'];
2397 if ( isset( $content_struct['display_name'] ) )
2398 $user_data['display_name'] = $content_struct['display_name'];
2400 if ( isset( $content_struct['nickname'] ) )
2401 $user_data['nickname'] = $content_struct['nickname'];
2403 if ( isset( $content_struct['nicename'] ) )
2404 $user_data['user_nicename'] = $content_struct['nicename'];
2406 if ( isset( $content_struct['bio'] ) )
2407 $user_data['description'] = $content_struct['bio'];
2409 $result = wp_update_user( $user_data );
2411 if ( is_wp_error( $result ) )
2412 return new IXR_Error( 500, $result->get_error_message() );
2415 return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2425 * @param array $args Method parameters. Contains:
2432 function wp_getPage($args) {
2433 $this->escape($args);
2435 $blog_id = (int) $args[0];
2436 $page_id = (int) $args[1];
2437 $username = $args[2];
2438 $password = $args[3];
2440 if ( !$user = $this->login($username, $password) ) {
2441 return $this->error;
2444 $page = get_post($page_id);
2446 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2448 if ( !current_user_can( 'edit_page', $page_id ) )
2449 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2451 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2452 do_action( 'xmlrpc_call', 'wp.getPage' );
2454 // If we found the page then format the data.
2455 if ( $page->ID && ($page->post_type == 'page') ) {
2456 return $this->_prepare_page( $page );
2458 // If the page doesn't exist indicate that.
2460 return(new IXR_Error(404, __('Sorry, no such page.')));
2469 * @param array $args Method parameters. Contains:
2476 function wp_getPages($args) {
2477 $this->escape($args);
2479 $blog_id = (int) $args[0];
2480 $username = $args[1];
2481 $password = $args[2];
2482 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
2484 if ( !$user = $this->login($username, $password) )
2485 return $this->error;
2487 if ( !current_user_can( 'edit_pages' ) )
2488 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2490 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2491 do_action( 'xmlrpc_call', 'wp.getPages' );
2493 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2494 $num_pages = count($pages);
2496 // If we have pages, put together their info.
2497 if ( $num_pages >= 1 ) {
2498 $pages_struct = array();
2500 foreach ($pages as $page) {
2501 if ( current_user_can( 'edit_page', $page->ID ) )
2502 $pages_struct[] = $this->_prepare_page( $page );
2505 return($pages_struct);
2507 // If no pages were found return an error.
2518 * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
2521 function wp_newPage($args) {
2522 // Items not escaped here will be escaped in newPost.
2523 $username = $this->escape($args[1]);
2524 $password = $this->escape($args[2]);
2526 $publish = $args[4];
2528 if ( !$user = $this->login($username, $password) )
2529 return $this->error;
2531 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2532 do_action( 'xmlrpc_call', 'wp.newPage' );
2534 // Mark this as content for a page.
2535 $args[3]["post_type"] = 'page';
2537 // Let mw_newPost do all of the heavy lifting.
2538 return($this->mw_newPost($args));
2546 * @param array $args Method parameters.
2547 * @return bool True, if success.
2549 function wp_deletePage($args) {
2550 $this->escape($args);
2552 $blog_id = (int) $args[0];
2553 $username = $args[1];
2554 $password = $args[2];
2555 $page_id = (int) $args[3];
2557 if ( !$user = $this->login($username, $password) )
2558 return $this->error;
2560 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2561 do_action( 'xmlrpc_call', 'wp.deletePage' );
2563 // Get the current page based on the page_id and
2564 // make sure it is a page and not a post.
2565 $actual_page = get_post($page_id, ARRAY_A);
2566 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2567 return(new IXR_Error(404, __('Sorry, no such page.')));
2569 // Make sure the user can delete pages.
2570 if ( !current_user_can('delete_page', $page_id) )
2571 return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
2573 // Attempt to delete the page.
2574 $result = wp_delete_post($page_id);
2576 return(new IXR_Error(500, __('Failed to delete the page.')));
2579 * Fires after a page has been successfully deleted via XML-RPC.
2583 * @param int $page_id ID of the deleted page.
2584 * @param array $args An array of arguments to delete the page.
2586 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2596 * @param array $args Method parameters.
2599 function wp_editPage($args) {
2600 // Items not escaped here will be escaped in editPost.
2601 $blog_id = (int) $args[0];
2602 $page_id = (int) $this->escape($args[1]);
2603 $username = $this->escape($args[2]);
2604 $password = $this->escape($args[3]);
2605 $content = $args[4];
2606 $publish = $args[5];
2608 if ( !$user = $this->login($username, $password) )
2609 return $this->error;
2611 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2612 do_action( 'xmlrpc_call', 'wp.editPage' );
2614 // Get the page data and make sure it is a page.
2615 $actual_page = get_post($page_id, ARRAY_A);
2616 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2617 return(new IXR_Error(404, __('Sorry, no such page.')));
2619 // Make sure the user is allowed to edit pages.
2620 if ( !current_user_can('edit_page', $page_id) )
2621 return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
2623 // Mark this as content for a page.
2624 $content['post_type'] = 'page';
2626 // Arrange args in the way mw_editPost understands.
2635 // Let mw_editPost do all of the heavy lifting.
2636 return($this->mw_editPost($args));
2640 * Retrieve page list.
2644 * @param array $args Method parameters.
2647 function wp_getPageList($args) {
2650 $this->escape($args);
2652 $blog_id = (int) $args[0];
2653 $username = $args[1];
2654 $password = $args[2];
2656 if ( !$user = $this->login($username, $password) )
2657 return $this->error;
2659 if ( !current_user_can( 'edit_pages' ) )
2660 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2662 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2663 do_action( 'xmlrpc_call', 'wp.getPageList' );
2665 // Get list of pages ids and titles
2666 $page_list = $wpdb->get_results("
2668 post_title page_title,
2669 post_parent page_parent_id,
2674 WHERE post_type = 'page'
2678 // The date needs to be formatted properly.
2679 $num_pages = count($page_list);
2680 for ( $i = 0; $i < $num_pages; $i++ ) {
2681 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2682 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2684 unset($page_list[$i]->post_date_gmt);
2685 unset($page_list[$i]->post_date);
2686 unset($page_list[$i]->post_status);
2693 * Retrieve authors list.
2697 * @param array $args Method parameters.
2700 function wp_getAuthors($args) {
2702 $this->escape($args);
2704 $blog_id = (int) $args[0];
2705 $username = $args[1];
2706 $password = $args[2];
2708 if ( !$user = $this->login($username, $password) )
2709 return $this->error;
2711 if ( !current_user_can('edit_posts') )
2712 return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
2714 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2715 do_action( 'xmlrpc_call', 'wp.getAuthors' );
2718 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2720 'user_id' => $user->ID,
2721 'user_login' => $user->user_login,
2722 'display_name' => $user->display_name
2730 * Get list of all tags
2734 * @param array $args Method parameters.
2737 function wp_getTags( $args ) {
2738 $this->escape( $args );
2740 $blog_id = (int) $args[0];
2741 $username = $args[1];
2742 $password = $args[2];
2744 if ( !$user = $this->login($username, $password) )
2745 return $this->error;
2747 if ( !current_user_can( 'edit_posts' ) )
2748 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2750 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2751 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2755 if ( $all_tags = get_tags() ) {
2756 foreach( (array) $all_tags as $tag ) {
2757 $struct['tag_id'] = $tag->term_id;
2758 $struct['name'] = $tag->name;
2759 $struct['count'] = $tag->count;
2760 $struct['slug'] = $tag->slug;
2761 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2762 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
2772 * Create new category.
2776 * @param array $args Method parameters.
2777 * @return int Category ID.
2779 function wp_newCategory($args) {
2780 $this->escape($args);
2782 $blog_id = (int) $args[0];
2783 $username = $args[1];
2784 $password = $args[2];
2785 $category = $args[3];
2787 if ( !$user = $this->login($username, $password) )
2788 return $this->error;
2790 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2791 do_action( 'xmlrpc_call', 'wp.newCategory' );
2793 // Make sure the user is allowed to add a category.
2794 if ( !current_user_can('manage_categories') )
2795 return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
2797 // If no slug was provided make it empty so that
2798 // WordPress will generate one.
2799 if ( empty($category['slug']) )
2800 $category['slug'] = '';
2802 // If no parent_id was provided make it empty
2803 // so that it will be a top level page (no parent).
2804 if ( !isset($category['parent_id']) )
2805 $category['parent_id'] = '';
2807 // If no description was provided make it empty.
2808 if ( empty($category["description"]) )
2809 $category["description"] = "";
2811 $new_category = array(
2812 'cat_name' => $category['name'],
2813 'category_nicename' => $category['slug'],
2814 'category_parent' => $category['parent_id'],
2815 'category_description' => $category['description']
2818 $cat_id = wp_insert_category($new_category, true);
2819 if ( is_wp_error( $cat_id ) ) {
2820 if ( 'term_exists' == $cat_id->get_error_code() )
2821 return (int) $cat_id->get_error_data();
2823 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2824 } elseif ( ! $cat_id ) {
2825 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2829 * Fires after a new category has been successfully created via XML-RPC.
2833 * @param int $cat_id ID of the new category.
2834 * @param array $args An array of new category arguments.
2836 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
2846 * @param array $args Method parameters.
2847 * @return mixed See {@link wp_delete_term()} for return info.
2849 function wp_deleteCategory($args) {
2850 $this->escape($args);
2852 $blog_id = (int) $args[0];
2853 $username = $args[1];
2854 $password = $args[2];
2855 $category_id = (int) $args[3];
2857 if ( !$user = $this->login($username, $password) )
2858 return $this->error;
2860 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2861 do_action( 'xmlrpc_call', 'wp.deleteCategory' );
2863 if ( !current_user_can('manage_categories') )
2864 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
2866 $status = wp_delete_term( $category_id, 'category' );
2868 if ( true == $status ) {
2870 * Fires after a category has been successfully deleted via XML-RPC.
2874 * @param int $category_id ID of the deleted category.
2875 * @param array $args An array of arguments to delete the category.
2877 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
2884 * Retrieve category list.
2888 * @param array $args Method parameters.
2891 function wp_suggestCategories($args) {
2892 $this->escape($args);
2894 $blog_id = (int) $args[0];
2895 $username = $args[1];
2896 $password = $args[2];
2897 $category = $args[3];
2898 $max_results = (int) $args[4];
2900 if ( !$user = $this->login($username, $password) )
2901 return $this->error;
2903 if ( !current_user_can( 'edit_posts' ) )
2904 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
2906 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2907 do_action( 'xmlrpc_call', 'wp.suggestCategories' );
2909 $category_suggestions = array();
2910 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
2911 foreach ( (array) get_categories($args) as $cat ) {
2912 $category_suggestions[] = array(
2913 'category_id' => $cat->term_id,
2914 'category_name' => $cat->name
2918 return($category_suggestions);
2926 * @param array $args Method parameters.
2929 function wp_getComment($args) {
2930 $this->escape($args);
2932 $blog_id = (int) $args[0];
2933 $username = $args[1];
2934 $password = $args[2];
2935 $comment_id = (int) $args[3];
2937 if ( !$user = $this->login($username, $password) )
2938 return $this->error;
2940 if ( !current_user_can( 'moderate_comments' ) )
2941 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2943 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2944 do_action( 'xmlrpc_call', 'wp.getComment' );
2946 if ( ! $comment = get_comment($comment_id) )
2947 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2949 return $this->_prepare_comment( $comment );
2953 * Retrieve comments.
2955 * Besides the common blog_id, username, and password arguments, it takes a filter
2956 * array as last argument.
2958 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
2960 * The defaults are as follows:
2961 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
2962 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
2963 * - 'number' - Default is 10. Total number of media items to retrieve.
2964 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2968 * @param array $args Method parameters.
2969 * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
2971 function wp_getComments($args) {
2972 $this->escape($args);
2974 $blog_id = (int) $args[0];
2975 $username = $args[1];
2976 $password = $args[2];
2977 $struct = isset( $args[3] ) ? $args[3] : array();
2979 if ( !$user = $this->login($username, $password) )
2980 return $this->error;
2982 if ( !current_user_can( 'moderate_comments' ) )
2983 return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
2985 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2986 do_action( 'xmlrpc_call', 'wp.getComments' );
2988 if ( isset($struct['status']) )
2989 $status = $struct['status'];
2994 if ( isset($struct['post_id']) )
2995 $post_id = absint($struct['post_id']);
2998 if ( isset($struct['offset']) )
2999 $offset = absint($struct['offset']);
3002 if ( isset($struct['number']) )
3003 $number = absint($struct['number']);
3005 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
3007 $comments_struct = array();
3009 foreach ( $comments as $comment ) {
3010 $comments_struct[] = $this->_prepare_comment( $comment );
3013 return $comments_struct;
3019 * By default, the comment will be moved to the trash instead of deleted.
3020 * See {@link wp_delete_comment()} for more information on
3025 * @param array $args Method parameters. Contains:
3030 * @return mixed {@link wp_delete_comment()}
3032 function wp_deleteComment($args) {
3033 $this->escape($args);
3035 $blog_id = (int) $args[0];
3036 $username = $args[1];
3037 $password = $args[2];
3038 $comment_ID = (int) $args[3];
3040 if ( !$user = $this->login($username, $password) )
3041 return $this->error;
3043 if ( !current_user_can( 'moderate_comments' ) )
3044 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3046 if ( ! get_comment($comment_ID) )
3047 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3049 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3050 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3052 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3053 do_action( 'xmlrpc_call', 'wp.deleteComment' );
3055 $status = wp_delete_comment( $comment_ID );
3057 if ( true == $status ) {
3059 * Fires after a comment has been successfully deleted via XML-RPC.
3063 * @param int $comment_ID ID of the deleted comment.
3064 * @param array $args An array of arguments to delete the comment.
3066 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
3075 * Besides the common blog_id, username, and password arguments, it takes a
3076 * comment_id integer and a content_struct array as last argument.
3078 * The allowed keys in the content_struct array are:
3083 * - 'date_created_gmt'
3084 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
3088 * @param array $args. Contains:
3094 * @return bool True, on success.
3096 function wp_editComment($args) {
3097 $this->escape($args);
3099 $blog_id = (int) $args[0];
3100 $username = $args[1];
3101 $password = $args[2];
3102 $comment_ID = (int) $args[3];
3103 $content_struct = $args[4];
3105 if ( !$user = $this->login($username, $password) )
3106 return $this->error;
3108 if ( !current_user_can( 'moderate_comments' ) )
3109 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3111 if ( ! get_comment($comment_ID) )
3112 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3114 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3115 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3117 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3118 do_action( 'xmlrpc_call', 'wp.editComment' );
3120 if ( isset($content_struct['status']) ) {
3121 $statuses = get_comment_statuses();
3122 $statuses = array_keys($statuses);
3124 if ( ! in_array($content_struct['status'], $statuses) )
3125 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
3126 $comment_approved = $content_struct['status'];
3129 // Do some timestamp voodoo
3130 if ( !empty( $content_struct['date_created_gmt'] ) ) {
3131 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
3132 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
3133 $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
3134 $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
3137 if ( isset($content_struct['content']) )
3138 $comment_content = $content_struct['content'];
3140 if ( isset($content_struct['author']) )
3141 $comment_author = $content_struct['author'];
3143 if ( isset($content_struct['author_url']) )
3144 $comment_author_url = $content_struct['author_url'];
3146 if ( isset($content_struct['author_email']) )
3147 $comment_author_email = $content_struct['author_email'];
3149 // We've got all the data -- post it:
3150 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
3152 $result = wp_update_comment($comment);
3153 if ( is_wp_error( $result ) )
3154 return new IXR_Error(500, $result->get_error_message());
3157 return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
3160 * Fires after a comment has been successfully updated via XML-RPC.
3164 * @param int $comment_ID ID of the updated comment.
3165 * @param array $args An array of arguments to update the comment.
3167 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
3173 * Create new comment.
3177 * @param array $args Method parameters.
3178 * @return mixed {@link wp_new_comment()}
3180 function wp_newComment($args) {
3183 $this->escape($args);
3185 $blog_id = (int) $args[0];
3186 $username = $args[1];
3187 $password = $args[2];
3189 $content_struct = $args[4];
3192 * Filter whether to allow anonymous comments over XML-RPC.
3196 * @param bool $allow Whether to allow anonymous commenting via XML-RPC.
3199 $allow_anon = apply_filters( 'xmlrpc_allow_anonymous_comments', false );
3201 $user = $this->login($username, $password);
3205 if ( $allow_anon && get_option('comment_registration') )
3206 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
3207 else if ( !$allow_anon )
3208 return $this->error;
3213 if ( is_numeric($post) )
3214 $post_id = absint($post);
3216 $post_id = url_to_postid($post);
3219 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3221 if ( ! get_post($post_id) )
3222 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3224 $comment['comment_post_ID'] = $post_id;
3227 $comment['comment_author'] = $this->escape( $user->display_name );
3228 $comment['comment_author_email'] = $this->escape( $user->user_email );
3229 $comment['comment_author_url'] = $this->escape( $user->user_url );
3230 $comment['user_ID'] = $user->ID;
3232 $comment['comment_author'] = '';
3233 if ( isset($content_struct['author']) )
3234 $comment['comment_author'] = $content_struct['author'];
3236 $comment['comment_author_email'] = '';
3237 if ( isset($content_struct['author_email']) )
3238 $comment['comment_author_email'] = $content_struct['author_email'];
3240 $comment['comment_author_url'] = '';
3241 if ( isset($content_struct['author_url']) )
3242 $comment['comment_author_url'] = $content_struct['author_url'];
3244 $comment['user_ID'] = 0;
3246 if ( get_option('require_name_email') ) {
3247 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
3248 return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
3249 elseif ( !is_email($comment['comment_author_email']) )
3250 return new IXR_Error( 403, __( 'A valid email address is required' ) );
3254 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
3256 $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
3258 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3259 do_action( 'xmlrpc_call', 'wp.newComment' );
3261 $comment_ID = wp_new_comment( $comment );
3264 * Fires after a new comment has been successfully created via XML-RPC.
3268 * @param int $comment_ID ID of the new comment.
3269 * @param array $args An array of new comment arguments.
3271 do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
3277 * Retrieve all of the comment status.
3281 * @param array $args Method parameters.
3284 function wp_getCommentStatusList($args) {
3285 $this->escape( $args );
3287 $blog_id = (int) $args[0];
3288 $username = $args[1];
3289 $password = $args[2];
3291 if ( !$user = $this->login($username, $password) )
3292 return $this->error;
3294 if ( !current_user_can( 'moderate_comments' ) )
3295 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3297 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3298 do_action( 'xmlrpc_call', 'wp.getCommentStatusList' );
3300 return get_comment_statuses();
3304 * Retrieve comment count.
3308 * @param array $args Method parameters.
3311 function wp_getCommentCount( $args ) {
3312 $this->escape($args);
3314 $blog_id = (int) $args[0];
3315 $username = $args[1];
3316 $password = $args[2];
3317 $post_id = (int) $args[3];
3319 if ( !$user = $this->login($username, $password) )
3320 return $this->error;
3322 if ( !current_user_can( 'edit_posts' ) )
3323 return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
3325 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3326 do_action( 'xmlrpc_call', 'wp.getCommentCount' );
3328 $count = wp_count_comments( $post_id );
3330 'approved' => $count->approved,
3331 'awaiting_moderation' => $count->moderated,
3332 'spam' => $count->spam,
3333 'total_comments' => $count->total_comments
3338 * Retrieve post statuses.
3342 * @param array $args Method parameters.
3345 function wp_getPostStatusList( $args ) {
3346 $this->escape( $args );
3348 $blog_id = (int) $args[0];
3349 $username = $args[1];
3350 $password = $args[2];
3352 if ( !$user = $this->login($username, $password) )
3353 return $this->error;
3355 if ( !current_user_can( 'edit_posts' ) )
3356 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3358 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3359 do_action( 'xmlrpc_call', 'wp.getPostStatusList' );
3361 return get_post_statuses();
3365 * Retrieve page statuses.
3369 * @param array $args Method parameters.
3372 function wp_getPageStatusList( $args ) {
3373 $this->escape( $args );
3375 $blog_id = (int) $args[0];
3376 $username = $args[1];
3377 $password = $args[2];
3379 if ( !$user = $this->login($username, $password) )
3380 return $this->error;
3382 if ( !current_user_can( 'edit_pages' ) )
3383 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3385 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3386 do_action( 'xmlrpc_call', 'wp.getPageStatusList' );
3388 return get_page_statuses();
3392 * Retrieve page templates.
3396 * @param array $args Method parameters.
3399 function wp_getPageTemplates( $args ) {
3400 $this->escape( $args );
3402 $blog_id = (int) $args[0];
3403 $username = $args[1];
3404 $password = $args[2];
3406 if ( !$user = $this->login($username, $password) )
3407 return $this->error;
3409 if ( !current_user_can( 'edit_pages' ) )
3410 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3412 $templates = get_page_templates();
3413 $templates['Default'] = 'default';
3419 * Retrieve blog options.
3423 * @param array $args Method parameters.
3426 function wp_getOptions( $args ) {
3427 $this->escape( $args );
3429 $blog_id = (int) $args[0];
3430 $username = $args[1];
3431 $password = $args[2];
3432 $options = isset( $args[3] ) ? (array) $args[3] : array();
3434 if ( !$user = $this->login($username, $password) )
3435 return $this->error;
3437 // If no specific options where asked for, return all of them
3438 if ( count( $options ) == 0 )
3439 $options = array_keys($this->blog_options);
3441 return $this->_getOptions($options);
3445 * Retrieve blog options value from list.
3449 * @param array $options Options to retrieve.
3452 function _getOptions($options) {
3454 $can_manage = current_user_can( 'manage_options' );
3455 foreach ( $options as $option ) {
3456 if ( array_key_exists( $option, $this->blog_options ) ) {
3457 $data[$option] = $this->blog_options[$option];
3458 //Is the value static or dynamic?
3459 if ( isset( $data[$option]['option'] ) ) {
3460 $data[$option]['value'] = get_option( $data[$option]['option'] );
3461 unset($data[$option]['option']);
3464 if ( ! $can_manage )
3465 $data[$option]['readonly'] = true;
3473 * Update blog options.
3477 * @param array $args Method parameters.
3480 function wp_setOptions( $args ) {
3481 $this->escape( $args );
3483 $blog_id = (int) $args[0];
3484 $username = $args[1];
3485 $password = $args[2];
3486 $options = (array) $args[3];
3488 if ( !$user = $this->login($username, $password) )
3489 return $this->error;
3491 if ( !current_user_can( 'manage_options' ) )
3492 return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
3494 foreach ( $options as $o_name => $o_value ) {
3495 $option_names[] = $o_name;
3496 if ( !array_key_exists( $o_name, $this->blog_options ) )
3499 if ( $this->blog_options[$o_name]['readonly'] == true )
3502 update_option( $this->blog_options[$o_name]['option'], wp_unslash( $o_value ) );
3505 //Now return the updated values
3506 return $this->_getOptions($option_names);
3510 * Retrieve a media item by ID
3514 * @param array $args Method parameters. Contains:
3519 * @return array. Associative array containing:
3520 * - 'date_created_gmt'
3529 function wp_getMediaItem($args) {
3530 $this->escape($args);
3532 $blog_id = (int) $args[0];
3533 $username = $args[1];
3534 $password = $args[2];
3535 $attachment_id = (int) $args[3];
3537 if ( !$user = $this->login($username, $password) )
3538 return $this->error;
3540 if ( !current_user_can( 'upload_files' ) )
3541 return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
3543 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3544 do_action( 'xmlrpc_call', 'wp.getMediaItem' );
3546 if ( ! $attachment = get_post($attachment_id) )
3547 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
3549 return $this->_prepare_media_item( $attachment );
3553 * Retrieves a collection of media library items (or attachments)
3555 * Besides the common blog_id, username, and password arguments, it takes a filter
3556 * array as last argument.
3558 * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
3560 * The defaults are as follows:
3561 * - 'number' - Default is 5. Total number of media items to retrieve.
3562 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
3563 * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
3564 * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
3568 * @param array $args Method parameters. Contains:
3573 * @return array. Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
3575 function wp_getMediaLibrary($args) {
3576 $this->escape($args);
3578 $blog_id = (int) $args[0];
3579 $username = $args[1];
3580 $password = $args[2];
3581 $struct = isset( $args[3] ) ? $args[3] : array() ;
3583 if ( !$user = $this->login($username, $password) )
3584 return $this->error;
3586 if ( !current_user_can( 'upload_files' ) )
3587 return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
3589 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3590 do_action( 'xmlrpc_call', 'wp.getMediaLibrary' );
3592 $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
3593 $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
3594 $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
3595 $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
3597 $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
3599 $attachments_struct = array();
3601 foreach ($attachments as $attachment )
3602 $attachments_struct[] = $this->_prepare_media_item( $attachment );
3604 return $attachments_struct;
3608 * Retrieves a list of post formats used by the site
3612 * @param array $args Method parameters. Contains:
3618 function wp_getPostFormats( $args ) {
3619 $this->escape( $args );
3621 $blog_id = (int) $args[0];
3622 $username = $args[1];
3623 $password = $args[2];
3625 if ( !$user = $this->login( $username, $password ) )
3626 return $this->error;
3628 if ( !current_user_can( 'edit_posts' ) )
3629 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3631 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3632 do_action( 'xmlrpc_call', 'wp.getPostFormats' );
3634 $formats = get_post_format_strings();
3636 # find out if they want a list of currently supports formats
3637 if ( isset( $args[3] ) && is_array( $args[3] ) ) {
3638 if ( $args[3]['show-supported'] ) {
3639 if ( current_theme_supports( 'post-formats' ) ) {
3640 $supported = get_theme_support( 'post-formats' );
3642 $data['all'] = $formats;
3643 $data['supported'] = $supported[0];
3654 * Retrieves a post type
3658 * @uses get_post_type_object()
3659 * @param array $args Method parameters. Contains:
3661 * - string $username
3662 * - string $password
3663 * - string $post_type_name
3665 * @return array contains:
3668 * - 'capability_type'
3676 function wp_getPostType( $args ) {
3677 if ( ! $this->minimum_args( $args, 4 ) )
3678 return $this->error;
3680 $this->escape( $args );
3682 $blog_id = (int) $args[0];
3683 $username = $args[1];
3684 $password = $args[2];
3685 $post_type_name = $args[3];
3687 if ( isset( $args[4] ) ) {
3691 * Filter the default query fields used by the given XML-RPC method.
3695 * @param array $fields An array of post type query fields for the given method.
3696 * @param string $method The method name.
3698 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
3701 if ( !$user = $this->login( $username, $password ) )
3702 return $this->error;
3704 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3705 do_action( 'xmlrpc_call', 'wp.getPostType' );
3707 if( ! post_type_exists( $post_type_name ) )
3708 return new IXR_Error( 403, __( 'Invalid post type' ) );
3710 $post_type = get_post_type_object( $post_type_name );
3712 if( ! current_user_can( $post_type->cap->edit_posts ) )
3713 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
3715 return $this->_prepare_post_type( $post_type, $fields );
3719 * Retrieves a post types
3723 * @uses get_post_types()
3724 * @param array $args Method parameters. Contains:
3726 * - string $username
3727 * - string $password
3732 function wp_getPostTypes( $args ) {
3733 if ( ! $this->minimum_args( $args, 3 ) )
3734 return $this->error;
3736 $this->escape( $args );
3738 $blog_id = (int) $args[0];
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:
3783 * - string $username
3784 * - string $password
3787 * @return array contains a collection of posts.
3789 function wp_getRevisions( $args ) {
3790 if ( ! $this->minimum_args( $args, 4 ) )
3791 return $this->error;
3793 $this->escape( $args );
3795 $blog_id = (int) $args[0];
3796 $username = $args[1];
3797 $password = $args[2];
3798 $post_id = (int) $args[3];
3800 if ( isset( $args[4] ) ) {
3804 * Filter the default revision query fields used by the given XML-RPC method.
3808 * @param array $field An array of revision query fields.
3809 * @param string $method The method name.
3811 $fields = apply_filters( 'xmlrpc_default_revision_fields', array( 'post_date', 'post_date_gmt' ), 'wp.getRevisions' );
3814 if ( ! $user = $this->login( $username, $password ) )
3815 return $this->error;
3817 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3818 do_action( 'xmlrpc_call', 'wp.getRevisions' );
3820 if ( ! $post = get_post( $post_id ) )
3821 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3823 if ( ! current_user_can( 'edit_post', $post_id ) )
3824 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
3826 // Check if revisions are enabled.
3827 if ( ! wp_revisions_enabled( $post ) )
3828 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
3830 $revisions = wp_get_post_revisions( $post_id );
3837 foreach ( $revisions as $revision ) {
3838 if ( ! current_user_can( 'read_post', $revision->ID ) )
3842 if ( wp_is_post_autosave( $revision ) )
3845 $struct[] = $this->_prepare_post( get_object_vars( $revision ), $fields );
3852 * Restore a post revision
3856 * @uses wp_restore_post_revision()
3858 * @param array $args Method parameters. Contains:
3860 * - string $username
3861 * - string $password
3863 * @return bool false if there was an error restoring, true if success.
3865 function wp_restoreRevision( $args ) {
3866 if ( ! $this->minimum_args( $args, 3 ) )
3867 return $this->error;
3869 $this->escape( $args );
3871 $blog_id = (int) $args[0];
3872 $username = $args[1];
3873 $password = $args[2];
3874 $revision_id = (int) $args[3];
3876 if ( ! $user = $this->login( $username, $password ) )
3877 return $this->error;
3879 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3880 do_action( 'xmlrpc_call', 'wp.restoreRevision' );
3882 if ( ! $revision = wp_get_post_revision( $revision_id ) )
3883 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3885 if ( wp_is_post_autosave( $revision ) )
3886 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3888 if ( ! $post = get_post( $revision->post_parent ) )
3889 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3891 if ( ! current_user_can( 'edit_post', $revision->post_parent ) )
3892 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
3894 // Check if revisions are disabled.
3895 if ( ! wp_revisions_enabled( $post ) )
3896 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
3898 $post = wp_restore_post_revision( $revision_id );
3900 return (bool) $post;
3903 /* Blogger API functions.
3904 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
3908 * Retrieve blogs that user owns.
3910 * Will make more sense once we support multiple blogs.
3914 * @param array $args Method parameters.
3917 function blogger_getUsersBlogs($args) {
3918 if ( is_multisite() )
3919 return $this->_multisite_getUsersBlogs($args);
3921 $this->escape($args);
3923 $username = $args[1];
3924 $password = $args[2];
3926 if ( !$user = $this->login($username, $password) )
3927 return $this->error;
3929 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3930 do_action( 'xmlrpc_call', 'blogger.getUsersBlogs' );
3932 $is_admin = current_user_can('manage_options');
3935 'isAdmin' => $is_admin,
3936 'url' => get_option('home') . '/',
3938 'blogName' => get_option('blogname'),
3939 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
3942 return array($struct);
3946 * Private function for retrieving a users blogs for multisite setups
3950 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.
3984 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.
4019 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.
4064 function blogger_getRecentPosts($args) {
4066 $this->escape($args);
4068 // $args[0] = appkey - ignored
4069 $blog_ID = (int) $args[1]; /* though we don't use it yet */
4070 $username = $args[2];
4071 $password = $args[3];
4072 if ( isset( $args[4] ) )
4073 $query = array( 'numberposts' => absint( $args[4] ) );
4077 if ( !$user = $this->login($username, $password) )
4078 return $this->error;
4080 if ( ! current_user_can( 'edit_posts' ) )
4081 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
4083 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4084 do_action( 'xmlrpc_call', 'blogger.getRecentPosts' );
4086 $posts_list = wp_get_recent_posts( $query );
4088 if ( !$posts_list ) {
4089 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
4090 return $this->error;
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']);
4105 'userid' => $entry['post_author'],
4106 'dateCreated' => $post_date,
4107 'content' => $content,
4108 'postid' => (string) $entry['ID'],
4113 $recent_posts = array();
4114 for ( $j=0; $j<count($struct); $j++ ) {
4115 array_push($recent_posts, $struct[$j]);
4118 return $recent_posts;
4127 function blogger_getTemplate($args) {
4128 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4137 function blogger_setTemplate($args) {
4138 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4146 * @param array $args Method parameters.
4149 function blogger_newPost($args) {
4151 $this->escape($args);
4153 $blog_ID = (int) $args[1]; /* though we don't use it yet */
4154 $username = $args[2];
4155 $password = $args[3];
4156 $content = $args[4];
4157 $publish = $args[5];
4159 if ( !$user = $this->login($username, $password) )
4160 return $this->error;
4162 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4163 do_action( 'xmlrpc_call', 'blogger.newPost' );
4165 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
4166 if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) || !current_user_can($cap) )
4167 return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
4169 $post_status = ($publish) ? 'publish' : 'draft';
4171 $post_author = $user->ID;
4173 $post_title = xmlrpc_getposttitle($content);
4174 $post_category = xmlrpc_getpostcategory($content);
4175 $post_content = xmlrpc_removepostdata($content);
4177 $post_date = current_time('mysql');
4178 $post_date_gmt = current_time('mysql', 1);
4180 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
4182 $post_ID = wp_insert_post($post_data);
4183 if ( is_wp_error( $post_ID ) )
4184 return new IXR_Error(500, $post_ID->get_error_message());
4187 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4189 $this->attach_uploads( $post_ID, $post_content );
4192 * Fires after a new post has been successfully created via the XML-RPC Blogger API.
4196 * @param int $post_ID ID of the new post.
4197 * @param array $args An array of new post arguments.
4199 do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
4209 * @param array $args Method parameters.
4210 * @return bool true when done.
4212 function blogger_editPost($args) {
4214 $this->escape($args);
4216 $post_ID = (int) $args[1];
4217 $username = $args[2];
4218 $password = $args[3];
4219 $content = $args[4];
4220 $publish = $args[5];
4222 if ( !$user = $this->login($username, $password) )
4223 return $this->error;
4225 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4226 do_action( 'xmlrpc_call', 'blogger.editPost' );
4228 $actual_post = get_post($post_ID,ARRAY_A);
4230 if ( !$actual_post || $actual_post['post_type'] != 'post' )
4231 return new IXR_Error(404, __('Sorry, no such post.'));
4233 $this->escape($actual_post);
4235 if ( !current_user_can('edit_post', $post_ID) )
4236 return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
4238 extract($actual_post, EXTR_SKIP);
4240 if ( ('publish' == $post_status) && !current_user_can('publish_posts') )
4241 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
4243 $post_title = xmlrpc_getposttitle($content);
4244 $post_category = xmlrpc_getpostcategory($content);
4245 $post_content = xmlrpc_removepostdata($content);
4247 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
4249 $result = wp_update_post($postdata);
4252 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
4254 $this->attach_uploads( $ID, $post_content );
4257 * Fires after a post has been successfully updated via the XML-RPC Blogger API.
4261 * @param int $post_ID ID of the updated post.
4262 * @param array $args An array of arguments for the post to edit.
4264 do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
4274 * @param array $args Method parameters.
4275 * @return bool True when post is deleted.
4277 function blogger_deletePost($args) {
4278 $this->escape($args);
4280 $post_ID = (int) $args[1];
4281 $username = $args[2];
4282 $password = $args[3];
4283 $publish = $args[4];
4285 if ( !$user = $this->login($username, $password) )
4286 return $this->error;
4288 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4289 do_action( 'xmlrpc_call', 'blogger.deletePost' );
4291 $actual_post = get_post($post_ID,ARRAY_A);
4293 if ( !$actual_post || $actual_post['post_type'] != 'post' )
4294 return new IXR_Error(404, __('Sorry, no such post.'));
4296 if ( !current_user_can('delete_post', $post_ID) )
4297 return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
4299 $result = wp_delete_post($post_ID);
4302 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
4305 * Fires after a post has been successfully deleted via the XML-RPC Blogger API.
4309 * @param int $post_ID ID of the deleted post.
4310 * @param array $args An array of arguments to delete the post.
4312 do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
4317 /* MetaWeblog API functions
4318 * specs on wherever Dave Winer wants them to be
4322 * Create a new post.
4324 * The 'content_struct' argument must contain:
4333 * Also, it can optionally contain:
4336 * - wp_page_parent_id
4339 * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
4340 * - mt_allow_comments - can be 'open' or 'closed'
4341 * - mt_allow_pings - can be 'open' or 'closed'
4342 * - date_created_gmt
4344 * - wp_post_thumbnail
4348 * @param array $args Method parameters. Contains:
4356 function mw_newPost($args) {
4357 $this->escape($args);
4359 $blog_ID = (int) $args[0];
4360 $username = $args[1];
4361 $password = $args[2];
4362 $content_struct = $args[3];
4363 $publish = isset( $args[4] ) ? $args[4] : 0;
4365 if ( !$user = $this->login($username, $password) )
4366 return $this->error;
4368 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4369 do_action( 'xmlrpc_call', 'metaWeblog.newPost' );
4371 $page_template = '';
4372 if ( !empty( $content_struct['post_type'] ) ) {
4373 if ( $content_struct['post_type'] == 'page' ) {
4375 $cap = 'publish_pages';
4376 elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
4377 $cap = 'publish_pages';
4379 $cap = 'edit_pages';
4380 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
4381 $post_type = 'page';
4382 if ( !empty( $content_struct['wp_page_template'] ) )
4383 $page_template = $content_struct['wp_page_template'];
4384 } elseif ( $content_struct['post_type'] == 'post' ) {
4386 $cap = 'publish_posts';
4387 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
4388 $cap = 'publish_posts';
4390 $cap = 'edit_posts';
4391 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4392 $post_type = 'post';
4394 // No other post_type values are allowed here
4395 return new IXR_Error( 401, __( 'Invalid post type' ) );
4399 $cap = 'publish_posts';
4400 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
4401 $cap = 'publish_posts';
4403 $cap = 'edit_posts';
4404 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4405 $post_type = 'post';
4408 if ( ! current_user_can( get_post_type_object( $post_type )->cap->create_posts ) )
4409 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts on this site.' ) );
4410 if ( !current_user_can( $cap ) )
4411 return new IXR_Error( 401, $error_message );
4413 // Check for a valid post format if one was given
4414 if ( isset( $content_struct['wp_post_format'] ) ) {
4415 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4416 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4417 return new IXR_Error( 404, __( 'Invalid post format' ) );
4421 // Let WordPress generate the post_name (slug) unless
4422 // one has been provided.
4424 if ( isset($content_struct['wp_slug']) )
4425 $post_name = $content_struct['wp_slug'];
4427 // Only use a password if one was given.
4428 if ( isset($content_struct['wp_password']) )
4429 $post_password = $content_struct['wp_password'];
4431 // Only set a post parent if one was provided.
4432 if ( isset($content_struct['wp_page_parent_id']) )
4433 $post_parent = $content_struct['wp_page_parent_id'];
4435 // Only set the menu_order if it was provided.
4436 if ( isset($content_struct['wp_page_order']) )
4437 $menu_order = $content_struct['wp_page_order'];
4439 $post_author = $user->ID;
4441 // If an author id was provided then use it instead.
4442 if ( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) {
4443 switch ( $post_type ) {
4445 if ( !current_user_can( 'edit_others_posts' ) )
4446 return( new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) ) );
4449 if ( !current_user_can( 'edit_others_pages' ) )
4450 return( new IXR_Error( 401, __( 'You are not allowed to create pages as this user.' ) ) );
4453 return( new IXR_Error( 401, __( 'Invalid post type' ) ) );
4456 $author = get_userdata( $content_struct['wp_author_id'] );
4458 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
4459 $post_author = $content_struct['wp_author_id'];
4462 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
4463 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
4465 $post_status = $publish ? 'publish' : 'draft';
4467 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4468 switch ( $content_struct["{$post_type}_status"] ) {
4473 $post_status = $content_struct["{$post_type}_status"];
4476 $post_status = $publish ? 'publish' : 'draft';
4481 $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
4482 $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
4484 $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
4486 if ( isset($content_struct['mt_allow_comments']) ) {
4487 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4488 switch ( $content_struct['mt_allow_comments'] ) {
4490 $comment_status = 'closed';
4493 $comment_status = 'open';
4496 $comment_status = get_option('default_comment_status');
4500 switch ( (int) $content_struct['mt_allow_comments'] ) {
4503 $comment_status = 'closed';
4506 $comment_status = 'open';
4509 $comment_status = get_option('default_comment_status');
4514 $comment_status = get_option('default_comment_status');
4517 if ( isset($content_struct['mt_allow_pings']) ) {
4518 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4519 switch ( $content_struct['mt_allow_pings'] ) {
4521 $ping_status = 'closed';
4524 $ping_status = 'open';
4527 $ping_status = get_option('default_ping_status');
4531 switch ( (int) $content_struct['mt_allow_pings'] ) {
4533 $ping_status = 'closed';
4536 $ping_status = 'open';
4539 $ping_status = get_option('default_ping_status');
4544 $ping_status = get_option('default_ping_status');
4548 $post_content = $post_content . '<!--more-->' . $post_more;
4551 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4552 $to_ping = $content_struct['mt_tb_ping_urls'];
4553 if ( is_array($to_ping) )
4554 $to_ping = implode(' ', $to_ping);
4557 // Do some timestamp voodoo
4558 if ( !empty( $content_struct['date_created_gmt'] ) )
4559 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4560 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4561 elseif ( !empty( $content_struct['dateCreated']) )
4562 $dateCreated = $content_struct['dateCreated']->getIso();
4564 if ( !empty( $dateCreated ) ) {
4565 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4566 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4568 $post_date = current_time('mysql');
4569 $post_date_gmt = current_time('mysql', 1);
4572 $post_category = array();
4573 if ( isset( $content_struct['categories'] ) ) {
4574 $catnames = $content_struct['categories'];
4576 if ( is_array($catnames) ) {
4577 foreach ($catnames as $cat) {
4578 $post_category[] = get_cat_ID($cat);
4583 $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');
4585 $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
4587 // Only posts can be sticky
4588 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4589 if ( $content_struct['sticky'] == true )
4590 stick_post( $post_ID );
4591 elseif ( $content_struct['sticky'] == false )
4592 unstick_post( $post_ID );
4595 if ( isset($content_struct['custom_fields']) )
4596 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4598 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4599 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4600 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4602 unset( $content_struct['wp_post_thumbnail'] );
4605 // Handle enclosures
4606 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4607 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4609 $this->attach_uploads( $post_ID, $post_content );
4611 // Handle post formats if assigned, value is validated earlier
4613 if ( isset( $content_struct['wp_post_format'] ) )
4614 set_post_format( $post_ID, $content_struct['wp_post_format'] );
4616 $post_ID = wp_insert_post( $postdata, true );
4617 if ( is_wp_error( $post_ID ) )
4618 return new IXR_Error(500, $post_ID->get_error_message());
4621 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4624 * Fires after a new post has been successfully created via the XML-RPC MovableType API.
4628 * @param int $post_ID ID of the new post.
4629 * @param array $args An array of arguments to create the new post.
4631 do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
4633 return strval($post_ID);
4636 function add_enclosure_if_new( $post_ID, $enclosure ) {
4637 if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
4638 $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'] . "\n";
4640 if ( $enclosures = get_post_meta( $post_ID, 'enclosure' ) ) {
4641 foreach ( $enclosures as $enc ) {
4642 // This method used to omit the trailing new line. #23219
4643 if ( rtrim( $enc, "\n" ) == rtrim( $encstring, "\n" ) ) {
4650 add_post_meta( $post_ID, 'enclosure', $encstring );
4655 * Attach upload to a post.
4659 * @param int $post_ID Post ID.
4660 * @param string $post_content Post Content for attachment.
4662 function attach_uploads( $post_ID, $post_content ) {
4665 // find any unattached files
4666 $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
4667 if ( is_array( $attachments ) ) {
4668 foreach ( $attachments as $file ) {
4669 if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false )
4670 $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
4680 * @param array $args Method parameters.
4681 * @return bool True on success.
4683 function mw_editPost($args) {
4685 $this->escape($args);
4687 $post_ID = (int) $args[0];
4688 $username = $args[1];
4689 $password = $args[2];
4690 $content_struct = $args[3];
4691 $publish = isset( $args[4] ) ? $args[4] : 0;
4693 if ( ! $user = $this->login($username, $password) )
4694 return $this->error;
4696 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4697 do_action( 'xmlrpc_call', 'metaWeblog.editPost' );
4699 $postdata = get_post( $post_ID, ARRAY_A );
4701 // If there is no post data for the give post id, stop
4702 // now and return an error. Other wise a new post will be
4703 // created (which was the old behavior).
4704 if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
4705 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4707 if ( ! current_user_can( 'edit_post', $post_ID ) )
4708 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this post.' ) );
4710 // Use wp.editPost to edit post types other than post and page.
4711 if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
4712 return new IXR_Error( 401, __( 'Invalid post type' ) );
4714 // Thwart attempt to change the post type.
4715 if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
4716 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
4718 // Check for a valid post format if one was given
4719 if ( isset( $content_struct['wp_post_format'] ) ) {
4720 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4721 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4722 return new IXR_Error( 404, __( 'Invalid post format' ) );
4726 $this->escape($postdata);
4727 extract($postdata, EXTR_SKIP);
4729 // Let WordPress manage slug if none was provided.
4731 $post_name = $postdata['post_name'];
4732 if ( isset($content_struct['wp_slug']) )
4733 $post_name = $content_struct['wp_slug'];
4735 // Only use a password if one was given.
4736 if ( isset($content_struct['wp_password']) )
4737 $post_password = $content_struct['wp_password'];
4739 // Only set a post parent if one was given.
4740 if ( isset($content_struct['wp_page_parent_id']) )
4741 $post_parent = $content_struct['wp_page_parent_id'];
4743 // Only set the menu_order if it was given.
4744 if ( isset($content_struct['wp_page_order']) )
4745 $menu_order = $content_struct['wp_page_order'];
4747 if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
4748 $page_template = $content_struct['wp_page_template'];
4750 $post_author = $postdata['post_author'];
4752 // Only set the post_author if one is set.
4753 if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
4754 switch ( $post_type ) {
4756 if ( !current_user_can('edit_others_posts') )
4757 return(new IXR_Error(401, __('You are not allowed to change the post author as this user.')));
4760 if ( !current_user_can('edit_others_pages') )
4761 return(new IXR_Error(401, __('You are not allowed to change the page author as this user.')));
4764 return(new IXR_Error(401, __('Invalid post type')));
4767 $post_author = $content_struct['wp_author_id'];
4770 if ( isset($content_struct['mt_allow_comments']) ) {
4771 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4772 switch ( $content_struct['mt_allow_comments'] ) {
4774 $comment_status = 'closed';
4777 $comment_status = 'open';
4780 $comment_status = get_option('default_comment_status');
4784 switch ( (int) $content_struct['mt_allow_comments'] ) {
4787 $comment_status = 'closed';
4790 $comment_status = 'open';
4793 $comment_status = get_option('default_comment_status');
4799 if ( isset($content_struct['mt_allow_pings']) ) {
4800 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4801 switch ( $content_struct['mt_allow_pings'] ) {
4803 $ping_status = 'closed';
4806 $ping_status = 'open';
4809 $ping_status = get_option('default_ping_status');
4813 switch ( (int) $content_struct["mt_allow_pings"] ) {
4815 $ping_status = 'closed';
4818 $ping_status = 'open';
4821 $ping_status = get_option('default_ping_status');
4827 if ( isset( $content_struct['title'] ) )
4828 $post_title = $content_struct['title'];
4830 if ( isset( $content_struct['description'] ) )
4831 $post_content = $content_struct['description'];
4833 $post_category = array();
4834 if ( isset( $content_struct['categories'] ) ) {
4835 $catnames = $content_struct['categories'];
4836 if ( is_array($catnames) ) {
4837 foreach ($catnames as $cat) {
4838 $post_category[] = get_cat_ID($cat);
4843 if ( isset( $content_struct['mt_excerpt'] ) )
4844 $post_excerpt = $content_struct['mt_excerpt'];
4846 $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
4848 $post_status = $publish ? 'publish' : 'draft';
4849 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4850 switch( $content_struct["{$post_type}_status"] ) {
4855 $post_status = $content_struct["{$post_type}_status"];
4858 $post_status = $publish ? 'publish' : 'draft';
4863 $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
4865 if ( ('publish' == $post_status) ) {
4866 if ( ( 'page' == $post_type ) && !current_user_can('publish_pages') )
4867 return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
4868 else if ( !current_user_can('publish_posts') )
4869 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
4873 $post_content = $post_content . "<!--more-->" . $post_more;
4876 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4877 $to_ping = $content_struct['mt_tb_ping_urls'];
4878 if ( is_array($to_ping) )
4879 $to_ping = implode(' ', $to_ping);
4882 // Do some timestamp voodoo
4883 if ( !empty( $content_struct['date_created_gmt'] ) )
4884 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4885 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4886 elseif ( !empty( $content_struct['dateCreated']) )
4887 $dateCreated = $content_struct['dateCreated']->getIso();
4889 if ( !empty( $dateCreated ) ) {
4890 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4891 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4893 $post_date = $postdata['post_date'];
4894 $post_date_gmt = $postdata['post_date_gmt'];
4897 // We've got all the data -- post it:
4898 $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');
4900 $result = wp_update_post($newpost, true);
4901 if ( is_wp_error( $result ) )
4902 return new IXR_Error(500, $result->get_error_message());
4905 return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
4907 // Only posts can be sticky
4908 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4909 if ( $content_struct['sticky'] == true )
4910 stick_post( $post_ID );
4911 elseif ( $content_struct['sticky'] == false )
4912 unstick_post( $post_ID );
4915 if ( isset($content_struct['custom_fields']) )
4916 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4918 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4919 // empty value deletes, non-empty value adds/updates
4920 if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
4921 delete_post_thumbnail( $post_ID );
4923 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4924 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4926 unset( $content_struct['wp_post_thumbnail'] );
4929 // Handle enclosures
4930 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4931 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4933 $this->attach_uploads( $ID, $post_content );
4935 // Handle post formats if assigned, validation is handled
4936 // earlier in this function
4937 if ( isset( $content_struct['wp_post_format'] ) )
4938 set_post_format( $post_ID, $content_struct['wp_post_format'] );
4941 * Fires after a post has been successfully updated via the XML-RPC MovableType API.
4945 * @param int $post_ID ID of the updated post.
4946 * @param array $args An array of arguments to update the post.
4948 do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
4958 * @param array $args Method parameters.
4961 function mw_getPost($args) {
4963 $this->escape($args);
4965 $post_ID = (int) $args[0];
4966 $username = $args[1];
4967 $password = $args[2];
4969 if ( !$user = $this->login($username, $password) )
4970 return $this->error;
4972 $postdata = get_post($post_ID, ARRAY_A);
4974 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4976 if ( !current_user_can( 'edit_post', $post_ID ) )
4977 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4979 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4980 do_action( 'xmlrpc_call', 'metaWeblog.getPost' );
4982 if ($postdata['post_date'] != '') {
4983 $post_date = $this->_convert_date( $postdata['post_date'] );
4984 $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'], $postdata['post_date'] );
4985 $post_modified = $this->_convert_date( $postdata['post_modified'] );
4986 $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
4988 $categories = array();
4989 $catids = wp_get_post_categories($post_ID);
4990 foreach($catids as $catid)
4991 $categories[] = get_cat_name($catid);
4993 $tagnames = array();
4994 $tags = wp_get_post_tags( $post_ID );
4995 if ( !empty( $tags ) ) {
4996 foreach ( $tags as $tag )
4997 $tagnames[] = $tag->name;
4998 $tagnames = implode( ', ', $tagnames );
5003 $post = get_extended($postdata['post_content']);
5004 $link = post_permalink($postdata['ID']);
5006 // Get the author info.
5007 $author = get_userdata($postdata['post_author']);
5009 $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
5010 $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
5012 // Consider future posts as published
5013 if ( $postdata['post_status'] === 'future' )
5014 $postdata['post_status'] = 'publish';
5017 $post_format = get_post_format( $post_ID );
5018 if ( empty( $post_format ) )
5019 $post_format = 'standard';
5022 if ( is_sticky( $post_ID ) )
5025 $enclosure = array();
5026 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
5027 if ($key == 'enclosure') {
5028 foreach ( (array) $val as $enc ) {
5029 $encdata = explode("\n", $enc);
5030 $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
5031 $enclosure['length'] = (int) trim($encdata[1]);
5032 $enclosure['type'] = trim($encdata[2]);
5039 'dateCreated' => $post_date,
5040 'userid' => $postdata['post_author'],
5041 'postid' => $postdata['ID'],
5042 'description' => $post['main'],
5043 'title' => $postdata['post_title'],
5045 'permaLink' => $link,
5046 // commented out because no other tool seems to use this
5047 // 'content' => $entry['post_content'],
5048 'categories' => $categories,
5049 'mt_excerpt' => $postdata['post_excerpt'],
5050 'mt_text_more' => $post['extended'],
5051 'wp_more_text' => $post['more_text'],
5052 'mt_allow_comments' => $allow_comments,
5053 'mt_allow_pings' => $allow_pings,
5054 'mt_keywords' => $tagnames,
5055 'wp_slug' => $postdata['post_name'],
5056 'wp_password' => $postdata['post_password'],
5057 'wp_author_id' => (string) $author->ID,
5058 'wp_author_display_name' => $author->display_name,
5059 'date_created_gmt' => $post_date_gmt,
5060 'post_status' => $postdata['post_status'],
5061 'custom_fields' => $this->get_custom_fields($post_ID),
5062 'wp_post_format' => $post_format,
5063 'sticky' => $sticky,
5064 'date_modified' => $post_modified,
5065 'date_modified_gmt' => $post_modified_gmt
5068 if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
5070 $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
5074 return new IXR_Error(404, __('Sorry, no such post.'));
5079 * Retrieve list of recent posts.
5083 * @param array $args Method parameters.
5086 function mw_getRecentPosts($args) {
5088 $this->escape($args);
5090 $blog_ID = (int) $args[0];
5091 $username = $args[1];
5092 $password = $args[2];
5093 if ( isset( $args[3] ) )
5094 $query = array( 'numberposts' => absint( $args[3] ) );
5098 if ( !$user = $this->login($username, $password) )
5099 return $this->error;
5101 if ( ! current_user_can( 'edit_posts' ) )
5102 return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
5104 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5105 do_action( 'xmlrpc_call', 'metaWeblog.getRecentPosts' );
5107 $posts_list = wp_get_recent_posts( $query );
5113 foreach ($posts_list as $entry) {
5114 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5117 $post_date = $this->_convert_date( $entry['post_date'] );
5118 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5119 $post_modified = $this->_convert_date( $entry['post_modified'] );
5120 $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
5122 $categories = array();
5123 $catids = wp_get_post_categories($entry['ID']);
5124 foreach( $catids as $catid )
5125 $categories[] = get_cat_name($catid);
5127 $tagnames = array();
5128 $tags = wp_get_post_tags( $entry['ID'] );
5129 if ( !empty( $tags ) ) {
5130 foreach ( $tags as $tag ) {
5131 $tagnames[] = $tag->name;
5133 $tagnames = implode( ', ', $tagnames );
5138 $post = get_extended($entry['post_content']);
5139 $link = post_permalink($entry['ID']);
5141 // Get the post author info.
5142 $author = get_userdata($entry['post_author']);
5144 $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
5145 $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
5147 // Consider future posts as published
5148 if ( $entry['post_status'] === 'future' )
5149 $entry['post_status'] = 'publish';
5152 $post_format = get_post_format( $entry['ID'] );
5153 if ( empty( $post_format ) )
5154 $post_format = 'standard';
5157 'dateCreated' => $post_date,
5158 'userid' => $entry['post_author'],
5159 'postid' => (string) $entry['ID'],
5160 'description' => $post['main'],
5161 'title' => $entry['post_title'],
5163 'permaLink' => $link,
5164 // commented out because no other tool seems to use this
5165 // 'content' => $entry['post_content'],
5166 'categories' => $categories,
5167 'mt_excerpt' => $entry['post_excerpt'],
5168 'mt_text_more' => $post['extended'],
5169 'wp_more_text' => $post['more_text'],
5170 'mt_allow_comments' => $allow_comments,
5171 'mt_allow_pings' => $allow_pings,
5172 'mt_keywords' => $tagnames,
5173 'wp_slug' => $entry['post_name'],
5174 'wp_password' => $entry['post_password'],
5175 'wp_author_id' => (string) $author->ID,
5176 'wp_author_display_name' => $author->display_name,
5177 'date_created_gmt' => $post_date_gmt,
5178 'post_status' => $entry['post_status'],
5179 'custom_fields' => $this->get_custom_fields($entry['ID']),
5180 'wp_post_format' => $post_format,
5181 'date_modified' => $post_modified,
5182 'date_modified_gmt' => $post_modified_gmt,
5183 'sticky' => ( $entry['post_type'] === 'post' && is_sticky( $entry['ID'] ) ),
5186 $entry_index = count( $struct ) - 1;
5187 $struct[ $entry_index ][ 'wp_post_thumbnail' ] = get_post_thumbnail_id( $entry['ID'] );
5190 $recent_posts = array();
5191 for ( $j=0; $j<count($struct); $j++ ) {
5192 array_push($recent_posts, $struct[$j]);
5195 return $recent_posts;
5199 * Retrieve the list of categories on a given blog.
5203 * @param array $args Method parameters.
5206 function mw_getCategories($args) {
5208 $this->escape($args);
5210 $blog_ID = (int) $args[0];
5211 $username = $args[1];
5212 $password = $args[2];
5214 if ( !$user = $this->login($username, $password) )
5215 return $this->error;
5217 if ( !current_user_can( 'edit_posts' ) )
5218 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5220 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5221 do_action( 'xmlrpc_call', 'metaWeblog.getCategories' );
5223 $categories_struct = array();
5225 if ( $cats = get_categories(array('get' => 'all')) ) {
5226 foreach ( $cats as $cat ) {
5227 $struct['categoryId'] = $cat->term_id;
5228 $struct['parentId'] = $cat->parent;
5229 $struct['description'] = $cat->name;
5230 $struct['categoryDescription'] = $cat->description;
5231 $struct['categoryName'] = $cat->name;
5232 $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
5233 $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
5235 $categories_struct[] = $struct;
5239 return $categories_struct;
5243 * Uploads a file, following your settings.
5245 * Adapted from a patch by Johann Richard.
5247 * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
5251 * @param array $args Method parameters.
5254 function mw_newMediaObject($args) {
5257 $blog_ID = (int) $args[0];
5258 $username = $this->escape($args[1]);
5259 $password = $this->escape($args[2]);
5262 $name = sanitize_file_name( $data['name'] );
5263 $type = $data['type'];
5264 $bits = $data['bits'];
5266 if ( !$user = $this->login($username, $password) )
5267 return $this->error;
5269 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5270 do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject' );
5272 if ( !current_user_can('upload_files') ) {
5273 $this->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
5274 return $this->error;
5278 * Filter whether to preempt the XML-RPC media upload.
5280 * Passing a truthy value will effectively short-circuit the media upload,
5281 * returning that value as a 500 error instead.
5285 * @param bool $error Whether to pre-empt the media upload. Default false.
5287 if ( $upload_err = apply_filters( 'pre_upload_error', false ) ) {
5288 return new IXR_Error( 500, $upload_err );
5291 if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) {
5292 // Get postmeta info on the object.
5293 $old_file = $wpdb->get_row("
5296 WHERE post_title = '{$name}'
5297 AND post_type = 'attachment'
5300 // Delete previous file.
5301 wp_delete_attachment($old_file->ID);
5303 // Make sure the new name is different by pre-pending the
5304 // previous post id.
5305 $filename = preg_replace('/^wpid\d+-/', '', $name);
5306 $name = "wpid{$old_file->ID}-{$filename}";
5309 $upload = wp_upload_bits($name, null, $bits);
5310 if ( ! empty($upload['error']) ) {
5311 $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
5312 return new IXR_Error(500, $errorString);
5314 // Construct the attachment array
5316 if ( ! empty( $data['post_id'] ) ) {
5317 $post_id = (int) $data['post_id'];
5319 if ( ! current_user_can( 'edit_post', $post_id ) )
5320 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
5322 $attachment = array(
5323 'post_title' => $name,
5324 'post_content' => '',
5325 'post_type' => 'attachment',
5326 'post_parent' => $post_id,
5327 'post_mime_type' => $type,
5328 'guid' => $upload[ 'url' ]
5332 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
5333 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
5336 * Fires after a new attachment has been added via the XML-RPC MovableType API.
5340 * @param int $id ID of the new attachment.
5341 * @param array $args An array of arguments to add the attachment.
5343 do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
5346 'id' => strval( $id ),
5348 'url' => $upload[ 'url' ],
5352 /** This filter is documented in wp-admin/includes/file.php */
5353 return apply_filters( 'wp_handle_upload', $struct, 'upload' );
5356 /* MovableType API functions
5357 * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
5361 * Retrieve the post titles of recent posts.
5365 * @param array $args Method parameters.
5368 function mt_getRecentPostTitles($args) {
5370 $this->escape($args);
5372 $blog_ID = (int) $args[0];
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;
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'] );
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
5413 $recent_posts = array();
5414 for ( $j=0; $j<count($struct); $j++ ) {
5415 array_push($recent_posts, $struct[$j]);
5418 return $recent_posts;
5422 * Retrieve list of all categories on blog.
5426 * @param array $args Method parameters.
5429 function mt_getCategoryList($args) {
5431 $this->escape($args);
5433 $blog_ID = (int) $args[0];
5434 $username = $args[1];
5435 $password = $args[2];
5437 if ( !$user = $this->login($username, $password) )
5438 return $this->error;
5440 if ( !current_user_can( 'edit_posts' ) )
5441 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5443 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5444 do_action( 'xmlrpc_call', 'mt.getCategoryList' );
5446 $categories_struct = array();
5448 if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
5449 foreach ( $cats as $cat ) {
5450 $struct['categoryId'] = $cat->term_id;
5451 $struct['categoryName'] = $cat->name;
5453 $categories_struct[] = $struct;
5457 return $categories_struct;
5461 * Retrieve post categories.
5465 * @param array $args Method parameters.
5468 function mt_getPostCategories($args) {
5470 $this->escape($args);
5472 $post_ID = (int) $args[0];
5473 $username = $args[1];
5474 $password = $args[2];
5476 if ( !$user = $this->login($username, $password) )
5477 return $this->error;
5479 if ( ! get_post( $post_ID ) )
5480 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5482 if ( !current_user_can( 'edit_post', $post_ID ) )
5483 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
5485 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5486 do_action( 'xmlrpc_call', 'mt.getPostCategories' );
5488 $categories = array();
5489 $catids = wp_get_post_categories(intval($post_ID));
5490 // first listed category will be the primary category
5492 foreach ( $catids as $catid ) {
5493 $categories[] = array(
5494 'categoryName' => get_cat_name($catid),
5495 'categoryId' => (string) $catid,
5496 'isPrimary' => $isPrimary
5505 * Sets categories for a post.
5509 * @param array $args Method parameters.
5510 * @return bool True on success.
5512 function mt_setPostCategories($args) {
5514 $this->escape($args);
5516 $post_ID = (int) $args[0];
5517 $username = $args[1];
5518 $password = $args[2];
5519 $categories = $args[3];
5521 if ( !$user = $this->login($username, $password) )
5522 return $this->error;
5524 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5525 do_action( 'xmlrpc_call', 'mt.setPostCategories' );
5527 if ( ! get_post( $post_ID ) )
5528 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5530 if ( !current_user_can('edit_post', $post_ID) )
5531 return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
5534 foreach ( $categories as $cat ) {
5535 $catids[] = $cat['categoryId'];
5538 wp_set_post_categories($post_ID, $catids);
5544 * Retrieve an array of methods supported by this server.
5548 * @param array $args Method parameters.
5551 function mt_supportedMethods($args) {
5553 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5554 do_action( 'xmlrpc_call', 'mt.supportedMethods' );
5556 $supported_methods = array();
5557 foreach ( $this->methods as $key => $value ) {
5558 $supported_methods[] = $key;
5561 return $supported_methods;
5565 * Retrieve an empty array because we don't support per-post text filters.
5569 * @param array $args Method parameters.
5571 function mt_supportedTextFilters($args) {
5572 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5573 do_action( 'xmlrpc_call', 'mt.supportedTextFilters' );
5576 * Filter the MoveableType text filters list for XML-RPC.
5580 * @param array $filters An array of text filters.
5582 return apply_filters( 'xmlrpc_text_filters', array() );
5586 * Retrieve trackbacks sent to a given post.
5590 * @param array $args Method parameters.
5593 function mt_getTrackbackPings($args) {
5597 $post_ID = intval($args);
5599 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5600 do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
5602 $actual_post = get_post($post_ID, ARRAY_A);
5604 if ( !$actual_post )
5605 return new IXR_Error(404, __('Sorry, no such post.'));
5607 $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) );
5612 $trackback_pings = array();
5613 foreach ( $comments as $comment ) {
5614 if ( 'trackback' == $comment->comment_type ) {
5615 $content = $comment->comment_content;
5616 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
5617 $trackback_pings[] = array(
5618 'pingTitle' => $title,
5619 'pingURL' => $comment->comment_author_url,
5620 'pingIP' => $comment->comment_author_IP
5625 return $trackback_pings;
5629 * Sets a post's publish status to 'publish'.
5633 * @param array $args Method parameters.
5636 function mt_publishPost($args) {
5638 $this->escape($args);
5640 $post_ID = (int) $args[0];
5641 $username = $args[1];
5642 $password = $args[2];
5644 if ( !$user = $this->login($username, $password) )
5645 return $this->error;
5647 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5648 do_action( 'xmlrpc_call', 'mt.publishPost' );
5650 $postdata = get_post($post_ID, ARRAY_A);
5652 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5654 if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
5655 return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
5657 $postdata['post_status'] = 'publish';
5660 $cats = wp_get_post_categories($post_ID);
5661 $postdata['post_category'] = $cats;
5662 $this->escape($postdata);
5664 $result = wp_update_post($postdata);
5669 /* PingBack functions
5670 * specs on www.hixie.ch/specs/pingback/pingback
5674 * Retrieves a pingback and registers it.
5678 * @param array $args Method parameters.
5681 function pingback_ping($args) {
5684 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5685 do_action( 'xmlrpc_call', 'pingback.ping' );
5687 $this->escape($args);
5689 $pagelinkedfrom = $args[0];
5690 $pagelinkedto = $args[1];
5694 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
5695 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
5696 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
5699 * Filter the pingback source URI.
5703 * @param string $pagelinkedfrom URI of the page linked from.
5704 * @param string $pagelinkedto URI of the page linked to.
5706 $pagelinkedfrom = apply_filters( 'pingback_ping_source_uri', $pagelinkedfrom, $pagelinkedto );
5708 if ( ! $pagelinkedfrom )
5709 return $this->pingback_error( 0, __( 'A valid URL was not provided.' ) );
5711 // Check if the page linked to is in our site
5712 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
5714 return $this->pingback_error( 0, __( 'Is there no link to us?' ) );
5716 // let's find which post is linked to
5717 // FIXME: does url_to_postid() cover all these cases already?
5718 // if so, then let's use it and drop the old code.
5719 $urltest = parse_url($pagelinkedto);
5720 if ( $post_ID = url_to_postid($pagelinkedto) ) {
5721 $way = 'url_to_postid()';
5722 } elseif ( preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
5723 // the path defines the post_ID (archives/p/XXXX)
5724 $blah = explode('/', $match[0]);
5725 $post_ID = (int) $blah[1];
5726 $way = 'from the path';
5727 } elseif ( isset( $urltest['query'] ) && preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
5728 // the querystring defines the post_ID (?p=XXXX)
5729 $blah = explode('=', $match[0]);
5730 $post_ID = (int) $blah[1];
5731 $way = 'from the querystring';
5732 } elseif ( isset($urltest['fragment']) ) {
5733 // an #anchor is there, it's either...
5734 if ( intval($urltest['fragment']) ) {
5735 // ...an integer #XXXX (simplest case)
5736 $post_ID = (int) $urltest['fragment'];
5737 $way = 'from the fragment (numeric)';
5738 } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
5739 // ...a post id in the form 'post-###'
5740 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
5741 $way = 'from the fragment (post-###)';
5742 } elseif ( is_string($urltest['fragment']) ) {
5743 // ...or a string #title, a little more complicated
5744 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
5745 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", like_escape( $title ) );
5746 if (! ($post_ID = $wpdb->get_var($sql)) ) {
5747 // returning unknown error '0' is better than die()ing
5748 return $this->pingback_error( 0, '' );
5750 $way = 'from the fragment (title)';
5753 // TODO: Attempt to extract a post ID from the given URL
5754 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.' ) );
5756 $post_ID = (int) $post_ID;
5758 $post = get_post($post_ID);
5760 if ( !$post ) // Post_ID not found
5761 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.' ) );
5763 if ( $post_ID == url_to_postid($pagelinkedfrom) )
5764 return $this->pingback_error( 0, __( 'The source URL and the target URL cannot both point to the same resource.' ) );
5766 // Check if pings are on
5767 if ( !pings_open($post) )
5768 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.' ) );
5770 // Let's check that the remote site didn't already pingback this entry
5771 if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
5772 return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
5774 // very stupid, but gives time to the 'from' server to publish !
5777 $remote_ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
5779 /** This filter is documented in wp-includes/class-http.php */
5780 $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $GLOBALS['wp_version'] . '; ' . get_bloginfo( 'url' ) );
5782 // Let's check the remote site
5783 $http_api_args = array(
5786 'limit_response_size' => 153600, // 150 KB
5787 'user-agent' => "$user_agent; verifying pingback from $remote_ip",
5789 'X-Pingback-Forwarded-For' => $remote_ip,
5792 $request = wp_safe_remote_get( $pagelinkedfrom, $http_api_args );
5793 $linea = wp_remote_retrieve_body( $request );
5796 return $this->pingback_error( 16, __( 'The source URL does not exist.' ) );
5799 * Filter the pingback remote source.
5803 * @param string $linea Response object for the page linked from.
5804 * @param string $pagelinkedto URL of the page linked to.
5806 $linea = apply_filters( 'pre_remote_source', $linea, $pagelinkedto );
5808 // Work around bug in strip_tags():
5809 $linea = str_replace('<!DOC', '<DOC', $linea);
5810 $linea = preg_replace( '/[\r\n\t ]+/', ' ', $linea ); // normalize spaces
5811 $linea = preg_replace( "/<\/*(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
5813 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
5814 $title = $matchtitle[1];
5815 if ( empty( $title ) )
5816 return $this->pingback_error( 32, __('We cannot find a title on that page.' ) );
5818 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
5820 $p = explode( "\n\n", $linea );
5822 $preg_target = preg_quote($pagelinkedto, '|');
5824 foreach ( $p as $para ) {
5825 if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
5826 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
5828 // If the URL isn't in a link context, keep looking
5829 if ( empty($context) )
5832 // We're going to use this fake tag to mark the context in a bit
5833 // the marker is needed in case the link text appears more than once in the paragraph
5834 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
5836 // prevent really long link text
5837 if ( strlen($context[1]) > 100 )
5838 $context[1] = substr($context[1], 0, 100) . '…';
5840 $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
5841 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
5842 $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
5843 $excerpt = trim($excerpt);
5844 $preg_marker = preg_quote($marker, '|');
5845 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
5846 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
5851 if ( empty($context) ) // Link to target not found
5852 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.' ) );
5854 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
5856 $context = '[…] ' . esc_html( $excerpt ) . ' […]';
5857 $pagelinkedfrom = $this->escape( $pagelinkedfrom );
5859 $comment_post_ID = (int) $post_ID;
5860 $comment_author = $title;
5861 $comment_author_email = '';
5862 $this->escape($comment_author);
5863 $comment_author_url = $pagelinkedfrom;
5864 $comment_content = $context;
5865 $this->escape($comment_content);
5866 $comment_type = 'pingback';
5868 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
5870 $comment_ID = wp_new_comment($commentdata);
5873 * Fires after a post pingback has been sent.
5877 * @param int $comment_ID Comment ID.
5879 do_action( 'pingback_post', $comment_ID );
5881 return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
5885 * Retrieve array of URLs that pingbacked the given URL.
5887 * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
5891 * @param array $args Method parameters.
5894 function pingback_extensions_getPingbacks($args) {
5898 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5899 do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' );
5901 $this->escape($args);
5905 $post_ID = url_to_postid($url);
5907 // We aren't sure that the resource is available and/or pingback enabled
5908 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.' ) );
5911 $actual_post = get_post($post_ID, ARRAY_A);
5913 if ( !$actual_post ) {
5914 // No such post = resource not found
5915 return $this->pingback_error( 32, __('The specified target URL does not exist.' ) );
5918 $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) );
5923 $pingbacks = array();
5924 foreach ( $comments as $comment ) {
5925 if ( 'pingback' == $comment->comment_type )
5926 $pingbacks[] = $comment->comment_author_url;
5932 protected function pingback_error( $code, $message ) {
5934 * Filter the XML-RPC pingback error return.
5938 * @param IXR_Error $error An IXR_Error object containing the error code and message.
5940 return apply_filters( 'xmlrpc_pingback_error', new IXR_Error( $code, $message ) );