PluginProbe
bbPress / 2.0-beta-3
bbPress v2.0-beta-3
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
bbpress / bbp-includes / bbp-core-compatibility.php

bbp-core-compatibility.php in bbPress 2.0-beta-3, at bbp-includes/bbp-core-compatibility.php

1,442 lines 39.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Core Theme Compatibility
5 *
6 * @package bbPress
7 * @subpackage ThemeCompatibility
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 /** Theme Compat **************************************************************/
14
15 /**
16 * What follows is an attempt at intercepting the natural page load process
17 * to replace the_content() with the appropriate bbPress content.
18 *
19 * To do this, bbPress does several direct manipulations of global variables
20 * and forces them to do what they are not supposed to be doing.
21 *
22 * Don't try anything you're about to witness here, at home. Ever.
23 */
24
25 /**
26 * Set the theme compat theme URL and DIR
27 *
28 * @since bbPress (r3311)
29 *
30 * @global bbPress $bbp
31 * @param string $theme
32 * @uses current_theme_supports()
33 */
34 function bbp_theme_compat_set_theme( $theme = array() ) {
35
36 // Check if current theme supports bbPress
37 if ( empty( $bbp->theme_compat->theme ) && !current_theme_supports( 'bbpress' ) ) {
38
39 global $bbp;
40
41 if ( empty( $theme ) ) {
42 $theme = array(
43 'dir' => $bbp->themes_dir . '/bbp-twentyten',
44 'url' => $bbp->themes_url . '/bbp-twentyten'
45 );
46 }
47
48 // Set the theme compat globals for help with loading template parts
49 $bbp->theme_compat->theme = $theme;
50 }
51 }
52
53 /**
54 * If not using a bbPress compatable theme, enqueue some basic styling and js
55 *
56 * @since bbPress (r3029)
57 *
58 * @global bbPress $bbp
59 * @uses bbp_set_compat_theme_dir() Set the compatable theme to bbp-twentyten
60 * @uses current_theme_supports() Check bbPress theme support
61 * @uses wp_enqueue_style() Enqueue the bbp-twentyten default CSS
62 * @uses wp_enqueue_script() Enqueue the bbp-twentyten default topic JS
63 */
64 function bbp_theme_compat_enqueue_css() {
65 global $bbp;
66
67 // Check if current theme supports bbPress
68 if ( !current_theme_supports( 'bbpress' ) ) {
69
70 /** Default CSS ***************************************************/
71
72 // Do not enqueue CSS in admin
73 if ( !is_admin() ) {
74
75 // Right to left
76 if ( is_rtl() ) {
77 wp_enqueue_style( 'bbpress-style', bbp_get_theme_compat_url() . '/css/bbpress-rtl.css' );
78
79 // Left to right
80 } else {
81 wp_enqueue_style( 'bbpress-style', bbp_get_theme_compat_url() . '/css/bbpress.css' );
82 }
83 }
84 }
85 }
86
87 /**
88 * Adds bbPress theme support to any active WordPress theme
89 *
90 * This function is really cool because it's responsible for managing the
91 * theme compatability layer when the current theme does not support bbPress.
92 * It uses the current_theme_supports() WordPress function to see if 'bbpress'
93 * is explicitly supported. If not, it will directly load the requested template
94 * part using load_template(). If so, it proceeds with using get_template_part()
95 * as per normal, and no one is the wiser.
96 *
97 * @since bbPress (r3032)
98 *
99 * @param string $slug
100 * @param string $name Optional. Default null
101 * @uses current_theme_supports()
102 * @uses load_template()
103 * @uses get_template_part()
104 */
105 function bbp_get_template_part( $slug, $name = null ) {
106
107 // Current theme does not support bbPress, so we need to do some heavy
108 // lifting to see if a bbPress template is needed in the current context
109 if ( !current_theme_supports( 'bbpress' ) )
110 load_template( bbp_get_theme_compat_dir() . '/' . $slug . '-' . $name . '.php', false );
111
112 // Current theme supports bbPress to proceed as usual
113 else
114 get_template_part( $slug, $name );
115
116 }
117
118 /**
119 * Gets the bbPress compatable theme used in the event the currently active
120 * WordPress theme does not explicitly support bbPress. This can be filtered,
121 * or set manually. Tricky theme authors can override the default and include
122 * their own bbPress compatability layers for their themes.
123 *
124 * @since bbPress (r3032)
125 *
126 * @global bbPress $bbp
127 * @uses apply_filters()
128 * @return string
129 */
130 function bbp_get_theme_compat_dir() {
131 global $bbp;
132
133 return apply_filters( 'bbp_get_theme_compat_dir', $bbp->theme_compat->theme['dir'] );
134 }
135
136 /**
137 * Gets the bbPress compatable theme used in the event the currently active
138 * WordPress theme does not explicitly support bbPress. This can be filtered,
139 * or set manually. Tricky theme authors can override the default and include
140 * their own bbPress compatability layers for their themes.
141 *
142 * @since bbPress (r3032)
143 *
144 * @global bbPress $bbp
145 * @uses apply_filters()
146 * @return string
147 */
148 function bbp_get_theme_compat_url() {
149 global $bbp;
150
151 return apply_filters( 'bbp_get_theme_compat_url', $bbp->theme_compat->theme['url'] );
152 }
153
154 /**
155 * Gets true/false if page is currently inside theme compatibility
156 *
157 * @since bbPress (r3265)
158 *
159 * @global bbPress $bbp
160 *
161 * @return bool
162 */
163 function bbp_is_theme_compat_active() {
164 global $bbp;
165
166 if ( empty( $bbp->theme_compat->active ) )
167 return false;
168
169 return $bbp->theme_compat->active;
170 }
171
172 /**
173 * Sets true/false if page is currently inside theme compatibility
174 *
175 * @since bbPress (r3265)
176 *
177 * @global bbPress $bbp
178 *
179 * @param bool $set
180 *
181 * @return bool
182 */
183 function bbp_set_theme_compat_active( $set = true ) {
184 global $bbp;
185
186 $bbp->theme_compat->active = $set;
187
188 return (bool) $bbp->theme_compat->active;
189 }
190
191 /**
192 * Set the theme compat templates global
193 *
194 * Stash possible template files for the current query. Useful if plugins want
195 * to override them, or see what files are being scanned for inclusion.
196 *
197 * @since bbPress (r3311)
198 *
199 * @global $bbp;
200 */
201 function bbp_set_theme_compat_templates( $templates = array() ) {
202 global $bbp;
203
204 $bbp->theme_compat->templates = $templates;
205
206 return $bbp->theme_compat->templates;
207 }
208
209 /**
210 * Set the theme compat template global
211 *
212 * Stash the template file for the current query. Useful if plugins want
213 * to override it, or see what file is being included.
214 *
215 * @since bbPress (r3311)
216 *
217 * @global $bbp;
218 */
219 function bbp_set_theme_compat_template( $template = '' ) {
220 global $bbp;
221
222 $bbp->theme_compat->template = $template;
223
224 return $bbp->theme_compat->template;
225 }
226
227 /**
228 * This fun little function fills up some WordPress globals with dummy data to
229 * stop your average page template from complaining about it missing.
230 *
231 * @since bbPress (r3108)
232 *
233 * @global WP_Query $wp_query
234 * @global object $post
235 * @param array $args
236 */
237 function bbp_theme_compat_reset_post( $args = array() ) {
238 global $wp_query, $post;
239
240 // Why would you ever want to do this otherwise?
241 if ( current_theme_supports( 'bbpress' ) )
242 wp_die( __( 'Hands off, partner!', 'bbpress' ) );
243
244 // Default for current post
245 if ( isset( $wp_query->post ) ) {
246 $defaults = array(
247 'ID' => get_the_ID(),
248 'post_title' => get_the_title(),
249 'post_author' => get_the_author_meta('ID'),
250 'post_date' => get_the_date(),
251 'post_content' => get_the_content(),
252 'post_type' => get_post_type(),
253 'post_status' => get_post_status()
254 );
255
256 // Empty defaults
257 } else {
258 $defaults = array(
259 'ID' => 0,
260 'post_title' => '',
261 'post_author' => 0,
262 'post_date' => 0,
263 'post_content' => '',
264 'post_type' => 'page',
265 'post_status' => 'publish'
266 );
267 }
268 $dummy = wp_parse_args( $args, $defaults );
269
270 // Setup the dummy post object
271 $wp_query->post->ID = $dummy['ID'];
272 $wp_query->post->post_title = $dummy['post_title'];
273 $wp_query->post->post_author = $dummy['post_author'];
274 $wp_query->post->post_date = $dummy['post_date'];
275 $wp_query->post->post_content = $dummy['post_content'];
276 $wp_query->post->post_type = $dummy['post_type'];
277 $wp_query->post->post_status = $dummy['post_status'];
278
279 // Set the $post global
280 $post = $wp_query->post;
281
282 // Setup the dummy post loop
283 $wp_query->posts[] = $wp_query->post;
284
285 // Prevent comments form from appearing
286 $wp_query->post_count = 1;
287 $wp_query->is_404 = false;
288 $wp_query->is_page = false;
289 $wp_query->is_single = false;
290 $wp_query->is_archive = false;
291 $wp_query->is_tax = false;
292
293 // If we are resetting a post, we are in theme compat
294 bbp_set_theme_compat_active();
295 }
296
297 /** Templates *****************************************************************/
298
299 /**
300 * Get the user profile template
301 *
302 * @since bbPress (r3311)
303 *
304 * @uses bbp_get_displayed_user_id()
305 * @uses apply_filters()
306 *
307 * @return array
308 */
309 function bbp_get_single_user_template() {
310
311 $displayed = bbp_get_displayed_user_id();
312 $templates = array(
313
314 // Single User ID
315 'single-user-' . $displayed . '.php',
316 'bbpress/single-user-' . $displayed . '.php',
317 'forums/single-user-' . $displayed . '.php',
318
319 // Single User
320 'single-user.php',
321 'bbpress/single-user.php',
322 'forums/single-user.php',
323
324 // User
325 'user.php',
326 'bbpress/user.php',
327 'forums/user.php',
328 );
329
330 $templates = apply_filters( 'bbp_get_profile_template', $templates );
331 $templates = bbp_set_theme_compat_templates( $templates );
332
333 $template = locate_template( $templates, false, false );
334 $template = bbp_set_theme_compat_template( $template );
335
336 return $template;
337 }
338
339 /**
340 * Get the user profile edit template
341 *
342 * @since bbPress (r3311)
343 *
344 * @uses $displayed
345 * @uses apply_filters()
346 *
347 * @return array
348 */
349 function bbp_get_single_user_edit_template() {
350
351 $displayed = bbp_get_displayed_user_id();
352 $templates = array(
353
354 // Single User Edit ID
355 'single-user-edit-' . $displayed . '.php',
356 'bbpress/single-user-edit-' . $displayed . '.php',
357 'forums/single-user-edit-' . $displayed . '.php',
358
359 // Single User Edit
360 'single-user-edit.php',
361 'bbpress/single-user-edit.php',
362 'forums/single-user-edit.php',
363
364 // User Edit
365 'user-edit.php',
366 'bbpress/user-edit.php',
367 'forums/user-edit.php',
368
369 // User
370 'forums/user.php',
371 'bbpress/user.php',
372 'user.php',
373 );
374
375 $templates = apply_filters( 'bbp_get_profile_edit_template', $templates );
376 $templates = bbp_set_theme_compat_templates( $templates );
377
378 $template = locate_template( $templates, false, false );
379 $template = bbp_set_theme_compat_template( $template );
380
381 return $template;
382 }
383
384 /**
385 * Get the view template
386 *
387 * @since bbPress (r3311)
388 *
389 * @uses bbp_get_view_id()
390 * @uses apply_filters()
391 *
392 * @return array
393 */
394 function bbp_get_single_view_template() {
395
396 $view_id = bbp_get_view_id();
397 $templates = array(
398
399 // Single View ID
400 'single-view-' . $view_id . '.php',
401 'bbpress/single-view-' . $view_id . '.php',
402 'forums/single-view-' . $view_id . '.php',
403
404 // View ID
405 'view-' . $view_id . '.php',
406 'bbpress/view-' . $view_id . '.php',
407 'forums/view-' . $view_id . '.php',
408
409 // Single View
410 'single-view.php',
411 'bbpress/single-view.php',
412 'forums/single-view.php',
413
414 // View
415 'view.php',
416 'bbpress/view.php',
417 'forums/view.php',
418 );
419
420 $templates = apply_filters( 'bbp_get_single_view_template', $templates );
421 $templates = bbp_set_theme_compat_templates( $templates );
422
423 $template = locate_template( $templates, false, false );
424 $template = bbp_set_theme_compat_template( $template );
425
426 return $template;
427 }
428
429 /**
430 * Get the topic edit template
431 *
432 * @since bbPress (r3311)
433 *
434 * @uses bbp_get_topic_post_type()
435 * @uses apply_filters()
436 *
437 * @return array
438 */
439 function bbp_get_topic_edit_template() {
440
441 $post_type = bbp_get_topic_post_type();
442 $templates = array(
443
444 // Single Topic Edit
445 'single-' . $post_type . '-edit.php',
446 'bbpress/single-' . $post_type . '-edit.php',
447 'forums/single-' . $post_type . '-edit.php',
448
449 // Single Action Edit Topic
450 'single-action-edit-' . $post_type . '.php',
451 'bbpress/single-action-edit-' . $post_type . '.php',
452 'forums/single-action-edit-' . $post_type . '.php',
453
454 // Single Action Edit
455 'single-action-edit.php',
456 'bbpress/single-action-edit.php',
457 'forums/single-action-edit.php',
458
459 // Action Edit
460 'action-edit.php',
461 'bbpress/action-edit.php',
462 'forums/action-edit.php',
463
464 // Single Topic
465 'single-' . $post_type . '.php',
466 'forums/single-' . $post_type . '.php',
467 'bbpress/single-' . $post_type . '.php',
468 );
469
470 $templates = apply_filters( 'bbp_get_topic_edit_template', $templates );
471 $templates = bbp_set_theme_compat_templates( $templates );
472
473 $template = locate_template( $templates, false, false );
474 $template = bbp_set_theme_compat_template( $template );
475
476 return $template;
477 }
478
479 /**
480 * Get the topic split template
481 *
482 * @since bbPress (r3311)
483 *
484 * @uses bbp_get_topic_post_type()
485 * @uses apply_filters()
486 *
487 * @return array
488 */
489 function bbp_get_topic_split_template() {
490
491 $post_type = bbp_get_topic_post_type();
492 $templates = array(
493
494 // Topic Split Split
495 'single-' . $post_type . '-split.php',
496 'bbpress/single-' . $post_type . '-split.php',
497 'forums/single-' . $post_type . '-split.php',
498
499 // Action Split Split
500 'single-action-split-merge.php',
501 'bbpress/single-action-split-merge.php',
502 'forums/single-action-split-merge.php',
503
504 // Action Split Split
505 'action-split-merge.php',
506 'bbpress/action-split-merge.php',
507 'forums/action-split-merge.php'
508 );
509
510 $templates = apply_filters( 'bbp_get_topic_edit_template', $templates );
511 $templates = bbp_set_theme_compat_templates( $templates );
512
513 $template = locate_template( $templates, false, false );
514 $template = bbp_set_theme_compat_template( $template );
515
516 return $template;
517 }
518
519 /**
520 * Get the topic merge template
521 *
522 * @since bbPress (r3311)
523 *
524 * @uses bbp_get_topic_post_type()
525 * @uses apply_filters()
526 *
527 * @return array
528 */
529 function bbp_get_topic_merge_template() {
530
531 $post_type = bbp_get_topic_post_type();
532 $templates = array(
533
534 // Topic Split Merge
535 'single-' . $post_type . '-merge.php',
536 'bbpress/single-' . $post_type . '-merge.php',
537 'forums/single-' . $post_type . '-merge.php',
538
539 // Action Split Merge
540 'single-action-split-merge.php',
541 'bbpress/single-action-split-merge.php',
542 'forums/single-action-split-merge.php',
543
544 // Action Split Merge
545 'action-split-merge.php',
546 'bbpress/action-split-merge.php',
547 'forums/action-split-merge.php'
548 );
549
550 $templates = apply_filters( 'bbp_get_topic_edit_template', $templates );
551 $templates = bbp_set_theme_compat_templates( $templates );
552
553 $template = locate_template( $templates, false, false );
554 $template = bbp_set_theme_compat_template( $template );
555
556 return $template;
557 }
558
559 /**
560 * Get the reply edit template
561 *
562 * @since bbPress (r3311)
563 *
564 * @uses bbp_get_reply_post_type()
565 * @uses apply_filters()
566 *
567 * @return array
568 */
569 function bbp_get_reply_edit_template() {
570
571 $post_type = bbp_get_reply_post_type();
572 $templates = array(
573
574 // Single Reply Edit
575 'single-' . $post_type . '-edit.php',
576 'bbpress/single-' . $post_type . '-edit.php',
577 'forums/single-' . $post_type . '-edit.php',
578
579 // Single Action Edit Reply
580 'single-action-edit-' . $post_type . '.php',
581 'bbpress/single-action-edit-' . $post_type . '.php',
582 'forums/single-action-edit-' . $post_type . '.php',
583
584 // Single Action Edit
585 'single-action-edit.php',
586 'bbpress/single-action-edit.php',
587 'forums/single-action-edit.php',
588
589 // Action Edit
590 'action-edit.php',
591 'bbpress/action-edit.php',
592 'forums/action-edit.php',
593
594 // Single Reply
595 'single-' . $post_type . '.php',
596 'forums/single-' . $post_type . '.php',
597 'bbpress/single-' . $post_type . '.php',
598 );
599
600 $templates = apply_filters( 'bbp_get_reply_edit_template', $templates );
601 $templates = bbp_set_theme_compat_templates( $templates );
602
603 $template = locate_template( $templates, false, false );
604 $template = bbp_set_theme_compat_template( $template );
605
606 return $template;
607 }
608
609 /**
610 * Get the files to fallback on to use for theme compatibility
611 *
612 * @since bbPress (r3311)
613 *
614 * @uses apply_filters()
615 * @uses bbp_set_theme_compat_templates();
616 *
617 * @return type
618 */
619 function bbp_get_theme_compat_templates() {
620
621 $templates = array(
622 'bbpress.php',
623 'forum.php',
624 'page.php',
625 'single.php',
626 'index.php'
627 );
628
629 $templates = apply_filters( 'bbp_get_theme_compat_templates', $templates );
630 $templates = bbp_set_theme_compat_templates( $templates );
631
632 $template = locate_template( $templates, false, false );
633 $template = bbp_set_theme_compat_template( $template );
634
635 return $template;
636 }
637
638 /**
639 * Possibly intercept the template being loaded
640 *
641 * Listens to the 'template_include' filter and waits for a bbPress post_type
642 * to appear. If the current theme does not explicitly support bbPress, it
643 * intercepts the page template and uses one served from the bbPress compatable
644 * theme, set in the $bbp->theme_compat global. If the current theme does
645 * support bbPress, we'll explore the template hierarchy and try to locate one.
646 *
647 * @since bbPress (r3032)
648 *
649 * @global bbPress $bbp
650 * @global WP_Query $post
651 *
652 * @param string $template
653 *
654 * @uses current_theme_supports() To check if theme supports bbPress
655 * @uses bbp_is_single_user() To check if page is single user
656 * @uses bbp_get_single_user_template() To get user template
657 * @uses bbp_is_single_user_edit() To check if page is single user edit
658 * @uses bbp_get_single_user_edit_template() To get user edit template
659 * @uses bbp_is_single_view() To check if page is single view
660 * @uses bbp_get_single_view_template() To get view template
661 * @uses bbp_is_topic_merge() To check if page is topic merge
662 * @uses bbp_get_topic_merge_template() To get topic merge template
663 * @uses bbp_is_topic_split() To check if page is topic split
664 * @uses bbp_get_topic_split_template() To get topic split template
665 * @uses bbp_is_topic_edit() To check if page is topic edit
666 * @uses bbp_get_topic_edit_template() To get topic edit template
667 * @uses bbp_is_reply_edit() To check if page is reply edit
668 * @uses bbp_get_reply_edit_template() To get reply edit template
669 * @uses bbp_set_theme_compat_template() To set the global theme compat template
670 *
671 * @return string The path to the template file that is being used
672 */
673 function bbp_template_include_theme_supports( $template = '' ) {
674 global $bbp;
675
676 // Current theme supports bbPress
677 if ( current_theme_supports( 'bbpress' ) ) {
678
679 // Viewing a user
680 if ( bbp_is_single_user() && ( $new_template = bbp_get_single_user_template() ) ) :
681
682 // Editing a user
683 elseif ( bbp_is_single_user_edit() && ( $new_template = bbp_get_single_user_edit_template() ) ) :
684
685 // Single View
686 elseif ( bbp_is_single_view() && ( $new_template = bbp_get_single_view_template() ) ) :
687
688 // Topic merge
689 elseif ( bbp_is_topic_merge() && ( $new_template = bbp_get_topic_merge_template() ) ) :
690
691 // Topic split
692 elseif ( bbp_is_topic_split() && ( $new_template = bbp_get_topic_split_template() ) ) :
693
694 // Topic edit
695 elseif ( bbp_is_topic_edit() && ( $new_template = bbp_get_topic_edit_template() ) ) :
696
697 // Editing a reply
698 elseif ( bbp_is_reply_edit() && ( $new_template = bbp_get_reply_edit_template() ) ) :
699 endif;
700
701 // Custom template file exists
702 $template = !empty( $new_template ) ? $new_template : $template;
703 }
704
705 return apply_filters( 'bbp_template_include_theme_supports', $template );
706 }
707
708 /**
709 * In this next bit, either the current theme does not support bbPress, or
710 * the theme author has incorrectly used add_theme_support( 'bbpress' )
711 * and we are going to help them out by silently filling in the blanks.
712 */
713 function bbp_template_include_theme_compat( $template = '' ) {
714 global $bbp;
715
716 if ( !current_theme_supports( 'bbpress' ) || ( !empty( $bbp->theme_compat->templates ) && empty( $bbp->theme_compat->template ) ) ) {
717
718 /** Users *************************************************************/
719
720 if ( bbp_is_single_user() || bbp_is_single_user_edit() ) {
721
722 // Reset post
723 bbp_theme_compat_reset_post( array(
724 'post_title' => esc_attr( bbp_get_displayed_user_field( 'display_name' ) )
725 ) );
726
727 /** Forums ************************************************************/
728
729 // Forum archive
730 } elseif ( bbp_is_forum_archive() ) {
731
732 // Reset post
733 bbp_theme_compat_reset_post( array(
734 'ID' => 0,
735 'post_title' => bbp_get_forum_archive_title(),
736 'post_author' => 0,
737 'post_date' => 0,
738 'post_content' => '',
739 'post_type' => bbp_get_forum_post_type(),
740 'post_status' => 'publish'
741 ) );
742
743 /** Topics ************************************************************/
744
745 // Topic archive
746 } elseif ( bbp_is_topic_archive() ) {
747
748 // Reset post
749 bbp_theme_compat_reset_post( array(
750 'ID' => 0,
751 'post_title' => bbp_get_topic_archive_title(),
752 'post_author' => 0,
753 'post_date' => 0,
754 'post_content' => '',
755 'post_type' => bbp_get_topic_post_type(),
756 'post_status' => 'publish'
757 ) );
758
759 // Single topic
760 } elseif ( bbp_is_topic_edit() || bbp_is_topic_split() || bbp_is_topic_merge() ) {
761
762 // Reset post
763 bbp_theme_compat_reset_post( array(
764 'ID' => bbp_get_topic_id(),
765 'post_title' => bbp_get_topic_title(),
766 'post_author' => bbp_get_topic_author_id(),
767 'post_date' => 0,
768 'post_content' => get_post_field( 'post_content', bbp_get_topic_id() ),
769 'post_type' => bbp_get_topic_post_type(),
770 'post_status' => bbp_get_topic_status()
771 ) );
772
773 /** Replies ***********************************************************/
774
775 // Reply archive
776 } elseif ( is_post_type_archive( bbp_get_reply_post_type() ) ) {
777
778 // Reset post
779 bbp_theme_compat_reset_post( array(
780 'ID' => 0,
781 'post_title' => __( 'Replies', 'bbpress' ),
782 'post_author' => 0,
783 'post_date' => 0,
784 'post_content' => '',
785 'post_type' => bbp_get_reply_post_type(),
786 'post_status' => 'publish'
787 ) );
788
789 // Single reply
790 } elseif ( bbp_is_reply_edit() ) {
791
792 // Reset post
793 bbp_theme_compat_reset_post( array(
794 'ID' => bbp_get_reply_id(),
795 'post_title' => bbp_get_reply_title(),
796 'post_author' => bbp_get_reply_author_id(),
797 'post_date' => 0,
798 'post_content' => get_post_field( 'post_content', bbp_get_reply_id() ),
799 'post_type' => bbp_get_reply_post_type(),
800 'post_status' => bbp_get_reply_status()
801 ) );
802
803 /** Views *************************************************************/
804
805 } elseif ( bbp_is_single_view() ) {
806
807 // Reset post
808 bbp_theme_compat_reset_post( array(
809 'ID' => 0,
810 'post_title' => bbp_get_view_title(),
811 'post_author' => 0,
812 'post_date' => 0,
813 'post_content' => '',
814 'post_type' => '',
815 'post_status' => 'publish'
816 ) );
817
818
819 /** Topic Tags ********************************************************/
820
821 } elseif ( bbp_is_topic_tag() ) {
822
823 // Stash the current term in a new var
824 set_query_var( 'bbp_topic_tag', get_query_var( 'term' ) );
825
826 // Reset the post with our new title
827 bbp_theme_compat_reset_post( array(
828 'post_title' => sprintf( __( 'Topic Tag: %s', 'bbpress' ), '<span>' . bbp_get_topic_tag_name() . '</span>' ),
829 ) );
830
831 /** Single Forums/Topics/Replies **************************************/
832
833 } elseif ( bbp_is_custom_post_type() ) {
834 bbp_set_theme_compat_active();
835 }
836
837 /**
838 * If we are relying on bbPress's built in theme compatibility to load
839 * the proper content, we need to intercept the_content, replace the
840 * output, and display ours instead.
841 *
842 * To do this, we first remove all filters from 'the_content' and hook
843 * our own function into it, which runs a series of checks to determine
844 * the context, and then uses the built in shortcodes to output the
845 * correct results.
846 *
847 * We default to using page.php, since it's most likely to exist and
848 * should be coded to work without superfluous elements and logic, like
849 * prev/next navigation, comments, date/time, etc... You can hook into
850 * the 'bbp_template_include' filter to override page.php.
851 */
852 if ( bbp_is_theme_compat_active() ) {
853
854 // Remove all filters from the_content
855 bbp_remove_all_filters( 'the_content' );
856
857 // Add a filter on the_content late, which we will later remove
858 add_filter( 'the_content', 'bbp_replace_the_content' );
859
860 // Find the appropriate template file
861 $template = bbp_get_theme_compat_templates();
862 }
863 }
864
865 return apply_filters( 'bbp_template_include_theme_compat', $template );
866 }
867
868 /**
869 * Replaces the_content() if the post_type being displayed is one that would
870 * normally be handled by bbPress, but proper single page templates do not
871 * exist in the currently active theme.
872 *
873 * @since bbPress (r3034)
874 *
875 * @global bbPress $bbp
876 * @global WP_Query $post
877 * @param string $content
878 * @return type
879 */
880 function bbp_replace_the_content( $content = '' ) {
881
882 // Current theme does not support bbPress, so we need to do some heavy
883 // lifting to see if a bbPress template is needed in the current context
884 if ( !current_theme_supports( 'bbpress' ) ) {
885
886 // Use the $post global to check it's post_type
887 global $bbp;
888
889 // Define local variable(s)
890 $new_content = '';
891
892 // Remove the filter that was added in bbp_template_include()
893 remove_filter( 'the_content', 'bbp_replace_the_content' );
894
895 // Bail if shortcodes are unset somehow
896 if ( empty( $bbp->shortcodes ) )
897 return $content;
898
899 // Use shortcode API to display forums/topics/replies because they are
900 // already output buffered and ready to fit inside the_content
901
902 /** Users *************************************************************/
903
904 // Profile View
905 if ( bbp_is_single_user() ) {
906 ob_start();
907
908 bbp_get_template_part( 'bbpress/content', 'single-user' );
909
910 $new_content = ob_get_contents();
911
912 ob_end_clean();
913
914 // Profile Edit
915 } elseif ( bbp_is_single_user_edit() ) {
916 ob_start();
917
918 bbp_get_template_part( 'bbpress/content', 'single-user-edit' );
919
920 $new_content = ob_get_contents();
921
922 ob_end_clean();
923
924 /** Forums ************************************************************/
925
926 // Forum archive
927 } elseif ( bbp_is_forum_archive() ) {
928
929 // Page exists where this archive should be
930 if ( $page = bbp_get_page_by_path( $bbp->root_slug ) ) {
931
932 // Start output buffer
933 ob_start();
934
935 // Restore previously unset filters
936 bbp_restore_all_filters( 'the_content' );
937
938 // Grab the content of this page
939 $new_content = do_shortcode( apply_filters( 'the_content', get_post_field( 'post_content', $page->ID ) ) );
940
941 // Clean up the buffer
942 ob_end_clean();
943
944 // No page so show the archive
945 } else {
946 $new_content = $bbp->shortcodes->display_forum_index();
947 }
948
949 /** Topics ************************************************************/
950
951 // Topic archive
952 } elseif ( bbp_is_topic_archive() ) {
953
954 // Page exists where this archive should be
955 if ( $page = bbp_get_page_by_path( $bbp->topic_archive_slug ) ) {
956
957 // Start output buffer
958 ob_start();
959
960 // Restore previously unset filters
961 bbp_restore_all_filters( 'the_content' );
962
963 // Grab the content of this page
964 $new_content = do_shortcode( apply_filters( 'the_content', get_post_field( 'post_content', $page->ID ) ) );
965
966 // Clean up the buffer
967 ob_end_clean();
968
969
970 // No page so show the archive
971 } else {
972 $new_content = $bbp->shortcodes->display_topic_index();
973 }
974
975 // Single topic
976 } elseif ( bbp_is_topic_edit() ) {
977
978 // Split
979 if ( bbp_is_topic_split() ) {
980 ob_start();
981
982 bbp_get_template_part( 'bbpress/form', 'topic-split' );
983
984 $new_content = ob_get_contents();
985
986 ob_end_clean();
987
988 // Merge
989 } elseif ( bbp_is_topic_merge() ) {
990 ob_start();
991
992 bbp_get_template_part( 'bbpress/form', 'topic-merge' );
993
994 $new_content = ob_get_contents();
995
996 ob_end_clean();
997
998 // Edit
999 } else {
1000 $new_content = $bbp->shortcodes->display_topic_form();
1001 }
1002
1003 /** Replies ***********************************************************/
1004
1005 // Reply archive
1006 } elseif ( is_post_type_archive( bbp_get_reply_post_type() ) ) {
1007 //$new_content = $bbp->shortcodes->display_reply_index();
1008
1009 // Reply Edit
1010 } elseif ( bbp_is_reply_edit() ) {
1011 $new_content = $bbp->shortcodes->display_reply_form();
1012
1013 /** Views *************************************************************/
1014
1015 } elseif ( bbp_is_single_view() ) {
1016 $new_content = $bbp->shortcodes->display_view( array( 'id' => get_query_var( 'bbp_view' ) ) );
1017
1018 /** Topic Tags ********************************************************/
1019
1020 } elseif ( get_query_var( 'bbp_topic_tag' ) ) {
1021 $new_content = $bbp->shortcodes->display_topics_of_tag( array( 'id' => bbp_get_topic_tag_id() ) );
1022
1023 /** Forums/Topics/Replies *********************************************/
1024
1025 } else {
1026
1027 // Check the post_type
1028 switch ( get_post_type() ) {
1029
1030 // Single Forum
1031 case bbp_get_forum_post_type() :
1032 $new_content = $bbp->shortcodes->display_forum( array( 'id' => get_the_ID() ) );
1033 break;
1034
1035 // Single Topic
1036 case bbp_get_topic_post_type() :
1037 $new_content = $bbp->shortcodes->display_topic( array( 'id' => get_the_ID() ) );
1038 break;
1039
1040 // Single Reply
1041 case bbp_get_reply_post_type() :
1042
1043 break;
1044 }
1045 }
1046
1047 // Juggle the content around and try to prevent unsightly comments
1048 if ( !empty( $new_content ) && ( $new_content != $content ) ) {
1049
1050 // Set the content to be the new content
1051 $content = apply_filters( 'bbp_replace_the_content', $new_content, $content );
1052
1053 // Clean up after ourselves
1054 unset( $new_content );
1055
1056 /**
1057 * Supplemental hack to prevent stubborn comments_template() output.
1058 *
1059 * By this time we can safely assume that everything we needed from
1060 * the {$post} global has been rendered into the buffer, so we're
1061 * going to empty it and {$withcomments} for good measure. This has
1062 * the added benefit of preventing an incorrect "Edit" link on the
1063 * bottom of most popular page templates, at the cost of rendering
1064 * these globals useless for the remaining page output without using
1065 * wp_reset_postdata() to get that data back.
1066 *
1067 * @see comments_template() For why we're doing this :)
1068 * @see wp_reset_postdata() If you need to get $post back
1069 *
1070 * Note: If a theme uses custom code to output comments, it's
1071 * possible all of this dancing around is for not.
1072 *
1073 * Note: If you need to keep these globals around for any special
1074 * reason, we've provided a failsafe hook to bypass this you
1075 * can put in your plugin or theme below ---v
1076 *
1077 * apply_filters( 'bbp_spill_the_beans', '__return_true' );
1078 */
1079 if ( !apply_filters( 'bbp_spill_the_beans', false ) ) {
1080
1081 // Setup the chopping block
1082 global $post, $withcomments;
1083
1084 // Empty out globals that aren't being used in this loop anymore
1085 $withcomments = $post = false;
1086 }
1087 }
1088 }
1089
1090 // Return possibly hi-jacked content
1091 return $content;
1092 }
1093
1094 /** Helpers *******************************************************************/
1095
1096 /**
1097 * Remove the canonical redirect to allow pretty pagination
1098 *
1099 * @since bbPress (r2628)
1100 *
1101 * @param string $redirect_url Redirect url
1102 * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks
1103 * @uses bbp_is_topic() To check if it's a topic page
1104 * @uses bbp_get_paged() To get the current page number
1105 * @uses bbp_is_forum() To check if it's a forum page
1106 * @return bool|string False if it's a topic/forum and their first page,
1107 * otherwise the redirect url
1108 */
1109 function bbp_redirect_canonical( $redirect_url ) {
1110 global $wp_rewrite;
1111
1112 if ( $wp_rewrite->using_permalinks() ) {
1113 if ( bbp_is_topic() && 1 < bbp_get_paged() )
1114 $redirect_url = false;
1115 elseif ( bbp_is_forum() && 1 < bbp_get_paged() )
1116 $redirect_url = false;
1117 }
1118
1119 return $redirect_url;
1120 }
1121
1122 /**
1123 * Sets the 404 status.
1124 *
1125 * Used primarily with topics/replies inside hidden forums.
1126 *
1127 * @since bbPress (r3051)
1128 *
1129 * @global WP_Query $wp_query
1130 * @uses WP_Query::set_404()
1131 */
1132 function bbp_set_404() {
1133 global $wp_query;
1134
1135 if ( ! isset( $wp_query ) ) {
1136 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
1137 return false;
1138 }
1139
1140 $wp_query->set_404();
1141 }
1142
1143 /**
1144 * Used to guess if page exists at requested path
1145 *
1146 * @since bbPress (r3304)
1147 *
1148 * @uses get_option() To see if pretty permalinks are enabled
1149 * @uses get_page_by_path() To see if page exists at path
1150 *
1151 * @param string $path
1152 * @return mixed False if no page, Page object if true
1153 */
1154 function bbp_get_page_by_path( $path = '' ) {
1155
1156 // Default to false
1157 $retval = false;
1158
1159 // Path is not empty
1160 if ( !empty( $path ) ) {
1161
1162 // Pretty permalinks are on so path might exist
1163 if ( get_option( 'permalink_structure' ) ) {
1164 $retval = get_page_by_path( $path );
1165 }
1166 }
1167
1168 return apply_filters( 'bbp_get_page_by_path', $retval, $path );
1169 }
1170
1171 /** Filters *******************************************************************/
1172
1173 /**
1174 * Removes all filters from a WordPress filter, and stashes them in the $bbp
1175 * global in the event they need to be restored later.
1176 *
1177 * @since bbPress (r3251)
1178 *
1179 * @global bbPress $bbp
1180 * @global WP_filter $wp_filter
1181 * @global array $merged_filters
1182 *
1183 * @param string $tag
1184 * @param int $priority
1185 *
1186 * @return bool
1187 */
1188 function bbp_remove_all_filters( $tag, $priority = false ) {
1189 global $bbp, $wp_filter, $merged_filters;
1190
1191 // Filters exist
1192 if ( isset( $wp_filter[$tag] ) ) {
1193
1194 // Filters exist in this priority
1195 if ( !empty( $priority ) && isset( $wp_filter[$tag][$priority] ) ) {
1196
1197 // Store filters in a backup
1198 $bbp->filters->wp_filter[$tag][$priority] = $wp_filter[$tag][$priority];
1199
1200 // Unset the filters
1201 unset( $wp_filter[$tag][$priority] );
1202
1203 // Priority is empty
1204 } else {
1205
1206 // Store filters in a backup
1207 $bbp->filters->wp_filter[$tag] = $wp_filter[$tag];
1208
1209 // Unset the filters
1210 unset( $wp_filter[$tag] );
1211 }
1212 }
1213
1214 // Check merged filters
1215 if ( isset( $merged_filters[$tag] ) ) {
1216
1217 // Store filters in a backup
1218 $bbp->filters->merged_filters[$tag] = $merged_filters[$tag];
1219
1220 // Unset the filters
1221 unset( $merged_filters[$tag] );
1222 }
1223
1224 return true;
1225 }
1226
1227 /**
1228 * Restores filters from the $bbp global that were removed using
1229 * bbp_remove_all_filters()
1230 *
1231 * @since bbPress (r3251)
1232 *
1233 * @global bbPress $bbp
1234 * @global WP_filter $wp_filter
1235 * @global array $merged_filters
1236 *
1237 * @param string $tag
1238 * @param int $priority
1239 *
1240 * @return bool
1241 */
1242 function bbp_restore_all_filters( $tag, $priority = false ) {
1243 global $bbp, $wp_filter, $merged_filters;
1244
1245 // Filters exist
1246 if ( isset( $bbp->filters->wp_filter[$tag] ) ) {
1247
1248 // Filters exist in this priority
1249 if ( !empty( $priority ) && isset( $bbp->filters->wp_filter[$tag][$priority] ) ) {
1250
1251 // Store filters in a backup
1252 $wp_filter[$tag][$priority] = $bbp->filters->wp_filter[$tag][$priority];
1253
1254 // Unset the filters
1255 unset( $bbp->filters->wp_filter[$tag][$priority] );
1256
1257 // Priority is empty
1258 } else {
1259
1260 // Store filters in a backup
1261 $wp_filter[$tag] = $bbp->filters->wp_filter[$tag];
1262
1263 // Unset the filters
1264 unset( $bbp->filters->wp_filter[$tag] );
1265 }
1266 }
1267
1268 // Check merged filters
1269 if ( isset( $bbp->filters->merged_filters[$tag] ) ) {
1270
1271 // Store filters in a backup
1272 $merged_filters[$tag] = $bbp->filters->merged_filters[$tag];
1273
1274 // Unset the filters
1275 unset( $bbp->filters->merged_filters[$tag] );
1276 }
1277
1278 return true;
1279 }
1280
1281 /**
1282 * Add checks for view page, user page, user edit, topic edit and reply edit
1283 * pages.
1284 *
1285 * If it's a user page, WP_Query::bbp_is_single_user is set to true.
1286 * If it's a user edit page, WP_Query::bbp_is_single_user_edit is set to true
1287 * and the the 'wp-admin/includes/user.php' file is included.
1288 * In addition, on user/user edit pages, WP_Query::home is set to false & query
1289 * vars 'bbp_user_id' with the displayed user id and 'author_name' with the
1290 * displayed user's nicename are added.
1291 *
1292 * If it's a topic edit, WP_Query::bbp_is_topic_edit is set to true and
1293 * similarly, if it's a reply edit, WP_Query::bbp_is_reply_edit is set to true.
1294 *
1295 * If it's a view page, WP_Query::bbp_is_view is set to true
1296 *
1297 * @since bbPress (r2688)
1298 *
1299 * @global bbPress $bbp
1300 * #global WP_Query $wp_query
1301 *
1302 * @uses get_query_var() To get {@link WP_Query} query var
1303 * @uses is_email() To check if the string is an email
1304 * @uses get_user_by() To try to get the user by email and nicename
1305 * @uses WP_User to get the user data
1306 * @uses WP_Query::set_404() To set a 404 status
1307 * @uses current_user_can() To check if the current user can edit the user
1308 * @uses apply_filters() Calls 'enable_edit_any_user_configuration' with true
1309 * @uses wp_die() To die
1310 * @uses bbp_is_query_name() Check if query name is 'bbp_widget'
1311 * @uses bbp_get_view_query_args() To get the view query args
1312 * @uses bbp_get_topic_post_type() To get the topic post type
1313 * @uses bbp_get_reply_post_type() To get the reply post type
1314 * @uses is_multisite() To check if it's a multisite
1315 * @uses remove_action() To remove the auto save post revision action
1316 */
1317 function bbp_pre_get_posts( $posts_query ) {
1318 global $bbp, $wp_the_query;
1319
1320 // Bail if $posts_query is not the main loop
1321 if ( $posts_query != $wp_the_query )
1322 return $posts_query;
1323
1324 // Bail if filters are suppressed on this query, or in admin
1325 if ( true == $posts_query->get( 'suppress_filters' ) )
1326 return $posts_query;
1327
1328 // Bail if in admin
1329 if ( is_admin() )
1330 return $posts_query;
1331
1332 // Get query variables
1333 $bbp_user = $posts_query->get( $bbp->user_id );
1334 $bbp_view = $posts_query->get( $bbp->view_id );
1335 $is_edit = $posts_query->get( $bbp->edit_id );
1336
1337 // It is a user page - We'll also check if it is user edit
1338 if ( !empty( $bbp_user ) ) {
1339
1340 // Not a user_id so try email and slug
1341 if ( !is_numeric( $bbp_user ) ) {
1342
1343 // Email was passed
1344 if ( is_email( $bbp_user ) )
1345 $bbp_user = get_user_by( 'email', $bbp_user );
1346 // Try nicename
1347 else
1348 $bbp_user = get_user_by( 'slug', $bbp_user );
1349
1350 // If we were successful, set to ID
1351 if ( is_object( $bbp_user ) )
1352 $bbp_user = $bbp_user->ID;
1353 }
1354
1355 // Create new user
1356 $user = new WP_User( $bbp_user );
1357
1358 // Stop if no user
1359 if ( !isset( $user ) || empty( $user ) || empty( $user->ID ) ) {
1360 $posts_query->set_404();
1361 return;
1362 }
1363
1364 /** User Exists *******************************************************/
1365
1366 // View or edit?
1367 if ( !empty( $is_edit ) ) {
1368
1369 // Only allow super admins on multisite to edit every user.
1370 if ( ( is_multisite() && !current_user_can( 'manage_network_users' ) && $user_id != $current_user->ID && !apply_filters( 'enable_edit_any_user_configuration', true ) ) || !current_user_can( 'edit_user', $user->ID ) )
1371 wp_die( __( 'You do not have the permission to edit this user.', 'bbpress' ) );
1372
1373 // We are editing a profile
1374 $posts_query->bbp_is_single_user_edit = true;
1375
1376 // Load the core WordPress contact methods
1377 if ( !function_exists( '_wp_get_user_contactmethods' ) )
1378 include_once( ABSPATH . 'wp-includes/registration.php' );
1379
1380 // Load the edit_user functions
1381 if ( !function_exists( 'edit_user' ) )
1382 require_once( ABSPATH . 'wp-admin/includes/user.php' );
1383
1384 // We are viewing a profile
1385 } else {
1386 $posts_query->bbp_is_single_user = true;
1387 }
1388
1389 // Make sure 404 is not set
1390 $posts_query->is_404 = false;
1391
1392 // Correct is_home variable
1393 $posts_query->is_home = false;
1394
1395 // Set bbp_user_id for future reference
1396 $posts_query->query_vars['bbp_user_id'] = $user->ID;
1397
1398 // Set author_name as current user's nicename to get correct posts
1399 if ( !bbp_is_query_name( 'bbp_widget' ) )
1400 $posts_query->query_vars['author_name'] = $user->user_nicename;
1401
1402 // Set the displayed user global to this user
1403 $bbp->displayed_user = $user;
1404
1405 // View Page
1406 } elseif ( !empty( $bbp_view ) ) {
1407
1408 // Check if the view exists by checking if there are query args are set
1409 $view_args = bbp_get_view_query_args( $bbp_view );
1410
1411 // Stop if view args is false - means the view isn't registered
1412 if ( false === $view_args ) {
1413 $posts_query->set_404();
1414 return;
1415 }
1416
1417 // Correct is_home variable
1418 $posts_query->is_home = false;
1419
1420 // We are in a custom topic view
1421 $posts_query->bbp_is_view = true;
1422
1423 // Topic/Reply Edit Page
1424 } elseif ( !empty( $is_edit ) ) {
1425
1426 // We are editing a topic
1427 if ( $posts_query->get( 'post_type' ) == bbp_get_topic_post_type() )
1428 $posts_query->bbp_is_topic_edit = true;
1429
1430 // We are editing a reply
1431 elseif ( $posts_query->get( 'post_type' ) == bbp_get_reply_post_type() )
1432 $posts_query->bbp_is_reply_edit = true;
1433
1434 // We save post revisions on our own
1435 remove_action( 'pre_post_update', 'wp_save_post_revision' );
1436 }
1437
1438 return $posts_query;
1439 }
1440
1441 ?>
1442