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