Skip to:
Content

BuddyPress.org

Ticket #8350: 8350.02.patch

File 8350.02.patch, 7.6 KB (added by imath, 6 years ago)
  • src/bp-core/bp-core-template-loader.php

    diff --git src/bp-core/bp-core-template-loader.php src/bp-core/bp-core-template-loader.php
    index 2c1000a1a..9191e2915 100644
    defined( 'ABSPATH' ) || exit; 
    1818 * Get a BuddyPress template part for display in a theme.
    1919 *
    2020 * @since 1.7.0
     21 * @since 7.0.0 Added $args parameter.
    2122 *
    2223 * @param string      $slug Template part slug. Used to generate filenames,
    2324 *                          eg 'friends' for 'friends.php'.
    2425 * @param string|null $name Optional. Template part name. Used to generate
    2526 *                          secondary filenames, eg 'personal' for 'activity-personal.php'.
     27 * @param array       $args Optional. Extra args to pass to locate_template().
    2628 * @return false|string Path to located template. See {@link bp_locate_template()}.
    2729 */
    28 function bp_get_template_part( $slug, $name = null ) {
     30function bp_get_template_part( $slug, $name = null, $args = array() ) {
    2931
    3032        /**
    3133         * Fires at the start of bp_get_template_part().
    function bp_get_template_part( $slug, $name = null ) { 
    3335         * This is a variable hook that is dependent on the slug passed in.
    3436         *
    3537         * @since 1.7.0
     38         * @since 7.0.0 Added $args parameter.
    3639         *
    3740         * @param string $slug Template part slug requested.
    3841         * @param string $name Template part name requested.
     42         * @param array  $args Extra args to pass to locate_template().
    3943         */
    40         do_action( 'get_template_part_' . $slug, $slug, $name );
     44        do_action( 'get_template_part_' . $slug, $slug, $name, $args );
    4145
    4246        // Setup possible parts.
    4347        $templates = array();
    function bp_get_template_part( $slug, $name = null ) { 
    5054         * Filters the template parts to be loaded.
    5155         *
    5256         * @since 1.7.0
     57         * @since 7.0.0 Added $args parameter.
    5358         *
    5459         * @param array  $templates Array of templates located.
    5560         * @param string $slug      Template part slug requested.
    5661         * @param string $name      Template part name requested.
     62         * @param array  $args      Extra args to pass to locate_template().
    5763         */
    58         $templates = apply_filters( 'bp_get_template_part', $templates, $slug, $name );
     64        $templates = apply_filters( 'bp_get_template_part', $templates, $slug, $name, $args );
    5965
    6066        // Return the part that is found.
    61         return bp_locate_template( $templates, true, false );
     67        return bp_locate_template( $templates, true, false, $args );
    6268}
    6369
    6470/**
    function bp_get_template_part( $slug, $name = null ) { 
    6874 * prepended to the slug.
    6975 *
    7076 * @since 2.6.0
     77 * @since 7.0.0 Added $args parameter.
    7178 *
    7279 * @see bp_get_template_part() for full documentation.
    7380 *
    7481 * @param string      $slug Template slug.
    7582 * @param string|null $name Template name.
     83 * @param array       $args Optional. Extra args to pass to locate_template().
    7684 * @return false|string
    7785 */
    78 function bp_get_asset_template_part( $slug, $name = null ) {
    79         return bp_get_template_part( "assets/{$slug}", $name );
     86function bp_get_asset_template_part( $slug, $name = null, $args = array() ) {
     87        return bp_get_template_part( "assets/{$slug}", $name, $args );
    8088}
    8189
    8290/**
    function bp_get_asset_template_part( $slug, $name = null ) { 
    8795 * not found in either of those, it looks in the theme-compat folder last.
    8896 *
    8997 * @since 1.7.0
     98 * @since 7.0.0 Added $args parameter.
    9099 *
    91100 * @param string|array $template_names Template file(s) to search for, in order.
    92101 * @param bool         $load           Optional. If true, the template file will be loaded when
    93102 *                                     found. If false, the path will be returned. Default: false.
    94103 * @param bool         $require_once   Optional. Whether to require_once or require. Has
    95104 *                                     no effect if $load is false. Default: true.
     105 * @param array        $args           Optional. Extra args to pass to locate_template().
    96106 * @return string The template filename if one is located.
    97107 */
    98 function bp_locate_template( $template_names, $load = false, $require_once = true ) {
     108function bp_locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
    99109
    100110        // Bail when there are no templates to locate.
    101111        if ( empty( $template_names ) ) {
    function bp_locate_template( $template_names, $load = false, $require_once = tru 
    140150         * If you want to override a specific template part, please either filter
    141151         * 'bp_get_template_part' or add a new location to the template stack.
    142152         */
    143         do_action( 'bp_locate_template', $located, $template_name, $template_names, $template_locations, $load, $require_once );
     153        do_action( 'bp_locate_template', $located, $template_name, $template_names, $template_locations, $load, $require_once, $args );
    144154
    145155        /**
    146156         * Filter here to allow/disallow template loading.
    function bp_locate_template( $template_names, $load = false, $require_once = tru 
    152162        $load_template = (bool) apply_filters( 'bp_locate_template_and_load', true );
    153163
    154164        if ( $load_template && $load && ! empty( $located ) ) {
    155                 load_template( $located, $require_once );
     165                load_template( $located, $require_once, $args );
    156166        }
    157167
    158168        return $located;
    function bp_get_template_stack() { 
    320330 * Put a template part into an output buffer, and return it.
    321331 *
    322332 * @since 1.7.0
     333 * @since 7.0.0 Added $args parameter.
    323334 *
    324  * @see bp_get_template_part() for a description of $slug and $name params.
     335 * @see bp_get_template_part() for a description of $slug, $name and $args params.
    325336 *
    326337 * @param string      $slug See {@link bp_get_template_part()}.
    327338 * @param string|null $name See {@link bp_get_template_part()}.
    328339 * @param bool        $echo If true, template content will be echoed. If false,
    329340 *                          returned. Default: true.
     341 * @param array       $args See {@link bp_get_template_part()}.
    330342 * @return string|null If $echo, returns the template content.
    331343 */
    332 function bp_buffer_template_part( $slug, $name = null, $echo = true ) {
     344function bp_buffer_template_part( $slug, $name = null, $echo = true, $args = array() ) {
    333345        ob_start();
    334346
    335347        // Remove 'bp_replace_the_content' filter to prevent infinite loops.
    336348        remove_filter( 'the_content', 'bp_replace_the_content' );
    337349
    338         bp_get_template_part( $slug, $name );
     350        bp_get_template_part( $slug, $name, $args );
    339351
    340352        // Remove 'bp_replace_the_content' filter to prevent infinite loops.
    341353        add_filter( 'the_content', 'bp_replace_the_content' );
  • new file tests/phpunit/assets/templates/index.php

    diff --git tests/phpunit/assets/templates/index.php tests/phpunit/assets/templates/index.php
    new file mode 100644
    index 000000000..e4379cf4b
    - +  
     1<?php
     2echo (int) $args['test'] + 1;
  • new file tests/phpunit/testcases/core/templateLoader.php

    diff --git tests/phpunit/testcases/core/templateLoader.php tests/phpunit/testcases/core/templateLoader.php
    new file mode 100644
    index 000000000..47b8d5da9
    - +  
     1<?php
     2
     3/**
     4 * @group core
     5 * @group core-template-loader
     6 */
     7class BP_Tests_Template_Loader_Functions extends BP_UnitTestCase {
     8
     9        public function setUp() {
     10                add_filter( 'bp_get_template_stack', array( $this, 'template_stack'), 10, 1 );
     11
     12                parent::setUp();
     13        }
     14
     15        public function tearDown() {
     16                remove_filter( 'bp_get_template_stack', array( $this, 'template_stack'), 10, 1 );
     17
     18                parent::tearDown();
     19        }
     20
     21        public function template_stack( $stack = array() ) {
     22                return array_merge(
     23                        array(
     24                                dirname( dirname( dirname( __FILE__ ) ) ) . '/assets/templates/',
     25                        )
     26                );
     27        }
     28
     29        /**
     30         * @group bp_buffer_template_part
     31         */
     32        public function test_bp_buffer_template_part() {
     33                $buffer = bp_buffer_template_part( 'index', null, false, array( 'test' => 1 ) );
     34                $this->assertTrue( 2 === (int) $buffer );
     35        }
     36}