Make WordPress Core

Ticket #23931: 23931.diff

File 23931.diff, 5.2 KB (added by westonruter, 10 years ago)

Refreshed patch. PR: https://github.com/x-team/wordpress-develop/pull/25

  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index dd45821..4a7873b 100644
    function wp_get_current_commenter() { 
    15891589 * 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'.
    15901590 *
    15911591 * @since 2.0.0
     1592 * @uses apply_filters() Calls 'wp_insert_comment_data' hook with comment data prior to insertion
     1593 * @uses do_action() Calls 'wp_insert_comment' hook with inserted comment ID and comment object
    15921594 * @uses $wpdb
    15931595 *
    15941596 * @param array $commentdata Contains information on the comment.
    1595  * @return int|bool The new comment's ID on success, false on failure.
     1597 * @return int|bool|WP_Error The new comment's ID, false on failure, or instance of WP_Error
    15961598 */
    15971599function wp_insert_comment( $commentdata ) {
    15981600        global $wpdb;
    1599         $data = wp_unslash( $commentdata );
    1600 
    1601         $comment_author       = ! isset( $data['comment_author'] )       ? '' : $data['comment_author'];
    1602         $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];
    1603         $comment_author_url   = ! isset( $data['comment_author_url'] )   ? '' : $data['comment_author_url'];
    1604         $comment_author_IP    = ! isset( $data['comment_author_IP'] )    ? '' : $data['comment_author_IP'];
    1605 
    1606         $comment_date     = ! isset( $data['comment_date'] )     ? current_time( 'mysql' )            : $data['comment_date'];
    1607         $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
    1608 
    1609         $comment_post_ID  = ! isset( $data['comment_post_ID'] )  ? '' : $data['comment_post_ID'];
    1610         $comment_content  = ! isset( $data['comment_content'] )  ? '' : $data['comment_content'];
    1611         $comment_karma    = ! isset( $data['comment_karma'] )    ? 0  : $data['comment_karma'];
    1612         $comment_approved = ! isset( $data['comment_approved'] ) ? 1  : $data['comment_approved'];
    1613         $comment_agent    = ! isset( $data['comment_agent'] )    ? '' : $data['comment_agent'];
    1614         $comment_type     = ! isset( $data['comment_type'] )     ? '' : $data['comment_type'];
    1615         $comment_parent   = ! isset( $data['comment_parent'] )   ? 0  : $data['comment_parent'];
     1601        $commentdata = wp_unslash( $commentdata );
     1602
     1603        $defaults = array(
     1604                'comment_post_ID' => '',
     1605                'comment_author' => '',
     1606                'comment_author_email' => '',
     1607                'comment_author_url' => '',
     1608                'comment_author_IP' => '',
     1609                'comment_date' => current_time( 'mysql', false ),
     1610                'comment_content' => '',
     1611                'comment_karma' => 0,
     1612                'comment_approved' => 1,
     1613                'comment_agent' => '',
     1614                'comment_type' => '',
     1615                'comment_parent' => 0,
     1616                'user_id' => 0,
     1617        );
     1618        if ( ! empty( $commentdata['comment_date'] ) ) {
     1619                $defaults['comment_date_gmt'] = get_gmt_from_date( $commentdata['comment_date'] );
     1620        } else {
     1621                $defaults['comment_date_gmt'] = current_time( 'mysql', true );
     1622        }
     1623        $commentdata = array_merge( $defaults, $commentdata );
     1624        $data = array_intersect_key( $commentdata, $defaults );
     1625        $data = apply_filters( 'wp_insert_comment_data', $data );
    16161626
    1617         $user_id  = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];
     1627        if ( empty( $data['comment_post_ID'] ) || ! get_post( $data['comment_post_ID'] ) ) {
     1628                return new WP_Error( 'invalid_comment_post_id', __( 'Missing or invalid comment_post_ID' ) );
     1629        }
    16181630
    1619         $compacted = 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' );
    1620         if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
     1631        if ( ! $wpdb->insert( $wpdb->comments, $data ) ) {
    16211632                return false;
    16221633        }
    16231634
    16241635        $id = (int) $wpdb->insert_id;
    16251636
    1626         if ( $comment_approved == 1 ) {
    1627                 wp_update_comment_count( $comment_post_ID );
     1637        if ( intval( $data['comment_approved'] ) === 1 ) {
     1638                wp_update_comment_count( $data['comment_post_ID'] );
    16281639        }
    16291640        $comment = get_comment( $id );
    16301641
  • tests/phpunit/tests/comment/query.php

    diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
    index 2dc3623..37c9a49 100644
    class Tests_Comment_Query extends WP_UnitTestCase { 
    180180                $this->assertEquals( $users[1], $comments[2]->user_id );
    181181
    182182        }
     183
     184        /**
     185         * Ticket @23931
     186         */
     187        function test_wp_insert_comment_data_filter() {
     188                $comment_args = array(
     189                        'comment_post_ID' => $this->factory->post->create(),
     190                        'comment_content' => 'not-filtered',
     191                );
     192                $filter = function ( $comment_data ) {
     193                        $comment_data['comment_content'] = 'filtered';
     194                        return $comment_data;
     195                };
     196                add_filter( 'wp_insert_comment_data', $filter );
     197                $comment_id = $this->factory->comment->create( $comment_args );
     198                remove_filter( 'wp_insert_comment_data', $filter );
     199                $comment = get_comment( $comment_id );
     200                $this->assertEquals( 'filtered', $comment->comment_content );
     201        }
     202
     203        /**
     204         * Ticket @23931
     205         */
     206        function test_error_when_not_supplying_comment_post_id() {
     207                $r = $this->factory->comment->create( array() );
     208                $this->assertInstanceOf( 'WP_Error', $r );
     209                $this->assertEquals( 'invalid_comment_post_id', $r->get_error_code() );
     210
     211                $post_id = $this->factory->post->create();
     212                $r = $this->factory->comment->create( array( 'comment_post_ID' => $post_id ) );
     213                $this->assertInternalType( 'int', $r );
     214        }
    183215}