Skip to:
Content

BuddyPress.org

Changeset 7205


Ignore:
Timestamp:
06/12/2013 09:54:52 PM (8 years ago)
Author:
boonebgorges
Message:

Allow JS and CSS assets to be stored in folder stacks, akin to template stacks

Since BP 1.7 and theme compatibility, it's been possible to override BP's
fallback templates by providing templates in a number of different
subdirectories of your theme: 'buddypress' and 'community', by default. This
functionality was not, however, extended to static theme assets like JS and
CSS files, meaning that theme authors were forced to have orphan 'css' and 'js'
files in their theme roots if they wanted to override bp-legacy's assets.

This changeset mirrors the template_stack() functionality for assets, by
looking inside of 'buddypress' and 'community' theme subdirectories before
looking in root 'css' and 'js' directories (which still work, for backpat).

The implementation adopted in this changset is probably temporary for 1.8.
Future iterations may use an abstracted version of the core template_stack
functionality.

Props hnla for early patches and feedback.

Fixes #4949

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-templates/bp-legacy/buddypress-functions.php

    r7193 r7205  
    196196
    197197        // LTR or RTL
    198         $file = is_rtl() ? 'css/buddypress-rtl.css' : 'css/buddypress.css';
    199 
    200         // Check child theme
    201         if ( file_exists( trailingslashit( get_stylesheet_directory() ) . $file ) ) {
    202             $location = trailingslashit( get_stylesheet_directory_uri() );
    203             $handle   = 'bp-child-css';
    204 
    205         // Check parent theme
    206         } elseif ( file_exists( trailingslashit( get_template_directory() ) . $file ) ) {
    207             $location = trailingslashit( get_template_directory_uri() );
    208             $handle   = 'bp-parent-css';
    209 
    210         // BuddyPress Theme Compatibility
    211         } else {
    212             $location = trailingslashit( $this->url );
    213             $handle   = 'bp-legacy-css';
    214         }
    215 
    216         // Enqueue the BuddyPress styling
    217         wp_enqueue_style( $handle, $location . $file, array(), $this->version, 'screen' );
     198        $file = is_rtl() ? 'buddypress-rtl.css' : 'buddypress.css';
     199
     200        // Locate the BP stylesheet
     201        $asset = $this->locate_asset_in_stack( $file, 'css' );
     202
     203        // Enqueue BuddyPress-specific styling, if found
     204        if ( isset( $asset['location'], $asset['handle'] ) ) {
     205            wp_enqueue_style( $asset['handle'], $asset['location'], array(), $this->version, 'screen' );
     206        }
    218207    }
    219208
     
    225214    public function enqueue_scripts() {
    226215
    227         // LTR or RTL
    228         $file = 'js/buddypress.js';
    229 
    230         // Check child theme
    231         if ( file_exists( trailingslashit( get_stylesheet_directory() ) . $file ) ) {
    232             $location = trailingslashit( get_stylesheet_directory_uri() );
    233             $handle   = 'bp-child-js';
    234 
    235         // Check parent theme
    236         } elseif ( file_exists( trailingslashit( get_template_directory() ) . $file ) ) {
    237             $location = trailingslashit( get_template_directory_uri() );
    238             $handle   = 'bp-parent-js';
    239 
    240         // BuddyPress Theme Compatibility
    241         } else {
    242             $location = trailingslashit( $this->url );
    243             $handle   = 'bp-legacy-js';
    244         }
    245 
    246         // Enqueue the global JS - Ajax will not work without it
    247         wp_enqueue_script( $handle, $location . $file, array( 'jquery' ), $this->version );
     216        $file = 'buddypress.js';
     217
     218        // Locate the BP JS file
     219        $asset = $this->locate_asset_in_stack( $file, 'js' );
     220
     221        // Enqueue the global JS, if found - AJAX will not work
     222        // without it
     223        if ( isset( $asset['location'], $asset['handle'] ) ) {
     224            wp_enqueue_script( $asset['handle'], $asset['location'], array( 'jquery' ), $this->version );
     225        }
    248226
    249227        // Add words that we need to use in JS to the end of the page so they can be translated and still used.
     
    262240            'unsaved_changes'   => __( 'Your profile has unsaved changes. If you leave the page, the changes will be lost.', 'buddypress' ),
    263241        );
    264         wp_localize_script( $handle, 'BP_DTheme', $params );
     242        wp_localize_script( $asset['handle'], 'BP_DTheme', $params );
    265243
    266244        // Maybe enqueue comment reply JS
     
    268246            wp_enqueue_script( 'comment-reply' );
    269247        }
     248    }
     249
     250    /**
     251     * Get the URL and handle of a web-accessible CSS or JS asset
     252     *
     253     * We provide two levels of customizability with respect to where CSS
     254     * and JS files can be stored: (1) the child theme/parent theme/theme
     255     * compat hierarchy, and (2) the "template stack" of /buddypress/css/,
     256     * /community/css/, and /css/. In this way, CSS and JS assets can be
     257     * overloaded, and default versions provided, in exactly the same way
     258     * as corresponding PHP templates.
     259     *
     260     * We are duplicating some of the logic that is currently found in
     261     * bp_locate_template() and the _template_stack() functions. Those
     262     * functions were built with PHP templates in mind, and will require
     263     * refactoring in order to provide "stack" functionality for assets
     264     * that must be accessible both using file_exists() (the file path)
     265     * and at a public URI.
     266     *
     267     * This method is marked private, with the understanding that the
     268     * implementation is subject to change or removal in an upcoming
     269     * release, in favor of a unified _template_stack() system. Plugin
     270     * and theme authors should not attempt to use what follows.
     271     *
     272     * @since BuddyPress (1.8)
     273     * @access private
     274     * @param string $file A filename like buddypress.cs
     275     * @param string $type css|js
     276     * @return array An array of data for the wp_enqueue_* function:
     277     *   'handle' (eg 'bp-child-css') and a 'location' (the URI of the
     278     *   asset)
     279     */
     280    private function locate_asset_in_stack( $file, $type = 'css' ) {
     281        // Child, parent, theme compat
     282        $locations = array();
     283
     284        // No need to check child if template == stylesheet
     285        if ( is_child_theme() ) {
     286            $locations['bp-child'] = array(
     287                'dir' => get_stylesheet_directory(),
     288                'uri' => get_stylesheet_directory_uri(),
     289            );
     290        }
     291
     292        $locations['bp-parent'] = array(
     293            'dir' => get_template_directory(),
     294            'uri' => get_template_directory_uri(),
     295        );
     296
     297        $locations['bp-legacy'] = array(
     298            'dir' => bp_get_theme_compat_dir(),
     299            'uri' => bp_get_theme_compat_url(),
     300        );
     301
     302        // Subdirectories within the top-level $locations directories
     303        $subdirs = array(
     304            'buddypress/' . $type,
     305            'community/' . $type,
     306            $type,
     307        );
     308
     309        $retval = array();
     310
     311        foreach ( $locations as $location_type => $location ) {
     312            foreach ( $subdirs as $subdir ) {
     313                if ( file_exists( trailingslashit( $location['dir'] ) . trailingslashit( $subdir ) . $file ) ) {
     314                    $retval['location'] = trailingslashit( $location['uri'] ) . trailingslashit( $subdir ) . $file;
     315                    $retval['handle']   = $location_type . '-' . $type;
     316                    break 2;
     317                }
     318            }
     319        }
     320
     321        return $retval;
    270322    }
    271323
Note: See TracChangeset for help on using the changeset viewer.