Skip to:
Content

BuddyPress.org

source: trunk/src/bp-core/bp-core-template-loader.php

Last change on this file was 14013, checked in by imath, 16 months ago

Introduce a function to inform about whether BP was loaded from src

bp_is_running_from_src_subdirectory() informs whether BuddyPress was loaded from the src subdirectory (trunk version). This function exposes the 'bp_is_running_from_src_subdirectory' to let developers tests code that is only available when BuddyPress is built.

Props emaralive, espellcaste.

See #9210
Fixes #9224
Closes https://github.com/buddypress/buddypress/pull/358

File size: 25.5 KB
Line 
1<?php
2/**
3 * BuddyPress Template Functions.
4 *
5 * This file contains functions necessary to mirror the WordPress core template
6 * loading process. Many of those functions are not filterable, and even then
7 * would not be robust enough to predict where BuddyPress templates might exist.
8 *
9 * @package BuddyPress
10 * @subpackage Core
11 * @since 1.7.0
12 */
13
14// Exit if accessed directly.
15defined( 'ABSPATH' ) || exit;
16
17/**
18 * Get a BuddyPress template part for display in a theme.
19 *
20 * @since 1.7.0
21 * @since 7.0.0 Added `$args` parameter.
22 *
23 * @param string      $slug Template part slug. Used to generate filenames,
24 *                          eg 'friends' for 'friends.php'.
25 * @param string|null $name Optional. Template part name. Used to generate
26 *                          secondary filenames, eg 'personal' for 'activity-personal.php'.
27 * @param array       $args Optional. Extra args to pass to locate_template().
28 * @return false|string Path to located template. See {@link bp_locate_template()}.
29 */
30function bp_get_template_part( $slug, $name = null, $args = array() ) {
31
32        /**
33         * Fires at the start of bp_get_template_part().
34         *
35         * This is a variable hook that is dependent on the slug passed in.
36         *
37         * @since 1.7.0
38         * @since 7.0.0 Added $args parameter.
39         *
40         * @param string $slug Template part slug requested.
41         * @param string $name Template part name requested.
42         * @param array  $args Extra args to pass to locate_template().
43         */
44        do_action( 'get_template_part_' . $slug, $slug, $name, $args );
45
46        // Setup possible parts.
47        $templates = array();
48        if ( isset( $name ) ) {
49                $templates[] = $slug . '-' . $name . '.php';
50        }
51        $templates[] = $slug . '.php';
52
53        /**
54         * Filters the template parts to be loaded.
55         *
56         * @since 1.7.0
57         * @since 7.0.0 Added `$args` parameter.
58         *
59         * @param array  $templates Array of templates located.
60         * @param string $slug      Template part slug requested.
61         * @param string $name      Template part name requested.
62         * @param array  $args      Extra args to pass to locate_template().
63         */
64        $templates = apply_filters( 'bp_get_template_part', $templates, $slug, $name, $args );
65
66        // Return the part that is found.
67        return bp_locate_template( $templates, true, false, $args );
68}
69
70/**
71 * Get an asset template part.
72 *
73 * Basically the same as {@link bp_get_template_part()}, but with 'assets/'
74 * prepended to the slug.
75 *
76 * @since 2.6.0
77 * @since 7.0.0 Added `$args` parameter.
78 *
79 * @see bp_get_template_part() for full documentation.
80 *
81 * @param string      $slug Template slug.
82 * @param string|null $name Template name.
83 * @param array       $args Optional. Extra args to pass to locate_template().
84 * @return false|string
85 */
86function bp_get_asset_template_part( $slug, $name = null, $args = array() ) {
87        return bp_get_template_part( "assets/{$slug}", $name, $args );
88}
89
90/**
91 * Get a dynamic template part.
92 *
93 * @since 9.0.0
94 *
95 * @param string $template     The Template Pack's relative path to the templata.
96 *                             Optional.
97 * @param string $type         Whether to use the template for JavaScript or PHP.
98 *                             Optional. Defaults to `js`.
99 * @param array  $tokens       The data to use to customize the template. Optional.
100 * @param array  $allowed_tags The allowed tags to use. Optional.
101 * @return string HTML/JS output.
102 */
103function bp_get_dynamic_template_part( $template = '', $type = 'js', $tokens = array(), $allowed_tags = array() ) {
104        $template_string = '';
105
106        if ( ! $template ) {
107                return '';
108        }
109
110        // Use the BP Theme Compat API to allow template override.
111        $template_path = bp_locate_template( $template );
112        if ( $template_path ) {
113                ob_start();
114                load_template( $template_path, false );
115
116                $template_string = ob_get_clean();
117        }
118
119        if ( ! $template_string ) {
120                return '';
121        }
122
123        if ( ! $allowed_tags ) {
124                $allowed_tags = array(
125                        'li'   => array( 'class' => true ),
126                        'div'  => array( 'class' => true ),
127                        'span' => array( 'class' => true ),
128                        'a'    => array(
129                                'href'            => true,
130                                'class'           => true,
131                                'data-bp-tooltip' => true,
132                        ),
133                        'img'  => array(
134                                'src'     => true,
135                                'class'   => true,
136                                'loading' => true,
137                        ),
138                );
139        }
140
141        if ( 'js' !== $type ) {
142                $template_string = wp_kses( $template_string, $allowed_tags );
143
144                return bp_core_replace_tokens_in_text( $template_string, $tokens );
145        }
146
147        return $template_string;
148}
149
150/**
151 * Retrieve the name of the highest priority template file that exists.
152 *
153 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
154 * inherit from a parent theme can just overload one file. If the template is
155 * not found in either of those, it looks in the theme-compat folder last.
156 *
157 * @since 1.7.0
158 * @since 7.0.0 Added `$args` parameter.
159 *
160 * @param string|array $template_names Template file(s) to search for, in order.
161 * @param bool         $load           Optional. If true, the template file will be loaded when
162 *                                     found. If false, the path will be returned. Default: false.
163 * @param bool         $require_once   Optional. Whether to require_once or require. Has
164 *                                     no effect if $load is false. Default: true.
165 * @param array        $args           Optional. Extra args to pass to locate_template().
166 * @return string The template filename if one is located.
167 */
168function bp_locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
169
170        // Bail when there are no templates to locate.
171        if ( empty( $template_names ) ) {
172                return false;
173        }
174
175        // No file found yet.
176        $located            = false;
177        $template_locations = bp_get_template_stack();
178
179        // Try to find a template file.
180        foreach ( (array) $template_names as $template_name ) {
181
182                // Continue if template is empty.
183                if ( empty( $template_name ) ) {
184                        continue;
185                }
186
187                // Trim off any slashes from the template name.
188                $template_name = ltrim( $template_name, '/' );
189
190                // Loop through template stack.
191                foreach ( (array) $template_locations as $template_location ) {
192
193                        // Continue if $template_location is empty.
194                        if ( empty( $template_location ) ) {
195                                continue;
196                        }
197
198                        // Check child theme first.
199                        if ( file_exists( trailingslashit( $template_location ) . $template_name ) ) {
200                                $located = trailingslashit( $template_location ) . $template_name;
201                                break 2;
202                        }
203                }
204        }
205
206        /**
207         * This action exists only to follow the standard BuddyPress coding convention,
208         * and should not be used to short-circuit any part of the template locater.
209         *
210         * If you want to override a specific template part, please either filter
211         * 'bp_get_template_part' or add a new location to the template stack.
212         */
213        do_action( 'bp_locate_template', $located, $template_name, $template_names, $template_locations, $load, $require_once, $args );
214
215        /**
216         * Filter here to allow/disallow template loading.
217         *
218         * @since 2.5.0
219         *
220         * @param bool $load_template True to load the template, false otherwise.
221         */
222        $load_template = (bool) apply_filters( 'bp_locate_template_and_load', true );
223
224        if ( $load_template && $load && ! empty( $located ) ) {
225                load_template( $located, $require_once, $args );
226        }
227
228        return $located;
229}
230
231/**
232 * Get file data of the highest priority asset that exists.
233 *
234 * Similar to {@link bp_locate_template()}, but for files like CSS and JS.
235 *
236 * @since 2.6.0
237 *
238 * @param string $filename Relative filename to search for.
239 * @return false|array Array of asset data if one is located (includes absolute filepath and URI).
240 *                     Boolean false on failure.
241 */
242function bp_locate_template_asset( $filename ) {
243        // Ensure assets can be located when running from /src/.
244        if ( bp_is_running_from_src_subdirectory() ) {
245                $filename = str_replace( '.min', '', $filename );
246        }
247
248        // Use bp_locate_template() to find our asset.
249        $located = bp_locate_template( $filename, false );
250        if ( false === $located ) {
251                return false;
252        }
253
254        // Set up data array.
255        $data         = array();
256        $data['file'] = $data['uri'] = $located;
257
258        $find = array(
259                get_theme_root(),
260                bp_get_theme_compat_dir(),
261        );
262
263        $replace = array(
264                get_theme_root_uri(),
265                bp_get_theme_compat_url(),
266        );
267
268        // Make sure URI path is relative to site URL.
269        $data['uri'] = str_replace( $find, $replace, $data['uri'] );
270
271        return $data;
272}
273
274/**
275 * Register a new template stack location.
276 *
277 * This allows for templates to live in places beyond just the parent/child
278 * relationship, to allow for custom template locations. Used in conjunction
279 * with bp_locate_template(), this allows for easy template overrides.
280 *
281 * @since 1.7.0
282 *
283 * @param string $location_callback Callback function that returns the stack location.
284 * @param int    $priority          Optional. The priority parameter as passed to
285 *                                  add_filter(). Default: 10.
286 * @return bool See {@link add_filter()}.
287 */
288function bp_register_template_stack( $location_callback = '', $priority = 10 ) {
289
290        // Bail if no location, or function/method is not callable.
291        if ( empty( $location_callback ) || ! is_callable( $location_callback ) ) {
292                return false;
293        }
294
295        // Add location callback to template stack.
296        return add_filter( 'bp_template_stack', $location_callback, (int) $priority );
297}
298
299/**
300 * Deregister a previously registered template stack location.
301 *
302 * @since 1.7.0
303 *
304 * @see bp_register_template_stack()
305 *
306 * @param string $location_callback Callback function that returns the stack location.
307 * @param int    $priority          Optional. The priority parameter passed to
308 *                                  {@link bp_register_template_stack()}. Default: 10.
309 * @return bool See {@link remove_filter()}.
310 */
311function bp_deregister_template_stack( $location_callback = '', $priority = 10 ) {
312
313        // Bail if no location, or function/method is not callable.
314        if ( empty( $location_callback ) || ! is_callable( $location_callback ) ) {
315                return false;
316        }
317
318        // Add location callback to template stack.
319        return remove_filter( 'bp_template_stack', $location_callback, (int) $priority );
320}
321
322/**
323 * Get the "template stack", a list of registered directories where templates can be found.
324 *
325 * Calls the functions added to the 'bp_template_stack' filter hook, and return
326 * an array of the template locations.
327 *
328 * @since 1.7.0
329 *
330 * @see bp_register_template_stack()
331 *
332 * @global array $wp_filter         Stores all of the filters.
333 * @global array $merged_filters    Merges the filter hooks using this function.
334 * @global array $wp_current_filter Stores the list of current filters with
335 *                                  the current one last.
336 * @return array The filtered value after all hooked functions are applied to it.
337 */
338function bp_get_template_stack() {
339        global $wp_filter, $merged_filters, $wp_current_filter;
340
341        // Setup some default variables.
342        $tag   = 'bp_template_stack';
343        $args  = array();
344        $stack = array();
345
346        // Add 'bp_template_stack' to the current filter array.
347        $wp_current_filter[] = $tag;
348
349        // Sort.
350        if ( class_exists( 'WP_Hook' ) ) {
351                $filter = $wp_filter[ $tag ]->callbacks;
352        } else {
353                $filter = &$wp_filter[ $tag ];
354
355                if ( ! isset( $merged_filters[ $tag ] ) ) {
356                        ksort( $filter );
357                        $merged_filters[ $tag ] = true;
358                }
359        }
360
361        // Ensure we're always at the beginning of the filter array.
362        reset( $filter );
363
364        // Loop through 'bp_template_stack' filters, and call callback functions.
365        do {
366                foreach ( (array) current( $filter ) as $the_ ) {
367                        if ( ! is_null( $the_['function'] ) ) {
368                                $args[1] = $stack;
369                                $stack[] = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) );
370                        }
371                }
372        } while ( next( $filter ) !== false );
373
374        // Remove 'bp_template_stack' from the current filter array.
375        array_pop( $wp_current_filter );
376
377        // Remove empties and duplicates.
378        $stack = array_unique( array_filter( $stack ) );
379
380        /**
381         * Filters the "template stack" list of registered directories where templates can be found.
382         *
383         * @since 1.7.0
384         *
385         * @param array $stack Array of registered directories for template locations.
386         */
387        return (array) apply_filters( 'bp_get_template_stack', $stack );
388}
389
390/**
391 * Put a template part into an output buffer, and return it.
392 *
393 * @since 1.7.0
394 * @since 7.0.0 Added `$args` parameter.
395 *
396 * @see bp_get_template_part() for a description of $slug, $name and $args params.
397 *
398 * @param string      $slug See {@link bp_get_template_part()}.
399 * @param string|null $name See {@link bp_get_template_part()}.
400 * @param bool        $ret  If true, template content will be echoed. If false,
401 *                          returned. Default: true.
402 * @param array       $args See {@link bp_get_template_part()}.
403 * @return string|null If $echo, returns the template content.
404 */
405function bp_buffer_template_part( $slug, $name = null, $ret = true, $args = array() ) {
406        ob_start();
407
408        // Remove 'bp_replace_the_content' filter to prevent infinite loops.
409        remove_filter( 'the_content', 'bp_replace_the_content' );
410
411        bp_get_template_part( $slug, $name, $args );
412
413        // Remove 'bp_replace_the_content' filter to prevent infinite loops.
414        add_filter( 'the_content', 'bp_replace_the_content' );
415
416        // Get the output buffer contents.
417        $output = ob_get_clean();
418
419        // Echo or return the output buffer contents.
420        if ( true === $ret ) {
421                // phpcs:ignore WordPress.Security.EscapeOutput
422                echo $output;
423        } else {
424                return $output;
425        }
426}
427
428/**
429 * Retrieve the path to a template.
430 *
431 * Used to quickly retrieve the path of a template without including the file
432 * extension. It will also check the parent theme and theme-compat theme with
433 * the use of {@link bp_locate_template()}. Allows for more generic template
434 * locations without the use of the other get_*_template() functions.
435 *
436 * @since 1.7.0
437 *
438 * @param string $type      Filename without extension.
439 * @param array  $templates An optional list of template candidates.
440 * @return string Full path to file.
441 */
442function bp_get_query_template( $type, $templates = array() ) {
443        $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
444
445        if ( empty( $templates ) ) {
446                $templates = array( "{$type}.php" );
447        }
448
449        /**
450         * Filters possible file paths to check for for a template.
451         *
452         * This is a variable filter based on the type passed into
453         * bp_get_query_template.
454         *
455         * @since 1.7.0
456         *
457         * @param array $templates Array of template files already prepared.
458         */
459        $templates = apply_filters( "bp_get_{$type}_template", $templates );
460
461        /*
462         * Filter possible templates, try to match one, and set any BuddyPress theme
463         * compat properties so they can be cross-checked later.
464         */
465        $templates = bp_set_theme_compat_templates( $templates );
466        $template  = bp_locate_template( $templates );
467
468        /*
469         * The current theme is using the WordPress Full Site Editing feature.
470         * BuddyPress then needs to use the WordPress template canvas to retrieve the community content.
471         */
472        if ( wp_is_block_theme() ) {
473                $template = ABSPATH . WPINC . '/template-canvas.php';
474        }
475
476        $template = bp_set_theme_compat_template( $template );
477
478        /**
479         * Filters the path to a template file.
480         *
481         * This is a variable filter based on the type passed into
482         * bp_get_query_template.
483         *
484         * @since 1.7.0
485         *
486         * @param string $template Path to the most appropriate found template file.
487         */
488        return apply_filters( "bp_{$type}_template", $template );
489}
490
491/**
492 * Get the possible subdirectories to check for templates in.
493 *
494 * @since 1.7.0
495 *
496 * @param array $templates Templates we are looking for.
497 * @return array Possible subfolders to look in.
498 */
499function bp_get_template_locations( $templates = array() ) {
500        $locations = array(
501                'buddypress',
502                'community',
503                '',
504        );
505
506        /**
507         * Filters the possible subdirectories to check for templates in.
508         *
509         * @since 1.7.0
510         *
511         * @param array $locations Array of subfolders to look in.
512         * @param array $templates Array of templates we are looking for.
513         */
514        return apply_filters( 'bp_get_template_locations', $locations, $templates );
515}
516
517/**
518 * Add template locations to template files being searched for.
519 *
520 * @since 1.7.0
521 *
522 * @param array $stacks Array of template locations.
523 * @return array Array of all template locations registered so far.
524 */
525function bp_add_template_stack_locations( $stacks = array() ) {
526        $retval = array();
527
528        // Get alternate locations.
529        $locations = bp_get_template_locations();
530
531        // Loop through locations and stacks and combine.
532        foreach ( (array) $stacks as $stack ) {
533                foreach ( (array) $locations as $custom_location ) {
534                        $retval[] = untrailingslashit( trailingslashit( $stack ) . $custom_location );
535                }
536        }
537
538        /**
539         * Filters the template locations to template files being searched for.
540         *
541         * @since 1.7.0
542         *
543         * @param array $template_locations  Array of all template locations registered so far.
544         * @param array $stacks Array of template locations.
545         */
546        return apply_filters( 'bp_add_template_stack_locations', array_unique( $retval ), $stacks );
547}
548
549/**
550 * Add checks for BuddyPress conditions to 'parse_query' action.
551 *
552 * @since 1.7.0
553 *
554 * @param WP_Query $posts_query WP_Query object.
555 */
556function bp_parse_query( $posts_query ) {
557        // Only run on the root site or if multiblog mode is on.
558        if ( ! bp_is_root_blog() && ! bp_is_multiblog_mode() ) {
559                return;
560        }
561
562        // Bail if $posts_query is not the main loop.
563        if ( ! $posts_query->is_main_query() ) {
564                return;
565        }
566
567        // Bail if filters are suppressed on this query.
568        if ( true === $posts_query->get( 'suppress_filters' ) ) {
569                return;
570        }
571
572        // Bail if in admin.
573        if ( is_admin() ) {
574                return;
575        }
576
577        // Eventually Set some missing URI globals.
578        $bp = buddypress();
579
580        if ( ! $bp->unfiltered_uri ) {
581                $unfiltered_uri = explode( '/', $GLOBALS['wp']->request );
582
583                // Make sure to set the BP unfiltered_uri global when plain links are used.
584                if ( ! bp_has_pretty_urls() ) {
585                        $bp_directories = array();
586                        foreach ( $bp->pages as $component_id => $page_properties ) {
587                                if ( isset( $bp->{$component_id}->rewrite_ids['directory'] ) ) {
588                                        $bp_directories[ $bp->{$component_id}->rewrite_ids['directory'] ] = $page_properties->slug;
589                                } elseif ( 'activate' === $component_id || 'register' === $component_id ) {
590                                        $bp_directories[ 'bp_member_' . $component_id ] = $page_properties->slug;
591                                }
592                        }
593
594                        $url_query_chunks = bp_parse_args( $GLOBALS['wp']->query_string, array() );
595                        $directory        = key( $url_query_chunks );
596                        if ( isset( $bp_directories[ $directory ] ) ) {
597                                $url_query_chunks[ $directory ] = $bp_directories[ $directory ];
598                        }
599
600                        $unfiltered_uri = array_values( $url_query_chunks );
601                }
602
603                $bp->unfiltered_uri        = $unfiltered_uri;
604                $bp->unfiltered_uri_offset = 0;
605        }
606
607        /**
608         * Fires at the end of the bp_parse_query function.
609         *
610         * Allow BuddyPress components to parse the main query.
611         *
612         * @since 1.7.0
613         *
614         * @param WP_Query $posts_query WP_Query instance. Passed by reference.
615         */
616        do_action_ref_array( 'bp_parse_query', array( &$posts_query ) );
617}
618
619/**
620 * Parse the query for the Ajax context.
621 *
622 * @since 12.0.0
623 *
624 * @param WP_Query $referer_query WP_Query object.
625 */
626function bp_parse_ajax_referer_query( $referer_query ) {
627        if ( ! wp_doing_ajax() || 'rewrites' !== bp_core_get_query_parser() ) {
628                return;
629        }
630
631        if ( isset( $_POST['action'] ) && 'heartbeat' === $_POST['action'] && empty( $_POST['data']['bp_heartbeat'] ) ) {
632                return;
633        }
634
635        // Prevent doing this again.
636        remove_action( 'parse_query', 'bp_parse_ajax_referer_query', 2 );
637
638        /**
639         * Fires at the end of the bp_parse_ajax_referer_query function.
640         *
641         * Allow BuddyPress components to parse the ajax referer query.
642         *
643         * @since 12.0.0
644         *
645         * @param WP_Query $referer_query WP_Query instance. Passed by reference.
646         */
647        do_action_ref_array( 'bp_parse_query', array( &$referer_query ) );
648}
649
650/**
651 * Resets the query to fit our permalink structure if needed.
652 *
653 * This is used for specific cases such as Root Member's profile.
654 *
655 * @since 12.0.0
656 *
657 * @global WP $wp WordPress main instance.
658 *
659 * @param string   $bp_request A specific BuddyPress request.
660 * @param WP_Query $query The WordPress query object.
661 * @return true
662 */
663function bp_reset_query( $bp_request = '', $query = null ) {
664        global $wp;
665
666        // Get BuddyPress main instance.
667        $bp = buddypress();
668
669        // Back up request uri.
670        $reset_server_request_uri = '';
671        if ( isset( $_SERVER['REQUEST_URI'] ) ) {
672                $reset_server_request_uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) );
673        }
674
675        // Use the BP Rewrites API to parse the ajax referer request.
676        if ( wp_doing_ajax() ) {
677                if ( ! bp_has_pretty_urls() ) {
678                        $matched_query = wp_parse_url( $bp_request, PHP_URL_QUERY );
679                } else {
680                        // Temporarily override the request uri.
681                        $_SERVER['REQUEST_URI'] = $bp_request;
682
683                        $wp_ajax = new WP();
684                        $wp_ajax->parse_request();
685
686                        // Extra step to check for root profiles.
687                        $member = bp_rewrites_get_member_data( $wp_ajax->request );
688                        if ( isset( $member['object'] ) && $member['object'] ) {
689                                $_SERVER['REQUEST_URI'] = trailingslashit( $bp->members->root_slug ) . $wp_ajax->request;
690
691                                // Reparse the request.
692                                $wp_ajax->parse_request();
693                        }
694
695                        $matched_query = $wp_ajax->matched_query;
696                }
697
698                // Use a specific function to fire the `bp_parse_query` hook.
699                add_action( 'parse_query', 'bp_parse_ajax_referer_query', 2 );
700
701                // Parse the matched query.
702                $query->parse_query( $matched_query );
703
704                // Use to request in case of root profiles.
705        } elseif ( isset( $wp->request ) ) {
706                // Temporarily override the request uri.
707                $_SERVER['REQUEST_URI'] = str_replace( $wp->request, $bp_request, $reset_server_request_uri );
708
709                // Reparse request.
710                $wp->parse_request();
711
712                // Reparse query.
713                bp_remove_all_filters( 'parse_query' );
714                $query->parse_query( $wp->query_vars );
715                bp_restore_all_filters( 'parse_query' );
716        }
717
718        // Restore request uri.
719        $_SERVER['REQUEST_URI'] = $reset_server_request_uri;
720
721        // The query is reset.
722        return true;
723}
724
725/**
726 * Possibly intercept the template being loaded.
727 *
728 * Listens to the 'template_include' filter and waits for any BuddyPress specific
729 * template condition to be met. If one is met and the template file exists,
730 * it will be used; otherwise.
731 *
732 * Note that the _edit() checks are ahead of their counterparts, to prevent them
733 * from being stomped on accident.
734 *
735 * @since 1.7.0
736 *
737 * @param string $template The path to the template file that is being used.
738 * @return string The path to the template file that is being used.
739 */
740function bp_template_include_theme_supports( $template = '' ) {
741
742        /**
743         * Filters whether or not to override the template being loaded in parent/child themes.
744         *
745         * @since 1.7.0
746         *
747         * @param bool   $value    Whether or not there is a file override. Default false.
748         * @param string $template The path to the template file that is being used.
749         */
750        $new_template = apply_filters( 'bp_get_root_template', false, $template );
751
752        // A BuddyPress template file was located, so override the WordPress
753        // template and use it to switch off BuddyPress's theme compatibility.
754        if ( ! empty( $new_template ) ) {
755                $template = bp_set_template_included( $new_template );
756        }
757
758        /**
759         * Filters the final template being loaded in parent/child themes.
760         *
761         * @since 1.7.0
762         *
763         * @param string $template The path to the template file that is being used.
764         */
765        return apply_filters( 'bp_template_include_theme_supports', $template );
766}
767
768/**
769 * Set the included template.
770 *
771 * @since 1.8.0
772 *
773 * @param mixed $template Default: false.
774 * @return mixed False if empty. Template name if template included.
775 */
776function bp_set_template_included( $template = false ) {
777        buddypress()->theme_compat->found_template = $template;
778
779        return buddypress()->theme_compat->found_template;
780}
781
782/**
783 * Is a BuddyPress template being included?
784 *
785 * @since 1.8.0
786 *
787 * @return bool
788 */
789function bp_is_template_included() {
790        return isset( buddypress()->theme_compat->found_template ) && buddypress()->theme_compat->found_template;
791}
792
793/**
794 * Attempt to load a custom BP functions file, similar to each themes functions.php file.
795 *
796 * @since 1.7.0
797 *
798 * @global string $pagenow The current page being loaded.
799 * @global WP_Query $wp_query The WordPress Query object.
800 */
801function bp_load_theme_functions() {
802        global $pagenow, $wp_query;
803
804        // Do not load our custom BP functions file if theme compat is disabled.
805        if ( ! bp_use_theme_compat_with_current_theme() ) {
806                return;
807        }
808
809        // Do not include on BuddyPress deactivation.
810        if ( bp_is_deactivation() ) {
811                return;
812        }
813
814        // If the $wp_query global is empty (the main query has not been run,
815        // or has been reset), load_template() will fail at setting certain
816        // global values. This does not happen on a normal page load, but can
817        // cause problems when running automated tests.
818        if ( ! is_a( $wp_query, 'WP_Query' ) ) {
819                return;
820        }
821
822        // Only include if not installing or if activating via wp-activate.php.
823        if ( ! defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ) {
824                bp_locate_template( 'buddypress-functions.php', true );
825        }
826}
827
828/**
829 * Get the templates to use as the endpoint for BuddyPress template parts.
830 *
831 * @since 1.7.0
832 * @since 2.4.0 Added singular.php to stack
833 *
834 * @return string Possible root level wrapper template files.
835 */
836function bp_get_theme_compat_templates() {
837        return bp_get_query_template(
838                'buddypress',
839                array(
840                        'plugin-buddypress.php',
841                        'buddypress.php',
842                        'community.php',
843                        'generic.php',
844                        'page.php',
845                        'single.php',
846                        'singular.php',
847                        'index.php',
848                )
849        );
850}
851
852/**
853 * Sets Block Theme compatibility if it supports BuddyPress.
854 *
855 * @since 14.0.0
856 */
857function bp_set_block_theme_compat() {
858        if ( wp_is_block_theme() && current_theme_supports( 'buddypress' ) ) {
859                bp_deregister_template_stack( 'get_stylesheet_directory', 10 );
860                bp_deregister_template_stack( 'get_template_directory', 12 );
861
862                $block_theme     = wp_get_theme();
863                $theme_compat_id = $block_theme->stylesheet;
864
865                bp_register_theme_package(
866                        array(
867                                'id'             => $theme_compat_id,
868                                'name'           => $block_theme->get( 'Name' ),
869                                'version'        => $block_theme->get( 'Version' ),
870                                'dir'            => '',
871                                'url'            => '',
872                                'is_block_theme' => true,
873                        )
874                );
875
876                bp_setup_theme_compat( $theme_compat_id );
877        }
878}
Note: See TracBrowser for help on using the repository browser.