Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-xmlrpc-server.php

    r19071 r20996  
    3737                        // WordPress API
    3838                        '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',
    3951                        'wp.getPage'                    => 'this:wp_getPage',
    4052                        'wp.getPages'                   => 'this:wp_getPages',
    … …  
    6577                        'wp.getMediaLibrary'    => 'this:wp_getMediaLibrary',
    6678                        'wp.getPostFormats'     => 'this:wp_getPostFormats',
     79                        'wp.getPostType'                => 'this:wp_getPostType',
     80                        'wp.getPostTypes'               => 'this:wp_getPostTypes',
    6781
    6882                        // Blogger API
    … …  
    110124                );
    111125
    112                 $this->initialise_blog_option_info( );
     126                $this->initialise_blog_option_info();
    113127                $this->methods = apply_filters('xmlrpc_methods', $this->methods);
    114128        }
    … …  
    157171        function login_pass_ok($user_login, $user_pass) {
    158172                if ( !get_option( 'enable_xmlrpc' ) ) {
    159                         $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.  An admin user can enable them at %s'),  admin_url('options-writing.php') ) );
     173                        $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'),  admin_url('options-writing.php') ) );
    160174                        return false;
    161175                }
    … …  
    179193        function login($username, $password) {
    180194                if ( !get_option( 'enable_xmlrpc' ) ) {
    181                         $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.  An admin user can enable them at %s'),  admin_url('options-writing.php') ) );
     195                        $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'),  admin_url('options-writing.php') ) );
    182196                        return false;
    183197                }
    … …  
    263277                                $meta['id'] = (int) $meta['id'];
    264278                                $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
    265                                 $meta['value'] = stripslashes_deep( $meta['value'] );
    266279                                if ( isset($meta['key']) ) {
    267280                                        $meta['key'] = stripslashes( $meta['key'] );
    268281                                        if ( $meta['key'] != $pmeta->meta_key )
    269282                                                continue;
     283                                        $meta['value'] = stripslashes_deep( $meta['value'] );
    270284                                        if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
    271285                                                update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
    … …  
    286300         * @since 2.6.0
    287301         */
    288         function initialise_blog_option_info( ) {
     302        function initialise_blog_option_info() {
    289303                global $wp_version;
    290304
    291305                $this->blog_options = array(
    292306                        // Read only options
    293                         'software_name'         => array(
    294                                 'desc'                  => __( 'Software Name' ),
    295                                 'readonly'              => true,
    296                                 'value'                 => 'WordPress'
     307                        'software_name'     => array(
     308                                'desc'          => __( 'Software Name' ),
     309                                'readonly'      => true,
     310                                'value'         => 'WordPress'
    297311                        ),
    298                         'software_version'      => array(
    299                                 'desc'                  => __( 'Software Version' ),
    300                                 'readonly'              => true,
    301                                 'value'                 => $wp_version
     312                        'software_version'  => array(
     313                                'desc'          => __( 'Software Version' ),
     314                                'readonly'      => true,
     315                                'value'         => $wp_version
    302316                        ),
    303                         'blog_url'                      => array(
    304                                 'desc'                  => __( 'Site URL' ),
    305                                 'readonly'              => true,
    306                                 'option'                => 'siteurl'
     317                        'blog_url'          => array(
     318                                'desc'          => __( 'Site URL' ),
     319                                'readonly'      => true,
     320                                'option'        => 'siteurl'
    307321                        ),
     322                        'image_default_link_type' => array(
     323                                'desc'          => __( 'Image default link type' ),
     324                                'readonly'      => true,
     325                                'option'        => 'image_default_link_type'
     326                        ),
     327                        'image_default_size' => array(
     328                                'desc'          => __( 'Image default size' ),
     329                                'readonly'      => true,
     330                                'option'        => 'image_default_size'
     331                        ),
     332                        'image_default_align' => array(
     333                                'desc'          => __( 'Image default align' ),
     334                                'readonly'      => true,
     335                                'option'        => 'image_default_align'
     336                        ),
     337                        'template'          => array(
     338                                'desc'          => __( 'Template' ),
     339                                'readonly'      => true,
     340                                'option'        => 'template'
     341                        ),
     342                        'stylesheet'        => array(
     343                                'desc'          => __( 'Stylesheet' ),
     344                                'readonly'      => true,
     345                                'option'        => 'stylesheet'
     346                        ),
     347                        'post_thumbnail'    => array(
     348                                'desc'          => __('Post Thumbnail'),
     349                                'readonly'      => true,
     350                                'value'         => current_theme_supports( 'post-thumbnails' )
     351                        ),
    308352
    309353                        // Updatable options
    310                         'time_zone'                     => array(
    311                                 'desc'                  => __( 'Time Zone' ),
    312                                 'readonly'              => false,
    313                                 'option'                => 'gmt_offset'
     354                        'time_zone'         => array(
     355                                'desc'          => __( 'Time Zone' ),
     356                                'readonly'      => false,
     357                                'option'        => 'gmt_offset'
    314358                        ),
    315                         'blog_title'            => array(
    316                                 'desc'                  => __( 'Site Title' ),
    317                                 'readonly'              => false,
    318                                 'option'                        => 'blogname'
     359                        'blog_title'        => array(
     360                                'desc'          => __( 'Site Title' ),
     361                                'readonly'      => false,
     362                                'option'        => 'blogname'
    319363                        ),
    320                         'blog_tagline'          => array(
    321                                 'desc'                  => __( 'Site Tagline' ),
    322                                 'readonly'              => false,
    323                                 'option'                => 'blogdescription'
     364                        'blog_tagline'      => array(
     365                                'desc'          => __( 'Site Tagline' ),
     366                                'readonly'      => false,
     367                                'option'        => 'blogdescription'
    324368                        ),
    325                         'date_format'           => array(
    326                                 'desc'                  => __( 'Date Format' ),
    327                                 'readonly'              => false,
    328                                 'option'                => 'date_format'
     369                        'date_format'       => array(
     370                                'desc'          => __( 'Date Format' ),
     371                                'readonly'      => false,
     372                                'option'        => 'date_format'
    329373                        ),
    330                         'time_format'           => array(
    331                                 'desc'                  => __( 'Time Format' ),
    332                                 'readonly'              => false,
    333                                 'option'                => 'time_format'
     374                        'time_format'       => array(
     375                                'desc'          => __( 'Time Format' ),
     376                                'readonly'      => false,
     377                                'option'        => 'time_format'
    334378                        ),
    335                         'users_can_register'    => array(
    336                                 'desc'                  => __( 'Allow new users to sign up' ),
    337                                 'readonly'              => false,
    338                                 'option'                => 'users_can_register'
     379                        'users_can_register' => array(
     380                                'desc'          => __( 'Allow new users to sign up' ),
     381                                'readonly'      => false,
     382                                'option'        => 'users_can_register'
    339383                        ),
    340                         'thumbnail_size_w'      => array(
    341                                 'desc'                  => __( 'Thumbnail Width' ),
    342                                 'readonly'              => false,
    343                                 'option'                => 'thumbnail_size_w'
     384                        'thumbnail_size_w'  => array(
     385                                'desc'          => __( 'Thumbnail Width' ),
     386                                'readonly'      => false,
     387                                'option'        => 'thumbnail_size_w'
    344388                        ),
    345                         'thumbnail_size_h'      => array(
    346                                 'desc'                  => __( 'Thumbnail Height' ),
    347                                 'readonly'              => false,
    348                                 'option'                => 'thumbnail_size_h'
     389                        'thumbnail_size_h'  => array(
     390                                'desc'          => __( 'Thumbnail Height' ),
     391                                'readonly'      => false,
     392                                'option'        => 'thumbnail_size_h'
    349393                        ),
    350                         'thumbnail_crop'        => array(
    351                                 'desc'                  => __( 'Crop thumbnail to exact dimensions' ),
    352                                 'readonly'              => false,
    353                                 'option'                => 'thumbnail_crop'
     394                        'thumbnail_crop'    => array(
     395                                'desc'          => __( 'Crop thumbnail to exact dimensions' ),
     396                                'readonly'      => false,
     397                                'option'        => 'thumbnail_crop'
    354398                        ),
    355                         'medium_size_w' => array(
    356                                 'desc'                  => __( 'Medium size image width' ),
    357                                 'readonly'              => false,
    358                                 'option'                => 'medium_size_w'
     399                        'medium_size_w'     => array(
     400                                'desc'          => __( 'Medium size image width' ),
     401                                'readonly'      => false,
     402                                'option'        => 'medium_size_w'
    359403                        ),
    360                         'medium_size_h' => array(
    361                                 'desc'                  => __( 'Medium size image height' ),
    362                                 'readonly'              => false,
    363                                 'option'                => 'medium_size_h'
     404                        'medium_size_h'     => array(
     405                                'desc'          => __( 'Medium size image height' ),
     406                                'readonly'      => false,
     407                                'option'        => 'medium_size_h'
    364408                        ),
    365                         'large_size_w'  => array(
    366                                 'desc'                  => __( 'Large size image width' ),
    367                                 'readonly'              => false,
    368                                 'option'                => 'large_size_w'
     409                        'large_size_w'      => array(
     410                                'desc'          => __( 'Large size image width' ),
     411                                'readonly'      => false,
     412                                'option'        => 'large_size_w'
    369413                        ),
    370                         'large_size_h'  => array(
    371                                 'desc'                  => __( 'Large size image height' ),
    372                                 'readonly'              => false,
    373                                 'option'                => 'large_size_h'
     414                        'large_size_h'      => array(
     415                                'desc'          => __( 'Large size image height' ),
     416                                'readonly'      => false,
     417                                'option'        => 'large_size_h'
     418                        ),
     419                        'default_comment_status' => array(
     420                                'desc'          => __( 'Allow people to post comments on new articles' ),
     421                                'readonly'      => false,
     422                                'option'        => 'default_comment_status'
     423                        ),
     424                        'default_ping_status' => array(
     425                                'desc'          => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
     426                                'readonly'      => false,
     427                                'option'        => 'default_ping_status'
    374428                        )
    375429                );
    … …  
    409463                        return $this->error;
    410464
    411 
    412465                do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
    413466
    414467                $blogs = (array) get_blogs_of_user( $user->ID );
    415                 $struct = array( );
     468                $struct = array();
    416469
    417470                foreach ( $blogs as $blog ) {
    … …  
    432485                        );
    433486
    434                         restore_current_blog( );
     487                        restore_current_blog();
     488                }
     489
     490                return $struct;
     491        }
     492
     493        /**
     494         * Checks if the method received at least the minimum number of arguments.
     495         *
     496         * @since 3.4.0
     497         *
     498         * @param string|array $args Sanitize single string or array of strings.
     499         * @param int $count Minimum number of arguments.
     500         * @return boolean if $args contains at least $count arguments.
     501         */
     502        protected function minimum_args( $args, $count ) {
     503                if ( count( $args ) < $count ) {
     504                        $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
     505                        return false;
     506                }
     507
     508                return true;
     509        }
     510
     511        /**
     512         * Prepares taxonomy data for return in an XML-RPC object.
     513         *
     514         * @access protected
     515         *
     516         * @param object $taxonomy The unprepared taxonomy data
     517         * @param array $fields The subset of taxonomy fields to return
     518         * @return array The prepared taxonomy data
     519         */
     520        protected function _prepare_taxonomy( $taxonomy, $fields ) {
     521                $_taxonomy = array(
     522                        'name' => $taxonomy->name,
     523                        'label' => $taxonomy->label,
     524                        'hierarchical' => (bool) $taxonomy->hierarchical,
     525                        'public' => (bool) $taxonomy->public,
     526                        'show_ui' => (bool) $taxonomy->show_ui,
     527                        '_builtin' => (bool) $taxonomy->_builtin,
     528                );
     529
     530                if ( in_array( 'labels', $fields ) )
     531                        $_taxonomy['labels'] = (array) $taxonomy->labels;
     532
     533                if ( in_array( 'cap', $fields ) )
     534                        $_taxonomy['cap'] = (array) $taxonomy->cap;
     535
     536                if ( in_array( 'object_type', $fields ) )
     537                        $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
     538
     539                return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
     540        }
     541
     542        /**
     543         * Prepares term data for return in an XML-RPC object.
     544         *
     545         * @access protected
     546         *
     547         * @param array|object $term The unprepared term data
     548         * @return array The prepared term data
     549         */
     550        protected function _prepare_term( $term ) {
     551                $_term = $term;
     552                if ( ! is_array( $_term) )
     553                        $_term = get_object_vars( $_term );
     554
     555                // For Intergers which may be largeer than XMLRPC supports ensure we return strings.
     556                $_term['term_id'] = strval( $_term['term_id'] );
     557                $_term['term_group'] = strval( $_term['term_group'] );
     558                $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
     559                $_term['parent'] = strval( $_term['parent'] );
     560
     561                // Count we are happy to return as an Integer because people really shouldn't use Terms that much.
     562                $_term['count'] = intval( $_term['count'] );
     563
     564                return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
     565        }
     566
     567        /**
     568         * Convert a WordPress date string to an IXR_Date object.
     569         *
     570         * @access protected
     571         *
     572         * @param string $date
     573         * @return IXR_Date
     574         */
     575        protected function _convert_date( $date ) {
     576                if ( $date === '0000-00-00 00:00:00' ) {
     577                        return new IXR_Date( '00000000T00:00:00Z' );
     578                }
     579                return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
     580        }
     581
     582        /**
     583         * Convert a WordPress GMT date string to an IXR_Date object.
     584         *
     585         * @access protected
     586         *
     587         * @param string $date_gmt
     588         * @param string $date
     589         * @return IXR_Date
     590         */
     591        protected function _convert_date_gmt( $date_gmt, $date ) {
     592                if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
     593                        return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
     594                }
     595                return $this->_convert_date( $date_gmt );
     596        }
     597
     598        /**
     599         * Prepares post data for return in an XML-RPC object.
     600         *
     601         * @access protected
     602         *
     603         * @param array $post The unprepared post data
     604         * @param array $fields The subset of post type fields to return
     605         * @return array The prepared post data
     606         */
     607        protected function _prepare_post( $post, $fields ) {
     608                // holds the data for this post. built up based on $fields
     609                $_post = array( 'post_id' => strval( $post['ID'] ) );
     610
     611                // prepare common post fields
     612                $post_fields = array(
     613                        'post_title'        => $post['post_title'],
     614                        'post_date'         => $this->_convert_date( $post['post_date'] ),
     615                        'post_date_gmt'     => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
     616                        'post_modified'     => $this->_convert_date( $post['post_modified'] ),
     617                        'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
     618                        'post_status'       => $post['post_status'],
     619                        'post_type'         => $post['post_type'],
     620                        'post_name'         => $post['post_name'],
     621                        'post_author'       => $post['post_author'],
     622                        'post_password'     => $post['post_password'],
     623                        'post_excerpt'      => $post['post_excerpt'],
     624                        'post_content'      => $post['post_content'],
     625                        'link'              => post_permalink( $post['ID'] ),
     626                        'comment_status'    => $post['comment_status'],
     627                        'ping_status'       => $post['ping_status'],
     628                        'sticky'            => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
     629                );
     630
     631                // Thumbnail
     632                $post_fields['post_thumbnail'] = array();
     633                $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
     634                if ( $thumbnail_id ) {
     635                        $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
     636                        $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
     637                }
     638
     639                // Consider future posts as published
     640                if ( $post_fields['post_status'] === 'future' )
     641                        $post_fields['post_status'] = 'publish';
     642
     643                // Fill in blank post format
     644                $post_fields['post_format'] = get_post_format( $post['ID'] );
     645                if ( empty( $post_fields['post_format'] ) )
     646                        $post_fields['post_format'] = 'standard';
     647
     648                // Merge requested $post_fields fields into $_post
     649                if ( in_array( 'post', $fields ) ) {
     650                        $_post = array_merge( $_post, $post_fields );
     651                } else {
     652                        $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
     653                        $_post = array_merge( $_post, $requested_fields );
     654                }
     655
     656                $all_taxonomy_fields = in_array( 'taxonomies', $fields );
     657
     658                if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
     659                        $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
     660                        $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
     661                        $_post['terms'] = array();
     662                        foreach ( $terms as $term ) {
     663                                $_post['terms'][] = $this->_prepare_term( $term );
     664                        }
     665                }
     666
     667                if ( in_array( 'custom_fields', $fields ) )
     668                        $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
     669
     670                if ( in_array( 'enclosure', $fields ) ) {
     671                        $_post['enclosure'] = array();
     672                        $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
     673                        if ( ! empty( $enclosures ) ) {
     674                                $encdata = explode( "\n", $enclosures[0] );
     675                                $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
     676                                $_post['enclosure']['length'] = (int) trim( $encdata[1] );
     677                                $_post['enclosure']['type'] = trim( $encdata[2] );
     678                        }
     679                }
     680
     681                return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
     682        }
     683
     684        /**
     685         * Prepares post data for return in an XML-RPC object.
     686         *
     687         * @access protected
     688         *
     689         * @param object $post_type Post type object
     690         * @param array $fields The subset of post fields to return
     691         * @return array The prepared post type data
     692         */
     693        protected function _prepare_post_type( $post_type, $fields ) {
     694                $_post_type = array(
     695                        'name' => $post_type->name,
     696                        'label' => $post_type->label,
     697                        'hierarchical' => (bool) $post_type->hierarchical,
     698                        'public' => (bool) $post_type->public,
     699                        'show_ui' => (bool) $post_type->show_ui,
     700                        '_builtin' => (bool) $post_type->_builtin,
     701                        'has_archive' => (bool) $post_type->has_archive,
     702                        'supports' => get_all_post_type_supports( $post_type->name ),
     703                );
     704
     705                if ( in_array( 'labels', $fields ) ) {
     706                        $_post_type['labels'] = (array) $post_type->labels;
     707                }
     708
     709                if ( in_array( 'cap', $fields ) ) {
     710                        $_post_type['cap'] = (array) $post_type->cap;
     711                        $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
     712                }
     713
     714                if ( in_array( 'menu', $fields ) ) {
     715                        $_post_type['menu_position'] = (int) $post_type->menu_position;
     716                        $_post_type['menu_icon'] = $post_type->menu_icon;
     717                        $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
     718                }
     719
     720                if ( in_array( 'taxonomies', $fields ) )
     721                        $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
     722
     723                return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
     724        }
     725
     726        /**
     727         * Prepares media item data for return in an XML-RPC object.
     728         *
     729         * @access protected
     730         *
     731         * @param object $media_item The unprepared media item data
     732         * @param string $thumbnail_size The image size to use for the thumbnail URL
     733         * @return array The prepared media item data
     734         */
     735        protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
     736                $_media_item = array(
     737                        'attachment_id'    => strval( $media_item->ID ),
     738                        'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
     739                        'parent'           => $media_item->post_parent,
     740                        'link'             => wp_get_attachment_url( $media_item->ID ),
     741                        'title'            => $media_item->post_title,
     742                        'caption'          => $media_item->post_excerpt,
     743                        'description'      => $media_item->post_content,
     744                        'metadata'         => wp_get_attachment_metadata( $media_item->ID ),
     745                );
     746
     747                $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
     748                if ( $thumbnail_src )
     749                        $_media_item['thumbnail'] = $thumbnail_src[0];
     750                else
     751                        $_media_item['thumbnail'] = $_media_item['link'];
     752
     753                return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
     754        }
     755
     756        /**
     757         * Prepares page data for return in an XML-RPC object.
     758         *
     759         * @access protected
     760         *
     761         * @param object $page The unprepared page data
     762         * @return array The prepared page data
     763         */
     764        protected function _prepare_page( $page ) {
     765                // Get all of the page content and link.
     766                $full_page = get_extended( $page->post_content );
     767                $link = post_permalink( $page->ID );
     768
     769                // Get info the page parent if there is one.
     770                $parent_title = "";
     771                if ( ! empty( $page->post_parent ) ) {
     772                        $parent = get_page( $page->post_parent );
     773                        $parent_title = $parent->post_title;
     774                }
     775
     776                // Determine comment and ping settings.
     777                $allow_comments = comments_open( $page->ID ) ? 1 : 0;
     778                $allow_pings = pings_open( $page->ID ) ? 1 : 0;
     779
     780                // Format page date.
     781                $page_date = $this->_convert_date( $page->post_date );
     782                $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
     783
     784                // Pull the categories info together.
     785                $categories = array();
     786                foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
     787                        $categories[] = get_cat_name( $cat_id );
     788                }
     789
     790                // Get the author info.
     791                $author = get_userdata( $page->post_author );
     792
     793                $page_template = get_page_template_slug( $page->ID );
     794                if ( empty( $page_template ) )
     795                        $page_template = 'default';
     796
     797                $_page = array(
     798                        'dateCreated'            => $page_date,
     799                        'userid'                 => $page->post_author,
     800                        'page_id'                => $page->ID,
     801                        'page_status'            => $page->post_status,
     802                        'description'            => $full_page['main'],
     803                        'title'                  => $page->post_title,
     804                        'link'                   => $link,
     805                        'permaLink'              => $link,
     806                        'categories'             => $categories,
     807                        'excerpt'                => $page->post_excerpt,
     808                        'text_more'              => $full_page['extended'],
     809                        'mt_allow_comments'      => $allow_comments,
     810                        'mt_allow_pings'         => $allow_pings,
     811                        'wp_slug'                => $page->post_name,
     812                        'wp_password'            => $page->post_password,
     813                        'wp_author'              => $author->display_name,
     814                        'wp_page_parent_id'      => $page->post_parent,
     815                        'wp_page_parent_title'   => $parent_title,
     816                        'wp_page_order'          => $page->menu_order,
     817                        'wp_author_id'           => (string) $author->ID,
     818                        'wp_author_display_name' => $author->display_name,
     819                        'date_created_gmt'       => $page_date_gmt,
     820                        'custom_fields'          => $this->get_custom_fields( $page->ID ),
     821                        'wp_page_template'       => $page_template
     822                );
     823
     824                return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
     825        }
     826
     827        /**
     828         * Prepares comment data for return in an XML-RPC object.
     829         *
     830         * @access protected
     831         *
     832         * @param object $comment The unprepared comment data
     833         * @return array The prepared comment data
     834         */
     835        protected function _prepare_comment( $comment ) {
     836                // Format page date.
     837                $comment_date = $this->_convert_date( $comment->comment_date );
     838                $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
     839
     840                if ( '0' == $comment->comment_approved )
     841                        $comment_status = 'hold';
     842                else if ( 'spam' == $comment->comment_approved )
     843                        $comment_status = 'spam';
     844                else if ( '1' == $comment->comment_approved )
     845                        $comment_status = 'approve';
     846                else
     847                        $comment_status = $comment->comment_approved;
     848
     849                $_comment = array(
     850                        'date_created_gmt' => $comment_date_gmt,
     851                        'user_id'          => $comment->user_id,
     852                        'comment_id'       => $comment->comment_ID,
     853                        'parent'           => $comment->comment_parent,
     854                        'status'           => $comment_status,
     855                        'content'          => $comment->comment_content,
     856                        'link'             => get_comment_link($comment),
     857                        'post_id'          => $comment->comment_post_ID,
     858                        'post_title'       => get_the_title($comment->comment_post_ID),
     859                        'author'           => $comment->comment_author,
     860                        'author_url'       => $comment->comment_author_url,
     861                        'author_email'     => $comment->comment_author_email,
     862                        'author_ip'        => $comment->comment_author_IP,
     863                        'type'             => $comment->comment_type,
     864                );
     865
     866                return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
     867        }
     868
     869        /**
     870         * Create a new post for any registered post type.
     871         *
     872         * @since 3.4.0
     873         *
     874         * @param array $args Method parameters. Contains:
     875         *  - int     $blog_id
     876         *  - string  $username
     877         *  - string  $password
     878         *  - array   $content_struct
     879         *      $content_struct can contain:
     880         *      - post_type (default: 'post')
     881         *      - post_status (default: 'draft')
     882         *      - post_title
     883         *      - post_author
     884         *      - post_exerpt
     885         *      - post_content
     886         *      - post_date_gmt | post_date
     887         *      - post_format
     888         *      - post_password
     889         *      - comment_status - can be 'open' | 'closed'
     890         *      - ping_status - can be 'open' | 'closed'
     891         *      - sticky
     892         *      - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
     893         *      - custom_fields - array, with each element containing 'key' and 'value'
     894         *      - terms - array, with taxonomy names as keys and arrays of term IDs as values
     895         *      - terms_names - array, with taxonomy names as keys and arrays of term names as values
     896         *      - enclosure
     897         *      - any other fields supported by wp_insert_post()
     898         * @return string post_id
     899         */
     900        function wp_newPost( $args ) {
     901                if ( ! $this->minimum_args( $args, 4 ) )
     902                        return $this->error;
     903
     904                $this->escape( $args );
     905
     906                $blog_id        = (int) $args[0];
     907                $username       = $args[1];
     908                $password       = $args[2];
     909                $content_struct = $args[3];
     910
     911                if ( ! $user = $this->login( $username, $password ) )
     912                        return $this->error;
     913
     914                do_action( 'xmlrpc_call', 'wp.newPost' );
     915
     916                unset( $content_struct['ID'] );
     917
     918                return $this->_insert_post( $user, $content_struct );
     919        }
     920
     921        /**
     922         * Helper method for filtering out elements from an array.
     923         *
     924         * @since 3.4.0
     925         *
     926         * @param int $count Number to compare to one.
     927         */
     928        private function _is_greater_than_one( $count ) {
     929                return $count > 1;
     930        }
     931
     932        /**
     933         * Helper method for wp_newPost and wp_editPost, containing shared logic.
     934         *
     935         * @since 3.4.0
     936         * @uses wp_insert_post()
     937         *
     938         * @param WP_User $user The post author if post_author isn't set in $content_struct.
     939         * @param array $content_struct Post data to insert.
     940         */
     941        protected function _insert_post( $user, $content_struct ) {
     942                $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
     943                        'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
     944
     945                $post_data = wp_parse_args( $content_struct, $defaults );
     946
     947                $post_type = get_post_type_object( $post_data['post_type'] );
     948                if ( ! $post_type )
     949                        return new IXR_Error( 403, __( 'Invalid post type' ) );
     950
     951                $update = ! empty( $post_data['ID'] );
     952
     953                if ( $update ) {
     954                        if ( ! get_post( $post_data['ID'] ) )
     955                                return new IXR_Error( 401, __( 'Invalid post ID.' ) );
     956                        if ( ! current_user_can( $post_type->cap->edit_post, $post_data['ID'] ) )
     957                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
     958                        if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
     959                                return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
     960                } else {
     961                        if ( ! current_user_can( $post_type->cap->edit_posts ) )
     962                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
     963                }
     964
     965                switch ( $post_data['post_status'] ) {
     966                        case 'draft':
     967                        case 'pending':
     968                                break;
     969                        case 'private':
     970                                if ( ! current_user_can( $post_type->cap->publish_posts ) )
     971                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
     972                                break;
     973                        case 'publish':
     974                        case 'future':
     975                                if ( ! current_user_can( $post_type->cap->publish_posts ) )
     976                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
     977                                break;
     978                        default:
     979                                $post_data['post_status'] = 'draft';
     980                        break;
     981                }
     982
     983                if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
     984                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
     985
     986                $post_data['post_author'] = absint( $post_data['post_author'] );
     987                if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
     988                        if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
     989                                return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
     990
     991                        $author = get_userdata( $post_data['post_author'] );
     992
     993                        if ( ! $author )
     994                                return new IXR_Error( 404, __( 'Invalid author ID.' ) );
     995                } else {
     996                        $post_data['post_author'] = $user->ID;
     997                }
     998
     999                if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
     1000                        unset( $post_data['comment_status'] );
     1001
     1002                if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
     1003                        unset( $post_data['ping_status'] );
     1004
     1005                // Do some timestamp voodoo
     1006                if ( ! empty( $post_data['post_date_gmt'] ) ) {
     1007                        // We know this is supposed to be GMT, so we're going to slap that Z on there by force
     1008                        $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
     1009                } elseif ( ! empty( $post_data['post_date'] ) ) {
     1010                        $dateCreated = $post_data['post_date']->getIso();
     1011                }
     1012
     1013                if ( ! empty( $dateCreated ) ) {
     1014                        $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
     1015                        $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
     1016                }
     1017
     1018                if ( ! isset( $post_data['ID'] ) )
     1019                        $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
     1020                $post_ID = $post_data['ID'];
     1021
     1022                if ( $post_data['post_type'] == 'post' ) {
     1023                        // Private and password-protected posts cannot be stickied.
     1024                        if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
     1025                                // Error if the client tried to stick the post, otherwise, silently unstick.
     1026                                if ( ! empty( $post_data['sticky'] ) )
     1027                                        return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
     1028                                if ( $update )
     1029                                        unstick_post( $post_ID );
     1030                        } elseif ( isset( $post_data['sticky'] ) )  {
     1031                                if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
     1032                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
     1033                                if ( $post_data['sticky'] )
     1034                                        stick_post( $post_ID );
     1035                                else
     1036                                        unstick_post( $post_ID );
     1037                        }
     1038                }
     1039
     1040                if ( isset( $post_data['post_thumbnail'] ) ) {
     1041                        // empty value deletes, non-empty value adds/updates
     1042                        if ( ! $post_data['post_thumbnail'] )
     1043                                delete_post_thumbnail( $post_ID );
     1044                        elseif ( ! set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] ) )
     1045                                        return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
     1046                        unset( $content_struct['post_thumbnail'] );
     1047                }
     1048
     1049                if ( isset( $post_data['custom_fields'] ) )
     1050                        $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
     1051
     1052                if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
     1053                        $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
     1054
     1055                        // accumulate term IDs from terms and terms_names
     1056                        $terms = array();
     1057
     1058                        // first validate the terms specified by ID
     1059                        if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
     1060                                $taxonomies = array_keys( $post_data['terms'] );
     1061
     1062                                // validating term ids
     1063                                foreach ( $taxonomies as $taxonomy ) {
     1064                                        if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
     1065                                                return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
     1066
     1067                                        if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
     1068                                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
     1069
     1070                                        $term_ids = $post_data['terms'][$taxonomy];
     1071                                        foreach ( $term_ids as $term_id ) {
     1072                                                $term = get_term_by( 'id', $term_id, $taxonomy );
     1073
     1074                                                if ( ! $term )
     1075                                                        return new IXR_Error( 403, __( 'Invalid term ID' ) );
     1076
     1077                                                $terms[$taxonomy][] = (int) $term_id;
     1078                                        }
     1079                                }
     1080                        }
     1081
     1082                        // now validate terms specified by name
     1083                        if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
     1084                                $taxonomies = array_keys( $post_data['terms_names'] );
     1085
     1086                                foreach ( $taxonomies as $taxonomy ) {
     1087                                        if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
     1088                                                return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
     1089
     1090                                        if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
     1091                                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
     1092
     1093                                        // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
     1094                                        $ambiguous_terms = array();
     1095                                        if ( is_taxonomy_hierarchical( $taxonomy ) ) {
     1096                                                $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
     1097
     1098                                                // count the number of terms with the same name
     1099                                                $tax_term_names_count = array_count_values( $tax_term_names );
     1100
     1101                                                // filter out non-ambiguous term names
     1102                                                $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
     1103
     1104                                                $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
     1105                                        }
     1106
     1107                                        $term_names = $post_data['terms_names'][$taxonomy];
     1108                                        foreach ( $term_names as $term_name ) {
     1109                                                if ( in_array( $term_name, $ambiguous_terms ) )
     1110                                                        return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
     1111
     1112                                                $term = get_term_by( 'name', $term_name, $taxonomy );
     1113
     1114                                                if ( ! $term ) {
     1115                                                        // term doesn't exist, so check that the user is allowed to create new terms
     1116                                                        if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
     1117                                                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
     1118
     1119                                                        // create the new term
     1120                                                        $term_info = wp_insert_term( $term_name, $taxonomy );
     1121                                                        if ( is_wp_error( $term_info ) )
     1122                                                                return new IXR_Error( 500, $term_info->get_error_message() );
     1123
     1124                                                        $terms[$taxonomy][] = (int) $term_info['term_id'];
     1125                                                } else {
     1126                                                        $terms[$taxonomy][] = (int) $term->term_id;
     1127                                                }
     1128                                        }
     1129                                }
     1130                        }
     1131
     1132                        $post_data['tax_input'] = $terms;
     1133                        unset( $post_data['terms'], $post_data['terms_names'] );
     1134                } else {
     1135                        // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
     1136                        unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
     1137                }
     1138
     1139                if ( isset( $post_data['post_format'] ) ) {
     1140                        $format = set_post_format( $post_ID, $post_data['post_format'] );
     1141
     1142                        if ( is_wp_error( $format ) )
     1143                                return new IXR_Error( 500, $format->get_error_message() );
     1144
     1145                        unset( $post_data['post_format'] );
     1146                }
     1147
     1148                // Handle enclosures
     1149                $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
     1150                $this->add_enclosure_if_new( $post_ID, $enclosure );
     1151
     1152                $this->attach_uploads( $post_ID, $post_data['post_content'] );
     1153
     1154                $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
     1155
     1156                $post_ID = wp_insert_post( $post_data, true );
     1157                if ( is_wp_error( $post_ID ) )
     1158                        return new IXR_Error( 500, $post_ID->get_error_message() );
     1159
     1160                if ( ! $post_ID )
     1161                        return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
     1162
     1163                return strval( $post_ID );
     1164        }
     1165
     1166        /**
     1167         * Edit a post for any registered post type.
     1168         *
     1169         * The $content_struct parameter only needs to contain fields that
     1170         * should be changed. All other fields will retain their existing values.
     1171         *
     1172         * @since 3.4.0
     1173         *
     1174         * @param array $args Method parameters. Contains:
     1175         *  - int     $blog_id
     1176         *  - string  $username
     1177         *  - string  $password
     1178         *  - int     $post_id
     1179         *  - array   $content_struct
     1180         * @return true on success
     1181         */
     1182        function wp_editPost( $args ) {
     1183                if ( ! $this->minimum_args( $args, 5 ) )
     1184                        return $this->error;
     1185
     1186                $this->escape( $args );
     1187
     1188                $blog_id        = (int) $args[0];
     1189                $username       = $args[1];
     1190                $password       = $args[2];
     1191                $post_id        = (int) $args[3];
     1192                $content_struct = $args[4];
     1193
     1194                if ( ! $user = $this->login( $username, $password ) )
     1195                        return $this->error;
     1196
     1197                do_action( 'xmlrpc_call', 'wp.editPost' );
     1198
     1199                $post = get_post( $post_id, ARRAY_A );
     1200
     1201                if ( empty( $post['ID'] ) )
     1202                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     1203
     1204                // convert the date field back to IXR form
     1205                $post['post_date'] = $this->_convert_date( $post['post_date'] );
     1206
     1207                // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
     1208                // since _insert_post will ignore the non-GMT date if the GMT date is set
     1209                if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
     1210                        unset( $post['post_date_gmt'] );
     1211                else
     1212                        $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
     1213
     1214                $this->escape( $post );
     1215                $merged_content_struct = array_merge( $post, $content_struct );
     1216
     1217                $retval = $this->_insert_post( $user, $merged_content_struct );
     1218                if ( $retval instanceof IXR_Error )
     1219                        return $retval;
     1220
     1221                return true;
     1222        }
     1223
     1224        /**
     1225         * Delete a post for any registered post type.
     1226         *
     1227         * @since 3.4.0
     1228         *
     1229         * @uses wp_delete_post()
     1230         * @param array $args Method parameters. Contains:
     1231         *  - int     $blog_id
     1232         *  - string  $username
     1233         *  - string  $password
     1234         *  - int     $post_id
     1235         * @return true on success
     1236         */
     1237        function wp_deletePost( $args ) {
     1238                if ( ! $this->minimum_args( $args, 4 ) )
     1239                        return $this->error;
     1240
     1241                $this->escape( $args );
     1242
     1243                $blog_id    = (int) $args[0];
     1244                $username   = $args[1];
     1245                $password   = $args[2];
     1246                $post_id    = (int) $args[3];
     1247
     1248                if ( ! $user = $this->login( $username, $password ) )
     1249                        return $this->error;
     1250
     1251                do_action( 'xmlrpc_call', 'wp.deletePost' );
     1252
     1253                $post = wp_get_single_post( $post_id, ARRAY_A );
     1254                if ( empty( $post['ID'] ) )
     1255                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     1256
     1257                $post_type = get_post_type_object( $post['post_type'] );
     1258                if ( ! current_user_can( $post_type->cap->delete_post, $post_id ) )
     1259                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
     1260
     1261                $result = wp_delete_post( $post_id );
     1262
     1263                if ( ! $result )
     1264                        return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
     1265
     1266                return true;
     1267        }
     1268
     1269        /**
     1270         * Retrieve a post.
     1271         *
     1272         * @since 3.4.0
     1273         *
     1274         * The optional $fields parameter specifies what fields will be included
     1275         * in the response array. This should be a list of field names. 'post_id' will
     1276         * always be included in the response regardless of the value of $fields.
     1277         *
     1278         * Instead of, or in addition to, individual field names, conceptual group
     1279         * names can be used to specify multiple fields. The available conceptual
     1280         * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
     1281         * and 'enclosure'.
     1282         *
     1283         * @uses wp_get_single_post()
     1284         * @param array $args Method parameters. Contains:
     1285         *  - int     $post_id
     1286         *  - string  $username
     1287         *  - string  $password
     1288         *  - array   $fields optional
     1289         * @return array contains (based on $fields parameter):
     1290         *  - 'post_id'
     1291         *  - 'post_title'
     1292         *  - 'post_date'
     1293         *  - 'post_date_gmt'
     1294         *  - 'post_modified'
     1295         *  - 'post_modified_gmt'
     1296         *  - 'post_status'
     1297         *  - 'post_type'
     1298         *  - 'post_name'
     1299         *  - 'post_author'
     1300         *  - 'post_password'
     1301         *  - 'post_excerpt'
     1302         *  - 'post_content'
     1303         *  - 'link'
     1304         *  - 'comment_status'
     1305         *  - 'ping_status'
     1306         *  - 'sticky'
     1307         *  - 'custom_fields'
     1308         *  - 'terms'
     1309         *  - 'categories'
     1310         *  - 'tags'
     1311         *  - 'enclosure'
     1312         */
     1313        function wp_getPost( $args ) {
     1314                if ( ! $this->minimum_args( $args, 4 ) )
     1315                        return $this->error;
     1316
     1317                $this->escape( $args );
     1318
     1319                $blog_id            = (int) $args[0];
     1320                $username           = $args[1];
     1321                $password           = $args[2];
     1322                $post_id            = (int) $args[3];
     1323
     1324                if ( isset( $args[4] ) )
     1325                        $fields = $args[4];
     1326                else
     1327                        $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
     1328
     1329                if ( ! $user = $this->login( $username, $password ) )
     1330                        return $this->error;
     1331
     1332                do_action( 'xmlrpc_call', 'wp.getPost' );
     1333
     1334                $post = wp_get_single_post( $post_id, ARRAY_A );
     1335
     1336                if ( empty( $post['ID'] ) )
     1337                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     1338
     1339                $post_type = get_post_type_object( $post['post_type'] );
     1340                if ( ! current_user_can( $post_type->cap->edit_posts, $post_id ) )
     1341                        return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
     1342
     1343                return $this->_prepare_post( $post, $fields );
     1344        }
     1345
     1346        /**
     1347         * Retrieve posts.
     1348         *
     1349         * @since 3.4.0
     1350         *
     1351         * The optional $filter parameter modifies the query used to retrieve posts.
     1352         * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
     1353         * 'orderby', and 'order'.
     1354         *
     1355         * The optional $fields parameter specifies what fields will be included
     1356         * in the response array.
     1357         *
     1358         * @uses wp_get_recent_posts()
     1359         * @see wp_getPost() for more on $fields
     1360         * @see get_posts() for more on $filter values
     1361         *
     1362         * @param array $args Method parameters. Contains:
     1363         *  - int     $blog_id
     1364         *  - string  $username
     1365         *  - string  $password
     1366         *  - array   $filter optional
     1367         *  - array   $fields optional
     1368         * @return array contains a collection of posts.
     1369         */
     1370        function wp_getPosts( $args ) {
     1371                if ( ! $this->minimum_args( $args, 3 ) )
     1372                        return $this->error;
     1373
     1374                $this->escape( $args );
     1375
     1376                $blog_id    = (int) $args[0];
     1377                $username   = $args[1];
     1378                $password   = $args[2];
     1379                $filter     = isset( $args[3] ) ? $args[3] : array();
     1380
     1381                if ( isset( $args[4] ) )
     1382                        $fields = $args[4];
     1383                else
     1384                        $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
     1385
     1386                if ( ! $user = $this->login( $username, $password ) )
     1387                        return $this->error;
     1388
     1389                do_action( 'xmlrpc_call', 'wp.getPosts' );
     1390
     1391                $query = array();
     1392
     1393                if ( isset( $filter['post_type'] ) ) {
     1394                        $post_type = get_post_type_object( $filter['post_type'] );
     1395                        if ( ! ( (bool) $post_type ) )
     1396                                return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
     1397
     1398                        if ( ! current_user_can( $post_type->cap->edit_posts ) )
     1399                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
     1400
     1401                        $query['post_type'] = $filter['post_type'];
     1402                }
     1403
     1404                if ( isset( $filter['post_status'] ) )
     1405                        $query['post_status'] = $filter['post_status'];
     1406
     1407                if ( isset( $filter['number'] ) )
     1408                        $query['numberposts'] = absint( $filter['number'] );
     1409
     1410                if ( isset( $filter['offset'] ) )
     1411                        $query['offset'] = absint( $filter['offset'] );
     1412
     1413                if ( isset( $filter['orderby'] ) ) {
     1414                        $query['orderby'] = $filter['orderby'];
     1415
     1416                        if ( isset( $filter['order'] ) )
     1417                                $query['order'] = $filter['order'];
     1418                }
     1419
     1420                $posts_list = wp_get_recent_posts( $query );
     1421
     1422                if ( ! $posts_list )
     1423                        return array();
     1424
     1425                // holds all the posts data
     1426                $struct = array();
     1427
     1428                foreach ( $posts_list as $post ) {
     1429                        $post_type = get_post_type_object( $post['post_type'] );
     1430                        if ( ! current_user_can( $post_type->cap->edit_posts, $post['ID'] ) )
     1431                                continue;
     1432
     1433                        $struct[] = $this->_prepare_post( $post, $fields );
     1434                }
     1435
     1436                return $struct;
     1437        }
     1438
     1439        /**
     1440         * Create a new term.
     1441         *
     1442         * @since 3.4.0
     1443         *
     1444         * @uses wp_insert_term()
     1445         * @param array $args Method parameters. Contains:
     1446         *  - int     $blog_id
     1447         *  - string  $username
     1448         *  - string  $password
     1449         *  - array   $content_struct
     1450         *      The $content_struct must contain:
     1451         *      - 'name'
     1452         *      - 'taxonomy'
     1453         *      Also, it can optionally contain:
     1454         *      - 'parent'
     1455         *      - 'description'
     1456         *      - 'slug'
     1457         * @return string term_id
     1458         */
     1459        function wp_newTerm( $args ) {
     1460                if ( ! $this->minimum_args( $args, 4 ) )
     1461                        return $this->error;
     1462
     1463                $this->escape( $args );
     1464
     1465                $blog_id            = (int) $args[0];
     1466                $username           = $args[1];
     1467                $password           = $args[2];
     1468                $content_struct     = $args[3];
     1469
     1470                if ( ! $user = $this->login( $username, $password ) )
     1471                        return $this->error;
     1472
     1473                do_action( 'xmlrpc_call', 'wp.newTerm' );
     1474
     1475                if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
     1476                        return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
     1477
     1478                $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
     1479
     1480                if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
     1481                        return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
     1482
     1483                $taxonomy = (array) $taxonomy;
     1484
     1485                // hold the data of the term
     1486                $term_data = array();
     1487
     1488                $term_data['name'] = trim( $content_struct['name'] );
     1489                if ( empty( $term_data['name'] ) )
     1490                        return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
     1491
     1492                if ( isset( $content_struct['parent'] ) ) {
     1493                        if ( ! $taxonomy['hierarchical'] )
     1494                                return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
     1495
     1496                        $parent_term_id = (int) $content_struct['parent'];
     1497                        $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
     1498
     1499                        if ( is_wp_error( $parent_term ) )
     1500                                return new IXR_Error( 500, $parent_term->get_error_message() );
     1501
     1502                        if ( ! $parent_term )
     1503                                return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
     1504
     1505                        $term_data['parent'] = $content_struct['parent'];
     1506                }
     1507
     1508                if ( isset( $content_struct['description'] ) )
     1509                        $term_data['description'] = $content_struct['description'];
     1510
     1511                if ( isset( $content_struct['slug'] ) )
     1512                        $term_data['slug'] = $content_struct['slug'];
     1513
     1514                $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
     1515
     1516                if ( is_wp_error( $term ) )
     1517                        return new IXR_Error( 500, $term->get_error_message() );
     1518
     1519                if ( ! $term )
     1520                        return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
     1521
     1522                return strval( $term['term_id'] );
     1523        }
     1524
     1525        /**
     1526         * Edit a term.
     1527         *
     1528         * @since 3.4.0
     1529         *
     1530         * @uses wp_update_term()
     1531         * @param array $args Method parameters. Contains:
     1532         *  - int     $blog_id
     1533         *  - string  $username
     1534         *  - string  $password
     1535         *  - string  $term_id
     1536         *  - array   $content_struct
     1537         *      The $content_struct must contain:
     1538         *      - 'taxonomy'
     1539         *      Also, it can optionally contain:
     1540         *      - 'name'
     1541         *      - 'parent'
     1542         *      - 'description'
     1543         *      - 'slug'
     1544         * @return bool True, on success.
     1545         */
     1546        function wp_editTerm( $args ) {
     1547                if ( ! $this->minimum_args( $args, 5 ) )
     1548                        return $this->error;
     1549
     1550                $this->escape( $args );
     1551
     1552                $blog_id            = (int) $args[0];
     1553                $username           = $args[1];
     1554                $password           = $args[2];
     1555                $term_id            = (int) $args[3];
     1556                $content_struct     = $args[4];
     1557
     1558                if ( ! $user = $this->login( $username, $password ) )
     1559                        return $this->error;
     1560
     1561                do_action( 'xmlrpc_call', 'wp.editTerm' );
     1562
     1563                if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
     1564                        return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
     1565
     1566                $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
     1567
     1568                if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
     1569                        return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
     1570
     1571                $taxonomy = (array) $taxonomy;
     1572
     1573                // hold the data of the term
     1574                $term_data = array();
     1575
     1576                $term = get_term( $term_id , $content_struct['taxonomy'] );
     1577
     1578                if ( is_wp_error( $term ) )
     1579                        return new IXR_Error( 500, $term->get_error_message() );
     1580
     1581                if ( ! $term )
     1582                        return new IXR_Error( 404, __( 'Invalid term ID' ) );
     1583
     1584                if ( isset( $content_struct['name'] ) ) {
     1585                        $term_data['name'] = trim( $content_struct['name'] );
     1586
     1587                        if ( empty( $term_data['name'] ) )
     1588                                return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
     1589                }
     1590
     1591                if ( isset( $content_struct['parent'] ) ) {
     1592                        if ( ! $taxonomy['hierarchical'] )
     1593                                return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
     1594
     1595                        $parent_term_id = (int) $content_struct['parent'];
     1596                        $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
     1597
     1598                        if ( is_wp_error( $parent_term ) )
     1599                                return new IXR_Error( 500, $parent_term->get_error_message() );
     1600
     1601                        if ( ! $parent_term )
     1602                                return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
     1603
     1604                        $term_data['parent'] = $content_struct['parent'];
     1605                }
     1606
     1607                if ( isset( $content_struct['description'] ) )
     1608                        $term_data['description'] = $content_struct['description'];
     1609
     1610                if ( isset( $content_struct['slug'] ) )
     1611                        $term_data['slug'] = $content_struct['slug'];
     1612
     1613                $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
     1614
     1615                if ( is_wp_error( $term ) )
     1616                        return new IXR_Error( 500, $term->get_error_message() );
     1617
     1618                if ( ! $term )
     1619                        return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
     1620
     1621                return true;
     1622        }
     1623
     1624        /**
     1625         * Delete a term.
     1626         *
     1627         * @since 3.4.0
     1628         *
     1629         * @uses wp_delete_term()
     1630         * @param array $args Method parameters. Contains:
     1631         *  - int     $blog_id
     1632         *  - string  $username
     1633         *  - string  $password
     1634         *  - string  $taxnomy_name
     1635         *  - string     $term_id
     1636         * @return boolean|IXR_Error If it suceeded true else a reason why not
     1637         */
     1638        function wp_deleteTerm( $args ) {
     1639                if ( ! $this->minimum_args( $args, 5 ) )
     1640                        return $this->error;
     1641
     1642                $this->escape( $args );
     1643
     1644                $blog_id            = (int) $args[0];
     1645                $username           = $args[1];
     1646                $password           = $args[2];
     1647                $taxonomy           = $args[3];
     1648                $term_id            = (int) $args[4];
     1649
     1650                if ( ! $user = $this->login( $username, $password ) )
     1651                        return $this->error;
     1652
     1653                do_action( 'xmlrpc_call', 'wp.deleteTerm' );
     1654
     1655                if ( ! taxonomy_exists( $taxonomy ) )
     1656                        return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
     1657
     1658                $taxonomy = get_taxonomy( $taxonomy );
     1659
     1660                if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
     1661                        return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
     1662
     1663                $term = get_term( $term_id, $taxonomy->name );
     1664
     1665                if ( is_wp_error( $term ) )
     1666                        return new IXR_Error( 500, $term->get_error_message() );
     1667
     1668                if ( ! $term )
     1669                        return new IXR_Error( 404, __( 'Invalid term ID' ) );
     1670
     1671                $result = wp_delete_term( $term_id, $taxonomy->name );
     1672
     1673                if ( is_wp_error( $result ) )
     1674                        return new IXR_Error( 500, $term->get_error_message() );
     1675
     1676                if ( ! $result )
     1677                        return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
     1678
     1679                return $result;
     1680        }
     1681
     1682        /**
     1683         * Retrieve a term.
     1684         *
     1685         * @since 3.4.0
     1686         *
     1687         * @uses get_term()
     1688         * @param array $args Method parameters. Contains:
     1689         *  - int     $blog_id
     1690         *  - string  $username
     1691         *  - string  $password
     1692         *  - string  $taxonomy
     1693         *  - string  $term_id
     1694         * @return array contains:
     1695         *  - 'term_id'
     1696         *  - 'name'
     1697         *  - 'slug'
     1698         *  - 'term_group'
     1699         *  - 'term_taxonomy_id'
     1700         *  - 'taxonomy'
     1701         *  - 'description'
     1702         *  - 'parent'
     1703         *  - 'count'
     1704         */
     1705        function wp_getTerm( $args ) {
     1706                if ( ! $this->minimum_args( $args, 5 ) )
     1707                        return $this->error;
     1708
     1709                $this->escape( $args );
     1710
     1711                $blog_id            = (int) $args[0];
     1712                $username           = $args[1];
     1713                $password           = $args[2];
     1714                $taxonomy           = $args[3];
     1715                $term_id            = (int) $args[4];
     1716
     1717                if ( ! $user = $this->login( $username, $password ) )
     1718                        return $this->error;
     1719
     1720                do_action( 'xmlrpc_call', 'wp.getTerm' );
     1721
     1722                if ( ! taxonomy_exists( $taxonomy ) )
     1723                        return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
     1724
     1725                $taxonomy = get_taxonomy( $taxonomy );
     1726
     1727                if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
     1728                        return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
     1729
     1730                $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
     1731
     1732                if ( is_wp_error( $term ) )
     1733                        return new IXR_Error( 500, $term->get_error_message() );
     1734
     1735                if ( ! $term )
     1736                        return new IXR_Error( 404, __( 'Invalid term ID' ) );
     1737
     1738                return $this->_prepare_term( $term );
     1739        }
     1740
     1741        /**
     1742         * Retrieve all terms for a taxonomy.
     1743         *
     1744         * @since 3.4.0
     1745         *
     1746         * The optional $filter parameter modifies the query used to retrieve terms.
     1747         * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
     1748         *
     1749         * @uses get_terms()
     1750         * @param array $args Method parameters. Contains:
     1751         *  - int     $blog_id
     1752         *  - string  $username
     1753         *  - string  $password
     1754         *  - string  $taxonomy
     1755         *  - array   $filter optional
     1756         * @return array terms
     1757         */
     1758        function wp_getTerms( $args ) {
     1759                if ( ! $this->minimum_args( $args, 4 ) )
     1760                        return $this->error;
     1761
     1762                $this->escape( $args );
     1763
     1764                $blog_id        = (int) $args[0];
     1765                $username       = $args[1];
     1766                $password       = $args[2];
     1767                $taxonomy       = $args[3];
     1768                $filter         = isset( $args[4] ) ? $args[4] : array();
     1769
     1770                if ( ! $user = $this->login( $username, $password ) )
     1771                        return $this->error;
     1772
     1773                do_action( 'xmlrpc_call', 'wp.getTerms' );
     1774
     1775                if ( ! taxonomy_exists( $taxonomy ) )
     1776                        return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
     1777
     1778                $taxonomy = get_taxonomy( $taxonomy );
     1779
     1780                if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
     1781                        return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
     1782
     1783                $query = array();
     1784
     1785                if ( isset( $filter['number'] ) )
     1786                        $query['number'] = absint( $filter['number'] );
     1787
     1788                if ( isset( $filter['offset'] ) )
     1789                        $query['offset'] = absint( $filter['offset'] );
     1790
     1791                if ( isset( $filter['orderby'] ) ) {
     1792                        $query['orderby'] = $filter['orderby'];
     1793
     1794                        if ( isset( $filter['order'] ) )
     1795                                $query['order'] = $filter['order'];
     1796                }
     1797
     1798                if ( isset( $filter['hide_empty'] ) )
     1799                        $query['hide_empty'] = $filter['hide_empty'];
     1800                else
     1801                        $query['get'] = 'all';
     1802
     1803                if ( isset( $filter['search'] ) )
     1804                        $query['search'] = $filter['search'];
     1805
     1806                $terms = get_terms( $taxonomy->name, $query );
     1807
     1808                if ( is_wp_error( $terms ) )
     1809                        return new IXR_Error( 500, $terms->get_error_message() );
     1810
     1811                $struct = array();
     1812
     1813                foreach ( $terms as $term ) {
     1814                        $struct[] = $this->_prepare_term( $term );
     1815                }
     1816
     1817                return $struct;
     1818        }
     1819
     1820        /**
     1821         * Retrieve a taxonomy.
     1822         *
     1823         * @since 3.4.0
     1824         *
     1825         * @uses get_taxonomy()
     1826         * @param array $args Method parameters. Contains:
     1827         *  - int     $blog_id
     1828         *  - string  $username
     1829         *  - string  $password
     1830         *  - string  $taxonomy
     1831         * @return array (@see get_taxonomy())
     1832         */
     1833        function wp_getTaxonomy( $args ) {
     1834                if ( ! $this->minimum_args( $args, 4 ) )
     1835                        return $this->error;
     1836
     1837                $this->escape( $args );
     1838
     1839                $blog_id        = (int) $args[0];
     1840                $username       = $args[1];
     1841                $password       = $args[2];
     1842                $taxonomy       = $args[3];
     1843
     1844                if ( isset( $args[4] ) )
     1845                        $fields = $args[4];
     1846                else
     1847                        $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
     1848
     1849                if ( ! $user = $this->login( $username, $password ) )
     1850                        return $this->error;
     1851
     1852                do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
     1853
     1854                if ( ! taxonomy_exists( $taxonomy ) )
     1855                        return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
     1856
     1857                $taxonomy = get_taxonomy( $taxonomy );
     1858
     1859                if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
     1860                        return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
     1861
     1862                return $this->_prepare_taxonomy( $taxonomy, $fields );
     1863        }
     1864
     1865        /**
     1866         * Retrieve all taxonomies.
     1867         *
     1868         * @since 3.4.0
     1869         *
     1870         * @uses get_taxonomies()
     1871         * @param array $args Method parameters. Contains:
     1872         *  - int     $blog_id
     1873         *  - string  $username
     1874         *  - string  $password
     1875         * @return array taxonomies
     1876         */
     1877        function wp_getTaxonomies( $args ) {
     1878                if ( ! $this->minimum_args( $args, 3 ) )
     1879                        return $this->error;
     1880
     1881                $this->escape( $args );
     1882
     1883                $blog_id            = (int) $args[0];
     1884                $username           = $args[1];
     1885                $password           = $args[2];
     1886                $filter             = isset( $args[3] ) ? $args[3] : array( 'public' => true );
     1887
     1888                if ( isset( $args[4] ) )
     1889                        $fields = $args[4];
     1890                else
     1891                        $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
     1892
     1893                if ( ! $user = $this->login( $username, $password ) )
     1894                        return $this->error;
     1895
     1896                do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
     1897
     1898                $taxonomies = get_taxonomies( $filter, 'objects' );
     1899
     1900                // holds all the taxonomy data
     1901                $struct = array();
     1902
     1903                foreach ( $taxonomies as $taxonomy ) {
     1904                        // capability check for post_types
     1905                        if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
     1906                                continue;
     1907
     1908                        $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
    4351909                }
    4361910
    … …  
    4621936                }
    4631937
     1938                $page = get_page($page_id);
     1939                if ( ! $page )
     1940                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     1941
    4641942                if ( !current_user_can( 'edit_page', $page_id ) )
    4651943                        return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
    … …  
    4671945                do_action('xmlrpc_call', 'wp.getPage');
    4681946
    469                 // Lookup page info.
    470                 $page = get_page($page_id);
    471 
    4721947                // If we found the page then format the data.
    4731948                if ( $page->ID && ($page->post_type == 'page') ) {
    474                         // Get all of the page content and link.
    475                         $full_page = get_extended($page->post_content);
    476                         $link = post_permalink($page->ID);
    477 
    478                         // Get info the page parent if there is one.
    479                         $parent_title = "";
    480                         if ( !empty($page->post_parent) ) {
    481                                 $parent = get_page($page->post_parent);
    482                                 $parent_title = $parent->post_title;
    483                         }
    484 
    485                         // Determine comment and ping settings.
    486                         $allow_comments = comments_open($page->ID) ? 1 : 0;
    487                         $allow_pings = pings_open($page->ID) ? 1 : 0;
    488 
    489                         // Format page date.
    490                         $page_date = mysql2date('Ymd\TH:i:s', $page->post_date, false);
    491                         $page_date_gmt = mysql2date('Ymd\TH:i:s', $page->post_date_gmt, false);
    492 
    493                         // For drafts use the GMT version of the date
    494                         if ( $page->post_status == 'draft' )
    495                                 $page_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page->post_date ), 'Ymd\TH:i:s' );
    496 
    497                         // Pull the categories info together.
    498                         $categories = array();
    499                         foreach ( wp_get_post_categories($page->ID) as $cat_id ) {
    500                                 $categories[] = get_cat_name($cat_id);
    501                         }
    502 
    503                         // Get the author info.
    504                         $author = get_userdata($page->post_author);
    505 
    506                         $page_template = get_post_meta( $page->ID, '_wp_page_template', true );
    507                         if ( empty( $page_template ) )
    508                                 $page_template = 'default';
    509 
    510                         $page_struct = array(
    511                                 'dateCreated'                   => new IXR_Date($page_date),
    512                                 'userid'                                => $page->post_author,
    513                                 'page_id'                               => $page->ID,
    514                                 'page_status'                   => $page->post_status,
    515                                 'description'                   => $full_page['main'],
    516                                 'title'                                 => $page->post_title,
    517                                 'link'                                  => $link,
    518                                 'permaLink'                             => $link,
    519                                 'categories'                    => $categories,
    520                                 'excerpt'                               => $page->post_excerpt,
    521                                 'text_more'                             => $full_page['extended'],
    522                                 'mt_allow_comments'             => $allow_comments,
    523                                 'mt_allow_pings'                => $allow_pings,
    524                                 'wp_slug'                               => $page->post_name,
    525                                 'wp_password'                   => $page->post_password,
    526                                 'wp_author'                             => $author->display_name,
    527                                 'wp_page_parent_id'             => $page->post_parent,
    528                                 'wp_page_parent_title'  => $parent_title,
    529                                 'wp_page_order'                 => $page->menu_order,
    530                                 'wp_author_id'                  => (string) $author->ID,
    531                                 'wp_author_display_name'        => $author->display_name,
    532                                 'date_created_gmt'              => new IXR_Date($page_date_gmt),
    533                                 'custom_fields'                 => $this->get_custom_fields($page_id),
    534                                 'wp_page_template'              => $page_template
    535                         );
    536 
    537                         return($page_struct);
     1949                        return $this->_prepare_page( $page );
    5381950                }
    5391951                // If the page doesn't exist indicate that.
    … …  
    5781990                        $pages_struct = array();
    5791991
    580                         for ( $i = 0; $i < $num_pages; $i++ ) {
    581                                 $page = wp_xmlrpc_server::wp_getPage(array(
    582                                         $blog_id, $pages[$i]->ID, $username, $password
    583                                 ));
    584                                 $pages_struct[] = $page;
     1992                        foreach ($pages as $page) {
     1993                                if ( current_user_can( 'edit_page', $page->ID ) )
     1994                                        $pages_struct[] = $this->_prepare_page( $page );
    5851995                        }
    5861996
    … …  
    6132023                do_action('xmlrpc_call', 'wp.newPage');
    6142024
    615                 // Make sure the user is allowed to add new pages.
    616                 if ( !current_user_can('publish_pages') )
    617                         return(new IXR_Error(401, __('Sorry, you cannot add new pages.')));
    618 
    6192025                // Mark this as content for a page.
    6202026                $args[3]["post_type"] = 'page';
    … …  
    6592065                if ( !$result )
    6602066                        return(new IXR_Error(500, __('Failed to delete the page.')));
     2067
     2068                do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
    6612069
    6622070                return(true);
    … …  
    7512159                $num_pages = count($page_list);
    7522160                for ( $i = 0; $i < $num_pages; $i++ ) {
    753                         $post_date = mysql2date('Ymd\TH:i:s', $page_list[$i]->post_date, false);
    754                         $post_date_gmt = mysql2date('Ymd\TH:i:s', $page_list[$i]->post_date_gmt, false);
    755 
    756                         $page_list[$i]->dateCreated = new IXR_Date($post_date);
    757                         $page_list[$i]->date_created_gmt = new IXR_Date($post_date_gmt);
    758 
    759                         // For drafts use the GMT version of the date
    760                         if ( $page_list[$i]->post_status == 'draft' ) {
    761                                 $page_list[$i]->date_created_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page_list[$i]->post_date ), 'Ymd\TH:i:s' );
    762                                 $page_list[$i]->date_created_gmt = new IXR_Date( $page_list[$i]->date_created_gmt );
    763                         }
     2161                        $page_list[$i]->dateCreated = $this->_convert_date(  $page_list[$i]->post_date );
     2162                        $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
    7642163
    7652164                        unset($page_list[$i]->post_date_gmt);
    … …  
    8102209         * Get list of all tags
    8112210         *
    812          * @since 2.7
     2211         * @since 2.7.0
    8132212         *
    8142213         * @param array $args Method parameters.
    … …  
    8302229                do_action( 'xmlrpc_call', 'wp.getKeywords' );
    8312230
    832                 $tags = array( );
     2231                $tags = array();
    8332232
    8342233                if ( $all_tags = get_tags() ) {
    … …  
    9042303                }
    9052304
    906                 return($cat_id);
     2305                do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
     2306
     2307                return $cat_id;
    9072308        }
    9082309
    … …  
    9312332                        return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
    9322333
    933                 return wp_delete_term( $category_id, 'category' );
     2334                $status = wp_delete_term( $category_id, 'category' );
     2335
     2336                if( true == $status )
     2337                        do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
     2338
     2339                return $status;
    9342340        }
    9352341
    … …  
    9982404                        return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
    9992405
    1000                 // Format page date.
    1001                 $comment_date = mysql2date('Ymd\TH:i:s', $comment->comment_date, false);
    1002                 $comment_date_gmt = mysql2date('Ymd\TH:i:s', $comment->comment_date_gmt, false);
    1003 
    1004                 if ( '0' == $comment->comment_approved )
    1005                         $comment_status = 'hold';
    1006                 else if ( 'spam' == $comment->comment_approved )
    1007                         $comment_status = 'spam';
    1008                 else if ( '1' == $comment->comment_approved )
    1009                         $comment_status = 'approve';
    1010                 else
    1011                         $comment_status = $comment->comment_approved;
    1012 
    1013                 $link = get_comment_link($comment);
    1014 
    1015                 $comment_struct = array(
    1016                         'date_created_gmt'              => new IXR_Date($comment_date_gmt),
    1017                         'user_id'                               => $comment->user_id,
    1018                         'comment_id'                    => $comment->comment_ID,
    1019                         'parent'                                => $comment->comment_parent,
    1020                         'status'                                => $comment_status,
    1021                         'content'                               => $comment->comment_content,
    1022                         'link'                                  => $link,
    1023                         'post_id'                               => $comment->comment_post_ID,
    1024                         'post_title'                    => get_the_title($comment->comment_post_ID),
    1025                         'author'                                => $comment->comment_author,
    1026                         'author_url'                    => $comment->comment_author_url,
    1027                         'author_email'                  => $comment->comment_author_email,
    1028                         'author_ip'                             => $comment->comment_author_IP,
    1029                         'type'                                  => $comment->comment_type,
    1030                 );
    1031 
    1032                 return $comment_struct;
     2406                return $this->_prepare_comment( $comment );
    10332407        }
    10342408
    … …  
    10532427         */
    10542428        function wp_getComments($args) {
    1055                 $raw_args = $args;
    10562429                $this->escape($args);
    10572430
    … …  
    10592432                $username       = $args[1];
    10602433                $password       = $args[2];
    1061                 $struct         = $args[3];
     2434                $struct         = isset( $args[3] ) ? $args[3] : array();
    10622435
    10632436                if ( !$user = $this->login($username, $password) )
    … …  
    10872460
    10882461                $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
    1089                 $num_comments = count($comments);
    1090 
    1091                 if ( ! $num_comments )
    1092                         return array();
    10932462
    10942463                $comments_struct = array();
    10952464
    1096     // FIXME: we already have the comments, why query them again?
    1097                 for ( $i = 0; $i < $num_comments; $i++ ) {
    1098                         $comment = wp_xmlrpc_server::wp_getComment(array(
    1099                                 $raw_args[0], $raw_args[1], $raw_args[2], $comments[$i]->comment_ID,
    1100                         ));
    1101                         $comments_struct[] = $comment;
     2465                foreach ( $comments as $comment ) {
     2466                        $comments_struct[] = $this->_prepare_comment( $comment );
    11022467                }
    11032468
    … …  
    11432508                do_action('xmlrpc_call', 'wp.deleteComment');
    11442509
    1145                 return wp_delete_comment($comment_ID);
     2510                $status = wp_delete_comment( $comment_ID );
     2511
     2512                if( true == $status )
     2513                        do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
     2514
     2515                return $status;
    11462516        }
    11472517
    … …  
    12042574                // Do some timestamp voodoo
    12052575                if ( !empty( $content_struct['date_created_gmt'] ) ) {
    1206                         $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
     2576                        // We know this is supposed to be GMT, so we're going to slap that Z on there by force
     2577                        $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
    12072578                        $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
    12082579                        $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
    … …  
    12302601                if ( !$result )
    12312602                        return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
     2603
     2604                do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
    12322605
    12332606                return true;
    … …  
    13142687                do_action('xmlrpc_call', 'wp.newComment');
    13152688
    1316                 return wp_new_comment($comment);
     2689                $comment_ID = wp_new_comment( $comment );
     2690
     2691                do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
     2692
     2693                return $comment_ID;
    13172694        }
    13182695
    … …  
    13402717                do_action('xmlrpc_call', 'wp.getCommentStatusList');
    13412718
    1342                 return get_comment_statuses( );
     2719                return get_comment_statuses();
    13432720        }
    13442721
    … …  
    13992776                do_action('xmlrpc_call', 'wp.getPostStatusList');
    14002777
    1401                 return get_post_statuses( );
     2778                return get_post_statuses();
    14022779        }
    14032780
    … …  
    14252802                do_action('xmlrpc_call', 'wp.getPageStatusList');
    14262803
    1427                 return get_page_statuses( );
     2804                return get_page_statuses();
    14282805        }
    14292806
    … …  
    14492826                        return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
    14502827
    1451                 $templates = get_page_templates( );
     2828                $templates = get_page_templates();
    14522829                $templates['Default'] = 'default';
    14532830
    … …  
    14902867         */
    14912868        function _getOptions($options) {
    1492                 $data = array( );
     2869                $data = array();
    14932870                foreach ( $options as $option ) {
    14942871                        if ( array_key_exists( $option, $this->blog_options ) ) {
    … …  
    15742951
    15752952                if ( !current_user_can( 'upload_files' ) )
    1576                         return new IXR_Error( 403, __( 'You are not allowed to upload files to this site.' ) );
     2953                        return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
    15772954
    15782955                do_action('xmlrpc_call', 'wp.getMediaItem');
    … …  
    15812958                        return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
    15822959
    1583                 // Format page date.
    1584                 $attachment_date = mysql2date('Ymd\TH:i:s', $attachment->post_date, false);
    1585                 $attachment_date_gmt = mysql2date('Ymd\TH:i:s', $attachment->post_date_gmt, false);
    1586 
    1587                 $link = wp_get_attachment_url($attachment->ID);
    1588                 $thumbnail_link = wp_get_attachment_thumb_url($attachment->ID);
    1589 
    1590                 $attachment_struct = array(
    1591                         'date_created_gmt'              => new IXR_Date($attachment_date_gmt),
    1592                         'parent'                                => $attachment->post_parent,
    1593                         'link'                                  => $link,
    1594                         'thumbnail'                             => $thumbnail_link,
    1595                         'title'                                 => $attachment->post_title,
    1596                         'caption'                               => $attachment->post_excerpt,
    1597                         'description'                   => $attachment->post_content,
    1598                         'metadata'                              => wp_get_attachment_metadata($attachment->ID),
    1599                 );
    1600 
    1601                 return $attachment_struct;
     2960                return $this->_prepare_media_item( $attachment );
    16022961        }
    16032962
    … …  
    16262985         */
    16272986        function wp_getMediaLibrary($args) {
    1628                 $raw_args = $args;
    16292987                $this->escape($args);
    16302988
    … …  
    16382996
    16392997                if ( !current_user_can( 'upload_files' ) )
    1640                         return new IXR_Error( 401, __( 'Sorry, you cannot upload files.' ) );
     2998                        return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
    16412999
    16423000                do_action('xmlrpc_call', 'wp.getMediaLibrary');
    … …  
    16483006
    16493007                $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
    1650                 $num_attachments = count($attachments);
    1651 
    1652                 if ( ! $num_attachments )
    1653                         return array();
    16543008
    16553009                $attachments_struct = array();
    16563010
    16573011                foreach ($attachments as $attachment )
    1658                         $attachments_struct[] = $this->wp_getMediaItem( array( $raw_args[0], $raw_args[1], $raw_args[2], $attachment->ID ) );
     3012                        $attachments_struct[] = $this->_prepare_media_item( $attachment );
    16593013
    16603014                return $attachments_struct;
    … …  
    16823036                        return $this->error;
    16833037
     3038                if ( !current_user_can( 'edit_posts' ) )
     3039                        return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
     3040
    16843041                do_action( 'xmlrpc_call', 'wp.getPostFormats' );
    16853042
    … …  
    17013058
    17023059                return $formats;
     3060        }
     3061
     3062        /**
     3063         * Retrieves a post type
     3064         *
     3065         * @since 3.4.0
     3066         *
     3067         * @uses get_post_type_object()
     3068         * @param array $args Method parameters. Contains:
     3069         *  - int     $blog_id
     3070         *  - string  $username
     3071         *  - string  $password
     3072         *  - string  $post_type_name
     3073         *  - array   $fields
     3074         * @return array contains:
     3075         *  - 'labels'
     3076         *  - 'description'
     3077         *  - 'capability_type'
     3078         *  - 'cap'
     3079         *  - 'map_meta_cap'
     3080         *  - 'hierarchical'
     3081         *  - 'menu_position'
     3082         *  - 'taxonomies'
     3083         *  - 'supports'
     3084         */
     3085        function wp_getPostType( $args ) {
     3086                if ( ! $this->minimum_args( $args, 4 ) )
     3087                        return $this->error;
     3088
     3089                $this->escape( $args );
     3090
     3091                $blog_id        = (int) $args[0];
     3092                $username       = $args[1];
     3093                $password       = $args[2];
     3094                $post_type_name = $args[3];
     3095
     3096                if ( isset( $args[4] ) )
     3097                        $fields = $args[4];
     3098                else
     3099                        $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
     3100
     3101                if ( !$user = $this->login( $username, $password ) )
     3102                        return $this->error;
     3103
     3104                do_action( 'xmlrpc_call', 'wp.getPostType' );
     3105
     3106                if( ! post_type_exists( $post_type_name ) )
     3107                        return new IXR_Error( 403, __( 'Invalid post type' ) );
     3108
     3109                $post_type = get_post_type_object( $post_type_name );
     3110
     3111                if( ! current_user_can( $post_type->cap->edit_posts ) )
     3112                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
     3113
     3114                return $this->_prepare_post_type( $post_type, $fields );
     3115        }
     3116
     3117        /**
     3118         * Retrieves a post types
     3119         *
     3120         * @since 3.4.0
     3121         *
     3122         * @uses get_post_types()
     3123         * @param array $args Method parameters. Contains:
     3124         *  - int     $blog_id
     3125         *  - string  $username
     3126         *  - string  $password
     3127         *  - array   $filter
     3128         *  - array   $fields
     3129         * @return array
     3130         */
     3131        function wp_getPostTypes( $args ) {
     3132                if ( ! $this->minimum_args( $args, 3 ) )
     3133                        return $this->error;
     3134
     3135                $this->escape( $args );
     3136
     3137                $blog_id            = (int) $args[0];
     3138                $username           = $args[1];
     3139                $password           = $args[2];
     3140                $filter             = isset( $args[3] ) ? $args[3] : array( 'public' => true );
     3141
     3142                if ( isset( $args[4] ) )
     3143                        $fields = $args[4];
     3144                else
     3145                        $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
     3146
     3147                if ( ! $user = $this->login( $username, $password ) )
     3148                        return $this->error;
     3149
     3150                do_action( 'xmlrpc_call', 'wp.getPostTypes' );
     3151
     3152                $post_types = get_post_types( $filter, 'objects' );
     3153
     3154                $struct = array();
     3155
     3156                foreach( $post_types as $post_type ) {
     3157                        if( ! current_user_can( $post_type->cap->edit_posts ) )
     3158                                continue;
     3159
     3160                        $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
     3161                }
     3162
     3163                return $struct;
    17033164        }
    17043165
    … …  
    18283289                        return $this->error;
    18293290
     3291                $post_data = wp_get_single_post($post_ID, ARRAY_A);
     3292                if ( ! $post_data )
     3293                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     3294
    18303295                if ( !current_user_can( 'edit_post', $post_ID ) )
    18313296                        return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
    18323297
    18333298                do_action('xmlrpc_call', 'blogger.getPost');
    1834 
    1835                 $post_data = wp_get_single_post($post_ID, ARRAY_A);
    18363299
    18373300                $categories = implode(',', wp_get_post_categories($post_ID));
    … …  
    18433306                $struct = array(
    18443307                        'userid'    => $post_data['post_author'],
    1845                         'dateCreated' => new IXR_Date(mysql2date('Ymd\TH:i:s', $post_data['post_date'], false)),
     3308                        'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
    18463309                        'content'     => $content,
    18473310                        'postid'  => (string) $post_data['ID']
    … …  
    18883351                                continue;
    18893352
    1890                         $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
     3353                        $post_date  = $this->_convert_date( $entry['post_date'] );
    18913354                        $categories = implode(',', wp_get_post_categories($entry['ID']));
    18923355
    … …  
    18973360                        $struct[] = array(
    18983361                                'userid' => $entry['post_author'],
    1899                                 'dateCreated' => new IXR_Date($post_date),
     3362                                'dateCreated' => $post_date,
    19003363                                'content' => $content,
    19013364                                'postid' => (string) $entry['ID'],
    … …  
    19353398
    19363399                if ( !current_user_can('edit_themes') )
    1937                         return new IXR_Error(401, __('Sorry, this user can not edit the template.'));
     3400                        return new IXR_Error(401, __('Sorry, this user cannot edit the template.'));
    19383401
    19393402                /* warning: here we make the assumption that the blog's URL is on the same server */
    … …  
    19463409
    19473410                /* so it is actually editable with a windows/mac client */
    1948                 // FIXME: (or delete me) do we really want to cater to bad clients at the expense of good ones by BEEPing up their line breaks? commented.     $content = str_replace("\n", "\r\n", $content);
     3411                // FIXME: (or delete me) do we really want to cater to bad clients at the expense of good ones by BEEPing up their line breaks? commented. $content = str_replace("\n", "\r\n", $content);
    19493412
    19503413                return $content;
    … …  
    20403503                $this->attach_uploads( $post_ID, $post_content );
    20413504
    2042                 logIO('O', "Posted ! ID: $post_ID");
     3505                do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
    20433506
    20443507                return $post_ID;
    … …  
    20963559                $this->attach_uploads( $ID, $post_content );
    20973560
     3561                do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
     3562
    20983563                return true;
    20993564        }
    … …  
    21323597                if ( !$result )
    21333598                        return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
     3599
     3600                do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
    21343601
    21353602                return true;
    … …  
    21633630         *  - date_created_gmt
    21643631         *  - dateCreated
     3632         *  - wp_post_thumbnail
    21653633         *
    21663634         * @since 1.5.0
    … …  
    21773645                $this->escape($args);
    21783646
    2179                 $blog_ID     = (int) $args[0]; // we will support this in the near future
     3647                $blog_ID     = (int) $args[0];
    21803648                $username  = $args[1];
    21813649                $password   = $args[2];
    … …  
    21933661                                if ( $publish )
    21943662                                        $cap  = 'publish_pages';
    2195                                 elseif ('publish' == $content_struct['page_status'])
     3663                                elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
    21963664                                        $cap  = 'publish_pages';
    21973665                                else
    … …  
    22043672                                if ( $publish )
    22053673                                        $cap  = 'publish_posts';
    2206                                 elseif ('publish' == $content_struct['post_status'])
     3674                                elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
    22073675                                        $cap  = 'publish_posts';
    22083676                                else
    … …  
    22123680                        } else {
    22133681                                // No other post_type values are allowed here
    2214                                 return new IXR_Error( 401, __( 'Invalid post type.' ) );
     3682                                return new IXR_Error( 401, __( 'Invalid post type' ) );
    22153683                        }
    22163684                } else {
    22173685                        if ( $publish )
    22183686                                $cap  = 'publish_posts';
    2219                         elseif ('publish' == $content_struct['post_status'])
     3687                        elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
    22203688                                $cap  = 'publish_posts';
    22213689                        else
    … …  
    22683736                                        break;
    22693737                                default:
    2270                                         return(new IXR_Error(401, __('Invalid post type.')));
     3738                                        return(new IXR_Error(401, __('Invalid post type')));
    22713739                                        break;
    22723740                        }
     3741                        $author = get_userdata( $content_struct['wp_author_id'] );
     3742                        if ( ! $author )
     3743                                return new IXR_Error( 404, __( 'Invalid author ID.' ) );
    22733744                        $post_author = $content_struct['wp_author_id'];
    22743745                }
    … …  
    23713842                // Do some timestamp voodoo
    23723843                if ( !empty( $content_struct['date_created_gmt'] ) )
    2373                         $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
     3844                        // We know this is supposed to be GMT, so we're going to slap that Z on there by force
     3845                        $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
    23743846                elseif ( !empty( $content_struct['dateCreated']) )
    23753847                        $dateCreated = $content_struct['dateCreated']->getIso();
    … …  
    23863858                if ( isset( $content_struct['categories'] ) ) {
    23873859                        $catnames = $content_struct['categories'];
    2388                         logIO('O', 'Post cats: ' . var_export($catnames,true));
    23893860
    23903861                        if ( is_array($catnames) ) {
    … …  
    24103881                        $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
    24113882
     3883                if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
     3884                        if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
     3885                                return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
     3886
     3887                        unset( $content_struct['wp_post_thumbnail'] );
     3888                }
     3889
    24123890                // Handle enclosures
    24133891                $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
    … …  
    24283906                        return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
    24293907
    2430                 logIO('O', "Posted ! ID: $post_ID");
     3908                do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
    24313909
    24323910                return strval($post_ID);
    … …  
    24863964                $this->escape($args);
    24873965
    2488                 $post_ID     = (int) $args[0];
    2489                 $username  = $args[1];
    2490                 $password   = $args[2];
     3966                $post_ID        = (int) $args[0];
     3967                $username       = $args[1];
     3968                $password       = $args[2];
    24913969                $content_struct = $args[3];
    2492                 $publish     = $args[4];
    2493 
    2494                 if ( !$user = $this->login($username, $password) )
     3970                $publish        = isset( $args[4] ) ? $args[4] : 0;
     3971
     3972                if ( ! $user = $this->login($username, $password) )
    24953973                        return $this->error;
    24963974
    24973975                do_action('xmlrpc_call', 'metaWeblog.editPost');
    24983976
    2499                 $cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
    2500                 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
    2501                 $post_type = 'post';
    2502                 $page_template = '';
    2503                 if ( !empty( $content_struct['post_type'] ) ) {
    2504                         if ( $content_struct['post_type'] == 'page' ) {
    2505                                 if ( $publish || 'publish' == $content_struct['page_status'] )
    2506                                         $cap  = 'publish_pages';
    2507                                 else
    2508                                         $cap = 'edit_pages';
    2509                                 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
    2510                                 $post_type = 'page';
    2511                                 if ( !empty( $content_struct['wp_page_template'] ) )
    2512                                         $page_template = $content_struct['wp_page_template'];
    2513                         } elseif ( $content_struct['post_type'] == 'post' ) {
    2514                                 if ( $publish || 'publish' == $content_struct['post_status'] )
    2515                                         $cap  = 'publish_posts';
    2516                                 else
    2517                                         $cap = 'edit_posts';
    2518                                 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
    2519                                 $post_type = 'post';
    2520                         } else {
    2521                                 // No other post_type values are allowed here
    2522                                 return new IXR_Error( 401, __( 'Invalid post type.' ) );
    2523                         }
    2524                 } else {
    2525                         if ( $publish || 'publish' == $content_struct['post_status'] )
    2526                                 $cap  = 'publish_posts';
    2527                         else
    2528                                 $cap = 'edit_posts';
    2529                         $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
    2530                         $post_type = 'post';
    2531                 }
    2532 
    2533                 if ( !current_user_can( $cap ) )
    2534                         return new IXR_Error( 401, $error_message );
     3977                $postdata = wp_get_single_post( $post_ID, ARRAY_A );
     3978
     3979                // If there is no post data for the give post id, stop
     3980                // now and return an error. Other wise a new post will be
     3981                // created (which was the old behavior).
     3982                if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
     3983                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     3984
     3985                if ( ! current_user_can( 'edit_post', $post_ID ) )
     3986                        return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this post.' ) );
     3987
     3988                // Use wp.editPost to edit post types other than post and page.
     3989                if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
     3990                        return new IXR_Error( 401, __( 'Invalid post type' ) );
     3991
     3992                // Thwart attempt to change the post type.
     3993                if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
     3994                        return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
    25353995
    25363996                // Check for a valid post format if one was given
    … …  
    25424002                }
    25434003
    2544                 $postdata = wp_get_single_post($post_ID, ARRAY_A);
    2545 
    2546                 // If there is no post data for the give post id, stop
    2547                 // now and return an error.  Other wise a new post will be
    2548                 // created (which was the old behavior).
    2549                 if ( empty($postdata["ID"]) )
    2550                         return(new IXR_Error(404, __('Invalid post ID.')));
    2551 
    25524004                $this->escape($postdata);
    25534005                extract($postdata, EXTR_SKIP);
    … …  
    25704022                if ( isset($content_struct['wp_page_order']) )
    25714023                        $menu_order = $content_struct['wp_page_order'];
     4024
     4025                if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
     4026                        $page_template = $content_struct['wp_page_template'];
    25724027
    25734028                $post_author = $postdata['post_author'];
    … …  
    25854040                                        break;
    25864041                                default:
    2587                                         return(new IXR_Error(401, __('Invalid post type.')));
     4042                                        return(new IXR_Error(401, __('Invalid post type')));
    25884043                                        break;
    25894044                        }
    … …  
    26484103                }
    26494104
    2650                 $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
    2651                 $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
     4105                if ( isset( $content_struct['title'] ) )
     4106                        $post_title =  $content_struct['title'];
     4107
     4108                if ( isset( $content_struct['description'] ) )
     4109                        $post_content = $content_struct['description'];
    26524110
    26534111                $post_category = array();
    … …  
    26614119                }
    26624120
    2663                 $post_excerpt = isset( $content_struct['mt_excerpt'] ) ? $content_struct['mt_excerpt'] : null;
     4121                if ( isset( $content_struct['mt_excerpt'] ) )
     4122                        $post_excerpt =  $content_struct['mt_excerpt'];
     4123
    26644124                $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
    26654125
    … …  
    27004160                // Do some timestamp voodoo
    27014161                if ( !empty( $content_struct['date_created_gmt'] ) )
    2702                         $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
     4162                        // We know this is supposed to be GMT, so we're going to slap that Z on there by force
     4163                        $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
    27034164                elseif ( !empty( $content_struct['dateCreated']) )
    27044165                        $dateCreated = $content_struct['dateCreated']->getIso();
    … …  
    27334194                        $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
    27344195
     4196                if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
     4197                        // empty value deletes, non-empty value adds/updates
     4198                        if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
     4199                                delete_post_thumbnail( $post_ID );
     4200                        } else {
     4201                                if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
     4202                                        return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
     4203                        }
     4204                        unset( $content_struct['wp_post_thumbnail'] );
     4205                }
     4206
    27354207                // Handle enclosures
    27364208                $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
    … …  
    27444216                        wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
    27454217
    2746                 logIO('O',"(MW) Edited ! ID: $post_ID");
     4218                do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
    27474219
    27484220                return true;
    … …  
    27684240                        return $this->error;
    27694241
     4242                $postdata = wp_get_single_post($post_ID, ARRAY_A);
     4243                if ( ! $postdata )
     4244                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     4245
    27704246                if ( !current_user_can( 'edit_post', $post_ID ) )
    27714247                        return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
    … …  
    27734249                do_action('xmlrpc_call', 'metaWeblog.getPost');
    27744250
    2775                 $postdata = wp_get_single_post($post_ID, ARRAY_A);
    2776 
    27774251                if ($postdata['post_date'] != '') {
    2778                         $post_date = mysql2date('Ymd\TH:i:s', $postdata['post_date'], false);
    2779                         $post_date_gmt = mysql2date('Ymd\TH:i:s', $postdata['post_date_gmt'], false);
    2780 
    2781                         // For drafts use the GMT version of the post date
    2782                         if ( $postdata['post_status'] == 'draft' )
    2783                                 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $postdata['post_date'] ), 'Ymd\TH:i:s' );
     4252                        $post_date = $this->_convert_date( $postdata['post_date'] );
     4253                        $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'],  $postdata['post_date'] );
     4254                        $post_modified = $this->_convert_date( $postdata['post_modified'] );
     4255                        $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
    27844256
    27854257                        $categories = array();
    … …  
    28244296                                if ($key == 'enclosure') {
    28254297                                        foreach ( (array) $val as $enc ) {
    2826                                                 $encdata = split("\n", $enc);
     4298                                                $encdata = explode("\n", $enc);
    28274299                                                $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
    28284300                                                $enclosure['length'] = (int) trim($encdata[1]);
    … …  
    28344306
    28354307                        $resp = array(
    2836                                 'dateCreated' => new IXR_Date($post_date),
     4308                                'dateCreated' => $post_date,
    28374309                                'userid' => $postdata['post_author'],
    28384310                                'postid' => $postdata['ID'],
    … …  
    28464318                                'mt_excerpt' => $postdata['post_excerpt'],
    28474319                                'mt_text_more' => $post['extended'],
     4320                                'wp_more_text' => $post['more_text'],
    28484321                                'mt_allow_comments' => $allow_comments,
    28494322                                'mt_allow_pings' => $allow_pings,
    … …  
    28524325                                'wp_password' => $postdata['post_password'],
    28534326                                'wp_author_id' => (string) $author->ID,
    2854                                 'wp_author_display_name'        => $author->display_name,
    2855                                 'date_created_gmt' => new IXR_Date($post_date_gmt),
     4327                                'wp_author_display_name' => $author->display_name,
     4328                                'date_created_gmt' => $post_date_gmt,
    28564329                                'post_status' => $postdata['post_status'],
    28574330                                'custom_fields' => $this->get_custom_fields($post_ID),
    28584331                                'wp_post_format' => $post_format,
    2859                                 'sticky' => $sticky
     4332                                'sticky' => $sticky,
     4333                                'date_modified' => $post_modified,
     4334                                'date_modified_gmt' => $post_modified_gmt
    28604335                        );
    28614336
    28624337                        if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
     4338
     4339                        $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
    28634340
    28644341                        return $resp;
    … …  
    28964373
    28974374                if ( !$posts_list )
    2898                         return array( );
     4375                        return array();
    28994376
    29004377                foreach ($posts_list as $entry) {
    … …  
    29024379                                continue;
    29034380
    2904                         $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
    2905                         $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt'], false);
    2906 
    2907                         // For drafts use the GMT version of the date
    2908                         if ( $entry['post_status'] == 'draft' )
    2909                                 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s' );
     4381                        $post_date = $this->_convert_date( $entry['post_date'] );
     4382                        $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
     4383                        $post_modified = $this->_convert_date( $entry['post_modified'] );
     4384                        $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
    29104385
    29114386                        $categories = array();
    … …  
    29444419
    29454420                        $struct[] = array(
    2946                                 'dateCreated' => new IXR_Date($post_date),
     4421                                'dateCreated' => $post_date,
    29474422                                'userid' => $entry['post_author'],
    29484423                                'postid' => (string) $entry['ID'],
    … …  
    29564431                                'mt_excerpt' => $entry['post_excerpt'],
    29574432                                'mt_text_more' => $post['extended'],
     4433                                'wp_more_text' => $post['more_text'],
    29584434                                'mt_allow_comments' => $allow_comments,
    29594435                                'mt_allow_pings' => $allow_pings,
    … …  
    29634439                                'wp_author_id' => (string) $author->ID,
    29644440                                'wp_author_display_name' => $author->display_name,
    2965                                 'date_created_gmt' => new IXR_Date($post_date_gmt),
     4441                                'date_created_gmt' => $post_date_gmt,
    29664442                                'post_status' => $entry['post_status'],
    29674443                                'custom_fields' => $this->get_custom_fields($entry['ID']),
    2968                                 'wp_post_format' => $post_format
     4444                                'wp_post_format' => $post_format,
     4445                                'date_modified' => $post_modified,
     4446                                'date_modified_gmt' => $post_modified_gmt
    29694447                        );
    29704448
     4449                        $entry_index = count( $struct ) - 1;
     4450                        $struct[ $entry_index ][ 'wp_post_thumbnail' ] = get_post_thumbnail_id( $entry['ID'] );
    29714451                }
    29724452
    … …  
    30464526                $bits = $data['bits'];
    30474527
    3048                 logIO('O', '(MW) Received '.strlen($bits).' bytes');
    3049 
    30504528                if ( !$user = $this->login($username, $password) )
    30514529                        return $this->error;
    … …  
    30544532
    30554533                if ( !current_user_can('upload_files') ) {
    3056                         logIO('O', '(MW) User does not have upload_files capability');
    3057                         $this->error = new IXR_Error(401, __('You are not allowed to upload files to this site.'));
     4534                        $this->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
    30584535                        return $this->error;
    30594536                }
    … …  
    30804557                }
    30814558
    3082                 $upload = wp_upload_bits($name, NULL, $bits);
     4559                $upload = wp_upload_bits($name, null, $bits);
    30834560                if ( ! empty($upload['error']) ) {
    30844561                        $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
    3085                         logIO('O', '(MW) ' . $errorString);
    30864562                        return new IXR_Error(500, $errorString);
    30874563                }
    … …  
    31024578                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    31034579
    3104                 return apply_filters( 'wp_handle_upload', array( 'file' => $name, 'url' => $upload[ 'url' ], 'type' => $type ), 'upload' );
     4580                do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
     4581
     4582                $struct = array(
     4583                        'id'   => strval( $id ),
     4584                        'file' => $name,
     4585                        'url'  => $upload[ 'url' ],
     4586                        'type' => $type
     4587                );
     4588                return apply_filters( 'wp_handle_upload', $struct, 'upload' );
    31054589        }
    31064590
    … …  
    31414625                }
    31424626
     4627                $struct = array();
     4628
    31434629                foreach ($posts_list as $entry) {
    31444630                        if ( !current_user_can( 'edit_post', $entry['ID'] ) )
    31454631                                continue;
    31464632
    3147                         $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
    3148                         $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt'], false);
    3149 
    3150                         // For drafts use the GMT version of the date
    3151                         if ( $entry['post_status'] == 'draft' )
    3152                                 $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s' );
     4633                        $post_date = $this->_convert_date( $entry['post_date'] );
     4634                        $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
    31534635
    31544636                        $struct[] = array(
    3155                                 'dateCreated' => new IXR_Date($post_date),
     4637                                'dateCreated' => $post_date,
    31564638                                'userid' => $entry['post_author'],
    31574639                                'postid' => (string) $entry['ID'],
    31584640                                'title' => $entry['post_title'],
    31594641                                'post_status' => $entry['post_status'],
    3160                                 'date_created_gmt' => new IXR_Date($post_date_gmt)
     4642                                'date_created_gmt' => $post_date_gmt
    31614643                        );
    31624644
    … …  
    32274709                if ( !$user = $this->login($username, $password) )
    32284710                        return $this->error;
     4711
     4712                if ( ! get_post( $post_ID ) )
     4713                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
    32294714
    32304715                if ( !current_user_can( 'edit_post', $post_ID ) )
    … …  
    32714756                do_action('xmlrpc_call', 'mt.setPostCategories');
    32724757
     4758                if ( ! get_post( $post_ID ) )
     4759                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     4760
    32734761                if ( !current_user_can('edit_post', $post_ID) )
    32744762                        return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
    … …  
    33784866                do_action('xmlrpc_call', 'mt.publishPost');
    33794867
     4868                $postdata = wp_get_single_post($post_ID, ARRAY_A);
     4869                if ( ! $postdata )
     4870                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     4871
    33804872                if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
    33814873                        return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
    3382 
    3383                 $postdata = wp_get_single_post($post_ID,ARRAY_A);
    33844874
    33854875                $postdata['post_status'] = 'publish';
    … …  
    34704960                $post_ID = (int) $post_ID;
    34714961
    3472 
    3473                 logIO("O","(PB) URL='$pagelinkedto' ID='$post_ID' Found='$way'");
    3474 
    34754962                $post = get_post($post_ID);
    34764963
    … …  
    36145101        }
    36155102}
    3616 ?>
Note: See TracChangeset for help on using the changeset viewer.