|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BulkWP\BulkDelete\Core\Base; |
|
4
|
|
|
|
|
5
|
|
|
use BulkWP\BulkDelete\Core\Base\Mixin\Renderer; |
|
6
|
|
|
|
|
7
|
1 |
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Encapsulates the Bulk Delete Meta box Module Logic. |
|
11
|
|
|
* |
|
12
|
|
|
* All Bulk Delete Meta box Modules should extend this class. |
|
13
|
|
|
* This class extends Renderer Mixin class since Bulk Delete still supports PHP 5.3. |
|
14
|
|
|
* Once PHP 5.3 support is dropped, Renderer will be implemented as a Trait and this class will `use` it. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 6.0.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class BaseModule extends Renderer { |
|
19
|
|
|
/** |
|
20
|
|
|
* Item Type. Possible values 'posts', 'pages', 'users' etc. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $item_type; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The hook suffix of the screen where this meta box would be shown. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $page_hook_suffix; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Slug of the page where this module will be shown. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $page_slug; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Slug of the meta box. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $meta_box_slug; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Action in which the delete operation should be performed. |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $action = ''; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Hook for scheduler. |
|
56
|
|
|
* |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $cron_hook; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Url of the scheduler addon. |
|
63
|
|
|
* |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $scheduler_url; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Messages shown to the user. |
|
70
|
|
|
* |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $messages = array( |
|
74
|
|
|
'box_label' => '', |
|
75
|
|
|
'cron_label' => '', |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Initialize and setup variables. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
abstract protected function initialize(); |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Render the Modules. |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
abstract public function render(); |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Process common filters. |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $request Request array. |
|
96
|
|
|
* |
|
97
|
|
|
* @return array User options. |
|
98
|
|
|
*/ |
|
99
|
|
|
abstract protected function parse_common_filters( $request ); |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Process user input and create metabox options. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $request Request array. |
|
105
|
|
|
* @param array $options User options. |
|
106
|
|
|
* |
|
107
|
|
|
* @return array User options. |
|
108
|
|
|
*/ |
|
109
|
|
|
abstract protected function convert_user_input_to_options( $request, $options ); |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Perform the deletion. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $options Array of Delete options. |
|
115
|
|
|
* |
|
116
|
|
|
* @return int Number of items that were deleted. |
|
117
|
|
|
*/ |
|
118
|
|
|
abstract protected function do_delete( $options ); |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get Success Message. |
|
122
|
|
|
* |
|
123
|
|
|
* @param int $items_deleted Number of items that were deleted. |
|
124
|
|
|
* |
|
125
|
|
|
* @return string Success message. |
|
126
|
|
|
*/ |
|
127
|
|
|
abstract protected function get_success_message( $items_deleted ); |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Create new instances of Modules. |
|
131
|
|
|
*/ |
|
132
|
374 |
|
public function __construct() { |
|
133
|
374 |
|
$this->initialize(); |
|
134
|
374 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Register. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $hook_suffix Page Hook Suffix. |
|
140
|
|
|
* @param string $page_slug Page slug. |
|
141
|
|
|
*/ |
|
142
|
7 |
|
public function register( $hook_suffix, $page_slug ) { |
|
143
|
7 |
|
$this->page_hook_suffix = $hook_suffix; |
|
144
|
7 |
|
$this->page_slug = $page_slug; |
|
145
|
|
|
|
|
146
|
7 |
|
add_action( "add_meta_boxes_{$this->page_hook_suffix}", array( $this, 'setup_metabox' ) ); |
|
147
|
|
|
|
|
148
|
7 |
|
add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) ); |
|
149
|
|
|
|
|
150
|
7 |
|
if ( ! empty( $this->action ) ) { |
|
151
|
7 |
|
add_action( 'bd_' . $this->action, array( $this, 'process' ) ); |
|
152
|
|
|
} |
|
153
|
7 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Setup the meta box. |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setup_metabox() { |
|
159
|
|
|
add_meta_box( |
|
160
|
|
|
$this->meta_box_slug, |
|
161
|
|
|
$this->messages['box_label'], |
|
162
|
|
|
array( $this, 'render_box' ), |
|
163
|
|
|
$this->page_hook_suffix, |
|
164
|
|
|
'advanced' |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Render the meta box. |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function render_box() { |
|
172
|
1 |
|
if ( $this->is_hidden() ) { |
|
173
|
|
|
printf( |
|
174
|
|
|
/* translators: 1 Module url */ |
|
175
|
|
|
__( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), |
|
176
|
|
|
'admin.php?page=' . esc_attr( $this->page_slug ) |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
return; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
1 |
|
$this->render(); |
|
183
|
1 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Is the current meta box hidden by user. |
|
187
|
|
|
* |
|
188
|
|
|
* @return bool True, if hidden. False, otherwise. |
|
189
|
|
|
*/ |
|
190
|
1 |
|
protected function is_hidden() { |
|
191
|
1 |
|
$current_user = wp_get_current_user(); |
|
192
|
1 |
|
$user_meta_field = $this->get_hidden_box_user_meta_field(); |
|
193
|
1 |
|
$hidden_boxes = get_user_meta( $current_user->ID, $user_meta_field, true ); |
|
194
|
|
|
|
|
195
|
1 |
|
return is_array( $hidden_boxes ) && in_array( $this->meta_box_slug, $hidden_boxes, true ); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get the user meta field that stores the status of the hidden meta boxes. |
|
200
|
|
|
* |
|
201
|
|
|
* @return string Name of the User Meta field. |
|
202
|
|
|
*/ |
|
203
|
1 |
|
protected function get_hidden_box_user_meta_field() { |
|
204
|
1 |
|
if ( 'posts' === $this->item_type ) { |
|
205
|
|
|
return 'metaboxhidden_toplevel_page_bulk-delete-posts'; |
|
206
|
|
|
} else { |
|
207
|
1 |
|
return 'metaboxhidden_bulk-wp_page_' . $this->page_slug; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Filter the js array. |
|
213
|
|
|
* |
|
214
|
|
|
* Use `append_to_js_array` function to append any module specific js options. |
|
215
|
|
|
* |
|
216
|
|
|
* @see $this->append_to_js_array |
|
217
|
|
|
* |
|
218
|
|
|
* @param array $js_array JavaScript Array. |
|
219
|
|
|
* |
|
220
|
|
|
* @return array Modified JavaScript Array |
|
221
|
|
|
*/ |
|
222
|
|
|
public function filter_js_array( $js_array ) { |
|
223
|
|
|
$js_array['dt_iterators'][] = '_' . $this->field_slug; |
|
224
|
|
|
|
|
225
|
|
|
$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' ); |
|
226
|
|
|
$js_array['msg']['selectPostOption'] = __( 'Please select posts from at least one option', 'bulk-delete' ); |
|
227
|
|
|
|
|
228
|
|
|
return $this->append_to_js_array( $js_array ); |
|
229
|
|
|
} |
|
230
|
1 |
|
|
|
231
|
1 |
|
/** |
|
232
|
1 |
|
* Append any module specific options to JS array. |
|
233
|
1 |
|
* |
|
234
|
|
|
* This function will be overridden by the child classes. |
|
235
|
|
|
* |
|
236
|
|
|
* @param array $js_array JavaScript Array. |
|
237
|
|
|
* |
|
238
|
|
|
* @return array Modified JavaScript Array |
|
239
|
|
|
*/ |
|
240
|
|
|
protected function append_to_js_array( $js_array ) { |
|
241
|
|
|
return $js_array; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
1 |
|
/** |
|
245
|
|
|
* Helper function for processing deletion. |
|
246
|
1 |
|
* Setups up cron and invokes the actual delete method. |
|
247
|
|
|
* |
|
248
|
|
|
* @param array $request Request array. |
|
249
|
1 |
|
*/ |
|
250
|
1 |
|
public function process( $request ) { |
|
251
|
|
|
$options = $this->parse_common_filters( $request ); |
|
252
|
|
|
$options = $this->convert_user_input_to_options( $request, $options ); |
|
253
|
1 |
|
$cron_options = $this->parse_cron_filters( $request ); |
|
254
|
1 |
|
|
|
255
|
1 |
|
/** |
|
256
|
1 |
|
* Filter the processed delete options. |
|
257
|
1 |
|
* |
|
258
|
|
|
* @since 6.0.0 |
|
259
|
1 |
|
* |
|
260
|
|
|
* @param array $options Processed options. |
|
261
|
|
|
* @param array $request Request array. |
|
262
|
|
|
* @param \BulkWP\BulkDelete\Core\Base\BaseModule The delete module. |
|
263
|
|
|
*/ |
|
264
|
|
|
$options = apply_filters( 'bd_processed_delete_options', $options, $request, $this ); |
|
265
|
|
|
|
|
266
|
|
|
if ( $this->is_scheduled( $cron_options ) ) { |
|
267
|
|
|
$msg = $this->schedule_deletion( $cron_options, $options ); |
|
268
|
299 |
|
} else { |
|
269
|
|
|
$items_deleted = $this->delete( $options ); |
|
270
|
|
|
$msg = sprintf( $this->get_success_message( $items_deleted ), $items_deleted ); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
add_settings_error( |
|
274
|
|
|
$this->page_slug, |
|
275
|
|
|
$this->action, |
|
276
|
|
|
$msg, |
|
277
|
299 |
|
'updated' |
|
278
|
|
|
); |
|
279
|
299 |
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Delete items based on delete options. |
|
283
|
|
|
* |
|
284
|
|
|
* @param array $options Delete Options. |
|
285
|
|
|
* |
|
286
|
|
|
* @return int Number of items deleted. |
|
287
|
|
|
*/ |
|
288
|
|
|
public function delete( $options ) { |
|
289
|
|
|
/** |
|
290
|
|
|
* Filter delete options before deleting items. |
|
291
|
|
|
* |
|
292
|
|
|
* @since 6.0.0 Added `Modules` parameter. |
|
293
|
|
|
* |
|
294
|
|
|
* @param array $options Delete options. |
|
295
|
|
|
* @param \BulkWP\BulkDelete\Core\Base\BaseModule Modules that is triggering deletion. |
|
296
|
|
|
*/ |
|
297
|
|
|
$options = apply_filters( 'bd_delete_options', $options, $this ); |
|
298
|
|
|
|
|
299
|
|
|
return $this->do_delete( $options ); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Getter for cron_hook. |
|
304
|
|
|
* |
|
305
|
308 |
|
* @return string Cron Hook name. |
|
306
|
308 |
|
*/ |
|
307
|
|
|
public function get_cron_hook() { |
|
308
|
|
|
return $this->cron_hook; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Getter for field slug. |
|
313
|
|
|
* |
|
314
|
|
|
* @return string Field Slug. |
|
315
|
|
|
*/ |
|
316
|
3 |
|
public function get_field_slug() { |
|
317
|
3 |
|
return $this->field_slug; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Getter for action. |
|
322
|
|
|
* |
|
323
|
|
|
* @return string Modules action. |
|
324
|
|
|
*/ |
|
325
|
|
|
public function get_action() { |
|
326
|
|
|
return $this->action; |
|
327
|
|
|
} |
|
328
|
32 |
|
|
|
329
|
32 |
|
/** |
|
330
|
8 |
|
* Is the current deletion request a scheduled request? |
|
331
|
|
|
* |
|
332
|
24 |
|
* @param array $cron_options Request object. |
|
333
|
|
|
* |
|
334
|
|
|
* @return bool True if it is a scheduled request, False otherwise. |
|
335
|
32 |
|
*/ |
|
336
|
|
|
protected function is_scheduled( $cron_options ) { |
|
337
|
|
|
return $cron_options['is_scheduled']; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Schedule Deletion of items. |
|
342
|
|
|
* |
|
343
|
32 |
|
* @param array $cron_options Cron options. |
|
344
|
32 |
|
* @param array $options Deletion option. |
|
345
|
|
|
* |
|
346
|
32 |
|
* @return string Message. |
|
347
|
32 |
|
*/ |
|
348
|
|
|
protected function schedule_deletion( $cron_options, $options ) { |
|
349
|
|
|
if ( '-1' === $cron_options['frequency'] ) { |
|
350
|
|
|
wp_schedule_single_event( $cron_options['start_time'], $this->cron_hook, array( $options ) ); |
|
351
|
|
|
} else { |
|
352
|
|
|
wp_schedule_event( $cron_options['start_time'], $cron_options['frequency'], $this->cron_hook, array( $options ) ); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
return $this->messages['scheduled'] . ' ' . $this->get_task_list_link(); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
31 |
|
/** |
|
359
|
|
|
* Get the link to the page that lists all the scheduled tasks. |
|
360
|
31 |
|
* |
|
361
|
|
|
* @return string Link to scheduled tasks page. |
|
362
|
|
|
*/ |
|
363
|
31 |
|
protected function get_task_list_link() { |
|
364
|
|
|
return sprintf( |
|
365
|
31 |
|
/* translators: 1 Cron page url */ |
|
366
|
28 |
|
__( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ), |
|
367
|
28 |
|
get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . \Bulk_Delete::CRON_PAGE_SLUG |
|
368
|
28 |
|
); |
|
369
|
|
|
} |
|
370
|
28 |
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* Parse request and create cron options. |
|
373
|
31 |
|
* |
|
374
|
|
|
* @param array $request Request array. |
|
375
|
|
|
* |
|
376
|
|
|
* @return array Parsed cron option. |
|
377
|
|
|
*/ |
|
378
|
|
|
protected function parse_cron_filters( $request ) { |
|
379
|
|
|
$cron_options = array( |
|
380
|
|
|
'is_scheduled' => false, |
|
381
|
28 |
|
); |
|
382
|
28 |
|
|
|
383
|
|
|
$scheduled = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_cron', false ); |
|
384
|
|
|
|
|
385
|
|
|
if ( $scheduled ) { |
|
386
|
|
|
$cron_options['is_scheduled'] = true; |
|
387
|
|
|
$cron_options['frequency'] = sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_freq' ] ); |
|
388
|
|
|
$cron_options['start_time'] = bd_get_gmt_offseted_time( sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_start' ] ) ); |
|
389
|
|
|
|
|
390
|
|
|
$cron_options['cron_label'] = $this->get_cron_label(); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
return $cron_options; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Get the human readable label for the Schedule job. |
|
398
|
|
|
* |
|
399
|
|
|
* @return string Human readable label for schedule job. |
|
400
|
|
|
*/ |
|
401
|
|
|
public function get_cron_label() { |
|
402
|
|
|
return $this->messages['cron_label']; |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* Get the name of the module. |
|
407
|
|
|
* |
|
408
|
|
|
* This is used as the key to identify the module from page. |
|
409
|
|
|
* |
|
410
|
|
|
* @return string Module name. |
|
411
|
|
|
*/ |
|
412
|
|
|
public function get_name() { |
|
413
|
|
|
return bd_get_short_class_name( $this ); |
|
414
|
|
|
} |
|
415
|
|
|
} |
|
416
|
|
|
|