Passed
Push — 262-fix/delete-sticky-posts ( ba9b70...d10b40 )
by Rajan
08:54
created

PostsModule   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 278
Duplicated Lines 0 %

Test Coverage

Coverage 17.65%

Importance

Changes 0
Metric Value
dl 0
loc 278
ccs 15
cts 85
cp 0.1765
rs 10
c 0
b 0
f 0
wmc 23

15 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 19 2
A filter_js_array() 0 23 1
A render_category_dropdown() 0 18 2
A delete_posts_from_query() 0 5 1
A render_tags_dropdown() 0 18 2
A render_post_type_dropdown() 0 2 1
A render_private_post_settings() 0 2 1
A render_sticky_post_dropdown() 0 21 2
A get_tags() 0 13 2
A get_categories() 0 11 1
A delete_posts_by_id() 0 12 3
A are_sticky_post_present() 0 3 1
A are_tags_present() 0 4 1
A get_sticky_posts() 0 3 1
A delete_sticky_posts() 0 8 2
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
	protected $item_type = 'posts';
26
27
	public function filter_js_array( $js_array ) {
28
		$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' );
29
		$js_array['msg']['selectPostOption']   = __( 'Please select posts from at least one option', 'bulk-delete' );
30
31
		$js_array['validators']['delete_posts_by_category'] = 'validateSelect2';
32
		$js_array['error_msg']['delete_posts_by_category']  = 'selectCategory';
33
		$js_array['msg']['selectCategory']                  = __( 'Please select at least one category', 'bulk-delete' );
34
35
		$js_array['validators']['delete_posts_by_tag']     = 'validateSelect2';
36
		$js_array['error_msg']['delete_posts_by_category'] = 'selectTag';
37
		$js_array['msg']['selectTag']                      = __( 'Please select at least one tag', 'bulk-delete' );
38
39
		$js_array['validators']['delete_posts_by_url'] = 'validateUrl';
40
		$js_array['error_msg']['delete_posts_by_url']  = 'enterUrl';
41
		$js_array['msg']['enterUrl']                   = __( 'Please enter at least one post url', 'bulk-delete' );
42
43
		$js_array['dt_iterators'][] = '_cats';
44
		$js_array['dt_iterators'][] = '_tags';
45
		$js_array['dt_iterators'][] = '_taxs';
46
		$js_array['dt_iterators'][] = '_types';
47
		$js_array['dt_iterators'][] = '_post_status';
48
49
		return $js_array;
50
	}
51
52 44
	public function delete( $options ) {
53
		/**
54
		 * Filter delete options before deleting posts.
55
		 *
56
		 * @since 6.0.0 Added `Modules` parameter.
57
		 *
58
		 * @param array $options Delete options.
59
		 * @param \BulkWP\BulkDelete\Core\Base\BaseModule Modules that is triggering deletion.
60
		 */
61 44
		$options = apply_filters( 'bd_delete_options', $options, $this );
62
63 44
		$query = $this->build_query( $options );
64
65 44
		if ( empty( $query ) ) {
66
			// Short circuit deletion, if nothing needs to be deleted.
67
			return 0;
68
		}
69
70 44
		return $this->delete_posts_from_query( $query, $options );
71
	}
72
73
	/**
74
	 * Build the query using query params and then Delete posts.
75
	 *
76
	 * @param array $query   Params for WP Query.
77
	 * @param array $options Delete Options.
78
	 *
79
	 * @return int Number of posts deleted.
80
	 */
81 44
	protected function delete_posts_from_query( $query, $options ) {
82 44
		$query    = bd_build_query_options( $options, $query );
83 44
		$post_ids = bd_query( $query );
84
85 44
		return $this->delete_posts_by_id( $post_ids, $options['force_delete'] );
86
	}
87
88
	/**
89
	 * Render the "private post" setting fields.
90
	 */
91
	protected function render_private_post_settings() {
92
		bd_render_private_post_settings( $this->field_slug );
93
	}
94
95
	/**
96
	 * Render Post type dropdown.
97
	 */
98
	protected function render_post_type_dropdown() {
99
		bd_render_post_type_dropdown( $this->field_slug );
100
	}
101
102
	/**
103
	 * Render Category dropdown.
104
	 */
105
	protected function render_category_dropdown() {
106
		$categories = $this->get_categories();
107
		?>
108
109
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
110
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
111
				data-taxonomy="category" multiple>
112
113
			<option value="all">
114
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
115
			</option>
116
117
			<?php foreach ( $categories as $category ) : ?>
118
				<option value="<?php echo absint( $category->cat_ID ); ?>">
119
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
120
				</option>
121
			<?php endforeach; ?>
122
123
		</select>
124
	<?php
125
	}
126
127
	/**
128
	 * Render Tags dropdown.
129
	 */
