Passed
Push — 265-fix/private-post-filter ( ba668e...255598 )
by Rajan
17:17 queued 14:21
created

are_private_posts_present()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Utility functions for displaying form.
4
 *
5
 * @since      5.5
6
 *
7
 * @author     Sudar
8
 *
9
 * @package    BulkDelete\Ui
10
 */
11
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
12
13
/**
14
 * Render filtering table header.
15
 *
16
 * @since 5.5
17
 */
18
function bd_render_filtering_table_header() {
19
?>
20
	<tr>
21
		<td colspan="2">
22
			<h4><?php _e( 'Choose your filtering options', 'bulk-delete' ); ?></h4>
23
		</td>
24
	</tr>
25
<?php
26
}
27
28
/**
29
 * Render "restrict by created date" dropdown.
30
 *
31
 * @since 5.5
32
 *
33
 * @param string $slug The slug to be used in field names.
34
 * @param string $item (optional) Item for which form is displayed. Default is 'posts'.
35
 */
36
function bd_render_restrict_settings( $slug, $item = 'posts' ) {
37
?>
38
	<tr>
39
		<td scope="row">
40
			<input name="smbd_<?php echo $slug; ?>_restrict" id="smbd_<?php echo $slug; ?>_restrict" value="true" type="checkbox">
41
		</td>
42
		<td>
43
			<?php printf( __( 'Only restrict to %s which are ', 'bulk-delete' ), $item );?>
44
			<select name="smbd_<?php echo $slug; ?>_op" id="smbd_<?php echo $slug; ?>_op" disabled>
45
				<option value="before"><?php _e( 'older than', 'bulk-delete' );?></option>
46
				<option value="after"><?php _e( 'posted within last', 'bulk-delete' );?></option>
47
			</select>
48
			<input type="number" name="smbd_<?php echo $slug; ?>_days" id="smbd_<?php echo $slug; ?>_days" class="screen-per-page" disabled value="0" min="0"><?php _e( 'days', 'bulk-delete' );?>
49
		</td>
50
	</tr>
51
<?php
52
}
53
54
/**
55
 * Render "force delete" setting fields.
56
 *
57
 * @since 5.5
58
 *
59
 * @param string $slug The slug to be used in field names.
60
 */
61
function bd_render_delete_settings( $slug ) {
62
?>
63
	<tr>
64
		<td scope="row" colspan="2" class="<?php if( $slug == 'sticky_post' ){ ?>sticky_force_delete<?php }?>">
65
			<input name="smbd_<?php echo $slug; ?>_force_delete" value="false" type="radio" checked> <?php _e( 'Move to Trash', 'bulk-delete' ); ?>
66
			<input name="smbd_<?php echo $slug; ?>_force_delete" value="true" type="radio"> <?php _e( 'Delete permanently', 'bulk-delete' ); ?>
67
		</td>
68
	</tr>
69
<?php
70
}
71
72
/**
73
 * Render "sticky option" setting fields.
74
 *
75
 * @since 6.0
76
 *
77
 * @param string $slug The slug to be used in field names.
78
 */
79
function bd_render_sticky_settings( $slug ) {
80
?>
81
	<tr>
82
		<td scope="row" colspan="2">
83
			<input name="smbd_<?php echo $slug; ?>_sticky_option" value="hide" type="radio" checked> <?php _e( 'Remove Sticky', 'bulk-delete' ); ?>
84
			<input name="smbd_<?php echo $slug; ?>_sticky_option" value="show" type="radio"> <?php _e( 'Delete Post', 'bulk-delete' ); ?>
85
		</td>
86
	</tr>
87
<?php
88
}
89
90
/**
91
 * Check there is have any private post is exist.
92
 *
93
 * @since 6.0.0
94
 *
95
 * @param string $post_type.
96
 */
97
function are_private_posts_present( $post_type='any') {
98
	$args  = array(
99
		'post_status' => array( 'private' ),
100
		'post_type'   => $post_type
101
	);
102
	$query = new WP_Query( $args );
103
	if ( $query->have_posts() ) {
104
		return true;
105
	}
106
	return false;
107
}
108
109
/**
110
 * Render the "private post" setting fields.
111
 *
112
 * @since 5.5
113
 *
114
 * @param string $slug The slug to be used in field names.
115
 */
