Completed
Pull Request — master (#20)
by Evan
05:00 queued 02:33
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;
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
{
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
166
     */
167
    public static function typeId()
168
    {
169
        return static::postTypeId();
170
    }
171
172
    /**
173
     * Get the post type identifier for this model
174
     *
175
     * @return string post type identifier (slug)
176
     */
177
    public static function postTypeId()
178
    {
179
        return static::POST_TYPE;
180
    }
181
182
    /**
183
     * Get the post type API
184
     *
185
     * @return mixed        Loads an existing type as a new PostType,
186
     *                      or returns a new PostTypeBuilder for registering a new type.
187
     */
188
    public static function postType()
189
    {
190
        return PostType::make(static::postTypeId());
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 (new QueryBuilder(new WP_Query))->setModel($this);
231
    }
232
233
    /**
234
     * Get the array of actions and their respective handler classes.
235
     *
236
     * @return array
237
     */
238
    protected function actionClasses()
239
    {
240
        return [
241
            'save'   => Action\PostSaver::class,
242
            'load'   => Action\PostLoader::class,
243
            'delete' => Action\PostDeleter::class,
244
        ];
245
    }
246
}
247