Completed
Push — dev/6.0.0 ( 4593c9...b14098 )
by Sudar
11:00 queued 07:23
created

PostsMetabox::get_enhanced_select_threshold()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 2
cp 0
crap 2
rs 9.6666
c 0
b 0
f 0
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
	 * @param string $name Name of the dropdown.
59
	 */
60
	protected function render_category_dropdown() {
61
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
62
63
		$categories = $this->get_categories();
64
65
		$class_name = 'select2';
66
		$extra_atts = '';
67
68
		if ( count( $categories ) >= $enhanced_select_threshold ) {
69
			$class_name = 'select2-ajax';
70
			$extra_atts = 'data-taxonomy="category"';
71
		}
72
		?>
73
74
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
75
			class="<?php echo sanitize_html_class( $class_name ); ?>" <?php echo esc_html( $extra_atts ); ?>>
76
77
			<option value="all">
78
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
79
			</option>
80
81
			<?php foreach ( $categories as $category ) : ?>
82
				<option value="<?php echo absint( $category->cat_ID ); ?>">
83
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
84
				</option>
85
			<?php endforeach; ?>
86
87
		</select>
88
	<?php
89
	}
90
91
	/**
92
	 * Delete sticky posts.
93
	 *
94
	 * @param bool $force_delete Whether to bypass trash and force deletion.
95
	 *
96
	 * @return int Number of posts deleted.
97
	 */
98
	protected function delete_sticky_posts( $force_delete ) {
99
		$sticky_post_ids = get_option( 'sticky_posts' );
100
101
		foreach ( $sticky_post_ids as $sticky_post_id ) {
102
			wp_delete_post( $sticky_post_id, $force_delete );
103
		}
104
105
		return count( $sticky_post_ids );
106
	}
107
108
	/**
109
	 * Get the list of post statuses.
110
	 *
111
	 * This includes all custom post status, but excludes built-in private posts.
112
	 *
113
	 * @return array List of post status objects.
114
	 */
115
	protected function get_post_statuses() {
116
		return bd_get_post_statuses();
117
	}
118
119
	/**
120
	 * Get the threshold after which enhanced select should be used.
121
	 *
122
	 * @return int Threshold.
123
	 */
124
	protected function get_enhanced_select_threshold() {
125
		/**
126
		 * Filter the enhanced select threshold.
127
		 *
128
		 * @since 6.0.0
129
		 *
130
		 * @param int Threshold.
131
		 */
132
		return apply_filters( 'bd_enhanced_select_threshold', 5 );
133
	}
134
135
	/**
136
	 * Get the list of categories.
137
	 *
138
	 * @return array List of categories.
139
	 */
140
	protected function get_categories() {
141
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
142
143
		$categories = get_categories(
144
			array(
145
				'hide_empty' => false,
146
				'number'     => $enhanced_select_threshold,
147
			)
148
		);
149
150
		return $categories;
151
	}
152
153
	/**
154
	 * Delete posts by ids.
155
	 *
156
	 * @param int[] $post_ids     List of post ids to delete.
157
	 * @param bool  $force_delete True to force delete posts, False otherwise.
158
	 *
159
	 * @return int Number of posts deleted.
160
	 */
161 11
	protected function delete_posts_by_id( $post_ids, $force_delete ) {
162 11
		foreach ( $post_ids as $post_id ) {
163
			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types.
164
			// See https://core.trac.wordpress.org/ticket/43672
165 11
			if ( $force_delete ) {
166 2
				wp_delete_post( $post_id, true );
167
			} else {
168 11
				wp_trash_post( $post_id );
169
			}
170
		}
171
172 11
		return count( $post_ids );
173
	}
174
}
175