Skip to:
Content

bbPress.org


Ignore:
Timestamp:
09/05/2018 06:55:06 PM (10 months ago)
Author:
johnjamesjacoby
Message:

Theme Compat: introduce bbp_locate_enqueueable() and bbp_urlize_enqueueable().

These functions are used to help make locating enqueueable assets easier, and use bbp_locate_template() interntally, now accepting an array of files.

In addition, bbp_locate_enqueueable() also internally juggles minimized file variations, and stacks them according to the SCRIPT_DEBUG constant. This ensures that both minimized and unminimized file variants are in the array in the preferred order.

This fixes a regression between bbPress 2.5 and 2.6 caused by the bundling of minimized assets in theme compatibility, and ensures that sites with their own bbpress.css files in their own locations will continue to get loaded, regardless of the SCRIPT_DEBUG setting.

Fixes #3218.

File:
1 edited

Legend:

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

    r6795 r6862  
    105105
    106106    return $located;
     107}
     108
     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 */
     126function 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 = defined( 'SCRIPT_DEBUG' ) && 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 */
     174function 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 );
    107189}
    108190
     
    124206 *                            'screen', 'tty', or 'tv'.
    125207 *
    126  * @return string The style filename if one is located.
     208 * @return mixed The style filename if one is located. False if not.
    127209 */
    128210function bbp_enqueue_style( $handle = '', $file = '', $deps = array(), $ver = false, $media = 'all' ) {
    129211
    130     // No file found yet
    131     $located = false;
    132 
    133     // Trim off any slashes from the template name
    134     $file = ltrim( $file, '/' );
    135 
    136     // Make sure there is always a version
    137     if ( empty( $ver ) ) {
    138         $ver = bbp_get_version();
    139     }
    140 
    141     // Loop through template stack
    142     foreach ( (array) bbp_get_template_stack() as $template_location ) {
    143 
    144         // Continue if $template_location is empty
    145         if ( empty( $template_location ) ) {
    146             continue;
    147         }
    148 
    149         // Check child theme first
    150         if ( file_exists( trailingslashit( $template_location ) . $file ) ) {
    151             $located = trailingslashit( $template_location ) . $file;
    152             break;
    153         }
    154     }
     212    // Attempt to locate an enqueueable
     213    $located = bbp_locate_enqueueable( $file );
    155214
    156215    // Enqueue if located
    157216    if ( ! empty( $located ) ) {
    158217
    159         $content_dir = constant( 'WP_CONTENT_DIR' );
    160 
    161         // IIS (Windows) here
    162         // Replace back slashes with forward slash
    163         if ( strpos( $located, '\\' ) !== false ) {
    164             $located     = str_replace( '\\', '/', $located     );
    165             $content_dir = str_replace( '\\', '/', $content_dir );
    166         }
    167 
    168         // Make path to file relative to site URL
    169         $located = str_replace( $content_dir, content_url(), $located );
     218        // Make sure there is always a version
     219        if ( empty( $ver ) ) {
     220            $ver = bbp_get_version();
     221        }
     222
     223        // Make path to file relative to site URL
     224        $located = bbp_urlize_enqueueable( $located );
    170225
    171226        // Register the style
     
    195250 *                               Default 'false'. Accepts 'false' or 'true'.
    196251 *
    197  * @return string The script filename if one is located.
     252 * @return mixed The script filename if one is located. False if not.
    198253 */
    199254function bbp_enqueue_script( $handle = '', $file = '', $deps = array(), $ver = false, $in_footer = false ) {
    200255
    201     // No file found yet
    202     $located = false;
    203 
    204     // Trim off any slashes from the template name
    205     $file = ltrim( $file, '/' );
    206 
    207     // Make sure there is always a version
    208     if ( empty( $ver ) ) {
    209         $ver = bbp_get_version();
    210     }
    211 
    212     // Loop through template stack
    213     foreach ( (array) bbp_get_template_stack() as $template_location ) {
    214 
    215         // Continue if $template_location is empty
    216         if ( empty( $template_location ) ) {
    217             continue;
    218         }
    219 
    220         // Check child theme first
    221         if ( file_exists( trailingslashit( $template_location ) . $file ) ) {
    222             $located = trailingslashit( $template_location ) . $file;
    223             break;
    224         }
    225     }
     256    // Attempt to locate an enqueueable
     257    $located = bbp_locate_enqueueable( $file );
    226258
    227259    // Enqueue if located
    228260    if ( ! empty( $located ) ) {
    229261
    230         $content_dir = constant( 'WP_CONTENT_DIR' );
    231 
    232         // IIS (Windows) here
    233         // Replace back slashes with forward slash
    234         if ( strpos( $located, '\\' ) !== false ) {
    235             $located     = str_replace( '\\', '/', $located     );
    236             $content_dir = str_replace( '\\', '/', $content_dir );
    237         }
    238 
    239         // Make path to file relative to site URL
    240         $located = str_replace( $content_dir, content_url(), $located );
     262        // Make sure there is always a version
     263        if ( empty( $ver ) ) {
     264            $ver = bbp_get_version();
     265        }
     266
     267        // Make path to file relative to site URL
     268        $located = bbp_urlize_enqueueable( $located );
    241269
    242270        // Register the style
Note: See TracChangeset for help on using the changeset viewer.