Notifications
3 months ago
Settings
3 months ago
Admin.php
3 months ago
CLI.php
6 months ago
Config.php
1 year ago
ConflictingPlugins.php
10 months ago
Cron.php
1 year ago
Invalidations.php
4 months ago
NitroPack.php
4 months ago
Settings.php
4 months ago
Invalidations.php
76 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 | public static function getInstance() { |
| 13 | if ( null === self::$instance ) { |
| 14 | self::$instance = new self(); |
| 15 | } |
| 16 | return self::$instance; |
| 17 | } |
| 18 | |
| 19 | public function __construct() { |
| 20 | add_action( 'woocommerce_update_product', [ $this, 'invalidate_product_on_update' ], 10, 2 ); |
| 21 | add_action( 'set_object_terms', [ $this, 'nitropack_sot' ], 10, 6 ); |
| 22 | } |
| 23 | /** |
| 24 | * Fires after a single post taxonomy (categories, tags etc) has been updated -> assigned or removed. |
| 25 | * @param int $object_id |
| 26 | * @param array $terms |
| 27 | * @param array $tt_ids |
| 28 | * @param string $taxonomy |
| 29 | * @param bool $append |
| 30 | * @param array $old_tt_ids |
| 31 | * @return void |
| 32 | */ |
| 33 | public function nitropack_sot( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { |
| 34 | if ( ! get_option( "nitropack-autoCachePurge", 1 ) ) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | $post = get_post( $object_id ); |
| 39 | $post_status = $post->post_status; |
| 40 | |
| 41 | if ( $post_status === 'auto-draft' || $post_status === 'draft' ) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | if ( ! defined( 'NITROPACK_PURGE_CACHE' ) ) { |
| 46 | $purgeCache = ! nitropack_compare_posts( $tt_ids, $old_tt_ids ); |
| 47 | $cleanCache = $purgeCache ? "YES" : "NO"; |
| 48 | if ( $purgeCache ) { |
| 49 | \NitroPack\WordPress\NitroPack::$np_loggedWarmups[] = get_permalink( $post ); |
| 50 | nitropack_clean_post_cache( $post ); |
| 51 | define( 'NITROPACK_PURGE_CACHE', true ); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | /** |
| 56 | * Invalidate product on update, including REST-API updates. |
| 57 | * Mainly used for REST-API, since the nitropack_handle_post_transition() does not cover that. |
| 58 | * @param int $id |
| 59 | * @param WC_Product $product |
| 60 | * @return void |
| 61 | */ |
| 62 | public function invalidate_product_on_update( $id, $product ) { |
| 63 | if ( ! get_option( "nitropack-autoCachePurge", 1 ) ) { |
| 64 | return; |
| 65 | } |
| 66 | if ( ! defined( 'NITROPACK_PURGE_CACHE' ) ) { |
| 67 | try { |
| 68 | $post = get_post( $id ); |
| 69 | nitropack_detect_changes_and_clean_post_cache( $post ); |
| 70 | define( 'NITROPACK_PURGE_CACHE', true ); |
| 71 | } catch (\Exception $e) { |
| 72 | |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |