Add after update action hook for theme

Should have done that a long time ago.
This commit is contained in:
Tetrakern 2024-03-09 03:53:15 +01:00
parent b94c9cb09d
commit f97d8721b1
2 changed files with 46 additions and 0 deletions

View File

@ -125,6 +125,14 @@ Fires after an user has been successfully created or logged-in via the OAuth 2.0
---
### `do_action( 'fictioneer_after_update' )`
Fires after the theme has been updated, once per version change.
**Hooked actions:**
* `fictioneer_purge_caches_after_update()` Purges selected caches and Transients. Priority 10.
---
### `do_action( 'fictioneer_archive_loop_after', $args )`
Archive template hook. Fires right after the result loop section in any archive template.

View File

@ -118,9 +118,47 @@ function fictioneer_theme_setup() {
// Add new size for cover snippets used in cards
add_image_size( 'snippet', 0, 200 );
// After update actions
$theme_status = get_option( 'fictioneer_theme_status' );
if ( empty( $theme_status ) || ( $theme_status['version'] ?? 0 ) !== FICTIONEER_VERSION ) {
$theme_status = is_array( $theme_status ) ? $theme_status : [];
$theme_status['version'] = FICTIONEER_VERSION;
update_option( 'fictioneer_theme_status', $theme_status );
do_action( 'fictioneer_after_update' );
}
}
add_action( 'after_setup_theme', 'fictioneer_theme_setup' );
// =============================================================================
// AFTER UPDATE
// =============================================================================
/**
* Purges selected caches and Transients after update
*
* @since 5.12.2
*/
function fictioneer_purge_caches_after_update() {
// Transients
fictioneer_delete_transients_like( 'fictioneer_' );
// Delete cached files
$cache_dir = WP_CONTENT_DIR . '/themes/fictioneer/cache/';
$files = glob( $cache_dir . '*' );
foreach ( $files as $file ) {
if ( is_file( $file ) ) {
unlink( $file );
}
}
}
add_action( 'fictioneer_after_update', 'fictioneer_purge_caches_after_update' );
// =============================================================================
// PROTECT META FIELDS
// =============================================================================