WordPress 4.0
[autoinstalls/wordpress.git] / wp-includes / class-wp-xmlrpc-server.php
1 <?php
2 /**
3  * XML-RPC protocol support for WordPress
4  *
5  * @package WordPress
6  * @subpackage Publishing
7  */
8
9 /**
10  * WordPress XMLRPC server implementation.
11  *
12  * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
13  * pingback. Additional WordPress API for managing comments, pages, posts,
14  * options, etc.
15  *
16  * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled
17  * via the xmlrpc_enabled filter found in wp_xmlrpc_server::login().
18  *
19  * @package WordPress
20  * @subpackage Publishing
21  * @since 1.5.0
22  */
23 class wp_xmlrpc_server extends IXR_Server {
24
25         /**
26          * Register all of the XMLRPC methods that XMLRPC server understands.
27          *
28          * Sets up server and method property. Passes XMLRPC
29          * methods through the 'xmlrpc_methods' filter to allow plugins to extend
30          * or replace XMLRPC methods.
31          *
32          * @since 1.5.0
33          *
34          * @return wp_xmlrpc_server
35          */
36         public function __construct() {
37                 $this->methods = array(
38                         // WordPress API
39                         'wp.getUsersBlogs'              => 'this:wp_getUsersBlogs',
40                         'wp.newPost'                    => 'this:wp_newPost',
41                         'wp.editPost'                   => 'this:wp_editPost',
42                         'wp.deletePost'                 => 'this:wp_deletePost',
43                         'wp.getPost'                    => 'this:wp_getPost',
44                         'wp.getPosts'                   => 'this:wp_getPosts',
45                         'wp.newTerm'                    => 'this:wp_newTerm',
46                         'wp.editTerm'                   => 'this:wp_editTerm',
47                         'wp.deleteTerm'                 => 'this:wp_deleteTerm',
48                         'wp.getTerm'                    => 'this:wp_getTerm',
49                         'wp.getTerms'                   => 'this:wp_getTerms',
50                         'wp.getTaxonomy'                => 'this:wp_getTaxonomy',
51                         'wp.getTaxonomies'              => 'this:wp_getTaxonomies',
52                         'wp.getUser'                    => 'this:wp_getUser',
53                         'wp.getUsers'                   => 'this:wp_getUsers',
54                         'wp.getProfile'                 => 'this:wp_getProfile',
55                         'wp.editProfile'                => 'this:wp_editProfile',
56                         'wp.getPage'                    => 'this:wp_getPage',
57                         'wp.getPages'                   => 'this:wp_getPages',
58                         'wp.newPage'                    => 'this:wp_newPage',
59                         'wp.deletePage'                 => 'this:wp_deletePage',
60                         'wp.editPage'                   => 'this:wp_editPage',
61                         'wp.getPageList'                => 'this:wp_getPageList',
62                         'wp.getAuthors'                 => 'this:wp_getAuthors',
63                         'wp.getCategories'              => 'this:mw_getCategories',             // Alias
64                         'wp.getTags'                    => 'this:wp_getTags',
65                         'wp.newCategory'                => 'this:wp_newCategory',
66                         'wp.deleteCategory'             => 'this:wp_deleteCategory',
67                         'wp.suggestCategories'  => 'this:wp_suggestCategories',
68                         'wp.uploadFile'                 => 'this:mw_newMediaObject',    // Alias
69                         'wp.deleteFile'                 => 'this:wp_deletePost',                // Alias
70                         'wp.getCommentCount'    => 'this:wp_getCommentCount',
71                         'wp.getPostStatusList'  => 'this:wp_getPostStatusList',
72                         'wp.getPageStatusList'  => 'this:wp_getPageStatusList',
73                         'wp.getPageTemplates'   => 'this:wp_getPageTemplates',
74                         'wp.getOptions'                 => 'this:wp_getOptions',
75                         'wp.setOptions'                 => 'this:wp_setOptions',
76                         'wp.getComment'                 => 'this:wp_getComment',
77                         'wp.getComments'                => 'this:wp_getComments',
78                         'wp.deleteComment'              => 'this:wp_deleteComment',
79                         'wp.editComment'                => 'this:wp_editComment',
80                         'wp.newComment'                 => 'this:wp_newComment',
81                         'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
82                         'wp.getMediaItem'               => 'this:wp_getMediaItem',
83                         'wp.getMediaLibrary'    => 'this:wp_getMediaLibrary',
84                         'wp.getPostFormats'     => 'this:wp_getPostFormats',
85                         'wp.getPostType'                => 'this:wp_getPostType',
86                         'wp.getPostTypes'               => 'this:wp_getPostTypes',
87                         'wp.getRevisions'               => 'this:wp_getRevisions',
88                         'wp.restoreRevision'    => 'this:wp_restoreRevision',
89
90                         // Blogger API
91                         'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
92                         'blogger.getUserInfo' => 'this:blogger_getUserInfo',
93                         'blogger.getPost' => 'this:blogger_getPost',
94                         'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
95                         'blogger.newPost' => 'this:blogger_newPost',
96                         'blogger.editPost' => 'this:blogger_editPost',
97                         'blogger.deletePost' => 'this:blogger_deletePost',
98
99                         // MetaWeblog API (with MT extensions to structs)
100                         'metaWeblog.newPost' => 'this:mw_newPost',
101                         'metaWeblog.editPost' => 'this:mw_editPost',
102                         'metaWeblog.getPost' => 'this:mw_getPost',
103                         'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
104                         'metaWeblog.getCategories' => 'this:mw_getCategories',
105                         'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
106
107                         // MetaWeblog API aliases for Blogger API
108                         // see http://www.xmlrpc.com/stories/storyReader$2460
109                         'metaWeblog.deletePost' => 'this:blogger_deletePost',
110                         'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
111
112                         // MovableType API
113                         'mt.getCategoryList' => 'this:mt_getCategoryList',
114                         'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
115                         'mt.getPostCategories' => 'this:mt_getPostCategories',
116                         'mt.setPostCategories' => 'this:mt_setPostCategories',
117                         'mt.supportedMethods' => 'this:mt_supportedMethods',
118                         'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
119                         'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
120                         'mt.publishPost' => 'this:mt_publishPost',
121
122                         // PingBack
123                         'pingback.ping' => 'this:pingback_ping',
124                         'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
125
126                         'demo.sayHello' => 'this:sayHello',
127                         'demo.addTwoNumbers' => 'this:addTwoNumbers'
128                 );
129
130                 $this->initialise_blog_option_info();
131
132                 /**
133                  * Filter the methods exposed by the XML-RPC server.
134                  *
135                  * This filter can be used to add new methods, and remove built-in methods.
136                  *
137                  * @since 1.5.0
138                  *
139                  * @param array $methods An array of XML-RPC methods.
140                  */
141                 $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
142         }
143
144         /**
145          * Make private/protected methods readable for backwards compatibility.
146          *
147          * @since 4.0.0
148          * @access public
149          *
150          * @param callable $name      Method to call.
151          * @param array    $arguments Arguments to pass when calling.
152          * @return mixed|bool Return value of the callback, false otherwise.
153          */
154         public function __call( $name, $arguments ) {
155                 return call_user_func_array( array( $this, $name ), $arguments );
156         }
157
158         public function serve_request() {
159                 $this->IXR_Server($this->methods);
160         }
161
162         /**
163          * Test XMLRPC API by saying, "Hello!" to client.
164          *
165          * @since 1.5.0
166          *
167          * @param array $args Method Parameters.
168          * @return string
169          */
170         public function sayHello($args) {
171                 return 'Hello!';
172         }
173
174         /**
175          * Test XMLRPC API by adding two numbers for client.
176          *
177          * @since 1.5.0
178          *
179          * @param array $args Method Parameters.
180          * @return int
181          */
182         public function addTwoNumbers($args) {
183                 $number1 = $args[0];
184                 $number2 = $args[1];
185                 return $number1 + $number2;
186         }
187
188         /**
189          * Log user in.
190          *
191          * @since 2.8.0
192          *
193          * @param string $username User's username.
194          * @param string $password User's password.
195          * @return mixed WP_User object if authentication passed, false otherwise
196          */
197         public function login( $username, $password ) {
198                 /*
199                  * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
200                  * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
201                  */
202                 $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
203                 if ( false === $enabled ) {
204                         $enabled = apply_filters( 'option_enable_xmlrpc', true );
205                 }
206
207                 /**
208                  * Filter whether XML-RPC is enabled.
209                  *
210                  * This is the proper filter for turning off XML-RPC.
211                  *
212                  * @since 3.5.0
213                  *
214                  * @param bool $enabled Whether XML-RPC is enabled. Default true.
215                  */
216                 $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
217
218                 if ( ! $enabled ) {
219                         $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
220                         return false;
221                 }
222
223                 $user = wp_authenticate($username, $password);
224
225                 if (is_wp_error($user)) {
226                         $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
227
228                         /**
229                          * Filter the XML-RPC user login error message.
230                          *
231                          * @since 3.5.0
232                          *
233                          * @param string  $error The XML-RPC error message.
234                          * @param WP_User $user  WP_User object.
235                          */
236                         $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
237                         return false;
238                 }
239
240                 wp_set_current_user( $user->ID );
241                 return $user;
242         }
243
244         /**
245          * Check user's credentials. Deprecated.
246          *
247          * @since 1.5.0
248          * @deprecated 2.8.0
249          * @deprecated use wp_xmlrpc_server::login
250          * @see wp_xmlrpc_server::login
251          *
252          * @param string $username User's username.
253          * @param string $password User's password.
254          * @return bool Whether authentication passed.
255          */
256         public function login_pass_ok( $username, $password ) {
257                 return (bool) $this->login( $username, $password );
258         }
259
260         /**
261          * Escape string or array of strings for database.
262          *
263          * @since 1.5.2
264          *
265          * @param string|array $data Escape single string or array of strings.
266          * @return string|array Type matches $data and sanitized for the database.
267          */
268         public function escape( &$data ) {
269                 if ( ! is_array( $data ) )
270                         return wp_slash( $data );
271
272                 foreach ( $data as &$v ) {
273                         if ( is_array( $v ) )
274                                 $this->escape( $v );
275                         elseif ( ! is_object( $v ) )
276                                 $v = wp_slash( $v );
277                 }
278         }
279
280         /**
281          * Retrieve custom fields for post.
282          *
283          * @since 2.5.0
284          *
285          * @param int $post_id Post ID.
286          * @return array Custom fields, if exist.
287          */
288         public function get_custom_fields($post_id) {
289                 $post_id = (int) $post_id;
290
291                 $custom_fields = array();
292
293                 foreach ( (array) has_meta($post_id) as $meta ) {
294                         // Don't expose protected fields.
295                         if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
296                                 continue;
297
298                         $custom_fields[] = array(
299                                 "id"    => $meta['meta_id'],
300                                 "key"   => $meta['meta_key'],
301                                 "value" => $meta['meta_value']
302                         );
303                 }
304
305                 return $custom_fields;
306         }
307
308         /**
309          * Set custom fields for post.
310          *
311          * @since 2.5.0
312          *
313          * @param int $post_id Post ID.
314          * @param array $fields Custom fields.
315          */
316         public function set_custom_fields($post_id, $fields) {
317                 $post_id = (int) $post_id;
318
319                 foreach ( (array) $fields as $meta ) {
320                         if ( isset($meta['id']) ) {
321                                 $meta['id'] = (int) $meta['id'];
322                                 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
323                                 if ( isset($meta['key']) ) {
324                                         $meta['key'] = wp_unslash( $meta['key'] );
325                                         if ( $meta['key'] !== $pmeta->meta_key )
326                                                 continue;
327                                         $meta['value'] = wp_unslash( $meta['value'] );
328                                         if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
329                                                 update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
330                                 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
331                                         delete_metadata_by_mid( 'post', $meta['id'] );
332                                 }
333                         } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
334                                 add_post_meta( $post_id, $meta['key'], $meta['value'] );
335                         }
336                 }
337         }
338
339         /**
340          * Set up blog options property.
341          *
342          * Passes property through 'xmlrpc_blog_options' filter.
343          *
344          * @since 2.6.0
345          */
346         public function initialise_blog_option_info() {
347                 global $wp_version;
348
349                 $this->blog_options = array(
350                         // Read only options
351                         'software_name'     => array(
352                                 'desc'          => __( 'Software Name' ),
353                                 'readonly'      => true,
354                                 'value'         => 'WordPress'
355                         ),
356                         'software_version'  => array(
357                                 'desc'          => __( 'Software Version' ),
358                                 'readonly'      => true,
359                                 'value'         => $wp_version
360                         ),
361                         'blog_url'          => array(
362                                 'desc'          => __( 'WordPress Address (URL)' ),
363                                 'readonly'      => true,
364                                 'option'        => 'siteurl'
365                         ),
366                         'home_url'          => array(
367                                 'desc'          => __( 'Site Address (URL)' ),
368                                 'readonly'      => true,
369                                 'option'        => 'home'
370                         ),
371                         'login_url'          => array(
372                                 'desc'          => __( 'Login Address (URL)' ),
373                                 'readonly'      => true,
374                                 'value'         => wp_login_url( )
375                         ),
376                         'admin_url'          => array(
377                                 'desc'          => __( 'The URL to the admin area' ),
378                                 'readonly'      => true,
379                                 'value'         => get_admin_url( )
380                         ),
381                         'image_default_link_type' => array(
382                                 'desc'          => __( 'Image default link type' ),
383                                 'readonly'      => true,
384                                 'option'        => 'image_default_link_type'
385                         ),
386                         'image_default_size' => array(
387                                 'desc'          => __( 'Image default size' ),
388                                 'readonly'      => true,
389                                 'option'        => 'image_default_size'
390                         ),
391                         'image_default_align' => array(
392                                 'desc'          => __( 'Image default align' ),
393                                 'readonly'      => true,
394                                 'option'        => 'image_default_align'
395                         ),
396                         'template'          => array(
397                                 'desc'          => __( 'Template' ),
398                                 'readonly'      => true,
399                                 'option'        => 'template'
400                         ),
401                         'stylesheet'        => array(
402                                 'desc'          => __( 'Stylesheet' ),
403                                 'readonly'      => true,
404                                 'option'        => 'stylesheet'
405                         ),
406                         'post_thumbnail'    => array(
407                                 'desc'          => __('Post Thumbnail'),
408                                 'readonly'      => true,
409                                 'value'         => current_theme_supports( 'post-thumbnails' )
410                         ),
411
412                         // Updatable options
413                         'time_zone'         => array(
414                                 'desc'          => __( 'Time Zone' ),
415                                 'readonly'      => false,
416                                 'option'        => 'gmt_offset'
417                         ),
418                         'blog_title'        => array(
419                                 'desc'          => __( 'Site Title' ),
420                                 'readonly'      => false,
421                                 'option'        => 'blogname'
422                         ),
423                         'blog_tagline'      => array(
424                                 'desc'          => __( 'Site Tagline' ),
425                                 'readonly'      => false,
426                                 'option'        => 'blogdescription'
427                         ),
428                         'date_format'       => array(
429                                 'desc'          => __( 'Date Format' ),
430                                 'readonly'      => false,
431                                 'option'        => 'date_format'
432                         ),
433                         'time_format'       => array(
434                                 'desc'          => __( 'Time Format' ),
435                                 'readonly'      => false,
436                                 'option'        => 'time_format'
437                         ),
438                         'users_can_register' => array(
439                                 'desc'          => __( 'Allow new users to sign up' ),
440                                 'readonly'      => false,
441                                 'option'        => 'users_can_register'
442                         ),
443                         'thumbnail_size_w'  => array(
444                                 'desc'          => __( 'Thumbnail Width' ),
445                                 'readonly'      => false,
446                                 'option'        => 'thumbnail_size_w'
447                         ),
448                         'thumbnail_size_h'  => array(
449                                 'desc'          => __( 'Thumbnail Height' ),
450                                 'readonly'      => false,
451                                 'option'        => 'thumbnail_size_h'
452                         ),
453                         'thumbnail_crop'    => array(
454                                 'desc'          => __( 'Crop thumbnail to exact dimensions' ),
455                                 'readonly'      => false,
456                                 'option'        => 'thumbnail_crop'
457                         ),
458                         'medium_size_w'     => array(
459                                 'desc'          => __( 'Medium size image width' ),
460                                 'readonly'      => false,
461                                 'option'        => 'medium_size_w'
462                         ),
463                         'medium_size_h'     => array(
464                                 'desc'          => __( 'Medium size image height' ),
465                                 'readonly'      => false,
466                                 'option'        => 'medium_size_h'
467                         ),
468                         'large_size_w'      => array(
469                                 'desc'          => __( 'Large size image width' ),
470                                 'readonly'      => false,
471                                 'option'        => 'large_size_w'
472                         ),
473                         'large_size_h'      => array(
474                                 'desc'          => __( 'Large size image height' ),
475                                 'readonly'      => false,
476                                 'option'        => 'large_size_h'
477                         ),
478                         'default_comment_status' => array(
479                                 'desc'          => __( 'Allow people to post comments on new articles' ),
480                                 'readonly'      => false,
481                                 'option'        => 'default_comment_status'
482                         ),
483                         'default_ping_status' => array(
484                                 'desc'          => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
485                                 'readonly'      => false,
486                                 'option'        => 'default_ping_status'
487                         )
488                 );
489
490                 /**
491                  * Filter the XML-RPC blog options property.
492                  *
493                  * @since 2.6.0
494                  *
495                  * @param array $blog_options An array of XML-RPC blog options.
496                  */
497                 $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
498         }
499
500         /**
501          * Retrieve the blogs of the user.
502          *
503          * @since 2.6.0
504          *
505          * @param array $args Method parameters. Contains:
506          *  - username
507          *  - password
508          * @return array. Contains:
509          *  - 'isAdmin'
510          *  - 'url'
511          *  - 'blogid'
512          *  - 'blogName'
513          *  - 'xmlrpc' - url of xmlrpc endpoint
514          */
515         public function wp_getUsersBlogs( $args ) {
516                 // If this isn't on WPMU then just use blogger_getUsersBlogs
517                 if ( !is_multisite() ) {
518                         array_unshift( $args, 1 );
519                         return $this->blogger_getUsersBlogs( $args );
520                 }
521
522                 $this->escape( $args );
523
524                 $username = $args[0];
525                 $password = $args[1];
526
527                 if ( !$user = $this->login($username, $password) )
528                         return $this->error;
529
530                 /**
531                  * Fires after the XML-RPC user has been authenticated but before the rest of
532                  * the method logic begins.
533                  *
534                  * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
535                  * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
536                  *
537                  * @since 2.5.0
538                  *
539                  * @param method $name The method name.
540                  */
541                 do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
542
543                 $blogs = (array) get_blogs_of_user( $user->ID );
544                 $struct = array();
545
546                 foreach ( $blogs as $blog ) {
547                         // Don't include blogs that aren't hosted at this site
548                         if ( $blog->site_id != get_current_site()->id )
549                                 continue;
550
551                         $blog_id = $blog->userblog_id;
552
553                         switch_to_blog( $blog_id );
554
555                         $is_admin = current_user_can( 'manage_options' );
556
557                         $struct[] = array(
558                                 'isAdmin'               => $is_admin,
559                                 'url'                   => home_url( '/' ),
560                                 'blogid'                => (string) $blog_id,
561                                 'blogName'              => get_option( 'blogname' ),
562                                 'xmlrpc'                => site_url( 'xmlrpc.php', 'rpc' ),
563                         );
564
565                         restore_current_blog();
566                 }
567
568                 return $struct;
569         }
570
571         /**
572          * Checks if the method received at least the minimum number of arguments.
573          *
574          * @since 3.4.0
575          *
576          * @param string|array $args Sanitize single string or array of strings.
577          * @param int $count Minimum number of arguments.
578          * @return boolean if $args contains at least $count arguments.
579          */
580         protected function minimum_args( $args, $count ) {
581                 if ( count( $args ) < $count ) {
582                         $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
583                         return false;
584                 }
585
586                 return true;
587         }
588
589         /**
590          * Prepares taxonomy data for return in an XML-RPC object.
591          *
592          * @access protected
593          *
594          * @param object $taxonomy The unprepared taxonomy data
595          * @param array $fields The subset of taxonomy fields to return
596          * @return array The prepared taxonomy data
597          */
598         protected function _prepare_taxonomy( $taxonomy, $fields ) {
599                 $_taxonomy = array(
600                         'name' => $taxonomy->name,
601                         'label' => $taxonomy->label,
602                         'hierarchical' => (bool) $taxonomy->hierarchical,
603                         'public' => (bool) $taxonomy->public,
604                         'show_ui' => (bool) $taxonomy->show_ui,
605                         '_builtin' => (bool) $taxonomy->_builtin,
606                 );
607
608                 if ( in_array( 'labels', $fields ) )
609                         $_taxonomy['labels'] = (array) $taxonomy->labels;
610
611                 if ( in_array( 'cap', $fields ) )
612                         $_taxonomy['cap'] = (array) $taxonomy->cap;
613
614                 if ( in_array( 'menu', $fields ) )
615                         $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
616
617                 if ( in_array( 'object_type', $fields ) )
618                         $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
619
620                 /**
621                  * Filter XML-RPC-prepared data for the given taxonomy.
622                  *
623                  * @since 3.4.0
624                  *
625                  * @param array  $_taxonomy An array of taxonomy data.
626                  * @param object $taxonomy  Taxonomy object.
627                  * @param array  $fields    The subset of taxonomy fields to return.
628                  */
629                 return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
630         }
631
632         /**
633          * Prepares term data for return in an XML-RPC object.
634          *
635          * @access protected
636          *
637          * @param array|object $term The unprepared term data
638          * @return array The prepared term data
639          */
640         protected function _prepare_term( $term ) {
641                 $_term = $term;
642                 if ( ! is_array( $_term) )
643                         $_term = get_object_vars( $_term );
644
645                 // For integers which may be larger than XML-RPC supports ensure we return strings.
646                 $_term['term_id'] = strval( $_term['term_id'] );
647                 $_term['term_group'] = strval( $_term['term_group'] );
648                 $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
649                 $_term['parent'] = strval( $_term['parent'] );
650
651                 // Count we are happy to return as an integer because people really shouldn't use terms that much.
652                 $_term['count'] = intval( $_term['count'] );
653
654                 /**
655                  * Filter XML-RPC-prepared data for the given term.
656                  *
657                  * @since 3.4.0
658                  *
659                  * @param array        $_term An array of term data.
660                  * @param array|object $term  Term object or array.
661                  */
662                 return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
663         }
664
665         /**
666          * Convert a WordPress date string to an IXR_Date object.
667          *
668          * @access protected
669          *
670          * @param string $date
671          * @return IXR_Date
672          */
673         protected function _convert_date( $date ) {
674                 if ( $date === '0000-00-00 00:00:00' ) {
675                         return new IXR_Date( '00000000T00:00:00Z' );
676                 }
677                 return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
678         }
679
680         /**
681          * Convert a WordPress GMT date string to an IXR_Date object.
682          *
683          * @access protected
684          *
685          * @param string $date_gmt
686          * @param string $date
687          * @return IXR_Date
688          */
689         protected function _convert_date_gmt( $date_gmt, $date ) {
690                 if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
691                         return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
692                 }
693                 return $this->_convert_date( $date_gmt );
694         }
695
696         /**
697          * Prepares post data for return in an XML-RPC object.
698          *
699          * @access protected
700          *
701          * @param array $post The unprepared post data
702          * @param array $fields The subset of post type fields to return
703          * @return array The prepared post data
704          */
705         protected function _prepare_post( $post, $fields ) {
706                 // holds the data for this post. built up based on $fields
707                 $_post = array( 'post_id' => strval( $post['ID'] ) );
708
709                 // prepare common post fields
710                 $post_fields = array(
711                         'post_title'        => $post['post_title'],
712                         'post_date'         => $this->_convert_date( $post['post_date'] ),
713                         'post_date_gmt'     => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
714                         'post_modified'     => $this->_convert_date( $post['post_modified'] ),
715                         'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
716                         'post_status'       => $post['post_status'],
717                         'post_type'         => $post['post_type'],
718                         'post_name'         => $post['post_name'],
719                         'post_author'       => $post['post_author'],
720                         'post_password'     => $post['post_password'],
721                         'post_excerpt'      => $post['post_excerpt'],
722                         'post_content'      => $post['post_content'],
723                         'post_parent'       => strval( $post['post_parent'] ),
724                         'post_mime_type'    => $post['post_mime_type'],
725                         'link'              => post_permalink( $post['ID'] ),
726                         'guid'              => $post['guid'],
727                         'menu_order'        => intval( $post['menu_order'] ),
728                         'comment_status'    => $post['comment_status'],
729                         'ping_status'       => $post['ping_status'],
730                         'sticky'            => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
731                 );
732
733                 // Thumbnail
734                 $post_fields['post_thumbnail'] = array();
735                 $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
736                 if ( $thumbnail_id ) {
737                         $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
738                         $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
739                 }
740
741                 // Consider future posts as published
742                 if ( $post_fields['post_status'] === 'future' )
743                         $post_fields['post_status'] = 'publish';
744
745                 // Fill in blank post format
746                 $post_fields['post_format'] = get_post_format( $post['ID'] );
747                 if ( empty( $post_fields['post_format'] ) )
748                         $post_fields['post_format'] = 'standard';
749
750                 // Merge requested $post_fields fields into $_post
751                 if ( in_array( 'post', $fields ) ) {
752                         $_post = array_merge( $_post, $post_fields );
753                 } else {
754                         $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
755                         $_post = array_merge( $_post, $requested_fields );
756                 }
757
758                 $all_taxonomy_fields = in_array( 'taxonomies', $fields );
759
760                 if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
761                         $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
762                         $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
763                         $_post['terms'] = array();
764                         foreach ( $terms as $term ) {
765                                 $_post['terms'][] = $this->_prepare_term( $term );
766                         }
767                 }
768
769                 if ( in_array( 'custom_fields', $fields ) )
770                         $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
771
772                 if ( in_array( 'enclosure', $fields ) ) {
773                         $_post['enclosure'] = array();
774                         $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
775                         if ( ! empty( $enclosures ) ) {
776                                 $encdata = explode( "\n", $enclosures[0] );
777                                 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
778                                 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
779                                 $_post['enclosure']['type'] = trim( $encdata[2] );
780                         }
781                 }
782
783                 /**
784                  * Filter XML-RPC-prepared date for the given post.
785                  *
786                  * @since 3.4.0
787                  *
788                  * @param array $_post  An array of modified post data.
789                  * @param array $post   An array of post data.
790                  * @param array $fields An array of post fields.
791                  */
792                 return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
793         }
794
795         /**
796          * Prepares post data for return in an XML-RPC object.
797          *
798          * @access protected
799          *
800          * @param object $post_type Post type object
801          * @param array $fields The subset of post fields to return
802          * @return array The prepared post type data
803          */
804         protected function _prepare_post_type( $post_type, $fields ) {
805                 $_post_type = array(
806                         'name' => $post_type->name,
807                         'label' => $post_type->label,
808                         'hierarchical' => (bool) $post_type->hierarchical,
809                         'public' => (bool) $post_type->public,
810                         'show_ui' => (bool) $post_type->show_ui,
811                         '_builtin' => (bool) $post_type->_builtin,
812                         'has_archive' => (bool) $post_type->has_archive,
813                         'supports' => get_all_post_type_supports( $post_type->name ),
814                 );
815
816                 if ( in_array( 'labels', $fields ) ) {
817                         $_post_type['labels'] = (array) $post_type->labels;
818                 }
819
820                 if ( in_array( 'cap', $fields ) ) {
821                         $_post_type['cap'] = (array) $post_type->cap;
822                         $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
823                 }
824
825                 if ( in_array( 'menu', $fields ) ) {
826                         $_post_type['menu_position'] = (int) $post_type->menu_position;
827                         $_post_type['menu_icon'] = $post_type->menu_icon;
828                         $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
829                 }
830
831                 if ( in_array( 'taxonomies', $fields ) )
832                         $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
833
834                 /**
835                  * Filter XML-RPC-prepared date for the given post type.
836                  *
837                  * @since 3.4.0
838                  *
839                  * @param array  $_post_type An array of post type data.
840                  * @param object $post_type  Post type object.
841                  */
842                 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
843         }
844
845         /**
846          * Prepares media item data for return in an XML-RPC object.
847          *
848          * @access protected
849          *
850          * @param object $media_item The unprepared media item data
851          * @param string $thumbnail_size The image size to use for the thumbnail URL
852          * @return array The prepared media item data
853          */
854         protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
855                 $_media_item = array(
856                         'attachment_id'    => strval( $media_item->ID ),
857                         'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
858                         'parent'           => $media_item->post_parent,
859                         'link'             => wp_get_attachment_url( $media_item->ID ),
860                         'title'            => $media_item->post_title,
861                         'caption'          => $media_item->post_excerpt,
862                         'description'      => $media_item->post_content,
863                         'metadata'         => wp_get_attachment_metadata( $media_item->ID ),
864                 );
865
866                 $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
867                 if ( $thumbnail_src )
868                         $_media_item['thumbnail'] = $thumbnail_src[0];
869                 else
870                         $_media_item['thumbnail'] = $_media_item['link'];
871
872                 /**
873                  * Filter XML-RPC-prepared data for the given media item.
874                  *
875                  * @since 3.4.0
876                  *
877                  * @param array  $_media_item    An array of media item data.
878                  * @param object $media_item     Media item object.
879                  * @param string $thumbnail_size Image size.
880                  */
881                 return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
882         }
883
884         /**
885          * Prepares page data for return in an XML-RPC object.
886          *
887          * @access protected
888          *
889          * @param object $page The unprepared page data
890          * @return array The prepared page data
891          */
892         protected function _prepare_page( $page ) {
893                 // Get all of the page content and link.
894                 $full_page = get_extended( $page->post_content );
895                 $link = post_permalink( $page->ID );
896
897                 // Get info the page parent if there is one.
898                 $parent_title = "";
899                 if ( ! empty( $page->post_parent ) ) {
900                         $parent = get_post( $page->post_parent );
901                         $parent_title = $parent->post_title;
902                 }
903
904                 // Determine comment and ping settings.
905                 $allow_comments = comments_open( $page->ID ) ? 1 : 0;
906                 $allow_pings = pings_open( $page->ID ) ? 1 : 0;
907
908                 // Format page date.
909                 $page_date = $this->_convert_date( $page->post_date );
910                 $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
911
912                 // Pull the categories info together.
913                 $categories = array();
914                 foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
915                         $categories[] = get_cat_name( $cat_id );
916                 }
917
918                 // Get the author info.
919                 $author = get_userdata( $page->post_author );
920
921                 $page_template = get_page_template_slug( $page->ID );
922                 if ( empty( $page_template ) )
923                         $page_template = 'default';
924
925                 $_page = array(
926                         'dateCreated'            => $page_date,
927                         'userid'                 => $page->post_author,
928                         'page_id'                => $page->ID,
929                         'page_status'            => $page->post_status,
930                         'description'            => $full_page['main'],
931                         'title'                  => $page->post_title,
932                         'link'                   => $link,
933                         'permaLink'              => $link,
934                         'categories'             => $categories,
935                         'excerpt'                => $page->post_excerpt,
936                         'text_more'              => $full_page['extended'],
937                         'mt_allow_comments'      => $allow_comments,
938                         'mt_allow_pings'         => $allow_pings,
939                         'wp_slug'                => $page->post_name,
940                         'wp_password'            => $page->post_password,
941                         'wp_author'              => $author->display_name,
942                         'wp_page_parent_id'      => $page->post_parent,
943                         'wp_page_parent_title'   => $parent_title,
944                         'wp_page_order'          => $page->menu_order,
945                         'wp_author_id'           => (string) $author->ID,
946                         'wp_author_display_name' => $author->display_name,
947                         'date_created_gmt'       => $page_date_gmt,
948                         'custom_fields'          => $this->get_custom_fields( $page->ID ),
949                         'wp_page_template'       => $page_template
950                 );
951
952                 /**
953                  * Filter XML-RPC-prepared data for the given page.
954                  *
955                  * @since 3.4.0
956                  *
957                  * @param array   $_page An array of page data.
958                  * @param WP_Post $page  Page object.
959                  */
960                 return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
961         }
962
963         /**
964          * Prepares comment data for return in an XML-RPC object.
965          *
966          * @access protected
967          *
968          * @param object $comment The unprepared comment data
969          * @return array The prepared comment data
970          */
971         protected function _prepare_comment( $comment ) {
972                 // Format page date.
973                 $comment_date = $this->_convert_date( $comment->comment_date );
974                 $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
975
976                 if ( '0' == $comment->comment_approved )
977                         $comment_status = 'hold';
978                 else if ( 'spam' == $comment->comment_approved )
979                         $comment_status = 'spam';
980                 else if ( '1' == $comment->comment_approved )
981                         $comment_status = 'approve';
982                 else
983                         $comment_status = $comment->comment_approved;
984
985                 $_comment = array(
986                         'date_created_gmt' => $comment_date_gmt,
987                         'user_id'          => $comment->user_id,
988                         'comment_id'       => $comment->comment_ID,
989                         'parent'           => $comment->comment_parent,
990                         'status'           => $comment_status,
991                         'content'          => $comment->comment_content,
992                         'link'             => get_comment_link($comment),
993                         'post_id'          => $comment->comment_post_ID,
994                         'post_title'       => get_the_title($comment->comment_post_ID),
995                         'author'           => $comment->comment_author,
996                         'author_url'       => $comment->comment_author_url,
997                         'author_email'     => $comment->comment_author_email,
998                         'author_ip'        => $comment->comment_author_IP,
999                         'type'             => $comment->comment_type,
1000                 );
1001
1002                 /**
1003                  * Filter XML-RPC-prepared data for the given comment.
1004                  *
1005                  * @since 3.4.0
1006                  *
1007                  * @param array  $_comment An array of prepared comment data.
1008                  * @param object $comment  Comment object.
1009                  */
1010                 return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
1011         }
1012
1013         /**
1014          * Prepares user data for return in an XML-RPC object.
1015          *
1016          * @access protected
1017          *
1018          * @param WP_User $user The unprepared user object
1019          * @param array $fields The subset of user fields to return
1020          * @return array The prepared user data
1021          */
1022         protected function _prepare_user( $user, $fields ) {
1023                 $_user = array( 'user_id' => strval( $user->ID ) );
1024
1025                 $user_fields = array(
1026                         'username'          => $user->user_login,
1027                         'first_name'        => $user->user_firstname,
1028                         'last_name'         => $user->user_lastname,
1029                         'registered'        => $this->_convert_date( $user->user_registered ),
1030                         'bio'               => $user->user_description,
1031                         'email'             => $user->user_email,
1032                         'nickname'          => $user->nickname,
1033                         'nicename'          => $user->user_nicename,
1034                         'url'               => $user->user_url,
1035                         'display_name'      => $user->display_name,
1036                         'roles'             => $user->roles,
1037                 );
1038
1039                 if ( in_array( 'all', $fields ) ) {
1040                         $_user = array_merge( $_user, $user_fields );
1041                 } else {
1042                         if ( in_array( 'basic', $fields ) ) {
1043                                 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
1044                                 $fields = array_merge( $fields, $basic_fields );
1045                         }
1046                         $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
1047                         $_user = array_merge( $_user, $requested_fields );
1048                 }
1049
1050                 /**
1051                  * Filter XML-RPC-prepared data for the given user.
1052                  *
1053                  * @since 3.5.0
1054                  *
1055                  * @param array   $_user  An array of user data.
1056                  * @param WP_User $user   User object.
1057                  * @param array   $fields An array of user fields.
1058                  */
1059                 return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
1060         }
1061
1062         /**
1063          * Create a new post for any registered post type.
1064          *
1065          * @since 3.4.0
1066          *
1067          * @param array $args Method parameters. Contains:
1068          *  - int     $blog_id
1069          *  - string  $username
1070          *  - string  $password
1071          *  - array   $content_struct
1072          *      $content_struct can contain:
1073          *      - post_type (default: 'post')
1074          *      - post_status (default: 'draft')
1075          *      - post_title
1076          *      - post_author
1077          *      - post_excerpt
1078          *      - post_content
1079          *      - post_date_gmt | post_date
1080          *      - post_format
1081          *      - post_password
1082          *      - comment_status - can be 'open' | 'closed'
1083          *      - ping_status - can be 'open' | 'closed'
1084          *      - sticky
1085          *      - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
1086          *      - custom_fields - array, with each element containing 'key' and 'value'
1087          *      - terms - array, with taxonomy names as keys and arrays of term IDs as values
1088          *      - terms_names - array, with taxonomy names as keys and arrays of term names as values
1089          *      - enclosure
1090          *      - any other fields supported by wp_insert_post()
1091          * @return string post_id
1092          */
1093         public function wp_newPost( $args ) {
1094                 if ( ! $this->minimum_args( $args, 4 ) )
1095                         return $this->error;
1096
1097                 $this->escape( $args );
1098
1099                 $blog_id        = (int) $args[0];
1100                 $username       = $args[1];
1101                 $password       = $args[2];
1102                 $content_struct = $args[3];
1103
1104                 if ( ! $user = $this->login( $username, $password ) )
1105                         return $this->error;
1106
1107                 // convert the date field back to IXR form
1108                 if ( isset( $content_struct['post_date'] ) && ! is_a( $content_struct['post_date'], 'IXR_Date' ) ) {
1109                         $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
1110                 }
1111
1112                 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1113                 // since _insert_post will ignore the non-GMT date if the GMT date is set
1114                 if ( isset( $content_struct['post_date_gmt'] ) && ! is_a( $content_struct['post_date_gmt'], 'IXR_Date' ) ) {
1115                         if ( $content_struct['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) {
1116                                 unset( $content_struct['post_date_gmt'] );
1117                         } else {
1118                                 $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
1119                         }
1120                 }
1121
1122                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1123                 do_action( 'xmlrpc_call', 'wp.newPost' );
1124
1125                 unset( $content_struct['ID'] );
1126
1127                 return $this->_insert_post( $user, $content_struct );
1128         }
1129
1130         /**
1131          * Helper method for filtering out elements from an array.
1132          *
1133          * @since 3.4.0
1134          *
1135          * @param int $count Number to compare to one.
1136          */
1137         private function _is_greater_than_one( $count ) {
1138                 return $count > 1;
1139         }
1140
1141         /**
1142          * Helper method for wp_newPost and wp_editPost, containing shared logic.
1143          *
1144          * @since 3.4.0
1145          * @uses wp_insert_post()
1146          *
1147          * @param WP_User $user The post author if post_author isn't set in $content_struct.
1148          * @param array $content_struct Post data to insert.
1149          */
1150         protected function _insert_post( $user, $content_struct ) {
1151                 $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
1152                         'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
1153
1154                 $post_data = wp_parse_args( $content_struct, $defaults );
1155
1156                 $post_type = get_post_type_object( $post_data['post_type'] );
1157                 if ( ! $post_type )
1158                         return new IXR_Error( 403, __( 'Invalid post type' ) );
1159
1160                 $update = ! empty( $post_data['ID'] );
1161
1162                 if ( $update ) {
1163                         if ( ! get_post( $post_data['ID'] ) )
1164                                 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
1165                         if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
1166                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
1167                         if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
1168                                 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
1169                 } else {
1170                         if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
1171                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
1172                 }
1173
1174                 switch ( $post_data['post_status'] ) {
1175                         case 'draft':
1176                         case 'pending':
1177                                 break;
1178                         case 'private':
1179                                 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1180                                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
1181                                 break;
1182                         case 'publish':
1183                         case 'future':
1184                                 if ( ! current_user_can( $post_type->cap->publish_posts ) )
1185                                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
1186                                 break;
1187                         default:
1188                                 if ( ! get_post_status_object( $post_data['post_status'] ) )
1189                                         $post_data['post_status'] = 'draft';
1190                         break;
1191                 }
1192
1193                 if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
1194                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
1195
1196                 $post_data['post_author'] = absint( $post_data['post_author'] );
1197                 if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
1198                         if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1199                                 return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
1200
1201                         $author = get_userdata( $post_data['post_author'] );
1202
1203                         if ( ! $author )
1204                                 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
1205                 } else {
1206                         $post_data['post_author'] = $user->ID;
1207                 }
1208
1209                 if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
1210                         unset( $post_data['comment_status'] );
1211
1212                 if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
1213                         unset( $post_data['ping_status'] );
1214
1215                 // Do some timestamp voodoo
1216                 if ( ! empty( $post_data['post_date_gmt'] ) ) {
1217                         // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1218                         $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
1219                 } elseif ( ! empty( $post_data['post_date'] ) ) {
1220                         $dateCreated = $post_data['post_date']->getIso();
1221                 }
1222
1223                 if ( ! empty( $dateCreated ) ) {
1224                         $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
1225                         $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
1226                 }
1227
1228                 if ( ! isset( $post_data['ID'] ) )
1229                         $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
1230                 $post_ID = $post_data['ID'];
1231
1232                 if ( $post_data['post_type'] == 'post' ) {
1233                         // Private and password-protected posts cannot be stickied.
1234                         if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
1235                                 // Error if the client tried to stick the post, otherwise, silently unstick.
1236                                 if ( ! empty( $post_data['sticky'] ) )
1237                                         return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
1238                                 if ( $update )
1239                                         unstick_post( $post_ID );
1240                         } elseif ( isset( $post_data['sticky'] ) )  {
1241                                 if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
1242                                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
1243                                 if ( $post_data['sticky'] )
1244                                         stick_post( $post_ID );
1245                                 else
1246                                         unstick_post( $post_ID );
1247                         }
1248                 }
1249
1250                 if ( isset( $post_data['post_thumbnail'] ) ) {
1251                         // empty value deletes, non-empty value adds/updates
1252                         if ( ! $post_data['post_thumbnail'] )
1253                                 delete_post_thumbnail( $post_ID );
1254                         elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
1255                                 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
1256                         set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
1257                         unset( $content_struct['post_thumbnail'] );
1258                 }
1259
1260                 if ( isset( $post_data['custom_fields'] ) )
1261                         $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
1262
1263                 if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
1264                         $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
1265
1266                         // accumulate term IDs from terms and terms_names
1267                         $terms = array();
1268
1269                         // first validate the terms specified by ID
1270                         if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
1271                                 $taxonomies = array_keys( $post_data['terms'] );
1272
1273                                 // validating term ids
1274                                 foreach ( $taxonomies as $taxonomy ) {
1275                                         if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1276                                                 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1277
1278                                         if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1279                                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1280
1281                                         $term_ids = $post_data['terms'][$taxonomy];
1282                                         $terms[ $taxonomy ] = array();
1283                                         foreach ( $term_ids as $term_id ) {
1284                                                 $term = get_term_by( 'id', $term_id, $taxonomy );
1285
1286                                                 if ( ! $term )
1287                                                         return new IXR_Error( 403, __( 'Invalid term ID' ) );
1288
1289                                                 $terms[$taxonomy][] = (int) $term_id;
1290                                         }
1291                                 }
1292                         }
1293
1294                         // now validate terms specified by name
1295                         if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
1296                                 $taxonomies = array_keys( $post_data['terms_names'] );
1297
1298                                 foreach ( $taxonomies as $taxonomy ) {
1299                                         if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
1300                                                 return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
1301
1302                                         if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
1303                                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
1304
1305                                         // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
1306                                         $ambiguous_terms = array();
1307                                         if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1308                                                 $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
1309
1310                                                 // count the number of terms with the same name
1311                                                 $tax_term_names_count = array_count_values( $tax_term_names );
1312
1313                                                 // filter out non-ambiguous term names
1314                                                 $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
1315
1316                                                 $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
1317                                         }
1318
1319                                         $term_names = $post_data['terms_names'][$taxonomy];
1320                                         foreach ( $term_names as $term_name ) {
1321                                                 if ( in_array( $term_name, $ambiguous_terms ) )
1322                                                         return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
1323
1324                                                 $term = get_term_by( 'name', $term_name, $taxonomy );
1325
1326                                                 if ( ! $term ) {
1327                                                         // term doesn't exist, so check that the user is allowed to create new terms
1328                                                         if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
1329                                                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
1330
1331                                                         // create the new term
1332                                                         $term_info = wp_insert_term( $term_name, $taxonomy );
1333                                                         if ( is_wp_error( $term_info ) )
1334                                                                 return new IXR_Error( 500, $term_info->get_error_message() );
1335
1336                                                         $terms[$taxonomy][] = (int) $term_info['term_id'];
1337                                                 } else {
1338                                                         $terms[$taxonomy][] = (int) $term->term_id;
1339                                                 }
1340                                         }
1341                                 }
1342                         }
1343
1344                         $post_data['tax_input'] = $terms;
1345                         unset( $post_data['terms'], $post_data['terms_names'] );
1346                 } else {
1347                         // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
1348                         unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
1349                 }
1350
1351                 if ( isset( $post_data['post_format'] ) ) {
1352                         $format = set_post_format( $post_ID, $post_data['post_format'] );
1353
1354                         if ( is_wp_error( $format ) )
1355                                 return new IXR_Error( 500, $format->get_error_message() );
1356
1357                         unset( $post_data['post_format'] );
1358                 }
1359
1360                 // Handle enclosures
1361                 $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1362                 $this->add_enclosure_if_new( $post_ID, $enclosure );
1363
1364                 $this->attach_uploads( $post_ID, $post_data['post_content'] );
1365
1366                 /**
1367                  * Filter post data array to be inserted via XML-RPC.
1368                  *
1369                  * @since 3.4.0
1370                  *
1371                  * @param array $post_data      Parsed array of post data.
1372                  * @param array $content_struct Post data array.
1373                  */
1374                 $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
1375
1376                 $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
1377                 if ( is_wp_error( $post_ID ) )
1378                         return new IXR_Error( 500, $post_ID->get_error_message() );
1379
1380                 if ( ! $post_ID )
1381                         return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
1382
1383                 return strval( $post_ID );
1384         }
1385
1386         /**
1387          * Edit a post for any registered post type.
1388          *
1389          * The $content_struct parameter only needs to contain fields that
1390          * should be changed. All other fields will retain their existing values.
1391          *
1392          * @since 3.4.0
1393          *
1394          * @param array $args Method parameters. Contains:
1395          *  - int     $blog_id
1396          *  - string  $username
1397          *  - string  $password
1398          *  - int     $post_id
1399          *  - array   $content_struct
1400          * @return true on success
1401          */
1402         public function wp_editPost( $args ) {
1403                 if ( ! $this->minimum_args( $args, 5 ) )
1404                         return $this->error;
1405
1406                 $this->escape( $args );
1407
1408                 $blog_id        = (int) $args[0];
1409                 $username       = $args[1];
1410                 $password       = $args[2];
1411                 $post_id        = (int) $args[3];
1412                 $content_struct = $args[4];
1413
1414                 if ( ! $user = $this->login( $username, $password ) )
1415                         return $this->error;
1416
1417                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1418                 do_action( 'xmlrpc_call', 'wp.editPost' );
1419
1420                 $post = get_post( $post_id, ARRAY_A );
1421
1422                 if ( empty( $post['ID'] ) )
1423                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1424
1425                 if ( isset( $content_struct['if_not_modified_since'] ) ) {
1426                         // If the post has been modified since the date provided, return an error.
1427                         if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
1428                                 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
1429                         }
1430                 }
1431
1432                 // convert the date field back to IXR form
1433                 $post['post_date'] = $this->_convert_date( $post['post_date'] );
1434
1435                 // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
1436                 // since _insert_post will ignore the non-GMT date if the GMT date is set
1437                 if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
1438                         unset( $post['post_date_gmt'] );
1439                 else
1440                         $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
1441
1442                 $this->escape( $post );
1443                 $merged_content_struct = array_merge( $post, $content_struct );
1444
1445                 $retval = $this->_insert_post( $user, $merged_content_struct );
1446                 if ( $retval instanceof IXR_Error )
1447                         return $retval;
1448
1449                 return true;
1450         }
1451
1452         /**
1453          * Delete a post for any registered post type.
1454          *
1455          * @since 3.4.0
1456          *
1457          * @uses wp_delete_post()
1458          * @param array $args Method parameters. Contains:
1459          *  - int     $blog_id
1460          *  - string  $username
1461          *  - string  $password
1462          *  - int     $post_id
1463          * @return true on success
1464          */
1465         public function wp_deletePost( $args ) {
1466                 if ( ! $this->minimum_args( $args, 4 ) )
1467                         return $this->error;
1468
1469                 $this->escape( $args );
1470
1471                 $blog_id    = (int) $args[0];
1472                 $username   = $args[1];
1473                 $password   = $args[2];
1474                 $post_id    = (int) $args[3];
1475
1476                 if ( ! $user = $this->login( $username, $password ) )
1477                         return $this->error;
1478
1479                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1480                 do_action( 'xmlrpc_call', 'wp.deletePost' );
1481
1482                 $post = get_post( $post_id, ARRAY_A );
1483                 if ( empty( $post['ID'] ) )
1484                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1485
1486                 if ( ! current_user_can( 'delete_post', $post_id ) )
1487                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
1488
1489                 $result = wp_delete_post( $post_id );
1490
1491                 if ( ! $result )
1492                         return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
1493
1494                 return true;
1495         }
1496
1497         /**
1498          * Retrieve a post.
1499          *
1500          * @since 3.4.0
1501          *
1502          * The optional $fields parameter specifies what fields will be included
1503          * in the response array. This should be a list of field names. 'post_id' will
1504          * always be included in the response regardless of the value of $fields.
1505          *
1506          * Instead of, or in addition to, individual field names, conceptual group
1507          * names can be used to specify multiple fields. The available conceptual
1508          * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
1509          * and 'enclosure'.
1510          *
1511          * @uses get_post()
1512          * @param array $args Method parameters. Contains:
1513          *  - int     $post_id
1514          *  - string  $username
1515          *  - string  $password
1516          *  - array   $fields optional
1517          * @return array contains (based on $fields parameter):
1518          *  - 'post_id'
1519          *  - 'post_title'
1520          *  - 'post_date'
1521          *  - 'post_date_gmt'
1522          *  - 'post_modified'
1523          *  - 'post_modified_gmt'
1524          *  - 'post_status'
1525          *  - 'post_type'
1526          *  - 'post_name'
1527          *  - 'post_author'
1528          *  - 'post_password'
1529          *  - 'post_excerpt'
1530          *  - 'post_content'
1531          *  - 'link'
1532          *  - 'comment_status'
1533          *  - 'ping_status'
1534          *  - 'sticky'
1535          *  - 'custom_fields'
1536          *  - 'terms'
1537          *  - 'categories'
1538          *  - 'tags'
1539          *  - 'enclosure'
1540          */
1541         public function wp_getPost( $args ) {
1542                 if ( ! $this->minimum_args( $args, 4 ) )
1543                         return $this->error;
1544
1545                 $this->escape( $args );
1546
1547                 $blog_id            = (int) $args[0];
1548                 $username           = $args[1];
1549                 $password           = $args[2];
1550                 $post_id            = (int) $args[3];
1551
1552                 if ( isset( $args[4] ) ) {
1553                         $fields = $args[4];
1554                 } else {
1555                         /**
1556                          * Filter the list of post query fields used by the given XML-RPC method.
1557                          *
1558                          * @since 3.4.0
1559                          *
1560                          * @param array $fields  Array of post fields.
1561                          * @param string $method Method name.
1562                          */
1563                         $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
1564                 }
1565
1566                 if ( ! $user = $this->login( $username, $password ) )
1567                         return $this->error;
1568
1569                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1570                 do_action( 'xmlrpc_call', 'wp.getPost' );
1571
1572                 $post = get_post( $post_id, ARRAY_A );
1573
1574                 if ( empty( $post['ID'] ) )
1575                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
1576
1577                 if ( ! current_user_can( 'edit_post', $post_id ) )
1578                         return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
1579
1580                 return $this->_prepare_post( $post, $fields );
1581         }
1582
1583         /**
1584          * Retrieve posts.
1585          *
1586          * @since 3.4.0
1587          *
1588          * The optional $filter parameter modifies the query used to retrieve posts.
1589          * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
1590          * 'orderby', and 'order'.
1591          *
1592          * The optional $fields parameter specifies what fields will be included
1593          * in the response array.
1594          *
1595          * @uses wp_get_recent_posts()
1596          * @see wp_getPost() for more on $fields
1597          * @see get_posts() for more on $filter values
1598          *
1599          * @param array $args Method parameters. Contains:
1600          *  - int     $blog_id
1601          *  - string  $username
1602          *  - string  $password
1603          *  - array   $filter optional
1604          *  - array   $fields optional
1605          * @return array contains a collection of posts.
1606          */
1607         public function wp_getPosts( $args ) {
1608                 if ( ! $this->minimum_args( $args, 3 ) )
1609                         return $this->error;
1610
1611                 $this->escape( $args );
1612
1613                 $blog_id    = (int) $args[0];
1614                 $username   = $args[1];
1615                 $password   = $args[2];
1616                 $filter     = isset( $args[3] ) ? $args[3] : array();
1617
1618                 if ( isset( $args[4] ) ) {
1619                         $fields = $args[4];
1620                 } else {
1621                         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1622                         $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
1623                 }
1624
1625                 if ( ! $user = $this->login( $username, $password ) )
1626                         return $this->error;
1627
1628                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1629                 do_action( 'xmlrpc_call', 'wp.getPosts' );
1630
1631                 $query = array();
1632
1633                 if ( isset( $filter['post_type'] ) ) {
1634                         $post_type = get_post_type_object( $filter['post_type'] );
1635                         if ( ! ( (bool) $post_type ) )
1636                                 return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
1637                 } else {
1638                         $post_type = get_post_type_object( 'post' );
1639                 }
1640
1641                 if ( ! current_user_can( $post_type->cap->edit_posts ) )
1642                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
1643
1644                 $query['post_type'] = $post_type->name;
1645
1646                 if ( isset( $filter['post_status'] ) )
1647                         $query['post_status'] = $filter['post_status'];
1648
1649                 if ( isset( $filter['number'] ) )
1650                         $query['numberposts'] = absint( $filter['number'] );
1651
1652                 if ( isset( $filter['offset'] ) )
1653                         $query['offset'] = absint( $filter['offset'] );
1654
1655                 if ( isset( $filter['orderby'] ) ) {
1656                         $query['orderby'] = $filter['orderby'];
1657
1658                         if ( isset( $filter['order'] ) )
1659                                 $query['order'] = $filter['order'];
1660                 }
1661
1662                 if ( isset( $filter['s'] ) ) {
1663                         $query['s'] = $filter['s'];
1664                 }
1665
1666                 $posts_list = wp_get_recent_posts( $query );
1667
1668                 if ( ! $posts_list )
1669                         return array();
1670
1671                 // holds all the posts data
1672                 $struct = array();
1673
1674                 foreach ( $posts_list as $post ) {
1675                         if ( ! current_user_can( 'edit_post', $post['ID'] ) )
1676                                 continue;
1677
1678                         $struct[] = $this->_prepare_post( $post, $fields );
1679                 }
1680
1681                 return $struct;
1682         }
1683
1684         /**
1685          * Create a new term.
1686          *
1687          * @since 3.4.0
1688          *
1689          * @uses wp_insert_term()
1690          * @param array $args Method parameters. Contains:
1691          *  - int     $blog_id
1692          *  - string  $username
1693          *  - string  $password
1694          *  - array   $content_struct
1695          *      The $content_struct must contain:
1696          *      - 'name'
1697          *      - 'taxonomy'
1698          *      Also, it can optionally contain:
1699          *      - 'parent'
1700          *      - 'description'
1701          *      - 'slug'
1702          * @return string term_id
1703          */
1704         public function wp_newTerm( $args ) {
1705                 if ( ! $this->minimum_args( $args, 4 ) )
1706                         return $this->error;
1707
1708                 $this->escape( $args );
1709
1710                 $blog_id            = (int) $args[0];
1711                 $username           = $args[1];
1712                 $password           = $args[2];
1713                 $content_struct     = $args[3];
1714
1715                 if ( ! $user = $this->login( $username, $password ) )
1716                         return $this->error;
1717
1718                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1719                 do_action( 'xmlrpc_call', 'wp.newTerm' );
1720
1721                 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1722                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1723
1724                 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1725
1726                 if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
1727                         return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
1728
1729                 $taxonomy = (array) $taxonomy;
1730
1731                 // hold the data of the term
1732                 $term_data = array();
1733
1734                 $term_data['name'] = trim( $content_struct['name'] );
1735                 if ( empty( $term_data['name'] ) )
1736                         return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1737
1738                 if ( isset( $content_struct['parent'] ) ) {
1739                         if ( ! $taxonomy['hierarchical'] )
1740                                 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
1741
1742                         $parent_term_id = (int) $content_struct['parent'];
1743                         $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1744
1745                         if ( is_wp_error( $parent_term ) )
1746                                 return new IXR_Error( 500, $parent_term->get_error_message() );
1747
1748                         if ( ! $parent_term )
1749                                 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1750
1751                         $term_data['parent'] = $content_struct['parent'];
1752                 }
1753
1754                 if ( isset( $content_struct['description'] ) )
1755                         $term_data['description'] = $content_struct['description'];
1756
1757                 if ( isset( $content_struct['slug'] ) )
1758                         $term_data['slug'] = $content_struct['slug'];
1759
1760                 $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
1761
1762                 if ( is_wp_error( $term ) )
1763                         return new IXR_Error( 500, $term->get_error_message() );
1764
1765                 if ( ! $term )
1766                         return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
1767
1768                 return strval( $term['term_id'] );
1769         }
1770
1771         /**
1772          * Edit a term.
1773          *
1774          * @since 3.4.0
1775          *
1776          * @uses wp_update_term()
1777          * @param array $args Method parameters. Contains:
1778          *  - int     $blog_id
1779          *  - string  $username
1780          *  - string  $password
1781          *  - string  $term_id
1782          *  - array   $content_struct
1783          *      The $content_struct must contain:
1784          *      - 'taxonomy'
1785          *      Also, it can optionally contain:
1786          *      - 'name'
1787          *      - 'parent'
1788          *      - 'description'
1789          *      - 'slug'
1790          * @return bool True, on success.
1791          */
1792         public function wp_editTerm( $args ) {
1793                 if ( ! $this->minimum_args( $args, 5 ) )
1794                         return $this->error;
1795
1796                 $this->escape( $args );
1797
1798                 $blog_id            = (int) $args[0];
1799                 $username           = $args[1];
1800                 $password           = $args[2];
1801                 $term_id            = (int) $args[3];
1802                 $content_struct     = $args[4];
1803
1804                 if ( ! $user = $this->login( $username, $password ) )
1805                         return $this->error;
1806
1807                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1808                 do_action( 'xmlrpc_call', 'wp.editTerm' );
1809
1810                 if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
1811                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1812
1813                 $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
1814
1815                 if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
1816                         return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
1817
1818                 $taxonomy = (array) $taxonomy;
1819
1820                 // hold the data of the term
1821                 $term_data = array();
1822
1823                 $term = get_term( $term_id , $content_struct['taxonomy'] );
1824
1825                 if ( is_wp_error( $term ) )
1826                         return new IXR_Error( 500, $term->get_error_message() );
1827
1828                 if ( ! $term )
1829                         return new IXR_Error( 404, __( 'Invalid term ID' ) );
1830
1831                 if ( isset( $content_struct['name'] ) ) {
1832                         $term_data['name'] = trim( $content_struct['name'] );
1833
1834                         if ( empty( $term_data['name'] ) )
1835                                 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
1836                 }
1837
1838                 if ( isset( $content_struct['parent'] ) ) {
1839                         if ( ! $taxonomy['hierarchical'] )
1840                                 return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
1841
1842                         $parent_term_id = (int) $content_struct['parent'];
1843                         $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
1844
1845                         if ( is_wp_error( $parent_term ) )
1846                                 return new IXR_Error( 500, $parent_term->get_error_message() );
1847
1848                         if ( ! $parent_term )
1849                                 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
1850
1851                         $term_data['parent'] = $content_struct['parent'];
1852                 }
1853
1854                 if ( isset( $content_struct['description'] ) )
1855                         $term_data['description'] = $content_struct['description'];
1856
1857                 if ( isset( $content_struct['slug'] ) )
1858                         $term_data['slug'] = $content_struct['slug'];
1859
1860                 $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
1861
1862                 if ( is_wp_error( $term ) )
1863                         return new IXR_Error( 500, $term->get_error_message() );
1864
1865                 if ( ! $term )
1866                         return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
1867
1868                 return true;
1869         }
1870
1871         /**
1872          * Delete a term.
1873          *
1874          * @since 3.4.0
1875          *
1876          * @uses wp_delete_term()
1877          * @param array $args Method parameters. Contains:
1878          *  - int     $blog_id
1879          *  - string  $username
1880          *  - string  $password
1881          *  - string  $taxnomy_name
1882          *  - string     $term_id
1883          * @return boolean|IXR_Error If it suceeded true else a reason why not
1884          */
1885         public function wp_deleteTerm( $args ) {
1886                 if ( ! $this->minimum_args( $args, 5 ) )
1887                         return $this->error;
1888
1889                 $this->escape( $args );
1890
1891                 $blog_id            = (int) $args[0];
1892                 $username           = $args[1];
1893                 $password           = $args[2];
1894                 $taxonomy           = $args[3];
1895                 $term_id            = (int) $args[4];
1896
1897                 if ( ! $user = $this->login( $username, $password ) )
1898                         return $this->error;
1899
1900                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1901                 do_action( 'xmlrpc_call', 'wp.deleteTerm' );
1902
1903                 if ( ! taxonomy_exists( $taxonomy ) )
1904                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1905
1906                 $taxonomy = get_taxonomy( $taxonomy );
1907
1908                 if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
1909                         return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
1910
1911                 $term = get_term( $term_id, $taxonomy->name );
1912
1913                 if ( is_wp_error( $term ) )
1914                         return new IXR_Error( 500, $term->get_error_message() );
1915
1916                 if ( ! $term )
1917                         return new IXR_Error( 404, __( 'Invalid term ID' ) );
1918
1919                 $result = wp_delete_term( $term_id, $taxonomy->name );
1920
1921                 if ( is_wp_error( $result ) )
1922                         return new IXR_Error( 500, $term->get_error_message() );
1923
1924                 if ( ! $result )
1925                         return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
1926
1927                 return $result;
1928         }
1929
1930         /**
1931          * Retrieve a term.
1932          *
1933          * @since 3.4.0
1934          *
1935          * @uses get_term()
1936          * @param array $args Method parameters. Contains:
1937          *  - int     $blog_id
1938          *  - string  $username
1939          *  - string  $password
1940          *  - string  $taxonomy
1941          *  - string  $term_id
1942          * @return array contains:
1943          *  - 'term_id'
1944          *  - 'name'
1945          *  - 'slug'
1946          *  - 'term_group'
1947          *  - 'term_taxonomy_id'
1948          *  - 'taxonomy'
1949          *  - 'description'
1950          *  - 'parent'
1951          *  - 'count'
1952          */
1953         public function wp_getTerm( $args ) {
1954                 if ( ! $this->minimum_args( $args, 5 ) )
1955                         return $this->error;
1956
1957                 $this->escape( $args );
1958
1959                 $blog_id            = (int) $args[0];
1960                 $username           = $args[1];
1961                 $password           = $args[2];
1962                 $taxonomy           = $args[3];
1963                 $term_id            = (int) $args[4];
1964
1965                 if ( ! $user = $this->login( $username, $password ) )
1966                         return $this->error;
1967
1968                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
1969                 do_action( 'xmlrpc_call', 'wp.getTerm' );
1970
1971                 if ( ! taxonomy_exists( $taxonomy ) )
1972                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
1973
1974                 $taxonomy = get_taxonomy( $taxonomy );
1975
1976                 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
1977                         return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
1978
1979                 $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
1980
1981                 if ( is_wp_error( $term ) )
1982                         return new IXR_Error( 500, $term->get_error_message() );
1983
1984                 if ( ! $term )
1985                         return new IXR_Error( 404, __( 'Invalid term ID' ) );
1986
1987                 return $this->_prepare_term( $term );
1988         }
1989
1990         /**
1991          * Retrieve all terms for a taxonomy.
1992          *
1993          * @since 3.4.0
1994          *
1995          * The optional $filter parameter modifies the query used to retrieve terms.
1996          * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
1997          *
1998          * @uses get_terms()
1999          * @param array $args Method parameters. Contains:
2000          *  - int     $blog_id
2001          *  - string  $username
2002          *  - string  $password
2003          *  - string  $taxonomy
2004          *  - array   $filter optional
2005          * @return array terms
2006          */
2007         public function wp_getTerms( $args ) {
2008                 if ( ! $this->minimum_args( $args, 4 ) )
2009                         return $this->error;
2010
2011                 $this->escape( $args );
2012
2013                 $blog_id        = (int) $args[0];
2014                 $username       = $args[1];
2015                 $password       = $args[2];
2016                 $taxonomy       = $args[3];
2017                 $filter         = isset( $args[4] ) ? $args[4] : array();
2018
2019                 if ( ! $user = $this->login( $username, $password ) )
2020                         return $this->error;
2021
2022                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2023                 do_action( 'xmlrpc_call', 'wp.getTerms' );
2024
2025                 if ( ! taxonomy_exists( $taxonomy ) )
2026                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2027
2028                 $taxonomy = get_taxonomy( $taxonomy );
2029
2030                 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2031                         return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2032
2033                 $query = array();
2034
2035                 if ( isset( $filter['number'] ) )
2036                         $query['number'] = absint( $filter['number'] );
2037
2038                 if ( isset( $filter['offset'] ) )
2039                         $query['offset'] = absint( $filter['offset'] );
2040
2041                 if ( isset( $filter['orderby'] ) ) {
2042                         $query['orderby'] = $filter['orderby'];
2043
2044                         if ( isset( $filter['order'] ) )
2045                                 $query['order'] = $filter['order'];
2046                 }
2047
2048                 if ( isset( $filter['hide_empty'] ) )
2049                         $query['hide_empty'] = $filter['hide_empty'];
2050                 else
2051                         $query['get'] = 'all';
2052
2053                 if ( isset( $filter['search'] ) )
2054                         $query['search'] = $filter['search'];
2055
2056                 $terms = get_terms( $taxonomy->name, $query );
2057
2058                 if ( is_wp_error( $terms ) )
2059                         return new IXR_Error( 500, $terms->get_error_message() );
2060
2061                 $struct = array();
2062
2063                 foreach ( $terms as $term ) {
2064                         $struct[] = $this->_prepare_term( $term );
2065                 }
2066
2067                 return $struct;
2068         }
2069
2070         /**
2071          * Retrieve a taxonomy.
2072          *
2073          * @since 3.4.0
2074          *
2075          * @uses get_taxonomy()
2076          * @param array $args Method parameters. Contains:
2077          *  - int     $blog_id
2078          *  - string  $username
2079          *  - string  $password
2080          *  - string  $taxonomy
2081          * @return array (@see get_taxonomy())
2082          */
2083         public function wp_getTaxonomy( $args ) {
2084                 if ( ! $this->minimum_args( $args, 4 ) )
2085                         return $this->error;
2086
2087                 $this->escape( $args );
2088
2089                 $blog_id        = (int) $args[0];
2090                 $username       = $args[1];
2091                 $password       = $args[2];
2092                 $taxonomy       = $args[3];
2093
2094                 if ( isset( $args[4] ) ) {
2095                         $fields = $args[4];
2096                 } else {
2097                         /**
2098                          * Filter the taxonomy query fields used by the given XML-RPC method.
2099                          *
2100                          * @since 3.4.0
2101                          *
2102                          * @param array  $fields An array of taxonomy fields to retrieve.
2103                          * @param string $method The method name.
2104                          */
2105                         $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
2106                 }
2107
2108                 if ( ! $user = $this->login( $username, $password ) )
2109                         return $this->error;
2110
2111                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2112                 do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
2113
2114                 if ( ! taxonomy_exists( $taxonomy ) )
2115                         return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
2116
2117                 $taxonomy = get_taxonomy( $taxonomy );
2118
2119                 if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2120                         return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
2121
2122                 return $this->_prepare_taxonomy( $taxonomy, $fields );
2123         }
2124
2125         /**
2126          * Retrieve all taxonomies.
2127          *
2128          * @since 3.4.0
2129          *
2130          * @uses get_taxonomies()
2131          * @param array $args Method parameters. Contains:
2132          *  - int     $blog_id
2133          *  - string  $username
2134          *  - string  $password
2135          * @return array taxonomies
2136          */
2137         public function wp_getTaxonomies( $args ) {
2138                 if ( ! $this->minimum_args( $args, 3 ) )
2139                         return $this->error;
2140
2141                 $this->escape( $args );
2142
2143                 $blog_id            = (int) $args[0];
2144                 $username           = $args[1];
2145                 $password           = $args[2];
2146                 $filter             = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2147
2148                 if ( isset( $args[4] ) ) {
2149                         $fields = $args[4];
2150                 } else {
2151                         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2152                         $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
2153                 }
2154
2155                 if ( ! $user = $this->login( $username, $password ) )
2156                         return $this->error;
2157
2158                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2159                 do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
2160
2161                 $taxonomies = get_taxonomies( $filter, 'objects' );
2162
2163                 // holds all the taxonomy data
2164                 $struct = array();
2165
2166                 foreach ( $taxonomies as $taxonomy ) {
2167                         // capability check for post_types
2168                         if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
2169                                 continue;
2170
2171                         $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
2172                 }
2173
2174                 return $struct;
2175         }
2176
2177         /**
2178          * Retrieve a user.
2179          *
2180          * The optional $fields parameter specifies what fields will be included
2181          * in the response array. This should be a list of field names. 'user_id' will
2182          * always be included in the response regardless of the value of $fields.
2183          *
2184          * Instead of, or in addition to, individual field names, conceptual group
2185          * names can be used to specify multiple fields. The available conceptual
2186          * groups are 'basic' and 'all'.
2187          *
2188          * @uses get_userdata()
2189          * @param array $args Method parameters. Contains:
2190          *  - int     $blog_id
2191          *  - string  $username
2192          *  - string  $password
2193          *  - int     $user_id
2194          *  - array   $fields optional
2195          * @return array contains (based on $fields parameter):
2196          *  - 'user_id'
2197          *  - 'username'
2198          *  - 'first_name'
2199          *  - 'last_name'
2200          *  - 'registered'
2201          *  - 'bio'
2202          *  - 'email'
2203          *  - 'nickname'
2204          *  - 'nicename'
2205          *  - 'url'
2206          *  - 'display_name'
2207          *  - 'roles'
2208          */
2209         public function wp_getUser( $args ) {
2210                 if ( ! $this->minimum_args( $args, 4 ) )
2211                         return $this->error;
2212
2213                 $this->escape( $args );
2214
2215                 $blog_id    = (int) $args[0];
2216                 $username   = $args[1];
2217                 $password   = $args[2];
2218                 $user_id    = (int) $args[3];
2219
2220                 if ( isset( $args[4] ) ) {
2221                         $fields = $args[4];
2222                 } else {
2223                         /**
2224                          * Filter the default user query fields used by the given XML-RPC method.
2225                          *
2226                          * @since 3.5.0
2227                          *
2228                          * @param array  $fields User query fields for given method. Default 'all'.
2229                          * @param string $method The method name.
2230                          */
2231                         $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
2232                 }
2233
2234                 if ( ! $user = $this->login( $username, $password ) )
2235                         return $this->error;
2236
2237                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2238                 do_action( 'xmlrpc_call', 'wp.getUser' );
2239
2240                 if ( ! current_user_can( 'edit_user', $user_id ) )
2241                         return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
2242
2243                 $user_data = get_userdata( $user_id );
2244
2245                 if ( ! $user_data )
2246                         return new IXR_Error( 404, __( 'Invalid user ID' ) );
2247
2248                 return $this->_prepare_user( $user_data, $fields );
2249         }
2250
2251         /**
2252          * Retrieve users.
2253          *
2254          * The optional $filter parameter modifies the query used to retrieve users.
2255          * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
2256          * 'who', 'orderby', and 'order'.
2257          *
2258          * The optional $fields parameter specifies what fields will be included
2259          * in the response array.
2260          *
2261          * @uses get_users()
2262          * @see wp_getUser() for more on $fields and return values
2263          *
2264          * @param array $args Method parameters. Contains:
2265          *  - int     $blog_id
2266          *  - string  $username
2267          *  - string  $password
2268          *  - array   $filter optional
2269          *  - array   $fields optional
2270          * @return array users data
2271          */
2272         public function wp_getUsers( $args ) {
2273                 if ( ! $this->minimum_args( $args, 3 ) )
2274                         return $this->error;
2275
2276                 $this->escape( $args );
2277
2278                 $blog_id    = (int) $args[0];
2279                 $username   = $args[1];
2280                 $password   = $args[2];
2281                 $filter     = isset( $args[3] ) ? $args[3] : array();
2282
2283                 if ( isset( $args[4] ) ) {
2284                         $fields = $args[4];
2285                 } else {
2286                         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2287                         $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
2288                 }
2289
2290                 if ( ! $user = $this->login( $username, $password ) )
2291                         return $this->error;
2292
2293                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2294                 do_action( 'xmlrpc_call', 'wp.getUsers' );
2295
2296                 if ( ! current_user_can( 'list_users' ) )
2297                         return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
2298
2299                 $query = array( 'fields' => 'all_with_meta' );
2300
2301                 $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
2302                 $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
2303
2304                 if ( isset( $filter['orderby'] ) ) {
2305                         $query['orderby'] = $filter['orderby'];
2306
2307                         if ( isset( $filter['order'] ) )
2308                                 $query['order'] = $filter['order'];
2309                 }
2310
2311                 if ( isset( $filter['role'] ) ) {
2312                         if ( get_role( $filter['role'] ) === null )
2313                                 return new IXR_Error( 403, __( 'The role specified is not valid' ) );
2314
2315                         $query['role'] = $filter['role'];
2316                 }
2317
2318                 if ( isset( $filter['who'] ) ) {
2319                         $query['who'] = $filter['who'];
2320                 }
2321
2322                 $users = get_users( $query );
2323
2324                 $_users = array();
2325                 foreach ( $users as $user_data ) {
2326                         if ( current_user_can( 'edit_user', $user_data->ID ) )
2327                                 $_users[] = $this->_prepare_user( $user_data, $fields );
2328                 }
2329                 return $_users;
2330         }
2331
2332         /**
2333          * Retrieve information about the requesting user.
2334          *
2335          * @uses get_userdata()
2336          * @param array $args Method parameters. Contains:
2337          *  - int     $blog_id
2338          *  - string  $username
2339          *  - string  $password
2340          *  - array   $fields optional
2341          * @return array (@see wp_getUser)
2342          */
2343         public function wp_getProfile( $args ) {
2344                 if ( ! $this->minimum_args( $args, 3 ) )
2345                         return $this->error;
2346
2347                 $this->escape( $args );
2348
2349                 $blog_id    = (int) $args[0];
2350                 $username   = $args[1];
2351                 $password   = $args[2];
2352
2353                 if ( isset( $args[3] ) ) {
2354                         $fields = $args[3];
2355                 } else {
2356                         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2357                         $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
2358                 }
2359
2360                 if ( ! $user = $this->login( $username, $password ) )
2361                         return $this->error;
2362
2363                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2364                 do_action( 'xmlrpc_call', 'wp.getProfile' );
2365
2366                 if ( ! current_user_can( 'edit_user', $user->ID ) )
2367                         return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2368
2369                 $user_data = get_userdata( $user->ID );
2370
2371                 return $this->_prepare_user( $user_data, $fields );
2372         }
2373
2374         /**
2375          * Edit user's profile.
2376          *
2377          * @uses wp_update_user()
2378          * @param array $args Method parameters. Contains:
2379          *  - int     $blog_id
2380          *  - string  $username
2381          *  - string  $password
2382          *  - array   $content_struct
2383          *      It can optionally contain:
2384          *      - 'first_name'
2385          *      - 'last_name'
2386          *      - 'website'
2387          *      - 'display_name'
2388          *      - 'nickname'
2389          *      - 'nicename'
2390          *      - 'bio'
2391          * @return bool True, on success.
2392          */
2393         public function wp_editProfile( $args ) {
2394                 if ( ! $this->minimum_args( $args, 4 ) )
2395                         return $this->error;
2396
2397                 $this->escape( $args );
2398
2399                 $blog_id        = (int) $args[0];
2400                 $username       = $args[1];
2401                 $password       = $args[2];
2402                 $content_struct = $args[3];
2403
2404                 if ( ! $user = $this->login( $username, $password ) )
2405                         return $this->error;
2406
2407                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2408                 do_action( 'xmlrpc_call', 'wp.editProfile' );
2409
2410                 if ( ! current_user_can( 'edit_user', $user->ID ) )
2411                         return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
2412
2413                 // holds data of the user
2414                 $user_data = array();
2415                 $user_data['ID'] = $user->ID;
2416
2417                 // only set the user details if it was given
2418                 if ( isset( $content_struct['first_name'] ) )
2419                         $user_data['first_name'] = $content_struct['first_name'];
2420
2421                 if ( isset( $content_struct['last_name'] ) )
2422                         $user_data['last_name'] = $content_struct['last_name'];
2423
2424                 if ( isset( $content_struct['url'] ) )
2425                         $user_data['user_url'] = $content_struct['url'];
2426
2427                 if ( isset( $content_struct['display_name'] ) )
2428                         $user_data['display_name'] = $content_struct['display_name'];
2429
2430                 if ( isset( $content_struct['nickname'] ) )
2431                         $user_data['nickname'] = $content_struct['nickname'];
2432
2433                 if ( isset( $content_struct['nicename'] ) )
2434                         $user_data['user_nicename'] = $content_struct['nicename'];
2435
2436                 if ( isset( $content_struct['bio'] ) )
2437                         $user_data['description'] = $content_struct['bio'];
2438
2439                 $result = wp_update_user( $user_data );
2440
2441                 if ( is_wp_error( $result ) )
2442                         return new IXR_Error( 500, $result->get_error_message() );
2443
2444                 if ( ! $result )
2445                         return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
2446
2447                 return true;
2448         }
2449
2450         /**
2451          * Retrieve page.
2452          *
2453          * @since 2.2.0
2454          *
2455          * @param array $args Method parameters. Contains:
2456          *  - blog_id
2457          *  - page_id
2458          *  - username
2459          *  - password
2460          * @return array
2461          */
2462         public function wp_getPage($args) {
2463                 $this->escape($args);
2464
2465                 $blog_id        = (int) $args[0];
2466                 $page_id        = (int) $args[1];
2467                 $username       = $args[2];
2468                 $password       = $args[3];
2469
2470                 if ( !$user = $this->login($username, $password) ) {
2471                         return $this->error;
2472                 }
2473
2474                 $page = get_post($page_id);
2475                 if ( ! $page )
2476                         return new IXR_Error( 404, __( 'Invalid post ID.' ) );
2477
2478                 if ( !current_user_can( 'edit_page', $page_id ) )
2479                         return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
2480
2481                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2482                 do_action( 'xmlrpc_call', 'wp.getPage' );
2483
2484                 // If we found the page then format the data.
2485                 if ( $page->ID && ($page->post_type == 'page') ) {
2486                         return $this->_prepare_page( $page );
2487                 }
2488                 // If the page doesn't exist indicate that.
2489                 else {
2490                         return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
2491                 }
2492         }
2493
2494         /**
2495          * Retrieve Pages.
2496          *
2497          * @since 2.2.0
2498          *
2499          * @param array $args Method parameters. Contains:
2500          *  - blog_id
2501          *  - username
2502          *  - password
2503          *  - num_pages
2504          * @return array
2505          */
2506         public function wp_getPages($args) {
2507                 $this->escape($args);
2508
2509                 $blog_id        = (int) $args[0];
2510                 $username       = $args[1];
2511                 $password       = $args[2];
2512                 $num_pages      = isset($args[3]) ? (int) $args[3] : 10;
2513
2514                 if ( !$user = $this->login($username, $password) )
2515                         return $this->error;
2516
2517                 if ( !current_user_can( 'edit_pages' ) )
2518                         return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2519
2520                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2521                 do_action( 'xmlrpc_call', 'wp.getPages' );
2522
2523                 $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
2524                 $num_pages = count($pages);
2525
2526                 // If we have pages, put together their info.
2527                 if ( $num_pages >= 1 ) {
2528                         $pages_struct = array();
2529
2530                         foreach ($pages as $page) {
2531                                 if ( current_user_can( 'edit_page', $page->ID ) )
2532                                         $pages_struct[] = $this->_prepare_page( $page );
2533                         }
2534
2535                         return($pages_struct);
2536                 }
2537                 // If no pages were found return an error.
2538                 else {
2539                         return(array());
2540                 }
2541         }
2542
2543         /**
2544          * Create new page.
2545          *
2546          * @since 2.2.0
2547          *
2548          * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
2549          * @return unknown
2550          */
2551         public function wp_newPage($args) {
2552                 // Items not escaped here will be escaped in newPost.
2553                 $username       = $this->escape($args[1]);
2554                 $password       = $this->escape($args[2]);
2555                 $page           = $args[3];
2556                 $publish        = $args[4];
2557
2558                 if ( !$user = $this->login($username, $password) )
2559                         return $this->error;
2560
2561                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2562                 do_action( 'xmlrpc_call', 'wp.newPage' );
2563
2564                 // Mark this as content for a page.
2565                 $args[3]["post_type"] = 'page';
2566
2567                 // Let mw_newPost do all of the heavy lifting.
2568                 return($this->mw_newPost($args));
2569         }
2570
2571         /**
2572          * Delete page.
2573          *
2574          * @since 2.2.0
2575          *
2576          * @param array $args Method parameters.
2577          * @return bool True, if success.
2578          */
2579         public function wp_deletePage($args) {
2580                 $this->escape($args);
2581
2582                 $blog_id        = (int) $args[0];
2583                 $username       = $args[1];
2584                 $password       = $args[2];
2585                 $page_id        = (int) $args[3];
2586
2587                 if ( !$user = $this->login($username, $password) )
2588                         return $this->error;
2589
2590                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2591                 do_action( 'xmlrpc_call', 'wp.deletePage' );
2592
2593                 // Get the current page based on the page_id and
2594                 // make sure it is a page and not a post.
2595                 $actual_page = get_post($page_id, ARRAY_A);
2596                 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2597                         return(new IXR_Error(404, __('Sorry, no such page.')));
2598
2599                 // Make sure the user can delete pages.
2600                 if ( !current_user_can('delete_page', $page_id) )
2601                         return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
2602
2603                 // Attempt to delete the page.
2604                 $result = wp_delete_post($page_id);
2605                 if ( !$result )
2606                         return(new IXR_Error(500, __('Failed to delete the page.')));
2607
2608                 /**
2609                  * Fires after a page has been successfully deleted via XML-RPC.
2610                  *
2611                  * @since 3.4.0
2612                  *
2613                  * @param int   $page_id ID of the deleted page.
2614                  * @param array $args    An array of arguments to delete the page.
2615                  */
2616                 do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
2617
2618                 return(true);
2619         }
2620
2621         /**
2622          * Edit page.
2623          *
2624          * @since 2.2.0
2625          *
2626          * @param array $args Method parameters.
2627          * @return unknown
2628          */
2629         public function wp_editPage($args) {
2630                 // Items not escaped here will be escaped in editPost.
2631                 $blog_id        = (int) $args[0];
2632                 $page_id        = (int) $this->escape($args[1]);
2633                 $username       = $this->escape($args[2]);
2634                 $password       = $this->escape($args[3]);
2635                 $content        = $args[4];
2636                 $publish        = $args[5];
2637
2638                 if ( !$user = $this->login($username, $password) )
2639                         return $this->error;
2640
2641                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2642                 do_action( 'xmlrpc_call', 'wp.editPage' );
2643
2644                 // Get the page data and make sure it is a page.
2645                 $actual_page = get_post($page_id, ARRAY_A);
2646                 if ( !$actual_page || ($actual_page['post_type'] != 'page') )
2647                         return(new IXR_Error(404, __('Sorry, no such page.')));
2648
2649                 // Make sure the user is allowed to edit pages.
2650                 if ( !current_user_can('edit_page', $page_id) )
2651                         return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
2652
2653                 // Mark this as content for a page.
2654                 $content['post_type'] = 'page';
2655
2656                 // Arrange args in the way mw_editPost understands.
2657                 $args = array(
2658                         $page_id,
2659                         $username,
2660                         $password,
2661                         $content,
2662                         $publish
2663                 );
2664
2665                 // Let mw_editPost do all of the heavy lifting.
2666                 return($this->mw_editPost($args));
2667         }
2668
2669         /**
2670          * Retrieve page list.
2671          *
2672          * @since 2.2.0
2673          *
2674          * @param array $args Method parameters.
2675          * @return unknown
2676          */
2677         public function wp_getPageList($args) {
2678                 global $wpdb;
2679
2680                 $this->escape($args);
2681
2682                 $blog_id                                = (int) $args[0];
2683                 $username                               = $args[1];
2684                 $password                               = $args[2];
2685
2686                 if ( !$user = $this->login($username, $password) )
2687                         return $this->error;
2688
2689                 if ( !current_user_can( 'edit_pages' ) )
2690                         return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
2691
2692                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2693                 do_action( 'xmlrpc_call', 'wp.getPageList' );
2694
2695                 // Get list of pages ids and titles
2696                 $page_list = $wpdb->get_results("
2697                         SELECT ID page_id,
2698                                 post_title page_title,
2699                                 post_parent page_parent_id,
2700                                 post_date_gmt,
2701                                 post_date,
2702                                 post_status
2703                         FROM {$wpdb->posts}
2704                         WHERE post_type = 'page'
2705                         ORDER BY ID
2706                 ");
2707
2708                 // The date needs to be formatted properly.
2709                 $num_pages = count($page_list);
2710                 for ( $i = 0; $i < $num_pages; $i++ ) {
2711                         $page_list[$i]->dateCreated = $this->_convert_date(  $page_list[$i]->post_date );
2712                         $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
2713
2714                         unset($page_list[$i]->post_date_gmt);
2715                         unset($page_list[$i]->post_date);
2716                         unset($page_list[$i]->post_status);
2717                 }
2718
2719                 return($page_list);
2720         }
2721
2722         /**
2723          * Retrieve authors list.
2724          *
2725          * @since 2.2.0
2726          *
2727          * @param array $args Method parameters.
2728          * @return array
2729          */
2730         public function wp_getAuthors($args) {
2731
2732                 $this->escape($args);
2733
2734                 $blog_id        = (int) $args[0];
2735                 $username       = $args[1];
2736                 $password       = $args[2];
2737
2738                 if ( !$user = $this->login($username, $password) )
2739                         return $this->error;
2740
2741                 if ( !current_user_can('edit_posts') )
2742                         return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
2743
2744                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2745                 do_action( 'xmlrpc_call', 'wp.getAuthors' );
2746
2747                 $authors = array();
2748                 foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
2749                         $authors[] = array(
2750                                 'user_id'       => $user->ID,
2751                                 'user_login'    => $user->user_login,
2752                                 'display_name'  => $user->display_name
2753                         );
2754                 }
2755
2756                 return $authors;
2757         }
2758
2759         /**
2760          * Get list of all tags
2761          *
2762          * @since 2.7.0
2763          *
2764          * @param array $args Method parameters.
2765          * @return array
2766          */
2767         public function wp_getTags( $args ) {
2768                 $this->escape( $args );
2769
2770                 $blog_id                = (int) $args[0];
2771                 $username               = $args[1];
2772                 $password               = $args[2];
2773
2774                 if ( !$user = $this->login($username, $password) )
2775                         return $this->error;
2776
2777                 if ( !current_user_can( 'edit_posts' ) )
2778                         return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
2779
2780                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2781                 do_action( 'xmlrpc_call', 'wp.getKeywords' );
2782
2783                 $tags = array();
2784
2785                 if ( $all_tags = get_tags() ) {
2786                         foreach( (array) $all_tags as $tag ) {
2787                                 $struct['tag_id']                       = $tag->term_id;
2788                                 $struct['name']                         = $tag->name;
2789                                 $struct['count']                        = $tag->count;
2790                                 $struct['slug']                         = $tag->slug;
2791                                 $struct['html_url']                     = esc_html( get_tag_link( $tag->term_id ) );
2792                                 $struct['rss_url']                      = esc_html( get_tag_feed_link( $tag->term_id ) );
2793
2794                                 $tags[] = $struct;
2795                         }
2796                 }
2797
2798                 return $tags;
2799         }
2800
2801         /**
2802          * Create new category.
2803          *
2804          * @since 2.2.0
2805          *
2806          * @param array $args Method parameters.
2807          * @return int Category ID.
2808          */
2809         public function wp_newCategory($args) {
2810                 $this->escape($args);
2811
2812                 $blog_id                                = (int) $args[0];
2813                 $username                               = $args[1];
2814                 $password                               = $args[2];
2815                 $category                               = $args[3];
2816
2817                 if ( !$user = $this->login($username, $password) )
2818                         return $this->error;
2819
2820                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2821                 do_action( 'xmlrpc_call', 'wp.newCategory' );
2822
2823                 // Make sure the user is allowed to add a category.
2824                 if ( !current_user_can('manage_categories') )
2825                         return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
2826
2827                 // If no slug was provided make it empty so that
2828                 // WordPress will generate one.
2829                 if ( empty($category['slug']) )
2830                         $category['slug'] = '';
2831
2832                 // If no parent_id was provided make it empty
2833                 // so that it will be a top level page (no parent).
2834                 if ( !isset($category['parent_id']) )
2835                         $category['parent_id'] = '';
2836
2837                 // If no description was provided make it empty.
2838                 if ( empty($category["description"]) )
2839                         $category["description"] = "";
2840
2841                 $new_category = array(
2842                         'cat_name'                              => $category['name'],
2843                         'category_nicename'             => $category['slug'],
2844                         'category_parent'               => $category['parent_id'],
2845                         'category_description'  => $category['description']
2846                 );
2847
2848                 $cat_id = wp_insert_category($new_category, true);
2849                 if ( is_wp_error( $cat_id ) ) {
2850                         if ( 'term_exists' == $cat_id->get_error_code() )
2851                                 return (int) $cat_id->get_error_data();
2852                         else
2853                                 return(new IXR_Error(500, __('Sorry, the new category failed.')));
2854                 } elseif ( ! $cat_id ) {
2855                         return(new IXR_Error(500, __('Sorry, the new category failed.')));
2856                 }
2857
2858                 /**
2859                  * Fires after a new category has been successfully created via XML-RPC.
2860                  *
2861                  * @since 3.4.0
2862                  *
2863                  * @param int   $cat_id ID of the new category.
2864                  * @param array $args   An array of new category arguments.
2865                  */
2866                 do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
2867
2868                 return $cat_id;
2869         }
2870
2871         /**
2872          * Remove category.
2873          *
2874          * @since 2.5.0
2875          *
2876          * @param array $args Method parameters.
2877          * @return mixed See {@link wp_delete_term()} for return info.
2878          */
2879         public function wp_deleteCategory($args) {
2880                 $this->escape($args);
2881
2882                 $blog_id                = (int) $args[0];
2883                 $username               = $args[1];
2884                 $password               = $args[2];
2885                 $category_id    = (int) $args[3];
2886
2887                 if ( !$user = $this->login($username, $password) )
2888                         return $this->error;
2889
2890                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2891                 do_action( 'xmlrpc_call', 'wp.deleteCategory' );
2892
2893                 if ( !current_user_can('manage_categories') )
2894                         return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
2895
2896                 $status = wp_delete_term( $category_id, 'category' );
2897
2898                 if ( true == $status ) {
2899                         /**
2900                          * Fires after a category has been successfully deleted via XML-RPC.
2901                          *
2902                          * @since 3.4.0
2903                          *
2904                          * @param int   $category_id ID of the deleted category.
2905                          * @param array $args        An array of arguments to delete the category.
2906                          */
2907                         do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
2908                 }
2909
2910                 return $status;
2911         }
2912
2913         /**
2914          * Retrieve category list.
2915          *
2916          * @since 2.2.0
2917          *
2918          * @param array $args Method parameters.
2919          * @return array
2920          */
2921         public function wp_suggestCategories($args) {
2922                 $this->escape($args);
2923
2924                 $blog_id                                = (int) $args[0];
2925                 $username                               = $args[1];
2926                 $password                               = $args[2];
2927                 $category                               = $args[3];
2928                 $max_results                    = (int) $args[4];
2929
2930                 if ( !$user = $this->login($username, $password) )
2931                         return $this->error;
2932
2933                 if ( !current_user_can( 'edit_posts' ) )
2934                         return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
2935
2936                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2937                 do_action( 'xmlrpc_call', 'wp.suggestCategories' );
2938
2939                 $category_suggestions = array();
2940                 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
2941                 foreach ( (array) get_categories($args) as $cat ) {
2942                         $category_suggestions[] = array(
2943                                 'category_id'   => $cat->term_id,
2944                                 'category_name' => $cat->name
2945                         );
2946                 }
2947
2948                 return($category_suggestions);
2949         }
2950
2951         /**
2952          * Retrieve comment.
2953          *
2954          * @since 2.7.0
2955          *
2956          * @param array $args Method parameters.
2957          * @return array
2958          */
2959         public function wp_getComment($args) {
2960                 $this->escape($args);
2961
2962                 $blog_id        = (int) $args[0];
2963                 $username       = $args[1];
2964                 $password       = $args[2];
2965                 $comment_id     = (int) $args[3];
2966
2967                 if ( !$user = $this->login($username, $password) )
2968                         return $this->error;
2969
2970                 if ( !current_user_can( 'moderate_comments' ) )
2971                         return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
2972
2973                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
2974                 do_action( 'xmlrpc_call', 'wp.getComment' );
2975
2976                 if ( ! $comment = get_comment($comment_id) )
2977                         return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
2978
2979                 return $this->_prepare_comment( $comment );
2980         }
2981
2982         /**
2983          * Retrieve comments.
2984          *
2985          * Besides the common blog_id, username, and password arguments, it takes a filter
2986          * array as last argument.
2987          *
2988          * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
2989          *
2990          * The defaults are as follows:
2991          * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
2992          * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
2993          * - 'number' - Default is 10. Total number of media items to retrieve.
2994          * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
2995          *
2996          * @since 2.7.0
2997          *
2998          * @param array $args Method parameters.
2999          * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
3000          */
3001         public function wp_getComments($args) {
3002                 $this->escape($args);
3003
3004                 $blog_id        = (int) $args[0];
3005                 $username       = $args[1];
3006                 $password       = $args[2];
3007                 $struct         = isset( $args[3] ) ? $args[3] : array();
3008
3009                 if ( !$user = $this->login($username, $password) )
3010                         return $this->error;
3011
3012                 if ( !current_user_can( 'moderate_comments' ) )
3013                         return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
3014
3015                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3016                 do_action( 'xmlrpc_call', 'wp.getComments' );
3017
3018                 if ( isset($struct['status']) )
3019                         $status = $struct['status'];
3020                 else
3021                         $status = '';
3022
3023                 $post_id = '';
3024                 if ( isset($struct['post_id']) )
3025                         $post_id = absint($struct['post_id']);
3026
3027                 $offset = 0;
3028                 if ( isset($struct['offset']) )
3029                         $offset = absint($struct['offset']);
3030
3031                 $number = 10;
3032                 if ( isset($struct['number']) )
3033                         $number = absint($struct['number']);
3034
3035                 $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
3036
3037                 $comments_struct = array();
3038
3039                 foreach ( $comments as $comment ) {
3040                         $comments_struct[] = $this->_prepare_comment( $comment );
3041                 }
3042
3043                 return $comments_struct;
3044         }
3045
3046         /**
3047          * Delete a comment.
3048          *
3049          * By default, the comment will be moved to the trash instead of deleted.
3050          * See {@link wp_delete_comment()} for more information on
3051          * this behavior.
3052          *
3053          * @since 2.7.0
3054          *
3055          * @param array $args Method parameters. Contains:
3056          *  - blog_id
3057          *  - username
3058          *  - password
3059          *  - comment_id
3060          * @return mixed {@link wp_delete_comment()}
3061          */
3062         public function wp_deleteComment($args) {
3063                 $this->escape($args);
3064
3065                 $blog_id        = (int) $args[0];
3066                 $username       = $args[1];
3067                 $password       = $args[2];
3068                 $comment_ID     = (int) $args[3];
3069
3070                 if ( !$user = $this->login($username, $password) )
3071                         return $this->error;
3072
3073                 if ( !current_user_can( 'moderate_comments' ) )
3074                         return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3075
3076                 if ( ! get_comment($comment_ID) )
3077                         return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
3078
3079                 if ( !current_user_can( 'edit_comment', $comment_ID ) )
3080                         return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
3081
3082                 /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
3083                 do_action( 'xmlrpc_call', 'wp.deleteComment' );
3084
3085                 $status = wp_delete_comment( $comment_ID );
3086
3087                 if ( true == $status ) {
3088                         /**
3089                          * Fires after a comment has been successfully deleted via XML-RPC.
3090                          *
3091                          * @since 3.4.0
3092                          *
3093                          * @param int   $comment_ID ID of the deleted comment.
3094                          * @param array $args       An array of arguments to delete the comment.
3095                          */
3096                         do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
3097                 }
3098
3099                 return $status;
3100         }
3101
3102         /**
3103          * Edit comment.
3104          *
3105          * Besides the common blog_id, username, and password arguments, it takes a
3106          * comment_id integer and a content_struct array as last argument.
3107          *
3108          * The allowed keys in the content_struct array are:
3109          *  - 'author'
3110          *  - 'author_url'
3111          *  - 'author_email'
3112          *  - 'content'
3113          *  - 'date_created_gmt'
3114          *  - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
3115          *
3116          * @since 2.7.0
3117          *
3118          * @param array $args. Contains:
3119          *  - blog_id
3120          *  - username
3121          *  - password
3122          *  - comment_id
3123          *  - content_struct
3124          * @return bool True, on success.
3125          */
3126         public function wp_editComment($args) {
3127                 $this->escape($args);
3128
3129                 $blog_id        = (int) $args[0];
3130                 $username       = $args[1];
3131                 $password       = $args[2];
3132                 $comment_ID     = (int) $args[3];
3133                 $content_struct = $args[4];
3134
3135 &nb