Ticket #23931: 23931.1.diff
File 23931.1.diff, 7.5 KB (added by , 10 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() { 2089 2089 * 2090 2090 * @since 2.0.0 2091 2091 * 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 2092 2094 * @global wpdb $wpdb WordPress database abstraction object. 2093 2095 * 2094 2096 * @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 2096 2098 */ 2097 2099 function wp_insert_comment( $commentdata ) { 2098 2100 global $wpdb; 2099 $ data = wp_unslash( $commentdata );2101 $commentdata = wp_unslash( $commentdata ); 2100 2102 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 ); 2116 2126 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 } 2118 2130 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 ) ) { 2121 2132 $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' ); 2122 2133 2123 2134 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 ] ); 2126 2137 } 2127 2138 } 2128 2139 2129 if ( ! $wpdb->insert( $wpdb->comments, $ compacted) ) {2140 if ( ! $wpdb->insert( $wpdb->comments, $data ) ) { 2130 2141 return false; 2131 2142 } 2132 2143 } 2133 2144 2134 2145 $id = (int) $wpdb->insert_id; 2135 2146 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'] ); 2138 2149 } 2139 2150 $comment = get_comment( $id ); 2140 2151 -
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 { 630 630 * @ticket 30478 631 631 */ 632 632 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 ) ); 634 634 add_comment_meta( $comments[0], 'foo', 'aaa' ); 635 635 add_comment_meta( $comments[1], 'foo', 'zzz' ); 636 636 add_comment_meta( $comments[2], 'foo', 'jjj' ); … … class Tests_Comment_Query extends WP_UnitTestCase { 656 656 */ 657 657 public function test_orderby_clause_key_as_secondary_sort() { 658 658 $c1 = $this->factory->comment->create( array( 659 'comment_post_ID' => $this->post_id, 659 660 'comment_date' => '2015-01-28 03:00:00', 660 661 ) ); 661 662 $c2 = $this->factory->comment->create( array( 663 'comment_post_ID' => $this->post_id, 662 664 'comment_date' => '2015-01-28 05:00:00', 663 665 ) ); 664 666 $c3 = $this->factory->comment->create( array( 667 'comment_post_ID' => $this->post_id, 665 668 'comment_date' => '2015-01-28 03:00:00', 666 669 ) ); 667 670 … … class Tests_Comment_Query extends WP_UnitTestCase { 691 694 * @ticket 30478 692 695 */ 693 696 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 ) ); 695 698 696 699 add_comment_meta( $comments[0], 'foo', 'jjj' ); 697 700 add_comment_meta( $comments[1], 'foo', 'zzz' ); … … class Tests_Comment_Query extends WP_UnitTestCase { 1592 1595 * @ticket 24826 1593 1596 */ 1594 1597 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 ) ); 1596 1599 1597 1600 $query1 = new WP_Comment_Query(); 1598 1601 $this->assertNull( $query1->query_vars ); … … class Tests_Comment_Query extends WP_UnitTestCase { 1632 1635 1633 1636 $this->assertSame( $num_queries, $wpdb->num_queries ); 1634 1637 } 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 } 1635 1669 }