-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposttype.php
More file actions
111 lines (92 loc) · 2.49 KB
/
posttype.php
File metadata and controls
111 lines (92 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace BestProject\Wordpress;
defined('ABSPATH') or die;
use BestProject\Wordpress\PostType\Labels,
BestProject\Wordpress\PostType\Arguments,
BestProject\Wordpress\PostType\MetaBox;
/**
* This class takes care of creating new custom post type.
*/
class PostType
{
/**
* Name of this post type.
*
* @var String
*/
protected $name;
/**
* The labels instance used for holding basic labels and descriptions of post type.
*
* @var Labels
*/
protected $labels;
/**
* The Arguments instance that holds all the arguments for this custom post type.
*
* @var Arguments
*/
protected $arguments;
/**
* Array of Field or MetaBox instances.
*
* @var Array
*/
protected $fields;
/**
* Creates and registers new Post Type (on object construct).
*
* @param String $name Name of this post type.
* @param Labels $labels The labels instance used for holding basic labels and descriptions of post type.
* @param Arguments $arguments The Arguments instance that holds all the arguments for this custom post type.
* @param Array $fields Array of Field or MetaBox instances.
*/
public function __construct($name, Labels $labels, Arguments $arguments,
Array $fields)
{
$this->name = $name;
$this->labels = $labels->toArray();
$arguments->labels = $this->labels;
$this->arguments = $arguments->toArray();
$this->fields = $fields;
$this->register();
$this->registerFields();
}
/**
* Method used to register post type.
*/
protected function register()
{
add_action('init', [$this, 'doRegister']);
}
/**
* This method does the actual registering of post type.
*/
public function doRegister()
{
register_post_type($this->name, $this->arguments);
// There is a thumbnail field, make sure that theme will mark this as supported.
if (isset($this->arguments['supports']) AND in_array('thumbnail',
$this->arguments['supports'])) {
add_theme_support('post-thumbnails');
add_image_size('featured_preview', 55, 55, true);
}
}
/**
* Register all the fields/metaboxes.
*/
protected function registerFields()
{
// Developer provided MetaBoxes array.
if ($this->fields[0] instanceof MetaBox) {
/* @var $metabox MetaBox */
foreach ($this->fields AS $metabox) {
$metabox->register($this->name);
}
// Developer provided Field array so create new metabox and register it.
} else {
$metabox = new MetaBox('advanced', 'METABOX_ADVANCED', $this->fields);
$metabox->register($this->name);
}
}
}