WordPress.org

Make WordPress Core

Changeset 7900


Ignore:
Timestamp:
05/05/2008 11:19:27 PM (10 years ago)
Author:
ryan
Message:

wp_insert_post() -- Validate page template, check return of insert and update queries, add option to return WP_Error. fixes #6227 see #6098

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r7894 r7900  
    11101110 * @return int post ID or 0 on error
    11111111 */
    1112 function wp_insert_post($postarr = array()) {
     1112function wp_insert_post($postarr = array(), $wp_error = false) {
    11131113    global $wpdb, $wp_rewrite, $user_ID;
    11141114
     
    11331133    }
    11341134
    1135     if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) )
    1136         return 0;
     1135    if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) ) {
     1136        if ( $wp_error )
     1137            return new WP_Error('empty_content', __('Content, title, and excerpt are empty.'));
     1138        else
     1139            return 0;
     1140    }
    11371141
    11381142    // Make sure we set a valid category
     
    12451249    if ($update) {
    12461250        do_action( 'pre_post_update', $post_ID );
    1247         $wpdb->update( $wpdb->posts, $data, $where );
     1251        if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
     1252            if ( $wp_error )
     1253                return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
     1254            else
     1255                return 0;
     1256        }
    12481257    } else {
    12491258        $data['post_mime_type'] = stripslashes( $post_mime_type ); // This isn't in the update
    1250         $wpdb->insert( $wpdb->posts, $data );
     1259        if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
     1260            if ( $wp_error )
     1261                return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
     1262            else
     1263                return 0;   
     1264        }
    12511265        $post_ID = (int) $wpdb->insert_id;
    12521266
     
    12651279    $current_guid = get_post_field( 'guid', $post_ID );
    12661280
    1267     if ( 'page' == $post_type ) {
     1281    if ( 'page' == $post_type )
    12681282        clean_page_cache($post_ID);
    1269     } else {
     1283    else
    12701284        clean_post_cache($post_ID);
    1271     }
    12721285
    12731286    // Set GUID
     
    12761289
    12771290    $post = get_post($post_ID);
    1278     if ( !empty($page_template) )
     1291
     1292    if ( !empty($page_template) && 'page' == $post_type ) {
    12791293        $post->page_template = $page_template;
     1294        $page_templates = get_page_templates();
     1295        if ( ! in_array($page_template, $page_templates) ) {
     1296            if ( $wp_error )
     1297                return new WP_Error('invalid_page_template', __('The page template is invalid.'));
     1298            else
     1299                return 0;
     1300        }
     1301        if ( ! update_post_meta($post_ID, '_wp_page_template',  $page_template) )
     1302            add_post_meta($post_ID, '_wp_page_template',  $page_template, true);
     1303    }
    12801304
    12811305    wp_transition_post_status($post_status, $previous_status, $post);
     
    28982922function _save_post_hook($post_id, $post) {
    28992923    if ( $post->post_type == 'page' ) {
    2900         if ( !empty($post->page_template) )
    2901             if ( ! update_post_meta($post_id, '_wp_page_template',  $post->page_template))
    2902                 add_post_meta($post_id, '_wp_page_template',  $post->page_template, true);
    2903 
    29042924        clean_page_cache($post_id);
    29052925        global $wp_rewrite;
Note: See TracChangeset for help on using the changeset viewer.