<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][7432] trunk/bp-core: Do a better job preventing theme compat from running when current_theme_supports( 'buddypress' )</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/7432">7432</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2013-10-16 00:52:42 +0000 (Wed, 16 Oct 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>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 <a href="http://buddypress.trac.wordpress.org/ticket/4879">#4879</a>

Props r-a-y</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpcorebpcoretemplateloaderphp">trunk/bp-core/bp-core-template-loader.php</a></li>
<li><a href="#trunkbpcorebpcorethemecompatibilityphp">trunk/bp-core/bp-core-theme-compatibility.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpcorebpcoretemplateloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-template-loader.php (7431 => 7432)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-template-loader.php        2013-10-16 00:52:39 UTC (rev 7431)
+++ trunk/bp-core/bp-core-template-loader.php   2013-10-16 00:52:42 UTC (rev 7432)
</span><span class="lines">@@ -382,6 +382,11 @@
</span><span class="cx"> function bp_load_theme_functions() {
</span><span class="cx">  global $pagenow;
</span><span class="cx"> 
</span><ins>+       // do not load our custom BP functions file if theme compat is disabled
+       if ( ! bp_use_theme_compat_with_current_theme() ) {
+               return;
+       }
+
</ins><span class="cx">   // Do not include on BuddyPress deactivation
</span><span class="cx">  if ( bp_is_deactivation() )
</span><span class="cx">          return;
</span></span></pre></div>
<a id="trunkbpcorebpcorethemecompatibilityphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-theme-compatibility.php (7431 => 7432)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-theme-compatibility.php    2013-10-16 00:52:39 UTC (rev 7431)
+++ trunk/bp-core/bp-core-theme-compatibility.php       2013-10-16 00:52:42 UTC (rev 7432)
</span><span class="lines">@@ -62,33 +62,16 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * Set up the BuddyPress-specific theme compat methods
+        *
</ins><span class="cx">    * Themes shoud use this method in their constructor.
</span><span class="cx">   *
</span><del>-        * In this method, we check all types of conditions where theme compatibility
-        * should *not* run.
-        *
-        * If we pass all conditions, then we setup some additional methods to use.
-        *
</del><span class="cx">    * @since BuddyPress (1.7)
</span><span class="cx">   */
</span><span class="cx">  protected function start() {
</span><del>-
-               // If the theme supports 'buddypress', bail.
-               if ( current_theme_supports( 'buddypress' ) ) {
</del><ins>+                // Sanity check
+               if ( ! bp_use_theme_compat_with_current_theme() ) {
</ins><span class="cx">                   return;
</span><del>-
-               // If the theme doesn't support BP, do some additional checks
-               } else {
-                       // Bail if theme is a derivative of bp-default
-                       if ( in_array( 'bp-default', array( get_template(), get_stylesheet() ) ) ) {
-                               return;
-                       }
-
-                       // Bruteforce check for a BP template
-                       // Examples are clones of bp-default
-                       if ( locate_template( 'members/members-loop.php', false, false ) ) {
-                               return;
-                       }
</del><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          // Setup methods
</span><span class="lines">@@ -226,6 +209,73 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Should we use theme compat for this theme?
+ *
+ * If the current theme's need for theme compat hasn't yet been detected, we
+ * do so using bp_detect_theme_compat_with_current_theme()
+ *
+ * @since BuddyPress (1.9.0)
+ *
+ * @uses bp_detect_theme_compat_with_current_theme()
+ *
+ * @return bool True if the current theme needs theme compatibility
+ */
+function bp_use_theme_compat_with_current_theme() {
+       if ( ! isset( buddypress()->theme_compat->use_with_current_theme ) ) {
+               bp_detect_theme_compat_with_current_theme();
+       }
+
+       return buddypress()->theme_compat->use_with_current_theme;
+}
+
+/**
+ * Set our flag to determine whether theme compat should be enabled.
+ *
+ * Theme compat is disabled when a theme meets one of the following criteria:
+ * 1) It declares BP support with add_theme_support( 'buddypress' )
+ * 2) It is bp-default, or a child theme of bp-default
+ * 3) A legacy template is found at members/members-loop.php. This is a
+ *    fallback check for themes that were derived from bp-default, and have
+ *    not been updated for BP 1.7+; we make the assumption that any theme in
+ *    this category will have the members-loop.php template, and so use its
+ *    presence as an indicator that theme compatibility is not required
+ *
+ * @since BuddyPress (1.9.0)
+ *
+ * @return bool True if the current theme needs theme compatibility.
+ */
+function bp_detect_theme_compat_with_current_theme() {
+       if ( isset( buddypress()->theme_compat->use_with_current_theme ) ) {
+               return buddypress()->theme_compat->use_with_current_theme;
+       }
+
+       // theme compat enabled by default
+       $theme_compat = true;
+
+       // If the theme supports 'buddypress', bail.
+       if ( current_theme_supports( 'buddypress' ) ) {
+               $theme_compat = false;
+
+       // If the theme doesn't support BP, do some additional checks
+       } else {
+               // Bail if theme is a derivative of bp-default
+               if ( in_array( 'bp-default', array( get_template(), get_stylesheet() ) ) ) {
+                       $theme_compat = false;
+
+               // Bruteforce check for a BP template
+               // Examples are clones of bp-default
+               } else if ( locate_template( 'members/members-loop.php', false, false ) ) {
+                       $theme_compat = false;
+               }
+       }
+
+       // set a flag in the buddypress() singleton so we don't have to run this again
+       buddypress()->theme_compat->use_with_current_theme = $theme_compat;
+
+       return $theme_compat;
+}
+
+/**
</ins><span class="cx">  * Gets true/false if the current, loaded page uses theme compatibility
</span><span class="cx">  *
</span><span class="cx">  * @since BuddyPress (1.7)
</span><span class="lines">@@ -497,6 +547,11 @@
</span><span class="cx">  */
</span><span class="cx"> function bp_template_include_theme_compat( $template = '' ) {
</span><span class="cx"> 
</span><ins>+       // If the current theme doesn't need theme compat, bail at this point.
+       if ( ! bp_use_theme_compat_with_current_theme() ) {
+               return $template;
+       }
+
</ins><span class="cx">   /**
</span><span class="cx">   * Use this action to execute code that will communicate to BuddyPress's
</span><span class="cx">   * theme compatibility layer whether or not we're replacing the_content()
</span></span></pre>
</div>
</div>

</body>
</html>