Completed
Branch develop (2a5993)
by Evan
02:52
created

Model   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 9
Bugs 0 Features 2
Metric Value
c 9
b 0
f 2
dl 0
loc 242
rs 10
wmc 25
lcom 1
cbo 7

15 Methods

Rating   Name   Duplication   Size   Complexity  
A untrash() 0 8 2
A newQuery() 0 4 1
A fromID() 0 10 2
A fromSlug() 0 10 2
A fromGlobal() 0 8 2
A __construct() 0 9 2
A fromWpPost() 0 8 2
A create() 0 11 1
A postTypeId() 0 4 1
A postType() 0 4 1
A url() 0 4 1
A trash() 0 8 2
A save() 0 16 3
A delete() 0 8 2
A refresh() 0 6 1
1
<?php
2
3
namespace Silk\Post;
4
5
use Silk\Exception\WP_ErrorException;
6
use stdClass;
7
use WP_Post;
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
{
45
    /**
46
     * The post type of the post this model wraps
47
     * @var string
48
     */
49
    const POST_TYPE = '';
50
51
    /**
52
     * The object type in WordPress
53
     * @var string
54
     */
55
    const OBJECT_TYPE = 'post';
56
57
    /**
58
     * The primary ID property on the object
59
     */
60
    const ID_PROPERTY = 'ID';
61
62
    /**
63
     * Create a new instance
64
     *
65
     * @param WP_Post $post  Post object to model
66
     */
67
    public function __construct(WP_Post $post = null)
68
    {
69
        if (! $post) {
70
            $post = new WP_Post(new stdClass);
71
            $post->post_type = static::postTypeId();
72
        }
73
74
        $this->object = $post;
75
    }
76
77
    /**
78
     * Create a new instance from the given WP_Post object
79
     *
80
     * @param  WP_Post $post
81
     *
82
     * @return static
83
     */
84
    public static function fromWpPost(WP_Post $post)
85
    {
86
        if ($post->post_type !== static::postTypeId()) {
87
            throw new ModelPostTypeMismatchException(static::class, $post);
88
        }
89
90
        return new static($post);
91
    }
92
93
    /**
94
     * Create a new instance from a Post with the given ID
95
     *
96
     * @param  int|string $id  Post ID of post to create the instance from
97
     *
98
     * @return static
99
     */
100
    public static function fromID($id)
101
    {
102
        $post = WP_Post::get_instance($id);
103
104
        if (false === $post) {
105
            throw new PostNotFoundException("No post found with ID {$id}");
106
        }
107
108
        return static::fromWpPost($post);
109
    }
110
111
    /**
112
     * Create a new instance from a Post with the given slug
113
     *
114
     * @param  string $slug  the post slug
115
     *
116
     * @return static
117
     */
118
    public static function fromSlug($slug)
119
    {
120
        $found = static::whereSlug($slug)->limit(1)->results();
121
122
        if ($found->isEmpty()) {
123
            throw new PostNotFoundException("No post found with slug {$slug}");
124
        }
125
126
        return $found->first();
127
    }
128
129
    /**
130
     * Create a new instance from the global $post
131
     *
132
     * @return static
133
     */
134
    public static function fromGlobal()
135
    {
136
        if (! $GLOBALS['post'] instanceof WP_Post) {
137
            throw new PostNotFoundException('Global $post not an instance of WP_Post');
138
        }
139
140
        return static::fromWpPost($GLOBALS['post']);
141
    }
142
143
    /**
144
     * Create a new post of the model's type
145
     *
146
     * @param  array $attributes
147
     *
148
     * @return static
149
     */
150
    public static function create($attributes = [])
151
    {
152
        $post = new WP_Post((object)
153
            Collection::make($attributes)
154
                ->except(static::ID_PROPERTY)
155
                ->put('post_type', static::postTypeId())
156
                ->all()
157
        );
158
159
        return static::fromWpPost($post)->save();
160
    }
161
162
    /**
163
     * Get the post type identifier for this model
164
     *
165
     * @return string post type identifier (slug)
166
     */
167
    public static function postTypeId()
168
    {
169
        return static::POST_TYPE;
170
    }
171
172
    /**
173
     * Get the post type API
174
     *
175
     * @return mixed        Loads an existing type as a new PostType,
176
     *                      or returns a new PostTypeBuilder for registering a new type.
177
     */
178
    public static function postType()
179
    {
180
        return PostType::make(static::postTypeId());
181
    }
182
183
    /**
184
     * Get the permalink URL.
185
     *
186
     * @return string|bool  The permalink URL, or false if the post does not exist.
187
     */
188
    public function url()
189
    {
190
        return get_permalink($this->id);
191
    }
192
193
    /**
194
     * Send the post to the trash
195
     *
196
     * If trash is disabled, the post or page is permanently deleted.
197
     *
198
     * @return $this
199
     */
200
    public function trash()
201
    {
202
        if (wp_trash_post($this->id)) {
203
            $this->refresh();
204
        }
205
206
        return $this;
207
    }
208
209
    /**
210
     * Restore a post or page from the Trash
211
     *
212
     * @return $this
213
     */
214
    public function untrash()
215
    {
216
        if (wp_untrash_post($this->id)) {
217
            $this->refresh();
218
        }
219
220
        return $this;
221
    }
222
223
    /**
224
     * Get a new query builder for the model.
225
     *
226
     * @return QueryBuilder
227
     */
228
    public function newQuery()
229
    {
230
        return QueryBuilder::make()->setModel($this);
231
    }
232
233
    /**
234
     * Save the post to the database.
235
     *
236
     * @throws WP_ErrorException
237
     *
238
     * @return $this
239
     */
240
    public function save()
241
    {
242
        if (! $this->id) {
243
            $result = wp_insert_post($this->object->to_array(), true);
244
        } else {
245
            $result = wp_update_post($this->object, true);
246
        }
247
248
        if (is_wp_error($result)) {
249
            throw new WP_ErrorException($result);
250
        }
251
252
        $this->setId($result)->refresh();
253
254
        return $this;
255
    }
256
257
    /**
258
     * Permanently delete the post from the database.
259
     *
260
     * @return $this
261
     */
262
    public function delete()
263
    {
264
        if (wp_delete_post($this->id, true)) {
265
            $this->refresh();
266
        }
267
268
        return $this;
269
    }
270
271
    /**
272
     * Update the modeled object with the current state from the database.
273
     *
274
     * @return $this
275
     */
276
    public function refresh()
277
    {
278
        $this->object = WP_Post::get_instance($this->id);
279
280
        return $this;
281
    }
282
283
284
}
285