116
function bd_render_private_post_settings( $slug ) {
117
	if ( are_private_posts_present() ) {
118
		?>
119
	<tr>
120
		<td scope="row" colspan="2">
121
			<input name="smbd_<?php echo $slug; ?>_private" value="false" type="radio" checked> <?php _e( 'Public posts', 'bulk-delete' ); ?>
122
			<input name="smbd_<?php echo $slug; ?>_private" value="true" type="radio"> <?php _e( 'Private Posts', 'bulk-delete' ); ?>
123
		</td>
124
	</tr>
125
		<?php
126
	}
127
}
128
129
/**
130
 * Render the "limit" setting fields.
131
 *
132
 * @since 5.5
133
 *
134
 * @param string $slug The slug to be used in field names.
135
 * @param string $item (Optional) Item type. Possible values are 'posts', 'pages', 'users'
136
 */
137
function bd_render_limit_settings( $slug, $item = 'posts' ) {
138
?>
139
	<tr>
140
		<td scope="row">
141
			<input name="smbd_<?php echo $slug; ?>_limit" id="smbd_<?php echo $slug; ?>_limit" value="true" type="checkbox">
142
		</td>
143
		<td>
144
			<?php _e( 'Only delete first ', 'bulk-delete' );?>
145
			<input type="number" name="smbd_<?php echo $slug; ?>_limit_to" id="smbd_<?php echo $slug; ?>_limit_to" class="screen-per-page" disabled value="0" min="0"> <?php echo $item;?>.
146
			<?php printf( __( 'Use this option if there are more than 1000 %s and the script times out.', 'bulk-delete' ), $item ); ?>
147
		</td>
148
	</tr>
149
<?php
150
}
151
152
/**
153
 * Render cron setting fields.
154
 *
155
 * @since 5.5
156
 *
157
 * @param string $slug      The slug to be used in field names.
158
 * @param string $addon_url Url for the pro addon.
159
 */
160
function bd_render_cron_settings( $slug, $addon_url ) {
161
	$pro_class = 'bd-' . str_replace( '_', '-', $slug ) . '-pro';
162
?>
163
	<tr>
164
		<td scope="row" colspan="2">
165
			<input name="smbd_<?php echo $slug; ?>_cron" value="false" type="radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?>
166
			<input name="smbd_<?php echo $slug; ?>_cron" value="true" type="radio" id="smbd_<?php echo $slug; ?>_cron" disabled > <?php _e( 'Schedule', 'bulk-delete' ); ?>
167
			<input name="smbd_<?php echo $slug; ?>_cron_start" id="smbd_<?php echo $slug; ?>_cron_start" value="now" type="text" disabled><?php _e( 'repeat ', 'bulk-delete' );?>
168
			<select name="smbd_<?php echo $slug; ?>_cron_freq" id="smbd_<?php echo $slug; ?>_cron_freq" disabled>
169
				<option value="-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option>
170
<?php
171
	$schedules = wp_get_schedules();
172
	foreach ( $schedules as $key => $value ) {
173
?>
174
				<option value="<?php echo $key; ?>"><?php echo $value['display']; ?></option>
175
<?php } ?>
176
			</select>
177
			<span class="<?php echo sanitize_html_class( apply_filters( 'bd_pro_class', $pro_class, $slug ) ); ?>" style="color:red"><?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href="<?php echo $addon_url; ?>">Buy now</a></span>
178
		</td>
179
	</tr>
180
181
	<tr>
182
		<td scope="row" colspan="2">
183
			<?php _e( 'Enter time in <strong>Y-m-d H:i:s</strong> format or enter <strong>now</strong> to use current time', 'bulk-delete' );?>
184
		</td>
185
	</tr>
186
<?php
187
}
188
189
/**
190
 * Render the submit button.
191
 *
192
 * @since 5.5
193
 *
194
 * @param string $action The action attribute of the submit button.
195
 */
196
function bd_render_submit_button( $action ) {
197
?>
198
	<p class="submit">
199
		<button type="submit" name="bd_action" value="<?php echo esc_attr( $action ); ?>" class="button-primary"><?php _e( 'Bulk Delete ', 'bulk-delete' ); ?>&raquo;</button>
200
	</p>
201
<?php
202
}
203
204
/**
205
 * Get the list of post type objects that will be used in filters.
206
 *
207
 * @since 5.6.0
208
 *
209
 * @return \WP_Post_Type[] List of post type objects.
210
 */
