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; |
| 18 | 18 | * Get a BuddyPress template part for display in a theme. |
| 19 | 19 | * |
| 20 | 20 | * @since 1.7.0 |
| | 21 | * @since 7.0.0 Added $args parameter. |
| 21 | 22 | * |
| 22 | 23 | * @param string $slug Template part slug. Used to generate filenames, |
| 23 | 24 | * eg 'friends' for 'friends.php'. |
| 24 | 25 | * @param string|null $name Optional. Template part name. Used to generate |
| 25 | 26 | * secondary filenames, eg 'personal' for 'activity-personal.php'. |
| | 27 | * @param array $args Optional. Extra args to pass to locate_template(). |
| 26 | 28 | * @return false|string Path to located template. See {@link bp_locate_template()}. |
| 27 | 29 | */ |
| 28 | | function bp_get_template_part( $slug, $name = null ) { |
| | 30 | function bp_get_template_part( $slug, $name = null, $args = array() ) { |
| 29 | 31 | |
| 30 | 32 | /** |
| 31 | 33 | * Fires at the start of bp_get_template_part(). |
| … |
… |
function bp_get_template_part( $slug, $name = null ) { |
| 33 | 35 | * This is a variable hook that is dependent on the slug passed in. |
| 34 | 36 | * |
| 35 | 37 | * @since 1.7.0 |
| | 38 | * @since 7.0.0 Added $args parameter. |
| 36 | 39 | * |
| 37 | 40 | * @param string $slug Template part slug requested. |
| 38 | 41 | * @param string $name Template part name requested. |
| | 42 | * @param array $args Extra args to pass to locate_template(). |
| 39 | 43 | */ |
| 40 | | do_action( 'get_template_part_' . $slug, $slug, $name ); |
| | 44 | do_action( 'get_template_part_' . $slug, $slug, $name, $args ); |
| 41 | 45 | |
| 42 | 46 | // Setup possible parts. |
| 43 | 47 | $templates = array(); |
| … |
… |
function bp_get_template_part( $slug, $name = null ) { |
| 50 | 54 | * Filters the template parts to be loaded. |
| 51 | 55 | * |
| 52 | 56 | * @since 1.7.0 |
| | 57 | * @since 7.0.0 Added $args parameter. |
| 53 | 58 | * |
| 54 | 59 | * @param array $templates Array of templates located. |
| 55 | 60 | * @param string $slug Template part slug requested. |
| 56 | 61 | * @param string $name Template part name requested. |
| | 62 | * @param array $args Extra args to pass to locate_template(). |
| 57 | 63 | */ |
| 58 | | $templates = apply_filters( 'bp_get_template_part', $templates, $slug, $name ); |
| | 64 | $templates = apply_filters( 'bp_get_template_part', $templates, $slug, $name, $args ); |
| 59 | 65 | |
| 60 | 66 | // Return the part that is found. |
| 61 | | return bp_locate_template( $templates, true, false ); |
| | 67 | return bp_locate_template( $templates, true, false, $args ); |
| 62 | 68 | } |
| 63 | 69 | |
| 64 | 70 | /** |
| … |
… |
function bp_get_template_part( $slug, $name = null ) { |
| 68 | 74 | * prepended to the slug. |
| 69 | 75 | * |
| 70 | 76 | * @since 2.6.0 |
| | 77 | * @since 7.0.0 Added $args parameter. |
| 71 | 78 | * |
| 72 | 79 | * @see bp_get_template_part() for full documentation. |
| 73 | 80 | * |
| 74 | 81 | * @param string $slug Template slug. |
| 75 | 82 | * @param string|null $name Template name. |
| | 83 | * @param array $args Optional. Extra args to pass to locate_template(). |
| 76 | 84 | * @return false|string |
| 77 | 85 | */ |
| 78 | | function bp_get_asset_template_part( $slug, $name = null ) { |
| 79 | | return bp_get_template_part( "assets/{$slug}", $name ); |
| | 86 | function bp_get_asset_template_part( $slug, $name = null, $args = array() ) { |
| | 87 | return bp_get_template_part( "assets/{$slug}", $name, $args ); |
| 80 | 88 | } |
| 81 | 89 | |
| 82 | 90 | /** |
| … |
… |
function bp_get_asset_template_part( $slug, $name = null ) { |
| 87 | 95 | * not found in either of those, it looks in the theme-compat folder last. |
| 88 | 96 | * |
| 89 | 97 | * @since 1.7.0 |
| | 98 | * @since 7.0.0 Added $args parameter. |
| 90 | 99 | * |
| 91 | 100 | * @param string|array $template_names Template file(s) to search for, in order. |
| 92 | 101 | * @param bool $load Optional. If true, the template file will be loaded when |
| 93 | 102 | * found. If false, the path will be returned. Default: false. |
| 94 | 103 | * @param bool $require_once Optional. Whether to require_once or require. Has |
| 95 | 104 | * no effect if $load is false. Default: true. |
| | 105 | * @param array $args Optional. Extra args to pass to locate_template(). |
| 96 | 106 | * @return string The template filename if one is located. |
| 97 | 107 | */ |
| 98 | | function bp_locate_template( $template_names, $load = false, $require_once = true ) { |
| | 108 | function bp_locate_template( $template_names, $load = false, $require_once = true, $args = array() ) { |
| 99 | 109 | |
| 100 | 110 | // Bail when there are no templates to locate. |
| 101 | 111 | if ( empty( $template_names ) ) { |
| … |
… |
function bp_locate_template( $template_names, $load = false, $require_once = tru |
| 140 | 150 | * If you want to override a specific template part, please either filter |
| 141 | 151 | * 'bp_get_template_part' or add a new location to the template stack. |
| 142 | 152 | */ |
| 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 ); |
| 144 | 154 | |
| 145 | 155 | /** |
| 146 | 156 | * Filter here to allow/disallow template loading. |
| … |
… |
function bp_locate_template( $template_names, $load = false, $require_once = tru |
| 152 | 162 | $load_template = (bool) apply_filters( 'bp_locate_template_and_load', true ); |
| 153 | 163 | |
| 154 | 164 | if ( $load_template && $load && ! empty( $located ) ) { |
| 155 | | load_template( $located, $require_once ); |
| | 165 | load_template( $located, $require_once, $args ); |
| 156 | 166 | } |
| 157 | 167 | |
| 158 | 168 | return $located; |
| … |
… |
function bp_get_template_stack() { |
| 320 | 330 | * Put a template part into an output buffer, and return it. |
| 321 | 331 | * |
| 322 | 332 | * @since 1.7.0 |
| | 333 | * @since 7.0.0 Added $args parameter. |
| 323 | 334 | * |
| 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. |
| 325 | 336 | * |
| 326 | 337 | * @param string $slug See {@link bp_get_template_part()}. |
| 327 | 338 | * @param string|null $name See {@link bp_get_template_part()}. |
| 328 | 339 | * @param bool $echo If true, template content will be echoed. If false, |
| 329 | 340 | * returned. Default: true. |
| | 341 | * @param array $args See {@link bp_get_template_part()}. |
| 330 | 342 | * @return string|null If $echo, returns the template content. |
| 331 | 343 | */ |
| 332 | | function bp_buffer_template_part( $slug, $name = null, $echo = true ) { |
| | 344 | function bp_buffer_template_part( $slug, $name = null, $echo = true, $args = array() ) { |
| 333 | 345 | ob_start(); |
| 334 | 346 | |
| 335 | 347 | // Remove 'bp_replace_the_content' filter to prevent infinite loops. |
| 336 | 348 | remove_filter( 'the_content', 'bp_replace_the_content' ); |
| 337 | 349 | |
| 338 | | bp_get_template_part( $slug, $name ); |
| | 350 | bp_get_template_part( $slug, $name, $args ); |
| 339 | 351 | |
| 340 | 352 | // Remove 'bp_replace_the_content' filter to prevent infinite loops. |
| 341 | 353 | add_filter( 'the_content', 'bp_replace_the_content' ); |
diff --git tests/phpunit/assets/templates/index.php tests/phpunit/assets/templates/index.php
new file mode 100644
index 000000000..e4379cf4b
|
-
|
+
|
|
| | 1 | <?php |
| | 2 | echo (int) $args['test'] + 1; |
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 | */ |
| | 7 | class 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 | } |