1 | <?php |
||
44 | abstract class Model |
||
45 | { |
||
46 | /** |
||
47 | * The post |
||
48 | * @var WP_Post |
||
49 | */ |
||
50 | protected $post; |
||
51 | |||
52 | /** |
||
53 | * Post ID |
||
54 | * @var int |
||
55 | */ |
||
56 | protected $id; |
||
57 | |||
58 | /** |
||
59 | * The post type of the post this model wraps |
||
60 | * @var string |
||
61 | */ |
||
62 | const POST_TYPE = ''; |
||
63 | |||
64 | /** |
||
65 | * The object type in WordPress |
||
66 | * @var string |
||
67 | */ |
||
68 | const OBJECT_TYPE = 'post'; |
||
69 | |||
70 | use TypeMeta; |
||
71 | |||
72 | /** |
||
73 | * Create a new instance |
||
74 | * |
||
75 | * @param WP_Post $post Post object to model |
||
76 | */ |
||
77 | public function __construct(WP_Post $post = null) |
||
87 | |||
88 | /** |
||
89 | * Create a new instance from the given WP_Post object |
||
90 | * |
||
91 | * @param WP_Post $post |
||
92 | * |
||
93 | * @return static |
||
94 | */ |
||
95 | public static function fromWpPost(WP_Post $post) |
||
103 | |||
104 | /** |
||
105 | * Create a new instance from a Post with the given ID |
||
106 | * |
||
107 | * @param int|string $id Post ID of post to create the instance from |
||
108 | * |
||
109 | * @return static |
||
110 | */ |
||
111 | public static function fromID($id) |
||
121 | |||
122 | /** |
||
123 | * Create a new instance from a Post with the given slug |
||
124 | * |
||
125 | * @param string $slug the post slug |
||
126 | * |
||
127 | * @return static |
||
128 | */ |
||
129 | public static function fromSlug($slug) |
||
139 | |||
140 | /** |
||
141 | * Create a new instance from the global $post |
||
142 | * |
||
143 | * @return static |
||
144 | */ |
||
145 | public static function fromGlobal() |
||
153 | |||
154 | /** |
||
155 | * Create a new post of the model's type |
||
156 | * |
||
157 | * @param array $attributes |
||
158 | * |
||
159 | * @return static |
||
160 | */ |
||
161 | public static function create($attributes = []) |
||
172 | |||
173 | /** |
||
174 | * Get the post type identifier for this model |
||
175 | * |
||
176 | * @return string post type identifier (slug) |
||
177 | */ |
||
178 | public static function postTypeId() |
||
182 | |||
183 | /** |
||
184 | * Get the post type API |
||
185 | * |
||
186 | * @return mixed Loads an existing type as a new PostType, |
||
187 | * or returns a new PostTypeBuilder for registering a new type. |
||
188 | */ |
||
189 | public static function postType() |
||
193 | |||
194 | /** |
||
195 | * Send the post to the trash |
||
196 | * |
||
197 | * If trash is disabled, the post or page is permanently deleted. |
||
198 | * |
||
199 | * @return $this |
||
200 | */ |
||
201 | public function trash() |
||
209 | |||
210 | /** |
||
211 | * Restore a post or page from the Trash |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function untrash() |
||
223 | |||
224 | /** |
||
225 | * Permanently deletes the post and related objects |
||
226 | * |
||
227 | * When the post and page is permanently deleted, everything that is |
||
228 | * tied to it is deleted also. This includes comments, post meta fields, |
||
229 | * and terms associated with the post. |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function delete() |
||
241 | |||
242 | /** |
||
243 | * Refresh the post object from cache/database |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function refresh() |
||
253 | |||
254 | /** |
||
255 | * Update the post in the database |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function save() |
||
275 | |||
276 | /** |
||
277 | * Get a new query builder for the model. |
||
278 | * |
||
279 | * @return Builder |
||
280 | */ |
||
281 | public function newQuery() |
||
285 | |||
286 | /** |
||
287 | * Create a new query builder instance for this model type. |
||
288 | * |
||
289 | * @return Builder |
||
290 | */ |
||
291 | public static function query() |
||
295 | |||
296 | /** |
||
297 | * Magic getter |
||
298 | * |
||
299 | * @param string $property |
||
300 | * |
||
301 | * @return mixed |
||
302 | */ |
||
303 | public function __get($property) |
||
314 | |||
315 | /** |
||
316 | * Magic setter |
||
317 | * |
||
318 | * @param string $property |
||
319 | * @param mixed $value |
||
320 | */ |
||
321 | public function __set($property, $value) |
||
327 | |||
328 | /** |
||
329 | * Handle dynamic static method calls on the model class. |
||
330 | * |
||
331 | * Proxies calls to direct method calls on a new instance |
||
332 | * |
||
333 | * @param string $method |
||
334 | * @param array $arguments |
||
335 | * |
||
336 | * @return mixed |
||
337 | */ |
||
338 | public static function __callStatic($method, array $arguments) |
||
342 | |||
343 | /** |
||
344 | * Handle dynamic method calls into the model. |
||
345 | * |
||
346 | * @param string $method |
||
347 | * @param array $arguments |
||
348 | * |
||
349 | * @return mixed |
||
350 | */ |
||
351 | public function __call($method, $arguments) |
||
357 | |||
358 | } |
||
359 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: