class-sst-adp.php
4 months ago
class-sst-composite-products.php
1 month ago
class-sst-deposits-for-wc.php
11 months ago
class-sst-dokan.php
8 months ago
class-sst-subscriptions.php
3 months ago
class-sst-wc-vendors.php
8 months ago
class-sst-wcfm.php
8 months ago
class-sst-wcmp.php
8 months ago
class-sst-adp.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly. |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Advanced Dynamic Pricing Integration Worker Proxy. |
| 9 | * |
| 10 | * This proxy extends ADP's WcNoFilterWorker to ensure that WooCommerce |
| 11 | * calculation hooks are always preserved, even in cases where ADP |
| 12 | * calls calculateTotals() without appropriate flags and filters. |
| 13 | * |
| 14 | * @since 8.4.4 |
| 15 | */ |
| 16 | class SST_ADP_Worker_Proxy extends \ADP\BaseVersion\Includes\WC\WcNoFilterWorker { |
| 17 | /** |
| 18 | * Overrides calculateTotals to always include 'allow_totals_hooks' flag. |
| 19 | * |
| 20 | * @param WC_Cart $wcCart |
| 21 | * @param array $flags |
| 22 | */ |
| 23 | public function calculateTotals( &$wcCart, ...$flags ) { |
| 24 | if ( ! in_array( self::FLAG_ALLOW_TOTALS_HOOKS, $flags ) ) { |
| 25 | $flags[] = self::FLAG_ALLOW_TOTALS_HOOKS; |
| 26 | } |
| 27 | return parent::calculateTotals( $wcCart, ...$flags ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Advanced Dynamic Pricing Integration. |
| 33 | * |
| 34 | * Prevents ADP from removing WooCommerce calculation hooks that are required |
| 35 | * for SST to function correctly. |
| 36 | * |
| 37 | * @author Simple Sales Tax |
| 38 | * @package SST |
| 39 | * @since 8.4.4 |
| 40 | */ |
| 41 | class SST_ADP { |
| 42 | |
| 43 | /** |
| 44 | * Constructor. |
| 45 | */ |
| 46 | public function __construct() { |
| 47 | add_filter( 'adp_calculate_totals_flags_for_cloned_cart_before_process', array( $this, 'inject_worker_proxy' ), 10, 5 ); |
| 48 | add_filter( 'adp_flags_for_final_calculate_totals', array( $this, 'add_allow_totals_hooks_flag' ) ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Injects a WorkerProxy into the CartProcessor using reflection. |
| 53 | * |
| 54 | * This ensures that EVERY call to calculateTotals within the processor |
| 55 | * will respect the 'allow_totals_hooks' flag, including unfiltered calls. |
| 56 | * |
| 57 | * @param array $flags |
| 58 | * @param object $worker |
| 59 | * @param bool $first |
| 60 | * @param object $clonedCart |
| 61 | * @param object $processor |
| 62 | * @return array |
| 63 | */ |
| 64 | public function inject_worker_proxy( $flags, $worker, $first, $clonedCart, $processor ) { |
| 65 | try { |
| 66 | $reflection = new ReflectionClass( $processor ); |
| 67 | if ( $reflection->hasProperty( 'wcNoFilterWorker' ) ) { |
| 68 | $property = $reflection->getProperty( 'wcNoFilterWorker' ); |
| 69 | $property->setAccessible( true ); |
| 70 | $current_worker = $property->getValue( $processor ); |
| 71 | if ( ! ( $current_worker instanceof SST_ADP_Worker_Proxy ) ) { |
| 72 | $property->setValue( $processor, new SST_ADP_Worker_Proxy() ); |
| 73 | } |
| 74 | } |
| 75 | } catch ( Exception $e ) { |
| 76 | // Log the error |
| 77 | SST_Logger::debug( |
| 78 | __( 'ADP Integration Error:', 'simple-sales-tax' ), |
| 79 | $e->getMessage() |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | return $this->add_allow_totals_hooks_flag( $flags ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Adds the 'allow_totals_hooks' flag to ADP's calculation flags. |
| 88 | * |
| 89 | * @param array $flags ADP calculation flags. |
| 90 | * @return array |
| 91 | */ |
| 92 | public function add_allow_totals_hooks_flag( $flags ) { |
| 93 | if ( is_array( $flags ) && ! in_array( 'allow_totals_hooks', $flags ) ) { |
| 94 | $flags[] = 'allow_totals_hooks'; |
| 95 | } |
| 96 | return $flags; |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | new SST_ADP(); |