Make WordPress Core

Ticket #18433: wp.getPosts.2.patch

File wp.getPosts.2.patch, 5.4 KB (added by maxcutler, 13 years ago)

Updated patch formatting and fixed some typos.

  • 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          * Retrieves posts
     1708          *
     1709          * @since 3.3
     1710          *
     1711          * @uses wp_get_recent_posts()
     1712          * @param array $args Method parameters. Contains:
     1713          *  - int     $blog_id
     1714          *  - string  $username
     1715          *  - string  $password
     1716          *  - array   $filter optional
     1717          * @return array
     1718          */
     1719        function wp_getPosts( $args ) {
     1720                $this->escape( $args );
     1721
     1722                $blog_ID    = (int) $args[0];
     1723                $username   = $args[1];
     1724                $password   = $args[2];
     1725                $filter     = $args[3];
     1726
     1727                if ( !$user = $this->login( $username, $password ) )
     1728                        return $this->error;
     1729
     1730                $query = array();
     1731
     1732                if ( isset( $filter['post_type'] ) ) {
     1733                        $post_type = get_post_type_object( $filter['post_type'] );
     1734                        if( !( (bool)$post_type ) )
     1735                                        return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
     1736
     1737                        if( ! current_user_can( $post_type->cap->edit_posts ) )
     1738                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
     1739                        $query['post_type'] = $filter['post_type'];
     1740                }
     1741
     1742                if ( isset ( $filter['numberposts'] ) ) {
     1743                        $query['numberposts'] = absint( $filter['numberposts'] );
     1744
     1745                        if ( isset ( $filter['offset'] ) ) {
     1746                                $query['offset'] = absint( $filter['offset'] );
     1747                        }
     1748                }
     1749
     1750                do_action('xmlrpc_call', 'wp.getPosts');
     1751
     1752                $posts_list = wp_get_recent_posts( $query );
     1753
     1754                if ( !$posts_list )
     1755                        return array( );
     1756
     1757                // holds all the post data
     1758                $struct = array();
     1759
     1760                foreach ( $posts_list as $post ) {
     1761
     1762                        $post_type = get_post_type_object( $post['post_type'] );
     1763                        if( !current_user_can( $post_type->cap->edit_posts ) )
     1764                                continue;
     1765
     1766                        $post_date = mysql2date( 'Ymd\TH:i:s', $post['post_date'], false );
     1767                        $post_date_gmt = mysql2date( 'Ymd\TH:i:s', $post['post_date_gmt'], false );
     1768
     1769                        // For drafts use the GMT version of the post date
     1770                        if ( $post['post_status'] == 'draft' )
     1771                                $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post['post_date'] ), 'Ymd\TH:i:s' );
     1772
     1773                        $post_content = get_extended( $post['post_content'] );
     1774                        $link = post_permalink( $post['ID'] );
     1775
     1776                        // Consider future posts as published
     1777                        if ( $post['post_status'] === 'future' )
     1778                                $post['post_status'] = 'publish';
     1779
     1780                        // Get post format
     1781                        $post_format = get_post_format( $post['ID'] );
     1782                        if ( empty( $post_format ) )
     1783                                $post_format = 'standard';
     1784
     1785                        $sticky = null;
     1786                        if( $post['post_type'] == 'post' ) {
     1787                                $sticky = false;
     1788                                if ( is_sticky( $post['ID'] ) )
     1789                                        $sticky = true;
     1790                        }
     1791
     1792                        $post_type_taxonomies = get_object_taxonomies( $post['post_type'] , 'names');
     1793                        $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
     1794
     1795                        $enclosure = array();
     1796                        foreach ( (array) get_post_custom( $post['ID'] ) as $key => $val) {
     1797                                if ($key == 'enclosure') {
     1798                                        foreach ( (array) $val as $enc ) {
     1799                                                $encdata = split("\n", $enc);
     1800                                                $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
     1801                                                $enclosure['length'] = (int) trim($encdata[1]);
     1802                                                $enclosure['type'] = trim($encdata[2]);
     1803                                                break 2;
     1804                                        }
     1805                                }
     1806                        }
     1807
     1808                        // backward compatiblity
     1809                        $categories = array();
     1810                        $catids = wp_get_post_categories( $post['ID'] );
     1811                        foreach($catids as $catid) {
     1812                                $categories[] = get_cat_name($catid);
     1813                        }
     1814
     1815                        $tagnames = array();
     1816                        $tags = wp_get_post_tags( $post['ID'] );
     1817                        if ( !empty( $tags ) ) {
     1818                                foreach ( $tags as $tag )
     1819                                        $tagnames[] = $tag->name;
     1820                                $tagnames = implode( ', ', $tagnames );
     1821                        } else {
     1822                                $tagnames = '';
     1823                        }
     1824
     1825                        $post_data = array(
     1826                                'postid'                        => $post['ID'],
     1827                                'title'                         => $post['post_title'],
     1828                                'description'           => $post_content['main'],
     1829                                'mt_excerpt'            => $post['post_excerpt'],
     1830
     1831                                'post_status'           => $post['post_status'],
     1832                                'post_type'                     => $post['post_type'],
     1833                                'wp_slug'                       => $post['post_name'],
     1834                                'wp_password'           => $post['post_password'],
     1835
     1836                                'wp_page_order'         => $post['menu_order'],
     1837                                'wp_page_parent_id'     => $post['post_parent'],
     1838
     1839                                'wp_author_id'          => $post['post_author'],
     1840
     1841                                'mt_allow_comments'     => $post['comment_status'],
     1842                                'mt_allow_pings'        => $post['ping_status'],
     1843
     1844                                'dateCreated'           => new IXR_Date($post_date),
     1845                                'date_created_gmt'      => new IXR_Date($post_date_gmt),
     1846
     1847                                'userid'                        => $post['post_author'],
     1848                                'sticky'                        => $sticky,
     1849                                'custom_fields'         => $this->get_custom_fields( $post['ID'] ),
     1850                                'terms'                         => $terms,
     1851
     1852                                'link'                          => $link,
     1853                                'permaLink'                     => $link,
     1854
     1855                                // backward compatibility
     1856                                'categories'            => $categories,
     1857                                'mt_keywords'           => $tagnames,
     1858                                'wp_post_format'        => $post_format,
     1859                        );
     1860
     1861                        if ( ! empty( $enclosure ) )
     1862                                $post_data['enclosure'] = $enclosure;
     1863
     1864                        $struct[] = $post_data;
     1865
     1866                }
     1867
     1868                return $struct;
     1869        }
     1870
    17051871        /* Blogger API functions.
    17061872         * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
    17071873         */