BatchProcess.php
5.56 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Class NF_Abstracts_Batch_Process
*/
abstract class NF_Abstracts_BatchProcess
{
protected $_db;
protected $_flag;
/**
* Array that holds data we're sending back to the JS front-end.
* @var array
*/
protected $response = array(
'batch_complete' => false
);
/**
* Constructor
*/
public function __construct( $data = array() )
{
//Bail if we aren't in the admin.
if ( ! is_admin() ||
! is_user_logged_in() ||
! current_user_can( apply_filters('ninja_forms_admin_all_forms_capabilities', 'manage_options') ) )
{
return false;
}
global $wpdb;
/**
* Set $_db to $wpdb.
* This helps us by not requiring us to declare global $wpdb in every class method.
*/
$this->_db = $wpdb;
// Run init.
$this->init();
}
/**
* Decides whether we need to run startup or restart and then calls processing.
*
* @since 3.4.0
* @return void
*/
public function init()
{
$this->_flag = 'nf_doing_' . $this->_slug;
if ( ! $this->flag( $this->_flag, 'check' ) ) {
// Run the startup process.
$this->startup();
} else {
// Otherwise... (We've already run startup.)
$this->restart();
}
// Determine how many steps this will take.
$this->response[ 'step_total' ] = $this->get_steps();
$this->flag( $this->_flag, 'add' );
// Run processing
$this->process();
}
/**
* Function to loop over the batch.
*
* @since 3.4.0
* @return void
*/
public function process()
{
/**
* This function intentionlly left empty.
*/
}
/**
* Function to run any setup steps necessary to begin processing.
*
* @since 3.4.0
* @return void
*/
public function startup()
{
/**
* This function intentionally left empty.
*/
}
/**
* Function to run any setup steps necessary to begin processing for steps after the first.
*
* @since 3.4.0
* @return void
*/
public function restart()
{
/**
* This function intentionally left empty.
*/
}
/**
* Returns how many steps we have in this process.
*
* If this method isn't overwritten by a child, it defaults to 1.
*
* @since 3.4.0
* @return int
*/
public function get_steps()
{
return 1;
}
/**
* Adds an error to the response object.
*
* @param $slug (String) The slug for this error code.
* @param $msg (String) The error message to be displayed.
* @param $type (String) warning or fatal, depending on the error.
* Defaults to warning.
*
* @since 3.4.11
*/
public function add_error( $slug, $msg, $type = 'warning' )
{
// Setup our errors array if it doesn't exist already.
if ( ! isset( $this->response[ 'errors' ] ) ) {
$this->response[ 'errors' ] = array();
}
$this->response[ 'errors' ][] = array(
'code' => $slug,
'message' => $msg,
'type' => $type
);
}
/**
* Function to cleanup any lingering temporary elements of a batch process after completion.
*
* @since 3.4.0
* @return void
*/
public function cleanup()
{
/**
* This function intentionally left empty.
*/
}
/**
* Method called when we are finished with this process.
*
* Deletes our "doing" option.
* Set's our response 'batch_complete' to true.
* Runs cleanup().
* Responds to the JS front-end.
*
* @since 3.4.0
* @return void
*/
public function batch_complete()
{
// Delete our options.
$this->flag( $this->_flag, 'remove' );
// Tell our JS that we're done.
$this->response[ 'batch_complete' ] = true;
$this->cleanup();
$this->respond();
}
/**
* Method that immediately moves on to the next step.
*
* Used in child methods to stop processing the current step an dmove to the next.
*
* @since 3.4.0
* @return void
*/
public function next_step()
{
// ..see how many steps we have left, update our option, and send the remaining step to the JS.
$this->response[ 'step_remaining' ] = $this->get_steps();
$this->respond();
}
/**
* Method that encodes $this->response and sends the data to the front-end.
*
* @since 3.4.0
* @updated 3.4.11
* @return void
*/
public function respond()
{
if ( ! empty( $this->response[ 'errors' ] ) ) {
$this->response[ 'errors' ] = array_unique( $this->response[ 'errors' ] );
}
echo wp_json_encode( $this->response );
wp_die();
}
/**
* Method to check or modify our processor flag.
*
* @since 3.5.0
* @param $flag (String) The flag to check
* @param $action (String) The type of interaction to be performed
* @return Mixed
*/
public function flag( $flag, $action )
{
switch($action) {
case 'add':
return add_option($flag, true);
case 'remove':
return delete_option($flag);
default:
// Default to 'check'.
return get_option($flag);
}
}
}