Make WordPress Core

Ticket #35512: 35512.2.patch

File 35512.2.patch, 6.3 KB (added by kouratoras, 10 years ago)

Modified patch

  • src/wp-includes/class-wp-comment-query.php

     
    757757
    758758                // If any post-related query vars are passed, join the posts table.
    759759                $join_posts_table = false;
    760                 $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
     760                $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) );
    761761                $post_fields = array_filter( $plucked );
    762762
    763763                if ( ! empty( $post_fields ) ) {
     
    769769                        }
    770770                }
    771771
     772                // Handle post_type separately, to include 'any'
     773                $field_name = 'post_type';
     774                $qtypes = array();
     775                if ( ! empty( $this->query_vars[$field_name] ) ) {
     776                        $qtypes = $this->query_vars[$field_name];
     777                        if ( ! is_array( $qtypes ) ) {
     778                                $qtypes = explode(',', $qtypes);
     779                        }
     780                        $join_posts_table = true;
     781                        $ptypes = array();
     782                        if ( in_array( 'any', $qtypes ) ) {     
     783                                $ptypes = get_post_types();
     784                                if ( empty( $ptypes ) ) {
     785                                        $this->sql_clauses['where'][ $field_name ] = ' AND 1=0 ';
     786                                }
     787                                else {
     788                                        $esses = array_fill( 0, count( (array) $ptypes ), '%s' );
     789                                        $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $ptypes );
     790                                }
     791                        } else {
     792                                foreach ( get_post_types() as $ptype ) {
     793                                        if ( in_array( $ptype, $qtypes ) ) {
     794                                                $ptypes[] = $ptype;
     795                                        }
     796                                }
     797                                $esses = array_fill( 0, count( (array) $ptypes ), '%s' );
     798                                $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $ptypes );
     799                        }
     800                }
     801
     802                // Handle post_status separately, to include 'any'
     803                $field_name = 'post_status';
     804                $qstatus = array();
     805                if ( ! empty( $this->query_vars[$field_name] ) ) {
     806                        $qstatus = $this->query_vars[$field_name];
     807                        if ( ! is_array( $qstatus ) ) {
     808                                $qstatus = explode(',', $qstatus);
     809                        }
     810                        $join_posts_table = true;
     811                        $pstatus = array();
     812                        if ( in_array( 'any', $qstatus ) ) {
     813                                $pstatus = get_post_stati();
     814                                if ( empty( $pstatus ) ) {
     815                                        $this->sql_clauses['where'][ $field_name ] = ' AND 1=0 ';
     816                                }
     817                                else {
     818                                        $esses = array_fill( 0, count( (array) $pstatus ), '%s' );
     819                                        $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $pstatus );
     820                                }
     821                        } else {
     822                                foreach ( get_post_stati() as $pstati ) {
     823                                        if ( in_array( $pstati, $qstatus ) ) {
     824                                                $pstatus[] = $pstati;
     825                                        }
     826                                }
     827                                $esses = array_fill( 0, count( (array) $pstatus ), '%s' );
     828                                $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $pstatus );
     829                        }
     830                }
     831
    772832                // Comment author IDs for an IN clause.
    773833                if ( ! empty( $this->query_vars['author__in'] ) ) {
    774834                        $this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
  • tests/phpunit/tests/comment/query.php

     
    18201820        }
    18211821
    18221822        /**
     1823         * @ticket 35512
     1824         */
     1825        public function test_comments_query_any_post_type() {
     1826                register_post_type( 'post-type-1' , array ( 'exclude_from_search' => false  ) );
     1827                register_post_type( 'post-type-2' , array ( 'exclude_from_search' => false  ) );
     1828                register_post_type( 'post-type-3' , array ( 'exclude_from_search' => false  ) );
     1829
     1830                $p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
     1831                $p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
     1832                $p3 = self::factory()->post->create( array( 'post_type' => 'post-type-3' ) );
     1833
     1834                $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
     1835                $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
     1836                $c3 = self::factory()->comment->create_post_comments( $p3, 1 );
     1837
     1838                //case 1
     1839                $q = new WP_Comment_Query();
     1840                $found = $q->query( array(
     1841                        'fields' => 'ids',
     1842                        'post_type' => array( 'any', 'post-type-1' ),
     1843                ) );
     1844                $this->assertEqualSets( array_merge( $c1, $c2, $c3 ), $found );
     1845
     1846                //case 2
     1847                $q = new WP_Comment_Query();
     1848                $found = $q->query( array(
     1849                        'fields' => 'ids',
     1850                        'post_type' => array( 'any' ),
     1851                ) );
     1852                $this->assertEqualSets( array_merge( $c1, $c2, $c3 ), $found );
     1853
     1854                //case 3
     1855                $q = new WP_Comment_Query();
     1856                $found = $q->query( array(
     1857                        'fields' => 'ids',
     1858                        'post_type' => 'any',
     1859                ) );
     1860                $this->assertEqualSets( array_merge( $c1, $c2, $c3 ), $found );
     1861
     1862                //case 4
     1863                $q = new WP_Comment_Query();
     1864                $found = $q->query( array(
     1865                        'fields' => 'ids',
     1866                ) );
     1867                $this->assertEqualSets( array_merge( $c1, $c2, $c3 ), $found );
     1868
     1869                _unregister_post_type( 'post-type-1' );
     1870                _unregister_post_type( 'post-type-2' );
     1871                _unregister_post_type( 'post-type-3' );
     1872        }
     1873
     1874        /**
     1875         * @ticket 35512
     1876         */
     1877        public function test_comments_query_any_post_status() {
     1878                $p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     1879                $p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
     1880
     1881                $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
     1882                $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
     1883
     1884                //case 1
     1885                $q = new WP_Comment_Query();
     1886                $found = $q->query( array(
     1887                        'fields' => 'ids',
     1888                        'post_status' => array( 'any', 'draft' ),
     1889                ) );
     1890                $this->assertEqualSets( array_merge( $c1, $c2 ), $found );
     1891
     1892                //case 2
     1893                $q = new WP_Comment_Query();
     1894                $found = $q->query( array(
     1895                        'fields' => 'ids',
     1896                        'post_status' => array( 'any' ),
     1897                ) );
     1898                $this->assertEqualSets( array_merge( $c1, $c2 ), $found );
     1899
     1900                //case 3
     1901                $q = new WP_Comment_Query();
     1902                $found = $q->query( array(
     1903                        'fields' => 'ids',
     1904                        'post_status' => 'any',
     1905                ) );
     1906                $this->assertEqualSets( array_merge( $c1, $c2 ), $found );
     1907
     1908                //case 4
     1909                $q = new WP_Comment_Query();
     1910                $found = $q->query( array(
     1911                        'fields' => 'ids',
     1912                ) );
     1913                $this->assertEqualSets( array_merge( $c1, $c2 ), $found );
     1914        }
     1915
     1916        /**
    18231917         * @ticket 24826
    18241918         */
    18251919        public function test_comment_query_object() {