Make WordPress Core

Ticket #17019: 170195.6.diff

File 170195.6.diff, 3.6 KB (added by Mte90, 11 years ago)

patch refreshed for 4.6

Line 
1Index: /src/wp-admin/includes/media.php
2===================================================================
3--- /src/wp-admin/includes/media.php (revisione 37161)
4+++ /src/wp-admin/includes/media.php (copia locale)
5@@ -42,8 +42,6 @@
6 * @return array $tabs with gallery if post has image attachment
7 */
8 function update_gallery_tab($tabs) {
9- global $wpdb;
10-
11 if ( !isset($_REQUEST['post_id']) ) {
12 unset($tabs['gallery']);
13 return $tabs;
14@@ -51,8 +49,17 @@
15
16 $post_id = intval($_REQUEST['post_id']);
17
18- if ( $post_id )
19- $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 ) ) );
20+ if ( $post_id ) {
21+ $args = array(
22+ 'fields' => 'count',
23+ 'post_type' => 'attachment',
24+ 'post_status' => 'any',
25+ 'post_parent' => $post_id,
26+ 'suppress_filters' => false,
27+ 'query_context' => 'gallery_attachment_count',
28+ );
29+ $attachments = (int) get_posts( $args );
30+ }
31
32 if ( empty($attachments) ) {
33 unset($tabs['gallery']);
34Index: /src/wp-includes/query.php
35===================================================================
36--- /src/wp-includes/query.php (revisione 37161)
37+++ /src/wp-includes/query.php (copia locale)
38@@ -1439,6 +1439,7 @@
39 , 'fields'
40 , 'menu_order'
41 , 'embed'
42+ , 'query_context'
43 );
44
45 foreach ( $keys as $key ) {
46@@ -2613,6 +2614,9 @@
47 case 'id=>parent':
48 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
49 break;
50+ case 'count':
51+ $fields = "COUNT(*)";
52+ break;
53 default:
54 $fields = "$wpdb->posts.*";
55 }
56@@ -3568,6 +3572,12 @@
57
58 return $r;
59 }
60+
61+ if ( 'count' == $q['fields'] ) {
62+ $this->posts = array();
63+ $this->post_count = $wpdb->get_var( $this->request );
64+ return $this->post_count;
65+ }
66
67 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
68Index: tests/phpunit/tests/query.php
69===================================================================
70--- tests/phpunit/tests/query.php (revisione 37195)
71+++ tests/phpunit/tests/query.php (copia locale)
72@@ -562,4 +562,44 @@
73 $this->assertSame( 'tax1', get_query_var( 'taxonomy' ) );
74 $this->assertSame( 'term1', get_query_var( 'term' ) );
75 }
76+
77+ /**
78+ * @ticket 17019
79+ */
80+ public function test_fields_count() {
81+ $this->factory->post->create_many( 3 );
82+
83+ $q = new WP_Query( array( 'fields' => 'count' ) );
84+ $this->assertNotEquals( 3, $q->posts );
85+ $this->assertEquals( 3, $q->post_count );
86+
87+ $p = get_posts( array( 'fields' => 'count' ) );
88+ $this->assertEquals( 3, $p );
89+ }
90+
91+ public function test_query_context() {
92+ $this->factory->post->create_many( 5 );
93+
94+ $q = new WP_Query( array( 'post_type' => 'post' ) );
95+ $this->assertEquals( 5, $q->post_count );
96+
97+ add_action( 'pre_get_posts', array( $this, 'filter_pre_get_posts' ) );
98+
99+ $q = new WP_Query( array( 'query_context' => 'burrito' ) );
100+ $this->assertEquals( 3, $q->post_count );
101+
102+ $q = new WP_Query( array( 'query_context' => 'taco' ) );
103+ $this->assertEquals( 5, $q->post_count );
104+
105+ remove_action( 'pre_get_posts', array( $this, 'filter_pre_get_posts' ) );
106+
107+ $q = new WP_Query( array( 'query_context' => 'burrito' ) );
108+ $this->assertEquals( 5, $q->post_count );
109+ }
110+
111+ public function filter_pre_get_posts( &$query ) {
112+ if ( 'burrito' === $query->get( 'query_context' ) ) {
113+ $query->set( 'posts_per_page', 3 );
114+ }
115+ }
116 }