Skip to content

Commit 8d297b4

Browse files
committed
First Commit.
0 parents  commit 8d297b4

File tree

2 files changed

+346
-0
lines changed

2 files changed

+346
-0
lines changed

readme.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== Super Light Cache Buster ===
2+
Contributors: mwalek
3+
Tags: asset version, cache, stop cache, cachebuster, cache busting
4+
Requires at least: 4.0
5+
Tested up to: 5.7.2
6+
Requires PHP: 5.2
7+
License: GPLv2 or later
8+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
9+
Stable tag: 1.0.0.1
10+
11+
12+
Stop browser caching by randomizing asset version numbers.
13+
14+
== Description ==
15+
Can't see the changes supposedly made by your developer? Temporarily activate this plugin.
16+
17+
Using less than 10 lines of code, this simple workaround adds random version numbers to CSS & JS assets to prevent browser caching getting in the way of your happiness. You can deactivate it immediately after or keep it active if the site is under development 😀.

super-light-cache-buster.php

Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
<?php
2+
/*
3+
* @since 1.0.0
4+
* @package Super_Light_Cache_Buster
5+
*
6+
* @wordpress-plugin
7+
* Plugin Name: Super Light Cache Buster
8+
* Description: Using less than 10 lines of code, this simple plugin adds random version numbers to CSS & JS assets to vanquish browser caching. Clear your Site and Server-side caches, and this plugin will do the rest.
9+
* Version: 1.0.1
10+
* Author: Mwale Kalenga
11+
* Author URI: https://mwale.me
12+
* License: GPL-2.0+
13+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
14+
*/
15+
16+
class Super_Light_Cache_Buster {
17+
public function __construct() {
18+
// Hook into the admin menu
19+
add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) );
20+
// Add Settings and Fields
21+
add_action( 'admin_init', array( $this, 'setup_sections' ) );
22+
add_action( 'admin_init', array( $this, 'setup_fields' ) );
23+
/*add_filter( 'register', 'sll_register_link' );
24+
add_action('login_head', 'control_logo_settings');*/
25+
}
26+
public function create_plugin_settings_page() {
27+
// Add the menu item and page
28+
$page_title = 'Super Light Cache Buster';
29+
$menu_title = 'Cache Buster';
30+
$capability = 'manage_options';
31+
$slug = 'slcb_options';
32+
$callback = array( $this, 'plugin_settings_page_content' );
33+
add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $slug, $callback );
34+
}
35+
public function plugin_settings_page_content() {?>
36+
<div class="wrap">
37+
<diV class="main_content">
38+
<h2>Super Light Cache Buster Settings</h2><?php /*
39+
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ){
40+
$this->admin_notice();
41+
}*/ ?>
42+
<form method="POST" action="options.php">
43+
<?php
44+
settings_fields( 'slcb_fields' );
45+
do_settings_sections( 'slcb_fields' );
46+
submit_button();
47+
?>
48+
</form>
49+
<diV>
50+
</div> <?php
51+
}
52+
53+
public function admin_notice() { ?>
54+
<div class="notice notice-success is-dismissible">
55+
<p>Your settings have been updated!</p>
56+
</div><?php
57+
}
58+
public function setup_sections() {
59+
add_settings_section( 'section_one', 'Set Cache Buster Status', array( $this, 'section_callback' ), 'slcb_fields' );
60+
add_settings_section( 'section_two', 'No Cache Header Setting', array( $this, 'section_callback' ), 'slcb_fields' );
61+
}
62+
public function section_callback( $arguments ) {
63+
switch( $arguments['id'] ){
64+
case 'section_one':
65+
echo "You can completely disable Cache Buster when you're not using it. Then it will be 100% idle.";
66+
echo '<hr>';
67+
break;
68+
case 'section_two':
69+
echo 'This will stop caching on the page in general.';
70+
echo '<hr>';
71+
break;
72+
}
73+
}
74+
public function setup_fields() {
75+
$fields = array(
76+
array(
77+
'uid' => 'randomizer_setting_one',
78+
'label' => 'Enable/Disable Cache Buster',
79+
'section' => 'section_one',
80+
'type' => 'select',
81+
'helper' => 'When disabled your cache will work normally.',
82+
'options' => array(
83+
'option1' => 'Enable',
84+
'option2' => 'Disable',
85+
),
86+
'default' => array()
87+
),
88+
array(
89+
'uid' => 'cache_header_one',
90+
'label' => 'Enable/Disable No Cache Headers',
91+
'section' => 'section_two',
92+
'type' => 'select',
93+
'helper' => 'When enabled your pages will instruct browsers not to cache them.',
94+
'options' => array(
95+
'option1' => 'Enable',
96+
'option2' => 'Disable',
97+
),
98+
'default' => array()
99+
)
100+
);
101+
foreach( $fields as $field ){
102+
add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), 'slcb_fields', $field['section'], $field );
103+
register_setting( 'slcb_fields', $field['uid'] );
104+
}
105+
}
106+
107+
public function field_callback( $arguments ) {
108+
$value = get_option( $arguments['uid'] );
109+
if( ! $value ) {
110+
$value = $arguments['default'];
111+
}
112+
switch( $arguments['type'] ){
113+
case 'text':
114+
case 'password':
115+
case 'number':
116+
printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />', $arguments['uid'], $arguments['type'], $arguments['placeholder'], $value );
117+
break;
118+
case 'textarea':
119+
printf( '<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea>', $arguments['uid'], $arguments['placeholder'], $value );
120+
break;
121+
case 'select':
122+
case 'multiselect':
123+
if( ! empty ( $arguments['options'] ) && is_array( $arguments['options'] ) ){
124+
$attributes = '';
125+
$options_markup = '';
126+
foreach( $arguments['options'] as $key => $label ){
127+
$options_markup .= sprintf( '<option value="%s" %s>%s</option>', $key, selected( $value[ array_search( $key, $value, true ) ], $key, false ), $label );
128+
}
129+
if( $arguments['type'] === 'multiselect' ){
130+
$attributes = ' multiple="multiple" ';
131+
}
132+
printf( '<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>', $arguments['uid'], $attributes, $options_markup );
133+
}
134+
break;
135+
case 'radio':
136+
case 'checkbox':
137+
if( ! empty ( $arguments['options'] ) && is_array( $arguments['options'] ) ){
138+
$options_markup = '';
139+
$iterator = 0;
140+
foreach( $arguments['options'] as $key => $label ){
141+
$iterator++;
142+
$options_markup .= sprintf( '<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>', $arguments['uid'], $arguments['type'], $key, checked( $value[ array_search( $key, $value, true ) ], $key, false ), $label, $iterator );
143+
}
144+
printf( '<fieldset>%s</fieldset>', $options_markup );
145+
}
146+
break;
147+
}
148+
if( $helper = $arguments['helper'] ){
149+
printf( '<span class="helper"> %s</span>', $helper );
150+
}
151+
/*if( $supplimental = $arguments['supplimental'] ){
152+
printf( '<p class="description">%s</p>', $supplimental );
153+
}*/
154+
}
155+
}
156+
157+
new Super_Light_Cache_Buster();
158+
159+
//var_dump($randomizer_control);
160+
161+
// Randomize version numbers
162+
function slcb_randomize_ver( $src ) {
163+
$randomizer_control = get_option('randomizer_setting_one');
164+
165+
if ( 'option1' == $randomizer_control[0] ) {
166+
167+
$random_number = wp_rand( 1000, 520000000 );
168+
$src = esc_url( add_query_arg( 'ver', $random_number, $src ) );
169+
return $src;
170+
171+
}
172+
return $src;
173+
}
174+
175+
// Add nocache_headers if enable in wp-admin options
176+
177+
function slcb_status_header() {
178+
$cache_header_control = get_option('cache_header_one');
179+
if ( 'option1' == $cache_header_control[0] ) {
180+
nocache_headers();
181+
header("Cache-Control: public, s-maxage=120");
182+
//define('WP_CACHE', false);
183+
}
184+
var_dump($cache_header_control[0]);
185+
}
186+
187+
/*add_action( 'template_redirect', array( $this, 'donotcachepage' ), 9999 );
188+
189+
public function donotcachepage() {
190+
if ( headers_sent() || ! defined( 'DONOTCACHEPAGE' ) ) {
191+
return;
192+
}
193+
194+
header( 'X-Cache-Enabled: False', true );
195+
header("Cache-Control: max-age=0, must-revalidate");
196+
}*/
197+
198+
add_action ( 'wp_head', 'hook_inHeader' );
199+
function hook_inHeader() {
200+
// Get the post id using the get_the_ID(); function:
201+
define('DONOTCACHEPAGE', true);
202+
}
203+
204+
// Randomize asset version for styles
205+
add_filter( 'style_loader_src', 'slcb_randomize_ver', 9999 );
206+
207+
// Randomize asset version for scripts
208+
add_filter( 'script_loader_src', 'slcb_randomize_ver', 9999 );
209+
210+
// NoCache Header
211+
add_action( 'send_headers', 'slcb_status_header', 9999 );
212+
213+
/*function wprdcv_param_redirect(){
214+
if( !is_admin() && !isset($_GET['cache']) ){
215+
$location = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
216+
$location .= utf8_decode('?cache=bypass');
217+
wp_redirect( $location );
218+
}
219+
}
220+
221+
add_action('template_redirect', 'wprdcv_param_redirect'); */
222+
223+
function wprdcv_param_redirect(){
224+
if( !is_admin() && !isset($_GET['cache']) ){
225+
$location = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
226+
$location .= "a=b";
227+
//$new = rawurlencode($location);
228+
// Parameters as array of key => value pairs
229+
//$newlocation = add_query_arg( 'hello', 'there', $location );
230+
wp_redirect( $location );
231+
//exit();
232+
}
233+
}
234+
235+
//add_action('template_redirect', 'wprdcv_param_redirect');
236+
237+
238+
/*add_action('init','wpse46108_register_param');
239+
function wpse46108_register_param() {
240+
global $wp;
241+
$wp->add_query_var('anyParamName');
242+
}
243+
244+
$anyParamNameValue = get_query_var('anyParamName');
245+
246+
var_dump($_SERVER["REQUEST_URI"]);
247+
var_dump($anyParamNameValue);
248+
*/
249+
250+
/*function add_custom_query_var( $vars ){
251+
$vars[] = "c";
252+
return $vars;
253+
}
254+
add_filter( 'query_vars', 'add_custom_query_var' );*/
255+
256+
/*add_action('init','add_query_args');
257+
function add_query_args()
258+
{
259+
add_query_arg( 'var1', 'val1' );
260+
}
261+
*/
262+
263+
function custom_rewrite_basic()
264+
{
265+
add_rewrite_rule('^about/([0-9]+)/?', 'about?c=$1', 'top');
266+
}
267+
//add_action('init', 'custom_rewrite_basic');
268+
269+
/**
270+
* Redirect any items without query string
271+
*
272+
* @return void
273+
274+
function wpse375877_redirect_to_referrer() {
275+
276+
if ( ! isset( $_GET, $_GET['rfd'], $_GET['dfr'] ) ) {
277+
278+
$location = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
279+
280+
wp_safe_redirect(
281+
add_query_arg( array(
282+
'rfd' => 'off',
283+
'dfr' => 'none'
284+
), $location )
285+
);
286+
287+
// wp_safe_redirect( get_permalink().'?page=1'); exit;
288+
exit();
289+
290+
}
291+
292+
}
293+
294+
add_action( 'template_redirect', 'wpse375877_redirect_to_referrer' ); */
295+
296+
/* add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
297+
function addnew_query_vars($vars)
298+
{
299+
$vars[] = 'var1'; // var1 is the name of variable you want to add
300+
return $vars;
301+
}
302+
303+
var_dump($_GET['var1']);*/
304+
305+
/*function myplugin_query_vars( $qvars ) {
306+
$qvars[] = 'custom_query_var';
307+
return $qvars;
308+
}
309+
add_filter( 'query_vars', 'myplugin_query_vars' );
310+
311+
function wpd_append_query_string( $url ) {
312+
$url = add_query_arg( 'ngg_force_update', 1, $url );
313+
return $url;
314+
}
315+
add_filter( 'page_link', 'wpd_append_query_string', 10, 2 );*/
316+
317+
// https://wordpress.stackexchange.com/questions/188749/i-am-looking-to-append-url-parameter-to-all-urls
318+
// https://wordpress.stackexchange.com/questions/250837/understanding-add-rewrite-rule
319+
// https://wordpress.stackexchange.com/questions/267131/how-to-filter-to-output-of-the-get-permalink-function
320+
// https://stackoverflow.com/questions/20754505/wordpress-add-rewrite-tag-add-rewrite-rule-and-post-link
321+
// https://newbedev.com/get-the-full-url-in-php
322+
// https://stackoverflow.com/questions/4586835/how-to-pass-extra-variables-in-url-with-wordpress
323+
// https://stackoverflow.com/questions/27432586/wordpress-page-link-filter
324+
// https://wordpress.stackexchange.com/questions/250837/understanding-add-rewrite-rule
325+
// https://wordpress.stackexchange.com/questions/257899/add-rewrite-rule-not-working-for-page-var
326+
// https://wordpress.stackexchange.com/questions/169787/adding-a-query-string-to-only-one-page-url
327+
// https://stackoverflow.com/questions/22014167/general-wordpress-link-filter
328+
// https://stackoverflow.com/questions/20754505/wordpress-add-rewrite-tag-add-rewrite-rule-and-post-link
329+
// https://wordpress.stackexchange.com/questions/375877/how-to-create-a-filter-and-add-query-params-to-all-links

0 commit comments

Comments
 (0)