Completed
Pull Request — dev/6.0.0 (#445)
by Rajan
14:28 queued 11:23
created

Fetcher   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
eloc 44
dl 0
loc 157
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get_post_types() 0 2 1
A get_taxonomies() 0 2 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_user_count_by_role() 0 10 2
A get_tags() 0 13 2
A are_tags_present() 0 4 1
A get_categories() 0 11 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
	protected function get_taxonomies() {
26
		return bd_get_taxonomies();
27
	}
28
29
	/**
30
	 * Get the list of post statuses.
31
	 *
32
	 * This includes all custom post status, but excludes built-in private posts.
33
	 *
34
	 * @return array List of post status objects.
35
	 */
36
	protected function get_post_statuses() {
37
		return bd_get_post_statuses();
38
	}
39
40
	/**
41
	 * Get the list of post types by post status and count.
42
	 *
43
	 * @return array Post types by post status.
44
	 */
45
	protected function get_post_types_by_status() {
46
		$post_types_by_status = array();
47
48
		$post_types    = $this->get_post_types();
49
		$post_statuses = $this->get_post_statuses();
50
51
		foreach ( $post_types as $post_type ) {
52
			$post_type_name = $post_type->name;
53
			$count_posts    = wp_count_posts( $post_type_name );
54
55
			foreach ( $post_statuses as $post_status ) {
56
				$post_status_name = $post_status->name;
57
58
				if ( ! property_exists( $count_posts, $post_status_name ) ) {
59
					continue;
60
				}
61
62
				if ( 0 === $count_posts->{$post_status_name} ) {
63
					continue;
64
				}
65
66
				$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' ) . ')';
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
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
80
81
		return $posts;
82
	}
83
84
	/**
85
	 * Get the list of categories.
86
	 *
87
	 * @return array List of categories.
88
	 */
89
	protected function get_categories() {
90
		$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

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