Completed
Push — 330-fix/delete-user-meta-add-a... ( d70968...8c3efe )
by Sudar
45:44 queued 42:41
created

Fetcher::are_private_posts_present()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Base\Mixin;
4
5 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Container of all Fetch related methods.
9
 *
10
 * Ideally this should be a Trait. Since Bulk Delete still supports PHP 5.3, this is implemented as a class.
11
 * Once the minimum requirement is increased to PHP 5.3, this will be changed into a Trait.
12
 *
13
 * @since 6.0.0
14
 */
15
abstract class Fetcher {
16
	/**
17
	 * Get the list of public post types registered in WordPress.
18
	 *
19
	 * @return \WP_Post_Type[]
20
	 */
21
	protected function get_post_types() {
22
		return bd_get_post_types();
23
	}
24
25
	/**
26
	 * Get the list of post statuses.
27
	 *
28
	 * This includes all custom post status, but excludes built-in private posts.
29
	 *
30
	 * @return array List of post status objects.
31
	 */
32
	protected function get_post_statuses() {
33
		return bd_get_post_statuses();
34
	}
35
36
	/**
37
	 * Get the list of post types by post status and count.
38
	 *
39
	 * @return array Post types by post status.
40
	 */
41
	protected function get_post_types_by_status() {
42
		$post_types_by_status = array();
43
44
		$post_types    = $this->get_post_types();
45
		$post_statuses = $this->get_post_statuses();
46
47
		foreach ( $post_types as $post_type ) {
48
			$post_type_name = $post_type->name;
49
			$count_posts    = wp_count_posts( $post_type_name );
50
51
			foreach ( $post_statuses as $post_status ) {
52
				$post_status_name = $post_status->name;
53
54
				if ( ! property_exists( $count_posts, $post_status_name ) ) {
55
					continue;
56
				}
57
58
				if ( 0 === $count_posts->{$post_status_name} ) {
59
					continue;
60
				}
61
62
				$post_types_by_status[ $post_type->labels->singular_name ][ "$post_type_name-$post_status_name" ] = $post_status->label . ' (' . $count_posts->{$post_status_name} . ' ' . __( 'Posts', 'bulk-delete' ) . ')';
63
			}
64
		}
65
66
		return $post_types_by_status;
67
	}
68
69
	/**
70
	 * Get the list of sticky posts.
71
	 *
72
	 * @return array List of sticky posts.
73
	 */
74
	protected function get_sticky_posts() {
75
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
76
77
		return $posts;
78
	}
79
80
	/**
81
	 * Get the list of categories.
82
	 *
83
	 * @return array List of categories.
84
	 */
85
	protected function get_categories() {
86
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
0 ignored issues
show
Bug introduced by
The method get_enhanced_select_threshold() does not exist on BulkWP\BulkDelete\Core\Base\Mixin\Fetcher. Since it exists in all sub-types, consider adding an abstract or default implementation to BulkWP\BulkDelete\Core\Base\Mixin\Fetcher. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
		/** @scrutinizer ignore-call */ 
87
  $enhanced_select_threshold = $this->get_enhanced_select_threshold();
Loading history...
87
88
		$categories = get_categories(
89
			array(
90
				'hide_empty' => false,
91
				'number'     => $enhanced_select_threshold,
92
			)
93
		);
94
95
		return $categories;
96
	}
97
98
	/**
99
	 * Are tags present in this WordPress installation?
100
	 *
101
	 * Only one tag is retrieved to check if tags are present for performance reasons.
102
	 *
103
	 * @return bool True if tags are present, False otherwise.
104
	 */
105
	protected function are_tags_present() {
106
		$tags = $this->get_tags( 1 );
107
108
		return ( count( $tags ) > 0 );
109
	}
110
111
	/**
112
	 * Get the list of tags.
113
	 *
114
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
115
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
116
	 *
117
	 * @return array List of tags.
118
	 */
119
	protected function get_tags( $max_count = 0 ) {
120
		if ( absint( $max_count ) === 0 ) {
121
			$max_count = $this->get_enhanced_select_threshold();
122
		}
123
124
		$tags = get_tags(
125
			array(
126
				'hide_empty' => false,
127
				'number'     => $max_count,
128
			)
129
		);
130
131
		return $tags;
132
	}
133
134
	/**
135
	 * Are sticky post present in this WordPress?
136
	 *
137
	 * Only one post is retrieved to check if stick post are present for performance reasons.
138
	 *
139
	 * @return bool True if posts are present, False otherwise.
140
	 */
141
	protected function are_sticky_post_present() {
142
		$sticky_post_ids = get_option( 'sticky_posts' );
143
144
		if ( ! is_array( $sticky_post_ids ) ) {
145
			return false;
146
		}
147
148
		return ( count( $sticky_post_ids ) > 0 );
149
	}
150
151
	/**
152
	 * Check if private posts are present in a post type.
153
	 *
154
	 * @param string $post_type Post type.
155
	 *
156
	 * @return bool
157
	 */
158
	protected function are_private_posts_present( $post_type = 'any' ) {
159
		$args  = array(
160
			'post_status' => array( 'private' ),
161
			'post_type'   => $post_type,
162
		);
163
		$query = new \WP_Query( $args );
164
165
		return $query->have_posts();
166
	}
167
168
	/**
169
	 * Get the number of users present in a role.
170
	 *
171
	 * @param string $role Role slug.
172
	 *
173
	 * @return int Number of users in that role.
174
	 */
175
	protected function get_user_count_by_role( $role ) {
176
		$users_count = count_users();
177
178
		$roles = $users_count['avail_roles'];
179
180
		if ( ! array_key_exists( $role, $roles ) ) {
181
			return 0;
182
		}
183
184
		return $roles[ $role ];
185
	}
186
}
187