abstracts
1 day ago
admin
1 week ago
ads
1 week ago
compatibility
1 week ago
crons
1 week ago
frontend
1 week ago
groups
1 day ago
importers
1 day ago
installation
1 year ago
interfaces
5 months ago
license
3 months ago
placements
1 week ago
rest
1 day ago
traits
1 week ago
utilities
1 week ago
cap_map.php
3 years ago
class-assets-registry.php
4 weeks ago
class-autoloader.php
1 week ago
class-cache-invalidator.php
1 week ago
class-constants.php
1 year ago
class-content-injector.php
4 weeks ago
class-entities.php
3 months ago
class-modal.php
1 year ago
class-modules.php
1 year ago
class-options.php
1 year ago
class-plugin.php
1 week ago
class-post-data.php
10 months ago
class-shortcodes.php
1 week ago
class-upgrades.php
1 day ago
class-widget.php
11 months ago
default-hooks.php
5 months ago
functions-ad.php
1 week ago
functions-components.php
3 months ago
functions-conditional.php
1 year ago
functions-core.php
1 year ago
functions-group.php
1 day ago
functions-placement.php
1 week ago
functions.php
1 week ago
index.php
2 years ago
load_modules.php
2 years ago
class-cache-invalidator.php
153 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Cache invalidation for Advanced Ads entity list caches. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 2.0.14 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds; |
| 11 | |
| 12 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 13 | use AdvancedAds\Utilities\Cache; |
| 14 | use AdvancedAds\Utilities\Validation; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Cache_Invalidator. |
| 20 | */ |
| 21 | class Cache_Invalidator implements Integration_Interface { |
| 22 | |
| 23 | /** |
| 24 | * Hook into WordPress. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function hooks(): void { |
| 29 | add_action( 'save_post', [ $this, 'invalidate_on_save_post' ], 99, 2 ); |
| 30 | add_action( 'deleted_post', [ $this, 'invalidate_on_post_change' ], 99 ); |
| 31 | add_action( 'trashed_post', [ $this, 'invalidate_on_post_change' ], 99 ); |
| 32 | add_action( 'untrashed_post', [ $this, 'invalidate_on_post_change' ], 99 ); |
| 33 | |
| 34 | add_action( 'created_term', [ $this, 'invalidate_on_term_change' ], 99, 3 ); |
| 35 | add_action( 'edited_term', [ $this, 'invalidate_on_term_change' ], 99, 3 ); |
| 36 | add_action( 'delete_term', [ $this, 'invalidate_on_term_change' ], 99, 3 ); |
| 37 | |
| 38 | add_action( 'advanced-ads-import', [ Cache_Invalidator::class, 'invalidate_all' ], 99 ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Invalidate ad list caches and factory instances. |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public static function invalidate_ads(): void { |
| 47 | Cache::flush_group( Cache::PREFIX_ADS ); |
| 48 | wp_advads_get_ad_factory()->clear_instance_cache(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Invalidate group list caches and factory instances. |
| 53 | * |
| 54 | * @return void |
| 55 | */ |
| 56 | public static function invalidate_groups(): void { |
| 57 | Cache::flush_group( Cache::PREFIX_GROUPS ); |
| 58 | wp_advads_get_group_factory()->clear_instance_cache(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Invalidate placement list caches and factory instances. |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | public static function invalidate_placements(): void { |
| 67 | Cache::flush_group( Cache::PREFIX_PLACEMENTS ); |
| 68 | wp_advads_get_placement_factory()->clear_instance_cache(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Invalidate all entity list caches and factory instances. |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public static function invalidate_all(): void { |
| 77 | Cache::flush_all(); |
| 78 | wp_advads_get_ad_factory()->clear_instance_cache(); |
| 79 | wp_advads_get_group_factory()->clear_instance_cache(); |
| 80 | wp_advads_get_placement_factory()->clear_instance_cache(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Invalidate caches when an ad or placement post is saved. |
| 85 | * |
| 86 | * @param int $post_id Post ID. |
| 87 | * @param WP_Post $post Post object. |
| 88 | * |
| 89 | * @return void |
| 90 | */ |
| 91 | public function invalidate_on_save_post( $post_id, $post ): void { |
| 92 | if ( ! Validation::check_save_post( $post_id, $post ) ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $this->invalidate_post_type( $post->post_type ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Invalidate caches when an ad or placement post is trashed, untrashed, or deleted. |
| 101 | * |
| 102 | * @param int $post_id Post ID. |
| 103 | * |
| 104 | * @return void |
| 105 | */ |
| 106 | public function invalidate_on_post_change( $post_id ): void { |
| 107 | $post_type = get_post_type( $post_id ); |
| 108 | |
| 109 | if ( ! $post_type ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | $this->invalidate_post_type( $post_type ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Invalidate caches when a group term is created, edited, or deleted. |
| 118 | * |
| 119 | * @param int $term_id Term ID. |
| 120 | * @param int $tt_id Term taxonomy ID. |
| 121 | * @param string $taxonomy Taxonomy slug. |
| 122 | * |
| 123 | * @return void |
| 124 | */ |
| 125 | public function invalidate_on_term_change( $term_id, $tt_id, $taxonomy ): void { |
| 126 | unset( $term_id, $tt_id ); |
| 127 | |
| 128 | if ( Constants::TAXONOMY_GROUP !== $taxonomy ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | self::invalidate_groups(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Invalidate caches for a supported post type. |
| 137 | * |
| 138 | * @param string $post_type Post type slug. |
| 139 | * |
| 140 | * @return void |
| 141 | */ |
| 142 | private function invalidate_post_type( $post_type ): void { |
| 143 | if ( Constants::POST_TYPE_AD === $post_type ) { |
| 144 | self::invalidate_ads(); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if ( Constants::POST_TYPE_PLACEMENT === $post_type ) { |
| 149 | self::invalidate_placements(); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 |