Completed
Push — 247-fix/delete-term-meta ( bfe6ea...f6166d )
by Sudar
10:13 queued 05:33
created

BaseModule   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 443
Duplicated Lines 0 %

Test Coverage

Coverage 61.17%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 105
dl 0
loc 443
ccs 63
cts 103
cp 0.6117
rs 9.68
c 2
b 0
f 0
wmc 34

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_cron_label() 0 2 1
A get_hidden_box_user_meta_field() 0 5 2
A render_box() 0 12 2
A is_hidden() 0 6 2
A register() 0 10 2
A get_name() 0 2 1
A get_field_slug() 0 2 1
A delete() 0 12 1
A get_success_message() 0 8 3
A append_to_js_array() 0 2 1
A process() 0 28 2
A parse_cron_filters() 0 16 2
A get_cron_hook() 0 2 1
A get_action() 0 2 1
A is_scheduled() 0 2 1
A get_page_slug() 0 2 1
A schedule_deletion() 0 10 2
A setup_metabox() 0 8 2
A filter_js_array() 0 24 4
A get_task_list_link() 0 5 1
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
		'validation_error'  => '',
77
		'confirm_deletion'  => '',
78
		'confirm_scheduled' => '',
79
		'scheduled'         => '',
80
		'nothing_to_delete' => '',
81
		'deleted_one'       => '',
82
		'deleted_multiple'  => '',
83
	);
84
85
	/**
86
	 * Initialize and setup variables.
87
	 *
88
	 * @return void
89
	 */
90
	abstract protected function initialize();
91
92
	/**
93
	 * Render the Modules.
94
	 *
95
	 * @return void
96
	 */
97
	abstract public function render();
98
99
	/**
100
	 * Process common filters.
101
	 *
102
	 * @param array $request Request array.
103
	 *
104
	 * @return array User options.
105
	 */
106
	abstract protected function parse_common_filters( $request );
107
108
	/**
109
	 * Process user input and create metabox options.
110
	 *
111
	 * @param array $request Request array.
112
	 * @param array $options User options.
113
	 *
114
	 * @return array User options.
115
	 */
116
	abstract protected function convert_user_input_to_options( $request, $options );
117
118
	/**
119
	 * Perform the deletion.
120
	 *
121
	 * @param array $options Array of Delete options.
122
	 *
123
	 * @return int Number of items that were deleted.
124
	 */
125
	abstract protected function do_delete( $options );
126
127
	/**
128
	 * Create new instances of Modules.
129
	 */
130 396
	public function __construct() {
131 396
		$this->initialize();
132
	}
133
134
	/**
135
	 * Register.
136
	 *
137
	 * @param string $hook_suffix Page Hook Suffix.
138
	 * @param string $page_slug   Page slug.
139
	 */
140 9
	public function register( $hook_suffix, $page_slug ) {
141 9
		$this->page_hook_suffix = $hook_suffix;
142 9
		$this->page_slug        = $page_slug;
143
144 9
		add_action( "add_meta_boxes_{$this->page_hook_suffix}", array( $this, 'setup_metabox' ) );
145
146 9
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
147
148 9
		if ( ! empty( $this->action ) ) {
149 9
			add_action( 'bd_' . $this->action, array( $this, 'process' ) );
150
		}
151
	}
152
153
	/**
154
	 * Setup the meta box.
155
	 */
