Skip to content

Instantly share code, notes, and snippets.

@hi-hai
Created September 12, 2018 19:53
Show Gist options
  • Save hi-hai/4f156a334a7753c819b041b0ca4cbc36 to your computer and use it in GitHub Desktop.
Save hi-hai/4f156a334a7753c819b041b0ca4cbc36 to your computer and use it in GitHub Desktop.
class-wcml-litespeed-cache.php
<?php
class WCML_LiteSpeed_Cache {
public $currency ;
function add_hooks() {
if ( method_exists( 'LiteSpeed_Cache_API', 'v' ) && LiteSpeed_Cache_API::v( '2.6' ) ) {
// LiteSpeed_Cache_API::vary is available since 2.6.
add_filter( 'wcml_client_currency', array( $this, 'apply_client_currency' ) );
add_action( 'wcml_set_client_currency', array( $this, 'set_client_currency' ) );
} elseif ( method_exists( 'LiteSpeed_Cache_API', 'hook_vary_finalize' ) ) {
// Fallback mode for older versions.
add_filter( 'wcml_client_currency', array( $this, 'compatible_apply_client_currency' ) );
add_filter( 'wcml_set_client_currency', array( $this, 'compatible_set_client_currency' ) );
// In LSCWP 2.6-, we have to use hook to affect the vary finalization
LiteSpeed_Cache_API::hook_vary_finalize( array( $this, 'compatible_apply_client_currency_to_vary' ) ) ;
}
}
function compatible_apply_client_currency( $currency ) {
// For older versions we have to use a temp variable to pass the currency to vary
if ( get_option( 'woocommerce_currency' ) != $currency ) {
$this->currency = $currency ;
}
return $currency;
}
function compatible_set_client_currency( $currency ) {
$this->compatible_apply_client_currency( $currency ) ;
add_filter( 'litespeed_ajax_vary', '__return_true' ) ;// This is to force vary set even its AJAX ( same as force_vary )
}
function compatible_apply_client_currency_to_vary( $vary ) {
if ( ! empty( $this->currency ) ) {
$vary[ 'wcml_currency' ] = $this->currency ;
}
return $vary ;
}
function set_client_currency( $currency ) {
// Add the currency to vary ( the default cache factor )
$this->apply_client_currency( $currency ) ;
LiteSpeed_Cache_API::force_vary();
}
function apply_client_currency( $currency ) {
LiteSpeed_Cache_API::vary( 'wcml_currency', $currency, get_option( 'woocommerce_currency' ) );
return $currency;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment