Passed
Push — 552-feature/reassign-contnet-o... ( e0dd10...9398c7 )
by
unknown
12:00
created

Renderer::render_post_reassign_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 17
ccs 0
cts 5
cp 0
crap 2
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Base\Mixin;
4
5 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Container of all Render methods.
9
 *
10
 * Ideally this should be a Trait. Since Bulk Delete still supports PHP 5.3, this is implemented as a class.
11
 * Once the minimum requirement is increased to PHP 5.3, this will be changed into a Trait.
12
 *
13
 * @since 6.0.0
14
 */
15
abstract class Renderer extends Fetcher {
16
	/**
17
	 * Slug for the form fields.
18
	 *
19
	 * @var string
20
	 */
21
	protected $field_slug;
22
23
	/**
24
	 * Render post status including custom post status.
25
	 *
26
	 * @param string $post_type The post type for which the post status should be displayed.
27
	 */
28
	protected function render_post_status( $post_type = 'post' ) {
29
		$post_statuses = $this->get_post_statuses();
30
		$post_count    = wp_count_posts( $post_type );
31
32
		foreach ( $post_statuses as $post_status ) : ?>
33
			<tr>
34
				<td>
35
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" id="smbd_<?php echo esc_attr( $post_status->name ); ?>"
36
						value="<?php echo esc_attr( $post_status->name ); ?>" type="checkbox">
37
38
					<label for="smbd_<?php echo esc_attr( $post_status->name ); ?>">
39
						<?php echo esc_html( $post_status->label ), ' '; ?>
40
						<?php if ( property_exists( $post_count, $post_status->name ) ) : ?>
41
							(<?php echo absint( $post_count->{ $post_status->name } ) . ' ', __( 'Posts', 'bulk-delete' ); ?>)
42
						<?php endif; ?>
43
					</label>
44
				</td>
45
			</tr>
46
		<?php endforeach;
47
	}
48
49
	/**
50
	 * Render Post Types as radio buttons.
51
	 */
52
	protected function render_post_type_as_radios() {
53
		$post_types = $this->get_post_types();
54
		?>
55
56
		<?php foreach ( $post_types as $post_type ) : ?>
57
58
			<tr>
59
				<td scope="row">
60
					<input type="radio" name="<?php echo esc_attr( $this->field_slug ); ?>_post_type"
61
						value="<?php echo esc_attr( $post_type->name ); ?>"
62
						id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
63
64
					<label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
65
						<?php echo esc_html( $post_type->label ); ?>
66
					</label>
67
				</td>
68
			</tr>
69
70
		<?php endforeach; ?>
71
		<?php
72
	}
73
74
	/**
75
	 * Render Post type with status and post count checkboxes.
76
	 */
77
	protected function render_post_type_with_status() {
78
		$post_types_by_status = $this->get_post_types_by_status();
79
		?>
80
		<tr>
81
			<td scope="row" colspan="2">
82
				<select class="enhanced-post-types-with-status" multiple="multiple" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]">
83
				<?php foreach ( $post_types_by_status as $post_type => $all_status ) : ?>
84
					<optgroup label="<?php echo esc_html( $post_type ); ?>">
85
					<?php foreach ( $all_status as $status_key => $status_value ) : ?>
86
						<option value="<?php echo esc_attr( $status_key ); ?>"><?php echo esc_html( $status_value ); ?></option>
87
					<?php endforeach; ?>
88
					</optgroup>
89
				<?php endforeach; ?>
90
				</select>
91
			</td>
92
		</tr>
93
		<?php
94
	}
95
96
	/**
97
	 * Split post type and status.
98
	 *
99
	 * @param string $str Post type and status combination.
100
	 *
101
	 * @return array Post type and status as elements of array.
102
	 */
