Make WordPress Core

Ticket #18432: wp.getPost.3.patch

File wp.getPost.3.patch, 6.6 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.getPost'                    => 'this:wp_getPost',
    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 a post.
     1813         *
     1814         * The optional $fields parameter specifies what fields will be included
     1815         * in the response array. This should be a list of field names. 'post_id' will
     1816         * always be included in the response regardless of the value of $fields.
     1817         *
     1818         * Instead of, or in addition to, individual field names, conceptual group
     1819         * names can be used to specify multiple fields. The available conceptual
     1820         * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
     1821         * and 'enclosure'.
     1822         *
     1823         * @uses wp_get_single_post()
     1824         * @param array $args Method parameters. Contains:
     1825         *  - int     $post_id
     1826         *  - string  $username
     1827         *  - string  $password
     1828         *  - array   $fields optional
     1829         * @return array contains (based on $fields parameter):
     1830         *  - 'post_id'
     1831         *  - 'post_title'
     1832         *  - 'post_date'
     1833         *  - 'post_date_gmt'
     1834         *  - 'post_modified'
     1835         *  - 'post_modified_gmt'
     1836         *  - 'post_status'
     1837         *  - 'post_type'
     1838         *  - 'post_slug'
     1839         *  - 'post_author'
     1840         *  - 'post_password'
     1841         *  - 'post_excerpt'
     1842         *  - 'post_content'
     1843         *  - 'link'
     1844         *  - 'comment_status'
     1845         *  - 'ping_status'
     1846         *  - 'sticky'
     1847         *  - 'custom_fields'
     1848         *  - 'terms'
     1849         *  - 'categories'
     1850         *  - 'tags'
     1851         *  - 'enclosure'
     1852         */
     1853        function wp_getPost( $args ) {
     1854                $this->escape( $args );
     1855
     1856                $blog_id            = (int) $args[0];
     1857                $username           = $args[1];
     1858                $password           = $args[2];
     1859                $post_id            = (int) $args[3];
     1860
     1861                if ( isset( $args[4] ) )
     1862                        $fields = $args[4];
     1863                else
     1864                        $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
     1865
     1866                if ( ! $user = $this->login( $username, $password ) )
     1867                        return $this->error;
     1868
     1869                do_action( 'xmlrpc_call', 'wp.getPost' );
     1870
     1871                $post = wp_get_single_post( $post_id, ARRAY_A );
     1872
     1873                if ( empty( $post["ID"] ) )
     1874                        return new IXR_Error( 404, __( 'Invalid post ID.' ) );
     1875
     1876                $post_type = get_post_type_object( $post['post_type'] );
     1877                if( ! current_user_can( $post_type->cap->edit_posts, $post_id ) )
     1878                        return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ));
     1879
     1880                return $this->prepare_post( $post, $fields );
     1881        }
     1882
    17051883        /* Blogger API functions.
    17061884         * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
    17071885         */