job-logger-trait.php
2 years ago
job-trait.php
2 years ago
self-execution-job-trait.php
2 years ago
time-based-trait.php
2 years ago
job-trait.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Jobs\Traits; |
| 5 | |
| 6 | use JFB_Modules\Post_Type\Module; |
| 7 | |
| 8 | // If this file is called directly, abort. |
| 9 | if ( ! defined( 'WPINC' ) ) { |
| 10 | die(); |
| 11 | } |
| 12 | |
| 13 | trait Job_Trait { |
| 14 | |
| 15 | private $hook = ''; |
| 16 | private $args = array(); |
| 17 | |
| 18 | public function set_hook( string $hook ) { |
| 19 | $this->hook = sprintf( 'jet-form-builder/as/%s', $hook ); |
| 20 | } |
| 21 | |
| 22 | public function get_hook(): string { |
| 23 | return $this->hook; |
| 24 | } |
| 25 | |
| 26 | public function set_arg( $name, $value ) { |
| 27 | $this->args[ $name ] = $value; |
| 28 | } |
| 29 | |
| 30 | public function get_arg( $name ) { |
| 31 | return $this->args[ $name ] ?? false; |
| 32 | } |
| 33 | |
| 34 | public function set_args( array $args ) { |
| 35 | $this->args = $args; |
| 36 | } |
| 37 | |
| 38 | public function get_args(): array { |
| 39 | return $this->args; |
| 40 | } |
| 41 | |
| 42 | public function unschedule() { |
| 43 | if ( empty( $this->get_args() ) ) { |
| 44 | as_unschedule_all_actions( $this->get_hook() ); |
| 45 | |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | as_unschedule_action( $this->get_hook(), $this->get_args(), Module::SLUG ); |
| 50 | } |
| 51 | |
| 52 | public function is_scheduled(): bool { |
| 53 | if ( ! function_exists( 'as_has_scheduled_action' ) ) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | $args = $this->get_args(); |
| 58 | // null will matches any args |
| 59 | $args = $args ?: null; |
| 60 | |
| 61 | return as_has_scheduled_action( $this->get_hook(), $args, Module::SLUG ); |
| 62 | } |
| 63 | |
| 64 | } |
| 65 |