Make WordPress Core

Ticket #18433: wp.getPosts.5.patch

File wp.getPosts.5.patch, 8.3 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 post data for return in an XML-RPC object
     1708         *
     1709         * @param array $post The unprepared post data
     1710         * @param array $fields The subset of post fields to return
     1711         * @return array The prepared post data
     1712         */
     1713        function prepare_post( $post, $fields ) {
     1714                // pre-calculate conceptual group in_array searches
     1715                $all_post_fields = in_array( 'post', $fields );
     1716                $all_taxonomy_fields = in_array( 'taxonomies', $fields );
     1717
     1718                // holds the data for this post. built up based on $fields
     1719                $_post = array( 'postid' => $post['ID'] );
     1720
     1721                if ( $all_post_fields || in_array( 'title', $fields ) )
     1722                        $_post['title'] = $post['post_title'];
     1723
     1724                if ( $all_post_fields || in_array( 'post_date', $fields ) )
     1725                        $_post['post_date'] = new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_date'], false ));
     1726
     1727                if ( $all_post_fields || in_array( 'post_date_gmt', $fields ) )
     1728                        $_post['post_date_gmt'] = new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_date_gmt'], false ));
     1729
     1730                if ( $all_post_fields || in_array( 'post_modified', $fields ) )
     1731                        $_post['post_modified'] = new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_modified'], false ));
     1732
     1733                if ( $all_post_fields || in_array( 'post_modified_gmt', $fields ) )
     1734                        $_post['post_modified_gmt'] = new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_modified_gmt'], false ));
     1735
     1736                if ( $all_post_fields || in_array( 'post_status', $fields ) ) {
     1737                        // Consider future posts as published
     1738                        if ( $post['post_status'] === 'future' )
     1739                                $_post['post_status'] = 'publish';
     1740                        else
     1741                                $_post['post_status'] = $post['post_status'];
     1742                }
     1743
     1744                if ( $all_post_fields || in_array( 'post_type', $fields ) )
     1745                        $_post['post_type'] = $post['post_type'];
     1746
     1747                if ( $all_post_fields || in_array( 'post_format', $fields ) ) {
     1748                        $post_format = get_post_format( $post['ID'] );
     1749                        if ( empty( $post_format ) )
     1750                                $post_format = 'standard';
     1751                        $_post['post_format'] = $post_format;
     1752                }
     1753
     1754                if ( $all_post_fields || in_array( 'wp_slug', $fields ) )
     1755                        $_post['wp_slug'] = $post['post_name'];
     1756
     1757                if ( $all_post_fields || in_array( 'link', $fields ) )
     1758                        $_post['link'] = post_permalink( $post['ID'] );
     1759
     1760                if ( $all_post_fields || in_array( 'permaLink', $fields ) )
     1761                        $_post['permaLink'] = post_permalink( $post['ID'] );
     1762
     1763                if ( $all_post_fields || in_array( 'userid', $fields ) )
     1764                        $_post['userid'] = $post['post_author'];
     1765
     1766                if ( $all_post_fields || in_array( 'wp_author_id', $fields ) )
     1767                        $_post['wp_author_id'] = $post['post_author'];
     1768
     1769                if ( $all_post_fields || in_array( 'mt_allow_comments', $fields ) )
     1770                        $_post['mt_allow_comments'] = $post['comment_status'];
     1771
     1772                if ( $all_post_fields || in_array( 'mt_allow_pings', $fields ) )
     1773                        $_post['mt_allow_pings'] = $post['ping_status'];
     1774
     1775                if ( $all_post_fields || in_array( 'sticky', $fields ) ) {
     1776                        $sticky = null;
     1777                        if( $post['post_type'] == 'post' ) {
     1778                                $sticky = false;
     1779                                if ( is_sticky( $post['ID'] ) )
     1780                                        $sticky = true;
     1781                        }
     1782                        $_post['sticky'] = $sticky;
     1783                }
     1784
     1785                if ( $all_post_fields || in_array( 'wp_password', $fields ) )
     1786                        $_post['wp_password'] = $post['post_password'];
     1787
     1788                if ( $all_post_fields || in_array( 'mt_excerpt', $fields ) )
     1789                        $_post['mt_excerpt'] = $post['post_excerpt'];
     1790
     1791                if ( $all_post_fields || in_array( 'description', $fields ) ) {
     1792                        $post_content = get_extended( $post['post_content'] );
     1793                        $_post['description'] = $post_content['main'];
     1794                        $_post['mt_text_more'] = $post_content['extended'];
     1795                }
     1796
     1797                if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
     1798                        $post_type_taxonomies = get_object_taxonomies( $post['post_type'] , 'names');
     1799                        $_post['terms'] = wp_get_object_terms( $post['ID'], $post_type_taxonomies );;
     1800                }
     1801
     1802                // backward compatiblity
     1803                if ( $all_taxonomy_fields || in_array( 'mt_keywords', $fields ) ) {
     1804                        $tagnames = array();
     1805                        $tags = wp_get_post_tags( $post['ID'] );
     1806                        if ( !empty( $tags ) ) {
     1807                                foreach ( $tags as $tag )
     1808                                        $tagnames[] = $tag->name;
     1809                                $tagnames = implode( ', ', $tagnames );
     1810                        } else {
     1811                                $tagnames = '';
     1812                        }
     1813                        $_post['mt_keywords'] = $tagnames;
     1814                }
     1815
     1816                // backward compatiblity
     1817                if ( $all_taxonomy_fields || in_array( 'categories', $fields ) ) {
     1818                        $categories = array();
     1819                        $catids = wp_get_post_categories( $post['ID'] );
     1820                        foreach($catids as $catid) {
     1821                                $categories[] = get_cat_name($catid);
     1822                        }
     1823                        $_post['categories'] = $categories;
     1824                }
     1825
     1826                if ( in_array( 'custom_fields', $fields ) )
     1827                        $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
     1828
     1829                if ( in_array( 'enclosure', $fields ) ) {
     1830                        $enclosure = array();
     1831                        foreach ( (array) get_post_custom( $post['ID'] ) as $key => $val) {
     1832                                if ($key == 'enclosure') {
     1833                                        foreach ( (array) $val as $enc ) {
     1834                                                $encdata = split("\n", $enc);
     1835                                                $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
     1836                                                $enclosure['length'] = (int) trim($encdata[1]);
     1837                                                $enclosure['type'] = trim($encdata[2]);
     1838                                                break 2;
     1839                                        }
     1840                                }
     1841                        }
     1842                        $_post['enclosure'] = $enclosure;
     1843                }
     1844
     1845                return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
     1846        }
     1847
     1848        /**
     1849         * Retrieve posts.
     1850         *
     1851         * Besides the common blog_id, username, and password arguments, it takes
     1852         * a filter array and a fields array.
     1853         *
     1854         * Accepted 'filter' keys are 'post_type', 'post_status', 'numberposts', 'offset',
     1855         * 'orderby', and 'order'.
     1856         *
     1857         * The 'fields' array specifies which post fields will be included in the response.
     1858         * Values can be either conceptual groups ('post', 'taxonomies', 'custom_fields')
     1859         * or specific field names. By default, all fields are returned.
     1860         *
     1861         * @uses wp_get_recent_posts()
     1862         * @param array $args Method parameters. Contains:
     1863         *  - int     $blog_id
     1864         *  - string  $username
     1865         *  - string  $password
     1866         *  - array   $fields optional
     1867         *  - array   $filter optional
     1868         * @return array. Contains a collection of posts.
     1869         */
     1870        function wp_getPosts( $args ) {
     1871                $this->escape( $args );
     1872
     1873                $blog_id    = (int) $args[0];
     1874                $username   = $args[1];
     1875                $password   = $args[2];
     1876
     1877                if ( isset( $args[3] ) )
     1878                        $fields = $args[3];
     1879                else
     1880                        $fields = array( 'post', 'taxonomies', 'custom_fields' );
     1881
     1882                if ( isset( $args[4] ) )
     1883                        $filter = $args[4];
     1884                else
     1885                        $filter = array();
     1886
     1887                if ( !$user = $this->login( $username, $password ) )
     1888                        return $this->error;
     1889
     1890                $query = array();
     1891
     1892                if ( isset( $filter['post_type'] ) ) {
     1893                        $post_type = get_post_type_object( $filter['post_type'] );
     1894                        if( !( (bool)$post_type ) )
     1895                                return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
     1896
     1897                        if( ! current_user_can( $post_type->cap->edit_posts ) )
     1898                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
     1899                        $query['post_type'] = $filter['post_type'];
     1900                }
     1901
     1902                if ( isset( $filter['post_status'] ) ) {
     1903                        $query['post_status'] = $filter['post_status'];
     1904                }
     1905
     1906                if ( isset ( $filter['numberposts'] ) ) {
     1907                        $query['numberposts'] = absint( $filter['numberposts'] );
     1908                }
     1909
     1910                if ( isset ( $filter['offset'] ) ) {
     1911                        $query['offset'] = absint( $filter['offset'] );
     1912                }
     1913
     1914                if ( isset ( $filter['orderby'] ) ) {
     1915                        $query['orderby'] = $filter['orderby'];
     1916
     1917                        if ( isset ( $filter['order'] ) ) {
     1918                                $query['order'] = $filter['order'];
     1919                        }
     1920                }
     1921
     1922                do_action('xmlrpc_call', 'wp.getPosts');
     1923
     1924                $posts_list = wp_get_recent_posts( $query );
     1925
     1926                if ( !$posts_list )
     1927                        return array( );
     1928
     1929                // holds all the posts data
     1930                $struct = array();
     1931
     1932                foreach ( $posts_list as $post ) {
     1933                        $post_type = get_post_type_object( $post['post_type'] );
     1934                        if( !current_user_can( $post_type->cap->edit_posts, $post['ID'] ) )
     1935                                continue;
     1936
     1937                        $struct[] = $this->prepare_post( $post, $fields );
     1938                }
     1939
     1940                return $struct;
     1941        }
     1942
    17051943        /* Blogger API functions.
    17061944         * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
    17071945         */