Completed
Push — 216-feature/delete-pages-by-po... ( 72eaef...512de1 )
by Sudar
244:33 queued 239:03
created

PostsMetabox   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 193
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0
wmc 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
A filter_js_array() 0 23 1
A render_post_type_dropdown() 0 2 1
A get_tags() 0 13 2
A render_category_dropdown() 0 18 2
A render_private_post_settings() 0 2 1
A render_tags_dropdown() 0 18 2
A get_post_statuses() 0 2 1
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
	protected $item_type = 'posts';
15
16
	public function filter_js_array( $js_array ) {
17
		$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' );
18
		$js_array['msg']['selectPostOption']   = __( 'Please select posts from at least one option', 'bulk-delete' );
19
20
		$js_array['validators']['delete_posts_by_category'] = 'validateSelect2';
21
		$js_array['error_msg']['delete_posts_by_category']  = 'selectCategory';
22
		$js_array['msg']['selectCategory']                  = __( 'Please select at least one category', 'bulk-delete' );
23
24
		$js_array['validators']['delete_posts_by_tag']     = 'validateSelect2';
25
		$js_array['error_msg']['delete_posts_by_category'] = 'selectTag';
26
		$js_array['msg']['selectTag']                      = __( 'Please select at least one tag', 'bulk-delete' );
27
28
		$js_array['validators']['delete_posts_by_url'] = 'validateUrl';
29
		$js_array['error_msg']['delete_posts_by_url']  = 'enterUrl';
30
		$js_array['msg']['enterUrl']                   = __( 'Please enter at least one post url', 'bulk-delete' );
31
32
		$js_array['dt_iterators'][] = '_cats';
33
		$js_array['dt_iterators'][] = '_tags';
34
		$js_array['dt_iterators'][] = '_taxs';
35
		$js_array['dt_iterators'][] = '_types';
36
		$js_array['dt_iterators'][] = '_post_status';
37
38
		return $js_array;
39
	}
40
41
	/**
42
	 * Render the "private post" setting fields.
43
	 */
44
	protected function render_private_post_settings() {
45
		bd_render_private_post_settings( $this->field_slug );
46
	}
47
48
	/**
49
	 * Render Post type dropdown.
50
	 */
51
	protected function render_post_type_dropdown() {
52
		bd_render_post_type_dropdown( $this->field_slug );
53
	}
54
55
	/**
56
	 * Render Category dropdown.
57
	 */
58
	protected function render_category_dropdown() {
59
		$categories = $this->get_categories();
60
		?>
61
62
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
63
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
64
				data-taxonomy="category" multiple>
65
66
			<option value="all">
67
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
68
			</option>
69
70
			<?php foreach ( $categories as $category ) : ?>
71
				<option value="<?php echo absint( $category->cat_ID ); ?>">
72
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
73
				</option>
74
			<?php endforeach; ?>
75
76
		</select>
77
	<?php
78
	}
79
80
	/**
81
	 * Render Tags dropdown.
82
	 */
83
	protected function render_tags_dropdown() {
84
		$tags = $this->get_tags();
85
		?>
86
87
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
88
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
89
				data-taxonomy="post_tag" multiple>
90
91
			<option value="all">
92
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
93
			</option>
94
95
			<?php foreach ( $tags as $tag ) : ?>
96
				<option value="<?php echo absint( $tag->term_id ); ?>">
97
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
98
				</option>
99
			<?php endforeach; ?>
100
		</select>
101
	<?php
102
	}
103
104
	/**
105
	 * Get the list of post statuses.
106
	 *
107
	 * This includes all custom post status, but excludes built-in private posts.
108
	 *
109
	 * @return array List of post status objects.
110
	 */
111
	protected function get_post_statuses() {
112
		return bd_get_post_statuses();
113
	}
114
115
	/**
116
	 * Get the list of categories.
117
	 *
118
	 * @return array List of categories.
119
	 */
120
	protected function get_categories() {
121
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
122
123
		$categories = get_categories(
124
			array(
125
				'hide_empty' => false,
126
				'number'     => $enhanced_select_threshold,
127
			)
128
		);
129
130
		return $categories;
131
	}
132
133
	/**
134
	 * Are tags present in this WordPress installation?
135
	 *
136
	 * Only one tag is retrieved to check if tags are present for performance reasons.
137
	 *
138
	 * @return bool True if tags are present, False otherwise.
139
	 */
140
	protected function are_tags_present() {
141
		$tags = $this->get_tags( 1 );
142
143
		return ( count( $tags ) > 0 );
144
	}
145
146
	/**
147
	 * Get the list of tags.
148
	 *
149
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
150
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
151
	 *
152
	 * @return array List of tags.
153
	 */
154
	protected function get_tags( $max_count = 0 ) {
155
		if ( absint( $max_count ) === 0 ) {
156
			$max_count = $this->get_enhanced_select_threshold();
157
		}
158
159
		$tags = get_tags(
160
			array(
161
				'hide_empty' => false,
162
				'number'     => $max_count,
163
			)
164
		);
165
166
		return $tags;
167
	}
168
169
	/**
170
	 * Delete sticky posts.
171
	 *
172
	 * @param bool $force_delete Whether to bypass trash and force deletion.
173
	 *
174
	 * @return int Number of posts deleted.
175
	 */
176
	protected function delete_sticky_posts( $force_delete ) {
177
		$sticky_post_ids = get_option( 'sticky_posts' );
178
179
		if ( ! is_array( $sticky_post_ids ) ) {
180
			return 0;
181
		}
182
183
		return $this->delete_posts_by_id( $sticky_post_ids, $force_delete );
184
	}
185
186
	/**
187
	 * Delete posts by ids.
188
	 *
189
	 * @param int[] $post_ids     List of post ids to delete.
190
	 * @param bool  $force_delete True to force delete posts, False otherwise.
191
	 *
192
	 * @return int Number of posts deleted.
193
	 */
194
	protected function delete_posts_by_id( $post_ids, $force_delete ) {
195
		foreach ( $post_ids as $post_id ) {
196
			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types.
197
			// See https://core.trac.wordpress.org/ticket/43672
198
			if ( $force_delete ) {
199
				wp_delete_post( $post_id, true );
200
			} else {
201
				wp_trash_post( $post_id );
202
			}
203
		}
204
205
		return count( $post_ids );
206
	}
207
}
208