1 | <?php |
||
45 | abstract class Model extends ActiveRecord |
||
46 | { |
||
47 | /** |
||
48 | * The post type of the post this model wraps |
||
49 | * @var string |
||
50 | */ |
||
51 | const POST_TYPE = ''; |
||
52 | |||
53 | /** |
||
54 | * The object type in WordPress |
||
55 | * @var string |
||
56 | */ |
||
57 | const OBJECT_TYPE = 'post'; |
||
58 | |||
59 | /** |
||
60 | * The primary ID property on the object |
||
61 | */ |
||
62 | const ID_PROPERTY = 'ID'; |
||
63 | |||
64 | use QueryBuilder; |
||
65 | |||
66 | /** |
||
67 | * Create a new instance |
||
68 | * |
||
69 | * @param WP_Post $post Post object to model |
||
70 | */ |
||
71 | public function __construct(WP_Post $post = null) |
||
72 | { |
||
73 | if (! $post) { |
||
74 | $post = new WP_Post(new stdClass); |
||
75 | $post->post_type = static::postTypeId(); |
||
76 | } |
||
77 | |||
78 | $this->object = $post; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Create a new instance from the given WP_Post object |
||
83 | * |
||
84 | * @param WP_Post $post |
||
85 | * |
||
86 | * @return static |
||
87 | */ |
||
88 | public static function fromWpPost(WP_Post $post) |
||
96 | |||
97 | /** |
||
98 | * Create a new instance from a Post with the given ID |
||
99 | * |
||
100 | * @param int|string $id Post ID of post to create the instance from |
||
101 | * |
||
102 | * @return static |
||
103 | */ |
||
104 | public static function fromID($id) |
||
114 | |||
115 | /** |
||
116 | * Create a new instance from a Post with the given slug |
||
117 | * |
||
118 | * @param string $slug the post slug |
||
119 | * |
||
120 | * @return static |
||
121 | */ |
||
122 | public static function fromSlug($slug) |
||
132 | |||
133 | /** |
||
134 | * Create a new instance from the global $post |
||
135 | * |
||
136 | * @return static |
||
137 | */ |
||
138 | public static function fromGlobal() |
||
146 | |||
147 | /** |
||
148 | * Create a new post of the model's type |
||
149 | * |
||
150 | * @param array $attributes |
||
151 | * |
||
152 | * @return static |
||
153 | */ |
||
154 | public static function create($attributes = []) |
||
155 | { |
||
156 | $post = new WP_Post((object) |
||
157 | Collection::make($attributes) |
||
158 | ->except(static::ID_PROPERTY) |
||
159 | ->put('post_type', static::postTypeId()) |
||
160 | ->all() |
||
161 | ); |
||
162 | |||
163 | return static::fromWpPost($post)->save(); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get the post type identifier for this model |
||
168 | * |
||
169 | * @return string post type identifier (slug) |
||
170 | */ |
||
171 | public static function postTypeId() |
||
175 | |||
176 | /** |
||
177 | * Get the post type API |
||
178 | * |
||
179 | * @return mixed Loads an existing type as a new PostType, |
||
180 | * or returns a new PostTypeBuilder for registering a new type. |
||
181 | */ |
||
182 | public static function postType() |
||
186 | |||
187 | /** |
||
188 | * Send the post to the trash |
||
189 | * |
||
190 | * If trash is disabled, the post or page is permanently deleted. |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function trash() |
||
202 | |||
203 | /** |
||
204 | * Restore a post or page from the Trash |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function untrash() |
||
216 | |||
217 | /** |
||
218 | * Get a new query builder for the model. |
||
219 | * |
||
220 | * @return Builder |
||
221 | */ |
||
222 | public function newQuery() |
||
226 | |||
227 | /** |
||
228 | * Get the array of actions and their respective handler classes. |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | protected function actionClasses() |
||
240 | } |
||
241 |
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: