Completed
Push — develop ( 4c8faa...5d0cc3 )
by Evan
03:17
created

Model::typeId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silk\Post;
4
5
use stdClass;
0 ignored issues
show
introduced by
Use classes must be in alphabetical order.
Loading history...
6
use WP_Post;
7
use Illuminate\Support\Collection;
8
use Silk\Type\Model as BaseModel;
9
use Silk\PostType\PostType;
10
use Silk\Post\Exception\PostNotFoundException;
11
use Silk\Post\Exception\ModelPostTypeMismatchException;
12
13
/**
14
 * @property-read WP_Post $post
15
 * @property-read int     $id
16
 *
17
 * @property int    $ID
18
 * @property int    $comment_count
19
 * @property string $comment_status
20
 * @property string $filter
21
 * @property string $guid
22
 * @property int    $menu_order
23
 * @property string $ping_status
24
 * @property string $pinged
25
 * @property int    $post_author
26
 * @property string $post_content
27
 * @property string $post_content_filtered
28
 * @property string $post_date
29
 * @property string $post_date_gmt
30
 * @property string $post_excerpt
31
 * @property string $post_mime_type
32
 * @property string $post_modified
33
 * @property string $post_modified_gmt
34
 * @property string $post_name
35
 * @property int    $post_parent
36
 * @property string $post_password
37
 * @property string $post_status
38
 * @property string $post_title
39
 * @property string $post_type
40
 * @property string $to_ping
41
 */
42
abstract class Model extends BaseModel
43
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
44
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
45
     * The post type of the post this model wraps
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
46
     * @var string
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
47
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
48
    const POST_TYPE = '';
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
49
50
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
51
     * The object type in WordPress
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
52
     * @var string
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
53
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
54
    const OBJECT_TYPE = 'post';
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
55
56
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
57
     * The primary ID property on the object
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
58
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
59
    const ID_PROPERTY = 'ID';
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
60
61
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
62
     * Create a new instance
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
63
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
64
     * @param WP_Post $post  Post object to model
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Possible doc block error: WP_Post seems to be missing type null.
Loading history...
introduced by
WP_Post => \WP_Post
Loading history...
65
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
66
    public function __construct(WP_Post $post = null)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
67
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
68
        if (! $post) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
69
            $post = new WP_Post(new stdClass);
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Calling class constructors must always include parentheses
Loading history...
70
            $post->post_type = static::postTypeId();
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
71
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
72
73
        $this->object = $post;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
74
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
75
76
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
77
     * Create a new instance from the given WP_Post object
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
78
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
79
     * @param  WP_Post $post
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
WP_Post => \WP_Post
Loading history...
80
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
81
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
82
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
83
    public static function fromWpPost(WP_Post $post)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
84
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
85
        if ($post->post_type !== static::postTypeId()) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
86
            throw new ModelPostTypeMismatchException(static::class, $post);
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
87
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
88
89
        return new static($post);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
90
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
91
92
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
93
     * Create a new instance from a Post with the given ID
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
94
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
95
     * @param  int|string $id  Post ID of post to create the instance from
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
96
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
97
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
98
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
99
    public static function fromID($id)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
100
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
101
        $post = WP_Post::get_instance($id);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
102
103
        if (false === $post) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Usage of Yoda conditions is not allowed. Switch the expression order.
Loading history...
104
            throw new PostNotFoundException("No post found with ID {$id}");
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
105
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
106
107
        return static::fromWpPost($post);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
108
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
109
110
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
111
     * Create a new instance from a Post with the given slug
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
112
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
113
     * @param  string $slug  the post slug
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
114
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
115
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
116
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
117
    public static function fromSlug($slug)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
118
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
119
        $found = static::whereSlug($slug)->limit(1)->results();
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
120
121
        if ($found->isEmpty()) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
122
            throw new PostNotFoundException("No post found with slug {$slug}");
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
123
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
124
125
        return $found->first();
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
126
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
127
128
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
129
     * Create a new instance from the global $post
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
130
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
131
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
132
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
133
    public static function fromGlobal()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
134
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
135
        if (! $GLOBALS['post'] instanceof WP_Post) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
136
            throw new PostNotFoundException('Global $post not an instance of WP_Post');
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
137
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
138
139
        return static::fromWpPost($GLOBALS['post']);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
140
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
141
142
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
143
     * Create a new post of the model's type
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
144
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
145
     * @param  array $attributes
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
146
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
147
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
148
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
149
    public static function create($attributes = [])
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
150
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
151
        $post = new WP_Post((object)
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
152
            Collection::make($attributes)
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
153
                ->except(static::ID_PROPERTY)
0 ignored issues
show
introduced by
16 spaces found, expected 4 tabs
Loading history...
154
                ->put('post_type', static::postTypeId())
0 ignored issues
show
introduced by
16 spaces found, expected 4 tabs
Loading history...
155
                ->all()
0 ignored issues
show
introduced by
16 spaces found, expected 4 tabs
Loading history...
156
        );
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
157
158
        return static::fromWpPost($post)->save();
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
159
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
160
161
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
162
     * Get the post type identifier for this model
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
163
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
164
     * @return string post type identifier (slug)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
165
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
166
    public static function postTypeId()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
167
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
168
        return static::POST_TYPE;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
169
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
170
171
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
172
     * Get the post type API
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
173
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
174
     * @return mixed        Loads an existing type as a new PostType,
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
175
     *                      or returns a new PostTypeBuilder for registering a new type.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
176
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
177
    public static function postType()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
178
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
179
        return PostType::make(static::postTypeId());
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
180
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
181
182
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
183
     * Send the post to the trash
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
184
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
185
     * If trash is disabled, the post or page is permanently deleted.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
186
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
187
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
188
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
189
    public function trash()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
190
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
191
        if (wp_trash_post($this->id)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
192
            $this->refresh();
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
193
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
194
195
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
196
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
197
198
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
199
     * Restore a post or page from the Trash
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
200
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
201
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
202
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
203
    public function untrash()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
204
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
205
        if (wp_untrash_post($this->id)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
206
            $this->refresh();
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
207
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
208
209
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
210
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
211
212
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
213
     * Get a new query builder for the model.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
214
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
215
     * @return QueryBuilder
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Invalid class name "QueryBuilder"
Loading history...
216
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
217
    public function newQuery()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
218
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
219
        return QueryBuilder::make()->setModel($this);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
220
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
221
222
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
223
     * Get the array of actions and their respective handler classes.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
224
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
225
     * @return array
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
226
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
227
    protected function actionClasses()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
228
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
229
        return [
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
230
            'save'   => Action\PostSaver::class,
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Double space found
Loading history...
introduced by
Use statement Action\PostSaver for PostSaver should be in use block.
Loading history...
231
            'load'   => Action\PostLoader::class,
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Double space found
Loading history...
introduced by
Use statement Action\PostLoader for PostLoader should be in use block.
Loading history...
232
            'delete' => Action\PostDeleter::class,
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Use statement Action\PostDeleter for PostDeleter should be in use block.
Loading history...
233
        ];
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
234
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
235
}
0 ignored issues
show
introduced by
Closing brace of a class must have a new line between itself and the last content.
Loading history...
236