| | 1706 | /** |
| | 1707 | * Edit a post |
| | 1708 | * |
| | 1709 | * @uses wp_update_post() |
| | 1710 | * @param array $args Method parameters. Contains: |
| | 1711 | * - int $post_id |
| | 1712 | * - string $username |
| | 1713 | * - string $password |
| | 1714 | * - array $content_struct |
| | 1715 | * - boolean $publish optional. Defaults to true |
| | 1716 | * @return string post_id |
| | 1717 | */ |
| | 1718 | function wp_editPost($args) { |
| | 1719 | |
| | 1720 | $this->escape( $args ); |
| | 1721 | |
| | 1722 | $post_ID = (int) $args[0]; |
| | 1723 | $username = $args[1]; |
| | 1724 | $password = $args[2]; |
| | 1725 | $content_struct = $args[3]; |
| | 1726 | $publish = isset( $args[4] ) ? $args[4] : false; |
| | 1727 | |
| | 1728 | if ( ! $user = $this->login( $username, $password ) ) |
| | 1729 | return $this->error; |
| | 1730 | |
| | 1731 | $post = wp_get_single_post( $post_ID, ARRAY_A ); |
| | 1732 | if ( empty( $post['ID'] ) ) |
| | 1733 | return new IXR_Error( 404, __( 'Invalid post ID.' ) ); |
| | 1734 | |
| | 1735 | $post_type = get_post_type_object( $post['post_type'] ); |
| | 1736 | if( ! current_user_can( $post_type->cap->edit_posts, $post_ID ) ) |
| | 1737 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to create posts in this post type' )); |
| | 1738 | |
| | 1739 | // this holds all the post data needed |
| | 1740 | $post_data = array(); |
| | 1741 | $post_data['ID'] = $post_ID; |
| | 1742 | $post_data['post_status'] = $publish ? 'publish' : 'draft'; |
| | 1743 | |
| | 1744 | if( isset ( $content_struct["{$content_struct['post_type']}_status"] ) ) |
| | 1745 | $post_data['post_status'] = $content_struct["{$post_data['post_type']}_status"]; |
| | 1746 | |
| | 1747 | |
| | 1748 | switch ( $post_data['post_status'] ) { |
| | 1749 | |
| | 1750 | case 'draft': |
| | 1751 | break; |
| | 1752 | case 'pending': |
| | 1753 | break; |
| | 1754 | case 'private': |
| | 1755 | if( ! current_user_can( $post_type->cap->publish_posts, $post_ID ) ) |
| | 1756 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' )); |
| | 1757 | break; |
| | 1758 | case 'publish': |
| | 1759 | if( ! current_user_can( $post_type->cap->publish_posts, $post_ID ) ) |
| | 1760 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' )); |
| | 1761 | break; |
| | 1762 | default: |
| | 1763 | return new IXR_Error( 401, __( 'The post status specified is not valid' ) ); |
| | 1764 | break; |
| | 1765 | |
| | 1766 | } |
| | 1767 | |
| | 1768 | // Only use a password if one was given. |
| | 1769 | if ( isset( $content_struct['wp_password'] ) ) { |
| | 1770 | |
| | 1771 | if( ! current_user_can( $post_type->cap->publish_posts ) ) |
| | 1772 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' )); |
| | 1773 | $post_data['post_password'] = $content_struct['wp_password']; |
| | 1774 | |
| | 1775 | } |
| | 1776 | |
| | 1777 | if ( isset( $content_struct['wp_slug'] ) ) |
| | 1778 | $post_data['post_name'] = $content_struct['wp_slug']; |
| | 1779 | |
| | 1780 | if ( isset( $content_struct['wp_page_order'] ) ) { |
| | 1781 | |
| | 1782 | if( ! post_type_supports( $content_struct['post_type'] , 'page-attributes' ) ) |
| | 1783 | return new IXR_Error( 401, __( 'This post type does not support page attributes' ) ); |
| | 1784 | |
| | 1785 | $post_data['menu_order'] = $content_struct['wp_page_order']; |
| | 1786 | |
| | 1787 | } |
| | 1788 | |
| | 1789 | if ( isset( $content_struct['wp_page_parent_id'] ) ) { |
| | 1790 | |
| | 1791 | if( ! $post_type->hierarchical ) |
| | 1792 | return new IXR_Error(401, __('This post type does not support post hierarchy')); |
| | 1793 | |
| | 1794 | // validating parent ID |
| | 1795 | $parent_ID = (int)$content_struct['wp_page_parent_id']; |
| | 1796 | if( $parent_ID != 0 ) { |
| | 1797 | |
| | 1798 | $parent_post = (array)wp_get_single_post( $parent_ID ); |
| | 1799 | if ( empty( $parent_post['ID'] ) ) |
| | 1800 | return new IXR_Error( 401, __( 'Invalid parent ID.' ) ); |
| | 1801 | |
| | 1802 | if ( $parent_post['post_type'] != $content_struct['post_type'] ) |
| | 1803 | return new IXR_Error( 401, __( 'The parent post is of different post type' ) ); |
| | 1804 | |
| | 1805 | } |
| | 1806 | |
| | 1807 | $post_data['post_parent'] = $content_struct['wp_page_parent_id']; |
| | 1808 | |
| | 1809 | } |
| | 1810 | |
| | 1811 | // page template is only supported only by pages |
| | 1812 | if ( isset( $content_struct['wp_page_template'] ) ) { |
| | 1813 | |
| | 1814 | if( $content_struct['post_type'] != 'page' ) |
| | 1815 | return new IXR_Error(401, __('Page templates are only supported by pages')); |
| | 1816 | |
| | 1817 | // validating page template |
| | 1818 | $page_templates = get_page_templates( ); |
| | 1819 | $page_templates['Default'] = 'default'; |
| | 1820 | |
| | 1821 | if( ! array_key_exists( $content_struct['wp_page_template'], $page_templates ) ) |
| | 1822 | return new IXR_Error( 403, __( 'Invalid page template.' ) ); |
| | 1823 | |
| | 1824 | $post_data['page_template'] = $content_struct['wp_page_template']; |
| | 1825 | |
| | 1826 | } |
| | 1827 | |
| | 1828 | |
| | 1829 | // If an author id was provided then use it instead. |
| | 1830 | if( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) { |
| | 1831 | |
| | 1832 | if( ! post_type_supports( $content_struct['post_type'] , 'author' ) ) |
| | 1833 | return new IXR_Error( 401, __( 'This post type does not support to set author.' ) ); |
| | 1834 | |
| | 1835 | if( ! current_user_can( $post_type->cap->edit_others_posts ) ) |
| | 1836 | return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) ); |
| | 1837 | |
| | 1838 | $author_ID = (int)$content_struct['wp_author_id']; |
| | 1839 | |
| | 1840 | $author = get_userdata( $author_ID ); |
| | 1841 | if( ! $author ) |
| | 1842 | return new IXR_Error( 404, __( 'Invalid author ID.' ) ); |
| | 1843 | |
| | 1844 | $post_data['post_author '] = $author_ID; |
| | 1845 | |
| | 1846 | } |
| | 1847 | |
| | 1848 | |
| | 1849 | if( isset ( $content_struct['title'] ) ) { |
| | 1850 | |
| | 1851 | if( ! post_type_supports( $content_struct['post_type'] , 'title' ) ) |
| | 1852 | return new IXR_Error( 401, __( 'This post type does not support title attribute.' ) ); |
| | 1853 | $post_data['post_title'] = $content_struct['title']; |
| | 1854 | |
| | 1855 | } |
| | 1856 | |
| | 1857 | if( isset ( $content_struct['post_content'] ) ) { |
| | 1858 | |
| | 1859 | if( ! post_type_supports( $content_struct['post_type'] , 'editor' ) ) |
| | 1860 | return new IXR_Error( 401, __( 'This post type does not support post content.' ) ); |
| | 1861 | $post_data['post_content'] = $content_struct['post_content']; |
| | 1862 | |
| | 1863 | } |
| | 1864 | |
| | 1865 | if( isset ( $content_struct['mt_excerpt'] ) ) { |
| | 1866 | |
| | 1867 | if( ! post_type_supports( $content_struct['post_type'] , 'excerpt' ) ) |
| | 1868 | return new IXR_Error( 401, __( 'This post type does not support post excerpt.' ) ); |
| | 1869 | $post_data['post_excerpt'] = $content_struct['mt_excerpt']; |
| | 1870 | |
| | 1871 | } |
| | 1872 | |
| | 1873 | if( isset( $content_struct['mt_allow_comments'] ) ) { |
| | 1874 | |
| | 1875 | if( ! post_type_supports( $content_struct['post_type'], 'comments' ) ) |
| | 1876 | return new IXR_Error( 401, __( 'This post type does not support comments.' ) ); |
| | 1877 | |
| | 1878 | if ( ! is_numeric( $content_struct['mt_allow_comments'] ) ) { |
| | 1879 | |
| | 1880 | switch ( $content_struct['mt_allow_comments'] ) { |
| | 1881 | case 'closed': |
| | 1882 | $post_data['comment_status']= 'closed'; |
| | 1883 | break; |
| | 1884 | case 'open': |
| | 1885 | $post_data['comment_status'] = 'open'; |
| | 1886 | break; |
| | 1887 | default: |
| | 1888 | return new IXR_Error( 401, __ ( 'Invalid comment option' ) ); |
| | 1889 | } |
| | 1890 | |
| | 1891 | } else { |
| | 1892 | |
| | 1893 | switch ( (int) $content_struct['mt_allow_comments'] ) { |
| | 1894 | case 0: |
| | 1895 | case 2: |
| | 1896 | $post_data['comment_status'] = 'closed'; |
| | 1897 | break; |
| | 1898 | case 1: |
| | 1899 | $post_data['comment_status'] = 'open'; |
| | 1900 | break; |
| | 1901 | default: |
| | 1902 | return new IXR_Error( 401, __ ( 'Invalid ping option.' ) ); |
| | 1903 | } |
| | 1904 | |
| | 1905 | } |
| | 1906 | |
| | 1907 | } |
| | 1908 | |
| | 1909 | |
| | 1910 | if( isset( $content_struct['mt_allow_pings'] ) ) { |
| | 1911 | |
| | 1912 | if( ! post_type_supports( $content_struct['post_type'], 'trackbacks' ) ) |
| | 1913 | return new IXR_Error(401, __('This post type does not support trackbacks')); |
| | 1914 | |
| | 1915 | if ( ! is_numeric( $content_struct['mt_allow_pings'] ) ) { |
| | 1916 | |
| | 1917 | switch ( $content_struct['mt_allow_pings'] ) { |
| | 1918 | case 'closed': |
| | 1919 | $post_data['ping_status']= 'closed'; |
| | 1920 | break; |
| | 1921 | case 'open': |
| | 1922 | $post_data['ping_status'] = 'open'; |
| | 1923 | break; |
| | 1924 | default: |
| | 1925 | break; |
| | 1926 | } |
| | 1927 | |
| | 1928 | } else { |
| | 1929 | |
| | 1930 | switch ( (int) $content_struct['mt_allow_pings'] ) { |
| | 1931 | case 0: |
| | 1932 | case 2: |
| | 1933 | $post_data['ping_status'] = 'closed'; |
| | 1934 | break; |
| | 1935 | case 1: |
| | 1936 | $post_data['ping_status'] = 'open'; |
| | 1937 | break; |
| | 1938 | default: |
| | 1939 | break; |
| | 1940 | } |
| | 1941 | |
| | 1942 | } |
| | 1943 | |
| | 1944 | } |
| | 1945 | |
| | 1946 | if( isset( $content_struct['mt_text_more'] ) ) { |
| | 1947 | |
| | 1948 | $post_data['post_more'] = $content_struct['mt_text_more']; |
| | 1949 | $post_data['post_content'] = $post_data['post_content'] . '<!--more-->' . $post_data['post_more']; |
| | 1950 | |
| | 1951 | } |
| | 1952 | |
| | 1953 | if ( isset( $content_struct['mt_tb_ping_urls'] ) ) { |
| | 1954 | |
| | 1955 | $post_data['to_ping'] = $content_struct['mt_tb_ping_urls']; |
| | 1956 | if ( is_array($to_ping) ) |
| | 1957 | $post_data['to_ping'] = implode(' ', $to_ping); |
| | 1958 | |
| | 1959 | } |
| | 1960 | |
| | 1961 | // Do some timestamp voodoo |
| | 1962 | if ( ! empty( $content_struct['date_created_gmt'] ) ) |
| | 1963 | $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force |
| | 1964 | elseif ( !empty( $content_struct['dateCreated']) ) |
| | 1965 | $dateCreated = $content_struct['dateCreated']->getIso(); |
| | 1966 | |
| | 1967 | if ( ! empty( $dateCreated ) ) { |
| | 1968 | |
| | 1969 | $post_data['post_date'] = get_date_from_gmt(iso8601_to_datetime($dateCreated)); |
| | 1970 | $post_data['post_date_gmt'] = iso8601_to_datetime($dateCreated, 'GMT'); |
| | 1971 | |
| | 1972 | } |
| | 1973 | |
| | 1974 | // we got everything we need |
| | 1975 | $post_ID = wp_update_post( $post_data, true ); |
| | 1976 | |
| | 1977 | if ( is_wp_error( $post_ID ) ) |
| | 1978 | return new IXR_Error(500, $post_ID->get_error_message()); |
| | 1979 | |
| | 1980 | if ( ! $post_ID ) |
| | 1981 | return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.')); |
| | 1982 | |
| | 1983 | if( isset ( $content_struct['sticky'] ) ) { |
| | 1984 | |
| | 1985 | $sticky = $content_struct['sticky'] ? true : false; |
| | 1986 | |
| | 1987 | if( $sticky ) { |
| | 1988 | |
| | 1989 | if( $post_data['post_status'] != 'publish' ) |
| | 1990 | return new IXR_Error( 401, __( 'Only published posts can be made sticky.' )); |
| | 1991 | |
| | 1992 | if( ! current_user_can( $post_type->cap->edit_others_posts ) ) |
| | 1993 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) ); |
| | 1994 | |
| | 1995 | stick_post( $post_ID ); |
| | 1996 | |
| | 1997 | } else { |
| | 1998 | |
| | 1999 | unstick_post( $post_ID ); |
| | 2000 | |
| | 2001 | } |
| | 2002 | |
| | 2003 | } |
| | 2004 | |
| | 2005 | if( isset ( $content_struct['custom_fields'] ) ) { |
| | 2006 | |
| | 2007 | if( ! post_type_supports( $content_struct['post_type'], 'custom-fields' ) ) |
| | 2008 | return new IXR_Error(401, __('This post type does not support custom fields')); |
| | 2009 | $wp_xmlrpc_server->set_custom_fields( $post_ID, $content_struct['custom_fields'] ); |
| | 2010 | |
| | 2011 | } |
| | 2012 | |
| | 2013 | $post_type_taxonomies = get_object_taxonomies( $post['post_type'] ); |
| | 2014 | |
| | 2015 | if( isset( $content_struct['terms'] ) ) { |
| | 2016 | |
| | 2017 | $terms = $content_struct['terms']; |
| | 2018 | $taxonomies = array_keys( $terms ); |
| | 2019 | |
| | 2020 | // validating term ids |
| | 2021 | foreach( $taxonomies as $taxonomy ) { |
| | 2022 | |
| | 2023 | if( ! in_array( $taxonomy , $post_type_taxonomies ) ) |
| | 2024 | return new IXR_Error( 401, __( 'Sorry, one of the given taxonomy is not supported by the post type.' )); |
| | 2025 | |
| | 2026 | $term_ids = $terms[ $taxonomy ]; |
| | 2027 | foreach ( $term_ids as $term_id) { |
| | 2028 | |
| | 2029 | $term = get_term( $term_id, $taxonomy ); |
| | 2030 | |
| | 2031 | if ( is_wp_error( $term ) ) |
| | 2032 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 2033 | |
| | 2034 | if ( ! $term ) |
| | 2035 | return new IXR_Error( 401, __( 'Invalid term ID' ) ); |
| | 2036 | |
| | 2037 | } |
| | 2038 | |
| | 2039 | } |
| | 2040 | |
| | 2041 | foreach( $taxonomies as $taxonomy ) { |
| | 2042 | |
| | 2043 | $term_ids = $terms[ $taxonomy ]; |
| | 2044 | $term_ids = array_map( 'intval', $term_ids ); |
| | 2045 | $term_ids = array_unique( $term_ids ); |
| | 2046 | wp_set_object_terms( $post_ID , $term_ids, $taxonomy , $append); |
| | 2047 | |
| | 2048 | } |
| | 2049 | |
| | 2050 | return true; |
| | 2051 | |
| | 2052 | } |
| | 2053 | |
| | 2054 | // backward compatiblity |
| | 2055 | if ( isset( $content_struct['categories'] ) ) { |
| | 2056 | |
| | 2057 | if( ! in_array( 'category', $post_type_taxonomies ) ) |
| | 2058 | return new IXR_Error( 401, __( 'Sorry, Categories are not supported by the post type' )); |
| | 2059 | |
| | 2060 | $category_names = $content_struct['categories']; |
| | 2061 | |
| | 2062 | foreach( $category_names as $category_name ) { |
| | 2063 | $category_ID = get_cat_ID( $category_name ); |
| | 2064 | if( ! $category_ID ) |
| | 2065 | return new IXR_Error( 401, __( 'Sorry, one of the given categories does not exist!' )); |
| | 2066 | $post_categories[] = $category_ID; |
| | 2067 | } |
| | 2068 | |
| | 2069 | wp_set_post_categories ($post_ID, $post_categories ); |
| | 2070 | |
| | 2071 | } |
| | 2072 | |
| | 2073 | if( isset( $content_struct['mt_keywords'] ) ) { |
| | 2074 | |
| | 2075 | if( ! in_array( 'post_tag' , $post_type_taxonomies ) ) |
| | 2076 | return new IXR_Error( 401, __( 'Sorry, post tags are not supported by the post type' )); |
| | 2077 | |
| | 2078 | wp_set_post_terms( $post_id, $tags, 'post_tag', false); // append is set false here |
| | 2079 | } |
| | 2080 | |
| | 2081 | if( isset( $content_struct['wp_post_format'] ) ) { |
| | 2082 | |
| | 2083 | if( ! in_array( 'post_format' , $post_type_taxonomies ) ) |
| | 2084 | return new IXR_Error( 401, __( 'Sorry, post formats are not supported by the post type' )); |
| | 2085 | |
| | 2086 | wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' ); |
| | 2087 | |
| | 2088 | } |
| | 2089 | |
| | 2090 | // Handle enclosures |
| | 2091 | $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null; |
| | 2092 | $wp_xmlrpc_server->add_enclosure_if_new($post_ID, $thisEnclosure); |
| | 2093 | $wp_xmlrpc_server->attach_uploads( $post_ID, $post_data['post_content'] ); |
| | 2094 | |
| | 2095 | return strval( $post_ID ); |
| | 2096 | |
| | 2097 | } |
| | 2098 | |