156
	public function setup_metabox() {
157
		if ( array_key_exists( 'box_label', $this->messages ) ) {
158
			add_meta_box(
159
				$this->meta_box_slug,
160
				$this->messages['box_label'],
161
				array( $this, 'render_box' ),
162
				$this->page_hook_suffix,
163
				'advanced'
164
			);
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
	}
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['pre_delete_msg'][ $this->action ] = $this->action . '_confirm_deletion';
226
		$js_array['error_msg'][ $this->action ]      = $this->action . '_error';
227
228
		$js_array['msg'][ $this->action . '_confirm_deletion' ] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' );
229
		$js_array['msg'][ $this->action . '_error' ]            = __( 'Please select posts from at least one option', 'bulk-delete' );
230
231
		if ( ! empty( $this->messages['confirm_deletion'] ) ) {
232
			$js_array['msg'][ $this->action . '_confirm_deletion' ] = $this->messages['confirm_deletion'];
233
		}
234
235
		if ( ! empty( $this->messages['confirm_scheduled'] ) ) {
236
			$js_array['pre_schedule_msg'][ $this->action ] = $this->action . '_confirm_scheduled';
237
238
			$js_array['msg'][ $this->action . '_confirm_scheduled' ] = $this->messages['confirm_scheduled'];
239
		}
240
241
		if ( ! empty( $this->messages['validation_error'] ) ) {
242
			$js_array['msg'][ $this->action . '_error' ] = $this->messages['validation_error'];
243
		}
244
245
		return $this->append_to_js_array( $js_array );
246
	}
247
248
	/**
249
	 * Append any module specific options to JS array.
250
	 *
251
	 * This function will be overridden by the child classes.
252
	 *
253
	 * @param array $js_array JavaScript Array.
254
	 *
255
	 * @return array Modified JavaScript Array
256
	 */
257
	protected function append_to_js_array( $js_array ) {
258
		return $js_array;
259
	}
260
261
	/**
262
	 * Helper function for processing deletion.
263
	 * Setups up cron and invokes the actual delete method.
264
	 *
265
	 * @param array $request Request array.
266
	 */
267 1
	public function process( $request ) {
268 1
		$options      = $this->parse_common_filters( $request );
269 1
		$options      = $this->convert_user_input_to_options( $request, $options );
270 1
		$cron_options = $this->parse_cron_filters( $request );
271
272
		/**
273
		 * Filter the processed delete options.
274
		 *
275
		 * @since 6.0.0
276
		 *
277
		 * @param array $options Processed options.
278
		 * @param array $request Request array.
279
		 * @param \BulkWP\BulkDelete\Core\Base\BaseModule The delete module.
280
		 */
281 1
		$options = apply_filters( 'bd_processed_delete_options', $options, $request, $this );
282
283 1
		if ( $this->is_scheduled( $cron_options ) ) {
284
			$msg = $this->schedule_deletion( $cron_options, $options );
285
		} else {
286 1
			$items_deleted = $this->delete( $options );
287 1
			$msg           = sprintf( $this->get_success_message( $items_deleted ), $items_deleted );
288
		}
289
290 1
		add_settings_error(
291 1
			$this->page_slug,
292 1
			$this->action,
293
			$msg,
294 1
			'updated'
295
		);
296
	}
297
298
	/**
299
	 * Delete items based on delete options.
300
	 *
301
	 * @param array $options Delete Options.
302
	 *
303
	 * @return int Number of items deleted.
304
	 */
305 315
	public function delete( $options ) {
306
		/**
307
		 * Filter delete options before deleting items.
308
		 *
309
		 * @since 6.0.0 Added `Modules` parameter.
310
		 *
311
		 * @param array $options Delete options.
312
		 * @param \BulkWP\BulkDelete\Core\Base\BaseModule Modules that is triggering deletion.
313
		 */
314 315
		$options = apply_filters( 'bd_delete_options', $options, $this );
315
316 315
		return $this->do_delete( $options );
317
	}
318
319
	/**
320
	 * Get Success Message.
321
	 *
322
	 * @param int $items_deleted Number of items that were deleted.
323
	 *
324
	 * @return string Success message.
325
	 */
