Make WordPress Core

Ticket #17019: 17019.5.diff

File 17019.5.diff, 4.5 KB (added by wonderboymusic, 10 years ago)
  • src/wp-admin/includes/class-wp-media-list-table.php

     
    4242        }
    4343
    4444        function get_views() {
    45                 global $wpdb, $post_mime_types, $avail_post_mime_types;
     45                global $post_mime_types, $avail_post_mime_types;
    4646
    4747                $type_links = array();
    4848                $_num_posts = (array) wp_count_attachments();
    4949                $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
    50                 $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
     50
     51                $args = array(
     52                        'fields' => 'count',
     53                        'post_type' => 'attachment',
     54                        'post_status' => 'any',
     55                        'post_parent' => 0,
     56                        'suppress_filters' => false,
     57                        'query_context' => 'attachment_orphans_count',
     58                );
     59                $total_orphans = (int) get_posts( $args );
     60
    5161                $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
    5262                foreach ( $matches as $type => $reals )
    5363                        foreach ( $reals as $real )
  • src/wp-admin/includes/media.php

     
    4040 * @return array $tabs with gallery if post has image attachment
    4141 */
    4242function update_gallery_tab($tabs) {
    43         global $wpdb;
    44 
    4543        if ( !isset($_REQUEST['post_id']) ) {
    4644                unset($tabs['gallery']);
    4745                return $tabs;
     
    4947
    5048        $post_id = intval($_REQUEST['post_id']);
    5149
    52         if ( $post_id )
    53                 $attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );
     50        if ( $post_id ) {
     51                $args = array(
     52                        'fields' => 'count',
     53                        'post_type' => 'attachment',
     54                        'post_status' => 'any',
     55                        'post_parent' => $post_id,
     56                        'suppress_filters' => false,
     57                        'query_context' => 'gallery_attachment_count',
     58                );
     59                $attachments = (int) get_posts( $args );
     60        }
    5461
    5562        if ( empty($attachments) ) {
    5663                unset($tabs['gallery']);
  • src/wp-includes/query.php

     
    14241424                        , 'sentence'
    14251425                        , 'fields'
    14261426                        , 'menu_order'
     1427                        , 'query_context'
    14271428                );
    14281429
    14291430                foreach ( $keys as $key ) {
     
    22722273                        case 'id=>parent':
    22732274                                $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
    22742275                                break;
     2276                        case 'count':
     2277                                $fields = "COUNT(*)";
     2278                                break;
    22752279                        default:
    22762280                                $fields = "$wpdb->posts.*";
    22772281                }
     
    29252929                        return $r;
    29262930                }
    29272931
     2932                if ( 'count' == $q['fields'] ) {
     2933                        $this->posts = array();
     2934                        $this->post_count = $wpdb->get_var( $this->request );
     2935                        return $this->post_count;
     2936                }
     2937
    29282938                $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
    29292939                $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
    29302940
  • tests/phpunit/tests/query.php

     
    7878                }
    7979
    8080        }
     81
     82        function test_fields_count() {
     83                $this->factory->post->create_many( 3 );
     84
     85                $q = new WP_Query( array( 'fields' => 'count' ) );
     86                $this->assertNotEquals( 3, $q->posts );
     87                $this->assertEquals( 3, $q->post_count );
     88
     89                $p = get_posts( array( 'fields' => 'count' ) );
     90                $this->assertEquals( 3, $p );
     91        }
     92
     93        function test_query_context() {
     94                $this->factory->post->create_many( 5 );
     95
     96                $q = new WP_Query( array( 'post_type' => 'post' ) );
     97                $this->assertEquals( 5, $q->post_count );
     98
     99                add_action( 'pre_get_posts', array( $this, 'filter_pre_get_posts' ) );
     100
     101                $q = new WP_Query( array( 'query_context' => 'burrito' ) );
     102                $this->assertEquals( 3, $q->post_count );
     103
     104                $q = new WP_Query( array( 'query_context' => 'taco' ) );
     105                $this->assertEquals( 5, $q->post_count );
     106
     107                remove_action( 'pre_get_posts', array( $this, 'filter_pre_get_posts' ) );
     108
     109                $q = new WP_Query( array( 'query_context' => 'burrito' ) );
     110                $this->assertEquals( 5, $q->post_count );
     111        }
     112
     113        function filter_pre_get_posts( &$query ) {
     114                if ( 'burrito' === $query->get( 'query_context' ) ) {
     115                        $query->set( 'posts_per_page', 3 );
     116                }
     117        }
    81118}
     119 No newline at end of file