Skip to:
Content

BuddyPress.org

Changeset 7809


Ignore:
Timestamp:
02/06/2014 06:58:17 PM (10 years ago)
Author:
boonebgorges
Message:

Store directory_pages data in the persistent cache.

This information is needed during every bootstrap, yet rarely changes, so there
should be significant performance benefit from caching it.

Invalidation takes place when one of the pages is updated, or when the bp-pages
option is changed.

Note that this change will likely be moot after migrating to the WP rewrite
API. See #4954.

See #5362

Props Denis-de-Bernardy

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-cache.php

    r7786 r7809  
    5656add_action( 'bp_first_activity_for_member',   'bp_core_clear_member_count_caches' );
    5757add_action( 'deleted_user',                   'bp_core_clear_member_count_caches' );
     58
     59/**
     60 * Clear the directory_pages cache when one of the pages is updated.
     61 *
     62 * @since BuddyPress (2.0.0)
     63 *
     64 * @param int $post_id
     65 */
     66function bp_core_clear_directory_pages_cache_page_edit( $post_id ) {
     67    if ( ! bp_is_root_blog() ) {
     68        return;
     69    }
     70
     71    // Bail if BP is not defined here
     72    if ( ! buddypress() ) {
     73        return;
     74    }
     75
     76    $page_ids = bp_core_get_directory_page_ids();
     77
     78    if ( ! in_array( $post_id, (array) $page_ids ) ) {
     79        return;
     80    }
     81
     82    wp_cache_delete( 'directory_pages', 'bp' );
     83}
     84add_action( 'save_post_page', 'bp_core_clear_directory_pages_cache_page_edit' );
     85
     86/**
     87 * Clear the directory_pages cache when the bp-pages option is updated.
     88 *
     89 * @since BuddyPress (2.0.0)
     90 */
     91function bp_core_clear_directory_pages_cache_settings_edit() {
     92    wp_cache_delete( 'directory_pages', 'bp' );
     93}
     94add_action( 'update_option_bp-pages', 'bp_core_clear_directory_pages_cache_settings_edit' );
    5895
    5996/**
  • trunk/bp-core/bp-core-functions.php

    r7800 r7809  
    358358    global $wpdb;
    359359
    360     // Set pages as standard class
    361     $pages = new stdClass;
    362 
    363     // Get pages and IDs
    364     $page_ids = bp_core_get_directory_page_ids();
    365     if ( !empty( $page_ids ) ) {
    366 
    367         // Always get page data from the root blog, except on multiblog mode, when it comes
    368         // from the current blog
    369         $posts_table_name = bp_is_multiblog_mode() ? $wpdb->posts : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'posts';
    370         $page_ids_sql     = implode( ',', wp_parse_id_list( $page_ids ) );
    371         $page_names       = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_title FROM {$posts_table_name} WHERE ID IN ({$page_ids_sql}) AND post_status = 'publish' " );
    372 
    373         foreach ( (array) $page_ids as $component_id => $page_id ) {
    374             foreach ( (array) $page_names as $page_name ) {
    375                 if ( $page_name->ID == $page_id ) {
    376                     if ( !isset( $pages->{$component_id} ) || !is_object( $pages->{$component_id} ) ) {
    377                         $pages->{$component_id} = new stdClass;
     360    // Look in cache first
     361    $pages = wp_cache_get( 'directory_pages', 'bp' );
     362
     363    if ( false === $pages ) {
     364
     365        // Set pages as standard class
     366        $pages = new stdClass;
     367
     368        // Get pages and IDs
     369        $page_ids = bp_core_get_directory_page_ids();
     370        if ( !empty( $page_ids ) ) {
     371
     372            // Always get page data from the root blog, except on multiblog mode, when it comes
     373            // from the current blog
     374            $posts_table_name = bp_is_multiblog_mode() ? $wpdb->posts : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'posts';
     375            $page_ids_sql     = implode( ',', wp_parse_id_list( $page_ids ) );
     376            $page_names       = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_title FROM {$posts_table_name} WHERE ID IN ({$page_ids_sql}) AND post_status = 'publish' " );
     377
     378            foreach ( (array) $page_ids as $component_id => $page_id ) {
     379                foreach ( (array) $page_names as $page_name ) {
     380                    if ( $page_name->ID == $page_id ) {
     381                        if ( !isset( $pages->{$component_id} ) || !is_object( $pages->{$component_id} ) ) {
     382                            $pages->{$component_id} = new stdClass;
     383                        }
     384
     385                        $pages->{$component_id}->name  = $page_name->post_name;
     386                        $pages->{$component_id}->id    = $page_name->ID;
     387                        $pages->{$component_id}->title = $page_name->post_title;
     388                        $slug[]                        = $page_name->post_name;
     389
     390                        // Get the slug
     391                        while ( $page_name->post_parent != 0 ) {
     392                            $parent                 = $wpdb->get_results( $wpdb->prepare( "SELECT post_name, post_parent FROM {$posts_table_name} WHERE ID = %d", $page_name->post_parent ) );
     393                            $slug[]                 = $parent[0]->post_name;
     394                            $page_name->post_parent = $parent[0]->post_parent;
     395                        }
     396
     397                        $pages->{$component_id}->slug = implode( '/', array_reverse( (array) $slug ) );
    378398                    }
    379399
    380                     $pages->{$component_id}->name  = $page_name->post_name;
    381                     $pages->{$component_id}->id    = $page_name->ID;
    382                     $pages->{$component_id}->title = $page_name->post_title;
    383                     $slug[]                        = $page_name->post_name;
    384 
    385                     // Get the slug
    386                     while ( $page_name->post_parent != 0 ) {
    387                         $parent                 = $wpdb->get_results( $wpdb->prepare( "SELECT post_name, post_parent FROM {$posts_table_name} WHERE ID = %d", $page_name->post_parent ) );
    388                         $slug[]                 = $parent[0]->post_name;
    389                         $page_name->post_parent = $parent[0]->post_parent;
    390                     }
    391 
    392                     $pages->{$component_id}->slug = implode( '/', array_reverse( (array) $slug ) );
     400                    unset( $slug );
    393401                }
    394 
    395                 unset( $slug );
    396402            }
    397403        }
     404
     405        wp_cache_set( 'directory_pages', $pages, 'bp' );
    398406    }
    399407
  • trunk/tests/testcases/core/functions.php

    r7426 r7809  
    256256    }
    257257
     258    /**
     259     * @group bp_core_get_directory_pages
     260     */
     261    public function test_bp_core_get_directory_pages_after_page_edit() {
     262        // Set the cache
     263        $pages = bp_core_get_directory_pages();
     264
     265        // Update one of the posts
     266        switch_to_blog( bp_get_root_blog_id() );
     267
     268        // grab the first one
     269        foreach ( $pages as $page ) {
     270            $p = $page;
     271            break;
     272        }
     273
     274        $post = get_post( $p->id );
     275        $post->post_title .= ' Foo';
     276        wp_update_post( $post );
     277
     278        restore_current_blog();
     279
     280        $this->assertFalse( wp_cache_get( 'directory_pages', 'bp' ) );
     281    }
     282
     283    /**
     284     * @group bp_core_get_directory_pages
     285     */
     286    public function test_bp_core_get_directory_pages_pages_settings_update() {
     287        // Set the cache
     288        $pages = bp_core_get_directory_pages();
     289
     290        // Mess with it but put it back
     291        $v = bp_get_option( 'bp-pages' );
     292        bp_update_option( 'bp-pages', 'foo' );
     293
     294        $this->assertFalse( wp_cache_get( 'directory_pages', 'bp' ) );
     295
     296        bp_update_option( 'bp-pages', $v );
     297    }
     298
    258299}
Note: See TracChangeset for help on using the changeset viewer.