Passed
Push — 249-fix/delete-posts-by-custom... ( cbb919...ce983c )
by Sudar
38:50 queued 28:43
created

PostsModule::render_sticky_post_dropdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 22
ccs 0
cts 7
cp 0
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
namespace BulkWP\BulkDelete\Core\Posts;
3
4
use BulkWP\BulkDelete\Core\Base\BaseModule;
5
6 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/**
9
 * Module for deleting posts.
10
 *
11
 * @since 6.0.0
12
 */
13
abstract class PostsModule extends BaseModule {
14
	/**
15
	 * Build query params for WP_Query by using delete options.
16
	 *
17
	 * Return an empty query array to short-circuit deletion.
18
	 *
19
	 * @param array $options Delete options.
20
	 *
21
	 * @return array Query.
22
	 */
23
	abstract protected function build_query( $options );
24
25
	/**
26
	 * Item Type. Possible values 'posts', 'pages', 'users' etc.
27
	 *
28
	 * @var string
29
	 */
30
	protected $item_type = 'posts';
31
32
	/**
33
	 * Handle common filters.
34
	 *
35
	 * @param array $request Request array.
36
	 *
37
	 * @return array User options.
38
	 */
39
	protected function parse_common_filters( $request ) {
40
		$options = array();
41
42
		$options['restrict']     = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false );
43
		$options['limit_to']     = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) );
44
		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false );
45
46
		$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' );
47
		$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) );
48
49
		return $options;
50
	}
51
52
	/**
53
	 * Filter JS Array and add pro hooks.
54
	 *
55
	 * @since 5.5
56
	 *
57
	 * @param array $js_array JavaScript Array.
58
	 *
59
	 * @return array Modified JavaScript Array
60
	 */
61
	public function filter_js_array( $js_array ) {
62
		$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' );
63
		$js_array['msg']['selectPostOption']   = __( 'Please select posts from at least one option', 'bulk-delete' );
64
65
		$js_array['validators']['delete_posts_by_category'] = 'validateSelect2';
66
		$js_array['error_msg']['delete_posts_by_category']  = 'selectCategory';
67
		$js_array['msg']['selectCategory']                  = __( 'Please select at least one category', 'bulk-delete' );
68
69
		$js_array['validators']['delete_posts_by_tag']     = 'validateSelect2';
70
		$js_array['error_msg']['delete_posts_by_category'] = 'selectTag';
71
		$js_array['msg']['selectTag']                      = __( 'Please select at least one tag', 'bulk-delete' );
72
73
		$js_array['validators']['delete_posts_by_url'] = 'validateUrl';
74
		$js_array['error_msg']['delete_posts_by_url']  = 'enterUrl';
75
		$js_array['msg']['enterUrl']                   = __( 'Please enter at least one post url', 'bulk-delete' );
76
77
		$js_array['dt_iterators'][] = '_cats';
78
		$js_array['dt_iterators'][] = '_tags';
79
		$js_array['dt_iterators'][] = '_taxs';
80
		$js_array['dt_iterators'][] = '_post_status';
81
82
		return $js_array;
83
	}
84
85
	/**
86
	 * Perform the deletion.
87
	 *
88
	 * @param array $options Array of Delete options.
89
	 *
90
	 * @return int Number of items that were deleted.
91
	 */
92 65
	protected function do_delete( $options ) {
93 65
		$query = $this->build_query( $options );
94
95 65
		if ( empty( $query ) ) {
96
			// Short circuit deletion, if nothing needs to be deleted.
97
			return 0;
98
		}
99
100 65
		return $this->delete_posts_from_query( $query, $options );
101
	}
102
103
	/**
104
	 * Build the query using query params and then Delete posts.
105
	 *
106
	 * @param array $query   Params for WP Query.
107
	 * @param array $options Delete Options.
108
	 *
109
	 * @return int Number of posts deleted.
110
	 */
111 74
	protected function delete_posts_from_query( $query, $options ) {
112 74
		$query        = bd_build_query_options( $options, $query );
113 74
		$post_ids     = bd_query( $query );
114 74
		$force_delete = isset( $options['force_delete'] ) ? $options['force_delete'] : false;
115
116 74
		return $this->delete_posts_by_id( $post_ids, $force_delete );
117
	}
118
119
	/**
120
	 * Render the "private post" setting fields.
121
	 */
122
	protected function render_private_post_settings() {
123
    if( $this->are_private_posts_present() ){
124
			bd_render_private_post_settings( $this->field_slug );
125
    }
126
	}
127
128
	/**
129
	 * Render Category dropdown.
130
	 */
131
	protected function render_category_dropdown() {
132
		$categories = $this->get_categories();
133
		?>
134
135
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
136
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
137
				data-taxonomy="category" multiple>
138
139
			<option value="all">
140
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
141
			</option>
142
143
			<?php foreach ( $categories as $category ) : ?>
144
				<option value="<?php echo absint( $category->cat_ID ); ?>">
145
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
146
				</option>
147
			<?php endforeach; ?>
148
149
		</select>
150
		<?php
151
	}
