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