Completed
Push — dev/6.1.0 ( 4894ee...611f0a )
by Sudar
21s queued 18s
created

PostsModule   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 62.26%

Importance

Changes 11
Bugs 1 Features 0
Metric Value
eloc 47
c 11
b 1
f 0
dl 0
loc 197
ccs 33
cts 53
cp 0.6226
rs 10
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A render_private_post_settings() 0 3 2
A delete_sticky_posts() 0 8 2
A parse_common_filters() 0 12 1
A do_delete() 0 9 2
A delete_posts_from_query() 0 28 1
A query() 0 2 1
A build_query_options() 0 2 1
A prepare_posts_for_deletion() 0 12 3
A delete_posts_by_id() 0 30 6
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['exclude_sticky'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_exclude_sticky', false );
40
		$options['force_delete']   = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false );
41
42
		$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' );
43
		$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) );
44
45
		return $options;
46
	}
47
48
	/**
49
	 * Helper function to build the query params.
50
	 *
51
	 * @param array $options Delete Options.
52
	 * @param array $query   Params for WP Query.
53
	 *
54
	 * @return array Delete options array
55
	 */
56 182
	protected function build_query_options( $options, $query ) {
57 182
		return bd_build_query_options( $options, $query );
58
	}
59
60
	/**
61
	 * Helper function for bd_query which runs query.
62
	 *
63
	 * @param array $query Params for WP Query.
64
	 *
65
	 * @return array Deleted Post IDs array
66
	 */
67 182
	protected function query( $query ) {
68 182
		return bd_query( $query );
69
	}
70
71 168
	protected function do_delete( $options ) {
72 168
		$query = $this->build_query( $options );
73
74 168
		if ( empty( $query ) ) {
75
			// Short circuit deletion, if nothing needs to be deleted.
76
			return 0;
77
		}
78
79 168
		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 182
	protected function delete_posts_from_query( $query, $options ) {
91 182
		$query = $this->build_query_options( $options, $query );
92 182
		$posts = $this->query( $query );
93
94 182
		$post_ids = $this->prepare_posts_for_deletion( $posts, $options );
95
96
		/**
97
		 * Triggered before the posts are deleted.
98
		 *
99
		 * @since 6.0.0
100
		 *
101
		 * @param array $post_ids List of post ids that are going to be deleted.
102
		 * @param array $options  List of Delete Options.
103
		 */
104 182
		do_action( 'bd_before_deleting_posts', $post_ids, $options );
105
106 182
		$delete_post_count = $this->delete_posts_by_id( $post_ids, $options['force_delete'] );
107
108
		/**
109
		 * Triggered after the posts are deleted.
110
		 *
111
		 * @since 6.0.0
112
		 *
113
		 * @param array $options Delete Options.
114
		 */
115 182
		do_action( 'bd_after_deleting_posts', $options );
116
117 182
		return $delete_post_count;
118
	}
119
120
	/**
121
	 * Render the "private post" setting fields.
122
	 */
123
	protected function render_private_post_settings() {
124
		if ( $this->are_private_posts_present() ) {
125
			bd_render_private_post_settings( $this->field_slug );
126
		}
127
	}
128
129
	/**
130
	 * Delete sticky posts.
131
	 *
132
	 * @param bool $force_delete Whether to bypass trash and force deletion.
133
	 *
134
	 * @return int Number of posts deleted.
135
	 */
136
	protected function delete_sticky_posts( $force_delete ) {
137
		$sticky_post_ids = get_option( 'sticky_posts' );
138
139
		if ( ! is_array( $sticky_post_ids ) ) {
140
			return 0;
141
		}
142
143
		return $this->delete_posts_by_id( $sticky_post_ids, $force_delete );
144
	}
145
146
	/**
147
	 * Delete posts by ids.
148
	 *
149
	 * @param int[] $post_ids     List of post ids to delete.
150
	 * @param bool  $force_delete True to force delete posts, False otherwise.
151
	 *
152
	 * @return int Number of posts deleted.
153
	 */
154 191
	protected function delete_posts_by_id( $post_ids, $force_delete ) {
155 191
		$count = 0;
156
		/**
157
		 * Filter the list of post ids that will be excluded from deletion.
158
		 *
159
		 * @since 6.0.0
160
		 *
161
		 * @param array $excluded_ids Post IDs to be excluded.
162
		 */
163 191
		$excluded_post_ids = apply_filters( 'bd_excluded_post_ids', array() );
164
165 191
		if ( is_array( $excluded_post_ids ) && ! empty( $excluded_post_ids ) ) {
166
			$post_ids = array_diff( $post_ids, $excluded_post_ids );
167
		}
168
169 191
		foreach ( $post_ids as $post_id ) {
170
			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types.
171
			// See https://core.trac.wordpress.org/ticket/43672
172 141
			if ( $force_delete ) {
173 63
				$deleted = wp_delete_post( $post_id, true );
174
			} else {
175 78
				$deleted = wp_trash_post( $post_id );
176
			}
177
178 141
			if ( $deleted instanceof \WP_Post ) {
179 141
				$count ++;
180
			}
181
		}
182
183 191
		return $count;
184
	}
185
186
	/**
187
	 * Prepare posts for deletion.
188
	 *
189
	 * Individual modules can override this method to exclude posts from getting deleted.
190
	 *
191
	 * @since 6.0.2
192
	 *
193
	 * @param int[]|\WP_Post[] $posts   List of posts to be deleted. It could be just post_ids.
194
	 * @param array            $options Delete options.
195
	 *
196
	 * @return int[] List of post ids that should be deleted.
197
	 */
198 182
	protected function prepare_posts_for_deletion( array $posts, array $options ) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

198
	protected function prepare_posts_for_deletion( array $posts, /** @scrutinizer ignore-unused */ array $options ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
199 182
		$post_ids = array();
200
201 182
		foreach ( $posts as $post ) {
202 134
			if ( $post instanceof \WP_Post ) {
203
				$post_ids[] = $post->ID;
204
			} else {
205 134
				$post_ids[] = $post;
206
			}
207
		}
208
209 182
		return $post_ids;
210
	}
211
}
212