OrdersVersionStringInvalidator.php
4 months ago
ProductCache.php
5 months ago
ProductCacheController.php
5 months ago
ProductVersionStringInvalidator.php
4 months ago
TaxRateVersionStringInvalidator.php
4 months ago
VersionStringGenerator.php
4 months ago
TaxRateVersionStringInvalidator.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\Caches; |
| 6 | |
| 7 | /** |
| 8 | * Tax rate version string invalidation handler. |
| 9 | * |
| 10 | * This class provides an 'invalidate' method that will invalidate |
| 11 | * the version string for a given tax rate, which in turn invalidates |
| 12 | * any cached REST API responses containing that tax rate. |
| 13 | * |
| 14 | * @since 10.6.0 |
| 15 | */ |
| 16 | class TaxRateVersionStringInvalidator { |
| 17 | |
| 18 | /** |
| 19 | * Initialize the invalidator and register hooks. |
| 20 | * |
| 21 | * Hooks are only registered when both conditions are met: |
| 22 | * - The REST API caching feature is enabled |
| 23 | * - The backend caching setting is active |
| 24 | * |
| 25 | * @return void |
| 26 | * |
| 27 | * @since 10.6.0 |
| 28 | * |
| 29 | * @internal |
| 30 | */ |
| 31 | final public function init(): void { |
| 32 | // We can't use FeaturesController::feature_is_enabled at this point |
| 33 | // (before the 'init' action is triggered) because that would cause |
| 34 | // "Translation loading for the woocommerce domain was triggered too early" warnings. |
| 35 | if ( 'yes' !== get_option( 'woocommerce_feature_rest_api_caching_enabled' ) ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if ( 'yes' === get_option( 'woocommerce_rest_api_enable_backend_caching', 'no' ) ) { |
| 40 | $this->register_hooks(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Register all tax rate-related hooks. |
| 46 | * |
| 47 | * Registers hooks for tax rate CRUD operations fired by WC_Tax class. |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
| 51 | private function register_hooks(): void { |
| 52 | add_action( 'woocommerce_tax_rate_added', array( $this, 'handle_woocommerce_tax_rate_added' ), 10, 1 ); |
| 53 | add_action( 'woocommerce_tax_rate_updated', array( $this, 'handle_woocommerce_tax_rate_updated' ), 10, 1 ); |
| 54 | add_action( 'woocommerce_tax_rate_deleted', array( $this, 'handle_woocommerce_tax_rate_deleted' ), 10, 1 ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Handle the woocommerce_tax_rate_added hook. |
| 59 | * |
| 60 | * @param int $tax_rate_id The tax rate ID. |
| 61 | * |
| 62 | * @return void |
| 63 | * |
| 64 | * @since 10.6.0 |
| 65 | * |
| 66 | * @internal |
| 67 | */ |
| 68 | public function handle_woocommerce_tax_rate_added( $tax_rate_id ): void { |
| 69 | $this->invalidate( (int) $tax_rate_id ); |
| 70 | $this->invalidate_tax_rates_list(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Handle the woocommerce_tax_rate_updated hook. |
| 75 | * |
| 76 | * @param int $tax_rate_id The tax rate ID. |
| 77 | * |
| 78 | * @return void |
| 79 | * |
| 80 | * @since 10.6.0 |
| 81 | * |
| 82 | * @internal |
| 83 | */ |
| 84 | public function handle_woocommerce_tax_rate_updated( $tax_rate_id ): void { |
| 85 | $this->invalidate( (int) $tax_rate_id ); |
| 86 | $this->invalidate_tax_rates_list(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Handle the woocommerce_tax_rate_deleted hook. |
| 91 | * |
| 92 | * @param int $tax_rate_id The tax rate ID. |
| 93 | * |
| 94 | * @return void |
| 95 | * |
| 96 | * @since 10.6.0 |
| 97 | * |
| 98 | * @internal |
| 99 | */ |
| 100 | public function handle_woocommerce_tax_rate_deleted( $tax_rate_id ): void { |
| 101 | $this->invalidate( (int) $tax_rate_id ); |
| 102 | $this->invalidate_tax_rates_list(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Invalidate the tax rates list version string. |
| 107 | * |
| 108 | * Called when tax rates are added, updated, or deleted, |
| 109 | * as these operations affect collection/list endpoints. |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | private function invalidate_tax_rates_list(): void { |
| 114 | wc_get_container()->get( VersionStringGenerator::class )->delete_version( 'list_tax_rates' ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Invalidate a tax rate version string. |
| 119 | * |
| 120 | * @param int $tax_rate_id The tax rate ID. |
| 121 | * |
| 122 | * @return void |
| 123 | * |
| 124 | * @since 10.6.0 |
| 125 | */ |
| 126 | public function invalidate( int $tax_rate_id ): void { |
| 127 | wc_get_container()->get( VersionStringGenerator::class )->delete_version( "tax_rate_{$tax_rate_id}" ); |
| 128 | } |
| 129 | } |
| 130 |