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