Make WordPress Core

Ticket #18429: wp.newPost.2.patch

File wp.newPost.2.patch, 5.4 KB (added by markoheijnen, 13 years ago)

Redone the first patch to be a wrapper for wp_insert_post. Removed a lot of checks and replaced code by WordPres functionality

  • wp-includes/class-wp-xmlrpc-server.php

     
    6464                        'wp.getMediaItem'               => 'this:wp_getMediaItem',
    6565                        'wp.getMediaLibrary'    => 'this:wp_getMediaLibrary',
    6666                        'wp.getPostFormats'     => 'this:wp_getPostFormats',
     67                        'wp.newPost'            => 'this:wp_newPost',
    6768
    6869                        // Blogger API
    6970                        'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
     
    17111712                return $formats;
    17121713        }
    17131714
     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                        'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
     1749                        'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
     1750                        'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '',
     1751                        'post_content' => '', 'post_title' => '' );
     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                do_action( 'xmlrpc_call', 'wp.newPost' );
     1763
     1764                switch ( $post_data['post_status'] ) {
     1765                        case 'draft':
     1766                        case 'pending':
     1767                                break;
     1768                        case 'private':
     1769                                if( ! current_user_can( $post_type->cap->publish_posts ) )
     1770                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ));
     1771                                break;
     1772                        case 'publish':
     1773                                if( ! current_user_can( $post_type->cap->publish_posts ) )
     1774                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ));
     1775                                break;
     1776                        default:
     1777                                $post_data['post_status'] = 'draft';
     1778                        break;
     1779                }
     1780
     1781                if( !isset( $post_data['ID'] ) || intval( $post_data['ID'] ) == 0 ) {
     1782                        $post_ID = $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
     1783                }
     1784                else {
     1785                        $post_ID = intval( $post_data['ID'] );
     1786                }
     1787
     1788                if( $post_data['post_type'] == 'post' ) {
     1789                        if( ! current_user_can( $post_type->cap->edit_others_posts ) )
     1790                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
     1791
     1792                        $sticky = $post_data['sticky'] ? true : false;
     1793
     1794                        if( $sticky ) {
     1795                                if( $post_data['post_status'] != 'publish' )
     1796                                        return new IXR_Error( 401, __( 'Only published posts can be made sticky.' ));
     1797                                stick_post( $post_ID );
     1798                        }
     1799                        else {
     1800                                unstick_post( $post_ID );
     1801                        }
     1802                }
     1803
     1804                if( isset ( $post_data['custom_fields'] ) && post_type_supports( $post_data['post_type'], 'custom-fields' ) ) {
     1805                        $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
     1806                }
     1807
     1808                if( isset( $post_data['terms'] ) ) {
     1809                        $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'] );
     1810                        $terms = $post_data['terms'];
     1811                        $taxonomies = array_keys( $terms );
     1812
     1813                        // validating term ids
     1814                        foreach( $taxonomies as $taxonomy ) {
     1815                                if( ! in_array( $taxonomy , $post_type_taxonomies ) )
     1816                                        return new IXR_Error( 401, __( 'Sorry, one of the given taxonomy is not supported by the post type.' ));
     1817
     1818                                $term_ids = $terms[ $taxonomy ];
     1819                                foreach ( $term_ids as $term_id) {
     1820                                        $term = get_term( $term_id, $taxonomy );
     1821
     1822                                        if ( is_wp_error( $term ) )
     1823                                                return new IXR_Error( 500, $term->get_error_message() );
     1824
     1825                                        if ( ! $term )
     1826                                                return new IXR_Error( 401, __( 'Invalid term ID' ) );
     1827                                }
     1828                        }
     1829
     1830                        foreach( $taxonomies as $taxonomy ) {
     1831                                $term_ids = $terms[ $taxonomy ];
     1832                                $term_ids = array_map( 'intval', $term_ids );
     1833                                $term_ids = array_unique( $term_ids );
     1834                                wp_set_object_terms( $post_ID , $term_ids, $taxonomy , $append);
     1835                        }
     1836                }
     1837
     1838                if( isset( $post_data['wp_post_format'] ) ) {
     1839                        set_post_format( $post_ID, $post_data['wp_post_format'] );
     1840                }
     1841
     1842                // Handle enclosures
     1843                $thisEnclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
     1844                $this->add_enclosure_if_new( $post_ID, $thisEnclosure );
     1845                $this->attach_uploads( $post_ID, $post_data['post_content'] );
     1846
     1847                $post_ID = wp_insert_post( $post_data, true );
     1848
     1849                if ( is_wp_error( $post_ID ) )
     1850                        return new IXR_Error( 500, $post_ID->get_error_message() );
     1851
     1852                if ( ! $post_ID )
     1853                        return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
     1854
     1855                return $post_ID;
     1856        }
     1857
    17141858        /* Blogger API functions.
    17151859         * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
    17161860         */