Make WordPress Core

Changeset 53291


Ignore:
Timestamp:
04/27/2022 04:08:16 AM (3 years ago)
Author:
peterwilsoncc
Message:

Comments: Avoid DB error in comment meta queries.

In WP_Comment_Query always include the table name when referencing wp_comments.comment_ID. This avoids ambiguity in when making meta queries as wp_commentmeta includes a column of the same name.

Follow up to [47887].

Props genosseeinhorn, azouamauriac, audrasjb, peterwilsoncc.
Fixes #55218.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment-query.php

    r53280 r53291  
    596596                    if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
    597597                        // Only include requested comment.
    598                         $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' AND comment_ID = %d )", $unapproved_identifier, (int) $_GET['unapproved'] );
     598                        $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' AND {$wpdb->comments}.comment_ID = %d )", $unapproved_identifier, (int) $_GET['unapproved'] );
    599599                    } else {
    600600                        // Include all of the author's unapproved comments.
  • trunk/tests/phpunit/tests/comment/query.php

    r53169 r53291  
    49694969        $this->assertSame( $num_queries_all_args, get_num_queries() );
    49704970    }
     4971
     4972    /**
     4973     * @ticket 55218
     4974     */
     4975    public function test_unapproved_comment_with_meta_query_does_not_trigger_ambiguous_identifier_error() {
     4976        $p       = self::$post_id;
     4977        $c       = self::factory()->comment->create(
     4978            array(
     4979                'comment_post_ID'      => $p,
     4980                'comment_content'      => '1',
     4981                'comment_approved'     => '0',
     4982                'comment_date_gmt'     => gmdate( 'Y-m-d H:i:s', time() ),
     4983                'comment_author_email' => 'foo@bar.mail',
     4984                'comment_meta'         => array( 'foo' => 'bar' ),
     4985            )
     4986        );
     4987        $comment = get_comment( $c );
     4988
     4989        /*
     4990         * This is used to get a bunch of globals set up prior to making the
     4991         * database query. This helps with prepping for the moderation hash.
     4992         */
     4993        $this->go_to(
     4994            add_query_arg(
     4995                array(
     4996                    'unapproved'      => $comment->comment_ID,
     4997                    'moderation-hash' => wp_hash( $comment->comment_date_gmt ),
     4998                ),
     4999                get_comment_link( $comment )
     5000            )
     5001        );
     5002
     5003        /*
     5004         * The result of the query is not needed so it's not assigned to variable.
     5005         *
     5006         * Returning the ID only limits the database query to only the one that was
     5007         * causing the error reported in ticket 55218.
     5008         */
     5009        new WP_Comment_Query(
     5010            array(
     5011                'include_unapproved' => array( 'foo@bar.mail' ),
     5012                'meta_query'         => array( array( 'key' => 'foo' ) ),
     5013                'post_id'            => $p,
     5014                'fields'             => 'ids',
     5015            )
     5016        );
     5017
     5018        global $wpdb;
     5019        $this->assertNotSame( "Column 'comment_ID' in where clause is ambiguous", $wpdb->last_error );
     5020        $this->assertStringNotContainsString( ' comment_ID ', $wpdb->last_query );
     5021    }
    49715022}
Note: See TracChangeset for help on using the changeset viewer.