Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1576 #1577

Merged
merged 6 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,9 @@ public function init() {
add_action( 'wp_enqueue_scripts', array( Plugin::class, 'addWarmupAjaxToOutput' ) );
add_action( 'admin_enqueue_scripts', array( Plugin::class, 'addWarmupAjaxToOutput' ) );

//Add custom hook to clear cache from cronjob
add_action( self::$clearCacheHook, array( $this, 'clearCache' ) );

add_action('plugins_loaded', array($this, 'commonsbooking_load_textdomain'), 20);

$map_admin = new LocationMapAdmin();
Expand Down Expand Up @@ -805,7 +808,7 @@ public function savePostActions( $post_id, $post, $update ) {
if ( ! in_array( $post->post_status, $ignoredStates ) || $update ) {
$tags = Wordpress::getRelatedPostIds( $post_id );
$tags[] = 'misc';
self::clearCache( $tags );
self::scheduleClearCache( $tags );
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/Service/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

trait Cache {

/**
* TODO: Refactor to constant after PHP 8.2
* @var string
*/
private static string $clearCacheHook = COMMONSBOOKING_PLUGIN_SLUG . '_clear_cache';

/**
* Returns cache item based on calling class, function and args.
*
Expand Down Expand Up @@ -196,6 +202,22 @@ public static function clearCache( array $tags = [] ) {
set_transient("clearCacheHasBeenDone", true, 45);
}

/**
* Calls clearCache using WP Cron.
* Why? ClearCache can be resource intensive on larger instances and should be offloaded.
*
* @param array $tags
*
* @return void
*/
public static function scheduleClearCache( array $tags = [] ) {
$event = wp_schedule_single_event( time(), self::$clearCacheHook, [ $tags ] );
if ( is_wp_error( $event ) ) {
//run the event right away when scheduling fails
self::clearCache( $tags );
}
}

/**
* Add js to frontend on cache clear.
* @return void
Expand Down
2 changes: 1 addition & 1 deletion src/Wordpress/CustomPostType/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ protected static function validateTimeFrame( $timeframe ): bool {

/**
* Will update the dynamic item / location assignment for all timeframes.
* Only valid for timeframes which can have a dynamic selection type (so far only holidays and repair timeframes)
*
* @return void
*/
Expand All @@ -953,7 +954,6 @@ public static function updateAllTimeframes() {
[],
[
Timeframe::HOLIDAYS_ID,
Timeframe::BOOKABLE_ID,
Timeframe::REPAIR_ID
]
);
Expand Down