Completed
Push — 741-fix/return-success-message ( 864d78...7cad88 )
by
unknown
82:23 queued 79:18
created

BaseModule::get_field_slug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
	 * List of legacy cron hooks that this module should support.
63
	 *
64
	 * Legacy cron hooks usually come from older version of the add-on(s).
65
	 *
66
	 * @since 6.1.0
67
	 *
68
	 * @var array
69
	 */
70
	protected $legacy_cron_hooks = [];
71
72
	/**
73
	 * Notify the user about success/error message.
74
	 *
75
	 * @since 6.1.0
76
	 *
77
	 * @var bool
78
	 */
79
	protected $enable_notification = false;
80
	/**
81
	 * Url of the scheduler addon.
82
	 *
83
	 * @var string
84
	 */
85
	protected $scheduler_url;
86
87
	/**
88
	 * Messages shown to the user.
89
	 *
90
	 * @var array
91
	 */
92
	protected $messages = array(
93
		'box_label'         => '',
94
		'cron_label'        => '',
95
		'validation_error'  => '',
96
		'confirm_deletion'  => '',
97
		'confirm_scheduled' => '',
98
		'scheduled'         => '',
99
		'nothing_to_delete' => '',
100
		'deleted_one'       => '',
101
		'deleted_multiple'  => '',
102
	);
103
104
	/**
105
	 * Initialize and setup variables.
106
	 *
107
	 * @return void
108
	 */
109
	abstract protected function initialize();
110
111
	/**
112
	 * Render the Modules.
113
	 *
114
	 * @return void
115
	 */
116
	abstract public function render();
117
118
	/**
119
	 * Process common filters.
120
	 *
121
	 * @param array $request Request array.
122
	 *
123
	 * @return array User options.
124
	 */
125
	abstract protected function parse_common_filters( $request );
126
127
	/**
128
	 * Process user input and create metabox options.
129
	 *
130
	 * @param array $request Request array.
131
	 * @param array $options User options.
132
	 *
133
	 * @return array User options.
134
	 */
135
	abstract protected function convert_user_input_to_options( $request, $options );
136
137
	/**
138
	 * Perform the deletion.
139
	 *
140
	 * @param array $options Array of Delete options.
141
	 *
142
	 * @return int Number of items that were deleted.
143
	 */
144
	abstract protected function do_delete( $options );
145
146
	/**
147
	 * Create new instances of Modules.
148
	 */
149 413
	public function __construct() {
150 413
		$this->initialize();
151
	}
152
153
	/**
154
	 * Register.
155
	 *
156
	 * @param string $hook_suffix Page Hook Suffix.
157
	 * @param string $page_slug   Page slug.
158
	 */
159 24
	public function register( $hook_suffix, $page_slug ) {
160 24
		$this->page_hook_suffix = $hook_suffix;
161 24
		$this->page_slug        = $page_slug;
162
163 24
		add_action( "add_meta_boxes_{$this->page_hook_suffix}", array( $this, 'setup_metabox' ) );
164
165 24
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
166
167 24
		if ( ! empty( $this->action ) ) {
168 24
			add_action( 'bd_' . $this->action, array( $this, 'process' ) );
169
		}
170
	}
171
172
	/**
173
	 * Setup the meta box.
174
	 */
175
	public function setup_metabox() {
176
		if ( array_key_exists( 'box_label', $this->messages ) ) {
177
			add_meta_box(
178
				$this->meta_box_slug,
179
				$this->messages['box_label'],
180
				array( $this, 'render_box' ),
181
				$this->page_hook_suffix,
182
				'advanced'
183
			);
184
		}
185
	}
186
187
	/**
188
	 * Render the meta box.
189
	 */
190 1
	public function render_box() {
191 1
		if ( $this->is_hidden() ) {
192
			printf(
193
				/* translators: 1 Module url */
194
				__( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ),
195
				'admin.php?page=' . esc_attr( $this->page_slug )
196
			);
197
198
			return;
199
		}
200
201 1
		$this->render();
202
	}
203
204
	/**
205
	 * Is the current meta box hidden by user.
206
	 *
207
	 * @return bool True, if hidden. False, otherwise.
208
	 */
209 1
	protected function is_hidden() {
210 1
		$current_user    = wp_get_current_user();
211 1
		$user_meta_field = $this->get_hidden_box_user_meta_field();
212 1
		$hidden_boxes    = get_user_meta( $current_user->ID, $user_meta_field, true );
213
214 1
		return is_array( $hidden_boxes ) && in_array( $this->meta_box_slug, $hidden_boxes, true );
215
	}
216
217
	/**
218
	 * Get the user meta field that stores the status of the hidden meta boxes.
219
	 *
220
	 * @return string Name of the User Meta field.
221
	 */
222 1
	protected function get_hidden_box_user_meta_field() {
223 1
		if ( 'posts' === $this->item_type ) {
224
			return 'metaboxhidden_toplevel_page_bulk-delete-posts';
225
		} else {
226 1
			return 'metaboxhidden_bulk-wp_page_' . $this->page_slug;
227
		}
228
	}
229
230
	/**
231
	 * Filter the js array.
232
	 *
233
	 * Use `append_to_js_array` function to append any module specific js options.
234
	 *
235
	 * @see $this->append_to_js_array
236
	 *
237
	 * @param array $js_array JavaScript Array.
238
	 *
239
	 * @return array Modified JavaScript Array
240
	 */
241
	public function filter_js_array( $js_array ) {
242
		$js_array['dt_iterators'][] = '_' . $this->field_slug;
243
244
		$js_array['pre_delete_msg'][ $this->action ] = $this->action . '_confirm_deletion';
245
		$js_array['error_msg'][ $this->action ]      = $this->action . '_error';
246
247
		$js_array['msg'][ $this->action . '_confirm_deletion' ] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' );
248
		$js_array['msg'][ $this->action . '_error' ]            = __( 'Please select posts from at least one option', 'bulk-delete' );
249
250
		if ( ! empty( $this->messages['confirm_deletion'] ) ) {
251
			$js_array['msg'][ $this->action . '_confirm_deletion' ] = $this->messages['confirm_deletion'];
252
		}
253
254
		if ( ! empty( $this->messages['confirm_scheduled'] ) ) {
255
			$js_array['pre_schedule_msg'][ $this->action ] = $this->action . '_confirm_scheduled';
256
257
			$js_array['msg'][ $this->action . '_confirm_scheduled' ] = $this->messages['confirm_scheduled'];
258
		}
259
260
		if ( ! empty( $this->messages['validation_error'] ) ) {
261
			$js_array['msg'][ $this->action . '_error' ] = $this->messages['validation_error'];
262
		}
263
264
		return $this->append_to_js_array( $js_array );
265
	}
266
267
	/**
268
	 * Append any module specific options to JS array.
269
	 *
270
	 * This function will be overridden by the child classes.
271
	 *
272
	 * @param array $js_array JavaScript Array.
273
	 *
274
	 * @return array Modified JavaScript Array
275
	 */
276
	protected function append_to_js_array( $js_array ) {
277
		return $js_array;
278
	}
279
280
	/**
281
	 * Helper function for processing deletion.
282
	 * Setups up cron and invokes the actual delete method.
283
	 *
284
	 * @param array $request Request array.
285
	 */
286 1
	public function process( $request ) {
287 1
		$options      = $this->parse_common_filters( $request );
288 1
		$options      = $this->convert_user_input_to_options( $request, $options );
289 1
		$cron_options = $this->parse_cron_filters( $request );
290
291
		/**
292
		 * Filter the processed delete options.
293
		 *
294
		 * @since 6.0.0
295
		 *
296
		 * @param array $options Processed options.
297
		 * @param array $request Request array.
298
		 * @param \BulkWP\BulkDelete\Core\Base\BaseModule The delete module.
299
		 */
300 1
		$options = apply_filters( 'bd_processed_delete_options', $options, $request, $this );
301
302 1
		if ( $this->is_scheduled( $cron_options ) ) {
303
			$msg = $this->schedule_deletion( $cron_options, $options );
304
		} else {
305 1
			$items_deleted = $this->delete( $options );
306 1
			$msg           = sprintf( $this->get_success_message( $items_deleted ), $items_deleted );
307
		}
308
309 1
		add_settings_error(
310 1
			$this->page_slug,
311 1
			$this->action,
312
			$msg,
313 1
			'updated'
314
		);
315
	}
316
317
	/**
318
	 * Delete items based on delete options.
319
	 *
320
	 * @param array $options Delete Options.
321
	 *
322
	 * @return int Number of items deleted.
323
	 */
324 330
	public function delete( $options ) {
325
		/**
326
		 * Filter delete options before deleting items.
327
		 *
328
		 * @since 6.0.0 Added `Modules` parameter.
329
		 *
330
		 * @param array $options Delete options.
331
		 * @param \BulkWP\BulkDelete\Core\Base\BaseModule Modules that is triggering deletion.
332
		 */
333 330
		$options = apply_filters( 'bd_delete_options', $options, $this );
334
335 330
		return $this->do_delete( $options );
336
	}
337
338
	/**
339
	 * Get Success Message.
340
	 *
341
	 * @param int $items_deleted Number of items that were deleted.
342
	 *
343
	 * @return string Success message.
344
	 */
345 1
	protected function get_success_message( $items_deleted ) {
346 1
		if ( $this->enable_notification ) {
347
			return $items_deleted;
348
		}
349 1
		if ( 0 === $items_deleted ) {
350
			if ( ! empty( $this->messages['nothing_to_delete'] ) ) {
351
				return $this->messages['nothing_to_delete'];
352
			}
353
		}
354
355 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
356
	}
357
358
	/**
359
	 * Getter for cron_hook.
360
	 *
361
	 * @return string Cron Hook name.
362
	 */
363
	public function get_cron_hook() {
364
		return $this->cron_hook;
365
	}
366
367
	/**
368
	 * Getter for legacy cron_hooks.
369
	 *
370
	 * @since 6.1.0
371
	 *
372
	 * @return array Legacy cron Hooks.
373
	 */
374
	public function get_legacy_cron_hooks() {
375
		return $this->legacy_cron_hooks;
376
	}
377
378
	/**
379
	 * Getter for field slug.
380
	 *
381
	 * @return string Field Slug.
382
	 */
383
	public function get_field_slug() {
384
		return $this->field_slug;
385
	}
386
387
	/**
388
	 * Getter for action.
389
	 *
390
	 * @return string Modules action.
391
	 */
392 340
	public function get_action() {
393 340
		return $this->action;
394
	}
395
396
	/**
397
	 * Is the current deletion request a scheduled request?
398
	 *
399
	 * @param array $cron_options Request object.
400
	 *
401
	 * @return bool True if it is a scheduled request, False otherwise.
402
	 */
403 3
	protected function is_scheduled( $cron_options ) {
404 3
		return $cron_options['is_scheduled'];
405
	}
406
407
	/**
408
	 * Schedule Deletion of items.
409
	 *
410
	 * @param array $cron_options Cron options.
411
	 * @param array $options      Deletion option.
412
	 *
413
	 * @return string Message.
414
	 */
415 32
	protected function schedule_deletion( $cron_options, $options ) {
416 32
		$options['cron_label'] = $cron_options['cron_label'];
417
418 32
		if ( '-1' === $cron_options['frequency'] ) {
419 8
			wp_schedule_single_event( $cron_options['start_time'], $this->cron_hook, array( $options ) );
420
		} else {
421 24
			wp_schedule_event( $cron_options['start_time'], $cron_options['frequency'], $this->cron_hook, array( $options ) );
422
		}
423
424 32
		return $this->messages['scheduled'] . ' ' . $this->get_task_list_link();
425
	}
426
427
	/**
428
	 * Get the link to the page that lists all the scheduled tasks.
429
	 *
430
	 * @return string Link to scheduled tasks page.
431
	 */
432 32
	protected function get_task_list_link() {
433 32
		return sprintf(
434
			/* translators: 1 Cron page url */
435 32
			__( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ),
436 32
			get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . \Bulk_Delete::CRON_PAGE_SLUG
437
		);
438
	}
439
440
	/**
441
	 * Parse request and create cron options.
442
	 *
443
	 * @param array $request Request array.
444
	 *
445
	 * @return array Parsed cron option.
446
	 */
447 31
	protected function parse_cron_filters( $request ) {
448
		$cron_options = array(
449 31
			'is_scheduled' => false,
450
		);
451
452 31
		$scheduled = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_cron', false );
453
454 31
		if ( $scheduled ) {
455 28
			$cron_options['is_scheduled'] = true;
456 28
			$cron_options['frequency']    = sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_freq' ] );
457 28
			$cron_options['start_time']   = bd_get_gmt_offseted_time( sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_start' ] ) );
458
459 28
			$cron_options['cron_label'] = $this->get_cron_label();
460
		}
461
462 31
		return $cron_options;
463
	}
464
465
	/**
466
	 * Get the human readable label for the Schedule job.
467
	 *
468
	 * @return string Human readable label for schedule job.
469
	 */
470 28
	public function get_cron_label() {
471 28
		return $this->messages['cron_label'];
472
	}
473
474
	/**
475
	 * Get the name of the module.
476
	 *
477
	 * This is used as the key to identify the module from page.
478
	 *
479
	 * @return string Module name.
480
	 */
481
	public function get_name() {
482
		return bd_get_short_class_name( $this );
483
	}
484
485
	/**
486
	 * Get the page slug of the module.
487
	 *
488
	 * @since 6.0.1
489
	 *
490
	 * @return string Page slug.
491
	 */
492
	public function get_page_slug() {
493
		return $this->page_slug;
494
	}
495
}
496