| 1 |
<?php |
| 2 |
/** |
| 3 |
* The provider for all purge related functionality. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Page_Cache\Purge; |
| 11 |
|
| 12 |
use SolidWP\Performance\Admin\Settings_Page; |
| 13 |
use SolidWP\Performance\Contracts\Service_Provider; |
| 14 |
use SolidWP\Performance\Page_Cache\Purge\Batch\Batch_Purger; |
| 15 |
use SolidWP\Performance\Page_Cache\Purge\Purgers\Author_Purger; |
| 16 |
use SolidWP\Performance\Page_Cache\Purge\Purgers\Archive_Purger; |
| 17 |
use SolidWP\Performance\Page_Cache\Purge\Purgers\Home_Purger; |
| 18 |
use SolidWP\Performance\Page_Cache\Purge\Purgers\Menu_Purger; |
| 19 |
use SolidWP\Performance\Page_Cache\Purge\Purgers\Option_Purger; |
| 20 |
use SolidWP\Performance\Page_Cache\Purge\Purgers\Post_Purger; |
| 21 |
use SolidWP\Performance\Page_Cache\Purge\Purgers\Settings_Purger; |
| 22 |
use SolidWP\Performance\Page_Cache\Purge\Purgers\Template_Purger; |
| 23 |
use SolidWP\Performance\Page_Cache\Purge\Purgers\Term_Purger; |
| 24 |
|
| 25 |
if ( ! defined( 'ABSPATH' ) ) { |
| 26 |
exit; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* The provider for all purge related functionality. |
| 31 |
* |
| 32 |
* @package SolidWP\Performance |
| 33 |
*/ |
| 34 |
final class Provider extends Service_Provider { |
| 35 |
|
| 36 |
/** |
| 37 |
* @inheritDoc |
| 38 |
*/ |
| 39 |
public function register(): void { |
| 40 |
$this->register_batch_purger(); |
| 41 |
$this->register_post_purger(); |
| 42 |
$this->register_author_purger(); |
| 43 |
$this->register_term_purger(); |
| 44 |
$this->register_archive_purger(); |
| 45 |
$this->register_home_purger(); |
| 46 |
$this->register_option_purger(); |
| 47 |
$this->register_template_purger(); |
| 48 |
$this->register_menu_purger(); |
| 49 |
$this->register_settings_purger(); |
| 50 |
|
| 51 |
// Allow the entire cache to be purged externally. |
| 52 |
add_action( |
| 53 |
'solidwp/performance/purge/all', |
| 54 |
function () { |
| 55 |
$this->container->get( Purge::class )->all_pages(); |
| 56 |
}, |
| 57 |
10, |
| 58 |
0 |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Register the batch purger. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
private function register_batch_purger(): void { |
| 68 |
$this->container->singleton( Batch_Purger::class, Batch_Purger::class ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Register the post purger. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
private function register_post_purger(): void { |
| 77 |
$this->container->singleton( Post_Purger::class, Post_Purger::class ); |
| 78 |
|
| 79 |
add_action( 'pre_post_update', $this->container->callback( Post_Purger::class, 'capture' ), 1, 2 ); |
| 80 |
add_filter( 'pre_trash_post', $this->container->callback( Post_Purger::class, 'capture_trashed' ), 1, 3 ); |
| 81 |
add_action( 'transition_post_status', $this->container->callback( Post_Purger::class, 'on_transition' ), 20, 3 ); |
| 82 |
add_action( 'before_delete_post', $this->container->callback( Post_Purger::class, 'on_delete' ), 20, 1 ); |
| 83 |
add_action( 'wp_update_comment_count', $this->container->callback( Post_Purger::class, 'on_comment_count' ), 20, 1 ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Register author purger. |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
private function register_author_purger(): void { |
| 92 |
add_action( 'solidwp/performance/cache/purge/post/queued', $this->container->callback( Author_Purger::class, 'on_post_purge' ), 10, 1 ); |
| 93 |
add_action( 'deleted_user', $this->container->callback( Author_Purger::class, 'on_user_change' ), 20, 0 ); |
| 94 |
add_action( 'profile_update', $this->container->callback( Author_Purger::class, 'on_user_change' ), 20, 0 ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Register term purger. |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
private function register_term_purger(): void { |
| 103 |
/** |
| 104 |
* Posts assigned more than the threshold will trigger a full cache purge. |
| 105 |
* |
| 106 |
* @param int $max_term_count The maximum number of terms a post can have. |
| 107 |
*/ |
| 108 |
$max_term_count = (int) apply_filters( 'solidwp/performance/cache/purge/max_term_count', 1500 ); |
| 109 |
|
| 110 |
$this->container->when( Term_Purger::class ) |
| 111 |
->needs( '$max_term_count' ) |
| 112 |
->give( static fn(): int => $max_term_count ); |
| 113 |
|
| 114 |
$this->container->singleton( Term_Purger::class, Term_Purger::class ); |
| 115 |
add_action( 'solidwp/performance/cache/purge/post/queued', $this->container->callback( Term_Purger::class, 'on_post_purge' ), 20, 1 ); |
| 116 |
add_action( |
| 117 |
'set_object_terms', |
| 118 |
function ( $object_id, $terms, $term_ids, $taxonomy, $append, $old_term_ids ): void { |
| 119 |
$this->container->get( Term_Purger::class )->on_term_set( (int) $object_id, (array) $term_ids, (array) $old_term_ids, (string) $taxonomy ); |
| 120 |
}, |
| 121 |
20, |
| 122 |
6 |
| 123 |
); |
| 124 |
|
| 125 |
add_action( |
| 126 |
'pre_delete_term', |
| 127 |
function ( $term_id, $taxonomy ): void { |
| 128 |
$this->container->get( Term_Purger::class )->on_term_change( $taxonomy ); |
| 129 |
}, |
| 130 |
1, |
| 131 |
2 |
| 132 |
); |
| 133 |
|
| 134 |
add_action( |
| 135 |
'edit_terms', |
| 136 |
function ( $term_id, $taxonomy ): void { |
| 137 |
$this->container->get( Term_Purger::class )->on_term_change( $taxonomy ); |
| 138 |
}, |
| 139 |
1, |
| 140 |
2 |
| 141 |
); |
| 142 |
|
| 143 |
add_action( |
| 144 |
'saved_term', |
| 145 |
function ( $term_id, $tt_id, $taxonomy ): void { |
| 146 |
$this->container->get( Term_Purger::class )->on_term_change( $taxonomy ); |
| 147 |
}, |
| 148 |
1, |
| 149 |
3 |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Register archive purger. |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
private function register_archive_purger(): void { |
| 159 |
add_action( 'solidwp/performance/cache/purge/post/queued', $this->container->callback( Archive_Purger::class, 'purge_date_archive' ), 10, 1 ); |
| 160 |
add_action( 'solidwp/performance/cache/purge/post/queued', $this->container->callback( Archive_Purger::class, 'purge_post_type_archive' ), 10, 1 ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Register home purger. |
| 165 |
* |
| 166 |
* @return void |
| 167 |
*/ |
| 168 |
private function register_home_purger(): void { |
| 169 |
add_action( 'solidwp/performance/cache/purge/post/queued', $this->container->callback( Home_Purger::class, 'on_post_purge' ), 15, 0 ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Configure the Option Purger with the different WP option names can trigger a cache flush. |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
private function register_option_purger(): void { |
| 178 |
$this->container->singleton( Option_Purger::class, Option_Purger::class ); |
| 179 |
$this->container->when( Option_Purger::class ) |
| 180 |
->needs( '$option_names' ) |
| 181 |
->give( |
| 182 |
// Different WP option names that will force a cache flush. |
| 183 |
static fn(): array => array_fill_keys( |
| 184 |
[ |
| 185 |
// options-general.php. |
| 186 |
'blogname', |
| 187 |
'blogdescription', |
| 188 |
'site_icon', |
| 189 |
'WPLANG', |
| 190 |
'timezone_string', |
| 191 |
'gmt_offset', |
| 192 |
'date_format', |
| 193 |
'time_format', |
| 194 |
'start_of_week', |
| 195 |
|
| 196 |
// options-reading.php. |
| 197 |
'page_for_posts', |
| 198 |
'page_on_front', |
| 199 |
'posts_per_page', |
| 200 |
'blog_public', |
| 201 |
|
| 202 |
// options-discussion.php. |
| 203 |
'require_name_email', |
| 204 |
'comment_registration', |
| 205 |
'close_comments_for_old_posts', |
| 206 |
'show_comments_cookies_opt_in', |
| 207 |
'thread_comments', |
| 208 |
'thread_comments_depth', |
| 209 |
'page_comments', |
| 210 |
'comments_per_page', |
| 211 |
'default_comments_page', |
| 212 |
'comment_order', |
| 213 |
'show_avatars', |
| 214 |
'avatar_rating', |
| 215 |
'avatar_default', |
| 216 |
|
| 217 |
// options-permalink.php. |
| 218 |
'permalink_structure', |
| 219 |
'category_base', |
| 220 |
'tag_base', |
| 221 |
|
| 222 |
// Appearance: themes.php. |
| 223 |
'template', |
| 224 |
'stylesheet', |
| 225 |
], |
| 226 |
true |
| 227 |
) |
| 228 |
); |
| 229 |
|
| 230 |
add_action( 'updated_option', $this->container->callback( Option_Purger::class, 'on_option_change' ), 10, 1 ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Register template purger. |
| 235 |
* |
| 236 |
* @return void |
| 237 |
*/ |
| 238 |
private function register_template_purger(): void { |
| 239 |
$this->container->singleton( Template_Purger::class, Template_Purger::class ); |
| 240 |
$this->container->when( Template_Purger::class ) |
| 241 |
->needs( '$post_types' ) |
| 242 |
->give( |
| 243 |
// Internal post types when changed, require a full cache purge. |
| 244 |
static fn(): array => [ |
| 245 |
// Gutenberg. |
| 246 |
'wp_block', |
| 247 |
'wp_navigation', |
| 248 |
'wp_template', |
| 249 |
'wp_template_part', |
| 250 |
'wp_global_styles', |
| 251 |
] |
| 252 |
); |
| 253 |
|
| 254 |
add_action( 'delete_post', $this->container->callback( Template_Purger::class, 'on_delete' ), 9, 1 ); |
| 255 |
add_action( 'wp_trash_post', $this->container->callback( Template_Purger::class, 'on_delete' ), 9, 1 ); |
| 256 |
add_action( 'transition_post_status', $this->container->callback( Template_Purger::class, 'on_transition' ), 9, 3 ); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Register the legacy menu purger. |
| 261 |
* |
| 262 |
* @return void |
| 263 |
*/ |
| 264 |
private function register_menu_purger(): void { |
| 265 |
add_action( 'wp_update_nav_menu', $this->container->callback( Menu_Purger::class, 'on_menu_update' ), 20, 0 ); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Register settings purger. |
| 270 |
* |
| 271 |
* @return void |
| 272 |
*/ |
| 273 |
private function register_settings_purger(): void { |
| 274 |
add_action( |
| 275 |
sprintf( 'update_option_%s', Settings_Page::SETTINGS_SLUG ), |
| 276 |
$this->container->callback( Settings_Purger::class, 'on_settings_change' ), |
| 277 |
5, |
| 278 |
2 |
| 279 |
); |
| 280 |
} |
| 281 |
} |
| 282 |
|