| 1 |
<?php namespace TierPricingTable\Core; |
| 2 |
|
| 3 |
/* |
| 4 |
* Class Cache |
| 5 |
* |
| 6 |
* @package TierPricingTable\Core |
| 7 |
*/ |
| 8 |
|
| 9 |
use TierPricingTable\TierPricingTablePlugin; |
| 10 |
use WC_Product; |
| 11 |
use WC_Product_Variable; |
| 12 |
|
| 13 |
class Cache { |
| 14 |
|
| 15 |
use ServiceContainerTrait; |
| 16 |
|
| 17 |
protected $variableProductsHashes = array(); |
| 18 |
|
| 19 |
const PURGE_CACHE_ACTION = 'tpt_purge_cache'; |
| 20 |
const PRODUCT_DATA_TRANSIENT_KEY = 'tpt_product_data'; |
| 21 |
|
| 22 |
protected $isEnabled = true; |
| 23 |
|
| 24 |
public function __construct() { |
| 25 |
|
| 26 |
$this->isEnabled = $this->getContainer()->getSettings()->get( 'cache_enabled', 'yes' ) === 'yes'; |
| 27 |
|
| 28 |
if ( ! $this->isEnabled ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
// Store variable products price hashes |
| 33 |
add_filter( 'woocommerce_get_variation_prices_hash', function ( $hash, WC_Product_Variable $product ) { |
| 34 |
$pricesDisplayType = ServiceContainer::getInstance()->getSettings()->get( 'tiered_price_at_catalog_type', |
| 35 |
'range' ); |
| 36 |
|
| 37 |
$hash[] = $product->get_date_modified(); |
| 38 |
$hash[] = $product->get_id(); |
| 39 |
|
| 40 |
if ( ! array_key_exists( $product->get_id(), $this->variableProductsHashes ) ) { |
| 41 |
$this->variableProductsHashes[ $product->get_id() ] = md5( wp_json_encode( $hash ) . $pricesDisplayType ); |
| 42 |
} |
| 43 |
|
| 44 |
return $hash; |
| 45 |
}, 999999, 2 ); |
| 46 |
|
| 47 |
add_action( 'admin_post_' . self::PURGE_CACHE_ACTION, function () { |
| 48 |
|
| 49 |
$nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( $_REQUEST['nonce'] ) : false; |
| 50 |
|
| 51 |
if ( current_user_can( 'manage_options' ) && wp_verify_nonce( $nonce, self::PURGE_CACHE_ACTION ) ) { |
| 52 |
|
| 53 |
ServiceContainer::getInstance()->getAdminNotifier()->flash( __( 'Cache has been purged successfully', |
| 54 |
'tier-pricing-table' ) ); |
| 55 |
|
| 56 |
$this->purge(); |
| 57 |
} |
| 58 |
|
| 59 |
return wp_safe_redirect( wp_get_referer() ); |
| 60 |
} ); |
| 61 |
} |
| 62 |
|
| 63 |
public function isEnabled(): bool { |
| 64 |
return $this->getContainer()->getSettings()->get( 'cache_enabled', 'yes' ) === 'yes'; |
| 65 |
} |
| 66 |
|
| 67 |
public function getProductCacheKey( WC_Product $product ) { |
| 68 |
|
| 69 |
if ( TierPricingTablePlugin::isVariableProductSupported( $product ) ) { |
| 70 |
|
| 71 |
if ( ! empty( $this->variableProductsHashes[ $product->get_id() ] ) ) { |
| 72 |
return $this->variableProductsHashes[ $product->get_id() ]; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return md5( $product->get_date_modified() . implode( ',', TierPricingTablePlugin::getCurrentUserRoles() ) ); |
| 77 |
} |
| 78 |
|
| 79 |
public function getProductData( WC_Product $product, $key = null ) { |
| 80 |
if ( ! $this->isEnabled ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
$productCacheKey = $this->getProductCacheKey( $product ); |
| 85 |
|
| 86 |
$data = (array) get_transient( self::PRODUCT_DATA_TRANSIENT_KEY ); |
| 87 |
|
| 88 |
if ( empty( $data[ $productCacheKey ] ) ) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
if ( $key ) { |
| 93 |
return $data[ $productCacheKey ][ $key ] ?? false; |
| 94 |
} |
| 95 |
|
| 96 |
return $data; |
| 97 |
} |
| 98 |
|
| 99 |
public function setProductData( WC_Product $product, $key, $value ) { |
| 100 |
|
| 101 |
if ( ! $this->isEnabled ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$productCacheKey = $this->getProductCacheKey( $product ); |
| 106 |
|
| 107 |
$data = (array) get_transient( self::PRODUCT_DATA_TRANSIENT_KEY ); |
| 108 |
|
| 109 |
if ( $key ) { |
| 110 |
$data[ $productCacheKey ][ $key ] = $value; |
| 111 |
} else { |
| 112 |
$data[ $productCacheKey ] = $value; |
| 113 |
} |
| 114 |
|
| 115 |
set_transient( self::PRODUCT_DATA_TRANSIENT_KEY, $data, DAY_IN_SECONDS * 10 + rand( 10, DAY_IN_SECONDS ) ); |
| 116 |
} |
| 117 |
|
| 118 |
public function purge( $product = null ) { |
| 119 |
|
| 120 |
if ( $product ) { |
| 121 |
$this->setProductData( $product, null, null ); |
| 122 |
} else { |
| 123 |
delete_transient( self::PRODUCT_DATA_TRANSIENT_KEY ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public function getPurgeURL(): string { |
| 128 |
return add_query_arg( array( |
| 129 |
'action' => self::PURGE_CACHE_ACTION, |
| 130 |
'nonce' => wp_create_nonce( self::PURGE_CACHE_ACTION ), |
| 131 |
), admin_url( 'admin-post.php' ) ); |
| 132 |
} |
| 133 |
} |
| 134 |
|