| 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 | $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', |
| 1748 | 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '', 'sticky' => 0 ); |
| 1749 | |
| 1750 | $post_data = wp_parse_args( $content_struct, $defaults ); |
| 1751 | |
| 1752 | $post_type = get_post_type_object( $post_data['post_type'] ); |
| 1753 | if( ! ( (bool)$post_type ) ) |
| 1754 | return new IXR_Error( 403, __( 'Invalid post type' ) ); |
| 1755 | |
| 1756 | if( ! current_user_can( $post_type->cap->edit_posts ) ) |
| 1757 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) ); |
| 1758 | |
| 1759 | do_action( 'xmlrpc_call', 'wp.newPost' ); |
| 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 | if( isset( $post_data['post_author'] ) && absint( $post_data['post_author'] ) != $user->ID ) { |
| 1784 | if( !current_user_can( $post_type->cap->edit_others_posts ) ) |
| 1785 | return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) ); |
| 1786 | |
| 1787 | $post_data['post_author'] = absint( $post_data['post_author'] ); |
| 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'] ) && ( !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'] ) && ( !post_type_supports( $post_data['post_type'], 'trackbacks' ) || ( !$post_data['ping_status'] == 'open' && !$post_data['ping_status'] == 'closed' ) ) ) |
| 1802 | unset( $post_data['ping_status'] ); |
| 1803 | |
| 1804 | // Do some timestamp voodoo |
| 1805 | if ( ! empty( $post_data['date_created_gmt'] ) ) |
| 1806 | $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 |
| 1807 | elseif ( !empty( $post_data['date_created']) ) |
| 1808 | $dateCreated = $post_data['date_created']->getIso(); |
| 1809 | |
| 1810 | if ( ! empty( $dateCreated ) ) { |
| 1811 | $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) ); |
| 1812 | $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' ); |
| 1813 | } |
| 1814 | |
| 1815 | $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID; |
| 1816 | |
| 1817 | $sticky = $post_data['sticky'] ? true : false; |
| 1818 | |
| 1819 | if( $post_data['post_type'] == 'post' && $sticky == true ) { |
| 1820 | if( ! current_user_can( $post_type->cap->edit_others_posts ) ) |
| 1821 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) ); |
| 1822 | |
| 1823 | if( $post_data['post_status'] != 'publish' ) |
| 1824 | return new IXR_Error( 401, __( 'Only published posts can be made sticky.' )); |
| 1825 | |
| 1826 | stick_post( $post_data['ID'] ); |
| 1827 | } |
| 1828 | |
| 1829 | if( isset ( $post_data['custom_fields'] ) && post_type_supports( $post_data['post_type'], 'custom-fields' ) ) { |
| 1830 | $this->set_custom_fields( $post_data['ID'], $post_data['custom_fields'] ); |
| 1831 | } |
| 1832 | |
| 1833 | if( isset( $post_data['tax_input'] ) ) { |
| 1834 | if( is_array( $post_data['tax_input'] ) ) { |
| 1835 | $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'] ); |
| 1836 | $terms = $post_data['tax_input']; |
| 1837 | $taxonomies = array_keys( $terms ); |
| 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', $terms[ $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( $terms[ $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( $terms[ $taxonomy ], $terms_int ) ) { |
| 1855 | $terms[ $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( $terms[ $taxonomy ], $terms_double ) != $terms_needed ) |
| 1861 | return new IXR_Error( 401, __( 'Sorry, one of the terms provided exists more then once' ) ); |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | foreach( $taxonomies as $taxonomy ) { |
| 1866 | wp_set_object_terms( $post_data['ID'] , $terms[ $taxonomy ], $taxonomy ); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | unset( $post_data['tax_input'] ); |
| 1871 | } |
| 1872 | |
| 1873 | if( isset( $post_data['wp_post_format'] ) ) { |
| 1874 | set_post_format( $post_data['ID'], $post_data['wp_post_format'] ); |
| 1875 | } |
| 1876 | |
| 1877 | // Handle enclosures |
| 1878 | $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null; |
| 1879 | $this->add_enclosure_if_new( $post_data['ID'], $enclosure ); |
| 1880 | $this->attach_uploads( $post_data['ID'], $post_data['post_content'] ); |
| 1881 | |
| 1882 | $post_ID = wp_insert_post( $post_data, true ); |
| 1883 | |
| 1884 | if ( is_wp_error( $post_ID ) ) |
| 1885 | return new IXR_Error( 500, $post_ID->get_error_message() ); |
| 1886 | |
| 1887 | if ( ! $post_ID ) |
| 1888 | return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) ); |
| 1889 | |
| 1890 | return $post_ID; |
| 1891 | } |
| 1892 | |