Completed
Push — 383-fix/add-tests-for-comparis... ( a2ea6f...61deeb )
by Sudar
42:36 queued 27:33
created

Renderer::render_user_role_dropdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 13
ccs 0
cts 4
cp 0
crap 6
rs 9.8333
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
	 * Render Post Types as radio buttons.
18
	 */
19
	protected function render_post_type_as_radios() {
20
		$field_slug = $this->field_slug;
21
22
		$post_types = $this->get_post_types();
23
		?>
24
25
		<?php foreach ( $post_types as $post_type ) : ?>
26
27
			<tr>
28
				<td scope="row">
29
					<input type="radio" name="<?php echo esc_attr( $field_slug ); ?>_post_type"
30
						value="<?php echo esc_attr( $post_type->name ); ?>"
31
						id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
32
33
					<label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
34
						<?php echo esc_html( $post_type->label ); ?>
35
					</label>
36
				</td>
37
			</tr>
38
39
		<?php endforeach; ?>
40
		<?php
41
	}
42
43
	/**
44
	 * Render Post type with status and post count checkboxes.
45
	 */
46
	protected function render_post_type_with_status() {
47
		$post_types_by_status = $this->get_post_types_by_status();
48
		?>
49
		<tr>
50
			<td scope="row" colspan="2">
51
				<select class="enhanced-post-types-with-status" multiple="multiple" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]">
52
				<?php foreach ( $post_types_by_status as $post_type => $all_status ) : ?>
53
					<optgroup label="<?php echo esc_html( $post_type ); ?>">
54
					<?php foreach ( $all_status as $status_key => $status_value ) : ?>
55
						<option value="<?php echo esc_attr( $status_key ); ?>"><?php echo esc_html( $status_value ); ?></option>
56
					<?php endforeach; ?>
57
					</optgroup>
58
				<?php endforeach; ?>
59
				</select>
60
			</td>
61
		</tr>
62
		<?php
63
	}
64
65
	/**
66
	 * Split post type and status.
67
	 *
68
	 * @param string $str Post type and status combination.
69
	 *
70
	 * @return array Post type and status as elements of array.
71
	 */
72 9
	protected function split_post_type_and_status( $str ) {
73 9
		$type_status = array();
74
75 9
		$str_arr = explode( '-', $str );
76
77 9
		if ( count( $str_arr ) > 1 ) {
78
			$type_status['status'] = end( $str_arr );
79
			$type_status['type']   = implode( '-', array_slice( $str_arr, 0, - 1 ) );
80
		} else {
81 9
			$type_status['status'] = 'publish';
82 9
			$type_status['type']   = $str;
83
		}
84
85 9
		return $type_status;
86
	}
87
88
	/**
89
	 * Render user role dropdown.
90
	 */
91
	protected function render_user_role_dropdown() {
92
		global $wp_roles;
93
		?>
94
95
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_roles[]" class="enhanced-dropdown"
96
				multiple="multiple" data-placeholder="<?php _e( 'Select User Role', 'bulk-delete' ); ?>">
97
98
			<?php foreach ( $wp_roles->roles as $role => $role_details ) : ?>
99
				<option value="<?php echo esc_attr( $role ); ?>">
100
					<?php echo esc_html( $role_details['name'] ), ' (', absint( $this->get_user_count_by_role( $role ) ), ' ', __( 'Users', 'bulk-delete' ), ')'; ?>
101
				</option>
102
			<?php endforeach; ?>
103
		</select>
104
105
		<?php
106
	}
107
108
	/**
109
	 * Render Post type dropdown.
110
	 */
111
	protected function render_post_type_dropdown() {
112
		bd_render_post_type_dropdown( $this->field_slug );
113
	}
114
115
	/**
116
	 * Render Taxonomy dropdown.
117
	 */
118
	protected function render_taxonomy_dropdown() {
119
		$taxonomies = get_taxonomies( array(), 'objects' );
120
		?>
121
122
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_taxonomy" class="enhanced-taxonomy-list" data-placeholder="<?php _e( 'Select Taxonomy', 'bulk-delete' ); ?>">
123
			<?php foreach ( $taxonomies as $taxonomy ) : ?>
124
				<option value="<?php echo esc_attr( $taxonomy->name ); ?>">
125
					<?php echo esc_html( $taxonomy->label ); ?>
126
				</option>
127
			<?php endforeach; ?>
128
		</select>
129
		<?php
130
	}
131
132
	/**
133
	 * Render Category dropdown.
134
	 */
135
	protected function render_category_dropdown() {
136
		$categories = $this->get_categories();
137
		?>
138
139
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
140
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
141
				data-taxonomy="category" multiple>
142
143
			<option value="all">
144
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
145
			</option>
146
147
			<?php foreach ( $categories as $category ) : ?>
148
				<option value="<?php echo absint( $category->cat_ID ); ?>">
149
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
150
				</option>
151
			<?php endforeach; ?>
152
153
		</select>
154
		<?php
155
	}
