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