1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Post; |
4
|
|
|
|
5
|
|
|
use stdClass; |
|
|
|
|
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
|
|
|
{ |
|
|
|
|
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 WP_Post $post Post object to model |
|
|
|
|
65
|
|
|
*/ |
|
|
|
|
66
|
|
|
public function __construct(WP_Post $post = null) |
|
|
|
|
67
|
|
|
{ |
|
|
|
|
68
|
|
|
if (! $post) { |
|
|
|
|
69
|
|
|
$post = new WP_Post(new stdClass); |
|
|
|
|
70
|
|
|
$post->post_type = static::postTypeId(); |
|
|
|
|
71
|
|
|
} |
|
|
|
|
72
|
|
|
|
73
|
|
|
$this->object = $post; |
|
|
|
|
74
|
|
|
} |
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
|
|
|
|
77
|
|
|
* Create a new instance from the given WP_Post object |
|
|
|
|
78
|
|
|
* |
|
|
|
|
79
|
|
|
* @param WP_Post $post |
|
|
|
|
80
|
|
|
* |
|
|
|
|
81
|
|
|
* @return static |
|
|
|
|
82
|
|
|
*/ |
|
|
|
|
83
|
|
|
public static function fromWpPost(WP_Post $post) |
|
|
|
|
84
|
|
|
{ |
|
|
|
|
85
|
|
|
if ($post->post_type !== static::postTypeId()) { |
|
|
|
|
86
|
|
|
throw new ModelPostTypeMismatchException(static::class, $post); |
|
|
|
|
87
|
|
|
} |
|
|
|
|
88
|
|
|
|
89
|
|
|
return new static($post); |
|
|
|
|
90
|
|
|
} |
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
|
|
|
|
93
|
|
|
* Create a new instance from a Post with the given ID |
|
|
|
|
94
|
|
|
* |
|
|
|
|
95
|
|
|
* @param int|string $id Post ID of post to create the instance from |
|
|
|
|
96
|
|
|
* |
|
|
|
|
97
|
|
|
* @return static |
|
|
|
|
98
|
|
|
*/ |
|
|
|
|
99
|
|
|
public static function fromID($id) |
|
|
|
|
100
|
|
|
{ |
|
|
|
|
101
|
|
|
$post = WP_Post::get_instance($id); |
|
|
|
|
102
|
|
|
|
103
|
|
|
if (false === $post) { |
|
|
|
|
104
|
|
|
throw new PostNotFoundException("No post found with ID {$id}"); |
|
|
|
|
105
|
|
|
} |
|
|
|
|
106
|
|
|
|
107
|
|
|
return static::fromWpPost($post); |
|
|
|
|
108
|
|
|
} |
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
|
|
|
|
111
|
|
|
* Create a new instance from a Post with the given slug |
|
|
|
|
112
|
|
|
* |
|
|
|
|
113
|
|
|
* @param string $slug the post slug |
|
|
|
|
114
|
|
|
* |
|
|
|
|
115
|
|
|
* @return static |
|
|
|
|
116
|
|
|
*/ |
|
|
|
|
117
|
|
|
public static function fromSlug($slug) |
|
|
|
|
118
|
|
|
{ |
|
|
|
|
119
|
|
|
$found = static::whereSlug($slug)->limit(1)->results(); |
|
|
|
|
120
|
|
|
|
121
|
|
|
if ($found->isEmpty()) { |
|
|
|
|
122
|
|
|
throw new PostNotFoundException("No post found with slug {$slug}"); |
|
|
|
|
123
|
|
|
} |
|
|
|
|
124
|
|
|
|
125
|
|
|
return $found->first(); |
|
|
|
|
126
|
|
|
} |
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
|
|
|
|
129
|
|
|
* Create a new instance from the global $post |
|
|
|
|
130
|
|
|
* |
|
|
|
|
131
|
|
|
* @return static |
|
|
|
|
132
|
|
|
*/ |
|
|
|
|
133
|
|
|
public static function fromGlobal() |
|
|
|
|
134
|
|
|
{ |
|
|
|
|
135
|
|
|
if (! $GLOBALS['post'] instanceof WP_Post) { |
|
|
|
|
136
|
|
|
throw new PostNotFoundException('Global $post not an instance of WP_Post'); |
|
|
|
|
137
|
|
|
} |
|
|
|
|
138
|
|
|
|
139
|
|
|
return static::fromWpPost($GLOBALS['post']); |
|
|
|
|
140
|
|
|
} |
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
|
|
|
|
143
|
|
|
* Create a new post of the model's type |
|
|
|
|
144
|
|
|
* |
|
|
|
|
145
|
|
|
* @param array $attributes |
|
|
|
|
146
|
|
|
* |
|
|
|
|
147
|
|
|
* @return static |
|
|
|
|
148
|
|
|
*/ |
|
|
|
|
149
|
|
|
public static function create($attributes = []) |
|
|
|
|
150
|
|
|
{ |
|
|
|
|
151
|
|
|
$post = new WP_Post((object) |
|
|
|
|
152
|
|
|
Collection::make($attributes) |
|
|
|
|
153
|
|
|
->except(static::ID_PROPERTY) |
|
|
|
|
154
|
|
|
->put('post_type', static::postTypeId()) |
|
|
|
|
155
|
|
|
->all() |
|
|
|
|
156
|
|
|
); |
|
|
|
|
157
|
|
|
|
158
|
|
|
return static::fromWpPost($post)->save(); |
|
|
|
|
159
|
|
|
} |
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
|
|
|
|
162
|
|
|
* Get the post type identifier for this model |
|
|
|
|
163
|
|
|
* |
|
|
|
|
164
|
|
|
* @return string post type identifier (slug) |
|
|
|
|
165
|
|
|
*/ |
|
|
|
|
166
|
|
|
public static function postTypeId() |
|
|
|
|
167
|
|
|
{ |
|
|
|
|
168
|
|
|
return static::POST_TYPE; |
|
|
|
|
169
|
|
|
} |
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
|
|
|
|
172
|
|
|
* Get the post type API |
|
|
|
|
173
|
|
|
* |
|
|
|
|
174
|
|
|
* @return mixed Loads an existing type as a new PostType, |
|
|
|
|
175
|
|
|
* or returns a new PostTypeBuilder for registering a new type. |
|
|
|
|
176
|
|
|
*/ |
|
|
|
|
177
|
|
|
public static function postType() |
|
|
|
|
178
|
|
|
{ |
|
|
|
|
179
|
|
|
return PostType::make(static::postTypeId()); |
|
|
|
|
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
|
|
|
* Get the array of actions and their respective handler classes. |
|
|
|
|
224
|
|
|
* |
|
|
|
|
225
|
|
|
* @return array |
|
|
|
|
226
|
|
|
*/ |
|
|
|
|
227
|
|
|
protected function actionClasses() |
|
|
|
|
228
|
|
|
{ |
|
|
|
|
229
|
|
|
return [ |
|
|
|
|
230
|
|
|
'save' => Action\PostSaver::class, |
|
|
|
|
231
|
|
|
'load' => Action\PostLoader::class, |
|
|
|
|
232
|
|
|
'delete' => Action\PostDeleter::class, |
|
|
|
|
233
|
|
|
]; |
|
|
|
|
234
|
|
|
} |
|
|
|
|
235
|
|
|
} |
|
|
|
|
236
|
|
|
|