task-queue.php
3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
* User Role Editor Pro WordPress plugin
* Author: Vladimir Garagulya
* Author email: support@role-editor.com
* Author URI: https://www.role-editor.com
* License: GPL v3
*
*/
/*
* User Role Editor's internal tasks queue
* Usage: on URE plugin activation URE adds 'on_activation' task to this queue, which fires 'ure_on_activation' action
* on the next WordPress call. It's useful when some action is needed unavailable at standard plugin activation point,
* like 'admin_menu', which is used for the admin menu access data conversion - class URE_Admin_Menu_Hashes.
* Class User_Role_Editor_Pro adds execute_once method for the 'ure_on_activation' action, where
* URE_Admin_Menu_Hashes::require_data_conversion(); method is called which registers tasks for data coversion, including
* individual tasks for every site of the multisite network
*
*/
class URE_Task_Queue {
private static $instance = null; // object exemplar reference according to singleton patern
const OPTION_NAME = 'ure_tasks_queue';
private $queue = null;
public static function get_instance() {
if (self::$instance===null) {
self::$instance = new URE_Task_Queue();
}
return self::$instance;
}
// end of get_instance()
protected function __construct() {
$this->init();
}
// end of __construct()
private function init() {
$this->queue = get_option(self::OPTION_NAME, array());
}
// end of init()
public function reinit() {
$this->init();
}
// end of reinit()
/**
*
* @param string $task_id
* @param array $args=array('action'=>'action_name', 'routine'=>'routine_name', 'priority'=>99)
*/
public function add($task_id, $args=array()) {
$this->queue[$task_id] = $args;
update_option(self::OPTION_NAME, $this->queue);
}
// end of add_task()
public function remove($task_id) {
if (isset($this->queue[$task_id])) {
unset($this->queue[$task_id]);
update_option(self::OPTION_NAME, $this->queue);
}
}
// end of remove_task()
/**
* Returns true in case a queue is empty
*
* @return boolean
*/
public function is_empty() {
return count($this->queue)==0;
}
// end of is_empty()
/**
* Consumers should add there tasks with add_method and add 'ure_fulfil_task' action routine to work on it.
* Do not forget remove task after it was fulfilled.
*
* @return void
*/
public function process() {
if ($this->is_empty()) {
return;
}
foreach($this->queue as $task_id=>$task) {
if ($task_id=='on_activation') {
do_action('ure_on_activation');
$this->remove('on_activation'); // remove this task after execution if it was defined
} elseif (!empty($task['action'])) {
$priority = empty($task['priority']) ? 10: $task['priority'];
add_action($task['action'], $task['routine'], $priority);
} else {
add_action('init', $task['routine']);
}
}
}
// end of process();
/**
* Prevent cloning of the instance of the *Singleton* instance.
*
* @return void
*/
public function __clone() {
throw new \Exception('Do not clone a singleton instance.');
}
// end of __clone()
/**
* Prevent unserializing of the *Singleton* instance.
*
* @return void
*/
public function __wakeup() {
throw new \Exception('Do not unserialize a singleton instance.');
}
// end of __wakeup()
}
// end of class URE_On_Activation