Make WordPress Core


Ignore:
Timestamp:
10/19/2014 07:38:16 PM (10 years ago)
Author:
boonebgorges
Message:

Use the comment API rather than direct SQL queries in comments_template().

comments_template() is used by most themes to display a post's comments. It
shows all comments that have been approved, and also shows all pending comments
by the current visitor (as determined by the comment cookies). However, the
comments API previously had no way of querying for "all comments that are
either approved, or are unapproved but written by foo@…". The
workaround was a direct SQL query: uncached, not subject to the same filters as
other comment queries, and just generally icky.

The new include_unapproved parameter for WP_Comment_Query accepts an array
of user IDs or email addresses. Pending comments associated with users in this
array will be included in query results, regardless of the value of the 'status'
parameter. In comments_template(), we leap from direct SQL queries to
get_comments() plus `include_unapproved', striving to put right what once
went wrong.

Props boonebgorges, simonwheatley, hardy101, jesin.
Fixes #19623.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/query.php

    r29935 r29965  
    361361        $this->assertEqualSets( array( $c3, $c4 ), $comment_ids );
    362362    }
     363
     364    /**
     365     * @ticket 19623
     366     */
     367    public function test_get_comments_with_status_all() {
     368        $comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     369        $comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     370        $comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     371        $comments_approved_1 = get_comments( array( 'status' => 'all' ) );
     372
     373        $comment_ids = get_comments( array( 'fields' => 'ids' ) );
     374        $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
     375    }
     376
     377    /**
     378     * @ticket 19623
     379     */
     380    public function test_get_comments_with_include_unapproved_user_id() {
     381        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     382        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     383        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     384        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 6, 'comment_approved' => '0' ) );
     385
     386        $found = get_comments( array(
     387            'fields' => 'ids',
     388            'include_unapproved' => 1,
     389            'status' => 'approve',
     390        ) );
     391
     392        $this->assertEqualSets( array( $c1, $c2, $c3 ), $found );
     393    }
     394
     395    /**
     396     * @ticket 19623
     397     */
     398    public function test_get_comments_with_include_unapproved_user_id_array() {
     399        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     400        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     401        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     402        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 6, 'comment_approved' => '0' ) );
     403        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 8, 'comment_approved' => '0' ) );
     404
     405        $found = get_comments( array(
     406            'fields' => 'ids',
     407            'include_unapproved' => array( 1, 8 ),
     408            'status' => 'approve',
     409        ) );
     410
     411        $this->assertEqualSets( array( $c1, $c2, $c3, $c5 ), $found );
     412    }
     413
     414    /**
     415     * @ticket 19623
     416     */
     417    public function test_get_comments_with_include_unapproved_user_id_comma_separated() {
     418        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     419        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     420        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     421        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 6, 'comment_approved' => '0' ) );
     422        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 8, 'comment_approved' => '0' ) );
     423
     424        $found = get_comments( array(
     425            'fields' => 'ids',
     426            'include_unapproved' => '1,8',
     427            'status' => 'approve',
     428        ) );
     429
     430        $this->assertEqualSets( array( $c1, $c2, $c3, $c5 ), $found );
     431    }
     432
     433    /**
     434     * @ticket 19623
     435     */
     436    public function test_get_comments_with_include_unapproved_author_email() {
     437        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     438        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     439        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     440        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     441
     442        $found = get_comments( array(
     443            'fields' => 'ids',
     444            'include_unapproved' => 'foo@example.com',
     445            'status' => 'approve',
     446        ) );
     447
     448        $this->assertEqualSets( array( $c1, $c2, $c3 ), $found );
     449    }
     450
     451    /**
     452     * @ticket 19623
     453     */
     454    public function test_get_comments_with_include_unapproved_mixed_array() {
     455        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     456        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     457        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     458        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     459        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     460
     461        $found = get_comments( array(
     462            'fields' => 'ids',
     463            'include_unapproved' => array( 'foo@example.com', 4 ),
     464            'status' => 'approve',
     465        ) );
     466
     467        $this->assertEqualSets( array( $c1, $c2, $c3, $c5 ), $found );
     468    }
     469
     470    /**
     471     * @ticket 19623
     472     */
     473    public function test_get_comments_with_include_unapproved_mixed_comma_separated() {
     474        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     475        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     476        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     477        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     478        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     479
     480        $found = get_comments( array(
     481            'fields' => 'ids',
     482            'include_unapproved' => 'foo@example.com, 4',
     483            'status' => 'approve',
     484        ) );
     485
     486        $this->assertEqualSets( array( $c1, $c2, $c3, $c5 ), $found );
     487    }
    363488}
Note: See TracChangeset for help on using the changeset viewer.