Passed
Push — 505-feature/tooltip-on-cron-sc... ( e62245...487564 )
by Maria Daniel Deepak
12:56
created

Renderer   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 444
Duplicated Lines 0 %

Test Coverage

Coverage 6.8%

Importance

Changes 0
Metric Value
eloc 248
dl 0
loc 444
ccs 7
cts 103
cp 0.068
rs 9.36
c 0
b 0
f 0
wmc 38

22 Methods

Rating   Name   Duplication   Size   Complexity  
A render_post_type_dropdown() 0 2 1
A render_string_comparison_operators() 0 9 1
A render_number_comparison_operators() 0 7 1
A get_enhanced_select_threshold() 0 9 1
A render_post_type_as_radios() 0 20 2
A render_limit_settings() 0 2 1
A render_tags_dropdown() 0 18 2
A render_sticky_posts_dropdown() 0 32 3
A render_user_role_dropdown() 0 13 2
A render_private_post_settings() 0 2 1
A enable_ajax_if_needed_to_dropdown_class_name() 0 6 2
A render_filtering_table_header() 0 2 1
A render_delete_settings() 0 2 1
A render_post_type_with_status() 0 14 3
A render_post_type_checkboxes() 0 19 2
A split_post_type_and_status() 0 14 2
A render_category_dropdown() 0 18 2
A render_sticky_action_settings() 0 12 1
A render_restrict_settings() 0 2 1
A render_taxonomy_dropdown() 0 11 2
A render_submit_button() 0 2 1
B render_cron_settings() 0 78 5
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 Types as radio buttons.
25
	 */
26
	protected function render_post_type_as_radios() {
27
		$post_types = $this->get_post_types();
28
		?>
29
30
		<?php foreach ( $post_types as $post_type ) : ?>
31
32
			<tr>
33
				<td scope="row">
34
					<input type="radio" name="<?php echo esc_attr( $this->field_slug ); ?>_post_type"
35
						value="<?php echo esc_attr( $post_type->name ); ?>"
36
						id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
37
38
					<label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
39
						<?php echo esc_html( $post_type->label ); ?>
40
					</label>
41
				</td>
42
			</tr>
43
44
		<?php endforeach; ?>
45
		<?php
46
	}
47
48
	/**
49
	 * Render Post type with status and post count checkboxes.
50
	 */
51
	protected function render_post_type_with_status() {
52
		$post_types_by_status = $this->get_post_types_by_status();
53
		?>
54
		<tr>
55
			<td scope="row" colspan="2">
56
				<select class="enhanced-post-types-with-status" multiple="multiple" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]">
57
				<?php foreach ( $post_types_by_status as $post_type => $all_status ) : ?>
58
					<optgroup label="<?php echo esc_html( $post_type ); ?>">
59
					<?php foreach ( $all_status as $status_key => $status_value ) : ?>
60
						<option value="<?php echo esc_attr( $status_key ); ?>"><?php echo esc_html( $status_value ); ?></option>
61
					<?php endforeach; ?>
62
					</optgroup>
63
				<?php endforeach; ?>
64
				</select>
65
			</td>
66
		</tr>
67
		<?php
68
	}
69
70
	/**
71
	 * Split post type and status.
72
	 *
73
	 * @param string $str Post type and status combination.
74
	 *
75
	 * @return array Post type and status as elements of array.
76
	 */
77 9
	protected function split_post_type_and_status( $str ) {
78 9
		$type_status = array();
79
80 9
		$str_arr = explode( '-', $str );
81
82 9
		if ( count( $str_arr ) > 1 ) {
83
			$type_status['status'] = end( $str_arr );
84
			$type_status['type']   = implode( '-', array_slice( $str_arr, 0, - 1 ) );
85
		} else {
86 9
			$type_status['status'] = 'publish';
87 9
			$type_status['type']   = $str;
88
		}
89
90 9
		return $type_status;
91
	}
92
93
	/**
94
	 * Render user role dropdown.
95
	 */
96
	protected function render_user_role_dropdown() {
97
		global $wp_roles;
98
		?>
99
100
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_roles[]" class="enhanced-role-dropdown"
101
				multiple="multiple" data-placeholder="<?php _e( 'Select User Role', 'bulk-delete' ); ?>">
102
103
			<?php foreach ( $wp_roles->roles as $role => $role_details ) : ?>
104
				<option value="<?php echo esc_attr( $role ); ?>">
105
					<?php echo esc_html( $role_details['name'] ), ' (', absint( $this->get_user_count_by_role( $role ) ), ' ', __( 'Users', 'bulk-delete' ), ')'; ?>
106
				</option>
107
			<?php endforeach; ?>
108
		</select>
109
110
		<?php
111
	}
112
113
	/**
114
	 * Render Post type dropdown.
115
	 */
116
	protected function render_post_type_dropdown() {
117
		bd_render_post_type_dropdown( $this->field_slug );
118
	}
119
120
	/**
121
	 * Render Taxonomy dropdown.
122
	 */
123
	protected function render_taxonomy_dropdown() {
124
		$taxonomies = get_taxonomies( array(), 'objects' );
125
		?>
126
127
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_taxonomy" class="enhanced-taxonomy-list" data-placeholder="<?php _e( 'Select Taxonomy', 'bulk-delete' ); ?>">
128
			<?php foreach ( $taxonomies as $taxonomy ) : ?>
129
				<option value="<?php echo esc_attr( $taxonomy->name ); ?>">
130
					<?php echo esc_html( $taxonomy->label . ' (' . $taxonomy->name . ')' ); ?>
131
				</option>
132
			<?php endforeach; ?>
133
		</select>
134
		<?php
135
	}
136
137
	/**
138
	 * Render Category dropdown.
139
	 */
140
	protected function render_category_dropdown() {
141
		$categories = $this->get_categories();
142
		?>
143
144
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
145
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
146
				data-taxonomy="category" multiple>
147
148
			<option value="all">
149
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
150
			</option>
151
152
			<?php foreach ( $categories as $category ) : ?>
153
				<option value="<?php echo absint( $category->cat_ID ); ?>">
154
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
155
				</option>
156
			<?php endforeach; ?>
157
158
		</select>
159
		<?php
160
	}
161
162
	/**
163
	 * Render String based comparison operators dropdown.
164
	 */
165
	protected function render_string_comparison_operators() {
166
		?>
167
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_operator">
168
			<option value="equal_to"><?php _e( 'equal to', 'bulk-delete' ); ?></option>
169
			<option value="not_equal_to"><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
170
			<option value="starts_with"><?php _e( 'starts with', 'bulk-delete' ); ?></option>
171
			<option value="ends_with"><?php _e( 'ends with', 'bulk-delete' ); ?></option>
172
			<option value="contains"><?php _e( 'contains', 'bulk-delete' ); ?></option>
173
			<option value="not_contains"><?php _e( 'not contains', 'bulk-delete' ); ?></option>
174
		</select>
175
		<?php
176
	}
177
178
	/**
179
	 * Render number based comparison operators dropdown.
180
	 */
181
	protected function render_number_comparison_operators() {
182
		?>
183
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_operator">
184
			<option value="equal_to"><?php _e( 'equal to', 'bulk-delete' ); ?></option>
185
			<option value="not_equal_to"><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
186
			<option value="less_than"><?php _e( 'less than', 'bulk-delete' ); ?></option>
187
			<option value="greater_than"><?php _e( 'greater than', 'bulk-delete' ); ?></option>
188
		</select>
189
		<?php
190
	}
191
192
	/**
193
	 * Render Tags dropdown.
194
	 */
195
	protected function render_tags_dropdown() {
196
		$tags = $this->get_tags();
197
		?>
198
199
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
200
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
201
				data-taxonomy="post_tag" multiple>
202
203
			<option value="all">
204
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
205
			</option>
206
207
			<?php foreach ( $tags as $tag ) : ?>
208
				<option value="<?php echo absint( $tag->term_id ); ?>">
209
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
210
				</option>
211
			<?php endforeach; ?>
212
		</select>
213
		<?php
214
	}
215
216
	/**
217
	 * Get the class name for select2 dropdown based on the number of items present.
218
	 *
219
	 * @param int    $count      The number of items present.
220
	 * @param string $class_name Primary class name.
221
	 *
222
	 * @return string Class name.
223
	 */
224
	protected function enable_ajax_if_needed_to_dropdown_class_name( $count, $class_name ) {
225
		if ( $count >= $this->get_enhanced_select_threshold() ) {
226
			$class_name .= '-ajax';
227
		}
228
229
		return $class_name;
230
	}
231
232
	/**
233
	 * Render Sticky Posts dropdown.
234
	 */
235
	protected function render_sticky_posts_dropdown() {
236
		$sticky_posts = $this->get_sticky_posts();
237
		?>
238
239
		<table class="optiontable">
240
			<?php if ( count( $sticky_posts ) > 1 ) : ?>
241
				<tr>
242
					<td scope="row">
243
						<label>
244
							<input type="checkbox" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="all" checked>
245
							<?php echo __( 'All sticky posts', 'bulk-delete' ), ' (', count( $sticky_posts ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
246
						</label>
247
					</td>
248
				</tr>
249
			<?php endif; ?>
250
251
			<?php foreach ( $sticky_posts as $post ) : ?>
252
				<?php $author = get_userdata( $post->post_author ); ?>
253
				<tr>
254
					<td scope="row">
255
						<label>
256
							<input type="checkbox" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
257
							<?php
258
								echo esc_html( $post->post_title ), ' - ',
259
									__( 'Published on', 'bulk-delete' ), ' ', get_the_date( get_option( 'date_format' ), $post->ID ),
0 ignored issues
show
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

259
									__( 'Published on', 'bulk-delete' ), ' ', /** @scrutinizer ignore-type */ get_the_date( get_option( 'date_format' ), $post->ID ),
Loading history...
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

259
									__( 'Published on', 'bulk-delete' ), ' ', get_the_date( /** @scrutinizer ignore-type */ get_option( 'date_format' ), $post->ID ),
Loading history...
260
									__( ' by ', 'bulk-delete' ), esc_html( $author->display_name );
261
							?>
262
						</label>
263
					</td>
264
				</tr>
265
			<?php endforeach; ?>
266
		</table>
267
		<?php
268
	}
269
270
	/**
271
	 * Render Post Types as checkboxes.
272
	 *
273
	 * @since 5.6.0
274
	 *
275
	 * @param string $name Name of post type checkboxes.
276
	 */
277
	protected function render_post_type_checkboxes( $name ) {
278
		$post_types = bd_get_post_types();
279
		?>
280
281
		<?php foreach ( $post_types as $post_type ) : ?>
282
283
		<tr>
284
			<td scope="row">
285
				<input type="checkbox" name="<?php echo esc_attr( $name ); ?>[]" value="<?php echo esc_attr( $post_type->name ); ?>"
286
					id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>" checked>
287
288
				<label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
289
					<?php echo esc_html( $post_type->label ); ?>
290
				</label>
291
			</td>
292
		</tr>
293
294
		<?php endforeach; ?>
295
		<?php
296
	}
297
298
	/**
299
	 * Render the "private post" setting fields.
300
	 */
301
	protected function render_private_post_settings() {
302
		bd_render_private_post_settings( $this->field_slug );
303
	}
304
305
	/**
306
	 * Get the threshold after which enhanced select should be used.
307
	 *
308
	 * @return int Threshold.
309
	 */
310
	protected function get_enhanced_select_threshold() {
311
		/**
312
		 * Filter the enhanced select threshold.
313
		 *
314
		 * @since 6.0.0
315
		 *
316
		 * @param int Threshold.
317
		 */
318
		return apply_filters( 'bd_enhanced_select_threshold', 1000 );
319
	}
320
321
	/**
322
	 * Render sticky settings.
323
	 */
324
	protected function render_sticky_action_settings() {
325
		?>
326
		<tr>
327
			<td scope="row" colspan="2">
328
				<label>
329
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_sticky_action" value="unsticky" type="radio" checked>
330
					<?php _e( 'Remove Sticky', 'bulk-delete' ); ?>
331
				</label>
332
				<label>
333
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_sticky_action" value="delete" type="radio">
334
					<?php _e( 'Delete Post', 'bulk-delete' ); ?>
335
				</label>
336
			</td>
337
		</tr>
338
		<?php
339
	}
340
341
	/**
342
	 * Render filtering table header.
343
	 */
344
	protected function render_filtering_table_header() {
345
		bd_render_filtering_table_header();
346
	}
347
348
	/**
349
	 * Render restrict settings.
350
	 */
351
	protected function render_restrict_settings() {
352
		bd_render_restrict_settings( $this->field_slug, $this->item_type );
353
	}
354
355
	/**
356
	 * Render delete settings.
357
	 */
358
	protected function render_delete_settings() {
359
		bd_render_delete_settings( $this->field_slug );
360
	}
361
362
	/**
363
	 * Render limit settings.
364
	 */
365
	protected function render_limit_settings() {
366
		bd_render_limit_settings( $this->field_slug, $this->item_type );
367
	}
368
369
	/**
370
	 * Render cron settings based on whether scheduler is present or not.
371
	 *
372
	 * @param bool $is_scheduler_enabled Displays Scheduler helper text when TRUE.
373
	 */
374
	protected function render_cron_settings( $is_scheduler_enabled = false ) {
375
		$disabled_attr = 'disabled';
376
		if ( empty( $this->scheduler_url ) ) {
377
			$disabled_attr = '';
378
		}
379
		?>
380
381
		<tr>
382
			<td scope="row" colspan="2">
383
				<label><input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="false" type="radio"
384
					checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?></label>
385
				<label><input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="true" type="radio"
386
					id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" <?php echo esc_attr( $disabled_attr ); ?>> <?php _e( 'Schedule', 'bulk-delete' ); ?></label>
387
				<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start"
388
					id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start" value="now"
389
					type="text" <?php echo esc_attr( $disabled_attr ); ?>><?php _e( 'repeat ', 'bulk-delete' ); ?>
390
391
				<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq"
392
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" <?php echo esc_attr( $disabled_attr ); ?>>
393
394
					<option value="-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option>
395
					<?php
396
					/**
397
					 * List of cron schedules.
398
					 *
399
					 * @since 6.0.0
400
					 *
401
					 * @param array                                   $cron_schedules List of cron schedules.
402
					 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module         Module.
403
					 */
404
					$cron_schedules = apply_filters( 'bd_cron_schedules', wp_get_schedules(), $this );
405
					?>
406
407
					<?php foreach ( $cron_schedules as $key => $value ) : ?>
408
						<option
409
							value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value['display'] ); ?></option>
410
					<?php endforeach; ?>
411
				</select>
412
413
				<?php if ( ! empty( $this->scheduler_url ) ) : ?>
414
					<?php
415
					$pro_class = 'bd-' . str_replace( '_', '-', $this->field_slug ) . '-pro';
416
417
					/**
418
					 * HTML class of the span that displays the 'Pro only feature' message.
419
					 *
420
					 * @since 6.0.0
421
					 *
422
					 * @param string                                  $pro_class  HTML class.
423
					 * @param string                                  $field_slug Field Slug of module.
424
					 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module     Module.
425
					 */
426
					apply_filters( 'bd_pro_only_feature_class', $pro_class, $this->field_slug, $this )
427
					?>
428
429
					<span class="<?php echo sanitize_html_class( $pro_class ); ?>" style="color:red">
430
						<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a
431
							href="<?php echo esc_url( $this->scheduler_url ); ?>">Buy now</a>
432
					</span>
433
				<?php endif; ?>
434
			</td>
435
		</tr>
436
437
		<?php if ( $is_scheduler_enabled ) : ?>
438
			<tr>
439
				<td scope="row" colspan="2">
440
					<?php
441
					_e( 'Enter time in <strong>Y-m-d H:i:s</strong> format or enter <strong>now</strong> to use current time', 'bulk-delete' );
442
					$link   = '<a href="https://bulkwp.com/docs/add-a-new-cron-schedule/">' . __( 'Click here', 'bulk-delete' ) . '</a>';
443
					$markup = sprintf( __( 'Want to add new a Cron schedule? %s', 'bulk-delete' ), $link );
444
445
					$content = __( 'Learn how to add your desired Cron schedule.', 'bulk-delete' );
446
					echo '&nbsp' . bd_generate_help_tooltip( $markup, $content );
447
					?>
448
				</td>
449
			</tr>
450
		<?php endif; ?>
451
		<?php
452
	}
453
454
	/**
455
	 * Render submit button.
456
	 */
457
	protected function render_submit_button() {
458
		bd_render_submit_button( $this->action );
459
	}
460
}
461