PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.18.9
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.18.9
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / classes / WordPress / Settings / StockRefresh.php
nitropack / classes / WordPress / Settings Last commit date
CacheWarmup.php 7 months ago Components.php 1 year ago GeneratePreview.php 7 months ago Logger.php 7 months ago OptimizationLevel.php 7 months ago Optimizations.php 5 months ago Shortcodes.php 1 year ago StockRefresh.php 5 months ago Subscription.php 6 months ago TestMode.php 10 months ago
StockRefresh.php
90 lines
1 <?php
2 namespace NitroPack\WordPress\Settings;
3
4 use NitroPack\WordPress\Nitropack;
5
6 class StockRefresh {
7 private static $instance = NULL;
8 public $option_name;
9 public function __construct() {
10 add_action( 'wp_ajax_nitropack_set_stock_reduce_status', [ $this, 'nitropack_set_stock_reduce_status' ] );
11 add_action( 'woocommerce_variation_set_stock', [ $this, 'invalidate_pages_on_product_stock_change' ], 10, 1 );
12 add_action( 'woocommerce_product_set_stock', [ $this, 'invalidate_pages_on_product_stock_change' ], 10, 1 );
13 $this->option_name = 'nitropack-stockReduceStatus';
14 }
15 public static function getInstance() {
16 if ( null === self::$instance ) {
17 self::$instance = new self();
18 }
19 return self::$instance;
20 }
21 /**
22 * AJAX - enable or disable the setting in Dashboard
23 * @return void
24 */
25 public function nitropack_set_stock_reduce_status() {
26 nitropack_verify_ajax_nonce( $_REQUEST );
27 $option = (int) ! empty( $_POST["data"]["stockReduceStatus"] );
28 $updated = update_option( $this->option_name, $option );
29 if ( $updated ) {
30 Nitropack::getInstance()->getLogger()->notice( 'Stock Reduce Status is ' . ( $option === 1 ? 'enabled' : 'disabled' ) );
31 nitropack_json_and_exit( array( "type" => "success", "message" => nitropack_admin_toast_msgs( 'success' ), "stockReduceStatus" => $option ) );
32 } else {
33 Nitropack::getInstance()->getLogger()->error( 'Stock Reduce Status cannot be' . ( $option === 1 ? 'enabled' : 'disabled' ) );
34 nitropack_json_and_exit( array(
35 "type" => "error",
36 "message" => nitropack_admin_toast_msgs( 'error' )
37 ) );
38 }
39 }
40 /**
41 * Invalidate cache when stock changes on any occasion - related pages when goes out of stock or back in stock - 0 or 1
42 * Else, keep only the product page invalidated.
43 * @param \WC_Product $product_with_stock The WooCommerce product object.
44 * @return void
45 */
46 public function invalidate_pages_on_product_stock_change( $product_with_stock ) {
47 if ( (int) get_option( $this->option_name ) !== 1 ) {
48 return;
49 }
50 $product_manage_stock = $product_with_stock->managing_stock();
51 if ( ! $product_manage_stock ) {
52 return;
53 }
54 $product_id = (int) $product_with_stock->get_id();
55 $post = get_post( $product_id );
56 $product_new_stock = $product_with_stock->get_stock_quantity();
57
58 $low_stock = [ 0, 1 ];
59 if ( in_array( $product_new_stock, $low_stock ) ) {
60 $reason = ( $product_new_stock == 0 ) ? "out of stock" : "low in stock";
61 nitropack_clean_post_cache( $post, array( 'added' => nitropack_get_taxonomies( $post ) ), true, sprintf( "Invalidate related pages due to %s change on product '%s'", $reason, $post->post_title ), true );
62 } else {
63 nitropack_clean_post_cache( $post, NULL, false, sprintf( "Invalidate stock change on product '%s'", $post->post_title ) );
64 }
65 }
66 /**
67 * Renders the setting in Dashboard
68 * @return void
69 */
70 public function render() {
71 $stockReduceStatus = get_option( 'nitropack-stockReduceStatus' );
72 ?>
73 <div class="nitro-option" id="real-time-stock-refresh-widget">
74 <div class="nitro-option-main">
75 <div class="text-box">
76 <h6><?php esc_html_e( 'Real-time Stock Refresh', 'nitropack' ); ?></h6>
77 <p>
78 <?php esc_html_e( 'Keeps product availability accurate by refreshing the cache whenever stock levels change. Best for stores that show stock quantities.', 'nitropack' ); ?>
79 </p>
80
81 </div>
82 <label class="inline-flex items-center cursor-pointer ml-auto">
83 <input type="checkbox" id="woo-stock-reduce-status" class="sr-only peer" <?php echo (int) $stockReduceStatus === 1 ? "checked" : ""; ?>>
84 <div class="toggle"></div>
85 </label>
86 </div>
87 </div>
88 <?php
89 }
90 }