Skip to:
Content

BuddyPress.org

Changeset 10862


Ignore:
Timestamp:
06/01/2016 10:30:19 PM (4 years ago)
Author:
r-a-y
Message:

Core: Introduce function, bp_locate_template_asset().

This function is similar to bp_locate_template(), but is meant to be used
for fetching assets such as CSS and JS files.

This will probably replace BP_Legacy::locate_asset_in_stack() in a future
release.

Props r-a-y, imath.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-template-loader.php

    r10835 r10862  
    148148
    149149    return $located;
     150}
     151
     152/**
     153 * Get file data of the highest priority asset that exists.
     154 *
     155 * Similar to {@link bp_locate_template()}, but for files like CSS and JS.
     156 *
     157 * @since 2.6.0
     158 *
     159 * @param  string     Relative filename to search for.
     160 * @return array|bool Array of asset data if one is located (includes absolute filepath and URI).
     161 *                    Boolean false on failure.
     162 */
     163function bp_locate_template_asset( $filename ) {
     164    // Ensure assets can be located when running from /src/.
     165    if ( defined( 'BP_SOURCE_SUBDIRECTORY' ) && 'src' === BP_SOURCE_SUBDIRECTORY ) {
     166        $filename = str_replace( '.min', '', $filename );
     167    }
     168
     169    // Use bp_locate_template() to find our asset.
     170    $located = bp_locate_template( $filename, false );
     171    if ( false === $located ) {
     172        return false;
     173    }
     174
     175    // Set up data array.
     176    $data = array();
     177    $data['file'] = $data['uri'] = $located;
     178
     179    $find = array(
     180        get_theme_root(),
     181        buddypress()->plugin_dir
     182    );
     183
     184    $replace = array(
     185        get_theme_root_uri(),
     186        buddypress()->plugin_url
     187    );
     188
     189    // Make sure URI path is relative to site URL.
     190    $data['uri'] = str_replace( $find, $replace, $data['uri'] );
     191
     192    return $data;
    150193}
    151194
Note: See TracChangeset for help on using the changeset viewer.