| 1 |
<?php |
| 2 |
namespace Depicter\Document\Models\Elements; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Arr; |
| 5 |
use Averta\Core\Utility\Embed; |
| 6 |
use Depicter\Document\Models; |
| 7 |
use Depicter\Html\Html; |
| 8 |
|
| 9 |
class EmbedVideo extends Models\Element{ |
| 10 |
|
| 11 |
public function render() { |
| 12 |
|
| 13 |
$videoUrl = !empty( $this->options->source ) ? $this->options->source : ''; |
| 14 |
|
| 15 |
$elementsAttrs = [ |
| 16 |
'data-type' => 'video' |
| 17 |
]; |
| 18 |
|
| 19 |
if( isset( $this->options->autoPlay ) ){ |
| 20 |
$elementsAttrs['data-autoplay'] = $this->options->autoPlay ? "true" : "false"; |
| 21 |
} |
| 22 |
if( isset( $this->options->autoPause ) ){ |
| 23 |
$elementsAttrs['data-auto-pause'] = $this->options->autoPause ? "true" : "false"; |
| 24 |
} |
| 25 |
if( isset( $this->options->mute ) ){ |
| 26 |
$elementsAttrs['data-muted'] = $this->options->mute ? "true" : "false"; |
| 27 |
} |
| 28 |
|
| 29 |
if( isset( $this->options->goNextSection ) ){ |
| 30 |
$elementsAttrs['data-goto-next'] = $this->options->goNextSection ? "true" : "false"; |
| 31 |
} |
| 32 |
if( isset( $this->options->loop ) ){ |
| 33 |
$elementsAttrs['data-loop'] = $this->options->loop ? "true" : "false"; |
| 34 |
} else { |
| 35 |
$elementsAttrs['data-loop'] = "true"; |
| 36 |
} |
| 37 |
|
| 38 |
$args = Arr::merge( $this->getDefaultAttributes(), $elementsAttrs ); |
| 39 |
|
| 40 |
$div = Html::div( $args ); |
| 41 |
|
| 42 |
$video = "\n" . $this->getEmbedVideo( $videoUrl ) . "\n"; |
| 43 |
|
| 44 |
return $div->nest( $video ) . "\n"; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
protected function getEmbedVideo( $videoUrl ){ |
| 49 |
|
| 50 |
$videoType = !empty( $this->options->type ) ? $this->options->type : ''; |
| 51 |
|
| 52 |
if( $videoType == 'youtube' ){ |
| 53 |
return $this->getYouTubeEmbed( $videoUrl ); |
| 54 |
} elseif( $videoType == 'vimeo' ){ |
| 55 |
return $this->getVimeoEmbed( $videoUrl ); |
| 56 |
} |
| 57 |
|
| 58 |
return ''; |
| 59 |
} |
| 60 |
|
| 61 |
protected function getYouTubeEmbed( $videoUrl ){ |
| 62 |
$embed = ''; |
| 63 |
|
| 64 |
if( $embedUrl = Embed::getYouTubeVimeoEmbedUrl( $videoUrl ) ){ |
| 65 |
$queryParams = []; |
| 66 |
if( isset( $this->options->autoPlay ) ){ |
| 67 |
$queryParams['autoplay'] = (int) $this->options->autoPlay; |
| 68 |
} |
| 69 |
if( isset( $this->options->autoPause ) ){ |
| 70 |
$queryParams['autopause'] = (int) $this->options->autoPause; |
| 71 |
} |
| 72 |
if( isset( $this->options->controls ) ){ |
| 73 |
$queryParams['controls'] = (int) $this->options->controls; |
| 74 |
} |
| 75 |
if( isset( $this->options->mute ) ){ |
| 76 |
$queryParams['mute'] = (int) $this->options->mute; |
| 77 |
} |
| 78 |
if( isset( $this->options->related ) ){ |
| 79 |
$queryParams['rel'] = $this->options->related; |
| 80 |
} |
| 81 |
|
| 82 |
$iframeAttrs = [ |
| 83 |
'src' => add_query_arg( $queryParams, $embedUrl ), |
| 84 |
'frameborder' => '0', |
| 85 |
'allow' => 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture', |
| 86 |
'allowfullscreen' => '' |
| 87 |
]; |
| 88 |
|
| 89 |
$embed = Html::iframe( $iframeAttrs ); |
| 90 |
} |
| 91 |
|
| 92 |
return $embed; |
| 93 |
} |
| 94 |
|
| 95 |
protected function getVimeoEmbed( $videoUrl ){ |
| 96 |
$embed = ''; |
| 97 |
|
| 98 |
if( $embedUrl = Embed::getYouTubeVimeoEmbedUrl( $videoUrl ) ){ |
| 99 |
|
| 100 |
$queryParams = []; |
| 101 |
|
| 102 |
if( isset( $this->options->autoPlay ) ){ |
| 103 |
$queryParams['autoplay'] = (int) $this->options->autoPlay; |
| 104 |
} |
| 105 |
if( isset( $this->options->autoPause ) ){ |
| 106 |
$queryParams['autopause'] = (int) $this->options->autoPause; |
| 107 |
} |
| 108 |
if( isset( $this->options->controls ) ){ |
| 109 |
$queryParams['controls'] = (int) $this->options->controls; |
| 110 |
} |
| 111 |
if( isset( $this->options->mute ) ){ |
| 112 |
$queryParams['muted'] = (int) $this->options->mute; |
| 113 |
} |
| 114 |
if( isset( $this->options->related ) ){ |
| 115 |
$queryParams['byline'] = (int) $this->options->related; |
| 116 |
} |
| 117 |
|
| 118 |
$iframeAttrs = [ |
| 119 |
'src' => add_query_arg( $queryParams, $embedUrl ), |
| 120 |
'frameborder' => '0', |
| 121 |
'allowfullscreen' => '', |
| 122 |
'webkitallowfullscreen' => '', |
| 123 |
'mozallowfullscreen' => '' |
| 124 |
]; |
| 125 |
|
| 126 |
$embed = Html::iframe( $iframeAttrs ); |
| 127 |
} |
| 128 |
|
| 129 |
return $embed; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|