Skip to content

Commit

Permalink
Merge branch 'refs/heads/bugfix/issue-1576' into release/flotte-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hansmorb committed Apr 18, 2024
2 parents 35c1f2d + 5c3daec commit d33db3b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use CommonsBooking\Map\SearchShortcode;
use CommonsBooking\Model\Booking;
use CommonsBooking\Model\BookingCode;
use CommonsBooking\Service\AsyncJob;
use CommonsBooking\Service\BookingRuleApplied;
use CommonsBooking\Service\Cache;
use CommonsBooking\Service\Scheduler;
Expand Down Expand Up @@ -803,7 +804,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 );
new AsyncJob( array( self::class, 'clearCache' ), [ $tags ] );
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/Service/AsyncJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace CommonsBooking\Service;

/**
* This class is used to schedule jobs to be executed in the background.
* The jobs are scheduled using cron and will be run upon the next cron run.
*
* We only support single actions that are run right away.
* Create a new instance of this class to schedule a new job to be run ASAP.
*/
class AsyncJob {

/**
* Will schedule a new job to be run as soon as possible.
*
* @param callable $callback The callback to run when the job is executed.
* @param array $args An array of args where each entry is a single argument.
*/
public function __construct( callable $callback, array $args = [] ) {
$jobhook = COMMONSBOOKING_PLUGIN_SLUG . '_async_' . wp_rand( 0, 999999999 );
add_action( $jobhook, $callback );
$success = $this->addAsync( $jobhook, $args );
if ( is_wp_error( $success ) ) {
$callback( $args );
}
}

/**
* Enqueue an action to run one time, as soon as possible
*
* @param string $hook The hook to trigger.
* @param array $args Arguments to pass when the hook triggers.
*
* @return bool|\WP_Error True if the action was successfully scheduled, WP_Error on failure.
*/
private function addAsync( string $hook, array $args = [] ): string {
return wp_schedule_single_event( time(), $hook, $args );
}

}
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

0 comments on commit d33db3b

Please sign in to comment.