| 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_get_view_query_args() To get the view query args |
| 198 |
* @uses bbp_get_forum_post_type() To get the forum post type |
| 199 |
* @uses bbp_get_topic_post_type() To get the topic post type |
| 200 |
* @uses bbp_get_reply_post_type() To get the reply post type |
| 201 |
* @uses remove_action() To remove the auto save post revision action |
| 202 |
*/ |
| 203 |
function bbp_parse_query( $posts_query ) { |
| 204 |
|
| 205 |
// Bail if $posts_query is not the main loop |
| 206 |
if ( ! $posts_query->is_main_query() ) |
| 207 |
return; |
| 208 |
|
| 209 |
// Bail if filters are suppressed on this query |
| 210 |
if ( true == $posts_query->get( 'suppress_filters' ) ) |
| 211 |
return; |
| 212 |
|
| 213 |
// Bail if in admin |
| 214 |
if ( is_admin() ) |
| 215 |
return; |
| 216 |
|
| 217 |
// Get query variables |
| 218 |
$bbp_user = $posts_query->get( bbp_get_user_rewrite_id() ); |
| 219 |
$bbp_view = $posts_query->get( bbp_get_view_rewrite_id() ); |
| 220 |
$is_edit = $posts_query->get( bbp_get_edit_rewrite_id() ); |
| 221 |
|
| 222 |
// It is a user page - We'll also check if it is user edit |
| 223 |
if ( !empty( $bbp_user ) ) { |
| 224 |
|
| 225 |
// Not a user_id so try email and slug |
| 226 |
if ( !is_numeric( $bbp_user ) ) { |
| 227 |
|
| 228 |
// Email was passed |
| 229 |
if ( is_email( $bbp_user ) ) { |
| 230 |
$bbp_user = get_user_by( 'email', $bbp_user ); |
| 231 |
|
| 232 |
// Try nicename |
| 233 |
} else { |
| 234 |
$bbp_user = get_user_by( 'slug', $bbp_user ); |
| 235 |
} |
| 236 |
|
| 237 |
// If we were successful, set to ID |
| 238 |
if ( is_object( $bbp_user ) ) { |
| 239 |
$bbp_user = $bbp_user->ID; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// Create new user |
| 244 |
$user = new WP_User( $bbp_user ); |
| 245 |
|
| 246 |
// Bail if no user |
| 247 |
if ( !isset( $user ) || empty( $user ) || empty( $user->ID ) ) { |
| 248 |
$posts_query->set_404(); |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
/** User Exists *******************************************************/ |
| 253 |
|
| 254 |
// View or edit? |
| 255 |
if ( !empty( $is_edit ) ) { |
| 256 |
|
| 257 |
// We are editing a profile |
| 258 |
$posts_query->bbp_is_single_user_edit = true; |
| 259 |
|
| 260 |
// Load the core WordPress contact methods |
| 261 |
if ( !function_exists( '_wp_get_user_contactmethods' ) ) { |
| 262 |
include_once( ABSPATH . 'wp-includes/registration.php' ); |
| 263 |
} |
| 264 |
|
| 265 |
// Load the edit_user functions |
| 266 |
if ( !function_exists( 'edit_user' ) ) { |
| 267 |
require_once( ABSPATH . 'wp-admin/includes/user.php' ); |
| 268 |
} |
| 269 |
|
| 270 |
// Editing a user |
| 271 |
$posts_query->bbp_is_edit = true; |
| 272 |
|
| 273 |
// We are viewing a profile |
| 274 |
} else { |
| 275 |
$posts_query->bbp_is_single_user = true; |
| 276 |
} |
| 277 |
|
| 278 |
// Make sure 404 is not set |
| 279 |
$posts_query->is_404 = false; |
| 280 |
|
| 281 |
// Correct is_home variable |
| 282 |
$posts_query->is_home = false; |
| 283 |
|
| 284 |
// Set bbp_user_id for future reference |
| 285 |
$posts_query->set( 'bbp_user_id', $user->ID ); |
| 286 |
|
| 287 |
// Set author_name as current user's nicename to get correct posts |
| 288 |
$posts_query->set( 'author_name', $user->user_nicename ); |
| 289 |
|
| 290 |
// Set the displayed user global to this user |
| 291 |
bbpress()->displayed_user = $user; |
| 292 |
|
| 293 |
// View Page |
| 294 |
} elseif ( !empty( $bbp_view ) ) { |
| 295 |
|
| 296 |
// Check if the view exists by checking if there are query args are set |
| 297 |
$view_args = bbp_get_view_query_args( $bbp_view ); |
| 298 |
|
| 299 |
// Bail if view args is false (view isn't registered) |
| 300 |
if ( false === $view_args ) { |
| 301 |
$posts_query->set_404(); |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
// Correct is_home variable |
| 306 |
$posts_query->is_home = false; |
| 307 |
|
| 308 |
// We are in a custom topic view |
| 309 |
$posts_query->bbp_is_view = true; |
| 310 |
|
| 311 |
// Forum/Topic/Reply Edit Page |
| 312 |
} elseif ( !empty( $is_edit ) ) { |
| 313 |
|
| 314 |
// Get the post type from the main query loop |
| 315 |
$post_type = $posts_query->get( 'post_type' ); |
| 316 |
|
| 317 |
// Check which post_type we are editing, if any |
| 318 |
if ( !empty( $post_type ) ) { |
| 319 |
switch( $post_type ) { |
| 320 |
|
| 321 |
// We are editing a forum |
| 322 |
case bbp_get_forum_post_type() : |
| 323 |
$posts_query->bbp_is_forum_edit = true; |
| 324 |
$posts_query->bbp_is_edit = true; |
| 325 |
break; |
| 326 |
|
| 327 |
// We are editing a topic |
| 328 |
case bbp_get_topic_post_type() : |
| 329 |
$posts_query->bbp_is_topic_edit = true; |
| 330 |
$posts_query->bbp_is_edit = true; |
| 331 |
break; |
| 332 |
|
| 333 |
// We are editing a reply |
| 334 |
case bbp_get_reply_post_type() : |
| 335 |
$posts_query->bbp_is_reply_edit = true; |
| 336 |
$posts_query->bbp_is_edit = true; |
| 337 |
break; |
| 338 |
} |
| 339 |
|
| 340 |
// We are editing a topic tag |
| 341 |
} elseif ( bbp_is_topic_tag() ) { |
| 342 |
$posts_query->bbp_is_topic_tag_edit = true; |
| 343 |
$posts_query->bbp_is_edit = true; |
| 344 |
} |
| 345 |
|
| 346 |
// We save post revisions on our own |
| 347 |
remove_action( 'pre_post_update', 'wp_save_post_revision' ); |
| 348 |
|
| 349 |
// Topic tag page |
| 350 |
} elseif ( bbp_is_topic_tag() ) { |
| 351 |
$posts_query->set( 'bbp_topic_tag', get_query_var( 'term' ) ); |
| 352 |
$posts_query->set( 'post_type', bbp_get_topic_post_type() ); |
| 353 |
$posts_query->set( 'posts_per_page', bbp_get_topics_per_page() ); |
| 354 |
} |
| 355 |
} |
| 356 |
|