| | 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', 'post_author' => $user->ID, |
| | 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 | if( ! current_user_can( $post_type->cap->publish_posts ) ) |
| | 1771 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' )); |
| | 1772 | break; |
| | 1773 | default: |
| | 1774 | $post_data['post_status'] = 'draft'; |
| | 1775 | break; |
| | 1776 | } |
| | 1777 | |
| | 1778 | $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID; |
| | 1779 | |
| | 1780 | if( $post_data['post_type'] == 'post' ) { |
| | 1781 | if( ! current_user_can( $post_type->cap->edit_others_posts ) ) |
| | 1782 | return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) ); |
| | 1783 | |
| | 1784 | $sticky = $post_data['sticky'] ? true : false; |
| | 1785 | |
| | 1786 | if( $sticky ) { |
| | 1787 | if( $post_data['post_status'] != 'publish' ) |
| | 1788 | return new IXR_Error( 401, __( 'Only published posts can be made sticky.' )); |
| | 1789 | stick_post( $post_data['ID'] ); |
| | 1790 | } |
| | 1791 | else { |
| | 1792 | unstick_post( $post_data['ID'] ); |
| | 1793 | } |
| | 1794 | } |
| | 1795 | |
| | 1796 | if( isset ( $post_data['custom_fields'] ) && post_type_supports( $post_data['post_type'], 'custom-fields' ) ) { |
| | 1797 | $this->set_custom_fields( $post_data['ID'], $post_data['custom_fields'] ); |
| | 1798 | } |
| | 1799 | |
| | 1800 | if( isset( $post_data['terms'] ) ) { |
| | 1801 | $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'] ); |
| | 1802 | $terms = $post_data['terms']; |
| | 1803 | $taxonomies = array_keys( $terms ); |
| | 1804 | |
| | 1805 | // validating term ids |
| | 1806 | foreach( $taxonomies as $taxonomy ) { |
| | 1807 | if( ! in_array( $taxonomy , $post_type_taxonomies ) ) |
| | 1808 | return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' )); |
| | 1809 | |
| | 1810 | $terms_int = array_map( 'intval', $terms[ $taxonomy ] ); |
| | 1811 | |
| | 1812 | if( is_taxonomy_hierarchical( $taxonomy ) ) { |
| | 1813 | if( array_diff( $terms[ $taxonomy ], $terms_int ) ) |
| | 1814 | return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is hierachical with string values' )); |
| | 1815 | } |
| | 1816 | else { |
| | 1817 | if( !array_diff( $terms[ $taxonomy ], $terms_int ) ) |
| | 1818 | $terms[ $taxonomy ] = $terms_int; |
| | 1819 | } |
| | 1820 | } |
| | 1821 | |
| | 1822 | foreach( $taxonomies as $taxonomy ) { |
| | 1823 | wp_set_object_terms( $post_data['ID'] , $terms[ $taxonomy ], $taxonomy ); |
| | 1824 | } |
| | 1825 | } |
| | 1826 | |
| | 1827 | if( isset( $post_data['wp_post_format'] ) ) { |
| | 1828 | set_post_format( $post_data['ID'], $post_data['wp_post_format'] ); |
| | 1829 | } |
| | 1830 | |
| | 1831 | // Handle enclosures |
| | 1832 | $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null; |
| | 1833 | $this->add_enclosure_if_new( $post_data['ID'], $enclosure ); |
| | 1834 | $this->attach_uploads( $post_data['ID'], $post_data['post_content'] ); |
| | 1835 | |
| | 1836 | $post_ID = wp_insert_post( $post_data, true ); |
| | 1837 | |
| | 1838 | if ( is_wp_error( $post_ID ) ) |
| | 1839 | return new IXR_Error( 500, $post_ID->get_error_message() ); |
| | 1840 | |
| | 1841 | if ( ! $post_ID ) |
| | 1842 | return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) ); |
| | 1843 | |
| | 1844 | return $post_ID; |
| | 1845 | } |
| | 1846 | |