Completed
Pull Request — master (#20)
by Evan
06:01 queued 02:57
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 Illuminate\Support\Collection;
7
use Silk\Type\Type;
8
use Silk\Exception\WP_ErrorException;
9
use Silk\PostType\Exception\NonExistentPostTypeException;
10
use Silk\PostType\Builder;
11
12
class PostType extends Type
13
{
14
    /**
15
     * PostType Constructor
16
     *
17
     * @param stdClass $object  The WordPress post type object
18
     *
19
     * @throws \InvalidArgumentException
20
     */
21
    public function __construct($object)
22
    {
23
        if (! is_object($object) || ! in_array(get_class($object), ['stdClass', 'WP_Post_Type'])) {
24
            throw new \InvalidArgumentException(static::class . ' can only be constructed with a Post Type object.');
25
        }
26
27
        $this->object = $object;
28
    }
29
30
    /**
31
     * Create a new instance using the post type slug.
32
     *
33
     * Loads an existing type, or returns a new builder for registering a new type.
34
     *
35
     * @param  string $slug  The post type slug
36
     *
37
     * @return static|PostTypeBuilder  If the post type has been registered, a new static instance is returned.
38
     *                                 Otherwise a new PostTypeBuilder is created for building a new post type to register.
39
     */
40
    public static function make($slug)
41
    {
42
        if (static::exists($slug)) {
43
            return static::load($slug);
44
        }
45
46
        return new Builder($slug);
47
    }
48
49
    /**
50
     * Create a new instance from an existing type.
51
     *
52
     * @param  string $slug  The post type slug
53
     *
54
     * @return static
55
     */
56
    public static function load($slug)
57
    {
58
        if (! $object = get_post_type_object($slug)) {
59
            throw new NonExistentPostTypeException("No post type exists with name '$slug'.");
60
        }
61
62
        return new static($object);
63
    }
64
65
    /**
66
     * Checks if a post type with this slug has been registered.
67
     *
68
     * @param string $slug  The post type slug
69
     *
70
     * @return bool
71
     */
72
    public static function exists($slug)
73
    {
74
        return post_type_exists($slug);
75
    }
76
77
    /**
78
     * Check for feature support.
79
     *
80
     * @param string,...|array $features  string - First feature of possible many,
0 ignored issues
show
Documentation introduced by
The doc-type string,...|array could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
81
     *                                    array - Many features to check support for.
82
     *
83
     * @return mixed
84
     */
85
    public function supports($features)
86
    {
87
        if (! is_array($features)) {
88
            $features = func_get_args();
89
        }
90
91
        return ! Collection::make($features)
92
            ->contains(function ($key, $feature) {
93
                return ! post_type_supports($this->slug, $feature);
94
            });
95
    }
96
97
    /**
98
     * Register support of certain features for an existing post type.
99
     *
100
     * @param mixed $features  string - single feature to add
101
     *                        array - multiple features to add
102
     */
103
    public function addSupportFor($features)
104
    {
105
        add_post_type_support($this->slug, is_array($features) ? $features : func_get_args());
106
107
        return $this;
108
    }
109
110
    /**
111
     * Deregister support of certain features for an existing post type.
112
     *
113
     * @param mixed $features  string - single feature to remove
114
     *                        array - multiple features to remove
115
     */
116
    public function removeSupportFor($features)
117
    {
118
        Collection::make(is_array($features) ? $features : func_get_args())
119
            ->each(function ($features) {
120
                remove_post_type_support($this->slug, $features);
121
            });
122
123
        return $this;
124
    }
125
126
    /**
127
     * Unregister the post type
128
     *
129
     * @return $this
130
     */
131
    public function unregister()
132
    {
133
        if (! static::exists($this->slug)) {
134
            throw new NonExistentPostTypeException("No post type exists with name '{$this->slug}'.");
135
        }
136
137
        if (is_wp_error($error = unregister_post_type($this->slug))) {
138
            throw new WP_ErrorException($error);
139
        }
140
141
        return $this;
142
    }
143
}
144