103 9
	protected function split_post_type_and_status( $str ) {
104 9
		$type_status = array();
105
106 9
		$str_arr = explode( '-', $str );
107
108 9
		if ( count( $str_arr ) > 1 ) {
109
			$type_status['status'] = end( $str_arr );
110
			$type_status['type']   = implode( '-', array_slice( $str_arr, 0, - 1 ) );
111
		} else {
112 9
			$type_status['status'] = 'publish';
113 9
			$type_status['type']   = $str;
114
		}
115
116 9
		return $type_status;
117
	}
118
119
	/**
120
	 * Render post reassign settings.
121
	 */
122
	protected function render_post_reassign_settings() {
123
		?>
124
		<tr>
125
			<td scope="row" colspan="2">
126
				<label><input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_post_reassign" value="false" type="radio"
127
					checked="checked" class="post-reassign"> <?php _e( 'Also delete all posts of the users', 'bulk-delete' ); ?></label>
128
				<label><input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_post_reassign" value="true" type="radio"
129
					id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_post_reassign" class="post-reassign"> <?php _e( 'Re-assign the posts to', 'bulk-delete' ); ?></label>
130
				<?php
131
				wp_dropdown_users(
132
					array(
133
						'name'  => 'smbd_' . esc_attr( $this->field_slug ) . '_reassign_user_id',
134
						'class' => 'reassign-user',
135
					)
136
				);
137
				?>
138
			</td>
139
		</tr>
140
		<?php
141
	}
142
143
	/**
144
	 * Render user role dropdown.
145
	 */
146
	protected function render_user_role_dropdown() {
147
		global $wp_roles;
148
		?>
149
150
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_roles[]" class="enhanced-role-dropdown"
151
				multiple="multiple" data-placeholder="<?php _e( 'Select User Role', 'bulk-delete' ); ?>">
152
153
			<?php foreach ( $wp_roles->roles as $role => $role_details ) : ?>
154
				<option value="<?php echo esc_attr( $role ); ?>">
155
					<?php echo esc_html( $role_details['name'] ), ' (', absint( $this->get_user_count_by_role( $role ) ), ' ', __( 'Users', 'bulk-delete' ), ')'; ?>
156
				</option>
157
			<?php endforeach; ?>
158
		</select>
159
160
		<?php
161
	}
162
163
	/**
164
	 * Render Post type dropdown.
165
	 */
166
	protected function render_post_type_dropdown() {
167
		bd_render_post_type_dropdown( $this->field_slug );
168
	}
169
170
	/**
171
	 * Render Taxonomy dropdown.
172
	 */
173
	protected function render_taxonomy_dropdown() {
174
		$taxonomies = get_taxonomies( array(), 'objects' );
175
		?>
176
177
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_taxonomy" class="enhanced-taxonomy-list" data-placeholder="<?php _e( 'Select Taxonomy', 'bulk-delete' ); ?>">
178
			<?php foreach ( $taxonomies as $taxonomy ) : ?>
179
				<option value="<?php echo esc_attr( $taxonomy->name ); ?>">
180
					<?php echo esc_html( $taxonomy->label . ' (' . $taxonomy->name . ')' ); ?>
181
				</option>
182
			<?php endforeach; ?>
183
		</select>
184
		<?php
185
	}
186
187
	/**
188
	 * Render Category dropdown.
189
	 */
190
	protected function render_category_dropdown() {
191
		$categories = $this->get_categories();
192
		?>
193
194
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
195
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
196
				data-taxonomy="category" multiple>
197
198
			<option value="all">
199
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
200
			</option>
201
202
			<?php foreach ( $categories as $category ) : ?>
203
				<option value="<?php echo absint( $category->cat_ID ); ?>">
204
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
205
				</option>
206
			<?php endforeach; ?>
207
208
		</select>
209
		<?php
210
	}
211
212
	/**
213
	 * Render String based comparison operators dropdown.
214
	 */
