Make WordPress Core

Ticket #18433: wp.getPosts.6.patch

File wp.getPosts.6.patch, 7.0 KB (added by maxcutler, 13 years ago)
  • wp-includes/class-wp-xmlrpc-server.php

     
    6464                        'wp.getMediaItem'               => 'this:wp_getMediaItem',
    6565                        'wp.getMediaLibrary'    => 'this:wp_getMediaLibrary',
    6666                        'wp.getPostFormats'     => 'this:wp_getPostFormats',
     67                        'wp.getPosts'                   => 'this:wp_getPosts',
    6768
    6869                        // Blogger API
    6970                        'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
     
    17021703                return $formats;
    17031704        }
    17041705
     1706        /**
     1707         * Prepares term data for return in an XML-RPC object
     1708         *
     1709         * @param array $term The unprepared term data
     1710         * @return array The prepared term data
     1711         */
     1712        function prepare_term( $term ) {
     1713                $_term = (array) $term;
     1714
     1715                return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
     1716        }
     1717
     1718        function prepare_post( $post, $fields ) {
     1719                // holds the data for this post. built up based on $fields
     1720                $_post = array( 'post_id' => $post['ID'] );
     1721
     1722                // prepare common post fields
     1723                $post_fields = array(
     1724                        'post_title' => $post['post_title'],
     1725                        'post_date' => new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_date'], false )),
     1726                        'post_date_gmt' => new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_date_gmt'], false )),
     1727                        'post_modified' => new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_modified'], false )),
     1728                        'post_modified_gmt' => new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_modified_gmt'], false )),
     1729                        'post_status' => $post['post_status'],
     1730                        'post_type' => $post['post_type'],
     1731                        'post_slug' => $post['post_name'],
     1732                        'post_author' => $post['post_author'],
     1733                        'post_password' => $post['post_password'],
     1734                        'post_excerpt' => $post['post_excerpt'],
     1735                        'post_content' => $post['post_content'],
     1736                        'link' => post_permalink( $post['ID'] ),
     1737                        'comment_status' => $post['comment_status'],
     1738                        'ping_status' => $post['ping_status'],
     1739                        'sticky' => ($post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
     1740                );
     1741
     1742                // Consider future posts as published
     1743                if ( $post_fields['post_status'] === 'future' )
     1744                        $post_fields['post_status'] = 'publish';
     1745
     1746                // Fill in blank post format
     1747                $post_fields['post_format'] = get_post_format( $post['ID'] );
     1748                if ( empty( $post_fields['post_format'] ) )
     1749                        $post_fields['post_format'] = 'standard';
     1750
     1751                // Merge requested $post_fields fields into $_post
     1752                if ( in_array( 'post', $fields ) ) {
     1753                        $_post = array_merge( $_post, $post_fields );
     1754                } else {
     1755                        $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
     1756                        $_post = array_merge( $_post, $requested_fields );
     1757                }
     1758
     1759                $all_taxonomy_fields = in_array( 'taxonomies', $fields );
     1760
     1761                if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
     1762                        $post_type_taxonomies = get_object_taxonomies( $post['post_type'] , 'names' );
     1763                        $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
     1764                        $_post['terms'] = array();
     1765                        foreach ( $terms as $term ) {
     1766                                $_post['terms'][] = $this->prepare_term( $term );
     1767                        }
     1768                }
     1769
     1770                // backward compatiblity
     1771                if ( $all_taxonomy_fields || in_array( 'tags', $fields ) ) {
     1772                        $tagnames = array();
     1773                        $tags = wp_get_post_tags( $post['ID'] );
     1774                        if ( !empty( $tags ) ) {
     1775                                foreach ( $tags as $tag )
     1776                                        $tagnames[] = $tag->name;
     1777                                $tagnames = implode( ', ', $tagnames );
     1778                        } else {
     1779                                $tagnames = '';
     1780                        }
     1781                        $_post['tags'] = $tagnames;
     1782                }
     1783
     1784                // backward compatiblity
     1785                if ( $all_taxonomy_fields || in_array( 'categories', $fields ) ) {
     1786                        $categories = array();
     1787                        $catids = wp_get_post_categories( $post['ID'] );
     1788                        foreach($catids as $catid) {
     1789                                $categories[] = get_cat_name($catid);
     1790                        }
     1791                        $_post['categories'] = $categories;
     1792                }
     1793
     1794                if ( in_array( 'custom_fields', $fields ) )
     1795                        $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
     1796
     1797                if ( in_array( 'enclosure', $fields ) ) {
     1798                        $_post['enclosure'] = array();
     1799                        $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
     1800                        if ( ! empty ( $enclosures ) ) {
     1801                                $encdata = explode("\n", $enclosures[0]);
     1802                                $_post['enclosure']['url'] = trim(htmlspecialchars($encdata[0]));
     1803                                $_post['enclosure']['length'] = (int) trim($encdata[1]);
     1804                                $_post['enclosure']['type'] = trim($encdata[2]);
     1805                        }
     1806                }
     1807
     1808                return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
     1809        }
     1810
     1811        /**
     1812         * Retrieve posts.
     1813         *
     1814         * The optional $filter parameter modifies the query used to retrieve posts.
     1815         * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
     1816         * 'orderby', and 'order'.
     1817         *
     1818         * The optional $fields parameter specifies what fields will be included
     1819         * in the response array.
     1820         *
     1821         * @uses wp_get_recent_posts()
     1822         * @see wp_getPost() for more on $fields
     1823         * @see get_posts() for more on $filter values
     1824         *
     1825         * @param array $args Method parameters. Contains:
     1826         *  - int     $blog_id
     1827         *  - string  $username
     1828         *  - string  $password
     1829         *  - array   $filter optional
     1830         *  - array   $fields optional
     1831         * @return array cntains a collection of posts.
     1832         */
     1833        function wp_getPosts( $args ) {
     1834                $this->escape( $args );
     1835
     1836                $blog_id    = (int) $args[0];
     1837                $username   = $args[1];
     1838                $password   = $args[2];
     1839                $filter     = isset( $args[3] ) ? $args[3] : array();
     1840
     1841                if ( isset( $args[4] ) )
     1842                        $fields = $args[4];
     1843                else
     1844                        $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
     1845
     1846                if ( !$user = $this->login( $username, $password ) )
     1847                        return $this->error;
     1848
     1849                do_action( 'xmlrpc_call', 'wp.getPosts' );
     1850
     1851                $query = array();
     1852
     1853                if ( isset( $filter['post_type'] ) ) {
     1854                        $post_type = get_post_type_object( $filter['post_type'] );
     1855                        if( !( (bool)$post_type ) )
     1856                                return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
     1857
     1858                        if( ! current_user_can( $post_type->cap->edit_posts ) )
     1859                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
     1860                        $query['post_type'] = $filter['post_type'];
     1861                }
     1862
     1863                if ( isset( $filter['post_status'] ) ) {
     1864                        $query['post_status'] = $filter['post_status'];
     1865                }
     1866
     1867                if ( isset ( $filter['number'] ) ) {
     1868                        $query['number'] = absint( $filter['number'] );
     1869                }
     1870
     1871                if ( isset ( $filter['offset'] ) ) {
     1872                        $query['offset'] = absint( $filter['offset'] );
     1873                }
     1874
     1875                if ( isset ( $filter['orderby'] ) ) {
     1876                        $query['orderby'] = $filter['orderby'];
     1877
     1878                        if ( isset ( $filter['order'] ) ) {
     1879                                $query['order'] = $filter['order'];
     1880                        }
     1881                }
     1882
     1883                do_action('xmlrpc_call', 'wp.getPosts');
     1884
     1885                $posts_list = wp_get_recent_posts( $query );
     1886
     1887                if ( !$posts_list )
     1888                        return array( );
     1889
     1890                // holds all the posts data
     1891                $struct = array();
     1892
     1893                foreach ( $posts_list as $post ) {
     1894                        $post_type = get_post_type_object( $post['post_type'] );
     1895                        if( !current_user_can( $post_type->cap->edit_posts, $post['ID'] ) )
     1896                                continue;
     1897
     1898                        $struct[] = $this->prepare_post( $post, $fields );
     1899                }
     1900
     1901                return $struct;
     1902        }
     1903
    17051904        /* Blogger API functions.
    17061905         * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
    17071906         */