| 1 | <?php |
||||||||
| 2 | |||||||||
| 3 | /* |
||||||||
| 4 | * Plugin Name: Auto Versioning Support |
||||||||
| 5 | * Plugin URI: https://github.com/pothi/wordpress-mu-plugins |
||||||||
| 6 | * Version: 0.2 |
||||||||
| 7 | * Description: Support for automating versioning of CSS and JS files |
||||||||
| 8 | * Author Name: Pothi Kalimuthu |
||||||||
| 9 | * Author URI: http://pothi.info |
||||||||
| 10 | */ |
||||||||
| 11 | |||||||||
| 12 | if ( !class_exists('AutoVersioning') ) { |
||||||||
| 13 | |||||||||
| 14 | /** |
||||||||
| 15 | * This class enables automatic versioning of CSS/JS by adding file modification time to the URLs. |
||||||||
| 16 | * @see http://stackoverflow.com/questions/118884/ |
||||||||
| 17 | */ |
||||||||
| 18 | class AutoVersioning { |
||||||||
| 19 | private static $version_in_filename = false; |
||||||||
| 20 | |||||||||
| 21 | /** |
||||||||
| 22 | * An auto-versioning wrapper for wp_register_s*() and wp_enqueue_s*() dependency APIs. |
||||||||
| 23 | * |
||||||||
| 24 | * @static |
||||||||
| 25 | * @param string $wp_api_function The name of the WP dependency API to call. |
||||||||
| 26 | * @param string $handle Script or stylesheet handle. |
||||||||
| 27 | * @param string $src Script or stylesheet URL. |
||||||||
| 28 | * @param array $deps Dependencies. |
||||||||
| 29 | * @param bool|string $last_param Either $media (for wp_register_style) or $in_footer (for wp_register_script). |
||||||||
| 30 | * @param bool $add_ver_to_filename TRUE = add version to filename, FALSE = add it to the query string. |
||||||||
| 31 | */ |
||||||||
| 32 | public static function add_dependency($wp_api_function, $handle, $src, $deps, $last_param, $add_ver_to_filename = false ) { |
||||||||
| 33 | list($src, $version) = self::auto_version($src, $add_ver_to_filename); |
||||||||
| 34 | call_user_func($wp_api_function, $handle, $src, $deps, $version, $last_param); |
||||||||
| 35 | } |
||||||||
| 36 | |||||||||
| 37 | /** |
||||||||
| 38 | * Automatically version a script or style sheet URL based on file modification time. |
||||||||
| 39 | * |
||||||||
| 40 | * Returns auto-versioned $src and $ver values suitable for use with WordPress dependency APIs like |
||||||||
| 41 | * wp_register_script() and wp_register_style(). |
||||||||
| 42 | * |
||||||||
| 43 | * @static |
||||||||
| 44 | * @param string $url |
||||||||
| 45 | * @param bool $add_ver_to_filename |
||||||||
| 46 | * @return array array($url, $version) |
||||||||
| 47 | */ |
||||||||
| 48 | private static function auto_version($url, $add_ver_to_filename = false) { |
||||||||
| 49 | $version = false; |
||||||||
| 50 | $filename = self::guess_filename_from_url($url); |
||||||||
| 51 | |||||||||
| 52 | if ( ($filename !== null) && is_file($filename) ) { |
||||||||
| 53 | $mtime = filemtime($filename); |
||||||||
| 54 | if ( $add_ver_to_filename ) { |
||||||||
| 55 | $url = preg_replace('@\.([^./\?]+)(\?.*)?$@', '.' . $mtime . '.$1', $url); |
||||||||
| 56 | $version = null; |
||||||||
| 57 | } else { |
||||||||
| 58 | $version = $mtime; |
||||||||
| 59 | } |
||||||||
| 60 | } |
||||||||
| 61 | |||||||||
| 62 | return array($url, $version); |
||||||||
| 63 | } |
||||||||
| 64 | |||||||||
| 65 | /** |
||||||||
| 66 | * @param string $url |
||||||||
| 67 | * |
||||||||
| 68 | * @return string |
||||||||
| 69 | */ |
||||||||
| 70 | private static function guess_filename_from_url($url) { |
||||||||
| 71 | $url_mappings = array( |
||||||||
| 72 | plugins_url() => WP_PLUGIN_DIR, |
||||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||||
| 73 | plugins_url('', WPMU_PLUGIN_DIR . '/dummy') => WPMU_PLUGIN_DIR, |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 74 | get_stylesheet_directory_uri() => get_stylesheet_directory(), |
||||||||
|
0 ignored issues
–
show
The function
get_stylesheet_directory_uri was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The function
get_stylesheet_directory was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 75 | get_template_directory_uri() => get_template_directory(), |
||||||||
|
0 ignored issues
–
show
The function
get_template_directory was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The function
get_template_directory_uri was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 76 | content_url() => WP_CONTENT_DIR, |
||||||||
|
0 ignored issues
–
show
The function
content_url was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 77 | site_url('/' . WPINC) => ABSPATH . WPINC, |
||||||||
|
0 ignored issues
–
show
The function
site_url was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 78 | ); |
||||||||
| 79 | |||||||||
| 80 | $filename = null; |
||||||||
| 81 | foreach($url_mappings as $root_url => $directory) { |
||||||||
| 82 | if ( strpos($url, $root_url) === 0 ) { |
||||||||
| 83 | $filename = $directory . '/' . substr($url, strlen($root_url)); |
||||||||
| 84 | //Get rid of the query string, if any. |
||||||||
| 85 | list($filename, ) = explode('?', $filename, 2); |
||||||||
| 86 | break; |
||||||||
| 87 | } |
||||||||
| 88 | } |
||||||||
| 89 | |||||||||
| 90 | return $filename; |
||||||||
| 91 | } |
||||||||
| 92 | |||||||||
| 93 | /** |
||||||||
| 94 | * Apply automatic versioning to all scripts and style sheets added using WP dependency APIs. |
||||||||
| 95 | * |
||||||||
| 96 | * If you set $add_ver_to_filename to TRUE, make sure to also add the following code to your |
||||||||
| 97 | * .htaccess file or your site may break: |
||||||||
| 98 | * |
||||||||
| 99 | * <IfModule mod_rewrite.c> |
||||||||
| 100 | * RewriteEngine On |
||||||||
| 101 | * RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L] |
||||||||
| 102 | * </IfModule> |
||||||||
| 103 | * |
||||||||
| 104 | * @static |
||||||||
| 105 | * @param bool $add_ver_to_filename |
||||||||
| 106 | */ |
||||||||
| 107 | public static function apply_to_all_dependencies($add_ver_to_filename = false) { |
||||||||
| 108 | self::$version_in_filename = $add_ver_to_filename; |
||||||||
| 109 | foreach(array('script_loader_src', 'style_loader_src') as $hook) { |
||||||||
| 110 | add_filter($hook, __CLASS__ . '::_filter_dependency_src', 10, 1); |
||||||||
|
0 ignored issues
–
show
The function
add_filter was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 111 | } |
||||||||
| 112 | } |
||||||||
| 113 | |||||||||
| 114 | public static function _filter_dependency_src($src) { |
||||||||
| 115 | //Only add version info to CSS/JS files that don't already have it in the file name. |
||||||||
| 116 | if ( preg_match('@(?<!\.\d{10})\.(css|js)(\?|$)@i', $src) ) { |
||||||||
| 117 | list($src, $version) = self::auto_version($src, self::$version_in_filename); |
||||||||
| 118 | if ( !empty($version) ) { |
||||||||
| 119 | $src = add_query_arg('ver', $version, $src); |
||||||||
|
0 ignored issues
–
show
The function
add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 120 | } |
||||||||
| 121 | } |
||||||||
| 122 | return $src; |
||||||||
| 123 | } |
||||||||
| 124 | } |
||||||||
| 125 | |||||||||
| 126 | } //class_exists() |
||||||||
| 127 | |||||||||
| 128 | if ( !function_exists('wp_register_auto_versioned_script') ) { |
||||||||
| 129 | function wp_register_auto_versioned_script($handle, $src, $deps = array(), $in_footer = false, $add_ver_to_filename = false) { |
||||||||
| 130 | AutoVersioning::add_dependency('wp_register_script', $handle, $src, $deps, $in_footer, $add_ver_to_filename); |
||||||||
| 131 | } |
||||||||
| 132 | } |
||||||||
| 133 | |||||||||
| 134 | if ( !function_exists('wp_register_auto_versioned_style') ) { |
||||||||
| 135 | function wp_register_auto_versioned_style( $handle, $src, $deps = array(), $media = 'all', $add_ver_to_filename = false) { |
||||||||
| 136 | AutoVersioning::add_dependency('wp_register_style', $handle, $src, $deps, $media, $add_ver_to_filename); |
||||||||
| 137 | } |
||||||||
| 138 | } |
||||||||
| 139 | |||||||||
| 140 | if ( !function_exists('wp_enqueue_auto_versioned_script') ) { |
||||||||
| 141 | function wp_enqueue_auto_versioned_script( $handle, $src, $deps = array(), $in_footer = false, $add_ver_to_filename = false ) { |
||||||||
| 142 | AutoVersioning::add_dependency('wp_enqueue_script', $handle, $src, $deps, $in_footer, $add_ver_to_filename); |
||||||||
| 143 | } |
||||||||
| 144 | } |
||||||||
| 145 | |||||||||
| 146 | if ( !function_exists('wp_enqueue_auto_versioned_style') ) { |
||||||||
| 147 | function wp_enqueue_auto_versioned_style( $handle, $src, $deps = array(), $media = 'all', $add_ver_to_filename = false ) { |
||||||||
| 148 | AutoVersioning::add_dependency('wp_enqueue_style', $handle, $src, $deps, $media, $add_ver_to_filename); |
||||||||
| 149 | } |
||||||||
| 150 | } |
||||||||
| 151 | |||||||||
| 152 | AutoVersioning::apply_to_all_dependencies( true ); |
||||||||
| 153 |