130
	protected function render_tags_dropdown() {
131
		$tags = $this->get_tags();
132
		?>
133
134
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
135
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
136
				data-taxonomy="post_tag" multiple>
137
138
			<option value="all">
139
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
140
			</option>
141
142
			<?php foreach ( $tags as $tag ) : ?>
143
				<option value="<?php echo absint( $tag->term_id ); ?>">
144
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
145
				</option>
146
			<?php endforeach; ?>
147
		</select>
148
	<?php
149
	}
150
151
	/**
152
	 * Render Sticky Posts dropdown.
153
	 */
154
	protected function render_sticky_post_dropdown() {
155
		$posts = $this->get_sticky_posts();
156
		?>
157
		<table class="optiontable">
158
			<tr>
159
				<td scope="row">
160
					<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="All">
161
					<label>All</label>	
162
				</td>
163
			</tr>
164
			<?php foreach ( $posts as $post ) : 
165
			$user = get_userdata( $post->post_author ); 
166
			?>
167
			<tr>
168
				<td scope="row">
169
				<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
170
				<label><?php echo esc_html( $post->post_title. ' Published by ' .$user->display_name. ' on ' .$post->post_date ); ?></label>
171
				</td>
172
			</tr>
173
			<?php endforeach; ?>
174
		</table>
175
	<?php
176
	}
177
178
	/**
179
	 * Get the list of sticky posts.
180
	 *
181
	 * @return array List of sticky posts.
182
	 */
183
	protected function get_sticky_posts(){
184
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
185
		return $posts;
186
	}
187
188
	/**
189
	 * Get the list of categories.
190
	 *
191
	 * @return array List of categories.
192
	 */
193
	protected function get_categories() {
194
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
195
196
		$categories = get_categories(
197
			array(
198
				'hide_empty' => false,
199
				'number'     => $enhanced_select_threshold,
200
			)
201
		);
202
203
		return $categories;
204
	}
205
206
	/**
207
	 * Are tags present in this WordPress installation?
208
	 *
209
	 * Only one tag is retrieved to check if tags are present for performance reasons.
210
	 *
211
	 * @return bool True if tags are present, False otherwise.
212
	 */
213
	protected function are_tags_present() {
214
		$tags = $this->get_tags( 1 );
215
216
		return ( count( $tags ) > 0 );
217
	}
218
219
	/**
220
	 * Are sticky post present in this WordPress?
221
	 *
222
	 * Only one post is retrieved to check if stick post are present for performance reasons.
223
	 *
224
	 * @return bool True if posts are present, False otherwise.
225
	 */
226
	protected function are_sticky_post_present() {
227
		$sticky_post_ids = get_option( 'sticky_posts' );
228
		return ( count( $sticky_post_ids ) > 0 );
0 ignored issues
show
Bug introduced by
It seems like $sticky_post_ids can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

228
		return ( count( /** @scrutinizer ignore-type */ $sticky_post_ids ) > 0 );
Loading history...
229
	}
230
231
	/**
232
	 * Get the list of tags.
233
	 *
234
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
235
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
236
	 *
237
	 * @return array List of tags.
238
	 */
239
	protected function get_tags( $max_count = 0 ) {
240
		if ( absint( $max_count ) === 0 ) {
241
			$max_count = $this->get_enhanced_select_threshold();
242
		}
243
244
		$tags = get_tags(
245
			array(
246
				'hide_empty' => false,
247
				'number'     => $max_count,
248
			)
249
		);
250
251
		return $tags;
252
	}
253
254
	/**
255
	 * Delete sticky posts.
256
	 *
257
	 * @param bool $force_delete Whether to bypass trash and force deletion.
258
	 *
259
	 * @return int Number of posts deleted.
260
	 */
261
	protected function delete_sticky_posts( $force_delete ) {
262
		$sticky_post_ids = get_option( 'sticky_posts' );
263
264
		if ( ! is_array( $sticky_post_ids ) ) {
265
			return 0;
266
		}
267
268
		return $this->delete_posts_by_id( $sticky_post_ids, $force_delete );
269
	}
270
271
	/**
272
	 * Delete posts by ids.
273
	 *
274
	 * @param int[] $post_ids     List of post ids to delete.
275
	 * @param bool  $force_delete True to force delete posts, False otherwise.
276
	 *
277
	 * @return int Number of posts deleted.
278
	 */
279 44
	protected function delete_posts_by_id( $post_ids, $force_delete ) {
280 44
		foreach ( $post_ids as $post_id ) {
281
			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types.
282
			// See https://core.trac.wordpress.org/ticket/43672
283 44
			if ( $force_delete ) {
284 12
				wp_delete_post( $post_id, true );
285
			} else {
286 44
				wp_trash_post( $post_id );
287
			}
288
		}
289
290 44
		return count( $post_ids );
291
	}
292
}
293