326 1
	protected function get_success_message( $items_deleted ) {
327 1
		if ( 0 === $items_deleted ) {
328
			if ( ! empty( $this->messages['nothing_to_delete'] ) ) {
329
				return $this->messages['nothing_to_delete'];
330
			}
331
		}
332
333 1
		return _n( $this->messages['deleted_one'], $this->messages['deleted_multiple'], $items_deleted, 'bulk-delete' ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle, WordPress.WP.I18n.NonSingularStringLiteralPlural
334
	}
335
336
	/**
337
	 * Getter for cron_hook.
338
	 *
339
	 * @return string Cron Hook name.
340
	 */
341
	public function get_cron_hook() {
342
		return $this->cron_hook;
343
	}
344
345
	/**
346
	 * Getter for field slug.
347
	 *
348
	 * @return string Field Slug.
349
	 */
350
	public function get_field_slug() {
351
		return $this->field_slug;
352
	}
353
354
	/**
355
	 * Getter for action.
356
	 *
357
	 * @return string Modules action.
358
	 */
359 325
	public function get_action() {
360 325
		return $this->action;
361
	}
362
363
	/**
364
	 * Is the current deletion request a scheduled request?
365
	 *
366
	 * @param array $cron_options Request object.
367
	 *
368
	 * @return bool True if it is a scheduled request, False otherwise.
369
	 */
370 3
	protected function is_scheduled( $cron_options ) {
371 3
		return $cron_options['is_scheduled'];
372
	}
373
374
	/**
375
	 * Schedule Deletion of items.
376
	 *
377
	 * @param array $cron_options Cron options.
378
	 * @param array $options      Deletion option.
379
	 *
380
	 * @return string Message.
381
	 */
382 32
	protected function schedule_deletion( $cron_options, $options ) {
383 32
		$options['cron_label'] = $cron_options['cron_label'];
384
385 32
		if ( '-1' === $cron_options['frequency'] ) {
386 8
			wp_schedule_single_event( $cron_options['start_time'], $this->cron_hook, array( $options ) );
387
		} else {
388 24
			wp_schedule_event( $cron_options['start_time'], $cron_options['frequency'], $this->cron_hook, array( $options ) );
389
		}
390
391 32
		return $this->messages['scheduled'] . ' ' . $this->get_task_list_link();
392
	}
393
394
	/**
395
	 * Get the link to the page that lists all the scheduled tasks.
396
	 *
397
	 * @return string Link to scheduled tasks page.
398
	 */
399 32
	protected function get_task_list_link() {
400 32
		return sprintf(
401
			/* translators: 1 Cron page url */
402 32
			__( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ),
403 32
			get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . \Bulk_Delete::CRON_PAGE_SLUG
404
		);
405
	}
406
407
	/**
408
	 * Parse request and create cron options.
409
	 *
410
	 * @param array $request Request array.
411
	 *
412
	 * @return array Parsed cron option.
413
	 */
414 31
	protected function parse_cron_filters( $request ) {
415
		$cron_options = array(
416 31
			'is_scheduled' => false,
417
		);
418
419 31
		$scheduled = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_cron', false );
420
421 31
		if ( $scheduled ) {
422 28
			$cron_options['is_scheduled'] = true;
423 28
			$cron_options['frequency']    = sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_freq' ] );
424 28
			$cron_options['start_time']   = bd_get_gmt_offseted_time( sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_start' ] ) );
425
426 28
			$cron_options['cron_label'] = $this->get_cron_label();
427
		}
428
429 31
		return $cron_options;
430
	}
431
432
	/**
433
	 * Get the human readable label for the Schedule job.
434
	 *
435
	 * @return string Human readable label for schedule job.
436
	 */
437 28
	public function get_cron_label() {
438 28
		return $this->messages['cron_label'];
439
	}
440
441
	/**
442
	 * Get the name of the module.
443
	 *
444
	 * This is used as the key to identify the module from page.
445
	 *
446
	 * @return string Module name.
447
	 */
448
	public function get_name() {
449
		return bd_get_short_class_name( $this );
450
	}
451
452
	/**
453
	 * Get the page slug of the module.
454
	 *
455
	 * @since 6.0.1
456
	 *
457
	 * @return string Page slug.
458
	 */
459
	public function get_page_slug() {
460
		return $this->page_slug;
461
	}
462
}
463