| 1 |
<?php //phpcs:ignore |
| 2 |
/** |
| 3 |
* Compatibility Action. |
| 4 |
* |
| 5 |
* @package PRAD\Compatibility |
| 6 |
* @since v.1.0.4 |
| 7 |
*/ |
| 8 |
namespace PRAD\Includes\Compatibility; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Compatibility class. |
| 14 |
*/ |
| 15 |
class Compatibility { |
| 16 |
/** |
| 17 |
* Constructor |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
|
| 21 |
// WPC Compatibility. |
| 22 |
add_filter( 'woosb_cart_item_subtotal', array( $this, 'handle_woosb_cart_item_subtotal' ), 99, 3 ); |
| 23 |
add_filter( 'woosb_cart_item_price', array( $this, 'handle_woosb_cart_item_price' ), 99, 3 ); |
| 24 |
add_filter( 'woosb_bundles_price', array( $this, 'handle_woosb_bundles_price' ), 99, 3 ); |
| 25 |
|
| 26 |
// PRAD single product page price. |
| 27 |
add_filter( 'prad_single_product_page_price', array( $this, 'handle_prad_single_product_page_price' ), 99, 1 ); |
| 28 |
|
| 29 |
// PRAD cart/checkout page price. |
| 30 |
add_filter( 'prad_cart_checkout_page_price', array( $this, 'handle_prad_cart_checkout_page_price' ), 99, 1 ); |
| 31 |
add_filter( 'prad_cart_checkout_page_percentage_price', array( $this, 'handle_prad_cart_checkout_page_percentage_price' ), 99, 1 ); |
| 32 |
|
| 33 |
add_filter( 'prad_percentage_based_price_raw', array( $this, 'handle_prad_percentage_based_price_raw' ), 99, 2 ); |
| 34 |
|
| 35 |
// Currency Reverted Price. |
| 36 |
add_filter( 'prad_get_currency_reverted_price', array( $this, 'handle_prad_get_currency_reverted_price' ), 99, 1 ); |
| 37 |
|
| 38 |
// WP Rocket Cache. |
| 39 |
add_action( 'prad_handle_cache_on_save', array( $this, 'handle_cache_on_save' ) ); |
| 40 |
|
| 41 |
// add body class in front end. |
| 42 |
add_filter( 'body_class', array( $this, 'add_body_class' ) ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Add Class to body |
| 47 |
* |
| 48 |
* @param array $classes Array of body classes. |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
public function add_body_class( $classes ) { |
| 52 |
|
| 53 |
$classes[] = 'prad-page'; |
| 54 |
return $classes; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Handle PRAD Type Percentage price |
| 59 |
* |
| 60 |
* Retrieves the product price. |
| 61 |
* |
| 62 |
* @since 1.0.6 |
| 63 |
* |
| 64 |
* @param string $product_id Product Id. |
| 65 |
* @param string $type Type of price handling (default: 'revert'). |
| 66 |
* |
| 67 |
* @return string Price. |
| 68 |
*/ |
| 69 |
public function handle_prad_percentage_based_price_raw( $product_id, $type = 'revert' ) { |
| 70 |
$product = wc_get_product( $product_id ); |
| 71 |
$sale_price = $product->get_sale_price(); |
| 72 |
$return_price = $sale_price || '' !== $sale_price ? $sale_price : $product->get_regular_price(); |
| 73 |
if ( 'revert' === $type ) { |
| 74 |
$return_price = apply_filters( |
| 75 |
'prad_get_currency_reverted_price', |
| 76 |
$return_price |
| 77 |
); |
| 78 |
} |
| 79 |
return $return_price; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Handle PRAD Cart/Checkout page Percentage price |
| 84 |
* |
| 85 |
* Retrieves the product price. |
| 86 |
* |
| 87 |
* @since 1.0.6 |
| 88 |
* |
| 89 |
* @param string $product_id Product Id. |
| 90 |
* |
| 91 |
* @return string Price. |
| 92 |
*/ |
| 93 |
public function handle_prad_cart_checkout_page_percentage_price( $product_id ) { |
| 94 |
return $this->handle_prad_percentage_based_price_raw( $product_id, 'revert' ); |
| 95 |
} |
| 96 |
/** |
| 97 |
* Handle PRADCart/Checkout page price |
| 98 |
* |
| 99 |
* Retrieves the product price. |
| 100 |
* |
| 101 |
* @since 1.0.6 |
| 102 |
* |
| 103 |
* @param string $product_id Product Id. |
| 104 |
* |
| 105 |
* @return string Price. |
| 106 |
*/ |
| 107 |
public function handle_prad_cart_checkout_page_price( $product_id ) { |
| 108 |
$product = wc_get_product( $product_id ); |
| 109 |
$sale_price = $product->get_sale_price(); |
| 110 |
$return_price = $sale_price || '' !== $sale_price ? $sale_price : $product->get_regular_price(); |
| 111 |
$return_price = apply_filters( |
| 112 |
'prad_get_currency_reverted_price', |
| 113 |
$return_price |
| 114 |
); |
| 115 |
return $return_price; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Reverts the given price from the converted currency back to the original currency. |
| 120 |
* |
| 121 |
* This method is used to calculate the original price before currency conversion, |
| 122 |
* based on the current conversion rate and any additional conversion charges. |
| 123 |
* |
| 124 |
* @param float|string $price The converted price to revert. |
| 125 |
* @return float The original price before conversion. Returns 0 if conversion rate is invalid. |
| 126 |
*/ |
| 127 |
public function handle_prad_get_currency_reverted_price( $price ) { |
| 128 |
return BaseCurrency::revert( floatval( $price ) ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Handle PRAD single product page price |
| 133 |
* |
| 134 |
* Retrieves the product price. |
| 135 |
* |
| 136 |
* @since 1.0.6 |
| 137 |
* |
| 138 |
* @param string $product_id Product Id. |
| 139 |
* |
| 140 |
* @return string Price. |
| 141 |
*/ |
| 142 |
public function handle_prad_single_product_page_price( $product_id ) { |
| 143 |
$product = wc_get_product( $product_id ); |
| 144 |
$sale_price = $product->get_sale_price(); |
| 145 |
$to_return = $sale_price || '' !== $sale_price ? $sale_price : $product->get_regular_price(); |
| 146 |
$to_return = apply_filters( |
| 147 |
'prad_raw_tax_compitable_price', |
| 148 |
array( |
| 149 |
'product_id' => $product_id, |
| 150 |
'price' => $to_return, |
| 151 |
'source' => 'product_page', |
| 152 |
) |
| 153 |
); |
| 154 |
return $to_return; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Handles Total Price of WPC Product Bundles for WooCommerce cart subtotal. |
| 159 |
* via the 'prad_selection' data in the cart item. |
| 160 |
* |
| 161 |
* This function checks if a custom price exists in the `prad_selection` array within the cart item |
| 162 |
* and whether the `woosb_price` flag is set. If both conditions are met, it adds the custom price |
| 163 |
* to the current bundles price. |
| 164 |
* |
| 165 |
* @param float $bundles_price The current calculated price of the bundle. |
| 166 |
* @param array $cart_item The cart item data, which may contain custom pricing. |
| 167 |
* |
| 168 |
* @return float The adjusted bundle price. |
| 169 |
*/ |
| 170 |
public function handle_woosb_bundles_price( $bundles_price, $cart_item ) { |
| 171 |
if ( isset( $cart_item['prad_selection']['price'] ) && isset( $cart_item['woosb_price'] ) && $cart_item['woosb_price'] ) { |
| 172 |
$bundles_price = $bundles_price + floatval( $cart_item['prad_selection']['price'] ); |
| 173 |
} |
| 174 |
return $bundles_price; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Handles item Price of WPC Product Bundles for WooCommerce cart item. |
| 179 |
* |
| 180 |
* This function allows customization of the displayed subtotal for bundled items. |
| 181 |
* |
| 182 |
* @param string $woosb_price The modified price to be returned. |
| 183 |
* @param string $price The original calculated price. |
| 184 |
* @param array $cart_item The cart item array containing product and bundle details. |
| 185 |
* |
| 186 |
* @return string The filtered price value. |
| 187 |
*/ |
| 188 |
public function handle_woosb_cart_item_price( $woosb_price, $price, $cart_item ) { |
| 189 |
if ( isset( $cart_item['prad_selection']['price'] ) && isset( $cart_item['woosb_ids'], $cart_item['woosb_price'], $cart_item['woosb_fixed_price'] ) && ! $cart_item['woosb_fixed_price'] ) { |
| 190 |
$woosb_price = wc_price( $cart_item['woosb_price'] + floatval( $cart_item['prad_selection']['price'] ) ); |
| 191 |
} |
| 192 |
return $woosb_price; |
| 193 |
} |
| 194 |
/** |
| 195 |
* Handles subtotal of WPC Product Bundles for WooCommerce cart item. |
| 196 |
* |
| 197 |
* This function allows customization of the displayed subtotal for bundled items. |
| 198 |
* |
| 199 |
* @param string $_subtotal The modified subtotal to be returned. |
| 200 |
* @param string $subtotal The original calculated subtotal. |
| 201 |
* @param array $cart_item The cart item array containing product and bundle details. |
| 202 |
* |
| 203 |
* @return string The filtered subtotal value. |
| 204 |
*/ |
| 205 |
public function handle_woosb_cart_item_subtotal( $_subtotal, $subtotal, $cart_item ) { |
| 206 |
if ( isset( $cart_item['prad_selection']['price'] ) && isset( $cart_item['woosb_ids'], $cart_item['woosb_price'], $cart_item['woosb_fixed_price'] ) && ! $cart_item['woosb_fixed_price'] ) { |
| 207 |
$_subtotal = wc_price( ( $cart_item['woosb_price'] + floatval( $cart_item['prad_selection']['price'] ) ) * $cart_item['quantity'] ); |
| 208 |
|
| 209 |
if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() && ! wc_prices_include_tax() ) { |
| 210 |
$_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>'; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return $_subtotal; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Clears the LSCache and WP Rocket cache for the current domain. |
| 219 |
* |
| 220 |
* This method checks if the WP Rocket `rocket_clean_domain()` function exists, |
| 221 |
* and if so, calls it to purge all cached content. |
| 222 |
* |
| 223 |
* Useful when dynamic content updates are not reflected for logged-out users |
| 224 |
* due to caching. |
| 225 |
* |
| 226 |
* @return void |
| 227 |
*/ |
| 228 |
public function handle_cache_on_save() { |
| 229 |
if ( function_exists( 'rocket_clean_domain' ) ) { |
| 230 |
rocket_clean_domain(); |
| 231 |
} elseif ( class_exists( '\LiteSpeed\Purge' ) && |
| 232 |
method_exists( '\LiteSpeed\Purge', 'purge_all_lscache' ) |
| 233 |
) { |
| 234 |
\LiteSpeed\Purge::purge_all_lscache(); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|