Changeset 2650

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

Consolidate post update code.

Files:

Legend:

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

    r2647 r2650  
    1212    $_POST['post_excerpt']  = $_POST['excerpt']; 
    1313    $_POST['post_parent'] = $_POST['parent_id']; 
     14    $_POST['to_ping'] = $_POST['trackback_url']; 
    1415 
    1516    if (! empty($_POST['post_author_override'])) { 
     
    2122    } 
    2223 
    23     if ( !user_can_edit_user($user_ID, $post_author) ) 
     24    if ( !user_can_edit_user($user_ID, $_POST['post_author']) ) 
    2425        die( __('You cannot post as this user.') ); 
    2526     
     
    5152    // Create the post. 
    5253    $post_ID = wp_insert_post($_POST); 
     54    add_meta($post_ID); 
     55} 
     56 
     57function edit_post() { 
     58    global $user_ID; 
     59 
     60    if ( !isset($blog_ID) )  
     61        $blog_ID = 1; 
     62 
     63    $post_ID = $_POST['post_ID']; 
     64 
     65    if (!user_can_edit_post($user_ID, $post_ID, $blog_ID)) 
     66        die( __('You are not allowed to edit this post.') ); 
     67 
     68    // Rename. 
     69    $_POST['ID'] = $_POST['post_ID']; 
     70    $_POST['post_content']  = $_POST['content']; 
     71    $_POST['post_excerpt']  = $_POST['excerpt']; 
     72    $_POST['post_parent'] = $_POST['parent_id']; 
     73    $_POST['to_ping'] = $_POST['trackback_url']; 
     74 
     75    if (! empty($_POST['post_author_override'])) { 
     76        $_POST['$post_author'] = (int) $_POST['post_author_override']; 
     77    } else if (! empty($_POST['post_author'])) { 
     78        $_POST['post_author'] = (int) $_POST['post_author']; 
     79    } else { 
     80        $_POST['post_author'] = (int) $_POST['user_ID']; 
     81    } 
     82 
     83    if ( !user_can_edit_user($user_ID, $_POST['post_author']) ) 
     84        die( __('You cannot post as this user.') ); 
     85 
     86    if (user_can_set_post_date($user_ID) && (!empty($_POST['edit_date']))) { 
     87        $aa = $_POST['aa']; 
     88        $mm = $_POST['mm']; 
     89        $jj = $_POST['jj']; 
     90        $hh = $_POST['hh']; 
     91        $mn = $_POST['mn']; 
     92        $ss = $_POST['ss']; 
     93        $jj = ($jj > 31) ? 31 : $jj; 
     94        $hh = ($hh > 23) ? $hh - 24 : $hh; 
     95        $mn = ($mn > 59) ? $mn - 60 : $mn; 
     96        $ss = ($ss > 59) ? $ss - 60 : $ss; 
     97        $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; 
     98        $_POST['post_date_gmt'] = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss"); 
     99    }  
     100 
     101    wp_update_post($_POST); 
     102 
     103    // Meta Stuff 
     104    if ($_POST['meta']) : 
     105        foreach ($_POST['meta'] as $key => $value) : 
     106            update_meta($key, $value['key'], $value['value']); 
     107        endforeach; 
     108    endif; 
     109 
     110    if ($_POST['deletemeta']) : 
     111        foreach ($_POST['deletemeta'] as $key => $value) : 
     112            delete_meta($key); 
     113        endforeach; 
     114    endif; 
     115 
    53116    add_meta($post_ID); 
    54117} 
  • trunk/wp-admin/post.php

    r2647 r2650  
    118118 
    119119case 'editpost': 
    120     // die(var_dump('<pre>', $_POST)); 
    121     if (!isset($blog_ID)) { 
    122         $blog_ID = 1; 
    123     } 
    124     $post_ID = $_POST['post_ID']; 
    125  
    126     if (!user_can_edit_post($user_ID, $post_ID, $blog_ID)) 
    127         die( __('You are not allowed to edit this post.') ); 
    128  
    129     $post_categories = $_POST['post_category']; 
    130     if (!$post_categories) $post_categories[] = 1; 
    131     $content = apply_filters('content_save_pre', $_POST['content']); 
    132     $excerpt = apply_filters('excerpt_save_pre', $_POST['excerpt']); 
    133     $post_title = $_POST['post_title']; 
    134     $prev_status = $_POST['prev_status']; 
    135     $post_status = $_POST['post_status']; 
    136     $menu_order = (int) $_POST['menu_order']; 
    137     if (! empty($_POST['post_author_override'])) { 
    138         $post_author = (int) $_POST['post_author_override']; 
    139     } else if (! empty($_POST['post_author'])) { 
    140         $post_author = (int) $_POST['post_author']; 
    141     } else { 
    142         $post_author = (int) $_POST['user_ID']; 
    143     } 
    144     if ( !user_can_edit_user($user_ID, $post_author) ) 
    145         die( __('You cannot post as this user.') ); 
    146  
    147     $comment_status = $_POST['comment_status']; 
    148     if (empty($comment_status)) $comment_status = 'closed'; 
    149     //if (!$_POST['comment_status']) $comment_status = get_settings('default_comment_status'); 
    150  
    151     $ping_status = $_POST['ping_status']; 
    152     if (empty($ping_status)) $ping_status = 'closed'; 
    153     //if (!$_POST['ping_status']) $ping_status = get_settings('default_ping_status'); 
    154     $post_password = $_POST['post_password']; 
    155     $post_name = $_POST['post_name']; 
    156  
    157     $post_parent = 0; 
    158     if (isset($_POST['parent_id'])) { 
    159         $post_parent = $_POST['parent_id']; 
    160     } 
    161  
    162     $trackback = $_POST['trackback_url']; 
    163     // Format trackbacks 
    164     $trackback = preg_replace('|\s+|', '\n', $trackback); 
    165      
    166     if (isset($_POST['publish'])) $post_status = 'publish'; 
    167     // Double-check 
    168     if ( 'publish' == $post_status && (!user_can_create_post($user_ID)) ) 
    169         $post_status = 'draft'; 
    170  
    171     if ( empty($post_name) ) { 
    172         if ( 'draft' != $post_status ) 
    173             $post_name = sanitize_title($post_title, $post_ID); 
    174     } else { 
    175         $post_name = sanitize_title($post_name, $post_ID); 
    176     } 
    177  
    178     if ('publish' == $post_status) { 
    179         $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"); 
    180         if ($post_name_check) { 
    181             $suffix = 2; 
    182             while ($post_name_check) { 
    183                 $alt_post_name = $post_name . "-$suffix"; 
    184                 $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"); 
    185                 $suffix++; 
    186             } 
    187             $post_name = $alt_post_name; 
    188         } 
    189     } 
    190  
    191     if (user_can_edit_post_date($user_ID, $post_ID) && (!empty($_POST['edit_date']))) { 
    192         $aa = $_POST['aa']; 
    193         $mm = $_POST['mm']; 
    194         $jj = $_POST['jj']; 
    195         $hh = $_POST['hh']; 
    196         $mn = $_POST['mn']; 
    197         $ss = $_POST['ss']; 
    198         $jj = ($jj > 31) ? 31 : $jj; 
    199         $hh = ($hh > 23) ? $hh - 24 : $hh; 
    200         $mn = ($mn > 59) ? $mn - 60 : $mn; 
    201         $ss = ($ss > 59) ? $ss - 60 : $ss; 
    202         $datemodif = ", post_date = '$aa-$mm-$jj $hh:$mn:$ss'"; 
    203     $datemodif_gmt = ", post_date_gmt = '".get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss")."'"; 
    204     } else { 
    205         $datemodif = ''; 
    206         $datemodif_gmt = ''; 
    207     } 
    208  
    209     $now = current_time('mysql'); 
    210     $now_gmt = current_time('mysql', 1); 
    211  
    212     $result = $wpdb->query(" 
    213         UPDATE $wpdb->posts SET 
    214             post_content = '$content', 
    215             post_excerpt = '$excerpt', 
    216             post_title = '$post_title'" 
    217             .$datemodif_gmt 
    218             .$datemodif.",           
    219             post_status = '$post_status', 
    220             comment_status = '$comment_status', 
    221             ping_status = '$ping_status', 
    222             post_author = '$post_author', 
    223             post_password = '$post_password', 
    224             post_name = '$post_name', 
    225             to_ping = '$trackback', 
    226             post_modified = '$now', 
    227             post_modified_gmt = '$now_gmt', 
    228             menu_order = '$menu_order', 
    229             post_parent = '$post_parent' 
    230         WHERE ID = $post_ID "); 
     120    edit_post(); 
    231121 
    232122    if ($_POST['save']) { 
     
    244134    } 
    245135    header ('Location: ' . $location); // Send user on their way while we keep working 
    246  
    247     // Meta Stuff 
    248     if ($_POST['meta']) : 
    249         foreach ($_POST['meta'] as $key => $value) : 
    250             update_meta($key, $value['key'], $value['value']); 
    251         endforeach; 
    252     endif; 
    253  
    254     if ($_POST['deletemeta']) : 
    255         foreach ($_POST['deletemeta'] as $key => $value) : 
    256             delete_meta($key); 
    257         endforeach; 
    258     endif; 
    259  
    260     add_meta($post_ID); 
    261  
    262     // Now it's category time! 
    263     // First the old categories 
    264     $old_categories = $wpdb->get_col("SELECT category_id FROM $wpdb->post2cat WHERE post_id = $post_ID"); 
    265      
    266     // Delete any? 
    267     foreach ($old_categories as $old_cat) { 
    268         if (!in_array($old_cat, $post_categories)) // If a category was there before but isn't now 
    269             $wpdb->query("DELETE FROM $wpdb->post2cat WHERE category_id = $old_cat AND post_id = $post_ID LIMIT 1"); 
    270     } 
    271      
    272     // Add any? 
    273     foreach ($post_categories as $new_cat) { 
    274         if (!in_array($new_cat, $old_categories)) 
    275             $wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ($post_ID, $new_cat)"); 
    276     } 
    277  
    278     if ($prev_status != 'publish' && $post_status == 'publish') 
    279         do_action('private_to_published', $post_ID); 
    280  
    281     do_action('edit_post', $post_ID); 
    282  
    283     if ($post_status == 'publish') { 
    284         do_action('publish_post', $post_ID); 
    285         do_trackbacks($post_ID); 
    286         do_enclose( $content, $post_ID ); 
    287         if ( get_option('default_pingback_flag') ) 
    288             pingback($content, $post_ID); 
    289     } 
    290  
    291     if ($post_status == 'static') { 
    292         generate_page_rewrite_rules(); 
    293  
    294         if ( ! update_post_meta($post_ID, '_wp_page_template',  $_POST['page_template'])) { 
    295             add_post_meta($post_ID, '_wp_page_template',  $_POST['page_template'], true); 
    296         } 
    297     } 
    298136 
    299137    exit(); 
  • trunk/wp-includes/functions-post.php

    r2648 r2650  
    88function wp_insert_post($postarr = array()) { 
    99    global $wpdb, $allowedtags, $user_ID; 
    10      
     10 
    1111    // export array as variables 
    1212    extract($postarr); 
     13 
     14    // Are we updating or creating? 
     15    $update = false; 
     16    if ( !empty($ID) ) { 
     17        $update = true; 
     18        $post = & get_post($ID); 
     19        $previous_status = $post->post_status; 
     20    } 
    1321 
    1422    // Get the basics. 
     
    3240        $post_status = 'draft'; 
    3341     
    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; 
     42    // Get the post ID. 
     43    if ( $update ) { 
     44        $post_ID = $ID; 
     45    } else { 
     46        $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->posts'"); 
     47        $post_ID = $id_result->Auto_increment; 
     48    } 
    3749 
    3850    // Create a valid post name.  Drafts are allowed to have an empty 
     
    5769        $post_pingback = get_option('default_pingback_flag'); 
    5870 
    59     if ( isset($trackback_url) ) 
    60         $trackback_url = preg_replace('|\s+|', "\n", $trackback_url); 
     71    if ( isset($to_ping) ) 
     72        $to_ping = preg_replace('|\s+|', "\n", $to_ping); 
    6173    else 
    62         $trackback_url = ''; 
     74        $to_ping = ''; 
    6375     
    6476    if ( isset($post_parent) ) 
     
    88100    } 
    89101 
    90     $postquery = "INSERT INTO $wpdb->posts 
     102    if ($update) { 
     103        $postquery = 
     104            "UPDATE $wpdb->posts SET 
     105            post_author = '$post_author', 
     106            post_date = '$post_date', 
     107            post_date_gmt = '$post_date_gmt', 
     108            post_content = '$post_content', 
     109            post_title = '$post_title', 
     110            post_excerpt = '$post_excerpt', 
     111            post_status = '$post_status', 
     112            comment_status = '$comment_status', 
     113            ping_status = '$ping_status', 
     114            post_password = '$post_password', 
     115            post_name = '$post_name', 
     116            to_ping = '$to_ping', 
     117            post_modified = '$post_date', 
     118            post_modified_gmt = '$post_date_gmt', 
     119            post_parent = '$post_parent', 
     120            menu_order = '$menu_order' 
     121            WHERE ID = $post_ID"; 
     122    } else { 
     123        $postquery = 
     124            "INSERT INTO $wpdb->posts 
    91125            (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) 
    92126            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            "; 
     127            ('$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')"; 
     128    } 
    95129     
    96130    $result = $wpdb->query($postquery); 
    97     $post_ID = $wpdb->insert_id; 
     131    if ( $update ) 
     132        $rval = $wpdb->rows_affected; 
     133    else  
     134        $rval = $wpdb->insert_id; 
    98135 
    99136    // Set GUID 
    100     $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); 
     137    if ( ! $update ) 
     138        $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); 
    101139     
    102140    wp_set_post_cats('', $post_ID, $post_category); 
    103141 
    104     $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); 
    105      
     142    if ( $update) { 
     143        if ($previous_status != 'publish' && $post_status == 'publish') 
     144            do_action('private_to_published', $post_ID); 
     145         
     146        do_action('edit_post', $post_ID); 
     147    } 
     148 
    106149    if ($post_status == 'publish') { 
    107150        do_action('publish_post', $post_ID); 
     
    111154        do_trackbacks($post_ID); 
    112155    }   else if ($post_status == 'static') { 
     156        generate_page_rewrite_rules(); 
     157 
    113158        if ( empty($page_template) ) 
    114159            $page_template = 'Default Template'; 
    115         generate_page_rewrite_rules(); 
    116         add_post_meta($post_ID, '_wp_page_template',  $page_template, true); 
     160 
     161        if ( ! update_post_meta($post_ID, '_wp_page_template',  $page_template)) 
     162            add_post_meta($post_ID, '_wp_page_template',  $page_template, true); 
    117163    } 
    118164 
     
    124170    global $wpdb; 
    125171 
    126     $sql = "SELECT * FROM $wpdb->posts WHERE ID=$postid"; 
    127     $result = $wpdb->get_row($sql, $mode); 
     172    $post = get_post($postid, $mode); 
    128173     
    129174    // Set categories 
    130175    if($mode == OBJECT) { 
    131         $result->post_category = wp_get_post_cats('',$postid); 
     176        $post->post_category = wp_get_post_cats('',$postid); 
    132177    }  
    133178    else { 
    134         $result['post_category'] = wp_get_post_cats('',$postid); 
    135     } 
    136  
    137     return $result; 
     179        $post['post_category'] = wp_get_post_cats('',$postid); 
     180    } 
     181 
     182    return $post; 
    138183} 
    139184 
     
    155200    global $wpdb; 
    156201 
    157     // First get all of the original fields 
    158     extract(wp_get_single_post($postarr['ID'], ARRAY_A));    
    159  
    160     // Now overwrite any changed values being passed in 
    161     extract($postarr); 
    162  
    163     // Make sure we set a valid category 
    164     if ( 0 == count($post_category) || !is_array($post_category) ) 
    165         $post_category = array($post_default_category); 
    166  
    167     // Do some escapes for safety 
    168     $post_title   = $wpdb->escape($post_title); 
    169     $post_excerpt = $wpdb->escape($post_excerpt); 
    170     $post_content = $wpdb->escape($post_content); 
    171  
    172     $post_modified = current_time('mysql'); 
    173     $post_modified_gmt = current_time('mysql', 1); 
    174  
    175     $sql = "UPDATE $wpdb->posts  
    176         SET post_content = '$post_content', 
    177         post_title = '$post_title', 
    178         post_category = $post_category[0], 
    179         post_status = '$post_status', 
    180         post_date = '$post_date', 
    181         post_date_gmt = '$post_date_gmt', 
    182         post_modified = '$post_modified', 
    183         post_modified_gmt = '$post_modified_gmt', 
    184         post_excerpt = '$post_excerpt', 
    185         ping_status = '$ping_status', 
    186         comment_status = '$comment_status' 
    187         WHERE ID = $ID"; 
    188          
    189     $result = $wpdb->query($sql); 
    190     $rows_affected = $wpdb->rows_affected; 
    191  
    192     wp_set_post_cats('', $ID, $post_category); 
    193  
    194     do_action('edit_post', $ID); 
    195  
    196     return $rows_affected; 
     202    // First, get all of the original fields 
     203    $post = wp_get_single_post($postarr['ID'], ARRAY_A);     
     204 
     205    // Escape data pulled from DB. 
     206    foreach ($post as $key => $value) 
     207        $post[$key] = $wpdb->escape($value); 
     208 
     209    // Passed post category list takes overwrites existing 
     210    // category list. 
     211    if ( isset($postarr['post_category']) ) 
     212        $post_cats = $postarr['post_category']; 
     213    else  
     214        $post_cats = $post['post_category']; 
     215 
     216    // Merge old and new fields with new fields overwriting old ones. 
     217    $postarr = array_merge($post, $postarr); 
     218    $postarr['post_category'] = $post_cats;  
     219 
     220    return wp_insert_post($postarr); 
    197221} 
    198222