Completed
Pull Request — dev/6.0.0 (#514)
by Maria Daniel Deepak
124:18 queued 120:55
created

BaseModule::render_sticky_settings()   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
	 * 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 333
	public function __construct() {
133 333
		$this->initialize();
134 333
	}
135
136
	/**
137
	 * Register.
138
	 *
139
	 * @param string $hook_suffix Page Hook Suffix.
140
	 * @param string $page_slug   Page slug.
141
	 */
142
	public function register( $hook_suffix, $page_slug ) {
143
		$this->page_hook_suffix = $hook_suffix;
144
		$this->page_slug        = $page_slug;
145
146
		add_action( "add_meta_boxes_{$this->page_hook_suffix}", array( $this, 'setup_metabox' ) );
147
148
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
149
150
		if ( ! empty( $this->action ) ) {
151
			add_action( 'bd_' . $this->action, array( $this, 'process' ) );
152
		}
153
	}
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
	 * This function will be overridden by the child classes.
215
	 *
216
	 * @param array $js_array JavaScript Array.
217
	 *
218
	 * @return array Modified JavaScript Array
219
	 */
220
	public function filter_js_array( $js_array ) {
221
		return $js_array;
222
	}
223
224
	/**
225
	 * Render filtering table header.
226
	 */
227
	protected function render_filtering_table_header() {
228
		bd_render_filtering_table_header();
229
	}
230
231
	/**
232
	 * Render restrict settings.
233
	 */
234
	protected function render_restrict_settings() {
235
		bd_render_restrict_settings( $this->field_slug, $this->item_type );
236
	}
237
238
	/**
239
	 * Render delete settings.
240
	 */
241
	protected function render_delete_settings() {
242
		bd_render_delete_settings( $this->field_slug );
243
	}
244
245
	/**
246
	 * Render sticky settings.
247
	 */
248
	protected function render_sticky_settings() {
249
		bd_render_sticky_settings( $this->field_slug );
0 ignored issues
show
Bug introduced by
The function bd_render_sticky_settings was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

249
		/** @scrutinizer ignore-call */ 
250
  bd_render_sticky_settings( $this->field_slug );
Loading history...
250
	}
251
252
	/**
253
	 * Render limit settings.
254
	 */
255
	protected function render_limit_settings() {
256
		bd_render_limit_settings( $this->field_slug, $this->item_type );
257
	}
258
259
	/**
260
	 * Render cron settings based on whether scheduler is present or not.
261
	 */
262
	protected function render_cron_settings() {
263
		$disabled_attr = 'disabled';
264
		if ( empty( $this->scheduler_url ) ) {
265
			$disabled_attr = '';
266
		}
267
		?>
268
269
		<tr>
270
			<td scope="row" colspan="2">
271
				<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="false" type="radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?>
272
				<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' ); ?>
273
				<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' ); ?>
274
275
				<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 ); ?>>
276
					<option value="-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option>
277
278
					<?php
279
					/**
280
					 * List of cron schedules.
281
					 *
282
					 * @since 6.0.0
283
					 *
284
					 * @param array                                   $cron_schedules List of cron schedules.
285
					 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module         Module.
286
					 */
287
					$cron_schedules = apply_filters( 'bd_cron_schedules', wp_get_schedules(), $this );
288
					?>
289
290
					<?php foreach ( $cron_schedules as $key => $value ) : ?>
291
						<option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value['display'] ); ?></option>
292
					<?php endforeach; ?>
293
				</select>
294
295
				<?php if ( ! empty( $this->scheduler_url ) ) : ?>
296
					<?php
297
					$pro_class = 'bd-' . str_replace( '_', '-', $this->field_slug ) . '-pro';
298
299
					/**
300
					 * HTML class of the span that displays the 'Pro only feature' message.
301
					 *
302
					 * @since 6.0.0
303
					 *
304
					 * @param string                                  $pro_class  HTML class.
305
					 * @param string                                  $field_slug Field Slug of module.
306
					 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module     Module.
307
					 */
308
					apply_filters( 'bd_pro_only_feature_class', $pro_class, $this->field_slug, $this )
309
					?>
310
311
					<span class="<?php echo sanitize_html_class( $pro_class ); ?>" style="color:red">
312
						<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href="<?php echo esc_url( $this->scheduler_url ); ?>">Buy now</a>
313
					</span>
314
				<?php endif; ?>
315
			</td>
316
		</tr>
317
318
		<tr>
319
			<td scope="row" colspan="2">
320
				<?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' );
321
322
				$link   = '<a href="https://bulkwp.com/docs/add-a-new-cron-schedule/">' . __( 'Click here', 'bulk-delete' ) . '</a>';
323
				$markup = sprintf( __( 'Want to add new a Cron schedule? %s', 'bulk-delete' ), $link );
324
325
				$content = __( 'Learn to add your desired Cron schedule.', 'bulk-delete' );
326
				echo '&nbsp' . bd_generate_help_tooltip( $markup, $content );
327
				?>
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 258
	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 258
		$options = apply_filters( 'bd_delete_options', $options, $this );
383
384 258
		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 267
	public function get_action() {
411 267
		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
			/* translators: 1 Cron page url */
451 32
			__( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ),
452 32
			get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . \Bulk_Delete::CRON_PAGE_SLUG
453
		);
454
	}
455
456
	/**
457
	 * Parse request and create cron options.
458
	 *
459
	 * @param array $request Request array.
460
	 *
461
	 * @return array Parsed cron option.
462
	 */
463 31
	protected function parse_cron_filters( $request ) {
464
		$cron_options = array(
465 31
			'is_scheduled' => false,
466
		);
467
468 31
		$scheduled = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_cron', false );
469
470 31
		if ( $scheduled ) {
471 28
			$cron_options['is_scheduled'] = true;
472 28
			$cron_options['frequency']    = sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_freq' ] );
473 28
			$cron_options['start_time']   = bd_get_gmt_offseted_time( sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_start' ] ) );
474
475 28
			$cron_options['cron_label'] = $this->get_cron_label();
476
		}
477
478 31
		return $cron_options;
479
	}
480
481
	/**
482
	 * Get the human readable label for the Schedule job.
483
	 *
484
	 * @return string Human readable label for schedule job.
485
	 */
486 28
	protected function get_cron_label() {
487 28
		return $this->messages['cron_label'];
488
	}
489
}
490