Passed
Push — 218-fix/metabox-for-delete-pos... ( a6ef9d...f10c22 )
by
unknown
10:44 queued 40s
created

BaseMetabox::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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