Passed
Push — 217-feature/delete-posts-by-po... ( d7bccc...aea384 )
by Sudar
04:38
created

BaseMetabox::is_scheduled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Base;
4
5 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Encapsulates the Bulk Delete Meta box Module Logic.
9
 *
10
 * All Bulk Delete Meta box Modules should extend this class.
11
 *
12
 * @since 6.0.0
13
 */
14
abstract class BaseMetabox {
15
	/**
16
	 * Item Type. Possible values 'posts', 'pages', 'users' etc.
17
	 *
18
	 * @var string
19
	 */
20
	protected $item_type;
21
22
	/**
23
	 * The hook suffix of the screen where this meta box would be shown.
24
	 *
25
	 * @var string
26
	 */
27
	protected $page_hook_suffix;
28
29
	/**
30
	 * Slug of the page where this module will be shown.
31
	 *
32
	 * @var string
33
	 */
34
	protected $page_slug;
35
36
	/**
37
	 * Slug for the form fields.
38
	 *
39
	 * @var string
40
	 */
41
	protected $field_slug;
42
43
	/**
44
	 * Slug of the meta box.
45
	 *
46
	 * @var string
47
	 */
48
	protected $meta_box_slug;
49
50
	/**
51
	 * Action in which the delete operation should be performed.
52
	 *
53
	 * @var string
54
	 */
55
	protected $action;
56
57
	/**
58
	 * Hook for scheduler.
59
	 *
60
	 * @var string
61
	 */
62
	protected $cron_hook;
63
64
	/**
65
	 * Url of the scheduler addon.
66
	 *
67
	 * @var string
68
	 */
69
	protected $scheduler_url;
70
71
	/**
72
	 * Messages shown to the user.
73
	 *
74
	 * @var array
75
	 */
76
	protected $messages = array(
77
		'box_label' => '',
78
	);
79
80
	/**
81
	 * Initialize and setup variables.
82
	 *
83
	 * @return void
84
	 */
85
	abstract protected function initialize();
86
87
	/**
88
	 * Render the Metabox.
89
	 *
90
	 * @return void
91
	 */
92
	abstract public function render();
93
94
	/**
95
	 * Process user input and create metabox options.
96
	 *
97
	 * @param array $request Request array.
98
	 * @param array $options User options.
99
	 *
100
	 * @return array User options.
101
	 */
102
	abstract protected function convert_user_input_to_options( $request, $options );
103
104
	/**
105
	 * Perform the deletion.
106
	 *
107
	 * @param array $delete_options Array of Delete options.
108
	 *
109
	 * @return int Number of items that were deleted.
110
	 */
111
	abstract public function delete( $delete_options );
112
113
	/**
114
	 * Get Success Message.
115
	 *
116
	 * @param int $items_deleted Number of items that were deleted.
117
	 *
118
	 * @return string Success message.
119
	 */
120
	abstract protected function get_success_message( $items_deleted );
121
122
	/**
123
	 * Create new instances of Metabox.
124
	 */
125 80
	public function __construct() {
126 80
		$this->initialize();
127 80
	}
128
129
	/**
130
	 * Register.
131
	 *
132
	 * @param string $hook_suffix Page Hook Suffix.
133
	 * @param string $page_slug   Page slug.
134
	 */
135
	public function register( $hook_suffix, $page_slug ) {
136
		$this->page_hook_suffix = $hook_suffix;
137
		$this->page_slug        = $page_slug;
138
139
		add_action( "add_meta_boxes_{$this->page_hook_suffix}", array( $this, 'setup_metabox' ) );
140
141
		add_action( 'bd_' . $this->action, array( $this, 'process' ) );
142
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
143
	}
144
145
	/**
146
	 * Setup the meta box.
147
	 */
148
	public function setup_metabox() {
149
		add_meta_box(
150
			$this->meta_box_slug,
151
			$this->messages['box_label'],
152
			array( $this, 'render_box' ),
153
			$this->page_hook_suffix,
154
			'advanced'
155
		);
156
	}
157
158
	/**
159
	 * Render the meta box.
160
	 */
161 1
	public function render_box() {
162 1
		if ( $this->is_hidden() ) {
163
			printf(
164
				/* translators: 1 module url */
165
				__( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ),
166
				'admin.php?page=' . $this->page_slug
167
			);
168
169
			return;
170
		}
171
172 1
		$this->render();
173 1
	}
174
175
	/**
176
	 * Is the current meta box hidden by user.
177
	 *
178
	 * @return bool True, if hidden. False, otherwise.
179
	 */
180 1
	protected function is_hidden() {
181 1
		$current_user    = wp_get_current_user();
182 1
		$user_meta_field = $this->get_hidden_box_user_meta_field();
183 1
		$hidden_boxes    = get_user_meta( $current_user->ID, $user_meta_field, true );
184
185 1
		return is_array( $hidden_boxes ) && in_array( $this->meta_box_slug, $hidden_boxes, true );
186
	}
187
188
	/**
189
	 * Get the user meta field that stores the status of the hidden meta boxes.
190
	 *
191
	 * @return string Name of the User Meta field.
192
	 */
193 1
	protected function get_hidden_box_user_meta_field() {
194 1
		if ( 'posts' === $this->item_type ) {
195
			return 'metaboxhidden_toplevel_page_bulk-delete-posts';
196
		} else {
197 1
			return 'metaboxhidden_bulk-wp_page_' . $this->page_slug;
198
		}
199
	}
200
201
	/**
202
	 * Filter the js array.
203
	 *
204
	 * This function will be overridden by the child classes.
205
	 *
206
	 * @param array $js_array JavaScript Array.
207
	 *
208
	 * @return array Modified JavaScript Array
209
	 */
210
	public function filter_js_array( $js_array ) {
211
		return $js_array;
212
	}
213
214
	/**
215
	 * Render filtering table header.
216
	 */
217
	protected function render_filtering_table_header() {
218
		bd_render_filtering_table_header();
219
	}
220
221
	/**
222
	 * Render restrict settings.
223
	 */
224
	protected function render_restrict_settings() {
225
		bd_render_restrict_settings( $this->field_slug, $this->item_type );
226
	}
227
228
	/**
229
	 * Render delete settings.
230
	 */
231
	protected function render_delete_settings() {
232
		bd_render_delete_settings( $this->field_slug );
233
	}
234
235
	/**
236
	 * Render limit settings.
237
	 */
238
	protected function render_limit_settings() {
239
		bd_render_limit_settings( $this->field_slug, $this->item_type );
240
	}
241
242
	/**
243
	 * Render cron settings.
244
	 */
245
	protected function render_cron_settings() {
246
		bd_render_cron_settings( $this->field_slug, $this->scheduler_url );
247
	}
248
249
	/**
250
	 * Render submit button.
251
	 */
252
	protected function render_submit_button() {
253
		bd_render_submit_button( $this->action );
254
	}
255
256
	/**
257
	 * Helper function for processing deletion.
258
	 * Setups up cron and invokes the actual delete method.
259
	 *
260
	 * @param array $request Request array.
261
	 */
262 1
	public function process( $request ) {
263 1
		$options = $this->parse_filters( $request );
264 1
		$options = $this->convert_user_input_to_options( $request, $options );
265
266 1
		$cron_options = $this->parse_cron_request( $request );
267
268 1
		if ( $this->is_scheduled( $cron_options ) ) {
269
			$msg = $this->schedule_deletion( $cron_options, $options );
270
		} else {
271 1
			$items_deleted = $this->delete( $options );
272 1
			$msg           = sprintf( $this->get_success_message( $items_deleted ), $items_deleted );
273
		}
274
275 1
		add_settings_error(
276 1
			$this->page_slug,
277 1
			$this->action,
278 1
			$msg,
279 1
			'updated'
280
		);
281 1
	}
282
283
	/**
284
	 * Getter for cron_hook.
285
	 *
286
	 * @return string Cron Hook name.
287
	 */
288
	public function get_cron_hook() {
289
		return $this->cron_hook;
290
	}
291
292
	/**
293
	 * Getter for field slug.
294
	 *
295
	 * @return string Field Slug.
296
	 */
297
	public function get_field_slug() {
298
		return $this->field_slug;
299
	}
300
301
	/**
302
	 * Getter for action.
303
	 *
304
	 * @return string Metabox action.
305
	 */
306
	public function get_action() {
307
		return $this->action;
308
	}
309
310
	/**
311
	 * Is the current deletion request a scheduled request?
312
	 *
313
	 * @param array $cron_options Request object.
314
	 *
315
	 * @return bool True if it is a scheduled request, False otherwise.
316
	 */
317 3
	protected function is_scheduled( $cron_options ) {
318 3
		return $cron_options['is_scheduled'];
319
	}
320
321
	/**
322
	 * Schedule Deletion of items.
323
	 *
324
	 * @param array $cron_options Cron options.
325
	 * @param array $options      Deletion option.
326
	 *
327
	 * @return string Message.
328
	 */
329 32
	protected function schedule_deletion( $cron_options, $options ) {
330 32
		if ( '-1' === $cron_options['frequency'] ) {
331 8
			wp_schedule_single_event( $cron_options['start_time'], $this->cron_hook, array( $options ) );
332
		} else {
333 24
			wp_schedule_event( $cron_options['start_time'], $cron_options['frequency'], $this->cron_hook, array( $options ) );
334
		}
335
336 32
		return $this->messages['scheduled'] . ' ' . $this->get_task_list_link();
337
	}
338
339
	/**
340
	 * Get the link to the page that lists all the scheduled tasks.
341
	 *
342
	 * @return string Link to scheduled tasks page.
343
	 */
344 32
	protected function get_task_list_link() {
345 32
		return sprintf(
346 32
			__( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ),
347 32
			get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . \Bulk_Delete::CRON_PAGE_SLUG
348
		);
349
	}
350
351
	/**
352
	 * Handle common filters.
353
	 *
354
	 * @param array $request Request array.
355
	 *
356
	 * @return array User options.
357
	 */
358 1
	protected function parse_filters( $request ) {
359 1
		$options = array();
360
361 1
		$options['restrict']     = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false );
362 1
		$options['limit_to']     = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) );
363 1
		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false );
364
365 1
		$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' );
366 1
		$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) );
367
368 1
		return $options;
369
	}
370
371
	/**
372
	 * Parse request and create cron options.
373
	 *
374
	 * @param array $request Request array.
375
	 *
376
	 * @return array Parsed cron option.
377
	 */
378 31
	protected function parse_cron_request( $request ) {
379
		$cron_options = array(
380 31
			'is_scheduled' => false,
381
		);
382
383 31
		$scheduled = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_cron', false );
384
385 31
		if ( $scheduled ) {
386 28
			$cron_options['is_scheduled'] = true;
387 28
			$cron_options['frequency']    = sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_freq' ] );
388 28
			$cron_options['start_time']   = bd_get_gmt_offseted_time( sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_start' ] ) );
389
		}
390
391 31
		return $cron_options;
392
	}
393
}
394