Completed
Pull Request — dev/6.0.0 (#277)
by Rajan
10:21 queued 06:20
created

BaseModule::render_submit_button()   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 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
	);
83
84
	/**
85
	 * Initialize and setup variables.
86
	 *
87
	 * @return void
88
	 */
89
	abstract protected function initialize();
90
91
	/**
92
	 * Render the Modules.
93
	 *
94
	 * @return void
95
	 */
96
	abstract public function render();
97
98
	/**
99
	 * Process user input and create metabox options.
100
	 *
101
	 * @param array $request Request array.
102
	 * @param array $options User options.
103
	 *
104
	 * @return array User options.
105
	 */
106
	abstract protected function convert_user_input_to_options( $request, $options );
107
108
	/**
109
	 * Perform the deletion.
110
	 *
111
	 * @param array $options Array of Delete options.
112
	 *
113
	 * @return int Number of items that were deleted.
114
	 */
115
	abstract public function delete( $options );
116
117
	/**
118
	 * Get Success Message.
119
	 *
120
	 * @param int $items_deleted Number of items that were deleted.
121
	 *
122
	 * @return string Success message.
123
	 */
124
	abstract protected function get_success_message( $items_deleted );
125
126
	/**
127
	 * Create new instances of Modules.
128
	 */
129 103
	public function __construct() {
130 103
		$this->initialize();
131 103
	}
132
133
	/**
134
	 * Register.
135
	 *
136
	 * @param string $hook_suffix Page Hook Suffix.
137
	 * @param string $page_slug   Page slug.
138
	 */
139
	public function register( $hook_suffix, $page_slug ) {
140
		$this->page_hook_suffix = $hook_suffix;
141
		$this->page_slug        = $page_slug;
142
143
		add_action( "add_meta_boxes_{$this->page_hook_suffix}", array( $this, 'setup_metabox' ) );
144
145
		add_action( 'bd_' . $this->action, array( $this, 'process' ) );
146
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
147
	}
148
149
	/**
150
	 * Setup the meta box.
151
	 */
152
	public function setup_metabox() {
153
		add_meta_box(
154
			$this->meta_box_slug,
155
			$this->messages['box_label'],
156
			array( $this, 'render_box' ),
157
			$this->page_hook_suffix,
158
			'advanced'
159
		);
160
	}
161
162
	/**
163
	 * Render the meta box.
164
	 */
165 1
	public function render_box() {
166 1
		if ( $this->is_hidden() ) {
167
			printf(
168
				/* translators: 1 module url */
169
				__( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ),
170
				'admin.php?page=' . $this->page_slug
171
			);
172
173
			return;
174
		}
175
176 1
		$this->render();
177 1
	}
178
179
	/**
180
	 * Is the current meta box hidden by user.
181
	 *
182
	 * @return bool True, if hidden. False, otherwise.
183
	 */
184 1
	protected function is_hidden() {
185 1
		$current_user    = wp_get_current_user();
186 1
		$user_meta_field = $this->get_hidden_box_user_meta_field();
187 1
		$hidden_boxes    = get_user_meta( $current_user->ID, $user_meta_field, true );
188
189 1
		return is_array( $hidden_boxes ) && in_array( $this->meta_box_slug, $hidden_boxes, true );
190
	}
191
192
	/**
193
	 * Get the user meta field that stores the status of the hidden meta boxes.
194
	 *
195
	 * @return string Name of the User Meta field.
196
	 */
197 1
	protected function get_hidden_box_user_meta_field() {
198 1
		if ( 'posts' === $this->item_type ) {
199
			return 'metaboxhidden_toplevel_page_bulk-delete-posts';
200
		} else {
201 1
			return 'metaboxhidden_bulk-wp_page_' . $this->page_slug;
202
		}
203
	}
204
205
	/**
206
	 * Filter the js array.
207
	 *
208
	 * This function will be overridden by the child classes.
209
	 *
210
	 * @param array $js_array JavaScript Array.
211
	 *
212
	 * @return array Modified JavaScript Array
213
	 */
214
	public function filter_js_array( $js_array ) {
215
		return $js_array;
216
	}
217
218
	/**
219
	 * Render filtering table header.
220
	 */
221
	protected function render_filtering_table_header() {
222
		bd_render_filtering_table_header();
223
	}
224
225
	/**
226
	 * Render restrict settings.
227
	 */
228
	protected function render_restrict_settings() {
229
		bd_render_restrict_settings( $this->field_slug, $this->item_type );
230
	}
231
232
	/**
233
	 * Render delete settings.
234
	 */
235
	protected function render_delete_settings() {
236
		bd_render_delete_settings( $this->field_slug );
237
	}
238
239
	/**
240
	 * Render limit settings.
241
	 */
242
	protected function render_limit_settings() {
243
		bd_render_limit_settings( $this->field_slug, $this->item_type );
244
	}
245
246
	/**
247
	 * Render cron settings.
248
	 */
249
	protected function render_cron_settings() {
250
		bd_render_cron_settings( $this->field_slug, $this->scheduler_url );
251
	}
252
253
	/**
254
	 * Render submit button.
255
	 */
256
	protected function render_submit_button() {
257
		bd_render_submit_button( $this->action );
258
	}
259
260
	/**
261
	 * Helper function for processing cron filter.
262
	 *
263
	 * @param mixed $options
264
	 */
265
	protected function process_cron_filters( $options ){
266 1
		$options['cron_name'] = $this->get_cron_name();
267 1
268 1
		return $options;
269
	}
