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 / Invalidations.php
nitropack / classes / WordPress Last commit date
Notifications 2 months ago Settings 2 months ago Admin.php 2 months ago CLI.php 6 months ago Config.php 1 year ago ConflictingPlugins.php 10 months ago Cron.php 1 year ago Invalidations.php 2 months ago NitroPack.php 4 months ago Settings.php 4 months ago
Invalidations.php
140 lines
1 <?php
2
3 namespace NitroPack\WordPress;
4
5 use WC_Product;
6
7 /**
8 * Post invalidations on specific events
9 */
10 class Invalidations {
11 private static $instance = null;
12 private $updated_product_props = [];
13
14 public static function getInstance() {
15 if ( null === self::$instance ) {
16 self::$instance = new self();
17 }
18 return self::$instance;
19 }
20
21 public function __construct() {
22 add_action( 'woocommerce_update_product', [ $this, 'invalidate_product_on_update' ], 10, 2 );
23 add_action( 'woocommerce_product_object_updated_props', [ $this, 'capture_product_updated_props' ], 10, 2 );
24 add_action( 'set_object_terms', [ $this, 'nitropack_sot' ], 10, 6 );
25 }
26
27 /**
28 * Capture updated WooCommerce product props for the current request.
29 *
30 * @param WC_Product $product Product object.
31 * @param array $updated_props Updated props.
32 * @return void
33 */
34 public function capture_product_updated_props( $product, $updated_props ) {
35 if ( ! $product instanceof WC_Product || ! is_array( $updated_props ) ) {
36 return;
37 }
38
39 $this->updated_product_props[ (int) $product->get_id() ] = $updated_props;
40 }
41 /**
42 * Fires after a single post taxonomy (categories, tags etc) has been updated -> assigned or removed.
43 * @param int $object_id
44 * @param array $terms
45 * @param array $tt_ids
46 * @param string $taxonomy
47 * @param bool $append
48 * @param array $old_tt_ids
49 * @return void
50 */
51 public function nitropack_sot( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
52 if ( ! get_option( "nitropack-autoCachePurge", 1 ) ) {
53 return;
54 }
55
56 $post = get_post( $object_id );
57 $post_status = $post->post_status;
58
59 if ( $post_status === 'auto-draft' || $post_status === 'draft' ) {
60 return;
61 }
62
63 if ( ! defined( 'NITROPACK_PURGE_CACHE' ) ) {
64 $purgeCache = ! nitropack_compare_posts( $tt_ids, $old_tt_ids );
65 if ( $purgeCache ) {
66 \NitroPack\WordPress\NitroPack::$np_loggedWarmups[] = get_permalink( $post );
67 nitropack_clean_post_cache( $post );
68 define( 'NITROPACK_PURGE_CACHE', true );
69 }
70 }
71 }
72 /**
73 * Invalidate product on update.
74 *
75 * @param int $id Product ID.
76 * @param WC_Product $product Product object.
77 * @return void
78 */
79 public function invalidate_product_on_update( $id, $product ) {
80 if ( ! get_option( "nitropack-autoCachePurge", 1 ) ) {
81 return;
82 }
83
84 if ( $this->should_skip_for_stock_only_update( $id ) ) {
85 return;
86 }
87
88 if ( ! defined( 'NITROPACK_PURGE_CACHE' ) ) {
89 try {
90 $post = get_post( $id );
91 nitropack_detect_changes_and_clean_post_cache( $post );
92 define( 'NITROPACK_PURGE_CACHE', true );
93 } catch (\Exception $e) {
94
95 }
96 }
97 }
98
99 /**
100 * Skip invalidation when the update changed stock-related props only.
101 *
102 * @param int $id Product ID.
103 * @return bool
104 */
105 private function should_skip_for_stock_only_update( $id ) {
106 $product_id = (int) $id;
107 $has_snapshot = array_key_exists( $product_id, $this->updated_product_props );
108 $updated_props = isset( $this->updated_product_props[ $product_id ] ) && is_array( $this->updated_product_props[ $product_id ] )
109 ? $this->updated_product_props[ $product_id ]
110 : [];
111
112 unset( $this->updated_product_props[ $product_id ] );
113
114 // Empty snapshots mean WooCommerce fired the update hook without product prop changes.
115 if ( $has_snapshot && empty( $updated_props ) ) {
116 return true;
117 }
118
119 if ( empty( $updated_props ) ) {
120 return false;
121 }
122
123 $stock_props = [
124 'manage_stock',
125 'stock_quantity',
126 'stock_status',
127 'backorders',
128 'low_stock_amount',
129 ];
130
131 foreach ( $updated_props as $prop ) {
132 if ( ! in_array( $prop, $stock_props, true ) ) {
133 return false;
134 }
135 }
136
137 return true;
138 }
139 }
140