Make WordPress Core

Ticket #24241: 24241.diff

File 24241.diff, 7.3 KB (added by stevegrunwell, 11 years ago)

Refreshed patch that properly prepares SQL queries, has unit tests, and better adheres to the WordPress coding standards.

  • src/wp-includes/comment.php

     
    101101                }
    102102        }
    103103
    104         /*
    105          * Check if the option to approve comments by previously-approved authors is enabled.
    106          *
    107          * If it is enabled, check whether the comment author has a previously-approved comment,
    108          * as well as whether there are any moderation keywords (if set) present in the author
    109          * email address. If both checks pass, return true. Otherwise, return false.
    110          */
    111         if ( 1 == get_option('comment_whitelist')) {
    112                 if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
     104        if ( 1 == get_option( 'comment_whitelist' ) ) {
     105
     106                // The author name and email are present and this is neither a trackback nor a pingback.
     107                if ( 'trackback' !== $comment_type && 'pingback' !== $comment_type && '' !== $author && '' !== $email ) {
    113108                        // expected_slashed ($author, $email)
    114                         $ok_to_comment = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1");
    115                         if ( ( 1 == $ok_to_comment ) &&
    116                                 ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
    117                                         return true;
    118                         else
     109                        $ok_to_comment = $wpdb->get_var( $wpdb->prepare(
     110                                "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = %s AND comment_author_email = %s and comment_approved = '1' LIMIT 1",
     111                                $author,
     112                                $email
     113                        ) );
     114                        if ( ( 1 == $ok_to_comment ) && ( empty( $mod_keys ) || false === strpos( $email, $mod_keys ) ) ) {
     115                                return true;
     116                        } else {
    119117                                return false;
     118                        }
     119
     120                // This comment is a trackback or pingback.
     121                } elseif ( in_array( $comment_type, array( 'trackback', 'pingback' ) ) ) {
     122                        $domain       = $wpdb->esc_like( parse_url( $url, PHP_URL_HOST ) );
     123                        $okay_to_ping = $wpdb->get_var( $wpdb->prepare(
     124                                "SELECT comment_approved FROM $wpdb->comments WHERE (comment_author_url LIKE %s OR comment_author_url LIKE %s) AND comment_type = %s AND comment_approved = '1' LIMIT 1",
     125                                'http://' . $domain . '%',
     126                                'https://' . $domain . '%',
     127                                $comment_type
     128                        ) );
     129
     130                        return '1' === $okay_to_ping;
    120131                } else {
    121132                        return false;
    122133                }
    123134        }
     135
    124136        return true;
    125137}
    126138
  • tests/phpunit/tests/comment/checkComment.php

     
    6363                $this->assertTrue( $results );
    6464        }
    6565
     66        /**
     67         * @ticket 24241
     68         */
     69        public function test_should_return_true_when_pingback_from_approved_site() {
     70                $post_id   = self::factory()->post->create();
     71                $uniqid    = uniqid( '', true );
     72                $prev_args = array(
     73                        'comment_post_ID'      => $post_id,
     74                        'comment_content'      => 'Can we build it?',
     75                        'comment_approved'     => 1,
     76                        'comment_author_email' => 'bob@example.com',
     77                        'comment_author_url'   => 'http://' . $uniqid . '.example.com/path',
     78                        'comment_author'       => 'BobtheBuilder',
     79                        'comment_type'         => 'pingback',
     80                );
     81                self::factory()->comment->create( $prev_args );
     82
     83                update_option( 'comment_whitelist', 1 );
     84
     85                $author       = 'BobtheBuilder';
     86                $author_email = 'bob@example.com';
     87                $author_url   = 'http://' . $uniqid . '.example.com';
     88                $comment      = 'Can we fix it? Yes, we can (thanks to Wendy).';
     89                $author_ip    = '192.168.0.1';
     90                $user_agent   = '';
     91                $comment_type = 'pingback';
     92
     93                $results = check_comment( $author, $author_email, $author_url, $comment, $author_ip, $user_agent, $comment_type );
     94                $this->assertTrue( $results );
     95        }
     96
     97        /**
     98         * Just like test_should_return_true_when_pingback_from_approved_site() but with a HTTP/HTTPS
     99         * protocol mismatch.
     100         *
     101         * @ticket 24241
     102         */
     103        public function test_should_return_true_when_pingback_from_approved_site_https() {
     104                $post_id   = self::factory()->post->create();
     105                $uniqid    = uniqid( '', true );
     106                $prev_args = array(
     107                        'comment_post_ID'      => $post_id,
     108                        'comment_content'      => 'Can we build it?',
     109                        'comment_approved'     => 1,
     110                        'comment_author_email' => 'bob@example.com',
     111                        'comment_author_url'   => 'https://' . $uniqid . '.example.com/path',
     112                        'comment_author'       => 'BobtheBuilder',
     113                        'comment_type'         => 'pingback',
     114                );
     115                self::factory()->comment->create( $prev_args );
     116
     117                update_option( 'comment_whitelist', 1 );
     118
     119                $author       = 'BobtheBuilder';
     120                $author_email = 'bob@example.com';
     121                $author_url   = 'http://' . $uniqid . '.example.com';
     122                $comment      = 'Can we fix it? Yes, we can (thanks to Wendy).';
     123                $author_ip    = '192.168.0.1';
     124                $user_agent   = '';
     125                $comment_type = 'pingback';
     126
     127                $results = check_comment( $author, $author_email, $author_url, $comment, $author_ip, $user_agent, $comment_type );
     128                $this->assertTrue( $results );
     129        }
     130
     131        /**
     132         * @ticket 24241
     133         */
     134        public function test_should_return_true_when_trackback_from_approved_site() {
     135                $post_id   = self::factory()->post->create();
     136                $uniqid    = uniqid( '', true );
     137                $prev_args = array(
     138                        'comment_post_ID'      => $post_id,
     139                        'comment_content'      => 'Can we build it?',
     140                        'comment_approved'     => 1,
     141                        'comment_author_email' => 'bob@example.com',
     142                        'comment_author_url'   => 'http://' . $uniqid . '.example.com/path',
     143                        'comment_author'       => 'BobtheBuilder',
     144                        'comment_type'         => 'trackback',
     145                );
     146                self::factory()->comment->create( $prev_args );
     147
     148                update_option( 'comment_whitelist', 1 );
     149
     150                $author       = 'BobtheBuilder';
     151                $author_email = 'bob@example.com';
     152                $author_url   = 'http://' . $uniqid . '.example.com';
     153                $comment      = 'Can we fix it? Yes, we can (thanks to Wendy).';
     154                $author_ip    = '192.168.0.1';
     155                $user_agent   = '';
     156                $comment_type = 'trackback';
     157
     158                $results = check_comment( $author, $author_email, $author_url, $comment, $author_ip, $user_agent, $comment_type );
     159                $this->assertTrue( $results );
     160        }
     161
     162        /**
     163         * This test is like the ones above it but the previously-approved comment type does not match
     164         * the type being created.
     165         *
     166         * @ticket 24241
     167         */
     168        public function test_should_return_false_when_author_url_approved_for_wrong_type() {
     169                $post_id   = self::factory()->post->create();
     170                $uniqid    = uniqid( '', true );
     171                $prev_args = array(
     172                        'comment_post_ID'      => $post_id,
     173                        'comment_content'      => 'Can we build it?',
     174                        'comment_approved'     => 1,
     175                        'comment_author_email' => 'bob@example.com',
     176                        'comment_author_url'   => 'http://' . $uniqid . '.example.com/path',
     177                        'comment_author'       => 'BobtheBuilder',
     178                        'comment_type'         => 'comment',
     179                );
     180                self::factory()->comment->create( $prev_args );
     181
     182                update_option( 'comment_whitelist', 1 );
     183
     184                $author       = 'BobtheBuilder';
     185                $author_email = 'bob@example.com';
     186                $author_url   = 'http://' . $uniqid . '.example.com';
     187                $comment      = 'Can we fix it? Yes, we can (thanks to Wendy).';
     188                $author_ip    = '192.168.0.1';
     189                $user_agent   = '';
     190                $comment_type = 'pingback';
     191
     192                $results = check_comment( $author, $author_email, $author_url, $comment, $author_ip, $user_agent, $comment_type );
     193                $this->assertFalse( $results );
     194        }
     195
    66196        public function test_should_return_false_when_content_matches_moderation_key() {
    67197                update_option( 'comment_whitelist', 0 );
    68198