Passed
Push — 262-fix/delete-sticky-posts ( 13b51e )
by Rajan
08:20
created

PostsModule::render_sticky_post_dropdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

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