Changeset 3027738
- Timestamp:
- 01/28/2024 05:47:12 AM (2 years ago)
- Location:
- disable-css-js-cache/trunk
- Files:
-
- 3 edited
-
README.txt (modified) (1 diff)
-
disable-css-js-cache.php (modified) (2 diffs)
-
includes/class-disable-css-js-cache.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
disable-css-js-cache/trunk/README.txt
r2967905 r3027738 45 45 == Changelog == 46 46 47 = 1.0.3 = 48 * Added support for wordpress 6.4.2 49 * Feature : Enable browser caching for static assets for performance improvement 50 47 51 = 1.0.2 = 48 52 * Added support for wordpress 6.3.1 -
disable-css-js-cache/trunk/disable-css-js-cache.php
r2967904 r3027738 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. 219 * Version: 1.0.3 20 20 * Author: Umang Prajapati 21 21 * License: GPL-2.0+ … … 38 38 * Rename this for your plugin and update it as you release new versions. 39 39 */ 40 define( 'DISABLE_CSS_JS_CACHE_VERSION', '1.0. 0' );40 define( 'DISABLE_CSS_JS_CACHE_VERSION', '1.0.3' ); 41 41 42 42 /** -
disable-css-js-cache/trunk/includes/class-disable-css-js-cache.php
r2909090 r3027738 196 196 function disable_css_js_settings_page() { 197 197 $disable_css_js_cache_radio = get_option('disable_css_js_cache_radio'); 198 $browser_caching_enabled = get_option('browser_caching_enabled', true); 199 $browser_cache_duration = get_option('browser_cache_duration', 604800); // 7 days by default 198 200 ?> 199 201 <div class="wrap"> … … 212 214 </fieldset></td> 213 215 </tr> 216 217 <tr> 218 <th scope="row">Browser Caching Header for Static Assets</th> 219 <td><fieldset><legend class="screen-reader-text"><span> 220 Static Assets</span></legend> 221 <label for="browser_caching_enabled"> 222 <input name="browser_caching_enabled" type="checkbox" id="browser_caching_enabled" value="1" <?php if($browser_caching_enabled == '1' ) { echo 'checked'; } ?>> 223 Enable browser caching for static assets</label> 224 </fieldset></td> 225 </tr> 226 <tr> 227 <th scope="row">Caching Duration for Static Assets</th> 228 <td><fieldset><legend class="screen-reader-text"><span> 229 Static Assets</span></legend> 230 <label for="browser_cache_duration"> 231 <input name="browser_cache_duration" type="number" id="browser_cache_duration" value="<?php echo esc_attr($browser_cache_duration); ?>"> 232 (Enter Value in Seconds. By default it set to 604800 = 1 Week)</label> 233 </fieldset></td> 234 </tr> 214 235 </tbody></table> 215 236 <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes"></p> … … 222 243 add_options_page( 223 244 'Disable CSS JS Cache Settings', 224 'Disable CSS JS ',245 'Disable CSS JS Cache', 225 246 'manage_options', 226 247 'disable-css-js-cache-settings', … … 235 256 add_option( 'disable_css_js_cache_radio', ''); 236 257 register_setting( 'dcjc_options_group', 'disable_css_js_cache_radio', 'dcjc_callback' ); 258 add_option( 'browser_caching_enabled', ''); 259 register_setting( 'dcjc_options_group', 'browser_caching_enabled', 'dcjc_callback' ); 260 add_option( 'browser_cache_duration', ''); 261 register_setting( 'dcjc_options_group', 'browser_cache_duration', 'dcjc_callback' ); 262 add_option( 'browser_caching_settings_changed', ''); 263 register_setting( 'dcjc_options_group', 'browser_caching_settings_changed', 'dcjc_callback' ); 237 264 238 265 } … … 250 277 } 251 278 /* End of Add plugin action links */ 279 280 // Add cache control directives to .htaccess based on plugin settings 281 function disable_css_js_add_browser_caching_to_htaccess() { 282 $browser_caching_enabled = get_option('browser_caching_enabled', true); 283 $cache_duration = get_option('browser_cache_duration', 604800); // 7 days by default 284 285 if ($browser_caching_enabled) { 286 // Custom comment to mark the beginning of cache control directives 287 $htaccess_rules = ' 288 # BEGIN Browser Caching Configuration 289 <IfModule mod_headers.c> 290 # One year for image and video files 291 <filesMatch ".(flv|gif|ico|jpg|jpeg|mp4|mpeg|png|svg|swf|webp)$"> 292 Header set Cache-Control "max-age=' . $cache_duration . ', public" 293 </filesMatch> 294 295 # One month for JavaScript and PDF files 296 <filesMatch ".(js|pdf)$"> 297 Header set Cache-Control "max-age=' . $cache_duration . ', public" 298 </filesMatch> 299 300 # One week for CSS files 301 <filesMatch ".(css)$"> 302 Header set Cache-Control "max-age=' . $cache_duration . ', public" 303 </filesMatch> 304 </IfModule> 305 # END Browser Caching Configuration 306 '; 307 308 // Add or update the rules in .htaccess 309 $htaccess_path = ABSPATH . '.htaccess'; 310 311 if (file_exists($htaccess_path) && is_writable($htaccess_path)) { 312 $htaccess_content = file_get_contents($htaccess_path); 313 314 // Remove existing cache control directives 315 $htaccess_content = preg_replace('/# BEGIN Browser Caching Configuration.*?# END Browser Caching Configuration/s', '', $htaccess_content); 316 317 // Append the new cache control directives 318 $htaccess_content .= $htaccess_rules; 319 320 // Remove leading and trailing blank lines 321 $htaccess_content = trim($htaccess_content); 322 323 // Save the modified .htaccess file 324 file_put_contents($htaccess_path, $htaccess_content); 325 } else { 326 // Output a warning if .htaccess is not writable 327 error_log("Browser Caching Configuration: Unable to modify .htaccess. Please ensure it is writable."); 328 } 329 } else { 330 // If caching is not enabled, remove the rules 331 $htaccess_path = ABSPATH . '.htaccess'; 332 333 if (file_exists($htaccess_path) && is_writable($htaccess_path)) { 334 $htaccess_content = file_get_contents($htaccess_path); 335 336 // Remove existing cache control directives 337 $htaccess_content = preg_replace('/# BEGIN Browser Caching Configuration.*?# END Browser Caching Configuration/s', '', $htaccess_content); 338 339 // Remove leading and trailing blank lines 340 $htaccess_content = trim($htaccess_content); 341 342 // Save the modified .htaccess file 343 file_put_contents($htaccess_path, $htaccess_content); 344 } else { 345 // Output a warning if .htaccess is not writable 346 error_log("Browser Caching Configuration: Unable to modify .htaccess. Please ensure it is writable."); 347 } 348 } 349 } 350 351 // Hook the function to a suitable action, such as 'admin_init' for settings changes 352 add_action('admin_init', 'disable_css_js_add_browser_caching_to_htaccess'); 353
Note: See TracChangeset
for help on using the changeset viewer.