| 1715 | /** |
| 1716 | * Create a new post |
| 1717 | * |
| 1718 | * Wraps the functionality of wp_insert_post |
| 1719 | * |
| 1720 | * The 'content_struct' argument the arguments of wp_insert_post with additional the following: |
| 1721 | * - sticky |
| 1722 | * - custom_fields |
| 1723 | * - terms - array of taxonomy_name => array_of_term_ids |
| 1724 | * - terms_names - array of taxonomy_name => array_of_term_names |
| 1725 | * - post_format |
| 1726 | * - enclosure |
| 1727 | * |
| 1728 | * @since 3.4 |
| 1729 | * |
| 1730 | * @param array $args Method parameters. Contains: |
| 1731 | * - blog_id |
| 1732 | * - username |
| 1733 | * - password |
| 1734 | * - content_struct |
| 1735 | * @return int |
| 1736 | */ |
| 1737 | function wp_newPost( $args ) { |
| 1738 | $this->escape( $args ); |
| 1739 | |
| 1740 | $blog_id = (int) $args[0]; // we will support this in the near future |
| 1741 | $username = $args[1]; |
| 1742 | $password = $args[2]; |
| 1743 | $content_struct = $args[3]; |
| 1744 | |
| 1745 | if ( ! $user = $this->login($username, $password) ) |
| 1746 | return $this->error; |
| 1747 | |
| 1748 | do_action( 'xmlrpc_call', 'wp.newPost' ); |
| 1749 | |
| 1750 | $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0, |
| 1751 | 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '', 'sticky' => 0 ); |
| 1752 | |
| 1753 | $post_data = wp_parse_args( $content_struct, $defaults ); |
| 1754 | |
| 1755 | $post_type = get_post_type_object( $post_data['post_type'] ); |
| 1756 | if( ! ( (bool)$post_type ) ) |
| 1757 | return new IXR_Error( 403, __( 'Invalid post type' ) ); |
| 1758 | |
| 1759 | if( ! current_user_can( $post_type->cap->edit_posts ) ) |
| 1760 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) ); |
| 1761 | |
| 1762 | switch ( $post_data['post_status'] ) { |
| 1763 | case 'draft': |
| 1764 | case 'pending': |
| 1765 | break; |
| 1766 | case 'private': |
| 1767 | if( ! current_user_can( $post_type->cap->publish_posts ) ) |
| 1768 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' )); |
| 1769 | break; |
| 1770 | case 'publish': |
| 1771 | case 'future': |
| 1772 | if( ! current_user_can( $post_type->cap->publish_posts ) ) |
| 1773 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' )); |
| 1774 | break; |
| 1775 | default: |
| 1776 | $post_data['post_status'] = 'draft'; |
| 1777 | break; |
| 1778 | } |
| 1779 | |
| 1780 | if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) ) |
| 1781 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) ); |
| 1782 | |
| 1783 | |
| 1784 | $post_data['post_author'] = absint( $post_data['post_author'] ); |
| 1785 | if( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) { |
| 1786 | if( ! current_user_can( $post_type->cap->edit_others_posts ) ) |
| 1787 | return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) ); |
| 1788 | |
| 1789 | $author = get_userdata( $post_data['post_author'] ); |
| 1790 | |
| 1791 | if( ! $author ) |
| 1792 | return new IXR_Error( 404, __( 'Invalid author ID.' ) ); |
| 1793 | } |
| 1794 | else { |
| 1795 | $post_data['post_author'] = $user->ID; |
| 1796 | } |
| 1797 | |
| 1798 | if( isset( $post_data['comment_status'] ) ) |
| 1799 | if( ! post_type_supports( $post_data['post_type'], 'comments' ) || ( $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' ) ) |
| 1800 | unset( $post_data['comment_status'] ); |
| 1801 | |
| 1802 | if( isset( $post_data['ping_status'] ) ) |
| 1803 | if( ! post_type_supports( $post_data['post_type'], 'trackbacks' ) || ( $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' ) ) |
| 1804 | unset( $post_data['ping_status'] ); |
| 1805 | |
| 1806 | // Do some timestamp voodoo |
| 1807 | if ( ! empty( $post_data['post_date_gmt'] ) ) |
| 1808 | $dateCreated = str_replace( 'Z', '', $post_data['post_date_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force |
| 1809 | elseif ( ! empty( $post_data['post_date']) ) |
| 1810 | $dateCreated = $post_data['post_date']->getIso(); |
| 1811 | |
| 1812 | if ( ! empty( $dateCreated ) ) { |
| 1813 | $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) ); |
| 1814 | $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' ); |
| 1815 | } |
| 1816 | |
| 1817 | $post_ID = $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID; |
| 1818 | |
| 1819 | $sticky = $post_data['sticky'] ? true : false; |
| 1820 | |
| 1821 | if( $post_data['post_type'] == 'post' && $sticky == true ) { |
| 1822 | if( ! current_user_can( $post_type->cap->edit_others_posts ) ) |
| 1823 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) ); |
| 1824 | |
| 1825 | if( $post_data['post_status'] != 'publish' ) |
| 1826 | return new IXR_Error( 401, __( 'Only published posts can be made sticky.' )); |
| 1827 | |
| 1828 | stick_post( $post_ID ); |
| 1829 | } |
| 1830 | |
| 1831 | if( isset ( $post_data['custom_fields'] ) && post_type_supports( $post_data['post_type'], 'custom-fields' ) ) { |
| 1832 | $this->set_custom_fields( $post_ID, $post_data['custom_fields'] ); |
| 1833 | } |
| 1834 | |
| 1835 | if( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) { |
| 1836 | $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' ); |
| 1837 | |
| 1838 | // accumulate term IDs from terms and terms_names |
| 1839 | $terms = array(); |
| 1840 | |
| 1841 | // first validate the terms specified by ID |
| 1842 | if( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) { |
| 1843 | $taxonomies = array_keys( $post_data['terms'] ); |
| 1844 | |
| 1845 | // validating term ids |
| 1846 | foreach ( $taxonomies as $taxonomy ) { |
| 1847 | if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) ) |
| 1848 | return new IXR_Error( 401, __( 'Sorry, one of the given taxonomy is not supported by the post type.' ) ); |
| 1849 | |
| 1850 | if( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) ) |
| 1851 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies' ) ); |
| 1852 | |
| 1853 | $term_ids = $post_data['terms'][$taxonomy]; |
| 1854 | foreach ( $term_ids as $term_id ) { |
| 1855 | $term = get_term_by( 'id', $term_id, $taxonomy ); |
| 1856 | |
| 1857 | if ( ! $term ) |
| 1858 | return new IXR_Error( 403, __( 'Invalid term ID' ) ); |
| 1859 | |
| 1860 | $terms[$taxonomy][] = (int)$term_id; |
| 1861 | } |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | // now validate terms specified by name |
| 1866 | if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) { |
| 1867 | $taxonomies = array_keys( $post_data['terms_names'] ); |
| 1868 | |
| 1869 | foreach ( $taxonomies as $taxonomy ) { |
| 1870 | if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) ) |
| 1871 | return new IXR_Error( 401, __( 'Sorry, one of the given taxonomy is not supported by the post type.' ) ); |
| 1872 | |
| 1873 | if( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) ) |
| 1874 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) ); |
| 1875 | |
| 1876 | // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name |
| 1877 | $ambiguous_terms = array(); |
| 1878 | if( is_taxonomy_hierarchical( $taxonomy ) ) { |
| 1879 | $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) ); |
| 1880 | |
| 1881 | // count the number of terms with the same name |
| 1882 | $tax_term_names_count = array_count_values( $tax_term_names ); |
| 1883 | |
| 1884 | // filter out non-ambiguous term names |
| 1885 | $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, function($count){ |
| 1886 | return $count > 1; |
| 1887 | } ); |
| 1888 | |
| 1889 | $ambiguous_terms = array_keys( $ambiguous_tax_term_counts ); |
| 1890 | } |
| 1891 | |
| 1892 | $term_names = $post_data['terms_names'][$taxonomy]; |
| 1893 | foreach ( $term_names as $term_name ) { |
| 1894 | if ( in_array( $term_name, $ambiguous_terms ) ) |
| 1895 | return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarhical taxonomy. Please use term ID instead.' ) ); |
| 1896 | |
| 1897 | $term = get_term_by( 'name', $term_name, $taxonomy ); |
| 1898 | |
| 1899 | if ( ! $term ) { |
| 1900 | // term doesn't exist, so check that the user is allowed to create new terms |
| 1901 | if( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) ) |
| 1902 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) ); |
| 1903 | |
| 1904 | // create the new term |
| 1905 | $term_info = wp_insert_term( $term_name, $taxonomy ); |
| 1906 | if ( is_wp_error( $term_info ) ) |
| 1907 | return new IXR_Error( 500, $term_info->get_error_message() ); |
| 1908 | |
| 1909 | $terms[$taxonomy][] = (int)$term_info['term_id']; |
| 1910 | } |
| 1911 | else { |
| 1912 | $terms[$taxonomy][] = (int)$term->term_id; |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | // wp_insert_post expects 'post_category' and 'tags_input', and all other taxes in 'tax_input' |
| 1919 | $post_data['tax_input'] = array(); |
| 1920 | foreach ( $terms as $taxonomy => $term_ids ) { |
| 1921 | switch ( $taxonomy ) { |
| 1922 | case 'category': |
| 1923 | $post_data['post_category'] = $term_ids; |
| 1924 | break; |
| 1925 | case 'post_tag': |
| 1926 | $post_data['tags_input'] = $term_ids; |
| 1927 | break; |
| 1928 | default: |
| 1929 | $post_data['tax_input'][$taxonomy] = $term_ids; |
| 1930 | break; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | unset( $post_data['terms'] ); |
| 1935 | unset( $post_data['terms_names'] ); |
| 1936 | } |
| 1937 | |
| 1938 | if( isset( $post_data['post_format'] ) ) { |
| 1939 | $format = set_post_format( $post_ID, $post_data['post_format'] ); |
| 1940 | |
| 1941 | if ( is_wp_error( $format ) ) |
| 1942 | return new IXR_Error( 500, $format->get_error_message() ); |
| 1943 | |
| 1944 | unset( $post_data['post_format'] ); |
| 1945 | } |
| 1946 | |
| 1947 | // Handle enclosures |
| 1948 | $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null; |
| 1949 | $this->add_enclosure_if_new( $post_ID, $enclosure ); |
| 1950 | |
| 1951 | $this->attach_uploads( $post_ID, $post_data['post_content'] ); |
| 1952 | |
| 1953 | $post_ID = wp_insert_post( $post_data, true ); |
| 1954 | if ( is_wp_error( $post_ID ) ) |
| 1955 | return new IXR_Error( 500, $post_ID->get_error_message() ); |
| 1956 | |
| 1957 | if ( ! $post_ID ) |
| 1958 | return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) ); |
| 1959 | |
| 1960 | return strval( $post_ID ); |
| 1961 | } |
| 1962 | |