Completed
Push — dev/6.0.1 ( 56df76...3c6ba8 )
by Sudar
13:47
created

Fetcher   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 206
ccs 0
cts 59
cp 0
rs 10
c 0
b 0
f 0
wmc 21

11 Methods

Rating   Name   Duplication   Size   Complexity  
A get_post_types() 0 2 1
A get_post_statuses() 0 2 1
A are_private_posts_present() 0 8 1
A get_sticky_posts() 0 11 2
A get_user_count_by_role() 0 12 4
A are_sticky_posts_present() 0 8 2
A get_tags() 0 13 2
A are_tags_present() 0 4 1
A get_categories() 0 11 1
A get_post_types_by_status() 0 30 5
A get_enhanced_select_threshold() 0 9 1
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_type_key               = $post_type->labels->singular_name . ' (' . $post_type_name . ')';
63
				$post_type_with_status_key   = $post_type_name . '-' . $post_status_name;
64
				$post_type_with_status_label = $post_status->label . ' (' . $count_posts->{$post_status_name} . ' ' . __( 'Posts', 'bulk-delete' ) . ')';
65
66
				$post_types_by_status[ $post_type_key ][ $post_type_with_status_key ] = $post_type_with_status_label;
67
			}
68
		}
69
70
		return $post_types_by_status;
71
	}
72
73
	/**
74
	 * Get the list of sticky posts.
75
	 *
76
	 * @return array List of sticky posts.
77
	 */
78
	protected function get_sticky_posts() {
79
		$sticky_post_ids = get_option( 'sticky_posts' );
80
81
		if ( empty( $sticky_post_ids ) ) {
82
			return array();
83
		}
84
85
		return get_posts(
86
			array(
87
				'numberposts' => count( $sticky_post_ids ),
88
				'post__in'    => $sticky_post_ids,
89
			)
90
		);
91
	}
92
93
	/**
94
	 * Get the list of categories.
95
	 *
96
	 * @return array List of categories.
97
	 */
98
	protected function get_categories() {
99
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
100
101
		$categories = get_categories(
102
			array(
103
				'hide_empty' => false,
104
				'number'     => $enhanced_select_threshold,
105
			)
106
		);
107
108
		return $categories;
109
	}
110
111
	/**
112
	 * Are tags present in this WordPress installation?
113
	 *
114
	 * Only one tag is retrieved to check if tags are present for performance reasons.
115
	 *
116
	 * @return bool True if tags are present, False otherwise.
117
	 */
118
	protected function are_tags_present() {
119
		$tags = $this->get_tags( 1 );
120
121
		return ( count( $tags ) > 0 );
122
	}
123
124
	/**
125
	 * Get the list of tags.
126
	 *
127
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
128
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
129
	 *
130
	 * @return array List of tags.
131
	 */
132
	protected function get_tags( $max_count = 0 ) {
133
		if ( absint( $max_count ) === 0 ) {
134
			$max_count = $this->get_enhanced_select_threshold();
135
		}
136
137
		$tags = get_tags(
138
			array(
139
				'hide_empty' => false,
140
				'number'     => $max_count,
141
			)
142
		);
143
144
		return $tags;
145
	}
146
147
	/**
148
	 * Are sticky post present in this WordPress installation?
149
	 *
150
	 * @return bool True if sticky posts are present, False otherwise.
151
	 */
152
	protected function are_sticky_posts_present() {
153
		$sticky_post_ids = get_option( 'sticky_posts' );
154
155
		if ( ! is_array( $sticky_post_ids ) ) {
156
			return false;
157
		}
158
159
		return ( count( $sticky_post_ids ) > 0 );
160
	}
161
162
	/**
163
	 * Check if private posts are present in a post type.
164
	 *
165
	 * @param string $post_type Post type.
166
	 *
167
	 * @return bool
168
	 */
169
	protected function are_private_posts_present( $post_type = 'any' ) {
170
		$args  = array(
171
			'post_status' => array( 'private' ),
172
			'post_type'   => $post_type,
173
		);
174
		$query = new \WP_Query( $args );
175
176
		return $query->have_posts();
177
	}
178
179
	/**
180
	 * Get the number of users present in a role.
181
	 *
182
	 * `count_users` function is very expensive. So this function takes an optional parameter to cache it.
183
	 *
184
	 * @see count_users
185
	 *
186
	 * @param string     $role        Role slug.
187
	 * @param array|null $users_count Result of the `count_users` function. Default null.
188
	 *
189
	 * @return int Number of users in that role.
190
	 */
191
	protected function get_user_count_by_role( $role, $users_count = null ) {
192
		if ( is_null( $users_count ) || ! is_array( $users_count ) ) {
193
			$users_count = count_users();
194
		}
195
196
		$roles = $users_count['avail_roles'];
197
198
		if ( ! array_key_exists( $role, $roles ) ) {
199
			return 0;
200
		}
201
202
		return $roles[ $role ];
203
	}
204
205
	/**
206
	 * Get the threshold after which enhanced select should be used.
207
	 *
208
	 * @since 6.0.1 moved to Fetcher from Renderer.
209
	 *
210
	 * @return int Threshold.
211
	 */
212
	protected function get_enhanced_select_threshold() {
213
		/**
214
		 * Filter the enhanced select threshold.
215
		 *
216
		 * @since 6.0.0
217
		 *
218
		 * @param int Threshold.
219
		 */
220
		return apply_filters( 'bd_enhanced_select_threshold', 1000 );
221
	}
222
}
223