152
153
	/**
154
	 * Render Tags dropdown.
155
	 */
156
	protected function render_tags_dropdown() {
157
		$tags = $this->get_tags();
158
		?>
159
160
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
161
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
162
				data-taxonomy="post_tag" multiple>
163
164
			<option value="all">
165
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
166
			</option>
167
168
			<?php foreach ( $tags as $tag ) : ?>
169
				<option value="<?php echo absint( $tag->term_id ); ?>">
170
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
171
				</option>
172
			<?php endforeach; ?>
173
		</select>
174
		<?php
175
	}
176
177
	/**
178
	 * Render Sticky Posts dropdown.
179
	 */
180
	protected function render_sticky_post_dropdown() {
181
		$posts = $this->get_sticky_posts();
182
		?>
183
		<table class="optiontable">
184
			<tr>
185
				<td scope="row">
186
					<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="All">
187
					<label>All</label>
188
				</td>
189
			</tr>
190
			<?php
191
			foreach ( $posts as $post ) :
192
				$user = get_userdata( $post->post_author );
193
				?>
194
			<tr>
195
				<td scope="row">
196
				<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
197
				<label><?php echo esc_html( $post->post_title . ' Published by ' . $user->display_name . ' on ' . $post->post_date ); ?></label>
198
				</td>
199
			</tr>
200
			<?php endforeach; ?>
201
		</table>
202
		<?php
203
	}
204
205
	/**
206
	 * Get the list of sticky posts.
207
	 *
208
	 * @return array List of sticky posts.
209
	 */
210
	protected function get_sticky_posts() {
211
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
212
213
		return $posts;
214
	}
215
216
	/**
217
	 * Get the list of categories.
218
	 *
219
	 * @return array List of categories.
220
	 */
221
	protected function get_categories() {
222
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
223
224
		$categories = get_categories(
225
			array(
226
				'hide_empty' => false,
227
				'number'     => $enhanced_select_threshold,
228
			)
229
		);
230
231
		return $categories;
232
	}
233
234
	/**
235
	 * Are tags present in this WordPress installation?
236
	 *
237
	 * Only one tag is retrieved to check if tags are present for performance reasons.
238
	 *
239
	 * @return bool True if tags are present, False otherwise.
240
	 */
241
	protected function are_tags_present() {
242
		$tags = $this->get_tags( 1 );
243
244
		return ( count( $tags ) > 0 );
245
	}
246
247
	/**
248
	 * Are sticky post present in this WordPress?
249
	 *
250
	 * Only one post is retrieved to check if stick post are present for performance reasons.
251
	 *
252
	 * @return bool True if posts are present, False otherwise.
253
	 */
254
	protected function are_sticky_post_present() {
255
		$sticky_post_ids = get_option( 'sticky_posts' );
256
257
		if ( ! is_array( $sticky_post_ids ) ) {
258
			return false;
259
		}
260
261
		return ( count( $sticky_post_ids ) > 0 );
262
	}
263
264
	/**
265
	 * Get the list of tags.
266
	 *
267
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
268
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
269
	 *
270
	 * @return array List of tags.
271
	 */
272
	protected function get_tags( $max_count = 0 ) {
273
		if ( absint( $max_count ) === 0 ) {
274
			$max_count = $this->get_enhanced_select_threshold();
0 ignored issues
show
Unused Code introduced by
The assignment to $max_count is dead and can be removed.
Loading history...
275
		}
276
	}
277
278
	/**
279
	 * Delete sticky posts.
280
	 *
281
	 * @param bool $force_delete Whether to bypass trash and force deletion.
282
	 *
283
	 * @return int Number of posts deleted.
284
	 */
285
	protected function delete_sticky_posts( $force_delete ) {
286
		$sticky_post_ids = get_option( 'sticky_posts' );
287
288
		if ( ! is_array( $sticky_post_ids ) ) {
289
			return 0;
290
		}
291
292
		return $this->delete_posts_by_id( $sticky_post_ids, $force_delete );
293
	}
294
295
	/**
296
	 * Delete posts by ids.
297
	 *
298
	 * @param int[] $post_ids     List of post ids to delete.
299
	 * @param bool  $force_delete True to force delete posts, False otherwise.
300
	 *
301
	 * @return int Number of posts deleted.
302
	 */
303 83
	protected function delete_posts_by_id( $post_ids, $force_delete ) {
304 83
		foreach ( $post_ids as $post_id ) {
305
			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types.
306
			// See https://core.trac.wordpress.org/ticket/43672
307 81
			if ( $force_delete ) {
308 22
				wp_delete_post( $post_id, true );
309
			} else {
310 81
				wp_trash_post( $post_id );
311
			}
312
		}
313
314 83
		return count( $post_ids );
315
	}
316
}
317