Skip to:
Content

bbPress.org

Changeset 3169


Ignore:
Timestamp:
05/18/2011 09:42:58 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Consolidate theme compat functions and add more verbose phpdoc where appropriate. Use template_include filter instead of template_redirect action, and add support for incorrect usage of add_theme_support( 'bbpress' ). See #1524.

Location:
branches/plugin/bbp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-core-hooks.php

    r3162 r3169  
    207207add_action( 'bbp_spammed_reply',   'bbp_update_reply_walker' );
    208208add_action( 'bbp_unspammed_reply', 'bbp_update_reply_walker' );
    209 
    210 // Custom Template - should be called at the end
    211 add_action( 'template_redirect', 'bbp_custom_template', 999 );
    212209
    213210/** FILTERS *******************************************************************/
  • branches/plugin/bbp-includes/bbp-general-template.php

    r3167 r3169  
    14301430
    14311431/**
    1432  * Load bbPress custom templates
    1433  *
    1434  * Loads custom templates for bbPress view page, user profile, user edit, topic
    1435  * edit and reply edit pages.
    1436  *
    1437  * @since bbPress (r2753)
    1438  *
    1439  * @uses bbp_is_user_profile_page() To check if it's a profile page
    1440  * @uses apply_filters() Calls 'bbp_profile_templates' with the profile
    1441  *                        templates array
    1442  * @uses bbp_is_user_profile_edit() To check if it's a profile edit page
    1443  * @uses apply_filters() Calls 'bbp_profile_edit_templates' with the profile
    1444  *                        edit templates array
    1445  * @uses bbp_is_view() To check if it's a view page
    1446  * @uses bbp_get_view_id() To get the view id
    1447  * @uses apply_filters() Calls 'bbp_view_templates' with the view templates array
    1448  * @uses bbp_is_topic_edit() To check if it's a topic edit page
    1449  * @uses bbp_get_topic_post_type() To get the topic post type
    1450  * @uses apply_filters() Calls 'bbp_topic_edit_templates' with the topic edit
    1451  *                        templates array
    1452  * @uses bbp_is_reply_edit() To check if it's a reply edit page
    1453  * @uses bbp_get_reply_post_type() To get the reply post type
    1454  * @uses apply_filters() Calls 'bbp_reply_edit_templates' with the reply edit
    1455  *                        templates array
    1456  * @uses apply_filters() Calls 'bbp_custom_template' with the template array
    1457  * @uses bbp_load_template() To load the template
    1458  */
    1459 function bbp_custom_template() {
    1460     global $bbp;
    1461 
    1462     // Bail if theme does not support bbPress
    1463     if ( !current_theme_supports( 'bbpress' ) )
    1464         return;
    1465 
    1466     $template = false;
    1467 
    1468     // Viewing a profile
    1469     if ( bbp_is_user_profile_page() ) {
    1470         $template = apply_filters( 'bbp_profile_templates', array(
    1471             'forums/user.php',
    1472             'bbpress/user.php',
    1473             'user.php',
    1474             'author.php',
    1475             'index.php'
    1476         ) );
    1477 
    1478     // Editing a profile
    1479     } elseif ( bbp_is_user_profile_edit() ) {
    1480         $template = apply_filters( 'bbp_profile_edit_templates', array(
    1481             'forums/user-edit.php',
    1482             'bbpress/user-edit.php',
    1483             'user-edit.php',
    1484             'forums/user.php',
    1485             'bbpress/user.php',
    1486             'user.php',
    1487             'author.php',
    1488             'index.php'
    1489         ) );
    1490 
    1491     // View page
    1492     } elseif ( bbp_is_view() ) {
    1493         $template = apply_filters( 'bbp_view_templates', array(
    1494             'forums/view-' . bbp_get_view_id(),
    1495             'bbpress/view-' . bbp_get_view_id(),
    1496             'forums/view.php',
    1497             'bbpress/view.php',
    1498             'view-' . bbp_get_view_id(),
    1499             'view.php',
    1500             'index.php'
    1501         ) );
    1502 
    1503     // Editing a topic
    1504     } elseif ( bbp_is_topic_edit() ) {
    1505         $template = array(
    1506             'forums/action-edit.php',
    1507             'bbpress/action-edit.php',
    1508             'forums/single-' . bbp_get_topic_post_type(),
    1509             'bbpress/single-' . bbp_get_topic_post_type(),
    1510             'action-bbp-edit.php',
    1511             'single-' . bbp_get_topic_post_type(),
    1512             'single.php',
    1513             'index.php'
    1514         );
    1515 
    1516         // Add split/merge to front of array if present in _GET
    1517         if ( !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'merge', 'split' ) ) ) {
    1518             array_unshift( $template,
    1519                 'forums/action-split-merge.php',
    1520                 'bbpress/action-split-merge.php',
    1521                 'action-split-merge.php'
    1522             );
    1523         }
    1524 
    1525         $template = apply_filters( 'bbp_topic_edit_templates', $template );
    1526 
    1527     // Editing a reply
    1528     } elseif ( bbp_is_reply_edit() ) {
    1529         $template = apply_filters( 'bbp_reply_edit_templates', array(
    1530             'forums/action-edit.php',
    1531             'bbpress/action-edit.php',
    1532             'forums/single-' . bbp_get_reply_post_type(),
    1533             'bbpress/single-' . bbp_get_reply_post_type(),
    1534             'action-bbp-edit.php',
    1535             'single-' . bbp_get_reply_post_type(),
    1536             'single.php',
    1537             'index.php'
    1538         ) );
    1539     }
    1540 
    1541     if ( !$template = apply_filters( 'bbp_custom_template', $template ) )
    1542         return false;
    1543 
    1544     // Try to load a template file
    1545     bbp_load_template( $template );
    1546 }
    1547 
    1548 /**
    1549  * Load custom template
    1550  *
    1551  * @param string|array $files
    1552  * @uses locate_template() To locate and include the template
    1553  * @return bool False on failure (nothing on success)
    1554  */
    1555 function bbp_load_template( $templates ) {
    1556 
    1557     // Bail if nothing passed
    1558     if ( empty( $templates ) )
    1559         return;
    1560 
    1561     // Force array
    1562     elseif ( is_string( $templates ) )
    1563         $templates = (array) $templates;
    1564 
    1565     // Theme compat
    1566     if ( !current_theme_supports( 'bbpress' ) ) {
    1567 
    1568         // Snippet taken from locate_template()
    1569         $located = '';
    1570         foreach ( (array) $templates as $template_name ) {
    1571 
    1572             // Skip to next item in array if this one is empty
    1573             if ( empty( $template_name ) )
    1574                 continue;
    1575 
    1576             // File exists in compat theme so exit the loop
    1577             if ( file_exists( bbp_get_theme_compat() . '/' . $template_name ) ) {
    1578                 $located = bbp_get_theme_compat() . '/' . $template_name;
    1579                 break;
    1580             }
    1581         }
    1582 
    1583         // Template file located
    1584         if ( !empty( $located ) ) {
    1585             load_template( $located, false );
    1586             exit();
    1587         }
    1588 
    1589     // Exit if file is found
    1590     } elseif ( locate_template( $templates, true ) ) {
    1591         exit();
    1592     }
    1593 }
    1594 
    1595 /**
    15961432 * Adds bbPress theme support to any active WordPress theme
    15971433 *
     
    19061742 * to appear. If the current theme does not explicitly support bbPress, it
    19071743 * intercepts the page template and uses one served from the bbPress compatable
    1908  * theme, set as the $bbp->theme_compat global.
     1744 * theme, set as the $bbp->theme_compat global. If the current theme does
     1745 * support bbPress, we'll explore the template hierarchy and try to locate one.
    19091746 *
    19101747 * @since bbPress (r3032)
     
    19151752 * @return string
    19161753 */
    1917 function bbp_template_include( $template ) {
     1754function bbp_template_include( $template = false ) {
    19181755    global $bbp;
    19191756
    1920     // Current theme does not support bbPress, so we need to do some heavy
    1921     // lifting to see if a bbPress template is needed in the current context
    1922     if ( !current_theme_supports( 'bbpress' ) ) {
     1757    // Prevent debug notices
     1758    $templates    = array();
     1759    $new_template = '';
     1760
     1761    // Current theme supports bbPress
     1762    if ( current_theme_supports( 'bbpress' ) ) {
     1763
     1764        // Viewing a profile
     1765        if ( bbp_is_user_profile_page() ) {
     1766            $templates = apply_filters( 'bbp_profile_templates', array(
     1767                'forums/user.php',
     1768                'bbpress/user.php',
     1769                'user.php',
     1770                'author.php',
     1771                'index.php'
     1772            ) );
     1773
     1774        // Editing a profile
     1775        } elseif ( bbp_is_user_profile_edit() ) {
     1776            $templates = apply_filters( 'bbp_profile_edit_templates', array(
     1777                'forums/user-edit.php',
     1778                'bbpress/user-edit.php',
     1779                'user-edit.php',
     1780                'forums/user.php',
     1781                'bbpress/user.php',
     1782                'user.php',
     1783                'author.php',
     1784                'index.php'
     1785            ) );
     1786
     1787        // View page
     1788        } elseif ( bbp_is_view() ) {
     1789            $templates = apply_filters( 'bbp_view_templates', array(
     1790                'forums/view-' . bbp_get_view_id(),
     1791                'bbpress/view-' . bbp_get_view_id(),
     1792                'forums/view.php',
     1793                'bbpress/view.php',
     1794                'view-' . bbp_get_view_id(),
     1795                'view.php',
     1796                'index.php'
     1797            ) );
     1798
     1799        // Editing a topic
     1800        } elseif ( bbp_is_topic_edit() ) {
     1801            $templates = array(
     1802                'forums/action-edit.php',
     1803                'bbpress/action-edit.php',
     1804                'forums/single-' . bbp_get_topic_post_type(),
     1805                'bbpress/single-' . bbp_get_topic_post_type(),
     1806                'action-bbp-edit.php',
     1807                'single-' . bbp_get_topic_post_type(),
     1808                'single.php',
     1809                'index.php'
     1810            );
     1811
     1812            // Add split/merge to front of array if present in _GET
     1813            if ( !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'merge', 'split' ) ) ) {
     1814                array_unshift( $templates,
     1815                    'forums/action-split-merge.php',
     1816                    'bbpress/action-split-merge.php',
     1817                    'action-split-merge.php'
     1818                );
     1819            }
     1820
     1821            $template = apply_filters( 'bbp_topic_edit_templates', $template );
     1822
     1823        // Editing a reply
     1824        } elseif ( bbp_is_reply_edit() ) {
     1825            $templates = apply_filters( 'bbp_reply_edit_templates', array(
     1826                'forums/action-edit.php',
     1827                'bbpress/action-edit.php',
     1828                'forums/single-' . bbp_get_reply_post_type(),
     1829                'bbpress/single-' . bbp_get_reply_post_type(),
     1830                'action-bbp-edit.php',
     1831                'single-' . bbp_get_reply_post_type(),
     1832                'single.php',
     1833                'index.php'
     1834            ) );
     1835        }
     1836
     1837        // Custom template file exists
     1838        if ( !empty( $templates ) && ( $new_template = locate_template( $templates, false, false ) ) ) {
     1839            $template = $new_template;
     1840        }
     1841    }
     1842
     1843    /**
     1844     * In this next bit, either the current theme does not support bbPress, or
     1845     * the theme author has incorrectly used add_theme_support( 'bbpress' )
     1846     * and we are going to help them out by silently filling in the blanks.
     1847     */
     1848    if ( !current_theme_supports( 'bbpress' ) || ( !empty( $templates ) && empty( $new_template ) ) ) {
    19231849
    19241850        // Assume we are not in theme compat
     
    20241950        }
    20251951
    2026         // Are we in theme compat mode?
     1952        /**
     1953         * If we are relying on bbPress's built in theme compatibility to load
     1954         * the proper content, we need to intercept the_content, replace the
     1955         * output, and display ours instead.
     1956         *
     1957         * To do this, we first remove all filters from 'the_content' and hook
     1958         * our own function into it, which runs a series of checks to determine
     1959         * the context, and then uses the built in shortcodes to output the
     1960         * correct results.
     1961         *
     1962         * We default to using page.php, since it's most likely to exist and
     1963         * should be coded to work without superfluous elements and logic, like
     1964         * prev/next navigation, comments, date/time, etc... You can hook into
     1965         * the 'bbp_template_include' filter to override page.php.
     1966         */
    20271967        if ( true === $in_theme_compat ) {
    20281968
     1969            // Remove all filters from the_content
     1970            remove_all_filters( 'the_content' );
     1971
    20291972            // Add a filter on the_content late, which we will later remove
    2030             add_filter( 'the_content', 'bbp_replace_the_content', 99999 );
     1973            add_filter( 'the_content', 'bbp_replace_the_content' );
    20311974
    20321975            // Default to the page template
    2033             $default_template = apply_filters( 'bbp_template_include', 'page.php' );
    2034             $template         = locate_template( $default_template, false, false );
     1976            $template = apply_filters( 'bbp_template_include', 'page.php' );
     1977            $template = locate_template( $template, false, false );
    20351978        }
    20361979    }
     
    20652008
    20662009        // Remove the filter that was added in bbp_template_include()
    2067         remove_filter( 'the_content', 'bbp_replace_the_content', 99999 );
     2010        remove_filter( 'the_content', 'bbp_replace_the_content' );
    20682011
    20692012        // Bail if shortcodes are unset somehow
Note: See TracChangeset for help on using the changeset viewer.