Completed
Push — 353-fix/delete-post-revisions-... ( 1454a7...25d415 )
by Rajan
13:52 queued 10:52
created

Renderer::are_private_posts_present()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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="select2-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
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_taxonomy" class="select2-dropdown" multiple="multiple" data-placeholder="<?php _e( 'Select Taxonomy', 'bulk-delete' ); ?>">
122
			<?php foreach ( $taxonomies as $taxonomy ) : ?>
123
				<option value="<?php echo esc_attr( $taxonomy->name ); ?>">
124
					<?php echo esc_html( $taxonomy->label ); ?>
125
				</option>
126
			<?php endforeach; ?>
127
		</select>
128
		<?php
129
	}
130
131
	/*
132
	 * Render Category dropdown.
133
	 */
134
	protected function render_category_dropdown() {
135
		$categories = $this->get_categories();
136
		?>
137
138
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
139
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
0 ignored issues
show
Bug introduced by
The method enable_ajax_if_needed_to_dropdown_class_name() does not exist on BulkWP\BulkDelete\Core\Base\Mixin\Renderer. Since it exists in all sub-types, consider adding an abstract or default implementation to BulkWP\BulkDelete\Core\Base\Mixin\Renderer. ( Ignorable by Annotation )

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

139
				class="<?php echo sanitize_html_class( $this->/** @scrutinizer ignore-call */ enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
Loading history...
140
				data-taxonomy="category" multiple>
141
142
			<option value="all">
143
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
144
			</option>
145
146
			<?php foreach ( $categories as $category ) : ?>
147
				<option value="<?php echo absint( $category->cat_ID ); ?>">
148
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
149
				</option>
150
			<?php endforeach; ?>
151
152
		</select>
153
		<?php
154
	}
155
156
	/**
157
	 * Render Tags dropdown.
158
	 */
159
	protected function render_tags_dropdown() {
160
		$tags = $this->get_tags();
161
		?>
162
163
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
164
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
165
				data-taxonomy="post_tag" multiple>
166
167
			<option value="all">
168
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
169
			</option>
170
171
			<?php foreach ( $tags as $tag ) : ?>
172
				<option value="<?php echo absint( $tag->term_id ); ?>">
173
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
174
				</option>
175
			<?php endforeach; ?>
176
		</select>
177
		<?php
178
	}
179
180
	/**
181
	 * Render term options.
182
	 */
183
	protected function render_term_options() {
184
		?>
185
		<h4><?php _e( 'Delete terms that', 'bulk-delete' ); ?></h4>
186
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term_opt">
187
			<?php if( $this->field_slug == 'terms_by_name' ){ ?>
188
				<option value="equal_to">equal to</option>
189
				<option value="not_equal_to">not equal to</option>
190
				<option value="starts">starts</option>
191
				<option value="ends">ends</option>
192
				<option value="contains">contains</option>
193
				<option value="non_contains">non contains</option>
194
			<?php }elseif( $this->field_slug == 'terms_by_post_count' ){ ?>
195
				<option value="equal_to">equal to</option>
196
				<option value="not_equal_to">not equal to</option>
197
				<option value="less_than">less than</option>
198
				<option value="greater_than">greater than</option>
199
			<?php }?>
200
		</select> With 
201
		<input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term_text">
202
		<?php 
203
	}
204
205
	/**
206
	 * Render have post settings.
207
	 */
208
	protected function render_have_post_settings() {
209
		?>
210
		<p><label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts"><input type="checkbox" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_no_posts"> <?php _e( 'Only if it doesn\'t contain any post', 'bulk-delete' ); ?></label></p>
211
		<?php 
212
	}
213
214
	/*
215
	 * Render Sticky Posts dropdown.
216
	 */
217
	protected function render_sticky_post_dropdown() {
218
		$posts = $this->get_sticky_posts();
219
		?>
220
		<table class="optiontable">
221
			<tr>
222
				<td scope="row">
223
					<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="All">
224
					<label>All</label>
225
				</td>
226
			</tr>
227
			<?php
228
			foreach ( $posts as $post ) :
229
				$user = get_userdata( $post->post_author );
230
				?>
231
			<tr>
232
				<td scope="row">
233
				<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
234
				<label><?php echo esc_html( $post->post_title . ' Published by ' . $user->display_name . ' on ' . $post->post_date ); ?></label>
235
				</td>
236
			</tr>
237
			<?php endforeach; ?>
238
		</table>
239
		<?php
240
	}
241
242
	/**
243
	 * Render Post Types as checkboxes.
244
	 *
245
	 * @since 5.6.0
246
	 *
247
	 * @param string $name Name of post type checkboxes.
248
	 */
249
	protected function render_post_type_checkboxes( $name ) {
250
		$post_types = bd_get_post_types();
251
		?>
252
253
		<?php foreach ( $post_types as $post_type ) : ?>
254
255
		<tr>
256
			<td scope="row">
257
				<input type="checkbox" name="<?php echo esc_attr( $name ); ?>[]" value="<?php echo esc_attr( $post_type->name ); ?>"
258
					id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>" checked>
259
260
				<label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>">
261
					<?php echo esc_html( $post_type->label ); ?>
262
				</label>
263
			</td>
264
		</tr>
265
266
		<?php endforeach; ?>
267
		<?php
268
	}
269
270
	/**
271
	 * Get the threshold after which enhanced select should be used.
272
	 *
273
	 * @return int Threshold.
274
	 */
275
	protected function get_enhanced_select_threshold() {
276
		/**
277
		 * Filter the enhanced select threshold.
278
		 *
279
		 * @since 6.0.0
280
		 *
281
		 * @param int Threshold.
282
		 */
283
		return apply_filters( 'bd_enhanced_select_threshold', 1000 );
284
	}
285
286
	/**
287
	 * Check there is have any private post is exist.
288
	 *
289
	 * @since 6.0.0
290
	 *
291
	 * @param string $post_type.
292
	 *
293
	 * @return bool
294
	 */
295
	public function are_private_posts_present( $post_type='any') {
296
		$args  = array(
297
			'post_status' => array( 'private' ),
298
			'post_type'   => $post_type,
299
		);
300
		$query = new \WP_Query( $args );
301
302
		return $query->have_posts();
303
	}
304
}
305