215
	protected function render_string_comparison_operators() {
216
		?>
217
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_operator">
218
			<option value="equal_to"><?php _e( 'equal to', 'bulk-delete' ); ?></option>
219
			<option value="not_equal_to"><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
220
			<option value="starts_with"><?php _e( 'starts with', 'bulk-delete' ); ?></option>
221
			<option value="ends_with"><?php _e( 'ends with', 'bulk-delete' ); ?></option>
222
			<option value="contains"><?php _e( 'contains', 'bulk-delete' ); ?></option>
223
			<option value="not_contains"><?php _e( 'not contains', 'bulk-delete' ); ?></option>
224
		</select>
225
		<?php
226
	}
227
228
	/**
229
	 * Render number based comparison operators dropdown.
230
	 */
231
	protected function render_number_comparison_operators() {
232
		?>
233
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_operator">
234
			<option value="equal_to"><?php _e( 'equal to', 'bulk-delete' ); ?></option>
235
			<option value="not_equal_to"><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
236
			<option value="less_than"><?php _e( 'less than', 'bulk-delete' ); ?></option>
237
			<option value="greater_than"><?php _e( 'greater than', 'bulk-delete' ); ?></option>
238
		</select>
239
		<?php
240
	}
241
242
	/**
243
	 * Render Tags dropdown.
244
	 */
245
	protected function render_tags_dropdown() {
246
		$tags = $this->get_tags();
247
		?>
248
249
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
250
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
251
				data-taxonomy="post_tag" multiple>
252
253
			<option value="all">
254
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
255
			</option>
256
257
			<?php foreach ( $tags as $tag ) : ?>
258
				<option value="<?php echo absint( $tag->term_id ); ?>">
259
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
260
				</option>
261
			<?php endforeach; ?>
262
		</select>
263
		<?php
264
	}
265
266
	/**
267
	 * Get the class name for select2 dropdown based on the number of items present.
268
	 *
269
	 * @param int    $count      The number of items present.
270
	 * @param string $class_name Primary class name.
271
	 *
272
	 * @return string Class name.
273
	 */
274
	protected function enable_ajax_if_needed_to_dropdown_class_name( $count, $class_name ) {
275
		if ( $count >= $this->get_enhanced_select_threshold() ) {
276
			$class_name .= '-ajax';
277
		}
278
279
		return $class_name;
280
	}
281
282
	/**
283
	 * Render Sticky Posts dropdown.
284
	 */
