interfaces
2 years ago
traits
2 years ago
async-job.php
2 years ago
module.php
2 years ago
once-job.php
2 years ago
recurring-job.php
2 years ago
module.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Jobs; |
| 5 | |
| 6 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 7 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 8 | use JFB_Components\Module\Base_Module_It; |
| 9 | use JFB_Components\Repository\Interfaces\Repository_Pattern_Interface; |
| 10 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 11 | use JFB_Modules\Jobs\Interfaces\Self_Execution_Job_It; |
| 12 | use JFB_Modules\Post_Type; |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | if ( ! defined( 'WPINC' ) ) { |
| 16 | die(); |
| 17 | } |
| 18 | |
| 19 | final class Module implements |
| 20 | Base_Module_It, |
| 21 | Base_Module_After_Install_It, |
| 22 | Repository_Pattern_Interface { |
| 23 | |
| 24 | use Repository_Pattern_Trait; |
| 25 | |
| 26 | private $action_id = 0; |
| 27 | |
| 28 | public function rep_item_id() { |
| 29 | return 'jobs'; |
| 30 | } |
| 31 | |
| 32 | public function condition(): bool { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | public function on_install() { |
| 37 | $this->rep_install(); |
| 38 | } |
| 39 | |
| 40 | public function on_uninstall() { |
| 41 | $this->rep_clear(); |
| 42 | } |
| 43 | |
| 44 | public function init_hooks() { |
| 45 | /** @var Self_Execution_Job_It $job */ |
| 46 | foreach ( $this->rep_generate_items() as $job ) { |
| 47 | $job->init_hooks(); |
| 48 | } |
| 49 | add_action( |
| 50 | 'action_scheduler_begin_execute', |
| 51 | array( $this, 'set_action_id' ) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | public function remove_hooks() { |
| 56 | /** @var Self_Execution_Job_It $job */ |
| 57 | foreach ( $this->rep_generate_items() as $job ) { |
| 58 | $job->remove_hooks(); |
| 59 | } |
| 60 | remove_action( |
| 61 | 'action_scheduler_begin_execute', |
| 62 | array( $this, 'set_action_id' ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | public function rep_instances(): array { |
| 67 | return array(); |
| 68 | } |
| 69 | |
| 70 | public function unschedule_all() { |
| 71 | if ( ! class_exists( '\ActionScheduler_DBStore' ) ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | \ActionScheduler_DBStore::instance()->cancel_actions_by_group( Post_Type\Module::SLUG ); |
| 76 | } |
| 77 | |
| 78 | public function set_action_id( $action_id ) { |
| 79 | $this->action_id = absint( $action_id ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return int |
| 84 | */ |
| 85 | public function get_action_id(): int { |
| 86 | return $this->action_id; |
| 87 | } |
| 88 | |
| 89 | public function install( Self_Execution_Job_It $job ) { |
| 90 | $this->rep_install_item_soft( $job ); |
| 91 | } |
| 92 | |
| 93 | public function uninstall( Self_Execution_Job_It $job ) { |
| 94 | $this->rep_remove( $job ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param string $job_hook |
| 99 | * |
| 100 | * @return Self_Execution_Job_It |
| 101 | * @throws Repository_Exception |
| 102 | */ |
| 103 | public function get( string $job_hook ): Self_Execution_Job_It { |
| 104 | return $this->rep_get_item( $job_hook ); |
| 105 | } |
| 106 | } |
| 107 |