PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 All 72 releases
← All changes | includes/core/theme-compat.php +561 -490 2.22.6.17 View file →
@@ -1,15 +1,15 @@
1 1 <?php
2 2
3 3 /**
4 - * bbPress Core Theme Compatibility
4 + * bbPress Theme Compatibility
5 5 *
6 6 * @package bbPress
7 - * @subpackage ThemeCompatibility
7 + * @subpackage Core
8 8 */
9 9
10 10 // Exit if accessed directly
11 -if ( !defined( 'ABSPATH' ) ) exit;
11 +defined( 'ABSPATH' ) || exit;
12 12
13 13 /** Theme Compat **************************************************************/
14 14
15 15 /**
@@ -27,12 +27,11 @@
27 27 /**
28 28 * Theme Compatibility base class
29 29 *
30 30 * This is only intended to be extended, and is included here as a basic guide
31 - * for future Theme Packs to use. @link BBP_Twenty_Ten is a good example of
32 - * extending this class, as is @link bbp_setup_theme_compat()
31 + * for future Template Packs to use. @link bbp_setup_theme_compat()
33 32 *
34 - * @since bbPress (r3506)
33 + * @since 2.0.0 bbPress (r3506)
35 34 */
36 35 class BBP_Theme_Compat {
37 36
38 37 /**
@@ -44,9 +43,9 @@
44 43 * 'version' => Theme version for cache busting scripts and styling
45 44 * 'dir' => Path to theme
46 45 * 'url' => URL to theme
47 46 * );
48 - * @var array
47 + * @var array
49 48 */
50 49 private $_data = array();
51 50
52 51 /**
@@ -51,12 +50,13 @@
51 50
52 51 /**
53 52 * Pass the $properties to the object on creation.
54 53 *
55 - * @since bbPress (r3926)
54 + * @since 2.1.0 bbPress (r3926)
55 + *
56 56 * @param array $properties
57 57 */
58 - public function __construct( Array $properties = array() ) {
58 + public function __construct( array $properties = array() ) {
59 59 $this->_data = $properties;
60 60 }
61 61
62 62 /**
@@ -61,131 +61,188 @@
61 61
62 62 /**
63 63 * Set a theme's property.
64 64 *
65 - * @since bbPress (r3926)
65 + * @since 2.1.0 bbPress (r3926)
66 + *
66 67 * @param string $property
67 68 * @param mixed $value
68 69 * @return mixed
69 70 */
70 71 public function __set( $property, $value ) {
71 - return $this->_data[$property] = $value;
72 + return $this->_data[ $property ] = $value;
72 73 }
73 74
74 75 /**
75 76 * Get a theme's property.
76 77 *
77 - * @since bbPress (r3926)
78 + * @since 2.1.0 bbPress (r3926)
79 + *
78 80 * @param string $property
79 81 * @param mixed $value
80 82 * @return mixed
81 83 */
82 84 public function __get( $property ) {
83 - return array_key_exists( $property, $this->_data ) ? $this->_data[$property] : '';
85 + return array_key_exists( $property, $this->_data )
86 + ? $this->_data[ $property ]
87 + : '';
84 88 }
89 +
90 + /**
91 + * Return the template directory.
92 + *
93 + * @since 2.6.0 bbPress (r6548)
94 + *
95 + * @return string
96 + */
97 + public function get_dir() {
98 + return $this->dir;
99 + }
85 100 }
86 101
87 102 /** Functions *****************************************************************/
88 103
89 104 /**
90 - * Setup the default theme compat theme
105 + * Setup the active template pack and register it's directory in the stack.
91 106 *
92 - * @since bbPress (r3311)
107 + * @since 2.0.0 bbPress (r3311)
108 + *
93 109 * @param BBP_Theme_Compat $theme
94 110 */
95 -function bbp_setup_theme_compat( $theme = '' ) {
111 +function bbp_setup_theme_compat( $theme = 'default' ) {
96 112 $bbp = bbpress();
97 113
98 - // Make sure theme package is available, set to default if not
99 - if ( ! isset( $bbp->theme_compat->packages[$theme] ) || ! is_a( $bbp->theme_compat->packages[$theme], 'BBP_Theme_Compat' ) ) {
114 + // Bail if something already has this under control
115 + if ( ! empty( $bbp->theme_compat->theme ) ) {
116 + return;
117 + }
118 +
119 + // Fallback for empty theme
120 + if ( empty( $theme ) ) {
100 121 $theme = 'default';
101 122 }
102 123
103 - // Set the active theme compat theme
104 - $bbp->theme_compat->theme = $bbp->theme_compat->packages[$theme];
124 + // If the theme is registered, use it and add it to the stack
125 + if ( isset( $bbp->theme_compat->packages[ $theme ] ) ) {
126 + $bbp->theme_compat->theme = $bbp->theme_compat->packages[ $theme ];
127 +
128 + // Setup the template stack for the active template pack
129 + bbp_register_template_stack( array( $bbp->theme_compat->theme, 'get_dir' ) );
130 + }
105 131 }
106 132
107 133 /**
108 - * Gets the name of the bbPress compatable theme used, in the event the
134 + * Get the current template pack package.
135 + *
136 + * @since 2.6.0 bbPress (r6548)
137 + *
138 + * @return BBP_Theme_Compat
139 + */
140 +function bbp_get_current_template_pack() {
141 + $bbp = bbpress();
142 +
143 + // Theme was not setup, so fallback to an empty object
144 + if ( empty( $bbp->theme_compat->theme ) ) {
145 + $bbp->theme_compat->theme = new BBP_Theme_Compat();
146 + }
147 +
148 + // Filter & return
149 + return apply_filters( 'bbp_get_current_template_pack', $bbp->theme_compat->theme );
150 +}
151 +
152 +/**
153 + * Gets the id of the bbPress compatible theme used, in the event the
109 154 * currently active WordPress theme does not explicitly support bbPress.
110 155 * This can be filtered or set manually. Tricky theme authors can override the
111 - * default and include their own bbPress compatability layers for their themes.
156 + * default and include their own bbPress compatibility layers for their themes.
112 157 *
113 - * @since bbPress (r3506)
114 - * @uses apply_filters()
158 + * @since 2.0.0 bbPress (r3506)
159 + *
115 160 * @return string
116 161 */
117 162 function bbp_get_theme_compat_id() {
118 - return apply_filters( 'bbp_get_theme_compat_id', bbpress()->theme_compat->theme->id );
163 +
164 + // Filter & return
165 + return apply_filters( 'bbp_get_theme_compat_id', bbp_get_current_template_pack()->id );
119 166 }
120 167
121 168 /**
122 - * Gets the name of the bbPress compatable theme used, in the event the
169 + * Gets the name of the bbPress compatible theme used, in the event the
123 170 * currently active WordPress theme does not explicitly support bbPress.
124 171 * This can be filtered or set manually. Tricky theme authors can override the
125 - * default and include their own bbPress compatability layers for their themes.
172 + * default and include their own bbPress compatibility layers for their themes.
126 173 *
127 - * @since bbPress (r3506)
128 - * @uses apply_filters()
174 + * @since 2.0.0 bbPress (r3506)
175 + *
129 176 * @return string
130 177 */
131 178 function bbp_get_theme_compat_name() {
132 - return apply_filters( 'bbp_get_theme_compat_name', bbpress()->theme_compat->theme->name );
179 +
180 + // Filter & return
181 + return apply_filters( 'bbp_get_theme_compat_name', bbp_get_current_template_pack()->name );
133 182 }
134 183
135 184 /**
136 - * Gets the version of the bbPress compatable theme used, in the event the
185 + * Gets the version of the bbPress compatible theme used, in the event the
137 186 * currently active WordPress theme does not explicitly support bbPress.
138 187 * This can be filtered or set manually. Tricky theme authors can override the
139 - * default and include their own bbPress compatability layers for their themes.
188 + * default and include their own bbPress compatibility layers for their themes.
140 189 *
141 - * @since bbPress (r3506)
142 - * @uses apply_filters()
190 + * @since 2.0.0 bbPress (r3506)
191 + *
143 192 * @return string
144 193 */
145 194 function bbp_get_theme_compat_version() {
146 - return apply_filters( 'bbp_get_theme_compat_version', bbpress()->theme_compat->theme->version );
195 +
196 + // Filter & return
197 + return apply_filters( 'bbp_get_theme_compat_version', bbp_get_current_template_pack()->version );
147 198 }
148 199
149 200 /**
150 - * Gets the bbPress compatable theme used in the event the currently active
201 + * Gets the bbPress compatible theme used in the event the currently active
151 202 * WordPress theme does not explicitly support bbPress. This can be filtered,
152 203 * or set manually. Tricky theme authors can override the default and include
153 - * their own bbPress compatability layers for their themes.
204 + * their own bbPress compatibility layers for their themes.
154 205 *
155 - * @since bbPress (r3032)
156 - * @uses apply_filters()
206 + * @since 2.0.0 bbPress (r3032)
207 + *
157 208 * @return string
158 209 */
159 210 function bbp_get_theme_compat_dir() {
160 - return apply_filters( 'bbp_get_theme_compat_dir', bbpress()->theme_compat->theme->dir );
211 +
212 + // Filter & return
213 + return apply_filters( 'bbp_get_theme_compat_dir', bbp_get_current_template_pack()->dir );
161 214 }
162 215
163 216 /**
164 - * Gets the bbPress compatable theme used in the event the currently active
217 + * Gets the bbPress compatible theme used in the event the currently active
165 218 * WordPress theme does not explicitly support bbPress. This can be filtered,
166 219 * or set manually. Tricky theme authors can override the default and include
167 - * their own bbPress compatability layers for their themes.
220 + * their own bbPress compatibility layers for their themes.
168 221 *
169 - * @since bbPress (r3032)
170 - * @uses apply_filters()
222 + * @since 2.0.0 bbPress (r3032)
223 + *
171 224 * @return string
172 225 */
173 226 function bbp_get_theme_compat_url() {
174 - return apply_filters( 'bbp_get_theme_compat_url', bbpress()->theme_compat->theme->url );
227 +
228 + // Filter & return
229 + return apply_filters( 'bbp_get_theme_compat_url', bbp_get_current_template_pack()->url );
175 230 }
176 231
177 232 /**
178 233 * Gets true/false if page is currently inside theme compatibility
179 234 *
180 - * @since bbPress (r3265)
235 + * @since 2.0.0 bbPress (r3265)
236 + *
181 237 * @return bool
182 238 */
183 239 function bbp_is_theme_compat_active() {
184 240 $bbp = bbpress();
185 241
186 - if ( empty( $bbp->theme_compat->active ) )
242 + if ( empty( $bbp->theme_compat->active ) ) {
187 243 return false;
244 + }
188 245
189 246 return $bbp->theme_compat->active;
190 247 }
191 248
@@ -191,9 +248,10 @@
191 248
192 249 /**
193 250 * Sets true/false if page is currently inside theme compatibility
194 251 *
195 - * @since bbPress (r3265)
252 + * @since 2.0.0 bbPress (r3265)
253 + *
196 254 * @param bool $set
197 255 * @return bool
198 256 */
199 257 function bbp_set_theme_compat_active( $set = true ) {
@@ -207,9 +265,9 @@
207 265 *
208 266 * Stash possible template files for the current query. Useful if plugins want
209 267 * to override them, or see what files are being scanned for inclusion.
210 268 *
211 - * @since bbPress (r3311)
269 + * @since 2.0.0 bbPress (r3311)
212 270 */
213 271 function bbp_set_theme_compat_templates( $templates = array() ) {
214 272 bbpress()->theme_compat->templates = $templates;
215 273
@@ -221,9 +279,9 @@
221 279 *
222 280 * Stash the template file for the current query. Useful if plugins want
223 281 * to override it, or see what file is being included.
224 282 *
225 - * @since bbPress (r3311)
283 + * @since 2.0.0 bbPress (r3311)
226 284 */
227 285 function bbp_set_theme_compat_template( $template = '' ) {
228 286 bbpress()->theme_compat->template = $template;
229 287
@@ -235,9 +293,9 @@
235 293 *
236 294 * Stash the original template file for the current query. Useful for checking
237 295 * if bbPress was able to find a more appropriate template.
238 296 *
239 - * @since bbPress (r3926)
297 + * @since 2.1.0 bbPress (r3926)
240 298 */
241 299 function bbp_set_theme_compat_original_template( $template = '' ) {
242 300 bbpress()->theme_compat->original_template = $template;
243 301
@@ -244,53 +302,60 @@
244 302 return bbpress()->theme_compat->original_template;
245 303 }
246 304
247 305 /**
248 - * Set the theme compat original_template global
306 + * Is a template the original_template global
249 307 *
250 308 * Stash the original template file for the current query. Useful for checking
251 309 * if bbPress was able to find a more appropriate template.
252 310 *
253 - * @since bbPress (r3926)
311 + * @since 2.1.0 bbPress (r3926)
254 312 */
255 313 function bbp_is_theme_compat_original_template( $template = '' ) {
256 314 $bbp = bbpress();
257 315
258 - if ( empty( $bbp->theme_compat->original_template ) )
316 + // Bail if no original template
317 + if ( empty( $bbp->theme_compat->original_template ) ) {
259 318 return false;
319 + }
260 320
261 - return (bool) ( $bbp->theme_compat->original_template == $template );
321 + return (bool) ( $bbp->theme_compat->original_template === $template );
262 322 }
263 323
264 324 /**
265 325 * Register a new bbPress theme package to the active theme packages array
266 326 *
267 - * @since bbPress (r3829)
327 + * @since 2.1.0 bbPress (r3829)
328 + *
268 329 * @param array $theme
269 330 */
270 331 function bbp_register_theme_package( $theme = array(), $override = true ) {
271 332
272 333 // Create new BBP_Theme_Compat object from the $theme array
273 - if ( is_array( $theme ) )
334 + if ( is_array( $theme ) ) {
274 335 $theme = new BBP_Theme_Compat( $theme );
336 + }
275 337
276 338 // Bail if $theme isn't a proper object
277 - if ( ! is_a( $theme, 'BBP_Theme_Compat' ) )
339 + if ( ! is_a( $theme, 'BBP_Theme_Compat' ) ) {
278 340 return;
341 + }
279 342
280 343 // Load up bbPress
281 344 $bbp = bbpress();
282 345
283 346 // Only override if the flag is set and not previously registered
284 - if ( empty( $bbp->theme_compat->packages[$theme->id] ) || ( true === $override ) ) {
285 - $bbp->theme_compat->packages[$theme->id] = $theme;
347 + if ( empty( $bbp->theme_compat->packages[ $theme->id ] ) || ( true === $override ) ) {
348 + $bbp->theme_compat->packages[ $theme->id ] = $theme;
286 349 }
287 350 }
351 +
288 352 /**
289 353 * This fun little function fills up some WordPress globals with dummy data to
290 354 * stop your average page template from complaining about it missing.
291 355 *
292 - * @since bbPress (r3108)
356 + * @since 2.0.0 bbPress (r3108)
357 + *
293 358 * @global WP_Query $wp_query
294 359 * @global object $post
295 360 * @param array $args
296 361 */
@@ -296,43 +361,12 @@
296 361 */
297 362 function bbp_theme_compat_reset_post( $args = array() ) {
298 363 global $wp_query, $post;
299 364
300 - // Default arguments
301 - $defaults = array(
302 - 'ID' => -9999,
303 - 'post_status' => bbp_get_public_status_id(),
304 - 'post_author' => 0,
305 - 'post_parent' => 0,
306 - 'post_type' => 'page',
307 - 'post_date' => 0,
308 - 'post_date_gmt' => 0,
309 - 'post_modified' => 0,
310 - 'post_modified_gmt' => 0,
311 - 'post_content' => '',
312 - 'post_title' => '',
313 - 'post_excerpt' => '',
314 - 'post_content_filtered' => '',
315 - 'post_mime_type' => '',
316 - 'post_password' => '',
317 - 'post_name' => '',
318 - 'guid' => '',
319 - 'menu_order' => 0,
320 - 'pinged' => '',
321 - 'to_ping' => '',
322 - 'ping_status' => '',
323 - 'comment_status' => 'closed',
324 - 'comment_count' => 0,
365 + // Switch defaults if post is set
366 + if ( isset( $wp_query->post ) ) {
325 367
326 - 'is_404' => false,
327 - 'is_page' => false,
328 - 'is_single' => false,
329 - 'is_archive' => false,
330 - 'is_tax' => false,
331 - );
332 -
333 - // Switch defaults if post is set
334 - if ( isset( $wp_query->post ) ) {
368 + // Use primarily Post attributes
335 369 $defaults = array(
336 370 'ID' => $wp_query->post->ID,
337 371 'post_status' => $wp_query->post->post_status,
338 372 'post_author' => $wp_query->post->post_author,
@@ -355,54 +389,72 @@
355 389 'to_ping' => $wp_query->post->to_ping,
356 390 'ping_status' => $wp_query->post->ping_status,
357 391 'comment_status' => $wp_query->post->comment_status,
358 392 'comment_count' => $wp_query->post->comment_count,
393 + 'filter' => $wp_query->post->filter,
359 394
360 - 'is_404' => false,
361 - 'is_page' => false,
362 - 'is_single' => false,
363 - 'is_archive' => false,
364 - 'is_tax' => false,
395 + 'is_404' => false,
396 + 'is_page' => false,
397 + 'is_single' => false,
398 + 'is_archive' => false,
399 + 'is_tax' => false
365 400 );
401 + } else {
402 +
403 + // Get the default zero date value a single time
404 + $zero_date = bbp_get_empty_datetime();
405 +
406 + // Use primarily empty attributes
407 + $defaults = array(
408 + 'ID' => -9999,
409 + 'post_status' => bbp_get_public_status_id(),
410 + 'post_author' => 0,
411 + 'post_parent' => 0,
412 + 'post_type' => 'page',
413 + 'post_date' => $zero_date,
414 + 'post_date_gmt' => $zero_date,
415 + 'post_modified' => $zero_date,
416 + 'post_modified_gmt' => $zero_date,
417 + 'post_content' => '',
418 + 'post_title' => '',
419 + 'post_excerpt' => '',
420 + 'post_content_filtered' => '',
421 + 'post_mime_type' => '',
422 + 'post_password' => '',
423 + 'post_name' => '',
424 + 'guid' => '',
425 + 'menu_order' => 0,
426 + 'pinged' => '',
427 + 'to_ping' => '',
428 + 'ping_status' => '',
429 + 'comment_status' => 'closed',
430 + 'comment_count' => 0,
431 + 'filter' => 'raw',
432 +
433 + 'is_404' => false,
434 + 'is_page' => false,
435 + 'is_single' => false,
436 + 'is_archive' => false,
437 + 'is_tax' => false
438 + );
366 439 }
440 +
441 + // Parse & filter
367 442 $dummy = bbp_parse_args( $args, $defaults, 'theme_compat_reset_post' );
368 443
369 - // Clear out the post related globals
370 - unset( $wp_query->posts );
371 - unset( $wp_query->post );
372 - unset( $post );
444 + // Bail if dummy post is empty
445 + if ( empty( $dummy ) ) {
446 + return;
447 + }
373 448
374 - // Setup the dummy post object
375 - $wp_query->post = new stdClass;
376 - $wp_query->post->ID = $dummy['ID'];
377 - $wp_query->post->post_status = $dummy['post_status'];
378 - $wp_query->post->post_author = $dummy['post_author'];
379 - $wp_query->post->post_parent = $dummy['post_parent'];
380 - $wp_query->post->post_type = $dummy['post_type'];
381 - $wp_query->post->post_date = $dummy['post_date'];
382 - $wp_query->post->post_date_gmt = $dummy['post_date_gmt'];
383 - $wp_query->post->post_modified = $dummy['post_modified'];
384 - $wp_query->post->post_modified_gmt = $dummy['post_modified_gmt'];
385 - $wp_query->post->post_content = $dummy['post_content'];
386 - $wp_query->post->post_title = $dummy['post_title'];
387 - $wp_query->post->post_excerpt = $dummy['post_excerpt'];
388 - $wp_query->post->post_content_filtered = $dummy['post_content_filtered'];
389 - $wp_query->post->post_mime_type = $dummy['post_mime_type'];
390 - $wp_query->post->post_password = $dummy['post_password'];
391 - $wp_query->post->post_name = $dummy['post_name'];
392 - $wp_query->post->guid = $dummy['guid'];
393 - $wp_query->post->menu_order = $dummy['menu_order'];
394 - $wp_query->post->pinged = $dummy['pinged'];
395 - $wp_query->post->to_ping = $dummy['to_ping'];
396 - $wp_query->post->ping_status = $dummy['ping_status'];
397 - $wp_query->post->comment_status = $dummy['comment_status'];
398 - $wp_query->post->comment_count = $dummy['comment_count'];
449 + // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
399 450
400 451 // Set the $post global
401 - $post = $wp_query->post;
452 + $post = new WP_Post( (object) $dummy );
402 453
403 - // Setup the dummy post loop
404 - $wp_query->posts[0] = $wp_query->post;
454 + // Copy the new post global into the main $wp_query
455 + $wp_query->post = $post;
456 + $wp_query->posts = array( $post );
405 457
406 458 // Prevent comments form from appearing
407 459 $wp_query->post_count = 1;
408 460 $wp_query->is_404 = $dummy['is_404'];
@@ -410,18 +462,17 @@
410 462 $wp_query->is_single = $dummy['is_single'];
411 463 $wp_query->is_archive = $dummy['is_archive'];
412 464 $wp_query->is_tax = $dummy['is_tax'];
413 465
414 - /**
415 - * Force the header back to 200 status if not a deliberate 404
416 - *
417 - * @see http://bbpress.trac.wordpress.org/ticket/1973
418 - */
419 - if ( ! $wp_query->is_404() )
420 - status_header( 200 );
466 + // Reset is_singular based on page/single args
467 + // https://bbpress.trac.wordpress.org/ticket/2545
468 + $wp_query->is_singular = $wp_query->is_single;
421 469
470 + // Clean up the dummy post
471 + unset( $dummy );
472 +
422 473 // If we are resetting a post, we are in theme compat
423 - bbp_set_theme_compat_active();
474 + bbp_set_theme_compat_active( true );
424 475 }
425 476
426 477 /**
427 478 * Reset main query vars and filter 'the_content' to output a bbPress
@@ -426,262 +477,64 @@
426 477 /**
427 478 * Reset main query vars and filter 'the_content' to output a bbPress
428 479 * template part as needed.
429 480 *
430 - * @since bbPress (r3032)
481 + * @since 2.0.0 bbPress (r3032)
482 + * @since 2.6.17 bbPress Added support for Block Themes
483 + *
431 484 * @param string $template
432 - * @uses bbp_is_single_user() To check if page is single user
433 - * @uses bbp_get_single_user_template() To get user template
434 - * @uses bbp_is_single_user_edit() To check if page is single user edit
435 - * @uses bbp_get_single_user_edit_template() To get user edit template
436 - * @uses bbp_is_single_view() To check if page is single view
437 - * @uses bbp_get_single_view_template() To get view template
438 - * @uses bbp_is_forum_edit() To check if page is forum edit
439 - * @uses bbp_get_forum_edit_template() To get forum edit template
440 - * @uses bbp_is_topic_merge() To check if page is topic merge
441 - * @uses bbp_get_topic_merge_template() To get topic merge template
442 - * @uses bbp_is_topic_split() To check if page is topic split
443 - * @uses bbp_get_topic_split_template() To get topic split template
444 - * @uses bbp_is_topic_edit() To check if page is topic edit
445 - * @uses bbp_get_topic_edit_template() To get topic edit template
446 - * @uses bbp_is_reply_edit() To check if page is reply edit
447 - * @uses bbp_get_reply_edit_template() To get reply edit template
448 - * @uses bbp_set_theme_compat_template() To set the global theme compat template
449 485 */
450 486 function bbp_template_include_theme_compat( $template = '' ) {
451 487
452 - // Bail if the template already matches a bbPress template. This includes
453 - // archive-* and single-* WordPress post_type matches (allowing
454 - // themes to use the expected format) as well as all bbPress-specific
455 - // template files for users, topics, forums, etc...
456 - if ( !empty( bbpress()->theme_compat->bbpress_template ) )
488 + /**
489 + * Bail if a root template was already found. This prevents unintended
490 + * recursive filtering of 'the_content'.
491 + *
492 + * @link https://bbpress.trac.wordpress.org/ticket/2429
493 + */
494 + if ( bbp_is_template_included() ) {
457 495 return $template;
458 -
459 - /** Users *************************************************************/
460 -
461 - if ( bbp_is_single_user_edit() || bbp_is_single_user() ) {
462 -
463 - // Reset post
464 - bbp_theme_compat_reset_post( array(
465 - 'ID' => 0,
466 - 'post_author' => 0,
467 - 'post_date' => 0,
468 - 'post_content' => '',
469 - 'post_type' => '',
470 - 'post_title' => esc_attr( bbp_get_displayed_user_field( 'display_name' ) ),
471 - 'post_status' => bbp_get_public_status_id(),
472 - 'is_archive' => false,
473 - 'comment_status' => 'closed'
474 - ) );
475 -
476 - /** Forums ************************************************************/
477 -
478 - // Forum archive
479 - } elseif ( bbp_is_forum_archive() ) {
480 -
481 - // Reset post
482 - bbp_theme_compat_reset_post( array(
483 - 'ID' => 0,
484 - 'post_title' => bbp_get_forum_archive_title(),
485 - 'post_author' => 0,
486 - 'post_date' => 0,
487 - 'post_content' => '',
488 - 'post_type' => bbp_get_forum_post_type(),
489 - 'post_status' => bbp_get_public_status_id(),
490 - 'is_archive' => true,
491 - 'comment_status' => 'closed'
492 - ) );
493 -
494 - // Single Forum
495 - } elseif ( bbp_is_forum_edit() || bbp_is_single_forum() ) {
496 -
497 - // Reset post
498 - bbp_theme_compat_reset_post( array(
499 - 'ID' => bbp_get_forum_id(),
500 - 'post_title' => bbp_get_forum_title(),
501 - 'post_author' => bbp_get_forum_author_id(),
502 - 'post_date' => 0,
503 - 'post_content' => get_post_field( 'post_content', bbp_get_forum_id() ),
504 - 'post_type' => bbp_get_forum_post_type(),
505 - 'post_status' => bbp_get_forum_visibility(),
506 - 'is_single' => true,
507 - 'comment_status' => 'closed'
508 - ) );
509 -
510 - /** Topics ************************************************************/
511 -
512 - // Topic archive
513 - } elseif ( bbp_is_topic_archive() ) {
514 -
515 - // Reset post
516 - bbp_theme_compat_reset_post( array(
517 - 'ID' => 0,
518 - 'post_title' => bbp_get_topic_archive_title(),
519 - 'post_author' => 0,
520 - 'post_date' => 0,
521 - 'post_content' => '',
522 - 'post_type' => bbp_get_topic_post_type(),
523 - 'post_status' => bbp_get_public_status_id(),
524 - 'is_archive' => true,
525 - 'comment_status' => 'closed'
526 - ) );
527 -
528 - // Single Topic
529 - } elseif ( bbp_is_topic_edit() || bbp_is_single_topic() ) {
530 -
531 - // Reset post
532 - bbp_theme_compat_reset_post( array(
533 - 'ID' => bbp_get_topic_id(),
534 - 'post_title' => bbp_get_topic_title(),
535 - 'post_author' => bbp_get_topic_author_id(),
536 - 'post_date' => 0,
537 - 'post_content' => get_post_field( 'post_content', bbp_get_topic_id() ),
538 - 'post_type' => bbp_get_topic_post_type(),
539 - 'post_status' => bbp_get_topic_status(),
540 - 'is_single' => true,
541 - 'comment_status' => 'closed'
542 - ) );
543 -
544 - /** Replies ***********************************************************/
545 -
546 - // Reply archive
547 - } elseif ( is_post_type_archive( bbp_get_reply_post_type() ) ) {
548 -
549 - // Reset post
550 - bbp_theme_compat_reset_post( array(
551 - 'ID' => 0,
552 - 'post_title' => __( 'Replies', 'bbpress' ),
553 - 'post_author' => 0,
554 - 'post_date' => 0,
555 - 'post_content' => '',
556 - 'post_type' => bbp_get_reply_post_type(),
557 - 'post_status' => bbp_get_public_status_id(),
558 - 'comment_status' => 'closed'
559 - ) );
560 -
561 - // Single Reply
562 - } elseif ( bbp_is_reply_edit() || bbp_is_single_reply() ) {
563 -
564 - // Reset post
565 - bbp_theme_compat_reset_post( array(
566 - 'ID' => bbp_get_reply_id(),
567 - 'post_title' => bbp_get_reply_title(),
568 - 'post_author' => bbp_get_reply_author_id(),
569 - 'post_date' => 0,
570 - 'post_content' => get_post_field( 'post_content', bbp_get_reply_id() ),
571 - 'post_type' => bbp_get_reply_post_type(),
572 - 'post_status' => bbp_get_reply_status(),
573 - 'comment_status' => 'closed'
574 - ) );
575 -
576 - /** Views *************************************************************/
577 -
578 - } elseif ( bbp_is_single_view() ) {
579 -
580 - // Reset post
581 - bbp_theme_compat_reset_post( array(
582 - 'ID' => 0,
583 - 'post_title' => bbp_get_view_title(),
584 - 'post_author' => 0,
585 - 'post_date' => 0,
586 - 'post_content' => '',
587 - 'post_type' => '',
588 - 'post_status' => bbp_get_public_status_id(),
589 - 'comment_status' => 'closed'
590 - ) );
591 -
592 - /** Topic Tags ********************************************************/
593 -
594 - // Topic Tag Edit
595 - } elseif ( bbp_is_topic_tag_edit() || bbp_is_topic_tag() ) {
596 -
597 - // Stash the current term in a new var
598 - set_query_var( 'bbp_topic_tag', get_query_var( 'term' ) );
599 -
600 - // Reset the post with our new title
601 - bbp_theme_compat_reset_post( array(
602 - 'ID' => 0,
603 - 'post_author' => 0,
604 - 'post_date' => 0,
605 - 'post_content' => '',
606 - 'post_type' => '',
607 - 'post_title' => sprintf( __( 'Topic Tag: %s', 'bbpress' ), '<span>' . bbp_get_topic_tag_name() . '</span>' ),
608 - 'post_status' => bbp_get_public_status_id(),
609 - 'comment_status' => 'closed'
610 - ) );
611 496 }
612 497
613 498 /**
614 - * If we are relying on bbPress's built in theme compatibility to load
615 - * the proper content, we need to intercept the_content, replace the
616 - * output, and display ours instead.
499 + * If BuddyPress is activated at a network level, the action order is
500 + * reversed, which causes the template integration to fail. If we're looking
501 + * at a BuddyPress page here, bail to prevent the extra processing.
617 502 *
618 - * To do this, we first remove all filters from 'the_content' and hook
619 - * our own function into it, which runs a series of checks to determine
620 - * the context, and then uses the built in shortcodes to output the
621 - * correct results from inside an output buffer.
622 - *
623 - * Uses bbp_get_theme_compat_templates() to provide fall-backs that
624 - * should be coded without superfluous mark-up and logic (prev/next
625 - * navigation, comments, date/time, etc...)
626 - *
627 - * Hook into the 'bbp_get_bbpress_template' to override the array of
628 - * possible templates, or 'bbp_bbpress_template' to override the result.
503 + * This is a bit more brute-force than is probably necessary, but gets the
504 + * job done while we work towards something more elegant.
629 505 */
630 - if ( bbp_is_theme_compat_active() ) {
631 -
632 - // Remove all filters from the_content
633 - bbp_remove_all_filters( 'the_content' );
634 -
635 - // Add a filter on the_content late, which we will later remove
636 - add_filter( 'the_content', 'bbp_replace_the_content' );
637 -
638 - // Find the appropriate template file
639 - $template = bbp_get_theme_compat_templates();
506 + if ( function_exists( 'is_buddypress' ) && is_buddypress() ) {
507 + return $template;
640 508 }
641 509
642 - return apply_filters( 'bbp_template_include_theme_compat', $template );
643 -}
644 -
645 -/**
646 - * Replaces the_content() if the post_type being displayed is one that would
647 - * normally be handled by bbPress, but proper single page templates do not
648 - * exist in the currently active theme.
649 - *
650 - * @since bbPress (r3034)
651 - * @param string $content
652 - * @return type
653 - */
654 -function bbp_replace_the_content( $content = '' ) {
655 -
656 - // Bail if not inside the main query loop
657 - if ( ! in_the_loop() || ! is_main_query() )
658 - return $content;
659 -
660 - $bbp = bbpress();
661 -
662 510 // Define local variable(s)
663 - $new_content = '';
511 + $bbp_shortcodes = bbpress()->shortcodes;
664 512
665 513 // Bail if shortcodes are unset somehow
666 - if ( !is_a( $bbp->shortcodes, 'BBP_Shortcodes' ) )
667 - return $content;
514 + if ( ! is_a( $bbp_shortcodes, 'BBP_Shortcodes' ) ) {
515 + return $template;
516 + }
668 517
669 - // Use shortcode API to display forums/topics/replies because they are
670 - // already output buffered and ready to fit inside the_content
671 -
672 518 /** Users *************************************************************/
673 519
674 - // Profile View
675 520 if ( bbp_is_single_user_edit() || bbp_is_single_user() ) {
676 - ob_start();
677 521
678 - bbp_get_template_part( 'content', 'single-user' );
522 + // Reset post
523 + bbp_theme_compat_reset_post(
524 + array(
525 + 'ID' => 0,
526 + 'post_author' => 0,
527 + 'post_date' => bbp_get_empty_datetime(),
528 + 'post_content' => bbp_buffer_template_part( 'content', 'single-user', false ),
529 + 'post_type' => '',
530 + 'post_title' => bbp_get_displayed_user_field( 'display_name' ),
531 + 'post_status' => bbp_get_public_status_id(),
532 + 'is_archive' => false,
533 + 'comment_status' => 'closed'
534 + )
535 + );
679 536
680 - $new_content = ob_get_contents();
681 -
682 - ob_end_clean();
683 -
684 537 /** Forums ************************************************************/
685 538
686 539 // Forum archive
687 540 } elseif ( bbp_is_forum_archive() ) {
@@ -687,41 +540,96 @@
687 540 } elseif ( bbp_is_forum_archive() ) {
688 541
689 542 // Page exists where this archive should be
690 543 $page = bbp_get_page_by_path( bbp_get_root_slug() );
691 - if ( !empty( $page ) ) {
692 544
693 - // Restore previously unset filters
694 - bbp_restore_all_filters( 'the_content' );
545 + // Should we replace the content...
546 + if ( empty( $page->post_content ) ) {
695 547
696 - // Remove 'bbp_replace_the_content' filter to prevent infinite loops
697 - remove_filter( 'the_content', 'bbp_replace_the_content' );
548 + // Use the topics archive
549 + if ( 'topics' === bbp_show_on_root() ) {
550 + $new_content = $bbp_shortcodes->display_topic_index();
698 551
699 - // Start output buffer
700 - ob_start();
552 + // No page so show the archive
553 + } else {
554 + $new_content = $bbp_shortcodes->display_forum_index();
555 + }
701 556
702 - // Grab the content of this page
557 + // ...or use the existing page content?
558 + } else {
703 559 $new_content = apply_filters( 'the_content', $page->post_content );
560 + }
704 561
705 - // Clean up the buffer
706 - ob_end_clean();
562 + // Should we replace the title...
563 + if ( empty( $page->post_title ) ) {
707 564
708 - // Add 'bbp_replace_the_content' filter back (@see $this::start())
709 - add_filter( 'the_content', 'bbp_replace_the_content' );
565 + // Use the topics archive
566 + if ( 'topics' === bbp_show_on_root() ) {
567 + $new_title = bbp_get_topic_archive_title();
710 568
711 - // No page so show the archive
569 + // No page so show the archive
570 + } else {
571 + $new_title = bbp_get_forum_archive_title();
572 + }
573 +
574 + // ...or use the existing page title?
712 575 } else {
713 - $new_content = $bbp->shortcodes->display_forum_index();
576 + $new_title = apply_filters( 'the_title', $page->post_title, $page->ID );
714 577 }
715 578
716 - // Forum Edit
579 + // Reset post
580 + bbp_theme_compat_reset_post(
581 + array(
582 + 'ID' => ! empty( $page->ID ) ? $page->ID : 0,
583 + 'post_title' => $new_title,
584 + 'post_author' => 0,
585 + 'post_date' => bbp_get_empty_datetime(),
586 + 'post_content' => $new_content,
587 + 'post_type' => bbp_get_forum_post_type(),
588 + 'post_status' => bbp_get_public_status_id(),
589 + 'is_archive' => true,
590 + 'comment_status' => 'closed'
591 + )
592 + );
593 +
594 + // Single Forum
717 595 } elseif ( bbp_is_forum_edit() ) {
718 - $new_content = $bbp->shortcodes->display_forum_form();
719 596
720 - // Single Forum
597 + // Reset post
598 + bbp_theme_compat_reset_post(
599 + array(
600 + 'ID' => bbp_get_forum_id(),
601 + 'post_title' => bbp_get_forum_title(),
602 + 'post_author' => bbp_get_forum_author_id(),
603 + 'post_date' => bbp_get_empty_datetime(),
604 + 'post_content' => $bbp_shortcodes->display_forum_form(),
605 + 'post_type' => bbp_get_forum_post_type(),
606 + 'post_status' => bbp_get_forum_visibility(),
607 + 'is_single' => true,
608 + 'comment_status' => 'closed'
609 + )
610 + );
611 +
612 + // Lock the forum from other edits
613 + bbp_set_post_lock( bbp_get_forum_id() );
614 +
721 615 } elseif ( bbp_is_single_forum() ) {
722 - $new_content = $bbp->shortcodes->display_forum( array( 'id' => get_the_ID() ) );
723 616
617 + // Reset post
618 + bbp_theme_compat_reset_post(
619 + array(
620 + 'ID' => bbp_get_forum_id(),
621 + 'post_title' => bbp_get_forum_title(),
622 + 'post_author' => bbp_get_forum_author_id(),
623 + 'post_date' => bbp_get_empty_datetime(),
624 + 'post_content' => $bbp_shortcodes->display_forum( array( 'id' => bbp_get_forum_id() ) ),
625 + 'post_type' => bbp_get_forum_post_type(),
626 + 'post_status' => bbp_get_forum_visibility(),
627 + 'is_single' => true,
628 + 'comment_status' => 'closed'
629 + )
630 + );
631 +
724 632 /** Topics ************************************************************/
725 633
726 634 // Topic archive
727 635 } elseif ( bbp_is_topic_archive() ) {
@@ -727,110 +635,253 @@
727 635 } elseif ( bbp_is_topic_archive() ) {
728 636
729 637 // Page exists where this archive should be
730 638 $page = bbp_get_page_by_path( bbp_get_topic_archive_slug() );
731 - if ( !empty( $page ) ) {
732 639
733 - // Restore previously unset filters
734 - bbp_restore_all_filters( 'the_content' );
640 + // Should we replace the content...
641 + if ( empty( $page->post_content ) ) {
642 + $new_content = $bbp_shortcodes->display_topic_index();
735 643
736 - // Remove 'bbp_replace_the_content' filter to prevent infinite loops
737 - remove_filter( 'the_content', 'bbp_replace_the_content' );
738 -
739 - // Start output buffer
740 - ob_start();
741 -
742 - // Grab the content of this page
644 + // ...or use the existing page content?
645 + } else {
743 646 $new_content = apply_filters( 'the_content', $page->post_content );
647 + }
744 648
745 - // Clean up the buffer
746 - ob_end_clean();
649 + // Should we replace the title...
650 + if ( empty( $page->post_title ) ) {
651 + $new_title = bbp_get_topic_archive_title();
747 652
748 - // Add 'bbp_replace_the_content' filter back (@see $this::start())
749 - add_filter( 'the_content', 'bbp_replace_the_content' );
750 -
751 - // No page so show the archive
653 + // ...or use the existing page title?
752 654 } else {
753 - $new_content = $bbp->shortcodes->display_topic_index();
655 + $new_title = apply_filters( 'the_title', $page->post_title );
754 656 }
755 657
756 - // Topic Edit
757 - } elseif ( bbp_is_topic_edit() ) {
658 + // Reset post
659 + bbp_theme_compat_reset_post(
660 + array(
661 + 'ID' => ! empty( $page->ID ) ? $page->ID : 0,
662 + 'post_title' => bbp_get_topic_archive_title(),
663 + 'post_author' => 0,
664 + 'post_date' => bbp_get_empty_datetime(),
665 + 'post_content' => $new_content,
666 + 'post_type' => bbp_get_topic_post_type(),
667 + 'post_status' => bbp_get_public_status_id(),
668 + 'is_archive' => true,
669 + 'comment_status' => 'closed'
670 + )
671 + );
758 672
673 + // Single Topic
674 + } elseif ( bbp_is_topic_edit() || bbp_is_single_topic() ) {
675 +
759 676 // Split
760 677 if ( bbp_is_topic_split() ) {
761 - ob_start();
678 + $new_content = bbp_buffer_template_part( 'form', 'topic-split', false );
762 679
763 - bbp_get_template_part( 'form', 'topic-split' );
764 -
765 - $new_content = ob_get_contents();
766 -
767 - ob_end_clean();
768 -
769 680 // Merge
770 681 } elseif ( bbp_is_topic_merge() ) {
771 - ob_start();
682 + $new_content = bbp_buffer_template_part( 'form', 'topic-merge', false );
772 683
773 - bbp_get_template_part( 'form', 'topic-merge' );
684 + // Edit
685 + } elseif ( bbp_is_topic_edit() ) {
686 + $new_content = $bbp_shortcodes->display_topic_form();
774 687
775 - $new_content = ob_get_contents();
688 + // Lock the topic from other edits
689 + bbp_set_post_lock( bbp_get_topic_id() );
776 690
777 - ob_end_clean();
778 -
779 - // Edit
691 + // Single
780 692 } else {
781 - $new_content = $bbp->shortcodes->display_topic_form();
693 + $new_content = $bbp_shortcodes->display_topic( array( 'id' => bbp_get_topic_id() ) );
782 694 }
783 695
784 - // Single Topic
785 - } elseif ( bbp_is_single_topic() ) {
786 - $new_content = $bbp->shortcodes->display_topic( array( 'id' => get_the_ID() ) );
696 + // Reset post
697 + bbp_theme_compat_reset_post(
698 + array(
699 + 'ID' => bbp_get_topic_id(),
700 + 'post_title' => bbp_get_topic_title(),
701 + 'post_author' => bbp_get_topic_author_id(),
702 + 'post_date' => bbp_get_empty_datetime(),
703 + 'post_content' => $new_content,
704 + 'post_type' => bbp_get_topic_post_type(),
705 + 'post_status' => bbp_get_topic_status(),
706 + 'is_single' => true,
707 + 'comment_status' => 'closed'
708 + )
709 + );
787 710
788 711 /** Replies ***********************************************************/
789 712
790 713 // Reply archive
791 714 } elseif ( is_post_type_archive( bbp_get_reply_post_type() ) ) {
792 - //$new_content = $bbp->shortcodes->display_reply_index();
793 715
794 - // Reply Edit
795 - } elseif ( bbp_is_reply_edit() ) {
796 - $new_content = $bbp->shortcodes->display_reply_form();
716 + // Reset post
717 + bbp_theme_compat_reset_post(
718 + array(
719 + 'ID' => 0,
720 + 'post_title' => esc_html__( 'Replies', 'bbpress' ),
721 + 'post_author' => 0,
722 + 'post_date' => bbp_get_empty_datetime(),
723 + 'post_content' => '',
724 + 'post_type' => bbp_get_reply_post_type(),
725 + 'post_status' => bbp_get_public_status_id(),
726 + 'is_archive' => true,
727 + 'comment_status' => 'closed'
728 + )
729 + );
797 730
798 731 // Single Reply
799 - } elseif ( bbp_is_single_reply() ) {
800 - $new_content = $bbp->shortcodes->display_reply( array( 'id' => get_the_ID() ) );
732 + } elseif ( bbp_is_reply_edit() || bbp_is_single_reply() ) {
801 733
734 + // Move
735 + if ( bbp_is_reply_move() ) {
736 + $new_content = bbp_buffer_template_part( 'form', 'reply-move', false );
737 +
738 + // Edit
739 + } elseif ( bbp_is_reply_edit() ) {
740 + $new_content = $bbp_shortcodes->display_reply_form();
741 +
742 + // Lock the reply from other edits
743 + bbp_set_post_lock( bbp_get_reply_id() );
744 +
745 + // Single
746 + } else {
747 + $new_content = $bbp_shortcodes->display_reply( array( 'id' => get_the_ID() ) );
748 + }
749 +
750 + // Reset post
751 + bbp_theme_compat_reset_post(
752 + array(
753 + 'ID' => bbp_get_reply_id(),
754 + 'post_title' => bbp_get_reply_title(),
755 + 'post_author' => bbp_get_reply_author_id(),
756 + 'post_date' => bbp_get_empty_datetime(),
757 + 'post_content' => $new_content,
758 + 'post_type' => bbp_get_reply_post_type(),
759 + 'post_status' => bbp_get_reply_status(),
760 + 'is_single' => true,
761 + 'comment_status' => 'closed'
762 + )
763 + );
764 +
802 765 /** Views *************************************************************/
803 766
804 767 } elseif ( bbp_is_single_view() ) {
805 - $new_content = $bbp->shortcodes->display_view( array( 'id' => get_query_var( 'bbp_view' ) ) );
806 768
769 + // Reset post
770 + bbp_theme_compat_reset_post(
771 + array(
772 + 'ID' => 0,
773 + 'post_title' => bbp_get_view_title(),
774 + 'post_author' => 0,
775 + 'post_date' => bbp_get_empty_datetime(),
776 + 'post_content' => $bbp_shortcodes->display_view( array( 'id' => get_query_var( bbp_get_view_rewrite_id() ) ) ),
777 + 'post_type' => '',
778 + 'post_status' => bbp_get_public_status_id(),
779 + 'is_archive' => true,
780 + 'comment_status' => 'closed'
781 + )
782 + );
783 +
784 + /** Search ************************************************************/
785 +
786 + } elseif ( bbp_is_search() ) {
787 +
788 + // Reset post
789 + bbp_theme_compat_reset_post(
790 + array(
791 + 'ID' => 0,
792 + 'post_title' => bbp_get_search_title(),
793 + 'post_author' => 0,
794 + 'post_date' => bbp_get_empty_datetime(),
795 + 'post_content' => $bbp_shortcodes->display_search( array( 'search' => get_query_var( bbp_get_search_rewrite_id() ) ) ),
796 + 'post_type' => '',
797 + 'post_status' => bbp_get_public_status_id(),
798 + 'is_archive' => true,
799 + 'comment_status' => 'closed'
800 + )
801 + );
802 +
807 803 /** Topic Tags ********************************************************/
808 804
809 - // Show topics of tag
810 - } elseif ( bbp_is_topic_tag() ) {
811 - $new_content = $bbp->shortcodes->display_topics_of_tag( array( 'id' => bbp_get_topic_tag_id() ) );
805 + // Topic Tag Edit
806 + } elseif ( bbp_is_topic_tag_edit() || bbp_is_topic_tag() ) {
812 807
813 - // Edit topic tag
814 - } elseif ( bbp_is_topic_tag_edit() ) {
815 - $new_content = $bbp->shortcodes->display_topic_tag_form();
808 + // Stash the current term in a new var
809 + set_query_var( 'bbp_topic_tag', get_query_var( 'term' ) );
810 +
811 + // Show topics of tag
812 + if ( bbp_is_topic_tag() ) {
813 + $new_content = $bbp_shortcodes->display_topics_of_tag( array( 'id' => bbp_get_topic_tag_id() ) );
814 +
815 + // Edit topic tag
816 + } elseif ( bbp_is_topic_tag_edit() ) {
817 + $new_content = $bbp_shortcodes->display_topic_tag_form();
818 + }
819 +
820 + // Reset the post with our new title
821 + bbp_theme_compat_reset_post(
822 + array(
823 + 'ID' => 0,
824 + 'post_author' => 0,
825 + 'post_date' => bbp_get_empty_datetime(),
826 + 'post_content' => $new_content,
827 + 'post_type' => '',
828 + /* translators: %s: Topic tag name */
829 + 'post_title' => sprintf( esc_html__( 'Topic Tag: %s', 'bbpress' ), bbp_get_topic_tag_name() ),
830 + 'post_status' => bbp_get_public_status_id(),
831 + 'is_tax' => true,
832 + 'is_archive' => true,
833 + 'comment_status' => 'closed'
834 + )
835 + );
816 836 }
817 837
818 - // Juggle the content around and try to prevent unsightly comments
819 - if ( !empty( $new_content ) && ( $new_content != $content ) ) {
838 + /**
839 + * Bail if the template already matches a bbPress template. This includes
840 + * archive-* and single-* WordPress post_type matches (allowing
841 + * themes to use the expected format) as well as all bbPress-specific
842 + * template files for users, topics, forums, etc...
843 + *
844 + * We do this after the above checks to prevent incorrect 404 body classes
845 + * and header statuses, as well as to set the post global as needed.
846 + *
847 + * @see https://bbpress.trac.wordpress.org/ticket/1478/
848 + */
849 + if ( bbp_is_template_included() ) {
850 + return $template;
820 851
821 - // Set the content to be the new content
822 - $content = apply_filters( 'bbp_replace_the_content', $new_content, $content );
852 + /**
853 + * If we are relying on the built-in theme compatibility API to load
854 + * the proper content, we need to intercept the_content, replace the
855 + * output, and display ours instead.
856 + *
857 + * To do this, we first remove all filters from 'the_content' and hook
858 + * our own function into it, which runs a series of checks to determine
859 + * the context, and then uses the built in shortcodes to output the
860 + * correct results from inside an output buffer.
861 + *
862 + * Uses bbp_get_theme_compat_templates() to provide fall-backs that
863 + * should be coded without superfluous mark-up and logic (prev/next
864 + * navigation, comments, date/time, etc...)
865 + *
866 + * Hook into the 'bbp_get_bbpress_template' to override the array of
867 + * possible templates, or 'bbp_bbpress_template' to override the result.
868 + */
869 + } elseif ( bbp_is_theme_compat_active() ) {
870 + bbp_remove_all_filters( 'the_content' );
823 871
824 - // Clean up after ourselves
825 - unset( $new_content );
872 + // Block themes
873 + if ( wp_is_block_theme() ) {
874 + $template = bbp_get_theme_canvas_template();
826 875
827 - // Reset the $post global
828 - wp_reset_postdata();
876 + // Non-block themes
877 + } else {
878 + $template = bbp_get_theme_compat_templates();
879 + }
829 880 }
830 881
831 - // Return possibly hi-jacked content
832 - return $content;
882 + // Filter & return
883 + return apply_filters( 'bbp_template_include_theme_compat', $template );
833 884 }
834 885
835 886 /** Helpers *******************************************************************/
836 887
@@ -836,22 +887,19 @@
836 887
837 888 /**
838 889 * Remove the canonical redirect to allow pretty pagination
839 890 *
840 - * @since bbPress (r2628)
891 + * @since 2.0.0 bbPress (r2628)
892 + *
841 893 * @param string $redirect_url Redirect url
842 - * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks
843 - * @uses bbp_get_paged() To get the current page number
844 - * @uses bbp_is_single_topic() To check if it's a topic page
845 - * @uses bbp_is_single_forum() To check if it's a forum page
894 + *
846 895 * @return bool|string False if it's a topic/forum and their first page,
847 896 * otherwise the redirect url
848 897 */
849 898 function bbp_redirect_canonical( $redirect_url ) {
850 - global $wp_rewrite;
851 899
852 900 // Canonical is for the beautiful
853 - if ( $wp_rewrite->using_permalinks() ) {
901 + if ( bbp_use_pretty_urls() ) {
854 902
855 903 // If viewing beyond page 1 of several
856 904 if ( 1 < bbp_get_paged() ) {
857 905
@@ -893,9 +941,10 @@
893 941 /**
894 942 * Removes all filters from a WordPress filter, and stashes them in the $bbp
895 943 * global in the event they need to be restored later.
896 944 *
897 - * @since bbPress (r3251)
945 + * @since 2.0.0 bbPress (r3251)
946 + *
898 947 * @global WP_filter $wp_filter
899 948 * @global array $merged_filters
900 949 * @param string $tag
901 950 * @param int $priority
@@ -906,38 +955,38 @@
906 955
907 956 $bbp = bbpress();
908 957
909 958 // Filters exist
910 - if ( isset( $wp_filter[$tag] ) ) {
959 + if ( isset( $wp_filter[ $tag ] ) ) {
911 960
912 961 // Filters exist in this priority
913 - if ( !empty( $priority ) && isset( $wp_filter[$tag][$priority] ) ) {
962 + if ( ! empty( $priority ) && isset( $wp_filter[ $tag ][ $priority ] ) ) {
914 963
915 964 // Store filters in a backup
916 - $bbp->filters->wp_filter[$tag][$priority] = $wp_filter[$tag][$priority];
965 + $bbp->filters->wp_filter[ $tag ][ $priority ] = $wp_filter[ $tag ][ $priority ];
917 966
918 967 // Unset the filters
919 - unset( $wp_filter[$tag][$priority] );
968 + unset( $wp_filter[ $tag ][ $priority ] );
920 969
921 970 // Priority is empty
922 971 } else {
923 972
924 973 // Store filters in a backup
925 - $bbp->filters->wp_filter[$tag] = $wp_filter[$tag];
974 + $bbp->filters->wp_filter[ $tag ] = $wp_filter[ $tag ];
926 975
927 976 // Unset the filters
928 - unset( $wp_filter[$tag] );
977 + unset( $wp_filter[ $tag ] );
929 978 }
930 979 }
931 980
932 981 // Check merged filters
933 - if ( isset( $merged_filters[$tag] ) ) {
982 + if ( isset( $merged_filters[ $tag ] ) ) {
934 983
935 984 // Store filters in a backup
936 - $bbp->filters->merged_filters[$tag] = $merged_filters[$tag];
985 + $bbp->filters->merged_filters[ $tag ] = $merged_filters[ $tag ];
937 986
938 987 // Unset the filters
939 - unset( $merged_filters[$tag] );
988 + unset( $merged_filters[ $tag ] );
940 989 }
941 990
942 991 return true;
943 992 }
@@ -945,9 +994,10 @@
945 994 /**
946 995 * Restores filters from the $bbp global that were removed using
947 996 * bbp_remove_all_filters()
948 997 *
949 - * @since bbPress (r3251)
998 + * @since 2.0.0 bbPress (r3251)
999 + *
950 1000 * @global WP_filter $wp_filter
951 1001 * @global array $merged_filters
952 1002 * @param string $tag
953 1003 * @param int $priority
@@ -955,41 +1005,43 @@
955 1005 */
956 1006 function bbp_restore_all_filters( $tag, $priority = false ) {
957 1007 global $wp_filter, $merged_filters;
958 1008
1009 + // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
1010 +
959 1011 $bbp = bbpress();
960 1012
961 1013 // Filters exist
962 - if ( isset( $bbp->filters->wp_filter[$tag] ) ) {
1014 + if ( isset( $bbp->filters->wp_filter[ $tag ] ) ) {
963 1015
964 1016 // Filters exist in this priority
965 - if ( !empty( $priority ) && isset( $bbp->filters->wp_filter[$tag][$priority] ) ) {
1017 + if ( ! empty( $priority ) && isset( $bbp->filters->wp_filter[ $tag ][ $priority ] ) ) {
966 1018
967 1019 // Store filters in a backup
968 - $wp_filter[$tag][$priority] = $bbp->filters->wp_filter[$tag][$priority];
1020 + $wp_filter[ $tag ][ $priority ] = $bbp->filters->wp_filter[ $tag ][ $priority ];
969 1021
970 1022 // Unset the filters
971 - unset( $bbp->filters->wp_filter[$tag][$priority] );
1023 + unset( $bbp->filters->wp_filter[ $tag ][ $priority ] );
972 1024
973 1025 // Priority is empty
974 1026 } else {
975 1027
976 1028 // Store filters in a backup
977 - $wp_filter[$tag] = $bbp->filters->wp_filter[$tag];
1029 + $wp_filter[ $tag ] = $bbp->filters->wp_filter[ $tag ];
978 1030
979 1031 // Unset the filters
980 - unset( $bbp->filters->wp_filter[$tag] );
1032 + unset( $bbp->filters->wp_filter[ $tag ] );
981 1033 }
982 1034 }
983 1035
984 1036 // Check merged filters
985 - if ( isset( $bbp->filters->merged_filters[$tag] ) ) {
1037 + if ( isset( $bbp->filters->merged_filters[ $tag ] ) ) {
986 1038
987 1039 // Store filters in a backup
988 - $merged_filters[$tag] = $bbp->filters->merged_filters[$tag];
1040 + $merged_filters[ $tag ] = $bbp->filters->merged_filters[ $tag ];
989 1041
990 1042 // Unset the filters
991 - unset( $bbp->filters->merged_filters[$tag] );
1043 + unset( $bbp->filters->merged_filters[ $tag ] );
992 1044 }
993 1045
994 1046 return true;
995 1047 }
@@ -996,29 +1048,48 @@
996 1048
997 1049 /**
998 1050 * Force comments_status to 'closed' for bbPress post types
999 1051 *
1000 - * @since bbPress (r3589)
1052 + * @since 2.1.0 bbPress (r3589)
1053 + *
1001 1054 * @param bool $open True if open, false if closed
1002 1055 * @param int $post_id ID of the post to check
1003 1056 * @return bool True if open, false if closed
1004 1057 */
1005 -function bbp_force_comment_status( $open, $post_id = 0 ) {
1058 +function bbp_force_comment_status( $open = false, $post_id = 0 ) {
1006 1059
1060 + // Default return value is what is passed in $open
1061 + $retval = (bool) $open;
1062 +
1007 1063 // Get the post type of the post ID
1008 1064 $post_type = get_post_type( $post_id );
1009 1065
1010 - // Default return value is what is passed in $open
1011 - $retval = $open;
1066 + // Only force for bbPress post types
1067 + if ( in_array( $post_type, bbp_get_post_types(), true ) ) {
1068 + $retval = false;
1069 + }
1012 1070
1013 - // Only force for bbPress post types
1014 - switch ( $post_type ) {
1015 - case bbp_get_forum_post_type() :
1016 - case bbp_get_topic_post_type() :
1017 - case bbp_get_reply_post_type() :
1018 - $retval = false;
1019 - break;
1071 + // Filter & return
1072 + return (bool) apply_filters( 'bbp_force_comment_status', $retval, $open, $post_id, $post_type );
1073 +}
1074 +
1075 +/**
1076 + * Remove "prev" and "next" relational links from <head> on bbPress pages.
1077 + *
1078 + * WordPress automatically generates these relational links to the current
1079 + * page, but bbPress does not use these links, nor would they work the same.
1080 + *
1081 + * In this function, we remove these links when on a bbPress page. This also
1082 + * prevents additional, unnecessary queries from running.
1083 + *
1084 + * @since 2.6.0 bbPress (r7071)
1085 + */
1086 +function bbp_remove_adjacent_posts() {
1087 +
1088 + // Bail if not a bbPress page
1089 + if ( ! is_bbpress() ) {
1090 + return;
1020 1091 }
1021 1092
1022 - // Allow override of the override
1023 - return apply_filters( 'bbp_force_comment_status', $retval, $open, $post_id, $post_type );
1093 + // Remove the WordPress core action for adjacent posts
1094 + remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10 );
1024 1095 }