]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-xmlrpc-server.php
WordPress 4.0
[autoinstalls/wordpress.git] / wp-includes / class-wp-xmlrpc-server.php
1 <?php
2 /**
3  * XML-RPC protocol support for WordPress
4  *
5  * @package WordPress
6  * @subpackage Publishing
7  */
8
9 /**
10  * WordPress XMLRPC server implementation.
11  *
12  * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
13  * pingback. Additional WordPress API for managing comments, pages, posts,
14  * options, etc.
15  *
16  * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled
17  * via the xmlrpc_enabled filter found in wp_xmlrpc_server::login().
18  *
19  * @package WordPress
20  * @subpackage Publishing
21  * @since 1.5.0
22  */
23 class wp_xmlrpc_server extends IXR_Server {
24
25         /**
26          * Register all of the XMLRPC methods that XMLRPC server understands.
27          *
28          * Sets up server and method property. Passes XMLRPC
29          * methods through the 'xmlrpc_methods' filter to allow plugins to extend
30          * or replace XMLRPC methods.
31          *
32          * @since 1.5.0
33          *
34          * @return wp_xmlrpc_server
35          */
36         public function __construct() {
37                 $this->methods = array(
38                         // WordPress API
39                         'wp.getUsersBlogs'              => 'this:wp_getUsersBlogs',
40                         'wp.newPost'                    => 'this:wp_newPost',
41                         'wp.editPost'                   => 'this:wp_editPost',
42                         'wp.deletePost'                 => 'this:wp_deletePost',
43                         'wp.getPost'                    => 'this:wp_getPost',
44                         'wp.getPosts'                   => 'this:wp_getPosts',
45                         'wp.newTerm'                    => 'this:wp_newTerm',
46                         'wp.editTerm'                   => 'this:wp_editTerm',
47                         'wp.deleteTerm'                 => 'this:wp_deleteTerm',
48                         'wp.getTerm'                    => 'this:wp_getTerm',
49                         'wp.getTerms'                   => 'this:wp_getTerms',
50                         'wp.getTaxonomy'                => 'this:wp_getTaxonomy',
51                         'wp.getTaxonomies'              => 'this:wp_getTaxonomies',
52                         'wp.getUser'                    => 'this:wp_getUser',
53                         'wp.getUsers'                   => 'this:wp_getUsers',
54                         'wp.getProfile'                 => 'this:wp_getProfile',
55                         'wp.editProfile'                => 'this:wp_editProfile',
56                         'wp.getPage'                    => 'this:wp_getPage',
57                         'wp.getPages'                   => 'this:wp_getPages',
58                         'wp.newPage'                    => 'this:wp_newPage',
59                         'wp.deletePage'                 => 'this:wp_deletePage',
60                         'wp.editPage'                   => 'this:wp_editPage',
61                         'wp.getPageList'                => 'this:wp_getPageList',
62                         'wp.getAuthors'                 => 'this:wp_getAuthors',
63                         'wp.getCategories'              => 'this:mw_getCategories',             // Alias
64                         'wp.getTags'                    => 'this:wp_getTags',
65                         'wp.newCategory'                => 'this:wp_newCategory',
66                         'wp.deleteCategory'             => 'this:wp_deleteCategory',
67                         'wp.suggestCategories'  => 'this:wp_suggestCategories',
68                         'wp.uploadFile'                 => 'this:mw_newMediaObject',    // Alias
69                         'wp.deleteFile'                 => 'this:wp_deletePost',                // Alias
70                         'wp.getCommentCount'    => 'this:wp_getCommentCount',
71                         'wp.getPostStatusList'  => 'this:wp_getPostStatusList',
72                         'wp.getPageStatusList'  => 'this:wp_getPageStatusList',
73                         'wp.getPageTemplates'   => 'this:wp_getPageTemplates',
74                         'wp.getOptions'                 => 'this:wp_getOptions',
75                         'wp.setOptions'                 => 'this:wp_setOptions',
76                         'wp.getComment'                 => 'this:wp_getComment',
77                         'wp.getComments'                => 'this:wp_getComments',
78                         'wp.deleteComment'              => 'this:wp_deleteComment',
79                         'wp.editComment'                => 'this:wp_editComment',
80                         'wp.newComment'                 => 'this:wp_newComment',
81                         'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
82                         'wp.getMediaItem'               => 'this:wp_getMediaItem',
83                         'wp.getMediaLibrary'    => 'this:wp_getMediaLibrary',
84                         'wp.getPostFormats'     => 'this:wp_getPostFormats',
85                         'wp.getPostType'                => 'this:wp_getPostType',
86                         'wp.getPostTypes'               => 'this:wp_getPostTypes',
87                         'wp.getRevisions'               => 'this:wp_getRevisions',
88                         'wp.restoreRevision'    => 'this:wp_restoreRevision',
89
90                         // Blogger API
91                         'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
92                         'blogger.getUserInfo' => 'this:blogger_getUserInfo',
93                         'blogger.getPost' => 'this:blogger_getPost',
94                         'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
95                         'blogger.newPost' => 'this:blogger_newPost',
96                         'blogger.editPost' => 'this:blogger_editPost',
97                         'blogger.deletePost' => 'this:blogger_deletePost',
98
99                         // MetaWeblog API (with MT extensions to structs)
100                         'metaWeblog.newPost' => 'this:mw_newPost',
101                         'metaWeblog.editPost' => 'this:mw_editPost',
102                         'metaWeblog.getPost' => 'this:mw_getPost',
103                         'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
104                         'metaWeblog.getCategories' => 'this:mw_getCategories',
105                         'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
106
107                         // MetaWeblog API aliases for Blogger API
108                         // see http://www.xmlrpc.com/stories/storyReader$2460
109                         'metaWeblog.deletePost' => 'this:blogger_deletePost',
110                         'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
111
112                         // MovableType API
113                         'mt.getCategoryList' => 'this:mt_getCategoryList',
114                         'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
115                         'mt.getPostCategories' => 'this:mt_getPostCategories',
116                         'mt.setPostCategories' => 'this:mt_setPostCategories',
117                         'mt.supportedMethods' => 'this:mt_supportedMethods',
118                         'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
119                         'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
120                         'mt.publishPost' => 'this:mt_publishPost',
121
122                         // PingBack
123                         'pingback.ping' => 'this:pingback_ping',
124                         'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
125
126                         'demo.sayHello' => 'this:sayHello',
127                         'demo.addTwoNumbers' => 'this:addTwoNumbers'
128                 );
129
130                 $this->initialise_blog_option_info();
131
132                 /**
133                  * Filter the methods exposed by the XML-RPC server.
134                  *
135                  * This filter can be used to add new methods, and remove built-in methods.
136                  *
137                  * @since 1.5.0
138                  *
139                  * @param array $methods An array of XML-RPC methods.
140                  */
141                 $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
142         }
143
144         /**
145          * Make private/protected methods readable for backwards compatibility.
146          *
147          * @since 4.0.0
148          * @access public
149          *
150          * @param callable $name      Method to call.
151          * @param array    $arguments Arguments to pass when calling.
152          * @return mixed|bool Return value of the callback, false otherwise.
153          */
154         public function __call( $name, $arguments ) {
155                 return call_user_func_array( array( $this, $name ), $arguments );
156         }
157
158         public function serve_request() {
159                 $this->IXR_Server($this->methods);
160         }
161
162         /**
163          * Test XMLRPC API by saying, "Hello!" to client.
164          *
165          * @since 1.5.0
166          *
167          * @param array $args Method Parameters.
168          * @return string
169          */
170         public function sayHello($args) {
171                 return 'Hello!';
172         }
173
174         /**
175          * Test XMLRPC API by adding two numbers for client.
176          *
177          * @since 1.5.0
178          *
179          * @param array $args Method Parameters.
180          * @return int
181          */
182         public function addTwoNumbers($args) {
183                 $number1 = $args[0];
184                 $number2 = $args[1];
185                 return $number1 + $number2;
186         }
187
188         /**
189          * Log user in.
190          *
191          * @since 2.8.0
192          *
193          * @param string $username User's username.
194          * @param string $password User's password.
195          * @return mixed WP_User object if authentication passed, false otherwise
196          */
197         public function login( $username, $password ) {
198                 /*
199                  * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
200                  * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
201                  */
202                 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
203                 if ( false === $enabled ) {
204                         $enabled = apply_filters( 'option_enable_xmlrpc', true );
205                 }
206
207                 /**
208                  * Filter whether XML-RPC is enabled.
209                  *
210                  * This is the proper filter for turning off XML-RPC.
211                  *
212                  * @since 3.5.0
213                  *
214                  * @param bool $enabled Whether XML-RPC is enabled. Default true.
215                  */
216                 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
217
218                 if ( ! $enabled ) {
219                         $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
220                         return false;
221                 }
222
223                 $user = wp_authenticate($username, $password);
224
225                 if (is_wp_error($user)) {
226                         $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
227
228                         /**
229                          * Filter the XML-RPC user login error message.
230                          *
231                          * @since 3.5.0
232                          *
233                          * @param string  $error The XML-RPC error message.
234                          * @param WP_User $user  WP_User object.
235                          */
236                         $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
237                         return false;
238                 }
239
240                 wp_set_current_user( $user->ID );
241                 return $user;
242         }
243
244         /**
245          * Check user's credentials. Deprecated.
246          *
247          * @since 1.5.0
248          * @deprecated 2.8.0
249          * @deprecated use wp_xmlrpc_server::login
250          * @see wp_xmlrpc_server::login
251          *
252          * @param string $username User's username.
253          * @param string $password User's password.
254          * @return bool Whether authentication passed.
255          */
256         public function login_pass_ok( $username, $password ) {
257                 return (bool) $this->login( $username, $password );
258         }
259
260         /**
261          * Escape string or array of strings for database.
262          *
263          * @since 1.5.2
264          *
265          * @param string|array $data Escape single string or array of strings.
266          * @return string|array Type matches $data and sanitized for the database.
267          */
268         public function escape( &$data ) {
269                 if ( ! is_array( $data ) )
270                         return wp_slash( $data );
271
272                 foreach ( $data as &$v ) {
273                         if ( is_array( $v ) )
274                                 $this->escape( $v );
275                         elseif ( ! is_object( $v ) )
276                                 $v = wp_slash( $v );
277                 }
278         }
279
280         /**
281          * Retrieve custom fields for post.
282          *
283          * @since 2.5.0
284          *
285          * @param int $post_id Post ID.
286          * @return array Custom fields, if exist.
287          */
288         public function get_custom_fields($post_id) {
289                 $post_id = (int) $post_id;
290
291                 $custom_fields = array();
292
293                 foreach ( (array) has_meta($post_id) as $meta ) {
294                         // Don't expose protected fields.
295                         if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
296                                 continue;
297
298                         $custom_fields[] = array(
299                                 "id"    => $meta['meta_id'],
300                                 "key"   => $meta['meta_key'],
301                                 "value" => $meta['meta_value']
302                         );
303                 }
304
305                 return $custom_fields;
306         }
307
308         /**
309          * Set custom fields for post.
310          *
311          * @since 2.5.0
312          *
313          * @param int $post_id Post ID.
314          * @param array $fields Custom fields.
315          */
316         public function set_custom_fields($post_id, $fields) {
317                 $post_id = (int) $post_id;
318
319                 foreach ( (array) $fields as $meta ) {
320                         if ( isset($meta['id']) ) {
321                                 $meta['id'] = (int) $meta['id'];
322                                 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
323                                 if ( isset($meta['key']) ) {
324                                         $meta['key'] = wp_unslash( $meta['key'] );
325                                         if ( $meta['key'] !== $pmeta->meta_key )
326                                                 continue;
327                                         $meta['value'] = wp_unslash( $meta['value'] );
328                                         if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
329                                                 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
330                                 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
331                                         delete_metadata_by_mid( 'post', $meta['id'] );
332                                 }
333                         } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
334                                 add_post_meta( $post_id, $meta['key'], $meta['value'] );
335                         }
336                 }
337         }
338
339         /**
340          * Set up blog options property.
341          *
342          * Passes property through 'xmlrpc_blog_options' filter.
343          *
344          * @since 2.6.0
345          */
346         public function initialise_blog_option_info() {
347                 global $wp_version;
348
349                 $this->blog_options = array(
350                         // Read only options
351                         'software_name'     => array(
352                                 'desc'          => __( 'Software Name' ),
353                                 'readonly'      => true,
354                                 'value'         => 'WordPress'
355                         ),
356                         'software_version'  => array(
357                                 'desc'          => __( 'Software Version' ),
358                                 'readonly'      => true,
359                                 'value'         => $wp_version
360                         ),
361                         'blog_url'          => array(
362                                 'desc'          => __( 'WordPress Address (URL)' ),
363                                 'readonly'      => true,
364                                 'option'        => 'siteurl'
365                         ),
366                         'home_url'          => array(
367                                 'desc'          => __( 'Site Address (URL)' ),
368                                 'readonly'      => true,
369                                 'option'        => 'home'
370                         ),
371                         'login_url'          => array(
372                                 'desc'          => __( 'Login Address (URL)' ),
373                                 'readonly'      => true,
374                                 'value'         => wp_login_url( )
375                         ),
376                         'admin_url'          => array(
377                                 'desc'          => __( 'The URL to the admin area' ),
378                                 'readonly'      => true,
379                                 'value'         => get_admin_url( )
380                         ),
381                         'image_default_link_type' => array(
382                                 'desc'          => __( 'Image default link type' ),
383                                 'readonly'      => true,
384                                 'option'        => 'image_default_link_type'
385                         ),
386                         'image_default_size' => array(
387                                 'desc'          => __( 'Image default size' ),
388                                 'readonly'      => true,
389                                 'option'        => 'image_default_size'
390                         ),
391                         'image_default_align' => array(
392                                 'desc'          => __( 'Image default align' ),
393                                 'readonly'      => true,
394                                 'option'        => 'image_default_align'
395                         ),
396                         'template'          => array(
397                                 'desc'          => __( 'Template' ),
398                                 'readonly'      => true,
399                                 'option'        => 'template'
400                         ),
401                         'stylesheet'        => array(
402                                 'desc'          => __( 'Stylesheet' ),
403                                 'readonly'      => true,
404                                 'option'        => 'stylesheet'
405                         ),
406                         'post_thumbnail'    => array(
407                                 'desc'          => __('Post Thumbnail'),
408                                 'readonly'      => true,
409                                 'value'         => current_theme_supports( 'post-thumbnails' )
410                         ),
411
412                         // Updatable options
413                         'time_zone'         => array(
414                                 'desc'          => __( 'Time Zone' ),
415                                 'readonly'      => false,
416                                 'option'        => 'gmt_offset'
417                         ),
418                         'blog_title'        => array(
419                                 'desc'          => __( 'Site Title' ),
420                                 'readonly'      => false,
421                                 'option'        => 'blogname'
422                         ),
423                         'blog_tagline'      => array(
424                                 'desc'          => __( 'Site Tagline' ),
425                                 'readonly'      => false,
426                                 'option'        => 'blogdescription'
427                         ),
428                         'date_format'       => array(
429                                 'desc'          => __( 'Date Format' ),
430                                 'readonly'      => false,
431                                 'option'        => 'date_format'
432                         ),
433                         'time_format'       => array(
434                                 'desc'          => __( 'Time Format' ),
435                                 'readonly'      => false,
436                                 'option'        => 'time_format'
437                         ),
438                         'users_can_register' => array(
439                                 'desc'          => __( 'Allow new users to sign up' ),
440                                 'readonly'      => false,
441                                 'option'        => 'users_can_register'
442                         ),
443                         'thumbnail_size_w'  => array(
444                                 'desc'          => __( 'Thumbnail Width' ),
445                                 'readonly'      => false,
446                                 'option'        => 'thumbnail_size_w'
447                         ),
448                         'thumbnail_size_h'  => array(
449                                 'desc'          => __( 'Thumbnail Height' ),
450                                 'readonly'      => false,
451                                 'option'        => 'thumbnail_size_h'
452                         ),
453                         'thumbnail_crop'    => array(
454                                 'desc'          => __( 'Crop thumbnail to exact dimensions' ),
455                                 'readonly'      => false,
456                                 'option'        => 'thumbnail_crop'
457                         ),
458                         'medium_size_w'     => array(
459                                 'desc'          => __( 'Medium size image width' ),
460                                 'readonly'      => false,
461                                 'option'        => 'medium_size_w'
462                         ),
463                         'medium_size_h'     => array(
464                                 'desc'          => __( 'Medium size image height' ),
465                                 'readonly'      => false,
466                                 'option'        => 'medium_size_h'
467                         ),
468                         'large_size_w'      => array(
469                                 'desc'          => __( 'Large size image width' ),
470                                 'readonly'      => false,
471                                 'option'        => 'large_size_w'
472                         ),
473                         'large_size_h'      => array(
474                                 'desc'          => __( 'Large size image height' ),
475                                 'readonly'      => false,
476                                 'option'        => 'large_size_h'
477                         ),
478                         'default_comment_status' => array(
479                                 'desc'          => __( 'Allow people to post comments on new articles' ),
480                                 'readonly'      => false,
481                                 'option'        => 'default_comment_status'
482                         ),
483                         'default_ping_status' => array(
484                                 'desc'          => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
485                                 'readonly'      => false,
486                                 'option'        => 'default_ping_status'
487                         )
488                 );
489
490                 /**
491                  * Filter the XML-RPC blog options property.
492                  *
493                  * @since 2.6.0
494                  *
495                  * @param array $blog_options An array of XML-RPC blog options.
496                  */
497                 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
498         }
499
500         /**
501          * Retrieve the blogs of the user.
502          *
503          * @since 2.6.0
504          *
505          * @param array $args Method parameters. Contains:
506          *  - username
507          *  - password
508          * @return array. Contains:
509          *  - 'isAdmin'
510          *  - 'url'
511          *  - 'blogid'
512          *  - 'blogName'
513          *  - 'xmlrpc' - url of xmlrpc endpoint
514          */
515         public function wp_getUsersBlogs( $args ) {
516                 // If this isn't on WPMU then just use blogger_getUsersBlogs
517                 if ( !is_multisite() ) {
518                         array_unshift( $args, 1 );
519                         return $this->blogger_getUsersBlogs( $args );
520                 }
521
522                 $this->escape( $args );
523
524                 $username = $args[0];
525                 $password = $args[1];
526
527                 if ( !$user = $this->login($username, $password) )
528                         return $this->error;
529
530                 /**
531                  * Fires after the XML-RPC user has been authenticated but before the rest of
532                  * the method logic begins.
533                  *
534                  * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
535                  * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
536                  *
537                  * @since 2.5.0
538                  *
539                  * @param method $name The method name.
540                  */
541                 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
542
543                 $blogs = (array) get_blogs_of_user( $user->ID );
544                 $struct = array();
545
546                 foreach ( $blogs as $blog ) {
547                         // Don't include blogs that aren't hosted at this site
548                         if ( $blog->site_id != get_current_site()->id )
549                                 continue;
550
551                         $blog_id = $blog->userblog_id;
552
553                         switch_to_blog( $blog_id );
554
555                         $is_admin = current_user_can( 'manage_options' );
556
557                         $struct[] = array(
558                                 'isAdmin'               => $is_admin,
559                                 'url'                   => home_url( '/' ),
560                                 'blogid'                => (string) $blog_id,
561                                 'blogName'              => get_option( 'blogname' ),
562                                 'xmlrpc'                => site_url( 'xmlrpc.php', 'rpc' ),
563                         );
564
565                         restore_current_blog();
566                 }
567
568                 return $struct;
569         }
570
571         /**
572          * Checks if the method received at least the minimum number of arguments.
573          *
574          * @since 3.4.0
575          *
576          * @param string|array $args Sanitize single string or array of strings.
577          * @param int $count Minimum number of arguments.
578          * @return boolean if $args contains at least $count arguments.
579          */
580         protected function minimum_args( $args, $count ) {
581                 if ( count( $args ) < $count ) {
582                         $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
583                         return false;
584                 }
585
586                 return true;
587         }
588
589         /**
590          * Prepares taxonomy data for return in an XML-RPC object.
591          *
592          * @access protected
593          *
594          * @param object $taxonomy The unprepared taxonomy data
595          * @param array $fields The subset of taxonomy fields to return
596          * @return array The prepared taxonomy data
597          */
598         protected function _prepare_taxonomy( $taxonomy, $fields ) {
599                 $_taxonomy = array(
600                         'name' => $taxonomy->name,
601                         'label' => $taxonomy->label,
602                         'hierarchical' => (bool) $taxonomy->hierarchical,
603                         'public' => (bool) $taxonomy->public,
604                         'show_ui' => (bool) $taxonomy->show_ui,
605                         '_builtin' => (bool) $taxonomy->_builtin,
606                 );
607
608                 if ( in_array( 'labels', $fields ) )
609                         $_taxonomy['labels'] = (array) $taxonomy->labels;
610
611                 if ( in_array( 'cap', $fields ) )
612                         $_taxonomy['cap'] = (array) $taxonomy->cap;
613
614                 if ( in_array( 'menu', $fields ) )
615                         $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
616
617                 if ( in_array( 'object_type', $fields ) )
618                         $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
619
620                 /**
621                  * Filter XML-RPC-prepared data for the given taxonomy.
622                  *
623                  * @since 3.4.0
624                  *
625                  * @param array  $_taxonomy An array of taxonomy data.
626                  * @param object $taxonomy  Taxonomy object.
627                  * @param array  $fields    The subset of taxonomy fields to return.
628                  */
629                 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
630         }
631
632         /**
633          * Prepares term data for return in an XML-RPC object.
634          *
635          * @access protected
636          *
637          * @param array|object $term The unprepared term data
638          * @return array The prepared term data
639          */
640         protected function _prepare_term( $term ) {
641                 $_term = $term;
642                 if ( ! is_array( $_term) )
643                         $_term = get_object_vars( $_term );
644
645                 // For integers which may be larger than XML-RPC supports ensure we return strings.
646                 $_term['term_id'] = strval( $_term['term_id'] );
647                 $_term['term_group'] = strval( $_term['term_group'] );
648                 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
649                 $_term['parent'] = strval( $_term['parent'] );
650
651                 // Count we are happy to return as an integer because people really shouldn't use terms that much.
652                 $_term['count'] = intval( $_term['count'] );
653
654                 /**
655                  * Filter XML-RPC-prepared data for the given term.
656                  *
657                  * @since 3.4.0
658                  *
659                  * @param array        $_term An array of term data.
660                  * @param array|object $term  Term object or array.
661                  */
662                 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
663         }
664
665         /**
666          * Convert a WordPress date string to an IXR_Date object.
667          *
668          * @access protected
669          *
670          * @param string $date
671          * @return IXR_Date
672          */
673         protected function _convert_date( $date ) {
674                 if ( $date === '0000-00-00 00:00:00' ) {
675                         return new IXR_Date( '00000000T00:00:00Z' );
676                 }
677                 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
678         }
679
680         /**
681          * Convert a WordPress GMT date string to an IXR_Date object.
682          *
683          * @access protected
684          *
685          * @param string $date_gmt
686          * @param string $date
687          * @return IXR_Date
688          */
689         protected function _convert_date_gmt( $date_gmt, $date ) {
690                 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
691                         return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
692                 }
693                 return $this->_convert_date( $date_gmt );
694         }
695
696         /**
697          * Prepares post data for return in an XML-RPC object.
698          *
699          * @access protected
700          *
701          * @param array $post The unprepared post data
702          * @param array $fields The subset of post type fields to return
703          * @return array The prepared post data
704          */
705         protected function _prepare_post( $post, $fields ) {
706                 // holds the data for this post. built up based on $fields
707                 $_post = array( 'post_id' => strval( $post['ID'] ) );
708
709                 // prepare common post fields
710                 $post_fields = array(
711                         'post_title'        => $post['post_title'],
712                         'post_date'         => $this->_convert_date( $post['post_date'] ),
713                         'post_date_gmt'     => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
714                         'post_modified'     => $this->_convert_date( $post['post_modified'] ),
715                         'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
716                         'post_status'       => $post['post_status'],
717                         'post_type'         => $post['post_type'],
718                         'post_name'         => $post['post_name'],
719                         'post_author'       => $post['post_author'],
720                         'post_password'     => $post['post_password'],
721                         'post_excerpt'      => $post['post_excerpt'],
722                         'post_content'      => $post['post_content'],
723                         'post_parent'       => strval( $post['post_parent'] ),
724                         'post_mime_type'    => $post['post_mime_type'],
725                         'link'              => post_permalink( $post['ID'] ),
726                         'guid'              => $post['guid'],
727                         'menu_order'        => intval( $post['menu_order'] ),
728                         'comment_status'    => $post['comment_status'],
729                         'ping_status'       => $post['ping_status'],
730                         'sticky'            => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
731                 );
732
733                 // Thumbnail
734                 $post_fields['post_thumbnail'] = array();
735                 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
736                 if ( $thumbnail_id ) {
737                         $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
738                         $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
739                 }
740
741                 // Consider future posts as published
742                 if ( $post_fields['post_status'] === 'future' )
743                         $post_fields['post_status'] = 'publish';
744
745                 // Fill in blank post format
746                 $post_fields['post_format'] = get_post_format( $post['ID'] );
747                 if ( empty( $post_fields['post_format'] ) )
748                         $post_fields['post_format'] = 'standard';
749
750                 // Merge requested $post_fields fields into $_post
751                 if ( in_array( 'post', $fields ) ) {
752                         $_post = array_merge( $_post, $post_fields );
753                 } else {
754                         $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
755                         $_post = array_merge( $_post, $requested_fields );
756                 }
757
758                 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
759
760                 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
761                         $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
762                         $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
763                         $_post['terms'] = array();
764                         foreach ( $terms as $term ) {
765                                 $_post['terms'][] = $this->_prepare_term( $term );
766                         }
767                 }
768
769                 if ( in_array( 'custom_fields', $fields ) )
770                         $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
771
772                 if ( in_array( 'enclosure', $fields ) ) {
773                         $_post['enclosure'] = array();
774                         $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
775                         if ( ! empty( $enclosures ) ) {
776                                 $encdata = explode( "\n", $enclosures[0] );
777                                 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
778                                 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
779                                 $_post['enclosure']['type'] = trim( $encdata[2] );
780                         }
781                 }
782
783                 /**
784                  * Filter XML-RPC-prepared date for the given post.
785                  *
786                  * @since 3.4.0
787                  *
788                  * @param array $_post  An array of modified post data.
789                  * @param array $post   An array of post data.
790                  * @param array $fields An array of post fields.
791                  */
792                 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
793         }
794
795         /**
796          * Prepares post data for return in an XML-RPC object.
797          *
798          * @access protected
799          *
800          * @param object $post_type Post type object
801          * @param array $fields The subset of post fields to return
802          * @return array The prepared post type data
803          */
804         protected function _prepare_post_type( $post_type, $fields ) {
805                 $_post_type = array(
806                         'name' => $post_type->name,
807                         'label' => $post_type->label,
808                         'hierarchical' => (bool) $post_type->hierarchical,
809                         'public' => (bool) $post_type->public,
810                         'show_ui' => (bool) $post_type->show_ui,
811                         '_builtin' => (bool) $post_type->_builtin,
812                         'has_archive' => (bool) $post_type->has_archive,
813                         'supports' => get_all_post_type_supports( $post_type->name ),
814                 );
815
816                 if ( in_array( 'labels', $fields ) ) {
817                         $_post_type['labels'] = (array) $post_type->labels;
818                 }
819
820                 if ( in_array( 'cap', $fields ) ) {
821                         $_post_type['cap'] = (array) $post_type->cap;
822                         $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
823                 }
824
825                 if ( in_array( 'menu', $fields ) ) {
826                         $_post_type['menu_position'] = (int) $post_type->menu_position;
827                         $_post_type['menu_icon'] = $post_type->menu_icon;
828                         $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
829                 }
830
831                 if ( in_array( 'taxonomies', $fields ) )
832                         $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
833
834                 /**
835                  * Filter XML-RPC-prepared date for the given post type.
836                  *
837                  * @since 3.4.0
838                  *
839                  * @param array  $_post_type An array of post type data.
840                  * @param object $post_type  Post type object.
841                  */
842                 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
843         }
844
845         /**
846          * Prepares media item data for return in an XML-RPC object.
847          *
848          * @access protected
849          *
850          * @param object $media_item The unprepared media item data
851          * @param string $thumbnail_size The image size to use for the thumbnail URL
852          * @return array The prepared media item data
853          */
854         protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
855                 $_media_item = array(
856                         'attachment_id'    => strval( $media_item->ID ),
857                         'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
858                         'parent'           => $media_item->post_parent,
859                         'link'             => wp_get_attachment_url( $media_item->ID ),
860                         'title'            => $media_item->post_title,
861                         'caption'          => $media_item->post_excerpt,
862                         'description'      => $media_item->post_content,
863                         'metadata'         => wp_get_attachment_metadata( $media_item->ID ),
864                 );
865
866                 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
867                 if ( $thumbnail_src )
868                         $_media_item['thumbnail'] = $thumbnail_src[0];
869                 else
870                         $_media_item['thumbnail'] = $_media_item['link'];
871
872                 /**
873                  * Filter XML-RPC-prepared data for the given media item.
874                  *
875                  * @since 3.4.0
876                  *
877                  * @param array  $_media_item    An array of media item data.
878                  * @param object $media_item     Media item object.
879                  * @param string $thumbnail_size Image size.
880                  */
881                 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
882         }
883
884         /**
885          * Prepares page data for return in an XML-RPC object.
886          *
887          * @access protected
888          *
889          * @param object $page The unprepared page data
890          * @return array The prepared page data
891          */
892         protected function _prepare_page( $page ) {
893                 // Get all of the page content and link.
894                 $full_page = get_extended( $page->post_content );
895                 $link = post_permalink( $page->ID );
896
897                 // Get info the page parent if there is one.
898                 $parent_title = "";
899                 if ( ! empty( $page->post_parent ) ) {
900                         $parent = get_post( $page->post_parent );
901                         $parent_title = $parent->post_title;
902                 }
903
904                 // Determine comment and ping settings.
905                 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
906                 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
907
908                 // Format page date.
909                 $page_date = $this->_convert_date( $page->post_date );
910                 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
911
912                 // Pull the categories info together.
913                 $categories = array();
914                 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
915                         $categories[] = get_cat_name( $cat_id );
916                 }
917
918                 // Get the author info.
919                 $author = get_userdata( $page->post_author );
920
921                 $page_template = get_page_template_slug( $page->ID );
922                 if ( empty( $page_template ) )
923                         $page_template = 'default';
924
925                 $_page = array(
926                         'dateCreated'            => $page_date,
927                         'userid'                 => $page->post_author,
928                         'page_id'                => $page->ID,
929                         'page_status'            => $page->post_status,
930                         'description'            => $full_page['main'],
931                         'title'                  => $page->post_title,
932                         'link'                   => $link,
933                         'permaLink'              => $link,
934                         'categories'             => $categories,
935                         'excerpt'                => $page->post_excerpt,
936                         'text_more'              => $full_page['extended'],
937                         'mt_allow_comments'      => $allow_comments,
938                         'mt_allow_pings'         => $allow_pings,
939                         'wp_slug'                => $page->post_name,
940                         'wp_password'            => $page->post_password,
941                         'wp_author'              => $author->display_name,
942                         'wp_page_parent_id'      => $page->post_parent,
943                         'wp_page_parent_title'   => $parent_title,
944                         'wp_page_order'          => $page->menu_order,
945                         'wp_author_id'           => (string) $author->ID,
946                         'wp_author_display_name' => $author->display_name,
947                         'date_created_gmt'       => $page_date_gmt,
948                         'custom_fields'          => $this->get_custom_fields( $page->ID ),
949                         'wp_page_template'       => $page_template
950                 );
951
952                 /**
953                  * Filter XML-RPC-prepared data for the given page.
954                  *
955                  * @since 3.4.0
956                  *
957                  * @param array   $_page An array of page data.
958                  * @param WP_Post $page  Page object.
959                  */
960                 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
961         }
962
963         /**
964          * Prepares comment data for return in an XML-RPC object.
965          *
966          * @access protected
967          *
968          * @param object $comment The unprepared comment data
969          * @return array The prepared comment data
970          */
971         protected function _prepare_comment( $comment ) {
972                 // Format page date.
973                 $comment_date = $this->_convert_date( $comment->comment_date );
974                 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
975
976                 if ( '0' == $comment->comment_approved )
977                         $comment_status = 'hold';
978                 else if ( 'spam' == $comment->comment_approved )
979                         $comment_status = 'spam';
980                 else if ( '1' == $comment->comment_approved )
981                         $comment_status = 'approve';
982                 else
983                         $comment_status = $comment->comment_approved;
984
985                 $_comment = array(
986                         'date_created_gmt' => $comment_date_gmt,
987                         'user_id'          => $comment->user_id,
988                         'comment_id'       => $comment->comment_ID,
989                         'parent'           => $comment->comment_parent,
990                         'status'           => $comment_status,
991                         'content'          => $comment->comment_content,
992                         'link'             => get_comment_link($comment),
993                         'post_id'          => $comment->comment_post_ID,
994                         'post_title'       => get_the_title($comment->comment_post_ID),
995                         'author'           => $comment->comment_author,
996                         'author_url'       => $comment->comment_author_url,
997                         'author_email'     => $comment->comment_author_email,
998                         'author_ip'        => $comment->comment_author_IP,
999                         'type'             => $comment->comment_type,
1000                 );
1001
1002                 /**
1003                  * Filter XML-RPC-prepared data for the given comment.
1004                  *
1005                  * @since 3.4.0
1006                  *
1007                  * @param array  $_comment An array of prepared comment data.
1008                  * @param object $comment  Comment object.
1009                  */
1010                 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
1011         }
1012
1013         /**
1014          * Prepares user data for return in an XML-RPC object.
1015          *
1016          * @access protected
1017          *
1018          * @param WP_User $user The unprepared user object
1019          * @param array $fields The subset of user fields to return
1020          * @return array The prepared user data
1021          */
1022         protected function _prepare_user( $user, $fields ) {
1023                 $_user = array( 'user_id' => strval( $user->ID ) );
1024
1025                 $user_fields = array(
1026                         'username'          => $user->user_login,
1027                         'first_name'        => $user->user_firstname,
1028                         'last_name'         => $user->user_lastname,
1029                         'registered'        => $this->_convert_date( $user->user_registered ),
1030                         'bio'               => $user->user_description,
1031                         'email'             => $user->user_email,
1032                         'nickname'          => $user->nickname,
1033                         'nicename'          => $user->user_nicename,
1034                         'url'               => $user->user_url,
1035                         'display_name'      => $user->display_name,
1036                         'roles'             => $user->roles,
1037                 );
1038
1039                 if ( in_array( 'all', $fields ) ) {
1040                         $_user = array_merge( $_user, $user_fields );
1041                 } else {
1042                         if ( in_array( 'basic', $fields ) ) {
1043                                 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
1044                                 $fields = array_merge( $fields, $basic_fields );
1045                         }
1046                         $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
1047                         $_user = array_merge( $_user, $requested_fields );
1048                 }
1049
1050                 /**
1051                  * Filter XML-RPC-prepared data for the given user.
1052                  *
1053                  * @since 3.5.0
1054                  *
1055                  * @param array   $_user  An array of user data.
1056                  * @param WP_User $user   User object.
1057                  * @param array   $fields An array of user fields.
1058                  */
1059                 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
1060         }
1061
1062         /**
1063          * Create a new post for any registered post type.
1064          *
1065          * @since 3.4.0
1066          *
1067          * @param array $args Method parameters. Contains:
1068          *  - int     $blog_id
1069          *  - string  $username
1070          *  - string  $password
1071          *  - array   $content_struct
1072          *      $content_struct can contain:
1073          *      - post_type (default: 'post')
1074          *      - post_status (default: 'draft')
1075          *      - post_title
1076          *      - post_author
1077          *      - post_excerpt
1078          *      - post_content
1079          *      - post_date_gmt | post_date
1080          *      - post_format
1081          *      - post_password
1082          *      - comment_status - can be 'open' | 'closed'
1083          *      - ping_status - can be 'open' | 'closed'
1084          *      - sticky
1085          *      - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
1086          *      - custom_fields - array, with each element containing 'key' and 'value'
1087          *      - terms - array, with taxonomy names as keys and arrays of term IDs as values
1088          *      - terms_names - array, with taxonomy names as keys and arrays of term names as values
1089          *      - enclosure
1090          *      - any other fields supported by wp_insert_post()
1091          * @return string post_id
1092          */
1093         public function wp_newPost( $args ) {
1094                 if ( ! $this->minimum_args( $args, 4 ) )
1095                         return $this->error;
1096
1097                 $this->escape( $args );
1098
1099                 $blog_id        = (int) $args[0];
1100                 $username       = $args[1];
1101                 $password       = $args[2];
1102                 $content_struct = $args[3];
1103
1104                 if ( ! $user = $this->login( $username, $password ) )
1105                         return $this->error;
1106
1107                 // convert the date field back to IXR form
1108                 if ( isset( $content_struct['post_date'] ) && ! is_a( $content_struct['post_date'], 'IXR_Date' ) ) {
1109                         $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
1110                 }
1111
1112                 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1113                 // since _insert_post will ignore the non-GMT date if the GMT date is set
1114                 if ( isset( $content_struct['post_date_gmt'] ) && ! is_a( $content_struct['post_date_gmt'], 'IXR_Date' ) ) {
1115                         if ( $content_struct['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) {
1116                                 unset( $content_struct['post_date_gmt'] );
1117                         } else {
1118                                 $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
1119                         }
1120                 }
1121
1122                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1123                 do_action( 'xmlrpc_call', 'wp.newPost' );
1124
1125                 unset( $content_struct['ID'] );
1126
1127                 return $this->_insert_post( $user, $content_struct );
1128         }
1129
1130         /**
1131          * Helper method for filtering out elements from an array.
1132          *
1133          * @since 3.4.0
1134          *
1135          * @param int $count Number to compare to one.
1136          */
1137         private function _is_greater_than_one( $count ) {
1138                 return $count > 1;
1139         }
1140
1141         /**
1142          * Helper method for wp_newPost and wp_editPost, containing shared logic.
1143          *
1144          * @since 3.4.0
1145          * @uses wp_insert_post()
1146          *
1147          * @param WP_User $user The post author if post_author isn't set in $content_struct.
1148          * @param array $content_struct Post data to insert.
1149          */
1150         protected function _insert_post( $user, $content_struct ) {
1151                 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1152                         'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1153
1154                 $post_data = wp_parse_args( $content_struct, $defaults );
1155
1156                 $post_type = get_post_type_object( $post_data['post_type'] );
1157                 if ( ! $post_type )
1158                         return new IXR_Error( 403, __( 'Invalid post type' ) );
1159
1160                 $update = ! empty( $post_data['ID'] );
1161
1162                 if ( $update ) {
1163                         if ( ! get_post( $post_data['ID'] ) )
1164                                 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1165                         if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1166                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1167                         if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1168                                 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1169                 } else {
1170                         if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1171                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1172                 }
1173
1174                 switch ( $post_data['post_status'] ) {
1175                         case 'draft':
1176                         case 'pending':
1177                                 break;
1178                         case 'private':
1179                                 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1180                                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1181                                 break;
1182                         case 'publish':
1183                         case 'future':
1184                                 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1185                                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1186                                 break;
1187                         default:
1188                                 if ( ! get_post_status_object( $post_data['post_status'] ) )
1189                                         $post_data['post_status'] = 'draft';
1190                         break;
1191                 }
1192
1193                 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1194                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1195
1196                 $post_data['post_author'] = absint( $post_data['post_author'] );
1197                 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1198                         if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1199                                 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1200
1201                         $author = get_userdata( $post_data['post_author'] );
1202
1203                         if ( ! $author )
1204                                 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1205                 } else {
1206                         $post_data['post_author'] = $user->ID;
1207                 }
1208
1209                 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1210                         unset( $post_data['comment_status'] );
1211
1212                 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1213                         unset( $post_data['ping_status'] );
1214
1215                 // Do some timestamp voodoo
1216                 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1217                         // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1218                         $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1219                 } elseif ( ! empty( $post_data['post_date'] ) ) {
1220                         $dateCreated = $post_data['post_date']->getIso();
1221                 }
1222
1223                 if ( ! empty( $dateCreated ) ) {
1224                         $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1225                         $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1226                 }
1227
1228                 if ( ! isset( $post_data['ID'] ) )
1229                         $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1230                 $post_ID = $post_data['ID'];
1231
1232                 if ( $post_data['post_type'] == 'post' ) {
1233                         // Private and password-protected posts cannot be stickied.
1234                         if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
1235                                 // Error if the client tried to stick the post, otherwise, silently unstick.
1236                                 if ( ! empty( $post_data['sticky'] ) )
1237                                         return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1238                                 if ( $update )
1239                                         unstick_post( $post_ID );
1240                         } elseif ( isset( $post_data['sticky'] ) )  {
1241                                 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1242                                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1243                                 if ( $post_data['sticky'] )
1244                                         stick_post( $post_ID );
1245                                 else
1246                                         unstick_post( $post_ID );
1247                         }
1248                 }
1249
1250                 if ( isset( $post_data['post_thumbnail'] ) ) {
1251                         // empty value deletes, non-empty value adds/updates
1252                         if ( ! $post_data['post_thumbnail'] )
1253                                 delete_post_thumbnail( $post_ID );
1254                         elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1255                                 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1256                         set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1257                         unset( $content_struct['post_thumbnail'] );
1258                 }
1259
1260                 if ( isset( $post_data['custom_fields'] ) )
1261                         $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1262
1263                 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1264                         $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1265
1266                         // accumulate term IDs from terms and terms_names
1267                         $terms = array();
1268
1269                         // first validate the terms specified by ID
1270                         if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1271                                 $taxonomies = array_keys( $post_data['terms'] );
1272
1273                                 // validating term ids
1274                                 foreach ( $taxonomies as $taxonomy ) {
1275                                         if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1276                                                 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1277
1278                                         if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1279                                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1280
1281                                         $term_ids = $post_data['terms'][$taxonomy];
1282                                         $terms[ $taxonomy ] = array();
1283                                         foreach ( $term_ids as $term_id ) {
1284                                                 $term = get_term_by( 'id', $term_id, $taxonomy );
1285
1286                                                 if ( ! $term )
1287                                                         return new IXR_Error( 403, __( 'Invalid term ID' ) );
1288
1289                                                 $terms[$taxonomy][] = (int) $term_id;
1290                                         }
1291                                 }
1292                         }
1293
1294                         // now validate terms specified by name
1295                         if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1296                                 $taxonomies = array_keys( $post_data['terms_names'] );
1297
1298                                 foreach ( $taxonomies as $taxonomy ) {
1299                                         if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1300                                                 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1301
1302                                         if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1303                                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1304
1305                                         // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
1306                                         $ambiguous_terms = array();
1307                                         if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1308                                                 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1309
1310                                                 // count the number of terms with the same name
1311                                                 $tax_term_names_count = array_count_values( $tax_term_names );
1312
1313                                                 // filter out non-ambiguous term names
1314                                                 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1315
1316                                                 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1317                                         }
1318
1319                                         $term_names = $post_data['terms_names'][$taxonomy];
1320                                         foreach ( $term_names as $term_name ) {
1321                                                 if ( in_array( $term_name, $ambiguous_terms ) )
1322                                                         return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1323
1324                                                 $term = get_term_by( 'name', $term_name, $taxonomy );
1325
1326                                                 if ( ! $term ) {
1327                                                         // term doesn't exist, so check that the user is allowed to create new terms
1328                                                         if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1329                                                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1330
1331                                                         // create the new term
1332                                                         $term_info = wp_insert_term( $term_name, $taxonomy );
1333                                                         if ( is_wp_error( $term_info ) )
1334                                                                 return new IXR_Error( 500, $term_info->get_error_message() );
1335
1336                                                         $terms[$taxonomy][] = (int) $term_info['term_id'];
1337                                                 } else {
1338                                                         $terms[$taxonomy][] = (int) $term->term_id;
1339                                                 }
1340                                         }
1341                                 }
1342                         }
1343
1344                         $post_data['tax_input'] = $terms;
1345                         unset( $post_data['terms'], $post_data['terms_names'] );
1346                 } else {
1347                         // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
1348                         unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1349                 }
1350
1351                 if ( isset( $post_data['post_format'] ) ) {
1352                         $format = set_post_format( $post_ID, $post_data['post_format'] );
1353
1354                         if ( is_wp_error( $format ) )
1355                                 return new IXR_Error( 500, $format->get_error_message() );
1356
1357                         unset( $post_data['post_format'] );
1358                 }
1359
1360                 // Handle enclosures
1361                 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1362                 $this->add_enclosure_if_new( $post_ID, $enclosure );
1363
1364                 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1365
1366                 /**
1367                  * Filter post data array to be inserted via XML-RPC.
1368                  *
1369                  * @since 3.4.0
1370                  *
1371                  * @param array $post_data      Parsed array of post data.
1372                  * @param array $content_struct Post data array.
1373                  */
1374                 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1375
1376                 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1377                 if ( is_wp_error( $post_ID ) )
1378                         return new IXR_Error( 500, $post_ID->get_error_message() );
1379
1380                 if ( ! $post_ID )
1381                         return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1382
1383                 return strval( $post_ID );
1384         }
1385
1386         /**
1387          * Edit a post for any registered post type.
1388          *
1389          * The $content_struct parameter only needs to contain fields that
1390          * should be changed. All other fields will retain their existing values.
1391          *
1392          * @since 3.4.0
1393          *
1394          * @param array $args Method parameters. Contains:
1395          *  - int     $blog_id
1396          *  - string  $username
1397          *  - string  $password
1398          *  - int     $post_id
1399          *  - array   $content_struct
1400          * @return true on success
1401          */
1402         public function wp_editPost( $args ) {
1403                 if ( ! $this->minimum_args( $args, 5 ) )
1404                         return $this->error;
1405
1406                 $this->escape( $args );
1407
1408                 $blog_id        = (int) $args[0];
1409                 $username       = $args[1];
1410                 $password       = $args[2];
1411                 $post_id        = (int) $args[3];
1412                 $content_struct = $args[4];
1413
1414                 if ( ! $user = $this->login( $username, $password ) )
1415                         return $this->error;
1416
1417                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1418                 do_action( 'xmlrpc_call', 'wp.editPost' );
1419
1420                 $post = get_post( $post_id, ARRAY_A );
1421
1422                 if ( empty( $post['ID'] ) )
1423                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1424
1425                 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1426                         // If the post has been modified since the date provided, return an error.
1427                         if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1428                                 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1429                         }
1430                 }
1431
1432                 // convert the date field back to IXR form
1433                 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1434
1435                 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1436                 // since _insert_post will ignore the non-GMT date if the GMT date is set
1437                 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1438                         unset( $post['post_date_gmt'] );
1439                 else
1440                         $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1441
1442                 $this->escape( $post );
1443                 $merged_content_struct = array_merge( $post, $content_struct );
1444
1445                 $retval = $this->_insert_post( $user, $merged_content_struct );
1446                 if ( $retval instanceof IXR_Error )
1447                         return $retval;
1448
1449                 return true;
1450         }
1451
1452         /**
1453          * Delete a post for any registered post type.
1454          *
1455          * @since 3.4.0
1456          *
1457          * @uses wp_delete_post()
1458          * @param array $args Method parameters. Contains:
1459          *  - int     $blog_id
1460          *  - string  $username
1461          *  - string  $password
1462          *  - int     $post_id
1463          * @return true on success
1464          */
1465         public function wp_deletePost( $args ) {
1466                 if ( ! $this->minimum_args( $args, 4 ) )
1467                         return $this->error;
1468
1469                 $this->escape( $args );
1470
1471                 $blog_id    = (int) $args[0];
1472                 $username   = $args[1];
1473                 $password   = $args[2];
1474                 $post_id    = (int) $args[3];
1475
1476                 if ( ! $user = $this->login( $username, $password ) )
1477                         return $this->error;
1478
1479                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1480                 do_action( 'xmlrpc_call', 'wp.deletePost' );
1481
1482                 $post = get_post( $post_id, ARRAY_A );
1483                 if ( empty( $post['ID'] ) )
1484                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1485
1486                 if ( ! current_user_can( 'delete_post', $post_id ) )
1487                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1488
1489                 $result = wp_delete_post( $post_id );
1490
1491                 if ( ! $result )
1492                         return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1493
1494                 return true;
1495         }
1496
1497         /**
1498          * Retrieve a post.
1499          *
1500          * @since 3.4.0
1501          *
1502          * The optional $fields parameter specifies what fields will be included
1503          * in the response array. This should be a list of field names. 'post_id' will
1504          * always be included in the response regardless of the value of $fields.
1505          *
1506          * Instead of, or in addition to, individual field names, conceptual group
1507          * names can be used to specify multiple fields. The available conceptual
1508          * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1509          * and 'enclosure'.
1510          *
1511          * @uses get_post()
1512          * @param array $args Method parameters. Contains:
1513          *  - int     $post_id
1514          *  - string  $username
1515          *  - string  $password
1516          *  - array   $fields optional
1517          * @return array contains (based on $fields parameter):
1518          *  - 'post_id'
1519          *  - 'post_title'
1520          *  - 'post_date'
1521          *  - 'post_date_gmt'
1522          *  - 'post_modified'
1523          *  - 'post_modified_gmt'
1524          *  - 'post_status'
1525          *  - 'post_type'
1526          *  - 'post_name'
1527          *  - 'post_author'
1528          *  - 'post_password'
1529          *  - 'post_excerpt'
1530          *  - 'post_content'
1531          *  - 'link'
1532          *  - 'comment_status'
1533          *  - 'ping_status'
1534          *  - 'sticky'
1535          *  - 'custom_fields'
1536          *  - 'terms'
1537          *  - 'categories'
1538          *  - 'tags'
1539          *  - 'enclosure'
1540          */
1541         public function wp_getPost( $args ) {
1542                 if ( ! $this->minimum_args( $args, 4 ) )
1543                         return $this->error;
1544
1545                 $this->escape( $args );
1546
1547                 $blog_id            = (int) $args[0];
1548                 $username           = $args[1];
1549                 $password           = $args[2];
1550                 $post_id            = (int) $args[3];
1551
1552                 if ( isset( $args[4] ) ) {
1553                         $fields = $args[4];
1554                 } else {
1555                         /**
1556                          * Filter the list of post query fields used by the given XML-RPC method.
1557                          *
1558                          * @since 3.4.0
1559                          *
1560                          * @param array $fields  Array of post fields.
1561                          * @param string $method Method name.
1562                          */
1563                         $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1564                 }
1565
1566                 if ( ! $user = $this->login( $username, $password ) )
1567                         return $this->error;
1568
1569                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1570                 do_action( 'xmlrpc_call', 'wp.getPost' );
1571
1572                 $post = get_post( $post_id, ARRAY_A );
1573
1574                 if ( empty( $post['ID'] ) )
1575                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1576
1577                 if ( ! current_user_can( 'edit_post', $post_id ) )
1578                         return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1579
1580                 return $this->_prepare_post( $post, $fields );
1581         }
1582
1583         /**
1584          * Retrieve posts.
1585          *
1586          * @since 3.4.0
1587          *
1588          * The optional $filter parameter modifies the query used to retrieve posts.
1589          * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
1590          * 'orderby', and 'order'.
1591          *
1592          * The optional $fields parameter specifies what fields will be included
1593          * in the response array.
1594          *
1595          * @uses wp_get_recent_posts()
1596          * @see wp_getPost() for more on $fields
1597          * @see get_posts() for more on $filter values
1598          *
1599          * @param array $args Method parameters. Contains:
1600          *  - int     $blog_id
1601          *  - string  $username
1602          *  - string  $password
1603          *  - array   $filter optional
1604          *  - array   $fields optional
1605          * @return array contains a collection of posts.
1606          */
1607         public function wp_getPosts( $args ) {
1608                 if ( ! $this->minimum_args( $args, 3 ) )
1609                         return $this->error;
1610
1611                 $this->escape( $args );
1612
1613                 $blog_id    = (int) $args[0];
1614                 $username   = $args[1];
1615                 $password   = $args[2];
1616                 $filter     = isset( $args[3] ) ? $args[3] : array();
1617
1618                 if ( isset( $args[4] ) ) {
1619                         $fields = $args[4];
1620                 } else {
1621                         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1622                         $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1623                 }
1624
1625                 if ( ! $user = $this->login( $username, $password ) )
1626                         return $this->error;
1627
1628                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1629                 do_action( 'xmlrpc_call', 'wp.getPosts' );
1630
1631                 $query = array();
1632
1633                 if ( isset( $filter['post_type'] ) ) {
1634                         $post_type = get_post_type_object( $filter['post_type'] );
1635                         if ( ! ( (bool) $post_type ) )
1636                                 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1637                 } else {
1638                         $post_type = get_post_type_object( 'post' );
1639                 }
1640
1641                 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1642                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1643
1644                 $query['post_type'] = $post_type->name;
1645
1646                 if ( isset( $filter['post_status'] ) )
1647                         $query['post_status'] = $filter['post_status'];
1648
1649                 if ( isset( $filter['number'] ) )
1650                         $query['numberposts'] = absint( $filter['number'] );
1651
1652                 if ( isset( $filter['offset'] ) )
1653                         $query['offset'] = absint( $filter['offset'] );
1654
1655                 if ( isset( $filter['orderby'] ) ) {
1656                         $query['orderby'] = $filter['orderby'];
1657
1658                         if ( isset( $filter['order'] ) )
1659                                 $query['order'] = $filter['order'];
1660                 }
1661
1662                 if ( isset( $filter['s'] ) ) {
1663                         $query['s'] = $filter['s'];
1664                 }
1665
1666                 $posts_list = wp_get_recent_posts( $query );
1667
1668                 if ( ! $posts_list )
1669                         return array();
1670
1671                 // holds all the posts data
1672                 $struct = array();
1673
1674                 foreach ( $posts_list as $post ) {
1675                         if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1676                                 continue;
1677
1678                         $struct[] = $this->_prepare_post( $post, $fields );
1679                 }
1680
1681                 return $struct;
1682         }
1683
1684         /**
1685          * Create a new term.
1686          *
1687          * @since 3.4.0
1688          *
1689          * @uses wp_insert_term()
1690          * @param array $args Method parameters. Contains:
1691          *  - int     $blog_id
1692          *  - string  $username
1693          *  - string  $password
1694          *  - array   $content_struct
1695          *      The $content_struct must contain:
1696          *      - 'name'
1697          *      - 'taxonomy'
1698          *      Also, it can optionally contain:
1699          *      - 'parent'
1700          *      - 'description'
1701          *      - 'slug'
1702          * @return string term_id
1703          */
1704         public function wp_newTerm( $args ) {
1705                 if ( ! $this->minimum_args( $args, 4 ) )
1706                         return $this->error;
1707
1708                 $this->escape( $args );
1709
1710                 $blog_id            = (int) $args[0];
1711                 $username           = $args[1];
1712                 $password           = $args[2];
1713                 $content_struct     = $args[3];
1714
1715                 if ( ! $user = $this->login( $username, $password ) )
1716                         return $this->error;
1717
1718                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1719                 do_action( 'xmlrpc_call', 'wp.newTerm' );
1720
1721                 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1722                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1723
1724                 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1725
1726                 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1727                         return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1728
1729                 $taxonomy = (array) $taxonomy;
1730
1731                 // hold the data of the term
1732                 $term_data = array();
1733
1734                 $term_data['name'] = trim( $content_struct['name'] );
1735                 if ( empty( $term_data['name'] ) )
1736                         return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1737
1738                 if ( isset( $content_struct['parent'] ) ) {
1739                         if ( ! $taxonomy['hierarchical'] )
1740                                 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1741
1742                         $parent_term_id = (int) $content_struct['parent'];
1743                         $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1744
1745                         if ( is_wp_error( $parent_term ) )
1746                                 return new IXR_Error( 500, $parent_term->get_error_message() );
1747
1748                         if ( ! $parent_term )
1749                                 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1750
1751                         $term_data['parent'] = $content_struct['parent'];
1752                 }
1753
1754                 if ( isset( $content_struct['description'] ) )
1755                         $term_data['description'] = $content_struct['description'];
1756
1757                 if ( isset( $content_struct['slug'] ) )
1758                         $term_data['slug'] = $content_struct['slug'];
1759
1760                 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1761
1762                 if ( is_wp_error( $term ) )
1763                         return new IXR_Error( 500, $term->get_error_message() );
1764
1765                 if ( ! $term )
1766                         return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1767
1768                 return strval( $term['term_id'] );
1769         }
1770
1771         /**
1772          * Edit a term.
1773          *
1774          * @since 3.4.0
1775          *
1776          * @uses wp_update_term()
1777          * @param array $args Method parameters. Contains:
1778          *  - int     $blog_id
1779          *  - string  $username
1780          *  - string  $password
1781          *  - string  $term_id
1782          *  - array   $content_struct
1783          *      The $content_struct must contain:
1784          *      - 'taxonomy'
1785          *      Also, it can optionally contain:
1786          *      - 'name'
1787          *      - 'parent'
1788          *      - 'description'
1789          *      - 'slug'
1790          * @return bool True, on success.
1791          */
1792         public function wp_editTerm( $args ) {
1793                 if ( ! $this->minimum_args( $args, 5 ) )
1794                         return $this->error;
1795
1796                 $this->escape( $args );
1797
1798                 $blog_id            = (int) $args[0];
1799                 $username           = $args[1];
1800                 $password           = $args[2];
1801                 $term_id            = (int) $args[3];
1802                 $content_struct     = $args[4];
1803
1804                 if ( ! $user = $this->login( $username, $password ) )
1805                         return $this->error;
1806
1807                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1808                 do_action( 'xmlrpc_call', 'wp.editTerm' );
1809
1810                 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1811                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1812
1813                 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1814
1815                 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1816                         return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1817
1818                 $taxonomy = (array) $taxonomy;
1819
1820                 // hold the data of the term
1821                 $term_data = array();
1822
1823                 $term = get_term( $term_id , $content_struct['taxonomy'] );
1824
1825                 if ( is_wp_error( $term ) )
1826                         return new IXR_Error( 500, $term->get_error_message() );
1827
1828                 if ( ! $term )
1829                         return new IXR_Error( 404, __( 'Invalid term ID' ) );
1830
1831                 if ( isset( $content_struct['name'] ) ) {
1832                         $term_data['name'] = trim( $content_struct['name'] );
1833
1834                         if ( empty( $term_data['name'] ) )
1835                                 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1836                 }
1837
1838                 if ( isset( $content_struct['parent'] ) ) {
1839                         if ( ! $taxonomy['hierarchical'] )
1840                                 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1841
1842                         $parent_term_id = (int) $content_struct['parent'];
1843                         $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1844
1845                         if ( is_wp_error( $parent_term ) )
1846                                 return new IXR_Error( 500, $parent_term->get_error_message() );
1847
1848                         if ( ! $parent_term )
1849                                 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1850
1851                         $term_data['parent'] = $content_struct['parent'];
1852                 }
1853
1854                 if ( isset( $content_struct['description'] ) )
1855                         $term_data['description'] = $content_struct['description'];
1856
1857                 if ( isset( $content_struct['slug'] ) )
1858                         $term_data['slug'] = $content_struct['slug'];
1859
1860                 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1861
1862                 if ( is_wp_error( $term ) )
1863                         return new IXR_Error( 500, $term->get_error_message() );
1864
1865                 if ( ! $term )
1866                         return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1867
1868                 return true;
1869         }
1870
1871         /**
1872          * Delete a term.
1873          *
1874          * @since 3.4.0
1875          *
1876          * @uses wp_delete_term()
1877          * @param array $args Method parameters. Contains:
1878          *  - int     $blog_id
1879          *  - string  $username
1880          *  - string  $password
1881          *  - string  $taxnomy_name
1882          *  - string     $term_id
1883          * @return boolean|IXR_Error If it suceeded true else a reason why not
1884          */
1885         public function wp_deleteTerm( $args ) {
1886                 if ( ! $this->minimum_args( $args, 5 ) )
1887                         return $this->error;
1888
1889                 $this->escape( $args );
1890
1891                 $blog_id            = (int) $args[0];
1892                 $username           = $args[1];
1893                 $password           = $args[2];
1894                 $taxonomy           = $args[3];
1895                 $term_id            = (int) $args[4];
1896
1897                 if ( ! $user = $this->login( $username, $password ) )
1898                         return $this->error;
1899
1900                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1901                 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1902
1903                 if ( ! taxonomy_exists( $taxonomy ) )
1904                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1905
1906                 $taxonomy = get_taxonomy( $taxonomy );
1907
1908                 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
1909                         return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
1910
1911                 $term = get_term( $term_id, $taxonomy->name );
1912
1913                 if ( is_wp_error( $term ) )
1914                         return new IXR_Error( 500, $term->get_error_message() );
1915
1916                 if ( ! $term )
1917                         return new IXR_Error( 404, __( 'Invalid term ID' ) );
1918
1919                 $result = wp_delete_term( $term_id, $taxonomy->name );
1920
1921                 if ( is_wp_error( $result ) )
1922                         return new IXR_Error( 500, $term->get_error_message() );
1923
1924                 if ( ! $result )
1925                         return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
1926
1927                 return $result;
1928         }
1929
1930         /**
1931          * Retrieve a term.
1932          *
1933          * @since 3.4.0
1934          *
1935          * @uses get_term()
1936          * @param array $args Method parameters. Contains:
1937          *  - int     $blog_id
1938          *  - string  $username
1939          *  - string  $password
1940          *  - string  $taxonomy
1941          *  - string  $term_id
1942          * @return array contains:
1943          *  - 'term_id'
1944          *  - 'name'
1945          *  - 'slug'
1946          *  - 'term_group'
1947          *  - 'term_taxonomy_id'
1948          *  - 'taxonomy'
1949          *  - 'description'
1950          *  - 'parent'
1951          *  - 'count'
1952          */
1953         public function wp_getTerm( $args ) {
1954                 if ( ! $this->minimum_args( $args, 5 ) )
1955                         return $this->error;
1956
1957                 $this->escape( $args );
1958
1959                 $blog_id            = (int) $args[0];
1960                 $username           = $args[1];
1961                 $password           = $args[2];
1962                 $taxonomy           = $args[3];
1963                 $term_id            = (int) $args[4];
1964
1965                 if ( ! $user = $this->login( $username, $password ) )
1966                         return $this->error;
1967
1968                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1969                 do_action( 'xmlrpc_call', 'wp.getTerm' );
1970
1971                 if ( ! taxonomy_exists( $taxonomy ) )
1972                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1973
1974                 $taxonomy = get_taxonomy( $taxonomy );
1975
1976                 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1977                         return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1978
1979                 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
1980
1981                 if ( is_wp_error( $term ) )
1982                         return new IXR_Error( 500, $term->get_error_message() );
1983
1984                 if ( ! $term )
1985                         return new IXR_Error( 404, __( 'Invalid term ID' ) );
1986
1987                 return $this->_prepare_term( $term );
1988         }
1989
1990         /**
1991          * Retrieve all terms for a taxonomy.
1992          *
1993          * @since 3.4.0
1994          *
1995          * The optional $filter parameter modifies the query used to retrieve terms.
1996          * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
1997          *
1998          * @uses get_terms()
1999          * @param array $args Method parameters. Contains:
2000          *  - int     $blog_id
2001          *  - string  $username
2002          *  - string  $password
2003          *  - string  $taxonomy
2004          *  - array   $filter optional
2005          * @return array terms
2006          */
2007         public function wp_getTerms( $args ) {
2008                 if ( ! $this->minimum_args( $args, 4 ) )
2009                         return $this->error;
2010
2011                 $this->escape( $args );
2012
2013                 $blog_id        = (int) $args[0];
2014                 $username       = $args[1];
2015                 $password       = $args[2];
2016                 $taxonomy       = $args[3];
2017                 $filter         = isset( $args[4] ) ? $args[4] : array();
2018
2019                 if ( ! $user = $this->login( $username, $password ) )
2020                         return $this->error;
2021
2022                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2023                 do_action( 'xmlrpc_call', 'wp.getTerms' );
2024
2025                 if ( ! taxonomy_exists( $taxonomy ) )
2026                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2027
2028                 $taxonomy = get_taxonomy( $taxonomy );
2029
2030                 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2031                         return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2032
2033                 $query = array();
2034
2035                 if ( isset( $filter['number'] ) )
2036                         $query['number'] = absint( $filter['number'] );
2037
2038                 if ( isset( $filter['offset'] ) )
2039                         $query['offset'] = absint( $filter['offset'] );
2040
2041                 if ( isset( $filter['orderby'] ) ) {
2042                         $query['orderby'] = $filter['orderby'];
2043
2044                         if ( isset( $filter['order'] ) )
2045                                 $query['order'] = $filter['order'];
2046                 }
2047
2048                 if ( isset( $filter['hide_empty'] ) )
2049                         $query['hide_empty'] = $filter['hide_empty'];
2050                 else
2051                         $query['get'] = 'all';
2052
2053                 if ( isset( $filter['search'] ) )
2054                         $query['search'] = $filter['search'];
2055
2056                 $terms = get_terms( $taxonomy->name, $query );
2057
2058                 if ( is_wp_error( $terms ) )
2059                         return new IXR_Error( 500, $terms->get_error_message() );
2060
2061                 $struct = array();
2062
2063                 foreach ( $terms as $term ) {
2064                         $struct[] = $this->_prepare_term( $term );
2065                 }
2066
2067                 return $struct;
2068         }
2069
2070         /**
2071          * Retrieve a taxonomy.
2072          *
2073          * @since 3.4.0
2074          *
2075          * @uses get_taxonomy()
2076          * @param array $args Method parameters. Contains:
2077          *  - int     $blog_id
2078          *  - string  $username
2079          *  - string  $password
2080          *  - string  $taxonomy
2081          * @return array (@see get_taxonomy())
2082          */
2083         public function wp_getTaxonomy( $args ) {
2084                 if ( ! $this->minimum_args( $args, 4 ) )
2085                         return $this->error;
2086
2087                 $this->escape( $args );
2088
2089                 $blog_id        = (int) $args[0];
2090                 $username       = $args[1];
2091                 $password       = $args[2];
2092                 $taxonomy       = $args[3];
2093
2094                 if ( isset( $args[4] ) ) {
2095                         $fields = $args[4];
2096                 } else {
2097                         /**
2098                          * Filter the taxonomy query fields used by the given XML-RPC method.
2099                          *
2100                          * @since 3.4.0
2101                          *
2102                          * @param array  $fields An array of taxonomy fields to retrieve.
2103                          * @param string $method The method name.
2104                          */
2105                         $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
2106                 }
2107
2108                 if ( ! $user = $this->login( $username, $password ) )
2109                         return $this->error;
2110
2111                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2112                 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
2113
2114                 if ( ! taxonomy_exists( $taxonomy ) )
2115                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2116
2117                 $taxonomy = get_taxonomy( $taxonomy );
2118
2119                 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2120                         return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2121
2122                 return $this->_prepare_taxonomy( $taxonomy, $fields );
2123         }
2124
2125         /**
2126          * Retrieve all taxonomies.
2127          *
2128          * @since 3.4.0
2129          *
2130          * @uses get_taxonomies()
2131          * @param array $args Method parameters. Contains:
2132          *  - int     $blog_id
2133          *  - string  $username
2134          *  - string  $password
2135          * @return array taxonomies
2136          */
2137         public function wp_getTaxonomies( $args ) {
2138                 if ( ! $this->minimum_args( $args, 3 ) )
2139                         return $this->error;
2140
2141                 $this->escape( $args );
2142
2143                 $blog_id            = (int) $args[0];
2144                 $username           = $args[1];
2145                 $password           = $args[2];
2146                 $filter             = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2147
2148                 if ( isset( $args[4] ) ) {
2149                         $fields = $args[4];
2150                 } else {
2151                         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2152                         $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
2153                 }
2154
2155                 if ( ! $user = $this->login( $username, $password ) )
2156                         return $this->error;
2157
2158                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2159                 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
2160
2161                 $taxonomies = get_taxonomies( $filter, 'objects' );
2162
2163                 // holds all the taxonomy data
2164                 $struct = array();
2165
2166                 foreach ( $taxonomies as $taxonomy ) {
2167                         // capability check for post_types
2168                         if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2169                                 continue;
2170
2171                         $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
2172                 }
2173
2174                 return $struct;
2175         }
2176
2177         /**
2178          * Retrieve a user.
2179          *
2180          * The optional $fields parameter specifies what fields will be included
2181          * in the response array. This should be a list of field names. 'user_id' will
2182          * always be included in the response regardless of the value of $fields.
2183          *
2184          * Instead of, or in addition to, individual field names, conceptual group
2185          * names can be used to specify multiple fields. The available conceptual
2186          * groups are 'basic' and 'all'.
2187          *
2188          * @uses get_userdata()
2189          * @param array $args Method parameters. Contains:
2190          *  - int     $blog_id
2191          *  - string  $username
2192          *  - string  $password
2193          *  - int     $user_id
2194          *  - array   $fields optional
2195          * @return array contains (based on $fields parameter):
2196          *  - 'user_id'
2197          *  - 'username'
2198          *  - 'first_name'
2199          *  - 'last_name'
2200          *  - 'registered'
2201          *  - 'bio'
2202          *  - 'email'
2203          *  - 'nickname'
2204          *  - 'nicename'
2205          *  - 'url'
2206          *  - 'display_name'
2207          *  - 'roles'
2208          */
2209         public function wp_getUser( $args ) {
2210                 if ( ! $this->minimum_args( $args, 4 ) )
2211                         return $this->error;
2212
2213                 $this->escape( $args );
2214
2215                 $blog_id    = (int) $args[0];
2216                 $username   = $args[1];
2217                 $password   = $args[2];
2218                 $user_id    = (int) $args[3];
2219
2220                 if ( isset( $args[4] ) ) {
2221                         $fields = $args[4];
2222                 } else {
2223                         /**
2224                          * Filter the default user query fields used by the given XML-RPC method.
2225                          *
2226                          * @since 3.5.0
2227                          *
2228                          * @param array  $fields User query fields for given method. Default 'all'.
2229                          * @param string $method The method name.
2230                          */
2231                         $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2232                 }
2233
2234                 if ( ! $user = $this->login( $username, $password ) )
2235                         return $this->error;
2236
2237                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2238                 do_action( 'xmlrpc_call', 'wp.getUser' );
2239
2240                 if ( ! current_user_can( 'edit_user', $user_id ) )
2241                         return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2242
2243                 $user_data = get_userdata( $user_id );
2244
2245                 if ( ! $user_data )
2246                         return new IXR_Error( 404, __( 'Invalid user ID' ) );
2247
2248                 return $this->_prepare_user( $user_data, $fields );
2249         }
2250
2251         /**
2252          * Retrieve users.
2253          *
2254          * The optional $filter parameter modifies the query used to retrieve users.
2255          * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2256          * 'who', 'orderby', and 'order'.
2257          *
2258          * The optional $fields parameter specifies what fields will be included
2259          * in the response array.
2260          *
2261          * @uses get_users()
2262          * @see wp_getUser() for more on $fields and return values
2263          *
2264          * @param array $args Method parameters. Contains:
2265          *  - int     $blog_id
2266          *  - string  $username
2267          *  - string  $password
2268          *  - array   $filter optional
2269          *  - array   $fields optional
2270          * @return array users data
2271          */
2272         public function wp_getUsers( $args ) {
2273                 if ( ! $this->minimum_args( $args, 3 ) )
2274                         return $this->error;
2275
2276                 $this->escape( $args );
2277
2278                 $blog_id    = (int) $args[0];
2279                 $username   = $args[1];
2280                 $password   = $args[2];
2281                 $filter     = isset( $args[3] ) ? $args[3] : array();
2282
2283                 if ( isset( $args[4] ) ) {
2284                         $fields = $args[4];
2285                 } else {
2286                         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2287                         $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2288                 }
2289
2290                 if ( ! $user = $this->login( $username, $password ) )
2291                         return $this->error;
2292
2293                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2294                 do_action( 'xmlrpc_call', 'wp.getUsers' );
2295
2296                 if ( ! current_user_can( 'list_users' ) )
2297                         return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
2298
2299                 $query = array( 'fields' => 'all_with_meta' );
2300
2301                 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2302                 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2303
2304                 if ( isset( $filter['orderby'] ) ) {
2305                         $query['orderby'] = $filter['orderby'];
2306
2307                         if ( isset( $filter['order'] ) )
2308                                 $query['order'] = $filter['order'];
2309                 }
2310
2311                 if ( isset( $filter['role'] ) ) {
2312                         if ( get_role( $filter['role'] ) === null )
2313                                 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2314
2315                         $query['role'] = $filter['role'];
2316                 }
2317
2318                 if ( isset( $filter['who'] ) ) {
2319                         $query['who'] = $filter['who'];
2320                 }
2321
2322                 $users = get_users( $query );
2323
2324                 $_users = array();
2325                 foreach ( $users as $user_data ) {
2326                         if ( current_user_can( 'edit_user', $user_data->ID ) )
2327                                 $_users[] = $this->_prepare_user( $user_data, $fields );
2328                 }
2329                 return $_users;
2330         }
2331
2332         /**
2333          * Retrieve information about the requesting user.
2334          *
2335          * @uses get_userdata()
2336          * @param array $args Method parameters. Contains:
2337          *  - int     $blog_id
2338          *  - string  $username
2339          *  - string  $password
2340          *  - array   $fields optional
2341          * @return array (@see wp_getUser)
2342          */
2343         public function wp_getProfile( $args ) {
2344                 if ( ! $this->minimum_args( $args, 3 ) )
2345                         return $this->error;
2346
2347                 $this->escape( $args );
2348
2349                 $blog_id    = (int) $args[0];
2350                 $username   = $args[1];
2351                 $password   = $args[2];
2352
2353                 if ( isset( $args[3] ) ) {
2354                         $fields = $args[3];
2355                 } else {
2356                         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2357                         $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2358                 }
2359
2360                 if ( ! $user = $this->login( $username, $password ) )
2361                         return $this->error;
2362
2363                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2364                 do_action( 'xmlrpc_call', 'wp.getProfile' );
2365
2366                 if ( ! current_user_can( 'edit_user', $user->ID ) )
2367                         return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2368
2369                 $user_data = get_userdata( $user->ID );
2370
2371                 return $this->_prepare_user( $user_data, $fields );
2372         }
2373
2374         /**
2375          * Edit user's profile.
2376          *
2377          * @uses wp_update_user()
2378          * @param array $args Method parameters. Contains:
2379          *  - int     $blog_id
2380          *  - string  $username
2381          *  - string  $password
2382          *  - array   $content_struct
2383          *      It can optionally contain:
2384          *      - 'first_name'
2385          *      - 'last_name'
2386          *      - 'website'
2387          *      - 'display_name'
2388          *      - 'nickname'
2389          *      - 'nicename'
2390          *      - 'bio'
2391          * @return bool True, on success.
2392          */
2393         public function wp_editProfile( $args ) {
2394                 if ( ! $this->minimum_args( $args, 4 ) )
2395                         return $this->error;
2396
2397                 $this->escape( $args );
2398
2399                 $blog_id        = (int) $args[0];
2400                 $username       = $args[1];
2401                 $password       = $args[2];
2402                 $content_struct = $args[3];
2403
2404                 if ( ! $user = $this->login( $username, $password ) )
2405                         return $this->error;
2406
2407                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2408                 do_action( 'xmlrpc_call', 'wp.editProfile' );
2409
2410                 if ( ! current_user_can( 'edit_user', $user->ID ) )
2411                         return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2412
2413                 // holds data of the user
2414                 $user_data = array();
2415                 $user_data['ID'] = $user->ID;
2416
2417                 // only set the user details if it was given
2418                 if ( isset( $content_struct['first_name'] ) )
2419                         $user_data['first_name'] = $content_struct['first_name'];
2420
2421                 if ( isset( $content_struct['last_name'] ) )
2422                         $user_data['last_name'] = $content_struct['last_name'];
2423
2424                 if ( isset( $content_struct['url'] ) )
2425                         $user_data['user_url'] = $content_struct['url'];
2426
2427                 if ( isset( $content_struct['display_name'] ) )
2428                         $user_data['display_name'] = $content_struct['display_name'];
2429
2430                 if ( isset( $content_struct['nickname'] ) )
2431                         $user_data['nickname'] = $content_struct['nickname'];
2432
2433                 if ( isset( $content_struct['nicename'] ) )
2434                         $user_data['user_nicename'] = $content_struct['nicename'];
2435
2436                 if ( isset( $content_struct['bio'] ) )
2437                         $user_data['description'] = $content_struct['bio'];
2438
2439                 $result = wp_update_user( $user_data );
2440
2441                 if ( is_wp_error( $result ) )
2442                         return new IXR_Error( 500, $result->get_error_message() );
2443
2444                 if ( ! $result )
2445                         return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2446
2447                 return true;
2448         }
2449
2450         /**
2451          * Retrieve page.
2452          *
2453          * @since 2.2.0
2454          *
2455          * @param array $args Method parameters. Contains:
2456          *  - blog_id
2457          *  - page_id
2458          *  - username
2459          *  - password
2460          * @return array
2461          */
2462         public function wp_getPage($args) {
2463                 $this->escape($args);
2464
2465                 $blog_id        = (int) $args[0];
2466                 $page_id        = (int) $args[1];
2467                 $username       = $args[2];
2468                 $password       = $args[3];
2469
2470                 if ( !$user = $this->login($username, $password) ) {
2471                         return $this->error;
2472                 }
2473
2474                 $page = get_post($page_id);
2475                 if ( ! $page )
2476                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2477
2478                 if ( !current_user_can( 'edit_page', $page_id ) )
2479                         return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2480
2481                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2482                 do_action( 'xmlrpc_call', 'wp.getPage' );
2483
2484                 // If we found the page then format the data.
2485                 if ( $page->ID && ($page->post_type == 'page') ) {
2486                         return $this->_prepare_page( $page );
2487                 }
2488                 // If the page doesn't exist indicate that.
2489                 else {
2490                         return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2491                 }
2492         }
2493
2494         /**
2495          * Retrieve Pages.
2496          *
2497          * @since 2.2.0
2498          *
2499          * @param array $args Method parameters. Contains:
2500          *  - blog_id
2501          *  - username
2502          *  - password
2503          *  - num_pages
2504          * @return array
2505          */
2506         public function wp_getPages($args) {
2507                 $this->escape($args);
2508
2509                 $blog_id        = (int) $args[0];
2510                 $username       = $args[1];
2511                 $password       = $args[2];
2512                 $num_pages      = isset($args[3]) ? (int) $args[3] : 10;
2513
2514                 if ( !$user = $this->login($username, $password) )
2515                         return $this->error;
2516
2517                 if ( !current_user_can( 'edit_pages' ) )
2518                         return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2519
2520                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2521                 do_action( 'xmlrpc_call', 'wp.getPages' );
2522
2523                 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2524                 $num_pages = count($pages);
2525
2526                 // If we have pages, put together their info.
2527                 if ( $num_pages >= 1 ) {
2528                         $pages_struct = array();
2529
2530                         foreach ($pages as $page) {
2531                                 if ( current_user_can( 'edit_page', $page->ID ) )
2532                                         $pages_struct[] = $this->_prepare_page( $page );
2533                         }
2534
2535                         return($pages_struct);
2536                 }
2537                 // If no pages were found return an error.
2538                 else {
2539                         return(array());
2540                 }
2541         }
2542
2543         /**
2544          * Create new page.
2545          *
2546          * @since 2.2.0
2547          *
2548          * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
2549          * @return unknown
2550          */
2551         public function wp_newPage($args) {
2552                 // Items not escaped here will be escaped in newPost.
2553                 $username       = $this->escape($args[1]);
2554                 $password       = $this->escape($args[2]);
2555                 $page           = $args[3];
2556                 $publish        = $args[4];
2557
2558                 if ( !$user = $this->login($username, $password) )
2559                         return $this->error;
2560
2561                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2562                 do_action( 'xmlrpc_call', 'wp.newPage' );
2563
2564                 // Mark this as content for a page.
2565                 $args[3]["post_type"] = 'page';
2566
2567                 // Let mw_newPost do all of the heavy lifting.
2568                 return($this->mw_newPost($args));
2569         }
2570
2571         /**
2572          * Delete page.
2573          *
2574          * @since 2.2.0
2575          *
2576          * @param array $args Method parameters.
2577          * @return bool True, if success.
2578          */
2579         public function wp_deletePage($args) {
2580                 $this->escape($args);
2581
2582                 $blog_id        = (int) $args[0];
2583                 $username       = $args[1];
2584                 $password       = $args[2];
2585                 $page_id        = (int) $args[3];
2586
2587                 if ( !$user = $this->login($username, $password) )
2588                         return $this->error;
2589
2590                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2591                 do_action( 'xmlrpc_call', 'wp.deletePage' );
2592
2593                 // Get the current page based on the page_id and
2594                 // make sure it is a page and not a post.
2595                 $actual_page = get_post($page_id, ARRAY_A);
2596                 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2597                         return(new IXR_Error(404, __('Sorry, no such page.')));
2598
2599                 // Make sure the user can delete pages.
2600                 if ( !current_user_can('delete_page', $page_id) )
2601                         return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
2602
2603                 // Attempt to delete the page.
2604                 $result = wp_delete_post($page_id);
2605                 if ( !$result )
2606                         return(new IXR_Error(500, __('Failed to delete the page.')));
2607
2608                 /**
2609                  * Fires after a page has been successfully deleted via XML-RPC.
2610                  *
2611                  * @since 3.4.0
2612                  *
2613                  * @param int   $page_id ID of the deleted page.
2614                  * @param array $args    An array of arguments to delete the page.
2615                  */
2616                 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2617
2618                 return(true);
2619         }
2620
2621         /**
2622          * Edit page.
2623          *
2624          * @since 2.2.0
2625          *
2626          * @param array $args Method parameters.
2627          * @return unknown
2628          */
2629         public function wp_editPage($args) {
2630                 // Items not escaped here will be escaped in editPost.
2631                 $blog_id        = (int) $args[0];
2632                 $page_id        = (int) $this->escape($args[1]);
2633                 $username       = $this->escape($args[2]);
2634                 $password       = $this->escape($args[3]);
2635                 $content        = $args[4];
2636                 $publish        = $args[5];
2637
2638                 if ( !$user = $this->login($username, $password) )
2639                         return $this->error;
2640
2641                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2642                 do_action( 'xmlrpc_call', 'wp.editPage' );
2643
2644                 // Get the page data and make sure it is a page.
2645                 $actual_page = get_post($page_id, ARRAY_A);
2646                 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2647                         return(new IXR_Error(404, __('Sorry, no such page.')));
2648
2649                 // Make sure the user is allowed to edit pages.
2650                 if ( !current_user_can('edit_page', $page_id) )
2651                         return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
2652
2653                 // Mark this as content for a page.
2654                 $content['post_type'] = 'page';
2655
2656                 // Arrange args in the way mw_editPost understands.
2657                 $args = array(
2658                         $page_id,
2659                         $username,
2660                         $password,
2661                         $content,
2662                         $publish
2663                 );
2664
2665                 // Let mw_editPost do all of the heavy lifting.
2666                 return($this->mw_editPost($args));
2667         }
2668
2669         /**
2670          * Retrieve page list.
2671          *
2672          * @since 2.2.0
2673          *
2674          * @param array $args Method parameters.
2675          * @return unknown
2676          */
2677         public function wp_getPageList($args) {
2678                 global $wpdb;
2679
2680                 $this->escape($args);
2681
2682                 $blog_id                                = (int) $args[0];
2683                 $username                               = $args[1];
2684                 $password                               = $args[2];
2685
2686                 if ( !$user = $this->login($username, $password) )
2687                         return $this->error;
2688
2689                 if ( !current_user_can( 'edit_pages' ) )
2690                         return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2691
2692                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2693                 do_action( 'xmlrpc_call', 'wp.getPageList' );
2694
2695                 // Get list of pages ids and titles
2696                 $page_list = $wpdb->get_results("
2697                         SELECT ID page_id,
2698                                 post_title page_title,
2699                                 post_parent page_parent_id,
2700                                 post_date_gmt,
2701                                 post_date,
2702                                 post_status
2703                         FROM {$wpdb->posts}
2704                         WHERE post_type = 'page'
2705                         ORDER BY ID
2706                 ");
2707
2708                 // The date needs to be formatted properly.
2709                 $num_pages = count($page_list);
2710                 for ( $i = 0; $i < $num_pages; $i++ ) {
2711                         $page_list[$i]->dateCreated = $this->_convert_date(  $page_list[$i]->post_date );
2712                         $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2713
2714                         unset($page_list[$i]->post_date_gmt);
2715                         unset($page_list[$i]->post_date);
2716                         unset($page_list[$i]->post_status);
2717                 }
2718
2719                 return($page_list);
2720         }
2721
2722         /**
2723          * Retrieve authors list.
2724          *
2725          * @since 2.2.0
2726          *
2727          * @param array $args Method parameters.
2728          * @return array
2729          */
2730         public function wp_getAuthors($args) {
2731
2732                 $this->escape($args);
2733
2734                 $blog_id        = (int) $args[0];
2735                 $username       = $args[1];
2736                 $password       = $args[2];
2737
2738                 if ( !$user = $this->login($username, $password) )
2739                         return $this->error;
2740
2741                 if ( !current_user_can('edit_posts') )
2742                         return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
2743
2744                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2745                 do_action( 'xmlrpc_call', 'wp.getAuthors' );
2746
2747                 $authors = array();
2748                 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2749                         $authors[] = array(
2750                                 'user_id'       => $user->ID,
2751                                 'user_login'    => $user->user_login,
2752                                 'display_name'  => $user->display_name
2753                         );
2754                 }
2755
2756                 return $authors;
2757         }
2758
2759         /**
2760          * Get list of all tags
2761          *
2762          * @since 2.7.0
2763          *
2764          * @param array $args Method parameters.
2765          * @return array
2766          */
2767         public function wp_getTags( $args ) {
2768                 $this->escape( $args );
2769
2770                 $blog_id                = (int) $args[0];
2771                 $username               = $args[1];
2772                 $password               = $args[2];
2773
2774                 if ( !$user = $this->login($username, $password) )
2775                         return $this->error;
2776
2777                 if ( !current_user_can( 'edit_posts' ) )
2778                         return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2779
2780                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2781                 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2782
2783                 $tags = array();
2784
2785                 if ( $all_tags = get_tags() ) {
2786                         foreach( (array) $all_tags as $tag ) {
2787                                 $struct['tag_id']                       = $tag->term_id;
2788                                 $struct['name']                         = $tag->name;
2789                                 $struct['count']                        = $tag->count;
2790                                 $struct['slug']                         = $tag->slug;
2791                                 $struct['html_url']                     = esc_html( get_tag_link( $tag->term_id ) );
2792                                 $struct['rss_url']                      = esc_html( get_tag_feed_link( $tag->term_id ) );
2793
2794                                 $tags[] = $struct;
2795                         }
2796                 }
2797
2798                 return $tags;
2799         }
2800
2801         /**
2802          * Create new category.
2803          *
2804          * @since 2.2.0
2805          *
2806          * @param array $args Method parameters.
2807          * @return int Category ID.
2808          */
2809         public function wp_newCategory($args) {
2810                 $this->escape($args);
2811
2812                 $blog_id                                = (int) $args[0];
2813                 $username                               = $args[1];
2814                 $password                               = $args[2];
2815                 $category                               = $args[3];
2816
2817                 if ( !$user = $this->login($username, $password) )
2818                         return $this->error;
2819
2820                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2821                 do_action( 'xmlrpc_call', 'wp.newCategory' );
2822
2823                 // Make sure the user is allowed to add a category.
2824                 if ( !current_user_can('manage_categories') )
2825                         return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
2826
2827                 // If no slug was provided make it empty so that
2828                 // WordPress will generate one.
2829                 if ( empty($category['slug']) )
2830                         $category['slug'] = '';
2831
2832                 // If no parent_id was provided make it empty
2833                 // so that it will be a top level page (no parent).
2834                 if ( !isset($category['parent_id']) )
2835                         $category['parent_id'] = '';
2836
2837                 // If no description was provided make it empty.
2838                 if ( empty($category["description"]) )
2839                         $category["description"] = "";
2840
2841                 $new_category = array(
2842                         'cat_name'                              => $category['name'],
2843                         'category_nicename'             => $category['slug'],
2844                         'category_parent'               => $category['parent_id'],
2845                         'category_description'  => $category['description']
2846                 );
2847
2848                 $cat_id = wp_insert_category($new_category, true);
2849                 if ( is_wp_error( $cat_id ) ) {
2850                         if ( 'term_exists' == $cat_id->get_error_code() )
2851                                 return (int) $cat_id->get_error_data();
2852                         else
2853                                 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2854                 } elseif ( ! $cat_id ) {
2855                         return(new IXR_Error(500, __('Sorry, the new category failed.')));
2856                 }
2857
2858                 /**
2859                  * Fires after a new category has been successfully created via XML-RPC.
2860                  *
2861                  * @since 3.4.0
2862                  *
2863                  * @param int   $cat_id ID of the new category.
2864                  * @param array $args   An array of new category arguments.
2865                  */
2866                 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
2867
2868                 return $cat_id;
2869         }
2870
2871         /**
2872          * Remove category.
2873          *
2874          * @since 2.5.0
2875          *
2876          * @param array $args Method parameters.
2877          * @return mixed See {@link wp_delete_term()} for return info.
2878          */
2879         public function wp_deleteCategory($args) {
2880                 $this->escape($args);
2881
2882                 $blog_id                = (int) $args[0];
2883                 $username               = $args[1];
2884                 $password               = $args[2];
2885                 $category_id    = (int) $args[3];
2886
2887                 if ( !$user = $this->login($username, $password) )
2888                         return $this->error;
2889
2890                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2891                 do_action( 'xmlrpc_call', 'wp.deleteCategory' );
2892
2893                 if ( !current_user_can('manage_categories') )
2894                         return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
2895
2896                 $status = wp_delete_term( $category_id, 'category' );
2897
2898                 if ( true == $status ) {
2899                         /**
2900                          * Fires after a category has been successfully deleted via XML-RPC.
2901                          *
2902                          * @since 3.4.0
2903                          *
2904                          * @param int   $category_id ID of the deleted category.
2905                          * @param array $args        An array of arguments to delete the category.
2906                          */
2907                         do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
2908                 }
2909
2910                 return $status;
2911         }
2912
2913         /**
2914          * Retrieve category list.
2915          *
2916          * @since 2.2.0
2917          *
2918          * @param array $args Method parameters.
2919          * @return array
2920          */
2921         public function wp_suggestCategories($args) {
2922                 $this->escape($args);
2923
2924                 $blog_id                                = (int) $args[0];
2925                 $username                               = $args[1];
2926                 $password                               = $args[2];
2927                 $category                               = $args[3];
2928                 $max_results                    = (int) $args[4];
2929
2930                 if ( !$user = $this->login($username, $password) )
2931                         return $this->error;
2932
2933                 if ( !current_user_can( 'edit_posts' ) )
2934                         return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
2935
2936                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2937                 do_action( 'xmlrpc_call', 'wp.suggestCategories' );
2938
2939                 $category_suggestions = array();
2940                 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
2941                 foreach ( (array) get_categories($args) as $cat ) {
2942                         $category_suggestions[] = array(
2943                                 'category_id'   => $cat->term_id,
2944                                 'category_name' => $cat->name
2945                         );
2946                 }
2947
2948                 return($category_suggestions);
2949         }
2950
2951         /**
2952          * Retrieve comment.
2953          *
2954          * @since 2.7.0
2955          *
2956          * @param array $args Method parameters.
2957          * @return array
2958          */
2959         public function wp_getComment($args) {
2960                 $this->escape($args);
2961
2962                 $blog_id        = (int) $args[0];
2963                 $username       = $args[1];
2964                 $password       = $args[2];
2965                 $comment_id     = (int) $args[3];
2966
2967                 if ( !$user = $this->login($username, $password) )
2968                         return $this->error;
2969
2970                 if ( !current_user_can( 'moderate_comments' ) )
2971                         return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2972
2973                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2974                 do_action( 'xmlrpc_call', 'wp.getComment' );
2975
2976                 if ( ! $comment = get_comment($comment_id) )
2977                         return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2978
2979                 return $this->_prepare_comment( $comment );
2980         }
2981
2982         /**
2983          * Retrieve comments.
2984          *
2985          * Besides the common blog_id, username, and password arguments, it takes a filter
2986          * array as last argument.
2987          *
2988          * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
2989          *
2990          * The defaults are as follows:
2991          * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
2992          * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
2993          * - 'number' - Default is 10. Total number of media items to retrieve.
2994          * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2995          *
2996          * @since 2.7.0
2997          *
2998          * @param array $args Method parameters.
2999          * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
3000          */
3001         public function wp_getComments($args) {
3002                 $this->escape($args);
3003
3004                 $blog_id        = (int) $args[0];
3005                 $username       = $args[1];
3006                 $password       = $args[2];
3007                 $struct         = isset( $args[3] ) ? $args[3] : array();
3008
3009                 if ( !$user = $this->login($username, $password) )
3010                         return $this->error;
3011
3012                 if ( !current_user_can( 'moderate_comments' ) )
3013                         return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
3014
3015                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3016                 do_action( 'xmlrpc_call', 'wp.getComments' );
3017
3018                 if ( isset($struct['status']) )
3019                         $status = $struct['status'];
3020                 else
3021                         $status = '';
3022
3023                 $post_id = '';
3024                 if ( isset($struct['post_id']) )
3025                         $post_id = absint($struct['post_id']);
3026
3027                 $offset = 0;
3028                 if ( isset($struct['offset']) )
3029                         $offset = absint($struct['offset']);
3030
3031                 $number = 10;
3032                 if ( isset($struct['number']) )
3033                         $number = absint($struct['number']);
3034
3035                 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
3036
3037                 $comments_struct = array();
3038
3039                 foreach ( $comments as $comment ) {
3040                         $comments_struct[] = $this->_prepare_comment( $comment );
3041                 }
3042
3043                 return $comments_struct;
3044         }
3045
3046         /**
3047          * Delete a comment.
3048          *
3049          * By default, the comment will be moved to the trash instead of deleted.
3050          * See {@link wp_delete_comment()} for more information on
3051          * this behavior.
3052          *
3053          * @since 2.7.0
3054          *
3055          * @param array $args Method parameters. Contains:
3056          *  - blog_id
3057          *  - username
3058          *  - password
3059          *  - comment_id
3060          * @return mixed {@link wp_delete_comment()}
3061          */
3062         public function wp_deleteComment($args) {
3063                 $this->escape($args);
3064
3065                 $blog_id        = (int) $args[0];
3066                 $username       = $args[1];
3067                 $password       = $args[2];
3068                 $comment_ID     = (int) $args[3];
3069
3070                 if ( !$user = $this->login($username, $password) )
3071                         return $this->error;
3072
3073                 if ( !current_user_can( 'moderate_comments' ) )
3074                         return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3075
3076                 if ( ! get_comment($comment_ID) )
3077                         return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3078
3079                 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3080                         return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3081
3082                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3083                 do_action( 'xmlrpc_call', 'wp.deleteComment' );
3084
3085                 $status = wp_delete_comment( $comment_ID );
3086
3087                 if ( true == $status ) {
3088                         /**
3089                          * Fires after a comment has been successfully deleted via XML-RPC.
3090                          *
3091                          * @since 3.4.0
3092                          *
3093                          * @param int   $comment_ID ID of the deleted comment.
3094                          * @param array $args       An array of arguments to delete the comment.
3095                          */
3096                         do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
3097                 }
3098
3099                 return $status;
3100         }
3101
3102         /**
3103          * Edit comment.
3104          *
3105          * Besides the common blog_id, username, and password arguments, it takes a
3106          * comment_id integer and a content_struct array as last argument.
3107          *
3108          * The allowed keys in the content_struct array are:
3109          *  - 'author'
3110          *  - 'author_url'
3111          *  - 'author_email'
3112          *  - 'content'
3113          *  - 'date_created_gmt'
3114          *  - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
3115          *
3116          * @since 2.7.0
3117          *
3118          * @param array $args. Contains:
3119          *  - blog_id
3120          *  - username
3121          *  - password
3122          *  - comment_id
3123          *  - content_struct
3124          * @return bool True, on success.
3125          */
3126         public function wp_editComment($args) {
3127                 $this->escape($args);
3128
3129                 $blog_id        = (int) $args[0];
3130                 $username       = $args[1];
3131                 $password       = $args[2];
3132                 $comment_ID     = (int) $args[3];
3133                 $content_struct = $args[4];
3134
3135                 if ( !$user = $this->login($username, $password) )
3136                         return $this->error;
3137
3138                 if ( !current_user_can( 'moderate_comments' ) )
3139                         return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3140
3141                 if ( ! get_comment($comment_ID) )
3142                         return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3143
3144                 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3145                         return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3146
3147                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3148                 do_action( 'xmlrpc_call', 'wp.editComment' );
3149
3150                 if ( isset($content_struct['status']) ) {
3151                         $statuses = get_comment_statuses();
3152                         $statuses = array_keys($statuses);
3153
3154                         if ( ! in_array($content_struct['status'], $statuses) )
3155                                 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
3156                         $comment_approved = $content_struct['status'];
3157                 }
3158
3159                 // Do some timestamp voodoo
3160                 if ( !empty( $content_struct['date_created_gmt'] ) ) {
3161                         // We know this is supposed to be GMT, so we're going to slap that Z on there by force
3162                         $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
3163                         $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
3164                         $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
3165                 }
3166
3167                 if ( isset($content_struct['content']) )
3168                         $comment_content = $content_struct['content'];
3169
3170                 if ( isset($content_struct['author']) )
3171                         $comment_author = $content_struct['author'];
3172
3173                 if ( isset($content_struct['author_url']) )
3174                         $comment_author_url = $content_struct['author_url'];
3175
3176                 if ( isset($content_struct['author_email']) )
3177                         $comment_author_email = $content_struct['author_email'];
3178
3179                 // We've got all the data -- post it:
3180                 $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
3181
3182                 $result = wp_update_comment($comment);
3183                 if ( is_wp_error( $result ) )
3184                         return new IXR_Error(500, $result->get_error_message());
3185
3186                 if ( !$result )
3187                         return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
3188
3189                 /**
3190                  * Fires after a comment has been successfully updated via XML-RPC.
3191                  *
3192                  * @since 3.4.0
3193                  *
3194                  * @param int   $comment_ID ID of the updated comment.
3195                  * @param array $args       An array of arguments to update the comment.
3196                  */
3197                 do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
3198
3199                 return true;
3200         }
3201
3202         /**
3203          * Create new comment.
3204          *
3205          * @since 2.7.0
3206          *
3207          * @param array $args Method parameters.
3208          * @return mixed {@link wp_new_comment()}
3209          */
3210         public function wp_newComment($args) {
3211                 $this->escape($args);
3212
3213                 $blog_id        = (int) $args[0];
3214                 $username       = $args[1];
3215                 $password       = $args[2];
3216                 $post           = $args[3];
3217                 $content_struct = $args[4];
3218
3219                 /**
3220                  * Filter whether to allow anonymous comments over XML-RPC.
3221                  *
3222                  * @since 2.7.0
3223                  *
3224                  * @param bool $allow Whether to allow anonymous commenting via XML-RPC.
3225                  *                    Default false.
3226                  */
3227                 $allow_anon = apply_filters( 'xmlrpc_allow_anonymous_comments', false );
3228
3229                 $user = $this->login($username, $password);
3230
3231                 if ( !$user ) {
3232                         $logged_in = false;
3233                         if ( $allow_anon && get_option('comment_registration') )
3234                                 return new IXR_Error( 403, __( 'You must be registered to comment' ) );
3235                         else if ( !$allow_anon )
3236                                 return $this->error;
3237                 } else {
3238                         $logged_in = true;
3239                 }
3240
3241                 if ( is_numeric($post) )
3242                         $post_id = absint($post);
3243                 else
3244                         $post_id = url_to_postid($post);
3245
3246                 if ( ! $post_id )
3247                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3248
3249                 if ( ! get_post($post_id) )
3250                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
3251
3252                 $comment['comment_post_ID'] = $post_id;
3253
3254                 if ( $logged_in ) {
3255                         $comment['comment_author'] = $this->escape( $user->display_name );
3256                         $comment['comment_author_email'] = $this->escape( $user->user_email );
3257                         $comment['comment_author_url'] = $this->escape( $user->user_url );
3258                         $comment['user_ID'] = $user->ID;
3259                 } else {
3260                         $comment['comment_author'] = '';
3261                         if ( isset($content_struct['author']) )
3262                                 $comment['comment_author'] = $content_struct['author'];
3263
3264                         $comment['comment_author_email'] = '';
3265                         if ( isset($content_struct['author_email']) )
3266                                 $comment['comment_author_email'] = $content_struct['author_email'];
3267
3268                         $comment['comment_author_url'] = '';
3269                         if ( isset($content_struct['author_url']) )
3270                                 $comment['comment_author_url'] = $content_struct['author_url'];
3271
3272                         $comment['user_ID'] = 0;
3273
3274                         if ( get_option('require_name_email') ) {
3275                                 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
3276                                         return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
3277                                 elseif ( !is_email($comment['comment_author_email']) )
3278                                         return new IXR_Error( 403, __( 'A valid email address is required' ) );
3279                         }
3280                 }
3281
3282                 $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
3283
3284                 $comment['comment_content'] =  isset($content_struct['content']) ? $content_struct['content'] : null;
3285
3286                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3287                 do_action( 'xmlrpc_call', 'wp.newComment' );
3288
3289                 $comment_ID = wp_new_comment( $comment );
3290
3291                 /**
3292                  * Fires after a new comment has been successfully created via XML-RPC.
3293                  *
3294                  * @since 3.4.0
3295                  *
3296                  * @param int   $comment_ID ID of the new comment.
3297                  * @param array $args       An array of new comment arguments.
3298                  */
3299                 do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
3300
3301                 return $comment_ID;
3302         }
3303
3304         /**
3305          * Retrieve all of the comment status.
3306          *
3307          * @since 2.7.0
3308          *
3309          * @param array $args Method parameters.
3310          * @return array
3311          */
3312         public function wp_getCommentStatusList($args) {
3313                 $this->escape( $args );
3314
3315                 $blog_id        = (int) $args[0];
3316                 $username       = $args[1];
3317                 $password       = $args[2];
3318
3319                 if ( !$user = $this->login($username, $password) )
3320                         return $this->error;
3321
3322                 if ( !current_user_can( 'moderate_comments' ) )
3323                         return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3324
3325                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3326                 do_action( 'xmlrpc_call', 'wp.getCommentStatusList' );
3327
3328                 return get_comment_statuses();
3329         }
3330
3331         /**
3332          * Retrieve comment count.
3333          *
3334          * @since 2.5.0
3335          *
3336          * @param array $args Method parameters.
3337          * @return array
3338          */
3339         public function wp_getCommentCount( $args ) {
3340                 $this->escape($args);
3341
3342                 $blog_id        = (int) $args[0];
3343                 $username       = $args[1];
3344                 $password       = $args[2];
3345                 $post_id        = (int) $args[3];
3346
3347                 if ( !$user = $this->login($username, $password) )
3348                         return $this->error;
3349
3350                 if ( !current_user_can( 'edit_posts' ) )
3351                         return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
3352
3353                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3354                 do_action( 'xmlrpc_call', 'wp.getCommentCount' );
3355
3356                 $count = wp_count_comments( $post_id );
3357                 return array(
3358                         'approved' => $count->approved,
3359                         'awaiting_moderation' => $count->moderated,
3360                         'spam' => $count->spam,
3361                         'total_comments' => $count->total_comments
3362                 );
3363         }
3364
3365         /**
3366          * Retrieve post statuses.
3367          *
3368          * @since 2.5.0
3369          *
3370          * @param array $args Method parameters.
3371          * @return array
3372          */
3373         public function wp_getPostStatusList( $args ) {
3374                 $this->escape( $args );
3375
3376                 $blog_id        = (int) $args[0];
3377                 $username       = $args[1];
3378                 $password       = $args[2];
3379
3380                 if ( !$user = $this->login($username, $password) )
3381                         return $this->error;
3382
3383                 if ( !current_user_can( 'edit_posts' ) )
3384                         return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3385
3386                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3387                 do_action( 'xmlrpc_call', 'wp.getPostStatusList' );
3388
3389                 return get_post_statuses();
3390         }
3391
3392         /**
3393          * Retrieve page statuses.
3394          *
3395          * @since 2.5.0
3396          *
3397          * @param array $args Method parameters.
3398          * @return array
3399          */
3400         public function wp_getPageStatusList( $args ) {
3401                 $this->escape( $args );
3402
3403                 $blog_id        = (int) $args[0];
3404                 $username       = $args[1];
3405                 $password       = $args[2];
3406
3407                 if ( !$user = $this->login($username, $password) )
3408                         return $this->error;
3409
3410                 if ( !current_user_can( 'edit_pages' ) )
3411                         return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3412
3413                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3414                 do_action( 'xmlrpc_call', 'wp.getPageStatusList' );
3415
3416                 return get_page_statuses();
3417         }
3418
3419         /**
3420          * Retrieve page templates.
3421          *
3422          * @since 2.6.0
3423          *
3424          * @param array $args Method parameters.
3425          * @return array
3426          */
3427         public function wp_getPageTemplates( $args ) {
3428                 $this->escape( $args );
3429
3430                 $blog_id        = (int) $args[0];
3431                 $username       = $args[1];
3432                 $password       = $args[2];
3433
3434                 if ( !$user = $this->login($username, $password) )
3435                         return $this->error;
3436
3437                 if ( !current_user_can( 'edit_pages' ) )
3438                         return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3439
3440                 $templates = get_page_templates();
3441                 $templates['Default'] = 'default';
3442
3443                 return $templates;
3444         }
3445
3446         /**
3447          * Retrieve blog options.
3448          *
3449          * @since 2.6.0
3450          *
3451          * @param array $args Method parameters.
3452          * @return array
3453          */
3454         public function wp_getOptions( $args ) {
3455                 $this->escape( $args );
3456
3457                 $blog_id        = (int) $args[0];
3458                 $username       = $args[1];
3459                 $password       = $args[2];
3460                 $options        = isset( $args[3] ) ? (array) $args[3] : array();
3461
3462                 if ( !$user = $this->login($username, $password) )
3463                         return $this->error;
3464
3465                 // If no specific options where asked for, return all of them
3466                 if ( count( $options ) == 0 )
3467                         $options = array_keys($this->blog_options);
3468
3469                 return $this->_getOptions($options);
3470         }
3471
3472         /**
3473          * Retrieve blog options value from list.
3474          *
3475          * @since 2.6.0
3476          *
3477          * @param array $options Options to retrieve.
3478          * @return array
3479          */
3480         public function _getOptions($options) {
3481                 $data = array();
3482                 $can_manage = current_user_can( 'manage_options' );
3483                 foreach ( $options as $option ) {
3484                         if ( array_key_exists( $option, $this->blog_options ) ) {
3485                                 $data[$option] = $this->blog_options[$option];
3486                                 //Is the value static or dynamic?
3487                                 if ( isset( $data[$option]['option'] ) ) {
3488                                         $data[$option]['value'] = get_option( $data[$option]['option'] );
3489                                         unset($data[$option]['option']);
3490                                 }
3491
3492                                 if ( ! $can_manage )
3493                                         $data[$option]['readonly'] = true;
3494                         }
3495                 }
3496
3497                 return $data;
3498         }
3499
3500         /**
3501          * Update blog options.
3502          *
3503          * @since 2.6.0
3504          *
3505          * @param array $args Method parameters.
3506          * @return unknown
3507          */
3508         public function wp_setOptions( $args ) {
3509                 $this->escape( $args );
3510
3511                 $blog_id        = (int) $args[0];
3512                 $username       = $args[1];
3513                 $password       = $args[2];
3514                 $options        = (array) $args[3];
3515
3516                 if ( !$user = $this->login($username, $password) )
3517                         return $this->error;
3518
3519                 if ( !current_user_can( 'manage_options' ) )
3520                         return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
3521
3522                 foreach ( $options as $o_name => $o_value ) {
3523                         $option_names[] = $o_name;
3524                         if ( !array_key_exists( $o_name, $this->blog_options ) )
3525                                 continue;
3526
3527                         if ( $this->blog_options[$o_name]['readonly'] == true )
3528                                 continue;
3529
3530                         update_option( $this->blog_options[$o_name]['option'], wp_unslash( $o_value ) );
3531                 }
3532
3533                 //Now return the updated values
3534                 return $this->_getOptions($option_names);
3535         }
3536
3537         /**
3538          * Retrieve a media item by ID
3539          *
3540          * @since 3.1.0
3541          *
3542          * @param array $args Method parameters. Contains:
3543          *  - blog_id
3544          *  - username
3545          *  - password
3546          *  - attachment_id
3547          * @return array. Associative array containing:
3548          *  - 'date_created_gmt'
3549          *  - 'parent'
3550          *  - 'link'
3551          *  - 'thumbnail'
3552          *  - 'title'
3553          *  - 'caption'
3554          *  - 'description'
3555          *  - 'metadata'
3556          */
3557         public function wp_getMediaItem($args) {
3558                 $this->escape($args);
3559
3560                 $blog_id                = (int) $args[0];
3561                 $username               = $args[1];
3562                 $password               = $args[2];
3563                 $attachment_id  = (int) $args[3];
3564
3565                 if ( !$user = $this->login($username, $password) )
3566                         return $this->error;
3567
3568                 if ( !current_user_can( 'upload_files' ) )
3569                         return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
3570
3571                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3572                 do_action( 'xmlrpc_call', 'wp.getMediaItem' );
3573
3574                 if ( ! $attachment = get_post($attachment_id) )
3575                         return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
3576
3577                 return $this->_prepare_media_item( $attachment );
3578         }
3579
3580         /**
3581          * Retrieves a collection of media library items (or attachments)
3582          *
3583          * Besides the common blog_id, username, and password arguments, it takes a filter
3584          * array as last argument.
3585          *
3586          * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
3587          *
3588          * The defaults are as follows:
3589          * - 'number' - Default is 5. Total number of media items to retrieve.
3590          * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
3591          * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
3592          * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
3593          *
3594          * @since 3.1.0
3595          *
3596          * @param array $args Method parameters. Contains:
3597          *  - blog_id
3598          *  - username
3599          *  - password
3600          *  - filter
3601          * @return array. Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
3602          */
3603         public function wp_getMediaLibrary($args) {
3604                 $this->escape($args);
3605
3606                 $blog_id        = (int) $args[0];
3607                 $username       = $args[1];
3608                 $password       = $args[2];
3609                 $struct         = isset( $args[3] ) ? $args[3] : array() ;
3610
3611                 if ( !$user = $this->login($username, $password) )
3612                         return $this->error;
3613
3614                 if ( !current_user_can( 'upload_files' ) )
3615                         return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
3616
3617                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3618                 do_action( 'xmlrpc_call', 'wp.getMediaLibrary' );
3619
3620                 $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
3621                 $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
3622                 $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
3623                 $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
3624
3625                 $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
3626
3627                 $attachments_struct = array();
3628
3629                 foreach ($attachments as $attachment )
3630                         $attachments_struct[] = $this->_prepare_media_item( $attachment );
3631
3632                 return $attachments_struct;
3633         }
3634
3635         /**
3636           * Retrieves a list of post formats used by the site
3637           *
3638           * @since 3.1.0
3639           *
3640           * @param array $args Method parameters. Contains:
3641           *  - blog_id
3642           *  - username
3643           *  - password
3644           * @return array
3645           */
3646         public function wp_getPostFormats( $args ) {
3647                 $this->escape( $args );
3648
3649                 $blog_id = (int) $args[0];
3650                 $username = $args[1];
3651                 $password = $args[2];
3652
3653                 if ( !$user = $this->login( $username, $password ) )
3654                         return $this->error;
3655
3656                 if ( !current_user_can( 'edit_posts' ) )
3657                         return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
3658
3659                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3660                 do_action( 'xmlrpc_call', 'wp.getPostFormats' );
3661
3662                 $formats = get_post_format_strings();
3663
3664                 # find out if they want a list of currently supports formats
3665                 if ( isset( $args[3] ) && is_array( $args[3] ) ) {
3666                         if ( $args[3]['show-supported'] ) {
3667                                 if ( current_theme_supports( 'post-formats' ) ) {
3668                                         $supported = get_theme_support( 'post-formats' );
3669
3670                                         $data['all'] = $formats;
3671                                         $data['supported'] = $supported[0];
3672
3673                                         $formats = $data;
3674                                 }
3675                         }
3676                 }
3677
3678                 return $formats;
3679         }
3680
3681         /**
3682          * Retrieves a post type
3683          *
3684          * @since 3.4.0
3685          *
3686          * @uses get_post_type_object()
3687          * @param array $args Method parameters. Contains:
3688          *  - int     $blog_id
3689          *  - string  $username
3690          *  - string  $password
3691          *  - string  $post_type_name
3692          *  - array   $fields
3693          * @return array contains:
3694          *  - 'labels'
3695          *  - 'description'
3696          *  - 'capability_type'
3697          *  - 'cap'
3698          *  - 'map_meta_cap'
3699          *  - 'hierarchical'
3700          *  - 'menu_position'
3701          *  - 'taxonomies'
3702          *  - 'supports'
3703          */
3704         public function wp_getPostType( $args ) {
3705                 if ( ! $this->minimum_args( $args, 4 ) )
3706                         return $this->error;
3707
3708                 $this->escape( $args );
3709
3710                 $blog_id        = (int) $args[0];
3711                 $username       = $args[1];
3712                 $password       = $args[2];
3713                 $post_type_name = $args[3];
3714
3715                 if ( isset( $args[4] ) ) {
3716                         $fields = $args[4];
3717                 } else {
3718                         /**
3719                          * Filter the default query fields used by the given XML-RPC method.
3720                          *
3721                          * @since 3.4.0
3722                          *
3723                          * @param array  $fields An array of post type query fields for the given method.
3724                          * @param string $method The method name.
3725                          */
3726                         $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
3727                 }
3728
3729                 if ( !$user = $this->login( $username, $password ) )
3730                         return $this->error;
3731
3732                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3733                 do_action( 'xmlrpc_call', 'wp.getPostType' );
3734
3735                 if( ! post_type_exists( $post_type_name ) )
3736                         return new IXR_Error( 403, __( 'Invalid post type' ) );
3737
3738                 $post_type = get_post_type_object( $post_type_name );
3739
3740                 if( ! current_user_can( $post_type->cap->edit_posts ) )
3741                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
3742
3743                 return $this->_prepare_post_type( $post_type, $fields );
3744         }
3745
3746         /**
3747          * Retrieves a post types
3748          *
3749          * @since 3.4.0
3750          *
3751          * @uses get_post_types()
3752          * @param array $args Method parameters. Contains:
3753          *  - int     $blog_id
3754          *  - string  $username
3755          *  - string  $password
3756          *  - array   $filter
3757          *  - array   $fields
3758          * @return array
3759          */
3760         public function wp_getPostTypes( $args ) {
3761                 if ( ! $this->minimum_args( $args, 3 ) )
3762                         return $this->error;
3763
3764                 $this->escape( $args );
3765
3766                 $blog_id            = (int) $args[0];
3767                 $username           = $args[1];
3768                 $password           = $args[2];
3769                 $filter             = isset( $args[3] ) ? $args[3] : array( 'public' => true );
3770
3771                 if ( isset( $args[4] ) ) {
3772                         $fields = $args[4];
3773                 } else {
3774                         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3775                         $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
3776                 }
3777
3778                 if ( ! $user = $this->login( $username, $password ) )
3779                         return $this->error;
3780
3781                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3782                 do_action( 'xmlrpc_call', 'wp.getPostTypes' );
3783
3784                 $post_types = get_post_types( $filter, 'objects' );
3785
3786                 $struct = array();
3787
3788                 foreach( $post_types as $post_type ) {
3789                         if( ! current_user_can( $post_type->cap->edit_posts ) )
3790                                 continue;
3791
3792                         $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
3793                 }
3794
3795                 return $struct;
3796         }
3797
3798         /**
3799          * Retrieve revisions for a specific post.
3800          *
3801          * @since 3.5.0
3802          *
3803          * The optional $fields parameter specifies what fields will be included
3804          * in the response array.
3805          *
3806          * @uses wp_get_post_revisions()
3807          * @see wp_getPost() for more on $fields
3808          *
3809          * @param array $args Method parameters. Contains:
3810          *  - int     $blog_id
3811          *  - string  $username
3812          *  - string  $password
3813          *  - int     $post_id
3814          *  - array   $fields
3815          * @return array contains a collection of posts.
3816          */
3817         public function wp_getRevisions( $args ) {
3818                 if ( ! $this->minimum_args( $args, 4 ) )
3819                         return $this->error;
3820
3821                 $this->escape( $args );
3822
3823                 $blog_id    = (int) $args[0];
3824                 $username   = $args[1];
3825                 $password   = $args[2];
3826                 $post_id    = (int) $args[3];
3827
3828                 if ( isset( $args[4] ) ) {
3829                         $fields = $args[4];
3830                 } else {
3831                         /**
3832                          * Filter the default revision query fields used by the given XML-RPC method.
3833                          *
3834                          * @since 3.5.0
3835                          *
3836                          * @param array  $field  An array of revision query fields.
3837                          * @param string $method The method name.
3838                          */
3839                         $fields = apply_filters( 'xmlrpc_default_revision_fields', array( 'post_date', 'post_date_gmt' ), 'wp.getRevisions' );
3840                 }
3841
3842                 if ( ! $user = $this->login( $username, $password ) )
3843                         return $this->error;
3844
3845                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3846                 do_action( 'xmlrpc_call', 'wp.getRevisions' );
3847
3848                 if ( ! $post = get_post( $post_id ) )
3849                         return new IXR_Error( 404, __( 'Invalid post ID' ) );
3850
3851                 if ( ! current_user_can( 'edit_post', $post_id ) )
3852                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
3853
3854                 // Check if revisions are enabled.
3855                 if ( ! wp_revisions_enabled( $post ) )
3856                         return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
3857
3858                 $revisions = wp_get_post_revisions( $post_id );
3859
3860                 if ( ! $revisions )
3861                         return array();
3862
3863                 $struct = array();
3864
3865                 foreach ( $revisions as $revision ) {
3866                         if ( ! current_user_can( 'read_post', $revision->ID ) )
3867                                 continue;
3868
3869                         // Skip autosaves
3870                         if ( wp_is_post_autosave( $revision ) )
3871                                 continue;
3872
3873                         $struct[] = $this->_prepare_post( get_object_vars( $revision ), $fields );
3874                 }
3875
3876                 return $struct;
3877         }
3878
3879         /**
3880          * Restore a post revision
3881          *
3882          * @since 3.5.0
3883          *
3884          * @uses wp_restore_post_revision()
3885          *
3886          * @param array $args Method parameters. Contains:
3887          *  - int     $blog_id
3888          *  - string  $username
3889          *  - string  $password
3890          *  - int     $post_id
3891          * @return bool false if there was an error restoring, true if success.
3892          */
3893         public function wp_restoreRevision( $args ) {
3894                 if ( ! $this->minimum_args( $args, 3 ) )
3895                         return $this->error;
3896
3897                 $this->escape( $args );
3898
3899                 $blog_id     = (int) $args[0];
3900                 $username    = $args[1];
3901                 $password    = $args[2];
3902                 $revision_id = (int) $args[3];
3903
3904                 if ( ! $user = $this->login( $username, $password ) )
3905                         return $this->error;
3906
3907                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3908                 do_action( 'xmlrpc_call', 'wp.restoreRevision' );
3909
3910                 if ( ! $revision = wp_get_post_revision( $revision_id ) )
3911                         return new IXR_Error( 404, __( 'Invalid post ID' ) );
3912
3913                 if ( wp_is_post_autosave( $revision ) )
3914                         return new IXR_Error( 404, __( 'Invalid post ID' ) );
3915
3916                 if ( ! $post = get_post( $revision->post_parent ) )
3917                         return new IXR_Error( 404, __( 'Invalid post ID' ) );
3918
3919                 if ( ! current_user_can( 'edit_post', $revision->post_parent ) )
3920                         return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
3921
3922                 // Check if revisions are disabled.
3923                 if ( ! wp_revisions_enabled( $post ) )
3924                         return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
3925
3926                 $post = wp_restore_post_revision( $revision_id );
3927
3928                 return (bool) $post;
3929         }
3930
3931         /* Blogger API functions.
3932          * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
3933          */
3934
3935         /**
3936          * Retrieve blogs that user owns.
3937          *
3938          * Will make more sense once we support multiple blogs.
3939          *
3940          * @since 1.5.0
3941          *
3942          * @param array $args Method parameters.
3943          * @return array
3944          */
3945         public function blogger_getUsersBlogs($args) {
3946                 if ( is_multisite() )
3947                         return $this->_multisite_getUsersBlogs($args);
3948
3949                 $this->escape($args);
3950
3951                 $username = $args[1];
3952                 $password  = $args[2];
3953
3954                 if ( !$user = $this->login($username, $password) )
3955                         return $this->error;
3956
3957                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3958                 do_action( 'xmlrpc_call', 'blogger.getUsersBlogs' );
3959
3960                 $is_admin = current_user_can('manage_options');
3961
3962                 $struct = array(
3963                         'isAdmin'  => $is_admin,
3964                         'url'      => get_option('home') . '/',
3965                         'blogid'   => '1',
3966                         'blogName' => get_option('blogname'),
3967                         'xmlrpc'   => site_url( 'xmlrpc.php', 'rpc' ),
3968                 );
3969
3970                 return array($struct);
3971         }
3972
3973         /**
3974          * Private function for retrieving a users blogs for multisite setups
3975          *
3976          * @access protected
3977          */
3978         protected function _multisite_getUsersBlogs($args) {
3979                 $current_blog = get_blog_details();
3980
3981                 $domain = $current_blog->domain;
3982                 $path = $current_blog->path . 'xmlrpc.php';
3983
3984                 $rpc = new IXR_Client( set_url_scheme( "http://{$domain}{$path}" ) );
3985                 $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
3986                 $blogs = $rpc->getResponse();
3987
3988                 if ( isset($blogs['faultCode']) )
3989                         return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
3990
3991                 if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
3992                         return $blogs;
3993                 } else {
3994                         foreach ( (array) $blogs as $blog ) {
3995                                 if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
3996                                         return array($blog);
3997                         }
3998                         return array();
3999                 }
4000         }
4001
4002         /**
4003          * Retrieve user's data.
4004          *
4005          * Gives your client some info about you, so you don't have to.
4006          *
4007          * @since 1.5.0
4008          *
4009          * @param array $args Method parameters.
4010          * @return array
4011          */
4012         public function blogger_getUserInfo($args) {
4013
4014                 $this->escape($args);
4015
4016                 $username = $args[1];
4017                 $password  = $args[2];
4018
4019                 if ( !$user = $this->login($username, $password) )
4020                         return $this->error;
4021
4022                 if ( !current_user_can( 'edit_posts' ) )
4023                         return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
4024
4025                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4026                 do_action( 'xmlrpc_call', 'blogger.getUserInfo' );
4027
4028                 $struct = array(
4029                         'nickname'  => $user->nickname,
4030                         'userid'    => $user->ID,
4031                         'url'       => $user->user_url,
4032                         'lastname'  => $user->last_name,
4033                         'firstname' => $user->first_name
4034                 );
4035
4036                 return $struct;
4037         }
4038
4039         /**
4040          * Retrieve post.
4041          *
4042          * @since 1.5.0
4043          *
4044          * @param array $args Method parameters.
4045          * @return array
4046          */
4047         public function blogger_getPost($args) {
4048
4049                 $this->escape($args);
4050
4051                 $post_ID    = (int) $args[1];
4052                 $username = $args[2];
4053                 $password  = $args[3];
4054
4055                 if ( !$user = $this->login($username, $password) )
4056                         return $this->error;
4057
4058                 $post_data = get_post($post_ID, ARRAY_A);
4059                 if ( ! $post_data )
4060                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4061
4062                 if ( !current_user_can( 'edit_post', $post_ID ) )
4063                         return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
4064
4065                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4066                 do_action( 'xmlrpc_call', 'blogger.getPost' );
4067
4068                 $categories = implode(',', wp_get_post_categories($post_ID));
4069
4070                 $content  = '<title>'.wp_unslash($post_data['post_title']).'</title>';
4071                 $content .= '<category>'.$categories.'</category>';
4072                 $content .= wp_unslash($post_data['post_content']);
4073
4074                 $struct = array(
4075                         'userid'    => $post_data['post_author'],
4076                         'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
4077                         'content'     => $content,
4078                         'postid'  => (string) $post_data['ID']
4079                 );
4080
4081                 return $struct;
4082         }
4083
4084         /**
4085          * Retrieve list of recent posts.
4086          *
4087          * @since 1.5.0
4088          *
4089          * @param array $args Method parameters.
4090          * @return array
4091          */
4092         public function blogger_getRecentPosts($args) {
4093
4094                 $this->escape($args);
4095
4096                 // $args[0] = appkey - ignored
4097                 $blog_ID    = (int) $args[1]; /* though we don't use it yet */
4098                 $username = $args[2];
4099                 $password  = $args[3];
4100                 if ( isset( $args[4] ) )
4101                         $query = array( 'numberposts' => absint( $args[4] ) );
4102                 else
4103                         $query = array();
4104
4105                 if ( !$user = $this->login($username, $password) )
4106                         return $this->error;
4107
4108                 if ( ! current_user_can( 'edit_posts' ) )
4109                         return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
4110
4111                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4112                 do_action( 'xmlrpc_call', 'blogger.getRecentPosts' );
4113
4114                 $posts_list = wp_get_recent_posts( $query );
4115
4116                 if ( !$posts_list ) {
4117                         $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
4118                         return $this->error;
4119                 }
4120
4121                 foreach ($posts_list as $entry) {
4122                         if ( !current_user_can( 'edit_post', $entry['ID'] ) )
4123                                 continue;
4124
4125                         $post_date  = $this->_convert_date( $entry['post_date'] );
4126                         $categories = implode(',', wp_get_post_categories($entry['ID']));
4127
4128                         $content  = '<title>'.wp_unslash($entry['post_title']).'</title>';
4129                         $content .= '<category>'.$categories.'</category>';
4130                         $content .= wp_unslash($entry['post_content']);
4131
4132                         $struct[] = array(
4133                                 'userid' => $entry['post_author'],
4134                                 'dateCreated' => $post_date,
4135                                 'content' => $content,
4136                                 'postid' => (string) $entry['ID'],
4137                         );
4138
4139                 }
4140
4141                 $recent_posts = array();
4142                 for ( $j=0; $j<count($struct); $j++ ) {
4143                         array_push($recent_posts, $struct[$j]);
4144                 }
4145
4146                 return $recent_posts;
4147         }
4148
4149         /**
4150          * Deprecated.
4151          *
4152          * @since 1.5.0
4153          * @deprecated 3.5.0
4154          */
4155         public function blogger_getTemplate($args) {
4156                 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4157         }
4158
4159         /**
4160          * Deprecated.
4161          *
4162          * @since 1.5.0
4163          * @deprecated 3.5.0
4164          */
4165         public function blogger_setTemplate($args) {
4166                 return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
4167         }
4168
4169         /**
4170          * Create new post.
4171          *
4172          * @since 1.5.0
4173          *
4174          * @param array $args Method parameters.
4175          * @return int
4176          */
4177         public function blogger_newPost($args) {
4178
4179                 $this->escape($args);
4180
4181                 $blog_ID    = (int) $args[1]; /* though we don't use it yet */
4182                 $username = $args[2];
4183                 $password  = $args[3];
4184                 $content    = $args[4];
4185                 $publish    = $args[5];
4186
4187                 if ( !$user = $this->login($username, $password) )
4188                         return $this->error;
4189
4190                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4191                 do_action( 'xmlrpc_call', 'blogger.newPost' );
4192
4193                 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
4194                 if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) || !current_user_can($cap) )
4195                         return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
4196
4197                 $post_status = ($publish) ? 'publish' : 'draft';
4198
4199                 $post_author = $user->ID;
4200
4201                 $post_title = xmlrpc_getposttitle($content);
4202                 $post_category = xmlrpc_getpostcategory($content);
4203                 $post_content = xmlrpc_removepostdata($content);
4204
4205                 $post_date = current_time('mysql');
4206                 $post_date_gmt = current_time('mysql', 1);
4207
4208                 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
4209
4210                 $post_ID = wp_insert_post($post_data);
4211                 if ( is_wp_error( $post_ID ) )
4212                         return new IXR_Error(500, $post_ID->get_error_message());
4213
4214                 if ( !$post_ID )
4215                         return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4216
4217                 $this->attach_uploads( $post_ID, $post_content );
4218
4219                 /**
4220                  * Fires after a new post has been successfully created via the XML-RPC Blogger API.
4221                  *
4222                  * @since 3.4.0
4223                  *
4224                  * @param int   $post_ID ID of the new post.
4225                  * @param array $args    An array of new post arguments.
4226                  */
4227                 do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
4228
4229                 return $post_ID;
4230         }
4231
4232         /**
4233          * Edit a post.
4234          *
4235          * @since 1.5.0
4236          *
4237          * @param array $args Method parameters.
4238          * @return bool true when done.
4239          */
4240         public function blogger_editPost( $args ) {
4241
4242                 $this->escape($args);
4243
4244                 $post_ID     = (int) $args[1];
4245                 $username  = $args[2];
4246                 $password   = $args[3];
4247                 $content     = $args[4];
4248                 $publish     = $args[5];
4249
4250                 if ( ! $user = $this->login( $username, $password ) ) {
4251                         return $this->error;
4252                 }
4253
4254                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4255                 do_action( 'xmlrpc_call', 'blogger.editPost' );
4256
4257                 $actual_post = get_post( $post_ID, ARRAY_A );
4258
4259                 if ( ! $actual_post || $actual_post['post_type'] != 'post' ) {
4260                         return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
4261                 }
4262
4263                 $this->escape($actual_post);
4264
4265                 if ( ! current_user_can( 'edit_post', $post_ID ) ) {
4266                         return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
4267                 }
4268                 if ( 'publish' == $actual_post['post_status'] && ! current_user_can( 'publish_posts' ) ) {
4269                         return new IXR_Error( 401, __( 'Sorry, you do not have the right to publish this post.' ) );
4270                 }
4271
4272                 $postdata = array();
4273                 $postdata['ID'] = $actual_post['ID'];
4274                 $postdata['post_content'] = xmlrpc_removepostdata( $content );
4275                 $postdata['post_title'] = xmlrpc_getposttitle( $content );
4276                 $postdata['post_category'] = xmlrpc_getpostcategory( $content );
4277                 $postdata['post_status'] = $actual_post['post_status'];
4278                 $postdata['post_excerpt'] = $actual_post['post_excerpt'];
4279
4280                 $result = wp_update_post( $postdata );
4281
4282                 if ( ! $result ) {
4283                         return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
4284                 }
4285                 $this->attach_uploads( $actual_post['ID'], $postdata['post_content'] );
4286
4287                 /**
4288                  * Fires after a post has been successfully updated via the XML-RPC Blogger API.
4289                  *
4290                  * @since 3.4.0
4291                  *
4292                  * @param int   $post_ID ID of the updated post.
4293                  * @param array $args    An array of arguments for the post to edit.
4294                  */
4295                 do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
4296
4297                 return true;
4298         }
4299
4300         /**
4301          * Remove a post.
4302          *
4303          * @since 1.5.0
4304          *
4305          * @param array $args Method parameters.
4306          * @return bool True when post is deleted.
4307          */
4308         public function blogger_deletePost($args) {
4309                 $this->escape($args);
4310
4311                 $post_ID     = (int) $args[1];
4312                 $username  = $args[2];
4313                 $password   = $args[3];
4314                 $publish     = $args[4];
4315
4316                 if ( !$user = $this->login($username, $password) )
4317                         return $this->error;
4318
4319                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4320                 do_action( 'xmlrpc_call', 'blogger.deletePost' );
4321
4322                 $actual_post = get_post($post_ID,ARRAY_A);
4323
4324                 if ( !$actual_post || $actual_post['post_type'] != 'post' )
4325                         return new IXR_Error(404, __('Sorry, no such post.'));
4326
4327                 if ( !current_user_can('delete_post', $post_ID) )
4328                         return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
4329
4330                 $result = wp_delete_post($post_ID);
4331
4332                 if ( !$result )
4333                         return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
4334
4335                 /**
4336                  * Fires after a post has been successfully deleted via the XML-RPC Blogger API.
4337                  *
4338                  * @since 3.4.0
4339                  *
4340                  * @param int   $post_ID ID of the deleted post.
4341                  * @param array $args    An array of arguments to delete the post.
4342                  */
4343                 do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
4344
4345                 return true;
4346         }
4347
4348         /* MetaWeblog API functions
4349          * specs on wherever Dave Winer wants them to be
4350          */
4351
4352         /**
4353          * Create a new post.
4354          *
4355          * The 'content_struct' argument must contain:
4356          *  - title
4357          *  - description
4358          *  - mt_excerpt
4359          *  - mt_text_more
4360          *  - mt_keywords
4361          *  - mt_tb_ping_urls
4362          *  - categories
4363          *
4364          * Also, it can optionally contain:
4365          *  - wp_slug
4366          *  - wp_password
4367          *  - wp_page_parent_id
4368          *  - wp_page_order
4369          *  - wp_author_id
4370          *  - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
4371          *  - mt_allow_comments - can be 'open' or 'closed'
4372          *  - mt_allow_pings - can be 'open' or 'closed'
4373          *  - date_created_gmt
4374          *  - dateCreated
4375          *  - wp_post_thumbnail
4376          *
4377          * @since 1.5.0
4378          *
4379          * @param array $args Method parameters. Contains:
4380          *  - blog_id
4381          *  - username
4382          *  - password
4383          *  - content_struct
4384          *  - publish
4385          * @return int
4386          */
4387         public function mw_newPost($args) {
4388                 $this->escape($args);
4389
4390                 $blog_ID     = (int) $args[0];
4391                 $username  = $args[1];
4392                 $password   = $args[2];
4393                 $content_struct = $args[3];
4394                 $publish     = isset( $args[4] ) ? $args[4] : 0;
4395
4396                 if ( !$user = $this->login($username, $password) )
4397                         return $this->error;
4398
4399                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4400                 do_action( 'xmlrpc_call', 'metaWeblog.newPost' );
4401
4402                 $page_template = '';
4403                 if ( !empty( $content_struct['post_type'] ) ) {
4404                         if ( $content_struct['post_type'] == 'page' ) {
4405                                 if ( $publish )
4406                                         $cap  = 'publish_pages';
4407                                 elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
4408                                         $cap  = 'publish_pages';
4409                                 else
4410                                         $cap = 'edit_pages';
4411                                 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
4412                                 $post_type = 'page';
4413                                 if ( !empty( $content_struct['wp_page_template'] ) )
4414                                         $page_template = $content_struct['wp_page_template'];
4415                         } elseif ( $content_struct['post_type'] == 'post' ) {
4416                                 if ( $publish )
4417                                         $cap  = 'publish_posts';
4418                                 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
4419                                         $cap  = 'publish_posts';
4420                                 else
4421                                         $cap = 'edit_posts';
4422                                 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4423                                 $post_type = 'post';
4424                         } else {
4425                                 // No other post_type values are allowed here
4426                                 return new IXR_Error( 401, __( 'Invalid post type' ) );
4427                         }
4428                 } else {
4429                         if ( $publish )
4430                                 $cap  = 'publish_posts';
4431                         elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
4432                                 $cap  = 'publish_posts';
4433                         else
4434                                 $cap = 'edit_posts';
4435                         $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
4436                         $post_type = 'post';
4437                 }
4438
4439                 if ( ! current_user_can( get_post_type_object( $post_type )->cap->create_posts ) )
4440                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts on this site.' ) );
4441                 if ( !current_user_can( $cap ) )
4442                         return new IXR_Error( 401, $error_message );
4443
4444                 // Check for a valid post format if one was given
4445                 if ( isset( $content_struct['wp_post_format'] ) ) {
4446                         $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4447                         if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4448                                 return new IXR_Error( 404, __( 'Invalid post format' ) );
4449                         }
4450                 }
4451
4452                 // Let WordPress generate the post_name (slug) unless
4453                 // one has been provided.
4454                 $post_name = "";
4455                 if ( isset($content_struct['wp_slug']) )
4456                         $post_name = $content_struct['wp_slug'];
4457
4458                 // Only use a password if one was given.
4459                 if ( isset($content_struct['wp_password']) )
4460                         $post_password = $content_struct['wp_password'];
4461
4462                 // Only set a post parent if one was provided.
4463                 if ( isset($content_struct['wp_page_parent_id']) )
4464                         $post_parent = $content_struct['wp_page_parent_id'];
4465
4466                 // Only set the menu_order if it was provided.
4467                 if ( isset($content_struct['wp_page_order']) )
4468                         $menu_order = $content_struct['wp_page_order'];
4469
4470                 $post_author = $user->ID;
4471
4472                 // If an author id was provided then use it instead.
4473                 if ( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) {
4474                         switch ( $post_type ) {
4475                                 case "post":
4476                                         if ( !current_user_can( 'edit_others_posts' ) )
4477                                                 return( new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) ) );
4478                                         break;
4479                                 case "page":
4480                                         if ( !current_user_can( 'edit_others_pages' ) )
4481                                                 return( new IXR_Error( 401, __( 'You are not allowed to create pages as this user.' ) ) );
4482                                         break;
4483                                 default:
4484                                         return( new IXR_Error( 401, __( 'Invalid post type' ) ) );
4485                                         break;
4486                         }
4487                         $author = get_userdata( $content_struct['wp_author_id'] );
4488                         if ( ! $author )
4489                                 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
4490                         $post_author = $content_struct['wp_author_id'];
4491                 }
4492
4493                 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
4494                 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
4495
4496                 $post_status = $publish ? 'publish' : 'draft';
4497
4498                 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4499                         switch ( $content_struct["{$post_type}_status"] ) {
4500                                 case 'draft':
4501                                 case 'pending':
4502                                 case 'private':
4503                                 case 'publish':
4504                                         $post_status = $content_struct["{$post_type}_status"];
4505                                         break;
4506                                 default:
4507                                         $post_status = $publish ? 'publish' : 'draft';
4508                                         break;
4509                         }
4510                 }
4511
4512                 $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
4513                 $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
4514
4515                 $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
4516
4517                 if ( isset($content_struct['mt_allow_comments']) ) {
4518                         if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4519                                 switch ( $content_struct['mt_allow_comments'] ) {
4520                                         case 'closed':
4521                                                 $comment_status = 'closed';
4522                                                 break;
4523                                         case 'open':
4524                                                 $comment_status = 'open';
4525                                                 break;
4526                                         default:
4527                                                 $comment_status = get_option('default_comment_status');
4528                                                 break;
4529                                 }
4530                         } else {
4531                                 switch ( (int) $content_struct['mt_allow_comments'] ) {
4532                                         case 0:
4533                                         case 2:
4534                                                 $comment_status = 'closed';
4535                                                 break;
4536                                         case 1:
4537                                                 $comment_status = 'open';
4538                                                 break;
4539                                         default:
4540                                                 $comment_status = get_option('default_comment_status');
4541                                                 break;
4542                                 }
4543                         }
4544                 } else {
4545                         $comment_status = get_option('default_comment_status');
4546                 }
4547
4548                 if ( isset($content_struct['mt_allow_pings']) ) {
4549                         if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4550                                 switch ( $content_struct['mt_allow_pings'] ) {
4551                                         case 'closed':
4552                                                 $ping_status = 'closed';
4553                                                 break;
4554                                         case 'open':
4555                                                 $ping_status = 'open';
4556                                                 break;
4557                                         default:
4558                                                 $ping_status = get_option('default_ping_status');
4559                                                 break;
4560                                 }
4561                         } else {
4562                                 switch ( (int) $content_struct['mt_allow_pings'] ) {
4563                                         case 0:
4564                                                 $ping_status = 'closed';
4565                                                 break;
4566                                         case 1:
4567                                                 $ping_status = 'open';
4568                                                 break;
4569                                         default:
4570                                                 $ping_status = get_option('default_ping_status');
4571                                                 break;
4572                                 }
4573                         }
4574                 } else {
4575                         $ping_status = get_option('default_ping_status');
4576                 }
4577
4578                 if ( $post_more )
4579                         $post_content = $post_content . '<!--more-->' . $post_more;
4580
4581                 $to_ping = null;
4582                 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4583                         $to_ping = $content_struct['mt_tb_ping_urls'];
4584                         if ( is_array($to_ping) )
4585                                 $to_ping = implode(' ', $to_ping);
4586                 }
4587
4588                 // Do some timestamp voodoo
4589                 if ( !empty( $content_struct['date_created_gmt'] ) )
4590                         // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4591                         $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4592                 elseif ( !empty( $content_struct['dateCreated']) )
4593                         $dateCreated = $content_struct['dateCreated']->getIso();
4594
4595                 if ( !empty( $dateCreated ) ) {
4596                         $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4597                         $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4598                 } else {
4599                         $post_date = current_time('mysql');
4600                         $post_date_gmt = current_time('mysql', 1);
4601                 }
4602
4603                 $post_category = array();
4604                 if ( isset( $content_struct['categories'] ) ) {
4605                         $catnames = $content_struct['categories'];
4606
4607                         if ( is_array($catnames) ) {
4608                                 foreach ($catnames as $cat) {
4609                                         $post_category[] = get_cat_ID($cat);
4610                                 }
4611                         }
4612                 }
4613
4614                 $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');
4615
4616                 $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
4617
4618                 // Only posts can be sticky
4619                 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4620                         if ( $content_struct['sticky'] == true )
4621                                 stick_post( $post_ID );
4622                         elseif ( $content_struct['sticky'] == false )
4623                                 unstick_post( $post_ID );
4624                 }
4625
4626                 if ( isset($content_struct['custom_fields']) )
4627                         $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4628
4629                 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4630                         if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4631                                 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4632
4633                         unset( $content_struct['wp_post_thumbnail'] );
4634                 }
4635
4636                 // Handle enclosures
4637                 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4638                 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4639
4640                 $this->attach_uploads( $post_ID, $post_content );
4641
4642                 // Handle post formats if assigned, value is validated earlier
4643                 // in this function
4644                 if ( isset( $content_struct['wp_post_format'] ) )
4645                         set_post_format( $post_ID, $content_struct['wp_post_format'] );
4646
4647                 $post_ID = wp_insert_post( $postdata, true );
4648                 if ( is_wp_error( $post_ID ) )
4649                         return new IXR_Error(500, $post_ID->get_error_message());
4650
4651                 if ( !$post_ID )
4652                         return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
4653
4654                 /**
4655                  * Fires after a new post has been successfully created via the XML-RPC MovableType API.
4656                  *
4657                  * @since 3.4.0
4658                  *
4659                  * @param int   $post_ID ID of the new post.
4660                  * @param array $args    An array of arguments to create the new post.
4661                  */
4662                 do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
4663
4664                 return strval($post_ID);
4665         }
4666
4667         public function add_enclosure_if_new( $post_ID, $enclosure ) {
4668                 if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
4669                         $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'] . "\n";
4670                         $found = false;
4671                         if ( $enclosures = get_post_meta( $post_ID, 'enclosure' ) ) {
4672                                 foreach ( $enclosures as $enc ) {
4673                                         // This method used to omit the trailing new line. #23219
4674                                         if ( rtrim( $enc, "\n" ) == rtrim( $encstring, "\n" ) ) {
4675                                                 $found = true;
4676                                                 break;
4677                                         }
4678                                 }
4679                         }
4680                         if ( ! $found )
4681                                 add_post_meta( $post_ID, 'enclosure', $encstring );
4682                 }
4683         }
4684
4685         /**
4686          * Attach upload to a post.
4687          *
4688          * @since 2.1.0
4689          *
4690          * @param int $post_ID Post ID.
4691          * @param string $post_content Post Content for attachment.
4692          */
4693         public function attach_uploads( $post_ID, $post_content ) {
4694                 global $wpdb;
4695
4696                 // find any unattached files
4697                 $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
4698                 if ( is_array( $attachments ) ) {
4699                         foreach ( $attachments as $file ) {
4700                                 if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false )
4701                                         $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
4702                         }
4703                 }
4704         }
4705
4706         /**
4707          * Edit a post.
4708          *
4709          * @since 1.5.0
4710          *
4711          * @param array $args Method parameters.
4712          * @return bool True on success.
4713          */
4714         public function mw_editPost($args) {
4715
4716                 $this->escape($args);
4717
4718                 $post_ID        = (int) $args[0];
4719                 $username       = $args[1];
4720                 $password       = $args[2];
4721                 $content_struct = $args[3];
4722                 $publish        = isset( $args[4] ) ? $args[4] : 0;
4723
4724                 if ( ! $user = $this->login($username, $password) )
4725                         return $this->error;
4726
4727                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
4728                 do_action( 'xmlrpc_call', 'metaWeblog.editPost' );
4729
4730                 $postdata = get_post( $post_ID, ARRAY_A );
4731
4732                 // If there is no post data for the give post id, stop
4733                 // now and return an error. Other wise a new post will be
4734                 // created (which was the old behavior).
4735                 if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
4736                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
4737
4738                 if ( ! current_user_can( 'edit_post', $post_ID ) )
4739                         return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this post.' ) );
4740
4741                 // Use wp.editPost to edit post types other than post and page.
4742                 if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
4743                         return new IXR_Error( 401, __( 'Invalid post type' ) );
4744
4745                 // Thwart attempt to change the post type.
4746                 if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
4747                         return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
4748
4749                 // Check for a valid post format if one was given
4750                 if ( isset( $content_struct['wp_post_format'] ) ) {
4751                         $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
4752                         if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
4753                                 return new IXR_Error( 404, __( 'Invalid post format' ) );
4754                         }
4755                 }
4756
4757                 $this->escape($postdata);
4758
4759                 $ID = $postdata['ID'];
4760                 $post_content = $postdata['post_content'];
4761                 $post_title = $postdata['post_title'];
4762                 $post_excerpt = $postdata['post_excerpt'];
4763                 $post_password = $postdata['post_password'];
4764                 $post_parent = $postdata['post_parent'];
4765                 $post_type = $postdata['post_type'];
4766                 $menu_order = $postdata['menu_order'];
4767
4768                 // Let WordPress manage slug if none was provided.
4769                 $post_name = "";
4770                 $post_name = $postdata['post_name'];
4771                 if ( isset($content_struct['wp_slug']) )
4772                         $post_name = $content_struct['wp_slug'];
4773
4774                 // Only use a password if one was given.
4775                 if ( isset($content_struct['wp_password']) )
4776                         $post_password = $content_struct['wp_password'];
4777
4778                 // Only set a post parent if one was given.
4779                 if ( isset($content_struct['wp_page_parent_id']) )
4780                         $post_parent = $content_struct['wp_page_parent_id'];
4781
4782                 // Only set the menu_order if it was given.
4783                 if ( isset($content_struct['wp_page_order']) )
4784                         $menu_order = $content_struct['wp_page_order'];
4785
4786                 $page_template = null;
4787                 if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
4788                         $page_template = $content_struct['wp_page_template'];
4789
4790                 $post_author = $postdata['post_author'];
4791
4792                 // Only set the post_author if one is set.
4793                 if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
4794                         switch ( $post_type ) {
4795                                 case 'post':
4796                                         if ( !current_user_can('edit_others_posts') )
4797                                                 return(new IXR_Error(401, __('You are not allowed to change the post author as this user.')));
4798                                         break;
4799                                 case 'page':
4800                                         if ( !current_user_can('edit_others_pages') )
4801                                                 return(new IXR_Error(401, __('You are not allowed to change the page author as this user.')));
4802                                         break;
4803                                 default:
4804                                         return(new IXR_Error(401, __('Invalid post type')));
4805                                         break;
4806                         }
4807                         $post_author = $content_struct['wp_author_id'];
4808                 }
4809
4810                 if ( isset($content_struct['mt_allow_comments']) ) {
4811                         if ( !is_numeric($content_struct['mt_allow_comments']) ) {
4812                                 switch ( $content_struct['mt_allow_comments'] ) {
4813                                         case 'closed':
4814                                                 $comment_status = 'closed';
4815                                                 break;
4816                                         case 'open':
4817                                                 $comment_status = 'open';
4818                                                 break;
4819                                         default:
4820                                                 $comment_status = get_option('default_comment_status');
4821                                                 break;
4822                                 }
4823                         } else {
4824                                 switch ( (int) $content_struct['mt_allow_comments'] ) {
4825                                         case 0:
4826                                         case 2:
4827                                                 $comment_status = 'closed';
4828                                                 break;
4829                                         case 1:
4830                                                 $comment_status = 'open';
4831                                                 break;
4832                                         default:
4833                                                 $comment_status = get_option('default_comment_status');
4834                                                 break;
4835                                 }
4836                         }
4837                 }
4838
4839                 if ( isset($content_struct['mt_allow_pings']) ) {
4840                         if ( !is_numeric($content_struct['mt_allow_pings']) ) {
4841                                 switch ( $content_struct['mt_allow_pings'] ) {
4842                                         case 'closed':
4843                                                 $ping_status = 'closed';
4844                                                 break;
4845                                         case 'open':
4846                                                 $ping_status = 'open';
4847                                                 break;
4848                                         default:
4849                                                 $ping_status = get_option('default_ping_status');
4850                                                 break;
4851                                 }
4852                         } else {
4853                                 switch ( (int) $content_struct["mt_allow_pings"] ) {
4854                                         case 0:
4855                                                 $ping_status = 'closed';
4856                                                 break;
4857                                         case 1:
4858                                                 $ping_status = 'open';
4859                                                 break;
4860                                         default:
4861                                                 $ping_status = get_option('default_ping_status');
4862                                                 break;
4863                                 }
4864                         }
4865                 }
4866
4867                 if ( isset( $content_struct['title'] ) )
4868                         $post_title =  $content_struct['title'];
4869
4870                 if ( isset( $content_struct['description'] ) )
4871                         $post_content = $content_struct['description'];
4872
4873                 $post_category = array();
4874                 if ( isset( $content_struct['categories'] ) ) {
4875                         $catnames = $content_struct['categories'];
4876                         if ( is_array($catnames) ) {
4877                                 foreach ($catnames as $cat) {
4878                                         $post_category[] = get_cat_ID($cat);
4879                                 }
4880                         }
4881                 }
4882
4883                 if ( isset( $content_struct['mt_excerpt'] ) )
4884                         $post_excerpt =  $content_struct['mt_excerpt'];
4885
4886                 $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
4887
4888                 $post_status = $publish ? 'publish' : 'draft';
4889                 if ( isset( $content_struct["{$post_type}_status"] ) ) {
4890                         switch( $content_struct["{$post_type}_status"] ) {
4891                                 case 'draft':
4892                                 case 'pending':
4893                                 case 'private':
4894                                 case 'publish':
4895                                         $post_status = $content_struct["{$post_type}_status"];
4896                                         break;
4897                                 default:
4898                                         $post_status = $publish ? 'publish' : 'draft';
4899                                         break;
4900                         }
4901                 }
4902
4903                 $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
4904
4905                 if ( ('publish' == $post_status) ) {
4906                         if ( ( 'page' == $post_type ) && !current_user_can('publish_pages') )
4907                                 return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
4908                         else if ( !current_user_can('publish_posts') )
4909                                 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
4910                 }
4911
4912                 if ( $post_more )
4913                         $post_content = $post_content . "<!--more-->" . $post_more;
4914
4915                 $to_ping = null;
4916                 if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
4917                         $to_ping = $content_struct['mt_tb_ping_urls'];
4918                         if ( is_array($to_ping) )
4919                                 $to_ping = implode(' ', $to_ping);
4920                 }
4921
4922                 // Do some timestamp voodoo
4923                 if ( !empty( $content_struct['date_created_gmt'] ) )
4924                         // We know this is supposed to be GMT, so we're going to slap that Z on there by force
4925                         $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
4926                 elseif ( !empty( $content_struct['dateCreated']) )
4927                         $dateCreated = $content_struct['dateCreated']->getIso();
4928
4929                 if ( !empty( $dateCreated ) ) {
4930                         $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
4931                         $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
4932                 } else {
4933                         $post_date     = $postdata['post_date'];
4934                         $post_date_gmt = $postdata['post_date_gmt'];
4935                 }
4936
4937                 // We've got all the data -- post it:
4938                 $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');
4939
4940                 $result = wp_update_post($newpost, true);
4941                 if ( is_wp_error( $result ) )
4942                         return new IXR_Error(500, $result->get_error_message());
4943
4944                 if ( !$result )
4945                         return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
4946
4947                 // Only posts can be sticky
4948                 if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
4949                         if ( $content_struct['sticky'] == true )
4950                                 stick_post( $post_ID );
4951                         elseif ( $content_struct['sticky'] == false )
4952                                 unstick_post( $post_ID );
4953                 }
4954
4955                 if ( isset($content_struct['custom_fields']) )
4956                         $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
4957
4958                 if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
4959                         // empty value deletes, non-empty value adds/updates
4960                         if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
4961                                 delete_post_thumbnail( $post_ID );
4962                         } else {
4963                                 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
4964                                         return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
4965                         }
4966                         unset( $content_struct['wp_post_thumbnail'] );
4967                 }
4968
4969                 // Handle enclosures
4970                 $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
4971                 $this->add_enclosure_if_new($post_ID, $thisEnclosure);
4972
4973                 $this->attach_uploads( $ID, $post_content );
4974
4975                 // Handle post formats if assigned, validation is handled
4976                 // earlier in this function
4977                 if ( isset( $content_struct['wp_post_format'] ) )
4978                         set_post_format( $post_ID, $content_struct['wp_post_format'] );
4979
4980                 /**
4981                  * Fires after a post has been successfully updated via the XML-RPC MovableType API.
4982                  *
4983                  * @since 3.4.0
4984                  *
4985                  * @param int   $post_ID ID of the updated post.
4986                  * @param array $args    An array of arguments to update the post.
4987                  */
4988                 do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
4989
4990                 return true;
4991         }
4992
4993         /**
4994          * Retrieve post.
4995          *
4996          * @since 1.5.0
4997          *
4998          * @param array $args Method parameters.
4999          * @return array
5000          */
5001         public function mw_getPost($args) {
5002
5003                 $this->escape($args);
5004
5005                 $post_ID     = (int) $args[0];
5006                 $username  = $args[1];
5007                 $password   = $args[2];
5008
5009                 if ( !$user = $this->login($username, $password) )
5010                         return $this->error;
5011
5012                 $postdata = get_post($post_ID, ARRAY_A);
5013                 if ( ! $postdata )
5014                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5015
5016                 if ( !current_user_can( 'edit_post', $post_ID ) )
5017                         return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
5018
5019                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5020                 do_action( 'xmlrpc_call', 'metaWeblog.getPost' );
5021
5022                 if ($postdata['post_date'] != '') {
5023                         $post_date = $this->_convert_date( $postdata['post_date'] );
5024                         $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'],  $postdata['post_date'] );
5025                         $post_modified = $this->_convert_date( $postdata['post_modified'] );
5026                         $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
5027
5028                         $categories = array();
5029                         $catids = wp_get_post_categories($post_ID);
5030                         foreach($catids as $catid)
5031                                 $categories[] = get_cat_name($catid);
5032
5033                         $tagnames = array();
5034                         $tags = wp_get_post_tags( $post_ID );
5035                         if ( !empty( $tags ) ) {
5036                                 foreach ( $tags as $tag )
5037                                         $tagnames[] = $tag->name;
5038                                 $tagnames = implode( ', ', $tagnames );
5039                         } else {
5040                                 $tagnames = '';
5041                         }
5042
5043                         $post = get_extended($postdata['post_content']);
5044                         $link = post_permalink($postdata['ID']);
5045
5046                         // Get the author info.
5047                         $author = get_userdata($postdata['post_author']);
5048
5049                         $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
5050                         $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
5051
5052                         // Consider future posts as published
5053                         if ( $postdata['post_status'] === 'future' )
5054                                 $postdata['post_status'] = 'publish';
5055
5056                         // Get post format
5057                         $post_format = get_post_format( $post_ID );
5058                         if ( empty( $post_format ) )
5059                                 $post_format = 'standard';
5060
5061                         $sticky = false;
5062                         if ( is_sticky( $post_ID ) )
5063                                 $sticky = true;
5064
5065                         $enclosure = array();
5066                         foreach ( (array) get_post_custom($post_ID) as $key => $val) {
5067                                 if ($key == 'enclosure') {
5068                                         foreach ( (array) $val as $enc ) {
5069                                                 $encdata = explode("\n", $enc);
5070                                                 $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
5071                                                 $enclosure['length'] = (int) trim($encdata[1]);
5072                                                 $enclosure['type'] = trim($encdata[2]);
5073                                                 break 2;
5074                                         }
5075                                 }
5076                         }
5077
5078                         $resp = array(
5079                                 'dateCreated' => $post_date,
5080                                 'userid' => $postdata['post_author'],
5081                                 'postid' => $postdata['ID'],
5082                                 'description' => $post['main'],
5083                                 'title' => $postdata['post_title'],
5084                                 'link' => $link,
5085                                 'permaLink' => $link,
5086                                 // commented out because no other tool seems to use this
5087                                 //            'content' => $entry['post_content'],
5088                                 'categories' => $categories,
5089                                 'mt_excerpt' => $postdata['post_excerpt'],
5090                                 'mt_text_more' => $post['extended'],
5091                                 'wp_more_text' => $post['more_text'],
5092                                 'mt_allow_comments' => $allow_comments,
5093                                 'mt_allow_pings' => $allow_pings,
5094                                 'mt_keywords' => $tagnames,
5095                                 'wp_slug' => $postdata['post_name'],
5096                                 'wp_password' => $postdata['post_password'],
5097                                 'wp_author_id' => (string) $author->ID,
5098                                 'wp_author_display_name' => $author->display_name,
5099                                 'date_created_gmt' => $post_date_gmt,
5100                                 'post_status' => $postdata['post_status'],
5101                                 'custom_fields' => $this->get_custom_fields($post_ID),
5102                                 'wp_post_format' => $post_format,
5103                                 'sticky' => $sticky,
5104                                 'date_modified' => $post_modified,
5105                                 'date_modified_gmt' => $post_modified_gmt
5106                         );
5107
5108                         if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
5109
5110                         $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
5111
5112                         return $resp;
5113                 } else {
5114                         return new IXR_Error(404, __('Sorry, no such post.'));
5115                 }
5116         }
5117
5118         /**
5119          * Retrieve list of recent posts.
5120          *
5121          * @since 1.5.0
5122          *
5123          * @param array $args Method parameters.
5124          * @return array
5125          */
5126         public function mw_getRecentPosts($args) {
5127
5128                 $this->escape($args);
5129
5130                 $blog_ID     = (int) $args[0];
5131                 $username  = $args[1];
5132                 $password   = $args[2];
5133                 if ( isset( $args[3] ) )
5134                         $query = array( 'numberposts' => absint( $args[3] ) );
5135                 else
5136                         $query = array();
5137
5138                 if ( !$user = $this->login($username, $password) )
5139                         return $this->error;
5140
5141                 if ( ! current_user_can( 'edit_posts' ) )
5142                         return new IXR_Error( 401, __( 'Sorry, you cannot edit posts on this site.' ) );
5143
5144                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5145                 do_action( 'xmlrpc_call', 'metaWeblog.getRecentPosts' );
5146
5147                 $posts_list = wp_get_recent_posts( $query );
5148
5149                 if ( !$posts_list )
5150                         return array();
5151
5152                 $struct = array();
5153                 foreach ($posts_list as $entry) {
5154                         if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5155                                 continue;
5156
5157                         $post_date = $this->_convert_date( $entry['post_date'] );
5158                         $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5159                         $post_modified = $this->_convert_date( $entry['post_modified'] );
5160                         $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
5161
5162                         $categories = array();
5163                         $catids = wp_get_post_categories($entry['ID']);
5164                         foreach( $catids as $catid )
5165                                 $categories[] = get_cat_name($catid);
5166
5167                         $tagnames = array();
5168                         $tags = wp_get_post_tags( $entry['ID'] );
5169                         if ( !empty( $tags ) ) {
5170                                 foreach ( $tags as $tag ) {
5171                                         $tagnames[] = $tag->name;
5172                                 }
5173                                 $tagnames = implode( ', ', $tagnames );
5174                         } else {
5175                                 $tagnames = '';
5176                         }
5177
5178                         $post = get_extended($entry['post_content']);
5179                         $link = post_permalink($entry['ID']);
5180
5181                         // Get the post author info.
5182                         $author = get_userdata($entry['post_author']);
5183
5184                         $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
5185                         $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
5186
5187                         // Consider future posts as published
5188                         if ( $entry['post_status'] === 'future' )
5189                                 $entry['post_status'] = 'publish';
5190
5191                         // Get post format
5192                         $post_format = get_post_format( $entry['ID'] );
5193                         if ( empty( $post_format ) )
5194                                 $post_format = 'standard';
5195
5196                         $struct[] = array(
5197                                 'dateCreated' => $post_date,
5198                                 'userid' => $entry['post_author'],
5199                                 'postid' => (string) $entry['ID'],
5200                                 'description' => $post['main'],
5201                                 'title' => $entry['post_title'],
5202                                 'link' => $link,
5203                                 'permaLink' => $link,
5204                                 // commented out because no other tool seems to use this
5205                                 // 'content' => $entry['post_content'],
5206                                 'categories' => $categories,
5207                                 'mt_excerpt' => $entry['post_excerpt'],
5208                                 'mt_text_more' => $post['extended'],
5209                                 'wp_more_text' => $post['more_text'],
5210                                 'mt_allow_comments' => $allow_comments,
5211                                 'mt_allow_pings' => $allow_pings,
5212                                 'mt_keywords' => $tagnames,
5213                                 'wp_slug' => $entry['post_name'],
5214                                 'wp_password' => $entry['post_password'],
5215                                 'wp_author_id' => (string) $author->ID,
5216                                 'wp_author_display_name' => $author->display_name,
5217                                 'date_created_gmt' => $post_date_gmt,
5218                                 'post_status' => $entry['post_status'],
5219                                 'custom_fields' => $this->get_custom_fields($entry['ID']),
5220                                 'wp_post_format' => $post_format,
5221                                 'date_modified' => $post_modified,
5222                                 'date_modified_gmt' => $post_modified_gmt,
5223                                 'sticky' => ( $entry['post_type'] === 'post' && is_sticky( $entry['ID'] ) ),
5224                         );
5225
5226                         $entry_index = count( $struct ) - 1;
5227                         $struct[ $entry_index ][ 'wp_post_thumbnail' ] = get_post_thumbnail_id( $entry['ID'] );
5228                 }
5229
5230                 $recent_posts = array();
5231                 for ( $j=0; $j<count($struct); $j++ ) {
5232                         array_push($recent_posts, $struct[$j]);
5233                 }
5234
5235                 return $recent_posts;
5236         }
5237
5238         /**
5239          * Retrieve the list of categories on a given blog.
5240          *
5241          * @since 1.5.0
5242          *
5243          * @param array $args Method parameters.
5244          * @return array
5245          */
5246         public function mw_getCategories($args) {
5247
5248                 $this->escape($args);
5249
5250                 $blog_ID     = (int) $args[0];
5251                 $username  = $args[1];
5252                 $password   = $args[2];
5253
5254                 if ( !$user = $this->login($username, $password) )
5255                         return $this->error;
5256
5257                 if ( !current_user_can( 'edit_posts' ) )
5258                         return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5259
5260                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5261                 do_action( 'xmlrpc_call', 'metaWeblog.getCategories' );
5262
5263                 $categories_struct = array();
5264
5265                 if ( $cats = get_categories(array('get' => 'all')) ) {
5266                         foreach ( $cats as $cat ) {
5267                                 $struct['categoryId'] = $cat->term_id;
5268                                 $struct['parentId'] = $cat->parent;
5269                                 $struct['description'] = $cat->name;
5270                                 $struct['categoryDescription'] = $cat->description;
5271                                 $struct['categoryName'] = $cat->name;
5272                                 $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
5273                                 $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
5274
5275                                 $categories_struct[] = $struct;
5276                         }
5277                 }
5278
5279                 return $categories_struct;
5280         }
5281
5282         /**
5283          * Uploads a file, following your settings.
5284          *
5285          * Adapted from a patch by Johann Richard.
5286          *
5287          * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
5288          *
5289          * @since 1.5.0
5290          *
5291          * @param array $args Method parameters.
5292          * @return array
5293          */
5294         public function mw_newMediaObject($args) {
5295                 global $wpdb;
5296
5297                 $blog_ID     = (int) $args[0];
5298                 $username  = $this->escape($args[1]);
5299                 $password   = $this->escape($args[2]);
5300                 $data        = $args[3];
5301
5302                 $name = sanitize_file_name( $data['name'] );
5303                 $type = $data['type'];
5304                 $bits = $data['bits'];
5305
5306                 if ( !$user = $this->login($username, $password) )
5307                         return $this->error;
5308
5309                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5310                 do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject' );
5311
5312                 if ( !current_user_can('upload_files') ) {
5313                         $this->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
5314                         return $this->error;
5315                 }
5316
5317                 /**
5318                  * Filter whether to preempt the XML-RPC media upload.
5319                  *
5320                  * Passing a truthy value will effectively short-circuit the media upload,
5321                  * returning that value as a 500 error instead.
5322                  *
5323                  * @since 2.1.0
5324                  *
5325                  * @param bool $error Whether to pre-empt the media upload. Default false.
5326                  */
5327                 if ( $upload_err = apply_filters( 'pre_upload_error', false ) ) {
5328                         return new IXR_Error( 500, $upload_err );
5329                 }
5330
5331                 if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) {
5332                         // Get postmeta info on the object.
5333                         $old_file = $wpdb->get_row("
5334                                 SELECT ID
5335                                 FROM {$wpdb->posts}
5336                                 WHERE post_title = '{$name}'
5337                                         AND post_type = 'attachment'
5338                         ");
5339
5340                         // Delete previous file.
5341                         wp_delete_attachment($old_file->ID);
5342
5343                         // Make sure the new name is different by pre-pending the
5344                         // previous post id.
5345                         $filename = preg_replace('/^wpid\d+-/', '', $name);
5346                         $name = "wpid{$old_file->ID}-{$filename}";
5347                 }
5348
5349                 $upload = wp_upload_bits($name, null, $bits);
5350                 if ( ! empty($upload['error']) ) {
5351                         $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
5352                         return new IXR_Error(500, $errorString);
5353                 }
5354                 // Construct the attachment array
5355                 $post_id = 0;
5356                 if ( ! empty( $data['post_id'] ) ) {
5357                         $post_id = (int) $data['post_id'];
5358
5359                         if ( ! current_user_can( 'edit_post', $post_id ) )
5360                                 return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
5361                 }
5362                 $attachment = array(
5363                         'post_title' => $name,
5364                         'post_content' => '',
5365                         'post_type' => 'attachment',
5366                         'post_parent' => $post_id,
5367                         'post_mime_type' => $type,
5368                         'guid' => $upload[ 'url' ]
5369                 );
5370
5371                 // Save the data
5372                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
5373                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
5374
5375                 /**
5376                  * Fires after a new attachment has been added via the XML-RPC MovableType API.
5377                  *
5378                  * @since 3.4.0
5379                  *
5380                  * @param int   $id   ID of the new attachment.
5381                  * @param array $args An array of arguments to add the attachment.
5382                  */
5383                 do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
5384
5385                 $struct = array(
5386                         'id'   => strval( $id ),
5387                         'file' => $name,
5388                         'url'  => $upload[ 'url' ],
5389                         'type' => $type
5390                 );
5391
5392                 /** This filter is documented in wp-admin/includes/file.php */
5393                 return apply_filters( 'wp_handle_upload', $struct, 'upload' );
5394         }
5395
5396         /* MovableType API functions
5397          * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
5398          */
5399
5400         /**
5401          * Retrieve the post titles of recent posts.
5402          *
5403          * @since 1.5.0
5404          *
5405          * @param array $args Method parameters.
5406          * @return array
5407          */
5408         public function mt_getRecentPostTitles($args) {
5409
5410                 $this->escape($args);
5411
5412                 $blog_ID     = (int) $args[0];
5413                 $username  = $args[1];
5414                 $password   = $args[2];
5415                 if ( isset( $args[3] ) )
5416                         $query = array( 'numberposts' => absint( $args[3] ) );
5417                 else
5418                         $query = array();
5419
5420                 if ( !$user = $this->login($username, $password) )
5421                         return $this->error;
5422
5423                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5424                 do_action( 'xmlrpc_call', 'mt.getRecentPostTitles' );
5425
5426                 $posts_list = wp_get_recent_posts( $query );
5427
5428                 if ( !$posts_list ) {
5429                         $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
5430                         return $this->error;
5431                 }
5432
5433                 $struct = array();
5434
5435                 foreach ($posts_list as $entry) {
5436                         if ( !current_user_can( 'edit_post', $entry['ID'] ) )
5437                                 continue;
5438
5439                         $post_date = $this->_convert_date( $entry['post_date'] );
5440                         $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
5441
5442                         $struct[] = array(
5443                                 'dateCreated' => $post_date,
5444                                 'userid' => $entry['post_author'],
5445                                 'postid' => (string) $entry['ID'],
5446                                 'title' => $entry['post_title'],
5447                                 'post_status' => $entry['post_status'],
5448                                 'date_created_gmt' => $post_date_gmt
5449                         );
5450
5451                 }
5452
5453                 $recent_posts = array();
5454                 for ( $j=0; $j<count($struct); $j++ ) {
5455                         array_push($recent_posts, $struct[$j]);
5456                 }
5457
5458                 return $recent_posts;
5459         }
5460
5461         /**
5462          * Retrieve list of all categories on blog.
5463          *
5464          * @since 1.5.0
5465          *
5466          * @param array $args Method parameters.
5467          * @return array
5468          */
5469         public function mt_getCategoryList($args) {
5470
5471                 $this->escape($args);
5472
5473                 $blog_ID     = (int) $args[0];
5474                 $username  = $args[1];
5475                 $password   = $args[2];
5476
5477                 if ( !$user = $this->login($username, $password) )
5478                         return $this->error;
5479
5480                 if ( !current_user_can( 'edit_posts' ) )
5481                         return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
5482
5483                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5484                 do_action( 'xmlrpc_call', 'mt.getCategoryList' );
5485
5486                 $categories_struct = array();
5487
5488                 if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
5489                         foreach ( $cats as $cat ) {
5490                                 $struct['categoryId'] = $cat->term_id;
5491                                 $struct['categoryName'] = $cat->name;
5492
5493                                 $categories_struct[] = $struct;
5494                         }
5495                 }
5496
5497                 return $categories_struct;
5498         }
5499
5500         /**
5501          * Retrieve post categories.
5502          *
5503          * @since 1.5.0
5504          *
5505          * @param array $args Method parameters.
5506          * @return array
5507          */
5508         public function mt_getPostCategories($args) {
5509
5510                 $this->escape($args);
5511
5512                 $post_ID     = (int) $args[0];
5513                 $username  = $args[1];
5514                 $password   = $args[2];
5515
5516                 if ( !$user = $this->login($username, $password) )
5517                         return $this->error;
5518
5519                 if ( ! get_post( $post_ID ) )
5520                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5521
5522                 if ( !current_user_can( 'edit_post', $post_ID ) )
5523                         return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
5524
5525                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5526                 do_action( 'xmlrpc_call', 'mt.getPostCategories' );
5527
5528                 $categories = array();
5529                 $catids = wp_get_post_categories(intval($post_ID));
5530                 // first listed category will be the primary category
5531                 $isPrimary = true;
5532                 foreach ( $catids as $catid ) {
5533                         $categories[] = array(
5534                                 'categoryName' => get_cat_name($catid),
5535                                 'categoryId' => (string) $catid,
5536                                 'isPrimary' => $isPrimary
5537                         );
5538                         $isPrimary = false;
5539                 }
5540
5541                 return $categories;
5542         }
5543
5544         /**
5545          * Sets categories for a post.
5546          *
5547          * @since 1.5.0
5548          *
5549          * @param array $args Method parameters.
5550          * @return bool True on success.
5551          */
5552         public function mt_setPostCategories($args) {
5553
5554                 $this->escape($args);
5555
5556                 $post_ID     = (int) $args[0];
5557                 $username  = $args[1];
5558                 $password   = $args[2];
5559                 $categories  = $args[3];
5560
5561                 if ( !$user = $this->login($username, $password) )
5562                         return $this->error;
5563
5564                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5565                 do_action( 'xmlrpc_call', 'mt.setPostCategories' );
5566
5567                 if ( ! get_post( $post_ID ) )
5568                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5569
5570                 if ( !current_user_can('edit_post', $post_ID) )
5571                         return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
5572
5573                 $catids = array();
5574                 foreach ( $categories as $cat ) {
5575                         $catids[] = $cat['categoryId'];
5576                 }
5577
5578                 wp_set_post_categories($post_ID, $catids);
5579
5580                 return true;
5581         }
5582
5583         /**
5584          * Retrieve an array of methods supported by this server.
5585          *
5586          * @since 1.5.0
5587          *
5588          * @param array $args Method parameters.
5589          * @return array
5590          */
5591         public function mt_supportedMethods($args) {
5592
5593                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5594                 do_action( 'xmlrpc_call', 'mt.supportedMethods' );
5595
5596                 $supported_methods = array();
5597                 foreach ( $this->methods as $key => $value ) {
5598                         $supported_methods[] = $key;
5599                 }
5600
5601                 return $supported_methods;
5602         }
5603
5604         /**
5605          * Retrieve an empty array because we don't support per-post text filters.
5606          *
5607          * @since 1.5.0
5608          *
5609          * @param array $args Method parameters.
5610          */
5611         public function mt_supportedTextFilters($args) {
5612                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5613                 do_action( 'xmlrpc_call', 'mt.supportedTextFilters' );
5614
5615                 /**
5616                  * Filter the MoveableType text filters list for XML-RPC.
5617                  *
5618                  * @since 2.2.0
5619                  *
5620                  * @param array $filters An array of text filters.
5621                  */
5622                 return apply_filters( 'xmlrpc_text_filters', array() );
5623         }
5624
5625         /**
5626          * Retrieve trackbacks sent to a given post.
5627          *
5628          * @since 1.5.0
5629          *
5630          * @param array $args Method parameters.
5631          * @return mixed
5632          */
5633         public function mt_getTrackbackPings($args) {
5634
5635                 global $wpdb;
5636
5637                 $post_ID = intval($args);
5638
5639                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5640                 do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
5641
5642                 $actual_post = get_post($post_ID, ARRAY_A);
5643
5644                 if ( !$actual_post )
5645                         return new IXR_Error(404, __('Sorry, no such post.'));
5646
5647                 $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) );
5648
5649                 if ( !$comments )
5650                         return array();
5651
5652                 $trackback_pings = array();
5653                 foreach ( $comments as $comment ) {
5654                         if ( 'trackback' == $comment->comment_type ) {
5655                                 $content = $comment->comment_content;
5656                                 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
5657                                 $trackback_pings[] = array(
5658                                         'pingTitle' => $title,
5659                                         'pingURL'   => $comment->comment_author_url,
5660                                         'pingIP'    => $comment->comment_author_IP
5661                                 );
5662                         }
5663                 }
5664
5665                 return $trackback_pings;
5666         }
5667
5668         /**
5669          * Sets a post's publish status to 'publish'.
5670          *
5671          * @since 1.5.0
5672          *
5673          * @param array $args Method parameters.
5674          * @return int
5675          */
5676         public function mt_publishPost($args) {
5677
5678                 $this->escape($args);
5679
5680                 $post_ID     = (int) $args[0];
5681                 $username  = $args[1];
5682                 $password   = $args[2];
5683
5684                 if ( !$user = $this->login($username, $password) )
5685                         return $this->error;
5686
5687                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5688                 do_action( 'xmlrpc_call', 'mt.publishPost' );
5689
5690                 $postdata = get_post($post_ID, ARRAY_A);
5691                 if ( ! $postdata )
5692                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
5693
5694                 if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
5695                         return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
5696
5697                 $postdata['post_status'] = 'publish';
5698
5699                 // retain old cats
5700                 $cats = wp_get_post_categories($post_ID);
5701                 $postdata['post_category'] = $cats;
5702                 $this->escape($postdata);
5703
5704                 $result = wp_update_post($postdata);
5705
5706                 return $result;
5707         }
5708
5709         /* PingBack functions
5710          * specs on www.hixie.ch/specs/pingback/pingback
5711          */
5712
5713         /**
5714          * Retrieves a pingback and registers it.
5715          *
5716          * @since 1.5.0
5717          *
5718          * @param array $args Method parameters.
5719          * @return array
5720          */
5721         public function pingback_ping($args) {
5722                 global $wpdb;
5723
5724                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5725                 do_action( 'xmlrpc_call', 'pingback.ping' );
5726
5727                 $this->escape($args);
5728
5729                 $pagelinkedfrom = $args[0];
5730                 $pagelinkedto   = $args[1];
5731
5732                 $title = '';
5733
5734                 $pagelinkedfrom = str_replace('&amp;', '&', $pagelinkedfrom);
5735                 $pagelinkedto = str_replace('&amp;', '&', $pagelinkedto);
5736                 $pagelinkedto = str_replace('&', '&amp;', $pagelinkedto);
5737
5738                 /**
5739                  * Filter the pingback source URI.
5740                  *
5741                  * @since 3.6.0
5742                  *
5743                  * @param string $pagelinkedfrom URI of the page linked from.
5744                  * @param string $pagelinkedto   URI of the page linked to.
5745                  */
5746                 $pagelinkedfrom = apply_filters( 'pingback_ping_source_uri', $pagelinkedfrom, $pagelinkedto );
5747
5748                 if ( ! $pagelinkedfrom )
5749                         return $this->pingback_error( 0, __( 'A valid URL was not provided.' ) );
5750
5751                 // Check if the page linked to is in our site
5752                 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
5753                 if ( !$pos1 )
5754                         return $this->pingback_error( 0, __( 'Is there no link to us?' ) );
5755
5756                 // let's find which post is linked to
5757                 // FIXME: does url_to_postid() cover all these cases already?
5758                 //        if so, then let's use it and drop the old code.
5759                 $urltest = parse_url($pagelinkedto);
5760                 if ( $post_ID = url_to_postid($pagelinkedto) ) {
5761                         $way = 'url_to_postid()';
5762                 } elseif ( isset( $urltest['path'] ) && preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
5763                         // the path defines the post_ID (archives/p/XXXX)
5764                         $blah = explode('/', $match[0]);
5765                         $post_ID = (int) $blah[1];
5766                         $way = 'from the path';
5767                 } elseif ( isset( $urltest['query'] ) && preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
5768                         // the querystring defines the post_ID (?p=XXXX)
5769                         $blah = explode('=', $match[0]);
5770                         $post_ID = (int) $blah[1];
5771                         $way = 'from the querystring';
5772                 } elseif ( isset($urltest['fragment']) ) {
5773                         // an #anchor is there, it's either...
5774                         if ( intval($urltest['fragment']) ) {
5775                                 // ...an integer #XXXX (simplest case)
5776                                 $post_ID = (int) $urltest['fragment'];
5777                                 $way = 'from the fragment (numeric)';
5778                         } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
5779                                 // ...a post id in the form 'post-###'
5780                                 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
5781                                 $way = 'from the fragment (post-###)';
5782                         } elseif ( is_string($urltest['fragment']) ) {
5783                                 // ...or a string #title, a little more complicated
5784                                 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
5785                                 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
5786                                 if (! ($post_ID = $wpdb->get_var($sql)) ) {
5787                                         // returning unknown error '0' is better than die()ing
5788                                         return $this->pingback_error( 0, '' );
5789                                 }
5790                                 $way = 'from the fragment (title)';
5791                         }
5792                 } else {
5793                         // TODO: Attempt to extract a post ID from the given URL
5794                         return $this->pingback_error( 33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
5795                 }
5796                 $post_ID = (int) $post_ID;
5797
5798                 $post = get_post($post_ID);
5799
5800                 if ( !$post ) // Post_ID not found
5801                         return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
5802
5803                 if ( $post_ID == url_to_postid($pagelinkedfrom) )
5804                         return $this->pingback_error( 0, __( 'The source URL and the target URL cannot both point to the same resource.' ) );
5805
5806                 // Check if pings are on
5807                 if ( !pings_open($post) )
5808                         return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
5809
5810                 // Let's check that the remote site didn't already pingback this entry
5811                 if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
5812                         return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
5813
5814                 // very stupid, but gives time to the 'from' server to publish !
5815                 sleep(1);
5816
5817                 $remote_ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
5818
5819                 /** This filter is documented in wp-includes/class-http.php */
5820                 $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $GLOBALS['wp_version'] . '; ' . get_bloginfo( 'url' ) );
5821
5822                 // Let's check the remote site
5823                 $http_api_args = array(
5824                         'timeout' => 10,
5825                         'redirection' => 0,
5826                         'limit_response_size' => 153600, // 150 KB
5827                         'user-agent' => "$user_agent; verifying pingback from $remote_ip",
5828                         'headers' => array(
5829                                 'X-Pingback-Forwarded-For' => $remote_ip,
5830                         ),
5831                 );
5832                 $request = wp_safe_remote_get( $pagelinkedfrom, $http_api_args );
5833                 $linea = wp_remote_retrieve_body( $request );
5834
5835                 if ( !$linea )
5836                         return $this->pingback_error( 16, __( 'The source URL does not exist.' ) );
5837
5838                 /**
5839                  * Filter the pingback remote source.
5840                  *
5841                  * @since 2.5.0
5842                  *
5843                  * @param string $linea        Response object for the page linked from.
5844                  * @param string $pagelinkedto URL of the page linked to.
5845                  */
5846                 $linea = apply_filters( 'pre_remote_source', $linea, $pagelinkedto );
5847
5848                 // Work around bug in strip_tags():
5849                 $linea = str_replace('<!DOC', '<DOC', $linea);
5850                 $linea = preg_replace( '/[\r\n\t ]+/', ' ', $linea ); // normalize spaces
5851                 $linea = preg_replace( "/<\/*(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
5852
5853                 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
5854                 $title = $matchtitle[1];
5855                 if ( empty( $title ) )
5856                         return $this->pingback_error( 32, __('We cannot find a title on that page.' ) );
5857
5858                 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
5859
5860                 $p = explode( "\n\n", $linea );
5861
5862                 $preg_target = preg_quote($pagelinkedto, '|');
5863
5864                 foreach ( $p as $para ) {
5865                         if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
5866                                 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
5867
5868                                 // If the URL isn't in a link context, keep looking
5869                                 if ( empty($context) )
5870                                         continue;
5871
5872                                 // We're going to use this fake tag to mark the context in a bit
5873                                 // the marker is needed in case the link text appears more than once in the paragraph
5874                                 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
5875
5876                                 // prevent really long link text
5877                                 if ( strlen($context[1]) > 100 )
5878                                         $context[1] = substr($context[1], 0, 100) . '&#8230;';
5879
5880                                 $marker = '<wpcontext>'.$context[1].'</wpcontext>';    // set up our marker
5881                                 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
5882                                 $excerpt = strip_tags($excerpt, '<wpcontext>');        // strip all tags but our context marker
5883                                 $excerpt = trim($excerpt);
5884                                 $preg_marker = preg_quote($marker, '|');
5885                                 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
5886                                 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
5887                                 break;
5888                         }
5889                 }
5890
5891                 if ( empty($context) ) // Link to target not found
5892                         return $this->pingback_error( 17, __( 'The source URL does not contain a link to the target URL, and so cannot be used as a source.' ) );
5893
5894                 $pagelinkedfrom = str_replace('&', '&amp;', $pagelinkedfrom);
5895
5896                 $context = '[&#8230;] ' . esc_html( $excerpt ) . ' [&#8230;]';
5897                 $pagelinkedfrom = $this->escape( $pagelinkedfrom );
5898
5899                 $comment_post_ID = (int) $post_ID;
5900                 $comment_author = $title;
5901                 $comment_author_email = '';
5902                 $this->escape($comment_author);
5903                 $comment_author_url = $pagelinkedfrom;
5904                 $comment_content = $context;
5905                 $this->escape($comment_content);
5906                 $comment_type = 'pingback';
5907
5908                 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
5909
5910                 $comment_ID = wp_new_comment($commentdata);
5911
5912                 /**
5913                  * Fires after a post pingback has been sent.
5914                  *
5915                  * @since 0.71
5916                  *
5917                  * @param int $comment_ID Comment ID.
5918                  */
5919                 do_action( 'pingback_post', $comment_ID );
5920
5921                 return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
5922         }
5923
5924         /**
5925          * Retrieve array of URLs that pingbacked the given URL.
5926          *
5927          * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
5928          *
5929          * @since 1.5.0
5930          *
5931          * @param array $args Method parameters.
5932          * @return array
5933          */
5934         public function pingback_extensions_getPingbacks($args) {
5935
5936                 global $wpdb;
5937
5938                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
5939                 do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' );
5940
5941                 $this->escape($args);
5942
5943                 $url = $args;
5944
5945                 $post_ID = url_to_postid($url);
5946                 if ( !$post_ID ) {
5947                         // We aren't sure that the resource is available and/or pingback enabled
5948                         return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
5949                 }
5950
5951                 $actual_post = get_post($post_ID, ARRAY_A);
5952
5953                 if ( !$actual_post ) {
5954                         // No such post = resource not found
5955                         return $this->pingback_error( 32, __('The specified target URL does not exist.' ) );
5956                 }
5957
5958                 $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) );
5959
5960                 if ( !$comments )
5961                         return array();
5962
5963                 $pingbacks = array();
5964                 foreach ( $comments as $comment ) {
5965                         if ( 'pingback' == $comment->comment_type )
5966                                 $pingbacks[] = $comment->comment_author_url;
5967                 }
5968
5969                 return $pingbacks;
5970         }
5971
5972         protected function pingback_error( $code, $message ) {
5973                 /**
5974                  * Filter the XML-RPC pingback error return.
5975                  *
5976                  * @since 3.5.1
5977                  *
5978                  * @param IXR_Error $error An IXR_Error object containing the error code and message.
5979                  */
5980                 return apply_filters( 'xmlrpc_pingback_error', new IXR_Error( $code, $message ) );
5981         }
5982 }