Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to ignore urls #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions advanced-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Redis_Page_Cache {
private static $headers = array();
private static $ignore_cookies = array( 'wordpress_test_cookie' );
private static $ignore_request_keys = array( 'utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign' );
private static $ignore_urls = array('/*jetpack=comms*', '/*removed_item*', '/my-account*', '/wc-api/*', '/edd-api/*', '/wp-json*');
private static $whitelist_cookies = null;
private static $bail_callback = false;
private static $debug = false;
Expand Down Expand Up @@ -356,6 +357,27 @@ private static function clean_request() {
}
}

/**
* Match string against a pattern.
*
* @param string $pattern The wildcard pattern.
* @param string $subject The tested string.
*
* @return boolean Returns true if there is a match, false otherwise.
*/
private static function wildcard_match($pattern, $subject) {

$pattern='#^'.preg_quote($pattern).'$#i'; // Case insensitive
$pattern=str_replace('\*', '.*', $pattern);

if(!preg_match($pattern, $subject, $regs)){
return false;
}

return true;

}

/**
* Check some conditions where pages should never be cached or served from cache.
*/
Expand Down Expand Up @@ -390,6 +412,26 @@ private static function maybe_bail() {
}
}

// Don't cache excluded URLs
$excluded_urls = self::$ignore_urls;
if( is_array($excluded_urls) && count($excluded_urls) > 0 ) {

$current_url = $_SERVER['REQUEST_URI'];

if( isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0 ){
$current_url .= "?{$_SERVER['QUERY_STRING']}";
}

foreach( $excluded_urls as $url_to_exclude ) {

if( self::wildcard_match($url_to_exclude, $current_url) ){
return true;
}

}

}

return false; // Don't bail.
}

Expand All @@ -411,6 +453,7 @@ private static function maybe_user_config() {
'unique',
'ignore_cookies',
'ignore_request_keys',
'ignore_urls',
'whitelist_cookies',
'bail_callback',
'debug',
Expand Down