function edit_post() {
	global $user_ID;

	$post_ID = (int) $_POST['post_ID'];

	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post_ID ) )
			wp_die( __('You are not allowed to edit this page.' ));
	} else {
		if ( !current_user_can( 'edit_post', $post_ID ) )
			wp_die( __('You are not allowed to edit this post.' ));
	}

	// Autosave shouldn't save too soon after a real save
	if ( 'autosave' == $_POST['action'] ) {
		$post =& get_post( $post_ID );
		$now = time();
		$then = strtotime($post->post_date_gmt . ' +0000');
		// Keep autosave_interval in sync with autosave-js.php.
		$delta = apply_filters( 'autosave_interval', 120 ) / 2;
		if ( ($now - $then) < $delta )
			return $post_ID;
	}

	// Rename.
	$_POST['ID'] = (int) $_POST['post_ID'];
	$_POST['post_content'] = $_POST['content'];
	$_POST['post_excerpt'] = $_POST['excerpt'];
	$_POST['post_parent'] = $_POST['parent_id'];
	$_POST['to_ping'] = $_POST['trackback_url'];

	if (!empty ( $_POST['post_author_override'] ) ) {
		$_POST['post_author'] = (int) $_POST['post_author_override'];
	} else
		if (!empty ( $_POST['post_author'] ) ) {
			$_POST['post_author'] = (int) $_POST['post_author'];
		} else {
			$_POST['post_author'] = (int) $_POST['user_ID'];
		}

	if ( $_POST['post_author'] != $_POST['user_ID'] ) {
		if ( 'page' == $_POST['post_type'] ) {
			if ( !current_user_can( 'edit_others_pages' ) )
				wp_die( __('You are not allowed to edit pages as this user.' ));
		} else {
			if ( !current_user_can( 'edit_others_posts' ) )
				wp_die( __('You are not allowed to edit posts as this user.' ));

		}
	}

	// What to do based on which button they pressed
	if ('' != $_POST['saveasdraft'] )
		$_POST['post_status'] = 'draft';
	if ('' != $_POST['saveasprivate'] )
		$_POST['post_status'] = 'private';
	if ('' != $_POST['publish'] )
		$_POST['post_status'] = 'publish';
	if ('' != $_POST['advanced'] )
		$_POST['post_status'] = 'draft';

	if ( 'page' == $_POST['post_type'] ) {
		if ('publish' == $_POST['post_status'] && !current_user_can( 'edit_published_pages' ))
			$_POST['post_status'] = 'draft';
	} else {
		if ('publish' == $_POST['post_status'] && !current_user_can( 'edit_published_posts' ))
			$_POST['post_status'] = 'draft';
	}

	if (!isset( $_POST['comment_status'] ))
		$_POST['comment_status'] = 'closed';

	if (!isset( $_POST['ping_status'] ))
		$_POST['ping_status'] = 'closed';

	if (!empty ( $_POST['edit_date'] ) ) {
		$aa = $_POST['aa'];
		$mm = $_POST['mm'];
		$jj = $_POST['jj'];
		$hh = $_POST['hh'];
		$mn = $_POST['mn'];
		$ss = $_POST['ss'];
		$jj = ($jj > 31 ) ? 31 : $jj;
		$hh = ($hh > 23 ) ? $hh -24 : $hh;
		$mn = ($mn > 59 ) ? $mn -60 : $mn;
		$ss = ($ss > 59 ) ? $ss -60 : $ss;
		$_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
		$_POST['post_date_gmt'] = get_gmt_from_date( "$aa-$mm-$jj $hh:$mn:$ss" );
	}

	// Meta Stuff
	if ( $_POST['meta'] ) {
		foreach ( $_POST['meta'] as $key => $value )
			update_meta( $key, $value['key'], $value['value'] );
	}

	if ( $_POST['deletemeta'] ) {
		foreach ( $_POST['deletemeta'] as $key => $value )
			delete_meta( $key );
	}

	add_meta( $post_ID );

	wp_update_post( $_POST );

	// Reunite any orphaned attachments with their parent
	if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
		$draft_ids = array();
	if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
		relocate_children( $draft_temp_id, $post_ID );

	// Now that we have an ID we can fix any attachment anchor hrefs
	fix_attachment_links( $post_ID );

	return $post_ID;
}
function write_post()
{
    global $user_ID;
    if (!current_user_can('edit_posts')) {
        die(__('You are not allowed to create posts or drafts on this blog.'));
    }
    // Rename.
    $_POST['post_content'] = $_POST['content'];
    $_POST['post_excerpt'] = $_POST['excerpt'];
    $_POST['post_parent'] = $_POST['parent_id'];
    $_POST['to_ping'] = $_POST['trackback_url'];
    if (!empty($_POST['post_author_override'])) {
        $_POST['post_author'] = (int) $_POST['post_author_override'];
    } else {
        if (!empty($_POST['post_author'])) {
            $_POST['post_author'] = (int) $_POST['post_author'];
        } else {
            $_POST['post_author'] = (int) $_POST['user_ID'];
        }
    }
    if ($_POST['post_author'] != $_POST['user_ID'] && !current_user_can('edit_others_posts')) {
        die(__('You cannot post as this user.'));
    }
    // What to do based on which button they pressed
    if ('' != $_POST['saveasdraft']) {
        $_POST['post_status'] = 'draft';
    }
    if ('' != $_POST['saveasprivate']) {
        $_POST['post_status'] = 'private';
    }
    if ('' != $_POST['publish']) {
        $_POST['post_status'] = 'publish';
    }
    if ('' != $_POST['advanced']) {
        $_POST['post_status'] = 'draft';
    }
    if ('' != $_POST['savepage']) {
        $_POST['post_status'] = 'static';
    }
    if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts')) {
        $_POST['post_status'] = 'draft';
    }
    if ('static' == $_POST['post_status'] && !current_user_can('edit_pages')) {
        die(__('This user cannot edit pages.'));
    }
    if (!isset($_POST['comment_status'])) {
        $_POST['comment_status'] = 'closed';
    }
    if (!isset($_POST['ping_status'])) {
        $_POST['ping_status'] = 'closed';
    }
    if (!empty($_POST['edit_date'])) {
        $aa = $_POST['aa'];
        $mm = $_POST['mm'];
        $jj = $_POST['jj'];
        $hh = $_POST['hh'];
        $mn = $_POST['mn'];
        $ss = $_POST['ss'];
        $jj = $jj > 31 ? 31 : $jj;
        $hh = $hh > 23 ? $hh - 24 : $hh;
        $mn = $mn > 59 ? $mn - 60 : $mn;
        $ss = $ss > 59 ? $ss - 60 : $ss;
        $_POST['post_date'] = "{$aa}-{$mm}-{$jj} {$hh}:{$mn}:{$ss}";
        $_POST['post_date_gmt'] = get_gmt_from_date("{$aa}-{$mm}-{$jj} {$hh}:{$mn}:{$ss}");
    }
    // Create the post.
    $post_ID = wp_insert_post($_POST);
    add_meta($post_ID);
    // Reunite any orphaned attachments with their parent
    if ($_POST['temp_ID']) {
        relocate_children($_POST['temp_ID'], $post_ID);
    }
    // Now that we have an ID we can fix any attachment anchor hrefs
    fix_attachment_links($post_ID);
    return $post_ID;
}
function wp_write_post()
{
    global $user_ID;
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_pages')) {
            return new WP_Error('edit_pages', __('You are not allowed to create pages on this blog.'));
        }
    } else {
        if (!current_user_can('edit_posts')) {
            return new WP_Error('edit_posts', __('You are not allowed to create posts or drafts on this blog.'));
        }
    }
    // Check for autosave collisions
    if (isset($_POST['temp_ID'])) {
        $temp_id = (int) $_POST['temp_ID'];
        if (!($draft_ids = get_user_option('autosave_draft_ids'))) {
            $draft_ids = array();
        }
        foreach ($draft_ids as $temp => $real) {
            if (time() + $temp > 86400) {
                // 1 day: $temp is equal to -1 * time( then )
                unset($draft_ids[$temp]);
            }
        }
        if (isset($draft_ids[$temp_id])) {
            // Edit, don't write
            $_POST['post_ID'] = $draft_ids[$temp_id];
            unset($_POST['temp_ID']);
            relocate_children($temp_id, $_POST['post_ID']);
            update_user_option($user_ID, 'autosave_draft_ids', $draft_ids);
            return edit_post();
        }
    }
    // Rename.
    $_POST['post_content'] = $_POST['content'];
    $_POST['post_excerpt'] = $_POST['excerpt'];
    $_POST['post_parent'] = $_POST['parent_id'];
    $_POST['to_ping'] = $_POST['trackback_url'];
    if (!empty($_POST['post_author_override'])) {
        $_POST['post_author'] = (int) $_POST['post_author_override'];
    } else {
        if (!empty($_POST['post_author'])) {
            $_POST['post_author'] = (int) $_POST['post_author'];
        } else {
            $_POST['post_author'] = (int) $_POST['user_ID'];
        }
    }
    if ($_POST['post_author'] != $_POST['user_ID']) {
        if ('page' == $_POST['post_type']) {
            if (!current_user_can('edit_others_pages')) {
                return new WP_Error('edit_others_pages', __('You are not allowed to create pages as this user.'));
            }
        } else {
            if (!current_user_can('edit_others_posts')) {
                return new WP_Error('edit_others_posts', __('You are not allowed to post as this user.'));
            }
        }
    }
    // What to do based on which button they pressed
    if ('' != $_POST['saveasdraft']) {
        $_POST['post_status'] = 'draft';
    }
    if ('' != $_POST['saveasprivate']) {
        $_POST['post_status'] = 'private';
    }
    if ('' != $_POST['publish']) {
        $_POST['post_status'] = 'publish';
    }
    if ('' != $_POST['advanced']) {
        $_POST['post_status'] = 'draft';
    }
    if ('page' == $_POST['post_type']) {
        if ('publish' == $_POST['post_status'] && !current_user_can('publish_pages')) {
            $_POST['post_status'] = 'draft';
        }
    } else {
        if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts')) {
            $_POST['post_status'] = 'draft';
        }
    }
    if (!isset($_POST['comment_status'])) {
        $_POST['comment_status'] = 'closed';
    }
    if (!isset($_POST['ping_status'])) {
        $_POST['ping_status'] = 'closed';
    }
    if (!empty($_POST['edit_date'])) {
        $aa = $_POST['aa'];
        $mm = $_POST['mm'];
        $jj = $_POST['jj'];
        $hh = $_POST['hh'];
        $mn = $_POST['mn'];
        $ss = $_POST['ss'];
        $jj = $jj > 31 ? 31 : $jj;
        $hh = $hh > 23 ? $hh - 24 : $hh;
        $mn = $mn > 59 ? $mn - 60 : $mn;
        $ss = $ss > 59 ? $ss - 60 : $ss;
        $_POST['post_date'] = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss);
        $_POST['post_date_gmt'] = get_gmt_from_date($_POST['post_date']);
    }
    // Create the post.
    $post_ID = wp_insert_post($_POST);
    add_meta($post_ID);
    // Reunite any orphaned attachments with their parent
    // Update autosave collision detection
    if ($temp_id) {
        relocate_children($temp_id, $post_ID);
        $draft_ids[$temp_id] = $post_ID;
        update_user_option($user_ID, 'autosave_draft_ids', $draft_ids);
    }
    // Now that we have an ID we can fix any attachment anchor hrefs
    fix_attachment_links($post_ID);
    return $post_ID;
}