WordPress.org

Make WordPress Core

Ticket #9886: apply_filters_ref_array.2.diff

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

Use in wp-includes/query.php

  • wp-includes/plugin.php

     
    174174} 
    175175 
    176176/** 
     177 * Execute functions hooked on a specific filter hook, specifying arguments in an array. 
     178 * 
     179 * @see apply_filters() This function is identical, but the arguments passed to the 
     180 * functions hooked to <tt>$tag</tt> are supplied using an array. 
     181 * 
     182 * @package WordPress 
     183 * @subpackage Plugin 
     184 * @since 3.0 
     185 * @global array $wp_filter Stores all of the filters 
     186 * @global array $merged_filters Merges the filter hooks using this function. 
     187 * @global array $wp_current_filter stores the list of current filters with the current one last 
     188 * 
     189 * @param string $tag The name of the filter hook. 
     190 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt> 
     191 * @return mixed The filtered value after all hooked functions are applied to it. 
     192 */ 
     193function apply_filters_ref_array($tag, $args) { 
     194        global $wp_filter, $merged_filters, $wp_current_filter; 
     195 
     196        $wp_current_filter[] = $tag; 
     197 
     198        $value = $args[0]; 
     199 
     200        // Do 'all' actions first 
     201        if ( isset($wp_filter['all']) ) { 
     202                $all_args = func_get_args(); 
     203                _wp_call_all_hook($all_args); 
     204        } 
     205 
     206        if ( !isset($wp_filter[$tag]) ) { 
     207                array_pop($wp_current_filter); 
     208                return $value; 
     209        } 
     210 
     211        // Sort 
     212        if ( !isset( $merged_filters[ $tag ] ) ) { 
     213                ksort($wp_filter[$tag]); 
     214                $merged_filters[ $tag ] = true; 
     215        } 
     216 
     217        reset( $wp_filter[ $tag ] ); 
     218 
     219        do { 
     220                foreach( (array) current($wp_filter[$tag]) as $the_ ) 
     221                        if ( !is_null($the_['function']) ) 
     222                                $value = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 
     223 
     224        } while ( next($wp_filter[$tag]) !== false ); 
     225 
     226        array_pop( $wp_current_filter ); 
     227 
     228        return $value; 
     229} 
     230 
     231/** 
    177232 * Removes a function from a specified filter hook. 
    178233 * 
    179234 * This function removes a function attached to a specified filter hook. This 
  • wp-includes/query.php

     
    17651765                                        $search .= " AND ($wpdb->posts.post_password = '') "; 
    17661766                        } 
    17671767                } 
    1768                 $search = apply_filters('posts_search', $search, $this); 
     1768                $search = apply_filters_ref_array('posts_search', array($search, &$this)); 
    17691769 
    17701770                // Category stuff 
    17711771 
     
    21962196                // Apply filters on where and join prior to paging so that any 
    21972197                // manipulations to them are reflected in the paging by day queries. 
    21982198                if ( !$q['suppress_filters'] ) { 
    2199                         $where = apply_filters('posts_where', $where); 
    2200                         $join = apply_filters('posts_join', $join); 
     2199                        $where = apply_filters_ref_array('posts_where', array($where, &$this)); 
     2200                        $join = apply_filters_ref_array('posts_join', array($join, &$this)); 
    22012201                } 
    22022202 
    22032203                // Paging 
     
    22312231                        } 
    22322232 
    22332233                        if ( !$q['suppress_filters'] ) { 
    2234                                 $cjoin = apply_filters('comment_feed_join', $cjoin); 
    2235                                 $cwhere = apply_filters('comment_feed_where', $cwhere); 
    2236                                 $cgroupby = apply_filters('comment_feed_groupby', $cgroupby); 
    2237                                 $corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC'); 
    2238                                 $climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss')); 
     2234                                $cjoin = apply_filters_ref_array('comment_feed_join', array($cjoin, &$this)); 
     2235                                $cwhere = apply_filters_ref_array('comment_feed_where', array($cwhere, &$this)); 
     2236                                $cgroupby = apply_filters_ref_array('comment_feed_groupby', array($cgroupby, &$this)); 
     2237                                $corderby = apply_filters_ref_array('comment_feed_orderby', array('comment_date_gmt DESC', &$this)); 
     2238                                $climits = apply_filters_ref_array('comment_feed_limits', array('LIMIT ' . get_option('posts_per_rss'), &$this)); 
    22392239                        } 
    22402240                        $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 
    22412241                        $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 
     
    22612261                // Apply post-paging filters on where and join.  Only plugins that 
    22622262                // manipulate paging queries should use these hooks. 
    22632263                if ( !$q['suppress_filters'] ) { 
    2264                         $where = apply_filters('posts_where_paged', $where); 
    2265                         $groupby = apply_filters('posts_groupby', $groupby); 
    2266                         $join = apply_filters('posts_join_paged', $join); 
    2267                         $orderby = apply_filters('posts_orderby', $orderby); 
    2268                         $distinct = apply_filters('posts_distinct', $distinct); 
    2269                         $limits = apply_filters( 'post_limits', $limits ); 
    2270  
    2271                         $fields = apply_filters('posts_fields', $fields); 
     2264                        $where = apply_filters_ref_array('posts_where_paged', array($where, &$this)); 
     2265                        $groupby = apply_filters_ref_array('posts_groupby', array($groupby, &$this)); 
     2266                        $join = apply_filters_ref_array('posts_join_paged', array($join, &$this)); 
     2267                        $orderby = apply_filters_ref_array('posts_orderby', array($orderby, &$this)); 
     2268                        $distinct = apply_filters_ref_array('posts_distinct', array($distinct, &$this)); 
     2269                        $limits = apply_filters_ref_array('post_limits', array($limits , &$this)); 
     2270                        $fields = apply_filters_ref_array('posts_fields', array($fields, &$this)); 
    22722271                } 
    22732272 
    22742273                // Announce current selection parameters.  For use by caching plugins. 
     
    22762275 
    22772276                // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above. 
    22782277                if ( !$q['suppress_filters'] ) { 
    2279                         $where = apply_filters('posts_where_request', $where); 
    2280                         $groupby = apply_filters('posts_groupby_request', $groupby); 
    2281                         $join = apply_filters('posts_join_request', $join); 
    2282                         $orderby = apply_filters('posts_orderby_request', $orderby); 
    2283                         $distinct = apply_filters('posts_distinct_request', $distinct); 
    2284                         $fields = apply_filters('posts_fields_request', $fields); 
    2285                         $limits = apply_filters( 'post_limits_request', $limits ); 
     2278                        $where = apply_filters_ref_array('posts_where_request', array($where, &$this)); 
     2279                        $groupby = apply_filters_ref_array('posts_groupby_request', array($groupby, &$this)); 
     2280                        $join = apply_filters_ref_array('posts_join_request', array($join, &$this)); 
     2281                        $orderby = apply_filters_ref_array('posts_orderby_request', array($orderby, &$this)); 
     2282                        $distinct = apply_filters_ref_array('posts_distinct_request', array($distinct, &$this)); 
     2283                        $fields = apply_filters_ref_array('posts_fields_request', array($fields, &$this)); 
     2284                        $limits = apply_filters_ref_array('post_limits_request', array($limits , &$this)); 
    22862285                } 
    22872286 
    22882287                if ( ! empty($groupby) ) 
     
    22952294 
    22962295                $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits"; 
    22972296                if ( !$q['suppress_filters'] ) 
    2298                         $this->request = apply_filters('posts_request', $this->request); 
     2297                        $this->request = apply_filters_ref_array('posts_request', array($this->request, &$this)); 
    22992298 
    23002299                $this->posts = $wpdb->get_results($this->request); 
    23012300                // Raw results filter.  Prior to status checks. 
    23022301                if ( !$q['suppress_filters'] ) 
    2303                         $this->posts = apply_filters('posts_results', $this->posts); 
     2302                        $this->posts = apply_filters_ref_array('posts_results', array($this->posts, &$this)); 
    23042303 
    23052304                if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) { 
    2306                         $cjoin = apply_filters('comment_feed_join', ''); 
    2307                         $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'"); 
    2308                         $cgroupby = apply_filters('comment_feed_groupby', ''); 
     2305                        $cjoin = apply_filters_ref_array('comment_feed_join', array('', &$this)); 
     2306                        $cwhere = apply_filters_ref_array('comment_feed_where', array("WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this)); 
     2307                        $cgroupby = apply_filters_ref_array('comment_feed_groupby', array('', &$this)); 
    23092308                        $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 
    2310                         $corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC'); 
     2309                        $corderby = apply_filters_ref_array('comment_feed_orderby', array('comment_date_gmt DESC', &$this)); 
    23112310                        $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 
    2312                         $climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss')); 
     2311                        $climits = apply_filters_ref_array('comment_feed_limits', array('LIMIT ' . get_option('posts_per_rss'), &$this)); 
    23132312                        $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits"; 
    23142313                        $this->comments = $wpdb->get_results($comments_request); 
    23152314                        $this->comment_count = count($this->comments); 
    23162315                } 
    23172316 
    23182317                if ( !empty($limits) ) { 
    2319                         $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' ); 
     2318                        $found_posts_query = apply_filters_ref_array( 'found_posts_query', array('SELECT FOUND_ROWS()' , &$this)); 
    23202319                        $this->found_posts = $wpdb->get_var( $found_posts_query ); 
    2321                         $this->found_posts = apply_filters( 'found_posts', $this->found_posts ); 
     2320                        $this->found_posts = apply_filters_ref_array( 'found_posts', array($this->found_posts , &$this)); 
    23222321                        $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); 
    23232322                } 
    23242323 
     
    23512350                        } 
    23522351 
    23532352                        if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) ) 
    2354                                 $this->posts[0] = apply_filters('the_preview', $this->posts[0]); 
     2353                                $this->posts[0] = apply_filters_ref_array('the_preview', array($this->posts[0], &$this)); 
    23552354                } 
    23562355 
    23572356                // Put sticky posts at the top of the posts array 
     
    24002399                } 
    24012400 
    24022401                if ( !$q['suppress_filters'] ) 
    2403                         $this->posts = apply_filters('the_posts', $this->posts); 
     2402                        $this->posts = apply_filters_ref_array('the_posts', array($this->posts, &$this)); 
    24042403 
    24052404                $this->post_count = count($this->posts); 
    24062405