| | 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 | $sticky = $post_data['sticky'] ? true : false; |
| | 1817 | |
| | 1818 | if( $post_data['post_type'] == 'post' && $sticky == true ) { |
| | 1819 | if( ! current_user_can( $post_type->cap->edit_others_posts ) ) |
| | 1820 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) ); |
| | 1821 | |
| | 1822 | if( $post_data['post_status'] != 'publish' ) |
| | 1823 | return new IXR_Error( 401, __( 'Only published posts can be made sticky.' )); |
| | 1824 | } |
| | 1825 | |
| | 1826 | if( isset( $post_data['tax_input'] ) ) { |
| | 1827 | if( is_array( $post_data['tax_input'] ) ) { |
| | 1828 | $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'] ); |
| | 1829 | $terms = $post_data['tax_input']; |
| | 1830 | $taxonomies = array_keys( $terms ); |
| | 1831 | |
| | 1832 | // validating term ids |
| | 1833 | foreach( $taxonomies as $taxonomy ) { |
| | 1834 | if( ! in_array( $taxonomy , $post_type_taxonomies ) ) |
| | 1835 | return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) ); |
| | 1836 | |
| | 1837 | $taxonomy_obj = get_taxonomy( $taxonomy ); |
| | 1838 | $terms_names = get_terms( $taxonomy, array( 'fields'=>'names', 'hide_empty' => false ) ); |
| | 1839 | $terms_int = array_map( 'intval', $terms[ $taxonomy ] ); |
| | 1840 | |
| | 1841 | if( ! current_user_can( $taxonomy_obj->cap->assign_terms ) ) |
| | 1842 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies' ) ); |
| | 1843 | |
| | 1844 | if( ! current_user_can( $taxonomy_obj->cap->edit_terms ) && array_diff( $terms[ $taxonomy ], $terms_names ) ) |
| | 1845 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies' ) ); |
| | 1846 | |
| | 1847 | if( ! array_diff( $terms[ $taxonomy ], $terms_int ) ) { |
| | 1848 | $terms[ $taxonomy ] = $terms_int; |
| | 1849 | } |
| | 1850 | else if( is_taxonomy_hierarchical( $taxonomy ) ) { |
| | 1851 | $terms_double = array_diff_key( $terms_names , array_unique( $terms_names ) ); |
| | 1852 | |
| | 1853 | if( array_diff( $terms[ $taxonomy ], $terms_double ) != $terms[ $taxonomy ] ) |
| | 1854 | return new IXR_Error( 401, __( 'Sorry, one of the terms provided exists more then once' ) ); |
| | 1855 | } |
| | 1856 | } |
| | 1857 | } |
| | 1858 | |
| | 1859 | unset( $post_data['tax_input'] ); |
| | 1860 | } |
| | 1861 | |
| | 1862 | $post_ID = wp_insert_post( $post_data, true ); |
| | 1863 | |
| | 1864 | if ( is_wp_error( $post_ID ) ) |
| | 1865 | return new IXR_Error( 500, $post_ID->get_error_message() ); |
| | 1866 | |
| | 1867 | if ( ! $post_ID ) |
| | 1868 | return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) ); |
| | 1869 | |
| | 1870 | |
| | 1871 | if( $post_data['post_type'] == 'post' && $sticky == true ) { |
| | 1872 | stick_post( $post_ID ); |
| | 1873 | } |
| | 1874 | |
| | 1875 | if( isset ( $post_data['custom_fields'] ) && post_type_supports( $post_data['post_type'], 'custom-fields' ) ) { |
| | 1876 | $this->set_custom_fields( $post_ID, $post_data['custom_fields'] ); |
| | 1877 | } |
| | 1878 | |
| | 1879 | // Loop the data that was previous defined |
| | 1880 | if( isset( $taxonomies, $terms ) ) { |
| | 1881 | foreach( $taxonomies as $taxonomy ) { |
| | 1882 | wp_set_object_terms( $post_ID, $terms[ $taxonomy ], $taxonomy ); |
| | 1883 | } |
| | 1884 | } |
| | 1885 | |
| | 1886 | if( isset( $post_data['wp_post_format'] ) ) { |
| | 1887 | set_post_format( $post_ID, $post_data['wp_post_format'] ); |
| | 1888 | } |
| | 1889 | |
| | 1890 | // Handle enclosures |
| | 1891 | $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null; |
| | 1892 | $this->add_enclosure_if_new( $post_ID, $enclosure ); |
| | 1893 | $this->attach_uploads( $post_ID, $post_data['post_content'] ); |
| | 1894 | |
| | 1895 | return $post_ID; |
| | 1896 | } |
| | 1897 | |