# This patch file was generated by NetBeans IDE
# Following Index: paths are relative to: /var/www/GSoC/wordtrunk/wp-includes
# This patch can be applied using context Tools: Patch action on respective folder.
# It uses platform neutral UTF-8 encoding and \n newlines.
# Above lines and this line are ignored by the patching process.
Index: class-wp-xmlrpc-server.php
--- class-wp-xmlrpc-server.php Base (BASE)
+++ class-wp-xmlrpc-server.php Locally Modified (Based On LOCAL)
@@ -64,6 +64,7 @@
 			'wp.getMediaItem'		=> 'this:wp_getMediaItem',
 			'wp.getMediaLibrary'	=> 'this:wp_getMediaLibrary',
 			'wp.getPostFormats'     => 'this:wp_getPostFormats',
+                        'wp.newPost'            => 'this:wp_newPost',
 
 			// Blogger API
 			'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
@@ -1702,6 +1703,452 @@
 		return $formats;
 	}
 
+        /**
+         * Create a new post
+         *
+         * @uses wp_insert_post()
+         * @param array $args Method parameters. Contains:
+         *  - int     $blog_id
+         *  - string  $username
+         *  - string  $password
+         *  - array     $content_struct.
+         *      The $content_struct must contain:
+         *      - 'post_type'
+         *      Also, it can optionally contain:
+         *      - 'post_status'
+         *      - 'wp_password'
+         *      - 'wp_slug
+         *      - 'wp_page_order'
+         *      - 'wp_page_parent_id'
+         *      - 'wp_page_template'
+         *      - 'wp_author_id'
+         *      - 'title'
+         *      - 'description'
+         *      - 'mt_excerpt'
+         *      - 'mt_allow_comments'
+         *      - 'mt_allow_pings'
+         *      - 'mt_text_more'
+         *      - 'mt_tb_ping_urls'
+         *      - 'date_created_gmt'
+         *      - 'dateCreated'
+         *      - 'sticky'
+         *      - 'custom_fields'
+         *      - 'terms'
+         *      - 'categories'
+         *      - 'mt_keywords'
+         *      - 'wp_post_format'
+         *  - boolean $publish optional. Defaults to true
+         * @return string post_id
+         */
+        function wp_newPost( $args ) {
+
+		$this->escape( $args );
+
+                $blog_ID        = (int) $args[0]; // for future use
+                $username       = $args[1];
+                $password       = $args[2];
+                $content_struct = $args[3];
+                $publish        = isset( $args[4] ) ? $args[4] : false;
+
+		if ( ! $user = $this->login( $username, $password ) )
+			return $this->error;
+                
+                $post_type = get_post_type_object( $content_struct['post_type'] );
+                if( ! ( (bool)$post_type ) )
+                        return new IXR_Error( 403, __( 'Invalid post type' ) );
+
+                if( ! current_user_can( $post_type->cap->edit_posts ) )
+                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to create posts in this post type' ));
+
+                // this holds all the post data needed
+                $post_data = array();
+                $post_data['post_type'] = $content_struct['post_type'];
+
+                $post_data['post_status'] = $publish ? 'publish' : 'draft';
+
+                if( isset ( $content_struct["{$content_struct['post_type']}_status"] ) )
+                        $post_data['post_status'] = $content_struct["{$post_data['post_type']}_status"];
+
+
+                switch ( $post_data['post_status'] ) {
+
+                        case 'draft':
+                                break;
+                        case 'pending':
+                                break;
+                        case 'private':
+                                if( ! current_user_can( $post_type->cap->publish_posts ) )
+                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ));
+                                break;
+                        case 'publish':
+                                if( ! current_user_can( $post_type->cap->publish_posts ) )
+                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ));
+                                break;
+                        default:
+                                return new IXR_Error( 401, __( 'Invalid post status' ) );
+                                break;
+                                
+                }
+
+                // Only use a password if one was given.
+                if ( isset( $content_struct['wp_password'] ) ) {
+
+                        if( ! current_user_can( $post_type->cap->publish_posts ) )
+                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
+                        $post_data['post_password'] = $content_struct['wp_password'];
+                        
+                }
+
+                // Let WordPress generate the post_name (slug) unless one has been provided.
+                $post_data['post_name'] = "";
+                if ( isset( $content_struct['wp_slug'] ) )
+                        $post_data['post_name'] = $content_struct['wp_slug'];
+
+                if ( isset( $content_struct['wp_page_order'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'page-attributes' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support page attributes.' ) );
+
+                        $post_data['menu_order'] = (int)$content_struct['wp_page_order'];
+                
+                }
+
+                if ( isset( $content_struct['wp_page_parent_id'] ) ) {
+
+                        if( ! $post_type->hierarchical )
+                                return new IXR_Error( 401, __( 'This post type does not support post hierarchy.' ) );
+
+                        // validating parent ID
+                        $parent_ID = (int)$content_struct['wp_page_parent_id'];
+                        if( $parent_ID != 0 ) {
+
+                                $parent_post = (array)wp_get_single_post( $parent_ID );
+                                if ( empty( $parent_post['ID'] ) )
+                                        return new IXR_Error( 401, __( 'Invalid parent ID.' ) );
+
+                                if ( $parent_post['post_type'] != $content_struct['post_type'] )
+                                        return new IXR_Error( 401, __( 'The parent post is of different post type.' ) );
+
+                        }
+
+                        $post_data['post_parent'] = $content_struct['wp_page_parent_id'];
+
+                }
+
+                // page template is only supported only by pages
+                if ( isset( $content_struct['wp_page_template'] ) ) {
+
+                        if( $content_struct['post_type'] != 'page'  )
+                                return new IXR_Error( 401, __( 'Page templates are only supported by pages.' ) );
+
+                        // validating page template
+                        $page_templates = get_page_templates( );
+                        $page_templates['Default'] = 'default';
+                        
+                        if( ! array_key_exists( $content_struct['wp_page_template'], $page_templates ) )
+                                return new IXR_Error( 403, __( 'Invalid page template.' ) );
+
+                        $post_data['page_template'] = $content_struct['wp_page_template'];
+
+                }
+
+                $post_data['post_author '] = $user->ID;
+
+                // If an author id was provided then use it instead.
+                if( isset( $content_struct['wp_author_id'] ) && ( $user->ID != (int)$content_struct['wp_author_id'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'author' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support to set author.' ) );
+
+                        if( ! current_user_can( $post_type->cap->edit_others_posts ) )
+                                return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
+                        
+                        $author_ID = (int)$content_struct['wp_author_id'];
+
+                        $author = get_userdata( $author_ID );
+                        if( ! $author )
+                                return new IXR_Error( 404, __( 'Invalid author ID.' ) );
+                        
+                        $post_data['post_author '] = $author_ID;
+                        
+                }
+
+                if( isset ( $content_struct['title'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'title' ) )
+                                return new IXR_Error( 401, __('This post type does not support title attribute.') );
+                        $post_data['post_title'] = $content_struct['title'];
+                        
+                }
+
+                if( isset ( $content_struct['description'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'editor' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support post content.' ) );
+                        $post_data['post_content'] = $content_struct['description'];
+                        
+                }
+
+                if( isset ( $content_struct['mt_excerpt'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'excerpt' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support post excerpt.' ) );
+                        $post_data['post_excerpt'] = $content_struct['mt_excerpt'];
+                        
+                }
+
+                if( post_type_supports( $content_struct['post_type'], 'comments' ) ) {
+
+                        $post_data['comment_status'] = get_option('default_comment_status');
+
+                        if( isset( $content_struct['mt_allow_comments'] ) ) {
+
+                                if( ! is_numeric( $content_struct['mt_allow_comments'] ) ) {
+
+                                        switch ( $content_struct['mt_allow_comments'] ) {
+                                                case 'closed':
+                                                        $post_data['comment_status']= 'closed';
+                                                        break;
+                                                case 'open':
+                                                        $post_data['comment_status'] = 'open';
+                                                        break;
+                                                default:
+                                                        return new IXR_Error( 401, __( 'Invalid comment option.' ) );
+                                        }
+
+                                } else {
+
+                                        switch ( (int) $content_struct['mt_allow_comments'] ) {
+                                                case 0: // for backward compatiblity
+                                                case 2:
+                                                        $post_data['comment_status'] = 'closed';
+                                                        break;
+                                                case 1:
+                                                        $post_data['comment_status'] = 'open';
+                                                        break;
+                                                default:
+                                                        return new IXR_Error( 401, __( 'Invalid comment option.' ) );
+                                        }
+
+                                }
+                                
+                        }
+
+                } else {
+
+                        if( isset( $content_struct['mt_allow_comments'] ) )
+                               return new IXR_Error( 401, __( 'This post type does not support comments.' ) );
+
+                }
+
+
+                if( post_type_supports( $content_struct['post_type'], 'trackbacks' ) ) {
+
+                        $post_data['ping_status'] = get_option('default_ping_status');
+
+                        if( isset( $content_struct['mt_allow_pings'] ) ) {
+
+                                if ( ! is_numeric( $content_struct['mt_allow_pings'] ) ) {
+
+                                        switch ( $content_struct['mt_allow_pings'] ) {
+                                                case 'closed':
+                                                        $post_data['ping_status']= 'closed';
+                                                        break;
+                                                case 'open':
+                                                        $post_data['ping_status'] = 'open';
+                                                        break;
+                                                default:
+                                                        return new IXR_Error( 401, __( 'Invalid ping option.' ) );
+                                        }
+
+                                } else {
+
+                                        switch ( (int) $content_struct['mt_allow_pings'] ) {
+                                                case 0:
+                                                case 2:
+                                                        $post_data['ping_status'] = 'closed';
+                                                        break;
+                                                case 1:
+                                                        $post_data['ping_status'] = 'open';
+                                                        break;
+                                                default:
+                                                        return new IXR_Error( 401, __( 'Invalid ping option.' ) );
+                                        }
+
+                                }
+
+                        }
+
+                } else {
+
+                        if( isset( $content_struct['mt_allow_pings'] ) )
+                               return new IXR_Error( 401, __( 'This post type does not support trackbacks.' ) );
+
+                }
+
+                $post_data['post_more'] = null;
+                if( isset( $content_struct['mt_text_more'] ) ) {
+
+                        $post_data['post_more'] = $content_struct['mt_text_more'];
+                        $post_data['post_content'] = $post_data['post_content'] . '<!--more-->' . $post_data['post_more'];
+                        
+                }
+
+                $post_data['to_ping'] = null;
+                if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
+
+                        $post_data['to_ping'] = $content_struct['mt_tb_ping_urls'];
+                        if ( is_array( $to_ping ) )
+                                $post_data['to_ping'] = implode(' ', $to_ping);
+                        
+                }
+
+                // Do some timestamp voodoo
+                if ( ! empty( $content_struct['date_created_gmt'] ) )
+                        $dateCreated = str_replace( 'Z', '', $content_struct['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
+                elseif ( !empty( $content_struct['dateCreated']) )
+                        $dateCreated = $content_struct['dateCreated']->getIso();
+
+                if ( ! empty( $dateCreated ) ) {
+                        $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
+                        $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
+                } else {
+                        $post_data['post_date'] = current_time('mysql');
+                        $post_data['post_date_gmt'] = current_time('mysql', 1);
+                }
+
+                // we got everything we need
+                $post_ID = wp_insert_post( $post_data, true );
+
+                if ( is_wp_error( $post_ID ) )
+                        return new IXR_Error( 500, $post_ID->get_error_message() );
+
+                if ( ! $post_ID )
+                        return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
+
+                // the default is to unstick
+                if( $content_struct['post_type'] == 'post' ) {
+
+                        $sticky = $content_struct['sticky'] ? true : false;
+                        if( $sticky ) {
+
+                                if( $post_data['post_status'] != 'publish' )
+                                        return new IXR_Error( 401, __( 'Only published posts can be made sticky.' ));
+
+                                if( ! current_user_can( $post_type->cap->edit_others_posts ) )
+                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
+
+                                stick_post( $post_ID );
+
+
+                        } else {
+
+                                unstick_post( $post_ID );
+
+                        }
+
+                } else {
+
+                        if( isset ( $content_struct['sticky'] ) )
+                                return new IXR_Error( 401, __( 'Sorry, only posts can be sticky.' ) );
+                        
+                }
+
+                if( isset ( $content_struct['custom_fields'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'], 'custom-fields' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support custom fields.' ) );
+                        $wp_xmlrpc_server->set_custom_fields( $post_ID, $content_struct['custom_fields'] );
+                        
+                }               
+
+                $post_type_taxonomies = get_object_taxonomies( $content_struct['post_type'] );
+
+                if( isset( $content_struct['terms'] ) ) {
+
+                        $terms = $content_struct['terms'];
+                        $taxonomies = array_keys( $terms );
+
+                        // validating term ids
+                        foreach( $taxonomies as $taxonomy ) {
+
+                                if( ! in_array( $taxonomy , $post_type_taxonomies ) )
+                                        return new IXR_Error( 401, __( 'Sorry, one of the given taxonomy is not supported by the post type.' ));
+
+                                $term_ids = $terms[ $taxonomy ];
+                                foreach ( $term_ids as $term_id) {
+
+                                        $term = get_term( $term_id, $taxonomy );
+
+                                        if ( is_wp_error( $term ) )
+                                                return new IXR_Error( 500, $term->get_error_message() );
+
+                                        if ( ! $term )
+                                                return new IXR_Error( 401, __( 'Invalid term ID' ) );
+
+                                }
+
+                        }
+
+                        foreach( $taxonomies as $taxonomy ) {
+
+                                $term_ids = $terms[ $taxonomy ];
+                                $term_ids = array_map( 'intval', $term_ids );
+                                $term_ids = array_unique( $term_ids );
+                                wp_set_object_terms( $post_ID , $term_ids, $taxonomy , $append);
+
+                        }
+
+                        return true;
+
+                }
+
+                // backward compatiblity
+		if ( isset( $content_struct['categories'] ) ) {
+
+                        if( ! in_array( 'category', $post_type_taxonomies ) )
+                                return new IXR_Error( 401, __( 'Sorry, Categories are not supported by the post type' ));
+                        
+			$category_names = $content_struct['categories'];
+
+                        foreach( $category_names as $category_name ) {
+                                $category_ID = get_cat_ID( $category_name );
+                                if( ! $category_ID )
+                                        return new IXR_Error( 401, __( 'Sorry, one of the given categories does not exist!' ));
+                                $post_categories[] = $category_ID;
+                        }
+
+                        wp_set_post_categories ($post_ID, $post_categories );
+
+		}
+
+                if( isset( $content_struct['mt_keywords'] ) ) {
+
+                        if( ! in_array( 'post_tag' , $post_type_taxonomies ) )
+                                return new IXR_Error( 401, __( 'Sorry, post tags are not supported by the post type' ));
+
+                        wp_set_post_terms( $post_id, $tags, 'post_tag', false); // append is set false here
+
+                }
+
+                if( isset( $content_struct['wp_post_format'] ) ) {
+
+                        if( ! in_array( 'post_format' , $post_type_taxonomies ) )
+                                return new IXR_Error( 401, __( 'Sorry, post formats are not supported by the post type' ));
+                        
+                        wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
+
+                }
+
+                // Handle enclosures
+                $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
+                $wp_xmlrpc_server->add_enclosure_if_new($post_ID, $thisEnclosure);
+                $wp_xmlrpc_server->attach_uploads( $post_ID, $post_data['post_content'] );
+
+                return strval( $post_ID );
+
+        }
+
 	/* Blogger API functions.
 	 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
 	 */

