From 5cfc1a248b2fbc4e93c32a7338ff87242f231500 Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 18 Jun 2005 19:08:38 +0000 Subject: [PATCH] Consolidate post creation code into wp_insert_post(). git-svn-id: http://svn.automattic.com/wordpress/trunk@2647 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-functions.php | 53 +++++++++++ wp-admin/post.php | 158 +-------------------------------- wp-includes/functions-post.php | 84 ++++++++++++++---- 3 files changed, 122 insertions(+), 173 deletions(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 9f29ff41e5..0b210fe0d6 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -1,5 +1,58 @@ 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); +} + 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 f929dbc2d9..0e4edcce5f 100644 --- a/wp-admin/post.php +++ b/wp-admin/post.php @@ -30,121 +30,9 @@ $editing = true; switch($action) { case 'post': - if ( !user_can_create_draft($user_ID) ) - die( __('You are not allowed to create posts or drafts on this blog.') ); - - $post_pingback = (int) $_POST['post_pingback']; - $content = apply_filters('content_save_pre', $_POST['content']); - $excerpt = apply_filters('excerpt_save_pre', $_POST['excerpt']); - $post_title = apply_filters('title_save_pre', $_POST['post_title']); - $post_categories = apply_filters('category_save_pre', $_POST['post_category']); - $post_status = apply_filters('status_save_pre', $_POST['post_status']); - $post_name = apply_filters('name_save_pre', $_POST['post_name']); - $post_parent = 0; - $menu_order = 0; - - - if ( isset($_POST['parent_id']) ) - $post_parent = (int) $_POST['parent_id']; - - if ( isset($_POST['menu_order']) ) - $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.') ); - - if ( empty($post_status) ) - $post_status = 'draft'; - // Double-check - if ( 'publish' == $post_status && (!user_can_create_post($user_ID)) ) - $post_status = 'draft'; - - $comment_status = $_POST['comment_status']; - if ( empty($comment_status) ) { - if ( !isset($_POST['advanced_view']) ) - $comment_status = get_option('default_comment_status'); - else - $comment_status = 'closed'; - } - - $ping_status = $_POST['ping_status']; - if ( empty($ping_status) ) { - if ( !isset($_POST['advanced_view']) ) - $ping_status = get_option('default_ping_status'); - else - $ping_status = 'closed'; - } - - $post_password = $_POST['post_password']; - - $trackback = $_POST['trackback_url']; - $trackback = preg_replace('|\s+|', "\n", $trackback); - - 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; - $now = "$aa-$mm-$jj $hh:$mn:$ss"; - $now_gmt = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss"); - } else { - $now = current_time('mysql'); - $now_gmt = current_time('mysql', 1); - } - - // What to do based on which button they pressed - if ('' != $_POST['saveasdraft']) $post_status = 'draft'; - if ('' != $_POST['saveasprivate']) $post_status = 'private'; - if ('' != $_POST['publish']) $post_status = 'publish'; - if ('' != $_POST['advanced']) $post_status = 'draft'; - if ('' != $_POST['savepage']) $post_status = 'static'; - - - - $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->posts'"); - $post_ID = $id_result->Auto_increment; - - 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; - } - } - - $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', '$now', '$now_gmt', '$content', '$post_title', '$excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$trackback', '$now', '$now_gmt', '$post_parent', '$menu_order') - "; - - $result = $wpdb->query($postquery); + write_post(); + // Redirect. if (!empty($_POST['mode'])) { switch($_POST['mode']) { case 'bookmarklet': @@ -167,47 +55,7 @@ case 'post': if ( '' != $_POST['advanced'] || isset($_POST['save']) ) $location = "post.php?action=edit&post=$post_ID"; - header("Location: $location"); // Send user on their way while we keep working - - // Insert categories - // Check to make sure there is a category, if not just set it to some default - if (!$post_categories) $post_categories[] = get_option('default_category'); - foreach ($post_categories as $post_category) { - // Double check it's not there already - $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_ID AND category_id = $post_category"); - - if (!$exists) { - $wpdb->query(" - INSERT INTO $wpdb->post2cat - (post_id, category_id) - VALUES - ($post_ID, $post_category) - "); - } - } - - add_meta($post_ID); - - $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); - - do_action('save_post', $post_ID); - - if ('publish' == $post_status) { - do_action('publish_post', $post_ID); - if ($post_pingback) - pingback($content, $post_ID); - do_enclose( $content, $post_ID ); - do_trackbacks($post_ID); - - } - - if ($post_status == 'static') { - generate_page_rewrite_rules(); - add_post_meta($post_ID, '_wp_page_template', $_POST['page_template'], true); - } - - require_once('admin-header.php'); - + header("Location: $location"); exit(); break; diff --git a/wp-includes/functions-post.php b/wp-includes/functions-post.php index 4b057d3bfb..710c11f4e2 100644 --- a/wp-includes/functions-post.php +++ b/wp-includes/functions-post.php @@ -6,37 +6,74 @@ * generic function for inserting data into the posts table. */ function wp_insert_post($postarr = array()) { - global $wpdb, $post_default_category, $allowedtags; + global $wpdb, $post_default_category, $allowedtags, $user_ID; // export array as variables extract($postarr); - - // Do some escapes for safety - $post_title = $wpdb->escape($post_title); - $post_name = sanitize_title($post_title); - $post_excerpt = $wpdb->escape($post_excerpt); - $post_content = $wpdb->escape($post_content); - $post_author = (int) $post_author; + // Get the basics. + $post_content = apply_filters('content_save_pre', $post_content); + $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt); + $post_title = apply_filters('title_save_pre', $post_title); + $post_category = apply_filters('category_save_pre', $post_category); + $post_status = apply_filters('status_save_pre', $post_status); + $post_name = apply_filters('name_save_pre', $post_name); + // Make sure we set a valid category if (0 == count($post_category) || !is_array($post_category)) { $post_category = array($post_default_category); } - $post_cat = $post_category[0]; + + if ( empty($post_author) ) + $post_author = $user_ID; + + 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; + + // Create a valid post name. Drafts are allowed to have an empty + // post name. + 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 (empty($post_date)) $post_date = current_time('mysql'); - // Make sure we have a good gmt date: if (empty($post_date_gmt)) - $post_date_gmt = get_gmt_from_date($post_date); + $post_date_gmt = current_time('mysql', 1); + if (empty($comment_status)) $comment_status = get_settings('default_comment_status'); if (empty($ping_status)) $ping_status = get_settings('default_ping_status'); - if ( empty($post_parent) ) + if ( empty($post_pingback) ) + $post_pingback = get_option('default_pingback_flag'); + + if ( isset($trackback_url) ) + $trackback_url = preg_replace('|\s+|', "\n", $trackback_url); + else + $trackback_url = ''; + + if ( isset($post_parent) ) + $post_parent = (int) $post_parent; + else $post_parent = 0; + if ( isset($menu_order) ) + $menu_order = (int) $menu_order; + else + $menu_order = 0; + + if ( !isset($post_password) ) + $post_password = ''; + 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) { @@ -50,24 +87,35 @@ function wp_insert_post($postarr = array()) { } } - $sql = "INSERT INTO $wpdb->posts - (post_author, post_date, post_date_gmt, post_modified, post_modified_gmt, post_content, post_title, post_excerpt, post_category, post_status, post_name, comment_status, ping_status, post_parent) - VALUES ('$post_author', '$post_date', '$post_date_gmt', '$post_date', '$post_date_gmt', '$post_content', '$post_title', '$post_excerpt', '$post_cat', '$post_status', '$post_name', '$comment_status', '$ping_status', '$post_parent')"; + $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') + "; - $result = $wpdb->query($sql); + $result = $wpdb->query($postquery); $post_ID = $wpdb->insert_id; // Set GUID $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 ($post_status == 'publish') { do_action('publish_post', $post_ID); + if ($post_pingback) + pingback($post_content, $post_ID); + do_enclose( $post_content, $post_ID ); + do_trackbacks($post_ID); + } else if ($post_status == 'static') { + if ( empty($page_template) ) + $page_template = 'Default Template'; + generate_page_rewrite_rules(); + add_post_meta($post_ID, '_wp_page_template', $page_template, true); } - pingback($content, $post_ID); - // Return insert_id if we got a good result, otherwise return zero. return $result ? $post_ID : 0; }