PluginProbe
bbPress / 2.1-rc2
bbPress v2.1-rc2
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-template-functions.php

bbp-template-functions.php in bbPress 2.1-rc2, at bbp-includes/bbp-template-functions.php

359 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Template Functions
5 *
6 * This file contains functions necessary to mirror the WordPress core template
7 * loading process. Many of those functions are not filterable, and even then
8 * would not be robust enough to predict where bbPress templates might exist.
9 *
10 * @package bbPress
11 * @subpackage TemplateFunctions
12 */
13
14 // Exit if accessed directly
15 if ( !defined( 'ABSPATH' ) ) exit;
16
17 /**
18 * Adds bbPress theme support to any active WordPress theme
19 *
20 * @since bbPress (r3032)
21 *
22 * @param string $slug
23 * @param string $name Optional. Default null
24 * @uses bbp_locate_template()
25 * @uses load_template()
26 * @uses get_template_part()
27 */
28 function bbp_get_template_part( $slug, $name = null ) {
29
30 // Execute code for this part
31 do_action( 'get_template_part_' . $slug, $slug, $name );
32
33 // Setup possible parts
34 $templates = array();
35 if ( isset( $name ) )
36 $templates[] = $slug . '-' . $name . '.php';
37 $templates[] = $slug . '.php';
38
39 // Allow template parst to be filtered
40 $templates = apply_filters( 'bbp_get_template_part', $templates, $slug, $name );
41
42 // Return the part that is found
43 return bbp_locate_template( $templates, true, false );
44 }
45
46 /**
47 * Retrieve the name of the highest priority template file that exists.
48 *
49 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
50 * inherit from a parent theme can just overload one file. If the template is
51 * not found in either of those, it looks in the theme-compat folder last.
52 *
53 * @since bbPres (r3618)
54 *
55 * @param string|array $template_names Template file(s) to search for, in order.
56 * @param bool $load If true the template file will be loaded if it is found.
57 * @param bool $require_once Whether to require_once or require. Default true.
58 * Has no effect if $load is false.
59 * @return string The template filename if one is located.
60 */
61 function bbp_locate_template( $template_names, $load = false, $require_once = true ) {
62
63 // No file found yet
64 $located = false;
65
66 // Try to find a template file
67 foreach ( (array) $template_names as $template_name ) {
68
69 // Continue if template is empty
70 if ( empty( $template_name ) )
71 continue;
72
73 // Trim off any slashes from the template name
74 $template_name = ltrim( $template_name, '/' );
75
76 // Check child theme first
77 if ( file_exists( trailingslashit( STYLESHEETPATH ) . $template_name ) ) {
78 $located = trailingslashit( STYLESHEETPATH ) . $template_name;
79 break;
80
81 // Check parent theme next
82 } elseif ( file_exists( trailingslashit( TEMPLATEPATH ) . $template_name ) ) {
83 $located = trailingslashit( TEMPLATEPATH ) . $template_name;
84 break;
85
86 // Check theme compatibility last
87 } elseif ( file_exists( trailingslashit( bbp_get_theme_compat_dir() ) . $template_name ) ) {
88 $located = trailingslashit( bbp_get_theme_compat_dir() ) . $template_name;
89 break;
90 }
91 }
92
93 if ( ( true == $load ) && !empty( $located ) )
94 load_template( $located, $require_once );
95
96 return $located;
97 }
98
99 /**
100 * Retrieve path to a template
101 *
102 * Used to quickly retrieve the path of a template without including the file
103 * extension. It will also check the parent theme and theme-compat theme with
104 * the use of {@link bbp_locate_template()}. Allows for more generic template
105 * locations without the use of the other get_*_template() functions.
106 *
107 * @since bbPress (r3629)
108 *
109 * @param string $type Filename without extension.
110 * @param array $templates An optional list of template candidates
111 * @uses bbp_set_theme_compat_templates()
112 * @uses bbp_locate_template()
113 * @uses bbp_set_theme_compat_template()
114 * @return string Full path to file.
115 */
116 function bbp_get_query_template( $type, $templates = array() ) {
117 $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
118
119 if ( empty( $templates ) )
120 $templates = array( "{$type}.php" );
121
122 // Filter possible templates, try to match one, and set any bbPress theme
123 // compat properties so they can be cross-checked later.
124 $templates = apply_filters( "bbp_get_{$type}_template", $templates );
125 $templates = bbp_set_theme_compat_templates( $templates );
126 $template = bbp_locate_template( $templates );
127 $template = bbp_set_theme_compat_template( $template );
128
129 return apply_filters( "bbp_{$type}_template", $template );
130 }
131
132 /**
133 * Get the possible subdirectories to check for templates in
134 *
135 * @since bbPress (r3738)
136 * @param array $templates Templates we are looking for
137 * @return array Possible subfolders to look in
138 */
139 function bbp_get_template_locations( $templates = array() ) {
140 $locations = array(
141 'bbpress',
142 'forums',
143 ''
144 );
145 return apply_filters( 'bbp_get_template_locations', $locations, $templates );
146 }
147
148 /**
149 * Add template locations to template files being searched for
150 *
151 * @since bbPress (r3738)
152 *
153 * @param array $templates
154 * @return array()
155 */
156 function bbp_add_template_locations( $templates = array() ) {
157 $retval = array();
158
159 // Get alternate locations
160 $locations = bbp_get_template_locations( $templates );
161
162 // Loop through locations and templates and combine
163 foreach ( $locations as $location )
164 foreach ( $templates as $template )
165 $retval[] = trailingslashit( $location ) . $template;
166
167 return apply_filters( 'bbp_add_template_locations', $retval, $templates );
168 }
169
170 /**
171 * Add checks for bbPress conditions to parse_query action
172 *
173 * If it's a user page, WP_Query::bbp_is_single_user is set to true.
174 * If it's a user edit page, WP_Query::bbp_is_single_user_edit is set to true
175 * and the the 'wp-admin/includes/user.php' file is included.
176 * In addition, on user/user edit pages, WP_Query::home is set to false & query
177 * vars 'bbp_user_id' with the displayed user id and 'author_name' with the
178 * displayed user's nicename are added.
179 *
180 * If it's a forum edit, WP_Query::bbp_is_forum_edit is set to true
181 * If it's a topic edit, WP_Query::bbp_is_topic_edit is set to true
182 * If it's a reply edit, WP_Query::bbp_is_reply_edit is set to true.
183 *
184 * If it's a view page, WP_Query::bbp_is_view is set to true
185 *
186 * @since bbPress (r2688)
187 *
188 * @param WP_Query $posts_query
189 *
190 * @uses get_query_var() To get {@link WP_Query} query var
191 * @uses is_email() To check if the string is an email
192 * @uses get_user_by() To try to get the user by email and nicename
193 * @uses WP_User to get the user data
194 * @uses WP_Query::set_404() To set a 404 status
195 * @uses current_user_can() To check if the current user can edit the user
196 * @uses apply_filters() Calls 'enable_edit_any_user_configuration' with true
197 * @uses bbp_is_query_name() Check if query name is 'bbp_widget'
198 * @uses bbp_get_view_query_args() To get the view query args
199 * @uses bbp_get_forum_post_type() To get the forum post type
200 * @uses bbp_get_topic_post_type() To get the topic post type
201 * @uses bbp_get_reply_post_type() To get the reply post type
202 * @uses remove_action() To remove the auto save post revision action
203 */
204 function bbp_parse_query( $posts_query ) {
205
206 // Bail if $posts_query is not the main loop
207 if ( ! $posts_query->is_main_query() )
208 return;
209
210 // Bail if filters are suppressed on this query
211 if ( true == $posts_query->get( 'suppress_filters' ) )
212 return;
213
214 // Bail if in admin
215 if ( is_admin() )
216 return;
217
218 // Get query variables
219 $bbp_user = $posts_query->get( bbp_get_user_rewrite_id() );
220 $bbp_view = $posts_query->get( bbp_get_view_rewrite_id() );
221 $is_edit = $posts_query->get( bbp_get_edit_rewrite_id() );
222
223 // It is a user page - We'll also check if it is user edit
224 if ( !empty( $bbp_user ) ) {
225
226 // Not a user_id so try email and slug
227 if ( !is_numeric( $bbp_user ) ) {
228
229 // Email was passed
230 if ( is_email( $bbp_user ) ) {
231 $bbp_user = get_user_by( 'email', $bbp_user );
232
233 // Try nicename
234 } else {
235 $bbp_user = get_user_by( 'slug', $bbp_user );
236 }
237
238 // If we were successful, set to ID
239 if ( is_object( $bbp_user ) ) {
240 $bbp_user = $bbp_user->ID;
241 }
242 }
243
244 // Create new user
245 $user = new WP_User( $bbp_user );
246
247 // Bail if no user
248 if ( !isset( $user ) || empty( $user ) || empty( $user->ID ) ) {
249 $posts_query->set_404();
250 return;
251 }
252
253 /** User Exists *******************************************************/
254
255 // View or edit?
256 if ( !empty( $is_edit ) ) {
257
258 // We are editing a profile
259 $posts_query->bbp_is_single_user_edit = true;
260
261 // Load the core WordPress contact methods
262 if ( !function_exists( '_wp_get_user_contactmethods' ) ) {
263 include_once( ABSPATH . 'wp-includes/registration.php' );
264 }
265
266 // Load the edit_user functions
267 if ( !function_exists( 'edit_user' ) ) {
268 require_once( ABSPATH . 'wp-admin/includes/user.php' );
269 }
270
271 // Editing a user
272 $posts_query->bbp_is_edit = true;
273
274 // We are viewing a profile
275 } else {
276 $posts_query->bbp_is_single_user = true;
277 }
278
279 // Make sure 404 is not set
280 $posts_query->is_404 = false;
281
282 // Correct is_home variable
283 $posts_query->is_home = false;
284
285 // Set bbp_user_id for future reference
286 $posts_query->set( 'bbp_user_id', $user->ID );
287
288 // Set author_name as current user's nicename to get correct posts
289 if ( !bbp_is_query_name( 'bbp_widget' ) ) {
290 $posts_query->set( 'author_name', $user->user_nicename );
291 }
292
293 // Set the displayed user global to this user
294 bbpress()->displayed_user = $user;
295
296 // View Page
297 } elseif ( !empty( $bbp_view ) ) {
298
299 // Check if the view exists by checking if there are query args are set
300 $view_args = bbp_get_view_query_args( $bbp_view );
301
302 // Bail if view args is false (view isn't registered)
303 if ( false === $view_args ) {
304 $posts_query->set_404();
305 return;
306 }
307
308 // Correct is_home variable
309 $posts_query->is_home = false;
310
311 // We are in a custom topic view
312 $posts_query->bbp_is_view = true;
313
314 // Forum/Topic/Reply Edit Page
315 } elseif ( !empty( $is_edit ) ) {
316
317 // Get the post type from the main query loop
318 $post_type = $posts_query->get( 'post_type' );
319
320 // Check which post_type we are editing, if any
321 if ( !empty( $post_type ) ) {
322 switch( $post_type ) {
323
324 // We are editing a forum
325 case bbp_get_forum_post_type() :
326 $posts_query->bbp_is_forum_edit = true;
327 $posts_query->bbp_is_edit = true;
328 break;
329
330 // We are editing a topic
331 case bbp_get_topic_post_type() :
332 $posts_query->bbp_is_topic_edit = true;
333 $posts_query->bbp_is_edit = true;
334 break;
335
336 // We are editing a reply
337 case bbp_get_reply_post_type() :
338 $posts_query->bbp_is_reply_edit = true;
339 $posts_query->bbp_is_edit = true;
340 break;
341 }
342
343 // We are editing a topic tag
344 } elseif ( bbp_is_topic_tag() ) {
345 $posts_query->bbp_is_topic_tag_edit = true;
346 $posts_query->bbp_is_edit = true;
347 }
348
349 // We save post revisions on our own
350 remove_action( 'pre_post_update', 'wp_save_post_revision' );
351
352 // Topic tag page
353 } elseif ( bbp_is_topic_tag() ) {
354 $posts_query->set( 'bbp_topic_tag', get_query_var( 'term' ) );
355 $posts_query->set( 'post_type', bbp_get_topic_post_type() );
356 $posts_query->set( 'posts_per_page', bbp_get_topics_per_page() );
357 }
358 }
359