270 1
271
	/**
272 1
	 * Helper function for processing deletion.
273
	 * Setups up cron and invokes the actual delete method.
274
	 *
275 1
	 * @param array $request Request array.
276 1
	 */
277
	public function process( $request ) {
278
		$options      = $this->parse_common_filters( $request );
279 1
		$options      = $this->convert_user_input_to_options( $request, $options );
280 1
		$options      = $this->process_cron_filters( $options );
281 1
		$cron_options = $this->parse_cron_filters( $request );
282 1
283 1
		if ( $this->is_scheduled( $cron_options ) ) {
284
			$msg = $this->schedule_deletion( $cron_options, $options );
285 1
		} else {
286
			$items_deleted = $this->delete( $options );
287
			$msg           = sprintf( $this->get_success_message( $items_deleted ), $items_deleted );
288
		}
289
290
		add_settings_error(
291
			$this->page_slug,
292
			$this->action,
293
			$msg,
294
			'updated'
295
		);
296
	}
297
298
	/**
299
	 * Getter for cron_hook.
300
	 *
301
	 * @return string Cron Hook name.
302
	 */
303
	public function get_cron_hook() {
304
		return $this->cron_hook;
305
	}
306
307
	/**
308
	 * Getter for field slug.
309
	 *
310 36
	 * @return string Field Slug.
311 36
	 */
312
	public function get_field_slug() {
313
		return $this->field_slug;
314
	}
315
316
	/**
317
	 * Getter for action.
318
	 *
319
	 * @return string Modules action.
320
	 */
321 3
	public function get_action() {
322 3
		return $this->action;
323
	}
324
325
	/**
326
	 * Is the current deletion request a scheduled request?
327
	 *
328
	 * @param array $cron_options Request object.
329
	 *
330
	 * @return bool True if it is a scheduled request, False otherwise.
331
	 */
332
	protected function is_scheduled( $cron_options ) {
333 32
		return $cron_options['is_scheduled'];
334 32
	}
335 8
336
	/**
337 24
	 * Schedule Deletion of items.
338
	 *
339
	 * @param array $cron_options Cron options.
340 32
	 * @param array $options      Deletion option.
341
	 *
342
	 * @return string Message.
343
	 */
344
	protected function schedule_deletion( $cron_options, $options ) {
345
		if ( '-1' === $cron_options['frequency'] ) {
346
			wp_schedule_single_event( $cron_options['start_time'], $this->cron_hook, array( $options ) );
347
		} else {
348 32
			wp_schedule_event( $cron_options['start_time'], $cron_options['frequency'], $this->cron_hook, array( $options ) );
349 32
		}
350 32
351 32
		return $this->messages['scheduled'] . ' ' . $this->get_task_list_link();
352
	}
353
354
	/**
355
	 * Get the link to the page that lists all the scheduled tasks.
356
	 *
357
	 * @return string Link to scheduled tasks page.
358
	 */
359
	protected function get_task_list_link() {
360
		return sprintf(
361
			__( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ),
362 1
			get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . \Bulk_Delete::CRON_PAGE_SLUG
363 1
		);
364
	}
365 1
366 1
	/**
367 1
	 * Handle common filters.
368
	 *
369 1
	 * @param array $request Request array.
370 1
	 *
371
	 * @return array User options.
372 1
	 */
373
	protected function parse_common_filters( $request ) {
374
		$options = array();
375
376
		$options['restrict']     = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false );
377
		$options['limit_to']     = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) );
378
		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false );
379
380
		$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' );
381
		$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) );
382 31
383
		return $options;
384 31
	}
385
386
	/**
387 31
	 * Parse request and create cron options.
388
	 *
389 31
	 * @param array $request Request array.
390 28
	 *
391 28
	 * @return array Parsed cron option.
392 28
	 */
393
	protected function parse_cron_filters( $request ) {
394
		$cron_options = array(
395 31
			'is_scheduled' => false,
396
		);
397
398
		$scheduled = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_cron', false );
399
400
		if ( $scheduled ) {
401
			$cron_options['is_scheduled'] = true;
402
			$cron_options['frequency']    = sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_freq' ] );
403
			$cron_options['start_time']   = bd_get_gmt_offseted_time( sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_start' ] ) );
404
		}
405
406
		return $cron_options;
407
	}
408
409
	/**
410
	 * Get the threshold after which enhanced select should be used.
411
	 *
412
	 * @return int Threshold.
413
	 */
414
	protected function get_enhanced_select_threshold() {
415
		/**
416
		 * Filter the enhanced select threshold.
417
		 *
418
		 * @since 6.0.0
419
		 *
420
		 * @param int Threshold.
421
		 */
422
		return apply_filters( 'bd_enhanced_select_threshold', 1000 );
423
	}
424
425
	/**
426
	 * Get the class name for select2 dropdown based on the number of items present.
427
	 *
428
	 * @param int    $count      The number of items present.
429
	 * @param string $class_name Primary class name.
430
	 *
431
	 * @return string Class name.
432
	 */
433
	protected function enable_ajax_if_needed_to_dropdown_class_name( $count, $class_name ) {
434
		if ( $count >= $this->get_enhanced_select_threshold() ) {
435
			$class_name .= '-ajax';
436
		}
437
438
		return $class_name;
439
	}
440
441
	/**
442
	 * Schedule job title.
443
	 *
444
	 * @return string humane readable title
445
	 */
446
	public function get_cron_name(){
447
		return $this->messages['cron_name'];
448
	}
449
}
450