|
<?php |
|
/* |
|
Plugin Name: cookie-hider.php |
|
Plugin URI: https://pagely.com |
|
Description: Remove Set-Cookie header from all requests unless it's on the list of excluded URLs. |
|
Author: pagely.com |
|
Version: 0.1 |
|
*/ |
|
|
|
/* |
|
Joshua Strebel's support staff from Pagely was asked about the currency switcher's suggested snippet located here |
|
https://aelia.freshdesk.com/support/solutions/articles/3000042591-how-to-add-dynamic-caching-to-your-site |
|
Instead of using that code, this is what was implemented on the server. |
|
*/ |
|
|
|
// Start off with all requests being forced to cache |
|
$pagely_trigger_remove = true; |
|
// Check for URLs to exclude |
|
// /store.*|/cart.*|/my-account.*|/checkout.*|/addons.* |
|
$exclude_urls = [ |
|
'\/cart.*', |
|
'\/checkout.*', |
|
'\/store.*', |
|
'\/my-account.*', |
|
'\/product-category.*', |
|
'\/product.*' |
|
]; |
|
// Loop over excluding REQUEST_URI's |
|
foreach ($exclude_urls as $exclude_url) { |
|
if (isset($_SERVER['REQUEST_URI']) && (preg_match("/" . $exclude_url . "/", $_SERVER['REQUEST_URI']))) { |
|
$pagely_trigger_remove = false; |
|
} |
|
} |
|
// Other check conditions if needed |
|
// Check trigger after all the checks |
|
if ($pagely_trigger_remove == true) { |
|
add_action('parse_request', 'pagely_remove_cookies'); |
|
add_action('wp_loaded', 'pagely_remove_cookies', 100000); |
|
add_action('wp_footer', 'pagely_remove_cookies', 100000); |
|
|
|
} else { |
|
// Send a header that the request was excluded |
|
// header("X-cookie-hider: excluded"); |
|
} |
|
// Function to remove cookies |
|
function pagely_remove_cookies() |
|
{ |
|
header("X-cookie-hider: triggered"); // Tag for easy debugging |
|
// remove all cookies |
|
header_remove("Set-Cookie"); |
|
header_remove("Pragma"); |
|
header_remove("Cache-Control"); |
|
header_remove("Expires"); |
|
$expiresOffset = 60*60*24*7; |
|
header("Expires: " . gmdate( "D, d M Y H:i:s", (time() + $expiresOffset) ) . " GMT"); |
|
} |