-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/bugfix/issue-1576' into release/flotte-testing
- Loading branch information
Showing
3 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters