Skip to:
Content

BuddyPress.org

Changeset 7131


Ignore:
Timestamp:
05/30/2013 10:58:46 PM (11 years ago)
Author:
johnjamesjacoby
Message:

Iterate on theme compatibility content replacement order. Introduce bp_do_theme_compat() and other helper functions to avoid breaking on nested calls to the_content() or nested usages of 'the_content' filter.

Also, experiment with not adding/removing all_filters from 'the_content' for a bit, and see if any issues come up in testing with other plugins active.

See #5021.

Location:
trunk/bp-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-template-loader.php

    r6824 r7131  
    212212
    213213    // Get the output buffer contents
    214     $output = ob_get_contents();
    215 
    216     // Flush the output buffer
    217     ob_end_clean();
     214    $output = ob_get_clean();
    218215
    219216    // Echo or return the output buffer contents
     
    342339    $new_template = apply_filters( 'bp_get_root_template', false, $template );
    343340
    344     // BuddyPress template file exists
     341    // A BuddyPress template file was located, so override the WordPress
     342    // template and use it to switch off BuddyPress's theme compatibility.
    345343    if ( !empty( $new_template ) ) {
    346 
    347         // Override the WordPress template with a BuddyPress one
    348         $template = $new_template;
    349 
    350         // @see: bp_template_include_theme_compat()
    351         buddypress()->theme_compat->found_template = true;
     344        $template = bbp_set_template_included( $new_template );
    352345    }
    353346
    354347    return apply_filters( 'bp_template_include_theme_supports', $template );
     348}
     349
     350/**
     351 * Set the included template
     352 *
     353 * @since BuddyPress (1.8)
     354 * @param mixed $template Default false
     355 * @return mixed False if empty. Template name if template included
     356 */
     357function bp_set_template_included( $template = false ) {
     358    buddypress()->theme_compat->found_template = $template;
     359
     360    return buddypress()->theme_compat->found_template;
     361}
     362
     363/**
     364 * Is a BuddyPress template being included?
     365 *
     366 * @since BuddyPress (1.8)
     367 * @return bool True if yes, false if no
     368 */
     369function bp_is_template_included() {
     370    return ! empty( buddypress()->theme_compat->found_template );
    355371}
    356372
  • trunk/bp-core/bp-core-theme-compatibility.php

    r7128 r7131  
    524524     */
    525525    if ( bp_is_theme_compat_active() ) {
    526 
    527         // Hook to the beginning of the main post loop to remove all filters
    528         // from the_content as late as possible.
    529         add_action( 'loop_start', 'bp_theme_compat_main_loop_start',  9999 );
    530 
    531         // Hook to the end of the main post loop to restore all filters to
    532         // the_content as early as possible.
    533         add_action( 'loop_end',   'bp_theme_compat_main_loop_end',   -9999 );
     526        $template = bp_get_theme_compat_templates();
     527
     528        add_filter( 'the_content', 'bp_replace_the_content' );
    534529
    535530        // Add BuddyPress's head action to wp_head
     
    537532            add_action( 'wp_head', 'bp_head' );
    538533        }
    539 
    540         // Find the appropriate template file
    541         $template = bp_get_theme_compat_templates();
    542534    }
    543535
     
    556548function bp_replace_the_content( $content = '' ) {
    557549
    558     if ( ! in_the_loop() )
     550    // Bail if not the main loop where theme compat is happening
     551    if ( ! bp_do_theme_compat() )
    559552        return $content;
    560553
     554    // Set theme compat to false early, to avoid recursion from nested calls to
     555    // the_content() that execute before theme compat has unhooked itself.
     556    bp_set_theme_compat_active( false );
     557
     558    // Do we have new content to replace the old content?
    561559    $new_content = apply_filters( 'bp_replace_the_content', $content );
    562560
    563561    // Juggle the content around and try to prevent unsightly comments
    564     if ( !empty( $new_content ) && ( $new_content != $content ) ) {
     562    if ( !empty( $new_content ) && ( $new_content !== $content ) ) {
    565563
    566564        // Set the content to be the new content
     
    579577
    580578/**
    581  * Helper function to conditionally toggle the_content filters in the main
    582  * query loop. Aids with theme compatibility.
     579 * Are we replacing the_content
    583580 *
    584581 * @since BuddyPress (1.8)
    585  * @internal Used only by theme compatibilty
    586  * @see bp_template_include_theme_compat()
    587  * @see bp_theme_compat_main_loop_end()
    588  */
    589 function bp_theme_compat_main_loop_start() {
    590 
    591     // Bail if not the main query
    592     if ( ! in_the_loop() )
    593         return;
    594 
    595     // Remove all of the filters from the_content
    596     bp_remove_all_filters( 'the_content' );
    597 
    598     // Make sure we replace the content
    599     add_filter( 'the_content', 'bp_replace_the_content' );
    600 }
    601 
    602 /**
    603  * Helper function to conditionally toggle the_content filters in the main
    604  * query loop. Aids with theme compatibility.
    605  *
    606  * @since BuddyPress (1.8)
    607  * @internal Used only by theme compatibilty
    608  * @see bp_template_include_theme_compat()
    609  * @see bp_theme_compat_main_loop_start()
    610  */
    611 function bp_theme_compat_main_loop_end() {
    612 
    613     // Bail if not the main query
    614     if ( ! in_the_loop() )
    615         return;
    616 
    617     // Put all the filters back
    618     bp_restore_all_filters( 'the_content' );
     582 * @return bool
     583 */
     584function bp_do_theme_compat() {
     585    return (bool) ( ! bp_is_template_included() && in_the_loop() && bp_is_theme_compat_active() );
    619586}
    620587
Note: See TracChangeset for help on using the changeset viewer.