Passed
Pull Request — dev/6.0.0 (#316)
by Rajan
24:44 queued 21:44
created

PostsModule::delete_posts_from_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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'][] = '_post_status';
47
48
		return $js_array;
49
	}
50
51 44
	public function delete( $options ) {
52
		/**
53
		 * Filter delete options before deleting posts.
54
		 *
55
		 * @since 6.0.0 Added `Modules` parameter.
56
		 *
57
		 * @param array $options Delete options.
58
		 * @param \BulkWP\BulkDelete\Core\Base\BaseModule Modules that is triggering deletion.
59
		 */
60 44
		$options = apply_filters( 'bd_delete_options', $options, $this );
61
62 44
		$query = $this->build_query( $options );
63
64 44
		if ( empty( $query ) ) {
65
			// Short circuit deletion, if nothing needs to be deleted.
66
			return 0;
67
		}
68
69 44
		return $this->delete_posts_from_query( $query, $options );
70
	}
71
72
	/**
73
	 * Build the query using query params and then Delete posts.
74
	 *
75
	 * @param array $query   Params for WP Query.
76
	 * @param array $options Delete Options.
77
	 *
78
	 * @return int Number of posts deleted.
79
	 */
80 44
	protected function delete_posts_from_query( $query, $options ) {
81 44
		$query    = bd_build_query_options( $options, $query );
82 44
		$post_ids = bd_query( $query );
83
84 44
		return $this->delete_posts_by_id( $post_ids, $options['force_delete'] );
85
	}
86
87
	/**
88
	 * Render the "private post" setting fields.
89
	 */
90
	protected function render_private_post_settings() {
91
		bd_render_private_post_settings( $this->field_slug );
92
	}
93
94
	/**
95
	 * Render Category dropdown.
96
	 */
97
	protected function render_category_dropdown() {
98
		$categories = $this->get_categories();
99
		?>
100
101
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
102
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
103
				data-taxonomy="category" multiple>
104
105
			<option value="all">
106
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
107
			</option>
108
109
			<?php foreach ( $categories as $category ) : ?>
110
				<option value="<?php echo absint( $category->cat_ID ); ?>">
111
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
112
				</option>
113
			<?php endforeach; ?>
114
115
		</select>
116
	<?php
117
	}
118
119
	/**
120
	 * Render Tags dropdown.
121
	 */
122
	protected function render_tags_dropdown() {
123
		$tags = $this->get_tags();
124
		?>
125
126
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
127
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
128
				data-taxonomy="post_tag" multiple>
129
130
			<option value="all">
131
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
132
			</option>
133
134
			<?php foreach ( $tags as $tag ) : ?>
135
				<option value="<?php echo absint( $tag->term_id ); ?>">
136
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
137
				</option>
138
			<?php endforeach; ?>
139
		</select>
140
	<?php
141
	}
142
143
	/**
144
	 * Render Sticky Posts dropdown.
145
	 */
146
	protected function render_sticky_post_dropdown() {
147
		$posts = $this->get_sticky_posts();
148
		?>
149
		<table class="optiontable">
150
			<tr>
151
				<td scope="row">
152
					<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="All">
153
					<label>All</label>	
154
				</td>
155
			</tr>
156
			<?php foreach ( $posts as $post ) :
157
			$user = get_userdata( $post->post_author );
158
			?>
159
			<tr>
160
				<td scope="row">
161
				<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
162
				<label><?php echo esc_html( $post->post_title . ' Published by ' . $user->display_name . ' on ' . $post->post_date ); ?></label>
163
				</td>
164
			</tr>
165
			<?php endforeach; ?>
166
		</table>
167
	<?php
168
	}
169
170
	/**
171
	 * Get the list of sticky posts.
172
	 *
173
	 * @return array List of sticky posts.
174
	 */
175
	protected function get_sticky_posts(){
176
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
177
178
		return $posts;
179
	}
180
181
	/**
182
	 * Get the list of categories.
183
	 *
184
	 * @return array List of categories.
185
	 */
186
	protected function get_categories() {
187
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
188
189
		$categories = get_categories(
190
			array(
191
				'hide_empty' => false,
192
				'number'     => $enhanced_select_threshold,
193
			)
194
		);
195
196
		return $categories;
197
	}
198
199
	/**
200
	 * Are tags present in this WordPress installation?
201
	 *
202
	 * Only one tag is retrieved to check if tags are present for performance reasons.
203
	 *
204
	 * @return bool True if tags are present, False otherwise.
205
	 */
206
	protected function are_tags_present() {
207
		$tags = $this->get_tags( 1 );
208
209
		return ( count( $tags ) > 0 );
210
	}
211
212
	/**
213
	 * Are sticky post present in this WordPress?
214
	 *
215
	 * Only one post is retrieved to check if stick post are present for performance reasons.
216
	 *
217
	 * @return bool True if posts are present, False otherwise.
218
	 */
219
	protected function are_sticky_post_present() {
220
		$sticky_post_ids = get_option( 'sticky_posts' );
221
222
		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

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