Completed
Push — dev/6.0.0 ( 350de0...06578d )
by Sudar
07:30
created

Fetcher::get_post_statuses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
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
	/**
27
	 * Get the list of post statuses.
28
	 *
29
	 * This includes all custom post status, but excludes built-in private posts.
30
	 *
31
	 * @return array List of post status objects.
32
	 */
33
	protected function get_post_statuses() {
34
		return bd_get_post_statuses();
35
	}
36
37
	/**
38
	 * Get the list of post types by post status and count.
39
	 *
40
	 * @return array Post types by post status.
41
	 */
42
	protected function get_post_types_by_status() {
43
		$post_types_by_status = array();
44
45
		$post_types    = $this->get_post_types();
46
		$post_statuses = $this->get_post_statuses();
47
48
		foreach ( $post_types as $post_type ) {
49
			$post_type_name = $post_type->name;
50
			$count_posts    = wp_count_posts( $post_type_name );
51
52
			foreach ( $post_statuses as $post_status ) {
53
				$post_status_name = $post_status->name;
54
55
				if ( ! property_exists( $count_posts, $post_status_name ) ) {
56
					continue;
57
				}
58
59
				if ( 0 === $count_posts->{$post_status_name} ) {
60
					continue;
61
				}
62
63
				$post_types_by_status[ "$post_type_name-$post_status_name" ] = $post_type->labels->singular_name . ' (' . $post_type->name . ')' .
64
										   ' - ' . $post_status->label . ' (' . $count_posts->{$post_status_name} . ' ' . __( 'Posts', 'bulk-delete' ) . ')';
65
			}
66
		}
67
68
		return $post_types_by_status;
69
	}
70
}
71