Make WordPress Core

Changeset 19848


Ignore:
Timestamp:
02/07/2012 11:33:39 AM (15 years ago)
Author:
westi
Message:

XMLRPC: Introduce new create,read,update and delete XMLRPC apis for Posts, Pages and all Custom Post Types.
Introduces: wp.newPost, wp.editPost, wp.deletePost, wp.getPost, and wp.getPosts
See #18429, #18430, #18431, #18432, and #18433 props maxcutler and markoheijnen.

File:
1 edited

Legend:

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

    r19726 r19848  
    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',
    3944                        'wp.getPage'                    => 'this:wp_getPage',
    4045                        'wp.getPages'                   => 'this:wp_getPages',
     
    448453
    449454        /**
     455         * Prepares post data for return in an XML-RPC object.
     456         *
     457         * @access private
     458        .*
     459         * @param array $post The unprepared post data
     460         * @param array $fields The subset of post fields to return
     461         * @return array The prepared post data
     462         */
     463        function _prepare_post( $post, $fields ) {
     464                // holds the data for this post. built up based on $fields
     465                $_post = array( 'post_id' => $post['ID'] );
     466
     467                // prepare common post fields
     468                $post_fields = array(
     469                        'post_title'        => $post['post_title'],
     470                        'post_date'         => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_date'], false ) ),
     471                        'post_date_gmt'     => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_date_gmt'], false ) ),
     472                        'post_modified'     => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_modified'], false ) ),
     473                        'post_modified_gmt' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_modified_gmt'], false ) ),
     474                        'post_status'       => $post['post_status'],
     475                        'post_type'         => $post['post_type'],
     476                        'post_name'         => $post['post_name'],
     477                        'post_author'       => $post['post_author'],
     478                        'post_password'     => $post['post_password'],
     479                        'post_excerpt'      => $post['post_excerpt'],
     480                        'post_content'      => $post['post_content'],
     481                        'link'              => post_permalink( $post['ID'] ),
     482                        'comment_status'    => $post['comment_status'],
     483                        'ping_status'       => $post['ping_status'],
     484                        'sticky'            => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
     485                );
     486
     487                // Consider future posts as published
     488                if ( $post_fields['post_status'] === 'future' )
     489                        $post_fields['post_status'] = 'publish';
     490
     491                // Fill in blank post format
     492                $post_fields['post_format'] = get_post_format( $post['ID'] );
     493                if ( empty( $post_fields['post_format'] ) )
     494                        $post_fields['post_format'] = 'standard';
     495
     496                // Merge requested $post_fields fields into $_post
     497                if ( in_array( 'post', $fields ) ) {
     498                        $_post = array_merge( $_post, $post_fields );
     499                } else {
     500                        $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
     501                        $_post = array_merge( $_post, $requested_fields );
     502                }
     503
     504                $all_taxonomy_fields = in_array( 'taxonomies', $fields );
     505
     506                if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
     507                        $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
     508                        $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
     509                        $_post['terms'] = array();
     510                        foreach ( $terms as $term ) {
     511                                $_post['terms'][] = (array) $term;
     512                        }
     513                }
     514
     515                // backward compatiblity
     516                if ( $all_taxonomy_fields || in_array( 'tags', $fields ) ) {
     517                        $tagnames = array();
     518                        $tags = wp_get_post_tags( $post['ID'] );
     519                        if ( ! empty( $tags ) ) {
     520                                foreach ( $tags as $tag ) {
     521                                        $tagnames[] = $tag->name;
     522                                }
     523                                $tagnames = implode( ', ', $tagnames );
     524                        } else {
     525                                $tagnames = '';
     526                        }
     527                        $_post['tags'] = $tagnames;
     528                }
     529
     530                // backward compatiblity
     531                if ( $all_taxonomy_fields || in_array( 'categories', $fields ) ) {
     532                        $categories = array();
     533                        $catids = wp_get_post_categories( $post['ID'] );
     534                        foreach ( $catids as $catid ) {
     535                                $categories[] = get_cat_name( $catid );
     536                        }
     537                        $_post['categories'] = $categories;
     538                }
     539
     540                if ( in_array( 'custom_fields', $fields ) )
     541                        $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
     542
     543                if ( in_array( 'enclosure', $fields ) ) {
     544                        $_post['enclosure'] = array();
     545                        $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
     546                        if ( ! empty( $enclosures ) ) {
     547                                $encdata = explode( "\n", $enclosures[0] );
     548                                $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
     549                                $_post['enclosure']['length'] = (int) trim( $encdata[1] );
     550                                $_post['enclosure']['type'] = trim( $encdata[2] );
     551                        }
     552                }
     553
     554                return apply_filters( 'xmlrpc__prepare_post', $_post, $post, $fields );
     555        }
     556
     557        /**
     558         * Create a new post for any registered post type.
     559         *
     560         * @uses wp_insert_post()
     561         * @param array $args Method parameters. Contains:
     562         *  - int     $blog_id
     563         *  - string  $username
     564         *  - string  $password
     565         *  - array   $content_struct
     566         *      $content_struct can contain:
     567         *      - post_type (default: 'post')
     568         *      - post_status (default: 'draft')
     569         *      - post_title
     570         *      - post_author
     571         *      - post_exerpt
     572         *      - post_content
     573         *      - post_date_gmt | post_date
     574         *      - post_format
     575         *      - post_password
     576         *      - comment_status - can be 'open' | 'closed'
     577         *      - ping_status - can be 'open' | 'closed'
     578         *      - sticky
     579         *      - custom_fields - array, with each element containing 'key' and 'value'
     580         *      - terms - array, with taxonomy names as keys and arrays of term IDs as values
     581         *      - terms_names - array, with taxonomy names as keys and arrays of term names as values
     582         *      - enclosure
     583         *      - any other fields supported by wp_insert_post()
     584         * @return string post_id
     585         */
     586        function wp_newPost( $args ) {
     587                $this->escape( $args );
     588
     589                $blog_id        = (int) $args[0];
     590                $username       = $args[1];
     591                $password       = $args[2];
     592                $content_struct = $args[3];
     593
     594                if ( ! $user = $this->login( $username, $password ) )
     595                        return $this->error;
     596
     597                do_action( 'xmlrpc_call', 'wp.newPost' );
     598
     599                unset( $content_struct['ID'] );
     600
     601                return $this->_wp_insertPost( $user, $content_struct );
     602        }
     603
     604        /*
     605         * Helper method for filtering out elements from an array.
     606         */
     607        function _is_greater_than_one( $count ){
     608                return $count > 1;
     609        }
     610
     611        /*
     612         * Helper method for wp_newPost and wp_editPost, containing shared logic.
     613         */
     614        function _wp_insertPost( $user, $content_struct ) {
     615                $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
     616                        'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '', 'sticky' => 0 );
     617
     618                $post_data = wp_parse_args( $content_struct, $defaults );
     619
     620                $post_type = get_post_type_object( $post_data['post_type'] );
     621                if( ! ( (bool) $post_type ) )
     622                        return new IXR_Error( 403, __( 'Invalid post type' ) );
     623
     624                if( ! current_user_can( $post_type->cap->edit_posts ) )
     625                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
     626
     627                switch ( $post_data['post_status'] ) {
     628                        case 'draft':
     629                        case 'pending':
     630                                break;
     631                        case 'private':
     632                                if( ! current_user_can( $post_type->cap->publish_posts ) )
     633                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ));
     634                                break;
     635                        case 'publish':
     636                        case 'future':
     637                                if( ! current_user_can( $post_type->cap->publish_posts ) )
     638                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ));
     639                                break;
     640                        default:
     641                                $post_data['post_status'] = 'draft';
     642                        break;
     643                }
     644
     645                if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
     646                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
     647
     648
     649                $post_data['post_author'] = absint( $post_data['post_author'] );
     650                if( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
     651                        if( ! current_user_can( $post_type->cap->edit_others_posts ) )
     652                                return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
     653
     654                        $author = get_userdata( $post_data['post_author'] );
     655
     656                        if( ! $author )
     657                                return new IXR_Error( 404, __( 'Invalid author ID.' ) );
     658                }
     659                else {
     660                        $post_data['post_author'] = $user->ID;
     661                }
     662
     663                if( isset( $post_data['comment_status'] ) ) {
     664                        if( ! post_type_supports( $post_data['post_type'], 'comments' ) || ( $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' ) ) {
     665                                unset( $post_data['comment_status'] );
     666                        }
     667                }
     668
     669                if( isset( $post_data['ping_status'] ) ) {
     670                        if( ! post_type_supports( $post_data['post_type'], 'trackbacks' ) || ( $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' ) ) {
     671                                unset( $post_data['ping_status'] );
     672                        }
     673                }
     674
     675                // Do some timestamp voodoo
     676                if ( ! empty( $post_data['post_date_gmt'] ) ) {
     677                        // We know this is supposed to be GMT, so we're going to slap that Z on there by force
     678                        $dateCreated = str_replace( 'Z', '', $post_data['post_date_gmt']->getIso() ) . 'Z';
     679                }elseif ( ! empty( $post_data['post_date'] ) ) {
     680                        $dateCreated = $post_data['post_date']->getIso();
     681                }
     682
     683                if ( ! empty( $dateCreated ) ) {
     684                        $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
     685                        $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
     686                }
     687
     688                if ( ! isset( $post_data['ID'] ) ) {
     689                        $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
     690                }
     691                $post_ID = $post_data['ID'];
     692
     693                $sticky = $post_data['sticky'] ? true : false;
     694
     695                if( $post_data['post_type'] == 'post' && $sticky == true ) {
     696                        if( ! current_user_can( $post_type->cap->edit_others_posts ) )
     697                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
     698
     699                        if( $post_data['post_status'] != 'publish' )
     700                                return new IXR_Error( 401, __( 'Only published posts can be made sticky.' ) );
     701
     702                        stick_post( $post_ID );
     703                }
     704
     705                if( isset ( $post_data['custom_fields'] ) && post_type_supports( $post_data['post_type'], 'custom-fields' ) ) {
     706                        $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
     707                }
     708
     709                if( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
     710                        $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
     711
     712                        // accumulate term IDs from terms and terms_names
     713                        $terms = array();
     714
     715                        // first validate the terms specified by ID
     716                        if( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
     717                                $taxonomies = array_keys( $post_data['terms'] );
     718
     719                                // validating term ids
     720                                foreach ( $taxonomies as $taxonomy ) {
     721                                        if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
     722                                                return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
     723
     724                                        if( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
     725                                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies' ) );
     726
     727                                        $term_ids = $post_data['terms'][$taxonomy];
     728                                        foreach ( $term_ids as $term_id ) {
     729                                                $term = get_term_by( 'id', $term_id, $taxonomy );
     730
     731                                                if ( ! $term )
     732                                                        return new IXR_Error( 403, __( 'Invalid term ID' ) );
     733
     734                                                $terms[$taxonomy][] = (int) $term_id;
     735                                        }
     736                                }
     737                        }
     738
     739                        // now validate terms specified by name
     740                        if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
     741                                $taxonomies = array_keys( $post_data['terms_names'] );
     742
     743                                foreach ( $taxonomies as $taxonomy ) {
     744                                        if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
     745                                                return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
     746
     747                                        if( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
     748                                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
     749
     750                                        // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
     751                                        $ambiguous_terms = array();
     752                                        if( is_taxonomy_hierarchical( $taxonomy ) ) {
     753                                                $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
     754
     755                                                // count the number of terms with the same name
     756                                                $tax_term_names_count = array_count_values( $tax_term_names );
     757
     758                                                // filter out non-ambiguous term names
     759                                                $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
     760
     761                                                $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
     762                                        }
     763
     764                                        $term_names = $post_data['terms_names'][$taxonomy];
     765                                        foreach ( $term_names as $term_name ) {
     766                                                if ( in_array( $term_name, $ambiguous_terms ) )
     767                                                        return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
     768
     769                                                $term = get_term_by( 'name', $term_name, $taxonomy );
     770
     771                                                if ( ! $term ) {
     772                                                        // term doesn't exist, so check that the user is allowed to create new terms
     773                                                        if( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
     774                                                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
     775
     776                                                        // create the new term
     777                                                        $term_info = wp_insert_term( $term_name, $taxonomy );
     778                                                        if ( is_wp_error( $term_info ) )
     779                                                                return new IXR_Error( 500, $term_info->get_error_message() );
     780
     781                                                        $terms[$taxonomy][] = (int) $term_info['term_id'];
     782                                                }
     783                                                else {
     784                                                        $terms[$taxonomy][] = (int) $term->term_id;
     785                                                }
     786                                        }
     787                                }
     788                        }
     789
     790                        $post_data['tax_input'] = $terms;
     791                        unset( $post_data['terms'] );
     792                        unset( $post_data['terms_names'] );
     793                }
     794                else {
     795                        // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
     796                        unset( $post_data['tax_input'] );
     797                }
     798
     799                if( isset( $post_data['post_format'] ) ) {
     800                        $format = set_post_format( $post_ID, $post_data['post_format'] );
     801
     802                        if ( is_wp_error( $format ) )
     803                                return new IXR_Error( 500, $format->get_error_message() );
     804
     805                        unset( $post_data['post_format'] );
     806                }
     807
     808                // Handle enclosures
     809                $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
     810                $this->add_enclosure_if_new( $post_ID, $enclosure );
     811
     812                $this->attach_uploads( $post_ID, $post_data['post_content'] );
     813
     814                $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
     815
     816                $post_ID = wp_insert_post( $post_data, true );
     817                if ( is_wp_error( $post_ID ) )
     818                        return new IXR_Error( 500, $post_ID->get_error_message() );
     819
     820                if ( ! $post_ID )
     821                        return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
     822
     823                return strval( $post_ID );
     824        }
     825
     826        /*
     827         * Edit a post for any registered post type.
     828         *
     829         * The $content_struct parameter only needs to contain fields that
     830         * should be changed. All other fields will retain their existing values.
     831         *
     832         * @uses wp_insert_post()
     833         * @param array $args Method parameters. Contains:
     834         *  - int     $blog_id
     835         *  - string  $username
     836         *  - string  $password
     837         *  - int     $post_id
     838         *  - array   $content_struct
     839         * @return true on success
     840         */
     841        function wp_editPost( $args ) {
     842                $this->escape( $args );
     843
     844                $blog_id        = (int) $args[0]; // we will support this in the near future
     845                $username       = $args[1];
     846                $password       = $args[2];
     847                $post_id        = (int) $args[3];
     848                $content_struct = $args[4];
     849
     850                if ( ! $user = $this->login( $username, $password ) )
     851                        return $this->error;
     852
     853                do_action( 'xmlrpc_call', 'wp.editPost' );
     854
     855                // User Capabilities are checked in _wp_insertPost.
     856
     857                $post = get_post( $post_id, ARRAY_A );
     858
     859                if ( empty( $post["ID"] ) )
     860                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     861
     862                // convert the date field back to IXR form
     863                $post['post_date'] = new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_date'], false ) );
     864
     865                // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
     866                // since _wp_insertPost will ignore the non-GMT date if the GMT date is set
     867                if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
     868                        unset( $post['post_date_gmt'] );
     869                else
     870                        $post['post_date_gmt'] = new IXR_Date( mysql2date( 'Ymd\TH:i:s', $post['post_date_gmt'], false ) );
     871
     872                $this->escape( $post );
     873                $merged_content_struct = array_merge( $post, $content_struct );
     874
     875                $retval = $this->_wp_insertPost( $user, $merged_content_struct );
     876                if ( $retval instanceof IXR_Error )
     877                        return $retval;
     878
     879                return true;
     880        }
     881
     882        /**
     883         * Delete a post for any registered post type.
     884         *
     885         * @uses wp_delete_post()
     886         * @param array $args Method parameters. Contains:
     887         *  - int     $blog_id
     888         *  - string  $username
     889         *  - string  $password
     890         *  - int     $post_id
     891         * @return true on success
     892         */
     893        function wp_deletePost( $args ) {
     894                $this->escape( $args );
     895
     896                $blog_id    = (int) $args[0];
     897                $username   = $args[1];
     898                $password   = $args[2];
     899                $post_id    = (int) $args[3];
     900
     901                if ( ! $user = $this->login( $username, $password ) )
     902                        return $this->error;
     903
     904                do_action( 'xmlrpc_call', 'wp.deletePost' );
     905
     906                $post = wp_get_single_post( $post_id, ARRAY_A );
     907                if ( empty( $post['ID'] ) )
     908                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     909
     910                $post_type = get_post_type_object( $post['post_type'] );
     911                if( ! current_user_can( $post_type->cap->delete_post, $post_id ) )
     912                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
     913
     914                $result = wp_delete_post( $post_id );
     915
     916                if ( ! $result )
     917                        return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
     918
     919                return true;
     920        }
     921
     922        /**
     923         * Retrieve a post.
     924         *
     925         * The optional $fields parameter specifies what fields will be included
     926         * in the response array. This should be a list of field names. 'post_id' will
     927         * always be included in the response regardless of the value of $fields.
     928         *
     929         * Instead of, or in addition to, individual field names, conceptual group
     930         * names can be used to specify multiple fields. The available conceptual
     931         * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
     932         * and 'enclosure'.
     933         *
     934         * @uses wp_get_single_post()
     935         * @param array $args Method parameters. Contains:
     936         *  - int     $post_id
     937         *  - string  $username
     938         *  - string  $password
     939         *  - array   $fields optional
     940         * @return array contains (based on $fields parameter):
     941         *  - 'post_id'
     942         *  - 'post_title'
     943         *  - 'post_date'
     944         *  - 'post_date_gmt'
     945         *  - 'post_modified'
     946         *  - 'post_modified_gmt'
     947         *  - 'post_status'
     948         *  - 'post_type'
     949         *  - 'post_name'
     950         *  - 'post_author'
     951         *  - 'post_password'
     952         *  - 'post_excerpt'
     953         *  - 'post_content'
     954         *  - 'link'
     955         *  - 'comment_status'
     956         *  - 'ping_status'
     957         *  - 'sticky'
     958         *  - 'custom_fields'
     959         *  - 'terms'
     960         *  - 'categories'
     961         *  - 'tags'
     962         *  - 'enclosure'
     963         */
     964        function wp_getPost( $args ) {
     965                $this->escape( $args );
     966
     967                $blog_id            = (int) $args[0];
     968                $username           = $args[1];
     969                $password           = $args[2];
     970                $post_id            = (int) $args[3];
     971
     972                if ( isset( $args[4] ) )
     973                        $fields = $args[4];
     974                else
     975                        $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
     976
     977                if ( ! $user = $this->login( $username, $password ) )
     978                        return $this->error;
     979
     980                do_action( 'xmlrpc_call', 'wp.getPost' );
     981
     982                $post = wp_get_single_post( $post_id, ARRAY_A );
     983
     984                if ( empty( $post["ID"] ) )
     985                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     986
     987                $post_type = get_post_type_object( $post['post_type'] );
     988                if ( ! current_user_can( $post_type->cap->edit_posts, $post_id ) )
     989                        return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
     990
     991                return $this->_prepare_post( $post, $fields );
     992        }
     993
     994        /**
     995         * Retrieve posts.
     996         *
     997         * The optional $filter parameter modifies the query used to retrieve posts.
     998         * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
     999         * 'orderby', and 'order'.
     1000         *
     1001         * The optional $fields parameter specifies what fields will be included
     1002         * in the response array.
     1003         *
     1004         * @uses wp_get_recent_posts()
     1005         * @see wp_getPost() for more on $fields
     1006         * @see get_posts() for more on $filter values
     1007         *
     1008         * @param array $args Method parameters. Contains:
     1009         *  - int     $blog_id
     1010         *  - string  $username
     1011         *  - string  $password
     1012         *  - array   $filter optional
     1013         *  - array   $fields optional
     1014         * @return array cntains a collection of posts.
     1015         */
     1016        function wp_getPosts( $args ) {
     1017                $this->escape( $args );
     1018
     1019                $blog_id    = (int) $args[0];
     1020                $username   = $args[1];
     1021                $password   = $args[2];
     1022                $filter     = isset( $args[3] ) ? $args[3] : array();
     1023
     1024                if ( isset( $args[4] ) )
     1025                        $fields = $args[4];
     1026                else
     1027                        $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
     1028
     1029                if ( ! $user = $this->login( $username, $password ) )
     1030                        return $this->error;
     1031
     1032                do_action( 'xmlrpc_call', 'wp.getPosts' );
     1033
     1034                $query = array();
     1035
     1036                if ( isset( $filter['post_type'] ) ) {
     1037                        $post_type = get_post_type_object( $filter['post_type'] );
     1038                        if ( ! ( (bool) $post_type ) )
     1039                                return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
     1040
     1041                        if ( ! current_user_can( $post_type->cap->edit_posts ) )
     1042                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
     1043
     1044                        $query['post_type'] = $filter['post_type'];
     1045                }
     1046
     1047                if ( isset( $filter['post_status'] ) )
     1048                        $query['post_status'] = $filter['post_status'];
     1049
     1050                if ( isset( $filter['number'] ) )
     1051                        $query['number'] = absint( $filter['number'] );
     1052
     1053                if ( isset( $filter['offset'] ) )
     1054                        $query['offset'] = absint( $filter['offset'] );
     1055
     1056                if ( isset( $filter['orderby'] ) ) {
     1057                        $query['orderby'] = $filter['orderby'];
     1058
     1059                        if ( isset( $filter['order'] ) )
     1060                                $query['order'] = $filter['order'];
     1061                }
     1062
     1063                do_action( 'xmlrpc_call', 'wp.getPosts' );
     1064
     1065                $posts_list = wp_get_recent_posts( $query );
     1066
     1067                if ( ! $posts_list )
     1068                        return array( );
     1069
     1070                // holds all the posts data
     1071                $struct = array();
     1072
     1073                foreach ( $posts_list as $post ) {
     1074                        $post_type = get_post_type_object( $post['post_type'] );
     1075                        if ( ! current_user_can( $post_type->cap->edit_posts, $post['ID'] ) )
     1076                                continue;
     1077
     1078                        $struct[] = $this->_prepare_post( $post, $fields );
     1079                }
     1080
     1081                return $struct;
     1082        }
     1083
     1084        /**
    4501085         * Retrieve page.
    4511086         *
Note: See TracChangeset for help on using the changeset viewer.