WordPress.org

Make WordPress Core

Ticket #17019: media-library-attachment-filters_3.2.patch

File media-library-attachment-filters_3.2.patch, 12.0 KB (added by kevinB, 3 years ago)

Optional context arg limits application of WP_Query filters: add_filter( array('tagname', 'context'), 'myfunc' ); Core get_posts() calls pass query_context in $args

Line 
1Index: wp-admin/includes/class-wp-media-list-table.php
2===================================================================
3--- wp-admin/includes/class-wp-media-list-table.php     (revision 17614)
4+++ wp-admin/includes/class-wp-media-list-table.php     (working copy)
5@@ -46,8 +46,34 @@
6                $type_links = array();
7                $_num_posts = (array) wp_count_attachments();
8                $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
9-               if ( !isset( $total_orphans ) )
10-                               $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
11+               if ( !isset( $total_orphans ) ) {
12+                       $args = array(
13+                               'fields' => 'count',
14+                               'post_type' => 'attachment',
15+                               'post_status' => 'any',  /* If legacy filtering is applied below, pass post_status as 'inherit'. */
16+                               'post_parent' => 0,
17+                               'suppress_filters' => false,
18+                               'query_context' => 'attachment_orphans_count',
19+                       );
20+
21+                       $total_orphans = intval( get_posts( $args ) );
22+               }
23+
24                $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
25                foreach ( $matches as $type => $reals )
26                        foreach ( $reals as $real )
27Index: wp-admin/includes/media.php
28===================================================================
29--- wp-admin/includes/media.php (revision 17614)
30+++ wp-admin/includes/media.php (working copy)
31@@ -42,9 +42,31 @@
32 
33        $post_id = intval($_REQUEST['post_id']);
34 
35-       if ( $post_id )
36-               $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 ) ) );
37+       if ( $post_id ) {
38+               $args = array(
39+                       'fields' => 'count',
40+                       'post_type' => 'attachment',
41+                       'post_status' => 'any',  /* If legacy filtering is applied below, pass post_status as 'inherit'. */
42+                       'post_parent' => $post_id,
43+                       'suppress_filters' => false,
44+                       'query_context' => 'gallery_attachment_count',
45+               );
46+
47+               $attachments = intval( get_posts( $args ) );
48+       }
49+               
50        if ( empty($attachments) ) {
51                unset($tabs['gallery']);
52                return $tabs;
53Index: plugin.php
54===================================================================
55--- plugin.php  (revision 17614)
56+++ plugin.php  (working copy)
57@@ -72,6 +72,32 @@
58 }
59 
60 /**
61+ * Hooks a function or method to a specific filter action, specifying a required context for application
62+ *
63+ * @package WordPress
64+ * @subpackage Plugin
65+ * @since 3.2
66+ * @global array $wp_filter Stores all of the filters added in the form of
67+ *     wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)']']
68+ * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added, it doesn't need to run through that process.
69+ *
70+ * @param string $tag The name of the filter to hook the $function_to_add to.
71+ * @param string $context Context to require for filter application.
72+ * @param callback $function_to_add The name of the function to be called when the filter is applied.
73+ * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
74+ * @param int $accepted_args optional. The number of arguments the function accept (default 1).
75+ * @return boolean true
76+ */
77+function add_context_filter($tag, $context, $function_to_add, $priority = 10, $accepted_args = 1) {
78+       global $wp_filter, $merged_filters;
79+
80+       $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
81+       $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args, 'context' => $context);
82+       unset( $merged_filters[ $tag ] );
83+       return true;
84+}
85+
86+/**
87  * Check if any filter has been registered for a hook.
88  *
89  * @package WordPress
90@@ -190,7 +216,7 @@
91  * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
92  * @return mixed The filtered value after all hooked functions are applied to it.
93  */
94-function apply_filters_ref_array($tag, $args) {
95+function apply_filters_ref_array( $tag, $args, $context = '' ) {
96        global $wp_filter, $merged_filters, $wp_current_filter;
97 
98        $wp_current_filter[] = $tag;
99@@ -216,7 +242,7 @@
100 
101        do {
102                foreach( (array) current($wp_filter[$tag]) as $the_ )
103-                       if ( !is_null($the_['function']) )
104+                       if ( !is_null($the_['function']) && ( empty($the_['context']) || ( $context == $the_['context'] ) ) )
105                                $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
106 
107        } while ( next($wp_filter[$tag]) !== false );
108Index: wp-includes/query.php
109===================================================================
110--- wp-includes/query.php       (revision 17614)
111+++ wp-includes/query.php       (working copy)
112@@ -1928,6 +1928,8 @@
113 
114                if ( !isset($q['suppress_filters']) )
115                        $q['suppress_filters'] = false;
116+               
117+               $context = isset( $q['query_context'] ) ? $q['query_context'] : '';
118 
119                if ( !isset($q['cache_results']) ) {
120                        if ( $_wp_using_ext_object_cache )
121@@ -2001,6 +2003,9 @@
122                        case 'id=>parent':
123                                $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
124                                break;
125+                       case 'count':
126+                               $fields = "COUNT(*)";
127+                               break;
128                        default:
129                                $fields = "$wpdb->posts.*";
130                }
131@@ -2167,7 +2172,7 @@
132                }
133 
134                // Allow plugins to contextually add/remove/modify the search section of the database query
135-               $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
136+               $search = apply_filters_ref_array('posts_search', array( $search, &$this ), $context );
137 
138                // Taxonomies
139                if ( !$this->is_singular ) {
140@@ -2483,8 +2488,8 @@
141                // Apply filters on where and join prior to paging so that any
142                // manipulations to them are reflected in the paging by day queries.
143                if ( !$q['suppress_filters'] ) {
144-                       $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
145-                       $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
146+                       $where = apply_filters_ref_array('posts_where', array( $where, &$this ), $context );
147+                       $join = apply_filters_ref_array('posts_join', array( $join, &$this ), $context );
148                }
149 
150                // Paging
151@@ -2549,16 +2554,16 @@
152                // Apply post-paging filters on where and join.  Only plugins that
153                // manipulate paging queries should use these hooks.
154                if ( !$q['suppress_filters'] ) {
155-                       $where          = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
156-                       $groupby        = apply_filters_ref_array( 'posts_groupby',             array( $groupby, &$this ) );
157-                       $join           = apply_filters_ref_array( 'posts_join_paged',  array( $join, &$this ) );
158-                       $orderby        = apply_filters_ref_array( 'posts_orderby',             array( $orderby, &$this ) );
159-                       $distinct       = apply_filters_ref_array( 'posts_distinct',    array( $distinct, &$this ) );
160-                       $limits         = apply_filters_ref_array( 'post_limits',               array( $limits, &$this ) );
161-                       $fields         = apply_filters_ref_array( 'posts_fields',              array( $fields, &$this ) );
162+                       $where          = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ), $context );
163+                       $groupby        = apply_filters_ref_array( 'posts_groupby',             array( $groupby, &$this ), $context );
164+                       $join           = apply_filters_ref_array( 'posts_join_paged',  array( $join, &$this ), $context );
165+                       $orderby        = apply_filters_ref_array( 'posts_orderby',             array( $orderby, &$this ), $context );
166+                       $distinct       = apply_filters_ref_array( 'posts_distinct',    array( $distinct, &$this ), $context );
167+                       $limits         = apply_filters_ref_array( 'post_limits',               array( $limits, &$this ), $context );
168+                       $fields         = apply_filters_ref_array( 'posts_fields',              array( $fields, &$this ), $context );
169 
170                        // Filter all clauses at once, for convenience
171-                       $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
172+                       $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ), $context );
173                        foreach ( $pieces as $piece )
174                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
175                }
176@@ -2568,16 +2573,16 @@
177 
178                // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
179                if ( !$q['suppress_filters'] ) {
180-                       $where          = apply_filters_ref_array( 'posts_where_request',               array( $where, &$this ) );
181-                       $groupby        = apply_filters_ref_array( 'posts_groupby_request',             array( $groupby, &$this ) );
182-                       $join           = apply_filters_ref_array( 'posts_join_request',                array( $join, &$this ) );
183-                       $orderby        = apply_filters_ref_array( 'posts_orderby_request',             array( $orderby, &$this ) );
184-                       $distinct       = apply_filters_ref_array( 'posts_distinct_request',    array( $distinct, &$this ) );
185-                       $fields         = apply_filters_ref_array( 'posts_fields_request',              array( $fields, &$this ) );
186-                       $limits         = apply_filters_ref_array( 'post_limits_request',               array( $limits, &$this ) );
187+                       $where          = apply_filters_ref_array( 'posts_where_request',               array( $where, &$this ), $context );
188+                       $groupby        = apply_filters_ref_array( 'posts_groupby_request',             array( $groupby, &$this ), $context );
189+                       $join           = apply_filters_ref_array( 'posts_join_request',                array( $join, &$this ), $context );
190+                       $orderby        = apply_filters_ref_array( 'posts_orderby_request',             array( $orderby, &$this ), $context );
191+                       $distinct       = apply_filters_ref_array( 'posts_distinct_request',    array( $distinct, &$this ), $context );
192+                       $fields         = apply_filters_ref_array( 'posts_fields_request',              array( $fields, &$this ), $context );
193+                       $limits         = apply_filters_ref_array( 'post_limits_request',               array( $limits, &$this ), $context );
194 
195                        // Filter all clauses at once, for convenience
196-                       $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
197+                       $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ), $context );
198                        foreach ( $pieces as $piece )
199                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
200                }
201@@ -2593,7 +2598,7 @@
202 
203                $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
204                if ( !$q['suppress_filters'] )
205-                       $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
206+                       $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ), $context );
207 
208                if ( 'ids' == $q['fields'] ) {
209                        $this->posts = $wpdb->get_col($this->request);
210@@ -2610,12 +2615,18 @@
211 
212                        return $r;
213                }
214+               
215+               if ( 'count' == $q['fields'] ) {
216+                       $this->posts = $wpdb->get_var($this->request);
217 
218+                       return $this->posts;
219+               }
220+
221                $this->posts = $wpdb->get_results($this->request);
222 
223                // Raw results filter.  Prior to status checks.
224                if ( !$q['suppress_filters'] )
225-                       $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
226+                       $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ), $context );
227 
228                if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
229                        $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
230@@ -2631,9 +2642,9 @@
231                }
232 
233                if ( !$q['no_found_rows'] && !empty($limits) ) {
234-                       $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
235+                       $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ), $context );
236                        $this->found_posts = $wpdb->get_var( $found_posts_query );
237-                       $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
238+                       $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ), $context );
239                        $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
240                }
241 
242@@ -2720,7 +2731,7 @@
243                }
244 
245                if ( !$q['suppress_filters'] )
246-                       $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
247+                       $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ), $context );
248 
249                $this->post_count = count($this->posts);
250