Skip to:
Content

bbPress.org

Changeset 5181


Ignore:
Timestamp:
11/25/2013 02:13:51 AM (7 years ago)
Author:
johnjamesjacoby
Message:

Introduce functions to enqueue scripts and styles according to the template stack. See #2478.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/core/template-functions.php

    r5043 r5181  
    5151 * not found in either of those, it looks in the theme-compat folder last.
    5252 *
    53  * @since bbPres (r3618)
     53 * @since bbPress (r3618)
    5454 *
    5555 * @param string|array $template_names Template file(s) to search for, in order.
     
    110110
    111111/**
     112 * Enqueue a script from the highest priority location in the template stack.
     113 *
     114 * Registers the style if file provided (does NOT overwrite) and enqueues.
     115 *
     116 * @since bbPress (r5180)
     117 *
     118 * @param string      $handle Name of the stylesheet.
     119 * @param string|bool $file   Relative path to stylesheet. Example: '/css/mystyle.css'.
     120 * @param array       $deps   An array of registered style handles this stylesheet depends on. Default empty array.
     121 * @param string|bool $ver    String specifying the stylesheet version number, if it has one. This parameter is used
     122 *                            to ensure that the correct version is sent to the client regardless of caching, and so
     123 *                            should be included if a version number is available and makes sense for the stylesheet.
     124 * @param string      $media  Optional. The media for which this stylesheet has been defined.
     125 *                            Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print',
     126 *                            'screen', 'tty', or 'tv'.
     127 *
     128 * @return string The style filename if one is located.
     129 */
     130function bbp_enqueue_style( $handle = '', $file = '', $dependencies = array(), $version = false, $media = 'all' ) {
     131
     132    // Trim off any slashes from the template name
     133    $file = ltrim( $file, '/' );
     134
     135    // Make sure there is always a version
     136    if ( empty( $version ) ) {
     137        $version = bbp_get_version();
     138    }
     139
     140    // Loop through template stack
     141    foreach ( (array) bbp_get_template_stack() as $template_location ) {
     142
     143        // Continue if $template_location is empty
     144        if ( empty( $template_location ) ) {
     145            continue;
     146        }
     147
     148        // Check child theme first
     149        if ( file_exists( trailingslashit( $template_location ) . $file ) ) {
     150            $located = trailingslashit( $template_location ) . $file;
     151            break;
     152        }
     153    }
     154
     155    // Make path to file relative to site URL
     156    $located = trailingslashit( site_url() ) . str_replace( ABSPATH, '', $located );
     157
     158    // Enqueue the style
     159    wp_enqueue_style( $handle, $located, $dependencies, $version, $media );
     160
     161    return $located;
     162}
     163
     164/**
     165 * Enqueue a script from the highest priority location in the template stack.
     166 *
     167 * Registers the style if file provided (does NOT overwrite) and enqueues.
     168 *
     169 * @since bbPress (r5180)
     170 *
     171 * @param string      $handle    Name of the script.
     172 * @param string|bool $file      Relative path to the script. Example: '/js/myscript.js'.
     173 * @param array       $deps      An array of registered handles this script depends on. Default empty array.
     174 * @param string|bool $ver       Optional. String specifying the script version number, if it has one. This parameter
     175 *                               is used to ensure that the correct version is sent to the client regardless of caching,
     176 *                               and so should be included if a version number is available and makes sense for the script.
     177 * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
     178 *                               Default 'false'. Accepts 'false' or 'true'.
     179 *
     180 * @return string The script filename if one is located.
     181 */
     182function bbp_enqueue_script( $handle = '', $file = '', $dependencies = array(), $version = false, $in_footer = 'all' ) {
     183
     184    // Trim off any slashes from the template name
     185    $file = ltrim( $file, '/' );
     186
     187    // Make sure there is always a version
     188    if ( empty( $version ) ) {
     189        $version = bbp_get_version();
     190    }
     191
     192    // Loop through template stack
     193    foreach ( (array) bbp_get_template_stack() as $template_location ) {
     194
     195        // Continue if $template_location is empty
     196        if ( empty( $template_location ) ) {
     197            continue;
     198        }
     199
     200        // Check child theme first
     201        if ( file_exists( trailingslashit( $template_location ) . $file ) ) {
     202            $located = trailingslashit( $template_location ) . $file;
     203            break;
     204        }
     205    }
     206
     207    // Make path to file relative to site URL
     208    $located = trailingslashit( site_url() ) . str_replace( ABSPATH, '', $located );
     209
     210    // Enqueue the style
     211    wp_enqueue_script( $handle, $located, $dependencies, $version, $in_footer );
     212
     213    return $located;
     214}
     215
     216/**
    112217 * This is really cool. This function registers a new template stack location,
    113218 * using WordPress's built in filters API.
     
    119224 * @since bbPress (r4323)
    120225 *
    121  * @param string $location Callback function that returns the 
     226 * @param string $location Callback function that returns the
    122227 * @param int $priority
    123228 */
     
    283388 *
    284389 * @param array $templates
    285  * @return array() 
     390 * @return array()
    286391 */
    287392function bbp_add_template_stack_locations( $stacks = array() ) {
     
    497602        // Get the post type from the main query loop
    498603        $post_type = $posts_query->get( 'post_type' );
    499        
     604
    500605        // Check which post_type we are editing, if any
    501606        if ( !empty( $post_type ) ) {
Note: See TracChangeset for help on using the changeset viewer.