custom-tasks-manager.php
4 years ago
custom-tasks.php
4 years ago
elementor-3-re-migrate-globals.php
4 years ago
manager.php
4 years ago
task.php
4 years ago
updater.php
6 years ago
upgrade-utils.php
6 years ago
upgrades.php
4 years ago
custom-tasks-manager.php
112 lines
| 1 | <?php |
| 2 | namespace Elementor\Core\Upgrade; |
| 3 | |
| 4 | use Elementor\Core\Base\Background_Task_Manager; |
| 5 | use Elementor\Plugin; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; // Exit if accessed directly |
| 9 | } |
| 10 | |
| 11 | class Custom_Tasks_Manager extends Background_Task_Manager { |
| 12 | const TASKS_OPTION_KEY = 'elementor_custom_tasks'; |
| 13 | |
| 14 | const QUERY_LIMIT = 100; |
| 15 | |
| 16 | public function get_name() { |
| 17 | return 'custom-task-manager'; |
| 18 | } |
| 19 | |
| 20 | public function get_action() { |
| 21 | return 'custom_task_manger'; |
| 22 | } |
| 23 | |
| 24 | public function get_plugin_name() { |
| 25 | return 'elementor'; |
| 26 | } |
| 27 | |
| 28 | public function get_plugin_label() { |
| 29 | return esc_html__( 'Elementor', 'elementor' ); |
| 30 | } |
| 31 | |
| 32 | public function get_task_runner_class() { |
| 33 | return Task::class; |
| 34 | } |
| 35 | |
| 36 | public function get_query_limit() { |
| 37 | return self::QUERY_LIMIT; |
| 38 | } |
| 39 | |
| 40 | protected function start_run() { |
| 41 | $custom_tasks_callbacks = $this->get_custom_tasks(); |
| 42 | |
| 43 | if ( empty( $custom_tasks_callbacks ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $task_runner = $this->get_task_runner(); |
| 48 | |
| 49 | foreach ( $custom_tasks_callbacks as $callback ) { |
| 50 | $task_runner->push_to_queue( [ |
| 51 | 'callback' => $callback, |
| 52 | ] ); |
| 53 | } |
| 54 | |
| 55 | $this->clear_tasks_requested_to_run(); |
| 56 | |
| 57 | Plugin::$instance->logger->get_logger()->info( 'Elementor custom task(s) process has been queued.', [ |
| 58 | 'meta' => [ $custom_tasks_callbacks ], |
| 59 | ] ); |
| 60 | |
| 61 | $task_runner->save()->dispatch(); |
| 62 | } |
| 63 | |
| 64 | public function get_tasks_class() { |
| 65 | return Custom_Tasks::class; |
| 66 | } |
| 67 | |
| 68 | public function get_tasks_requested_to_run() { |
| 69 | return get_option( self::TASKS_OPTION_KEY, [] ); |
| 70 | } |
| 71 | |
| 72 | public function clear_tasks_requested_to_run() { |
| 73 | return update_option( self::TASKS_OPTION_KEY, [], false ); |
| 74 | } |
| 75 | |
| 76 | public function add_tasks_requested_to_run( $tasks = [] ) { |
| 77 | $current_tasks = $this->get_tasks_requested_to_run(); |
| 78 | $current_tasks = array_merge( $current_tasks, $tasks ); |
| 79 | |
| 80 | update_option( self::TASKS_OPTION_KEY, $current_tasks, false ); |
| 81 | } |
| 82 | |
| 83 | private function get_custom_tasks() { |
| 84 | $tasks_requested_to_run = $this->get_tasks_requested_to_run(); |
| 85 | |
| 86 | $tasks_class = $this->get_tasks_class(); |
| 87 | $tasks_reflection = new \ReflectionClass( $tasks_class ); |
| 88 | |
| 89 | $callbacks = []; |
| 90 | |
| 91 | foreach ( $tasks_reflection->getMethods() as $method ) { |
| 92 | $method_name = $method->getName(); |
| 93 | |
| 94 | if ( in_array( $method_name, $tasks_requested_to_run, true ) ) { |
| 95 | $callbacks[] = [ $tasks_class, $method_name ]; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return $callbacks; |
| 100 | } |
| 101 | |
| 102 | public function __construct() { |
| 103 | $task_runner = $this->get_task_runner(); |
| 104 | |
| 105 | if ( $task_runner->is_running() ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | $this->start_run(); |
| 110 | } |
| 111 | } |
| 112 |