Ticket #9886: apply_filters_ref_array.2.diff

File apply_filters_ref_array.2.diff, 9.4 KB (added by scribu, 3 years ago)

Use in wp-includes/query.php

Line 
1Index: wp-includes/plugin.php
2===================================================================
3--- wp-includes/plugin.php      (revision 13430)
4+++ wp-includes/plugin.php      (working copy)
5@@ -174,6 +174,61 @@
6 }
7 
8 /**
9+ * Execute functions hooked on a specific filter hook, specifying arguments in an array.
10+ *
11+ * @see apply_filters() This function is identical, but the arguments passed to the
12+ * functions hooked to <tt>$tag</tt> are supplied using an array.
13+ *
14+ * @package WordPress
15+ * @subpackage Plugin
16+ * @since 3.0
17+ * @global array $wp_filter Stores all of the filters
18+ * @global array $merged_filters Merges the filter hooks using this function.
19+ * @global array $wp_current_filter stores the list of current filters with the current one last
20+ *
21+ * @param string $tag The name of the filter hook.
22+ * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
23+ * @return mixed The filtered value after all hooked functions are applied to it.
24+ */
25+function apply_filters_ref_array($tag, $args) {
26+       global $wp_filter, $merged_filters, $wp_current_filter;
27+
28+       $wp_current_filter[] = $tag;
29+
30+       $value = $args[0];
31+
32+       // Do 'all' actions first
33+       if ( isset($wp_filter['all']) ) {
34+               $all_args = func_get_args();
35+               _wp_call_all_hook($all_args);
36+       }
37+
38+       if ( !isset($wp_filter[$tag]) ) {
39+               array_pop($wp_current_filter);
40+               return $value;
41+       }
42+
43+       // Sort
44+       if ( !isset( $merged_filters[ $tag ] ) ) {
45+               ksort($wp_filter[$tag]);
46+               $merged_filters[ $tag ] = true;
47+       }
48+
49+       reset( $wp_filter[ $tag ] );
50+
51+       do {
52+               foreach( (array) current($wp_filter[$tag]) as $the_ )
53+                       if ( !is_null($the_['function']) )
54+                               $value = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
55+
56+       } while ( next($wp_filter[$tag]) !== false );
57+
58+       array_pop( $wp_current_filter );
59+
60+       return $value;
61+}
62+
63+/**
64  * Removes a function from a specified filter hook.
65  *
66  * This function removes a function attached to a specified filter hook. This
67Index: wp-includes/query.php
68===================================================================
69--- wp-includes/query.php       (revision 13430)
70+++ wp-includes/query.php       (working copy)
71@@ -1765,7 +1765,7 @@
72                                        $search .= " AND ($wpdb->posts.post_password = '') ";
73                        }
74                }
75-               $search = apply_filters('posts_search', $search, $this);
76+               $search = apply_filters_ref_array('posts_search', array($search, &$this));
77 
78                // Category stuff
79 
80@@ -2196,8 +2196,8 @@
81                // Apply filters on where and join prior to paging so that any
82                // manipulations to them are reflected in the paging by day queries.
83                if ( !$q['suppress_filters'] ) {
84-                       $where = apply_filters('posts_where', $where);
85-                       $join = apply_filters('posts_join', $join);
86+                       $where = apply_filters_ref_array('posts_where', array($where, &$this));
87+                       $join = apply_filters_ref_array('posts_join', array($join, &$this));
88                }
89 
90                // Paging
91@@ -2231,11 +2231,11 @@
92                        }
93 
94                        if ( !$q['suppress_filters'] ) {
95-                               $cjoin = apply_filters('comment_feed_join', $cjoin);
96-                               $cwhere = apply_filters('comment_feed_where', $cwhere);
97-                               $cgroupby = apply_filters('comment_feed_groupby', $cgroupby);
98-                               $corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC');
99-                               $climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss'));
100+                               $cjoin = apply_filters_ref_array('comment_feed_join', array($cjoin, &$this));
101+                               $cwhere = apply_filters_ref_array('comment_feed_where', array($cwhere, &$this));
102+                               $cgroupby = apply_filters_ref_array('comment_feed_groupby', array($cgroupby, &$this));
103+                               $corderby = apply_filters_ref_array('comment_feed_orderby', array('comment_date_gmt DESC', &$this));
104+                               $climits = apply_filters_ref_array('comment_feed_limits', array('LIMIT ' . get_option('posts_per_rss'), &$this));
105                        }
106                        $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
107                        $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
108@@ -2261,14 +2261,13 @@
109                // Apply post-paging filters on where and join.  Only plugins that
110                // manipulate paging queries should use these hooks.
111                if ( !$q['suppress_filters'] ) {
112-                       $where = apply_filters('posts_where_paged', $where);
113-                       $groupby = apply_filters('posts_groupby', $groupby);
114-                       $join = apply_filters('posts_join_paged', $join);
115-                       $orderby = apply_filters('posts_orderby', $orderby);
116-                       $distinct = apply_filters('posts_distinct', $distinct);
117-                       $limits = apply_filters( 'post_limits', $limits );
118-
119-                       $fields = apply_filters('posts_fields', $fields);
120+                       $where = apply_filters_ref_array('posts_where_paged', array($where, &$this));
121+                       $groupby = apply_filters_ref_array('posts_groupby', array($groupby, &$this));
122+                       $join = apply_filters_ref_array('posts_join_paged', array($join, &$this));
123+                       $orderby = apply_filters_ref_array('posts_orderby', array($orderby, &$this));
124+                       $distinct = apply_filters_ref_array('posts_distinct', array($distinct, &$this));
125+                       $limits = apply_filters_ref_array('post_limits', array($limits , &$this));
126+                       $fields = apply_filters_ref_array('posts_fields', array($fields, &$this));
127                }
128 
129                // Announce current selection parameters.  For use by caching plugins.
130@@ -2276,13 +2275,13 @@
131 
132                // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
133                if ( !$q['suppress_filters'] ) {
134-                       $where = apply_filters('posts_where_request', $where);
135-                       $groupby = apply_filters('posts_groupby_request', $groupby);
136-                       $join = apply_filters('posts_join_request', $join);
137-                       $orderby = apply_filters('posts_orderby_request', $orderby);
138-                       $distinct = apply_filters('posts_distinct_request', $distinct);
139-                       $fields = apply_filters('posts_fields_request', $fields);
140-                       $limits = apply_filters( 'post_limits_request', $limits );
141+                       $where = apply_filters_ref_array('posts_where_request', array($where, &$this));
142+                       $groupby = apply_filters_ref_array('posts_groupby_request', array($groupby, &$this));
143+                       $join = apply_filters_ref_array('posts_join_request', array($join, &$this));
144+                       $orderby = apply_filters_ref_array('posts_orderby_request', array($orderby, &$this));
145+                       $distinct = apply_filters_ref_array('posts_distinct_request', array($distinct, &$this));
146+                       $fields = apply_filters_ref_array('posts_fields_request', array($fields, &$this));
147+                       $limits = apply_filters_ref_array('post_limits_request', array($limits , &$this));
148                }
149 
150                if ( ! empty($groupby) )
151@@ -2295,30 +2294,30 @@
152 
153                $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
154                if ( !$q['suppress_filters'] )
155-                       $this->request = apply_filters('posts_request', $this->request);
156+                       $this->request = apply_filters_ref_array('posts_request', array($this->request, &$this));
157 
158                $this->posts = $wpdb->get_results($this->request);
159                // Raw results filter.  Prior to status checks.
160                if ( !$q['suppress_filters'] )
161-                       $this->posts = apply_filters('posts_results', $this->posts);
162+                       $this->posts = apply_filters_ref_array('posts_results', array($this->posts, &$this));
163 
164                if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
165-                       $cjoin = apply_filters('comment_feed_join', '');
166-                       $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'");
167-                       $cgroupby = apply_filters('comment_feed_groupby', '');
168+                       $cjoin = apply_filters_ref_array('comment_feed_join', array('', &$this));
169+                       $cwhere = apply_filters_ref_array('comment_feed_where', array("WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this));
170+                       $cgroupby = apply_filters_ref_array('comment_feed_groupby', array('', &$this));
171                        $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
172-                       $corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC');
173+                       $corderby = apply_filters_ref_array('comment_feed_orderby', array('comment_date_gmt DESC', &$this));
174                        $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
175-                       $climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss'));
176+                       $climits = apply_filters_ref_array('comment_feed_limits', array('LIMIT ' . get_option('posts_per_rss'), &$this));
177                        $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
178                        $this->comments = $wpdb->get_results($comments_request);
179                        $this->comment_count = count($this->comments);
180                }
181 
182                if ( !empty($limits) ) {
183-                       $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
184+                       $found_posts_query = apply_filters_ref_array( 'found_posts_query', array('SELECT FOUND_ROWS()' , &$this));
185                        $this->found_posts = $wpdb->get_var( $found_posts_query );
186-                       $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
187+                       $this->found_posts = apply_filters_ref_array( 'found_posts', array($this->found_posts , &$this));
188                        $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
189                }
190 
191@@ -2351,7 +2350,7 @@
192                        }
193 
194                        if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) )
195-                               $this->posts[0] = apply_filters('the_preview', $this->posts[0]);
196+                               $this->posts[0] = apply_filters_ref_array('the_preview', array($this->posts[0], &$this));
197                }
198 
199                // Put sticky posts at the top of the posts array
200@@ -2400,7 +2399,7 @@
201                }
202 
203                if ( !$q['suppress_filters'] )
204-                       $this->posts = apply_filters('the_posts', $this->posts);
205+                       $this->posts = apply_filters_ref_array('the_posts', array($this->posts, &$this));
206 
207                $this->post_count = count($this->posts);
208