[wp-trac] [WordPress Trac] #4712: get_pages filter may be applied twice

WordPress Trac wp-trac at lists.automattic.com
Wed Aug 8 02:13:49 GMT 2007


#4712: get_pages filter may be applied twice
----------------------+-----------------------------------------------------
 Reporter:  DD32      |       Owner:  anonymous
     Type:  defect    |      Status:  new      
 Priority:  normal    |   Milestone:           
Component:  Template  |     Version:  2.2      
 Severity:  normal    |    Keywords:           
----------------------+-----------------------------------------------------
 This is a bit hard to explain; so hang in there while reading.

 Under certain conditions(WP Object cache being enabled), the get_pages
 filter is applied twice:

 First, We get the pages from the database, And filter it.
 {{{
 $pages = $wpdb->get_results($query);
         $pages = apply_filters('get_pages', $pages, $r);
 }}}

 Thats fine, Next, We locate any children, and cache it:

 {{{
 if ( $child_of || $hierarchical )
                 $pages = & get_page_children($child_of, $pages);

         $cache[ $key ] = $pages;
         wp_cache_set( 'get_pages', $cache, 'page' );
 }}}

 Thats also fine, So we've got the results, we've filtered it, and now
 we've put it into the cache as filtered.

 Now, Next time the function runs:

 {{{
         $key = md5( serialize( $r ) );
         if ( $cache = wp_cache_get( 'get_pages', 'page' ) )
                 if ( isset( $cache[ $key ] ) )
                         return apply_filters('get_pages', $cache[ $key ],
 $r );
 }}}

 We pull the cached _filtered_ results, and then we filter it again.

 In general this isnt an issue, But if you've got a plugin adding pages to
 the get_pages function, AND the cache is enabled, then it ends up that the
 page will be added twice, Once when pulling from the DB, And then it'll be
 added again when its pulled from the cache later.

 I've attached a patch to remove the filtering upon a cache retrieval, This
 has the downside that any plugin which modifies the get_pages output will
 have to deal with its output being cached.

 I've also attached a patch to update the cache before filtering, So the
 filtering allways takes place.

-- 
Ticket URL: <http://trac.wordpress.org/ticket/4712>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list