Tribe__Assets::maybe_get_min_file( string $url )
Returns the path to a minified version of a js or css file, if it exists.
Contents
If the file does not exist, returns false.
Parameters
- $url
-
(string) (Required) The absolute URL to the un-minified file.
Return
(string|false) The url to the minified version or false, if file not found.
Source
File: src/Tribe/Assets.php
public static function maybe_get_min_file( $url ) {
$urls = array();
$wpmu_plugin_url = set_url_scheme( WPMU_PLUGIN_URL );
$wp_plugin_url = set_url_scheme( WP_PLUGIN_URL );
$wp_content_url = set_url_scheme( WP_CONTENT_URL );
$plugins_url = plugins_url();
if ( 0 === strpos( $url, $wpmu_plugin_url ) ) {
// URL inside WPMU plugin dir.
$base_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
$base_url = $wpmu_plugin_url;
} elseif ( 0 === strpos( $url, $wp_plugin_url ) ) {
// URL inside WP plugin dir.
$base_dir = wp_normalize_path( WP_PLUGIN_DIR );
$base_url = $wp_plugin_url;
} elseif ( 0 === strpos( $url, $wp_content_url ) ) {
// URL inside WP content dir.
$base_dir = wp_normalize_path( WP_CONTENT_DIR );
$base_url = $wp_content_url;
} elseif ( 0 === strpos( $url, $plugins_url ) ) {
$base_dir = wp_normalize_path( WP_PLUGIN_DIR );
$base_url = $plugins_url;
} else {
// Resource needs to be inside wp-content or a plugins dir.
return false;
}
// Strip the plugin URL and make this relative.
$relative_location = str_replace( $base_url, '', $url );
// If needed add the Min Files.
if ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) {
if ( substr( $relative_location, - 3, 3 ) === '.js' ) {
$urls[] = substr_replace( $relative_location, '.min', - 3, 0 );
}
if ( substr( $relative_location, - 4, 4 ) === '.css' ) {
$urls[] = substr_replace( $relative_location, '.min', - 4, 0 );
}
}
// Add the actual url after having the min file added.
$urls[] = $relative_location;
// Check for all Urls added to the array.
foreach ( $urls as $partial_path ) {
$file_path = wp_normalize_path( $base_dir . $partial_path );
$file_url = plugins_url( basename( $file_path ), $file_path );
if ( file_exists( $file_path ) ) {
return $file_url;
}
}
// If we don't have any real file return false
return false;
}
Changelog
| Version | Description |
|---|---|
| 4.5.10 | Removed ability to pass a filepath as $url |
| 4.3 | Introduced. |