PluginProbe
Tiered Pricing Table for WooCommerce / 8.1.0
Tiered Pricing Table for WooCommerce v8.1.0
8.1.0 8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 All 92 releases
← All changes | src/Core/Cache.php +44 -26 6.5.0 → 8.1.0 View file →
@@ -23,43 +23,58 @@
23 23
24 24 public function __construct() {
25 25
26 26 $this->isEnabled = $this->getContainer()->getSettings()->get( 'cache_enabled', 'yes' ) === 'yes';
27 -
27 +
28 + // The purge action must be available even when caching is disabled: the "Purge cache" button is always
29 + // rendered, and a stale transient left over from when caching was enabled must be removable.
30 + add_action( 'admin_post_' . self::PURGE_CACHE_ACTION, array( $this, 'handlePurgeRequest' ) );
31 +
28 32 if ( ! $this->isEnabled ) {
29 33 return;
30 34 }
31 -
35 +
32 36 // Store variable products price hashes
33 37 add_filter( 'woocommerce_get_variation_prices_hash', function ( $hash, WC_Product_Variable $product ) {
34 38 $pricesDisplayType = ServiceContainer::getInstance()->getSettings()->get( 'tiered_price_at_catalog_type',
35 39 'range' );
36 -
40 +
37 41 $hash[] = $product->get_date_modified();
38 42 $hash[] = $product->get_id();
39 -
43 +
40 44 if ( ! array_key_exists( $product->get_id(), $this->variableProductsHashes ) ) {
41 45 $this->variableProductsHashes[ $product->get_id() ] = md5( wp_json_encode( $hash ) . $pricesDisplayType );
42 46 }
43 -
47 +
44 48 return $hash;
45 49 }, 999999, 2 );
46 -
47 - add_action( 'admin_post_' . self::PURGE_CACHE_ACTION, function () {
50 + }
51 +
52 + public function handlePurgeRequest() {
48 53
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 - } );
54 + $nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( $_REQUEST['nonce'] ) : false;
55 +
56 + if ( current_user_can( 'manage_options' ) && wp_verify_nonce( $nonce, self::PURGE_CACHE_ACTION ) ) {
57 +
58 + ServiceContainer::getInstance()->getAdminNotifier()->flash( __( 'Cache has been purged successfully',
59 + 'tier-pricing-table' ) );
60 +
61 + $this->purge();
62 + }
63 +
64 + wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() );
65 + exit;
61 66 }
67 +
68 + /**
69 + * Read the stored product data. A missing transient returns false, which "(array) false" would turn into
70 + * array( 0 => false ), so normalize explicitly.
71 + */
72 + protected function readStoredData(): array {
73 + $data = get_transient( self::PRODUCT_DATA_TRANSIENT_KEY );
74 +
75 + return is_array( $data ) ? $data : array();
76 + }
62 77
63 78 public function isEnabled(): bool {
64 79 return $this->getContainer()->getSettings()->get( 'cache_enabled', 'yes' ) === 'yes';
65 80 }
@@ -72,9 +87,12 @@
72 87 return $this->variableProductsHashes[ $product->get_id() ];
73 88 }
74 89 }
75 90
76 - return md5( $product->get_date_modified() . implode( ',', TierPricingTablePlugin::getCurrentUserRoles() ) );
91 + // add-ons whose pricing changes without the product changing (scheduled global rules, for instance) salt the key
92 + $salt = (string) apply_filters( 'tiered_pricing_table/cache/product_cache_key_salt', '', $product );
93 +
94 + return md5( $product->get_date_modified() . implode( ',', TierPricingTablePlugin::getCurrentUserRoles() ) . $salt );
77 95 }
78 96
79 97 public function getProductData( WC_Product $product, $key = null ) {
80 98 if ( ! $this->isEnabled ) {
@@ -81,11 +99,11 @@
81 99 return false;
82 100 }
83 101
84 102 $productCacheKey = $this->getProductCacheKey( $product );
85 -
86 - $data = (array) get_transient( self::PRODUCT_DATA_TRANSIENT_KEY );
87 -
103 +
104 + $data = $this->readStoredData();
105 +
88 106 if ( empty( $data[ $productCacheKey ] ) ) {
89 107 return false;
90 108 }
91 109
@@ -102,11 +120,11 @@
102 120 return;
103 121 }
104 122
105 123 $productCacheKey = $this->getProductCacheKey( $product );
106 -
107 - $data = (array) get_transient( self::PRODUCT_DATA_TRANSIENT_KEY );
108 -
124 +
125 + $data = $this->readStoredData();
126 +
109 127 if ( $key ) {
110 128 $data[ $productCacheKey ][ $key ] = $value;
111 129 } else {
112 130 $data[ $productCacheKey ] = $value;