Completed
Pull Request — 330-fix/delete-user-meta-add-a... (#524)
by Sudar
26:21 queued 11:22
created

Fetcher::are_sticky_posts_present()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 1
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
		$sticky_post_ids = get_option( 'sticky_posts' );
76
77
		if ( empty( $sticky_post_ids ) ) {
78
			return array();
79
		}
80
81
		return get_posts(
82
			array(
83
				'numberposts' => count( $sticky_post_ids ),
84
				'post__in'    => $sticky_post_ids,
85
			)
86
		);
87
	}
88
89
	/**
90
	 * Get the list of categories.
91
	 *
92
	 * @return array List of categories.
93
	 */
94
	protected function get_categories() {
95
		$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

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