156
157
	/**
158
	 * Render String based comparison operators dropdown.
159
	 */
160
	protected function render_string_comparison_operators() {
161
		?>
162
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_operator">
163
			<option value="equal_to"><?php _e( 'equal to', 'bulk-delete' ); ?></option>
164
			<option value="not_equal_to"><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
165
			<option value="starts_with"><?php _e( 'starts with', 'bulk-delete' ); ?></option>
166
			<option value="ends_with"><?php _e( 'ends with', 'bulk-delete' ); ?></option>
167
			<option value="contains"><?php _e( 'contains', 'bulk-delete' ); ?></option>
168
			<option value="not_contains"><?php _e( 'not contains', 'bulk-delete' ); ?></option>
169
		</select>
170
		<?php
171
	}
172
173
	/**
174
	 * Render number based comparison operators dropdown.
175
	 */
176
	protected function render_number_comparison_operators() {
177
		?>
178
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_operator">
179
			<option value="equal_to"><?php _e( 'equal to', 'bulk-delete' ); ?></option>
180
			<option value="not_equal_to"><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
181
			<option value="less_than"><?php _e( 'less than', 'bulk-delete' ); ?></option>
182
			<option value="greater_than"><?php _e( 'greater than', 'bulk-delete' ); ?></option>
183
		</select>
184
		<?php
185
	}
186
187
	/**
188
	 * Render Tags dropdown.
189
	 */
190
	protected function render_tags_dropdown() {
191
		$tags = $this->get_tags();
192
		?>
193
194
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
195
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
196
				data-taxonomy="post_tag" multiple>
197
198
			<option value="all">
199
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
200
			</option>
201
202
			<?php foreach ( $tags as $tag ) : ?>
203
				<option value="<?php echo absint( $tag->term_id ); ?>">
204
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
205
				</option>
206
			<?php endforeach; ?>
207
		</select>
208
		<?php
209
	}
210
211
	/**
212
	 * Get the class name for select2 dropdown based on the number of items present.
213
	 *
214
	 * @param int    $count      The number of items present.
215
	 * @param string $class_name Primary class name.
216
	 *
217
	 * @return string Class name.
218
	 */
219
	protected function enable_ajax_if_needed_to_dropdown_class_name( $count, $class_name ) {
220
		if ( $count >= $this->get_enhanced_select_threshold() ) {
221
			$class_name .= '-ajax';
222
		}
223
224
		return $class_name;
225
	}
226
227
	/**
228
	 * Render Sticky Posts dropdown.
229
	 */
230
	protected function render_sticky_post_dropdown() {
231
		$posts = $this->get_sticky_posts();
232
		?>
233
		<table class="optiontable">
234
			<tr>
235
				<td scope="row">
236
					<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="All">
237
					<label>All</label>
238
				</td>
239
			</tr>
240
			<?php
241
			foreach ( $posts as $post ) :
242
				$user = get_userdata( $post->post_author );
243
				?>
244
			<tr>
245
				<td scope="row">
246
				<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
247
				<label><?php echo esc_html( $post->post_title . ' Published by ' . $user->display_name . ' on ' . $post->post_date ); ?></label>
248
				</td>
249
			</tr>
250
			<?php endforeach; ?>
251
		</table>
252
		<?php
253
	}
254
255
	/**
256
	 * Render Post Types as checkboxes.
257
	 *
258
	 * @since 5.6.0
259
	 *
260
	 * @param string $name Name of post type checkboxes.
261
	 */
262
	protected function render_post_type_checkboxes( $name ) {
263
		$post_types = bd_get_post_types();
264
		?>
265
266
		<?php foreach ( $post_types as $post_type ) : ?>
267
268
		<tr>
269
			<td scope="row">
270
				<input type="checkbox" name="<?php echo esc_attr( $name ); ?>[]" value="<?php echo esc_attr( $post_type->name ); ?>"
271
					id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>" checked>
272
273
				<label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
274
					<?php echo esc_html( $post_type->label ); ?>
275
				</label>
276
			</td>
277
		</tr>
278
279
		<?php endforeach; ?>
280
		<?php
281
	}
282
283
	/**
284
	 * Render the "private post" setting fields.
285
	 */
286
	protected function render_private_post_settings() {
287
		bd_render_private_post_settings( $this->field_slug );
288
	}
289
290
	/**
291
	 * Get the threshold after which enhanced select should be used.
292
	 *
293
	 * @return int Threshold.
294
	 */
295
	protected function get_enhanced_select_threshold() {
296
		/**
297
		 * Filter the enhanced select threshold.
298
		 *
299
		 * @since 6.0.0
300
		 *
301
		 * @param int Threshold.
302
		 */
303
		return apply_filters( 'bd_enhanced_select_threshold', 1000 );
304
	}
305
}
306