Skip to content

Commit

Permalink
Tweak: manage cache exclusion using existing method "addRules"
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Torrisi committed Nov 11, 2024
1 parent 54061e4 commit f7257a9
Showing 1 changed file with 14 additions and 83 deletions.
97 changes: 14 additions & 83 deletions includes/CacheTypes/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
class Browser extends CacheBase {







/**
* The file marker name.
*
Expand All @@ -44,7 +39,7 @@ public function __construct() {

new OptionListener( Performance::OPTION_CACHE_LEVEL, array( __CLASS__, 'maybeAddRules' ) );

new OptionListener( CacheExclusionController::OPTION_CACHE_EXCLUSION, array( __CLASS__, 'excludeFromCache' ) );
new OptionListener( CacheExclusionController::OPTION_CACHE_EXCLUSION, array( __CLASS__, 'maybeAddRules' ) );

add_filter( 'newfold_update_htaccess', array( $this, 'onRewrite' ) );
}
Expand Down Expand Up @@ -96,6 +91,19 @@ public static function addRules( $cacheLevel ) {
}
}

$cache_exclusion_parameters = array_map( 'trim', explode( ',', get_option( CacheExclusionController::OPTION_CACHE_EXCLUSION ) ) );

// Add the cache exclusion rules.
$rules[] = 'RewriteEngine On';
foreach ( $cache_exclusion_parameters as $param ) {
if ( ! empty( $param ) ) {
$rules[] = "RewriteCond %{REQUEST_URI} !{$param} [NC]";
}
}
$rules[] = 'RewriteRule .* - [E=Cache-Control:no-cache]';

// Add the end of the rules about cache exclusion.

$rules[] = '</IfModule>';

$htaccess = new htaccess( self::MARKER );
Expand Down Expand Up @@ -173,81 +181,4 @@ public static function onActivation() {
public static function onDeactivation() {
self::removeRules();
}


/**
* Write .htaccess file in order to xxclude page from cache.
*
* @return void
*/
/*
public function excludeFromCache() {
$cache_exclusion_parameters = array_map( 'trim', explode( ',', get_option( CacheExclusionController::OPTION_CACHE_EXCLUSION ) ) );
$tab = "\t";
$rules[] = '<IfModule mod_expires.c>';
$rules[] = "{$tab}RewriteEngine On";
foreach ( $cache_exclusion_parameters as $param ) {
if ( ! empty( $param ) ) {
// Exclude from cache urls that containt one of following parameters.
$rules[] = "RewriteCond %{REQUEST_URI} !{$param} [NC]\n";
}
}
$rules[] = 'RewriteRule .* - [E=Cache-Control:no-cache]';
$rules[] = '</IfModule>';
// END Custom Cache Exclusions.
$htaccess = new htaccess( self::MARKER );
return $htaccess->addContent( $rules );
}/*
/**
* Write .htaccess file in order to xxclude page from cache.
*
* @return void
*/
public function excludeFromCache() {

$htaccess_path = ABSPATH . '.htaccess';

$cache_exclusion_parameters = array_map( 'trim', explode( ',', get_option( CacheExclusionController::OPTION_CACHE_EXCLUSION ) ) );

$rewrite_conds = '';
foreach ( $cache_exclusion_parameters as $param ) {
if ( ! empty( $param ) ) {
$rewrite_conds .= "RewriteCond %{REQUEST_URI} !{$param} [NC]\n";
}
}
$htaccess_content = "
# BEGIN Custom Cache Exclusions
<IfModule mod_rewrite.c>
RewriteEngine On
# Exclude from cache urls that containt one of following parameters.
{$rewrite_conds}
RewriteRule .* - [E=Cache-Control:no-cache]
</IfModule>
# END Custom Cache Exclusions
";

if ( file_exists( $htaccess_path ) ) {
$existing_content = file_get_contents( $htaccess_path );
} else {
$existing_content = '';
}

// Remove any existing block created by this method previously.
$new_content = preg_replace( '/# BEGIN Custom Cache Exclusions.*# END Custom Cache Exclusions/s', '', $existing_content );

// Add new block to .htaccess file.
$new_content .= "\n" . $htaccess_content;

// Write the new content in .htaccess file.
file_put_contents( $htaccess_path, $new_content );
}
}

0 comments on commit f7257a9

Please sign in to comment.