Completed
Pull Request — master (#20)
by Evan
05:00 queued 02:33
created

PostType::object()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
rs 10
1
<?php
2
3
namespace Silk\PostType;
4
5
use stdClass;
6
use Silk\Type\Type;
7
use Illuminate\Support\Collection;
8
use Silk\Exception\WP_ErrorException;
9
use Silk\PostType\Exception\NonExistentPostTypeException;
10
11
class PostType extends Type
12
{
13
    /**
14
     * PostType Constructor
15
     *
16
     * @param stdClass $object  The WordPress post type object
17
     *
18
     * @throws \InvalidArgumentException
19
     */
20
    public function __construct($object)
21
    {
22
        if (! is_object($object) || ! in_array(get_class($object), ['stdClass', 'WP_Post_Type'])) {
23
            throw new \InvalidArgumentException(static::class . ' can only be constructed with a Post Type object.');
24
        }
25
26
        $this->object = $object;
27
    }
28
29
    /**
30
     * Create a new instance using the post type slug.
31
     *
32
     * Loads an existing type, or returns a new builder for registering a new type.
33
     *
34
     * @param  string $slug    The post type slug
35
     *
36
     * @return static|Builder  If the post type has been registered, a new static instance is returned.
37
     *                         Otherwise a new Builder is created for building a new post type to register.
38
     */
39
    public static function make($slug)
40
    {
41
        if (static::exists($slug)) {
42
            return static::load($slug);
43
        }
44
45
        return new Builder($slug);
46
    }
47
48
    /**
49
     * Create a new instance from an existing type.
50
     *
51
     * @param  string $slug  The post type slug
52
     *
53
     * @return static
54
     */
55
    public static function load($slug)
56
    {
57
        if (! $object = get_post_type_object($slug)) {
58
            throw new NonExistentPostTypeException("No post type exists with name '$slug'.");
59
        }
60
61
        return new static($object);
62
    }
63
64
    /**
65
     * Checks if a post type with this slug has been registered.
66
     *
67
     * @param string $slug  The post type slug
68
     *
69
     * @return bool
70
     */
71
    public static function exists($slug)
72
    {
73
        return post_type_exists($slug);
74
    }
75
76
    /**
77
     * Check for feature support.
78
     *
79
     * @param string|array $features  string - First feature of possible many,
80
     *                                array - Many features to check support for.
81
     *
82
     * @return mixed
83
     */
84
    public function supports($features)
85
    {
86
        if (! is_array($features)) {
87
            $features = func_get_args();
88
        }
89
90
        return ! Collection::make($features)
91
            ->contains(function ($key, $feature) {
92
                return ! post_type_supports($this->slug, $feature);
93
            });
94
    }
95
96
    /**
97
     * Register support of certain features for an existing post type.
98
     *
99
     * @param mixed $features string - single feature to add
100
     *                        array - multiple features to add
101
     *
102
     * @return $this
103
     */
104
    public function addSupportFor($features)
105
    {
106
        add_post_type_support($this->slug, is_array($features) ? $features : func_get_args());
107
108
        return $this;
109
    }
110
111
    /**
112
     * Deregister support of certain features for an existing post type.
113
     *
114
     * @param mixed $features string - single feature to remove
115
     *                        array - multiple features to remove
116
     *
117
     * @return $this
118
     */
119
    public function removeSupportFor($features)
120
    {
121
        Collection::make(is_array($features) ? $features : func_get_args())
122
            ->each(function ($features) {
123
                remove_post_type_support($this->slug, $features);
124
            });
125
126
        return $this;
127
    }
128
129
    /**
130
     * Unregister the post type
131
     *
132
     * @throws NonExistentPostTypeException
133
     * @throws WP_ErrorException
134
     *
135
     * @return $this
136
     */
137
    public function unregister()
138
    {
139
        if (! static::exists($this->slug)) {
140
            throw new NonExistentPostTypeException("No post type exists with name '{$this->slug}'.");
141
        }
142
143
        if (is_wp_error($error = unregister_post_type($this->slug))) {
144
            throw new WP_ErrorException($error);
145
        }
146
147
        return $this;
148
    }
149
}
150