| @@ -11,20 +11,17 @@ | ||
| 11 | 11 | * @subpackage TemplateFunctions |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | // Exit if accessed directly |
| 15 | -if ( !defined( 'ABSPATH' ) ) exit; | |
| 15 | +defined( 'ABSPATH' ) || exit; | |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Adds bbPress theme support to any active WordPress theme |
| 19 | 19 | * |
| 20 | - * @since bbPress (r3032) | |
| 20 | + * @since 2.0.0 bbPress (r3032) | |
| 21 | 21 | * |
| 22 | 22 | * @param string $slug |
| 23 | 23 | * @param string $name Optional. Default null |
| 24 | - * @uses bbp_locate_template() | |
| 25 | - * @uses load_template() | |
| 26 | - * @uses get_template_part() | |
| 27 | 24 | */ |
| 28 | 25 | function bbp_get_template_part( $slug, $name = null ) { |
| 29 | 26 | |
| 30 | 27 | // Execute code for this part |
| @@ -31,10 +28,11 @@ | ||
| 31 | 28 | do_action( 'get_template_part_' . $slug, $slug, $name ); |
| 32 | 29 | |
| 33 | 30 | // Setup possible parts |
| 34 | 31 | $templates = array(); |
| 35 | - if ( isset( $name ) ) | |
| 32 | + if ( isset( $name ) ) { | |
| 36 | 33 | $templates[] = $slug . '-' . $name . '.php'; |
| 34 | + } | |
| 37 | 35 | $templates[] = $slug . '.php'; |
| 38 | 36 | |
| 39 | 37 | // Allow template parst to be filtered |
| 40 | 38 | $templates = apply_filters( 'bbp_get_template_part', $templates, $slug, $name ); |
| @@ -49,9 +47,9 @@ | ||
| 49 | 47 | * Searches in the child theme before parent theme so that themes which |
| 50 | 48 | * inherit from a parent theme can just overload one file. If the template is |
| 51 | 49 | * not found in either of those, it looks in the theme-compat folder last. |
| 52 | 50 | * |
| 53 | - * @since bbPres (r3618) | |
| 51 | + * @since 2.1.0 bbPress (r3618) | |
| 54 | 52 | * |
| 55 | 53 | * @param string|array $template_names Template file(s) to search for, in order. |
| 56 | 54 | * @param bool $load If true the template file will be loaded if it is found. |
| 57 | 55 | * @param bool $require_once Whether to require_once or require. Default true. |
| @@ -67,10 +65,11 @@ | ||
| 67 | 65 | // Try to find a template file |
| 68 | 66 | foreach ( (array) $template_names as $template_name ) { |
| 69 | 67 | |
| 70 | 68 | // Continue if template is empty |
| 71 | - if ( empty( $template_name ) ) | |
| 69 | + if ( empty( $template_name ) ) { | |
| 72 | 70 | continue; |
| 71 | + } | |
| 73 | 72 | |
| 74 | 73 | // Trim off any slashes from the template name |
| 75 | 74 | $template_name = ltrim( $template_name, '/' ); |
| 76 | 75 | |
| @@ -77,10 +76,11 @@ | ||
| 77 | 76 | // Loop through template stack |
| 78 | 77 | foreach ( (array) $template_locations as $template_location ) { |
| 79 | 78 | |
| 80 | 79 | // Continue if $template_location is empty |
| 81 | - if ( empty( $template_location ) ) | |
| 80 | + if ( empty( $template_location ) ) { | |
| 82 | 81 | continue; |
| 82 | + } | |
| 83 | 83 | |
| 84 | 84 | // Check child theme first |
| 85 | 85 | if ( file_exists( trailingslashit( $template_location ) . $template_name ) ) { |
| 86 | 86 | $located = trailingslashit( $template_location ) . $template_name; |
| @@ -88,16 +88,197 @@ | ||
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | + /** | |
| 93 | + * This action exists only to follow the standard bbPress coding convention, | |
| 94 | + * and should not be used to short-circuit any part of the template locator. | |
| 95 | + * | |
| 96 | + * If you want to override a specific template part, please either filter | |
| 97 | + * 'bbp_get_template_part' or add a new location to the template stack. | |
| 98 | + */ | |
| 99 | + do_action( 'bbp_locate_template', $located, $template_name, $template_names, $template_locations, $load, $require_once ); | |
| 100 | + | |
| 92 | 101 | // Maybe load the template if one was located |
| 93 | - if ( ( true == $load ) && !empty( $located ) ) | |
| 102 | + if ( ( defined( 'WP_USE_THEMES' ) && WP_USE_THEMES ) && ( true === $load ) && ! empty( $located ) ) { | |
| 94 | 103 | load_template( $located, $require_once ); |
| 104 | + } | |
| 95 | 105 | |
| 96 | 106 | return $located; |
| 97 | 107 | } |
| 98 | 108 | |
| 99 | 109 | /** |
| 110 | + * Locate an enqueueable file on the server. Used before being enqueued. | |
| 111 | + * | |
| 112 | + * If SCRIPT_DEBUG is set and the file includes a .min suffix, this function | |
| 113 | + * will automatically attempt to locate a non-minified version of that file. | |
| 114 | + * | |
| 115 | + * If SCRIPT_DEBUG is not set and the file exclude a .min suffix, this function | |
| 116 | + * will automatically attempt to locate a minified version of that file. | |
| 117 | + * | |
| 118 | + * See: https://bbpress.trac.wordpress.org/ticket/3218 | |
| 119 | + * | |
| 120 | + * @since 2.6.0 | |
| 121 | + * | |
| 122 | + * @param string $file | |
| 123 | + * | |
| 124 | + * @return boolean | |
| 125 | + */ | |
| 126 | +function bbp_locate_enqueueable( $file = '' ) { | |
| 127 | + | |
| 128 | + // Bail if no file to locate | |
| 129 | + if ( empty( $file ) ) { | |
| 130 | + return false; | |
| 131 | + } | |
| 132 | + | |
| 133 | + // Add file to files array | |
| 134 | + $files = array( $file ); | |
| 135 | + | |
| 136 | + // Get the file variant (minified or not, but opposite of $file) | |
| 137 | + $file_is_min = ( false !== strpos( $file, '.min' ) ); | |
| 138 | + $file_variant = ( false === $file_is_min ) | |
| 139 | + ? str_replace( array( '.css', '.js' ), array( '.min.css', '.min.js' ), $file ) | |
| 140 | + : str_replace( '.min', '', $file ); | |
| 141 | + | |
| 142 | + // Are we debugging? | |
| 143 | + $script_debug = bbp_doing_script_debug(); | |
| 144 | + | |
| 145 | + // Debugging, so prefer unminified files | |
| 146 | + if ( true === $script_debug ) { | |
| 147 | + if ( true === $file_is_min ) { | |
| 148 | + array_unshift( $files, $file_variant ); | |
| 149 | + } else { | |
| 150 | + array_push( $files, $file_variant ); | |
| 151 | + } | |
| 152 | + | |
| 153 | + // Not debugging, so prefer minified files | |
| 154 | + } elseif ( false === $script_debug ) { | |
| 155 | + if ( true === $file_is_min ) { | |
| 156 | + array_push( $files, $file_variant ); | |
| 157 | + } else { | |
| 158 | + array_unshift( $files, $file_variant ); | |
| 159 | + } | |
| 160 | + } | |
| 161 | + | |
| 162 | + // Return first found file location in the stack | |
| 163 | + return bbp_locate_template( $files, false, false ); | |
| 164 | +} | |
| 165 | + | |
| 166 | +/** | |
| 167 | + * Convert an enqueueable file path to a URL | |
| 168 | + * | |
| 169 | + * @since 2.6.0 | |
| 170 | + * @param string $file | |
| 171 | + * | |
| 172 | + * @return string | |
| 173 | + */ | |
| 174 | +function bbp_urlize_enqueueable( $file = '' ) { | |
| 175 | + | |
| 176 | + // Get DIR and URL | |
| 177 | + $content_dir = constant( 'WP_CONTENT_DIR' ); | |
| 178 | + $content_url = content_url(); | |
| 179 | + | |
| 180 | + // IIS (Windows) here | |
| 181 | + // Replace back slashes with forward slash | |
| 182 | + if ( false !== strpos( $file, '\\' ) ) { | |
| 183 | + $file = str_replace( '\\', '/', $file ); | |
| 184 | + $content_dir = str_replace( '\\', '/', $content_dir ); | |
| 185 | + } | |
| 186 | + | |
| 187 | + // Return path to file relative to site URL | |
| 188 | + return str_replace( $content_dir, $content_url, $file ); | |
| 189 | +} | |
| 190 | + | |
| 191 | +/** | |
| 192 | + * Enqueue a script from the highest priority location in the template stack. | |
| 193 | + * | |
| 194 | + * Registers the style if file provided (does NOT overwrite) and enqueues. | |
| 195 | + * | |
| 196 | + * @since 2.5.0 bbPress (r5180) | |
| 197 | + * | |
| 198 | + * @param string $handle Name of the stylesheet. | |
| 199 | + * @param string|bool $file Relative path to stylesheet. Example: '/css/mystyle.css'. | |
| 200 | + * @param array $deps An array of registered style handles this stylesheet depends on. Default empty array. | |
| 201 | + * @param string|bool $ver String specifying the stylesheet version number, if it has one. This parameter is used | |
| 202 | + * to ensure that the correct version is sent to the client regardless of caching, and so | |
| 203 | + * should be included if a version number is available and makes sense for the stylesheet. | |
| 204 | + * @param string $media Optional. The media for which this stylesheet has been defined. | |
| 205 | + * Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print', | |
| 206 | + * 'screen', 'tty', or 'tv'. | |
| 207 | + * | |
| 208 | + * @return mixed The style filename if one is located. False if not. | |
| 209 | + */ | |
| 210 | +function bbp_enqueue_style( $handle = '', $file = '', $deps = array(), $ver = false, $media = 'all' ) { | |
| 211 | + | |
| 212 | + // Attempt to locate an enqueueable | |
| 213 | + $located = bbp_locate_enqueueable( $file ); | |
| 214 | + | |
| 215 | + // Enqueue if located | |
| 216 | + if ( ! empty( $located ) ) { | |
| 217 | + | |
| 218 | + // Make sure there is always a version | |
| 219 | + if ( empty( $ver ) ) { | |
| 220 | + $ver = bbp_get_asset_version(); | |
| 221 | + } | |
| 222 | + | |
| 223 | + // Make path to file relative to site URL | |
| 224 | + $located = bbp_urlize_enqueueable( $located ); | |
| 225 | + | |
| 226 | + // Register the style | |
| 227 | + wp_register_style( $handle, $located, $deps, $ver, $media ); | |
| 228 | + | |
| 229 | + // Enqueue the style | |
| 230 | + wp_enqueue_style( $handle ); | |
| 231 | + } | |
| 232 | + | |
| 233 | + return $located; | |
| 234 | +} | |
| 235 | + | |
| 236 | +/** | |
| 237 | + * Enqueue a script from the highest priority location in the template stack. | |
| 238 | + * | |
| 239 | + * Registers the style if file provided (does NOT overwrite) and enqueues. | |
| 240 | + * | |
| 241 | + * @since 2.5.0 bbPress (r5180) | |
| 242 | + * | |
| 243 | + * @param string $handle Name of the script. | |
| 244 | + * @param string|bool $file Relative path to the script. Example: '/js/myscript.js'. | |
| 245 | + * @param array $deps An array of registered handles this script depends on. Default empty array. | |
| 246 | + * @param string|bool $ver Optional. String specifying the script version number, if it has one. This parameter | |
| 247 | + * is used to ensure that the correct version is sent to the client regardless of caching, | |
| 248 | + * and so should be included if a version number is available and makes sense for the script. | |
| 249 | + * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>. | |
| 250 | + * Default 'false'. Accepts 'false' or 'true'. | |
| 251 | + * | |
| 252 | + * @return mixed The script filename if one is located. False if not. | |
| 253 | + */ | |
| 254 | +function bbp_enqueue_script( $handle = '', $file = '', $deps = array(), $ver = false, $in_footer = false ) { | |
| 255 | + | |
| 256 | + // Attempt to locate an enqueueable | |
| 257 | + $located = bbp_locate_enqueueable( $file ); | |
| 258 | + | |
| 259 | + // Enqueue if located | |
| 260 | + if ( ! empty( $located ) ) { | |
| 261 | + | |
| 262 | + // Make sure there is always a version | |
| 263 | + if ( empty( $ver ) ) { | |
| 264 | + $ver = bbp_get_asset_version(); | |
| 265 | + } | |
| 266 | + | |
| 267 | + // Make path to file relative to site URL | |
| 268 | + $located = bbp_urlize_enqueueable( $located ); | |
| 269 | + | |
| 270 | + // Register the style | |
| 271 | + wp_register_script( $handle, $located, $deps, $ver, $in_footer ); | |
| 272 | + | |
| 273 | + // Enqueue the style | |
| 274 | + wp_enqueue_script( $handle ); | |
| 275 | + } | |
| 276 | + | |
| 277 | + return $located; | |
| 278 | +} | |
| 279 | + | |
| 280 | +/** | |
| 100 | 281 | * This is really cool. This function registers a new template stack location, |
| 101 | 282 | * using WordPress's built in filters API. |
| 102 | 283 | * |
| 103 | 284 | * This allows for templates to live in places beyond just the parent/child |
| @@ -103,31 +284,51 @@ | ||
| 103 | 284 | * This allows for templates to live in places beyond just the parent/child |
| 104 | 285 | * relationship, to allow for custom template locations. Used in conjunction |
| 105 | 286 | * with bbp_locate_template(), this allows for easy template overrides. |
| 106 | 287 | * |
| 107 | - * @since bbPress (r4323) | |
| 288 | + * @since 2.2.0 bbPress (r4323) | |
| 108 | 289 | * |
| 109 | - * @param string $location Callback function that returns the | |
| 290 | + * @param string $location_callback Callback function that returns the | |
| 110 | 291 | * @param int $priority |
| 111 | 292 | */ |
| 112 | 293 | function bbp_register_template_stack( $location_callback = '', $priority = 10 ) { |
| 113 | 294 | |
| 114 | - // Bail if no location, or function does not exist | |
| 115 | - if ( empty( $location_callback ) || ! function_exists( $location_callback ) ) | |
| 295 | + // Bail if no location, or function/method is not callable | |
| 296 | + if ( empty( $location_callback ) || ! is_callable( $location_callback ) ) { | |
| 116 | 297 | return false; |
| 298 | + } | |
| 117 | 299 | |
| 118 | 300 | // Add location callback to template stack |
| 119 | - add_filter( 'bbp_template_stack', $location_callback, (int) $priority ); | |
| 301 | + return add_filter( 'bbp_template_stack', $location_callback, (int) $priority ); | |
| 120 | 302 | } |
| 121 | 303 | |
| 122 | 304 | /** |
| 305 | + * Deregisters a previously registered template stack location. | |
| 306 | + * | |
| 307 | + * @since 2.3.0 bbPress (r4652) | |
| 308 | + * | |
| 309 | + * @param string $location_callback Callback function that returns the | |
| 310 | + * @param int $priority | |
| 311 | + * @return bool Whether stack was removed | |
| 312 | + */ | |
| 313 | +function bbp_deregister_template_stack( $location_callback = '', $priority = 10 ) { | |
| 314 | + | |
| 315 | + // Bail if no location, or function/method is not callable | |
| 316 | + if ( empty( $location_callback ) || ! is_callable( $location_callback ) ) { | |
| 317 | + return false; | |
| 318 | + } | |
| 319 | + | |
| 320 | + // Remove location callback to template stack | |
| 321 | + return remove_filter( 'bbp_template_stack', $location_callback, (int) $priority ); | |
| 322 | +} | |
| 323 | + | |
| 324 | +/** | |
| 123 | 325 | * Call the functions added to the 'bbp_template_stack' filter hook, and return |
| 124 | 326 | * an array of the template locations. |
| 125 | 327 | * |
| 126 | - * @see bbp_register_template_stack() | |
| 328 | + * @since 2.2.0 bbPress (r4323) | |
| 329 | + * @since 2.6.0 bbPress (r5944) Added support for `WP_Hook` | |
| 127 | 330 | * |
| 128 | - * @since bbPress (r4323) | |
| 129 | - * | |
| 130 | 331 | * @global array $wp_filter Stores all of the filters |
| 131 | 332 | * @global array $merged_filters Merges the filter hooks using this function. |
| 132 | 333 | * @global array $wp_current_filter stores the list of current filters with the current one last |
| 133 | 334 | * |
| @@ -140,28 +341,43 @@ | ||
| 140 | 341 | $tag = 'bbp_template_stack'; |
| 141 | 342 | $args = $stack = array(); |
| 142 | 343 | |
| 143 | 344 | // Add 'bbp_template_stack' to the current filter array |
| 345 | + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | |
| 144 | 346 | $wp_current_filter[] = $tag; |
| 145 | 347 | |
| 146 | - // Sort | |
| 147 | - if ( ! isset( $merged_filters[ $tag ] ) ) { | |
| 148 | - ksort( $wp_filter[$tag] ); | |
| 149 | - $merged_filters[ $tag ] = true; | |
| 348 | + // Bail if no stack setup | |
| 349 | + if ( empty( $wp_filter[ $tag ] ) ) { | |
| 350 | + return array(); | |
| 150 | 351 | } |
| 151 | 352 | |
| 353 | + // Check if WP_Hook class exists, see #WP17817 | |
| 354 | + if ( class_exists( 'WP_Hook' ) ) { | |
| 355 | + $filter = $wp_filter[ $tag ]->callbacks; | |
| 356 | + } else { | |
| 357 | + $filter = &$wp_filter[ $tag ]; | |
| 358 | + | |
| 359 | + // Sort | |
| 360 | + if ( ! isset( $merged_filters[ $tag ] ) ) { | |
| 361 | + ksort( $filter ); | |
| 362 | + | |
| 363 | + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | |
| 364 | + $merged_filters[ $tag ] = true; | |
| 365 | + } | |
| 366 | + } | |
| 367 | + | |
| 152 | 368 | // Ensure we're always at the beginning of the filter array |
| 153 | - reset( $wp_filter[ $tag ] ); | |
| 369 | + reset( $filter ); | |
| 154 | 370 | |
| 155 | 371 | // Loop through 'bbp_template_stack' filters, and call callback functions |
| 156 | 372 | do { |
| 157 | - foreach( (array) current( $wp_filter[$tag] ) as $the_ ) { | |
| 373 | + foreach ( (array) current( $filter ) as $the_ ) { | |
| 158 | 374 | if ( ! is_null( $the_['function'] ) ) { |
| 159 | 375 | $args[1] = $stack; |
| 160 | 376 | $stack[] = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) ); |
| 161 | 377 | } |
| 162 | 378 | } |
| 163 | - } while ( next( $wp_filter[$tag] ) !== false ); | |
| 379 | + } while ( next( $filter ) !== false ); | |
| 164 | 380 | |
| 165 | 381 | // Remove 'bbp_template_stack' from the current filter array |
| 166 | 382 | array_pop( $wp_current_filter ); |
| 167 | 383 | |
| @@ -167,12 +383,38 @@ | ||
| 167 | 383 | |
| 168 | 384 | // Remove empties and duplicates |
| 169 | 385 | $stack = array_unique( array_filter( $stack ) ); |
| 170 | 386 | |
| 171 | - return (array) apply_filters( 'bbp_get_template_stack', $stack ) ; | |
| 387 | + // Filter & return | |
| 388 | + return (array) apply_filters( 'bbp_get_template_stack', $stack ); | |
| 172 | 389 | } |
| 173 | 390 | |
| 174 | 391 | /** |
| 392 | + * Get a template part in an output buffer, and return it | |
| 393 | + * | |
| 394 | + * @since 2.4.0 bbPress (r5043) | |
| 395 | + * | |
| 396 | + * @param string $slug | |
| 397 | + * @param string $name | |
| 398 | + * @return string | |
| 399 | + */ | |
| 400 | +function bbp_buffer_template_part( $slug, $name = null, $echo = true ) { | |
| 401 | + ob_start(); | |
| 402 | + | |
| 403 | + bbp_get_template_part( $slug, $name ); | |
| 404 | + | |
| 405 | + // Get the output buffer contents | |
| 406 | + $output = ob_get_clean(); | |
| 407 | + | |
| 408 | + // Echo or return the output buffer contents | |
| 409 | + if ( true === $echo ) { | |
| 410 | + echo $output; | |
| 411 | + } else { | |
| 412 | + return $output; | |
| 413 | + } | |
| 414 | +} | |
| 415 | + | |
| 416 | +/** | |
| 175 | 417 | * Retrieve path to a template |
| 176 | 418 | * |
| 177 | 419 | * Used to quickly retrieve the path of a template without including the file |
| 178 | 420 | * extension. It will also check the parent theme and theme-compat theme with |
| @@ -178,39 +420,45 @@ | ||
| 178 | 420 | * extension. It will also check the parent theme and theme-compat theme with |
| 179 | 421 | * the use of {@link bbp_locate_template()}. Allows for more generic template |
| 180 | 422 | * locations without the use of the other get_*_template() functions. |
| 181 | 423 | * |
| 182 | - * @since bbPress (r3629) | |
| 424 | + * @since 2.1.0 bbPress (r3629) | |
| 183 | 425 | * |
| 184 | 426 | * @param string $type Filename without extension. |
| 185 | 427 | * @param array $templates An optional list of template candidates |
| 186 | - * @uses bbp_set_theme_compat_templates() | |
| 187 | - * @uses bbp_locate_template() | |
| 188 | - * @uses bbp_set_theme_compat_template() | |
| 189 | 428 | * @return string Full path to file. |
| 190 | 429 | */ |
| 191 | 430 | function bbp_get_query_template( $type, $templates = array() ) { |
| 192 | 431 | $type = preg_replace( '|[^a-z0-9-]+|', '', $type ); |
| 193 | 432 | |
| 194 | - if ( empty( $templates ) ) | |
| 433 | + // Fallback template | |
| 434 | + if ( empty( $templates ) ) { | |
| 195 | 435 | $templates = array( "{$type}.php" ); |
| 436 | + } | |
| 196 | 437 | |
| 197 | - // Filter possible templates, try to match one, and set any bbPress theme | |
| 198 | - // compat properties so they can be cross-checked later. | |
| 438 | + // Filter possible templates | |
| 199 | 439 | $templates = apply_filters( "bbp_get_{$type}_template", $templates ); |
| 200 | - $templates = bbp_set_theme_compat_templates( $templates ); | |
| 201 | - $template = bbp_locate_template( $templates ); | |
| 202 | - $template = bbp_set_theme_compat_template( $template ); | |
| 203 | 440 | |
| 204 | - return apply_filters( "bbp_{$type}_template", $template ); | |
| 441 | + // Stash the possible templates for this query, for later use | |
| 442 | + bbp_set_theme_compat_templates( $templates ); | |
| 443 | + | |
| 444 | + // Try to locate a template in the stack | |
| 445 | + $template = bbp_locate_template( $templates ); | |
| 446 | + | |
| 447 | + // Stash the located template for this query, for later use | |
| 448 | + bbp_set_theme_compat_template( $template ); | |
| 449 | + | |
| 450 | + // Filter & return | |
| 451 | + return apply_filters( "bbp_{$type}_template", $template, $templates ); | |
| 205 | 452 | } |
| 206 | 453 | |
| 207 | 454 | /** |
| 208 | 455 | * Get the possible subdirectories to check for templates in |
| 209 | 456 | * |
| 210 | - * @since bbPress (r3738) | |
| 457 | + * @since 2.1.0 bbPress (r3738) | |
| 458 | + * | |
| 211 | 459 | * @param array $templates Templates we are looking for |
| 212 | - * @return array Possible subfolders to look in | |
| 460 | + * @return array Possible subdirectories to look in | |
| 213 | 461 | */ |
| 214 | 462 | function bbp_get_template_locations( $templates = array() ) { |
| 215 | 463 | $locations = array( |
| 216 | 464 | 'bbpress', |
| @@ -216,8 +464,10 @@ | ||
| 216 | 464 | 'bbpress', |
| 217 | 465 | 'forums', |
| 218 | 466 | '' |
| 219 | 467 | ); |
| 468 | + | |
| 469 | + // Filter & return | |
| 220 | 470 | return apply_filters( 'bbp_get_template_locations', $locations, $templates ); |
| 221 | 471 | } |
| 222 | 472 | |
| 223 | 473 | /** |
| @@ -222,25 +472,28 @@ | ||
| 222 | 472 | |
| 223 | 473 | /** |
| 224 | 474 | * Add template locations to template files being searched for |
| 225 | 475 | * |
| 226 | - * @since bbPress (r3738) | |
| 476 | + * @since 2.1.0 bbPress (r3738) | |
| 227 | 477 | * |
| 228 | - * @param array $templates | |
| 229 | - * @return array() | |
| 478 | + * @param array $stacks | |
| 479 | + * @return array() | |
| 230 | 480 | */ |
| 231 | -function bbp_add_template_locations( $templates = array() ) { | |
| 481 | +function bbp_add_template_stack_locations( $stacks = array() ) { | |
| 232 | 482 | $retval = array(); |
| 233 | 483 | |
| 234 | 484 | // Get alternate locations |
| 235 | - $locations = bbp_get_template_locations( $templates ); | |
| 485 | + $locations = bbp_get_template_locations(); | |
| 236 | 486 | |
| 237 | - // Loop through locations and templates and combine | |
| 238 | - foreach ( (array) $locations as $location ) | |
| 239 | - foreach ( (array) $templates as $template ) | |
| 240 | - $retval[] = ltrim( trailingslashit( $location ) . $template, '/' ); | |
| 487 | + // Loop through locations and stacks and combine | |
| 488 | + foreach ( (array) $stacks as $stack ) { | |
| 489 | + foreach ( (array) $locations as $custom_location ) { | |
| 490 | + $retval[] = untrailingslashit( trailingslashit( $stack ) . $custom_location ); | |
| 491 | + } | |
| 492 | + } | |
| 241 | 493 | |
| 242 | - return apply_filters( 'bbp_add_template_locations', array_unique( $retval ), $templates ); | |
| 494 | + // Filter & return | |
| 495 | + return (array) apply_filters( 'bbp_add_template_stack_locations', array_unique( $retval ), $stacks ); | |
| 243 | 496 | } |
| 244 | 497 | |
| 245 | 498 | /** |
| 246 | 499 | * Add checks for bbPress conditions to parse_query action |
| @@ -245,113 +498,103 @@ | ||
| 245 | 498 | /** |
| 246 | 499 | * Add checks for bbPress conditions to parse_query action |
| 247 | 500 | * |
| 248 | 501 | * If it's a user page, WP_Query::bbp_is_single_user is set to true. |
| 502 | + * | |
| 249 | 503 | * If it's a user edit page, WP_Query::bbp_is_single_user_edit is set to true |
| 250 | 504 | * and the the 'wp-admin/includes/user.php' file is included. |
| 505 | + * | |
| 251 | 506 | * In addition, on user/user edit pages, WP_Query::home is set to false & query |
| 252 | - * vars 'bbp_user_id' with the displayed user id and 'author_name' with the | |
| 253 | - * displayed user's nicename are added. | |
| 507 | + * vars 'bbp_user_id' with the displayed user id is added. | |
| 254 | 508 | * |
| 509 | + * In 2.6.0, the 'author_name' variable is no longer set when viewing a single | |
| 510 | + * user, because of is_author() weirdness. If this removal causes problems, it | |
| 511 | + * may come back in a future release. | |
| 512 | + * | |
| 255 | 513 | * If it's a forum edit, WP_Query::bbp_is_forum_edit is set to true |
| 256 | 514 | * If it's a topic edit, WP_Query::bbp_is_topic_edit is set to true |
| 257 | 515 | * If it's a reply edit, WP_Query::bbp_is_reply_edit is set to true. |
| 258 | 516 | * |
| 259 | 517 | * If it's a view page, WP_Query::bbp_is_view is set to true |
| 518 | + * If it's a search page, WP_Query::bbp_is_search is set to true | |
| 260 | 519 | * |
| 261 | - * @since bbPress (r2688) | |
| 520 | + * @since 2.0.0 bbPress (r2688) | |
| 262 | 521 | * |
| 263 | 522 | * @param WP_Query $posts_query |
| 264 | - * | |
| 265 | - * @uses get_query_var() To get {@link WP_Query} query var | |
| 266 | - * @uses is_email() To check if the string is an email | |
| 267 | - * @uses get_user_by() To try to get the user by email and nicename | |
| 268 | - * @uses get_userdata() to get the user data | |
| 269 | - * @uses current_user_can() To check if the current user can edit the user | |
| 270 | - * @uses is_user_member_of_blog() To check if user profile page exists | |
| 271 | - * @uses WP_Query::set_404() To set a 404 status | |
| 272 | - * @uses apply_filters() Calls 'enable_edit_any_user_configuration' with true | |
| 273 | - * @uses bbp_get_view_query_args() To get the view query args | |
| 274 | - * @uses bbp_get_forum_post_type() To get the forum post type | |
| 275 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 276 | - * @uses bbp_get_reply_post_type() To get the reply post type | |
| 277 | - * @uses remove_action() To remove the auto save post revision action | |
| 278 | 523 | */ |
| 279 | 524 | function bbp_parse_query( $posts_query ) { |
| 280 | 525 | |
| 281 | 526 | // Bail if $posts_query is not the main loop |
| 282 | - if ( ! $posts_query->is_main_query() ) | |
| 527 | + if ( ! $posts_query->is_main_query() ) { | |
| 283 | 528 | return; |
| 529 | + } | |
| 284 | 530 | |
| 285 | 531 | // Bail if filters are suppressed on this query |
| 286 | - if ( true == $posts_query->get( 'suppress_filters' ) ) | |
| 532 | + if ( true === $posts_query->get( 'suppress_filters' ) ) { | |
| 287 | 533 | return; |
| 534 | + } | |
| 288 | 535 | |
| 289 | 536 | // Bail if in admin |
| 290 | - if ( is_admin() ) | |
| 537 | + if ( is_admin() ) { | |
| 291 | 538 | return; |
| 539 | + } | |
| 292 | 540 | |
| 293 | - // Get query variables | |
| 294 | - $bbp_view = $posts_query->get( bbp_get_view_rewrite_id() ); | |
| 295 | - $bbp_user = $posts_query->get( bbp_get_user_rewrite_id() ); | |
| 296 | - $is_edit = $posts_query->get( bbp_get_edit_rewrite_id() ); | |
| 541 | + // Get query variables (default to null if not set) | |
| 542 | + $bbp_view = $posts_query->get( bbp_get_view_rewrite_id(), null ); | |
| 543 | + $bbp_user = $posts_query->get( bbp_get_user_rewrite_id(), null ); | |
| 544 | + $is_edit = $posts_query->get( bbp_get_edit_rewrite_id(), null ); | |
| 545 | + $is_search = $posts_query->get( bbp_get_search_rewrite_id(), null ); | |
| 297 | 546 | |
| 298 | 547 | // It is a user page - We'll also check if it is user edit |
| 299 | - if ( !empty( $bbp_user ) ) { | |
| 548 | + if ( ! is_null( $bbp_user ) ) { | |
| 300 | 549 | |
| 301 | - // Not a user_id so try email and slug | |
| 302 | - if ( !is_numeric( $bbp_user ) ) { | |
| 550 | + /** Find User *********************************************************/ | |
| 303 | 551 | |
| 304 | - // Email was passed | |
| 305 | - if ( is_email( $bbp_user ) ) { | |
| 306 | - $bbp_user = get_user_by( 'email', $bbp_user ); | |
| 552 | + // Setup the default user variable | |
| 553 | + $the_user = false; | |
| 307 | 554 | |
| 308 | - // Try nicename | |
| 309 | - } else { | |
| 310 | - $bbp_user = get_user_by( 'slug', $bbp_user ); | |
| 311 | - } | |
| 555 | + // If using pretty permalinks, always use slug | |
| 556 | + if ( get_option( 'permalink_structure' ) ) { | |
| 557 | + $the_user = get_user_by( 'slug', $bbp_user ); | |
| 312 | 558 | |
| 313 | - // If we were successful, set to ID | |
| 314 | - if ( is_object( $bbp_user ) ) { | |
| 315 | - $bbp_user = $bbp_user->ID; | |
| 316 | - } | |
| 559 | + // If not using pretty permalinks, always use numeric ID | |
| 560 | + } elseif ( is_numeric( $bbp_user ) ) { | |
| 561 | + $the_user = get_user_by( 'id', $bbp_user ); | |
| 317 | 562 | } |
| 318 | 563 | |
| 319 | - // Cast as int, just in case | |
| 320 | - $bbp_user = (int) $bbp_user; | |
| 321 | - | |
| 322 | 564 | // 404 and bail if user does not have a profile |
| 323 | - if ( ! bbp_user_has_profile( $bbp_user ) ) { | |
| 324 | - $posts_query->set_404(); | |
| 565 | + if ( empty( $the_user->ID ) || ! bbp_user_has_profile( $the_user->ID ) ) { | |
| 566 | + $posts_query->bbp_is_404 = true; | |
| 325 | 567 | return; |
| 326 | 568 | } |
| 327 | 569 | |
| 328 | 570 | /** User Exists *******************************************************/ |
| 329 | 571 | |
| 330 | - $is_favs = $posts_query->get( bbp_get_user_favorites_rewrite_id() ); | |
| 331 | - $is_subs = $posts_query->get( bbp_get_user_subscriptions_rewrite_id() ); | |
| 332 | - $is_topics = $posts_query->get( bbp_get_user_topics_rewrite_id() ); | |
| 333 | - $is_replies = $posts_query->get( bbp_get_user_replies_rewrite_id() ); | |
| 572 | + $is_favs = $posts_query->get( bbp_get_user_favorites_rewrite_id() ); | |
| 573 | + $is_subs = $posts_query->get( bbp_get_user_subscriptions_rewrite_id() ); | |
| 574 | + $is_topics = $posts_query->get( bbp_get_user_topics_rewrite_id() ); | |
| 575 | + $is_replies = $posts_query->get( bbp_get_user_replies_rewrite_id() ); | |
| 576 | + $is_engagements = $posts_query->get( bbp_get_user_engagements_rewrite_id() ); | |
| 334 | 577 | |
| 335 | 578 | // View or edit? |
| 336 | - if ( !empty( $is_edit ) ) { | |
| 579 | + if ( ! is_null( $is_edit ) ) { | |
| 337 | 580 | |
| 338 | 581 | // We are editing a profile |
| 339 | 582 | $posts_query->bbp_is_single_user_edit = true; |
| 340 | 583 | |
| 341 | 584 | // Load the core WordPress contact methods |
| 342 | - if ( !function_exists( '_wp_get_user_contactmethods' ) ) { | |
| 343 | - include_once( ABSPATH . 'wp-includes/registration.php' ); | |
| 585 | + if ( ! function_exists( '_wp_get_user_contactmethods' ) ) { | |
| 586 | + require_once ABSPATH . 'wp-includes/registration.php'; | |
| 344 | 587 | } |
| 345 | 588 | |
| 346 | 589 | // Load the edit_user functions |
| 347 | - if ( !function_exists( 'edit_user' ) ) { | |
| 348 | - require_once( ABSPATH . 'wp-admin/includes/user.php' ); | |
| 590 | + if ( ! function_exists( 'edit_user' ) ) { | |
| 591 | + require_once ABSPATH . 'wp-admin/includes/user.php'; | |
| 349 | 592 | } |
| 350 | 593 | |
| 351 | 594 | // Load the grant/revoke super admin functions |
| 352 | - if ( is_multisite() && !function_exists( 'revoke_super_admin' ) ) { | |
| 353 | - require_once( ABSPATH . 'wp-admin/includes/ms.php' ); | |
| 595 | + if ( is_multisite() && ! function_exists( 'revoke_super_admin' ) ) { | |
| 596 | + require_once ABSPATH . 'wp-admin/includes/ms.php'; | |
| 354 | 597 | } |
| 355 | 598 | |
| 356 | 599 | // Editing a user |
| 357 | 600 | $posts_query->bbp_is_edit = true; |
| @@ -371,16 +614,17 @@ | ||
| 371 | 614 | // User topics |
| 372 | 615 | } elseif ( ! empty( $is_replies ) ) { |
| 373 | 616 | $posts_query->bbp_is_single_user_replies = true; |
| 374 | 617 | |
| 618 | + // User engagements | |
| 619 | + } elseif ( ! empty( $is_engagements ) ) { | |
| 620 | + $posts_query->bbp_is_single_user_engagements = true; | |
| 621 | + | |
| 375 | 622 | // User profile |
| 376 | 623 | } else { |
| 377 | 624 | $posts_query->bbp_is_single_user_profile = true; |
| 378 | 625 | } |
| 379 | 626 | |
| 380 | - // Looking at a single user | |
| 381 | - $posts_query->bbp_is_single_user = true; | |
| 382 | - | |
| 383 | 627 | // Make sure 404 is not set |
| 384 | 628 | $posts_query->is_404 = false; |
| 385 | 629 | |
| 386 | 630 | // Correct is_home variable |
| @@ -385,34 +629,34 @@ | ||
| 385 | 629 | |
| 386 | 630 | // Correct is_home variable |
| 387 | 631 | $posts_query->is_home = false; |
| 388 | 632 | |
| 389 | - // Get the user data | |
| 390 | - $user = get_userdata( $bbp_user ); | |
| 633 | + // Looking at a single user | |
| 634 | + $posts_query->bbp_is_single_user = true; | |
| 391 | 635 | |
| 636 | + // User found so don't 404 yet | |
| 637 | + $posts_query->bbp_is_404 = false; | |
| 638 | + | |
| 392 | 639 | // User is looking at their own profile |
| 393 | - if ( get_current_user_id() == $user->ID ) { | |
| 640 | + if ( bbp_get_current_user_id() === $the_user->ID ) { | |
| 394 | 641 | $posts_query->bbp_is_single_user_home = true; |
| 395 | 642 | } |
| 396 | 643 | |
| 397 | 644 | // Set bbp_user_id for future reference |
| 398 | - $posts_query->set( 'bbp_user_id', $user->ID ); | |
| 645 | + $posts_query->set( 'bbp_user_id', $the_user->ID ); | |
| 399 | 646 | |
| 400 | - // Set author_name as current user's nicename to get correct posts | |
| 401 | - $posts_query->set( 'author_name', $user->user_nicename ); | |
| 402 | - | |
| 403 | 647 | // Set the displayed user global to this user |
| 404 | - bbpress()->displayed_user = $user; | |
| 648 | + bbpress()->displayed_user = $the_user; | |
| 405 | 649 | |
| 406 | 650 | // View Page |
| 407 | - } elseif ( !empty( $bbp_view ) ) { | |
| 651 | + } elseif ( ! is_null( $bbp_view ) ) { | |
| 408 | 652 | |
| 409 | 653 | // Check if the view exists by checking if there are query args are set |
| 410 | 654 | $view_args = bbp_get_view_query_args( $bbp_view ); |
| 411 | 655 | |
| 412 | - // Bail if view args is false (view isn't registered) | |
| 413 | - if ( false === $view_args ) { | |
| 414 | - $posts_query->set_404(); | |
| 656 | + // Bail if view args are empty | |
| 657 | + if ( empty( $view_args ) ) { | |
| 658 | + $posts_query->bbp_is_404 = true; | |
| 415 | 659 | return; |
| 416 | 660 | } |
| 417 | 661 | |
| 418 | 662 | // Correct is_home variable |
| @@ -420,34 +664,58 @@ | ||
| 420 | 664 | |
| 421 | 665 | // We are in a custom topic view |
| 422 | 666 | $posts_query->bbp_is_view = true; |
| 423 | 667 | |
| 668 | + // No 404 because views are all (currently) public | |
| 669 | + $posts_query->bbp_is_404 = false; | |
| 670 | + | |
| 671 | + // Search Page | |
| 672 | + } elseif ( ! is_null( $is_search ) ) { | |
| 673 | + | |
| 674 | + // Check if there are search query args set | |
| 675 | + $search_terms = bbp_get_search_terms(); | |
| 676 | + if ( ! empty( $search_terms ) ) { | |
| 677 | + $posts_query->bbp_search_terms = $search_terms; | |
| 678 | + } | |
| 679 | + | |
| 680 | + // Correct is_home variable | |
| 681 | + $posts_query->is_home = false; | |
| 682 | + | |
| 683 | + // We are in a search query | |
| 684 | + $posts_query->bbp_is_search = true; | |
| 685 | + | |
| 686 | + // No 404 because search is always public | |
| 687 | + $posts_query->bbp_is_404 = false; | |
| 688 | + | |
| 424 | 689 | // Forum/Topic/Reply Edit Page |
| 425 | - } elseif ( !empty( $is_edit ) ) { | |
| 690 | + } elseif ( ! is_null( $is_edit ) ) { | |
| 426 | 691 | |
| 427 | 692 | // Get the post type from the main query loop |
| 428 | 693 | $post_type = $posts_query->get( 'post_type' ); |
| 429 | - | |
| 694 | + | |
| 430 | 695 | // Check which post_type we are editing, if any |
| 431 | - if ( !empty( $post_type ) ) { | |
| 432 | - switch( $post_type ) { | |
| 696 | + if ( ! empty( $post_type ) ) { | |
| 697 | + switch ( $post_type ) { | |
| 433 | 698 | |
| 434 | 699 | // We are editing a forum |
| 435 | 700 | case bbp_get_forum_post_type() : |
| 436 | - $posts_query->bbp_is_forum_edit = true; | |
| 437 | - $posts_query->bbp_is_edit = true; | |
| 701 | + $posts_query->bbp_is_forum_edit = true; | |
| 702 | + $posts_query->bbp_is_edit = true; | |
| 703 | + $posts_query->bbp_is_404 = false; | |
| 438 | 704 | break; |
| 439 | 705 | |
| 440 | 706 | // We are editing a topic |
| 441 | 707 | case bbp_get_topic_post_type() : |
| 442 | - $posts_query->bbp_is_topic_edit = true; | |
| 443 | - $posts_query->bbp_is_edit = true; | |
| 708 | + $posts_query->bbp_is_topic_edit = true; | |
| 709 | + $posts_query->bbp_is_edit = true; | |
| 710 | + $posts_query->bbp_is_404 = false; | |
| 444 | 711 | break; |
| 445 | 712 | |
| 446 | 713 | // We are editing a reply |
| 447 | 714 | case bbp_get_reply_post_type() : |
| 448 | - $posts_query->bbp_is_reply_edit = true; | |
| 449 | - $posts_query->bbp_is_edit = true; | |
| 715 | + $posts_query->bbp_is_reply_edit = true; | |
| 716 | + $posts_query->bbp_is_edit = true; | |
| 717 | + $posts_query->bbp_is_404 = false; | |
| 450 | 718 | break; |
| 451 | 719 | } |
| 452 | 720 | |
| 453 | 721 | // We are editing a topic tag |
| @@ -453,8 +721,9 @@ | ||
| 453 | 721 | // We are editing a topic tag |
| 454 | 722 | } elseif ( bbp_is_topic_tag() ) { |
| 455 | 723 | $posts_query->bbp_is_topic_tag_edit = true; |
| 456 | 724 | $posts_query->bbp_is_edit = true; |
| 725 | + $posts_query->bbp_is_404 = false; | |
| 457 | 726 | } |
| 458 | 727 | |
| 459 | 728 | // We save post revisions on our own |
| 460 | 729 | remove_action( 'pre_post_update', 'wp_save_post_revision' ); |
| @@ -463,6 +732,10 @@ | ||
| 463 | 732 | } elseif ( bbp_is_topic_tag() ) { |
| 464 | 733 | $posts_query->set( 'bbp_topic_tag', get_query_var( 'term' ) ); |
| 465 | 734 | $posts_query->set( 'post_type', bbp_get_topic_post_type() ); |
| 466 | 735 | $posts_query->set( 'posts_per_page', bbp_get_topics_per_page() ); |
| 736 | + | |
| 737 | + // Do topics on forums root | |
| 738 | + } elseif ( is_post_type_archive( bbp_get_post_types( array( 'has_archive' => true ) ) ) && ( 'topics' === bbp_show_on_root() ) ) { | |
| 739 | + $posts_query->bbp_show_topics_on_root = true; | |
| 467 | 740 | } |
| 468 | 741 | } |