Make WordPress Core

Ticket #18433: wp.getPosts.3.patch

File wp.getPosts.3.patch, 7.7 KB (added by maxcutler, 13 years ago)

Added 'fields' parameter using nacin's suggested strategy.

  • wp-includes/class-wp-xmlrpc-server.php

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