Changeset 2647

Show
Ignore:
Timestamp:
06/18/05 19:08:38 (4 years ago)
Author:
ryan
Message:

Consolidate post creation code into wp_insert_post().

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/admin-functions.php

    r2639 r2647  
    11<?php 
     2 
     3// Creates a new post from the "Write Post" form. 
     4function write_post() { 
     5    global $user_ID; 
     6 
     7    if ( !user_can_create_draft($user_ID) ) 
     8        die( __('You are not allowed to create posts or drafts on this blog.') ); 
     9 
     10    // Rename. 
     11    $_POST['post_content']  = $_POST['content']; 
     12    $_POST['post_excerpt']  = $_POST['excerpt']; 
     13    $_POST['post_parent'] = $_POST['parent_id']; 
     14 
     15    if (! empty($_POST['post_author_override'])) { 
     16        $_POST['$post_author'] = (int) $_POST['post_author_override']; 
     17    } else if (! empty($_POST['post_author'])) { 
     18        $_POST['post_author'] = (int) $_POST['post_author']; 
     19    } else { 
     20        $_POST['post_author'] = (int) $_POST['user_ID']; 
     21    } 
     22 
     23    if ( !user_can_edit_user($user_ID, $post_author) ) 
     24        die( __('You cannot post as this user.') ); 
     25     
     26    if ( 'publish' == $_POST['post_status'] && (!user_can_create_post($user_ID)) ) 
     27        $_POST['post_status'] = 'draft'; 
     28     
     29    // What to do based on which button they pressed 
     30    if ('' != $_POST['saveasdraft']) $_POST['post_status'] = 'draft'; 
     31    if ('' != $_POST['saveasprivate']) $_POST['post_status'] = 'private'; 
     32    if ('' != $_POST['publish']) $_POST['post_status'] = 'publish'; 
     33    if ('' != $_POST['advanced']) $_POST['post_status'] = 'draft'; 
     34    if ('' != $_POST['savepage']) $_POST['post_status'] = 'static'; 
     35         
     36    if (user_can_set_post_date($user_ID) && (!empty($_POST['edit_date']))) { 
     37        $aa = $_POST['aa']; 
     38        $mm = $_POST['mm']; 
     39        $jj = $_POST['jj']; 
     40        $hh = $_POST['hh']; 
     41        $mn = $_POST['mn']; 
     42        $ss = $_POST['ss']; 
     43        $jj = ($jj > 31) ? 31 : $jj; 
     44        $hh = ($hh > 23) ? $hh - 24 : $hh; 
     45        $mn = ($mn > 59) ? $mn - 60 : $mn; 
     46        $ss = ($ss > 59) ? $ss - 60 : $ss; 
     47        $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; 
     48        $_POST['post_date_gmt'] = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss"); 
     49    }  
     50 
     51    // Create the post. 
     52    $post_ID = wp_insert_post($_POST); 
     53    add_meta($post_ID); 
     54} 
    255 
    356function url_shorten ($url) { 
  • trunk/wp-admin/post.php

    r2639 r2647  
    3131case 'post': 
    3232 
    33     if ( !user_can_create_draft($user_ID) ) 
    34         die( __('You are not allowed to create posts or drafts on this blog.') ); 
    35  
    36     $post_pingback = (int) $_POST['post_pingback']; 
    37     $content         = apply_filters('content_save_pre',  $_POST['content']); 
    38     $excerpt         = apply_filters('excerpt_save_pre',  $_POST['excerpt']); 
    39     $post_title      = apply_filters('title_save_pre',    $_POST['post_title']); 
    40     $post_categories = apply_filters('category_save_pre', $_POST['post_category']); 
    41     $post_status     = apply_filters('status_save_pre',   $_POST['post_status']); 
    42     $post_name       = apply_filters('name_save_pre',     $_POST['post_name']); 
    43     $post_parent = 0; 
    44     $menu_order  = 0; 
    45      
    46  
    47     if ( isset($_POST['parent_id']) ) 
    48         $post_parent = (int) $_POST['parent_id']; 
    49  
    50     if ( isset($_POST['menu_order']) ) 
    51         $menu_order = (int) $_POST['menu_order']; 
    52  
    53     if (! empty($_POST['post_author_override'])) { 
    54         $post_author = (int) $_POST['post_author_override']; 
    55     } else if (! empty($_POST['post_author'])) { 
    56         $post_author = (int) $_POST['post_author']; 
    57     } else { 
    58         $post_author = (int) $_POST['user_ID']; 
    59     } 
    60     if ( !user_can_edit_user($user_ID, $post_author) ) 
    61         die( __('You cannot post as this user.') ); 
    62  
    63     if ( empty($post_status) ) 
    64         $post_status = 'draft'; 
    65     // Double-check 
    66     if ( 'publish' == $post_status && (!user_can_create_post($user_ID)) ) 
    67         $post_status = 'draft'; 
    68          
    69     $comment_status = $_POST['comment_status']; 
    70     if ( empty($comment_status) ) { 
    71         if ( !isset($_POST['advanced_view']) ) 
    72             $comment_status = get_option('default_comment_status'); 
    73         else 
    74             $comment_status = 'closed'; 
    75         } 
    76  
    77     $ping_status = $_POST['ping_status']; 
    78     if ( empty($ping_status) ) { 
    79         if ( !isset($_POST['advanced_view']) ) 
    80             $ping_status = get_option('default_ping_status');            
    81         else 
    82             $ping_status = 'closed'; 
    83         } 
    84  
    85     $post_password = $_POST['post_password']; 
    86      
    87     $trackback = $_POST['trackback_url']; 
    88     $trackback = preg_replace('|\s+|', "\n", $trackback); 
    89  
    90     if (user_can_set_post_date($user_ID) && (!empty($_POST['edit_date']))) { 
    91         $aa = $_POST['aa']; 
    92         $mm = $_POST['mm']; 
    93         $jj = $_POST['jj']; 
    94         $hh = $_POST['hh']; 
    95         $mn = $_POST['mn']; 
    96         $ss = $_POST['ss']; 
    97         $jj = ($jj > 31) ? 31 : $jj; 
    98         $hh = ($hh > 23) ? $hh - 24 : $hh; 
    99         $mn = ($mn > 59) ? $mn - 60 : $mn; 
    100         $ss = ($ss > 59) ? $ss - 60 : $ss; 
    101         $now = "$aa-$mm-$jj $hh:$mn:$ss"; 
    102         $now_gmt = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss"); 
    103     } else { 
    104         $now = current_time('mysql'); 
    105         $now_gmt = current_time('mysql', 1); 
    106     } 
    107  
    108     // What to do based on which button they pressed 
    109     if ('' != $_POST['saveasdraft']) $post_status = 'draft'; 
    110     if ('' != $_POST['saveasprivate']) $post_status = 'private'; 
    111     if ('' != $_POST['publish']) $post_status = 'publish'; 
    112     if ('' != $_POST['advanced']) $post_status = 'draft'; 
    113     if ('' != $_POST['savepage']) $post_status = 'static'; 
    114  
    115  
    116  
    117     $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->posts'"); 
    118     $post_ID = $id_result->Auto_increment; 
    119  
    120     if ( empty($post_name) ) { 
    121         if ( 'draft' != $post_status ) 
    122             $post_name = sanitize_title($post_title, $post_ID); 
    123     } else { 
    124         $post_name = sanitize_title($post_name, $post_ID); 
    125     } 
    126  
    127     if ('publish' == $post_status) { 
    128         $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"); 
    129         if ($post_name_check) { 
    130             $suffix = 2; 
    131             while ($post_name_check) { 
    132                 $alt_post_name = $post_name . "-$suffix"; 
    133                 $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"); 
    134                 $suffix++; 
    135             } 
    136             $post_name = $alt_post_name; 
    137         } 
    138     } 
    139  
    140     $postquery ="INSERT INTO $wpdb->posts 
    141             (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) 
    142             VALUES 
    143             ('$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') 
    144             "; 
    145  
    146     $result = $wpdb->query($postquery); 
    147  
     33    write_post(); 
     34 
     35    // Redirect. 
    14836    if (!empty($_POST['mode'])) { 
    14937    switch($_POST['mode']) { 
     
    16856        $location = "post.php?action=edit&post=$post_ID"; 
    16957 
    170     header("Location: $location"); // Send user on their way while we keep working 
    171  
    172     // Insert categories 
    173     // Check to make sure there is a category, if not just set it to some default 
    174     if (!$post_categories) $post_categories[] = get_option('default_category'); 
    175     foreach ($post_categories as $post_category) { 
    176         // Double check it's not there already 
    177         $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_ID AND category_id = $post_category"); 
    178  
    179          if (!$exists) {  
    180             $wpdb->query(" 
    181             INSERT INTO $wpdb->post2cat 
    182             (post_id, category_id) 
    183             VALUES 
    184             ($post_ID, $post_category) 
    185             "); 
    186         } 
    187     } 
    188  
    189     add_meta($post_ID); 
    190  
    191     $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); 
    192  
    193     do_action('save_post', $post_ID); 
    194  
    195     if ('publish' == $post_status) { 
    196         do_action('publish_post', $post_ID); 
    197         if ($post_pingback) 
    198             pingback($content, $post_ID); 
    199         do_enclose( $content, $post_ID ); 
    200         do_trackbacks($post_ID); 
    201          
    202     } 
    203  
    204     if ($post_status == 'static') { 
    205         generate_page_rewrite_rules(); 
    206         add_post_meta($post_ID, '_wp_page_template',  $_POST['page_template'], true); 
    207     } 
    208  
    209     require_once('admin-header.php'); 
    210  
     58    header("Location: $location"); 
    21159    exit(); 
    21260    break; 
  • trunk/wp-includes/functions-post.php

    r2633 r2647  
    77 */ 
    88function wp_insert_post($postarr = array()) { 
    9     global $wpdb, $post_default_category, $allowedtags
     9    global $wpdb, $post_default_category, $allowedtags, $user_ID
    1010     
    1111    // export array as variables 
    1212    extract($postarr); 
    13      
    14     // Do some escapes for safety 
    15     $post_title = $wpdb->escape($post_title); 
    16     $post_name = sanitize_title($post_title); 
    17     $post_excerpt = $wpdb->escape($post_excerpt); 
    18     $post_content = $wpdb->escape($post_content); 
    19     $post_author = (int) $post_author; 
    20  
     13 
     14    // Get the basics. 
     15    $post_content    = apply_filters('content_save_pre',  $post_content); 
     16    $post_excerpt    = apply_filters('excerpt_save_pre',  $post_excerpt); 
     17    $post_title      = apply_filters('title_save_pre',    $post_title); 
     18    $post_category   = apply_filters('category_save_pre', $post_category); 
     19    $post_status     = apply_filters('status_save_pre',   $post_status); 
     20    $post_name       = apply_filters('name_save_pre',     $post_name); 
     21     
    2122    // Make sure we set a valid category 
    2223    if (0 == count($post_category) || !is_array($post_category)) { 
    2324        $post_category = array($post_default_category); 
    2425    } 
    25  
    2626    $post_cat = $post_category[0]; 
     27 
     28    if ( empty($post_author) ) 
     29        $post_author = $user_ID; 
     30 
     31    if ( empty($post_status) ) 
     32        $post_status = 'draft'; 
     33     
     34    // Get the next post ID. 
     35    $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->posts'"); 
     36    $post_ID = $id_result->Auto_increment; 
     37 
     38    // Create a valid post name.  Drafts are allowed to have an empty 
     39    // post name. 
     40    if ( empty($post_name) ) { 
     41        if ( 'draft' != $post_status ) 
     42            $post_name = sanitize_title($post_title, $post_ID); 
     43    } else { 
     44        $post_name = sanitize_title($post_name, $post_ID); 
     45    } 
    2746     
    2847    if (empty($post_date)) 
    2948        $post_date = current_time('mysql'); 
    30     // Make sure we have a good gmt date: 
    3149    if (empty($post_date_gmt))  
    32         $post_date_gmt = get_gmt_from_date($post_date); 
     50        $post_date_gmt = current_time('mysql', 1); 
     51 
    3352    if (empty($comment_status)) 
    3453        $comment_status = get_settings('default_comment_status'); 
    3554    if (empty($ping_status)) 
    3655        $ping_status = get_settings('default_ping_status'); 
    37     if ( empty($post_parent) ) 
     56    if ( empty($post_pingback) ) 
     57        $post_pingback = get_option('default_pingback_flag'); 
     58 
     59    if ( isset($trackback_url) ) 
     60        $trackback_url = preg_replace('|\s+|', "\n", $trackback_url); 
     61    else 
     62        $trackback_url = ''; 
     63     
     64    if ( isset($post_parent) ) 
     65        $post_parent = (int) $post_parent; 
     66    else 
    3867        $post_parent = 0; 
     68 
     69    if ( isset($menu_order) ) 
     70        $menu_order = (int) $menu_order; 
     71    else 
     72        $menu_order = 0; 
     73 
     74    if ( !isset($post_password) ) 
     75        $post_password = ''; 
    3976 
    4077    if ('publish' == $post_status) { 
     
    5188    } 
    5289 
    53     $sql = "INSERT INTO $wpdb->posts  
    54         (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)  
    55         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')"; 
    56      
    57     $result = $wpdb->query($sql); 
     90    $postquery = "INSERT INTO $wpdb->posts 
     91            (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) 
     92            VALUES 
     93            ('$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') 
     94            "; 
     95     
     96    $result = $wpdb->query($postquery); 
    5897    $post_ID = $wpdb->insert_id; 
    5998 
     
    62101     
    63102    wp_set_post_cats('', $post_ID, $post_category); 
     103 
     104    $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); 
    64105     
    65106    if ($post_status == 'publish') { 
    66107        do_action('publish_post', $post_ID); 
    67     } 
    68  
    69     pingback($content, $post_ID); 
     108        if ($post_pingback) 
     109            pingback($post_content, $post_ID); 
     110        do_enclose( $post_content, $post_ID ); 
     111        do_trackbacks($post_ID); 
     112    }   else if ($post_status == 'static') { 
     113        if ( empty($page_template) ) 
     114            $page_template = 'Default Template'; 
     115        generate_page_rewrite_rules(); 
     116        add_post_meta($post_ID, '_wp_page_template',  $page_template, true); 
     117    } 
    70118 
    71119    // Return insert_id if we got a good result, otherwise return zero.