Completed
Push — 383-fix/add-tests-for-comparis... ( a2ea6f...61deeb )
by Sudar
42:36 queued 27:33
created

BaseModule::get_enhanced_select_threshold()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 2
cp 0
crap 2
rs 10
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 for the form fields.
42
	 *
43
	 * @var string
44
	 */
45
	protected $field_slug;
46
47
	/**
48
	 * Slug of the meta box.
49
	 *
50
	 * @var string
51
	 */
52
	protected $meta_box_slug;
53
54
	/**
55
	 * Action in which the delete operation should be performed.
56
	 *
57
	 * @var string
58
	 */
59
	protected $action = '';
60
61
	/**
62
	 * Hook for scheduler.
63
	 *
64
	 * @var string
65
	 */
66
	protected $cron_hook;
67
68
	/**
69
	 * Url of the scheduler addon.
70
	 *
71
	 * @var string
72
	 */
73
	protected $scheduler_url;
74
75
	/**
76
	 * Messages shown to the user.
77
	 *
78
	 * @var array
79
	 */
80
	protected $messages = array(
81
		'box_label'  => '',
82
		'cron_label' => '',
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
	 * Get Success Message.
129
	 *
130
	 * @param int $items_deleted Number of items that were deleted.
131
	 *
132
	 * @return string Success message.
133
	 */
134
	abstract protected function get_success_message( $items_deleted );
135
136
	/**
137
	 * Create new instances of Modules.
138
	 */
139 152
	public function __construct() {
140 152
		$this->initialize();
141 152
	}
142
143
	/**
144
	 * Register.
145
	 *
146
	 * @param string $hook_suffix Page Hook Suffix.
147
	 * @param string $page_slug   Page slug.
148
	 */
149
	public function register( $hook_suffix, $page_slug ) {
150
		$this->page_hook_suffix = $hook_suffix;
151
		$this->page_slug        = $page_slug;
152
153
		add_action( "add_meta_boxes_{$this->page_hook_suffix}", array( $this, 'setup_metabox' ) );
154
155
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
156
157
		if ( ! empty( $this->action ) ) {
158
			add_action( 'bd_' . $this->action, array( $this, 'process' ) );
159
		}
160
	}
161
162
	/**
163
	 * Setup the meta box.
164
	 */
165
	public function setup_metabox() {
166
		add_meta_box(
167
			$this->meta_box_slug,
168
			$this->messages['box_label'],
169
			array( $this, 'render_box' ),
170
			$this->page_hook_suffix,
171
			'advanced'
172
		);
173
	}
174
175
	/**
176
	 * Render the meta box.
177
	 */
178 1
	public function render_box() {
179 1
		if ( $this->is_hidden() ) {
180
			printf(
181
				/* translators: 1 module url */
182
				__( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ),
183
				'admin.php?page=' . $this->page_slug
184
			);
185
186
			return;
187
		}
188
189 1
		$this->render();
190 1
	}
191
192
	/**
193
	 * Is the current meta box hidden by user.
194
	 *
195
	 * @return bool True, if hidden. False, otherwise.
196
	 */
197 1
	protected function is_hidden() {
198 1
		$current_user    = wp_get_current_user();
199 1
		$user_meta_field = $this->get_hidden_box_user_meta_field();
200 1
		$hidden_boxes    = get_user_meta( $current_user->ID, $user_meta_field, true );
201
202 1
		return is_array( $hidden_boxes ) && in_array( $this->meta_box_slug, $hidden_boxes, true );
203
	}
204
205
	/**
206
	 * Get the user meta field that stores the status of the hidden meta boxes.
207
	 *
208
	 * @return string Name of the User Meta field.
209
	 */
210 1
	protected function get_hidden_box_user_meta_field() {
211 1
		if ( 'posts' === $this->item_type ) {
212
			return 'metaboxhidden_toplevel_page_bulk-delete-posts';
213
		} else {
214 1
			return 'metaboxhidden_bulk-wp_page_' . $this->page_slug;
215
		}
216
	}
217
218
	/**
219
	 * Filter the js array.
220
	 *
221
	 * This function will be overridden by the child classes.
222
	 *
223
	 * @param array $js_array JavaScript Array.
224
	 *
225
	 * @return array Modified JavaScript Array
226
	 */
227
	public function filter_js_array( $js_array ) {
228
		return $js_array;
229
	}
230
231
	/**
232
	 * Render filtering table header.
233
	 */
234
	protected function render_filtering_table_header() {
235
		bd_render_filtering_table_header();
236
	}
237
238
	/**
239
	 * Render restrict settings.
240
	 */
241
	protected function render_restrict_settings() {
242
		bd_render_restrict_settings( $this->field_slug, $this->item_type );
243
	}
244
245
	/**
246
	 * Render delete settings.
247
	 */
248
	protected function render_delete_settings() {
249
		bd_render_delete_settings( $this->field_slug );
250
	}
251
252
	/**
253
	 * Render sticky settings.
254
	 */
255
	protected function render_sticky_settings() {
256
		bd_render_sticky_settings( $this->field_slug );
257
	}
258
259
	/**
260
	 * Render limit settings.
261
	 */
262
	protected function render_limit_settings() {
263
		bd_render_limit_settings( $this->field_slug, $this->item_type );
264
	}
265
266
	/**
267
	 * Render cron settings based on whether scheduler is present or not.
268
	 */
269
	protected function render_cron_settings() {
270
		$disabled_attr = 'disabled';
271
		if ( empty( $this->scheduler_url ) ) {
272
			$disabled_attr = '';
273
		}
274
		?>
275
276
		<tr>
277
			<td scope="row" colspan="2">
278
				<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="false" type="radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?>
279
				<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="true" type="radio" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" <?php echo esc_attr( $disabled_attr ); ?>> <?php _e( 'Schedule', 'bulk-delete' ); ?>
280
				<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start" value="now" type="text" <?php echo esc_attr( $disabled_attr ); ?>><?php _e( 'repeat ', 'bulk-delete' ); ?>
281
282
				<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" <?php echo esc_attr( $disabled_attr ); ?>>
283
					<option value="-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option>
284
285
					<?php
286
					/**
287
					 * List of cron schedules.
288
					 *
289
					 * @since 6.0.0
290
					 *
291
					 * @param array                                   $cron_schedules List of cron schedules.
292
					 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module         Module.
293
					 */
294
					$cron_schedules = apply_filters( 'bd_cron_schedules', wp_get_schedules(), $this );
295
					?>
296
297
					<?php foreach ( $cron_schedules as $key => $value ) : ?>
298
						<option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value['display'] ); ?></option>
299
					<?php endforeach; ?>
300
				</select>
301
302
				<?php if ( ! empty( $this->scheduler_url ) ) : ?>
303
					<?php
304
					$pro_class = 'bd-' . str_replace( '_', '-', $this->field_slug ) . '-pro';
305
306
					/**
307
					 * HTML class of the span that displays the 'Pro only feature' message.
308
					 *
309
					 * @since 6.0.0
310
					 *
311
					 * @param string                                  $pro_class  HTML class.
312
					 * @param string                                  $field_slug Field Slug of module.
313
					 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module     Module.
314
					 */
315
					apply_filters( 'bd_pro_only_feature_class', $pro_class, $this->field_slug, $this )
316
					?>
317
318
					<span class="<?php echo sanitize_html_class( $pro_class ); ?>" style="color:red">
319
						<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href="<?php echo esc_url( $this->scheduler_url ); ?>">Buy now</a>
320
					</span>
321
				<?php endif; ?>
322
			</td>
323
		</tr>
324
325
		<tr>
326
			<td scope="row" colspan="2">
327
				<?php _e( 'Enter time in <strong>Y-m-d H:i:s</strong> format or enter <strong>now</strong> to use current time', 'bulk-delete' ); ?>
328
			</td>
329
		</tr>
330
		<?php
331
	}
332
333
	/**
334
	 * Render submit button.
335
	 */
336
	protected function render_submit_button() {
337
		bd_render_submit_button( $this->action );
338
	}
339
340
	/**
341
	 * Helper function for processing deletion.
342
	 * Setups up cron and invokes the actual delete method.
343
	 *
344
	 * @param array $request Request array.
345
	 */
346 1
	public function process( $request ) {
347 1
		$options      = $this->parse_common_filters( $request );
348 1
		$options      = $this->convert_user_input_to_options( $request, $options );
349 1
		$cron_options = $this->parse_cron_filters( $request );
350
351 1
		if ( $this->is_scheduled( $cron_options ) ) {
352
			$msg = $this->schedule_deletion( $cron_options, $options );
353
		} else {
354 1
			$items_deleted = $this->delete( $options );
355 1
			$msg           = sprintf( $this->get_success_message( $items_deleted ), $items_deleted );
356
		}
357
358 1
		add_settings_error(
359 1
			$this->page_slug,
360 1
			$this->action,
361 1
			$msg,
362 1
			'updated'
363
		);
364 1
	}
365
366
	/**
367
	 * Delete items based on delete options.
368
	 *
369
	 * @param array $options Delete Options.
370
	 *
371
	 * @return int Number of items deleted.
372
	 */
373 77
	public function delete( $options ) {
374
		/**
375
		 * Filter delete options before deleting items.
376
		 *
377
		 * @since 6.0.0 Added `Modules` parameter.
378
		 *
379
		 * @param array $options Delete options.
380
		 * @param \BulkWP\BulkDelete\Core\Base\BaseModule Modules that is triggering deletion.
381
		 */
382 77
		$options = apply_filters( 'bd_delete_options', $options, $this );
383
384 77
		return $this->do_delete( $options );
385
	}
386
387
	/**
388
	 * Getter for cron_hook.
389
	 *
390
	 * @return string Cron Hook name.
391
	 */
392
	public function get_cron_hook() {
393
		return $this->cron_hook;
394
	}
395
396
	/**
397
	 * Getter for field slug.
398
	 *
399
	 * @return string Field Slug.
400
	 */
401
	public function get_field_slug() {
402
		return $this->field_slug;
403
	}
404
405
	/**
406
	 * Getter for action.
407
	 *
408
	 * @return string Modules action.
409
	 */
410 86
	public function get_action() {
411 86
		return $this->action;
412
	}
413
414
	/**
415
	 * Is the current deletion request a scheduled request?
416
	 *
417
	 * @param array $cron_options Request object.
418
	 *
419
	 * @return bool True if it is a scheduled request, False otherwise.
420
	 */
421 3
	protected function is_scheduled( $cron_options ) {
422 3
		return $cron_options['is_scheduled'];
423
	}
424
425
	/**
426
	 * Schedule Deletion of items.
427
	 *
428
	 * @param array $cron_options Cron options.
429
	 * @param array $options      Deletion option.
430
	 *
431
	 * @return string Message.
432
	 */
433 32
	protected function schedule_deletion( $cron_options, $options ) {
434 32
		if ( '-1' === $cron_options['frequency'] ) {
435 8
			wp_schedule_single_event( $cron_options['start_time'], $this->cron_hook, array( $options ) );
436
		} else {
437 24
			wp_schedule_event( $cron_options['start_time'], $cron_options['frequency'], $this->cron_hook, array( $options ) );
438
		}
439
440 32
		return $this->messages['scheduled'] . ' ' . $this->get_task_list_link();
441
	}
442
443
	/**
444
	 * Get the link to the page that lists all the scheduled tasks.
445
	 *
446
	 * @return string Link to scheduled tasks page.
447
	 */
448 32
	protected function get_task_list_link() {
449 32
		return sprintf(
450 32
			__( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ),
451 32
			get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . \Bulk_Delete::CRON_PAGE_SLUG
452
		);
453
	}
454
455
	/**
456
	 * Parse request and create cron options.
457
	 *
458
	 * @param array $request Request array.
459
	 *
460
	 * @return array Parsed cron option.
461
	 */
462 31
	protected function parse_cron_filters( $request ) {
463
		$cron_options = array(
464 31
			'is_scheduled' => false,
465
		);
466
467 31
		$scheduled = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_cron', false );
468
469 31
		if ( $scheduled ) {
470 28
			$cron_options['is_scheduled'] = true;
471 28
			$cron_options['frequency']    = sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_freq' ] );
472 28
			$cron_options['start_time']   = bd_get_gmt_offseted_time( sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_start' ] ) );
473
474 28
			$cron_options['cron_label'] = $this->get_cron_label();
475
		}
476
477 31
		return $cron_options;
478
	}
479
480
	/**
481
	 * Get the human readable label for the Schedule job.
482
	 *
483
	 * @return string Human readable label for schedule job.
484
	 */
485
	protected function get_cron_label() {
486
		return $this->messages['cron_label'];
487
	}
488
}
489