| | 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 | |
| | 1753 | $post_type = get_post_type_object( $filter['post_type'] ); |
| | 1754 | if( !( (bool)$post_type ) ) |
| | 1755 | return new IXR_Error( 403, __( 'The post type specified is not valid' ) ); |
| | 1756 | |
| | 1757 | if( ! current_user_can( $post_type->cap->edit_posts ) ) |
| | 1758 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' )); |
| | 1759 | $query['post_type'] = $filter['post_type']; |
| | 1760 | |
| | 1761 | } |
| | 1762 | |
| | 1763 | if ( isset( $filter['post_status'] ) ) { |
| | 1764 | |
| | 1765 | $statuses = $filter['post_status']; |
| | 1766 | $post_statuses = get_post_stati (); |
| | 1767 | $query_statuses = array(); |
| | 1768 | |
| | 1769 | foreach ( $statuses as $status ) { |
| | 1770 | |
| | 1771 | if( !in_array( $status, $post_statuses ) ) |
| | 1772 | return new IXR_Error( 403, __( 'One of the post status specified is not valid' ) ); |
| | 1773 | $query_statuses[] = $status; |
| | 1774 | |
| | 1775 | } |
| | 1776 | |
| | 1777 | $query['post_status'] = $query_statuses; |
| | 1778 | |
| | 1779 | } |
| | 1780 | |
| | 1781 | if ( isset ( $filter['numberposts'] ) ) { |
| | 1782 | $query['numberposts'] = absint( $filter['numberposts'] ); |
| | 1783 | } |
| | 1784 | |
| | 1785 | if ( isset ( $filter['offset'] ) ) { |
| | 1786 | $query['offset'] = absint( $filter['offset'] ); |
| | 1787 | } |
| | 1788 | |
| | 1789 | do_action('xmlrpc_call', 'wp.getPosts'); |
| | 1790 | |
| | 1791 | $posts_list = wp_get_recent_posts( $query ); |
| | 1792 | |
| | 1793 | if ( !$posts_list ) |
| | 1794 | return array( ); |
| | 1795 | |
| | 1796 | // holds all the posts data |
| | 1797 | $struct = array(); |
| | 1798 | |
| | 1799 | // pre-calculate conceptual group in_array searches |
| | 1800 | $all_post_fields = in_array( 'post', $fields ); |
| | 1801 | $all_taxonomy_fields = in_array( 'taxonomies', $fields ); |
| | 1802 | |
| | 1803 | foreach ( $posts_list as $post ) { |
| | 1804 | |
| | 1805 | $post_type = get_post_type_object( $post['post_type'] ); |
| | 1806 | |
| | 1807 | if( !current_user_can( $post_type->cap->edit_posts, $post['ID'] ) ) |
| | 1808 | continue; |
| | 1809 | |
| | 1810 | // holds the data for this post. built up based on $fields |
| | 1811 | $post_data = array( 'postid' => $post['ID'] ); |
| | 1812 | |
| | 1813 | if ( $all_post_fields || in_array( 'title', $fields ) ) |
| | 1814 | $post_data['title'] = $post['post_title']; |
| | 1815 | |
| | 1816 | if ( $all_post_fields || in_array( 'dateCreated', $fields ) ) |
| | 1817 | $post_data['dateCreated'] = new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_date'], false )); |
| | 1818 | |
| | 1819 | if ( $all_post_fields || in_array( 'date_created_gmt', $fields ) ) |
| | 1820 | $post_data['date_created_gmt'] = new IXR_Date(mysql2date( 'Ymd\TH:i:s', $post['post_date_gmt'], false )); |
| | 1821 | |
| | 1822 | if ( $all_post_fields || in_array( 'post_status', $fields ) ) { |
| | 1823 | // Consider future posts as published |
| | 1824 | if ( $post['post_status'] === 'future' ) |
| | 1825 | $post_data['post_status'] = 'publish'; |
| | 1826 | else |
| | 1827 | $post_data['post_status'] = $post['post_status']; |
| | 1828 | } |
| | 1829 | |
| | 1830 | if ( $all_post_fields || in_array( 'post_type', $fields ) ) |
| | 1831 | $post_data['post_type'] = $post['post_type']; |
| | 1832 | |
| | 1833 | if ( $all_post_fields || in_array( 'post_format', $fields ) ) { |
| | 1834 | $post_format = get_post_format( $post['ID'] ); |
| | 1835 | if ( empty( $post_format ) ) |
| | 1836 | $post_format = 'standard'; |
| | 1837 | $post_data['post_format'] = $post_format; |
| | 1838 | } |
| | 1839 | |
| | 1840 | if ( $all_post_fields || in_array( 'wp_slug', $fields ) ) |
| | 1841 | $post_data['wp_slug'] = $post['post_name']; |
| | 1842 | |
| | 1843 | if ( $all_post_fields || in_array( 'link', $fields ) ) |
| | 1844 | $post_data['link'] = post_permalink( $post['ID'] ); |
| | 1845 | |
| | 1846 | if ( $all_post_fields || in_array( 'permaLink', $fields ) ) |
| | 1847 | $post_data['permaLink'] = post_permalink( $post['ID'] ); |
| | 1848 | |
| | 1849 | if ( $all_post_fields || in_array( 'userid', $fields ) ) |
| | 1850 | $post_data['userid'] = $post['post_author']; |
| | 1851 | |
| | 1852 | if ( $all_post_fields || in_array( 'wp_author_id', $fields ) ) |
| | 1853 | $post_data['wp_author_id'] = $post['post_author']; |
| | 1854 | |
| | 1855 | if ( $all_post_fields || in_array( 'mt_allow_comments', $fields ) ) |
| | 1856 | $post_data['mt_allow_comments'] = $post['comment_status']; |
| | 1857 | |
| | 1858 | if ( $all_post_fields || in_array( 'mt_allow_pings', $fields ) ) |
| | 1859 | $post_data['mt_allow_pings'] = $post['ping_status']; |
| | 1860 | |
| | 1861 | if ( $all_post_fields || in_array( 'sticky', $fields ) ) { |
| | 1862 | $sticky = null; |
| | 1863 | if( $post['post_type'] == 'post' ) { |
| | 1864 | $sticky = false; |
| | 1865 | if ( is_sticky( $post['ID'] ) ) |
| | 1866 | $sticky = true; |
| | 1867 | } |
| | 1868 | $post_data['sticky'] = $sticky; |
| | 1869 | } |
| | 1870 | |
| | 1871 | if ( $all_post_fields || in_array( 'wp_password', $fields ) ) |
| | 1872 | $post_data['wp_password'] = $post['post_password']; |
| | 1873 | |
| | 1874 | if ( $all_post_fields || in_array( 'mt_excerpt', $fields ) ) |
| | 1875 | $post_data['mt_excerpt'] = $post['post_excerpt']; |
| | 1876 | |
| | 1877 | if ( $all_post_fields || in_array( 'description', $fields ) ) { |
| | 1878 | $post_content = get_extended( $post['post_content'] ); |
| | 1879 | $post_data['description'] = $post_content['main']; |
| | 1880 | $post_data['mt_text_more'] = $post_content['extended']; |
| | 1881 | } |
| | 1882 | |
| | 1883 | if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) { |
| | 1884 | $post_type_taxonomies = get_object_taxonomies( $post['post_type'] , 'names'); |
| | 1885 | $post_data['terms'] = wp_get_object_terms( $post['ID'], $post_type_taxonomies );; |
| | 1886 | } |
| | 1887 | |
| | 1888 | // backward compatiblity |
| | 1889 | if ( $all_taxonomy_fields || in_array( 'mt_keywords', $fields ) ) { |
| | 1890 | $tagnames = array(); |
| | 1891 | $tags = wp_get_post_tags( $post['ID'] ); |
| | 1892 | if ( !empty( $tags ) ) { |
| | 1893 | foreach ( $tags as $tag ) |
| | 1894 | $tagnames[] = $tag->name; |
| | 1895 | $tagnames = implode( ', ', $tagnames ); |
| | 1896 | } else { |
| | 1897 | $tagnames = ''; |
| | 1898 | } |
| | 1899 | $post_data['mt_keywords'] = $tagnames; |
| | 1900 | } |
| | 1901 | |
| | 1902 | // backward compatiblity |
| | 1903 | if ( $all_taxonomy_fields || in_array( 'categories', $fields ) ) { |
| | 1904 | $categories = array(); |
| | 1905 | $catids = wp_get_post_categories( $post['ID'] ); |
| | 1906 | foreach($catids as $catid) { |
| | 1907 | $categories[] = get_cat_name($catid); |
| | 1908 | } |
| | 1909 | $post_data['categories'] = $categories; |
| | 1910 | } |
| | 1911 | |
| | 1912 | if ( in_array( 'custom_fields', $fields ) ) |
| | 1913 | $post_data['custom_fields'] = $this->get_custom_fields( $post['ID'] ); |
| | 1914 | |
| | 1915 | if ( in_array( 'enclosure', $fields ) ) { |
| | 1916 | $enclosure = array(); |
| | 1917 | foreach ( (array) get_post_custom( $post['ID'] ) as $key => $val) { |
| | 1918 | if ($key == 'enclosure') { |
| | 1919 | foreach ( (array) $val as $enc ) { |
| | 1920 | $encdata = split("\n", $enc); |
| | 1921 | $enclosure['url'] = trim(htmlspecialchars($encdata[0])); |
| | 1922 | $enclosure['length'] = (int) trim($encdata[1]); |
| | 1923 | $enclosure['type'] = trim($encdata[2]); |
| | 1924 | break 2; |
| | 1925 | } |
| | 1926 | } |
| | 1927 | } |
| | 1928 | $post_data['enclosure'] = $enclosure; |
| | 1929 | } |
| | 1930 | |
| | 1931 | $struct[] = $post_data; |
| | 1932 | } |
| | 1933 | |
| | 1934 | return $struct; |
| | 1935 | } |
| | 1936 | |