3 * XML-RPC protocol support for WordPress
9 * WordPress XMLRPC server implementation.
11 * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
12 * pingback. Additional WordPress API for managing comments, pages, posts,
15 * Since WordPress 2.6.0, WordPress XMLRPC server can be disabled in the
16 * administration panels.
19 * @subpackage Publishing
22 class wp_xmlrpc_server extends IXR_Server {
25 * Register all of the XMLRPC methods that XMLRPC server understands.
27 * Sets up server and method property. Passes XMLRPC
28 * methods through the 'xmlrpc_methods' filter to allow plugins to extend
29 * or replace XMLRPC methods.
33 * @return wp_xmlrpc_server
35 function __construct() {
36 $this->methods = array(
38 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
39 'wp.newPost' => 'this:wp_newPost',
40 'wp.editPost' => 'this:wp_editPost',
41 'wp.deletePost' => 'this:wp_deletePost',
42 'wp.getPost' => 'this:wp_getPost',
43 'wp.getPosts' => 'this:wp_getPosts',
44 'wp.newTerm' => 'this:wp_newTerm',
45 'wp.editTerm' => 'this:wp_editTerm',
46 'wp.deleteTerm' => 'this:wp_deleteTerm',
47 'wp.getTerm' => 'this:wp_getTerm',
48 'wp.getTerms' => 'this:wp_getTerms',
49 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
50 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
51 'wp.getPage' => 'this:wp_getPage',
52 'wp.getPages' => 'this:wp_getPages',
53 'wp.newPage' => 'this:wp_newPage',
54 'wp.deletePage' => 'this:wp_deletePage',
55 'wp.editPage' => 'this:wp_editPage',
56 'wp.getPageList' => 'this:wp_getPageList',
57 'wp.getAuthors' => 'this:wp_getAuthors',
58 'wp.getCategories' => 'this:mw_getCategories', // Alias
59 'wp.getTags' => 'this:wp_getTags',
60 'wp.newCategory' => 'this:wp_newCategory',
61 'wp.deleteCategory' => 'this:wp_deleteCategory',
62 'wp.suggestCategories' => 'this:wp_suggestCategories',
63 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
64 'wp.getCommentCount' => 'this:wp_getCommentCount',
65 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
66 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
67 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
68 'wp.getOptions' => 'this:wp_getOptions',
69 'wp.setOptions' => 'this:wp_setOptions',
70 'wp.getComment' => 'this:wp_getComment',
71 'wp.getComments' => 'this:wp_getComments',
72 'wp.deleteComment' => 'this:wp_deleteComment',
73 'wp.editComment' => 'this:wp_editComment',
74 'wp.newComment' => 'this:wp_newComment',
75 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
76 'wp.getMediaItem' => 'this:wp_getMediaItem',
77 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
78 'wp.getPostFormats' => 'this:wp_getPostFormats',
79 'wp.getPostType' => 'this:wp_getPostType',
80 'wp.getPostTypes' => 'this:wp_getPostTypes',
83 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
84 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
85 'blogger.getPost' => 'this:blogger_getPost',
86 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
87 'blogger.getTemplate' => 'this:blogger_getTemplate',
88 'blogger.setTemplate' => 'this:blogger_setTemplate',
89 'blogger.newPost' => 'this:blogger_newPost',
90 'blogger.editPost' => 'this:blogger_editPost',
91 'blogger.deletePost' => 'this:blogger_deletePost',
93 // MetaWeblog API (with MT extensions to structs)
94 'metaWeblog.newPost' => 'this:mw_newPost',
95 'metaWeblog.editPost' => 'this:mw_editPost',
96 'metaWeblog.getPost' => 'this:mw_getPost',
97 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
98 'metaWeblog.getCategories' => 'this:mw_getCategories',
99 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
101 // MetaWeblog API aliases for Blogger API
102 // see http://www.xmlrpc.com/stories/storyReader$2460
103 'metaWeblog.deletePost' => 'this:blogger_deletePost',
104 'metaWeblog.getTemplate' => 'this:blogger_getTemplate',
105 'metaWeblog.setTemplate' => 'this:blogger_setTemplate',
106 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
109 'mt.getCategoryList' => 'this:mt_getCategoryList',
110 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
111 'mt.getPostCategories' => 'this:mt_getPostCategories',
112 'mt.setPostCategories' => 'this:mt_setPostCategories',
113 'mt.supportedMethods' => 'this:mt_supportedMethods',
114 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
115 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
116 'mt.publishPost' => 'this:mt_publishPost',
119 'pingback.ping' => 'this:pingback_ping',
120 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
122 'demo.sayHello' => 'this:sayHello',
123 'demo.addTwoNumbers' => 'this:addTwoNumbers'
126 $this->initialise_blog_option_info();
127 $this->methods = apply_filters('xmlrpc_methods', $this->methods);
130 function serve_request() {
131 $this->IXR_Server($this->methods);
135 * Test XMLRPC API by saying, "Hello!" to client.
139 * @param array $args Method Parameters.
142 function sayHello($args) {
147 * Test XMLRPC API by adding two numbers for client.
151 * @param array $args Method Parameters.
154 function addTwoNumbers($args) {
157 return $number1 + $number2;
161 * Check user's credentials.
165 * @param string $user_login User's username.
166 * @param string $user_pass User's password.
167 * @return bool Whether authentication passed.
168 * @deprecated use wp_xmlrpc_server::login
169 * @see wp_xmlrpc_server::login
171 function login_pass_ok($user_login, $user_pass) {
172 if ( !get_option( 'enable_xmlrpc' ) ) {
173 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'), admin_url('options-writing.php') ) );
177 if (!user_pass_ok($user_login, $user_pass)) {
178 $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
189 * @param string $username User's username.
190 * @param string $password User's password.
191 * @return mixed WP_User object if authentication passed, false otherwise
193 function login($username, $password) {
194 if ( !get_option( 'enable_xmlrpc' ) ) {
195 $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'), admin_url('options-writing.php') ) );
199 $user = wp_authenticate($username, $password);
201 if (is_wp_error($user)) {
202 $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
206 wp_set_current_user( $user->ID );
211 * Sanitize string or array of strings for database.
215 * @param string|array $array Sanitize single string or array of strings.
216 * @return string|array Type matches $array and sanitized for the database.
218 function escape(&$array) {
221 if (!is_array($array)) {
222 return($wpdb->escape($array));
224 foreach ( (array) $array as $k => $v ) {
225 if ( is_array($v) ) {
226 $this->escape($array[$k]);
227 } else if ( is_object($v) ) {
230 $array[$k] = $wpdb->escape($v);
237 * Retrieve custom fields for post.
241 * @param int $post_id Post ID.
242 * @return array Custom fields, if exist.
244 function get_custom_fields($post_id) {
245 $post_id = (int) $post_id;
247 $custom_fields = array();
249 foreach ( (array) has_meta($post_id) as $meta ) {
250 // Don't expose protected fields.
251 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
254 $custom_fields[] = array(
255 "id" => $meta['meta_id'],
256 "key" => $meta['meta_key'],
257 "value" => $meta['meta_value']
261 return $custom_fields;
265 * Set custom fields for post.
269 * @param int $post_id Post ID.
270 * @param array $fields Custom fields.
272 function set_custom_fields($post_id, $fields) {
273 $post_id = (int) $post_id;
275 foreach ( (array) $fields as $meta ) {
276 if ( isset($meta['id']) ) {
277 $meta['id'] = (int) $meta['id'];
278 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
279 if ( isset($meta['key']) ) {
280 $meta['key'] = stripslashes( $meta['key'] );
281 if ( $meta['key'] != $pmeta->meta_key )
283 $meta['value'] = stripslashes_deep( $meta['value'] );
284 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
285 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
286 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
287 delete_metadata_by_mid( 'post', $meta['id'] );
289 } elseif ( current_user_can( 'add_post_meta', $post_id, stripslashes( $meta['key'] ) ) ) {
290 add_post_meta( $post_id, $meta['key'], $meta['value'] );
296 * Set up blog options property.
298 * Passes property through 'xmlrpc_blog_options' filter.
302 function initialise_blog_option_info() {
305 $this->blog_options = array(
307 'software_name' => array(
308 'desc' => __( 'Software Name' ),
310 'value' => 'WordPress'
312 'software_version' => array(
313 'desc' => __( 'Software Version' ),
315 'value' => $wp_version
318 'desc' => __( 'Site URL' ),
320 'option' => 'siteurl'
322 'image_default_link_type' => array(
323 'desc' => __( 'Image default link type' ),
325 'option' => 'image_default_link_type'
327 'image_default_size' => array(
328 'desc' => __( 'Image default size' ),
330 'option' => 'image_default_size'
332 'image_default_align' => array(
333 'desc' => __( 'Image default align' ),
335 'option' => 'image_default_align'
338 'desc' => __( 'Template' ),
340 'option' => 'template'
342 'stylesheet' => array(
343 'desc' => __( 'Stylesheet' ),
345 'option' => 'stylesheet'
347 'post_thumbnail' => array(
348 'desc' => __('Post Thumbnail'),
350 'value' => current_theme_supports( 'post-thumbnails' )
354 'time_zone' => array(
355 'desc' => __( 'Time Zone' ),
357 'option' => 'gmt_offset'
359 'blog_title' => array(
360 'desc' => __( 'Site Title' ),
362 'option' => 'blogname'
364 'blog_tagline' => array(
365 'desc' => __( 'Site Tagline' ),
367 'option' => 'blogdescription'
369 'date_format' => array(
370 'desc' => __( 'Date Format' ),
372 'option' => 'date_format'
374 'time_format' => array(
375 'desc' => __( 'Time Format' ),
377 'option' => 'time_format'
379 'users_can_register' => array(
380 'desc' => __( 'Allow new users to sign up' ),
382 'option' => 'users_can_register'
384 'thumbnail_size_w' => array(
385 'desc' => __( 'Thumbnail Width' ),
387 'option' => 'thumbnail_size_w'
389 'thumbnail_size_h' => array(
390 'desc' => __( 'Thumbnail Height' ),
392 'option' => 'thumbnail_size_h'
394 'thumbnail_crop' => array(
395 'desc' => __( 'Crop thumbnail to exact dimensions' ),
397 'option' => 'thumbnail_crop'
399 'medium_size_w' => array(
400 'desc' => __( 'Medium size image width' ),
402 'option' => 'medium_size_w'
404 'medium_size_h' => array(
405 'desc' => __( 'Medium size image height' ),
407 'option' => 'medium_size_h'
409 'large_size_w' => array(
410 'desc' => __( 'Large size image width' ),
412 'option' => 'large_size_w'
414 'large_size_h' => array(
415 'desc' => __( 'Large size image height' ),
417 'option' => 'large_size_h'
419 'default_comment_status' => array(
420 'desc' => __( 'Allow people to post comments on new articles' ),
422 'option' => 'default_comment_status'
424 'default_ping_status' => array(
425 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
427 'option' => 'default_ping_status'
431 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
435 * Retrieve the blogs of the user.
439 * @param array $args Method parameters. Contains:
442 * @return array. Contains:
447 * - 'xmlrpc' - url of xmlrpc endpoint
449 function wp_getUsersBlogs( $args ) {
450 global $current_site;
451 // If this isn't on WPMU then just use blogger_getUsersBlogs
452 if ( !is_multisite() ) {
453 array_unshift( $args, 1 );
454 return $this->blogger_getUsersBlogs( $args );
457 $this->escape( $args );
459 $username = $args[0];
460 $password = $args[1];
462 if ( !$user = $this->login($username, $password) )
465 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
467 $blogs = (array) get_blogs_of_user( $user->ID );
470 foreach ( $blogs as $blog ) {
471 // Don't include blogs that aren't hosted at this site
472 if ( $blog->site_id != $current_site->id )
475 $blog_id = $blog->userblog_id;
476 switch_to_blog($blog_id);
477 $is_admin = current_user_can('manage_options');
480 'isAdmin' => $is_admin,
481 'url' => get_option( 'home' ) . '/',
482 'blogid' => (string) $blog_id,
483 'blogName' => get_option( 'blogname' ),
484 'xmlrpc' => site_url( 'xmlrpc.php' )
487 restore_current_blog();
494 * Checks if the method received at least the minimum number of arguments.
498 * @param string|array $args Sanitize single string or array of strings.
499 * @param int $count Minimum number of arguments.
500 * @return boolean if $args contains at least $count arguments.
502 protected function minimum_args( $args, $count ) {
503 if ( count( $args ) < $count ) {
504 $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
512 * Prepares taxonomy data for return in an XML-RPC object.
516 * @param object $taxonomy The unprepared taxonomy data
517 * @param array $fields The subset of taxonomy fields to return
518 * @return array The prepared taxonomy data
520 protected function _prepare_taxonomy( $taxonomy, $fields ) {
522 'name' => $taxonomy->name,
523 'label' => $taxonomy->label,
524 'hierarchical' => (bool) $taxonomy->hierarchical,
525 'public' => (bool) $taxonomy->public,
526 'show_ui' => (bool) $taxonomy->show_ui,
527 '_builtin' => (bool) $taxonomy->_builtin,
530 if ( in_array( 'labels', $fields ) )
531 $_taxonomy['labels'] = (array) $taxonomy->labels;
533 if ( in_array( 'cap', $fields ) )
534 $_taxonomy['cap'] = (array) $taxonomy->cap;
536 if ( in_array( 'object_type', $fields ) )
537 $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
539 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
543 * Prepares term data for return in an XML-RPC object.
547 * @param array|object $term The unprepared term data
548 * @return array The prepared term data
550 protected function _prepare_term( $term ) {
552 if ( ! is_array( $_term) )
553 $_term = get_object_vars( $_term );
555 // For Intergers which may be largeer than XMLRPC supports ensure we return strings.
556 $_term['term_id'] = strval( $_term['term_id'] );
557 $_term['term_group'] = strval( $_term['term_group'] );
558 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
559 $_term['parent'] = strval( $_term['parent'] );
561 // Count we are happy to return as an Integer because people really shouldn't use Terms that much.
562 $_term['count'] = intval( $_term['count'] );
564 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
568 * Convert a WordPress date string to an IXR_Date object.
572 * @param string $date
575 protected function _convert_date( $date ) {
576 if ( $date === '0000-00-00 00:00:00' ) {
577 return new IXR_Date( '00000000T00:00:00Z' );
579 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
583 * Convert a WordPress GMT date string to an IXR_Date object.
587 * @param string $date_gmt
588 * @param string $date
591 protected function _convert_date_gmt( $date_gmt, $date ) {
592 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
593 return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
595 return $this->_convert_date( $date_gmt );
599 * Prepares post data for return in an XML-RPC object.
603 * @param array $post The unprepared post data
604 * @param array $fields The subset of post type fields to return
605 * @return array The prepared post data
607 protected function _prepare_post( $post, $fields ) {
608 // holds the data for this post. built up based on $fields
609 $_post = array( 'post_id' => strval( $post['ID'] ) );
611 // prepare common post fields
612 $post_fields = array(
613 'post_title' => $post['post_title'],
614 'post_date' => $this->_convert_date( $post['post_date'] ),
615 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
616 'post_modified' => $this->_convert_date( $post['post_modified'] ),
617 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
618 'post_status' => $post['post_status'],
619 'post_type' => $post['post_type'],
620 'post_name' => $post['post_name'],
621 'post_author' => $post['post_author'],
622 'post_password' => $post['post_password'],
623 'post_excerpt' => $post['post_excerpt'],
624 'post_content' => $post['post_content'],
625 'link' => post_permalink( $post['ID'] ),
626 'comment_status' => $post['comment_status'],
627 'ping_status' => $post['ping_status'],
628 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
632 $post_fields['post_thumbnail'] = array();
633 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
634 if ( $thumbnail_id ) {
635 $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
636 $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
639 // Consider future posts as published
640 if ( $post_fields['post_status'] === 'future' )
641 $post_fields['post_status'] = 'publish';
643 // Fill in blank post format
644 $post_fields['post_format'] = get_post_format( $post['ID'] );
645 if ( empty( $post_fields['post_format'] ) )
646 $post_fields['post_format'] = 'standard';
648 // Merge requested $post_fields fields into $_post
649 if ( in_array( 'post', $fields ) ) {
650 $_post = array_merge( $_post, $post_fields );
652 $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
653 $_post = array_merge( $_post, $requested_fields );
656 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
658 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
659 $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
660 $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
661 $_post['terms'] = array();
662 foreach ( $terms as $term ) {
663 $_post['terms'][] = $this->_prepare_term( $term );
667 if ( in_array( 'custom_fields', $fields ) )
668 $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
670 if ( in_array( 'enclosure', $fields ) ) {
671 $_post['enclosure'] = array();
672 $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
673 if ( ! empty( $enclosures ) ) {
674 $encdata = explode( "\n", $enclosures[0] );
675 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
676 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
677 $_post['enclosure']['type'] = trim( $encdata[2] );
681 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
685 * Prepares post data for return in an XML-RPC object.
689 * @param object $post_type Post type object
690 * @param array $fields The subset of post fields to return
691 * @return array The prepared post type data
693 protected function _prepare_post_type( $post_type, $fields ) {
695 'name' => $post_type->name,
696 'label' => $post_type->label,
697 'hierarchical' => (bool) $post_type->hierarchical,
698 'public' => (bool) $post_type->public,
699 'show_ui' => (bool) $post_type->show_ui,
700 '_builtin' => (bool) $post_type->_builtin,
701 'has_archive' => (bool) $post_type->has_archive,
702 'supports' => get_all_post_type_supports( $post_type->name ),
705 if ( in_array( 'labels', $fields ) ) {
706 $_post_type['labels'] = (array) $post_type->labels;
709 if ( in_array( 'cap', $fields ) ) {
710 $_post_type['cap'] = (array) $post_type->cap;
711 $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
714 if ( in_array( 'menu', $fields ) ) {
715 $_post_type['menu_position'] = (int) $post_type->menu_position;
716 $_post_type['menu_icon'] = $post_type->menu_icon;
717 $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
720 if ( in_array( 'taxonomies', $fields ) )
721 $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
723 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
727 * Prepares media item data for return in an XML-RPC object.
731 * @param object $media_item The unprepared media item data
732 * @param string $thumbnail_size The image size to use for the thumbnail URL
733 * @return array The prepared media item data
735 protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
736 $_media_item = array(
737 'attachment_id' => strval( $media_item->ID ),
738 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
739 'parent' => $media_item->post_parent,
740 'link' => wp_get_attachment_url( $media_item->ID ),
741 'title' => $media_item->post_title,
742 'caption' => $media_item->post_excerpt,
743 'description' => $media_item->post_content,
744 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
747 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
748 if ( $thumbnail_src )
749 $_media_item['thumbnail'] = $thumbnail_src[0];
751 $_media_item['thumbnail'] = $_media_item['link'];
753 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
757 * Prepares page data for return in an XML-RPC object.
761 * @param object $page The unprepared page data
762 * @return array The prepared page data
764 protected function _prepare_page( $page ) {
765 // Get all of the page content and link.
766 $full_page = get_extended( $page->post_content );
767 $link = post_permalink( $page->ID );
769 // Get info the page parent if there is one.
771 if ( ! empty( $page->post_parent ) ) {
772 $parent = get_page( $page->post_parent );
773 $parent_title = $parent->post_title;
776 // Determine comment and ping settings.
777 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
778 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
781 $page_date = $this->_convert_date( $page->post_date );
782 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
784 // Pull the categories info together.
785 $categories = array();
786 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
787 $categories[] = get_cat_name( $cat_id );
790 // Get the author info.
791 $author = get_userdata( $page->post_author );
793 $page_template = get_page_template_slug( $page->ID );
794 if ( empty( $page_template ) )
795 $page_template = 'default';
798 'dateCreated' => $page_date,
799 'userid' => $page->post_author,
800 'page_id' => $page->ID,
801 'page_status' => $page->post_status,
802 'description' => $full_page['main'],
803 'title' => $page->post_title,
805 'permaLink' => $link,
806 'categories' => $categories,
807 'excerpt' => $page->post_excerpt,
808 'text_more' => $full_page['extended'],
809 'mt_allow_comments' => $allow_comments,
810 'mt_allow_pings' => $allow_pings,
811 'wp_slug' => $page->post_name,
812 'wp_password' => $page->post_password,
813 'wp_author' => $author->display_name,
814 'wp_page_parent_id' => $page->post_parent,
815 'wp_page_parent_title' => $parent_title,
816 'wp_page_order' => $page->menu_order,
817 'wp_author_id' => (string) $author->ID,
818 'wp_author_display_name' => $author->display_name,
819 'date_created_gmt' => $page_date_gmt,
820 'custom_fields' => $this->get_custom_fields( $page->ID ),
821 'wp_page_template' => $page_template
824 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
828 * Prepares comment data for return in an XML-RPC object.
832 * @param object $comment The unprepared comment data
833 * @return array The prepared comment data
835 protected function _prepare_comment( $comment ) {
837 $comment_date = $this->_convert_date( $comment->comment_date );
838 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
840 if ( '0' == $comment->comment_approved )
841 $comment_status = 'hold';
842 else if ( 'spam' == $comment->comment_approved )
843 $comment_status = 'spam';
844 else if ( '1' == $comment->comment_approved )
845 $comment_status = 'approve';
847 $comment_status = $comment->comment_approved;
850 'date_created_gmt' => $comment_date_gmt,
851 'user_id' => $comment->user_id,
852 'comment_id' => $comment->comment_ID,
853 'parent' => $comment->comment_parent,
854 'status' => $comment_status,
855 'content' => $comment->comment_content,
856 'link' => get_comment_link($comment),
857 'post_id' => $comment->comment_post_ID,
858 'post_title' => get_the_title($comment->comment_post_ID),
859 'author' => $comment->comment_author,
860 'author_url' => $comment->comment_author_url,
861 'author_email' => $comment->comment_author_email,
862 'author_ip' => $comment->comment_author_IP,
863 'type' => $comment->comment_type,
866 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
870 * Create a new post for any registered post type.
874 * @param array $args Method parameters. Contains:
878 * - array $content_struct
879 * $content_struct can contain:
880 * - post_type (default: 'post')
881 * - post_status (default: 'draft')
886 * - post_date_gmt | post_date
889 * - comment_status - can be 'open' | 'closed'
890 * - ping_status - can be 'open' | 'closed'
892 * - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
893 * - custom_fields - array, with each element containing 'key' and 'value'
894 * - terms - array, with taxonomy names as keys and arrays of term IDs as values
895 * - terms_names - array, with taxonomy names as keys and arrays of term names as values
897 * - any other fields supported by wp_insert_post()
898 * @return string post_id
900 function wp_newPost( $args ) {
901 if ( ! $this->minimum_args( $args, 4 ) )
904 $this->escape( $args );
906 $blog_id = (int) $args[0];
907 $username = $args[1];
908 $password = $args[2];
909 $content_struct = $args[3];
911 if ( ! $user = $this->login( $username, $password ) )
914 do_action( 'xmlrpc_call', 'wp.newPost' );
916 unset( $content_struct['ID'] );
918 return $this->_insert_post( $user, $content_struct );
922 * Helper method for filtering out elements from an array.
926 * @param int $count Number to compare to one.
928 private function _is_greater_than_one( $count ) {
933 * Helper method for wp_newPost and wp_editPost, containing shared logic.
936 * @uses wp_insert_post()
938 * @param WP_User $user The post author if post_author isn't set in $content_struct.
939 * @param array $content_struct Post data to insert.
941 protected function _insert_post( $user, $content_struct ) {
942 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
943 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
945 $post_data = wp_parse_args( $content_struct, $defaults );
947 $post_type = get_post_type_object( $post_data['post_type'] );
949 return new IXR_Error( 403, __( 'Invalid post type' ) );
951 $update = ! empty( $post_data['ID'] );
954 if ( ! get_post( $post_data['ID'] ) )
955 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
956 if ( ! current_user_can( $post_type->cap->edit_post, $post_data['ID'] ) )
957 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
958 if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
959 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
961 if ( ! current_user_can( $post_type->cap->edit_posts ) )
962 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
965 switch ( $post_data['post_status'] ) {
970 if ( ! current_user_can( $post_type->cap->publish_posts ) )
971 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
975 if ( ! current_user_can( $post_type->cap->publish_posts ) )
976 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
979 $post_data['post_status'] = 'draft';
983 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
984 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
986 $post_data['post_author'] = absint( $post_data['post_author'] );
987 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
988 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
989 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
991 $author = get_userdata( $post_data['post_author'] );
994 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
996 $post_data['post_author'] = $user->ID;
999 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1000 unset( $post_data['comment_status'] );
1002 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1003 unset( $post_data['ping_status'] );
1005 // Do some timestamp voodoo
1006 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1007 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1008 $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1009 } elseif ( ! empty( $post_data['post_date'] ) ) {
1010 $dateCreated = $post_data['post_date']->getIso();
1013 if ( ! empty( $dateCreated ) ) {
1014 $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1015 $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1018 if ( ! isset( $post_data['ID'] ) )
1019 $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1020 $post_ID = $post_data['ID'];
1022 if ( $post_data['post_type'] == 'post' ) {
1023 // Private and password-protected posts cannot be stickied.
1024 if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
1025 // Error if the client tried to stick the post, otherwise, silently unstick.
1026 if ( ! empty( $post_data['sticky'] ) )
1027 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1029 unstick_post( $post_ID );
1030 } elseif ( isset( $post_data['sticky'] ) ) {
1031 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1032 return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1033 if ( $post_data['sticky'] )
1034 stick_post( $post_ID );
1036 unstick_post( $post_ID );
1040 if ( isset( $post_data['post_thumbnail'] ) ) {
1041 // empty value deletes, non-empty value adds/updates
1042 if ( ! $post_data['post_thumbnail'] )
1043 delete_post_thumbnail( $post_ID );
1044 elseif ( ! set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] ) )
1045 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1046 unset( $content_struct['post_thumbnail'] );
1049 if ( isset( $post_data['custom_fields'] ) )
1050 $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1052 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1053 $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1055 // accumulate term IDs from terms and terms_names
1058 // first validate the terms specified by ID
1059 if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1060 $taxonomies = array_keys( $post_data['terms'] );
1062 // validating term ids
1063 foreach ( $taxonomies as $taxonomy ) {
1064 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1065 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1067 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1068 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1070 $term_ids = $post_data['terms'][$taxonomy];
1071 foreach ( $term_ids as $term_id ) {
1072 $term = get_term_by( 'id', $term_id, $taxonomy );
1075 return new IXR_Error( 403, __( 'Invalid term ID' ) );
1077 $terms[$taxonomy][] = (int) $term_id;
1082 // now validate terms specified by name
1083 if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1084 $taxonomies = array_keys( $post_data['terms_names'] );
1086 foreach ( $taxonomies as $taxonomy ) {
1087 if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1088 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1090 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1091 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1093 // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
1094 $ambiguous_terms = array();
1095 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1096 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1098 // count the number of terms with the same name
1099 $tax_term_names_count = array_count_values( $tax_term_names );
1101 // filter out non-ambiguous term names
1102 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1104 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1107 $term_names = $post_data['terms_names'][$taxonomy];
1108 foreach ( $term_names as $term_name ) {
1109 if ( in_array( $term_name, $ambiguous_terms ) )
1110 return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1112 $term = get_term_by( 'name', $term_name, $taxonomy );
1115 // term doesn't exist, so check that the user is allowed to create new terms
1116 if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1117 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1119 // create the new term
1120 $term_info = wp_insert_term( $term_name, $taxonomy );
1121 if ( is_wp_error( $term_info ) )
1122 return new IXR_Error( 500, $term_info->get_error_message() );
1124 $terms[$taxonomy][] = (int) $term_info['term_id'];
1126 $terms[$taxonomy][] = (int) $term->term_id;
1132 $post_data['tax_input'] = $terms;
1133 unset( $post_data['terms'], $post_data['terms_names'] );
1135 // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
1136 unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1139 if ( isset( $post_data['post_format'] ) ) {
1140 $format = set_post_format( $post_ID, $post_data['post_format'] );
1142 if ( is_wp_error( $format ) )
1143 return new IXR_Error( 500, $format->get_error_message() );
1145 unset( $post_data['post_format'] );
1148 // Handle enclosures
1149 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1150 $this->add_enclosure_if_new( $post_ID, $enclosure );
1152 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1154 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1156 $post_ID = wp_insert_post( $post_data, true );
1157 if ( is_wp_error( $post_ID ) )
1158 return new IXR_Error( 500, $post_ID->get_error_message() );
1161 return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1163 return strval( $post_ID );
1167 * Edit a post for any registered post type.
1169 * The $content_struct parameter only needs to contain fields that
1170 * should be changed. All other fields will retain their existing values.
1174 * @param array $args Method parameters. Contains:
1176 * - string $username
1177 * - string $password
1179 * - array $content_struct
1180 * @return true on success
1182 function wp_editPost( $args ) {
1183 if ( ! $this->minimum_args( $args, 5 ) )
1184 return $this->error;
1186 $this->escape( $args );
1188 $blog_id = (int) $args[0];
1189 $username = $args[1];
1190 $password = $args[2];
1191 $post_id = (int) $args[3];
1192 $content_struct = $args[4];
1194 if ( ! $user = $this->login( $username, $password ) )
1195 return $this->error;
1197 do_action( 'xmlrpc_call', 'wp.editPost' );
1199 $post = get_post( $post_id, ARRAY_A );
1201 if ( empty( $post['ID'] ) )
1202 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1204 // convert the date field back to IXR form
1205 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1207 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1208 // since _insert_post will ignore the non-GMT date if the GMT date is set
1209 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1210 unset( $post['post_date_gmt'] );
1212 $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1214 $this->escape( $post );
1215 $merged_content_struct = array_merge( $post, $content_struct );
1217 $retval = $this->_insert_post( $user, $merged_content_struct );
1218 if ( $retval instanceof IXR_Error )
1225 * Delete a post for any registered post type.
1229 * @uses wp_delete_post()
1230 * @param array $args Method parameters. Contains:
1232 * - string $username
1233 * - string $password
1235 * @return true on success
1237 function wp_deletePost( $args ) {
1238 if ( ! $this->minimum_args( $args, 4 ) )
1239 return $this->error;
1241 $this->escape( $args );
1243 $blog_id = (int) $args[0];
1244 $username = $args[1];
1245 $password = $args[2];
1246 $post_id = (int) $args[3];
1248 if ( ! $user = $this->login( $username, $password ) )
1249 return $this->error;
1251 do_action( 'xmlrpc_call', 'wp.deletePost' );
1253 $post = wp_get_single_post( $post_id, ARRAY_A );
1254 if ( empty( $post['ID'] ) )
1255 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1257 $post_type = get_post_type_object( $post['post_type'] );
1258 if ( ! current_user_can( $post_type->cap->delete_post, $post_id ) )
1259 return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1261 $result = wp_delete_post( $post_id );
1264 return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1274 * The optional $fields parameter specifies what fields will be included
1275 * in the response array. This should be a list of field names. 'post_id' will
1276 * always be included in the response regardless of the value of $fields.
1278 * Instead of, or in addition to, individual field names, conceptual group
1279 * names can be used to specify multiple fields. The available conceptual
1280 * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1283 * @uses wp_get_single_post()
1284 * @param array $args Method parameters. Contains:
1286 * - string $username
1287 * - string $password
1288 * - array $fields optional
1289 * @return array contains (based on $fields parameter):
1295 * - 'post_modified_gmt'
1304 * - 'comment_status'
1313 function wp_getPost( $args ) {
1314 if ( ! $this->minimum_args( $args, 4 ) )
1315 return $this->error;
1317 $this->escape( $args );
1319 $blog_id = (int) $args[0];
1320 $username = $args[1];
1321 $password = $args[2];
1322 $post_id = (int) $args[3];
1324 if ( isset( $args[4] ) )
1327 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1329 if ( ! $user = $this->login( $username, $password ) )
1330 return $this->error;
1332 do_action( 'xmlrpc_call', 'wp.getPost' );
1334 $post = wp_get_single_post( $post_id, ARRAY_A );
1336 if ( empty( $post['ID'] ) )
1337 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1339 $post_type = get_post_type_object( $post['post_type'] );
1340 if ( ! current_user_can( $post_type->cap->edit_posts, $post_id ) )
1341 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1343 return $this->_prepare_post( $post, $fields );
1351 * The optional $filter parameter modifies the query used to retrieve posts.
1352 * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
1353 * 'orderby', and 'order'.
1355 * The optional $fields parameter specifies what fields will be included
1356 * in the response array.
1358 * @uses wp_get_recent_posts()
1359 * @see wp_getPost() for more on $fields
1360 * @see get_posts() for more on $filter values
1362 * @param array $args Method parameters. Contains:
1364 * - string $username
1365 * - string $password
1366 * - array $filter optional
1367 * - array $fields optional
1368 * @return array contains a collection of posts.
1370 function wp_getPosts( $args ) {
1371 if ( ! $this->minimum_args( $args, 3 ) )
1372 return $this->error;
1374 $this->escape( $args );
1376 $blog_id = (int) $args[0];
1377 $username = $args[1];
1378 $password = $args[2];
1379 $filter = isset( $args[3] ) ? $args[3] : array();
1381 if ( isset( $args[4] ) )
1384 $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1386 if ( ! $user = $this->login( $username, $password ) )
1387 return $this->error;
1389 do_action( 'xmlrpc_call', 'wp.getPosts' );
1393 if ( isset( $filter['post_type'] ) ) {
1394 $post_type = get_post_type_object( $filter['post_type'] );
1395 if ( ! ( (bool) $post_type ) )
1396 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1398 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1399 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1401 $query['post_type'] = $filter['post_type'];
1404 if ( isset( $filter['post_status'] ) )
1405 $query['post_status'] = $filter['post_status'];
1407 if ( isset( $filter['number'] ) )
1408 $query['numberposts'] = absint( $filter['number'] );
1410 if ( isset( $filter['offset'] ) )
1411 $query['offset'] = absint( $filter['offset'] );
1413 if ( isset( $filter['orderby'] ) ) {
1414 $query['orderby'] = $filter['orderby'];
1416 if ( isset( $filter['order'] ) )
1417 $query['order'] = $filter['order'];
1420 $posts_list = wp_get_recent_posts( $query );
1422 if ( ! $posts_list )
1425 // holds all the posts data
1428 foreach ( $posts_list as $post ) {
1429 $post_type = get_post_type_object( $post['post_type'] );
1430 if ( ! current_user_can( $post_type->cap->edit_posts, $post['ID'] ) )
1433 $struct[] = $this->_prepare_post( $post, $fields );
1440 * Create a new term.
1444 * @uses wp_insert_term()
1445 * @param array $args Method parameters. Contains:
1447 * - string $username
1448 * - string $password
1449 * - array $content_struct
1450 * The $content_struct must contain:
1453 * Also, it can optionally contain:
1457 * @return string term_id
1459 function wp_newTerm( $args ) {
1460 if ( ! $this->minimum_args( $args, 4 ) )
1461 return $this->error;
1463 $this->escape( $args );
1465 $blog_id = (int) $args[0];
1466 $username = $args[1];
1467 $password = $args[2];
1468 $content_struct = $args[3];
1470 if ( ! $user = $this->login( $username, $password ) )
1471 return $this->error;
1473 do_action( 'xmlrpc_call', 'wp.newTerm' );
1475 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1476 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1478 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1480 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1481 return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1483 $taxonomy = (array) $taxonomy;
1485 // hold the data of the term
1486 $term_data = array();
1488 $term_data['name'] = trim( $content_struct['name'] );
1489 if ( empty( $term_data['name'] ) )
1490 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1492 if ( isset( $content_struct['parent'] ) ) {
1493 if ( ! $taxonomy['hierarchical'] )
1494 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1496 $parent_term_id = (int) $content_struct['parent'];
1497 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1499 if ( is_wp_error( $parent_term ) )
1500 return new IXR_Error( 500, $parent_term->get_error_message() );
1502 if ( ! $parent_term )
1503 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1505 $term_data['parent'] = $content_struct['parent'];
1508 if ( isset( $content_struct['description'] ) )
1509 $term_data['description'] = $content_struct['description'];
1511 if ( isset( $content_struct['slug'] ) )
1512 $term_data['slug'] = $content_struct['slug'];
1514 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1516 if ( is_wp_error( $term ) )
1517 return new IXR_Error( 500, $term->get_error_message() );
1520 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1522 return strval( $term['term_id'] );
1530 * @uses wp_update_term()
1531 * @param array $args Method parameters. Contains:
1533 * - string $username
1534 * - string $password
1536 * - array $content_struct
1537 * The $content_struct must contain:
1539 * Also, it can optionally contain:
1544 * @return bool True, on success.
1546 function wp_editTerm( $args ) {
1547 if ( ! $this->minimum_args( $args, 5 ) )
1548 return $this->error;
1550 $this->escape( $args );
1552 $blog_id = (int) $args[0];
1553 $username = $args[1];
1554 $password = $args[2];
1555 $term_id = (int) $args[3];
1556 $content_struct = $args[4];
1558 if ( ! $user = $this->login( $username, $password ) )
1559 return $this->error;
1561 do_action( 'xmlrpc_call', 'wp.editTerm' );
1563 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1564 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1566 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1568 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1569 return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1571 $taxonomy = (array) $taxonomy;
1573 // hold the data of the term
1574 $term_data = array();
1576 $term = get_term( $term_id , $content_struct['taxonomy'] );
1578 if ( is_wp_error( $term ) )
1579 return new IXR_Error( 500, $term->get_error_message() );
1582 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1584 if ( isset( $content_struct['name'] ) ) {
1585 $term_data['name'] = trim( $content_struct['name'] );
1587 if ( empty( $term_data['name'] ) )
1588 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1591 if ( isset( $content_struct['parent'] ) ) {
1592 if ( ! $taxonomy['hierarchical'] )
1593 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1595 $parent_term_id = (int) $content_struct['parent'];
1596 $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1598 if ( is_wp_error( $parent_term ) )
1599 return new IXR_Error( 500, $parent_term->get_error_message() );
1601 if ( ! $parent_term )
1602 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1604 $term_data['parent'] = $content_struct['parent'];
1607 if ( isset( $content_struct['description'] ) )
1608 $term_data['description'] = $content_struct['description'];
1610 if ( isset( $content_struct['slug'] ) )
1611 $term_data['slug'] = $content_struct['slug'];
1613 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1615 if ( is_wp_error( $term ) )
1616 return new IXR_Error( 500, $term->get_error_message() );
1619 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1629 * @uses wp_delete_term()
1630 * @param array $args Method parameters. Contains:
1632 * - string $username
1633 * - string $password
1634 * - string $taxnomy_name
1636 * @return boolean|IXR_Error If it suceeded true else a reason why not
1638 function wp_deleteTerm( $args ) {
1639 if ( ! $this->minimum_args( $args, 5 ) )
1640 return $this->error;
1642 $this->escape( $args );
1644 $blog_id = (int) $args[0];
1645 $username = $args[1];
1646 $password = $args[2];
1647 $taxonomy = $args[3];
1648 $term_id = (int) $args[4];
1650 if ( ! $user = $this->login( $username, $password ) )
1651 return $this->error;
1653 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1655 if ( ! taxonomy_exists( $taxonomy ) )
1656 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1658 $taxonomy = get_taxonomy( $taxonomy );
1660 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
1661 return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
1663 $term = get_term( $term_id, $taxonomy->name );
1665 if ( is_wp_error( $term ) )
1666 return new IXR_Error( 500, $term->get_error_message() );
1669 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1671 $result = wp_delete_term( $term_id, $taxonomy->name );
1673 if ( is_wp_error( $result ) )
1674 return new IXR_Error( 500, $term->get_error_message() );
1677 return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
1688 * @param array $args Method parameters. Contains:
1690 * - string $username
1691 * - string $password
1692 * - string $taxonomy
1694 * @return array contains:
1699 * - 'term_taxonomy_id'
1705 function wp_getTerm( $args ) {
1706 if ( ! $this->minimum_args( $args, 5 ) )
1707 return $this->error;
1709 $this->escape( $args );
1711 $blog_id = (int) $args[0];
1712 $username = $args[1];
1713 $password = $args[2];
1714 $taxonomy = $args[3];
1715 $term_id = (int) $args[4];
1717 if ( ! $user = $this->login( $username, $password ) )
1718 return $this->error;
1720 do_action( 'xmlrpc_call', 'wp.getTerm' );
1722 if ( ! taxonomy_exists( $taxonomy ) )
1723 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1725 $taxonomy = get_taxonomy( $taxonomy );
1727 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1728 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1730 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
1732 if ( is_wp_error( $term ) )
1733 return new IXR_Error( 500, $term->get_error_message() );
1736 return new IXR_Error( 404, __( 'Invalid term ID' ) );
1738 return $this->_prepare_term( $term );
1742 * Retrieve all terms for a taxonomy.
1746 * The optional $filter parameter modifies the query used to retrieve terms.
1747 * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
1750 * @param array $args Method parameters. Contains:
1752 * - string $username
1753 * - string $password
1754 * - string $taxonomy
1755 * - array $filter optional
1756 * @return array terms
1758 function wp_getTerms( $args ) {
1759 if ( ! $this->minimum_args( $args, 4 ) )
1760 return $this->error;
1762 $this->escape( $args );
1764 $blog_id = (int) $args[0];
1765 $username = $args[1];
1766 $password = $args[2];
1767 $taxonomy = $args[3];
1768 $filter = isset( $args[4] ) ? $args[4] : array();
1770 if ( ! $user = $this->login( $username, $password ) )
1771 return $this->error;
1773 do_action( 'xmlrpc_call', 'wp.getTerms' );
1775 if ( ! taxonomy_exists( $taxonomy ) )
1776 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1778 $taxonomy = get_taxonomy( $taxonomy );
1780 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1781 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1785 if ( isset( $filter['number'] ) )
1786 $query['number'] = absint( $filter['number'] );
1788 if ( isset( $filter['offset'] ) )
1789 $query['offset'] = absint( $filter['offset'] );
1791 if ( isset( $filter['orderby'] ) ) {
1792 $query['orderby'] = $filter['orderby'];
1794 if ( isset( $filter['order'] ) )
1795 $query['order'] = $filter['order'];
1798 if ( isset( $filter['hide_empty'] ) )
1799 $query['hide_empty'] = $filter['hide_empty'];
1801 $query['get'] = 'all';
1803 if ( isset( $filter['search'] ) )
1804 $query['search'] = $filter['search'];
1806 $terms = get_terms( $taxonomy->name, $query );
1808 if ( is_wp_error( $terms ) )
1809 return new IXR_Error( 500, $terms->get_error_message() );
1813 foreach ( $terms as $term ) {
1814 $struct[] = $this->_prepare_term( $term );
1821 * Retrieve a taxonomy.
1825 * @uses get_taxonomy()
1826 * @param array $args Method parameters. Contains:
1828 * - string $username
1829 * - string $password
1830 * - string $taxonomy
1831 * @return array (@see get_taxonomy())
1833 function wp_getTaxonomy( $args ) {
1834 if ( ! $this->minimum_args( $args, 4 ) )
1835 return $this->error;
1837 $this->escape( $args );
1839 $blog_id = (int) $args[0];
1840 $username = $args[1];
1841 $password = $args[2];
1842 $taxonomy = $args[3];
1844 if ( isset( $args[4] ) )
1847 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
1849 if ( ! $user = $this->login( $username, $password ) )
1850 return $this->error;
1852 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
1854 if ( ! taxonomy_exists( $taxonomy ) )
1855 return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1857 $taxonomy = get_taxonomy( $taxonomy );
1859 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1860 return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1862 return $this->_prepare_taxonomy( $taxonomy, $fields );
1866 * Retrieve all taxonomies.
1870 * @uses get_taxonomies()
1871 * @param array $args Method parameters. Contains:
1873 * - string $username
1874 * - string $password
1875 * @return array taxonomies
1877 function wp_getTaxonomies( $args ) {
1878 if ( ! $this->minimum_args( $args, 3 ) )
1879 return $this->error;
1881 $this->escape( $args );
1883 $blog_id = (int) $args[0];
1884 $username = $args[1];
1885 $password = $args[2];
1886 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
1888 if ( isset( $args[4] ) )
1891 $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
1893 if ( ! $user = $this->login( $username, $password ) )
1894 return $this->error;
1896 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
1898 $taxonomies = get_taxonomies( $filter, 'objects' );
1900 // holds all the taxonomy data
1903 foreach ( $taxonomies as $taxonomy ) {
1904 // capability check for post_types
1905 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1908 $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
1919 * @param array $args Method parameters. Contains:
1926 function wp_getPage($args) {
1927 $this->escape($args);
1929 $blog_id = (int) $args[0];
1930 $page_id = (int) $args[1];
1931 $username = $args[2];
1932 $password = $args[3];
1934 if ( !$user = $this->login($username, $password) ) {
1935 return $this->error;
1938 $page = get_page($page_id);
1940 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1942 if ( !current_user_can( 'edit_page', $page_id ) )
1943 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
1945 do_action('xmlrpc_call', 'wp.getPage');
1947 // If we found the page then format the data.
1948 if ( $page->ID && ($page->post_type == 'page') ) {
1949 return $this->_prepare_page( $page );
1951 // If the page doesn't exist indicate that.
1953 return(new IXR_Error(404, __('Sorry, no such page.')));
1962 * @param array $args Method parameters. Contains:
1969 function wp_getPages($args) {
1970 $this->escape($args);
1972 $blog_id = (int) $args[0];
1973 $username = $args[1];
1974 $password = $args[2];
1975 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
1977 if ( !$user = $this->login($username, $password) )
1978 return $this->error;
1980 if ( !current_user_can( 'edit_pages' ) )
1981 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
1983 do_action('xmlrpc_call', 'wp.getPages');
1985 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
1986 $num_pages = count($pages);
1988 // If we have pages, put together their info.
1989 if ( $num_pages >= 1 ) {
1990 $pages_struct = array();
1992 foreach ($pages as $page) {
1993 if ( current_user_can( 'edit_page', $page->ID ) )
1994 $pages_struct[] = $this->_prepare_page( $page );
1997 return($pages_struct);
1999 // If no pages were found return an error.
2010 * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
2013 function wp_newPage($args) {
2014 // Items not escaped here will be escaped in newPost.
2015 $username = $this->escape($args[1]);
2016 $password = $this->escape($args[2]);
2018 $publish = $args[4];
2020 if ( !$user = $this->login($username, $password) )
2021 return $this->error;
2023 do_action('xmlrpc_call', 'wp.newPage');
2025 // Mark this as content for a page.
2026 $args[3]["post_type"] = 'page';
2028 // Let mw_newPost do all of the heavy lifting.
2029 return($this->mw_newPost($args));
2037 * @param array $args Method parameters.
2038 * @return bool True, if success.
2040 function wp_deletePage($args) {
2041 $this->escape($args);
2043 $blog_id = (int) $args[0];
2044 $username = $args[1];
2045 $password = $args[2];
2046 $page_id = (int) $args[3];
2048 if ( !$user = $this->login($username, $password) )
2049 return $this->error;
2051 do_action('xmlrpc_call', 'wp.deletePage');
2053 // Get the current page based on the page_id and
2054 // make sure it is a page and not a post.
2055 $actual_page = wp_get_single_post($page_id, ARRAY_A);
2056 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2057 return(new IXR_Error(404, __('Sorry, no such page.')));
2059 // Make sure the user can delete pages.
2060 if ( !current_user_can('delete_page', $page_id) )
2061 return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
2063 // Attempt to delete the page.
2064 $result = wp_delete_post($page_id);
2066 return(new IXR_Error(500, __('Failed to delete the page.')));
2068 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2078 * @param array $args Method parameters.
2081 function wp_editPage($args) {
2082 // Items not escaped here will be escaped in editPost.
2083 $blog_id = (int) $args[0];
2084 $page_id = (int) $this->escape($args[1]);
2085 $username = $this->escape($args[2]);
2086 $password = $this->escape($args[3]);
2087 $content = $args[4];
2088 $publish = $args[5];
2090 if ( !$user = $this->login($username, $password) )
2091 return $this->error;
2093 do_action('xmlrpc_call', 'wp.editPage');
2095 // Get the page data and make sure it is a page.
2096 $actual_page = wp_get_single_post($page_id, ARRAY_A);
2097 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2098 return(new IXR_Error(404, __('Sorry, no such page.')));
2100 // Make sure the user is allowed to edit pages.
2101 if ( !current_user_can('edit_page', $page_id) )
2102 return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
2104 // Mark this as content for a page.
2105 $content['post_type'] = 'page';
2107 // Arrange args in the way mw_editPost understands.
2116 // Let mw_editPost do all of the heavy lifting.
2117 return($this->mw_editPost($args));
2121 * Retrieve page list.
2125 * @param array $args Method parameters.
2128 function wp_getPageList($args) {
2131 $this->escape($args);
2133 $blog_id = (int) $args[0];
2134 $username = $args[1];
2135 $password = $args[2];
2137 if ( !$user = $this->login($username, $password) )
2138 return $this->error;
2140 if ( !current_user_can( 'edit_pages' ) )
2141 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2143 do_action('xmlrpc_call', 'wp.getPageList');
2145 // Get list of pages ids and titles
2146 $page_list = $wpdb->get_results("
2148 post_title page_title,
2149 post_parent page_parent_id,
2154 WHERE post_type = 'page'
2158 // The date needs to be formatted properly.
2159 $num_pages = count($page_list);
2160 for ( $i = 0; $i < $num_pages; $i++ ) {
2161 $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
2162 $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2164 unset($page_list[$i]->post_date_gmt);
2165 unset($page_list[$i]->post_date);
2166 unset($page_list[$i]->post_status);
2173 * Retrieve authors list.
2177 * @param array $args Method parameters.
2180 function wp_getAuthors($args) {
2182 $this->escape($args);
2184 $blog_id = (int) $args[0];
2185 $username = $args[1];
2186 $password = $args[2];
2188 if ( !$user = $this->login($username, $password) )
2189 return $this->error;
2191 if ( !current_user_can('edit_posts') )
2192 return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
2194 do_action('xmlrpc_call', 'wp.getAuthors');
2197 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2199 'user_id' => $user->ID,
2200 'user_login' => $user->user_login,
2201 'display_name' => $user->display_name
2209 * Get list of all tags
2213 * @param array $args Method parameters.
2216 function wp_getTags( $args ) {
2217 $this->escape( $args );
2219 $blog_id = (int) $args[0];
2220 $username = $args[1];
2221 $password = $args[2];
2223 if ( !$user = $this->login($username, $password) )
2224 return $this->error;
2226 if ( !current_user_can( 'edit_posts' ) )
2227 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2229 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2233 if ( $all_tags = get_tags() ) {
2234 foreach( (array) $all_tags as $tag ) {
2235 $struct['tag_id'] = $tag->term_id;
2236 $struct['name'] = $tag->name;
2237 $struct['count'] = $tag->count;
2238 $struct['slug'] = $tag->slug;
2239 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
2240 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
2250 * Create new category.
2254 * @param array $args Method parameters.
2255 * @return int Category ID.
2257 function wp_newCategory($args) {
2258 $this->escape($args);
2260 $blog_id = (int) $args[0];
2261 $username = $args[1];
2262 $password = $args[2];
2263 $category = $args[3];
2265 if ( !$user = $this->login($username, $password) )
2266 return $this->error;
2268 do_action('xmlrpc_call', 'wp.newCategory');
2270 // Make sure the user is allowed to add a category.
2271 if ( !current_user_can('manage_categories') )
2272 return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
2274 // If no slug was provided make it empty so that
2275 // WordPress will generate one.
2276 if ( empty($category['slug']) )
2277 $category['slug'] = '';
2279 // If no parent_id was provided make it empty
2280 // so that it will be a top level page (no parent).
2281 if ( !isset($category['parent_id']) )
2282 $category['parent_id'] = '';
2284 // If no description was provided make it empty.
2285 if ( empty($category["description"]) )
2286 $category["description"] = "";
2288 $new_category = array(
2289 'cat_name' => $category['name'],
2290 'category_nicename' => $category['slug'],
2291 'category_parent' => $category['parent_id'],
2292 'category_description' => $category['description']
2295 $cat_id = wp_insert_category($new_category, true);
2296 if ( is_wp_error( $cat_id ) ) {
2297 if ( 'term_exists' == $cat_id->get_error_code() )
2298 return (int) $cat_id->get_error_data();
2300 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2301 } elseif ( ! $cat_id ) {
2302 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2305 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
2315 * @param array $args Method parameters.
2316 * @return mixed See {@link wp_delete_term()} for return info.
2318 function wp_deleteCategory($args) {
2319 $this->escape($args);
2321 $blog_id = (int) $args[0];
2322 $username = $args[1];
2323 $password = $args[2];
2324 $category_id = (int) $args[3];
2326 if ( !$user = $this->login($username, $password) )
2327 return $this->error;
2329 do_action('xmlrpc_call', 'wp.deleteCategory');
2331 if ( !current_user_can('manage_categories') )
2332 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
2334 $status = wp_delete_term( $category_id, 'category' );
2336 if( true == $status )
2337 do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
2343 * Retrieve category list.
2347 * @param array $args Method parameters.
2350 function wp_suggestCategories($args) {
2351 $this->escape($args);
2353 $blog_id = (int) $args[0];
2354 $username = $args[1];
2355 $password = $args[2];
2356 $category = $args[3];
2357 $max_results = (int) $args[4];
2359 if ( !$user = $this->login($username, $password) )
2360 return $this->error;
2362 if ( !current_user_can( 'edit_posts' ) )
2363 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
2365 do_action('xmlrpc_call', 'wp.suggestCategories');
2367 $category_suggestions = array();
2368 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
2369 foreach ( (array) get_categories($args) as $cat ) {
2370 $category_suggestions[] = array(
2371 'category_id' => $cat->term_id,
2372 'category_name' => $cat->name
2376 return($category_suggestions);
2384 * @param array $args Method parameters.
2387 function wp_getComment($args) {
2388 $this->escape($args);
2390 $blog_id = (int) $args[0];
2391 $username = $args[1];
2392 $password = $args[2];
2393 $comment_id = (int) $args[3];
2395 if ( !$user = $this->login($username, $password) )
2396 return $this->error;
2398 if ( !current_user_can( 'moderate_comments' ) )
2399 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2401 do_action('xmlrpc_call', 'wp.getComment');
2403 if ( ! $comment = get_comment($comment_id) )
2404 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2406 return $this->_prepare_comment( $comment );
2410 * Retrieve comments.
2412 * Besides the common blog_id, username, and password arguments, it takes a filter
2413 * array as last argument.
2415 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
2417 * The defaults are as follows:
2418 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
2419 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
2420 * - 'number' - Default is 10. Total number of media items to retrieve.
2421 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2425 * @param array $args Method parameters.
2426 * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
2428 function wp_getComments($args) {
2429 $this->escape($args);
2431 $blog_id = (int) $args[0];
2432 $username = $args[1];
2433 $password = $args[2];
2434 $struct = isset( $args[3] ) ? $args[3] : array();
2436 if ( !$user = $this->login($username, $password) )
2437 return $this->error;
2439 if ( !current_user_can( 'moderate_comments' ) )
2440 return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
2442 do_action('xmlrpc_call', 'wp.getComments');
2444 if ( isset($struct['status']) )
2445 $status = $struct['status'];
2450 if ( isset($struct['post_id']) )
2451 $post_id = absint($struct['post_id']);
2454 if ( isset($struct['offset']) )
2455 $offset = absint($struct['offset']);
2458 if ( isset($struct['number']) )
2459 $number = absint($struct['number']);
2461 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
2463 $comments_struct = array();
2465 foreach ( $comments as $comment ) {
2466 $comments_struct[] = $this->_prepare_comment( $comment );
2469 return $comments_struct;
2475 * By default, the comment will be moved to the trash instead of deleted.
2476 * See {@link wp_delete_comment()} for more information on
2481 * @param array $args Method parameters. Contains:
2486 * @return mixed {@link wp_delete_comment()}
2488 function wp_deleteComment($args) {
2489 $this->escape($args);
2491 $blog_id = (int) $args[0];
2492 $username = $args[1];
2493 $password = $args[2];
2494 $comment_ID = (int) $args[3];
2496 if ( !$user = $this->login($username, $password) )
2497 return $this->error;
2499 if ( !current_user_can( 'moderate_comments' ) )
2500 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2502 if ( ! get_comment($comment_ID) )
2503 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2505 if ( !current_user_can( 'edit_comment', $comment_ID ) )
2506 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2508 do_action('xmlrpc_call', 'wp.deleteComment');
2510 $status = wp_delete_comment( $comment_ID );
2512 if( true == $status )
2513 do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
2521 * Besides the common blog_id, username, and password arguments, it takes a
2522 * comment_id integer and a content_struct array as last argument.
2524 * The allowed keys in the content_struct array are:
2529 * - 'date_created_gmt'
2530 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
2534 * @param array $args. Contains:
2540 * @return bool True, on success.
2542 function wp_editComment($args) {
2543 $this->escape($args);
2545 $blog_id = (int) $args[0];
2546 $username = $args[1];
2547 $password = $args[2];
2548 $comment_ID = (int) $args[3];
2549 $content_struct = $args[4];
2551 if ( !$user = $this->login($username, $password) )
2552 return $this->error;
2554 if ( !current_user_can( 'moderate_comments' ) )
2555 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2557 if ( ! get_comment($comment_ID) )
2558 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2560 if ( !current_user_can( 'edit_comment', $comment_ID ) )
2561 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2563 do_action('xmlrpc_call', 'wp.editComment');
2565 if ( isset($content_struct['status']) ) {
2566 $statuses = get_comment_statuses();
2567 $statuses = array_keys($statuses);
2569 if ( ! in_array($content_struct['status'], $statuses) )
2570 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
2571 $comment_approved = $content_struct['status'];
2574 // Do some timestamp voodoo
2575 if ( !empty( $content_struct['date_created_gmt'] ) ) {
2576 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
2577 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
2578 $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
2579 $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
2582 if ( isset($content_struct['content']) )
2583 $comment_content = $content_struct['content'];
2585 if ( isset($content_struct['author']) )
2586 $comment_author = $content_struct['author'];
2588 if ( isset($content_struct['author_url']) )
2589 $comment_author_url = $content_struct['author_url'];
2591 if ( isset($content_struct['author_email']) )
2592 $comment_author_email = $content_struct['author_email'];
2594 // We've got all the data -- post it:
2595 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
2597 $result = wp_update_comment($comment);
2598 if ( is_wp_error( $result ) )
2599 return new IXR_Error(500, $result->get_error_message());
2602 return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
2604 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
2610 * Create new comment.
2614 * @param array $args Method parameters.
2615 * @return mixed {@link wp_new_comment()}
2617 function wp_newComment($args) {
2620 $this->escape($args);
2622 $blog_id = (int) $args[0];
2623 $username = $args[1];
2624 $password = $args[2];
2626 $content_struct = $args[4];
2628 $allow_anon = apply_filters('xmlrpc_allow_anonymous_comments', false);
2630 $user = $this->login($username, $password);
2634 if ( $allow_anon && get_option('comment_registration') )
2635 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
2636 else if ( !$allow_anon )
2637 return $this->error;
2642 if ( is_numeric($post) )
2643 $post_id = absint($post);
2645 $post_id = url_to_postid($post);
2648 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2650 if ( ! get_post($post_id) )
2651 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2653 $comment['comment_post_ID'] = $post_id;
2656 $comment['comment_author'] = $wpdb->escape( $user->display_name );
2657 $comment['comment_author_email'] = $wpdb->escape( $user->user_email );
2658 $comment['comment_author_url'] = $wpdb->escape( $user->user_url );
2659 $comment['user_ID'] = $user->ID;
2661 $comment['comment_author'] = '';
2662 if ( isset($content_struct['author']) )
2663 $comment['comment_author'] = $content_struct['author'];
2665 $comment['comment_author_email'] = '';
2666 if ( isset($content_struct['author_email']) )
2667 $comment['comment_author_email'] = $content_struct['author_email'];
2669 $comment['comment_author_url'] = '';
2670 if ( isset($content_struct['author_url']) )
2671 $comment['comment_author_url'] = $content_struct['author_url'];
2673 $comment['user_ID'] = 0;
2675 if ( get_option('require_name_email') ) {
2676 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
2677 return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
2678 elseif ( !is_email($comment['comment_author_email']) )
2679 return new IXR_Error( 403, __( 'A valid email address is required' ) );
2683 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
2685 $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
2687 do_action('xmlrpc_call', 'wp.newComment');
2689 $comment_ID = wp_new_comment( $comment );
2691 do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
2697 * Retrieve all of the comment status.
2701 * @param array $args Method parameters.
2704 function wp_getCommentStatusList($args) {
2705 $this->escape( $args );
2707 $blog_id = (int) $args[0];
2708 $username = $args[1];
2709 $password = $args[2];
2711 if ( !$user = $this->login($username, $password) )
2712 return $this->error;
2714 if ( !current_user_can( 'moderate_comments' ) )
2715 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
2717 do_action('xmlrpc_call', 'wp.getCommentStatusList');
2719 return get_comment_statuses();
2723 * Retrieve comment count.
2727 * @param array $args Method parameters.
2730 function wp_getCommentCount( $args ) {
2731 $this->escape($args);
2733 $blog_id = (int) $args[0];
2734 $username = $args[1];
2735 $password = $args[2];
2736 $post_id = (int) $args[3];
2738 if ( !$user = $this->login($username, $password) )
2739 return $this->error;
2741 if ( !current_user_can( 'edit_posts' ) )
2742 return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
2744 do_action('xmlrpc_call', 'wp.getCommentCount');
2746 $count = wp_count_comments( $post_id );
2748 'approved' => $count->approved,
2749 'awaiting_moderation' => $count->moderated,
2750 'spam' => $count->spam,
2751 'total_comments' => $count->total_comments
2756 * Retrieve post statuses.
2760 * @param array $args Method parameters.
2763 function wp_getPostStatusList( $args ) {
2764 $this->escape( $args );
2766 $blog_id = (int) $args[0];
2767 $username = $args[1];
2768 $password = $args[2];
2770 if ( !$user = $this->login($username, $password) )
2771 return $this->error;
2773 if ( !current_user_can( 'edit_posts' ) )
2774 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
2776 do_action('xmlrpc_call', 'wp.getPostStatusList');
2778 return get_post_statuses();
2782 * Retrieve page statuses.
2786 * @param array $args Method parameters.
2789 function wp_getPageStatusList( $args ) {
2790 $this->escape( $args );
2792 $blog_id = (int) $args[0];
2793 $username = $args[1];
2794 $password = $args[2];
2796 if ( !$user = $this->login($username, $password) )
2797 return $this->error;
2799 if ( !current_user_can( 'edit_pages' ) )
2800 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
2802 do_action('xmlrpc_call', 'wp.getPageStatusList');
2804 return get_page_statuses();
2808 * Retrieve page templates.
2812 * @param array $args Method parameters.
2815 function wp_getPageTemplates( $args ) {
2816 $this->escape( $args );
2818 $blog_id = (int) $args[0];
2819 $username = $args[1];
2820 $password = $args[2];
2822 if ( !$user = $this->login($username, $password) )
2823 return $this->error;
2825 if ( !current_user_can( 'edit_pages' ) )
2826 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
2828 $templates = get_page_templates();
2829 $templates['Default'] = 'default';
2835 * Retrieve blog options.
2839 * @param array $args Method parameters.
2842 function wp_getOptions( $args ) {
2843 $this->escape( $args );
2845 $blog_id = (int) $args[0];
2846 $username = $args[1];
2847 $password = $args[2];
2848 $options = isset( $args[3] ) ? (array) $args[3] : array();
2850 if ( !$user = $this->login($username, $password) )
2851 return $this->error;
2853 // If no specific options where asked for, return all of them
2854 if ( count( $options ) == 0 )
2855 $options = array_keys($this->blog_options);
2857 return $this->_getOptions($options);
2861 * Retrieve blog options value from list.
2865 * @param array $options Options to retrieve.
2868 function _getOptions($options) {
2870 foreach ( $options as $option ) {
2871 if ( array_key_exists( $option, $this->blog_options ) ) {
2872 $data[$option] = $this->blog_options[$option];
2873 //Is the value static or dynamic?
2874 if ( isset( $data[$option]['option'] ) ) {
2875 $data[$option]['value'] = get_option( $data[$option]['option'] );
2876 unset($data[$option]['option']);
2885 * Update blog options.
2889 * @param array $args Method parameters.
2892 function wp_setOptions( $args ) {
2893 $this->escape( $args );
2895 $blog_id = (int) $args[0];
2896 $username = $args[1];
2897 $password = $args[2];
2898 $options = (array) $args[3];
2900 if ( !$user = $this->login($username, $password) )
2901 return $this->error;
2903 if ( !current_user_can( 'manage_options' ) )
2904 return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
2906 foreach ( $options as $o_name => $o_value ) {
2907 $option_names[] = $o_name;
2908 if ( !array_key_exists( $o_name, $this->blog_options ) )
2911 if ( $this->blog_options[$o_name]['readonly'] == true )
2914 update_option( $this->blog_options[$o_name]['option'], $o_value );
2917 //Now return the updated values
2918 return $this->_getOptions($option_names);
2922 * Retrieve a media item by ID
2926 * @param array $args Method parameters. Contains:
2931 * @return array. Associative array containing:
2932 * - 'date_created_gmt'
2941 function wp_getMediaItem($args) {
2942 $this->escape($args);
2944 $blog_id = (int) $args[0];
2945 $username = $args[1];
2946 $password = $args[2];
2947 $attachment_id = (int) $args[3];
2949 if ( !$user = $this->login($username, $password) )
2950 return $this->error;
2952 if ( !current_user_can( 'upload_files' ) )
2953 return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
2955 do_action('xmlrpc_call', 'wp.getMediaItem');
2957 if ( ! $attachment = get_post($attachment_id) )
2958 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
2960 return $this->_prepare_media_item( $attachment );
2964 * Retrieves a collection of media library items (or attachments)
2966 * Besides the common blog_id, username, and password arguments, it takes a filter
2967 * array as last argument.
2969 * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
2971 * The defaults are as follows:
2972 * - 'number' - Default is 5. Total number of media items to retrieve.
2973 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2974 * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
2975 * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
2979 * @param array $args Method parameters. Contains:
2984 * @return array. Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
2986 function wp_getMediaLibrary($args) {
2987 $this->escape($args);
2989 $blog_id = (int) $args[0];
2990 $username = $args[1];
2991 $password = $args[2];
2992 $struct = isset( $args[3] ) ? $args[3] : array() ;
2994 if ( !$user = $this->login($username, $password) )
2995 return $this->error;
2997 if ( !current_user_can( 'upload_files' ) )
2998 return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
3000 do_action('xmlrpc_call', 'wp.getMediaLibrary');
3002 $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
3003 $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
3004 $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
3005 $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
3007 $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
3009 $attachments_struct = array();
3011 foreach ($attachments as $attachment )
3012 $attachments_struct[] = $this->_prepare_media_item( $attachment );
3014 return $attachments_struct;
3018 * Retrieves a list of post formats used by the site
3022 * @param array $args Method parameters. Contains:
3028 function wp_getPostFormats( $args ) {
3029 $this->escape( $args );
3031 $blog_id = (int) $args[0];
3032 $username = $args[1];
3033 $password = $args[2];
3035 if ( !$user = $this->login( $username, $password ) )
3036 return $this->error;
3038 if ( !current_user_can( 'edit_posts' ) )
3039 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3041 do_action( 'xmlrpc_call', 'wp.getPostFormats' );
3043 $formats = get_post_format_strings();
3045 # find out if they want a list of currently supports formats
3046 if ( isset( $args[3] ) && is_array( $args[3] ) ) {
3047 if ( $args[3]['show-supported'] ) {
3048 if ( current_theme_supports( 'post-formats' ) ) {
3049 $supported = get_theme_support( 'post-formats' );
3051 $data['all'] = $formats;
3052 $data['supported'] = $supported[0];
3063 * Retrieves a post type
3067 * @uses get_post_type_object()
3068 * @param array $args Method parameters. Contains:
3070 * - string $username
3071 * - string $password
3072 * - string $post_type_name
3074 * @return array contains:
3077 * - 'capability_type'
3085 function wp_getPostType( $args ) {
3086 if ( ! $this->minimum_args( $args, 4 ) )
3087 return $this->error;
3089 $this->escape( $args );
3091 $blog_id = (int) $args[0];
3092 $username = $args[1];
3093 $password = $args[2];
3094 $post_type_name = $args[3];
3096 if ( isset( $args[4] ) )
3099 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
3101 if ( !$user = $this->login( $username, $password ) )
3102 return $this->error;
3104 do_action( 'xmlrpc_call', 'wp.getPostType' );
3106 if( ! post_type_exists( $post_type_name ) )
3107 return new IXR_Error( 403, __( 'Invalid post type' ) );
3109 $post_type = get_post_type_object( $post_type_name );
3111 if( ! current_user_can( $post_type->cap->edit_posts ) )
3112 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
3114 return $this->_prepare_post_type( $post_type, $fields );
3118 * Retrieves a post types
3122 * @uses get_post_types()
3123 * @param array $args Method parameters. Contains:
3125 * - string $username
3126 * - string $password
3131 function wp_getPostTypes( $args ) {
3132 if ( ! $this->minimum_args( $args, 3 ) )
3133 return $this->error;
3135 $this->escape( $args );
3137 $blog_id = (int) $args[0];
3138 $username = $args[1];
3139 $password = $args[2];
3140 $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
3142 if ( isset( $args[4] ) )
3145 $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
3147 if ( ! $user = $this->login( $username, $password ) )
3148 return $this->error;
3150 do_action( 'xmlrpc_call', 'wp.getPostTypes' );
3152 $post_types = get_post_types( $filter, 'objects' );
3156 foreach( $post_types as $post_type ) {
3157 if( ! current_user_can( $post_type->cap->edit_posts ) )
3160 $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
3166 /* Blogger API functions.
3167 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
3171 * Retrieve blogs that user owns.
3173 * Will make more sense once we support multiple blogs.
3177 * @param array $args Method parameters.
3180 function blogger_getUsersBlogs($args) {
3181 if ( is_multisite() )
3182 return $this->_multisite_getUsersBlogs($args);
3184 $this->escape($args);
3186 $username = $args[1];
3187 $password = $args[2];
3189 if ( !$user = $this->login($username, $password) )
3190 return $this->error;
3192 do_action('xmlrpc_call', 'blogger.getUsersBlogs');
3194 $is_admin = current_user_can('manage_options');
3197 'isAdmin' => $is_admin,
3198 'url' => get_option('home') . '/',
3200 'blogName' => get_option('blogname'),
3201 'xmlrpc' => site_url( 'xmlrpc.php' )
3204 return array($struct);
3208 * Private function for retrieving a users blogs for multisite setups
3212 function _multisite_getUsersBlogs($args) {
3213 global $current_blog;
3214 $domain = $current_blog->domain;
3215 $path = $current_blog->path . 'xmlrpc.php';
3216 $protocol = is_ssl() ? 'https' : 'http';
3218 $rpc = new IXR_Client("$protocol://{$domain}{$path}");
3219 $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
3220 $blogs = $rpc->getResponse();
3222 if ( isset($blogs['faultCode']) )
3223 return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
3225 if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
3228 foreach ( (array) $blogs as $blog ) {
3229 if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
3230 return array($blog);
3237 * Retrieve user's data.
3239 * Gives your client some info about you, so you don't have to.
3243 * @param array $args Method parameters.
3246 function blogger_getUserInfo($args) {
3248 $this->escape($args);
3250 $username = $args[1];
3251 $password = $args[2];
3253 if ( !$user = $this->login($username, $password) )
3254 return $this->error;
3256 if ( !current_user_can( 'edit_posts' ) )
3257 return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
3259 do_action('xmlrpc_call', 'blogger.getUserInfo');
3262 'nickname' => $user->nickname,
3263 'userid' => $user->ID,
3264 'url' => $user->user_url,
3265 'lastname' => $user->last_name,
3266 'firstname' => $user->first_name
3277 * @param array $args Method parameters.
3280 function blogger_getPost($args) {
3282 $this->escape($args);
3284 $post_ID = (int) $args[1];
3285 $username = $args[2];
3286 $password = $args[3];
3288 if ( !$user = $this->login($username, $password) )
3289 return $this->error;
3291 $post_data = wp_get_single_post($post_ID, ARRAY_A);
3293 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3295 if ( !current_user_can( 'edit_post', $post_ID ) )
3296 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
3298 do_action('xmlrpc_call', 'blogger.getPost');
3300 $categories = implode(',', wp_get_post_categories($post_ID));
3302 $content = '<title>'.stripslashes($post_data['post_title']).'</title>';
3303 $content .= '<category>'.$categories.'</category>';
3304 $content .= stripslashes($post_data['post_content']);
3307 'userid' => $post_data['post_author'],
3308 'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
3309 'content' => $content,
3310 'postid' => (string) $post_data['ID']
3317 * Retrieve list of recent posts.
3321 * @param array $args Method parameters.
3324 function blogger_getRecentPosts($args) {
3326 $this->escape($args);
3328 // $args[0] = appkey - ignored
3329 $blog_ID = (int) $args[1]; /* though we don't use it yet */
3330 $username = $args[2];
3331 $password = $args[3];
3332 if ( isset( $args[4] ) )
3333 $query = array( 'numberposts' => absint( $args[4] ) );
3337 if ( !$user = $this->login($username, $password) )
3338 return $this->error;
3340 do_action('xmlrpc_call', 'blogger.getRecentPosts');
3342 $posts_list = wp_get_recent_posts( $query );
3344 if ( !$posts_list ) {
3345 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
3346 return $this->error;
3349 foreach ($posts_list as $entry) {
3350 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
3353 $post_date = $this->_convert_date( $entry['post_date'] );
3354 $categories = implode(',', wp_get_post_categories($entry['ID']));
3356 $content = '<title>'.stripslashes($entry['post_title']).'</title>';
3357 $content .= '<category>'.$categories.'</category>';
3358 $content .= stripslashes($entry['post_content']);
3361 'userid' => $entry['post_author'],
3362 'dateCreated' => $post_date,
3363 'content' => $content,
3364 'postid' => (string) $entry['ID'],
3369 $recent_posts = array();
3370 for ( $j=0; $j<count($struct); $j++ ) {
3371 array_push($recent_posts, $struct[$j]);
3374 return $recent_posts;
3378 * Retrieve blog_filename content.
3382 * @param array $args Method parameters.
3385 function blogger_getTemplate($args) {
3387 $this->escape($args);
3389 $blog_ID = (int) $args[1];
3390 $username = $args[2];
3391 $password = $args[3];
3392 $template = $args[4]; /* could be 'main' or 'archiveIndex', but we don't use it */
3394 if ( !$user = $this->login($username, $password) )
3395 return $this->error;
3397 do_action('xmlrpc_call', 'blogger.getTemplate');
3399 if ( !current_user_can('edit_themes') )
3400 return new IXR_Error(401, __('Sorry, this user cannot edit the template.'));
3402 /* warning: here we make the assumption that the blog's URL is on the same server */
3403 $filename = get_option('home') . '/';
3404 $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
3406 $f = fopen($filename, 'r');
3407 $content = fread($f, filesize($filename));
3410 /* so it is actually editable with a windows/mac client */
3411 // FIXME: (or delete me) do we really want to cater to bad clients at the expense of good ones by BEEPing up their line breaks? commented. $content = str_replace("\n", "\r\n", $content);
3417 * Updates the content of blog_filename.
3421 * @param array $args Method parameters.
3422 * @return bool True when done.
3424 function blogger_setTemplate($args) {
3426 $this->escape($args);
3428 $blog_ID = (int) $args[1];
3429 $username = $args[2];
3430 $password = $args[3];
3431 $content = $args[4];
3432 $template = $args[5]; /* could be 'main' or 'archiveIndex', but we don't use it */
3434 if ( !$user = $this->login($username, $password) )
3435 return $this->error;
3437 do_action('xmlrpc_call', 'blogger.setTemplate');
3439 if ( !current_user_can('edit_themes') )
3440 return new IXR_Error(401, __('Sorry, this user cannot edit the template.'));
3442 /* warning: here we make the assumption that the blog's URL is on the same server */
3443 $filename = get_option('home') . '/';
3444 $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
3446 if ($f = fopen($filename, 'w+')) {
3447 fwrite($f, $content);
3450 return new IXR_Error(500, __('Either the file is not writable, or something wrong happened. The file has not been updated.'));
3461 * @param array $args Method parameters.
3464 function blogger_newPost($args) {
3466 $this->escape($args);
3468 $blog_ID = (int) $args[1]; /* though we don't use it yet */
3469 $username = $args[2];
3470 $password = $args[3];
3471 $content = $args[4];
3472 $publish = $args[5];
3474 if ( !$user = $this->login($username, $password) )
3475 return $this->error;
3477 do_action('xmlrpc_call', 'blogger.newPost');
3479 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
3480 if ( !current_user_can($cap) )
3481 return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
3483 $post_status = ($publish) ? 'publish' : 'draft';
3485 $post_author = $user->ID;
3487 $post_title = xmlrpc_getposttitle($content);
3488 $post_category = xmlrpc_getpostcategory($content);
3489 $post_content = xmlrpc_removepostdata($content);
3491 $post_date = current_time('mysql');
3492 $post_date_gmt = current_time('mysql', 1);
3494 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
3496 $post_ID = wp_insert_post($post_data);
3497 if ( is_wp_error( $post_ID ) )
3498 return new IXR_Error(500, $post_ID->get_error_message());
3501 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
3503 $this->attach_uploads( $post_ID, $post_content );
3505 do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
3515 * @param array $args Method parameters.
3516 * @return bool true when done.
3518 function blogger_editPost($args) {
3520 $this->escape($args);
3522 $post_ID = (int) $args[1];
3523 $username = $args[2];
3524 $password = $args[3];
3525 $content = $args[4];
3526 $publish = $args[5];
3528 if ( !$user = $this->login($username, $password) )
3529 return $this->error;
3531 do_action('xmlrpc_call', 'blogger.editPost');
3533 $actual_post = wp_get_single_post($post_ID,ARRAY_A);
3535 if ( !$actual_post || $actual_post['post_type'] != 'post' )
3536 return new IXR_Error(404, __('Sorry, no such post.'));
3538 $this->escape($actual_post);
3540 if ( !current_user_can('edit_post', $post_ID) )
3541 return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
3543 extract($actual_post, EXTR_SKIP);
3545 if ( ('publish' == $post_status) && !current_user_can('publish_posts') )
3546 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
3548 $post_title = xmlrpc_getposttitle($content);
3549 $post_category = xmlrpc_getpostcategory($content);
3550 $post_content = xmlrpc_removepostdata($content);
3552 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
3554 $result = wp_update_post($postdata);
3557 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
3559 $this->attach_uploads( $ID, $post_content );
3561 do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
3571 * @param array $args Method parameters.
3572 * @return bool True when post is deleted.
3574 function blogger_deletePost($args) {
3575 $this->escape($args);
3577 $post_ID = (int) $args[1];
3578 $username = $args[2];
3579 $password = $args[3];
3580 $publish = $args[4];
3582 if ( !$user = $this->login($username, $password) )
3583 return $this->error;
3585 do_action('xmlrpc_call', 'blogger.deletePost');
3587 $actual_post = wp_get_single_post($post_ID,ARRAY_A);
3589 if ( !$actual_post || $actual_post['post_type'] != 'post' )
3590 return new IXR_Error(404, __('Sorry, no such post.'));
3592 if ( !current_user_can('delete_post', $post_ID) )
3593 return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
3595 $result = wp_delete_post($post_ID);
3598 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
3600 do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
3605 /* MetaWeblog API functions
3606 * specs on wherever Dave Winer wants them to be
3610 * Create a new post.
3612 * The 'content_struct' argument must contain:
3621 * Also, it can optionally contain:
3624 * - wp_page_parent_id
3627 * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
3628 * - mt_allow_comments - can be 'open' or 'closed'
3629 * - mt_allow_pings - can be 'open' or 'closed'
3630 * - date_created_gmt
3632 * - wp_post_thumbnail
3636 * @param array $args Method parameters. Contains:
3644 function mw_newPost($args) {
3645 $this->escape($args);
3647 $blog_ID = (int) $args[0];
3648 $username = $args[1];
3649 $password = $args[2];
3650 $content_struct = $args[3];
3651 $publish = isset( $args[4] ) ? $args[4] : 0;
3653 if ( !$user = $this->login($username, $password) )
3654 return $this->error;
3656 do_action('xmlrpc_call', 'metaWeblog.newPost');
3658 $page_template = '';
3659 if ( !empty( $content_struct['post_type'] ) ) {
3660 if ( $content_struct['post_type'] == 'page' ) {
3662 $cap = 'publish_pages';
3663 elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
3664 $cap = 'publish_pages';
3666 $cap = 'edit_pages';
3667 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
3668 $post_type = 'page';
3669 if ( !empty( $content_struct['wp_page_template'] ) )
3670 $page_template = $content_struct['wp_page_template'];
3671 } elseif ( $content_struct['post_type'] == 'post' ) {
3673 $cap = 'publish_posts';
3674 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
3675 $cap = 'publish_posts';
3677 $cap = 'edit_posts';
3678 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
3679 $post_type = 'post';
3681 // No other post_type values are allowed here
3682 return new IXR_Error( 401, __( 'Invalid post type' ) );
3686 $cap = 'publish_posts';
3687 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
3688 $cap = 'publish_posts';
3690 $cap = 'edit_posts';
3691 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
3692 $post_type = 'post';
3695 if ( !current_user_can( $cap ) )
3696 return new IXR_Error( 401, $error_message );
3698 // Check for a valid post format if one was given
3699 if ( isset( $content_struct['wp_post_format'] ) ) {
3700 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
3701 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
3702 return new IXR_Error( 404, __( 'Invalid post format' ) );
3706 // Let WordPress generate the post_name (slug) unless
3707 // one has been provided.
3709 if ( isset($content_struct['wp_slug']) )
3710 $post_name = $content_struct['wp_slug'];
3712 // Only use a password if one was given.
3713 if ( isset($content_struct['wp_password']) )
3714 $post_password = $content_struct['wp_password'];
3716 // Only set a post parent if one was provided.
3717 if ( isset($content_struct['wp_page_parent_id']) )
3718 $post_parent = $content_struct['wp_page_parent_id'];
3720 // Only set the menu_order if it was provided.
3721 if ( isset($content_struct['wp_page_order']) )
3722 $menu_order = $content_struct['wp_page_order'];
3724 $post_author = $user->ID;
3726 // If an author id was provided then use it instead.
3727 if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
3728 switch ( $post_type ) {
3730 if ( !current_user_can('edit_others_posts') )
3731 return(new IXR_Error(401, __('You are not allowed to post as this user')));
3734 if ( !current_user_can('edit_others_pages') )
3735 return(new IXR_Error(401, __('You are not allowed to create pages as this user')));
3738 return(new IXR_Error(401, __('Invalid post type')));
3741 $author = get_userdata( $content_struct['wp_author_id'] );
3743 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
3744 $post_author = $content_struct['wp_author_id'];
3747 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
3748 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
3750 $post_status = $publish ? 'publish' : 'draft';
3752 if ( isset( $content_struct["{$post_type}_status"] ) ) {
3753 switch ( $content_struct["{$post_type}_status"] ) {
3758 $post_status = $content_struct["{$post_type}_status"];
3761 $post_status = $publish ? 'publish' : 'draft';
3766 $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
3767 $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
3769 $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
3771 if ( isset($content_struct['mt_allow_comments']) ) {
3772 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
3773 switch ( $content_struct['mt_allow_comments'] ) {
3775 $comment_status = 'closed';
3778 $comment_status = 'open';
3781 $comment_status = get_option('default_comment_status');
3785 switch ( (int) $content_struct['mt_allow_comments'] ) {
3788 $comment_status = 'closed';
3791 $comment_status = 'open';
3794 $comment_status = get_option('default_comment_status');
3799 $comment_status = get_option('default_comment_status');
3802 if ( isset($content_struct['mt_allow_pings']) ) {
3803 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
3804 switch ( $content_struct['mt_allow_pings'] ) {
3806 $ping_status = 'closed';
3809 $ping_status = 'open';
3812 $ping_status = get_option('default_ping_status');
3816 switch ( (int) $content_struct['mt_allow_pings'] ) {
3818 $ping_status = 'closed';
3821 $ping_status = 'open';
3824 $ping_status = get_option('default_ping_status');
3829 $ping_status = get_option('default_ping_status');
3833 $post_content = $post_content . '<!--more-->' . $post_more;
3836 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
3837 $to_ping = $content_struct['mt_tb_ping_urls'];
3838 if ( is_array($to_ping) )
3839 $to_ping = implode(' ', $to_ping);
3842 // Do some timestamp voodoo
3843 if ( !empty( $content_struct['date_created_gmt'] ) )
3844 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
3845 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
3846 elseif ( !empty( $content_struct['dateCreated']) )
3847 $dateCreated = $content_struct['dateCreated']->getIso();
3849 if ( !empty( $dateCreated ) ) {
3850 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
3851 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
3853 $post_date = current_time('mysql');
3854 $post_date_gmt = current_time('mysql', 1);
3857 $post_category = array();
3858 if ( isset( $content_struct['categories'] ) ) {
3859 $catnames = $content_struct['categories'];
3861 if ( is_array($catnames) ) {
3862 foreach ($catnames as $cat) {
3863 $post_category[] = get_cat_ID($cat);
3868 $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');
3870 $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
3872 // Only posts can be sticky
3873 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
3874 if ( $content_struct['sticky'] == true )
3875 stick_post( $post_ID );
3876 elseif ( $content_struct['sticky'] == false )
3877 unstick_post( $post_ID );
3880 if ( isset($content_struct['custom_fields']) )
3881 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
3883 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
3884 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
3885 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
3887 unset( $content_struct['wp_post_thumbnail'] );
3890 // Handle enclosures
3891 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
3892 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
3894 $this->attach_uploads( $post_ID, $post_content );
3896 // Handle post formats if assigned, value is validated earlier
3898 if ( isset( $content_struct['wp_post_format'] ) )
3899 wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
3901 $post_ID = wp_insert_post( $postdata, true );
3902 if ( is_wp_error( $post_ID ) )
3903 return new IXR_Error(500, $post_ID->get_error_message());
3906 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
3908 do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
3910 return strval($post_ID);
3913 function add_enclosure_if_new($post_ID, $enclosure) {
3914 if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
3916 $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'];
3918 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
3919 if ($key == 'enclosure') {
3920 foreach ( (array) $val as $enc ) {
3921 if ($enc == $encstring) {
3929 add_post_meta( $post_ID, 'enclosure', $encstring );
3934 * Attach upload to a post.
3938 * @param int $post_ID Post ID.
3939 * @param string $post_content Post Content for attachment.
3941 function attach_uploads( $post_ID, $post_content ) {
3944 // find any unattached files
3945 $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
3946 if ( is_array( $attachments ) ) {
3947 foreach ( $attachments as $file ) {
3948 if ( strpos( $post_content, $file->guid ) !== false )
3949 $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
3959 * @param array $args Method parameters.
3960 * @return bool True on success.
3962 function mw_editPost($args) {
3964 $this->escape($args);
3966 $post_ID = (int) $args[0];
3967 $username = $args[1];
3968 $password = $args[2];
3969 $content_struct = $args[3];
3970 $publish = isset( $args[4] ) ? $args[4] : 0;
3972 if ( ! $user = $this->login($username, $password) )
3973 return $this->error;
3975 do_action('xmlrpc_call', 'metaWeblog.editPost');
3977 $postdata = wp_get_single_post( $post_ID, ARRAY_A );
3979 // If there is no post data for the give post id, stop
3980 // now and return an error. Other wise a new post will be
3981 // created (which was the old behavior).
3982 if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
3983 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3985 if ( ! current_user_can( 'edit_post', $post_ID ) )
3986 return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this post.' ) );
3988 // Use wp.editPost to edit post types other than post and page.
3989 if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
3990 return new IXR_Error( 401, __( 'Invalid post type' ) );
3992 // Thwart attempt to change the post type.
3993 if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
3994 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
3996 // Check for a valid post format if one was given
3997 if ( isset( $content_struct['wp_post_format'] ) ) {
3998 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
3999 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4000 return new IXR_Error( 404, __( 'Invalid post format' ) );
4004 $this->escape($postdata);
4005 extract($postdata, EXTR_SKIP);
4007 // Let WordPress manage slug if none was provided.
4009 $post_name = $postdata['post_name'];
4010 if ( isset($content_struct['wp_slug']) )
4011 $post_name = $content_struct['wp_slug'];
4013 // Only use a password if one was given.
4014 if ( isset($content_struct['wp_password']) )
4015 $post_password = $content_struct['wp_password'];
4017 // Only set a post parent if one was given.
4018 if ( isset($content_struct['wp_page_parent_id']) )
4019 $post_parent = $content_struct['wp_page_parent_id'];
4021 // Only set the menu_order if it was given.
4022 if ( isset($content_struct['wp_page_order']) )
4023 $menu_order = $content_struct['wp_page_order'];
4025 if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
4026 $page_template = $content_struct['wp_page_template'];
4028 $post_author = $postdata['post_author'];
4030 // Only set the post_author if one is set.
4031 if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
4032 switch ( $post_type ) {
4034 if ( !current_user_can('edit_others_posts') )
4035 return(new IXR_Error(401, __('You are not allowed to change the post author as this user.')));
4038 if ( !current_user_can('edit_others_pages') )
4039 return(new IXR_Error(401, __('You are not allowed to change the page author as this user.')));
4042 return(new IXR_Error(401, __('Invalid post type')));
4045 $post_author = $content_struct['wp_author_id'];
4048 if ( isset($content_struct['mt_allow_comments']) ) {
4049 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4050 switch ( $content_struct['mt_allow_comments'] ) {
4052 $comment_status = 'closed';
4055 $comment_status = 'open';
4058 $comment_status = get_option('default_comment_status');
4062 switch ( (int) $content_struct['mt_allow_comments'] ) {
4065 $comment_status = 'closed';
4068 $comment_status = 'open';
4071 $comment_status = get_option('default_comment_status');
4077 if ( isset($content_struct['mt_allow_pings']) ) {
4078 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4079 switch ( $content_struct['mt_allow_pings'] ) {
4081 $ping_status = 'closed';
4084 $ping_status = 'open';
4087 $ping_status = get_option('default_ping_status');
4091 switch ( (int) $content_struct["mt_allow_pings"] ) {
4093 $ping_status = 'closed';
4096 $ping_status = 'open';
4099 $ping_status = get_option('default_ping_status');
4105 if ( isset( $content_struct['title'] ) )
4106 $post_title = $content_struct['title'];
4108 if ( isset( $content_struct['description'] ) )
4109 $post_content = $content_struct['description'];
4111 $post_category = array();
4112 if ( isset( $content_struct['categories'] ) ) {
4113 $catnames = $content_struct['categories'];
4114 if ( is_array($catnames) ) {
4115 foreach ($catnames as $cat) {
4116 $post_category[] = get_cat_ID($cat);
4121 if ( isset( $content_struct['mt_excerpt'] ) )
4122 $post_excerpt = $content_struct['mt_excerpt'];
4124 $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
4126 $post_status = $publish ? 'publish' : 'draft';
4127 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4128 switch( $content_struct["{$post_type}_status"] ) {
4133 $post_status = $content_struct["{$post_type}_status"];
4136 $post_status = $publish ? 'publish' : 'draft';
4141 $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
4143 if ( ('publish' == $post_status) ) {
4144 if ( ( 'page' == $post_type ) && !current_user_can('publish_pages') )
4145 return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
4146 else if ( !current_user_can('publish_posts') )
4147 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
4151 $post_content = $post_content . "<!--more-->" . $post_more;
4154 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4155 $to_ping = $content_struct['mt_tb_ping_urls'];
4156 if ( is_array($to_ping) )
4157 $to_ping = implode(' ', $to_ping);
4160 // Do some timestamp voodoo
4161 if ( !empty( $content_struct['date_created_gmt'] ) )
4162 // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4163 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4164 elseif ( !empty( $content_struct['dateCreated']) )
4165 $dateCreated = $content_struct['dateCreated']->getIso();
4167 if ( !empty( $dateCreated ) ) {
4168 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4169 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4171 $post_date = $postdata['post_date'];
4172 $post_date_gmt = $postdata['post_date_gmt'];
4175 // We've got all the data -- post it:
4176 $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');
4178 $result = wp_update_post($newpost, true);
4179 if ( is_wp_error( $result ) )
4180 return new IXR_Error(500, $result->get_error_message());
4183 return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
4185 // Only posts can be sticky
4186 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4187 if ( $content_struct['sticky'] == true )
4188 stick_post( $post_ID );
4189 elseif ( $content_struct['sticky'] == false )
4190 unstick_post( $post_ID );
4193 if ( isset($content_struct['custom_fields']) )
4194 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4196 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4197 // empty value deletes, non-empty value adds/updates
4198 if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
4199 delete_post_thumbnail( $post_ID );
4201 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4202 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4204 unset( $content_struct['wp_post_thumbnail'] );
4207 // Handle enclosures
4208 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4209 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4211 $this->attach_uploads( $ID, $post_content );
4213 // Handle post formats if assigned, validation is handled
4214 // earlier in this function
4215 if ( isset( $content_struct['wp_post_format'] ) )
4216 wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
4218 do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
4228 * @param array $args Method parameters.
4231 function mw_getPost($args) {
4233 $this->escape($args);
4235 $post_ID = (int) $args[0];
4236 $username = $args[1];
4237 $password = $args[2];
4239 if ( !$user = $this->login($username, $password) )
4240 return $this->error;
4242 $postdata = wp_get_single_post($post_ID, ARRAY_A);
4244 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4246 if ( !current_user_can( 'edit_post', $post_ID ) )
4247 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4249 do_action('xmlrpc_call', 'metaWeblog.getPost');
4251 if ($postdata['post_date'] != '') {
4252 $post_date = $this->_convert_date( $postdata['post_date'] );
4253 $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'], $postdata['post_date'] );
4254 $post_modified = $this->_convert_date( $postdata['post_modified'] );
4255 $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
4257 $categories = array();
4258 $catids = wp_get_post_categories($post_ID);
4259 foreach($catids as $catid)
4260 $categories[] = get_cat_name($catid);
4262 $tagnames = array();
4263 $tags = wp_get_post_tags( $post_ID );
4264 if ( !empty( $tags ) ) {
4265 foreach ( $tags as $tag )
4266 $tagnames[] = $tag->name;
4267 $tagnames = implode( ', ', $tagnames );
4272 $post = get_extended($postdata['post_content']);
4273 $link = post_permalink($postdata['ID']);
4275 // Get the author info.
4276 $author = get_userdata($postdata['post_author']);
4278 $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
4279 $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
4281 // Consider future posts as published
4282 if ( $postdata['post_status'] === 'future' )
4283 $postdata['post_status'] = 'publish';
4286 $post_format = get_post_format( $post_ID );
4287 if ( empty( $post_format ) )
4288 $post_format = 'standard';
4291 if ( is_sticky( $post_ID ) )
4294 $enclosure = array();
4295 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
4296 if ($key == 'enclosure') {
4297 foreach ( (array) $val as $enc ) {
4298 $encdata = explode("\n", $enc);
4299 $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
4300 $enclosure['length'] = (int) trim($encdata[1]);
4301 $enclosure['type'] = trim($encdata[2]);
4308 'dateCreated' => $post_date,
4309 'userid' => $postdata['post_author'],
4310 'postid' => $postdata['ID'],
4311 'description' => $post['main'],
4312 'title' => $postdata['post_title'],
4314 'permaLink' => $link,
4315 // commented out because no other tool seems to use this
4316 // 'content' => $entry['post_content'],
4317 'categories' => $categories,
4318 'mt_excerpt' => $postdata['post_excerpt'],
4319 'mt_text_more' => $post['extended'],
4320 'wp_more_text' => $post['more_text'],
4321 'mt_allow_comments' => $allow_comments,
4322 'mt_allow_pings' => $allow_pings,
4323 'mt_keywords' => $tagnames,
4324 'wp_slug' => $postdata['post_name'],
4325 'wp_password' => $postdata['post_password'],
4326 'wp_author_id' => (string) $author->ID,
4327 'wp_author_display_name' => $author->display_name,
4328 'date_created_gmt' => $post_date_gmt,
4329 'post_status' => $postdata['post_status'],
4330 'custom_fields' => $this->get_custom_fields($post_ID),
4331 'wp_post_format' => $post_format,
4332 'sticky' => $sticky,
4333 'date_modified' => $post_modified,
4334 'date_modified_gmt' => $post_modified_gmt
4337 if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
4339 $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
4343 return new IXR_Error(404, __('Sorry, no such post.'));
4348 * Retrieve list of recent posts.
4352 * @param array $args Method parameters.
4355 function mw_getRecentPosts($args) {
4357 $this->escape($args);
4359 $blog_ID = (int) $args[0];
4360 $username = $args[1];
4361 $password = $args[2];
4362 if ( isset( $args[3] ) )
4363 $query = array( 'numberposts' => absint( $args[3] ) );
4367 if ( !$user = $this->login($username, $password) )
4368 return $this->error;
4370 do_action('xmlrpc_call', 'metaWeblog.getRecentPosts');
4372 $posts_list = wp_get_recent_posts( $query );
4377 foreach ($posts_list as $entry) {
4378 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
4381 $post_date = $this->_convert_date( $entry['post_date'] );
4382 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
4383 $post_modified = $this->_convert_date( $entry['post_modified'] );
4384 $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
4386 $categories = array();
4387 $catids = wp_get_post_categories($entry['ID']);
4388 foreach( $catids as $catid )
4389 $categories[] = get_cat_name($catid);
4391 $tagnames = array();
4392 $tags = wp_get_post_tags( $entry['ID'] );
4393 if ( !empty( $tags ) ) {
4394 foreach ( $tags as $tag ) {
4395 $tagnames[] = $tag->name;
4397 $tagnames = implode( ', ', $tagnames );
4402 $post = get_extended($entry['post_content']);
4403 $link = post_permalink($entry['ID']);
4405 // Get the post author info.
4406 $author = get_userdata($entry['post_author']);
4408 $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
4409 $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
4411 // Consider future posts as published
4412 if ( $entry['post_status'] === 'future' )
4413 $entry['post_status'] = 'publish';
4416 $post_format = get_post_format( $entry['ID'] );
4417 if ( empty( $post_format ) )
4418 $post_format = 'standard';
4421 'dateCreated' => $post_date,
4422 'userid' => $entry['post_author'],
4423 'postid' => (string) $entry['ID'],
4424 'description' => $post['main'],
4425 'title' => $entry['post_title'],
4427 'permaLink' => $link,
4428 // commented out because no other tool seems to use this
4429 // 'content' => $entry['post_content'],
4430 'categories' => $categories,
4431 'mt_excerpt' => $entry['post_excerpt'],
4432 'mt_text_more' => $post['extended'],
4433 'wp_more_text' => $post['more_text'],
4434 'mt_allow_comments' => $allow_comments,
4435 'mt_allow_pings' => $allow_pings,
4436 'mt_keywords' => $tagnames,
4437 'wp_slug' => $entry['post_name'],
4438 'wp_password' => $entry['post_password'],
4439 'wp_author_id' => (string) $author->ID,
4440 'wp_author_display_name' => $author->display_name,
4441 'date_created_gmt' => $post_date_gmt,
4442 'post_status' => $entry['post_status'],
4443 'custom_fields' => $this->get_custom_fields($entry['ID']),
4444 'wp_post_format' => $post_format,
4445 'date_modified' => $post_modified,
4446 'date_modified_gmt' => $post_modified_gmt
4449 $entry_index = count( $struct ) - 1;
4450 $struct[ $entry_index ][ 'wp_post_thumbnail' ] = get_post_thumbnail_id( $entry['ID'] );
4453 $recent_posts = array();
4454 for ( $j=0; $j<count($struct); $j++ ) {
4455 array_push($recent_posts, $struct[$j]);
4458 return $recent_posts;
4462 * Retrieve the list of categories on a given blog.
4466 * @param array $args Method parameters.
4469 function mw_getCategories($args) {
4471 $this->escape($args);
4473 $blog_ID = (int) $args[0];
4474 $username = $args[1];
4475 $password = $args[2];
4477 if ( !$user = $this->login($username, $password) )
4478 return $this->error;
4480 if ( !current_user_can( 'edit_posts' ) )
4481 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
4483 do_action('xmlrpc_call', 'metaWeblog.getCategories');
4485 $categories_struct = array();
4487 if ( $cats = get_categories(array('get' => 'all')) ) {
4488 foreach ( $cats as $cat ) {
4489 $struct['categoryId'] = $cat->term_id;
4490 $struct['parentId'] = $cat->parent;
4491 $struct['description'] = $cat->name;
4492 $struct['categoryDescription'] = $cat->description;
4493 $struct['categoryName'] = $cat->name;
4494 $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
4495 $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
4497 $categories_struct[] = $struct;
4501 return $categories_struct;
4505 * Uploads a file, following your settings.
4507 * Adapted from a patch by Johann Richard.
4509 * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
4513 * @param array $args Method parameters.
4516 function mw_newMediaObject($args) {
4519 $blog_ID = (int) $args[0];
4520 $username = $wpdb->escape($args[1]);
4521 $password = $wpdb->escape($args[2]);
4524 $name = sanitize_file_name( $data['name'] );
4525 $type = $data['type'];
4526 $bits = $data['bits'];
4528 if ( !$user = $this->login($username, $password) )
4529 return $this->error;
4531 do_action('xmlrpc_call', 'metaWeblog.newMediaObject');
4533 if ( !current_user_can('upload_files') ) {
4534 $this->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
4535 return $this->error;
4538 if ( $upload_err = apply_filters( 'pre_upload_error', false ) )
4539 return new IXR_Error(500, $upload_err);
4541 if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) {
4542 // Get postmeta info on the object.
4543 $old_file = $wpdb->get_row("
4546 WHERE post_title = '{$name}'
4547 AND post_type = 'attachment'
4550 // Delete previous file.
4551 wp_delete_attachment($old_file->ID);
4553 // Make sure the new name is different by pre-pending the
4554 // previous post id.
4555 $filename = preg_replace('/^wpid\d+-/', '', $name);
4556 $name = "wpid{$old_file->ID}-{$filename}";
4559 $upload = wp_upload_bits($name, null, $bits);
4560 if ( ! empty($upload['error']) ) {
4561 $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
4562 return new IXR_Error(500, $errorString);
4564 // Construct the attachment array
4565 // attach to post_id 0
4567 $attachment = array(
4568 'post_title' => $name,
4569 'post_content' => '',
4570 'post_type' => 'attachment',
4571 'post_parent' => $post_id,
4572 'post_mime_type' => $type,
4573 'guid' => $upload[ 'url' ]
4577 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
4578 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
4580 do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
4583 'id' => strval( $id ),
4585 'url' => $upload[ 'url' ],
4588 return apply_filters( 'wp_handle_upload', $struct, 'upload' );
4591 /* MovableType API functions
4592 * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
4596 * Retrieve the post titles of recent posts.
4600 * @param array $args Method parameters.
4603 function mt_getRecentPostTitles($args) {
4605 $this->escape($args);
4607 $blog_ID = (int) $args[0];
4608 $username = $args[1];
4609 $password = $args[2];
4610 if ( isset( $args[3] ) )
4611 $query = array( 'numberposts' => absint( $args[3] ) );
4615 if ( !$user = $this->login($username, $password) )
4616 return $this->error;
4618 do_action('xmlrpc_call', 'mt.getRecentPostTitles');
4620 $posts_list = wp_get_recent_posts( $query );
4622 if ( !$posts_list ) {
4623 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
4624 return $this->error;
4629 foreach ($posts_list as $entry) {
4630 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
4633 $post_date = $this->_convert_date( $entry['post_date'] );
4634 $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
4637 'dateCreated' => $post_date,
4638 'userid' => $entry['post_author'],
4639 'postid' => (string) $entry['ID'],
4640 'title' => $entry['post_title'],
4641 'post_status' => $entry['post_status'],
4642 'date_created_gmt' => $post_date_gmt
4647 $recent_posts = array();
4648 for ( $j=0; $j<count($struct); $j++ ) {
4649 array_push($recent_posts, $struct[$j]);
4652 return $recent_posts;
4656 * Retrieve list of all categories on blog.
4660 * @param array $args Method parameters.
4663 function mt_getCategoryList($args) {
4665 $this->escape($args);
4667 $blog_ID = (int) $args[0];
4668 $username = $args[1];
4669 $password = $args[2];
4671 if ( !$user = $this->login($username, $password) )
4672 return $this->error;
4674 if ( !current_user_can( 'edit_posts' ) )
4675 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
4677 do_action('xmlrpc_call', 'mt.getCategoryList');
4679 $categories_struct = array();
4681 if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
4682 foreach ( $cats as $cat ) {
4683 $struct['categoryId'] = $cat->term_id;
4684 $struct['categoryName'] = $cat->name;
4686 $categories_struct[] = $struct;
4690 return $categories_struct;
4694 * Retrieve post categories.
4698 * @param array $args Method parameters.
4701 function mt_getPostCategories($args) {
4703 $this->escape($args);
4705 $post_ID = (int) $args[0];
4706 $username = $args[1];
4707 $password = $args[2];
4709 if ( !$user = $this->login($username, $password) )
4710 return $this->error;
4712 if ( ! get_post( $post_ID ) )
4713 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4715 if ( !current_user_can( 'edit_post', $post_ID ) )
4716 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
4718 do_action('xmlrpc_call', 'mt.getPostCategories');
4720 $categories = array();
4721 $catids = wp_get_post_categories(intval($post_ID));
4722 // first listed category will be the primary category
4724 foreach ( $catids as $catid ) {
4725 $categories[] = array(
4726 'categoryName' => get_cat_name($catid),
4727 'categoryId' => (string) $catid,
4728 'isPrimary' => $isPrimary
4737 * Sets categories for a post.
4741 * @param array $args Method parameters.
4742 * @return bool True on success.
4744 function mt_setPostCategories($args) {
4746 $this->escape($args);
4748 $post_ID = (int) $args[0];
4749 $username = $args[1];
4750 $password = $args[2];
4751 $categories = $args[3];
4753 if ( !$user = $this->login($username, $password) )
4754 return $this->error;
4756 do_action('xmlrpc_call', 'mt.setPostCategories');
4758 if ( ! get_post( $post_ID ) )
4759 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4761 if ( !current_user_can('edit_post', $post_ID) )
4762 return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
4764 foreach ( $categories as $cat ) {
4765 $catids[] = $cat['categoryId'];
4768 wp_set_post_categories($post_ID, $catids);
4774 * Retrieve an array of methods supported by this server.
4778 * @param array $args Method parameters.
4781 function mt_supportedMethods($args) {
4783 do_action('xmlrpc_call', 'mt.supportedMethods');
4785 $supported_methods = array();
4786 foreach ( $this->methods as $key => $value ) {
4787 $supported_methods[] = $key;
4790 return $supported_methods;
4794 * Retrieve an empty array because we don't support per-post text filters.
4798 * @param array $args Method parameters.
4800 function mt_supportedTextFilters($args) {
4801 do_action('xmlrpc_call', 'mt.supportedTextFilters');
4802 return apply_filters('xmlrpc_text_filters', array());
4806 * Retrieve trackbacks sent to a given post.
4810 * @param array $args Method parameters.
4813 function mt_getTrackbackPings($args) {
4817 $post_ID = intval($args);
4819 do_action('xmlrpc_call', 'mt.getTrackbackPings');
4821 $actual_post = wp_get_single_post($post_ID, ARRAY_A);
4823 if ( !$actual_post )
4824 return new IXR_Error(404, __('Sorry, no such post.'));
4826 $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) );
4831 $trackback_pings = array();
4832 foreach ( $comments as $comment ) {
4833 if ( 'trackback' == $comment->comment_type ) {
4834 $content = $comment->comment_content;
4835 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
4836 $trackback_pings[] = array(
4837 'pingTitle' => $title,
4838 'pingURL' => $comment->comment_author_url,
4839 'pingIP' => $comment->comment_author_IP
4844 return $trackback_pings;
4848 * Sets a post's publish status to 'publish'.
4852 * @param array $args Method parameters.
4855 function mt_publishPost($args) {
4857 $this->escape($args);
4859 $post_ID = (int) $args[0];
4860 $username = $args[1];
4861 $password = $args[2];
4863 if ( !$user = $this->login($username, $password) )
4864 return $this->error;
4866 do_action('xmlrpc_call', 'mt.publishPost');
4868 $postdata = wp_get_single_post($post_ID, ARRAY_A);
4870 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4872 if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
4873 return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
4875 $postdata['post_status'] = 'publish';
4878 $cats = wp_get_post_categories($post_ID);
4879 $postdata['post_category'] = $cats;
4880 $this->escape($postdata);
4882 $result = wp_update_post($postdata);
4887 /* PingBack functions
4888 * specs on www.hixie.ch/specs/pingback/pingback
4892 * Retrieves a pingback and registers it.
4896 * @param array $args Method parameters.
4899 function pingback_ping($args) {
4902 do_action('xmlrpc_call', 'pingback.ping');
4904 $this->escape($args);
4906 $pagelinkedfrom = $args[0];
4907 $pagelinkedto = $args[1];
4911 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
4912 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
4913 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
4915 // Check if the page linked to is in our site
4916 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
4918 return new IXR_Error(0, __('Is there no link to us?'));
4920 // let's find which post is linked to
4921 // FIXME: does url_to_postid() cover all these cases already?
4922 // if so, then let's use it and drop the old code.
4923 $urltest = parse_url($pagelinkedto);
4924 if ( $post_ID = url_to_postid($pagelinkedto) ) {
4925 $way = 'url_to_postid()';
4926 } elseif ( preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
4927 // the path defines the post_ID (archives/p/XXXX)
4928 $blah = explode('/', $match[0]);
4929 $post_ID = (int) $blah[1];
4930 $way = 'from the path';
4931 } elseif ( preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
4932 // the querystring defines the post_ID (?p=XXXX)
4933 $blah = explode('=', $match[0]);
4934 $post_ID = (int) $blah[1];
4935 $way = 'from the querystring';
4936 } elseif ( isset($urltest['fragment']) ) {
4937 // an #anchor is there, it's either...
4938 if ( intval($urltest['fragment']) ) {
4939 // ...an integer #XXXX (simplest case)
4940 $post_ID = (int) $urltest['fragment'];
4941 $way = 'from the fragment (numeric)';
4942 } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
4943 // ...a post id in the form 'post-###'
4944 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
4945 $way = 'from the fragment (post-###)';
4946 } elseif ( is_string($urltest['fragment']) ) {
4947 // ...or a string #title, a little more complicated
4948 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
4949 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", like_escape( $title ) );
4950 if (! ($post_ID = $wpdb->get_var($sql)) ) {
4951 // returning unknown error '0' is better than die()ing
4952 return new IXR_Error(0, '');
4954 $way = 'from the fragment (title)';
4957 // TODO: Attempt to extract a post ID from the given URL
4958 return new IXR_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.'));
4960 $post_ID = (int) $post_ID;
4962 $post = get_post($post_ID);
4964 if ( !$post ) // Post_ID not found
4965 return new IXR_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.'));
4967 if ( $post_ID == url_to_postid($pagelinkedfrom) )
4968 return new IXR_Error(0, __('The source URL and the target URL cannot both point to the same resource.'));
4970 // Check if pings are on
4971 if ( !pings_open($post) )
4972 return new IXR_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.'));
4974 // Let's check that the remote site didn't already pingback this entry
4975 if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
4976 return new IXR_Error( 48, __( 'The pingback has already been registered.' ) );
4978 // very stupid, but gives time to the 'from' server to publish !
4981 // Let's check the remote site
4982 $linea = wp_remote_fopen( $pagelinkedfrom );
4984 return new IXR_Error(16, __('The source URL does not exist.'));
4986 $linea = apply_filters('pre_remote_source', $linea, $pagelinkedto);
4988 // Work around bug in strip_tags():
4989 $linea = str_replace('<!DOC', '<DOC', $linea);
4990 $linea = preg_replace( '/[\s\r\n\t]+/', ' ', $linea ); // normalize spaces
4991 $linea = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
4993 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
4994 $title = $matchtitle[1];
4995 if ( empty( $title ) )
4996 return new IXR_Error(32, __('We cannot find a title on that page.'));
4998 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
5000 $p = explode( "\n\n", $linea );
5002 $preg_target = preg_quote($pagelinkedto, '|');
5004 foreach ( $p as $para ) {
5005 if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
5006 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
5008 // If the URL isn't in a link context, keep looking
5009 if ( empty($context) )
5012 // We're going to use this fake tag to mark the context in a bit
5013 // the marker is needed in case the link text appears more than once in the paragraph
5014 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
5016 // prevent really long link text
5017 if ( strlen($context[1]) > 100 )
5018 $context[1] = substr($context[1], 0, 100) . '...';
5020 $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
5021 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
5022 $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
5023 $excerpt = trim($excerpt);
5024 $preg_marker = preg_quote($marker, '|');
5025 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
5026 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
5031 if ( empty($context) ) // Link to target not found
5032 return new IXR_Error(17, __('The source URL does not contain a link to the target URL, and so cannot be used as a source.'));
5034 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
5036 $context = '[...] ' . esc_html( $excerpt ) . ' [...]';
5037 $pagelinkedfrom = $wpdb->escape( $pagelinkedfrom );
5039 $comment_post_ID = (int) $post_ID;
5040 $comment_author = $title;
5041 $comment_author_email = '';
5042 $this->escape($comment_author);
5043 $comment_author_url = $pagelinkedfrom;
5044 $comment_content = $context;
5045 $this->escape($comment_content);
5046 $comment_type = 'pingback';
5048 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
5050 $comment_ID = wp_new_comment($commentdata);
5051 do_action('pingback_post', $comment_ID);
5053 return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
5057 * Retrieve array of URLs that pingbacked the given URL.
5059 * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
5063 * @param array $args Method parameters.
5066 function pingback_extensions_getPingbacks($args) {
5070 do_action('xmlrpc_call', 'pingback.extensions.getPingbacks');
5072 $this->escape($args);
5076 $post_ID = url_to_postid($url);
5078 // We aren't sure that the resource is available and/or pingback enabled
5079 return new IXR_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.'));
5082 $actual_post = wp_get_single_post($post_ID, ARRAY_A);
5084 if ( !$actual_post ) {
5085 // No such post = resource not found
5086 return new IXR_Error(32, __('The specified target URL does not exist.'));
5089 $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) );
5094 $pingbacks = array();
5095 foreach ( $comments as $comment ) {
5096 if ( 'pingback' == $comment->comment_type )
5097 $pingbacks[] = $comment->comment_author_url;