Completed
Pull Request — 330-fix/delete-user-meta-add-a... (#524)
by Sudar
26:21 queued 11:22
created

bd_render_sticky_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 2
cp 0
crap 2
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
			<label for="smbd_<?php echo $slug; ?>_restrict"><?php printf( __( 'Only restrict to %s which are ', 'bulk-delete' ), $item ); ?></label>
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">
65
			<label><input name="smbd_<?php echo $slug; ?>_force_delete" value="false" type="radio" checked><?php _e( 'Move to Trash', 'bulk-delete' ); ?></label>
66
			<label><input name="smbd_<?php echo $slug; ?>_force_delete" value="true" type="radio"><?php _e( 'Delete permanently', 'bulk-delete' ); ?></label>
67
		</td>
68
	</tr>
69
<?php
70
}
71
72
/**
73
 * Render the "private post" setting fields.
74
 *
75
 * @since 5.5
76
 *
77
 * @param string $slug The slug to be used in field names.
78
 */
79
function bd_render_private_post_settings( $slug ) {
80
		?>
81
	<tr>
82
		<td scope="row" colspan="2">
83
			<label><input name="smbd_<?php echo $slug; ?>_private" value="false" type="radio" checked> <?php _e( 'Public posts', 'bulk-delete' ); ?></label>
84
			<label><input name="smbd_<?php echo $slug; ?>_private" value="true" type="radio"> <?php _e( 'Private Posts', 'bulk-delete' ); ?></label>
85
		</td>
86
	</tr>
87
		<?php
88
}
89
90
/**
91
 * Render the "limit" setting fields.
92
 *
93
 * @since 5.5
94
 *
95
 * @param string $slug The slug to be used in field names.
96
 * @param string $item (Optional) Item type. Possible values are 'posts', 'pages', 'users'
97
 */
98
function bd_render_limit_settings( $slug, $item = 'posts' ) {
99
?>
100
	<tr>
101
		<td scope="row">
102
			<input name="smbd_<?php echo $slug; ?>_limit" id="smbd_<?php echo $slug; ?>_limit" value="true" type="checkbox">
103
		</td>
104
		<td>
105
			<label for="smbd_<?php echo $slug; ?>_limit"><?php _e( 'Only delete first ', 'bulk-delete' ); ?></label>
106
			<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;?>.
107
			<?php printf( __( 'Use this option if there are more than 1000 %s and the script times out.', 'bulk-delete' ), $item ); ?>
108
		</td>
109
	</tr>
110
<?php
111
}
112
113
/**
114
 * Render cron setting fields.
115
 *
116
 * @since 5.5
117
 *
118
 * @param string $slug      The slug to be used in field names.
119
 * @param string $addon_url Url for the pro addon.
120
 */
121
function bd_render_cron_settings( $slug, $addon_url ) {
122
	$pro_class = 'bd-' . str_replace( '_', '-', $slug ) . '-pro';
123
?>
124
	<tr>
125
		<td scope="row" colspan="2">
126
			<label><input name="smbd_<?php echo $slug; ?>_cron" value="false" type="radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?></label>
127
			<label><input name="smbd_<?php echo $slug; ?>_cron" value="true" type="radio" id="smbd_<?php echo $slug; ?>_cron" disabled > <?php _e( 'Schedule', 'bulk-delete' ); ?></label>
128
			<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' );?>
129
			<select name="smbd_<?php echo $slug; ?>_cron_freq" id="smbd_<?php echo $slug; ?>_cron_freq" disabled>
130
				<option value="-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option>
131
<?php
132
	$schedules = wp_get_schedules();
133
	foreach ( $schedules as $key => $value ) {
134
?>
135
				<option value="<?php echo $key; ?>"><?php echo $value['display']; ?></option>
136
<?php } ?>
137
			</select>
138
			<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>
139
		</td>
140
	</tr>
141
142
	<tr>
143
		<td scope="row" colspan="2">
144
			<?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' );?>
145
		</td>
146
	</tr>
147
<?php
148
}
149
150
/**
151
 * Render the submit button.
152
 *
153
 * @since 5.5
154
 *
155
 * @param string $action The action attribute of the submit button.
156
 */
157
function bd_render_submit_button( $action ) {
158
?>
159
	<p class="submit">
160
		<button type="submit" name="bd_action" value="<?php echo esc_attr( $action ); ?>" class="button-primary"><?php _e( 'Bulk Delete ', 'bulk-delete' ); ?>&raquo;</button>
161
	</p>
162
<?php
163
}
164
165
/**
166
 * Get the list of post type objects that will be used in filters.
167
 *
168
 * @since 5.6.0
169
 *
170
 * @return \WP_Post_Type[] List of post type objects.
171
 */
