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.getPage' => 'this:wp_getPage',
40 'wp.getPages' => 'this:wp_getPages',
41 'wp.newPage' => 'this:wp_newPage',
42 'wp.deletePage' => 'this:wp_deletePage',
43 'wp.editPage' => 'this:wp_editPage',
44 'wp.getPageList' => 'this:wp_getPageList',
45 'wp.getAuthors' => 'this:wp_getAuthors',
46 'wp.getCategories' => 'this:mw_getCategories', // Alias
47 'wp.getTags' => 'this:wp_getTags',
48 'wp.newCategory' => 'this:wp_newCategory',
49 'wp.deleteCategory' => 'this:wp_deleteCategory',
50 'wp.suggestCategories' => 'this:wp_suggestCategories',
51 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
52 'wp.getCommentCount' => 'this:wp_getCommentCount',
53 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
54 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
55 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
56 'wp.getOptions' => 'this:wp_getOptions',
57 'wp.setOptions' => 'this:wp_setOptions',
58 'wp.getComment' => 'this:wp_getComment',
59 'wp.getComments' => 'this:wp_getComments',
60 'wp.deleteComment' => 'this:wp_deleteComment',
61 'wp.editComment' => 'this:wp_editComment',
62 'wp.newComment' => 'this:wp_newComment',
63 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
64 'wp.getMediaItem' => 'this:wp_getMediaItem',
65 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
66 'wp.getPostFormats' => 'this:wp_getPostFormats',
69 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
70 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
71 'blogger.getPost' => 'this:blogger_getPost',
72 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
73 'blogger.getTemplate' => 'this:blogger_getTemplate',
74 'blogger.setTemplate' => 'this:blogger_setTemplate',
75 'blogger.newPost' => 'this:blogger_newPost',
76 'blogger.editPost' => 'this:blogger_editPost',
77 'blogger.deletePost' => 'this:blogger_deletePost',
79 // MetaWeblog API (with MT extensions to structs)
80 'metaWeblog.newPost' => 'this:mw_newPost',
81 'metaWeblog.editPost' => 'this:mw_editPost',
82 'metaWeblog.getPost' => 'this:mw_getPost',
83 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
84 'metaWeblog.getCategories' => 'this:mw_getCategories',
85 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
87 // MetaWeblog API aliases for Blogger API
88 // see http://www.xmlrpc.com/stories/storyReader$2460
89 'metaWeblog.deletePost' => 'this:blogger_deletePost',
90 'metaWeblog.getTemplate' => 'this:blogger_getTemplate',
91 'metaWeblog.setTemplate' => 'this:blogger_setTemplate',
92 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
95 'mt.getCategoryList' => 'this:mt_getCategoryList',
96 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
97 'mt.getPostCategories' => 'this:mt_getPostCategories',
98 'mt.setPostCategories' => 'this:mt_setPostCategories',
99 'mt.supportedMethods' => 'this:mt_supportedMethods',
100 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
101 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
102 'mt.publishPost' => 'this:mt_publishPost',
105 'pingback.ping' => 'this:pingback_ping',
106 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
108 'demo.sayHello' => 'this:sayHello',
109 'demo.addTwoNumbers' => 'this:addTwoNumbers'
112 $this->initialise_blog_option_info( );
113 $this->methods = apply_filters('xmlrpc_methods', $this->methods);
116 function serve_request() {
117 $this->IXR_Server($this->methods);
121 * Test XMLRPC API by saying, "Hello!" to client.
125 * @param array $args Method Parameters.
128 function sayHello($args) {
133 * Test XMLRPC API by adding two numbers for client.
137 * @param array $args Method Parameters.
140 function addTwoNumbers($args) {
143 return $number1 + $number2;
147 * Check user's credentials.
151 * @param string $user_login User's username.
152 * @param string $user_pass User's password.
153 * @return bool Whether authentication passed.
154 * @deprecated use wp_xmlrpc_server::login
155 * @see wp_xmlrpc_server::login
157 function login_pass_ok($user_login, $user_pass) {
158 if ( !get_option( 'enable_xmlrpc' ) ) {
159 $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') ) );
163 if (!user_pass_ok($user_login, $user_pass)) {
164 $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
175 * @param string $username User's username.
176 * @param string $password User's password.
177 * @return mixed WP_User object if authentication passed, false otherwise
179 function login($username, $password) {
180 if ( !get_option( 'enable_xmlrpc' ) ) {
181 $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') ) );
185 $user = wp_authenticate($username, $password);
187 if (is_wp_error($user)) {
188 $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
192 wp_set_current_user( $user->ID );
197 * Sanitize string or array of strings for database.
201 * @param string|array $array Sanitize single string or array of strings.
202 * @return string|array Type matches $array and sanitized for the database.
204 function escape(&$array) {
207 if (!is_array($array)) {
208 return($wpdb->escape($array));
210 foreach ( (array) $array as $k => $v ) {
211 if ( is_array($v) ) {
212 $this->escape($array[$k]);
213 } else if ( is_object($v) ) {
216 $array[$k] = $wpdb->escape($v);
223 * Retrieve custom fields for post.
227 * @param int $post_id Post ID.
228 * @return array Custom fields, if exist.
230 function get_custom_fields($post_id) {
231 $post_id = (int) $post_id;
233 $custom_fields = array();
235 foreach ( (array) has_meta($post_id) as $meta ) {
236 // Don't expose protected fields.
237 if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
240 $custom_fields[] = array(
241 "id" => $meta['meta_id'],
242 "key" => $meta['meta_key'],
243 "value" => $meta['meta_value']
247 return $custom_fields;
251 * Set custom fields for post.
255 * @param int $post_id Post ID.
256 * @param array $fields Custom fields.
258 function set_custom_fields($post_id, $fields) {
259 $post_id = (int) $post_id;
261 foreach ( (array) $fields as $meta ) {
262 if ( isset($meta['id']) ) {
263 $meta['id'] = (int) $meta['id'];
264 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
265 $meta['value'] = stripslashes_deep( $meta['value'] );
266 if ( isset($meta['key']) ) {
267 $meta['key'] = stripslashes( $meta['key'] );
268 if ( $meta['key'] != $pmeta->meta_key )
270 if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
271 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
272 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
273 delete_metadata_by_mid( 'post', $meta['id'] );
275 } elseif ( current_user_can( 'add_post_meta', $post_id, stripslashes( $meta['key'] ) ) ) {
276 add_post_meta( $post_id, $meta['key'], $meta['value'] );
282 * Set up blog options property.
284 * Passes property through 'xmlrpc_blog_options' filter.
288 function initialise_blog_option_info( ) {
291 $this->blog_options = array(
293 'software_name' => array(
294 'desc' => __( 'Software Name' ),
296 'value' => 'WordPress'
298 'software_version' => array(
299 'desc' => __( 'Software Version' ),
301 'value' => $wp_version
304 'desc' => __( 'Site URL' ),
306 'option' => 'siteurl'
310 'time_zone' => array(
311 'desc' => __( 'Time Zone' ),
313 'option' => 'gmt_offset'
315 'blog_title' => array(
316 'desc' => __( 'Site Title' ),
318 'option' => 'blogname'
320 'blog_tagline' => array(
321 'desc' => __( 'Site Tagline' ),
323 'option' => 'blogdescription'
325 'date_format' => array(
326 'desc' => __( 'Date Format' ),
328 'option' => 'date_format'
330 'time_format' => array(
331 'desc' => __( 'Time Format' ),
333 'option' => 'time_format'
335 'users_can_register' => array(
336 'desc' => __( 'Allow new users to sign up' ),
338 'option' => 'users_can_register'
340 'thumbnail_size_w' => array(
341 'desc' => __( 'Thumbnail Width' ),
343 'option' => 'thumbnail_size_w'
345 'thumbnail_size_h' => array(
346 'desc' => __( 'Thumbnail Height' ),
348 'option' => 'thumbnail_size_h'
350 'thumbnail_crop' => array(
351 'desc' => __( 'Crop thumbnail to exact dimensions' ),
353 'option' => 'thumbnail_crop'
355 'medium_size_w' => array(
356 'desc' => __( 'Medium size image width' ),
358 'option' => 'medium_size_w'
360 'medium_size_h' => array(
361 'desc' => __( 'Medium size image height' ),
363 'option' => 'medium_size_h'
365 'large_size_w' => array(
366 'desc' => __( 'Large size image width' ),
368 'option' => 'large_size_w'
370 'large_size_h' => array(
371 'desc' => __( 'Large size image height' ),
373 'option' => 'large_size_h'
377 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
381 * Retrieve the blogs of the user.
385 * @param array $args Method parameters. Contains:
388 * @return array. Contains:
393 * - 'xmlrpc' - url of xmlrpc endpoint
395 function wp_getUsersBlogs( $args ) {
396 global $current_site;
397 // If this isn't on WPMU then just use blogger_getUsersBlogs
398 if ( !is_multisite() ) {
399 array_unshift( $args, 1 );
400 return $this->blogger_getUsersBlogs( $args );
403 $this->escape( $args );
405 $username = $args[0];
406 $password = $args[1];
408 if ( !$user = $this->login($username, $password) )
412 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
414 $blogs = (array) get_blogs_of_user( $user->ID );
417 foreach ( $blogs as $blog ) {
418 // Don't include blogs that aren't hosted at this site
419 if ( $blog->site_id != $current_site->id )
422 $blog_id = $blog->userblog_id;
423 switch_to_blog($blog_id);
424 $is_admin = current_user_can('manage_options');
427 'isAdmin' => $is_admin,
428 'url' => get_option( 'home' ) . '/',
429 'blogid' => (string) $blog_id,
430 'blogName' => get_option( 'blogname' ),
431 'xmlrpc' => site_url( 'xmlrpc.php' )
434 restore_current_blog( );
445 * @param array $args Method parameters. Contains:
452 function wp_getPage($args) {
453 $this->escape($args);
455 $blog_id = (int) $args[0];
456 $page_id = (int) $args[1];
457 $username = $args[2];
458 $password = $args[3];
460 if ( !$user = $this->login($username, $password) ) {
464 if ( !current_user_can( 'edit_page', $page_id ) )
465 return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
467 do_action('xmlrpc_call', 'wp.getPage');
470 $page = get_page($page_id);
472 // If we found the page then format the data.
473 if ( $page->ID && ($page->post_type == 'page') ) {
474 // Get all of the page content and link.
475 $full_page = get_extended($page->post_content);
476 $link = post_permalink($page->ID);
478 // Get info the page parent if there is one.
480 if ( !empty($page->post_parent) ) {
481 $parent = get_page($page->post_parent);
482 $parent_title = $parent->post_title;
485 // Determine comment and ping settings.
486 $allow_comments = comments_open($page->ID) ? 1 : 0;
487 $allow_pings = pings_open($page->ID) ? 1 : 0;
490 $page_date = mysql2date('Ymd\TH:i:s', $page->post_date, false);
491 $page_date_gmt = mysql2date('Ymd\TH:i:s', $page->post_date_gmt, false);
493 // For drafts use the GMT version of the date
494 if ( $page->post_status == 'draft' )
495 $page_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page->post_date ), 'Ymd\TH:i:s' );
497 // Pull the categories info together.
498 $categories = array();
499 foreach ( wp_get_post_categories($page->ID) as $cat_id ) {
500 $categories[] = get_cat_name($cat_id);
503 // Get the author info.
504 $author = get_userdata($page->post_author);
506 $page_template = get_post_meta( $page->ID, '_wp_page_template', true );
507 if ( empty( $page_template ) )
508 $page_template = 'default';
510 $page_struct = array(
511 'dateCreated' => new IXR_Date($page_date),
512 'userid' => $page->post_author,
513 'page_id' => $page->ID,
514 'page_status' => $page->post_status,
515 'description' => $full_page['main'],
516 'title' => $page->post_title,
518 'permaLink' => $link,
519 'categories' => $categories,
520 'excerpt' => $page->post_excerpt,
521 'text_more' => $full_page['extended'],
522 'mt_allow_comments' => $allow_comments,
523 'mt_allow_pings' => $allow_pings,
524 'wp_slug' => $page->post_name,
525 'wp_password' => $page->post_password,
526 'wp_author' => $author->display_name,
527 'wp_page_parent_id' => $page->post_parent,
528 'wp_page_parent_title' => $parent_title,
529 'wp_page_order' => $page->menu_order,
530 'wp_author_id' => (string) $author->ID,
531 'wp_author_display_name' => $author->display_name,
532 'date_created_gmt' => new IXR_Date($page_date_gmt),
533 'custom_fields' => $this->get_custom_fields($page_id),
534 'wp_page_template' => $page_template
537 return($page_struct);
539 // If the page doesn't exist indicate that.
541 return(new IXR_Error(404, __('Sorry, no such page.')));
550 * @param array $args Method parameters. Contains:
557 function wp_getPages($args) {
558 $this->escape($args);
560 $blog_id = (int) $args[0];
561 $username = $args[1];
562 $password = $args[2];
563 $num_pages = isset($args[3]) ? (int) $args[3] : 10;
565 if ( !$user = $this->login($username, $password) )
568 if ( !current_user_can( 'edit_pages' ) )
569 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
571 do_action('xmlrpc_call', 'wp.getPages');
573 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
574 $num_pages = count($pages);
576 // If we have pages, put together their info.
577 if ( $num_pages >= 1 ) {
578 $pages_struct = array();
580 for ( $i = 0; $i < $num_pages; $i++ ) {
581 $page = wp_xmlrpc_server::wp_getPage(array(
582 $blog_id, $pages[$i]->ID, $username, $password
584 $pages_struct[] = $page;
587 return($pages_struct);
589 // If no pages were found return an error.
600 * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
603 function wp_newPage($args) {
604 // Items not escaped here will be escaped in newPost.
605 $username = $this->escape($args[1]);
606 $password = $this->escape($args[2]);
610 if ( !$user = $this->login($username, $password) )
613 do_action('xmlrpc_call', 'wp.newPage');
615 // Make sure the user is allowed to add new pages.
616 if ( !current_user_can('publish_pages') )
617 return(new IXR_Error(401, __('Sorry, you cannot add new pages.')));
619 // Mark this as content for a page.
620 $args[3]["post_type"] = 'page';
622 // Let mw_newPost do all of the heavy lifting.
623 return($this->mw_newPost($args));
631 * @param array $args Method parameters.
632 * @return bool True, if success.
634 function wp_deletePage($args) {
635 $this->escape($args);
637 $blog_id = (int) $args[0];
638 $username = $args[1];
639 $password = $args[2];
640 $page_id = (int) $args[3];
642 if ( !$user = $this->login($username, $password) )
645 do_action('xmlrpc_call', 'wp.deletePage');
647 // Get the current page based on the page_id and
648 // make sure it is a page and not a post.
649 $actual_page = wp_get_single_post($page_id, ARRAY_A);
650 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
651 return(new IXR_Error(404, __('Sorry, no such page.')));
653 // Make sure the user can delete pages.
654 if ( !current_user_can('delete_page', $page_id) )
655 return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
657 // Attempt to delete the page.
658 $result = wp_delete_post($page_id);
660 return(new IXR_Error(500, __('Failed to delete the page.')));
670 * @param array $args Method parameters.
673 function wp_editPage($args) {
674 // Items not escaped here will be escaped in editPost.
675 $blog_id = (int) $args[0];
676 $page_id = (int) $this->escape($args[1]);
677 $username = $this->escape($args[2]);
678 $password = $this->escape($args[3]);
682 if ( !$user = $this->login($username, $password) )
685 do_action('xmlrpc_call', 'wp.editPage');
687 // Get the page data and make sure it is a page.
688 $actual_page = wp_get_single_post($page_id, ARRAY_A);
689 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
690 return(new IXR_Error(404, __('Sorry, no such page.')));
692 // Make sure the user is allowed to edit pages.
693 if ( !current_user_can('edit_page', $page_id) )
694 return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
696 // Mark this as content for a page.
697 $content['post_type'] = 'page';
699 // Arrange args in the way mw_editPost understands.
708 // Let mw_editPost do all of the heavy lifting.
709 return($this->mw_editPost($args));
713 * Retrieve page list.
717 * @param array $args Method parameters.
720 function wp_getPageList($args) {
723 $this->escape($args);
725 $blog_id = (int) $args[0];
726 $username = $args[1];
727 $password = $args[2];
729 if ( !$user = $this->login($username, $password) )
732 if ( !current_user_can( 'edit_pages' ) )
733 return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
735 do_action('xmlrpc_call', 'wp.getPageList');
737 // Get list of pages ids and titles
738 $page_list = $wpdb->get_results("
740 post_title page_title,
741 post_parent page_parent_id,
746 WHERE post_type = 'page'
750 // The date needs to be formatted properly.
751 $num_pages = count($page_list);
752 for ( $i = 0; $i < $num_pages; $i++ ) {
753 $post_date = mysql2date('Ymd\TH:i:s', $page_list[$i]->post_date, false);
754 $post_date_gmt = mysql2date('Ymd\TH:i:s', $page_list[$i]->post_date_gmt, false);
756 $page_list[$i]->dateCreated = new IXR_Date($post_date);
757 $page_list[$i]->date_created_gmt = new IXR_Date($post_date_gmt);
759 // For drafts use the GMT version of the date
760 if ( $page_list[$i]->post_status == 'draft' ) {
761 $page_list[$i]->date_created_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page_list[$i]->post_date ), 'Ymd\TH:i:s' );
762 $page_list[$i]->date_created_gmt = new IXR_Date( $page_list[$i]->date_created_gmt );
765 unset($page_list[$i]->post_date_gmt);
766 unset($page_list[$i]->post_date);
767 unset($page_list[$i]->post_status);
774 * Retrieve authors list.
778 * @param array $args Method parameters.
781 function wp_getAuthors($args) {
783 $this->escape($args);
785 $blog_id = (int) $args[0];
786 $username = $args[1];
787 $password = $args[2];
789 if ( !$user = $this->login($username, $password) )
792 if ( !current_user_can('edit_posts') )
793 return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
795 do_action('xmlrpc_call', 'wp.getAuthors');
798 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
800 'user_id' => $user->ID,
801 'user_login' => $user->user_login,
802 'display_name' => $user->display_name
810 * Get list of all tags
814 * @param array $args Method parameters.
817 function wp_getTags( $args ) {
818 $this->escape( $args );
820 $blog_id = (int) $args[0];
821 $username = $args[1];
822 $password = $args[2];
824 if ( !$user = $this->login($username, $password) )
827 if ( !current_user_can( 'edit_posts' ) )
828 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
830 do_action( 'xmlrpc_call', 'wp.getKeywords' );
834 if ( $all_tags = get_tags() ) {
835 foreach( (array) $all_tags as $tag ) {
836 $struct['tag_id'] = $tag->term_id;
837 $struct['name'] = $tag->name;
838 $struct['count'] = $tag->count;
839 $struct['slug'] = $tag->slug;
840 $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
841 $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
851 * Create new category.
855 * @param array $args Method parameters.
856 * @return int Category ID.
858 function wp_newCategory($args) {
859 $this->escape($args);
861 $blog_id = (int) $args[0];
862 $username = $args[1];
863 $password = $args[2];
864 $category = $args[3];
866 if ( !$user = $this->login($username, $password) )
869 do_action('xmlrpc_call', 'wp.newCategory');
871 // Make sure the user is allowed to add a category.
872 if ( !current_user_can('manage_categories') )
873 return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
875 // If no slug was provided make it empty so that
876 // WordPress will generate one.
877 if ( empty($category['slug']) )
878 $category['slug'] = '';
880 // If no parent_id was provided make it empty
881 // so that it will be a top level page (no parent).
882 if ( !isset($category['parent_id']) )
883 $category['parent_id'] = '';
885 // If no description was provided make it empty.
886 if ( empty($category["description"]) )
887 $category["description"] = "";
889 $new_category = array(
890 'cat_name' => $category['name'],
891 'category_nicename' => $category['slug'],
892 'category_parent' => $category['parent_id'],
893 'category_description' => $category['description']
896 $cat_id = wp_insert_category($new_category, true);
897 if ( is_wp_error( $cat_id ) ) {
898 if ( 'term_exists' == $cat_id->get_error_code() )
899 return (int) $cat_id->get_error_data();
901 return(new IXR_Error(500, __('Sorry, the new category failed.')));
902 } elseif ( ! $cat_id ) {
903 return(new IXR_Error(500, __('Sorry, the new category failed.')));
914 * @param array $args Method parameters.
915 * @return mixed See {@link wp_delete_term()} for return info.
917 function wp_deleteCategory($args) {
918 $this->escape($args);
920 $blog_id = (int) $args[0];
921 $username = $args[1];
922 $password = $args[2];
923 $category_id = (int) $args[3];
925 if ( !$user = $this->login($username, $password) )
928 do_action('xmlrpc_call', 'wp.deleteCategory');
930 if ( !current_user_can('manage_categories') )
931 return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
933 return wp_delete_term( $category_id, 'category' );
937 * Retrieve category list.
941 * @param array $args Method parameters.
944 function wp_suggestCategories($args) {
945 $this->escape($args);
947 $blog_id = (int) $args[0];
948 $username = $args[1];
949 $password = $args[2];
950 $category = $args[3];
951 $max_results = (int) $args[4];
953 if ( !$user = $this->login($username, $password) )
956 if ( !current_user_can( 'edit_posts' ) )
957 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
959 do_action('xmlrpc_call', 'wp.suggestCategories');
961 $category_suggestions = array();
962 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
963 foreach ( (array) get_categories($args) as $cat ) {
964 $category_suggestions[] = array(
965 'category_id' => $cat->term_id,
966 'category_name' => $cat->name
970 return($category_suggestions);
978 * @param array $args Method parameters.
981 function wp_getComment($args) {
982 $this->escape($args);
984 $blog_id = (int) $args[0];
985 $username = $args[1];
986 $password = $args[2];
987 $comment_id = (int) $args[3];
989 if ( !$user = $this->login($username, $password) )
992 if ( !current_user_can( 'moderate_comments' ) )
993 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
995 do_action('xmlrpc_call', 'wp.getComment');
997 if ( ! $comment = get_comment($comment_id) )
998 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
1000 // Format page date.
1001 $comment_date = mysql2date('Ymd\TH:i:s', $comment->comment_date, false);
1002 $comment_date_gmt = mysql2date('Ymd\TH:i:s', $comment->comment_date_gmt, false);
1004 if ( '0' == $comment->comment_approved )
1005 $comment_status = 'hold';
1006 else if ( 'spam' == $comment->comment_approved )
1007 $comment_status = 'spam';
1008 else if ( '1' == $comment->comment_approved )
1009 $comment_status = 'approve';
1011 $comment_status = $comment->comment_approved;
1013 $link = get_comment_link($comment);
1015 $comment_struct = array(
1016 'date_created_gmt' => new IXR_Date($comment_date_gmt),
1017 'user_id' => $comment->user_id,
1018 'comment_id' => $comment->comment_ID,
1019 'parent' => $comment->comment_parent,
1020 'status' => $comment_status,
1021 'content' => $comment->comment_content,
1023 'post_id' => $comment->comment_post_ID,
1024 'post_title' => get_the_title($comment->comment_post_ID),
1025 'author' => $comment->comment_author,
1026 'author_url' => $comment->comment_author_url,
1027 'author_email' => $comment->comment_author_email,
1028 'author_ip' => $comment->comment_author_IP,
1029 'type' => $comment->comment_type,
1032 return $comment_struct;
1036 * Retrieve comments.
1038 * Besides the common blog_id, username, and password arguments, it takes a filter
1039 * array as last argument.
1041 * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
1043 * The defaults are as follows:
1044 * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
1045 * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
1046 * - 'number' - Default is 10. Total number of media items to retrieve.
1047 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
1051 * @param array $args Method parameters.
1052 * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
1054 function wp_getComments($args) {
1056 $this->escape($args);
1058 $blog_id = (int) $args[0];
1059 $username = $args[1];
1060 $password = $args[2];
1063 if ( !$user = $this->login($username, $password) )
1064 return $this->error;
1066 if ( !current_user_can( 'moderate_comments' ) )
1067 return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
1069 do_action('xmlrpc_call', 'wp.getComments');
1071 if ( isset($struct['status']) )
1072 $status = $struct['status'];
1077 if ( isset($struct['post_id']) )
1078 $post_id = absint($struct['post_id']);
1081 if ( isset($struct['offset']) )
1082 $offset = absint($struct['offset']);
1085 if ( isset($struct['number']) )
1086 $number = absint($struct['number']);
1088 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
1089 $num_comments = count($comments);
1091 if ( ! $num_comments )
1094 $comments_struct = array();
1096 // FIXME: we already have the comments, why query them again?
1097 for ( $i = 0; $i < $num_comments; $i++ ) {
1098 $comment = wp_xmlrpc_server::wp_getComment(array(
1099 $raw_args[0], $raw_args[1], $raw_args[2], $comments[$i]->comment_ID,
1101 $comments_struct[] = $comment;
1104 return $comments_struct;
1110 * By default, the comment will be moved to the trash instead of deleted.
1111 * See {@link wp_delete_comment()} for more information on
1116 * @param array $args Method parameters. Contains:
1121 * @return mixed {@link wp_delete_comment()}
1123 function wp_deleteComment($args) {
1124 $this->escape($args);
1126 $blog_id = (int) $args[0];
1127 $username = $args[1];
1128 $password = $args[2];
1129 $comment_ID = (int) $args[3];
1131 if ( !$user = $this->login($username, $password) )
1132 return $this->error;
1134 if ( !current_user_can( 'moderate_comments' ) )
1135 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
1137 if ( ! get_comment($comment_ID) )
1138 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
1140 if ( !current_user_can( 'edit_comment', $comment_ID ) )
1141 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
1143 do_action('xmlrpc_call', 'wp.deleteComment');
1145 return wp_delete_comment($comment_ID);
1151 * Besides the common blog_id, username, and password arguments, it takes a
1152 * comment_id integer and a content_struct array as last argument.
1154 * The allowed keys in the content_struct array are:
1159 * - 'date_created_gmt'
1160 * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
1164 * @param array $args. Contains:
1170 * @return bool True, on success.
1172 function wp_editComment($args) {
1173 $this->escape($args);
1175 $blog_id = (int) $args[0];
1176 $username = $args[1];
1177 $password = $args[2];
1178 $comment_ID = (int) $args[3];
1179 $content_struct = $args[4];
1181 if ( !$user = $this->login($username, $password) )
1182 return $this->error;
1184 if ( !current_user_can( 'moderate_comments' ) )
1185 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
1187 if ( ! get_comment($comment_ID) )
1188 return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
1190 if ( !current_user_can( 'edit_comment', $comment_ID ) )
1191 return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
1193 do_action('xmlrpc_call', 'wp.editComment');
1195 if ( isset($content_struct['status']) ) {
1196 $statuses = get_comment_statuses();
1197 $statuses = array_keys($statuses);
1199 if ( ! in_array($content_struct['status'], $statuses) )
1200 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
1201 $comment_approved = $content_struct['status'];
1204 // Do some timestamp voodoo
1205 if ( !empty( $content_struct['date_created_gmt'] ) ) {
1206 $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1207 $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
1208 $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
1211 if ( isset($content_struct['content']) )
1212 $comment_content = $content_struct['content'];
1214 if ( isset($content_struct['author']) )
1215 $comment_author = $content_struct['author'];
1217 if ( isset($content_struct['author_url']) )
1218 $comment_author_url = $content_struct['author_url'];
1220 if ( isset($content_struct['author_email']) )
1221 $comment_author_email = $content_struct['author_email'];
1223 // We've got all the data -- post it:
1224 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
1226 $result = wp_update_comment($comment);
1227 if ( is_wp_error( $result ) )
1228 return new IXR_Error(500, $result->get_error_message());
1231 return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
1237 * Create new comment.
1241 * @param array $args Method parameters.
1242 * @return mixed {@link wp_new_comment()}
1244 function wp_newComment($args) {
1247 $this->escape($args);
1249 $blog_id = (int) $args[0];
1250 $username = $args[1];
1251 $password = $args[2];
1253 $content_struct = $args[4];
1255 $allow_anon = apply_filters('xmlrpc_allow_anonymous_comments', false);
1257 $user = $this->login($username, $password);
1261 if ( $allow_anon && get_option('comment_registration') )
1262 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
1263 else if ( !$allow_anon )
1264 return $this->error;
1269 if ( is_numeric($post) )
1270 $post_id = absint($post);
1272 $post_id = url_to_postid($post);
1275 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1277 if ( ! get_post($post_id) )
1278 return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1280 $comment['comment_post_ID'] = $post_id;
1283 $comment['comment_author'] = $wpdb->escape( $user->display_name );
1284 $comment['comment_author_email'] = $wpdb->escape( $user->user_email );
1285 $comment['comment_author_url'] = $wpdb->escape( $user->user_url );
1286 $comment['user_ID'] = $user->ID;
1288 $comment['comment_author'] = '';
1289 if ( isset($content_struct['author']) )
1290 $comment['comment_author'] = $content_struct['author'];
1292 $comment['comment_author_email'] = '';
1293 if ( isset($content_struct['author_email']) )
1294 $comment['comment_author_email'] = $content_struct['author_email'];
1296 $comment['comment_author_url'] = '';
1297 if ( isset($content_struct['author_url']) )
1298 $comment['comment_author_url'] = $content_struct['author_url'];
1300 $comment['user_ID'] = 0;
1302 if ( get_option('require_name_email') ) {
1303 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
1304 return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
1305 elseif ( !is_email($comment['comment_author_email']) )
1306 return new IXR_Error( 403, __( 'A valid email address is required' ) );
1310 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
1312 $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
1314 do_action('xmlrpc_call', 'wp.newComment');
1316 return wp_new_comment($comment);
1320 * Retrieve all of the comment status.
1324 * @param array $args Method parameters.
1327 function wp_getCommentStatusList($args) {
1328 $this->escape( $args );
1330 $blog_id = (int) $args[0];
1331 $username = $args[1];
1332 $password = $args[2];
1334 if ( !$user = $this->login($username, $password) )
1335 return $this->error;
1337 if ( !current_user_can( 'moderate_comments' ) )
1338 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
1340 do_action('xmlrpc_call', 'wp.getCommentStatusList');
1342 return get_comment_statuses( );
1346 * Retrieve comment count.
1350 * @param array $args Method parameters.
1353 function wp_getCommentCount( $args ) {
1354 $this->escape($args);
1356 $blog_id = (int) $args[0];
1357 $username = $args[1];
1358 $password = $args[2];
1359 $post_id = (int) $args[3];
1361 if ( !$user = $this->login($username, $password) )
1362 return $this->error;
1364 if ( !current_user_can( 'edit_posts' ) )
1365 return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
1367 do_action('xmlrpc_call', 'wp.getCommentCount');
1369 $count = wp_count_comments( $post_id );
1371 'approved' => $count->approved,
1372 'awaiting_moderation' => $count->moderated,
1373 'spam' => $count->spam,
1374 'total_comments' => $count->total_comments
1379 * Retrieve post statuses.
1383 * @param array $args Method parameters.
1386 function wp_getPostStatusList( $args ) {
1387 $this->escape( $args );
1389 $blog_id = (int) $args[0];
1390 $username = $args[1];
1391 $password = $args[2];
1393 if ( !$user = $this->login($username, $password) )
1394 return $this->error;
1396 if ( !current_user_can( 'edit_posts' ) )
1397 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
1399 do_action('xmlrpc_call', 'wp.getPostStatusList');
1401 return get_post_statuses( );
1405 * Retrieve page statuses.
1409 * @param array $args Method parameters.
1412 function wp_getPageStatusList( $args ) {
1413 $this->escape( $args );
1415 $blog_id = (int) $args[0];
1416 $username = $args[1];
1417 $password = $args[2];
1419 if ( !$user = $this->login($username, $password) )
1420 return $this->error;
1422 if ( !current_user_can( 'edit_pages' ) )
1423 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
1425 do_action('xmlrpc_call', 'wp.getPageStatusList');
1427 return get_page_statuses( );
1431 * Retrieve page templates.
1435 * @param array $args Method parameters.
1438 function wp_getPageTemplates( $args ) {
1439 $this->escape( $args );
1441 $blog_id = (int) $args[0];
1442 $username = $args[1];
1443 $password = $args[2];
1445 if ( !$user = $this->login($username, $password) )
1446 return $this->error;
1448 if ( !current_user_can( 'edit_pages' ) )
1449 return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
1451 $templates = get_page_templates( );
1452 $templates['Default'] = 'default';
1458 * Retrieve blog options.
1462 * @param array $args Method parameters.
1465 function wp_getOptions( $args ) {
1466 $this->escape( $args );
1468 $blog_id = (int) $args[0];
1469 $username = $args[1];
1470 $password = $args[2];
1471 $options = isset( $args[3] ) ? (array) $args[3] : array();
1473 if ( !$user = $this->login($username, $password) )
1474 return $this->error;
1476 // If no specific options where asked for, return all of them
1477 if ( count( $options ) == 0 )
1478 $options = array_keys($this->blog_options);
1480 return $this->_getOptions($options);
1484 * Retrieve blog options value from list.
1488 * @param array $options Options to retrieve.
1491 function _getOptions($options) {
1493 foreach ( $options as $option ) {
1494 if ( array_key_exists( $option, $this->blog_options ) ) {
1495 $data[$option] = $this->blog_options[$option];
1496 //Is the value static or dynamic?
1497 if ( isset( $data[$option]['option'] ) ) {
1498 $data[$option]['value'] = get_option( $data[$option]['option'] );
1499 unset($data[$option]['option']);
1508 * Update blog options.
1512 * @param array $args Method parameters.
1515 function wp_setOptions( $args ) {
1516 $this->escape( $args );
1518 $blog_id = (int) $args[0];
1519 $username = $args[1];
1520 $password = $args[2];
1521 $options = (array) $args[3];
1523 if ( !$user = $this->login($username, $password) )
1524 return $this->error;
1526 if ( !current_user_can( 'manage_options' ) )
1527 return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
1529 foreach ( $options as $o_name => $o_value ) {
1530 $option_names[] = $o_name;
1531 if ( !array_key_exists( $o_name, $this->blog_options ) )
1534 if ( $this->blog_options[$o_name]['readonly'] == true )
1537 update_option( $this->blog_options[$o_name]['option'], $o_value );
1540 //Now return the updated values
1541 return $this->_getOptions($option_names);
1545 * Retrieve a media item by ID
1549 * @param array $args Method parameters. Contains:
1554 * @return array. Associative array containing:
1555 * - 'date_created_gmt'
1564 function wp_getMediaItem($args) {
1565 $this->escape($args);
1567 $blog_id = (int) $args[0];
1568 $username = $args[1];
1569 $password = $args[2];
1570 $attachment_id = (int) $args[3];
1572 if ( !$user = $this->login($username, $password) )
1573 return $this->error;
1575 if ( !current_user_can( 'upload_files' ) )
1576 return new IXR_Error( 403, __( 'You are not allowed to upload files to this site.' ) );
1578 do_action('xmlrpc_call', 'wp.getMediaItem');
1580 if ( ! $attachment = get_post($attachment_id) )
1581 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1583 // Format page date.
1584 $attachment_date = mysql2date('Ymd\TH:i:s', $attachment->post_date, false);
1585 $attachment_date_gmt = mysql2date('Ymd\TH:i:s', $attachment->post_date_gmt, false);
1587 $link = wp_get_attachment_url($attachment->ID);
1588 $thumbnail_link = wp_get_attachment_thumb_url($attachment->ID);
1590 $attachment_struct = array(
1591 'date_created_gmt' => new IXR_Date($attachment_date_gmt),
1592 'parent' => $attachment->post_parent,
1594 'thumbnail' => $thumbnail_link,
1595 'title' => $attachment->post_title,
1596 'caption' => $attachment->post_excerpt,
1597 'description' => $attachment->post_content,
1598 'metadata' => wp_get_attachment_metadata($attachment->ID),
1601 return $attachment_struct;
1605 * Retrieves a collection of media library items (or attachments)
1607 * Besides the common blog_id, username, and password arguments, it takes a filter
1608 * array as last argument.
1610 * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
1612 * The defaults are as follows:
1613 * - 'number' - Default is 5. Total number of media items to retrieve.
1614 * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
1615 * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
1616 * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
1620 * @param array $args Method parameters. Contains:
1625 * @return array. Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
1627 function wp_getMediaLibrary($args) {
1629 $this->escape($args);
1631 $blog_id = (int) $args[0];
1632 $username = $args[1];
1633 $password = $args[2];
1634 $struct = isset( $args[3] ) ? $args[3] : array() ;
1636 if ( !$user = $this->login($username, $password) )
1637 return $this->error;
1639 if ( !current_user_can( 'upload_files' ) )
1640 return new IXR_Error( 401, __( 'Sorry, you cannot upload files.' ) );
1642 do_action('xmlrpc_call', 'wp.getMediaLibrary');
1644 $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
1645 $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
1646 $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
1647 $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
1649 $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
1650 $num_attachments = count($attachments);
1652 if ( ! $num_attachments )
1655 $attachments_struct = array();
1657 foreach ($attachments as $attachment )
1658 $attachments_struct[] = $this->wp_getMediaItem( array( $raw_args[0], $raw_args[1], $raw_args[2], $attachment->ID ) );
1660 return $attachments_struct;
1664 * Retrieves a list of post formats used by the site
1668 * @param array $args Method parameters. Contains:
1674 function wp_getPostFormats( $args ) {
1675 $this->escape( $args );
1677 $blog_id = (int) $args[0];
1678 $username = $args[1];
1679 $password = $args[2];
1681 if ( !$user = $this->login( $username, $password ) )
1682 return $this->error;
1684 do_action( 'xmlrpc_call', 'wp.getPostFormats' );
1686 $formats = get_post_format_strings();
1688 # find out if they want a list of currently supports formats
1689 if ( isset( $args[3] ) && is_array( $args[3] ) ) {
1690 if ( $args[3]['show-supported'] ) {
1691 if ( current_theme_supports( 'post-formats' ) ) {
1692 $supported = get_theme_support( 'post-formats' );
1694 $data['all'] = $formats;
1695 $data['supported'] = $supported[0];
1705 /* Blogger API functions.
1706 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
1710 * Retrieve blogs that user owns.
1712 * Will make more sense once we support multiple blogs.
1716 * @param array $args Method parameters.
1719 function blogger_getUsersBlogs($args) {
1720 if ( is_multisite() )
1721 return $this->_multisite_getUsersBlogs($args);
1723 $this->escape($args);
1725 $username = $args[1];
1726 $password = $args[2];
1728 if ( !$user = $this->login($username, $password) )
1729 return $this->error;
1731 do_action('xmlrpc_call', 'blogger.getUsersBlogs');
1733 $is_admin = current_user_can('manage_options');
1736 'isAdmin' => $is_admin,
1737 'url' => get_option('home') . '/',
1739 'blogName' => get_option('blogname'),
1740 'xmlrpc' => site_url( 'xmlrpc.php' )
1743 return array($struct);
1747 * Private function for retrieving a users blogs for multisite setups
1751 function _multisite_getUsersBlogs($args) {
1752 global $current_blog;
1753 $domain = $current_blog->domain;
1754 $path = $current_blog->path . 'xmlrpc.php';
1755 $protocol = is_ssl() ? 'https' : 'http';
1757 $rpc = new IXR_Client("$protocol://{$domain}{$path}");
1758 $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
1759 $blogs = $rpc->getResponse();
1761 if ( isset($blogs['faultCode']) )
1762 return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
1764 if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
1767 foreach ( (array) $blogs as $blog ) {
1768 if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
1769 return array($blog);
1776 * Retrieve user's data.
1778 * Gives your client some info about you, so you don't have to.
1782 * @param array $args Method parameters.
1785 function blogger_getUserInfo($args) {
1787 $this->escape($args);
1789 $username = $args[1];
1790 $password = $args[2];
1792 if ( !$user = $this->login($username, $password) )
1793 return $this->error;
1795 if ( !current_user_can( 'edit_posts' ) )
1796 return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
1798 do_action('xmlrpc_call', 'blogger.getUserInfo');
1801 'nickname' => $user->nickname,
1802 'userid' => $user->ID,
1803 'url' => $user->user_url,
1804 'lastname' => $user->last_name,
1805 'firstname' => $user->first_name
1816 * @param array $args Method parameters.
1819 function blogger_getPost($args) {
1821 $this->escape($args);
1823 $post_ID = (int) $args[1];
1824 $username = $args[2];
1825 $password = $args[3];
1827 if ( !$user = $this->login($username, $password) )
1828 return $this->error;
1830 if ( !current_user_can( 'edit_post', $post_ID ) )
1831 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1833 do_action('xmlrpc_call', 'blogger.getPost');
1835 $post_data = wp_get_single_post($post_ID, ARRAY_A);
1837 $categories = implode(',', wp_get_post_categories($post_ID));
1839 $content = '<title>'.stripslashes($post_data['post_title']).'</title>';
1840 $content .= '<category>'.$categories.'</category>';
1841 $content .= stripslashes($post_data['post_content']);
1844 'userid' => $post_data['post_author'],
1845 'dateCreated' => new IXR_Date(mysql2date('Ymd\TH:i:s', $post_data['post_date'], false)),
1846 'content' => $content,
1847 'postid' => (string) $post_data['ID']
1854 * Retrieve list of recent posts.
1858 * @param array $args Method parameters.
1861 function blogger_getRecentPosts($args) {
1863 $this->escape($args);
1865 // $args[0] = appkey - ignored
1866 $blog_ID = (int) $args[1]; /* though we don't use it yet */
1867 $username = $args[2];
1868 $password = $args[3];
1869 if ( isset( $args[4] ) )
1870 $query = array( 'numberposts' => absint( $args[4] ) );
1874 if ( !$user = $this->login($username, $password) )
1875 return $this->error;
1877 do_action('xmlrpc_call', 'blogger.getRecentPosts');
1879 $posts_list = wp_get_recent_posts( $query );
1881 if ( !$posts_list ) {
1882 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
1883 return $this->error;
1886 foreach ($posts_list as $entry) {
1887 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
1890 $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
1891 $categories = implode(',', wp_get_post_categories($entry['ID']));
1893 $content = '<title>'.stripslashes($entry['post_title']).'</title>';
1894 $content .= '<category>'.$categories.'</category>';
1895 $content .= stripslashes($entry['post_content']);
1898 'userid' => $entry['post_author'],
1899 'dateCreated' => new IXR_Date($post_date),
1900 'content' => $content,
1901 'postid' => (string) $entry['ID'],
1906 $recent_posts = array();
1907 for ( $j=0; $j<count($struct); $j++ ) {
1908 array_push($recent_posts, $struct[$j]);
1911 return $recent_posts;
1915 * Retrieve blog_filename content.
1919 * @param array $args Method parameters.
1922 function blogger_getTemplate($args) {
1924 $this->escape($args);
1926 $blog_ID = (int) $args[1];
1927 $username = $args[2];
1928 $password = $args[3];
1929 $template = $args[4]; /* could be 'main' or 'archiveIndex', but we don't use it */
1931 if ( !$user = $this->login($username, $password) )
1932 return $this->error;
1934 do_action('xmlrpc_call', 'blogger.getTemplate');
1936 if ( !current_user_can('edit_themes') )
1937 return new IXR_Error(401, __('Sorry, this user can not edit the template.'));
1939 /* warning: here we make the assumption that the blog's URL is on the same server */
1940 $filename = get_option('home') . '/';
1941 $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
1943 $f = fopen($filename, 'r');
1944 $content = fread($f, filesize($filename));
1947 /* so it is actually editable with a windows/mac client */
1948 // 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);
1954 * Updates the content of blog_filename.
1958 * @param array $args Method parameters.
1959 * @return bool True when done.
1961 function blogger_setTemplate($args) {
1963 $this->escape($args);
1965 $blog_ID = (int) $args[1];
1966 $username = $args[2];
1967 $password = $args[3];
1968 $content = $args[4];
1969 $template = $args[5]; /* could be 'main' or 'archiveIndex', but we don't use it */
1971 if ( !$user = $this->login($username, $password) )
1972 return $this->error;
1974 do_action('xmlrpc_call', 'blogger.setTemplate');
1976 if ( !current_user_can('edit_themes') )
1977 return new IXR_Error(401, __('Sorry, this user cannot edit the template.'));
1979 /* warning: here we make the assumption that the blog's URL is on the same server */
1980 $filename = get_option('home') . '/';
1981 $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
1983 if ($f = fopen($filename, 'w+')) {
1984 fwrite($f, $content);
1987 return new IXR_Error(500, __('Either the file is not writable, or something wrong happened. The file has not been updated.'));
1998 * @param array $args Method parameters.
2001 function blogger_newPost($args) {
2003 $this->escape($args);
2005 $blog_ID = (int) $args[1]; /* though we don't use it yet */
2006 $username = $args[2];
2007 $password = $args[3];
2008 $content = $args[4];
2009 $publish = $args[5];
2011 if ( !$user = $this->login($username, $password) )
2012 return $this->error;
2014 do_action('xmlrpc_call', 'blogger.newPost');
2016 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
2017 if ( !current_user_can($cap) )
2018 return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
2020 $post_status = ($publish) ? 'publish' : 'draft';
2022 $post_author = $user->ID;
2024 $post_title = xmlrpc_getposttitle($content);
2025 $post_category = xmlrpc_getpostcategory($content);
2026 $post_content = xmlrpc_removepostdata($content);
2028 $post_date = current_time('mysql');
2029 $post_date_gmt = current_time('mysql', 1);
2031 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
2033 $post_ID = wp_insert_post($post_data);
2034 if ( is_wp_error( $post_ID ) )
2035 return new IXR_Error(500, $post_ID->get_error_message());
2038 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
2040 $this->attach_uploads( $post_ID, $post_content );
2042 logIO('O', "Posted ! ID: $post_ID");
2052 * @param array $args Method parameters.
2053 * @return bool true when done.
2055 function blogger_editPost($args) {
2057 $this->escape($args);
2059 $post_ID = (int) $args[1];
2060 $username = $args[2];
2061 $password = $args[3];
2062 $content = $args[4];
2063 $publish = $args[5];
2065 if ( !$user = $this->login($username, $password) )
2066 return $this->error;
2068 do_action('xmlrpc_call', 'blogger.editPost');
2070 $actual_post = wp_get_single_post($post_ID,ARRAY_A);
2072 if ( !$actual_post || $actual_post['post_type'] != 'post' )
2073 return new IXR_Error(404, __('Sorry, no such post.'));
2075 $this->escape($actual_post);
2077 if ( !current_user_can('edit_post', $post_ID) )
2078 return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
2080 extract($actual_post, EXTR_SKIP);
2082 if ( ('publish' == $post_status) && !current_user_can('publish_posts') )
2083 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
2085 $post_title = xmlrpc_getposttitle($content);
2086 $post_category = xmlrpc_getpostcategory($content);
2087 $post_content = xmlrpc_removepostdata($content);
2089 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
2091 $result = wp_update_post($postdata);
2094 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
2096 $this->attach_uploads( $ID, $post_content );
2106 * @param array $args Method parameters.
2107 * @return bool True when post is deleted.
2109 function blogger_deletePost($args) {
2110 $this->escape($args);
2112 $post_ID = (int) $args[1];
2113 $username = $args[2];
2114 $password = $args[3];
2115 $publish = $args[4];
2117 if ( !$user = $this->login($username, $password) )
2118 return $this->error;
2120 do_action('xmlrpc_call', 'blogger.deletePost');
2122 $actual_post = wp_get_single_post($post_ID,ARRAY_A);
2124 if ( !$actual_post || $actual_post['post_type'] != 'post' )
2125 return new IXR_Error(404, __('Sorry, no such post.'));
2127 if ( !current_user_can('delete_post', $post_ID) )
2128 return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
2130 $result = wp_delete_post($post_ID);
2133 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
2138 /* MetaWeblog API functions
2139 * specs on wherever Dave Winer wants them to be
2143 * Create a new post.
2145 * The 'content_struct' argument must contain:
2154 * Also, it can optionally contain:
2157 * - wp_page_parent_id
2160 * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
2161 * - mt_allow_comments - can be 'open' or 'closed'
2162 * - mt_allow_pings - can be 'open' or 'closed'
2163 * - date_created_gmt
2168 * @param array $args Method parameters. Contains:
2176 function mw_newPost($args) {
2177 $this->escape($args);
2179 $blog_ID = (int) $args[0]; // we will support this in the near future
2180 $username = $args[1];
2181 $password = $args[2];
2182 $content_struct = $args[3];
2183 $publish = isset( $args[4] ) ? $args[4] : 0;
2185 if ( !$user = $this->login($username, $password) )
2186 return $this->error;
2188 do_action('xmlrpc_call', 'metaWeblog.newPost');
2190 $page_template = '';
2191 if ( !empty( $content_struct['post_type'] ) ) {
2192 if ( $content_struct['post_type'] == 'page' ) {
2194 $cap = 'publish_pages';
2195 elseif ('publish' == $content_struct['page_status'])
2196 $cap = 'publish_pages';
2198 $cap = 'edit_pages';
2199 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
2200 $post_type = 'page';
2201 if ( !empty( $content_struct['wp_page_template'] ) )
2202 $page_template = $content_struct['wp_page_template'];
2203 } elseif ( $content_struct['post_type'] == 'post' ) {
2205 $cap = 'publish_posts';
2206 elseif ('publish' == $content_struct['post_status'])
2207 $cap = 'publish_posts';
2209 $cap = 'edit_posts';
2210 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
2211 $post_type = 'post';
2213 // No other post_type values are allowed here
2214 return new IXR_Error( 401, __( 'Invalid post type.' ) );
2218 $cap = 'publish_posts';
2219 elseif ('publish' == $content_struct['post_status'])
2220 $cap = 'publish_posts';
2222 $cap = 'edit_posts';
2223 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
2224 $post_type = 'post';
2227 if ( !current_user_can( $cap ) )
2228 return new IXR_Error( 401, $error_message );
2230 // Check for a valid post format if one was given
2231 if ( isset( $content_struct['wp_post_format'] ) ) {
2232 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
2233 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
2234 return new IXR_Error( 404, __( 'Invalid post format' ) );
2238 // Let WordPress generate the post_name (slug) unless
2239 // one has been provided.
2241 if ( isset($content_struct['wp_slug']) )
2242 $post_name = $content_struct['wp_slug'];
2244 // Only use a password if one was given.
2245 if ( isset($content_struct['wp_password']) )
2246 $post_password = $content_struct['wp_password'];
2248 // Only set a post parent if one was provided.
2249 if ( isset($content_struct['wp_page_parent_id']) )
2250 $post_parent = $content_struct['wp_page_parent_id'];
2252 // Only set the menu_order if it was provided.
2253 if ( isset($content_struct['wp_page_order']) )
2254 $menu_order = $content_struct['wp_page_order'];
2256 $post_author = $user->ID;
2258 // If an author id was provided then use it instead.
2259 if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
2260 switch ( $post_type ) {
2262 if ( !current_user_can('edit_others_posts') )
2263 return(new IXR_Error(401, __('You are not allowed to post as this user')));
2266 if ( !current_user_can('edit_others_pages') )
2267 return(new IXR_Error(401, __('You are not allowed to create pages as this user')));
2270 return(new IXR_Error(401, __('Invalid post type.')));
2273 $post_author = $content_struct['wp_author_id'];
2276 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
2277 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
2279 $post_status = $publish ? 'publish' : 'draft';
2281 if ( isset( $content_struct["{$post_type}_status"] ) ) {
2282 switch ( $content_struct["{$post_type}_status"] ) {
2287 $post_status = $content_struct["{$post_type}_status"];
2290 $post_status = $publish ? 'publish' : 'draft';
2295 $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
2296 $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
2298 $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
2300 if ( isset($content_struct['mt_allow_comments']) ) {
2301 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
2302 switch ( $content_struct['mt_allow_comments'] ) {
2304 $comment_status = 'closed';
2307 $comment_status = 'open';
2310 $comment_status = get_option('default_comment_status');
2314 switch ( (int) $content_struct['mt_allow_comments'] ) {
2317 $comment_status = 'closed';
2320 $comment_status = 'open';
2323 $comment_status = get_option('default_comment_status');
2328 $comment_status = get_option('default_comment_status');
2331 if ( isset($content_struct['mt_allow_pings']) ) {
2332 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
2333 switch ( $content_struct['mt_allow_pings'] ) {
2335 $ping_status = 'closed';
2338 $ping_status = 'open';
2341 $ping_status = get_option('default_ping_status');
2345 switch ( (int) $content_struct['mt_allow_pings'] ) {
2347 $ping_status = 'closed';
2350 $ping_status = 'open';
2353 $ping_status = get_option('default_ping_status');
2358 $ping_status = get_option('default_ping_status');
2362 $post_content = $post_content . '<!--more-->' . $post_more;
2365 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
2366 $to_ping = $content_struct['mt_tb_ping_urls'];
2367 if ( is_array($to_ping) )
2368 $to_ping = implode(' ', $to_ping);
2371 // Do some timestamp voodoo
2372 if ( !empty( $content_struct['date_created_gmt'] ) )
2373 $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
2374 elseif ( !empty( $content_struct['dateCreated']) )
2375 $dateCreated = $content_struct['dateCreated']->getIso();
2377 if ( !empty( $dateCreated ) ) {
2378 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
2379 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
2381 $post_date = current_time('mysql');
2382 $post_date_gmt = current_time('mysql', 1);
2385 $post_category = array();
2386 if ( isset( $content_struct['categories'] ) ) {
2387 $catnames = $content_struct['categories'];
2388 logIO('O', 'Post cats: ' . var_export($catnames,true));
2390 if ( is_array($catnames) ) {
2391 foreach ($catnames as $cat) {
2392 $post_category[] = get_cat_ID($cat);
2397 $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');
2399 $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
2401 // Only posts can be sticky
2402 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
2403 if ( $content_struct['sticky'] == true )
2404 stick_post( $post_ID );
2405 elseif ( $content_struct['sticky'] == false )
2406 unstick_post( $post_ID );
2409 if ( isset($content_struct['custom_fields']) )
2410 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
2412 // Handle enclosures
2413 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
2414 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
2416 $this->attach_uploads( $post_ID, $post_content );
2418 // Handle post formats if assigned, value is validated earlier
2420 if ( isset( $content_struct['wp_post_format'] ) )
2421 wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
2423 $post_ID = wp_insert_post( $postdata, true );
2424 if ( is_wp_error( $post_ID ) )
2425 return new IXR_Error(500, $post_ID->get_error_message());
2428 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
2430 logIO('O', "Posted ! ID: $post_ID");
2432 return strval($post_ID);
2435 function add_enclosure_if_new($post_ID, $enclosure) {
2436 if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
2438 $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'];
2440 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
2441 if ($key == 'enclosure') {
2442 foreach ( (array) $val as $enc ) {
2443 if ($enc == $encstring) {
2451 add_post_meta( $post_ID, 'enclosure', $encstring );
2456 * Attach upload to a post.
2460 * @param int $post_ID Post ID.
2461 * @param string $post_content Post Content for attachment.
2463 function attach_uploads( $post_ID, $post_content ) {
2466 // find any unattached files
2467 $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
2468 if ( is_array( $attachments ) ) {
2469 foreach ( $attachments as $file ) {
2470 if ( strpos( $post_content, $file->guid ) !== false )
2471 $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
2481 * @param array $args Method parameters.
2482 * @return bool True on success.
2484 function mw_editPost($args) {
2486 $this->escape($args);
2488 $post_ID = (int) $args[0];
2489 $username = $args[1];
2490 $password = $args[2];
2491 $content_struct = $args[3];
2492 $publish = $args[4];
2494 if ( !$user = $this->login($username, $password) )
2495 return $this->error;
2497 do_action('xmlrpc_call', 'metaWeblog.editPost');
2499 $cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
2500 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
2501 $post_type = 'post';
2502 $page_template = '';
2503 if ( !empty( $content_struct['post_type'] ) ) {
2504 if ( $content_struct['post_type'] == 'page' ) {
2505 if ( $publish || 'publish' == $content_struct['page_status'] )
2506 $cap = 'publish_pages';
2508 $cap = 'edit_pages';
2509 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
2510 $post_type = 'page';
2511 if ( !empty( $content_struct['wp_page_template'] ) )
2512 $page_template = $content_struct['wp_page_template'];
2513 } elseif ( $content_struct['post_type'] == 'post' ) {
2514 if ( $publish || 'publish' == $content_struct['post_status'] )
2515 $cap = 'publish_posts';
2517 $cap = 'edit_posts';
2518 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
2519 $post_type = 'post';
2521 // No other post_type values are allowed here
2522 return new IXR_Error( 401, __( 'Invalid post type.' ) );
2525 if ( $publish || 'publish' == $content_struct['post_status'] )
2526 $cap = 'publish_posts';
2528 $cap = 'edit_posts';
2529 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
2530 $post_type = 'post';
2533 if ( !current_user_can( $cap ) )
2534 return new IXR_Error( 401, $error_message );
2536 // Check for a valid post format if one was given
2537 if ( isset( $content_struct['wp_post_format'] ) ) {
2538 $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
2539 if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
2540 return new IXR_Error( 404, __( 'Invalid post format' ) );
2544 $postdata = wp_get_single_post($post_ID, ARRAY_A);
2546 // If there is no post data for the give post id, stop
2547 // now and return an error. Other wise a new post will be
2548 // created (which was the old behavior).
2549 if ( empty($postdata["ID"]) )
2550 return(new IXR_Error(404, __('Invalid post ID.')));
2552 $this->escape($postdata);
2553 extract($postdata, EXTR_SKIP);
2555 // Let WordPress manage slug if none was provided.
2557 $post_name = $postdata['post_name'];
2558 if ( isset($content_struct['wp_slug']) )
2559 $post_name = $content_struct['wp_slug'];
2561 // Only use a password if one was given.
2562 if ( isset($content_struct['wp_password']) )
2563 $post_password = $content_struct['wp_password'];
2565 // Only set a post parent if one was given.
2566 if ( isset($content_struct['wp_page_parent_id']) )
2567 $post_parent = $content_struct['wp_page_parent_id'];
2569 // Only set the menu_order if it was given.
2570 if ( isset($content_struct['wp_page_order']) )
2571 $menu_order = $content_struct['wp_page_order'];
2573 $post_author = $postdata['post_author'];
2575 // Only set the post_author if one is set.
2576 if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
2577 switch ( $post_type ) {
2579 if ( !current_user_can('edit_others_posts') )
2580 return(new IXR_Error(401, __('You are not allowed to change the post author as this user.')));
2583 if ( !current_user_can('edit_others_pages') )
2584 return(new IXR_Error(401, __('You are not allowed to change the page author as this user.')));
2587 return(new IXR_Error(401, __('Invalid post type.')));
2590 $post_author = $content_struct['wp_author_id'];
2593 if ( isset($content_struct['mt_allow_comments']) ) {
2594 if ( !is_numeric($content_struct['mt_allow_comments']) ) {
2595 switch ( $content_struct['mt_allow_comments'] ) {
2597 $comment_status = 'closed';
2600 $comment_status = 'open';
2603 $comment_status = get_option('default_comment_status');
2607 switch ( (int) $content_struct['mt_allow_comments'] ) {
2610 $comment_status = 'closed';
2613 $comment_status = 'open';
2616 $comment_status = get_option('default_comment_status');
2622 if ( isset($content_struct['mt_allow_pings']) ) {
2623 if ( !is_numeric($content_struct['mt_allow_pings']) ) {
2624 switch ( $content_struct['mt_allow_pings'] ) {
2626 $ping_status = 'closed';
2629 $ping_status = 'open';
2632 $ping_status = get_option('default_ping_status');
2636 switch ( (int) $content_struct["mt_allow_pings"] ) {
2638 $ping_status = 'closed';
2641 $ping_status = 'open';
2644 $ping_status = get_option('default_ping_status');
2650 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
2651 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
2653 $post_category = array();
2654 if ( isset( $content_struct['categories'] ) ) {
2655 $catnames = $content_struct['categories'];
2656 if ( is_array($catnames) ) {
2657 foreach ($catnames as $cat) {
2658 $post_category[] = get_cat_ID($cat);
2663 $post_excerpt = isset( $content_struct['mt_excerpt'] ) ? $content_struct['mt_excerpt'] : null;
2664 $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
2666 $post_status = $publish ? 'publish' : 'draft';
2667 if ( isset( $content_struct["{$post_type}_status"] ) ) {
2668 switch( $content_struct["{$post_type}_status"] ) {
2673 $post_status = $content_struct["{$post_type}_status"];
2676 $post_status = $publish ? 'publish' : 'draft';
2681 $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
2683 if ( ('publish' == $post_status) ) {
2684 if ( ( 'page' == $post_type ) && !current_user_can('publish_pages') )
2685 return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
2686 else if ( !current_user_can('publish_posts') )
2687 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
2691 $post_content = $post_content . "<!--more-->" . $post_more;
2694 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
2695 $to_ping = $content_struct['mt_tb_ping_urls'];
2696 if ( is_array($to_ping) )
2697 $to_ping = implode(' ', $to_ping);
2700 // Do some timestamp voodoo
2701 if ( !empty( $content_struct['date_created_gmt'] ) )
2702 $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
2703 elseif ( !empty( $content_struct['dateCreated']) )
2704 $dateCreated = $content_struct['dateCreated']->getIso();
2706 if ( !empty( $dateCreated ) ) {
2707 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
2708 $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
2710 $post_date = $postdata['post_date'];
2711 $post_date_gmt = $postdata['post_date_gmt'];
2714 // We've got all the data -- post it:
2715 $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');
2717 $result = wp_update_post($newpost, true);
2718 if ( is_wp_error( $result ) )
2719 return new IXR_Error(500, $result->get_error_message());
2722 return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
2724 // Only posts can be sticky
2725 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
2726 if ( $content_struct['sticky'] == true )
2727 stick_post( $post_ID );
2728 elseif ( $content_struct['sticky'] == false )
2729 unstick_post( $post_ID );
2732 if ( isset($content_struct['custom_fields']) )
2733 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
2735 // Handle enclosures
2736 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
2737 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
2739 $this->attach_uploads( $ID, $post_content );
2741 // Handle post formats if assigned, validation is handled
2742 // earlier in this function
2743 if ( isset( $content_struct['wp_post_format'] ) )
2744 wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
2746 logIO('O',"(MW) Edited ! ID: $post_ID");
2756 * @param array $args Method parameters.
2759 function mw_getPost($args) {
2761 $this->escape($args);
2763 $post_ID = (int) $args[0];
2764 $username = $args[1];
2765 $password = $args[2];
2767 if ( !$user = $this->login($username, $password) )
2768 return $this->error;
2770 if ( !current_user_can( 'edit_post', $post_ID ) )
2771 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
2773 do_action('xmlrpc_call', 'metaWeblog.getPost');
2775 $postdata = wp_get_single_post($post_ID, ARRAY_A);
2777 if ($postdata['post_date'] != '') {
2778 $post_date = mysql2date('Ymd\TH:i:s', $postdata['post_date'], false);
2779 $post_date_gmt = mysql2date('Ymd\TH:i:s', $postdata['post_date_gmt'], false);
2781 // For drafts use the GMT version of the post date
2782 if ( $postdata['post_status'] == 'draft' )
2783 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $postdata['post_date'] ), 'Ymd\TH:i:s' );
2785 $categories = array();
2786 $catids = wp_get_post_categories($post_ID);
2787 foreach($catids as $catid)
2788 $categories[] = get_cat_name($catid);
2790 $tagnames = array();
2791 $tags = wp_get_post_tags( $post_ID );
2792 if ( !empty( $tags ) ) {
2793 foreach ( $tags as $tag )
2794 $tagnames[] = $tag->name;
2795 $tagnames = implode( ', ', $tagnames );
2800 $post = get_extended($postdata['post_content']);
2801 $link = post_permalink($postdata['ID']);
2803 // Get the author info.
2804 $author = get_userdata($postdata['post_author']);
2806 $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
2807 $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
2809 // Consider future posts as published
2810 if ( $postdata['post_status'] === 'future' )
2811 $postdata['post_status'] = 'publish';
2814 $post_format = get_post_format( $post_ID );
2815 if ( empty( $post_format ) )
2816 $post_format = 'standard';
2819 if ( is_sticky( $post_ID ) )
2822 $enclosure = array();
2823 foreach ( (array) get_post_custom($post_ID) as $key => $val) {
2824 if ($key == 'enclosure') {
2825 foreach ( (array) $val as $enc ) {
2826 $encdata = split("\n", $enc);
2827 $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
2828 $enclosure['length'] = (int) trim($encdata[1]);
2829 $enclosure['type'] = trim($encdata[2]);
2836 'dateCreated' => new IXR_Date($post_date),
2837 'userid' => $postdata['post_author'],
2838 'postid' => $postdata['ID'],
2839 'description' => $post['main'],
2840 'title' => $postdata['post_title'],
2842 'permaLink' => $link,
2843 // commented out because no other tool seems to use this
2844 // 'content' => $entry['post_content'],
2845 'categories' => $categories,
2846 'mt_excerpt' => $postdata['post_excerpt'],
2847 'mt_text_more' => $post['extended'],
2848 'mt_allow_comments' => $allow_comments,
2849 'mt_allow_pings' => $allow_pings,
2850 'mt_keywords' => $tagnames,
2851 'wp_slug' => $postdata['post_name'],
2852 'wp_password' => $postdata['post_password'],
2853 'wp_author_id' => (string) $author->ID,
2854 'wp_author_display_name' => $author->display_name,
2855 'date_created_gmt' => new IXR_Date($post_date_gmt),
2856 'post_status' => $postdata['post_status'],
2857 'custom_fields' => $this->get_custom_fields($post_ID),
2858 'wp_post_format' => $post_format,
2862 if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
2866 return new IXR_Error(404, __('Sorry, no such post.'));
2871 * Retrieve list of recent posts.
2875 * @param array $args Method parameters.
2878 function mw_getRecentPosts($args) {
2880 $this->escape($args);
2882 $blog_ID = (int) $args[0];
2883 $username = $args[1];
2884 $password = $args[2];
2885 if ( isset( $args[3] ) )
2886 $query = array( 'numberposts' => absint( $args[3] ) );
2890 if ( !$user = $this->login($username, $password) )
2891 return $this->error;
2893 do_action('xmlrpc_call', 'metaWeblog.getRecentPosts');
2895 $posts_list = wp_get_recent_posts( $query );
2900 foreach ($posts_list as $entry) {
2901 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
2904 $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
2905 $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt'], false);
2907 // For drafts use the GMT version of the date
2908 if ( $entry['post_status'] == 'draft' )
2909 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s' );
2911 $categories = array();
2912 $catids = wp_get_post_categories($entry['ID']);
2913 foreach( $catids as $catid )
2914 $categories[] = get_cat_name($catid);
2916 $tagnames = array();
2917 $tags = wp_get_post_tags( $entry['ID'] );
2918 if ( !empty( $tags ) ) {
2919 foreach ( $tags as $tag ) {
2920 $tagnames[] = $tag->name;
2922 $tagnames = implode( ', ', $tagnames );
2927 $post = get_extended($entry['post_content']);
2928 $link = post_permalink($entry['ID']);
2930 // Get the post author info.
2931 $author = get_userdata($entry['post_author']);
2933 $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
2934 $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
2936 // Consider future posts as published
2937 if ( $entry['post_status'] === 'future' )
2938 $entry['post_status'] = 'publish';
2941 $post_format = get_post_format( $entry['ID'] );
2942 if ( empty( $post_format ) )
2943 $post_format = 'standard';
2946 'dateCreated' => new IXR_Date($post_date),
2947 'userid' => $entry['post_author'],
2948 'postid' => (string) $entry['ID'],
2949 'description' => $post['main'],
2950 'title' => $entry['post_title'],
2952 'permaLink' => $link,
2953 // commented out because no other tool seems to use this
2954 // 'content' => $entry['post_content'],
2955 'categories' => $categories,
2956 'mt_excerpt' => $entry['post_excerpt'],
2957 'mt_text_more' => $post['extended'],
2958 'mt_allow_comments' => $allow_comments,
2959 'mt_allow_pings' => $allow_pings,
2960 'mt_keywords' => $tagnames,
2961 'wp_slug' => $entry['post_name'],
2962 'wp_password' => $entry['post_password'],
2963 'wp_author_id' => (string) $author->ID,
2964 'wp_author_display_name' => $author->display_name,
2965 'date_created_gmt' => new IXR_Date($post_date_gmt),
2966 'post_status' => $entry['post_status'],
2967 'custom_fields' => $this->get_custom_fields($entry['ID']),
2968 'wp_post_format' => $post_format
2973 $recent_posts = array();
2974 for ( $j=0; $j<count($struct); $j++ ) {
2975 array_push($recent_posts, $struct[$j]);
2978 return $recent_posts;
2982 * Retrieve the list of categories on a given blog.
2986 * @param array $args Method parameters.
2989 function mw_getCategories($args) {
2991 $this->escape($args);
2993 $blog_ID = (int) $args[0];
2994 $username = $args[1];
2995 $password = $args[2];
2997 if ( !$user = $this->login($username, $password) )
2998 return $this->error;
3000 if ( !current_user_can( 'edit_posts' ) )
3001 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
3003 do_action('xmlrpc_call', 'metaWeblog.getCategories');
3005 $categories_struct = array();
3007 if ( $cats = get_categories(array('get' => 'all')) ) {
3008 foreach ( $cats as $cat ) {
3009 $struct['categoryId'] = $cat->term_id;
3010 $struct['parentId'] = $cat->parent;
3011 $struct['description'] = $cat->name;
3012 $struct['categoryDescription'] = $cat->description;
3013 $struct['categoryName'] = $cat->name;
3014 $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
3015 $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
3017 $categories_struct[] = $struct;
3021 return $categories_struct;
3025 * Uploads a file, following your settings.
3027 * Adapted from a patch by Johann Richard.
3029 * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
3033 * @param array $args Method parameters.
3036 function mw_newMediaObject($args) {
3039 $blog_ID = (int) $args[0];
3040 $username = $wpdb->escape($args[1]);
3041 $password = $wpdb->escape($args[2]);
3044 $name = sanitize_file_name( $data['name'] );
3045 $type = $data['type'];
3046 $bits = $data['bits'];
3048 logIO('O', '(MW) Received '.strlen($bits).' bytes');
3050 if ( !$user = $this->login($username, $password) )
3051 return $this->error;
3053 do_action('xmlrpc_call', 'metaWeblog.newMediaObject');
3055 if ( !current_user_can('upload_files') ) {
3056 logIO('O', '(MW) User does not have upload_files capability');
3057 $this->error = new IXR_Error(401, __('You are not allowed to upload files to this site.'));
3058 return $this->error;
3061 if ( $upload_err = apply_filters( 'pre_upload_error', false ) )
3062 return new IXR_Error(500, $upload_err);
3064 if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) {
3065 // Get postmeta info on the object.
3066 $old_file = $wpdb->get_row("
3069 WHERE post_title = '{$name}'
3070 AND post_type = 'attachment'
3073 // Delete previous file.
3074 wp_delete_attachment($old_file->ID);
3076 // Make sure the new name is different by pre-pending the
3077 // previous post id.
3078 $filename = preg_replace('/^wpid\d+-/', '', $name);
3079 $name = "wpid{$old_file->ID}-{$filename}";
3082 $upload = wp_upload_bits($name, NULL, $bits);
3083 if ( ! empty($upload['error']) ) {
3084 $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
3085 logIO('O', '(MW) ' . $errorString);
3086 return new IXR_Error(500, $errorString);
3088 // Construct the attachment array
3089 // attach to post_id 0
3091 $attachment = array(
3092 'post_title' => $name,
3093 'post_content' => '',
3094 'post_type' => 'attachment',
3095 'post_parent' => $post_id,
3096 'post_mime_type' => $type,
3097 'guid' => $upload[ 'url' ]
3101 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
3102 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
3104 return apply_filters( 'wp_handle_upload', array( 'file' => $name, 'url' => $upload[ 'url' ], 'type' => $type ), 'upload' );
3107 /* MovableType API functions
3108 * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
3112 * Retrieve the post titles of recent posts.
3116 * @param array $args Method parameters.
3119 function mt_getRecentPostTitles($args) {
3121 $this->escape($args);
3123 $blog_ID = (int) $args[0];
3124 $username = $args[1];
3125 $password = $args[2];
3126 if ( isset( $args[3] ) )
3127 $query = array( 'numberposts' => absint( $args[3] ) );
3131 if ( !$user = $this->login($username, $password) )
3132 return $this->error;
3134 do_action('xmlrpc_call', 'mt.getRecentPostTitles');
3136 $posts_list = wp_get_recent_posts( $query );
3138 if ( !$posts_list ) {
3139 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
3140 return $this->error;
3143 foreach ($posts_list as $entry) {
3144 if ( !current_user_can( 'edit_post', $entry['ID'] ) )
3147 $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
3148 $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt'], false);
3150 // For drafts use the GMT version of the date
3151 if ( $entry['post_status'] == 'draft' )
3152 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s' );
3155 'dateCreated' => new IXR_Date($post_date),
3156 'userid' => $entry['post_author'],
3157 'postid' => (string) $entry['ID'],
3158 'title' => $entry['post_title'],
3159 'post_status' => $entry['post_status'],
3160 'date_created_gmt' => new IXR_Date($post_date_gmt)
3165 $recent_posts = array();
3166 for ( $j=0; $j<count($struct); $j++ ) {
3167 array_push($recent_posts, $struct[$j]);
3170 return $recent_posts;
3174 * Retrieve list of all categories on blog.
3178 * @param array $args Method parameters.
3181 function mt_getCategoryList($args) {
3183 $this->escape($args);
3185 $blog_ID = (int) $args[0];
3186 $username = $args[1];
3187 $password = $args[2];
3189 if ( !$user = $this->login($username, $password) )
3190 return $this->error;
3192 if ( !current_user_can( 'edit_posts' ) )
3193 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
3195 do_action('xmlrpc_call', 'mt.getCategoryList');
3197 $categories_struct = array();
3199 if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
3200 foreach ( $cats as $cat ) {
3201 $struct['categoryId'] = $cat->term_id;
3202 $struct['categoryName'] = $cat->name;
3204 $categories_struct[] = $struct;
3208 return $categories_struct;
3212 * Retrieve post categories.
3216 * @param array $args Method parameters.
3219 function mt_getPostCategories($args) {
3221 $this->escape($args);
3223 $post_ID = (int) $args[0];
3224 $username = $args[1];
3225 $password = $args[2];
3227 if ( !$user = $this->login($username, $password) )
3228 return $this->error;
3230 if ( !current_user_can( 'edit_post', $post_ID ) )
3231 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
3233 do_action('xmlrpc_call', 'mt.getPostCategories');
3235 $categories = array();
3236 $catids = wp_get_post_categories(intval($post_ID));
3237 // first listed category will be the primary category
3239 foreach ( $catids as $catid ) {
3240 $categories[] = array(
3241 'categoryName' => get_cat_name($catid),
3242 'categoryId' => (string) $catid,
3243 'isPrimary' => $isPrimary
3252 * Sets categories for a post.
3256 * @param array $args Method parameters.
3257 * @return bool True on success.
3259 function mt_setPostCategories($args) {
3261 $this->escape($args);
3263 $post_ID = (int) $args[0];
3264 $username = $args[1];
3265 $password = $args[2];
3266 $categories = $args[3];
3268 if ( !$user = $this->login($username, $password) )
3269 return $this->error;
3271 do_action('xmlrpc_call', 'mt.setPostCategories');
3273 if ( !current_user_can('edit_post', $post_ID) )
3274 return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
3276 foreach ( $categories as $cat ) {
3277 $catids[] = $cat['categoryId'];
3280 wp_set_post_categories($post_ID, $catids);
3286 * Retrieve an array of methods supported by this server.
3290 * @param array $args Method parameters.
3293 function mt_supportedMethods($args) {
3295 do_action('xmlrpc_call', 'mt.supportedMethods');
3297 $supported_methods = array();
3298 foreach ( $this->methods as $key => $value ) {
3299 $supported_methods[] = $key;
3302 return $supported_methods;
3306 * Retrieve an empty array because we don't support per-post text filters.
3310 * @param array $args Method parameters.
3312 function mt_supportedTextFilters($args) {
3313 do_action('xmlrpc_call', 'mt.supportedTextFilters');
3314 return apply_filters('xmlrpc_text_filters', array());
3318 * Retrieve trackbacks sent to a given post.
3322 * @param array $args Method parameters.
3325 function mt_getTrackbackPings($args) {
3329 $post_ID = intval($args);
3331 do_action('xmlrpc_call', 'mt.getTrackbackPings');
3333 $actual_post = wp_get_single_post($post_ID, ARRAY_A);
3335 if ( !$actual_post )
3336 return new IXR_Error(404, __('Sorry, no such post.'));
3338 $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) );
3343 $trackback_pings = array();
3344 foreach ( $comments as $comment ) {
3345 if ( 'trackback' == $comment->comment_type ) {
3346 $content = $comment->comment_content;
3347 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
3348 $trackback_pings[] = array(
3349 'pingTitle' => $title,
3350 'pingURL' => $comment->comment_author_url,
3351 'pingIP' => $comment->comment_author_IP
3356 return $trackback_pings;
3360 * Sets a post's publish status to 'publish'.
3364 * @param array $args Method parameters.
3367 function mt_publishPost($args) {
3369 $this->escape($args);
3371 $post_ID = (int) $args[0];
3372 $username = $args[1];
3373 $password = $args[2];
3375 if ( !$user = $this->login($username, $password) )
3376 return $this->error;
3378 do_action('xmlrpc_call', 'mt.publishPost');
3380 if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
3381 return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
3383 $postdata = wp_get_single_post($post_ID,ARRAY_A);
3385 $postdata['post_status'] = 'publish';
3388 $cats = wp_get_post_categories($post_ID);
3389 $postdata['post_category'] = $cats;
3390 $this->escape($postdata);
3392 $result = wp_update_post($postdata);
3397 /* PingBack functions
3398 * specs on www.hixie.ch/specs/pingback/pingback
3402 * Retrieves a pingback and registers it.
3406 * @param array $args Method parameters.
3409 function pingback_ping($args) {
3412 do_action('xmlrpc_call', 'pingback.ping');
3414 $this->escape($args);
3416 $pagelinkedfrom = $args[0];
3417 $pagelinkedto = $args[1];
3421 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
3422 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
3423 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
3425 // Check if the page linked to is in our site
3426 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
3428 return new IXR_Error(0, __('Is there no link to us?'));
3430 // let's find which post is linked to
3431 // FIXME: does url_to_postid() cover all these cases already?
3432 // if so, then let's use it and drop the old code.
3433 $urltest = parse_url($pagelinkedto);
3434 if ( $post_ID = url_to_postid($pagelinkedto) ) {
3435 $way = 'url_to_postid()';
3436 } elseif ( preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
3437 // the path defines the post_ID (archives/p/XXXX)
3438 $blah = explode('/', $match[0]);
3439 $post_ID = (int) $blah[1];
3440 $way = 'from the path';
3441 } elseif ( preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
3442 // the querystring defines the post_ID (?p=XXXX)
3443 $blah = explode('=', $match[0]);
3444 $post_ID = (int) $blah[1];
3445 $way = 'from the querystring';
3446 } elseif ( isset($urltest['fragment']) ) {
3447 // an #anchor is there, it's either...
3448 if ( intval($urltest['fragment']) ) {
3449 // ...an integer #XXXX (simplest case)
3450 $post_ID = (int) $urltest['fragment'];
3451 $way = 'from the fragment (numeric)';
3452 } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
3453 // ...a post id in the form 'post-###'
3454 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
3455 $way = 'from the fragment (post-###)';
3456 } elseif ( is_string($urltest['fragment']) ) {
3457 // ...or a string #title, a little more complicated
3458 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
3459 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", like_escape( $title ) );
3460 if (! ($post_ID = $wpdb->get_var($sql)) ) {
3461 // returning unknown error '0' is better than die()ing
3462 return new IXR_Error(0, '');
3464 $way = 'from the fragment (title)';
3467 // TODO: Attempt to extract a post ID from the given URL
3468 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.'));
3470 $post_ID = (int) $post_ID;
3473 logIO("O","(PB) URL='$pagelinkedto' ID='$post_ID' Found='$way'");
3475 $post = get_post($post_ID);
3477 if ( !$post ) // Post_ID not found
3478 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.'));
3480 if ( $post_ID == url_to_postid($pagelinkedfrom) )
3481 return new IXR_Error(0, __('The source URL and the target URL cannot both point to the same resource.'));
3483 // Check if pings are on
3484 if ( !pings_open($post) )
3485 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.'));
3487 // Let's check that the remote site didn't already pingback this entry
3488 if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
3489 return new IXR_Error( 48, __( 'The pingback has already been registered.' ) );
3491 // very stupid, but gives time to the 'from' server to publish !
3494 // Let's check the remote site
3495 $linea = wp_remote_fopen( $pagelinkedfrom );
3497 return new IXR_Error(16, __('The source URL does not exist.'));
3499 $linea = apply_filters('pre_remote_source', $linea, $pagelinkedto);
3501 // Work around bug in strip_tags():
3502 $linea = str_replace('<!DOC', '<DOC', $linea);
3503 $linea = preg_replace( '/[\s\r\n\t]+/', ' ', $linea ); // normalize spaces
3504 $linea = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
3506 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
3507 $title = $matchtitle[1];
3508 if ( empty( $title ) )
3509 return new IXR_Error(32, __('We cannot find a title on that page.'));
3511 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
3513 $p = explode( "\n\n", $linea );
3515 $preg_target = preg_quote($pagelinkedto, '|');
3517 foreach ( $p as $para ) {
3518 if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
3519 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
3521 // If the URL isn't in a link context, keep looking
3522 if ( empty($context) )
3525 // We're going to use this fake tag to mark the context in a bit
3526 // the marker is needed in case the link text appears more than once in the paragraph
3527 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
3529 // prevent really long link text
3530 if ( strlen($context[1]) > 100 )
3531 $context[1] = substr($context[1], 0, 100) . '...';
3533 $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
3534 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
3535 $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
3536 $excerpt = trim($excerpt);
3537 $preg_marker = preg_quote($marker, '|');
3538 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
3539 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
3544 if ( empty($context) ) // Link to target not found
3545 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.'));
3547 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
3549 $context = '[...] ' . esc_html( $excerpt ) . ' [...]';
3550 $pagelinkedfrom = $wpdb->escape( $pagelinkedfrom );
3552 $comment_post_ID = (int) $post_ID;
3553 $comment_author = $title;
3554 $comment_author_email = '';
3555 $this->escape($comment_author);
3556 $comment_author_url = $pagelinkedfrom;
3557 $comment_content = $context;
3558 $this->escape($comment_content);
3559 $comment_type = 'pingback';
3561 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
3563 $comment_ID = wp_new_comment($commentdata);
3564 do_action('pingback_post', $comment_ID);
3566 return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
3570 * Retrieve array of URLs that pingbacked the given URL.
3572 * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
3576 * @param array $args Method parameters.
3579 function pingback_extensions_getPingbacks($args) {
3583 do_action('xmlrpc_call', 'pingback.extensions.getPingbacks');
3585 $this->escape($args);
3589 $post_ID = url_to_postid($url);
3591 // We aren't sure that the resource is available and/or pingback enabled
3592 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.'));
3595 $actual_post = wp_get_single_post($post_ID, ARRAY_A);
3597 if ( !$actual_post ) {
3598 // No such post = resource not found
3599 return new IXR_Error(32, __('The specified target URL does not exist.'));
3602 $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) );
3607 $pingbacks = array();
3608 foreach ( $comments as $comment ) {
3609 if ( 'pingback' == $comment->comment_type )
3610 $pingbacks[] = $comment->comment_author_url;