Make WordPress Core

Ticket #23931: comment.php.1.diff

File comment.php.1.diff, 1.9 KB (added by westonruter, 12 years ago)

Simplify construction of commentdata array for insertion with defaults

  • wp-includes/comment.php

     
    12621262 */
    12631263function wp_insert_comment($commentdata) {
    12641264        global $wpdb;
    1265         extract(wp_unslash($commentdata), EXTR_SKIP);
    1266 
    1267         if ( ! isset($comment_author_IP) )
    1268                 $comment_author_IP = '';
    1269         if ( ! isset($comment_date) )
    1270                 $comment_date = current_time('mysql');
    1271         if ( ! isset($comment_date_gmt) )
    1272                 $comment_date_gmt = get_gmt_from_date($comment_date);
    1273         if ( ! isset($comment_parent) )
    1274                 $comment_parent = 0;
    1275         if ( ! isset($comment_approved) )
    1276                 $comment_approved = 1;
    1277         if ( ! isset($comment_karma) )
    1278                 $comment_karma = 0;
    1279         if ( ! isset($user_id) )
    1280                 $user_id = 0;
    1281         if ( ! isset($comment_type) )
    1282                 $comment_type = '';
    1283 
    1284         $data = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id');
     1265        $commentdata = wp_unslash( $commentdata );
     1266        $defaults = array(
     1267                'comment_post_ID' => null,
     1268                'comment_author' => '',
     1269                'comment_author_email' => '',
     1270                'comment_author_url' => '',
     1271                'comment_author_IP' => '',
     1272                'comment_date' => current_time( 'mysql', false ),
     1273                'comment_date_gmt' => current_time( 'mysql', true ),
     1274                'comment_content' => '',
     1275                'comment_karma' => 0,
     1276                'comment_approved' => 1,
     1277                'comment_agent' => '',
     1278                'comment_type' => '',
     1279                'comment_parent' => 0,
     1280                'user_id' => 0,
     1281        );
     1282        $commentdata = array_merge( $defaults, $commentdata );
     1283        $data = array_intersect_key( $commentdata, $defaults );
     1284        $data = apply_filters( 'wp_insert_comment_data', $data );
     1285        extract($data, EXTR_SKIP);
    12851286        $wpdb->insert($wpdb->comments, $data);
    12861287
    12871288        $id = (int) $wpdb->insert_id;