Completed
Push — 478-fix/add-test-for-delete-te... ( 59b352...607a71 )
by Sudar
27:34 queued 12:18
created

BaseModule::render_delete_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 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
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
	public function __construct() {
133
		$this->initialize();
134
	}
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
	public function render_box() {
172
		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
		$this->render();
183
	}
184
185
	/**
186
	 * Is the current meta box hidden by user.
187
	 *
188
	 * @return bool True, if hidden. False, otherwise.
189
	 */
190
	protected function is_hidden() {
191
		$current_user    = wp_get_current_user();
192
		$user_meta_field = $this->get_hidden_box_user_meta_field();
193
		$hidden_boxes    = get_user_meta( $current_user->ID, $user_meta_field, true );
194
195
		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
	protected function get_hidden_box_user_meta_field() {
204
		if ( 'posts' === $this->item_type ) {
205
			return 'metaboxhidden_toplevel_page_bulk-delete-posts';
206
		} else {
207
			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
	 * Helper function for processing deletion.
226
	 * Setups up cron and invokes the actual delete method.
227
	 *
228
	 * @param array $request Request array.
229
	 */
230
	public function process( $request ) {
231
		$options      = $this->parse_common_filters( $request );
232
		$options      = $this->convert_user_input_to_options( $request, $options );
233
		$cron_options = $this->parse_cron_filters( $request );
234
235
		if ( $this->is_scheduled( $cron_options ) ) {
236
			$msg = $this->schedule_deletion( $cron_options, $options );
237
		} else {
238
			$items_deleted = $this->delete( $options );
239
			$msg           = sprintf( $this->get_success_message( $items_deleted ), $items_deleted );
240
		}
241
242
		add_settings_error(
243
			$this->page_slug,
244
			$this->action,
245
			$msg,
246
			'updated'
247
		);
248
	}
249
250
	/**
251
	 * Delete items based on delete options.
252
	 *
253
	 * @param array $options Delete Options.
254
	 *
255
	 * @return int Number of items deleted.
256
	 */
257
	public function delete( $options ) {
258
		/**
259
		 * Filter delete options before deleting items.
260
		 *
261
		 * @since 6.0.0 Added `Modules` parameter.
262
		 *
263
		 * @param array $options Delete options.
264
		 * @param \BulkWP\BulkDelete\Core\Base\BaseModule Modules that is triggering deletion.
265
		 */
266
		$options = apply_filters( 'bd_delete_options', $options, $this );
267
268
		return $this->do_delete( $options );
269
	}
270
271
	/**
272
	 * Getter for cron_hook.
273
	 *
274
	 * @return string Cron Hook name.
275
	 */
276
	public function get_cron_hook() {
277
		return $this->cron_hook;
278
	}
279
280
	/**
281
	 * Getter for field slug.
282
	 *
283
	 * @return string Field Slug.
284
	 */
285
	public function get_field_slug() {
286
		return $this->field_slug;
287
	}
288
289
	/**
290
	 * Getter for action.
291
	 *
292
	 * @return string Modules action.
293
	 */
294
	public function get_action() {
295
		return $this->action;
296
	}
297
298
	/**
299
	 * Is the current deletion request a scheduled request?
300
	 *
301
	 * @param array $cron_options Request object.
302
	 *
303
	 * @return bool True if it is a scheduled request, False otherwise.
304
	 */
305
	protected function is_scheduled( $cron_options ) {
306
		return $cron_options['is_scheduled'];
307
	}
308
309
	/**
310
	 * Schedule Deletion of items.
311
	 *
312
	 * @param array $cron_options Cron options.
313
	 * @param array $options      Deletion option.
314
	 *
315
	 * @return string Message.
316
	 */
317
	protected function schedule_deletion( $cron_options, $options ) {
318
		if ( '-1' === $cron_options['frequency'] ) {
319
			wp_schedule_single_event( $cron_options['start_time'], $this->cron_hook, array( $options ) );
320
		} else {
321
			wp_schedule_event( $cron_options['start_time'], $cron_options['frequency'], $this->cron_hook, array( $options ) );
322
		}
323
324
		return $this->messages['scheduled'] . ' ' . $this->get_task_list_link();
325
	}
326
327
	/**
328
	 * Get the link to the page that lists all the scheduled tasks.
329
	 *
330
	 * @return string Link to scheduled tasks page.
331
	 */
332
	protected function get_task_list_link() {
333
		return sprintf(
334
			/* translators: 1 Cron page url */
335
			__( 'See the full list of <a href = "%s">scheduled tasks</a>', 'bulk-delete' ),
336
			get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . \Bulk_Delete::CRON_PAGE_SLUG
337
		);
338
	}
339
340
	/**
341
	 * Parse request and create cron options.
342
	 *
343
	 * @param array $request Request array.
344
	 *
345
	 * @return array Parsed cron option.
346
	 */
347
	protected function parse_cron_filters( $request ) {
348
		$cron_options = array(
349
			'is_scheduled' => false,
350
		);
351
352
		$scheduled = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_cron', false );
353
354
		if ( $scheduled ) {
355
			$cron_options['is_scheduled'] = true;
356
			$cron_options['frequency']    = sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_freq' ] );
357
			$cron_options['start_time']   = bd_get_gmt_offseted_time( sanitize_text_field( $request[ 'smbd_' . $this->field_slug . '_cron_start' ] ) );
358
359
			$cron_options['cron_label'] = $this->get_cron_label();
360
		}
361
362
		return $cron_options;
363
	}
364
365
	/**
366
	 * Get the human readable label for the Schedule job.
367
	 *
368
	 * @return string Human readable label for schedule job.
369
	 */
370
	protected function get_cron_label() {
371
		return $this->messages['cron_label'];
372
	}
373
}
374