Make WordPress Core

Ticket #6098: page_template_validation.diff

File page_template_validation.diff, 3.1 KB (added by ryan, 17 years ago)
  • wp-includes/post.php

     
    11091109 * @param array $postarr post contents
    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
    11151115        $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
     
    11321132                $previous_status = 'new';
    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
    11391143        if (0 == count($post_category) || !is_array($post_category)) {
     
    12441248
    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
    12531267                // use the newly generated $post_ID
     
    12641278
    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
    12741287        if ( !$update && '' == $current_guid )
    12751288                $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
    12761289
     1290        if ( !empty($page_template) && 'page' == $post_type ) {
     1291                $page_templates = get_page_templates();
     1292                if ( ! in_array($page_template, $page_templates) ) {
     1293                        if ( $wp_error )
     1294                                return new WP_Error('invalid_page_template', __('The page template is invalid.'));
     1295                        else
     1296                                return 0;
     1297                }
     1298                if ( ! update_post_meta($post_ID, '_wp_page_template',  $page_template) )
     1299                        add_post_meta($post_ID, '_wp_page_template',  $page_template, true);
     1300        }
     1301
    12771302        $post = get_post($post_ID);
    1278         if ( !empty($page_template) )
    1279                 $post->page_template = $page_template;
    12801303
    12811304        wp_transition_post_status($post_status, $previous_status, $post);
    12821305
     
    28972920 */
    28982921function _save_post_hook($post_id, $post) {
    28992922        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 
    29042923                clean_page_cache($post_id);
    29052924                global $wp_rewrite;
    29062925                $wp_rewrite->flush_rules();