Make WordPress Core

Ticket #20006: 20006.3.patch

File 20006.3.patch, 3.5 KB (added by ianmjones, 9 years ago)

This patch does not use foreach and includes unit tests.

  • src/wp-includes/comment.php

     
    674674                if ( ! empty( $post_fields ) ) {
    675675                        $join_posts_table = true;
    676676                        foreach ( $post_fields as $field_name => $field_value ) {
    677                                 $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} = %s", $field_value );
     677                                if ( is_array( $field_value ) && count( $field_value ) > 1 ) {
     678                                        $esses = array_fill( 0, count( $field_value ), '%s' );
     679                                        $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
     680                                } else {
     681                                        $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} = %s", $field_value );
     682                                }
    678683                        }
    679684                }
    680685
  • tests/phpunit/tests/comment.php

     
    4242
    4343                $this->assertSame( array(), $found );
    4444        }
     45
     46        /**
     47         * @ticket 20006
     48         */
     49        function test_get_comments_for_multiple_field_values() {
     50                register_post_type( 'post-type-1' );
     51                register_post_type( 'post-type-2' );
     52
     53                $args = array(
     54                        'post_type' => 'post-type-1',
     55                        'post_name' => 'some-slug-1',
     56                        'post_status' => 'publish',
     57                );
     58                $one = $this->factory->post->create( $args );
     59                $args['post_type'] = 'post-type-2';
     60                $args['post_name'] = 'some-slug-2';
     61                $args['post_status'] = 'draft';
     62                $two = $this->factory->post->create( $args );
     63
     64                $this->factory->comment->create_post_comments( $one, 5 );
     65                $this->factory->comment->create_post_comments( $two, 2 );
     66
     67                // Single post_type as string arg.
     68                $comments = get_comments( array( 'post_type' => 'post-type-1' ) );
     69                $this->assertEquals( 5, count( $comments ), 'single post_type as string' );
     70
     71                // Single post_type as array arg.
     72                $comments = get_comments( array( 'post_type' => array( 'post-type-2' ) ) );
     73                $this->assertEquals( 2, count( $comments ), 'single post_type as array' );
     74
     75                // Multiple post_types as array arg.
     76                $comments = get_comments( array( 'post_type' => array( 'post-type-1', 'post-type-2' ) ) );
     77                $this->assertEquals( 7, count( $comments ), 'multiple post_types as array' );
     78
     79                // Single post_name as string arg.
     80                $comments = get_comments( array( 'post_name' => 'some-slug-1' ) );
     81                $this->assertEquals( 5, count( $comments ), 'single post_name as string' );
     82
     83                // Single post_name as array arg.
     84                $comments = get_comments( array( 'post_name' => array( 'some-slug-2' ) ) );
     85                $this->assertEquals( 2, count( $comments ), 'single post_name as array' );
     86
     87                // Multiple post_names as array arg.
     88                $comments = get_comments( array( 'post_name' => array( 'some-slug-1', 'some-slug-2' ) ) );
     89                $this->assertEquals( 7, count( $comments ), 'multiple post_names as array' );
     90
     91                // Single post_status as string arg.
     92                $comments = get_comments( array( 'post_status' => 'publish' ) );
     93                $this->assertEquals( 5, count( $comments ), 'single post_status as string' );
     94
     95                // Single post_status as array arg.
     96                $comments = get_comments( array( 'post_status' => array( 'draft' ) ) );
     97                $this->assertEquals( 2, count( $comments ), 'single post_status as array' );
     98
     99                // Multiple post_statuses as array arg.
     100                $comments = get_comments( array( 'post_status' => array( 'publish', 'draft' ) ) );
     101                $this->assertEquals( 7, count( $comments ), 'multiple post_statuses as array' );
     102
     103                _unregister_post_type( 'post-type-1' );
     104                _unregister_post_type( 'post-type-2' );
     105        }
    45106}