Passed
Pull Request — dev/6.0.0 (#445)
by Rajan
20:39 queued 11:49
created

Fetcher   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
eloc 50
dl 0
loc 181
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A get_post_types() 0 2 1
A get_taxonomies() 0 4 1
A get_post_statuses() 0 2 1
A get_post_types_by_status() 0 26 5
A are_sticky_post_present() 0 8 2
A get_sticky_posts() 0 4 1
A get_tags() 0 13 2
A are_tags_present() 0 4 1
A get_categories() 0 11 1
A are_private_posts_present() 0 8 1
A get_user_count_by_role() 0 10 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 taxompmies registered in WordPress.
27
	 *
28
	 * @return A list of taxonomy names
0 ignored issues
show
Bug introduced by
The type BulkWP\BulkDelete\Core\Base\Mixin\A was not found. Did you mean A? If so, make sure to prefix the type with \.
Loading history...
29
	 */
30
	protected function get_taxonomies() {
31
		$taxonomies = get_taxonomies();
32
33
		return $taxonomies;
34
	}
35
36
	/**
37
	 * Get the list of post statuses.
38
	 *
39
	 * This includes all custom post status, but excludes built-in private posts.
40
	 *
41
	 * @return array List of post status objects.
42
	 */
43
	protected function get_post_statuses() {
44
		return bd_get_post_statuses();
45
	}
46
47
	/**
48
	 * Get the list of post types by post status and count.
49
	 *
50
	 * @return array Post types by post status.
51
	 */
52
	protected function get_post_types_by_status() {
53
		$post_types_by_status = array();
54
55
		$post_types    = $this->get_post_types();
56
		$post_statuses = $this->get_post_statuses();
57
58
		foreach ( $post_types as $post_type ) {
59
			$post_type_name = $post_type->name;
60
			$count_posts    = wp_count_posts( $post_type_name );
61
62
			foreach ( $post_statuses as $post_status ) {
63
				$post_status_name = $post_status->name;
64
65
				if ( ! property_exists( $count_posts, $post_status_name ) ) {
66
					continue;
67
				}
68
69
				if ( 0 === $count_posts->{$post_status_name} ) {
70
					continue;
71
				}
72
73
				$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' ) . ')';
74
			}
75
		}
76
77
		return $post_types_by_status;
78
	}
79
80
	/**
81
	 * Get the list of sticky posts.
82
	 *
83
	 * @return array List of sticky posts.
84
	 */
85
	protected function get_sticky_posts() {
86
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
87
88
		return $posts;
89
	}
90
91
	/**
92
	 * Get the list of categories.
93
	 *
94
	 * @return array List of categories.
95
	 */
96
	protected function get_categories() {
97
		$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

97
		/** @scrutinizer ignore-call */ 
98
  $enhanced_select_threshold = $this->get_enhanced_select_threshold();
Loading history...
98
99
		$categories = get_categories(
100
			array(
101
				'hide_empty' => false,
102
				'number'     => $enhanced_select_threshold,
103
			)
104
		);
105
106
		return $categories;
107
	}
108
109
	/**
110
	 * Are tags present in this WordPress installation?
111
	 *
112
	 * Only one tag is retrieved to check if tags are present for performance reasons.
113
	 *
114
	 * @return bool True if tags are present, False otherwise.
115
	 */
116
	protected function are_tags_present() {
117
		$tags = $this->get_tags( 1 );
118
119
		return ( count( $tags ) > 0 );
120
	}
121
122
	/**
123
	 * Get the list of tags.
124
	 *
125
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
126
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
127
	 *
128
	 * @return array List of tags.
129
	 */
130
	protected function get_tags( $max_count = 0 ) {
131
		if ( absint( $max_count ) === 0 ) {
132
			$max_count = $this->get_enhanced_select_threshold();
133
		}
134
135
		$tags = get_tags(
136
			array(
137
				'hide_empty' => false,
138
				'number'     => $max_count,
139
			)
140
		);
141
142
		return $tags;
143
	}
144
145
	/**
146
	 * Are sticky post present in this WordPress?
147
	 *
148
	 * Only one post is retrieved to check if stick post are present for performance reasons.
149
	 *
150
	 * @return bool True if posts are present, False otherwise.
151
	 */
152
	protected function are_sticky_post_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
	 * @param string $role Role slug.
183
	 *
184
	 * @return int Number of users in that role.
185
	 */
186
	protected function get_user_count_by_role( $role ) {
187
		$users_count = count_users();
188
189
		$roles = $users_count['avail_roles'];
190
191
		if ( ! array_key_exists( $role, $roles ) ) {
192
			return 0;
193
		}
194
195
		return $roles[ $role ];
196
	}
197
}
198