Completed
Push — 192-feature/delete-posts-by-UR... ( 8c9c60...3f0497 )
by Rajan
12:40 queued 09:41
created

Fetcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 53
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_post_types() 0 2 1
A get_post_statuses() 0 2 1
B get_post_types_by_status() 0 27 5
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_name-$post_status_name" ] = $post_type->labels->singular_name . ' (' . $post_type->name . ')' .
63
										   ' - ' . $post_status->label . ' (' . $count_posts->{$post_status_name} . ' ' . __( 'Posts', 'bulk-delete' ) . ')';
64
			}
65
		}
66
67
		return $post_types_by_status;
68
	}
69
}
70