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