211
function bd_get_post_types() {
212
	$custom_types = get_post_types( array( '_builtin' => false ), 'objects' );
213
214
	$builtin_types = array(
215
		'post' => get_post_type_object( 'post' ),
216
		'page' => get_post_type_object( 'page' ),
217
	);
218
219
	return array_merge( $builtin_types, $custom_types );
220
}
221
222
/**
223
 * Render Post type dropdown.
224
 *
225
 * @param string $field_slug Field slug.
226
 */
227
function bd_render_post_type_dropdown( $field_slug ) {
228
	$types = bd_get_post_types();
229
	?>
230
	<tr>
231
		<td scope="row" >
232
			<select class="select2" name="smbd_<?php echo esc_attr( $field_slug ); ?>_post_type">
233
				<?php foreach ( $types as $type ) : ?>
234
					<option value="<?php echo esc_attr( $type->name ); ?>">
235
						<?php echo esc_html( $type->labels->singular_name . ' (' . $type->name . ')' ); ?>
236
					</option>
237
				<?php endforeach; ?>
238
			</select>
239
		</td>
240
	</tr>
241
	<?php
242
}
243
244
/**
245
 * Render the post status filter.
246
 *
247
 * @since 5.6.0
248
 *
249
 * @param string $slug     The slug to be used in field names.
250
 * @param string $selected Default selected status.
251
 */
252
function bd_render_post_status_filter( $slug, $selected = 'publish' ) {
253
	$post_statuses = bd_get_post_statuses();
254
255
	foreach ( $post_statuses as $key => $value ) {
256
		?>
257
		<tr>
258
			<td>
259
				<label>
260
					<input name="smbd_<?php echo esc_attr( $slug ); ?>_post_status[]" type="checkbox"
261
							value="<?php echo esc_attr( $key ); ?>" <?php checked( $key, $selected ); ?>>
262
263
					<?php echo __( 'All', 'bulk-delete' ), ' ', esc_html( $value->label ), ' ', __( 'Posts', 'bulk-delete' ); ?>
264
				</label>
265
			</td>
266
		</tr>
267
		<?php
268
	}
269
}
270
271
/**
272
 * Get the list of post statuses.
273
 *
274
 * This includes all custom post status, but excludes built-in private posts.
275
 *
276
 * @since 5.6.0
277
 *
278
 * @return array List of post status objects.
279
 */
280
function bd_get_post_statuses() {
281
	$post_statuses = get_post_stati( array(), 'object' );
282
283
	$exclude_post_statuses = bd_get_excluded_post_statuses();
284
	foreach ( $exclude_post_statuses as $key ) {
285
		unset( $post_statuses[ $key ] );
286
	}
287
288
	/**
289
	 * List of post statuses that are displayed in the post status filter.
290
	 *
291
	 * @since 5.6.0
292
	 *
293
	 * @param array $post_statuses List of post statuses.
294
	 */
295
	return apply_filters( 'bd_post_statuses', $post_statuses );
296
}
297
298
/**
299
 * Get the list of excluded post statuses.
300
 *
301
 * @since 6.0.0
302
 *
303
 * @return array List of excluded post statuses.
304
 */
305
function bd_get_excluded_post_statuses() {
306
	/**
307
	 * List of post statuses that should be excluded from post status filter.
308
	 *
309
	 * @since 5.6.0
310
	 *
311
	 * @param array $post_statuses List of post statuses to exclude.
312
	 */
313
	return apply_filters(
314
		'bd_excluded_post_statuses',
315
		array(
316
			'inherit',
317
			'trash',
318
			'auto-draft',
319
			'request-pending',
320
			'request-confirmed',
321
			'request-failed',
322
			'request-completed',
323
		)
324
	);
325
}
326
327
/**
328
 * Generate help tooltip and append it to existing markup.
329
 *
330
 * @param string $markup  Existing markup.
331
 * @param string $content Tooltip content.
332
 *
333
 * @return string Markup with tooltip markup appended to it.
334
 */
335
function bd_generate_help_tooltip( $markup, $content ) {
336
	if ( empty( $content ) ) {
337
		return $markup;
338
	}
339
340
	$tooltip = '<span alt="f223" class="bd-help dashicons dashicons-editor-help" title="' . $content . '"></span>';
341
342
	return $markup . $tooltip;
343
}
344