From 67846fb123e4227fffd950e9d6e4db9d16f58036 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Sun, 19 Jun 2005 01:33:38 +0000 Subject: [PATCH] Consolidate post update code. git-svn-id: https://develop.svn.wordpress.org/trunk@2650 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/admin-functions.php | 65 ++++++++++++- wp-admin/post.php | 164 +-------------------------------- wp-includes/functions-post.php | 138 +++++++++++++++------------ 3 files changed, 146 insertions(+), 221 deletions(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 0b210fe0d6..8e55f4b29e 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -11,6 +11,7 @@ function write_post() { $_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']; @@ -20,7 +21,7 @@ function write_post() { $_POST['post_author'] = (int) $_POST['user_ID']; } - if ( !user_can_edit_user($user_ID, $post_author) ) + if ( !user_can_edit_user($user_ID, $_POST['post_author']) ) die( __('You cannot post as this user.') ); if ( 'publish' == $_POST['post_status'] && (!user_can_create_post($user_ID)) ) @@ -53,6 +54,68 @@ function write_post() { add_meta($post_ID); } +function edit_post() { + global $user_ID; + + if ( !isset($blog_ID) ) + $blog_ID = 1; + + $post_ID = $_POST['post_ID']; + + if (!user_can_edit_post($user_ID, $post_ID, $blog_ID)) + die( __('You are not allowed to edit this post.') ); + + // Rename. + $_POST['ID'] = $_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 ( !user_can_edit_user($user_ID, $_POST['post_author']) ) + die( __('You cannot post as this user.') ); + + if (user_can_set_post_date($user_ID) && (!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"); + } + + wp_update_post($_POST); + + // Meta Stuff + if ($_POST['meta']) : + foreach ($_POST['meta'] as $key => $value) : + update_meta($key, $value['key'], $value['value']); + endforeach; + endif; + + if ($_POST['deletemeta']) : + foreach ($_POST['deletemeta'] as $key => $value) : + delete_meta($key); + endforeach; + endif; + + add_meta($post_ID); +} + function url_shorten ($url) { $short_url = str_replace('http://', '', stripslashes($url)); $short_url = str_replace('www.', '', $short_url); diff --git a/wp-admin/post.php b/wp-admin/post.php index 0e4edcce5f..93b6f431bb 100644 --- a/wp-admin/post.php +++ b/wp-admin/post.php @@ -117,117 +117,7 @@ case 'edit': break; case 'editpost': - // die(var_dump('
', $_POST));
-	if (!isset($blog_ID)) {
-		$blog_ID = 1;
-	}
-	$post_ID = $_POST['post_ID'];
-
-	if (!user_can_edit_post($user_ID, $post_ID, $blog_ID))
-		die( __('You are not allowed to edit this post.') );
-
-	$post_categories = $_POST['post_category'];
-	if (!$post_categories) $post_categories[] = 1;
-	$content = apply_filters('content_save_pre', $_POST['content']);
-	$excerpt = apply_filters('excerpt_save_pre', $_POST['excerpt']);
-	$post_title = $_POST['post_title'];
-	$prev_status = $_POST['prev_status'];
-	$post_status = $_POST['post_status'];
-	$menu_order = (int) $_POST['menu_order'];
-	if (! empty($_POST['post_author_override'])) {
-		$post_author = (int) $_POST['post_author_override'];
-	} else if (! empty($_POST['post_author'])) {
-		$post_author = (int) $_POST['post_author'];
-	} else {
-		$post_author = (int) $_POST['user_ID'];
-	}
-	if ( !user_can_edit_user($user_ID, $post_author) )
-		die( __('You cannot post as this user.') );
-
-	$comment_status = $_POST['comment_status'];
-	if (empty($comment_status)) $comment_status = 'closed';
-	//if (!$_POST['comment_status']) $comment_status = get_settings('default_comment_status');
-
-	$ping_status = $_POST['ping_status'];
-	if (empty($ping_status)) $ping_status = 'closed';
-	//if (!$_POST['ping_status']) $ping_status = get_settings('default_ping_status');
-	$post_password = $_POST['post_password'];
-	$post_name = $_POST['post_name'];
-
-	$post_parent = 0;
-	if (isset($_POST['parent_id'])) {
-		$post_parent = $_POST['parent_id'];
-	}
-
-	$trackback = $_POST['trackback_url'];
-	// Format trackbacks
-	$trackback = preg_replace('|\s+|', '\n', $trackback);
-	
-	if (isset($_POST['publish'])) $post_status = 'publish';
-	// Double-check
-	if ( 'publish' == $post_status && (!user_can_create_post($user_ID)) )
-		$post_status = 'draft';
-
-	if ( empty($post_name) ) {
-		if ( 'draft' != $post_status )
-			$post_name = sanitize_title($post_title, $post_ID);
-	} else {
-		$post_name = sanitize_title($post_name, $post_ID);
-	}
-
-	if ('publish' == $post_status) {
-		$post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1");
-		if ($post_name_check) {
-			$suffix = 2;
-			while ($post_name_check) {
-				$alt_post_name = $post_name . "-$suffix";
-				$post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1");
-				$suffix++;
-			}
-			$post_name = $alt_post_name;
-		}
-	}
-
-	if (user_can_edit_post_date($user_ID, $post_ID) && (!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;
-		$datemodif = ", post_date = '$aa-$mm-$jj $hh:$mn:$ss'";
-	$datemodif_gmt = ", post_date_gmt = '".get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss")."'";
-	} else {
-		$datemodif = '';
-		$datemodif_gmt = '';
-	}
-
-	$now = current_time('mysql');
-	$now_gmt = current_time('mysql', 1);
-
-	$result = $wpdb->query("
-		UPDATE $wpdb->posts SET
-			post_content = '$content',
-			post_excerpt = '$excerpt',
-			post_title = '$post_title'"
-			.$datemodif_gmt
-			.$datemodif.",			
-			post_status = '$post_status',
-			comment_status = '$comment_status',
-			ping_status = '$ping_status',
-			post_author = '$post_author',
-			post_password = '$post_password',
-			post_name = '$post_name',
-			to_ping = '$trackback',
-			post_modified = '$now',
-			post_modified_gmt = '$now_gmt',
-			menu_order = '$menu_order',
-			post_parent = '$post_parent'
-		WHERE ID = $post_ID ");
+	edit_post();
 
 	if ($_POST['save']) {
 		$location = $_SERVER['HTTP_REFERER'];
@@ -244,58 +134,6 @@ case 'editpost':
 	}
 	header ('Location: ' . $location); // Send user on their way while we keep working
 
-	// Meta Stuff
-	if ($_POST['meta']) :
-		foreach ($_POST['meta'] as $key => $value) :
-			update_meta($key, $value['key'], $value['value']);
-		endforeach;
-	endif;
-
-	if ($_POST['deletemeta']) :
-		foreach ($_POST['deletemeta'] as $key => $value) :
-			delete_meta($key);
-		endforeach;
-	endif;
-
-	add_meta($post_ID);
-
-	// Now it's category time!
-	// First the old categories
-	$old_categories = $wpdb->get_col("SELECT category_id FROM $wpdb->post2cat WHERE post_id = $post_ID");
-	
-	// Delete any?
-	foreach ($old_categories as $old_cat) {
-		if (!in_array($old_cat, $post_categories)) // If a category was there before but isn't now
-			$wpdb->query("DELETE FROM $wpdb->post2cat WHERE category_id = $old_cat AND post_id = $post_ID LIMIT 1");
-	}
-	
-	// Add any?
-	foreach ($post_categories as $new_cat) {
-		if (!in_array($new_cat, $old_categories))
-			$wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ($post_ID, $new_cat)");
-	}
-
-	if ($prev_status != 'publish' && $post_status == 'publish')
-		do_action('private_to_published', $post_ID);
-
-	do_action('edit_post', $post_ID);
-
-	if ($post_status == 'publish') {
-		do_action('publish_post', $post_ID);
-		do_trackbacks($post_ID);
-		do_enclose( $content, $post_ID );
-		if ( get_option('default_pingback_flag') )
-			pingback($content, $post_ID);
-	}
-
-	if ($post_status == 'static') {
-		generate_page_rewrite_rules();
-
-		if ( ! update_post_meta($post_ID, '_wp_page_template',  $_POST['page_template'])) {
-			add_post_meta($post_ID, '_wp_page_template',  $_POST['page_template'], true);
-		}
-	}
-
 	exit();
 	break;
 
diff --git a/wp-includes/functions-post.php b/wp-includes/functions-post.php
index c7988d3408..99be3ac2b5 100644
--- a/wp-includes/functions-post.php
+++ b/wp-includes/functions-post.php
@@ -7,10 +7,18 @@
  */
 function wp_insert_post($postarr = array()) {
 	global $wpdb, $allowedtags, $user_ID;
-	
+
 	// export array as variables
 	extract($postarr);
 
+	// Are we updating or creating?
+	$update = false;
+	if ( !empty($ID) ) {
+		$update = true;
+		$post = & get_post($ID);
+		$previous_status = $post->post_status;
+	}
+
 	// Get the basics.
 	$post_content    = apply_filters('content_save_pre',  $post_content);
 	$post_excerpt    = apply_filters('excerpt_save_pre',  $post_excerpt);
@@ -31,9 +39,13 @@ function wp_insert_post($postarr = array()) {
 	if ( empty($post_status) )
 		$post_status = 'draft';
 	
-	// Get the next post ID.
-	$id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->posts'");
-	$post_ID = $id_result->Auto_increment;
+	// Get the post ID.
+	if ( $update ) {
+		$post_ID = $ID;
+	} else {
+		$id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->posts'");
+		$post_ID = $id_result->Auto_increment;
+	}
 
 	// Create a valid post name.  Drafts are allowed to have an empty
 	// post name.
@@ -56,10 +68,10 @@ function wp_insert_post($postarr = array()) {
 	if ( empty($post_pingback) )
 		$post_pingback = get_option('default_pingback_flag');
 
-	if ( isset($trackback_url) )
-		$trackback_url = preg_replace('|\s+|', "\n", $trackback_url);
+	if ( isset($to_ping) )
+		$to_ping = preg_replace('|\s+|', "\n", $to_ping);
 	else
-		$trackback_url = '';
+		$to_ping = '';
 	
 	if ( isset($post_parent) )
 		$post_parent = (int) $post_parent;
@@ -87,22 +99,53 @@ function wp_insert_post($postarr = array()) {
 		}
 	}
 
-	$postquery = "INSERT INTO $wpdb->posts
+	if ($update) {
+		$postquery =
+			"UPDATE $wpdb->posts SET
+			post_author = '$post_author',
+			post_date = '$post_date',
+			post_date_gmt = '$post_date_gmt',
+			post_content = '$post_content',
+			post_title = '$post_title',
+			post_excerpt = '$post_excerpt',
+			post_status = '$post_status',
+			comment_status = '$comment_status',
+			ping_status = '$ping_status',
+			post_password = '$post_password',
+			post_name = '$post_name',
+			to_ping = '$to_ping',
+			post_modified = '$post_date',
+			post_modified_gmt = '$post_date_gmt',
+			post_parent = '$post_parent',
+			menu_order = '$menu_order'
+			WHERE ID = $post_ID";
+	} else {
+		$postquery =
+			"INSERT INTO $wpdb->posts
 			(ID, post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt,  post_status, comment_status, ping_status, post_password, post_name, to_ping, post_modified, post_modified_gmt, post_parent, menu_order)
 			VALUES
-			('$post_ID', '$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$trackback_url', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order')
-			";
+			('$post_ID', '$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order')";
+	}
 	
 	$result = $wpdb->query($postquery);
-	$post_ID = $wpdb->insert_id;
+	if ( $update )
+		$rval = $wpdb->rows_affected;
+	else 
+		$rval = $wpdb->insert_id;
 
 	// Set GUID
-	$wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
+	if ( ! $update )
+		$wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
 	
 	wp_set_post_cats('', $post_ID, $post_category);
 
-	$wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
-	
+	if ( $update) {
+		if ($previous_status != 'publish' && $post_status == 'publish')
+			do_action('private_to_published', $post_ID);
+		
+		do_action('edit_post', $post_ID);
+	}
+
 	if ($post_status == 'publish') {
 		do_action('publish_post', $post_ID);
 		if ($post_pingback)
@@ -110,10 +153,13 @@ function wp_insert_post($postarr = array()) {
 		do_enclose( $post_content, $post_ID );
 		do_trackbacks($post_ID);
 	}	else if ($post_status == 'static') {
+		generate_page_rewrite_rules();
+
 		if ( empty($page_template) )
 			$page_template = 'Default Template';
-		generate_page_rewrite_rules();
-		add_post_meta($post_ID, '_wp_page_template',  $page_template, true);
+
+		if ( ! update_post_meta($post_ID, '_wp_page_template',  $page_template))
+			add_post_meta($post_ID, '_wp_page_template',  $page_template, true);
 	}
 
 	// Return insert_id if we got a good result, otherwise return zero.
@@ -123,18 +169,17 @@ function wp_insert_post($postarr = array()) {
 function wp_get_single_post($postid = 0, $mode = OBJECT) {
 	global $wpdb;
 
-	$sql = "SELECT * FROM $wpdb->posts WHERE ID=$postid";
-	$result = $wpdb->get_row($sql, $mode);
+	$post = get_post($postid, $mode);
 	
 	// Set categories
 	if($mode == OBJECT) {
-		$result->post_category = wp_get_post_cats('',$postid);
+		$post->post_category = wp_get_post_cats('',$postid);
 	} 
 	else {
-		$result['post_category'] = wp_get_post_cats('',$postid);
+		$post['post_category'] = wp_get_post_cats('',$postid);
 	}
 
-	return $result;
+	return $post;
 }
 
 function wp_get_recent_posts($num = 10) {
@@ -154,46 +199,25 @@ function wp_get_recent_posts($num = 10) {
 function wp_update_post($postarr = array()) {
 	global $wpdb;
 
-	// First get all of the original fields
-	extract(wp_get_single_post($postarr['ID'], ARRAY_A));	
+	// First, get all of the original fields
+	$post = wp_get_single_post($postarr['ID'], ARRAY_A);	
 
-	// Now overwrite any changed values being passed in
-	extract($postarr);
+	// Escape data pulled from DB.
+	foreach ($post as $key => $value)
+		$post[$key] = $wpdb->escape($value);
 
-	// Make sure we set a valid category
-	if ( 0 == count($post_category) || !is_array($post_category) )
-		$post_category = array($post_default_category);
+	// Passed post category list takes overwrites existing
+	// category list.
+ 	if ( isset($postarr['post_category']) )
+ 		$post_cats = $postarr['post_category'];
+ 	else 
+ 		$post_cats = $post['post_category'];
 
-	// Do some escapes for safety
-	$post_title   = $wpdb->escape($post_title);
-	$post_excerpt = $wpdb->escape($post_excerpt);
-	$post_content = $wpdb->escape($post_content);
+	// Merge old and new fields with new fields overwriting old ones.
+	$postarr = array_merge($post, $postarr);
+	$postarr['post_category'] = $post_cats;	
 
-	$post_modified = current_time('mysql');
-	$post_modified_gmt = current_time('mysql', 1);
-
-	$sql = "UPDATE $wpdb->posts 
-		SET post_content = '$post_content',
-		post_title = '$post_title',
-		post_category = $post_category[0],
-		post_status = '$post_status',
-		post_date = '$post_date',
-		post_date_gmt = '$post_date_gmt',
-		post_modified = '$post_modified',
-		post_modified_gmt = '$post_modified_gmt',
-		post_excerpt = '$post_excerpt',
-		ping_status = '$ping_status',
-		comment_status = '$comment_status'
-		WHERE ID = $ID";
-		
-	$result = $wpdb->query($sql);
-	$rows_affected = $wpdb->rows_affected;
-
-	wp_set_post_cats('', $ID, $post_category);
-
-	do_action('edit_post', $ID);
-
-	return $rows_affected;
+	return wp_insert_post($postarr);
 }
 
 function wp_get_post_cats($blogid = '1', $post_ID = 0) {