Passed
Push — dev/6.0.0 ( 1c71e1...e60df2 )
by Sudar
09:05 queued 06:18
created

PostsModule::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

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