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