PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.4
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.4
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
AutoPurge.php 4 months ago BeaverBuilder.php 4 months ago CPTOptimization.php 4 months ago CacheWarmup.php 3 months ago CartCache.php 4 months ago Components.php 3 months ago EditorClearCache.php 4 months ago GeneratePreview.php 7 months ago HTMLCompression.php 3 months ago Logger.php 4 months ago OptimizationLevel.php 3 months ago Optimizations.php 4 months ago PurgeCache.php 4 months ago Shortcodes.php 4 months ago StockRefresh.php 2 months ago Subscription.php 4 months ago SystemReport.php 3 months ago TestMode.php 4 months ago
StockRefresh.php
96 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 related pages when stock crosses zero (to 0 or from 0 to positive).
42 * For all other stock changes, invalidate only the product page.
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 $product_data = method_exists( $product_with_stock, 'get_data' ) ? $product_with_stock->get_data() : [];
58 $product_old_stock = isset( $product_data['stock_quantity'] ) ? $product_data['stock_quantity'] : null;
59
60 $product_old_stock = is_numeric( $product_old_stock ) ? (float) $product_old_stock : null;
61 $product_new_stock = is_numeric( $product_new_stock ) ? (float) $product_new_stock : null;
62
63 $went_out_of_stock = null !== $product_new_stock && 0.0 === $product_new_stock && ( null === $product_old_stock || 0.0 !== $product_old_stock );
64 $went_back_in_stock = null !== $product_old_stock && 0.0 === $product_old_stock && null !== $product_new_stock && $product_new_stock > 0;
65
66 if ( $went_out_of_stock || $went_back_in_stock ) {
67 $reason = $went_out_of_stock ? "out of stock" : "back in stock";
68 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 );
69 } else {
70 nitropack_clean_post_cache( $post, NULL, false, sprintf( "Invalidate stock change on product '%s'", $post->post_title ) );
71 }
72 }
73 /**
74 * Renders the setting in Dashboard
75 * @return void
76 */
77 public function render() {
78 $stockReduceStatus = get_option( $this->option_name );
79 ?>
80 <div class="nitro-option" id="real-time-stock-refresh-widget">
81 <div class="nitro-option-main">
82 <div class="text-box">
83 <h6><?php esc_html_e( 'Real-time Stock Refresh', 'nitropack' ); ?></h6>
84 <p>
85 <?php esc_html_e( 'Keeps product availability accurate by refreshing the cache whenever stock levels change. Best for stores that show stock quantities.', 'nitropack' ); ?>
86 </p>
87
88 </div>
89 <?php $components = new Components();
90 $components->render_toggle( 'woo-stock-reduce-status', $stockReduceStatus );
91 ?>
92 </div>
93 </div>
94 <?php
95 }
96 }