background-task-manager.php (2532B)
1 <?php 2 3 namespace Elementor\Core\Base; 4 5 use Elementor\Core\Base\Module as BaseModule; 6 use Elementor\Plugin; 7 8 if ( ! defined( 'ABSPATH' ) ) { 9 exit; // Exit if accessed directly 10 } 11 12 abstract class Background_Task_Manager extends BaseModule { 13 /** 14 * @var Background_Task 15 */ 16 protected $task_runner; 17 18 abstract public function get_action(); 19 abstract public function get_plugin_name(); 20 abstract public function get_plugin_label(); 21 abstract public function get_task_runner_class(); 22 abstract public function get_query_limit(); 23 24 abstract protected function start_run(); 25 26 public function on_runner_start() { 27 $logger = Plugin::$instance->logger->get_logger(); 28 $logger->info( $this->get_plugin_name() . '::' . $this->get_action() . ' Started' ); 29 } 30 31 public function on_runner_complete( $did_tasks = false ) { 32 $logger = Plugin::$instance->logger->get_logger(); 33 $logger->info( $this->get_plugin_name() . '::' . $this->get_action() . ' Completed' ); 34 } 35 36 public function get_task_runner() { 37 if ( empty( $this->task_runner ) ) { 38 $class_name = $this->get_task_runner_class(); 39 $this->task_runner = new $class_name( $this ); 40 } 41 42 return $this->task_runner; 43 } 44 45 // TODO: Replace with a db settings system. 46 protected function add_flag( $flag ) { 47 add_option( $this->get_plugin_name() . '_' . $this->get_action() . '_' . $flag, 1 ); 48 } 49 50 protected function get_flag( $flag ) { 51 return get_option( $this->get_plugin_name() . '_' . $this->get_action() . '_' . $flag ); 52 } 53 54 protected function delete_flag( $flag ) { 55 delete_option( $this->get_plugin_name() . '_' . $this->get_action() . '_' . $flag ); 56 } 57 58 protected function get_start_action_url() { 59 return wp_nonce_url( add_query_arg( $this->get_action(), 'run' ), $this->get_action() . 'run' ); 60 } 61 62 protected function get_continue_action_url() { 63 return wp_nonce_url( add_query_arg( $this->get_action(), 'continue' ), $this->get_action() . 'continue' ); 64 } 65 66 private function continue_run() { 67 $runner = $this->get_task_runner(); 68 $runner->continue_run(); 69 } 70 71 public function __construct() { 72 if ( empty( $_GET[ $this->get_action() ] ) ) { 73 return; 74 } 75 76 Plugin::$instance->init_common(); 77 78 if ( 'run' === $_GET[ $this->get_action() ] && check_admin_referer( $this->get_action() . 'run' ) ) { 79 $this->start_run(); 80 } 81 82 if ( 'continue' === $_GET[ $this->get_action() ] && check_admin_referer( $this->get_action() . 'continue' ) ) { 83 $this->continue_run(); 84 } 85 86 wp_safe_redirect( remove_query_arg( [ $this->get_action(), '_wpnonce' ] ) ); 87 die; 88 } 89 }