Passed
Push — dev/6.0.0 ( 149ee4...e588d2 )
by Sudar
05:50
created

PostsMetabox::render_private_post_settings()   A

Complexity

Conditions 1
Paths 1

Size

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