PostType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 115
ccs 0
cts 42
cp 0
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeLabels() 0 3 1
A register() 0 8 1
A normalizeMenuName() 0 5 2
A normalize() 0 6 2
A init() 0 9 2
A setLabels() 0 16 1
1
<?php
2
3
namespace GeminiLabs\Pollux\PostType;
4
5
use GeminiLabs\Pollux\Component;
6
use GeminiLabs\Pollux\PostType\Columns;
7
8
class PostType extends Component
9
{
10
	use Columns;
0 ignored issues
show
introduced by
The trait GeminiLabs\Pollux\PostType\Columns requires some properties which are not provided by GeminiLabs\Pollux\PostType\PostType: $post_name, $config
Loading history...
11
12
	const CUSTOM_KEYS = [
13
		'columns', 'menu_name', 'plural', 'single',
14
	];
15
16
	const POST_TYPE_DEFAULTS = [
17
		'capability_type' => 'post',
18
		'columns' => [],
19
		'has_archive' => false,
20
		'hierarchical' => false,
21
		'labels' => [],
22
		'map_meta_cap' => true,
23
		'menu_icon' => null,
24
		'menu_name' => '',
25
		'menu_position' => 20,
26
		'plural' => '',
27
		'public' => true,
28
		'query_var' => true,
29
		'rewrite' => true,
30
		'show_in_menu' => true,
31
		'show_ui' => true,
32
		'single' => '',
33
		'supports' => ['title', 'editor', 'thumbnail'],
34
		'taxonomies' => [],
35
	];
36
37
	/**
38
	 * @var array
39
	 */
40
	public $types = [];
41
42
	/**
43
	 * {@inheritdoc}
44
	 */
45
	public function init()
46
	{
47
		if( empty( $this->app->config->post_types ))return;
48
49
		$this->setColumns();
50
		$this->normalize();
51
		$this->initColumns();
52
53
		add_action( 'init', [$this, 'register'] );
54
	}
55
56
	/**
57
	 * @return void
58
	 * @action init
59
	 */
60
	public function register()
61
	{
62
		$types = array_diff_key(
63
			$this->types,
64
			get_post_types( ['_builtin' => true] )
65
		);
66
		array_walk( $types, function( $args, $type ) {
67
			register_post_type( $type, array_diff_key( $args, array_flip( static::CUSTOM_KEYS )));
68
		});
69
	}
70
71
	/**
72
	 * {@inheritdoc}
73
	 */
74
	protected function normalize()
75
	{
76
		foreach( $this->app->config->post_types as $type => $args ) {
77
			$this->types[$type] = apply_filters( 'pollux/post_type/args',
78
				$this->normalizeThis( $args, static::POST_TYPE_DEFAULTS, $type ),
79
				$type
80
			);
81
		}
82
	}
83
84
	/**
85
	 * @param mixed $labels
86
	 * @return array
87
	 */
88
	protected function normalizeLabels( $labels, array $args )
89
	{
90
		return wp_parse_args( $labels, $this->setLabels( $args ));
91
	}
92
93
	/**
94
	 * @param string $menuname
95
	 * @return string
96
	 */
97
	protected function normalizeMenuName( $menuname, array $args )
98
	{
99
		return empty( $menuname )
100
			? $args['plural']
101
			: $menuname;
102
	}
103
104
	/**
105
	 * @return array
106
	 */
107
	protected function setLabels( array $args )
108
	{
109
		return apply_filters( 'pollux/post_type/labels', [
110
			'add_new' => __( 'Add New', 'pollux' ),
111
			'add_new_item' => sprintf( _x( 'Add New %s', 'Add new post', 'pollux' ), $args['single'] ),
112
			'all_items' => sprintf( _x( 'All %s', 'All posts', 'pollux' ), $args['plural'] ),
113
			'edit_item' => sprintf( _x( 'Edit %s', 'Edit post', 'pollux' ), $args['single'] ),
114
			'menu_name' => $this->normalizeMenuName( $args['menu_name'], $args ),
115
			'name' => $args['plural'],
116
			'new_item' => sprintf( _x( 'New %s', 'New post', 'pollux' ), $args['single'] ),
117
			'not_found' => sprintf( _x( 'No %s found', 'No posts found', 'pollux' ), $args['plural'] ),
118
			'not_found_in_trash' => sprintf( _x( 'No %s found in Trash', 'No posts found in trash', 'pollux' ), $args['plural'] ),
119
			'search_items' => sprintf( _x( 'Search %s', 'Search posts', 'pollux' ), $args['plural'] ),
120
			'singular_name' => $args['single'],
121
			'view_item' => sprintf( _x( 'View %s', 'View post', 'pollux' ), $args['single'] ),
122
		], $args );
123
	}
124
}
125