| 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 a post |
| 1850 | * |
| 1851 | * @uses wp_get_single_post() |
| 1852 | * @param array $args Method parameters. Contains: |
| 1853 | * - int $post_id |
| 1854 | * - string $username |
| 1855 | * - string $password |
| 1856 | * - array $fields optional |
| 1857 | * @return array contains (based on $fields parameter): |
| 1858 | * - 'postid' |
| 1859 | * - 'title' |
| 1860 | * - 'description' |
| 1861 | * - 'mt_excerpt' |
| 1862 | * - 'post_status' |
| 1863 | * - 'post_type' |
| 1864 | * - 'wp_slug' |
| 1865 | * - 'wp_password' |
| 1866 | * - 'wp_page_order' |
| 1867 | * - 'wp_page_parent_id' |
| 1868 | * - 'wp_author_id' |
| 1869 | * - 'mt_allow_comments' |
| 1870 | * - 'mt_allow_pings' |
| 1871 | * - 'post_date' |
| 1872 | * - 'post_date_gmt' |
| 1873 | * - 'post_modified' |
| 1874 | * - 'post_modified_gmt' |
| 1875 | * - 'userid' |
| 1876 | * - 'sticky' |
| 1877 | * - 'custom_fields' |
| 1878 | * - 'terms' |
| 1879 | * - 'link' |
| 1880 | * - 'permaLink' |
| 1881 | * - 'categories' |
| 1882 | * - 'mt_keywords' |
| 1883 | * - 'wp_post_format' |
| 1884 | */ |
| 1885 | function wp_getPost( $args ) { |
| 1886 | $this->escape( $args ); |
| 1887 | |
| 1888 | $blog_id = (int) $args[0]; |
| 1889 | $username = $args[1]; |
| 1890 | $password = $args[2]; |
| 1891 | $post_id = (int) $args[3]; |
| 1892 | |
| 1893 | if ( isset( $args[4] ) ) |
| 1894 | $fields = $args[4]; |
| 1895 | else |
| 1896 | $fields = array( 'post', 'taxonomies', 'custom_fields' ); |
| 1897 | |
| 1898 | if ( ! $user = $this->login( $username, $password ) ) |
| 1899 | return $this->error; |
| 1900 | |
| 1901 | $post = wp_get_single_post( $post_id, ARRAY_A ); |
| 1902 | |
| 1903 | if ( empty( $post["ID"] ) ) |
| 1904 | return new IXR_Error( 404, __( 'Invalid post ID.' ) ); |
| 1905 | |
| 1906 | $post_type = get_post_type_object( $post['post_type'] ); |
| 1907 | if( ! current_user_can( $post_type->cap->edit_posts, $post_id ) ) |
| 1908 | return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' )); |
| 1909 | |
| 1910 | return $this->prepare_post( $post, $fields ); |
| 1911 | } |
| 1912 | |