| @@ -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 | } |
| @@ -81,11 +96,11 @@ | ||
| 81 | 96 | return false; |
| 82 | 97 | } |
| 83 | 98 | |
| 84 | 99 | $productCacheKey = $this->getProductCacheKey( $product ); |
| 85 | - | |
| 86 | - $data = (array) get_transient( self::PRODUCT_DATA_TRANSIENT_KEY ); | |
| 87 | - | |
| 100 | + | |
| 101 | + $data = $this->readStoredData(); | |
| 102 | + | |
| 88 | 103 | if ( empty( $data[ $productCacheKey ] ) ) { |
| 89 | 104 | return false; |
| 90 | 105 | } |
| 91 | 106 | |
| @@ -102,11 +117,11 @@ | ||
| 102 | 117 | return; |
| 103 | 118 | } |
| 104 | 119 | |
| 105 | 120 | $productCacheKey = $this->getProductCacheKey( $product ); |
| 106 | - | |
| 107 | - $data = (array) get_transient( self::PRODUCT_DATA_TRANSIENT_KEY ); | |
| 108 | - | |
| 121 | + | |
| 122 | + $data = $this->readStoredData(); | |
| 123 | + | |
| 109 | 124 | if ( $key ) { |
| 110 | 125 | $data[ $productCacheKey ][ $key ] = $value; |
| 111 | 126 | } else { |
| 112 | 127 | $data[ $productCacheKey ] = $value; |