Passed
Push — 218-fix/metabox-for-delete-pos... ( e74883...7af138 )
by Sudar
10:53 queued 03:34
created

PostsMetabox   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filter_js_array() 0 23 1
A render_private_post_settings() 0 2 1
A get_post_statuses() 0 2 1
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
	public function render_private_post_settings() {
45
		bd_render_private_post_settings( $this->field_slug );
46
	}
47
48
	/**
49
	 * Delete sticky posts.
50
	 *
51
	 * @param bool $force_delete Whether to bypass trash and force deletion.
52
	 *
53
	 * @return int Number of posts deleted.
54
	 */
55
	protected function delete_sticky_posts( $force_delete ) {
56
		$sticky_post_ids = get_option( 'sticky_posts' );
57
58
		foreach ( $sticky_post_ids as $sticky_post_id ) {
59
			wp_delete_post( $sticky_post_id, $force_delete );
60
		}
61
62
		return count( $sticky_post_ids );
63
	}
64
65
	/**
66
	 * Get the list of post statuses.
67
	 *
68
	 * This includes all custom post status, but excludes built-in private posts.
69
	 *
70
	 * @return array List of post status objects.
71
	 */
72
	protected function get_post_statuses() {
73
		return bd_get_post_statuses();
74
	}
75
}
76