Completed
Pull Request — master (#23)
by Evan
05:32 queued 02:29
created

Model::postType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silk\Post;
4
5
use stdClass;
6
use WP_Post;
7
use Silk\Type\Model as BaseModel;
8
use Silk\PostType\PostType;
9
use Silk\Exception\WP_ErrorException;
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
{
44
    /**
45
     * The post type of the post this model wraps
46
     * @var string
47
     */
48
    const POST_TYPE = '';
49
50
    /**
51
     * The object type in WordPress
52
     * @var string
53
     */
54
    const OBJECT_TYPE = 'post';
55
56
    /**
57
     * The primary ID property on the object
58
     */
59
    const ID_PROPERTY = 'ID';
60
61
    /**
62
     * Create a new instance
63
     *
64
     * @param array|WP_Post $post  Post object or array of attributes
65
     *
66
     * @throws ModelPostTypeMismatchException
67
     */
68 View Code Duplication
    public function __construct($post = [])
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $attributes = is_array($post) ? $post : [];
71
72
        if (! $post instanceof WP_Post) {
73
            $post = new WP_Post(new stdClass);
74
            $post->post_type = static::postTypeId();
75
        } elseif ($post->post_type !== static::postTypeId()) {
76
            throw new ModelPostTypeMismatchException(static::class, $post);
77
        }
78
79
        $this->setObject($post);
80
81
        $this->fill($attributes);
82
    }
83
84
    /**
85
     * Create a new instance from the given WP_Post object
86
     * @deprecated - use static::make()
87
     *
88
     * @param  WP_Post $post
89
     *
90
     * @return static
91
     */
92
    public static function fromWpPost(WP_Post $post)
93
    {
94
        if ($post->post_type !== static::postTypeId()) {
95
            throw new ModelPostTypeMismatchException(static::class, $post);
96
        }
97
98
        return new static($post);
99
    }
100
101
    /**
102
     * Create a new instance from a Post with the given ID
103
     *
104
     * @param  int|string $id  Post ID of post to create the instance from
105
     *
106
     * @return static
107
     */
108
    public static function fromID($id)
109
    {
110
        $post = WP_Post::get_instance($id);
111
112
        if (false === $post) {
113
            throw new PostNotFoundException("No post found with ID {$id}");
114
        }
115
116
        return new static($post);
117
    }
118
119
    /**
120
     * Create a new instance from a Post with the given slug
121
     *
122
     * @param  string $slug  the post slug
123
     *
124
     * @return static
125
     */
126
    public static function fromSlug($slug)
127
    {
128
        $found = static::whereSlug($slug)->limit(1)->results();
129
130
        if ($found->isEmpty()) {
131
            throw new PostNotFoundException("No post found with slug {$slug}");
132
        }
133
134
        return $found->first();
135
    }
136
137
    /**
138
     * Create a new instance from the global $post
139
     *
140
     * @return static
141
     */
142
    public static function fromGlobal()
143
    {
144
        if (! $GLOBALS['post'] instanceof WP_Post) {
145
            throw new PostNotFoundException('Global $post not an instance of WP_Post');
146
        }
147
148
        return new static($GLOBALS['post']);
149
    }
150
151
    /**
152
     * Get the post type identifier for this model
153
     *
154
     * @return string post type identifier (slug)
155
     */
156
    public static function postTypeId()
157
    {
158
        return static::POST_TYPE;
159
    }
160
161
    /**
162
     * Get the post type API
163
     *
164
     * @return mixed        Loads an existing type as a new PostType,
165
     *                      or returns a new PostTypeBuilder for registering a new type.
166
     */
167
    public static function postType()
168
    {
169
        return PostType::make(static::postTypeId());
170
    }
171
172
    /**
173
     * Get the permalink URL.
174
     *
175
     * @return string|bool  The permalink URL, or false if the post does not exist.
176
     */
177
    public function url()
178
    {
179
        return get_permalink($this->id);
180
    }
181
182
    /**
183
     * Send the post to the trash
184
     *
185
     * If trash is disabled, the post or page is permanently deleted.
186
     *
187
     * @return $this
188
     */
189
    public function trash()
190
    {
191
        if (wp_trash_post($this->id)) {
192
            $this->refresh();
193
        }
194
195
        return $this;
196
    }
197
198
    /**
199
     * Restore a post or page from the Trash
200
     *
201
     * @return $this
202
     */
203
    public function untrash()
204
    {
205
        if (wp_untrash_post($this->id)) {
206
            $this->refresh();
207
        }
208
209
        return $this;
210
    }
211
212
    /**
213
     * Get a new query builder for the model.
214
     *
215
     * @return QueryBuilder
216
     */
217
    public function newQuery()
218
    {
219
        return QueryBuilder::make()->setModel($this);
220
    }
221
222
    /**
223
     * Save the post to the database.
224
     *
225
     * @throws WP_ErrorException
226
     *
227
     * @return $this
228
     */
229 View Code Duplication
    public function save()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
    {
231
        if (! $this->id) {
232
            $result = wp_insert_post($this->object->to_array(), true);
233
        } else {
234
            $result = wp_update_post($this->object, true);
235
        }
236
237
        if (is_wp_error($result)) {
238
            throw new WP_ErrorException($result);
239
        }
240
241
        $this->setId($result)->refresh();
242
243
        return $this;
244
    }
245
246
    /**
247
     * Permanently delete the post from the database.
248
     *
249
     * @return $this
250
     */
251
    public function delete()
252
    {
253
        if (wp_delete_post($this->id, true)) {
254
            $this->refresh();
255
        }
256
257
        return $this;
258
    }
259
260
    /**
261
     * Update the modeled object with the current state from the database.
262
     *
263
     * @return $this
264
     */
265
    public function refresh()
266
    {
267
        $this->setObject(WP_Post::get_instance($this->id));
268
269
        return $this;
270
    }
271
}
272