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