WordPress.org

Make WordPress Core

Ticket #23931: 23931.1.diff

File 23931.1.diff, 7.5 KB (added by valendesigns, 5 years ago)
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 88508fa..74c81cc 100644
    function wp_get_current_commenter() { 
    20892089 *
    20902090 * @since 2.0.0
    20912091 *
     2092 * @uses apply_filters() Calls 'wp_insert_comment_data' hook with comment data prior to insertion
     2093 * @uses do_action() Calls 'wp_insert_comment' hook with inserted comment ID and comment object
    20922094 * @global wpdb $wpdb WordPress database abstraction object.
    20932095 *
    20942096 * @param array $commentdata Contains information on the comment.
    2095  * @return int|bool The new comment's ID on success, false on failure.
     2097 * @return int|bool|WP_Error The new comment's ID, false on failure, or instance of WP_Error
    20962098 */
    20972099function wp_insert_comment( $commentdata ) {
    20982100        global $wpdb;
    2099         $data = wp_unslash( $commentdata );
     2101        $commentdata = wp_unslash( $commentdata );
    21002102
    2101         $comment_author       = ! isset( $data['comment_author'] )       ? '' : $data['comment_author'];
    2102         $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];
    2103         $comment_author_url   = ! isset( $data['comment_author_url'] )   ? '' : $data['comment_author_url'];
    2104         $comment_author_IP    = ! isset( $data['comment_author_IP'] )    ? '' : $data['comment_author_IP'];
    2105 
    2106         $comment_date     = ! isset( $data['comment_date'] )     ? current_time( 'mysql' )            : $data['comment_date'];
    2107         $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
    2108 
    2109         $comment_post_ID  = ! isset( $data['comment_post_ID'] )  ? '' : $data['comment_post_ID'];
    2110         $comment_content  = ! isset( $data['comment_content'] )  ? '' : $data['comment_content'];
    2111         $comment_karma    = ! isset( $data['comment_karma'] )    ? 0  : $data['comment_karma'];
    2112         $comment_approved = ! isset( $data['comment_approved'] ) ? 1  : $data['comment_approved'];
    2113         $comment_agent    = ! isset( $data['comment_agent'] )    ? '' : $data['comment_agent'];
    2114         $comment_type     = ! isset( $data['comment_type'] )     ? '' : $data['comment_type'];
    2115         $comment_parent   = ! isset( $data['comment_parent'] )   ? 0  : $data['comment_parent'];
     2103        $defaults = array(
     2104                'comment_post_ID' => '',
     2105                'comment_author' => '',
     2106                'comment_author_email' => '',
     2107                'comment_author_url' => '',
     2108                'comment_author_IP' => '',
     2109                'comment_date' => current_time( 'mysql', false ),
     2110                'comment_content' => '',
     2111                'comment_karma' => 0,
     2112                'comment_approved' => 1,
     2113                'comment_agent' => '',
     2114                'comment_type' => '',
     2115                'comment_parent' => 0,
     2116                'user_id' => 0,
     2117        );
     2118        if ( ! empty( $commentdata['comment_date'] ) ) {
     2119                $defaults['comment_date_gmt'] = get_gmt_from_date( $commentdata['comment_date'] );
     2120        } else {
     2121                $defaults['comment_date_gmt'] = current_time( 'mysql', true );
     2122        }
     2123        $commentdata = array_merge( $defaults, $commentdata );
     2124        $data = array_intersect_key( $commentdata, $defaults );
     2125        $data = apply_filters( 'wp_insert_comment_data', $data );
    21162126
    2117         $user_id  = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];
     2127        if ( empty( $data['comment_post_ID'] ) || ! get_post( $data['comment_post_ID'] ) ) {
     2128                return new WP_Error( 'invalid_comment_post_id', __( 'Missing or invalid comment_post_ID' ) );
     2129        }
    21182130
    2119         $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' );
    2120         if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
     2131        if ( ! $wpdb->insert( $wpdb->comments, $data ) ) {
    21212132                $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
    21222133
    21232134                foreach( $fields as $field ) {
    2124                         if ( isset( $compacted[ $field ] ) ) {
    2125                                 $compacted[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $compacted[ $field ] );
     2135                        if ( isset( $data[ $field ] ) ) {
     2136                                $data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $data[ $field ] );
    21262137                        }
    21272138                }
    21282139
    2129                 if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
     2140                if ( ! $wpdb->insert( $wpdb->comments, $data ) ) {
    21302141                        return false;
    21312142                }
    21322143        }
    21332144
    21342145        $id = (int) $wpdb->insert_id;
    21352146
    2136         if ( $comment_approved == 1 ) {
    2137                 wp_update_comment_count( $comment_post_ID );
     2147        if ( intval( $data['comment_approved'] ) === 1 ) {
     2148                wp_update_comment_count( $data['comment_post_ID'] );
    21382149        }
    21392150        $comment = get_comment( $id );
    21402151
  • tests/phpunit/tests/comment/query.php

    diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
    index 1958310..22d45f0 100644
    class Tests_Comment_Query extends WP_UnitTestCase { 
    630630         * @ticket 30478
    631631         */
    632632        public function test_orderby_clause_key() {
    633                 $comments = $this->factory->comment->create_many( 3 );
     633                $comments = $this->factory->comment->create_many( 3, array( 'comment_post_ID' => $this->post_id ) );
    634634                add_comment_meta( $comments[0], 'foo', 'aaa' );
    635635                add_comment_meta( $comments[1], 'foo', 'zzz' );
    636636                add_comment_meta( $comments[2], 'foo', 'jjj' );
    class Tests_Comment_Query extends WP_UnitTestCase { 
    656656         */
    657657        public function test_orderby_clause_key_as_secondary_sort() {
    658658                $c1 = $this->factory->comment->create( array(
     659                        'comment_post_ID' => $this->post_id,
    659660                        'comment_date' => '2015-01-28 03:00:00',
    660661                ) );
    661662                $c2 = $this->factory->comment->create( array(
     663                        'comment_post_ID' => $this->post_id,
    662664                        'comment_date' => '2015-01-28 05:00:00',
    663665                ) );
    664666                $c3 = $this->factory->comment->create( array(
     667                        'comment_post_ID' => $this->post_id,
    665668                        'comment_date' => '2015-01-28 03:00:00',
    666669                ) );
    667670
    class Tests_Comment_Query extends WP_UnitTestCase { 
    691694         * @ticket 30478
    692695         */
    693696        public function test_orderby_more_than_one_clause_key() {
    694                 $comments = $this->factory->comment->create_many( 3 );
     697                $comments = $this->factory->comment->create_many( 3, array( 'comment_post_ID' => $this->post_id ) );
    695698
    696699                add_comment_meta( $comments[0], 'foo', 'jjj' );
    697700                add_comment_meta( $comments[1], 'foo', 'zzz' );
    class Tests_Comment_Query extends WP_UnitTestCase { 
    15921595         * @ticket 24826
    15931596         */
    15941597        public function test_comment_query_object() {
    1595                 $comment_id = $this->factory->comment->create();
     1598                $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id ) );
    15961599
    15971600                $query1 = new WP_Comment_Query();
    15981601                $this->assertNull( $query1->query_vars );
    class Tests_Comment_Query extends WP_UnitTestCase { 
    16321635
    16331636                $this->assertSame( $num_queries, $wpdb->num_queries );
    16341637        }
     1638
     1639        /**
     1640         * Ticket @23931
     1641         */
     1642        function test_wp_insert_comment_data_filter() {
     1643                $comment_args = array(
     1644                        'comment_post_ID' => $this->post_id,
     1645                        'comment_content' => 'not-filtered',
     1646                );
     1647                $filter = function ( $comment_data ) {
     1648                        $comment_data['comment_content'] = 'filtered';
     1649                        return $comment_data;
     1650                };
     1651                add_filter( 'wp_insert_comment_data', $filter );
     1652                $comment_id = $this->factory->comment->create( $comment_args );
     1653                remove_filter( 'wp_insert_comment_data', $filter );
     1654                $comment = get_comment( $comment_id );
     1655                $this->assertEquals( 'filtered', $comment->comment_content );
     1656        }
     1657
     1658        /**
     1659         * Ticket @23931
     1660         */
     1661        function test_error_when_not_supplying_comment_post_id() {
     1662                $r = $this->factory->comment->create( array() );
     1663                $this->assertInstanceOf( 'WP_Error', $r );
     1664                $this->assertEquals( 'invalid_comment_post_id', $r->get_error_code() );
     1665
     1666                $r = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id ) );
     1667                $this->assertInternalType( 'int', $r );
     1668        }
    16351669}