Make WordPress Core

Changeset 36486


Ignore:
Timestamp:
02/06/2016 04:50:05 AM (8 years ago)
Author:
boonebgorges
Message:

Allow comments to be queried by 'any' post_type or post_status.

Props kouratoras.
Fixes #35512.

Location:
trunk
Files:
2 edited

Legend:

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

    r36480 r36486  
    221221     *                                                   Default empty.
    222222     *     @type int          $post_author               Post author ID to limit results by. Default empty.
    223      *     @type string       $post_status               Post status to retrieve affiliated comments for.
    224      *                                                   Default empty.
    225      *     @type string       $post_type                 Post type to retrieve affiliated comments for.
    226      *                                                   Default empty.
     223     *     @type string|array $post_status               Post status or array of post statuses to retrieve
     224     *                                                   affiliated comments for. Pass 'any' to match any value.
     225     *                                                   Default empty.
     226     *     @type string       $post_type                 Post type or array of post types to retrieve affiliated
     227     *                                                   comments for. Pass 'any' to match any value. Default empty.
    227228     *     @type string       $post_name                 Post name to retrieve affiliated comments for.
    228229     *                                                   Default empty.
     
    761762        // If any post-related query vars are passed, join the posts table.
    762763        $join_posts_table = false;
    763         $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
     764        $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) );
    764765        $post_fields = array_filter( $plucked );
    765766
     
    770771                $esses = array_fill( 0, count( (array) $field_value ), '%s' );
    771772                $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
     773            }
     774        }
     775
     776        // 'post_status' and 'post_type' are handled separately, due to the specialized behavior of 'any'.
     777        foreach ( array( 'post_status', 'post_type' ) as $field_name ) {
     778            $q_values = array();
     779            if ( ! empty( $this->query_vars[ $field_name ] ) ) {
     780                $q_values = $this->query_vars[ $field_name ];
     781                if ( ! is_array( $q_values ) ) {
     782                    $q_values = explode( ',', $q_values );
     783                }
     784
     785                // 'any' will cause the query var to be ignored.
     786                if ( in_array( 'any', $q_values, true ) || empty( $q_values ) ) {
     787                    continue;
     788                }
     789
     790                $join_posts_table = true;
     791
     792                $esses = array_fill( 0, count( $q_values ), '%s' );
     793                $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
    772794            }
    773795        }
  • trunk/tests/phpunit/tests/comment/query.php

    r36480 r36486  
    17511751
    17521752    /**
     1753     * @ticket 35512
     1754     */
     1755    public function test_post_type_any_should_override_other_post_types() {
     1756        register_post_type( 'post-type-1', array( 'exclude_from_search' => false ) );
     1757        register_post_type( 'post-type-2', array( 'exclude_from_search' => false ) );
     1758
     1759        $p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
     1760        $p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
     1761
     1762        $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
     1763        $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
     1764
     1765        $q = new WP_Comment_Query();
     1766        $found = $q->query( array(
     1767            'fields' => 'ids',
     1768            'post_type' => array( 'any', 'post-type-1' ),
     1769        ) );
     1770        $this->assertEqualSets( array_merge( $c1, $c2 ), $found );
     1771    }
     1772
     1773    /**
     1774     * @ticket 35512
     1775     */
     1776    public function test_post_type_any_as_part_of_an_array_of_post_types() {
     1777        register_post_type( 'post-type-1', array( 'exclude_from_search' => false ) );
     1778        register_post_type( 'post-type-2', array( 'exclude_from_search' => false ) );
     1779
     1780        $p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
     1781        $p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
     1782
     1783        $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
     1784        $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
     1785
     1786        $q = new WP_Comment_Query();
     1787        $found = $q->query( array(
     1788            'fields' => 'ids',
     1789            'post_type' => array( 'any' ),
     1790        ) );
     1791        $this->assertEqualSets( array_merge( $c1, $c2 ), $found );
     1792    }
     1793
     1794    /**
     1795     * @ticket 35512
     1796     */
     1797    public function test_post_status_any_should_override_other_post_statuses() {
     1798        $p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     1799        $p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
     1800
     1801        $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
     1802        $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
     1803
     1804        $q = new WP_Comment_Query();
     1805        $found = $q->query( array(
     1806            'fields' => 'ids',
     1807            'post_status' => array( 'any', 'draft' ),
     1808        ) );
     1809        $this->assertEqualSets( array_merge( $c1, $c2 ), $found );
     1810    }
     1811
     1812    /**
     1813     * @ticket 35512
     1814     */
     1815    public function test_post_status_any_as_part_of_an_array_of_post_statuses() {
     1816        $p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     1817        $p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
     1818
     1819        $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
     1820        $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
     1821
     1822        $q = new WP_Comment_Query();
     1823        $found = $q->query( array(
     1824            'fields' => 'ids',
     1825            'post_status' => array( 'any' ),
     1826        ) );
     1827        $this->assertEqualSets( array_merge( $c1, $c2 ), $found );
     1828    }
     1829
     1830    /**
    17531831     * @ticket 24826
    17541832     */
Note: See TracChangeset for help on using the changeset viewer.