285
	protected function render_sticky_posts_dropdown() {
286
		$sticky_posts = $this->get_sticky_posts();
287
		?>
288
289
		<table class="optiontable">
290
			<?php if ( count( $sticky_posts ) > 1 ) : ?>
291
				<tr>
292
					<td scope="row">
293
						<label>
294
							<input type="checkbox" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="all">
295
							<?php echo __( 'All sticky posts', 'bulk-delete' ), ' (', count( $sticky_posts ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
296
						</label>
297
					</td>
298
				</tr>
299
			<?php endif; ?>
300
301
			<?php foreach ( $sticky_posts as $post ) : ?>
302
				<?php $author = get_userdata( $post->post_author ); ?>
303
				<tr>
304
					<td scope="row">
305
						<label>
306
							<input type="checkbox" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
307
							<?php
308
								echo esc_html( $post->post_title ), ' - ',
309
									__( 'Published on', 'bulk-delete' ), ' ', get_the_date( get_option( 'date_format' ), $post->ID ),
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $d of get_the_date() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

309
									__( 'Published on', 'bulk-delete' ), ' ', get_the_date( /** @scrutinizer ignore-type */ get_option( 'date_format' ), $post->ID ),
Loading history...
Bug introduced by
Are you sure get_the_date(get_option(...te_format'), $post->ID) of type false|string can be used in echo? ( Ignorable by Annotation )

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

309
									__( 'Published on', 'bulk-delete' ), ' ', /** @scrutinizer ignore-type */ get_the_date( get_option( 'date_format' ), $post->ID ),
Loading history...
310
									__( ' by ', 'bulk-delete' ), esc_html( $author->display_name );
311
							?>
312
						</label>
313
					</td>
314
				</tr>
315
			<?php endforeach; ?>
316
		</table>
317
		<?php
318
	}
319
320
	/**
321
	 * Renders exclude sticky posts checkbox.
322
	 */
323
	protected function render_exclude_sticky_settings() {
324
		if ( $this->are_sticky_posts_present() ) : // phpcs:ignore?>
325
		<tr>
326
			<td scope="row">
327
				<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_exclude_sticky" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_exclude_sticky" value="true" type="checkbox">
328
			</td>
329
			<td>
330
				<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_exclude_sticky"><?php _e( 'Exclude sticky posts', 'bulk-delete' ); ?></label>
331
			</td>
332
		</tr>
333
		<?php endif; // phpcs:ignore?>
334
		<?php
335
	}
336
337
	/**
338
	 * Render Post Types as checkboxes.
339
	 *
340
	 * @since 5.6.0
341
	 *
342
	 * @param string $name Name of post type checkboxes.
343
	 */
344
	protected function render_post_type_checkboxes( $name ) {
345
		$post_types = bd_get_post_types();
346
		?>
347
348
		<?php foreach ( $post_types as $post_type ) : ?>
349
350
		<tr>
351
			<td scope="row">
352
				<input type="checkbox" name="<?php echo esc_attr( $name ); ?>[]" value="<?php echo esc_attr( $post_type->name ); ?>"
353
					id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>" checked>
354
355
				<label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
356
					<?php echo esc_html( $post_type->label ); ?>
357
				</label>
358
			</td>
359
		</tr>
360
361
		<?php endforeach; ?>
362
		<?php
363
	}
364
365
	/**
366
	 * Render the "private post" setting fields.
367
	 */
368
	protected function render_private_post_settings() {
369
		bd_render_private_post_settings( $this->field_slug );
370
	}
371
372
	/**
373
	 * Get the threshold after which enhanced select should be used.
374
	 *
375
	 * @return int Threshold.
376
	 */
377
	protected function get_enhanced_select_threshold() {
378
		/**
379
		 * Filter the enhanced select threshold.
380
		 *
381
		 * @since 6.0.0
382
		 *
383
		 * @param int Threshold.
384
		 */
385
		return apply_filters( 'bd_enhanced_select_threshold', 1000 );
386
	}
387
388
	/**
389
	 * Render sticky settings.
390
	 */
391
	protected function render_sticky_action_settings() {
392
		?>
393
		<tr>
394
			<td scope="row" colspan="2">
395
				<label>
396
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_sticky_action" value="unsticky" type="radio" checked>
397
					<?php _e( 'Remove Sticky', 'bulk-delete' ); ?>
398
				</label>
399
				<label>
400
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_sticky_action" value="delete" type="radio">
401
					<?php _e( 'Delete Post', 'bulk-delete' ); ?>
402
				</label>
403
			</td>
404
		</tr>
405
		<?php
406
	}
407
408
	/**
409
	 * Render filtering table header.
410
	 */
411
	protected function render_filtering_table_header() {
412
		bd_render_filtering_table_header();
413
	}
414
415
	/**
416
	 * Render restrict settings.
417
	 */
418
	protected function render_restrict_settings() {
419
		bd_render_restrict_settings( $this->field_slug, $this->item_type );
420
	}
421
422
	/**
423
	 * Render delete settings.
424
	 */
425
	protected function render_delete_settings() {
426
		bd_render_delete_settings( $this->field_slug );
427
		/**
428
		 * This action is primarily for adding delete attachment settings.
429
		 *
430
		 * @since 6.0.0
431
		 *
432
		 * @param \BulkWP\BulkDelete\Core\Base\BaseModule The delete module.
433
		 */
434
		do_action( 'bd_render_attachment_settings', $this );
435
	}
436
437
	/**
438
	 * Render limit settings.
439
	 */
440
	protected function render_limit_settings() {
441
		bd_render_limit_settings( $this->field_slug, $this->item_type );
442
	}
443
444
	/**
445
	 * Render cron settings based on whether scheduler is present or not.
446
	 */
447
	protected function render_cron_settings() {
448
		$disabled_attr = 'disabled';
449
		if ( empty( $this->scheduler_url ) ) {
450
			$disabled_attr = '';
451
		}
452
		?>
453
454
		<tr>
455
			<td scope="row" colspan="2">
456
				<label><input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="false" type="radio"
457
					checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?></label>
458
				<label><input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="true" type="radio"
459
					id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" <?php echo esc_attr( $disabled_attr ); ?>> <?php _e( 'Schedule', 'bulk-delete' ); ?></label>
460
				<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start"
461
					id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start" value="now"
462
					type="text" <?php echo esc_attr( $disabled_attr ); ?>><?php _e( 'repeat ', 'bulk-delete' ); ?>
463
464
				<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq"
465
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" <?php echo esc_attr( $disabled_attr ); ?>>
466
467
					<option value="-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option>
468
					<?php
469
					/**
470
					 * List of cron schedules.
471
					 *
472
					 * @since 6.0.0
473
					 *
474
					 * @param array                                   $cron_schedules List of cron schedules.
475
					 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module         Module.
476
					 */
477
					$cron_schedules = apply_filters( 'bd_cron_schedules', wp_get_schedules(), $this );
478
					?>
479
480
					<?php foreach ( $cron_schedules as $key => $value ) : ?>
481
						<option
482
							value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value['display'] ); ?></option>
483
					<?php endforeach; ?>
484
				</select>
485
486
				<?php if ( ! empty( $this->scheduler_url ) ) : ?>
487
					<?php
488
					$pro_class = 'bd-' . str_replace( '_', '-', $this->field_slug ) . '-pro';
489
490
					/**
491
					 * HTML class of the span that displays the 'Pro only feature' message.
492
					 *
493
					 * @since 6.0.0
494
					 *
495
					 * @param string                                  $pro_class  HTML class.
496
					 * @param string                                  $field_slug Field Slug of module.
497
					 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module     Module.
498
					 */
499
					apply_filters( 'bd_pro_only_feature_class', $pro_class, $this->field_slug, $this )
500
					?>
501
502
					<span class="<?php echo sanitize_html_class( $pro_class ); ?>" style="color:red">
503
						<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a
504
							href="<?php echo esc_url( $this->scheduler_url ); ?>">Buy now</a>
505
					</span>
506
				<?php endif; ?>
507
			</td>
508
		</tr>
509
510
		<tr class="<?php echo sanitize_html_class( $pro_class ); ?>" style="display: none;">
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pro_class does not seem to be defined for all execution paths leading up to this point.
Loading history...
511
			<td scope="row" colspan="2">
512
				<?php
513
				_e( 'Enter time in <strong>Y-m-d H:i:s</strong> format or enter <strong>now</strong> to use current time.', 'bulk-delete' );
514
				$link   = '<a href="https://bulkwp.com/docs/add-a-new-cron-schedule/">' . __( 'Click here', 'bulk-delete' ) . '</a>';
515
				$markup = sprintf( __( 'Want to add new a Cron schedule? %s', 'bulk-delete' ), $link );
516
517
				$content = __( 'Learn how to add your desired Cron schedule.', 'bulk-delete' );
518
				echo '&nbsp' . bd_generate_help_tooltip( $markup, $content );
519
				?>
520
			</td>
521
		</tr>
522
		<?php
523
	}
524
525
	/**
526
	 * Render submit button.
527
	 */
528
	protected function render_submit_button() {
529
		bd_render_submit_button( $this->action );
530
	}
531
}
532