PluginProbe
Cachebuster / 1.2
Cachebuster v1.2
1.10 trunk 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
cachebuster / cachebuster.php

cachebuster.php in Cachebuster 1.2, at cachebuster.php

53 lines 1.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Cachebuster
4 Description: Append a file's modification time to its URL and prevent stale caches.
5 Version: 1.2
6 Author: Albert Peschar
7 Author URI: https://kiboit.com
8 License: GPLv2 or later
9 */
10
11 function cabu_src($src, $handle) {
12 static $site_url;
13
14 if(!isset($site_url))
15 $site_url = cabu_normalize_url(get_site_url());
16
17 $file = cabu_normalize_url($src);
18
19 if($site_url && strpos($file, $site_url) === 0)
20 $file = substr($file, strlen($site_url));
21 elseif(strpos($file, '//') === 0)
22 return $src;
23
24 if(($pos = strpos($file, '?')) !== false)
25 $file = substr($file, 0, $pos);
26
27 $file = ltrim($file, '/');
28
29 if($file == '')
30 return $src;
31
32 $path = ABSPATH . '/' . $file;
33
34 $mtime = @filemtime($path);
35
36 if(!$mtime)
37 return $src;
38
39 $query = '?=' . $mtime;
40
41 if(($pos = strpos($src, '?')) !== false)
42 $src = substr($src, 0, $pos);
43
44 return $src . $query;
45 }
46
47 function cabu_normalize_url($url) {
48 return preg_replace('|^https?://|i', '//', $url);
49 }
50
51 add_filter('style_loader_src', 'cabu_src', 10, 2);
52 add_filter('script_loader_src', 'cabu_src', 10, 2);
53