| 1706 | /** |
| 1707 | * Retrieve posts |
| 1708 | * |
| 1709 | * @uses wp_get_recent_posts() |
| 1710 | * @param array $args Method parameters. Contains: |
| 1711 | * - int $blog_id |
| 1712 | * - string $username |
| 1713 | * - string $password |
| 1714 | * - array $filter optional |
| 1715 | * @return array |
| 1716 | */ |
| 1717 | function wp_getPosts( $args ) { |
| 1718 | |
| 1719 | $this->escape( $args ); |
| 1720 | |
| 1721 | $blog_ID = (int) $args[0]; |
| 1722 | $username = $args[1]; |
| 1723 | $password = $args[2]; |
| 1724 | $filter = $args[3]; |
| 1725 | |
| 1726 | if ( ! $user = $this->login( $username, $password ) ) |
| 1727 | return $this->error; |
| 1728 | |
| 1729 | $query = array(); |
| 1730 | |
| 1731 | if ( isset( $filter['post_type'] ) ) { |
| 1732 | |
| 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 | |
| 1743 | $query['numberposts'] = apply_filters( 'xmlrpc_getPosts_maxvalue', 10 );// maximum value |
| 1744 | if ( isset ( $filter['numberposts'] ) ) { |
| 1745 | |
| 1746 | if( absint( $filter['numberposts'] ) < 10 ) |
| 1747 | $query['number'] = absint( $filter['numberposts'] ); |
| 1748 | |
| 1749 | } |
| 1750 | |
| 1751 | $posts = wp_get_recent_posts( $query ); |
| 1752 | |
| 1753 | if ( ! $posts ) |
| 1754 | return array( ); |
| 1755 | |
| 1756 | // holds all the post data |
| 1757 | $struct = array(); |
| 1758 | |
| 1759 | foreach ( $posts as $post ) { |
| 1760 | |
| 1761 | $post_type = get_post_type_object( $post['post_type'] ); |
| 1762 | |
| 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 | |
| 1788 | $sticky = false; |
| 1789 | if ( is_sticky( $post_ID ) ) |
| 1790 | $sticky = true; |
| 1791 | |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | $post_type_taxonomies = get_object_taxonomies( $post['post_type'] , 'names'); |
| 1796 | $terms = wp_get_object_terms( $post_ID, $post_type_taxonomies ); |
| 1797 | |
| 1798 | $enclosure = array(); |
| 1799 | foreach ( (array) get_post_custom($post_ID) as $key => $val) { |
| 1800 | if ($key == 'enclosure') { |
| 1801 | foreach ( (array) $val as $enc ) { |
| 1802 | $encdata = split("\n", $enc); |
| 1803 | $enclosure['url'] = trim(htmlspecialchars($encdata[0])); |
| 1804 | $enclosure['length'] = (int) trim($encdata[1]); |
| 1805 | $enclosure['type'] = trim($encdata[2]); |
| 1806 | break 2; |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | |
| 1812 | // backward compatiblity |
| 1813 | $categories = array(); |
| 1814 | $catids = wp_get_post_categories($post_ID); |
| 1815 | foreach($catids as $catid) { |
| 1816 | $categories[] = get_cat_name($catid); |
| 1817 | } |
| 1818 | |
| 1819 | $tagnames = array(); |
| 1820 | $tags = wp_get_post_tags( $post_ID ); |
| 1821 | if ( !empty( $tags ) ) { |
| 1822 | foreach ( $tags as $tag ) |
| 1823 | $tagnames[] = $tag->name; |
| 1824 | $tagnames = implode( ', ', $tagnames ); |
| 1825 | } else { |
| 1826 | $tagnames = ''; |
| 1827 | } |
| 1828 | |
| 1829 | $post_data[] = array( |
| 1830 | 'postid' => $post['ID'], |
| 1831 | 'title' => $post['post_title'], |
| 1832 | 'description' => $post_content['main'], |
| 1833 | 'mt_excerpt' => $post['post_excerpt'], |
| 1834 | |
| 1835 | 'post_status' => $post['post_status'], |
| 1836 | 'post_type' => $post['post_type'], |
| 1837 | 'wp_slug' => $post['post_name'], |
| 1838 | 'wp_password' => $post['post_password'], |
| 1839 | |
| 1840 | 'wp_page_order' => $post['menu_order'], |
| 1841 | 'wp_page_parent_id' => $post['post_parent'], |
| 1842 | |
| 1843 | 'wp_author_id' => $post['post_author'], |
| 1844 | |
| 1845 | 'mt_allow_comments' => $post['comment_status'], |
| 1846 | 'mt_allow_pings' => $post['ping_status'], |
| 1847 | |
| 1848 | 'dateCreated' => new IXR_Date($post_date), |
| 1849 | 'date_created_gmt' => new IXR_Date($post_date_gmt), |
| 1850 | |
| 1851 | 'userid' => $post['post_author'], |
| 1852 | 'sticky' => $sticky, |
| 1853 | 'custom_fields' => $wp_xmlrpc_server->get_custom_fields($post_ID), |
| 1854 | 'terms' => $terms, |
| 1855 | |
| 1856 | 'link' => $link, |
| 1857 | 'permaLink' => $link, |
| 1858 | |
| 1859 | // backward compatibility |
| 1860 | 'categories' => $categories, |
| 1861 | 'mt_keywords' => $tagnames, |
| 1862 | 'wp_post_format' => $post_format, |
| 1863 | |
| 1864 | ); |
| 1865 | |
| 1866 | if ( ! empty( $enclosure ) ) |
| 1867 | $post_data['enclosure'] = $enclosure; |
| 1868 | |
| 1869 | $struct[] = $post_date; |
| 1870 | |
| 1871 | } |
| 1872 | |
| 1873 | return $struct; |
| 1874 | |
| 1875 | } |
| 1876 | |