Background.php
1 year ago
BackgroundDefaults.php
2 years ago
BackgroundImage.php
1 year ago
BackgroundOverlay.php
1 year ago
BackgroundSlideshow.php
1 year ago
BackgroundVideo.php
1 year ago
BackgroundVideo.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\Background; |
| 4 | |
| 5 | use Kubio\Core\Element; |
| 6 | use Kubio\Core\ElementBase; |
| 7 | use Kubio\Core\Utils; |
| 8 | use function floatval; |
| 9 | |
| 10 | class BackgroundVideo extends ElementBase { |
| 11 | const YOUTUBE_MIME = 'video/x-youtube'; |
| 12 | function __construct( $value ) { |
| 13 | parent::__construct( $value, BackgroundDefaults::getDefaultVideo() ); |
| 14 | } |
| 15 | |
| 16 | function wrapperComputedStyle() { |
| 17 | $url = $this->get( 'poster.url' ); |
| 18 | $url = kubio_wpml_get_translated_media_url( $url ); |
| 19 | $style = array( |
| 20 | 'backgroundImage' => "url(\"$url\")", |
| 21 | ); |
| 22 | return $style; |
| 23 | } |
| 24 | |
| 25 | function __toString() { |
| 26 | $poster = $this->get( 'poster.url' ); |
| 27 | $videoType = $this->get( 'type' ); |
| 28 | $position = $this->get( 'position' ); |
| 29 | $internalVideoMime = $this->get( 'internal.mimeType' ); |
| 30 | |
| 31 | // fallback on the 'mime' path |
| 32 | if ( empty( $internalVideoMimeType ) ) { |
| 33 | $internalVideoMime = $this->get( 'internal.mime' ); |
| 34 | } |
| 35 | |
| 36 | $url = $this->get( "{$videoType}.url" ); |
| 37 | $url = kubio_wpml_get_translated_media_url( $url ); |
| 38 | $positionX = floatval( $position['x'] ); |
| 39 | $positionY = floatval( $position['y'] ); |
| 40 | |
| 41 | $mimeType = $videoType === 'internal' ? $internalVideoMime : self::YOUTUBE_MIME; |
| 42 | |
| 43 | $id = 'background-video'; |
| 44 | |
| 45 | $scriptData = Utils::useJSComponentProps( |
| 46 | 'video-background', |
| 47 | array( |
| 48 | 'positionX' => $positionX, |
| 49 | 'positionY' => $positionY, |
| 50 | 'mimeType' => $mimeType, |
| 51 | 'poster' => $poster, |
| 52 | 'video' => $url, |
| 53 | ) |
| 54 | ); |
| 55 | |
| 56 | $props = array_merge( |
| 57 | array( |
| 58 | 'id' => $id, |
| 59 | 'style' => $this->wrapperComputedStyle(), |
| 60 | 'className' => array( |
| 61 | 'cp-video-bg', |
| 62 | 'background-layer', |
| 63 | 'kubio-video-background', |
| 64 | ), |
| 65 | ), |
| 66 | $scriptData |
| 67 | ); |
| 68 | |
| 69 | return new Element( Element::DIV, $props ) . ''; |
| 70 | } |
| 71 | } |
| 72 |