Completed
Pull Request — dev/6.0.0 (#307)
by Rajan
10:54 queued 07:36
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 4
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'][] = '_post_status';
47
		$js_array['dt_iterators'][] = '_sticky_post';
48
49
		return $js_array;
50
	}
51
52
	public function delete( $options ) {
53 44
		/**
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
		$options = apply_filters( 'bd_delete_options', $options, $this );
62 44
63
		$query = $this->build_query( $options );
64 44
65
		if ( empty( $query ) ) {
66 44
			// Short circuit deletion, if nothing needs to be deleted.
67
			return 0;
68
		}
69
70
		return $this->delete_posts_from_query( $query, $options );
71 44
	}
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
	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 44
85
		return $this->delete_posts_by_id( $post_ids, $options['force_delete'] );
86 44
	}
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
158
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" class="select2-sticky-post" data-placeholder="<?php _e( 'Select Posts', 'bulk-delete' ); ?>" multiple>
159
160
			<option value="all">
161
				<?php _e( 'All Posts', 'bulk-delete' ); ?>
162
			</option>
163
164
			<?php foreach ( $posts as $post ) : ?>
165
				<option value="<?php echo absint( $post->ID ); ?>">
166
					<?php echo esc_html( $post->post_title. ' (' .$post->post_date. ')' ); ?>
167
				</option>
168
			<?php endforeach; ?>
169
		</select>
170
	<?php
171
	}
172
173
	protected function get_sticky_posts(){
174
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
175
		return $posts;
176
	}
177
178
	/**
179
	 * Get the list of categories.
180
	 *
181
	 * @return array List of categories.
182
	 */
183
	protected function get_categories() {
184
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
185
186
		$categories = get_categories(
187
			array(
188
				'hide_empty' => false,
189
				'number'     => $enhanced_select_threshold,
190
			)
191
		);
192
193
		return $categories;
194
	}
195
196
	/**
197
	 * Are tags present in this WordPress installation?
198
	 *
199
	 * Only one tag is retrieved to check if tags are present for performance reasons.
200
	 *
201
	 * @return bool True if tags are present, False otherwise.
202
	 */
203
	protected function are_tags_present() {
204
		$tags = $this->get_tags( 1 );
205
206
		return ( count( $tags ) > 0 );
207
	}
208
209
	/**
210
	 * Are sticky post present in this WordPress?
211
	 *
212
	 * Only one post is retrieved to check if stick post are present for performance reasons.
213
	 *
214
	 * @return bool True if posts are present, False otherwise.
215
	 */
216
	protected function are_stickt_post_present() {
217
		$sticky_post_ids = get_option( 'sticky_posts' );
218
		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

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