Video_Post
Class Post
Source
File: includes/libs/video-post.class.php
016 | class Video_Post{ |
017 |
018 | /** |
019 | * Vimeo video ID. |
020 | * |
021 | * @var string |
022 | */ |
023 | public $video_id = '' ; |
024 |
025 | /** |
026 | * Vimeo uploader username. |
027 | * |
028 | * @var string |
029 | */ |
030 | public $uploader = '' ; |
031 |
032 | /** |
033 | * Vimeo uploader URL. |
034 | * |
035 | * @var string |
036 | */ |
037 | public $uploader_uri = '' ; |
038 |
039 | /** |
040 | * Vimeo publishing date. |
041 | * |
042 | * @var string |
043 | */ |
044 | public $published = '' ; |
045 |
046 | /** |
047 | * Vimeo publishing date formatted as "M dS, Y" |
048 | * |
049 | * @var string |
050 | */ |
051 | public $_published = '' ; |
052 |
053 | /** |
054 | * Vimeo video update date. |
055 | * |
056 | * @var string |
057 | */ |
058 | public $updated = '' ; |
059 |
060 | /** |
061 | * Vimeo video title. |
062 | * |
063 | * @var string |
064 | */ |
065 | public $title = '' ; |
066 |
067 | /** |
068 | * Vimeo video description. |
069 | * |
070 | * @var string |
071 | */ |
072 | public $description = '' ; |
073 |
074 | /** |
075 | * @ignore |
076 | * |
077 | * @todo Determine how categories will be stored |
078 | * |
079 | * @var |
080 | */ |
081 | public $category ; |
082 |
083 | /** |
084 | * Vimeo video tags. |
085 | * |
086 | * @var array |
087 | */ |
088 | public $tags = []; |
089 |
090 | /** |
091 | * Vimeo video duration. |
092 | * |
093 | * @var string |
094 | */ |
095 | public $duration = '' ; |
096 |
097 | /** |
098 | * Vimeo video duration as HH:MM:SS. |
099 | * |
100 | * @var string |
101 | */ |
102 | public $_duration = '' ; |
103 |
104 | /** |
105 | * Vimeo video thumbnails. |
106 | * |
107 | * @var array |
108 | */ |
109 | public $thumbnails = []; |
110 |
111 | /** |
112 | * The unique image URI. |
113 | * |
114 | * @var string |
115 | */ |
116 | public $image_uri = '' ; |
117 |
118 | /** |
119 | * Video statistics (view count, comment count and like count). |
120 | * |
121 | * @var array |
122 | */ |
123 | public $stats = []; |
124 |
125 | /** |
126 | * Video privacy. |
127 | * |
128 | * @var string |
129 | */ |
130 | public $privacy = '' ; |
131 |
132 | /** |
133 | * Video view privacy. |
134 | * |
135 | * @var string |
136 | */ |
137 | public $view_privacy = '' ; |
138 |
139 | /** |
140 | * Vimeo embed privacy. |
141 | * |
142 | * @var string |
143 | */ |
144 | public $embed_privacy = '' ; |
145 |
146 | /** |
147 | * Video size (width, height and size ratio). |
148 | * |
149 | * @var array |
150 | */ |
151 | public $size = []; |
152 |
153 | /** |
154 | * Vimeo video type (video, live). |
155 | * |
156 | * @var string |
157 | */ |
158 | public $type = '' ; |
159 |
160 | /** |
161 | * Vimeo video URI (ie. videos/2312953). |
162 | * |
163 | * @var string |
164 | */ |
165 | public $uri = '' ; |
166 |
167 | /** |
168 | * Vimeo video URL. |
169 | * |
170 | * @var string |
171 | */ |
172 | public $link = '' ; |
173 |
174 | /** |
175 | * The WP_Post object. |
176 | * |
177 | * @var array|\WP_Post|null |
178 | */ |
179 | private $_post = null; |
180 |
181 | /** |
182 | * The video image import class reference. |
183 | * |
184 | * @var Image_Import |
185 | */ |
186 | private $_image ; |
187 |
188 | /** |
189 | * Post constructor. |
190 | * |
191 | * @param \WP_Post|int $post |
192 | */ |
193 | public function __construct( $post ) { |
194 | $this ->_post = get_post( $post , OBJECT, 'raw' ); |
195 | $this ->_image = new Image_Import( $this ); |
196 |
197 | $meta = $this ->get_video_data(); |
198 | if ( $meta ){ |
199 | $this ->_set_properties( $meta ); |
200 | } |
201 | } |
202 |
203 | /** |
204 | * Set class properties |
205 | * @ignore |
206 | * |
207 | * @param $data |
208 | */ |
209 | private function _set_properties( $data ){ |
210 | foreach ( $data as $key => $value ){ |
211 | if ( property_exists( $this , $key ) ){ |
212 | $this -> $key = $value ; |
213 | } |
214 | } |
215 | } |
216 |
217 | /** |
218 | * Check if it is a video post. |
219 | * |
220 | * @return bool |
221 | */ |
222 | public function is_video(){ |
223 | if ( ! $this ->get_post() ){ |
224 | return false; |
225 | } |
226 |
227 | $result = $this ->cpt()->get_post_type() == $this ->get_post()->post_type; |
228 |
229 | /** |
230 | * Filter on checkup if the post is a Vimeotheque post. |
231 | * |
232 | * @param bool $result Is the post a Vimeotheque post (true) or is not (false). |
233 | * @param Video_Post $video_post Video post \Vimeotheque\Video_Post object reference. |
234 | */ |
235 | return apply_filters( 'vimeotheque\video\is_video' , $result , $this ); |
236 | } |
237 |
238 | /** |
239 | * Get the video data stored on post. |
240 | * |
241 | * @return mixed|void |
242 | */ |
243 | public function get_video_data(){ |
244 | if ( ! $this ->get_post() ){ |
245 | return []; |
246 | } |
247 |
248 | $meta = $this ->get_meta( |
249 | $this ->cpt()->get_post_settings()->get_meta_video_data() |
250 | ); |
251 |
252 | if ( isset( $meta [ 'video_id' ] ) && stristr ( $meta [ 'video_id' ] , ':' ) ){ |
253 | $parts = explode ( ':' , $meta [ 'video_id' ] ); |
254 | $meta [ 'video_id' ] = $parts [0]; |
255 | } |
256 |
257 | return $meta ; |
258 | } |
259 |
260 | /** |
261 | * Set video data on post. |
262 | * |
263 | * @param array $data An array of options that should be set on the post. |
264 | * |
265 | * @return bool|int|void |
266 | */ |
267 | public function set_video_data( $data ){ |
268 | if ( ! $this ->_post ){ |
269 | return ; |
270 | } |
271 |
272 | $_data = $this ->get_video_data(); |
273 | if ( $_data ) { |
274 | foreach ( $data as $key => $value ) { |
275 | if ( isset( $_data [ $key ] ) ) { |
276 | $_data [ $key ] = $value ; |
277 | } |
278 | } |
279 | } else { |
280 | // if set for the first time, set the entire data as post meta |
281 | $_data = $data ; |
282 | $this ->_set_properties( $data ); |
283 | } |
284 |
285 | return $this ->update_meta( |
286 | $this ->cpt()->get_post_settings()->get_meta_video_data(), |
287 | $_data |
288 | ); |
289 | } |
290 |
291 | /** |
292 | * Returns embed options for the post. |
293 | * |
294 | * @param bool $output |
295 | * |
296 | * @return array|mixed|void |
297 | */ |
298 | public function get_embed_options( $output = false ){ |
299 | if ( ! $this ->_post ){ |
300 | return ; |
301 | } |
302 | /** |
303 | * @var Options |
304 | */ |
305 | $options_obj = Plugin::instance()->get_embed_options_obj(); |
306 | $override = $options_obj ->get_option( 'allow_override' ); |
307 |
308 | if ( !is_wp_error( $override ) && $override ){ |
309 | $options = $options_obj ->get_options(); |
310 | } else { |
311 | $options = $this ->get_meta( $this ->cpt()->get_post_settings()->get_meta_embed_settings() ); |
312 | foreach ( $options_obj ->get_options() as $key => $value ){ |
313 | if ( !isset( $options [ $key ] ) ){ |
314 | $options [ $key ] = $value ; |
315 | } |
316 | // when values are in output booleans needs to be 0 or 1 |
317 | if ( $output && is_bool ( $value ) ){ |
318 | $options [ $key ] = absint( $options [ $key ] ); |
319 | } |
320 | } |
321 | } |
322 |
323 | $options [ 'size_ratio' ] = false; |
324 | if ( $options_obj ->get_option( 'aspect_override' ) ){ |
325 | $options [ 'size_ratio' ] = isset( $this ->size[ 'ratio' ] ) ? $this ->size[ 'ratio' ] : false; |
326 | } |
327 |
328 | return $options ; |
329 | } |
330 |
331 | /** |
332 | * Update video playback options |
333 | * |
334 | * @param array $values |
335 | * @param bool $_use_defaults |
336 | * |
337 | * @return void |
338 | */ |
339 | public function set_embed_options( $values = [], $_use_defaults = false ){ |
340 | if ( ! $this ->_post ){ |
341 | return ; |
342 | } |
343 |
344 | $defaults = Helper::get_embed_options(); |
345 |
346 | foreach ( $defaults as $key => $val ){ |
347 | if ( is_numeric ( $val ) ){ |
348 | if ( isset( $values [ $key ] ) ){ |
349 | $defaults [ $key ] = (int) $values [ $key ]; |
350 | } else { |
351 | // if flagged to use the default values, just skip the setting and allow the default |
352 | if ( $_use_defaults ){ |
353 | continue ; |
354 | } |
355 |
356 | // some defaults are numeric but can only have value 1 or 0 |
357 | // if so, the option is a checkbox that is unchecked, set it to 0 |
358 | if ( 0 == $defaults [ $key ] || 1 == $defaults [ $key ] ){ |
359 | $defaults [ $key ] = 0; |
360 | } |
361 | } |
362 | continue ; |
363 | } |
364 | if ( is_bool ( $val ) ){ |
365 | if ( $_use_defaults ){ |
366 | continue ; |
367 | } |
368 |
369 | $defaults [ $key ] = isset( $values [ $key ] ); |
370 | continue ; |
371 | } |
372 |
373 | if ( isset( $values [ $key ] ) ){ |
374 | $defaults [ $key ] = $values [ $key ]; |
375 | } |
376 | } |
377 |
378 | $this ->update_meta( |
379 | $this ->cpt()->get_post_settings()->get_meta_embed_settings(), |
380 | $defaults |
381 | ); |
382 | } |
383 |
384 | /** |
385 | * Unpublish the post by changing its status |
386 | */ |
387 | public function unpublish(){ |
388 | $this ->set_post_status( 'pending' ); |
389 | } |
390 |
391 | /** |
392 | * @param string $post_status |
393 | * |
394 | * @return int|void|\WP_Error |
395 | */ |
396 | private function set_post_status( $post_status = 'publish' ){ |
397 | if ( ! $this ->_post ){ |
398 | return ; |
399 | } |
400 |
401 | $statuses = [ 'publish' , 'pending' , 'draft' , 'private' ]; |
402 | if ( !in_array( $post_status , $statuses ) ){ |
403 | trigger_error( |
404 | sprintf( |
405 | 'Post status cannot be changed to %s. Allowed values are: %s.' , |
406 | $post_status , |
407 | implode( ', ' , $statuses ) |
408 | ), |
409 | E_USER_WARNING |
410 | ); |
411 | return ; |
412 | } |
413 |
414 | return wp_update_post([ |
415 | 'post_status' => $post_status , |
416 | 'ID' => $this ->_post->ID |
417 | ]); |
418 | } |
419 |
420 | /** |
421 | * Set video ID meta |
422 | */ |
423 | public function set_video_id_meta(){ |
424 | if ( $this ->video_id ) { |
425 | $this ->update_meta( |
426 | $this ->cpt()->get_post_settings()->get_meta_video_id(), |
427 | $this ->video_id |
428 | ); |
429 | } |
430 | } |
431 |
432 | /** |
433 | * Set video url meta |
434 | */ |
435 | public function set_video_url_meta(){ |
436 | if ( $this ->link ){ |
437 | $this ->update_meta( |
438 | $this ->cpt()->get_post_settings()->get_meta_video_url(), |
439 | $this ->link |
440 | ); |
441 | } |
442 | } |
443 |
444 | /** |
445 | * @param $key |
446 | * @param $value |
447 | * |
448 | * @return bool|int |
449 | */ |
450 | protected function update_meta( $key , $value ){ |
451 | if ( $this ->_post ) { |
452 | return update_post_meta( |
453 | $this ->_post->ID, |
454 | $key , |
455 | $value |
456 | ); |
457 | } |
458 | } |
459 |
460 | /** |
461 | * @param $key |
462 | * @param bool $single |
463 | * @param array $default - a default value that should be returned in case the meta isn't found |
464 | * |
465 | * @return mixed |
466 | */ |
467 | public function get_meta( $key , $single = true, $default = [] ){ |
468 | if ( $this ->_post ){ |
469 | $meta = get_post_meta( |
470 | $this ->_post->ID, |
471 | $key , |
472 | $single |
473 | ); |
474 |
475 | return $meta === Helper::get_metadata_default( 'post' , $this ->_post->ID, $key , $single ) ? $default : $meta ; |
476 | } |
477 |
478 | return $default ; |
479 | } |
480 |
481 | /** |
482 | * Set featured image on post |
483 | * |
484 | * @param bool $refresh |
485 | * |
486 | * @return array|bool|void |
487 | */ |
488 | public function set_featured_image( $refresh = false ){ |
489 | return $this ->_image->set_featured_image( $refresh ); |
490 | } |
491 |
492 | /** |
493 | * Return duration in ISO format (ie. PT1H43M23S) |
494 | * |
495 | * @return string |
496 | */ |
497 | public function get_iso_duration(){ |
498 | $seconds = $this ->duration; |
499 | $iso_format = 'PT' ; |
500 |
501 | if ( $seconds > 3600 ) { |
502 | $hours = floor ( $seconds / 3600 ); |
503 | $iso_format .= $hours . 'H' ; |
504 | $seconds = $seconds - ( $hours * 3600 ); |
505 | } |
506 |
507 | if ( $seconds > 60 ) { |
508 | $minutes = floor ( $seconds / 60 ); |
509 | $iso_format .= $minutes . 'M' ; |
510 | $seconds = $seconds - ( $minutes * 60 ); |
511 | } |
512 |
513 | if ( $seconds > 0 ) { |
514 | $iso_format .= $seconds . 'S' ; |
515 | } |
516 |
517 | return $iso_format ; |
518 | } |
519 |
520 | /** |
521 | * Returns embed URL |
522 | * |
523 | * @return string |
524 | */ |
525 | public function get_embed_url(){ |
526 | return sprintf( |
528 | $this ->video_id |
529 | ); |
530 | } |
531 |
532 | /** |
533 | * @return Post_Type |
534 | */ |
535 | private function cpt(){ |
536 | return Plugin::instance()->get_cpt(); |
537 | } |
538 |
539 | /** |
540 | * @return array|\WP_Post|null |
541 | */ |
542 | public function get_post(){ |
543 | return $this ->_post; |
544 | } |
545 |
546 | } |
Methods
- __construct — Post constructor.
- cpt
- get_embed_options — Returns embed options for the post.
- get_embed_url — Returns embed URL
- get_iso_duration — Return duration in ISO format (ie. PT1H43M23S)
- get_meta
- get_post
- get_video_data — Get the video data stored on post.
- is_video — Check if it is a video post.
- set_embed_options — Update video playback options
- set_featured_image — Set featured image on post
- set_post_status
- set_video_data — Set video data on post.
- set_video_id_meta — Set video ID meta
- set_video_url_meta — Set video url meta
- unpublish — Unpublish the post by changing its status
- update_meta