| 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 |
* @param $flag |
| 46 |
* @return void |
| 47 |
* // TODO: Replace with a db settings system. |
| 48 |
*/ |
| 49 |
protected function add_flag( $flag ) { |
| 50 |
add_option( $this->get_plugin_name() . '_' . $this->get_action() . '_' . $flag, 1 ); |
| 51 |
} |
| 52 |
|
| 53 |
protected function get_flag( $flag ) { |
| 54 |
return get_option( $this->get_plugin_name() . '_' . $this->get_action() . '_' . $flag ); |
| 55 |
} |
| 56 |
|
| 57 |
protected function delete_flag( $flag ) { |
| 58 |
delete_option( $this->get_plugin_name() . '_' . $this->get_action() . '_' . $flag ); |
| 59 |
} |
| 60 |
|
| 61 |
protected function get_start_action_url() { |
| 62 |
return wp_nonce_url( add_query_arg( $this->get_action(), 'run' ), $this->get_action() . 'run' ); |
| 63 |
} |
| 64 |
|
| 65 |
protected function get_continue_action_url() { |
| 66 |
return wp_nonce_url( add_query_arg( $this->get_action(), 'continue' ), $this->get_action() . 'continue' ); |
| 67 |
} |
| 68 |
|
| 69 |
private function continue_run() { |
| 70 |
$runner = $this->get_task_runner(); |
| 71 |
$runner->continue_run(); |
| 72 |
} |
| 73 |
|
| 74 |
public function __construct() { |
| 75 |
if ( empty( $_GET[ $this->get_action() ] ) ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
Plugin::$instance->init_common(); |
| 80 |
|
| 81 |
if ( 'run' === $_GET[ $this->get_action() ] && check_admin_referer( $this->get_action() . 'run' ) ) { |
| 82 |
$this->start_run(); |
| 83 |
} |
| 84 |
|
| 85 |
if ( 'continue' === $_GET[ $this->get_action() ] && check_admin_referer( $this->get_action() . 'continue' ) ) { |
| 86 |
$this->continue_run(); |
| 87 |
} |
| 88 |
|
| 89 |
wp_safe_redirect( remove_query_arg( [ $this->get_action(), '_wpnonce' ] ) ); |
| 90 |
die; |
| 91 |
} |
| 92 |
} |
| 93 |
|