| 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 |
| 1724 | * - wp_post_format |
| 1725 | * - enclosure |
| 1726 | * |
| 1727 | * @since 3.4 |
| 1728 | * |
| 1729 | * @param array $args Method parameters. Contains: |
| 1730 | * - blog_id |
| 1731 | * - username |
| 1732 | * - password |
| 1733 | * - content_struct |
| 1734 | * @return int |
| 1735 | */ |
| 1736 | function wp_newPost( $args ) { |
| 1737 | $this->escape( $args ); |
| 1738 | |
| 1739 | $blog_id = (int) $args[0]; // we will support this in the near future |
| 1740 | $username = $args[1]; |
| 1741 | $password = $args[2]; |
| 1742 | $content_struct = $args[3]; |
| 1743 | |
| 1744 | if ( ! $user = $this->login($username, $password) ) |
| 1745 | return $this->error; |
| 1746 | |
| 1747 | do_action( 'xmlrpc_call', 'wp.newPost' ); |
| 1748 | |
| 1749 | $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0, |
| 1750 | 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '', 'sticky' => 0 ); |
| 1751 | |
| 1752 | $post_data = wp_parse_args( $content_struct, $defaults ); |
| 1753 | |
| 1754 | $post_type = get_post_type_object( $post_data['post_type'] ); |
| 1755 | if( ! ( (bool)$post_type ) ) |
| 1756 | return new IXR_Error( 403, __( 'Invalid post type' ) ); |
| 1757 | |
| 1758 | if( ! current_user_can( $post_type->cap->edit_posts ) ) |
| 1759 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) ); |
| 1760 | |
| 1761 | switch ( $post_data['post_status'] ) { |
| 1762 | case 'draft': |
| 1763 | case 'pending': |
| 1764 | break; |
| 1765 | case 'private': |
| 1766 | if( ! current_user_can( $post_type->cap->publish_posts ) ) |
| 1767 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' )); |
| 1768 | break; |
| 1769 | case 'publish': |
| 1770 | case 'future': |
| 1771 | if( ! current_user_can( $post_type->cap->publish_posts ) ) |
| 1772 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' )); |
| 1773 | break; |
| 1774 | default: |
| 1775 | $post_data['post_status'] = 'draft'; |
| 1776 | break; |
| 1777 | } |
| 1778 | |
| 1779 | if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) ) |
| 1780 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) ); |
| 1781 | |
| 1782 | |
| 1783 | $post_data['post_author'] = absint( $post_data['post_author'] ); |
| 1784 | if( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) { |
| 1785 | if( ! current_user_can( $post_type->cap->edit_others_posts ) ) |
| 1786 | return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) ); |
| 1787 | |
| 1788 | $author = get_userdata( $post_data['post_author'] ); |
| 1789 | |
| 1790 | if( ! $author ) |
| 1791 | return new IXR_Error( 404, __( 'Invalid author ID.' ) ); |
| 1792 | } |
| 1793 | else { |
| 1794 | $post_data['post_author'] = $user->ID; |
| 1795 | } |
| 1796 | |
| 1797 | if( isset( $post_data['comment_status'] ) ) |
| 1798 | if( ! post_type_supports( $post_data['post_type'], 'comments' ) || ( $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' ) ) |
| 1799 | unset( $post_data['comment_status'] ); |
| 1800 | |
| 1801 | if( isset( $post_data['ping_status'] ) ) |
| 1802 | if( ! post_type_supports( $post_data['post_type'], 'trackbacks' ) || ( $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' ) ) |
| 1803 | unset( $post_data['ping_status'] ); |
| 1804 | |
| 1805 | // Do some timestamp voodoo |
| 1806 | if ( ! empty( $post_data['date_created_gmt'] ) ) |
| 1807 | $dateCreated = str_replace( 'Z', '', $post_data['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 |
| 1808 | elseif ( ! empty( $post_data['date_created']) ) |
| 1809 | $dateCreated = $post_data['date_created']->getIso(); |
| 1810 | |
| 1811 | if ( ! empty( $dateCreated ) ) { |
| 1812 | $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) ); |
| 1813 | $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' ); |
| 1814 | } |
| 1815 | |
| 1816 | $post_ID = $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID; |
| 1817 | |
| 1818 | $sticky = $post_data['sticky'] ? true : false; |
| 1819 | |
| 1820 | if( $post_data['post_type'] == 'post' && $sticky == true ) { |
| 1821 | if( ! current_user_can( $post_type->cap->edit_others_posts ) ) |
| 1822 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) ); |
| 1823 | |
| 1824 | if( $post_data['post_status'] != 'publish' ) |
| 1825 | return new IXR_Error( 401, __( 'Only published posts can be made sticky.' )); |
| 1826 | |
| 1827 | stick_post( $post_ID ); |
| 1828 | } |
| 1829 | |
| 1830 | if( isset ( $post_data['custom_fields'] ) && post_type_supports( $post_data['post_type'], 'custom-fields' ) ) { |
| 1831 | $this->set_custom_fields( $post_ID, $post_data['custom_fields'] ); |
| 1832 | } |
| 1833 | |
| 1834 | if( isset( $post_data['tax_input'] ) ) { |
| 1835 | if( is_array( $post_data['tax_input'] ) ) { |
| 1836 | $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'] ); |
| 1837 | $taxonomies = array_keys( $post_data['tax_input'] ); |
| 1838 | |
| 1839 | // validating term ids |
| 1840 | foreach( $taxonomies as $taxonomy ) { |
| 1841 | if( ! in_array( $taxonomy , $post_type_taxonomies ) ) |
| 1842 | return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) ); |
| 1843 | |
| 1844 | $taxonomy_obj = get_taxonomy( $taxonomy ); |
| 1845 | $terms_names = get_terms( $taxonomy, array( 'fields'=>'names', 'hide_empty' => false ) ); |
| 1846 | $terms_int = array_map( 'intval', $post_data['tax_input'][ $taxonomy ] ); |
| 1847 | |
| 1848 | if( ! current_user_can( $taxonomy_obj->cap->assign_terms ) ) |
| 1849 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies' ) ); |
| 1850 | |
| 1851 | if( ! current_user_can( $taxonomy_obj->cap->edit_terms ) && array_diff( $post_data['tax_input'][ $taxonomy ], $terms_names ) ) |
| 1852 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies' ) ); |
| 1853 | |
| 1854 | if( ! array_diff( $post_data['tax_input'][ $taxonomy ], $terms_int ) ) { |
| 1855 | $post_data['tax_input'][ $taxonomy ] = $terms_int; |
| 1856 | } |
| 1857 | else if( is_taxonomy_hierarchical( $taxonomy ) ) { |
| 1858 | $terms_double = array_diff_key( $terms_names , array_unique( $terms_names ) ); |
| 1859 | |
| 1860 | if( array_diff( $post_data['tax_input'][ $taxonomy ], $terms_double ) != $post_data['tax_input'][ $taxonomy ] ) |
| 1861 | return new IXR_Error( 401, __( 'Sorry, one of the terms provided exists more then once' ) ); |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | foreach( $taxonomies as $taxonomy ) { |
| 1867 | wp_set_object_terms( $post_ID, $post_data['tax_input'][ $taxonomy ], $taxonomy ); |
| 1868 | |
| 1869 | if( is_taxonomy_hierarchical( $taxonomy ) ) { |
| 1870 | $terms_int = array_map( 'intval', $post_data['tax_input'][ $taxonomy ] ); |
| 1871 | |
| 1872 | if( array_diff( $post_data['tax_input'][ $taxonomy ], $terms_int ) ) { |
| 1873 | $post_category = array(); |
| 1874 | |
| 1875 | foreach( $post_data['tax_input'][ $taxonomy ] as $term_name ) { |
| 1876 | $term_object = get_term_by( 'name', $term_name, $taxonomy ); |
| 1877 | |
| 1878 | if( $term_object ) { |
| 1879 | $post_category[] = intval( $term_object->term_id ); |
| 1880 | } |
| 1881 | else { |
| 1882 | $post_category[] = $term_name; |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | $post_data['tax_input'][ $taxonomy ] = $post_category; |
| 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | if( isset( $post_data['wp_post_format'] ) ) { |
| 1893 | set_post_format( $post_ID, $post_data['wp_post_format'] ); |
| 1894 | } |
| 1895 | |
| 1896 | // Handle enclosures |
| 1897 | $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null; |
| 1898 | $this->add_enclosure_if_new( $post_ID, $enclosure ); |
| 1899 | |
| 1900 | $this->attach_uploads( $post_ID, $post_data['post_content'] ); |
| 1901 | |
| 1902 | $post_ID = wp_insert_post( $post_data, true ); |
| 1903 | if ( is_wp_error( $post_ID ) ) |
| 1904 | return new IXR_Error( 500, $post_ID->get_error_message() ); |
| 1905 | |
| 1906 | if ( ! $post_ID ) |
| 1907 | return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) ); |
| 1908 | |
| 1909 | return strval( $post_ID ); |
| 1910 | } |
| 1911 | |