|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\Castor; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\Castor\Helpers\PostMeta; |
|
6
|
|
|
use GeminiLabs\Castor\Helpers\Theme; |
|
7
|
|
|
use GeminiLabs\Castor\Helpers\Utility; |
|
8
|
|
|
use GeminiLabs\Castor\Image; |
|
9
|
|
|
use GeminiLabs\Castor\Oembed; |
|
10
|
|
|
|
|
11
|
|
|
class Video |
|
12
|
|
|
{ |
|
13
|
|
|
public $video; |
|
14
|
|
|
|
|
15
|
|
|
protected $args; |
|
16
|
|
|
protected $image; |
|
17
|
|
|
protected $oembed; |
|
18
|
|
|
protected $postmeta; |
|
19
|
|
|
protected $theme; |
|
20
|
|
|
protected $utility; |
|
21
|
|
|
|
|
22
|
|
View Code Duplication |
public function __construct( Image $image, Oembed $oembed, PostMeta $postmeta, Theme $theme, Utility $utility ) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->image = $image; |
|
25
|
|
|
$this->oembed = $oembed; |
|
26
|
|
|
$this->postmeta = $postmeta; |
|
27
|
|
|
$this->theme = $theme; |
|
28
|
|
|
$this->utility = $utility; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function get( $args = [] ) |
|
32
|
|
|
{ |
|
33
|
|
|
$args = $this->normalize( $args ); |
|
34
|
|
|
$this->video = $this->oembed->request( $args['url'], $args['player'] ); |
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function render() |
|
39
|
|
|
{ |
|
40
|
|
|
if( !isset( $this->video->html ))return; |
|
41
|
|
|
return sprintf( |
|
42
|
|
|
'<div class="video embed">%s%s</div>', |
|
43
|
|
|
$this->renderScreenshot(), |
|
44
|
|
|
$this->video->html |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function renderPlayButton() |
|
49
|
|
|
{ |
|
50
|
|
|
return sprintf( |
|
51
|
|
|
'<div class="video-play">' . |
|
52
|
|
|
'<div class="video-play-pulse pulse1"></div>' . |
|
53
|
|
|
'<div class="video-play-pulse pulse2"></div>' . |
|
54
|
|
|
'<div class="video-play-pulse pulse3"></div>' . |
|
55
|
|
|
'<a href="%s" class="video-play-btn">%s</a>' . |
|
56
|
|
|
'</div>', |
|
57
|
|
|
$this->args['url'], |
|
58
|
|
|
$this->theme->svg( 'play.svg' ) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function renderScreenshot() |
|
63
|
|
|
{ |
|
64
|
|
|
if( !$this->args['image'] )return; |
|
65
|
|
|
return sprintf( '<div class="video-screenshot" style="background-image: url(%s)">%s</div>%s', |
|
66
|
|
|
$this->args['image'], |
|
67
|
|
|
$this->renderPlayButton(), |
|
68
|
|
|
$this->renderSpinner() |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function renderSpinner() |
|
73
|
|
|
{ |
|
74
|
|
|
return sprintf( |
|
75
|
|
|
'<div class="video-spinner">' . |
|
76
|
|
|
'<div class="spinner"><div class="spinner-dots">%s</div></div>' . |
|
77
|
|
|
'</div>', |
|
78
|
|
|
implode( '', array_fill( 0, 8, '<div class="spinner-dot"></div>' )) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function setImage( $image ) |
|
83
|
|
|
{ |
|
84
|
|
|
$image = $this->image->get( $image )->image; |
|
85
|
|
|
$this->args['image'] = isset( $image->large ) |
|
86
|
|
|
? $image->large['url'] |
|
87
|
|
|
: null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function setUrl( $url ) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->args['url'] = !filter_var( $url, FILTER_VALIDATE_URL ) |
|
93
|
|
|
? $this->postmeta->get( $url ) |
|
94
|
|
|
: $url; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function normalize( $args ) |
|
98
|
|
|
{ |
|
99
|
|
|
if( is_string( $args )) { |
|
100
|
|
|
$args = ['url' => $args]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$this->args = shortcode_atts([ |
|
104
|
|
|
'image' => '', // string || int |
|
105
|
|
|
'player' => '', // string || array |
|
106
|
|
|
'url' => '', // string |
|
107
|
|
|
], $args ); |
|
108
|
|
|
|
|
109
|
|
|
foreach( $this->args as $key => $value ) { |
|
110
|
|
|
$method = $this->utility->buildMethodName( $key, 'set' ); |
|
111
|
|
|
if( !method_exists( $this, $method ))continue; |
|
112
|
|
|
call_user_func([ $this, $method ], $value ); |
|
113
|
|
|
} |
|
114
|
|
|
return $this->args; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|