/** * Get the URL and handle of a web-accessible CSS or JS asset * * We provide two levels of customizability with respect to where CSS * and JS files can be stored: (1) the child theme/parent theme/theme * compat hierarchy, and (2) the "template stack" of /buddypress/css/, * /community/css/, and /css/. In this way, CSS and JS assets can be * overloaded, and default versions provided, in exactly the same way * as corresponding PHP templates. * * We are duplicating some of the logic that is currently found in * bp_locate_template() and the _template_stack() functions. Those * functions were built with PHP templates in mind, and will require * refactoring in order to provide "stack" functionality for assets * that must be accessible both using file_exists() (the file path) * and at a public URI. * * This method is marked private, with the understanding that the * implementation is subject to change or removal in an upcoming * release, in favor of a unified _template_stack() system. Plugin * and theme authors should not attempt to use what follows. * * @since BuddyPress (1.8) * @access private * @param string $file A filename like buddypress.cs * @param string $type css|js * @return array An array of data for the wp_enqueue_* function: * 'handle' (eg 'bp-child-css') and a 'location' (the URI of the * asset) */ private function locate_asset_in_stack($file, $type = 'css') { // Child, parent, theme compat $locations = array(); // No need to check child if template == stylesheet if (is_child_theme()) { $locations['bp-child'] = array('dir' => get_stylesheet_directory(), 'uri' => get_stylesheet_directory_uri()); } $locations['bp-parent'] = array('dir' => get_template_directory(), 'uri' => get_template_directory_uri()); $locations['bp-legacy'] = array('dir' => bp_get_theme_compat_dir(), 'uri' => bp_get_theme_compat_url()); // Subdirectories within the top-level $locations directories $subdirs = array('buddypress/' . $type, 'community/' . $type, $type); $retval = array(); foreach ($locations as $location_type => $location) { foreach ($subdirs as $subdir) { if (file_exists(trailingslashit($location['dir']) . trailingslashit($subdir) . $file)) { $retval['location'] = trailingslashit($location['uri']) . trailingslashit($subdir) . $file; $retval['handle'] = $location_type . '-' . $type; break 2; } } } return $retval; }
/** * Get the URL and handle of a web-accessible CSS or JS asset * * We provide two levels of customizability with respect to where CSS * and JS files can be stored: (1) the child theme/parent theme/theme * compat hierarchy, and (2) the "template stack" of /buddypress/css/, * /community/css/, and /css/. In this way, CSS and JS assets can be * overloaded, and default versions provided, in exactly the same way * as corresponding PHP templates. * * We are duplicating some of the logic that is currently found in * bp_locate_template() and the _template_stack() functions. Those * functions were built with PHP templates in mind, and will require * refactoring in order to provide "stack" functionality for assets * that must be accessible both using file_exists() (the file path) * and at a public URI. * * This method is marked private, with the understanding that the * implementation is subject to change or removal in an upcoming * release, in favor of a unified _template_stack() system. Plugin * and theme authors should not attempt to use what follows. * * @since BuddyPress (1.8) * @access private * @param string $file A filename like buddypress.css * @param string $type Optional. Either "js" or "css" (the default). * @param string $script_handle Optional. If set, used as the script name in `wp_enqueue_script`. * @return array An array of data for the wp_enqueue_* function: * 'handle' (eg 'bp-child-css') and a 'location' (the URI of the * asset) */ private function locate_asset_in_stack($file, $type = 'css', $script_handle = '') { $locations = array(); // Ensure the assets can be located when running from /src/. if (defined('BP_SOURCE_SUBDIRECTORY') && BP_SOURCE_SUBDIRECTORY === 'src') { $file = str_replace('.min', '', $file); } // No need to check child if template == stylesheet if (is_child_theme()) { $locations['bp-child'] = array('dir' => get_stylesheet_directory(), 'uri' => get_stylesheet_directory_uri(), 'file' => str_replace('.min', '', $file)); } $locations['bp-parent'] = array('dir' => get_template_directory(), 'uri' => get_template_directory_uri(), 'file' => str_replace('.min', '', $file)); $locations['bp-legacy'] = array('dir' => bp_get_theme_compat_dir(), 'uri' => bp_get_theme_compat_url(), 'file' => $file); // Subdirectories within the top-level $locations directories $subdirs = array('buddypress/' . $type, 'community/' . $type, $type); $retval = array(); foreach ($locations as $location_type => $location) { foreach ($subdirs as $subdir) { if (file_exists(trailingslashit($location['dir']) . trailingslashit($subdir) . $location['file'])) { $retval['location'] = trailingslashit($location['uri']) . trailingslashit($subdir) . $location['file']; $retval['handle'] = $script_handle ? $script_handle : "{$location_type}-{$type}"; break 2; } } } return $retval; }
/** * Get file data of the highest priority asset that exists. * * Similar to {@link bp_locate_template()}, but for files like CSS and JS. * * @since 2.6.0 * * @param string $filename Relative filename to search for. * @return array|bool Array of asset data if one is located (includes absolute filepath and URI). * Boolean false on failure. */ function bp_locate_template_asset($filename) { // Ensure assets can be located when running from /src/. if (defined('BP_SOURCE_SUBDIRECTORY') && 'src' === BP_SOURCE_SUBDIRECTORY) { $filename = str_replace('.min', '', $filename); } // Use bp_locate_template() to find our asset. $located = bp_locate_template($filename, false); if (false === $located) { return false; } // Set up data array. $data = array(); $data['file'] = $data['uri'] = $located; $find = array(get_theme_root(), bp_get_theme_compat_dir()); $replace = array(get_theme_root_uri(), bp_get_theme_compat_url()); // Make sure URI path is relative to site URL. $data['uri'] = str_replace($find, $replace, $data['uri']); return $data; }