Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/16/2013 12:52:42 AM (11 years ago)
Author:
boonebgorges
Message:

Do a better job preventing theme compat from running when current_theme_supports( 'buddypress' )

This changeset ensures that when a theme declares add_theme_support( 'buddypress' ),
BuddyPress will not load any of its theme compatibility layer. Previously,
'buddypress' support would prevent templates from loading, but would still
allow the buddypress-functions.php file to load in certain cases.

To this end, two new functions are introduced:

  • bp_detect_theme_compat_with_current_theme(), which centralizes the logic used by BuddyPress to detect whether the current theme will require theme compatibility (themes do not need theme compat when current_theme_supports( 'buddypress' ), when it's related to bp-default, or when members/members-loop.php is found). The function sets a global flag which can be checked later.
  • bp_use_theme_compat_with_current_theme(), which is used to check the flag set in bp_detect_theme_compat_with_current_theme().

Fixes #4879

Props r-a-y

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-theme-compatibility.php

    r7266 r7432  
    6363
    6464    /**
     65     * Set up the BuddyPress-specific theme compat methods
     66     *
    6567     * Themes shoud use this method in their constructor.
    6668     *
    67      * In this method, we check all types of conditions where theme compatibility
    68      * should *not* run.
    69      *
    70      * If we pass all conditions, then we setup some additional methods to use.
    71      *
    7269     * @since BuddyPress (1.7)
    7370     */
    7471    protected function start() {
    75 
    76         // If the theme supports 'buddypress', bail.
    77         if ( current_theme_supports( 'buddypress' ) ) {
     72        // Sanity check
     73        if ( ! bp_use_theme_compat_with_current_theme() ) {
    7874            return;
    79 
    80         // If the theme doesn't support BP, do some additional checks
    81         } else {
    82             // Bail if theme is a derivative of bp-default
    83             if ( in_array( 'bp-default', array( get_template(), get_stylesheet() ) ) ) {
    84                 return;
    85             }
    86 
    87             // Bruteforce check for a BP template
    88             // Examples are clones of bp-default
    89             if ( locate_template( 'members/members-loop.php', false, false ) ) {
    90                 return;
    91             }
    9275        }
    9376
     
    224207function bp_get_theme_compat_url() {
    225208    return apply_filters( 'bp_get_theme_compat_url', buddypress()->theme_compat->theme->url );
     209}
     210
     211/**
     212 * Should we use theme compat for this theme?
     213 *
     214 * If the current theme's need for theme compat hasn't yet been detected, we
     215 * do so using bp_detect_theme_compat_with_current_theme()
     216 *
     217 * @since BuddyPress (1.9.0)
     218 *
     219 * @uses bp_detect_theme_compat_with_current_theme()
     220 *
     221 * @return bool True if the current theme needs theme compatibility
     222 */
     223function bp_use_theme_compat_with_current_theme() {
     224    if ( ! isset( buddypress()->theme_compat->use_with_current_theme ) ) {
     225        bp_detect_theme_compat_with_current_theme();
     226    }
     227
     228    return buddypress()->theme_compat->use_with_current_theme;
     229}
     230
     231/**
     232 * Set our flag to determine whether theme compat should be enabled.
     233 *
     234 * Theme compat is disabled when a theme meets one of the following criteria:
     235 * 1) It declares BP support with add_theme_support( 'buddypress' )
     236 * 2) It is bp-default, or a child theme of bp-default
     237 * 3) A legacy template is found at members/members-loop.php. This is a
     238 *    fallback check for themes that were derived from bp-default, and have
     239 *    not been updated for BP 1.7+; we make the assumption that any theme in
     240 *    this category will have the members-loop.php template, and so use its
     241 *    presence as an indicator that theme compatibility is not required
     242 *
     243 * @since BuddyPress (1.9.0)
     244 *
     245 * @return bool True if the current theme needs theme compatibility.
     246 */
     247function bp_detect_theme_compat_with_current_theme() {
     248    if ( isset( buddypress()->theme_compat->use_with_current_theme ) ) {
     249        return buddypress()->theme_compat->use_with_current_theme;
     250    }
     251
     252    // theme compat enabled by default
     253    $theme_compat = true;
     254
     255    // If the theme supports 'buddypress', bail.
     256    if ( current_theme_supports( 'buddypress' ) ) {
     257        $theme_compat = false;
     258
     259    // If the theme doesn't support BP, do some additional checks
     260    } else {
     261        // Bail if theme is a derivative of bp-default
     262        if ( in_array( 'bp-default', array( get_template(), get_stylesheet() ) ) ) {
     263            $theme_compat = false;
     264
     265        // Bruteforce check for a BP template
     266        // Examples are clones of bp-default
     267        } else if ( locate_template( 'members/members-loop.php', false, false ) ) {
     268            $theme_compat = false;
     269        }
     270    }
     271
     272    // set a flag in the buddypress() singleton so we don't have to run this again
     273    buddypress()->theme_compat->use_with_current_theme = $theme_compat;
     274
     275    return $theme_compat;
    226276}
    227277
     
    498548function bp_template_include_theme_compat( $template = '' ) {
    499549
     550    // If the current theme doesn't need theme compat, bail at this point.
     551    if ( ! bp_use_theme_compat_with_current_theme() ) {
     552        return $template;
     553    }
     554
    500555    /**
    501556     * Use this action to execute code that will communicate to BuddyPress's
Note: See TracChangeset for help on using the changeset viewer.