172
function bd_get_post_types() {
173
	$custom_types = get_post_types( array( '_builtin' => false ), 'objects' );
174
175
	$builtin_types = array(
176
		'post' => get_post_type_object( 'post' ),
177
		'page' => get_post_type_object( 'page' ),
178
	);
179
180
	return array_merge( $builtin_types, $custom_types );
181
}
182
183
/**
184
 * Render Post type dropdown.
185
 *
186
 * @param string $field_slug Field slug.
187
 */
188
function bd_render_post_type_dropdown( $field_slug ) {
189
	$types = bd_get_post_types();
190
	?>
191
	<tr>
192
		<td scope="row" >
193
			<select class="enhanced-dropdown" name="smbd_<?php echo esc_attr( $field_slug ); ?>_post_type">
194
				<?php foreach ( $types as $type ) : ?>
195
					<option value="<?php echo esc_attr( $type->name ); ?>">
196
						<?php echo esc_html( $type->labels->singular_name . ' (' . $type->name . ')' ); ?>
197
					</option>
198
				<?php endforeach; ?>
199
			</select>
200
		</td>
201
	</tr>
202
	<?php
203
}
204
205
/**
206
 * Render the post status filter.
207
 *
208
 * @since 5.6.0
209
 *
210
 * @param string $slug     The slug to be used in field names.
211
 * @param string $selected Default selected status.
212
 */
213
function bd_render_post_status_filter( $slug, $selected = 'publish' ) {
214
	$post_statuses = bd_get_post_statuses();
215
216
	foreach ( $post_statuses as $key => $value ) {
217
		?>
218
		<tr>
219
			<td>
220
				<label>
221
					<input name="smbd_<?php echo esc_attr( $slug ); ?>_post_status[]" type="checkbox"
222
							value="<?php echo esc_attr( $key ); ?>" <?php checked( $key, $selected ); ?>>
223
224
					<?php echo __( 'All', 'bulk-delete' ), ' ', esc_html( $value->label ), ' ', __( 'Posts', 'bulk-delete' ); ?>
225
				</label>
226
			</td>
227
		</tr>
228
		<?php
229
	}
230
}
231
232
/**
233
 * Get the list of post statuses.
234
 *
235
 * This includes all custom post status, but excludes built-in private posts.
236
 *
237
 * @since 5.6.0
238
 *
239
 * @return array List of post status objects.
240
 */
241
function bd_get_post_statuses() {
242
	$post_statuses = get_post_stati( array(), 'object' );
243
244
	$exclude_post_statuses = bd_get_excluded_post_statuses();
245
	foreach ( $exclude_post_statuses as $key ) {
246
		unset( $post_statuses[ $key ] );
247
	}
248
249
	/**
250
	 * List of post statuses that are displayed in the post status filter.
251
	 *
252
	 * @since 5.6.0
253
	 *
254
	 * @param array $post_statuses List of post statuses.
255
	 */
256
	return apply_filters( 'bd_post_statuses', $post_statuses );
257
}
258
259
/**
260
 * Get the list of excluded post statuses.
261
 *
262
 * @since 6.0.0
263
 *
264
 * @return array List of excluded post statuses.
265
 */
266
function bd_get_excluded_post_statuses() {
267
	/**
268
	 * List of post statuses that should be excluded from post status filter.
269
	 *
270
	 * @since 5.6.0
271
	 *
272
	 * @param array $post_statuses List of post statuses to exclude.
273
	 */
274
	return apply_filters(
275
		'bd_excluded_post_statuses',
276
		array(
277
			'inherit',
278
			'trash',
279
			'auto-draft',
280
			'request-pending',
281
			'request-confirmed',
282
			'request-failed',
283
			'request-completed',
284
		)
285
	);
286
}
287
288
/**
289
 * Generate help tooltip and append it to existing markup.
290
 *
291
 * @param string $markup  Existing markup.
292
 * @param string $content Tooltip content.
293
 *
294
 * @return string Markup with tooltip markup appended to it.
295
 */
296
function bd_generate_help_tooltip( $markup, $content ) {
297
	if ( empty( $content ) ) {
298
		return $markup;
299
	}
300
301
	$tooltip = '<span alt="f223" class="bd-help dashicons dashicons-editor-help" title="' . $content . '"></span>';
302
303
	return $markup . $tooltip;
304
}
305