Completed
Push — 192-feature/delete-posts-by-UR... ( 8c9c60...3f0497 )
by Rajan
12:40 queued 09:41
created

PostsMetabox   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Test Coverage

Coverage 20.83%

Importance

Changes 0
Metric Value
dl 0
loc 229
ccs 15
cts 72
cp 0.2083
rs 10
c 0
b 0
f 0
wmc 19

12 Methods

Rating   Name   Duplication   Size   Complexity  
A render_category_dropdown() 0 18 2
A render_private_post_settings() 0 2 1
A render_post_type_dropdown() 0 2 1
A delete_posts_from_query() 0 5 1
A render_tags_dropdown() 0 18 2
A delete() 0 19 2
A filter_js_array() 0 23 1
A get_tags() 0 13 2
A are_tags_present() 0 4 1
A get_categories() 0 11 1
A delete_posts_by_id() 0 12 3
A delete_sticky_posts() 0 8 2
1
<?php
2
namespace BulkWP\BulkDelete\Core\Posts;
3
4
use BulkWP\BulkDelete\Core\Base\BaseMetabox;
5
6 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/**
9
 * Metabox for deleting posts.
10
 *
11
 * @since 6.0.0
12
 */
13
abstract class PostsMetabox extends BaseMetabox {
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'][] = '_types';
47
		$js_array['dt_iterators'][] = '_post_status';
48
49
		return $js_array;
50
	}
51
52 36
	public function delete( $options ) {
53
		/**
54
		 * Filter delete options before deleting posts.
55
		 *
56
		 * @since 6.0.0 Added `Metabox` parameter.
57
		 *
58
		 * @param array $options Delete options.
59
		 * @param \BulkWP\BulkDelete\Core\Base\BaseMetabox Metabox that is triggering deletion.
60
		 */
61 36
		$options = apply_filters( 'bd_delete_options', $options, $this );
62
63 36
		$query = $this->build_query( $options );
64
65 36
		if ( empty( $query ) ) {
66
			// Short circuit deletion, if nothing needs to be deleted.
67
			return 0;
68
		}
69
70 36
		return $this->delete_posts_from_query( $query, $options );
71
	}
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 36
	protected function delete_posts_from_query( $query, $options ) {
82 36
		$query    = bd_build_query_options( $options, $query );
83 36
		$post_ids = bd_query( $query );
84
85 36
		return $this->delete_posts_by_id( $post_ids, $options['force_delete'] );
86
	}
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
	 * Get the list of categories.
153
	 *
154
	 * @return array List of categories.
155
	 */
156
	protected function get_categories() {
157
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
158
159
		$categories = get_categories(
160
			array(
161
				'hide_empty' => false,
162
				'number'     => $enhanced_select_threshold,
163
			)
164
		);
165
166
		return $categories;
167
	}
168
169
	/**
170
	 * Are tags present in this WordPress installation?
171
	 *
172
	 * Only one tag is retrieved to check if tags are present for performance reasons.
173
	 *
174
	 * @return bool True if tags are present, False otherwise.
175
	 */
176
	protected function are_tags_present() {
177
		$tags = $this->get_tags( 1 );
178
179
		return ( count( $tags ) > 0 );
180
	}
181
182
	/**
183
	 * Get the list of tags.
184
	 *
185
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
186
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
187
	 *
188
	 * @return array List of tags.
189
	 */
190
	protected function get_tags( $max_count = 0 ) {
191
		if ( absint( $max_count ) === 0 ) {
192
			$max_count = $this->get_enhanced_select_threshold();
193
		}
194
195
		$tags = get_tags(
196
			array(
197
				'hide_empty' => false,
198
				'number'     => $max_count,
199
			)
200
		);
201
202
		return $tags;
203
	}
204
205
	/**
206
	 * Delete sticky posts.
207
	 *
208
	 * @param bool $force_delete Whether to bypass trash and force deletion.
209
	 *
210
	 * @return int Number of posts deleted.
211
	 */
212
	protected function delete_sticky_posts( $force_delete ) {
213
		$sticky_post_ids = get_option( 'sticky_posts' );
214
215
		if ( ! is_array( $sticky_post_ids ) ) {
216
			return 0;
217
		}
218
219
		return $this->delete_posts_by_id( $sticky_post_ids, $force_delete );
220
	}
221
222
	/**
223
	 * Delete posts by ids.
224
	 *
225
	 * @param int[] $post_ids     List of post ids to delete.
226
	 * @param bool  $force_delete True to force delete posts, False otherwise.
227
	 *
228
	 * @return int Number of posts deleted.
229
	 */
230 36
	protected function delete_posts_by_id( $post_ids, $force_delete ) {
231 36
		foreach ( $post_ids as $post_id ) {
232
			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types.
233
			// See https://core.trac.wordpress.org/ticket/43672
234 36
			if ( $force_delete ) {
235 10
				wp_delete_post( $post_id, true );
236
			} else {
237 36
				wp_trash_post( $post_id );
238
			}
239
		}
240
241 36
		return count( $post_ids );
242
	}
243
}
244