Passed
Pull Request — dev/6.0.0 (#382)
by Rajan
12:20
created

PostsModule::do_delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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