3 * XML-RPC protocol support for WordPress
9 * WordPress XMLRPC server implementation.
11 * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
12 * pingback. Additional WordPress API for managing comments, pages, posts,
15 * Since WordPress 2.6.0, WordPress XMLRPC server can be disabled in the
16 * administration panels.
19 * @subpackage Publishing
22 class wp_xmlrpc_server extends IXR_Server {
25 * Register all of the XMLRPC methods that XMLRPC server understands.
27 * Sets up server and method property. Passes XMLRPC
28 * methods through the 'xmlrpc_methods' filter to allow plugins to extend
29 * or replace XMLRPC methods.
33 * @return wp_xmlrpc_server
35 function __construct() {
36 $this->methods = array(
38 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
39 'wp.newPost' => 'this:wp_newPost',
40 'wp.editPost' => 'this:wp_editPost',
41 'wp.deletePost' => 'this:wp_deletePost',
42 'wp.getPost' => 'this:wp_getPost',
43 'wp.getPosts' => 'this:wp_getPosts',
44 'wp.newTerm' => 'this:wp_newTerm',
45 'wp.editTerm' => 'this:wp_editTerm',
46 'wp.deleteTerm' => 'this:wp_deleteTerm',
47 'wp.getTerm' => 'this:wp_getTerm',
48 'wp.getTerms' => 'this:wp_getTerms',
49 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
50 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
51 'wp.getUser' => 'this:wp_getUser',
52 'wp.getUsers' => 'this:wp_getUsers',
53 'wp.getProfile' => 'this:wp_getProfile',
54 'wp.editProfile' => 'this:wp_editProfile',
55 'wp.getPage' => 'this:wp_getPage',
56 'wp.getPages' => 'this:wp_getPages',
57 'wp.newPage' => 'this:wp_newPage',
58 'wp.deletePage' => 'this:wp_deletePage',
59 'wp.editPage' => 'this:wp_editPage',
60 'wp.getPageList' => 'this:wp_getPageList',
61 'wp.getAuthors' => 'this:wp_getAuthors',
62 'wp.getCategories' => 'this:mw_getCategories', // Alias
63 'wp.getTags' => 'this:wp_getTags',
64 'wp.newCategory' => 'this:wp_newCategory',
65 'wp.deleteCategory' => 'this:wp_deleteCategory',
66 'wp.suggestCategories' => 'this:wp_suggestCategories',
67 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
68 'wp.getCommentCount' => 'this:wp_getCommentCount',
69 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
70 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
71 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
72 'wp.getOptions' => 'this:wp_getOptions',
73 'wp.setOptions' => 'this:wp_setOptions',
74 'wp.getComment' => 'this:wp_getComment',
75 'wp.getComments' => 'this:wp_getComments',
76 'wp.deleteComment' => 'this:wp_deleteComment',
77 'wp.editComment' => 'this:wp_editComment',
78 'wp.newComment' => 'this:wp_newComment',
79 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
80 'wp.getMediaItem' => 'this:wp_getMediaItem',
81 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
82 'wp.getPostFormats' => 'this:wp_getPostFormats',
83 'wp.getPostType' => 'this:wp_getPostType',
84 'wp.getPostTypes' => 'this:wp_getPostTypes',
85 'wp.getRevisions' => 'this:wp_getRevisions',
86 'wp.restoreRevision' => 'this:wp_restoreRevision',
89 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
90 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
91 'blogger.getPost' => 'this:blogger_getPost',
92 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
93 'blogger.newPost' => 'this:blogger_newPost',
94 'blogger.editPost' => 'this:blogger_editPost',
95 'blogger.deletePost' => 'this:blogger_deletePost',
97 // MetaWeblog API (with MT extensions to structs)
98 'metaWeblog.newPost' => 'this:mw_newPost',
99 'metaWeblog.editPost' => 'this:mw_editPost',
100 'metaWeblog.getPost' => 'this:mw_getPost',
101 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
102 'metaWeblog.getCategories' => 'this:mw_getCategories',
103 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
105 // MetaWeblog API aliases for Blogger API
106 // see http://www.xmlrpc.com/stories/storyReader$2460
107 'metaWeblog.deletePost' => 'this:blogger_deletePost',
108 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
111 'mt.getCategoryList' => 'this:mt_getCategoryList',
112 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
113 'mt.getPostCategories' => 'this:mt_getPostCategories',
114 'mt.setPostCategories' => 'this:mt_setPostCategories',
115 'mt.supportedMethods' => 'this:mt_supportedMethods',
116 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
117 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
118 'mt.publishPost' => 'this:mt_publishPost',
121 'pingback.ping' => 'this:pingback_ping',
122 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
124 'demo.sayHello' => 'this:sayHello',
125 'demo.addTwoNumbers' => 'this:addTwoNumbers'
128 $this->initialise_blog_option_info();
129 $this->methods = apply_filters('xmlrpc_methods', $this->methods);
132 function serve_request() {
133 $this->IXR_Server($this->methods);
137 * Test XMLRPC API by saying, "Hello!" to client.
141 * @param array $args Method Parameters.
144 function sayHello($args) {
149 * Test XMLRPC API by adding two numbers for client.
153 * @param array $args Method Parameters.
156 function addTwoNumbers($args) {
159 return $number1 + $number2;
167 * @param string $username User's username.
168 * @param string $password User's password.
169 * @return mixed WP_User object if authentication passed, false otherwise
171 function login( $username, $password ) {
172 // Respect any old filters against get_option() for 'enable_xmlrpc'.
173 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false ); // Deprecated
174 if ( false === $enabled )
175 $enabled = apply_filters( 'option_enable_xmlrpc', true ); // Deprecated
177 // Proper filter for turning off XML-RPC. It is on by default.
178 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
181 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
185 $user = wp_authenticate($username, $password);
187 if (is_wp_error($user)) {
188 $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
189 $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
193 wp_set_current_user( $user->ID );
198 * Check user's credentials. Deprecated.
202 * @deprecated use wp_xmlrpc_server::login
203 * @see wp_xmlrpc_server::login
205 * @param string $username User's username.
206 * @param string $password User's password.
207 * @return bool Whether authentication passed.
209 function login_pass_ok( $username, $password ) {
210 return (bool) $this->login( $username, $password );
214 * Sanitize string or array of strings for database.
218 * @param string|array $array Sanitize single string or array of strings.
219 * @return string|array Type matches $array and sanitized for the database.
221 function escape(&$array) {
224 if (!is_array($array)) {
225 return($wpdb->escape($array));
227 foreach ( (array) $array as $k => $v ) {
228 if ( is_array($v) ) {
229 $this->escape($array[$k]);
230 } else if ( is_object($v) ) {
233 $array[$k] = $wpdb->escape($v);
240 * Retrieve custom fields for post.
244 * @param int $post_id Post ID.
245 * @return array Custom fields, if exist.
247 function get_custom_fields($post_id) {
248 $post_id = (int) $post_id;
250 $custom_fields = array();
252 foreach ( (array) has_meta($post_id) as $meta ) {
253 // Don't expose protected fields.
254 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
257 $custom_fields[] = array(
258 "id" => $meta['meta_id'],
259 "key" => $meta['meta_key'],
260 "value" => $meta['meta_value']
264 return $custom_fields;
268 * Set custom fields for post.
272 * @param int $post_id Post ID.
273 * @param array $fields Custom fields.
275 function set_custom_fields($post_id, $fields) {
276 $post_id = (int) $post_id;
278 foreach ( (array) $fields as $meta ) {
279 if ( isset($meta['id']) ) {
280 $meta['id'] = (int) $meta['id'];
281 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
282 if ( isset($meta['key']) ) {
283 $meta['key'] = stripslashes( $meta['key'] );
284 if ( $meta['key'] != $pmeta->meta_key )
286 $meta['value'] = stripslashes_deep( $meta['value'] );
287 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
288 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
289 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
290 delete_metadata_by_mid( 'post', $meta['id'] );
292 } elseif ( current_user_can( 'add_post_meta', $post_id, stripslashes( $meta['key'] ) ) ) {
293 add_post_meta( $post_id, $meta['key'], $meta['value'] );
299 * Set up blog options property.
301 * Passes property through 'xmlrpc_blog_options' filter.
305 function initialise_blog_option_info() {
308 $this->blog_options = array(
310 'software_name' => array(
311 'desc' => __( 'Software Name' ),
313 'value' => 'WordPress'
315 'software_version' => array(
316 'desc' => __( 'Software Version' ),
318 'value' => $wp_version
321 'desc' => __( 'Site URL' ),
323 'option' => 'siteurl'
326 'desc' => __( 'Home URL' ),
330 'image_default_link_type' => array(
331 'desc' => __( 'Image default link type' ),
333 'option' => 'image_default_link_type'
335 'image_default_size' => array(
336 'desc' => __( 'Image default size' ),
338 'option' => 'image_default_size'
340 'image_default_align' => array(
341 'desc' => __( 'Image default align' ),
343 'option' => 'image_default_align'
346 'desc' => __( 'Template' ),
348 'option' => 'template'
350 'stylesheet' => array(
351 'desc' => __( 'Stylesheet' ),
353 'option' => 'stylesheet'
355 'post_thumbnail' => array(
356 'desc' => __('Post Thumbnail'),
358 'value' => current_theme_supports( 'post-thumbnails' )
362 'time_zone' => array(
363 'desc' => __( 'Time Zone' ),
365 'option' => 'gmt_offset'
367 'blog_title' => array(
368 'desc' => __( 'Site Title' ),
370 'option' => 'blogname'
372 'blog_tagline' => array(
373 'desc' => __( 'Site Tagline' ),
375 'option' => 'blogdescription'
377 'date_format' => array(
378 'desc' => __( 'Date Format' ),
380 'option' => 'date_format'
382 'time_format' => array(
383 'desc' => __( 'Time Format' ),
385 'option' => 'time_format'
387 'users_can_register' => array(
388 'desc' => __( 'Allow new users to sign up' ),
390 'option' => 'users_can_register'
392 'thumbnail_size_w' => array(
393 'desc' => __( 'Thumbnail Width' ),
395 'option' => 'thumbnail_size_w'
397 'thumbnail_size_h' => array(
398 'desc' => __( 'Thumbnail Height' ),
400 'option' => 'thumbnail_size_h'
402 'thumbnail_crop' => array(
403 'desc' => __( 'Crop thumbnail to exact dimensions' ),
405 'option' => 'thumbnail_crop'
407 'medium_size_w' => array(
408 'desc' => __( 'Medium size image width' ),
410 'option' => 'medium_size_w'
412 'medium_size_h' => array(
413 'desc' => __( 'Medium size image height' ),
415 'option' => 'medium_size_h'
417 'large_size_w' => array(
418 'desc' => __( 'Large size image width' ),
420 'option' => 'large_size_w'
422 'large_size_h' => array(
423 'desc' => __( 'Large size image height' ),
425 'option' => 'large_size_h'
427 'default_comment_status' => array(
428 'desc' => __( 'Allow people to post comments on new articles' ),
430 'option' => 'default_comment_status'
432 'default_ping_status' => array(
433 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
435 'option' => 'default_ping_status'
439 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
443 * Retrieve the blogs of the user.
447 * @param array $args Method parameters. Contains:
450 * @return array. Contains:
455 * - 'xmlrpc' - url of xmlrpc endpoint
457 function wp_getUsersBlogs( $args ) {
458 global $current_site;
459 // If this isn't on WPMU then just use blogger_getUsersBlogs
460 if ( !is_multisite() ) {
461 array_unshift( $args, 1 );
462 return $this->blogger_getUsersBlogs( $args );
465 $this->escape( $args );
467 $username = $args[0];
468 $password = $args[1];
470 if ( !$user = $this->login($username, $password) )
473 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
475 $blogs = (array) get_blogs_of_user( $user->ID );
478 foreach ( $blogs as $blog ) {
479 // Don't include blogs that aren't hosted at this site
480 if ( $blog->site_id != $current_site->id )
483 $blog_id = $blog->userblog_id;
485 switch_to_blog( $blog_id );
487 $is_admin = current_user_can( 'manage_options' );
490 'isAdmin' => $is_admin,
491 'url' => home_url( '/' ),
492 'blogid' => (string) $blog_id,
493 'blogName' => get_option( 'blogname' ),
494 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
497 restore_current_blog();
504 * Checks if the method received at least the minimum number of arguments.
508 * @param string|array $args Sanitize single string or array of strings.
509 * @param int $count Minimum number of arguments.
510 * @return boolean if $args contains at least $count arguments.
512 protected function minimum_args( $args, $count ) {
513 if ( count( $args ) < $count ) {
514 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
522 * Prepares taxonomy data for return in an XML-RPC object.
526 * @param object $taxonomy The unprepared taxonomy data
527 * @param array $fields The subset of taxonomy fields to return
528 * @return array The prepared taxonomy data
530 protected function _prepare_taxonomy( $taxonomy, $fields ) {
532 'name' => $taxonomy->name,
533 'label' => $taxonomy->label,
534 'hierarchical' => (bool) $taxonomy->hierarchical,
535 'public' => (bool) $taxonomy->public,
536 'show_ui' => (bool) $taxonomy->show_ui,
537 '_builtin' => (bool) $taxonomy->_builtin,
540 if ( in_array( 'labels', $fields ) )
541 $_taxonomy['labels'] = (array) $taxonomy->labels;
543 if ( in_array( 'cap', $fields ) )
544 $_taxonomy['cap'] = (array) $taxonomy->cap;
546 if ( in_array( 'object_type', $fields ) )
547 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
549 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
553 * Prepares term data for return in an XML-RPC object.
557 * @param array|object $term The unprepared term data
558 * @return array The prepared term data
560 protected function _prepare_term( $term ) {
562 if ( ! is_array( $_term) )
563 $_term = get_object_vars( $_term );
565 // For Intergers which may be largeer than XMLRPC supports ensure we return strings.
566 $_term['term_id'] = strval( $_term['term_id'] );
567 $_term['term_group'] = strval( $_term['term_group'] );
568 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
569 $_term['parent'] = strval( $_term['parent'] );
571 // Count we are happy to return as an Integer because people really shouldn't use Terms that much.
572 $_term['count'] = intval( $_term['count'] );
574 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
578 * Convert a WordPress date string to an IXR_Date object.
582 * @param string $date
585 protected function _convert_date( $date ) {
586 if ( $date === '0000-00-00 00:00:00' ) {
587 return new IXR_Date( '00000000T00:00:00Z' );
589 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
593 * Convert a WordPress GMT date string to an IXR_Date object.
597 * @param string $date_gmt
598 * @param string $date
601 protected function _convert_date_gmt( $date_gmt, $date ) {
602 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
603 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
605 return $this->_convert_date( $date_gmt );
609 * Prepares post data for return in an XML-RPC object.
613 * @param array $post The unprepared post data
614 * @param array $fields The subset of post type fields to return
615 * @return array The prepared post data
617 protected function _prepare_post( $post, $fields ) {
618 // holds the data for this post. built up based on $fields
619 $_post = array( 'post_id' => strval( $post['ID'] ) );
621 // prepare common post fields
622 $post_fields = array(
623 'post_title' => $post['post_title'],
624 'post_date' => $this->_convert_date( $post['post_date'] ),
625 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
626 'post_modified' => $this->_convert_date( $post['post_modified'] ),
627 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
628 'post_status' => $post['post_status'],
629 'post_type' => $post['post_type'],
630 'post_name' => $post['post_name'],
631 'post_author' => $post['post_author'],
632 'post_password' => $post['post_password'],
633 'post_excerpt' => $post['post_excerpt'],
634 'post_content' => $post['post_content'],
635 'post_parent' => strval( $post['post_parent'] ),
636 'post_mime_type' => $post['post_mime_type'],
637 'link' => post_permalink( $post['ID'] ),
638 'guid' => $post['guid'],
639 'menu_order' => intval( $post['menu_order'] ),
640 'comment_status' => $post['comment_status'],
641 'ping_status' => $post['ping_status'],
642 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
646 $post_fields['post_thumbnail'] = array();
647 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
648 if ( $thumbnail_id ) {
649 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
650 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
653 // Consider future posts as published
654 if ( $post_fields['post_status'] === 'future' )
655 $post_fields['post_status'] = 'publish';
657 // Fill in blank post format
658 $post_fields['post_format'] = get_post_format( $post['ID'] );
659 if ( empty( $post_fields['post_format'] ) )
660 $post_fields['post_format'] = 'standard';
662 // Merge requested $post_fields fields into $_post
663 if ( in_array( 'post', $fields ) ) {
664 $_post = array_merge( $_post, $post_fields );
666 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
667 $_post = array_merge( $_post, $requested_fields );
670 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
672 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
673 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
674 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
675 $_post['terms'] = array();
676 foreach ( $terms as $term ) {
677 $_post['terms'][] = $this->_prepare_term( $term );
681 if ( in_array( 'custom_fields', $fields ) )
682 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
684 if ( in_array( 'enclosure', $fields ) ) {
685 $_post['enclosure'] = array();
686 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
687 if ( ! empty( $enclosures ) ) {
688 $encdata = explode( "\n", $enclosures[0] );
689 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
690 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
691 $_post['enclosure']['type'] = trim( $encdata[2] );
695 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
699 * Prepares post data for return in an XML-RPC object.
703 * @param object $post_type Post type object
704 * @param array $fields The subset of post fields to return
705 * @return array The prepared post type data
707 protected function _prepare_post_type( $post_type, $fields ) {
709 'name' => $post_type->name,
710 'label' => $post_type->label,
711 'hierarchical' => (bool) $post_type->hierarchical,
712 'public' => (bool) $post_type->public,
713 'show_ui' => (bool) $post_type->show_ui,
714 '_builtin' => (bool) $post_type->_builtin,
715 'has_archive' => (bool) $post_type->has_archive,
716 'supports' => get_all_post_type_supports( $post_type->name ),
719 if ( in_array( 'labels', $fields ) ) {
720 $_post_type['labels'] = (array) $post_type->labels;
723 if ( in_array( 'cap', $fields ) ) {
724 $_post_type['cap'] = (array) $post_type->cap;
725 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
728 if ( in_array( 'menu', $fields ) ) {
729 $_post_type['menu_position'] = (int) $post_type->menu_position;
730 $_post_type['menu_icon'] = $post_type->menu_icon;
731 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
734 if ( in_array( 'taxonomies', $fields ) )
735 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
737 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
741 * Prepares media item data for return in an XML-RPC object.
745 * @param object $media_item The unprepared media item data
746 * @param string $thumbnail_size The image size to use for the thumbnail URL
747 * @return array The prepared media item data
749 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
750 $_media_item = array(
751 'attachment_id' => strval( $media_item->ID ),
752 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
753 'parent' => $media_item->post_parent,
754 'link' => wp_get_attachment_url( $media_item->ID ),
755 'title' => $media_item->post_title,
756 'caption' => $media_item->post_excerpt,
757 'description' => $media_item->post_content,
758 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
761 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
762 if ( $thumbnail_src )
763 $_media_item['thumbnail'] = $thumbnail_src[0];
765 $_media_item['thumbnail'] = $_media_item['link'];
767 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
771 * Prepares page data for return in an XML-RPC object.
775 * @param object $page The unprepared page data
776 * @return array The prepared page data
778 protected function _prepare_page( $page ) {
779 // Get all of the page content and link.
780 $full_page = get_extended( $page->post_content );
781 $link = post_permalink( $page->ID );
783 // Get info the page parent if there is one.
785 if ( ! empty( $page->post_parent ) ) {
786 $parent = get_post( $page->post_parent );
787 $parent_title = $parent->post_title;
790 // Determine comment and ping settings.
791 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
792 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
795 $page_date = $this->_convert_date( $page->post_date );
796 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
798 // Pull the categories info together.
799 $categories = array();
800 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
801 $categories[] = get_cat_name( $cat_id );
804 // Get the author info.
805 $author = get_userdata( $page->post_author );
807 $page_template = get_page_template_slug( $page->ID );
808 if ( empty( $page_template ) )
809 $page_template = 'default';
812 'dateCreated' => $page_date,
813 'userid' => $page->post_author,
814 'page_id' => $page->ID,
815 'page_status' => $page->post_status,
816 'description' => $full_page['main'],
817 'title' => $page->post_title,
819 'permaLink' => $link,
820 'categories' => $categories,
821 'excerpt' => $page->post_excerpt,
822 'text_more' => $full_page['extended'],
823 'mt_allow_comments' => $allow_comments,
824 'mt_allow_pings' => $allow_pings,
825 'wp_slug' => $page->post_name,
826 'wp_password' => $page->post_password,
827 'wp_author' => $author->display_name,
828 'wp_page_parent_id' => $page->post_parent,
829 'wp_page_parent_title' => $parent_title,
830 'wp_page_order' => $page->menu_order,
831 'wp_author_id' => (string) $author->ID,
832 'wp_author_display_name' => $author->display_name,
833 'date_created_gmt' => $page_date_gmt,
834 'custom_fields' => $this->get_custom_fields( $page->ID ),
835 'wp_page_template' => $page_template
838 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
842 * Prepares comment data for return in an XML-RPC object.
846 * @param object $comment The unprepared comment data
847 * @return array The prepared comment data
849 protected function _prepare_comment( $comment ) {
851 $comment_date = $this->_convert_date( $comment->comment_date );
852 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
854 if ( '0' == $comment->comment_approved )
855 $comment_status = 'hold';
856 else if ( 'spam' == $comment->comment_approved )
857 $comment_status = 'spam';
858 else if ( '1' == $comment->comment_approved )
859 $comment_status = 'approve';
861 $comment_status = $comment->comment_approved;
864 'date_created_gmt' => $comment_date_gmt,
865 'user_id' => $comment->user_id,
866 'comment_id' => $comment->comment_ID,
867 'parent' => $comment->comment_parent,
868 'status' => $comment_status,
869 'content' => $comment->comment_content,
870 'link' => get_comment_link($comment),
871 'post_id' => $comment->comment_post_ID,
872 'post_title' => get_the_title($comment->comment_post_ID),
873 'author' => $comment->comment_author,
874 'author_url' => $comment->comment_author_url,
875 'author_email' => $comment->comment_author_email,
876 'author_ip' => $comment->comment_author_IP,
877 'type' => $comment->comment_type,
880 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
884 * Prepares user data for return in an XML-RPC object.
888 * @param WP_User $user The unprepared user object
889 * @param array $fields The subset of user fields to return
890 * @return array The prepared user data
892 protected function _prepare_user( $user, $fields ) {
893 $_user = array( 'user_id' => strval( $user->ID ) );
895 $user_fields = array(
896 'username' => $user->user_login,
897 'first_name' => $user->user_firstname,
898 'last_name' => $user->user_lastname,
899 'registered' => $this->_convert_date( $user->user_registered ),
900 'bio' => $user->user_description,
901 'email' => $user->user_email,
902 'nickname' => $user->nickname,
903 'nicename' => $user->user_nicename,
904 'url' => $user->user_url,
905 'display_name' => $user->display_name,
906 'roles' => $user->roles,
909 if ( in_array( 'all', $fields ) ) {
910 $_user = array_merge( $_user, $user_fields );
912 if ( in_array( 'basic', $fields ) ) {
913 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
914 $fields = array_merge( $fields, $basic_fields );
916 $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
917 $_user = array_merge( $_user, $requested_fields );
920 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
924 * Create a new post for any registered post type.
928 * @param array $args Method parameters. Contains:
932 * - array $content_struct
933 * $content_struct can contain:
934 * - post_type (default: 'post')
935 * - post_status (default: 'draft')
940 * - post_date_gmt | post_date
943 * - comment_status - can be 'open' | 'closed'
944 * - ping_status - can be 'open' | 'closed'
946 * - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
947 * - custom_fields - array, with each element containing 'key' and 'value'
948 * - terms - array, with taxonomy names as keys and arrays of term IDs as values
949 * - terms_names - array, with taxonomy names as keys and arrays of term names as values
951 * - any other fields supported by wp_insert_post()
952 * @return string post_id
954 function wp_newPost( $args ) {
955 if ( ! $this->minimum_args( $args, 4 ) )
958 $this->escape( $args );
960 $blog_id = (int) $args[0];
961 $username = $args[1];
962 $password = $args[2];
963 $content_struct = $args[3];
965 if ( ! $user = $this->login( $username, $password ) )
968 do_action( 'xmlrpc_call', 'wp.newPost' );
970 unset( $content_struct['ID'] );
972 return $this->_insert_post( $user, $content_struct );
976 * Helper method for filtering out elements from an array.
980 * @param int $count Number to compare to one.
982 private function _is_greater_than_one( $count ) {
987 * Helper method for wp_newPost and wp_editPost, containing shared logic.
990 * @uses wp_insert_post()
992 * @param WP_User $user The post author if post_author isn't set in $content_struct.
993 * @param array $content_struct Post data to insert.
995 protected function _insert_post( $user, $content_struct ) {
996 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
997 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
999 $post_data = wp_parse_args( $content_struct, $defaults );
1001 $post_type = get_post_type_object( $post_data['post_type'] );
1003 return new IXR_Error( 403, __( 'Invalid post type' ) );
1005 $update = ! empty( $post_data['ID'] );
1008 if ( ! get_post( $post_data['ID'] ) )
1009 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1010 if ( ! current_user_can( $post_type->cap->edit_post, $post_data['ID'] ) )
1011 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1012 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1013 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1015 if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1016 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1019 switch ( $post_data['post_status'] ) {
1024 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1025 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1029 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1030 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1033 if ( ! get_post_status_object( $post_data['post_status'] ) )
1034 $post_data['post_status'] = 'draft';
1038 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1039 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1041 $post_data['post_author'] = absint( $post_data['post_author'] );
1042 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1043 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1044 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1046 $author = get_userdata( $post_data['post_author'] );
1049 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1051 $post_data['post_author'] = $user->ID;
1054 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1055 unset( $post_data['comment_status'] );
1057 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1058 unset( $post_data['ping_status'] );
1060 // Do some timestamp voodoo
1061 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1062 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1063 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1064 } elseif ( ! empty( $post_data['post_date'] ) ) {
1065 $dateCreated = $post_data['post_date']->getIso();
1068 if ( ! empty( $dateCreated ) ) {
1069 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1070 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1073 if ( ! isset( $post_data['ID'] ) )
1074 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1075 $post_ID = $post_data['ID'];
1077 if ( $post_data['post_type'] == 'post' ) {
1078 // Private and password-protected posts cannot be stickied.
1079 if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
1080 // Error if the client tried to stick the post, otherwise, silently unstick.
1081 if ( ! empty( $post_data['sticky'] ) )
1082 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1084 unstick_post( $post_ID );
1085 } elseif ( isset( $post_data['sticky'] ) ) {
1086 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1087 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1088 if ( $post_data['sticky'] )
1089 stick_post( $post_ID );
1091 unstick_post( $post_ID );
1095 if ( isset( $post_data['post_thumbnail'] ) ) {
1096 // empty value deletes, non-empty value adds/updates
1097 if ( ! $post_data['post_thumbnail'] )
1098 delete_post_thumbnail( $post_ID );
1099 elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1100 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1101 set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1102 unset( $content_struct['post_thumbnail'] );
1105 if ( isset( $post_data['custom_fields'] ) )
1106 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1108 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1109 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1111 // accumulate term IDs from terms and terms_names
1114 // first validate the terms specified by ID
1115 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1116 $taxonomies = array_keys( $post_data['terms'] );
1118 // validating term ids
1119 foreach ( $taxonomies as $taxonomy ) {
1120 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1121 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1123 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1124 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1126 $term_ids = $post_data['terms'][$taxonomy];
1127 foreach ( $term_ids as $term_id ) {
1128 $term = get_term_by( 'id', $term_id, $taxonomy );
1131 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1133 $terms[$taxonomy][] = (int) $term_id;
1138 // now validate terms specified by name
1139 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1140 $taxonomies = array_keys( $post_data['terms_names'] );
1142 foreach ( $taxonomies as $taxonomy ) {
1143 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1144 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1146 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1147 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1149 // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
1150 $ambiguous_terms = array();
1151 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1152 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1154 // count the number of terms with the same name
1155 $tax_term_names_count = array_count_values( $tax_term_names );
1157 // filter out non-ambiguous term names
1158 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1160 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1163 $term_names = $post_data['terms_names'][$taxonomy];
1164 foreach ( $term_names as $term_name ) {
1165 if ( in_array( $term_name, $ambiguous_terms ) )
1166 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1168 $term = get_term_by( 'name', $term_name, $taxonomy );
1171 // term doesn't exist, so check that the user is allowed to create new terms
1172 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1173 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1175 // create the new term
1176 $term_info = wp_insert_term( $term_name, $taxonomy );
1177 if ( is_wp_error( $term_info ) )
1178 return new IXR_Error( 500, $term_info->get_error_message() );
1180 $terms[$taxonomy][] = (int) $term_info['term_id'];
1182 $terms[$taxonomy][] = (int) $term->term_id;
1188 $post_data['tax_input'] = $terms;
1189 unset( $post_data['terms'], $post_data['terms_names'] );
1191 // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
1192 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1195 if ( isset( $post_data['post_format'] ) ) {
1196 $format = set_post_format( $post_ID, $post_data['post_format'] );
1198 if ( is_wp_error( $format ) )
1199 return new IXR_Error( 500, $format->get_error_message() );
1201 unset( $post_data['post_format'] );
1204 // Handle enclosures
1205 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1206 $this->add_enclosure_if_new( $post_ID, $enclosure );
1208 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1210 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1212 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1213 if ( is_wp_error( $post_ID ) )
1214 return new IXR_Error( 500, $post_ID->get_error_message() );
1217 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1219 return strval( $post_ID );
1223 * Edit a post for any registered post type.
1225 * The $content_struct parameter only needs to contain fields that
1226 * should be changed. All other fields will retain their existing values.
1230 * @param array $args Method parameters. Contains:
1232 * - string $username
1233 * - string $password
1235 * - array $content_struct
1236 * @return true on success
1238 function wp_editPost( $args ) {
1239 if ( ! $this->minimum_args( $args, 5 ) )
1240 return $this->error;
1242 $this->escape( $args );
1244 $blog_id = (int) $args[0];
1245 $username = $args[1];
1246 $password = $args[2];
1247 $post_id = (int) $args[3];
1248 $content_struct = $args[4];
1250 if ( ! $user = $this->login( $username, $password ) )
1251 return $this->error;
1253 do_action( 'xmlrpc_call', 'wp.editPost' );
1255 $post = get_post( $post_id, ARRAY_A );
1257 if ( empty( $post['ID'] ) )
1258 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1260 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1261 // If the post has been modified since the date provided, return an error.
1262 if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1263 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1267 // convert the date field back to IXR form
1268 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1270 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1271 // since _insert_post will ignore the non-GMT date if the GMT date is set
1272 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1273 unset( $post['post_date_gmt'] );
1275 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1277 $this->escape( $post );
1278 $merged_content_struct = array_merge( $post, $content_struct );
1280 $retval = $this->_insert_post( $user, $merged_content_struct );
1281 if ( $retval instanceof IXR_Error )
1288 * Delete a post for any registered post type.
1292 * @uses wp_delete_post()
1293 * @param array $args Method parameters. Contains:
1295 * - string $username
1296 * - string $password
1298 * @return true on success
1300 function wp_deletePost( $args ) {
1301 if ( ! $this->minimum_args( $args, 4 ) )
1302 return $this->error;
1304 $this->escape( $args );
1306 $blog_id = (int) $args[0];
1307 $username = $args[1];
1308 $password = $args[2];
1309 $post_id = (int) $args[3];
1311 if ( ! $user = $this->login( $username, $password ) )
1312 return $this->error;
1314 do_action( 'xmlrpc_call', 'wp.deletePost' );
1316 $post = get_post( $post_id, ARRAY_A );
1317 if ( empty( $post['ID'] ) )
1318 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1320 $post_type = get_post_type_object( $post['post_type'] );
1321 if ( ! current_user_can( $post_type->cap->delete_post, $post_id ) )
1322 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1324 $result = wp_delete_post( $post_id );
1327 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1337 * The optional $fields parameter specifies what fields will be included
1338 * in the response array. This should be a list of field names. 'post_id' will
1339 * always be included in the response regardless of the value of $fields.
1341 * Instead of, or in addition to, individual field names, conceptual group
1342 * names can be used to specify multiple fields. The available conceptual
1343 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1347 * @param array $args Method parameters. Contains:
1349 * - string $username
1350 * - string $password
1351 * - array $fields optional
1352 * @return array contains (based on $fields parameter):
1358 * - 'post_modified_gmt'
1367 * - 'comment_status'
1376 function wp_getPost( $args ) {
1377 if ( ! $this->minimum_args( $args, 4 ) )
1378 return $this->error;
1380 $this->escape( $args );
1382 $blog_id = (int) $args[0];
1383 $username = $args[1];
1384 $password = $args[2];
1385 $post_id = (int) $args[3];
1387 if ( isset( $args[4] ) )
1390 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1392 if ( ! $user = $this->login( $username, $password ) )
1393 return $this->error;
1395 do_action( 'xmlrpc_call', 'wp.getPost' );
1397 $post = get_post( $post_id, ARRAY_A );
1399 if ( empty( $post['ID'] ) )
1400 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1402 $post_type = get_post_type_object( $post['post_type'] );
1403 if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) )
1404 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1406 return $this->_prepare_post( $post, $fields );
1414 * The optional $filter parameter modifies the query used to retrieve posts.
1415 * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
1416 * 'orderby', and 'order'.
1418 * The optional $fields parameter specifies what fields will be included
1419 * in the response array.
1421 * @uses wp_get_recent_posts()
1422 * @see wp_getPost() for more on $fields
1423 * @see get_posts() for more on $filter values
1425 * @param array $args Method parameters. Contains:
1427 * - string $username
1428 * - string $password
1429 * - array $filter optional
1430 * - array $fields optional
1431 * @return array contains a collection of posts.
1433 function wp_getPosts( $args ) {
1434 if ( ! $this->minimum_args( $args, 3 ) )
1435 return $this->error;
1437 $this->escape( $args );
1439 $blog_id = (int) $args[0];
1440 $username = $args[1];
1441 $password = $args[2];
1442 $filter = isset( $args[3] ) ? $args[3] : array();
1444 if ( isset( $args[4] ) )
1447 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1449 if ( ! $user = $this->login( $username, $password ) )
1450 return $this->error;
1452 do_action( 'xmlrpc_call', 'wp.getPosts' );
1456 if ( isset( $filter['post_type'] ) ) {
1457 $post_type = get_post_type_object( $filter['post_type'] );
1458 if ( ! ( (bool) $post_type ) )
1459 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1461 $post_type = get_post_type_object( 'post' );
1464 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1465 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1467 $query['post_type'] = $post_type->name;
1469 if ( isset( $filter['post_status'] ) )
1470 $query['post_status'] = $filter['post_status'];
1472 if ( isset( $filter['number'] ) )
1473 $query['numberposts'] = absint( $filter['number'] );
1475 if ( isset( $filter['offset'] ) )
1476 $query['offset'] = absint( $filter['offset'] );
1478 if ( isset( $filter['orderby'] ) ) {
1479 $query['orderby'] = $filter['orderby'];
1481 if ( isset( $filter['order'] ) )
1482 $query['order'] = $filter['order'];
1485 if ( isset( $filter['s'] ) ) {
1486 $query['s'] = $filter['s'];
1489 $posts_list = wp_get_recent_posts( $query );
1491 if ( ! $posts_list )
1494 // holds all the posts data
1497 foreach ( $posts_list as $post ) {
1498 $post_type = get_post_type_object( $post['post_type'] );
1499 if ( ! current_user_can( $post_type->cap->edit_post, $post['ID'] ) )
1502 $struct[] = $this->_prepare_post( $post, $fields );
1509 * Create a new term.
1513 * @uses wp_insert_term()
1514 * @param array $args Method parameters. Contains:
1516 * - string $username
1517 * - string $password
1518 * - array $content_struct
1519 * The $content_struct must contain:
1522 * Also, it can optionally contain:
1526 * @return string term_id
1528 function wp_newTerm( $args ) {
1529 if ( ! $this->minimum_args( $args, 4 ) )
1530 return $this->error;
1532 $this->escape( $args );
1534 $blog_id = (int) $args[0];
1535 $username = $args[1];
1536 $password = $args[2];
1537 $content_struct = $args[3];
1539 if ( ! $user = $this->login( $username, $password ) )
1540 return $this->error;
1542 do_action( 'xmlrpc_call', 'wp.newTerm' );
1544 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1545 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1547 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1549 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1550 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1552 $taxonomy = (array) $taxonomy;
1554 // hold the data of the term
1555 $term_data = array();
1557 $term_data['name'] = trim( $content_struct['name'] );
1558 if ( empty( $term_data['name'] ) )
1559 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1561 if ( isset( $content_struct['parent'] ) ) {
1562 if ( ! $taxonomy['hierarchical'] )
1563 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1565 $parent_term_id = (int) $content_struct['parent'];
1566 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1568 if ( is_wp_error( $parent_term ) )
1569 return new IXR_Error( 500, $parent_term->get_error_message() );
1571 if ( ! $parent_term )
1572 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1574 $term_data['parent'] = $content_struct['parent'];
1577 if ( isset( $content_struct['description'] ) )
1578 $term_data['description'] = $content_struct['description'];
1580 if ( isset( $content_struct['slug'] ) )
1581 $term_data['slug'] = $content_struct['slug'];
1583 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1585 if ( is_wp_error( $term ) )
1586 return new IXR_Error( 500, $term->get_error_message() );
1589 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1591 return strval( $term['term_id'] );
1599 * @uses wp_update_term()
1600 * @param array $args Method parameters. Contains:
1602 * - string $username
1603 * - string $password
1605 * - array $content_struct
1606 * The $content_struct must contain:
1608 * Also, it can optionally contain:
1613 * @return bool True, on success.
1615 function wp_editTerm( $args ) {
1616 if ( ! $this->minimum_args( $args, 5 ) )
1617 return $this->error;
1619 $this->escape( $args );
1621 $blog_id = (int) $args[0];
1622 $username = $args[1];
1623 $password = $args[2];
1624 $term_id = (int) $args[3];
1625 $content_struct = $args[4];
1627 if ( ! $user = $this->login( $username, $password ) )
1628 return $this->error;
1630 do_action( 'xmlrpc_call', 'wp.editTerm' );
1632 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1633 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1635 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1637 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1638 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1640 $taxonomy = (array) $taxonomy;
1642 // hold the data of the term
1643 $term_data = array();
1645 $term = get_term( $term_id , $content_struct['taxonomy'] );
1647 if ( is_wp_error( $term ) )
1648 return new IXR_Error( 500, $term->get_error_message() );
1651 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1653 if ( isset( $content_struct['name'] ) ) {
1654 $term_data['name'] = trim( $content_struct['name'] );
1656 if ( empty( $term_data['name'] ) )
1657 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1660 if ( isset( $content_struct['parent'] ) ) {
1661 if ( ! $taxonomy['hierarchical'] )
1662 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1664 $parent_term_id = (int) $content_struct['parent'];
1665 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1667 if ( is_wp_error( $parent_term ) )
1668 return new IXR_Error( 500, $parent_term->get_error_message() );
1670 if ( ! $parent_term )
1671 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1673 $term_data['parent'] = $content_struct['parent'];
1676 if ( isset( $content_struct['description'] ) )
1677 $term_data['description'] = $content_struct['description'];
1679 if ( isset( $content_struct['slug'] ) )
1680 $term_data['slug'] = $content_struct['slug'];
1682 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1684 if ( is_wp_error( $term ) )
1685 return new IXR_Error( 500, $term->get_error_message() );
1688 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1698 * @uses wp_delete_term()
1699 * @param array $args Method parameters. Contains:
1701 * - string $username
1702 * - string $password
1703 * - string $taxnomy_name
1705 * @return boolean|IXR_Error If it suceeded true else a reason why not
1707 function wp_deleteTerm( $args ) {
1708 if ( ! $this->minimum_args( $args, 5 ) )
1709 return $this->error;
1711 $this->escape( $args );
1713 $blog_id = (int) $args[0];
1714 $username = $args[1];
1715 $password = $args[2];
1716 $taxonomy = $args[3];
1717 $term_id = (int) $args[4];
1719 if ( ! $user = $this->login( $username, $password ) )
1720 return $this->error;
1722 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1724 if ( ! taxonomy_exists( $taxonomy ) )
1725 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1727 $taxonomy = get_taxonomy( $taxonomy );
1729 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
1730 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
1732 $term = get_term( $term_id, $taxonomy->name );
1734 if ( is_wp_error( $term ) )
1735 return new IXR_Error( 500, $term->get_error_message() );
1738 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1740 $result = wp_delete_term( $term_id, $taxonomy->name );
1742 if ( is_wp_error( $result ) )
1743 return new IXR_Error( 500, $term->get_error_message() );
1746 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
1757 * @param array $args Method parameters. Contains:
1759 * - string $username
1760 * - string $password
1761 * - string $taxonomy
1763 * @return array contains:
1768 * - 'term_taxonomy_id'
1774 function wp_getTerm( $args ) {
1775 if ( ! $this->minimum_args( $args, 5 ) )
1776 return $this->error;
1778 $this->escape( $args );
1780 $blog_id = (int) $args[0];
1781 $username = $args[1];
1782 $password = $args[2];
1783 $taxonomy = $args[3];
1784 $term_id = (int) $args[4];
1786 if ( ! $user = $this->login( $username, $password ) )
1787 return $this->error;
1789 do_action( 'xmlrpc_call', 'wp.getTerm' );
1791 if ( ! taxonomy_exists( $taxonomy ) )
1792 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1794 $taxonomy = get_taxonomy( $taxonomy );
1796 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1797 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1799 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
1801 if ( is_wp_error( $term ) )
1802 return new IXR_Error( 500, $term->get_error_message() );
1805 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1807 return $this->_prepare_term( $term );
1811 * Retrieve all terms for a taxonomy.
1815 * The optional $filter parameter modifies the query used to retrieve terms.
1816 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
1819 * @param array $args Method parameters. Contains:
1821 * - string $username
1822 * - string $password
1823 * - string $taxonomy
1824 * - array $filter optional
1825 * @return array terms
1827 function wp_getTerms( $args ) {
1828 if ( ! $this->minimum_args( $args, 4 ) )
1829 return $this->error;
1831 $this->escape( $args );
1833 $blog_id = (int) $args[0];
1834 $username = $args[1];
1835 $password = $args[2];
1836 $taxonomy = $args[3];
1837 $filter = isset( $args[4] ) ? $args[4] : array();
1839 if ( ! $user = $this->login( $username, $password ) )
1840 return $this->error;
1842 do_action( 'xmlrpc_call', 'wp.getTerms' );
1844 if ( ! taxonomy_exists( $taxonomy ) )
1845 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1847 $taxonomy = get_taxonomy( $taxonomy );
1849 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1850 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1854 if ( isset( $filter['number'] ) )
1855 $query['number'] = absint( $filter['number'] );
1857 if ( isset( $filter['offset'] ) )
1858 $query['offset'] = absint( $filter['offset'] );
1860 if ( isset( $filter['orderby'] ) ) {
1861 $query['orderby'] = $filter['orderby'];
1863 if ( isset( $filter['order'] ) )
1864 $query['order'] = $filter['order'];
1867 if ( isset( $filter['hide_empty'] ) )
1868 $query['hide_empty'] = $filter['hide_empty'];
1870 $query['get'] = 'all';
1872 if ( isset( $filter['search'] ) )
1873 $query['search'] = $filter['search'];
1875 $terms = get_terms( $taxonomy->name, $query );
1877 if ( is_wp_error( $terms ) )
1878 return new IXR_Error( 500, $terms->get_error_message() );
1882 foreach ( $terms as $term ) {
1883 $struct[] = $this->_prepare_term( $term );
1890 * Retrieve a taxonomy.
1894 * @uses get_taxonomy()
1895 * @param array $args Method parameters. Contains:
1897 * - string $username
1898 * - string $password
1899 * - string $taxonomy
1900 * @return array (@see get_taxonomy())
1902 function wp_getTaxonomy( $args ) {
1903 if ( ! $this->minimum_args( $args, 4 ) )
1904 return $this->error;
1906 $this->escape( $args );
1908 $blog_id = (int) $args[0];
1909 $username = $args[1];
1910 $password = $args[2];
1911 $taxonomy = $args[3];
1913 if ( isset( $args[4] ) )
1916 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
1918 if ( ! $user = $this->login( $username, $password ) )
1919 return $this->error;
1921 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
1923 if ( ! taxonomy_exists( $taxonomy ) )
1924 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1926 $taxonomy = get_taxonomy( $taxonomy );
1928 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1929 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1931 return $this->_prepare_taxonomy( $taxonomy, $fields );
1935 * Retrieve all taxonomies.
1939 * @uses get_taxonomies()
1940 * @param array $args Method parameters. Contains:
1942 * - string $username
1943 * - string $password
1944 * @return array taxonomies
1946 function wp_getTaxonomies( $args ) {
1947 if ( ! $this->minimum_args( $args, 3 ) )
1948 return $this->error;
1950 $this->escape( $args );
1952 $blog_id = (int) $args[0];
1953 $username = $args[1];
1954 $password = $args[2];
1955 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
1957 if ( isset( $args[4] ) )
1960 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
1962 if ( ! $user = $this->login( $username, $password ) )
1963 return $this->error;
1965 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
1967 $taxonomies = get_taxonomies( $filter, 'objects' );
1969 // holds all the taxonomy data
1972 foreach ( $taxonomies as $taxonomy ) {
1973 // capability check for post_types
1974 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1977 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
1986 * The optional $fields parameter specifies what fields will be included
1987 * in the response array. This should be a list of field names. 'user_id' will
1988 * always be included in the response regardless of the value of $fields.
1990 * Instead of, or in addition to, individual field names, conceptual group
1991 * names can be used to specify multiple fields. The available conceptual
1992 * groups are 'basic' and 'all'.
1994 * @uses get_userdata()
1995 * @param array $args Method parameters. Contains:
1997 * - string $username
1998 * - string $password
2000 * - array $fields optional
2001 * @return array contains (based on $fields parameter):
2015 function wp_getUser( $args ) {
2016 if ( ! $this->minimum_args( $args, 4 ) )
2017 return $this->error;
2019 $this->escape( $args );
2021 $blog_id = (int) $args[0];
2022 $username = $args[1];
2023 $password = $args[2];
2024 $user_id = (int) $args[3];
2026 if ( isset( $args[4] ) )
2029 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2031 if ( ! $user = $this->login( $username, $password ) )
2032 return $this->error;
2034 do_action( 'xmlrpc_call', 'wp.getUser' );
2036 if ( ! current_user_can( 'edit_user', $user_id ) )
2037 return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2039 $user_data = get_userdata( $user_id );
2042 return new IXR_Error( 404, __( 'Invalid user ID' ) );
2044 return $this->_prepare_user( $user_data, $fields );
2050 * The optional $filter parameter modifies the query used to retrieve users.
2051 * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2052 * 'who', 'orderby', and 'order'.
2054 * The optional $fields parameter specifies what fields will be included
2055 * in the response array.
2058 * @see wp_getUser() for more on $fields and return values
2060 * @param array $args Method parameters. Contains:
2062 * - string $username
2063 * - string $password
2064 * - array $filter optional
2065 * - array $fields optional
2066 * @return array users data
2068 function wp_getUsers( $args ) {
2069 if ( ! $this->minimum_args( $args, 3 ) )
2070 return $this->error;
2072 $this->escape( $args );
2074 $blog_id = (int) $args[0];
2075 $username = $args[1];
2076 $password = $args[2];
2077 $filter = isset( $args[3] ) ? $args[3] : array();
2079 if ( isset( $args[4] ) )
2082 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2084 if ( ! $user = $this->login( $username, $password ) )
2085 return $this->error;
2087 do_action( 'xmlrpc_call', 'wp.getUsers' );
2089 if ( ! current_user_can( 'list_users' ) )
2090 return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
2092 $query = array( 'fields' => 'all_with_meta' );
2094 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2095 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2097 if ( isset( $filter['orderby'] ) ) {
2098 $query['orderby'] = $filter['orderby'];
2100 if ( isset( $filter['order'] ) )
2101 $query['order'] = $filter['order'];
2104 if ( isset( $filter['role'] ) ) {
2105 if ( get_role( $filter['role'] ) === null )
2106 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2108 $query['role'] = $filter['role'];
2111 if ( isset( $filter['who'] ) ) {
2112 $query['who'] = $filter['who'];
2115 $users = get_users( $query );
2118 foreach ( $users as $user_data ) {
2119 if ( current_user_can( 'edit_user', $user_data->ID ) )
2120 $_users[] = $this->_prepare_user( $user_data, $fields );
2126 * Retrieve information about the requesting user.
2128 * @uses get_userdata()
2129 * @param array $args Method parameters. Contains:
2131 * - string $username
2132 * - string $password
2133 * - array $fields optional
2134 * @return array (@see wp_getUser)
2136 function wp_getProfile( $args ) {
2137 if ( ! $this->minimum_args( $args, 3 ) )
2138 return $this->error;
2140 $this->escape( $args );
2142 $blog_id = (int) $args[0];
2143 $username = $args[1];
2144 $password = $args[2];
2146 if ( isset( $args[3] ) )
2149 $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2151 if ( ! $user = $this->login( $username, $password ) )
2152 return $this->error;
2154 do_action( 'xmlrpc_call', 'wp.getProfile' );
2156 if ( ! current_user_can( 'edit_user', $user->ID ) )
2157 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2159 $user_data = get_userdata( $user->ID );
2161 return $this->_prepare_user( $user_data, $fields );
2165 * Edit user's profile.
2167 * @uses wp_update_user()
2168 * @param array $args Method parameters. Contains:
2170 * - string $username
2171 * - string $password
2172 * - array $content_struct
2173 * It can optionally contain:
2181 * @return bool True, on success.
2183 function wp_editProfile( $args ) {
2184 if ( ! $this->minimum_args( $args, 4 ) )
2185 return $this->error;
2187 $this->escape( $args );
2189 $blog_id = (int) $args[0];
2190 $username = $args[1];
2191 $password = $args[2];
2192 $content_struct = $args[3];
2194 if ( ! $user = $this->login( $username, $password ) )
2195 return $this->error;
2197 do_action( 'xmlrpc_call', 'wp.editProfile' );
2199 if ( ! current_user_can( 'edit_user', $user->ID ) )
2200 return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2202 // holds data of the user
2203 $user_data = array();
2204 $user_data['ID'] = $user->ID;
2206 // only set the user details if it was given
2207 if ( isset( $content_struct['first_name'] ) )
2208 $user_data['first_name'] = $content_struct['first_name'];
2210 if ( isset( $content_struct['last_name'] ) )
2211 $user_data['last_name'] = $content_struct['last_name'];
2213 if ( isset( $content_struct['url'] ) )
2214 $user_data['user_url'] = $content_struct['url'];
2216 if ( isset( $content_struct['display_name'] ) )
2217 $user_data['display_name'] = $content_struct['display_name'];
2219 if ( isset( $content_struct['nickname'] ) )
2220 $user_data['nickname'] = $content_struct['nickname'];
2222 if ( isset( $content_struct['nicename'] ) )
2223 $user_data['user_nicename'] = $content_struct['nicename'];
2225 if ( isset( $content_struct['bio'] ) )
2226 $user_data['description'] = $content_struct['bio'];
2228 $result = wp_update_user( $user_data );
2230 if ( is_wp_error( $result ) )
2231 return new IXR_Error( 500, $result->get_error_message() );
2234 return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2244 * @param array $args Method parameters. Contains:
2251 function wp_getPage($args) {
2252 $this->escape($args);
2254 $blog_id = (int) $args[0];
2255 $page_id = (int) $args[1];
2256 $username = $args[2];
2257 $password = $args[3];
2259 if ( !$user = $this->login($username, $password) ) {
2260 return $this->error;
2263 $page = get_post($page_id);
2265 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2267 if ( !current_user_can( 'edit_page', $page_id ) )
2268 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2270 do_action('xmlrpc_call', 'wp.getPage');
2272 // If we found the page then format the data.
2273 if ( $page->ID && ($page->post_type == 'page') ) {
2274 return $this->_prepare_page( $page );
2276 // If the page doesn't exist indicate that.
2278 return(new IXR_Error(404, __('Sorry, no such page.')));
2287 * @param array $args Method parameters. Contains:
2294 function wp_getPages($args) {
2295 $this->escape($args);
2297 $blog_id = (int) $args[0];
2298 $username = $args[1];
2299 $password = $args[2];
2300 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
2302 if ( !$user = $this->login($username, $password) )
2303 return $this->error;
2305 if ( !current_user_can( 'edit_pages' ) )
2306 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2308 do_action('xmlrpc_call', 'wp.getPages');
2310 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2311 $num_pages = count($pages);
2313 // If we have pages, put together their info.
2314 if ( $num_pages >= 1 ) {
2315 $pages_struct = array();
2317 foreach ($pages as $page) {
2318 if ( current_user_can( 'edit_page', $page->ID ) )
2319 $pages_struct[] = $this->_prepare_page( $page );
2322 return($pages_struct);
2324 // If no pages were found return an error.
2335 * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
2338 function wp_newPage($args) {
2339 // Items not escaped here will be escaped in newPost.
2340 $username = $this->escape($args[1]);
2341 $password = $this->escape($args[2]);
2343 $publish = $args[4];
2345 if ( !$user = $this->login($username, $password) )
2346 return $this->error;
2348 do_action('xmlrpc_call', 'wp.newPage');
2350 // Mark this as content for a page.
2351 $args[3]["post_type"] = 'page';
2353 // Let mw_newPost do all of the heavy lifting.
2354 return($this->mw_newPost($args));
2362 * @param array $args Method parameters.
2363 * @return bool True, if success.
2365 function wp_deletePage($args) {
2366 $this->escape($args);
2368 $blog_id = (int) $args[0];
2369 $username = $args[1];
2370 $password = $args[2];
2371 $page_id = (int) $args[3];
2373 if ( !$user = $this->login($username, $password) )
2374 return $this->error;
2376 do_action('xmlrpc_call', 'wp.deletePage');
2378 // Get the current page based on the page_id and
2379 // make sure it is a page and not a post.
2380 $actual_page = get_post($page_id, ARRAY_A);
2381 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2382 return(new IXR_Error(404, __('Sorry, no such page.')));
2384 // Make sure the user can delete pages.
2385 if ( !current_user_can('delete_page', $page_id) )
2386 return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
2388 // Attempt to delete the page.
2389 $result = wp_delete_post($page_id);
2391 return(new IXR_Error(500, __('Failed to delete the page.')));
2393 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2403 * @param array $args Method parameters.
2406 function wp_editPage($args) {
2407 // Items not escaped here will be escaped in editPost.
2408 $blog_id = (int) $args[0];
2409 $page_id = (int) $this->escape($args[1]);
2410 $username = $this->escape($args[2]);
2411 $password = $this->escape($args[3]);
2412 $content = $args[4];
2413 $publish = $args[5];
2415 if ( !$user = $this->login($username, $password) )
2416 return $this->error;
2418 do_action('xmlrpc_call', 'wp.editPage');
2420 // Get the page data and make sure it is a page.
2421 $actual_page = get_post($page_id, ARRAY_A);
2422 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2423 return(new IXR_Error(404, __('Sorry, no such page.')));
2425 // Make sure the user is allowed to edit pages.
2426 if ( !current_user_can('edit_page', $page_id) )
2427 return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
2429 // Mark this as content for a page.
2430 $content['post_type'] = 'page';
2432 // Arrange args in the way mw_editPost understands.
2441 // Let mw_editPost do all of the heavy lifting.
2442 return($this->mw_editPost($args));
2446 * Retrieve page list.
2450 * @param array $args Method parameters.
2453 function wp_getPageList($args) {
2456 $this->escape($args);
2458 $blog_id = (int) $args[0];
2459 $username = $args[1];
2460 $password = $args[2];
2462 if ( !$user = $this->login($username, $password) )
2463 return $this->error;
2465 if ( !current_user_can( 'edit_pages' ) )
2466 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2468 do_action('xmlrpc_call', 'wp.getPageList');
2470 // Get list of pages ids and titles
2471 $page_list = $wpdb->get_results("
2473 post_title page_title,
2474 post_parent page_parent_id,
2479 WHERE post_type = 'page'
2483 // The date needs to be formatted properly.
2484 $num_pages = count($page_list);
2485 for ( $i = 0; $i < $num_pages; $i++ ) {
2486 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2487 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2489 unset($page_list[$i]->post_date_gmt);
2490 unset($page_list[$i]->post_date);
2491 unset($page_list[$i]->post_status);
2498 * Retrieve authors list.
2502 * @param array $args Method parameters.
2505 function wp_getAuthors($args) {
2507 $this->escape($args);
2509 $blog_id = (int) $args[0];
2510 $username = $args[1];
2511 $password = $args[2];
2513 if ( !$user = $this->login($username, $password) )
2514 return $this->error;
2516 if ( !current_user_can('edit_posts') )
2517 return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
2519 do_action('xmlrpc_call', 'wp.getAuthors');
2522 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2524 'user_id' => $user->ID,
2525 'user_login' => $user->user_login,
2526 'display_name' => $user->display_name
2534 * Get list of all tags
2538 * @param array $args Method parameters.
2541 function wp_getTags( $args ) {
2542 $this->escape( $args );
2544 $blog_id = (int) $args[0];
2545 $username = $args[1];
2546 $password = $args[2];
2548 if ( !$user = $this->login($username, $password) )
2549 return $this->error;
2551 if ( !current_user_can( 'edit_posts' ) )
2552 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2554 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2558 if ( $all_tags = get_tags() ) {
2559 foreach( (array) $all_tags as $tag ) {
2560 $struct['tag_id'] = $tag->term_id;
2561 $struct['name'] = $tag->name;
2562 $struct['count'] = $tag->count;
2563 $struct['slug'] = $tag->slug;
2564 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2565 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
2575 * Create new category.
2579 * @param array $args Method parameters.
2580 * @return int Category ID.
2582 function wp_newCategory($args) {
2583 $this->escape($args);
2585 $blog_id = (int) $args[0];
2586 $username = $args[1];
2587 $password = $args[2];
2588 $category = $args[3];
2590 if ( !$user = $this->login($username, $password) )
2591 return $this->error;
2593 do_action('xmlrpc_call', 'wp.newCategory');
2595 // Make sure the user is allowed to add a category.
2596 if ( !current_user_can('manage_categories') )
2597 return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
2599 // If no slug was provided make it empty so that
2600 // WordPress will generate one.
2601 if ( empty($category['slug']) )
2602 $category['slug'] = '';
2604 // If no parent_id was provided make it empty
2605 // so that it will be a top level page (no parent).
2606 if ( !isset($category['parent_id']) )
2607 $category['parent_id'] = '';
2609 // If no description was provided make it empty.
2610 if ( empty($category["description"]) )
2611 $category["description"] = "";
2613 $new_category = array(
2614 'cat_name' => $category['name'],
2615 'category_nicename' => $category['slug'],
2616 'category_parent' => $category['parent_id'],
2617 'category_description' => $category['description']
2620 $cat_id = wp_insert_category($new_category, true);
2621 if ( is_wp_error( $cat_id ) ) {
2622 if ( 'term_exists' == $cat_id->get_error_code() )
2623 return (int) $cat_id->get_error_data();
2625 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2626 } elseif ( ! $cat_id ) {
2627 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2630 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
2640 * @param array $args Method parameters.
2641 * @return mixed See {@link wp_delete_term()} for return info.
2643 function wp_deleteCategory($args) {
2644 $this->escape($args);
2646 $blog_id = (int) $args[0];
2647 $username = $args[1];
2648 $password = $args[2];
2649 $category_id = (int) $args[3];
2651 if ( !$user = $this->login($username, $password) )
2652 return $this->error;
2654 do_action('xmlrpc_call', 'wp.deleteCategory');
2656 if ( !current_user_can('manage_categories') )
2657 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
2659 $status = wp_delete_term( $category_id, 'category' );
2661 if( true == $status )
2662 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
2668 * Retrieve category list.
2672 * @param array $args Method parameters.
2675 function wp_suggestCategories($args) {
2676 $this->escape($args);
2678 $blog_id = (int) $args[0];
2679 $username = $args[1];
2680 $password = $args[2];
2681 $category = $args[3];
2682 $max_results = (int) $args[4];
2684 if ( !$user = $this->login($username, $password) )
2685 return $this->error;
2687 if ( !current_user_can( 'edit_posts' ) )
2688 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
2690 do_action('xmlrpc_call', 'wp.suggestCategories');
2692 $category_suggestions = array();
2693 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
2694 foreach ( (array) get_categories($args) as $cat ) {
2695 $category_suggestions[] = array(
2696 'category_id' => $cat->term_id,
2697 'category_name' => $cat->name
2701 return($category_suggestions);
2709 * @param array $args Method parameters.
2712 function wp_getComment($args) {
2713 $this->escape($args);
2715 $blog_id = (int) $args[0];
2716 $username = $args[1];
2717 $password = $args[2];
2718 $comment_id = (int) $args[3];
2720 if ( !$user = $this->login($username, $password) )
2721 return $this->error;
2723 if ( !current_user_can( 'moderate_comments' ) )
2724 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2726 do_action('xmlrpc_call', 'wp.getComment');
2728 if ( ! $comment = get_comment($comment_id) )
2729 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2731 return $this->_prepare_comment( $comment );
2735 * Retrieve comments.
2737 * Besides the common blog_id, username, and password arguments, it takes a filter
2738 * array as last argument.
2740 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
2742 * The defaults are as follows:
2743 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
2744 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
2745 * - 'number' - Default is 10. Total number of media items to retrieve.
2746 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2750 * @param array $args Method parameters.
2751 * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
2753 function wp_getComments($args) {
2754 $this->escape($args);
2756 $blog_id = (int) $args[0];
2757 $username = $args[1];
2758 $password = $args[2];
2759 $struct = isset( $args[3] ) ? $args[3] : array();
2761 if ( !$user = $this->login($username, $password) )
2762 return $this->error;
2764 if ( !current_user_can( 'moderate_comments' ) )
2765 return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
2767 do_action('xmlrpc_call', 'wp.getComments');
2769 if ( isset($struct['status']) )
2770 $status = $struct['status'];
2775 if ( isset($struct['post_id']) )
2776 $post_id = absint($struct['post_id']);
2779 if ( isset($struct['offset']) )
2780 $offset = absint($struct['offset']);
2783 if ( isset($struct['number']) )
2784 $number = absint($struct['number']);
2786 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
2788 $comments_struct = array();
2790 foreach ( $comments as $comment ) {
2791 $comments_struct[] = $this->_prepare_comment( $comment );
2794 return $comments_struct;
2800 * By default, the comment will be moved to the trash instead of deleted.
2801 * See {@link wp_delete_comment()} for more information on
2806 * @param array $args Method parameters. Contains:
2811 * @return mixed {@link wp_delete_comment()}
2813 function wp_deleteComment($args) {
2814 $this->escape($args);
2816 $blog_id = (int) $args[0];
2817 $username = $args[1];
2818 $password = $args[2];
2819 $comment_ID = (int) $args[3];
2821 if ( !$user = $this->login($username, $password) )
2822 return $this->error;
2824 if ( !current_user_can( 'moderate_comments' ) )
2825 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2827 if ( ! get_comment($comment_ID) )
2828 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2830 if ( !current_user_can( 'edit_comment', $comment_ID ) )
2831 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2833 do_action('xmlrpc_call', 'wp.deleteComment');
2835 $status = wp_delete_comment( $comment_ID );
2837 if( true == $status )
2838 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
2846 * Besides the common blog_id, username, and password arguments, it takes a
2847 * comment_id integer and a content_struct array as last argument.
2849 * The allowed keys in the content_struct array are:
2854 * - 'date_created_gmt'
2855 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
2859 * @param array $args. Contains:
2865 * @return bool True, on success.
2867 function wp_editComment($args) {
2868 $this->escape($args);
2870 $blog_id = (int) $args[0];
2871 $username = $args[1];
2872 $password = $args[2];
2873 $comment_ID = (int) $args[3];
2874 $content_struct = $args[4];
2876 if ( !$user = $this->login($username, $password) )
2877 return $this->error;
2879 if ( !current_user_can( 'moderate_comments' ) )
2880 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2882 if ( ! get_comment($comment_ID) )
2883 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2885 if ( !current_user_can( 'edit_comment', $comment_ID ) )
2886 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2888 do_action('xmlrpc_call', 'wp.editComment');
2890 if ( isset($content_struct['status']) ) {
2891 $statuses = get_comment_statuses();
2892 $statuses = array_keys($statuses);
2894 if ( ! in_array($content_struct['status'], $statuses) )
2895 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
2896 $comment_approved = $content_struct['status'];
2899 // Do some timestamp voodoo
2900 if ( !empty( $content_struct['date_created_gmt'] ) ) {
2901 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
2902 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
2903 $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
2904 $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
2907 if ( isset($content_struct['content']) )
2908 $comment_content = $content_struct['content'];
2910 if ( isset($content_struct['author']) )
2911 $comment_author = $content_struct['author'];
2913 if ( isset($content_struct['author_url']) )
2914 $comment_author_url = $content_struct['author_url'];
2916 if ( isset($content_struct['author_email']) )
2917 $comment_author_email = $content_struct['author_email'];
2919 // We've got all the data -- post it:
2920 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
2922 $result = wp_update_comment($comment);
2923 if ( is_wp_error( $result ) )
2924 return new IXR_Error(500, $result->get_error_message());
2927 return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
2929 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
2935 * Create new comment.
2939 * @param array $args Method parameters.
2940 * @return mixed {@link wp_new_comment()}
2942 function wp_newComment($args) {
2945 $this->escape($args);
2947 $blog_id = (int) $args[0];
2948 $username = $args[1];
2949 $password = $args[2];
2951 $content_struct = $args[4];
2953 $allow_anon = apply_filters('xmlrpc_allow_anonymous_comments', false);
2955 $user = $this->login($username, $password);
2959 if ( $allow_anon && get_option('comment_registration') )
2960 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
2961 else if ( !$allow_anon )
2962 return $this->error;
2967 if ( is_numeric($post) )
2968 $post_id = absint($post);
2970 $post_id = url_to_postid($post);
2973 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2975 if ( ! get_post($post_id) )
2976 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2978 $comment['comment_post_ID'] = $post_id;
2981 $comment['comment_author'] = $wpdb->escape( $user->display_name );
2982 $comment['comment_author_email'] = $wpdb->escape( $user->user_email );
2983 $comment['comment_author_url'] = $wpdb->escape( $user->user_url );
2984 $comment['user_ID'] = $user->ID;
2986 $comment['comment_author'] = '';
2987 if ( isset($content_struct['author']) )
2988 $comment['comment_author'] = $content_struct['author'];
2990 $comment['comment_author_email'] = '';
2991 if ( isset($content_struct['author_email']) )
2992 $comment['comment_author_email'] = $content_struct['author_email'];
2994 $comment['comment_author_url'] = '';
2995 if ( isset($content_struct['author_url']) )
2996 $comment['comment_author_url'] = $content_struct['author_url'];
2998 $comment['user_ID'] = 0;
3000 if ( get_option('require_name_email') ) {
3001 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
3002 return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
3003 elseif ( !is_email($comment['comment_author_email']) )
3004 return new IXR_Error( 403, __( 'A valid email address is required' ) );
3008 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
3010 $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
3012 do_action('xmlrpc_call', 'wp.newComment');
3014 $comment_ID = wp_new_comment( $comment );
3016 do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
3022 * Retrieve all of the comment status.
3026 * @param array $args Method parameters.
3029 function wp_getCommentStatusList($args) {
3030 $this->escape( $args );
3032 $blog_id = (int) $args[0];
3033 $username = $args[1];
3034 $password = $args[2];
3036 if ( !$user = $this->login($username, $password) )
3037 return $this->error;
3039 if ( !current_user_can( 'moderate_comments' ) )
3040 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3042 do_action('xmlrpc_call', 'wp.getCommentStatusList');
3044 return get_comment_statuses();
3048 * Retrieve comment count.
3052 * @param array $args Method parameters.
3055 function wp_getCommentCount( $args ) {
3056 $this->escape($args);
3058 $blog_id = (int) $args[0];
3059 $username = $args[1];
3060 $password = $args[2];
3061 $post_id = (int) $args[3];
3063 if ( !$user = $this->login($username, $password) )
3064 return $this->error;
3066 if ( !current_user_can( 'edit_posts' ) )
3067 return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
3069 do_action('xmlrpc_call', 'wp.getCommentCount');
3071 $count = wp_count_comments( $post_id );
3073 'approved' => $count->approved,
3074 'awaiting_moderation' => $count->moderated,
3075 'spam' => $count->spam,
3076 'total_comments' => $count->total_comments
3081 * Retrieve post statuses.
3085 * @param array $args Method parameters.
3088 function wp_getPostStatusList( $args ) {
3089 $this->escape( $args );
3091 $blog_id = (int) $args[0];
3092 $username = $args[1];
3093 $password = $args[2];
3095 if ( !$user = $this->login($username, $password) )
3096 return $this->error;
3098 if ( !current_user_can( 'edit_posts' ) )
3099 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3101 do_action('xmlrpc_call', 'wp.getPostStatusList');
3103 return get_post_statuses();
3107 * Retrieve page statuses.
3111 * @param array $args Method parameters.
3114 function wp_getPageStatusList( $args ) {
3115 $this->escape( $args );
3117 $blog_id = (int) $args[0];
3118 $username = $args[1];
3119 $password = $args[2];
3121 if ( !$user = $this->login($username, $password) )
3122 return $this->error;
3124 if ( !current_user_can( 'edit_pages' ) )
3125 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3127 do_action('xmlrpc_call', 'wp.getPageStatusList');
3129 return get_page_statuses();
3133 * Retrieve page templates.
3137 * @param array $args Method parameters.
3140 function wp_getPageTemplates( $args ) {
3141 $this->escape( $args );
3143 $blog_id = (int) $args[0];
3144 $username = $args[1];
3145 $password = $args[2];
3147 if ( !$user = $this->login($username, $password) )
3148 return $this->error;
3150 if ( !current_user_can( 'edit_pages' ) )
3151 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3153 $templates = get_page_templates();
3154 $templates['Default'] = 'default';
3160 * Retrieve blog options.
3164 * @param array $args Method parameters.
3167 function wp_getOptions( $args ) {
3168 $this->escape( $args );
3170 $blog_id = (int) $args[0];
3171 $username = $args[1];
3172 $password = $args[2];
3173 $options = isset( $args[3] ) ? (array) $args[3] : array();
3175 if ( !$user = $this->login($username, $password) )
3176 return $this->error;
3178 // If no specific options where asked for, return all of them
3179 if ( count( $options ) == 0 )
3180 $options = array_keys($this->blog_options);
3182 return $this->_getOptions($options);
3186 * Retrieve blog options value from list.
3190 * @param array $options Options to retrieve.
3193 function _getOptions($options) {
3195 foreach ( $options as $option ) {
3196 if ( array_key_exists( $option, $this->blog_options ) ) {
3197 $data[$option] = $this->blog_options[$option];
3198 //Is the value static or dynamic?
3199 if ( isset( $data[$option]['option'] ) ) {
3200 $data[$option]['value'] = get_option( $data[$option]['option'] );
3201 unset($data[$option]['option']);
3210 * Update blog options.
3214 * @param array $args Method parameters.
3217 function wp_setOptions( $args ) {
3218 $this->escape( $args );
3220 $blog_id = (int) $args[0];
3221 $username = $args[1];
3222 $password = $args[2];
3223 $options = (array) $args[3];
3225 if ( !$user = $this->login($username, $password) )
3226 return $this->error;
3228 if ( !current_user_can( 'manage_options' ) )
3229 return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
3231 foreach ( $options as $o_name => $o_value ) {
3232 $option_names[] = $o_name;
3233 if ( !array_key_exists( $o_name, $this->blog_options ) )
3236 if ( $this->blog_options[$o_name]['readonly'] == true )
3239 update_option( $this->blog_options[$o_name]['option'], $o_value );
3242 //Now return the updated values
3243 return $this->_getOptions($option_names);
3247 * Retrieve a media item by ID
3251 * @param array $args Method parameters. Contains:
3256 * @return array. Associative array containing:
3257 * - 'date_created_gmt'
3266 function wp_getMediaItem($args) {
3267 $this->escape($args);
3269 $blog_id = (int) $args[0];
3270 $username = $args[1];
3271 $password = $args[2];
3272 $attachment_id = (int) $args[3];
3274 if ( !$user = $this->login($username, $password) )
3275 return $this->error;
3277 if ( !current_user_can( 'upload_files' ) )
3278 return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
3280 do_action('xmlrpc_call', 'wp.getMediaItem');
3282 if ( ! $attachment = get_post($attachment_id) )
3283 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
3285 return $this->_prepare_media_item( $attachment );
3289 * Retrieves a collection of media library items (or attachments)
3291 * Besides the common blog_id, username, and password arguments, it takes a filter
3292 * array as last argument.
3294 * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
3296 * The defaults are as follows:
3297 * - 'number' - Default is 5. Total number of media items to retrieve.
3298 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
3299 * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
3300 * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
3304 * @param array $args Method parameters. Contains:
3309 * @return array. Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
3311 function wp_getMediaLibrary($args) {
3312 $this->escape($args);
3314 $blog_id = (int) $args[0];
3315 $username = $args[1];
3316 $password = $args[2];
3317 $struct = isset( $args[3] ) ? $args[3] : array() ;
3319 if ( !$user = $this->login($username, $password) )
3320 return $this->error;
3322 if ( !current_user_can( 'upload_files' ) )
3323 return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
3325 do_action('xmlrpc_call', 'wp.getMediaLibrary');
3327 $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
3328 $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
3329 $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
3330 $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
3332 $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
3334 $attachments_struct = array();
3336 foreach ($attachments as $attachment )
3337 $attachments_struct[] = $this->_prepare_media_item( $attachment );
3339 return $attachments_struct;
3343 * Retrieves a list of post formats used by the site
3347 * @param array $args Method parameters. Contains:
3353 function wp_getPostFormats( $args ) {
3354 $this->escape( $args );
3356 $blog_id = (int) $args[0];
3357 $username = $args[1];
3358 $password = $args[2];
3360 if ( !$user = $this->login( $username, $password ) )
3361 return $this->error;
3363 if ( !current_user_can( 'edit_posts' ) )
3364 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3366 do_action( 'xmlrpc_call', 'wp.getPostFormats' );
3368 $formats = get_post_format_strings();
3370 # find out if they want a list of currently supports formats
3371 if ( isset( $args[3] ) && is_array( $args[3] ) ) {
3372 if ( $args[3]['show-supported'] ) {
3373 if ( current_theme_supports( 'post-formats' ) ) {
3374 $supported = get_theme_support( 'post-formats' );
3376 $data['all'] = $formats;
3377 $data['supported'] = $supported[0];
3388 * Retrieves a post type
3392 * @uses get_post_type_object()
3393 * @param array $args Method parameters. Contains:
3395 * - string $username
3396 * - string $password
3397 * - string $post_type_name
3399 * @return array contains:
3402 * - 'capability_type'
3410 function wp_getPostType( $args ) {
3411 if ( ! $this->minimum_args( $args, 4 ) )
3412 return $this->error;
3414 $this->escape( $args );
3416 $blog_id = (int) $args[0];
3417 $username = $args[1];
3418 $password = $args[2];
3419 $post_type_name = $args[3];
3421 if ( isset( $args[4] ) )
3424 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
3426 if ( !$user = $this->login( $username, $password ) )
3427 return $this->error;
3429 do_action( 'xmlrpc_call', 'wp.getPostType' );
3431 if( ! post_type_exists( $post_type_name ) )
3432 return new IXR_Error( 403, __( 'Invalid post type' ) );
3434 $post_type = get_post_type_object( $post_type_name );
3436 if( ! current_user_can( $post_type->cap->edit_posts ) )
3437 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
3439 return $this->_prepare_post_type( $post_type, $fields );
3443 * Retrieves a post types
3447 * @uses get_post_types()
3448 * @param array $args Method parameters. Contains:
3450 * - string $username
3451 * - string $password
3456 function wp_getPostTypes( $args ) {
3457 if ( ! $this->minimum_args( $args, 3 ) )
3458 return $this->error;
3460 $this->escape( $args );
3462 $blog_id = (int) $args[0];
3463 $username = $args[1];
3464 $password = $args[2];
3465 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
3467 if ( isset( $args[4] ) )
3470 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
3472 if ( ! $user = $this->login( $username, $password ) )
3473 return $this->error;
3475 do_action( 'xmlrpc_call', 'wp.getPostTypes' );
3477 $post_types = get_post_types( $filter, 'objects' );
3481 foreach( $post_types as $post_type ) {
3482 if( ! current_user_can( $post_type->cap->edit_posts ) )
3485 $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
3492 * Retrieve revisions for a specific post.
3496 * The optional $fields parameter specifies what fields will be included
3497 * in the response array.
3499 * @uses wp_get_post_revisions()
3500 * @see wp_getPost() for more on $fields
3502 * @param array $args Method parameters. Contains:
3504 * - string $username
3505 * - string $password
3508 * @return array contains a collection of posts.
3510 function wp_getRevisions( $args ) {
3511 if ( ! $this->minimum_args( $args, 4 ) )
3512 return $this->error;
3514 $this->escape( $args );
3516 $blog_id = (int) $args[0];
3517 $username = $args[1];
3518 $password = $args[2];
3519 $post_id = (int) $args[3];
3521 if ( isset( $args[4] ) )
3524 $fields = apply_filters( 'xmlrpc_default_revision_fields', array( 'post_date', 'post_date_gmt' ), 'wp.getRevisions' );
3526 if ( ! $user = $this->login( $username, $password ) )
3527 return $this->error;
3529 do_action( 'xmlrpc_call', 'wp.getRevisions' );
3531 if ( ! $post = get_post( $post_id ) )
3532 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3534 if ( ! current_user_can( 'edit_post', $post_id ) )
3535 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
3537 // Check if revisions are enabled.
3538 if ( ! WP_POST_REVISIONS || ! post_type_supports( $post->post_type, 'revisions' ) )
3539 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
3541 $revisions = wp_get_post_revisions( $post_id );
3548 foreach ( $revisions as $revision ) {
3549 if ( ! current_user_can( 'read_post', $revision->ID ) )
3553 if ( wp_is_post_autosave( $revision ) )
3556 $struct[] = $this->_prepare_post( get_object_vars( $revision ), $fields );
3563 * Restore a post revision
3567 * @uses wp_restore_post_revision()
3569 * @param array $args Method parameters. Contains:
3571 * - string $username
3572 * - string $password
3574 * @return bool false if there was an error restoring, true if success.
3576 function wp_restoreRevision( $args ) {
3577 if ( ! $this->minimum_args( $args, 3 ) )
3578 return $this->error;
3580 $this->escape( $args );
3582 $blog_id = (int) $args[0];
3583 $username = $args[1];
3584 $password = $args[2];
3585 $revision_id = (int) $args[3];
3587 if ( ! $user = $this->login( $username, $password ) )
3588 return $this->error;
3590 do_action( 'xmlrpc_call', 'wp.restoreRevision' );
3592 if ( ! $revision = wp_get_post_revision( $revision_id ) )
3593 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3595 if ( wp_is_post_autosave( $revision ) )
3596 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3598 if ( ! $post = get_post( $revision->post_parent ) )
3599 return new IXR_Error( 404, __( 'Invalid post ID' ) );
3601 if ( ! current_user_can( 'edit_post', $revision->post_parent ) )
3602 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
3604 // Check if revisions are disabled.
3605 if ( ! WP_POST_REVISIONS || ! post_type_supports( $post->post_type, 'revisions' ) )
3606 return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
3608 $post = wp_restore_post_revision( $revision_id );
3610 return (bool) $post;
3613 /* Blogger API functions.
3614 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
3618 * Retrieve blogs that user owns.
3620 * Will make more sense once we support multiple blogs.
3624 * @param array $args Method parameters.
3627 function blogger_getUsersBlogs($args) {
3628 if ( is_multisite() )
3629 return $this->_multisite_getUsersBlogs($args);
3631 $this->escape($args);
3633 $username = $args[1];
3634 $password = $args[2];
3636 if ( !$user = $this->login($username, $password) )
3637 return $this->error;
3639 do_action('xmlrpc_call', 'blogger.getUsersBlogs');
3641 $is_admin = current_user_can('manage_options');
3644 'isAdmin' => $is_admin,
3645 'url' => get_option('home') . '/',
3647 'blogName' => get_option('blogname'),
3648 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
3651 return array($struct);
3655 * Private function for retrieving a users blogs for multisite setups
3659 function _multisite_getUsersBlogs($args) {
3660 $current_blog = get_blog_details();
3662 $domain = $current_blog->domain;
3663 $path = $current_blog->path . 'xmlrpc.php';
3665 $rpc = new IXR_Client( set_url_scheme( "http://{$domain}{$path}" ) );
3666 $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
3667 $blogs = $rpc->getResponse();
3669 if ( isset($blogs['faultCode']) )
3670 return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
3672 if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
3675 foreach ( (array) $blogs as $blog ) {
3676 if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
3677 return array($blog);
3684 * Retrieve user's data.
3686 * Gives your client some info about you, so you don't have to.
3690 * @param array $args Method parameters.
3693 function blogger_getUserInfo($args) {
3695 $this->escape($args);
3697 $username = $args[1];
3698 $password = $args[2];
3700 if ( !$user = $this->login($username, $password) )
3701 return $this->error;
3703 if ( !current_user_can( 'edit_posts' ) )
3704 return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
3706 do_action('xmlrpc_call', 'blogger.getUserInfo');
3709 'nickname' => $user->nickname,
3710 'userid' => $user->ID,
3711 'url' => $user->user_url,
3712 'lastname' => $user->last_name,
3713 'firstname' => $user->first_name
3724 * @param array $args Method parameters.
3727 function blogger_getPost($args) {
3729 $this->escape($args);
3731 $post_ID = (int) $args[1];
3732 $username = $args[2];
3733 $password = $args[3];
3735 if ( !$user = $this->login($username, $password) )
3736 return $this->error;
3738 $post_data = get_post($post_ID, ARRAY_A);
3740 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3742 if ( !current_user_can( 'edit_post', $post_ID ) )
3743 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
3745 do_action('xmlrpc_call', 'blogger.getPost');
3747 $categories = implode(',', wp_get_post_categories($post_ID));
3749 $content = '<title>'.stripslashes($post_data['post_title']).'</title>';
3750 $content .= '<category>'.$categories.'</category>';
3751 $content .= stripslashes($post_data['post_content']);
3754 'userid' => $post_data['post_author'],
3755 'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
3756 'content' => $content,
3757 'postid' => (string) $post_data['ID']
3764 * Retrieve list of recent posts.
3768 * @param array $args Method parameters.
3771 function blogger_getRecentPosts($args) {
3773 $this->escape($args);
3775 // $args[0] = appkey - ignored
3776 $blog_ID = (int) $args[1]; /* though we don't use it yet */
3777 $username = $args[2];
3778 $password = $args[3];
3779 if ( isset( $args[4] ) )
3780 $query = array( 'numberposts' => absint( $args[4] ) );
3784 if ( !$user = $this->login($username, $password) )
3785 return $this->error;
3787 do_action('xmlrpc_call', 'blogger.getRecentPosts');
3789 $posts_list = wp_get_recent_posts( $query );
3791 if ( !$posts_list ) {
3792 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
3793 return $this->error;
3796 foreach ($posts_list as $entry) {
3797 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
3800 $post_date = $this->_convert_date( $entry['post_date'] );
3801 $categories = implode(',', wp_get_post_categories($entry['ID']));
3803 $content = '<title>'.stripslashes($entry['post_title']).'</title>';
3804 $content .= '<category>'.$categories.'</category>';
3805 $content .= stripslashes($entry['post_content']);
3808 'userid' => $entry['post_author'],
3809 'dateCreated' => $post_date,
3810 'content' => $content,
3811 'postid' => (string) $entry['ID'],
3816 $recent_posts = array();
3817 for ( $j=0; $j<count($struct); $j++ ) {
3818 array_push($recent_posts, $struct[$j]);
3821 return $recent_posts;
3830 function blogger_getTemplate($args) {
3831 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
3840 function blogger_setTemplate($args) {
3841 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
3849 * @param array $args Method parameters.
3852 function blogger_newPost($args) {
3854 $this->escape($args);
3856 $blog_ID = (int) $args[1]; /* though we don't use it yet */
3857 $username = $args[2];
3858 $password = $args[3];
3859 $content = $args[4];
3860 $publish = $args[5];
3862 if ( !$user = $this->login($username, $password) )
3863 return $this->error;
3865 do_action('xmlrpc_call', 'blogger.newPost');
3867 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
3868 if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) || !current_user_can($cap) )
3869 return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
3871 $post_status = ($publish) ? 'publish' : 'draft';
3873 $post_author = $user->ID;
3875 $post_title = xmlrpc_getposttitle($content);
3876 $post_category = xmlrpc_getpostcategory($content);
3877 $post_content = xmlrpc_removepostdata($content);
3879 $post_date = current_time('mysql');
3880 $post_date_gmt = current_time('mysql', 1);
3882 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
3884 $post_ID = wp_insert_post($post_data);
3885 if ( is_wp_error( $post_ID ) )
3886 return new IXR_Error(500, $post_ID->get_error_message());
3889 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
3891 $this->attach_uploads( $post_ID, $post_content );
3893 do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
3903 * @param array $args Method parameters.
3904 * @return bool true when done.
3906 function blogger_editPost($args) {
3908 $this->escape($args);
3910 $post_ID = (int) $args[1];
3911 $username = $args[2];
3912 $password = $args[3];
3913 $content = $args[4];
3914 $publish = $args[5];
3916 if ( !$user = $this->login($username, $password) )
3917 return $this->error;
3919 do_action('xmlrpc_call', 'blogger.editPost');
3921 $actual_post = get_post($post_ID,ARRAY_A);
3923 if ( !$actual_post || $actual_post['post_type'] != 'post' )
3924 return new IXR_Error(404, __('Sorry, no such post.'));
3926 $this->escape($actual_post);
3928 if ( !current_user_can('edit_post', $post_ID) )
3929 return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
3931 extract($actual_post, EXTR_SKIP);
3933 if ( ('publish' == $post_status) && !current_user_can('publish_posts') )
3934 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
3936 $post_title = xmlrpc_getposttitle($content);
3937 $post_category = xmlrpc_getpostcategory($content);
3938 $post_content = xmlrpc_removepostdata($content);
3940 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
3942 $result = wp_update_post($postdata);
3945 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
3947 $this->attach_uploads( $ID, $post_content );
3949 do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
3959 * @param array $args Method parameters.
3960 * @return bool True when post is deleted.
3962 function blogger_deletePost($args) {
3963 $this->escape($args);
3965 $post_ID = (int) $args[1];
3966 $username = $args[2];
3967 $password = $args[3];
3968 $publish = $args[4];
3970 if ( !$user = $this->login($username, $password) )
3971 return $this->error;
3973 do_action('xmlrpc_call', 'blogger.deletePost');
3975 $actual_post = get_post($post_ID,ARRAY_A);
3977 if ( !$actual_post || $actual_post['post_type'] != 'post' )
3978 return new IXR_Error(404, __('Sorry, no such post.'));
3980 if ( !current_user_can('delete_post', $post_ID) )
3981 return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
3983 $result = wp_delete_post($post_ID);
3986 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
3988 do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
3993 /* MetaWeblog API functions
3994 * specs on wherever Dave Winer wants them to be
3998 * Create a new post.
4000 * The 'content_struct' argument must contain:
4009 * Also, it can optionally contain:
4012 * - wp_page_parent_id
4015 * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
4016 * - mt_allow_comments - can be 'open' or 'closed'
4017 * - mt_allow_pings - can be 'open' or 'closed'
4018 * - date_created_gmt
4020 * - wp_post_thumbnail
4024 * @param array $args Method parameters. Contains:
4032 function mw_newPost($args) {
4033 $this->escape($args);
4035 $blog_ID = (int) $args[0];
4036 $username = $args[1];
4037 $password = $args[2];
4038 $content_struct = $args[3];
4039 $publish = isset( $args[4] ) ? $args[4] : 0;
4041 if ( !$user = $this->login($username, $password) )
4042 return $this->error;
4044 do_action('xmlrpc_call', 'metaWeblog.newPost');
4046 $page_template = '';
4047 if ( !empty( $content_struct['post_type'] ) ) {
4048 if ( $content_struct['post_type'] == 'page' ) {
4050 $cap = 'publish_pages';
4051 elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
4052 $cap = 'publish_pages';
4054 $cap = 'edit_pages';
4055 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
4056 $post_type = 'page';
4057 if ( !empty( $content_struct['wp_page_template'] ) )
4058 $page_template = $content_struct['wp_page_template'];
4059 } elseif ( $content_struct['post_type'] == 'post' ) {
4061 $cap = 'publish_posts';
4062 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
4063 $cap = 'publish_posts';
4065 $cap = 'edit_posts';
4066 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4067 $post_type = 'post';
4069 // No other post_type values are allowed here
4070 return new IXR_Error( 401, __( 'Invalid post type' ) );
4074 $cap = 'publish_posts';
4075 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
4076 $cap = 'publish_posts';
4078 $cap = 'edit_posts';
4079 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4080 $post_type = 'post';
4083 if ( ! current_user_can( get_post_type_object( $post_type )->cap->create_posts ) )
4084 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts on this site.' ) );
4085 if ( !current_user_can( $cap ) )
4086 return new IXR_Error( 401, $error_message );
4088 // Check for a valid post format if one was given
4089 if ( isset( $content_struct['wp_post_format'] ) ) {
4090 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4091 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4092 return new IXR_Error( 404, __( 'Invalid post format' ) );
4096 // Let WordPress generate the post_name (slug) unless
4097 // one has been provided.
4099 if ( isset($content_struct['wp_slug']) )
4100 $post_name = $content_struct['wp_slug'];
4102 // Only use a password if one was given.
4103 if ( isset($content_struct['wp_password']) )
4104 $post_password = $content_struct['wp_password'];
4106 // Only set a post parent if one was provided.
4107 if ( isset($content_struct['wp_page_parent_id']) )
4108 $post_parent = $content_struct['wp_page_parent_id'];
4110 // Only set the menu_order if it was provided.
4111 if ( isset($content_struct['wp_page_order']) )
4112 $menu_order = $content_struct['wp_page_order'];
4114 $post_author = $user->ID;
4116 // If an author id was provided then use it instead.
4117 if ( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) {
4118 switch ( $post_type ) {
4120 if ( !current_user_can( 'edit_others_posts' ) )
4121 return( new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) ) );
4124 if ( !current_user_can( 'edit_others_pages' ) )
4125 return( new IXR_Error( 401, __( 'You are not allowed to create pages as this user.' ) ) );
4128 return( new IXR_Error( 401, __( 'Invalid post type' ) ) );
4131 $author = get_userdata( $content_struct['wp_author_id'] );
4133 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
4134 $post_author = $content_struct['wp_author_id'];
4137 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
4138 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
4140 $post_status = $publish ? 'publish' : 'draft';
4142 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4143 switch ( $content_struct["{$post_type}_status"] ) {
4148 $post_status = $content_struct["{$post_type}_status"];
4151 $post_status = $publish ? 'publish' : 'draft';
4156 $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
4157 $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
4159 $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
4161 if ( isset($content_struct['mt_allow_comments']) ) {
4162 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4163 switch ( $content_struct['mt_allow_comments'] ) {
4165 $comment_status = 'closed';
4168 $comment_status = 'open';
4171 $comment_status = get_option('default_comment_status');
4175 switch ( (int) $content_struct['mt_allow_comments'] ) {
4178 $comment_status = 'closed';
4181 $comment_status = 'open';
4184 $comment_status = get_option('default_comment_status');
4189 $comment_status = get_option('default_comment_status');
4192 if ( isset($content_struct['mt_allow_pings']) ) {
4193 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4194 switch ( $content_struct['mt_allow_pings'] ) {
4196 $ping_status = 'closed';
4199 $ping_status = 'open';
4202 $ping_status = get_option('default_ping_status');
4206 switch ( (int) $content_struct['mt_allow_pings'] ) {
4208 $ping_status = 'closed';
4211 $ping_status = 'open';
4214 $ping_status = get_option('default_ping_status');
4219 $ping_status = get_option('default_ping_status');
4223 $post_content = $post_content . '<!--more-->' . $post_more;
4226 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4227 $to_ping = $content_struct['mt_tb_ping_urls'];
4228 if ( is_array($to_ping) )
4229 $to_ping = implode(' ', $to_ping);
4232 // Do some timestamp voodoo
4233 if ( !empty( $content_struct['date_created_gmt'] ) )
4234 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4235 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4236 elseif ( !empty( $content_struct['dateCreated']) )
4237 $dateCreated = $content_struct['dateCreated']->getIso();
4239 if ( !empty( $dateCreated ) ) {
4240 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4241 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4243 $post_date = current_time('mysql');
4244 $post_date_gmt = current_time('mysql', 1);
4247 $post_category = array();
4248 if ( isset( $content_struct['categories'] ) ) {
4249 $catnames = $content_struct['categories'];
4251 if ( is_array($catnames) ) {
4252 foreach ($catnames as $cat) {
4253 $post_category[] = get_cat_ID($cat);
4258 $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');
4260 $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
4262 // Only posts can be sticky
4263 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4264 if ( $content_struct['sticky'] == true )
4265 stick_post( $post_ID );
4266 elseif ( $content_struct['sticky'] == false )
4267 unstick_post( $post_ID );
4270 if ( isset($content_struct['custom_fields']) )
4271 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4273 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4274 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4275 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4277 unset( $content_struct['wp_post_thumbnail'] );
4280 // Handle enclosures
4281 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4282 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4284 $this->attach_uploads( $post_ID, $post_content );
4286 // Handle post formats if assigned, value is validated earlier
4288 if ( isset( $content_struct['wp_post_format'] ) )
4289 set_post_format( $post_ID, $content_struct['wp_post_format'] );
4291 $post_ID = wp_insert_post( $postdata, true );
4292 if ( is_wp_error( $post_ID ) )
4293 return new IXR_Error(500, $post_ID->get_error_message());
4296 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4298 do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
4300 return strval($post_ID);
4303 function add_enclosure_if_new($post_ID, $enclosure) {
4304 if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
4306 $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'];
4308 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
4309 if ($key == 'enclosure') {
4310 foreach ( (array) $val as $enc ) {
4311 if ($enc == $encstring) {
4319 add_post_meta( $post_ID, 'enclosure', $encstring );
4324 * Attach upload to a post.
4328 * @param int $post_ID Post ID.
4329 * @param string $post_content Post Content for attachment.
4331 function attach_uploads( $post_ID, $post_content ) {
4334 // find any unattached files
4335 $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
4336 if ( is_array( $attachments ) ) {
4337 foreach ( $attachments as $file ) {
4338 if ( strpos( $post_content, $file->guid ) !== false )
4339 $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
4349 * @param array $args Method parameters.
4350 * @return bool True on success.
4352 function mw_editPost($args) {
4354 $this->escape($args);
4356 $post_ID = (int) $args[0];
4357 $username = $args[1];
4358 $password = $args[2];
4359 $content_struct = $args[3];
4360 $publish = isset( $args[4] ) ? $args[4] : 0;
4362 if ( ! $user = $this->login($username, $password) )
4363 return $this->error;
4365 do_action('xmlrpc_call', 'metaWeblog.editPost');
4367 $postdata = get_post( $post_ID, ARRAY_A );
4369 // If there is no post data for the give post id, stop
4370 // now and return an error. Other wise a new post will be
4371 // created (which was the old behavior).
4372 if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
4373 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4375 if ( ! current_user_can( 'edit_post', $post_ID ) )
4376 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this post.' ) );
4378 // Use wp.editPost to edit post types other than post and page.
4379 if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
4380 return new IXR_Error( 401, __( 'Invalid post type' ) );
4382 // Thwart attempt to change the post type.
4383 if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
4384 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
4386 // Check for a valid post format if one was given
4387 if ( isset( $content_struct['wp_post_format'] ) ) {
4388 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4389 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4390 return new IXR_Error( 404, __( 'Invalid post format' ) );
4394 $this->escape($postdata);
4395 extract($postdata, EXTR_SKIP);
4397 // Let WordPress manage slug if none was provided.
4399 $post_name = $postdata['post_name'];
4400 if ( isset($content_struct['wp_slug']) )
4401 $post_name = $content_struct['wp_slug'];
4403 // Only use a password if one was given.
4404 if ( isset($content_struct['wp_password']) )
4405 $post_password = $content_struct['wp_password'];
4407 // Only set a post parent if one was given.
4408 if ( isset($content_struct['wp_page_parent_id']) )
4409 $post_parent = $content_struct['wp_page_parent_id'];
4411 // Only set the menu_order if it was given.
4412 if ( isset($content_struct['wp_page_order']) )
4413 $menu_order = $content_struct['wp_page_order'];
4415 if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
4416 $page_template = $content_struct['wp_page_template'];
4418 $post_author = $postdata['post_author'];
4420 // Only set the post_author if one is set.
4421 if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
4422 switch ( $post_type ) {
4424 if ( !current_user_can('edit_others_posts') )
4425 return(new IXR_Error(401, __('You are not allowed to change the post author as this user.')));
4428 if ( !current_user_can('edit_others_pages') )
4429 return(new IXR_Error(401, __('You are not allowed to change the page author as this user.')));
4432 return(new IXR_Error(401, __('Invalid post type')));
4435 $post_author = $content_struct['wp_author_id'];
4438 if ( isset($content_struct['mt_allow_comments']) ) {
4439 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4440 switch ( $content_struct['mt_allow_comments'] ) {
4442 $comment_status = 'closed';
4445 $comment_status = 'open';
4448 $comment_status = get_option('default_comment_status');
4452 switch ( (int) $content_struct['mt_allow_comments'] ) {
4455 $comment_status = 'closed';
4458 $comment_status = 'open';
4461 $comment_status = get_option('default_comment_status');
4467 if ( isset($content_struct['mt_allow_pings']) ) {
4468 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4469 switch ( $content_struct['mt_allow_pings'] ) {
4471 $ping_status = 'closed';
4474 $ping_status = 'open';
4477 $ping_status = get_option('default_ping_status');
4481 switch ( (int) $content_struct["mt_allow_pings"] ) {
4483 $ping_status = 'closed';
4486 $ping_status = 'open';
4489 $ping_status = get_option('default_ping_status');
4495 if ( isset( $content_struct['title'] ) )
4496 $post_title = $content_struct['title'];
4498 if ( isset( $content_struct['description'] ) )
4499 $post_content = $content_struct['description'];
4501 $post_category = array();
4502 if ( isset( $content_struct['categories'] ) ) {
4503 $catnames = $content_struct['categories'];
4504 if ( is_array($catnames) ) {
4505 foreach ($catnames as $cat) {
4506 $post_category[] = get_cat_ID($cat);
4511 if ( isset( $content_struct['mt_excerpt'] ) )
4512 $post_excerpt = $content_struct['mt_excerpt'];
4514 $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
4516 $post_status = $publish ? 'publish' : 'draft';
4517 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4518 switch( $content_struct["{$post_type}_status"] ) {
4523 $post_status = $content_struct["{$post_type}_status"];
4526 $post_status = $publish ? 'publish' : 'draft';
4531 $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
4533 if ( ('publish' == $post_status) ) {
4534 if ( ( 'page' == $post_type ) && !current_user_can('publish_pages') )
4535 return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
4536 else if ( !current_user_can('publish_posts') )
4537 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
4541 $post_content = $post_content . "<!--more-->" . $post_more;
4544 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4545 $to_ping = $content_struct['mt_tb_ping_urls'];
4546 if ( is_array($to_ping) )
4547 $to_ping = implode(' ', $to_ping);
4550 // Do some timestamp voodoo
4551 if ( !empty( $content_struct['date_created_gmt'] ) )
4552 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4553 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4554 elseif ( !empty( $content_struct['dateCreated']) )
4555 $dateCreated = $content_struct['dateCreated']->getIso();
4557 if ( !empty( $dateCreated ) ) {
4558 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4559 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4561 $post_date = $postdata['post_date'];
4562 $post_date_gmt = $postdata['post_date_gmt'];
4565 // We've got all the data -- post it:
4566 $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');
4568 $result = wp_update_post($newpost, true);
4569 if ( is_wp_error( $result ) )
4570 return new IXR_Error(500, $result->get_error_message());
4573 return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
4575 // Only posts can be sticky
4576 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4577 if ( $content_struct['sticky'] == true )
4578 stick_post( $post_ID );
4579 elseif ( $content_struct['sticky'] == false )
4580 unstick_post( $post_ID );
4583 if ( isset($content_struct['custom_fields']) )
4584 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4586 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4587 // empty value deletes, non-empty value adds/updates
4588 if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
4589 delete_post_thumbnail( $post_ID );
4591 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4592 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4594 unset( $content_struct['wp_post_thumbnail'] );
4597 // Handle enclosures
4598 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4599 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4601 $this->attach_uploads( $ID, $post_content );
4603 // Handle post formats if assigned, validation is handled
4604 // earlier in this function
4605 if ( isset( $content_struct['wp_post_format'] ) )
4606 set_post_format( $post_ID, $content_struct['wp_post_format'] );
4608 do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
4618 * @param array $args Method parameters.
4621 function mw_getPost($args) {
4623 $this->escape($args);
4625 $post_ID = (int) $args[0];
4626 $username = $args[1];
4627 $password = $args[2];
4629 if ( !$user = $this->login($username, $password) )
4630 return $this->error;
4632 $postdata = get_post($post_ID, ARRAY_A);
4634 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4636 if ( !current_user_can( 'edit_post', $post_ID ) )
4637 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4639 do_action('xmlrpc_call', 'metaWeblog.getPost');
4641 if ($postdata['post_date'] != '') {
4642 $post_date = $this->_convert_date( $postdata['post_date'] );
4643 $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'], $postdata['post_date'] );
4644 $post_modified = $this->_convert_date( $postdata['post_modified'] );
4645 $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
4647 $categories = array();
4648 $catids = wp_get_post_categories($post_ID);
4649 foreach($catids as $catid)
4650 $categories[] = get_cat_name($catid);
4652 $tagnames = array();
4653 $tags = wp_get_post_tags( $post_ID );
4654 if ( !empty( $tags ) ) {
4655 foreach ( $tags as $tag )
4656 $tagnames[] = $tag->name;
4657 $tagnames = implode( ', ', $tagnames );
4662 $post = get_extended($postdata['post_content']);
4663 $link = post_permalink($postdata['ID']);
4665 // Get the author info.
4666 $author = get_userdata($postdata['post_author']);
4668 $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
4669 $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
4671 // Consider future posts as published
4672 if ( $postdata['post_status'] === 'future' )
4673 $postdata['post_status'] = 'publish';
4676 $post_format = get_post_format( $post_ID );
4677 if ( empty( $post_format ) )
4678 $post_format = 'standard';
4681 if ( is_sticky( $post_ID ) )
4684 $enclosure = array();
4685 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
4686 if ($key == 'enclosure') {
4687 foreach ( (array) $val as $enc ) {
4688 $encdata = explode("\n", $enc);
4689 $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
4690 $enclosure['length'] = (int) trim($encdata[1]);
4691 $enclosure['type'] = trim($encdata[2]);
4698 'dateCreated' => $post_date,
4699 'userid' => $postdata['post_author'],
4700 'postid' => $postdata['ID'],
4701 'description' => $post['main'],
4702 'title' => $postdata['post_title'],
4704 'permaLink' => $link,
4705 // commented out because no other tool seems to use this
4706 // 'content' => $entry['post_content'],
4707 'categories' => $categories,
4708 'mt_excerpt' => $postdata['post_excerpt'],
4709 'mt_text_more' => $post['extended'],
4710 'wp_more_text' => $post['more_text'],
4711 'mt_allow_comments' => $allow_comments,
4712 'mt_allow_pings' => $allow_pings,
4713 'mt_keywords' => $tagnames,
4714 'wp_slug' => $postdata['post_name'],
4715 'wp_password' => $postdata['post_password'],
4716 'wp_author_id' => (string) $author->ID,
4717 'wp_author_display_name' => $author->display_name,
4718 'date_created_gmt' => $post_date_gmt,
4719 'post_status' => $postdata['post_status'],
4720 'custom_fields' => $this->get_custom_fields($post_ID),
4721 'wp_post_format' => $post_format,
4722 'sticky' => $sticky,
4723 'date_modified' => $post_modified,
4724 'date_modified_gmt' => $post_modified_gmt
4727 if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
4729 $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
4733 return new IXR_Error(404, __('Sorry, no such post.'));
4738 * Retrieve list of recent posts.
4742 * @param array $args Method parameters.
4745 function mw_getRecentPosts($args) {
4747 $this->escape($args);
4749 $blog_ID = (int) $args[0];
4750 $username = $args[1];
4751 $password = $args[2];
4752 if ( isset( $args[3] ) )
4753 $query = array( 'numberposts' => absint( $args[3] ) );
4757 if ( !$user = $this->login($username, $password) )
4758 return $this->error;
4760 do_action('xmlrpc_call', 'metaWeblog.getRecentPosts');
4762 $posts_list = wp_get_recent_posts( $query );
4768 foreach ($posts_list as $entry) {
4769 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
4772 $post_date = $this->_convert_date( $entry['post_date'] );
4773 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
4774 $post_modified = $this->_convert_date( $entry['post_modified'] );
4775 $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
4777 $categories = array();
4778 $catids = wp_get_post_categories($entry['ID']);
4779 foreach( $catids as $catid )
4780 $categories[] = get_cat_name($catid);
4782 $tagnames = array();
4783 $tags = wp_get_post_tags( $entry['ID'] );
4784 if ( !empty( $tags ) ) {
4785 foreach ( $tags as $tag ) {
4786 $tagnames[] = $tag->name;
4788 $tagnames = implode( ', ', $tagnames );
4793 $post = get_extended($entry['post_content']);
4794 $link = post_permalink($entry['ID']);
4796 // Get the post author info.
4797 $author = get_userdata($entry['post_author']);
4799 $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
4800 $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
4802 // Consider future posts as published
4803 if ( $entry['post_status'] === 'future' )
4804 $entry['post_status'] = 'publish';
4807 $post_format = get_post_format( $entry['ID'] );
4808 if ( empty( $post_format ) )
4809 $post_format = 'standard';
4812 'dateCreated' => $post_date,
4813 'userid' => $entry['post_author'],
4814 'postid' => (string) $entry['ID'],
4815 'description' => $post['main'],
4816 'title' => $entry['post_title'],
4818 'permaLink' => $link,
4819 // commented out because no other tool seems to use this
4820 // 'content' => $entry['post_content'],
4821 'categories' => $categories,
4822 'mt_excerpt' => $entry['post_excerpt'],
4823 'mt_text_more' => $post['extended'],
4824 'wp_more_text' => $post['more_text'],
4825 'mt_allow_comments' => $allow_comments,
4826 'mt_allow_pings' => $allow_pings,
4827 'mt_keywords' => $tagnames,
4828 'wp_slug' => $entry['post_name'],
4829 'wp_password' => $entry['post_password'],
4830 'wp_author_id' => (string) $author->ID,
4831 'wp_author_display_name' => $author->display_name,
4832 'date_created_gmt' => $post_date_gmt,
4833 'post_status' => $entry['post_status'],
4834 'custom_fields' => $this->get_custom_fields($entry['ID']),
4835 'wp_post_format' => $post_format,
4836 'date_modified' => $post_modified,
4837 'date_modified_gmt' => $post_modified_gmt
4840 $entry_index = count( $struct ) - 1;
4841 $struct[ $entry_index ][ 'wp_post_thumbnail' ] = get_post_thumbnail_id( $entry['ID'] );
4844 $recent_posts = array();
4845 for ( $j=0; $j<count($struct); $j++ ) {
4846 array_push($recent_posts, $struct[$j]);
4849 return $recent_posts;
4853 * Retrieve the list of categories on a given blog.
4857 * @param array $args Method parameters.
4860 function mw_getCategories($args) {
4862 $this->escape($args);
4864 $blog_ID = (int) $args[0];
4865 $username = $args[1];
4866 $password = $args[2];
4868 if ( !$user = $this->login($username, $password) )
4869 return $this->error;
4871 if ( !current_user_can( 'edit_posts' ) )
4872 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
4874 do_action('xmlrpc_call', 'metaWeblog.getCategories');
4876 $categories_struct = array();
4878 if ( $cats = get_categories(array('get' => 'all')) ) {
4879 foreach ( $cats as $cat ) {
4880 $struct['categoryId'] = $cat->term_id;
4881 $struct['parentId'] = $cat->parent;
4882 $struct['description'] = $cat->name;
4883 $struct['categoryDescription'] = $cat->description;
4884 $struct['categoryName'] = $cat->name;
4885 $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
4886 $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
4888 $categories_struct[] = $struct;
4892 return $categories_struct;
4896 * Uploads a file, following your settings.
4898 * Adapted from a patch by Johann Richard.
4900 * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
4904 * @param array $args Method parameters.
4907 function mw_newMediaObject($args) {
4910 $blog_ID = (int) $args[0];
4911 $username = $wpdb->escape($args[1]);
4912 $password = $wpdb->escape($args[2]);
4915 $name = sanitize_file_name( $data['name'] );
4916 $type = $data['type'];
4917 $bits = $data['bits'];
4919 if ( !$user = $this->login($username, $password) )
4920 return $this->error;
4922 do_action('xmlrpc_call', 'metaWeblog.newMediaObject');
4924 if ( !current_user_can('upload_files') ) {
4925 $this->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
4926 return $this->error;
4929 if ( $upload_err = apply_filters( 'pre_upload_error', false ) )
4930 return new IXR_Error(500, $upload_err);
4932 if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) {
4933 // Get postmeta info on the object.
4934 $old_file = $wpdb->get_row("
4937 WHERE post_title = '{$name}'
4938 AND post_type = 'attachment'
4941 // Delete previous file.
4942 wp_delete_attachment($old_file->ID);
4944 // Make sure the new name is different by pre-pending the
4945 // previous post id.
4946 $filename = preg_replace('/^wpid\d+-/', '', $name);
4947 $name = "wpid{$old_file->ID}-{$filename}";
4950 $upload = wp_upload_bits($name, null, $bits);
4951 if ( ! empty($upload['error']) ) {
4952 $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
4953 return new IXR_Error(500, $errorString);
4955 // Construct the attachment array
4957 if ( ! empty( $data['post_id'] ) ) {
4958 $post_id = (int) $data['post_id'];
4960 if ( ! current_user_can( 'edit_post', $post_id ) )
4961 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4963 $attachment = array(
4964 'post_title' => $name,
4965 'post_content' => '',
4966 'post_type' => 'attachment',
4967 'post_parent' => $post_id,
4968 'post_mime_type' => $type,
4969 'guid' => $upload[ 'url' ]
4973 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
4974 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
4976 do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
4979 'id' => strval( $id ),
4981 'url' => $upload[ 'url' ],
4984 return apply_filters( 'wp_handle_upload', $struct, 'upload' );
4987 /* MovableType API functions
4988 * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
4992 * Retrieve the post titles of recent posts.
4996 * @param array $args Method parameters.
4999 function mt_getRecentPostTitles($args) {
5001 $this->escape($args);
5003 $blog_ID = (int) $args[0];
5004 $username = $args[1];
5005 $password = $args[2];
5006 if ( isset( $args[3] ) )
5007 $query = array( 'numberposts' => absint( $args[3] ) );
5011 if ( !$user = $this->login($username, $password) )
5012 return $this->error;
5014 do_action('xmlrpc_call', 'mt.getRecentPostTitles');
5016 $posts_list = wp_get_recent_posts( $query );
5018 if ( !$posts_list ) {
5019 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
5020 return $this->error;
5025 foreach ($posts_list as $entry) {
5026 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5029 $post_date = $this->_convert_date( $entry['post_date'] );
5030 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5033 'dateCreated' => $post_date,
5034 'userid' => $entry['post_author'],
5035 'postid' => (string) $entry['ID'],
5036 'title' => $entry['post_title'],
5037 'post_status' => $entry['post_status'],
5038 'date_created_gmt' => $post_date_gmt
5043 $recent_posts = array();
5044 for ( $j=0; $j<count($struct); $j++ ) {
5045 array_push($recent_posts, $struct[$j]);
5048 return $recent_posts;
5052 * Retrieve list of all categories on blog.
5056 * @param array $args Method parameters.
5059 function mt_getCategoryList($args) {
5061 $this->escape($args);
5063 $blog_ID = (int) $args[0];
5064 $username = $args[1];
5065 $password = $args[2];
5067 if ( !$user = $this->login($username, $password) )
5068 return $this->error;
5070 if ( !current_user_can( 'edit_posts' ) )
5071 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5073 do_action('xmlrpc_call', 'mt.getCategoryList');
5075 $categories_struct = array();
5077 if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
5078 foreach ( $cats as $cat ) {
5079 $struct['categoryId'] = $cat->term_id;
5080 $struct['categoryName'] = $cat->name;
5082 $categories_struct[] = $struct;
5086 return $categories_struct;
5090 * Retrieve post categories.
5094 * @param array $args Method parameters.
5097 function mt_getPostCategories($args) {
5099 $this->escape($args);
5101 $post_ID = (int) $args[0];
5102 $username = $args[1];
5103 $password = $args[2];
5105 if ( !$user = $this->login($username, $password) )
5106 return $this->error;
5108 if ( ! get_post( $post_ID ) )
5109 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5111 if ( !current_user_can( 'edit_post', $post_ID ) )
5112 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
5114 do_action('xmlrpc_call', 'mt.getPostCategories');
5116 $categories = array();
5117 $catids = wp_get_post_categories(intval($post_ID));
5118 // first listed category will be the primary category
5120 foreach ( $catids as $catid ) {
5121 $categories[] = array(
5122 'categoryName' => get_cat_name($catid),
5123 'categoryId' => (string) $catid,
5124 'isPrimary' => $isPrimary
5133 * Sets categories for a post.
5137 * @param array $args Method parameters.
5138 * @return bool True on success.
5140 function mt_setPostCategories($args) {
5142 $this->escape($args);
5144 $post_ID = (int) $args[0];
5145 $username = $args[1];
5146 $password = $args[2];
5147 $categories = $args[3];
5149 if ( !$user = $this->login($username, $password) )
5150 return $this->error;
5152 do_action('xmlrpc_call', 'mt.setPostCategories');
5154 if ( ! get_post( $post_ID ) )
5155 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5157 if ( !current_user_can('edit_post', $post_ID) )
5158 return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
5161 foreach ( $categories as $cat ) {
5162 $catids[] = $cat['categoryId'];
5165 wp_set_post_categories($post_ID, $catids);
5171 * Retrieve an array of methods supported by this server.
5175 * @param array $args Method parameters.
5178 function mt_supportedMethods($args) {
5180 do_action('xmlrpc_call', 'mt.supportedMethods');
5182 $supported_methods = array();
5183 foreach ( $this->methods as $key => $value ) {
5184 $supported_methods[] = $key;
5187 return $supported_methods;
5191 * Retrieve an empty array because we don't support per-post text filters.
5195 * @param array $args Method parameters.
5197 function mt_supportedTextFilters($args) {
5198 do_action('xmlrpc_call', 'mt.supportedTextFilters');
5199 return apply_filters('xmlrpc_text_filters', array());
5203 * Retrieve trackbacks sent to a given post.
5207 * @param array $args Method parameters.
5210 function mt_getTrackbackPings($args) {
5214 $post_ID = intval($args);
5216 do_action('xmlrpc_call', 'mt.getTrackbackPings');
5218 $actual_post = get_post($post_ID, ARRAY_A);
5220 if ( !$actual_post )
5221 return new IXR_Error(404, __('Sorry, no such post.'));
5223 $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) );
5228 $trackback_pings = array();
5229 foreach ( $comments as $comment ) {
5230 if ( 'trackback' == $comment->comment_type ) {
5231 $content = $comment->comment_content;
5232 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
5233 $trackback_pings[] = array(
5234 'pingTitle' => $title,
5235 'pingURL' => $comment->comment_author_url,
5236 'pingIP' => $comment->comment_author_IP
5241 return $trackback_pings;
5245 * Sets a post's publish status to 'publish'.
5249 * @param array $args Method parameters.
5252 function mt_publishPost($args) {
5254 $this->escape($args);
5256 $post_ID = (int) $args[0];
5257 $username = $args[1];
5258 $password = $args[2];
5260 if ( !$user = $this->login($username, $password) )
5261 return $this->error;
5263 do_action('xmlrpc_call', 'mt.publishPost');
5265 $postdata = get_post($post_ID, ARRAY_A);
5267 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5269 if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
5270 return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
5272 $postdata['post_status'] = 'publish';
5275 $cats = wp_get_post_categories($post_ID);
5276 $postdata['post_category'] = $cats;
5277 $this->escape($postdata);
5279 $result = wp_update_post($postdata);
5284 /* PingBack functions
5285 * specs on www.hixie.ch/specs/pingback/pingback
5289 * Retrieves a pingback and registers it.
5293 * @param array $args Method parameters.
5296 function pingback_ping($args) {
5299 do_action('xmlrpc_call', 'pingback.ping');
5301 $this->escape($args);
5303 $pagelinkedfrom = $args[0];
5304 $pagelinkedto = $args[1];
5308 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
5309 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
5310 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
5312 $pagelinkedfrom = apply_filters( 'pingback_ping_source_uri', $pagelinkedfrom, $pagelinkedto );
5313 if ( ! $pagelinkedfrom )
5314 return $this->pingback_error( 0, __( 'A valid URL was not provided.' ) );
5316 // Check if the page linked to is in our site
5317 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
5319 return $this->pingback_error( 0, __( 'Is there no link to us?' ) );
5321 // let's find which post is linked to
5322 // FIXME: does url_to_postid() cover all these cases already?
5323 // if so, then let's use it and drop the old code.
5324 $urltest = parse_url($pagelinkedto);
5325 if ( $post_ID = url_to_postid($pagelinkedto) ) {
5326 $way = 'url_to_postid()';
5327 } elseif ( preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
5328 // the path defines the post_ID (archives/p/XXXX)
5329 $blah = explode('/', $match[0]);
5330 $post_ID = (int) $blah[1];
5331 $way = 'from the path';
5332 } elseif ( isset( $urltest['query'] ) && preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
5333 // the querystring defines the post_ID (?p=XXXX)
5334 $blah = explode('=', $match[0]);
5335 $post_ID = (int) $blah[1];
5336 $way = 'from the querystring';
5337 } elseif ( isset($urltest['fragment']) ) {
5338 // an #anchor is there, it's either...
5339 if ( intval($urltest['fragment']) ) {
5340 // ...an integer #XXXX (simplest case)
5341 $post_ID = (int) $urltest['fragment'];
5342 $way = 'from the fragment (numeric)';
5343 } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
5344 // ...a post id in the form 'post-###'
5345 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
5346 $way = 'from the fragment (post-###)';
5347 } elseif ( is_string($urltest['fragment']) ) {
5348 // ...or a string #title, a little more complicated
5349 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
5350 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", like_escape( $title ) );
5351 if (! ($post_ID = $wpdb->get_var($sql)) ) {
5352 // returning unknown error '0' is better than die()ing
5353 return $this->pingback_error( 0, '' );
5355 $way = 'from the fragment (title)';
5358 // TODO: Attempt to extract a post ID from the given URL
5359 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.' ) );
5361 $post_ID = (int) $post_ID;
5363 $post = get_post($post_ID);
5365 if ( !$post ) // Post_ID not found
5366 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.' ) );
5368 if ( $post_ID == url_to_postid($pagelinkedfrom) )
5369 return $this->pingback_error( 0, __( 'The source URL and the target URL cannot both point to the same resource.' ) );
5371 // Check if pings are on
5372 if ( !pings_open($post) )
5373 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.' ) );
5375 // Let's check that the remote site didn't already pingback this entry
5376 if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
5377 return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
5379 // very stupid, but gives time to the 'from' server to publish !
5382 // Let's check the remote site
5383 $linea = wp_remote_retrieve_body( wp_remote_get( $pagelinkedfrom, array( 'timeout' => 10, 'redirection' => 0 ) ) );
5385 return $this->pingback_error( 16, __( 'The source URL does not exist.' ) );
5387 $linea = apply_filters('pre_remote_source', $linea, $pagelinkedto);
5389 // Work around bug in strip_tags():
5390 $linea = str_replace('<!DOC', '<DOC', $linea);
5391 $linea = preg_replace( '/[\s\r\n\t]+/', ' ', $linea ); // normalize spaces
5392 $linea = preg_replace( "/<\/*(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
5394 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
5395 $title = $matchtitle[1];
5396 if ( empty( $title ) )
5397 return $this->pingback_error( 32, __('We cannot find a title on that page.' ) );
5399 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
5401 $p = explode( "\n\n", $linea );
5403 $preg_target = preg_quote($pagelinkedto, '|');
5405 foreach ( $p as $para ) {
5406 if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
5407 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
5409 // If the URL isn't in a link context, keep looking
5410 if ( empty($context) )
5413 // We're going to use this fake tag to mark the context in a bit
5414 // the marker is needed in case the link text appears more than once in the paragraph
5415 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
5417 // prevent really long link text
5418 if ( strlen($context[1]) > 100 )
5419 $context[1] = substr($context[1], 0, 100) . '...';
5421 $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
5422 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
5423 $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
5424 $excerpt = trim($excerpt);
5425 $preg_marker = preg_quote($marker, '|');
5426 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
5427 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
5432 if ( empty($context) ) // Link to target not found
5433 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.' ) );
5435 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
5437 $context = '[...] ' . esc_html( $excerpt ) . ' [...]';
5438 $pagelinkedfrom = $wpdb->escape( $pagelinkedfrom );
5440 $comment_post_ID = (int) $post_ID;
5441 $comment_author = $title;
5442 $comment_author_email = '';
5443 $this->escape($comment_author);
5444 $comment_author_url = $pagelinkedfrom;
5445 $comment_content = $context;
5446 $this->escape($comment_content);
5447 $comment_type = 'pingback';
5449 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
5451 $comment_ID = wp_new_comment($commentdata);
5452 do_action('pingback_post', $comment_ID);
5454 return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
5458 * Retrieve array of URLs that pingbacked the given URL.
5460 * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
5464 * @param array $args Method parameters.
5467 function pingback_extensions_getPingbacks($args) {
5471 do_action('xmlrpc_call', 'pingback.extensions.getPingbacks');
5473 $this->escape($args);
5477 $post_ID = url_to_postid($url);
5479 // We aren't sure that the resource is available and/or pingback enabled
5480 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.' ) );
5483 $actual_post = get_post($post_ID, ARRAY_A);
5485 if ( !$actual_post ) {
5486 // No such post = resource not found
5487 return $this->pingback_error( 32, __('The specified target URL does not exist.' ) );
5490 $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) );
5495 $pingbacks = array();
5496 foreach ( $comments as $comment ) {
5497 if ( 'pingback' == $comment->comment_type )
5498 $pingbacks[] = $comment->comment_author_url;
5504 protected function pingback_error( $code, $message ) {
5505 return apply_filters( 'xmlrpc_pingback_error', new IXR_Error( $code, $message ) );