Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/05/2016 04:26:30 AM (8 years ago)
Author:
boonebgorges
Message:

Move bp-core classes to their own files.

See #6870.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-core-theme-compat.php

    r10515 r10518  
    11<?php
    22/**
    3  * BuddyPress Core Theme Compatibility.
     3 * BuddyPress Core Theme Compatibility Base Class.
    44 *
    55 * @package BuddyPress
     
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
    12 
    13 /** Theme Compat **************************************************************/
    14 
    15 /**
    16  * What follows is an attempt at intercepting the natural page load process
    17  * to replace the_content() with the appropriate BuddyPress content.
    18  *
    19  * To do this, BuddyPress does several direct manipulations of global variables
    20  * and forces them to do what they are not supposed to be doing.
    21  *
    22  * Don't try anything you're about to witness here, at home. Ever.
    23  */
    24 
    25 /** Base Class ****************************************************************/
    2612
    2713/**
     
    133119    }
    134120}
    135 
    136 /** Functions *****************************************************************/
    137 
    138 /**
    139  * Set up the default theme compat theme.
    140  *
    141  * @since 1.7.0
    142  *
    143  * @param string $theme Optional. The unique ID identifier of a theme package.
    144  */
    145 function bp_setup_theme_compat( $theme = '' ) {
    146     $bp = buddypress();
    147 
    148     // Make sure theme package is available, set to default if not.
    149     if ( ! isset( $bp->theme_compat->packages[$theme] ) || ! is_a( $bp->theme_compat->packages[$theme], 'BP_Theme_Compat' ) ) {
    150         $theme = 'legacy';
    151     }
    152 
    153     // Set the active theme compat theme.
    154     $bp->theme_compat->theme = $bp->theme_compat->packages[$theme];
    155 }
    156 
    157 /**
    158  * Get the ID of the theme package being used.
    159  *
    160  * This can be filtered or set manually. Tricky theme authors can override the
    161  * default and include their own BuddyPress compatibility layers for their themes.
    162  *
    163  * @since 1.7.0
    164  *
    165  * @uses apply_filters()
    166  *
    167  * @return string ID of the theme package in use.
    168  */
    169 function bp_get_theme_compat_id() {
    170 
    171     /**
    172      * Filters the ID of the theme package being used.
    173      *
    174      * @since 1.7.0
    175      *
    176      * @param string $id ID of the theme package in use.
    177      */
    178     return apply_filters( 'bp_get_theme_compat_id', buddypress()->theme_compat->theme->id );
    179 }
    180 
    181 /**
    182  * Get the name of the theme package being used.
    183  *
    184  * This can be filtered or set manually. Tricky theme authors can override the
    185  * default and include their own BuddyPress compatibility layers for their themes.
    186  *
    187  * @since 1.7.0
    188  *
    189  * @uses apply_filters()
    190  *
    191  * @return string Name of the theme package currently in use.
    192  */
    193 function bp_get_theme_compat_name() {
    194 
    195     /**
    196      * Filters the name of the theme package being used.
    197      *
    198      * @since 1.7.0
    199      *
    200      * @param string $name Name of the theme package in use.
    201      */
    202     return apply_filters( 'bp_get_theme_compat_name', buddypress()->theme_compat->theme->name );
    203 }
    204 
    205 /**
    206  * Get the version of the theme package being used.
    207  *
    208  * This can be filtered or set manually. Tricky theme authors can override the
    209  * default and include their own BuddyPress compatibility layers for their themes.
    210  *
    211  * @since 1.7.0
    212  *
    213  * @uses apply_filters()
    214  *
    215  * @return string The version string of the theme package currently in use.
    216  */
    217 function bp_get_theme_compat_version() {
    218 
    219     /**
    220      * Filters the version of the theme package being used.
    221      *
    222      * @since 1.7.0
    223      *
    224      * @param string $version The version string of the theme package in use.
    225      */
    226     return apply_filters( 'bp_get_theme_compat_version', buddypress()->theme_compat->theme->version );
    227 }
    228 
    229 /**
    230  * Get the absolute path of the theme package being used.
    231  *
    232  * Or set manually. Tricky theme authors can override the default and include
    233  * their own BuddyPress compatibility layers for their themes.
    234  *
    235  * @since 1.7.0
    236  *
    237  * @uses apply_filters()
    238  *
    239  * @return string The absolute path of the theme package currently in use.
    240  */
    241 function bp_get_theme_compat_dir() {
    242 
    243     /**
    244      * Filters the absolute path of the theme package being used.
    245      *
    246      * @since 1.7.0
    247      *
    248      * @param string $dir The absolute path of the theme package in use.
    249      */
    250     return apply_filters( 'bp_get_theme_compat_dir', buddypress()->theme_compat->theme->dir );
    251 }
    252 
    253 /**
    254  * Get the URL of the theme package being used.
    255  *
    256  * This can be filtered, or set manually. Tricky theme authors can override
    257  * the default and include their own BuddyPress compatibility layers for their
    258  * themes.
    259  *
    260  * @since 1.7.0
    261  *
    262  * @uses apply_filters()
    263  *
    264  * @return string URL of the theme package currently in use.
    265  */
    266 function bp_get_theme_compat_url() {
    267 
    268     /**
    269      * Filters the URL of the theme package being used.
    270      *
    271      * @since 1.7.0
    272      *
    273      * @param string $url URL of the theme package in use.
    274      */
    275     return apply_filters( 'bp_get_theme_compat_url', buddypress()->theme_compat->theme->url );
    276 }
    277 
    278 /**
    279  * Should we use theme compat for this theme?
    280  *
    281  * If the current theme's need for theme compat hasn't yet been detected, we
    282  * do so using bp_detect_theme_compat_with_current_theme().
    283  *
    284  * @since 1.9.0
    285  *
    286  * @uses bp_detect_theme_compat_with_current_theme()
    287  *
    288  * @return bool True if the current theme needs theme compatibility.
    289  */
    290 function bp_use_theme_compat_with_current_theme() {
    291     if ( ! isset( buddypress()->theme_compat->use_with_current_theme ) ) {
    292         bp_detect_theme_compat_with_current_theme();
    293     }
    294 
    295     /**
    296      * Filters whether or not to use theme compat for the active theme.
    297      *
    298      * @since 1.9.0
    299      *
    300      * @param bool $use_with_current_theme True if the current theme needs theme compatibility.
    301      */
    302     return apply_filters( 'bp_use_theme_compat_with_current_theme', buddypress()->theme_compat->use_with_current_theme );
    303 }
    304 
    305 /**
    306  * Set our flag to determine whether theme compat should be enabled.
    307  *
    308  * Theme compat is disabled when a theme meets one of the following criteria:
    309  * 1) It declares BP support with add_theme_support( 'buddypress' )
    310  * 2) It is bp-default, or a child theme of bp-default
    311  * 3) A legacy template is found at members/members-loop.php. This is a
    312  *    fallback check for themes that were derived from bp-default, and have
    313  *    not been updated for BP 1.7+; we make the assumption that any theme in
    314  *    this category will have the members-loop.php template, and so use its
    315  *    presence as an indicator that theme compatibility is not required
    316  *
    317  * @since 1.9.0
    318  *
    319  * @return bool True if the current theme needs theme compatibility.
    320  */
    321 function bp_detect_theme_compat_with_current_theme() {
    322     if ( isset( buddypress()->theme_compat->use_with_current_theme ) ) {
    323         return buddypress()->theme_compat->use_with_current_theme;
    324     }
    325 
    326     // Theme compat enabled by default.
    327     $theme_compat = true;
    328 
    329     // If the theme supports 'buddypress', bail.
    330     if ( current_theme_supports( 'buddypress' ) ) {
    331         $theme_compat = false;
    332 
    333     // If the theme doesn't support BP, do some additional checks.
    334     } else {
    335         // Bail if theme is a derivative of bp-default.
    336         if ( in_array( 'bp-default', array( get_template(), get_stylesheet() ) ) ) {
    337             $theme_compat = false;
    338 
    339         // Brute-force check for a BP template.
    340         // Examples are clones of bp-default.
    341         } elseif ( locate_template( 'members/members-loop.php', false, false ) ) {
    342             $theme_compat = false;
    343         }
    344     }
    345 
    346     // Set a flag in the buddypress() singleton so we don't have to run this again.
    347     buddypress()->theme_compat->use_with_current_theme = $theme_compat;
    348 
    349     return $theme_compat;
    350 }
    351 
    352 /**
    353  * Is the current page using theme compatibility?
    354  *
    355  * @since 1.7.0
    356  *
    357  * @return bool True if the current page uses theme compatibility.
    358  */
    359 function bp_is_theme_compat_active() {
    360     $bp = buddypress();
    361 
    362     if ( empty( $bp->theme_compat->active ) ) {
    363         return false;
    364     }
    365 
    366     return $bp->theme_compat->active;
    367 }
    368 
    369 /**
    370  * Set the flag that tells whether the current page is using theme compatibility.
    371  *
    372  * @since 1.7.0
    373  *
    374  * @param bool $set True to set the flag to true, false to set it to false.
    375  * @return bool Returns the value of $set.
    376  */
    377 function bp_set_theme_compat_active( $set = true ) {
    378     buddypress()->theme_compat->active = $set;
    379 
    380     return (bool) buddypress()->theme_compat->active;
    381 }
    382 
    383 /**
    384  * Set the theme compat templates global.
    385  *
    386  * Stash possible template files for the current query. Useful if plugins want
    387  * to override them, or see what files are being scanned for inclusion.
    388  *
    389  * @since 1.7.0
    390  *
    391  * @param array $templates The template stack.
    392  * @return array The template stack (value of $templates).
    393  */
    394 function bp_set_theme_compat_templates( $templates = array() ) {
    395     buddypress()->theme_compat->templates = $templates;
    396 
    397     return buddypress()->theme_compat->templates;
    398 }
    399 
    400 /**
    401  * Set the theme compat template global.
    402  *
    403  * Stash the template file for the current query. Useful if plugins want
    404  * to override it, or see what file is being included.
    405  *
    406  * @since 1.7.0
    407  *
    408  * @param string $template The template currently in use.
    409  * @return string The template currently in use (value of $template).
    410  */
    411 function bp_set_theme_compat_template( $template = '' ) {
    412     buddypress()->theme_compat->template = $template;
    413 
    414     return buddypress()->theme_compat->template;
    415 }
    416 
    417 /**
    418  * Set the theme compat original_template global.
    419  *
    420  * Stash the original template file for the current query. Useful for checking
    421  * if BuddyPress was able to find a more appropriate template.
    422  *
    423  * @since 1.7.0
    424  *
    425  * @param string $template The template originally selected by WP.
    426  * @return string The template originally selected by WP (value of $template).
    427  */
    428 function bp_set_theme_compat_original_template( $template = '' ) {
    429     buddypress()->theme_compat->original_template = $template;
    430 
    431     return buddypress()->theme_compat->original_template;
    432 }
    433 
    434 /**
    435  * Set a theme compat feature
    436  *
    437  * @since 2.4.0
    438  *
    439  * @param  string $theme_id The theme id (eg: legacy).
    440  * @param  array  $feature  An associative array (eg: array( name => 'feature_name', 'settings' => array() )).
    441  */
    442 function bp_set_theme_compat_feature( $theme_id, $feature = array() ) {
    443     if ( empty( $theme_id ) || empty( $feature['name'] ) ) {
    444         return;
    445     }
    446 
    447     // Get BuddyPress instance.
    448     $bp = buddypress();
    449 
    450     // Get current theme compat theme.
    451     $theme_compat_theme = $bp->theme_compat->theme;
    452 
    453     // Bail if the Theme Compat theme is not in use.
    454     if ( $theme_id !== bp_get_theme_compat_id() ) {
    455         return;
    456     }
    457 
    458     $features = $theme_compat_theme->__get( 'features' );
    459     if ( empty( $features ) ) {
    460         $features = array();
    461     }
    462 
    463     // Bail if the feature is already registered or no settings were provided.
    464     if ( isset( $features[ $feature['name'] ] ) || empty( $feature['settings'] ) ) {
    465         return;
    466     }
    467 
    468     // Add the feature.
    469     $features[ $feature['name'] ] = (object) $feature['settings'];
    470 
    471     // The feature is attached to components.
    472     if ( isset( $features[ $feature['name'] ]->components ) ) {
    473         // Set the feature for each concerned component.
    474         foreach ( (array) $features[ $feature['name'] ]->components as $component ) {
    475             // The xProfile component is specific.
    476             if ( 'xprofile' === $component ) {
    477                 $component = 'profile';
    478             }
    479 
    480             if ( isset( $bp->{$component} ) ) {
    481                 if ( isset( $bp->{$component}->features ) ) {
    482                     $bp->{$component}->features[] = $feature['name'];
    483                 } else {
    484                     $bp->{$component}->features = array( $feature['name'] );
    485                 }
    486             }
    487         }
    488     }
    489 
    490     // Finally update the theme compat features.
    491     $theme_compat_theme->__set( 'features', $features );
    492 }
    493 
    494 /**
    495  * Get a theme compat feature
    496  *
    497  * @since 2.4.0
    498  *
    499  * @param  string $feature The feature (eg: cover_image).
    500  * @return object          The feature settings.
    501  */
    502 function bp_get_theme_compat_feature( $feature = '' ) {
    503     // Get current theme compat theme.
    504     $theme_compat_theme = buddypress()->theme_compat->theme;
    505 
    506     // Get features.
    507     $features = $theme_compat_theme->__get( 'features' );
    508 
    509     if ( ! isset( $features[ $feature ] ) ) {
    510         return false;
    511     }
    512 
    513     return $features[ $feature ];
    514 }
    515 
    516 /**
    517  * Setup the theme's features
    518  *
    519  * Note: BP Legacy's buddypress-functions.php is not loaded in WP Administration
    520  * as it's loaded using bp_locate_template(). That's why this function is here.
    521  *
    522  * @since 2.4.0
    523  *
    524  * @global string $content_width the content width of the theme
    525  */
    526 function bp_register_theme_compat_default_features() {
    527     global $content_width;
    528 
    529     // Do not set up default features on deactivation.
    530     if ( bp_is_deactivation() ) {
    531         return;
    532     }
    533 
    534     // If the current theme doesn't need theme compat, bail at this point.
    535     if ( ! bp_use_theme_compat_with_current_theme() ) {
    536         return;
    537     }
    538 
    539     // Make sure BP Legacy is the Theme Compat in use.
    540     if ( 'legacy' !== bp_get_theme_compat_id() ) {
    541         return;
    542     }
    543 
    544     // Get the theme.
    545     $current_theme = wp_get_theme();
    546     $theme_handle  = $current_theme->get_stylesheet();
    547     $parent        = $current_theme->parent();
    548 
    549     if ( $parent ) {
    550         $theme_handle = $parent->get_stylesheet();
    551     }
    552 
    553     /**
    554      * Since Companion stylesheets, the $content_width is smaller
    555      * than the width used by BuddyPress, so we need to manually set the
    556      * content width for the concerned themes.
    557      *
    558      * Example: array( stylesheet => content width used by BuddyPress )
    559      */
    560     $bp_content_widths = array(
    561         'twentyfifteen'  => 1300,
    562         'twentyfourteen' => 955,
    563         'twentythirteen' => 890,
    564     );
    565 
    566     // Default values.
    567     $bp_content_width = (int) $content_width;
    568     $bp_handle        = 'bp-legacy-css';
    569 
    570     // Specific to themes having companion stylesheets.
    571     if ( isset( $bp_content_widths[ $theme_handle ] ) ) {
    572         $bp_content_width = $bp_content_widths[ $theme_handle ];
    573         $bp_handle        = 'bp-' . $theme_handle;
    574     }
    575 
    576     if ( is_rtl() ) {
    577         $bp_handle .= '-rtl';
    578     }
    579 
    580     $top_offset    = 150;
    581     $avatar_height = apply_filters( 'bp_core_avatar_full_height', $top_offset );
    582 
    583     if ( $avatar_height > $top_offset ) {
    584         $top_offset = $avatar_height;
    585     }
    586 
    587     bp_set_theme_compat_feature( 'legacy', array(
    588         'name'     => 'cover_image',
    589         'settings' => array(
    590             'components'   => array( 'xprofile', 'groups' ),
    591             'width'        => $bp_content_width,
    592             'height'       => $top_offset + round( $avatar_height / 2 ),
    593             'callback'     => 'bp_legacy_theme_cover_image',
    594             'theme_handle' => $bp_handle,
    595         ),
    596     ) );
    597 }
    598 
    599 /**
    600  * Check whether a given template is the one that WP originally selected to display current page.
    601  *
    602  * @since 1.7.0
    603  *
    604  * @param string $template The template name to check.
    605  * @return bool True if the value of $template is the same as the
    606  *              "original_template" originally selected by WP. Otherwise false.
    607  */
    608 function bp_is_theme_compat_original_template( $template = '' ) {
    609     $bp = buddypress();
    610 
    611     if ( empty( $bp->theme_compat->original_template ) ) {
    612         return false;
    613     }
    614 
    615     return (bool) ( $bp->theme_compat->original_template == $template );
    616 }
    617 
    618 /**
    619  * Register a new BuddyPress theme package in the active theme packages array.
    620  *
    621  * For an example of how this function is used, see:
    622  * {@link BuddyPress::register_theme_packages()}.
    623  *
    624  * @since 1.7.0
    625  *
    626  * @see BP_Theme_Compat for a description of the $theme parameter arguments.
    627  *
    628  * @param array $theme    See {@link BP_Theme_Compat}.
    629  * @param bool  $override If true, overrides whatever package is currently set.
    630  *                        Default: true.
    631  */
    632 function bp_register_theme_package( $theme = array(), $override = true ) {
    633 
    634     // Create new BP_Theme_Compat object from the $theme array.
    635     if ( is_array( $theme ) ) {
    636         $theme = new BP_Theme_Compat( $theme );
    637     }
    638 
    639     // Bail if $theme isn't a proper object.
    640     if ( ! is_a( $theme, 'BP_Theme_Compat' ) ) {
    641         return;
    642     }
    643 
    644     // Load up BuddyPress.
    645     $bp = buddypress();
    646 
    647     // Only set if the theme package was not previously registered or if the
    648     // override flag is set.
    649     if ( empty( $bp->theme_compat->packages[$theme->id] ) || ( true === $override ) ) {
    650         $bp->theme_compat->packages[$theme->id] = $theme;
    651     }
    652 }
    653 
    654 /**
    655  * Populate various WordPress globals with dummy data to prevent errors.
    656  *
    657  * This dummy data is necessary because theme compatibility essentially fakes
    658  * WordPress into thinking that there is content where, in fact, there is none
    659  * (at least, no WordPress post content). By providing dummy data, we ensure
    660  * that template functions - things like is_page() - don't throw errors.
    661  *
    662  * @since 1.7.0
    663  *
    664  * @global WP_Query $wp_query WordPress database access object.
    665  * @global object $post Current post object.
    666  *
    667  * @param array $args Array of optional arguments. Arguments parallel the properties
    668  *                    of {@link WP_Post}; see that class for more details.
    669  */
    670 function bp_theme_compat_reset_post( $args = array() ) {
    671     global $wp_query, $post;
    672 
    673     // Switch defaults if post is set.
    674     if ( isset( $wp_query->post ) ) {
    675         $dummy = wp_parse_args( $args, array(
    676             'ID'                    => $wp_query->post->ID,
    677             'post_status'           => $wp_query->post->post_status,
    678             'post_author'           => $wp_query->post->post_author,
    679             'post_parent'           => $wp_query->post->post_parent,
    680             'post_type'             => $wp_query->post->post_type,
    681             'post_date'             => $wp_query->post->post_date,
    682             'post_date_gmt'         => $wp_query->post->post_date_gmt,
    683             'post_modified'         => $wp_query->post->post_modified,
    684             'post_modified_gmt'     => $wp_query->post->post_modified_gmt,
    685             'post_content'          => $wp_query->post->post_content,
    686             'post_title'            => $wp_query->post->post_title,
    687             'post_excerpt'          => $wp_query->post->post_excerpt,
    688             'post_content_filtered' => $wp_query->post->post_content_filtered,
    689             'post_mime_type'        => $wp_query->post->post_mime_type,
    690             'post_password'         => $wp_query->post->post_password,
    691             'post_name'             => $wp_query->post->post_name,
    692             'guid'                  => $wp_query->post->guid,
    693             'menu_order'            => $wp_query->post->menu_order,
    694             'pinged'                => $wp_query->post->pinged,
    695             'to_ping'               => $wp_query->post->to_ping,
    696             'ping_status'           => $wp_query->post->ping_status,
    697             'comment_status'        => $wp_query->post->comment_status,
    698             'comment_count'         => $wp_query->post->comment_count,
    699             'filter'                => $wp_query->post->filter,
    700 
    701             'is_404'                => false,
    702             'is_page'               => false,
    703             'is_single'             => false,
    704             'is_archive'            => false,
    705             'is_tax'                => false,
    706         ) );
    707     } else {
    708         $dummy = wp_parse_args( $args, array(
    709             'ID'                    => -9999,
    710             'post_status'           => 'public',
    711             'post_author'           => 0,
    712             'post_parent'           => 0,
    713             'post_type'             => 'page',
    714             'post_date'             => 0,
    715             'post_date_gmt'         => 0,
    716             'post_modified'         => 0,
    717             'post_modified_gmt'     => 0,
    718             'post_content'          => '',
    719             'post_title'            => '',
    720             'post_excerpt'          => '',
    721             'post_content_filtered' => '',
    722             'post_mime_type'        => '',
    723             'post_password'         => '',
    724             'post_name'             => '',
    725             'guid'                  => '',
    726             'menu_order'            => 0,
    727             'pinged'                => '',
    728             'to_ping'               => '',
    729             'ping_status'           => '',
    730             'comment_status'        => 'closed',
    731             'comment_count'         => 0,
    732             'filter'                => 'raw',
    733 
    734             'is_404'                => false,
    735             'is_page'               => false,
    736             'is_single'             => false,
    737             'is_archive'            => false,
    738             'is_tax'                => false,
    739         ) );
    740     }
    741 
    742     // Bail if dummy post is empty.
    743     if ( empty( $dummy ) ) {
    744         return;
    745     }
    746 
    747     // Set the $post global.
    748     $post = new WP_Post( (object) $dummy );
    749 
    750     // Copy the new post global into the main $wp_query.
    751     $wp_query->post       = $post;
    752     $wp_query->posts      = array( $post );
    753 
    754     // Prevent comments form from appearing.
    755     $wp_query->post_count = 1;
    756     $wp_query->is_404     = $dummy['is_404'];
    757     $wp_query->is_page    = $dummy['is_page'];
    758     $wp_query->is_single  = $dummy['is_single'];
    759     $wp_query->is_archive = $dummy['is_archive'];
    760     $wp_query->is_tax     = $dummy['is_tax'];
    761 
    762     // Clean up the dummy post.
    763     unset( $dummy );
    764 
    765     /**
    766      * Force the header back to 200 status if not a deliberate 404
    767      *
    768      * @see https://bbpress.trac.wordpress.org/ticket/1973
    769      */
    770     if ( ! $wp_query->is_404() ) {
    771         status_header( 200 );
    772     }
    773 
    774     // If we are resetting a post, we are in theme compat.
    775     bp_set_theme_compat_active( true );
    776 
    777     // If we are in theme compat, we don't need the 'Edit' post link.
    778     add_filter( 'get_edit_post_link', 'bp_core_filter_edit_post_link', 10, 2 );
    779 }
    780 
    781 /**
    782  * Reset main query vars and filter 'the_content' to output a BuddyPress template part as needed.
    783  *
    784  * @since 1.7.0
    785  *
    786  * @uses bp_is_single_user() To check if page is single user.
    787  * @uses bp_get_single_user_template() To get user template.
    788  * @uses bp_is_single_user_edit() To check if page is single user edit.
    789  * @uses bp_get_single_user_edit_template() To get user edit template.
    790  * @uses bp_is_single_view() To check if page is single view.
    791  * @uses bp_get_single_view_template() To get view template.
    792  * @uses bp_is_forum_edit() To check if page is forum edit.
    793  * @uses bp_get_forum_edit_template() To get forum edit template.
    794  * @uses bp_is_topic_merge() To check if page is topic merge.
    795  * @uses bp_get_topic_merge_template() To get topic merge template.
    796  * @uses bp_is_topic_split() To check if page is topic split.
    797  * @uses bp_get_topic_split_template() To get topic split template.
    798  * @uses bp_is_topic_edit() To check if page is topic edit.
    799  * @uses bp_get_topic_edit_template() To get topic edit template.
    800  * @uses bp_is_reply_edit() To check if page is reply edit.
    801  * @uses bp_get_reply_edit_template() To get reply edit template.
    802  * @uses bp_set_theme_compat_template() To set the global theme compat template.
    803  *
    804  * @param string $template Template name.
    805  * @return string $template Template name.
    806  */
    807 function bp_template_include_theme_compat( $template = '' ) {
    808 
    809     // If the current theme doesn't need theme compat, bail at this point.
    810     if ( ! bp_use_theme_compat_with_current_theme() ) {
    811         return $template;
    812     }
    813 
    814     /**
    815      * Fires when resetting main query vars and filtering 'the_content' to output BuddyPress template parts.
    816      *
    817      * Use this action to execute code that will communicate to BuddyPress's
    818      * theme compatibility layer whether or not we're replacing the_content()
    819      * with some other template part.
    820      *
    821      * @since 1.7.0
    822      */
    823     do_action( 'bp_template_include_reset_dummy_post_data' );
    824 
    825     // Bail if the template already matches a BuddyPress template.
    826     if ( ! empty( buddypress()->theme_compat->found_template ) ) {
    827         return $template;
    828     }
    829 
    830     /**
    831      * If we are relying on BuddyPress's built in theme compatibility to load
    832      * the proper content, we need to intercept the_content, replace the
    833      * output, and display ours instead.
    834      *
    835      * To do this, we first remove all filters from 'the_content' and hook
    836      * our own function into it, which runs a series of checks to determine
    837      * the context, and then uses the built in shortcodes to output the
    838      * correct results from inside an output buffer.
    839      *
    840      * Uses bp_get_theme_compat_templates() to provide fall-backs that
    841      * should be coded without superfluous mark-up and logic (prev/next
    842      * navigation, comments, date/time, etc...)
    843      *
    844      * Hook into 'bp_get_buddypress_template' to override the array of
    845      * possible templates, or 'bp_buddypress_template' to override the result.
    846      */
    847     if ( bp_is_theme_compat_active() ) {
    848         $template = bp_get_theme_compat_templates();
    849 
    850         add_filter( 'the_content', 'bp_replace_the_content' );
    851 
    852         // Add BuddyPress's head action to wp_head.
    853         if ( ! has_action( 'wp_head', 'bp_head' ) ) {
    854             add_action( 'wp_head', 'bp_head' );
    855         }
    856     }
    857 
    858     /**
    859      * Filters the template name to include.
    860      *
    861      * @since 1.7.0
    862      *
    863      * @param string $template Template name.
    864      */
    865     return apply_filters( 'bp_template_include_theme_compat', $template );
    866 }
    867 
    868 /**
    869  * Conditionally replace 'the_content'.
    870  *
    871  * Replaces the_content() if the post_type being displayed is one that would
    872  * normally be handled by BuddyPress, but proper single page templates do not
    873  * exist in the currently active theme.
    874  *
    875  * @since 1.7.0
    876  *
    877  * @param string $content Original post content.
    878  * @return string $content Post content, potentially modified.
    879  */
    880 function bp_replace_the_content( $content = '' ) {
    881 
    882     // Bail if not the main loop where theme compat is happening.
    883     if ( ! bp_do_theme_compat() ) {
    884         return $content;
    885     }
    886 
    887     // Set theme compat to false early, to avoid recursion from nested calls to
    888     // the_content() that execute before theme compat has unhooked itself.
    889     bp_set_theme_compat_active( false );
    890 
    891     /**
    892      * Filters the content to replace in the post.
    893      *
    894      * @since 1.7.0
    895      *
    896      * @param string $content Original post content.
    897      */
    898     $new_content = apply_filters( 'bp_replace_the_content', $content );
    899 
    900     // Juggle the content around and try to prevent unsightly comments.
    901     if ( ! empty( $new_content ) && ( $new_content !== $content ) ) {
    902 
    903         // Set the content to be the new content.
    904         $content = $new_content;
    905 
    906         // Clean up after ourselves.
    907         unset( $new_content );
    908 
    909         // Reset the $post global.
    910         wp_reset_postdata();
    911     }
    912 
    913     // Return possibly hi-jacked content.
    914     return $content;
    915 }
    916 
    917 /**
    918  * Are we currently replacing the_content?
    919  *
    920  * @since 1.8.0
    921  *
    922  * @return bool True if the_content is currently in the process of being
    923  *              filtered and replaced.
    924  */
    925 function bp_do_theme_compat() {
    926     return (bool) ( ! bp_is_template_included() && in_the_loop() && bp_is_theme_compat_active() );
    927 }
    928 
    929 /** Filters *******************************************************************/
    930 
    931 /**
    932  * Remove all filters from a WordPress filter hook.
    933  *
    934  * Removed filters are stashed in the $bp global, in case they need to be
    935  * restored later.
    936  *
    937  * @since 1.7.0
    938  *
    939  * @global WP_filter $wp_filter
    940  * @global array $merged_filters
    941  *
    942  * @param string   $tag      The filter tag to remove filters from.
    943  * @param int|bool $priority Optional. If present, only those callbacks attached
    944  *                           at a given priority will be removed. Otherwise, all callbacks
    945  *                           attached to the tag will be removed, regardless of priority.
    946  * @return bool True on success.
    947  */
    948 function bp_remove_all_filters( $tag, $priority = false ) {
    949     global $wp_filter, $merged_filters;
    950 
    951     $bp = buddypress();
    952 
    953     // Filters exist.
    954     if ( isset( $wp_filter[$tag] ) ) {
    955 
    956         // Filters exist in this priority.
    957         if ( ! empty( $priority ) && isset( $wp_filter[$tag][$priority] ) ) {
    958 
    959             // Store filters in a backup.
    960             $bp->filters->wp_filter[$tag][$priority] = $wp_filter[$tag][$priority];
    961 
    962             // Unset the filters.
    963             unset( $wp_filter[$tag][$priority] );
    964 
    965         // Priority is empty.
    966         } else {
    967 
    968             // Store filters in a backup.
    969             $bp->filters->wp_filter[$tag] = $wp_filter[$tag];
    970 
    971             // Unset the filters.
    972             unset( $wp_filter[$tag] );
    973         }
    974     }
    975 
    976     // Check merged filters.
    977     if ( isset( $merged_filters[$tag] ) ) {
    978 
    979         // Store filters in a backup.
    980         $bp->filters->merged_filters[$tag] = $merged_filters[$tag];
    981 
    982         // Unset the filters.
    983         unset( $merged_filters[$tag] );
    984     }
    985 
    986     return true;
    987 }
    988 
    989 /**
    990  * Restore filters that were removed using bp_remove_all_filters().
    991  *
    992  * @since 1.7.0
    993  *
    994  * @global WP_filter $wp_filter
    995  * @global array $merged_filters
    996  *
    997  * @param string   $tag      The tag to which filters should be restored.
    998  * @param int|bool $priority Optional. If present, only those filters that were originally
    999  *                           attached to the tag with $priority will be restored. Otherwise,
    1000  *                           all available filters will be restored, regardless of priority.
    1001  * @return bool True on success.
    1002  */
    1003 function bp_restore_all_filters( $tag, $priority = false ) {
    1004     global $wp_filter, $merged_filters;
    1005 
    1006     $bp = buddypress();
    1007 
    1008     // Filters exist.
    1009     if ( isset( $bp->filters->wp_filter[$tag] ) ) {
    1010 
    1011         // Filters exist in this priority.
    1012         if ( ! empty( $priority ) && isset( $bp->filters->wp_filter[$tag][$priority] ) ) {
    1013 
    1014             // Store filters in a backup.
    1015             $wp_filter[$tag][$priority] = $bp->filters->wp_filter[$tag][$priority];
    1016 
    1017             // Unset the filters.
    1018             unset( $bp->filters->wp_filter[$tag][$priority] );
    1019 
    1020         // Priority is empty.
    1021         } else {
    1022 
    1023             // Store filters in a backup.
    1024             $wp_filter[$tag] = $bp->filters->wp_filter[$tag];
    1025 
    1026             // Unset the filters.
    1027             unset( $bp->filters->wp_filter[$tag] );
    1028         }
    1029     }
    1030 
    1031     // Check merged filters.
    1032     if ( isset( $bp->filters->merged_filters[$tag] ) ) {
    1033 
    1034         // Store filters in a backup.
    1035         $merged_filters[$tag] = $bp->filters->merged_filters[$tag];
    1036 
    1037         // Unset the filters.
    1038         unset( $bp->filters->merged_filters[$tag] );
    1039     }
    1040 
    1041     return true;
    1042 }
    1043 
    1044 /**
    1045  * Force comments_status to 'closed' for BuddyPress post types.
    1046  *
    1047  * @since 1.7.0
    1048  *
    1049  * @param bool $open    True if open, false if closed.
    1050  * @param int  $post_id ID of the post to check.
    1051  * @return bool True if open, false if closed.
    1052  */
    1053 function bp_comments_open( $open, $post_id = 0 ) {
    1054 
    1055     $retval = is_buddypress() ? false : $open;
    1056 
    1057     /**
    1058      * Filters whether or not to force comments_status to closed for BuddyPress post types.
    1059      *
    1060      * @since 1.7.0
    1061      *
    1062      * @param bool $retval  Whether or not we are on a BuddyPress post type.
    1063      * @param bool $open    True if comments open, false if closed.
    1064      * @param int  $post_id Post ID for the checked post.
    1065      */
    1066     return apply_filters( 'bp_force_comment_status', $retval, $open, $post_id );
    1067 }
    1068 
    1069 /**
    1070  * Do not allow {@link comments_template()} to render during theme compatibility.
    1071  *
    1072  * When theme compatibility sets the 'is_page' flag to true via
    1073  * {@link bp_theme_compat_reset_post()}, themes that use comments_template()
    1074  * in their page template will run.
    1075  *
    1076  * To prevent comments_template() from rendering, we set the 'is_page' and
    1077  * 'is_single' flags to false since that function looks at these conditionals
    1078  * before querying the database for comments and loading the comments template.
    1079  *
    1080  * This is done during the output buffer as late as possible to prevent any
    1081  * wonkiness.
    1082  *
    1083  * @since 1.9.2
    1084  *
    1085  * @param string $retval The current post content.
    1086  * @return string $retval
    1087  */
    1088 function bp_theme_compat_toggle_is_page( $retval = '' ) {
    1089     global $wp_query;
    1090 
    1091     $wp_query->is_page = false;
    1092 
    1093     // Set a switch so we know that we've toggled these WP_Query properties.
    1094     buddypress()->theme_compat->is_page_toggled = true;
    1095 
    1096     return $retval;
    1097 }
    1098 add_filter( 'bp_replace_the_content', 'bp_theme_compat_toggle_is_page', 9999 );
    1099 
    1100 /**
    1101  * Restores the 'is_single' and 'is_page' flags if toggled by BuddyPress.
    1102  *
    1103  * @since 1.9.2
    1104  *
    1105  * @see bp_theme_compat_toggle_is_page()
    1106  * @param object $query The WP_Query object.
    1107  */
    1108 function bp_theme_compat_loop_end( $query ) {
    1109 
    1110     // Get BuddyPress.
    1111     $bp = buddypress();
    1112 
    1113     // Bail if page is not toggled.
    1114     if ( ! isset( $bp->theme_compat->is_page_toggled ) ) {
    1115         return;
    1116     }
    1117 
    1118     // Revert our toggled WP_Query properties.
    1119     $query->is_page = true;
    1120 
    1121     // Unset our switch.
    1122     unset( $bp->theme_compat->is_page_toggled );
    1123 }
    1124 add_action( 'loop_end', 'bp_theme_compat_loop_end' );
Note: See TracChangeset for help on using the changeset viewer.