Completed
Branch develop (409c5d)
by Evan
02:43
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 WP_Post_Type;
7
use Illuminate\Support\Collection;
8
use Silk\Type\Type;
9
use Silk\Exception\WP_ErrorException;
10
use Silk\PostType\Exception\NonExistentPostTypeException;
11
use Silk\PostType\Builder;
12
13
class PostType extends Type
14
{
15
    /**
16
     * PostType Constructor
17
     *
18
     * @param stdClass $object  The WordPress post type object
19
     *
20
     * @throws \InvalidArgumentException
21
     */
22
    public function __construct($object)
23
    {
24
        if (! $object instanceof stdClass && ! $object instanceof WP_Post_Type) {
1 ignored issue
show
Bug introduced by
The class WP_Post_Type does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
25
            throw new \InvalidArgumentException(static::class . ' can only be constructed with a Post Type object.');
26
        }
27
28
        $this->object = $object;
29
    }
30
31
    /**
32
     * Create a new instance using the post type slug.
33
     *
34
     * Loads an existing type, or returns a new builder for registering a new type.
35
     *
36
     * @param  string $slug  The post type slug
37
     *
38
     * @return static|PostTypeBuilder  If the post type has been registered, a new static instance is returned.
39
     *                                 Otherwise a new PostTypeBuilder is created for building a new post type to register.
40
     */
41
    public static function make($slug)
42
    {
43
        if (static::exists($slug)) {
44
            return static::load($slug);
45
        }
46
47
        return new Builder($slug);
48
    }
49
50
    /**
51
     * Create a new instance from an existing type.
52
     *
53
     * @param  string $slug  The post type slug
54
     *
55
     * @return static
56
     */
57
    public static function load($slug)
58
    {
59
        if (! $object = get_post_type_object($slug)) {
60
            throw new NonExistentPostTypeException("No post type exists with name '$slug'.");
61
        }
62
63
        return new static($object);
64
    }
65
66
    /**
67
     * Checks if a post type with this slug has been registered.
68
     *
69
     * @param string $slug  The post type slug
70
     *
71
     * @return bool
72
     */
73
    public static function exists($slug)
74
    {
75
        return post_type_exists($slug);
76
    }
77
78
    /**
79
     * Check for feature support.
80
     *
81
     * @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...
82
     *                                    array - Many features to check support for.
83
     *
84
     * @return mixed
85
     */
86
    public function supports($features)
87
    {
88
        if (! is_array($features)) {
89
            $features = func_get_args();
90
        }
91
92
        return ! Collection::make($features)
93
            ->contains(function ($key, $feature) {
94
                return ! post_type_supports($this->slug, $feature);
95
            });
96
    }
97
98
    /**
99
     * Register support of certain features for an existing post type.
100
     *
101
     * @param mixed $features  string - single feature to add
102
     *                        array - multiple features to add
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
    public function removeSupportFor($features)
118
    {
119
        Collection::make(is_array($features) ? $features : func_get_args())
120
            ->each(function ($features) {
121
                remove_post_type_support($this->slug, $features);
122
            });
123
124
        return $this;
125
    }
126
127
    /**
128
     * Unregister the post type
129
     *
130
     * @return $this
131
     */
132
    public function unregister()
133
    {
134
        if (! static::exists($this->slug)) {
135
            throw new NonExistentPostTypeException("No post type exists with name '{$this->slug}'.");
136
        }
137
138
        if (is_wp_error($error = unregister_post_type($this->slug))) {
139
            throw new WP_ErrorException($error);
140
        }
141
142
        return $this;
143
    }
144
}
145