Completed
Push — 249-fix/delete-posts-by-custom... ( 3680d9...3680d9 )
by Rajan
23:06 queued 23:03
created

PostsModule   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 287
Duplicated Lines 0 %

Test Coverage

Coverage 16.48%

Importance

Changes 0
Metric Value
eloc 111
dl 0
loc 287
ccs 15
cts 91
cp 0.1648
rs 10
c 0
b 0
f 0
wmc 25

15 Methods

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