These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Silk\Post; |
||
4 | |||
5 | use stdClass; |
||
6 | use WP_Post; |
||
7 | use Silk\Type\Model as BaseModel; |
||
8 | use Silk\PostType\PostType; |
||
9 | use Silk\Exception\WP_ErrorException; |
||
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 array|WP_Post $post Post object or array of attributes |
||
65 | * |
||
66 | * @throws ModelPostTypeMismatchException |
||
67 | */ |
||
68 | View Code Duplication | public function __construct($post = []) |
|
1 ignored issue
–
show
|
|||
69 | { |
||
70 | $attributes = is_array($post) ? $post : []; |
||
71 | |||
72 | if (! $post instanceof WP_Post) { |
||
73 | $post = new WP_Post(new stdClass); |
||
74 | $post->post_type = static::postTypeId(); |
||
75 | } elseif ($post->post_type !== static::postTypeId()) { |
||
76 | throw new ModelPostTypeMismatchException(static::class, $post); |
||
77 | } |
||
78 | |||
79 | $this->setObject($post); |
||
80 | |||
81 | $this->fill($attributes); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Create a new instance from a Post with the given ID |
||
86 | * |
||
87 | * @param int|string $id Post ID of post to create the instance from |
||
88 | * |
||
89 | * @return static |
||
90 | */ |
||
91 | public static function fromID($id) |
||
92 | { |
||
93 | $post = WP_Post::get_instance($id); |
||
94 | |||
95 | if (false === $post) { |
||
96 | throw new PostNotFoundException("No post found with ID {$id}"); |
||
97 | } |
||
98 | |||
99 | return new static($post); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Create a new instance from a Post with the given slug |
||
104 | * |
||
105 | * @param string $slug the post slug |
||
106 | * |
||
107 | * @return static |
||
108 | */ |
||
109 | public static function fromSlug($slug) |
||
110 | { |
||
111 | $found = static::whereSlug($slug)->limit(1)->results(); |
||
112 | |||
113 | if ($found->isEmpty()) { |
||
114 | throw new PostNotFoundException("No post found with slug {$slug}"); |
||
115 | } |
||
116 | |||
117 | return $found->first(); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Create a new instance from the global $post |
||
122 | * |
||
123 | * @return static |
||
124 | */ |
||
125 | public static function fromGlobal() |
||
126 | { |
||
127 | if (! $GLOBALS['post'] instanceof WP_Post) { |
||
128 | throw new PostNotFoundException('Global $post not an instance of WP_Post'); |
||
129 | } |
||
130 | |||
131 | return new static($GLOBALS['post']); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get the post type identifier for this model |
||
136 | * |
||
137 | * @return string post type identifier (slug) |
||
138 | */ |
||
139 | public static function postTypeId() |
||
140 | { |
||
141 | return static::POST_TYPE; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Get the post type API |
||
146 | * |
||
147 | * @return mixed Loads an existing type as a new PostType, |
||
148 | * or returns a new PostTypeBuilder for registering a new type. |
||
149 | */ |
||
150 | public static function postType() |
||
151 | { |
||
152 | return PostType::make(static::postTypeId()); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Get the permalink URL. |
||
157 | * |
||
158 | * @return string|bool The permalink URL, or false if the post does not exist. |
||
159 | */ |
||
160 | public function url() |
||
161 | { |
||
162 | return get_permalink($this->id); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Send the post to the trash |
||
167 | * |
||
168 | * If trash is disabled, the post or page is permanently deleted. |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function trash() |
||
173 | { |
||
174 | if (wp_trash_post($this->id)) { |
||
175 | $this->refresh(); |
||
176 | } |
||
177 | |||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Restore a post or page from the Trash |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function untrash() |
||
187 | { |
||
188 | if (wp_untrash_post($this->id)) { |
||
189 | $this->refresh(); |
||
190 | } |
||
191 | |||
192 | return $this; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get a new query builder for the model. |
||
197 | * |
||
198 | * @return QueryBuilder |
||
199 | */ |
||
200 | public function newQuery() |
||
201 | { |
||
202 | return QueryBuilder::make()->setModel($this); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Save the post to the database. |
||
207 | * |
||
208 | * @throws WP_ErrorException |
||
209 | * |
||
210 | * @return $this |
||
211 | */ |
||
212 | View Code Duplication | public function save() |
|
1 ignored issue
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
213 | { |
||
214 | if (! $this->id) { |
||
215 | $result = wp_insert_post($this->object->to_array(), true); |
||
216 | } else { |
||
217 | $result = wp_update_post($this->object, true); |
||
218 | } |
||
219 | |||
220 | if (is_wp_error($result)) { |
||
221 | throw new WP_ErrorException($result); |
||
222 | } |
||
223 | |||
224 | $this->setId($result)->refresh(); |
||
225 | |||
226 | return $this; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Permanently delete the post from the database. |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function delete() |
||
235 | { |
||
236 | if (wp_delete_post($this->id, true)) { |
||
237 | $this->refresh(); |
||
238 | } |
||
239 | |||
240 | return $this; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Update the modeled object with the current state from the database. |
||
245 | * |
||
246 | * @return $this |
||
247 | */ |
||
248 | public function refresh() |
||
249 | { |
||
250 | $this->setObject(WP_Post::get_instance($this->id)); |
||
251 | |||
252 | return $this; |
||
253 | } |
||
254 | } |
||
255 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.