Changeset 3257297
- Timestamp:
- 03/17/2025 05:30:18 PM (13 months ago)
- Location:
- disable-css-js-cache/trunk
- Files:
-
- 3 edited
-
README.txt (modified) (1 diff)
-
disable-css-js-cache.php (modified) (1 diff)
-
includes/class-disable-css-js-cache.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
disable-css-js-cache/trunk/README.txt
r3257290 r3257297 47 47 == Changelog == 48 48 49 = 1.0.7 = 50 * fixed frequent updating .htaccess file issue on page load 51 49 52 = 1.0.6 = 50 53 * Added support for wordpress 6.7.2 -
disable-css-js-cache/trunk/disable-css-js-cache.php
r3257290 r3257297 17 17 * Plugin URI: https://phptutorialpoints.in 18 18 * Description: This plugin helps prevent browser caching of CSS and JS files from theme in WordPress. 19 * Version: 1.0. 619 * Version: 1.0.7 20 20 * Author: Umang Prajapati 21 21 * License: GPL-2.0+ -
disable-css-js-cache/trunk/includes/class-disable-css-js-cache.php
r3257290 r3257297 280 280 /* End of Add plugin action links */ 281 281 282 // Add cache control directives to .htaccess based on plugin settings283 282 function disable_css_js_add_browser_caching_to_htaccess() { 284 283 $browser_caching_enabled = get_option('browser_caching_enabled', true); 285 $cache_duration = get_option('browser_cache_duration', 604800); // 7 days by default 286 284 $cache_duration = absint(get_option('browser_cache_duration', 604800)); // 7 days by default 285 286 // Validate cache duration 287 if ($cache_duration < 60 || $cache_duration > 31536000) { 288 $cache_duration = 604800; // Default to 7 days 289 } 290 291 $htaccess_path = ABSPATH . '.htaccess'; 292 293 if (!file_exists($htaccess_path)) { 294 add_settings_error('disable_css_js_cache', 'htaccess_missing', 'The .htaccess file does not exist.', 'error'); 295 return; 296 } 297 298 if (!is_writable($htaccess_path)) { 299 add_settings_error('disable_css_js_cache', 'htaccess_not_writable', 'The .htaccess file is not writable. Please check file permissions.', 'error'); 300 return; 301 } 302 303 $htaccess_content = file_get_contents($htaccess_path); 304 305 // Generate the new rules 306 $new_rules = ''; 287 307 if ($browser_caching_enabled) { 288 // Custom comment to mark the beginning of cache control directives 289 $htaccess_rules = ' 308 $new_rules = ' 290 309 # BEGIN Browser Caching Configuration 291 <IfModule mod_headers.c> 310 <IfModule mod_headers.c> 292 311 # One year for image and video files 293 312 <filesMatch ".(flv|gif|ico|jpg|jpeg|mp4|mpeg|png|svg|swf|webp)$"> … … 307 326 # END Browser Caching Configuration 308 327 '; 309 310 // Add or update the rules in .htaccess 311 $htaccess_path = ABSPATH . '.htaccess'; 312 313 if (file_exists($htaccess_path) && is_writable($htaccess_path)) { 314 $htaccess_content = file_get_contents($htaccess_path); 315 316 // Remove existing cache control directives 317 $htaccess_content = preg_replace('/# BEGIN Browser Caching Configuration.*?# END Browser Caching Configuration/s', '', $htaccess_content); 318 319 // Append the new cache control directives 320 $htaccess_content .= $htaccess_rules; 321 322 // Remove leading and trailing blank lines 323 $htaccess_content = trim($htaccess_content); 324 325 // Save the modified .htaccess file 326 file_put_contents($htaccess_path, $htaccess_content); 327 } else { 328 // Output a warning if .htaccess is not writable 329 error_log("Browser Caching Configuration: Unable to modify .htaccess. Please ensure it is writable."); 328 } 329 330 // Check if the new rules are already present 331 $existing_rules = preg_match('/# BEGIN Browser Caching Configuration.*?# END Browser Caching Configuration/s', $htaccess_content, $matches) ? $matches[0] : ''; 332 333 if ($existing_rules !== $new_rules) { 334 // Remove existing rules 335 $htaccess_content = preg_replace('/# BEGIN Browser Caching Configuration.*?# END Browser Caching Configuration/s', '', $htaccess_content); 336 337 // Append new rules if caching is enabled 338 if ($browser_caching_enabled) { 339 $htaccess_content .= $new_rules; 330 340 } 331 } else { 332 // If caching is not enabled, remove the rules 333 $htaccess_path = ABSPATH . '.htaccess'; 334 335 if (file_exists($htaccess_path) && is_writable($htaccess_path)) { 336 $htaccess_content = file_get_contents($htaccess_path); 337 338 // Remove existing cache control directives 339 $htaccess_content = preg_replace('/# BEGIN Browser Caching Configuration.*?# END Browser Caching Configuration/s', '', $htaccess_content); 340 341 // Remove leading and trailing blank lines 342 $htaccess_content = trim($htaccess_content); 343 344 // Save the modified .htaccess file 345 file_put_contents($htaccess_path, $htaccess_content); 346 } else { 347 // Output a warning if .htaccess is not writable 348 error_log("Browser Caching Configuration: Unable to modify .htaccess. Please ensure it is writable."); 349 } 350 } 351 } 352 353 // Hook the function to a suitable action, such as 'admin_init' for settings changes 354 add_action('admin_init', 'disable_css_js_add_browser_caching_to_htaccess'); 355 341 342 // Save the modified .htaccess file 343 file_put_contents($htaccess_path, trim($htaccess_content)); 344 345 add_settings_error('disable_css_js_cache', 'htaccess_updated', 'Browser caching rules have been updated in .htaccess.', 'updated'); 346 } 347 } 348 349 // Hook the function to run only when relevant options are updated 350 add_action('update_option_browser_caching_enabled', 'disable_css_js_add_browser_caching_to_htaccess'); 351 add_action('update_option_browser_cache_duration', 'disable_css_js_add_browser_caching_to_htaccess'); 352
Note: See TracChangeset
for help on using the changeset viewer.