Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active March 26, 2019 21:24
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save glueckpress/d379c5534708f440942523fe205ec840 to your computer and use it in GitHub Desktop.
Save glueckpress/d379c5534708f440942523fe205ec840 to your computer and use it in GitHub Desktop.
[WordPress][WP Rocket] Creates cached files based on dynamic cookies for AffiliateWP. (WP Rocket 2.7+ required.)
<?php
/**
* Plugin Name: WP Rocket | AffiliateWP Cookie Cache
* Description: Creates a specific cache for each affiliate ref ID.
* Author: WP Rocket Support Team
* Author URI: http://wp-rocket.me/
* Plugin URI: https://gist.github.com/glueckpress/d379c5534708f440942523fe205ec840
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
defined( 'ABSPATH' ) or die();
/**
* Add Affiliate WP cookie ID to dynamic caches.
* @param array $cookies Array of cookie IDs used for dynamic caches
* @return array (Maybe) Extended array of cookie IDs
*/
function wp_rocket_affiliate_wp__dynamic_cookie( $cookies ) {
$cookies[] = 'affwp_ref';
return $cookies;
}
add_filter( 'rocket_cache_dynamic_cookies', 'wp_rocket_affiliate_wp__dynamic_cookie' );
/**
* Updates .htaccess, and regenerates config file.
*
* @return bool
*/
function wp_rocket_affiliate_wp__housekeeping() {
if ( ! function_exists( 'flush_rocket_htaccess' )
|| ! function_exists( 'rocket_generate_config_file' ) ) {
return false;
}
flush_rocket_htaccess();
rocket_generate_config_file();
return true;
}
register_activation_hook( __FILE__, 'wp_rocket_affiliate_wp__housekeeping' );
/**
* Removes plugin additions, updates .htaccess, and regenerates config file.
*
* @return bool
*/
function wp_rocket_affiliate_wp__deactivate() {
remove_filter( 'rocket_cache_dynamic_cookies', 'wp_rocket_affiliate_wp__dynamic_cookie' );
wp_rocket_affiliate_wp__housekeeping();
}
register_deactivation_hook( __FILE__, 'wp_rocket_affiliate_wp__deactivate' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment