Completed
Pull Request — dev/6.0.0 (#319)
by Rajan
13:05 queued 08:51
created

BaseModule::render_cron_settings()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 60
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.3731

Importance

Changes 0
Metric Value
cc 4
eloc 41
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 60
ccs 15
cts 21
cp 0.7143
crap 4.3731
rs 8.9618

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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