| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) exit; |
| 4 |
|
| 5 |
use BerqWP\BerqWP; |
| 6 |
|
| 7 |
class berqWooCommerce extends berqIntegrations { |
| 8 |
function __construct() { |
| 9 |
add_action( 'woocommerce_product_set_stock_status', [$this, 'stock_update'] ); |
| 10 |
// add_action( 'woocommerce_delete_product_transients', [$this, 'delete_transient'] ); |
| 11 |
|
| 12 |
add_action('woocommerce_order_status_changed', function() { |
| 13 |
remove_action('woocommerce_delete_product_transients', [$this, 'delete_transient']); |
| 14 |
}, 1); |
| 15 |
|
| 16 |
add_action('woocommerce_order_status_changed', function() { |
| 17 |
add_action('woocommerce_delete_product_transients', [$this, 'delete_transient']); |
| 18 |
}, 999); |
| 19 |
|
| 20 |
add_action( 'wc_delete_related_product_transients_async', function () { |
| 21 |
add_filter( 'berqwp_can_flush_cache_on_post_update', '__return_false' ); |
| 22 |
} ); |
| 23 |
|
| 24 |
add_action('woocommerce_scheduled_sales', [$this, 'product_sale_end_actions']); |
| 25 |
// add_filter('berqwp_purge_home_post_types', [$this, 'purge_home']); |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
function purge_home($post_types) { |
| 30 |
|
| 31 |
if (!in_array('product', $post_types)) { |
| 32 |
$post_types[] = 'product'; |
| 33 |
} |
| 34 |
|
| 35 |
return $post_types; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
function flush_product_critical_css($post_id) { |
| 40 |
// Ensure it's a product |
| 41 |
if ( get_post_type( $post_id ) != 'product' ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
// Get the full URL of the product |
| 46 |
$product_url = get_permalink( $post_id ); |
| 47 |
$berqwp = new BerqWP(berqwp_get_license_key(), null, null); |
| 48 |
$berqwp->purge_criticlecss_url($product_url); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
function stock_update($post_id) { |
| 53 |
|
| 54 |
global $berq_log; |
| 55 |
$berq_log->info("WC stock update fired: $post_id"); |
| 56 |
|
| 57 |
$this->flush_product_cache($post_id); |
| 58 |
} |
| 59 |
|
| 60 |
function delete_transient($post_id) { |
| 61 |
|
| 62 |
if (class_exists('\JtlWooCommerceConnector\Controllers\ProductController')) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
global $berq_log; |
| 67 |
$berq_log->info("WC delete transients fired: $post_id"); |
| 68 |
|
| 69 |
$this->flush_product_cache($post_id); |
| 70 |
} |
| 71 |
|
| 72 |
function flush_product_cache($post_id) { |
| 73 |
// Ensure it's a product |
| 74 |
if ( get_post_type( $post_id ) != 'product' ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
global $berq_log; |
| 79 |
$berq_log->info("Flush product cache $post_id"); |
| 80 |
|
| 81 |
// Get the full URL of the product |
| 82 |
$product_url = get_permalink( $post_id ); |
| 83 |
berqCache::purge_page($product_url, true); |
| 84 |
} |
| 85 |
|
| 86 |
function product_sale_end_actions() { |
| 87 |
|
| 88 |
if (!function_exists('wc_get_products')) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
global $berq_log; |
| 93 |
$berq_log->info("Starting product_sale_end_actions"); |
| 94 |
|
| 95 |
$current_time = time(); |
| 96 |
$last_check = get_option( 'berqwp_product_sale_check', false ); |
| 97 |
|
| 98 |
// If is first time run |
| 99 |
if (empty($last_check)) { |
| 100 |
$last_check = time() - DAY_IN_SECONDS; |
| 101 |
} |
| 102 |
|
| 103 |
$products = wc_get_products([ |
| 104 |
'status' => 'publish', |
| 105 |
'meta_query' => [ |
| 106 |
[ |
| 107 |
'key' => '_sale_price_dates_to', |
| 108 |
'value' => $current_time, |
| 109 |
'compare' => '<=', |
| 110 |
'type' => 'NUMERIC' |
| 111 |
] |
| 112 |
] |
| 113 |
]); |
| 114 |
|
| 115 |
$berq_log->info("Found " . count($products) . " products with expired sales"); |
| 116 |
|
| 117 |
foreach ($products as $product) { |
| 118 |
$product_id = $product->get_id(); |
| 119 |
$sale_end_date = get_post_meta( $product_id, '_sale_price_dates_to', true ); |
| 120 |
if (!empty($sale_end_date) && $sale_end_date >= $last_check && $sale_end_date <= $current_time) { |
| 121 |
$berq_log->info("Flushing cache for product ID: $product_id"); |
| 122 |
$this->flush_product_cache($product_id); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
update_option( 'berqwp_product_sale_check', $current_time ); |
| 127 |
|
| 128 |
$berq_log->info("Completed product_sale_end_actions"); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
new berqWooCommerce(); |
| 135 |
|