index.php
380 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use Kubio\AssetsDependencyInjector; |
| 6 | use Kubio\Core\Blocks\BlockBase; |
| 7 | use Kubio\Core\LodashBasic; |
| 8 | use Kubio\Core\Registry; |
| 9 | |
| 10 | class VideoBlock extends BlockBase { |
| 11 | |
| 12 | const OUTER = 'outer'; |
| 13 | const POSTER = 'poster'; |
| 14 | const VIDEO = 'video'; |
| 15 | const LIGHTBOX = 'lightbox'; |
| 16 | |
| 17 | private $displayAsVideo = false; |
| 18 | private $displayAsPosterImage = false; |
| 19 | private $displayAsIconWithLightbox = false; |
| 20 | |
| 21 | public function computed() { |
| 22 | $this->initInternalData(); |
| 23 | |
| 24 | return array( |
| 25 | 'displayAsVideo' => $this->displayAsVideo, |
| 26 | 'displayAsPoster' => $this->displayAsPosterImage, |
| 27 | 'displayAsIconWithLightbox' => $this->displayAsIconWithLightbox, |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | public function initInternalData() { |
| 32 | $displayAs = $this->getAttribute( 'displayAs', 'video' ); |
| 33 | $this->displayAsVideo = $displayAs === 'video'; |
| 34 | $this->displayAsPosterImage = $displayAs === 'posterImage'; |
| 35 | $this->displayAsIconWithLightbox = $displayAs === 'iconWithLightbox'; |
| 36 | } |
| 37 | |
| 38 | public function mapPropsToElements() { |
| 39 | $this->initInternalData(); |
| 40 | $params = $this->getVideoParameters(); |
| 41 | |
| 42 | $shortcodeContent = $this->getShortcode( $params ); |
| 43 | |
| 44 | $frontendAttributes = $this->getFrontendScriptAttributes(); |
| 45 | $url = $this->getAttribute('posterImage.url'); |
| 46 | $url = $this->getEscapedUrl($url); |
| 47 | |
| 48 | |
| 49 | return array( |
| 50 | self::VIDEO => array( |
| 51 | 'innerHTML' => $shortcodeContent, |
| 52 | ), |
| 53 | self::OUTER => array_merge( |
| 54 | array( |
| 55 | 'className' => $this->getOuterClasses(), |
| 56 | 'data-kubio-component' => 'video', |
| 57 | ), |
| 58 | $frontendAttributes |
| 59 | ), |
| 60 | self::POSTER => array_merge( |
| 61 | array( |
| 62 | 'style' => array( |
| 63 | 'background-image' => "url($url)", |
| 64 | ), |
| 65 | ), |
| 66 | $frontendAttributes |
| 67 | ), |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | public function getEscapedUrl($url) { |
| 72 | $url = esc_url($url); |
| 73 | |
| 74 | // Allow only http/https |
| 75 | if (! empty($url)) { |
| 76 | $parsed = wp_parse_url($url); |
| 77 | if (! isset($parsed['scheme']) || ! in_array($parsed['scheme'], ['http', 'https'], true)) { |
| 78 | $url = ''; |
| 79 | } |
| 80 | } else { |
| 81 | $url = ''; |
| 82 | } |
| 83 | |
| 84 | return $url; |
| 85 | } |
| 86 | public function getVideoParameters() { |
| 87 | $paramList = array( 'internalUrl', 'youtubeUrl', 'vimeoUrl', 'videoCategory', 'displayAs', 'playerOptions' ); |
| 88 | $params = array(); |
| 89 | |
| 90 | foreach ( $paramList as $paramName ) { |
| 91 | $params[ $paramName ] = $this->getAttribute( $paramName ); |
| 92 | if ( $paramName === 'internalUrl' && empty( $params[ $paramName ] ) ) { |
| 93 | $params[ $paramName ] = 'https://static-assets.kubiobuilder.com/defaults/kubio-intro-video.mp4'; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | $displayAs = LodashBasic::get( $params, 'displayAs' ); |
| 98 | $displayAsVideo = $displayAs === 'video'; |
| 99 | $displayAsPosterImage = $displayAs === 'posterImage'; |
| 100 | $displayAsLighbox = $displayAs === 'iconWithLightbox'; |
| 101 | $videoCategory = LodashBasic::get( $params, 'videoCategory' ); |
| 102 | $isInternal = $videoCategory === 'internal'; |
| 103 | |
| 104 | $params = array_merge( |
| 105 | $params, |
| 106 | array( |
| 107 | 'displayAsVideo' => $displayAsVideo, |
| 108 | 'displayAsPosterImage' => $displayAsPosterImage, |
| 109 | 'displayAsLightbox' => $displayAsLighbox, |
| 110 | 'isInternal' => $isInternal, |
| 111 | ) |
| 112 | ); |
| 113 | |
| 114 | $url = $this->generateUrl( $params ); |
| 115 | |
| 116 | $params['url'] = $url; |
| 117 | |
| 118 | return $params; |
| 119 | } |
| 120 | |
| 121 | public function generateUrl( $params ) { |
| 122 | $videoCategory = $params['videoCategory']; |
| 123 | switch ( $videoCategory ) { |
| 124 | case 'internal': |
| 125 | return $this->generateInternalUrl( $params ); |
| 126 | case 'youtube': |
| 127 | return $this->generateYoutubeUrl( $params ); |
| 128 | case 'vimeo': |
| 129 | return $this->generateVimeoUrl( $params ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public function generateInternalUrl( $params ) { |
| 134 | $internalUrl = LodashBasic::get( $params, 'internalUrl' ); |
| 135 | $playerOptions = LodashBasic::get( $params, 'playerOptions' ); |
| 136 | $startTime = LodashBasic::get( $playerOptions, 'startTime' ); |
| 137 | $endTime = LodashBasic::get( $playerOptions, 'endTime' ); |
| 138 | $startTime = $startTime ? $startTime : 0; |
| 139 | $endTime = $endTime ? $endTime : 0; |
| 140 | $time = ''; |
| 141 | if ( ( $startTime && $endTime ) || ( $startTime === 0 && $endTime ) ) { |
| 142 | $time = sprintf( '#t=%s,%s', $startTime, $endTime ); |
| 143 | } else { |
| 144 | if ( $startTime && ! $endTime ) { |
| 145 | $time = sprintf( '#t=%s', $startTime ); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return sprintf( '%s%s', $internalUrl, $time ); |
| 150 | } |
| 151 | |
| 152 | public function generateYoutubeUrl( $params ) { |
| 153 | $youtubeUrl = LodashBasic::get( $params, 'youtubeUrl' ); |
| 154 | $playerOptions = LodashBasic::get( $params, 'playerOptions' ); |
| 155 | $url = $youtubeUrl; |
| 156 | if ( ! $url ) { |
| 157 | return $url; |
| 158 | } |
| 159 | $youtubeRegex = '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i'; |
| 160 | preg_match( $youtubeRegex, $url, $urlMatch ); |
| 161 | if ( count( $urlMatch ) === 0 ) { |
| 162 | return $url; |
| 163 | } |
| 164 | $playList = $urlMatch[1]; |
| 165 | $url = sprintf( 'https://www.youtube.com/embed/%s?', $playList ); |
| 166 | $queryParams = array( |
| 167 | 'start' => LodashBasic::get( $playerOptions, 'startTime' ), |
| 168 | 'end' => LodashBasic::get( $playerOptions, 'endTime' ), |
| 169 | 'autoplay' => LodashBasic::get( $playerOptions, 'autoPlay' ) && ! $this->displayAsPosterImage, |
| 170 | 'mute' => LodashBasic::get( $playerOptions, 'mute' ), |
| 171 | 'loop' => LodashBasic::get( $playerOptions, 'loop' ), |
| 172 | 'controls' => LodashBasic::get( $playerOptions, 'playerControls' ), |
| 173 | 'modestBranding' => LodashBasic::get( $playerOptions, 'modestBranding' ), |
| 174 | 'rel' => LodashBasic::get( $playerOptions, 'suggestedVideo' ), |
| 175 | 'enablejsapi' => true, |
| 176 | ); |
| 177 | |
| 178 | $queryString = $this->convertExternalParamsToQueryString( $queryParams ); |
| 179 | |
| 180 | if ( $queryParams['loop'] ) { |
| 181 | $queryString[] = sprintf( 'playlist=%s', $playList ); |
| 182 | } |
| 183 | |
| 184 | if ( LodashBasic::get( $playerOptions, 'privacyMode' ) ) { |
| 185 | $url = str_replace( 'youtube', 'youtube-nocookie', $url ); |
| 186 | } |
| 187 | |
| 188 | $url .= implode( '&', $queryString ); |
| 189 | |
| 190 | return $url; |
| 191 | } |
| 192 | |
| 193 | protected function convertExternalParamsToQueryString( $queryParams ) { |
| 194 | $queryString = array(); |
| 195 | foreach ( $queryParams as $paramName => $paramValue ) { |
| 196 | if ( gettype( $paramValue ) === 'boolean' ) { |
| 197 | $paramValue = $paramValue ? 1 : 0; |
| 198 | } |
| 199 | if(!empty($paramValue)) { |
| 200 | $paramValue = urlencode( $paramValue ); |
| 201 | } |
| 202 | $queryString[] = sprintf( '%s=%s', $paramName, $paramValue ); |
| 203 | } |
| 204 | |
| 205 | return $queryString; |
| 206 | } |
| 207 | |
| 208 | private function colorToHex( $color ) { |
| 209 | |
| 210 | if ( empty( $color ) ) { |
| 211 | return ''; |
| 212 | } |
| 213 | |
| 214 | if ( strpos( $color, 'rgb' ) === 0 ) { |
| 215 | preg_match( '/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i', $color, $by_color ); |
| 216 | return sprintf( '#%02x%02x%02x', $by_color[1], $by_color[2], $by_color[3] ); |
| 217 | } else { |
| 218 | return $color; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | public function generateVimeoUrl( $params ) { |
| 223 | $vimeoUrl = LodashBasic::get( $params, 'vimeoUrl' ); |
| 224 | $playerOptions = LodashBasic::get( $params, 'playerOptions' ); |
| 225 | $url = $vimeoUrl; |
| 226 | if ( ! $url ) { |
| 227 | return $url; |
| 228 | } |
| 229 | $vimeoRegex = '/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^\/]*)\/videos\/|album\/(?:\d+)\/video\/|video\/|)(\d+)(?:[a-zA-Z0-9_\-]+)?/i'; |
| 230 | preg_match( $vimeoRegex, $url, $urlMatch ); |
| 231 | if ( count( $urlMatch ) === 0 ) { |
| 232 | return $url; |
| 233 | } |
| 234 | $playList = $urlMatch[1]; |
| 235 | $url = sprintf( 'https://player.vimeo.com/video/%s?', $playList ); |
| 236 | |
| 237 | $autoplay = LodashBasic::get( $playerOptions, 'autoPlay' ); |
| 238 | |
| 239 | $queryParams = array( |
| 240 | 'autoplay' => $autoplay && ! $this->displayAsPosterImage, |
| 241 | 'autopause' => $autoplay, |
| 242 | 'muted' => LodashBasic::get( $playerOptions, 'mute' ), |
| 243 | 'loop' => LodashBasic::get( $playerOptions, 'loop' ), |
| 244 | 'title' => LodashBasic::get( $playerOptions, 'introTitle' ), |
| 245 | 'portrait' => LodashBasic::get( $playerOptions, 'introPortrait' ), |
| 246 | 'byline' => LodashBasic::get( $playerOptions, 'introByLine' ), |
| 247 | 'color' => str_replace( '#', '', $this->colorToHex( LodashBasic::get( $playerOptions, 'controlsColor' ) ) ), |
| 248 | 'api' => true, |
| 249 | ); |
| 250 | |
| 251 | $queryString = $this->convertExternalParamsToQueryString( $queryParams ); |
| 252 | |
| 253 | $url .= implode( '&', $queryString ); |
| 254 | |
| 255 | $startTime = LodashBasic::get( $playerOptions, 'startTime' ); |
| 256 | if ( $startTime ) { |
| 257 | $url .= sprintf( '#t=%s', $this->getVimeoStartTime( $startTime ) ); |
| 258 | } |
| 259 | |
| 260 | return $url; |
| 261 | } |
| 262 | |
| 263 | protected function getVimeoStartTime( $startTime ) { |
| 264 | $time = gmdate( 'H\ms\s', $startTime ); |
| 265 | |
| 266 | return $time; |
| 267 | } |
| 268 | |
| 269 | public function getShortcode( $params ) { |
| 270 | $url = LodashBasic::get( $params, 'url' ); |
| 271 | $isInternal = LodashBasic::get( $params, 'isInternal' ); |
| 272 | $playerOptions = LodashBasic::get( $params, 'playerOptions' ); |
| 273 | $internalUrlAttributes = $this->getInternalUrlAttributes( $params ); |
| 274 | |
| 275 | $shortcodeAttributes = array( |
| 276 | 'url' => $url, |
| 277 | 'type' => $isInternal ? 'internal' : 'external', |
| 278 | 'autoplay' => LodashBasic::get( $playerOptions, 'autoPlay' ) ? 1 : 0, |
| 279 | 'attributes' => $internalUrlAttributes, |
| 280 | ); |
| 281 | |
| 282 | return $this->kubio_video_shortcode( $shortcodeAttributes ); |
| 283 | } |
| 284 | |
| 285 | public function getInternalUrlAttributes( $params ) { |
| 286 | $playerOptions = $params['playerOptions']; |
| 287 | |
| 288 | $attributesValues = array( |
| 289 | 'autoplay' => isset( $playerOptions['autoPlay'] ) && $playerOptions['autoPlay'] && ! $this->displayAsPosterImage, |
| 290 | 'muted' => esc_attr( $playerOptions['mute'] ), |
| 291 | 'loop' => esc_attr( $playerOptions['loop'] ), |
| 292 | 'controls' => esc_attr( $playerOptions['playerControls'] ), |
| 293 | ); |
| 294 | $atts = array(); |
| 295 | foreach ( $attributesValues as $attributeName => $attributeValue ) { |
| 296 | if ( $attributeValue ) { |
| 297 | $atts[] = $attributeName; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return implode( ' ', $atts ); |
| 302 | } |
| 303 | |
| 304 | public function getFrontendScriptAttributes() { |
| 305 | $useLightbox = $this->getAttribute( 'posterImage.lightbox' ) && $this->displayAsPosterImage || $this->displayAsIconWithLightbox; |
| 306 | $autoPlay = $this->getAttribute( 'playerOptions.autoPlay' ) && $this->displayAsVideo; |
| 307 | |
| 308 | if ( $useLightbox ) { |
| 309 | AssetsDependencyInjector::injectKubioFrontendStyleDependencies( 'fancybox' ); |
| 310 | AssetsDependencyInjector::injectKubioScriptDependencies( 'fancybox' ); |
| 311 | } |
| 312 | |
| 313 | return array( |
| 314 | 'data-display-as' => $this->getAttribute( 'displayAs' ), |
| 315 | 'data-light-box' => $useLightbox ? '1' : '0', |
| 316 | 'data-video-category' => $this->getAttribute( 'videoCategory' ), |
| 317 | 'data-autoplay' => $autoPlay ? '1' : '0', |
| 318 | 'data-start-time' => $this->getAttribute( 'playerOptions.startTime' ), |
| 319 | 'data-end-time' => $this->getAttribute( 'playerOptions.endTime' ), |
| 320 | ); |
| 321 | } |
| 322 | |
| 323 | public function getOuterClasses() { |
| 324 | $classes = array(); |
| 325 | if ( ! $this->displayAsIconWithLightbox ) { |
| 326 | $aspectRatio = $this->getAttribute( 'aspectRatio', '16-9' ); |
| 327 | $classes[] = sprintf( 'h-aspect-ratio--%s', $aspectRatio ); |
| 328 | } |
| 329 | |
| 330 | return implode( ' ', $classes ); |
| 331 | } |
| 332 | |
| 333 | function kubio_video_shortcode( $atts ) { |
| 334 | $atts = array_merge( |
| 335 | array( |
| 336 | 'url' => '', |
| 337 | 'autoplay' => '0', |
| 338 | 'type' => 'internal', |
| 339 | 'attributes' => '', |
| 340 | ), |
| 341 | $atts |
| 342 | ); |
| 343 | |
| 344 | if ( $atts['type'] === 'external' ) { |
| 345 | $content = $this->doIframe( $atts['url'], $atts['autoplay'] ); |
| 346 | } else { |
| 347 | $content = $this->doVideo( $atts['url'], $atts['attributes'] ); |
| 348 | } |
| 349 | return $content; |
| 350 | } |
| 351 | |
| 352 | function doIframe( $url, $autoplay ) { |
| 353 | $autoplay = "{$autoplay}" === '0' ? 'allow="autoplay"' : ''; |
| 354 | return sprintf( |
| 355 | '<iframe src="%s" class="h-video-main" allowfullscreen %s ></iframe>', |
| 356 | esc_url( $url ), |
| 357 | $autoplay |
| 358 | ); |
| 359 | } |
| 360 | |
| 361 | function doVideo( $url, $attributes ) { |
| 362 | $poster_url = $this->getAttribute( 'posterImage.url' ); |
| 363 | $poster_url = $this->getEscapedUrl($poster_url); |
| 364 | if ( !empty($poster_url) ) { |
| 365 | $attributes .= ' poster="' . esc_url( $poster_url ) . '"'; |
| 366 | } |
| 367 | |
| 368 | return sprintf( |
| 369 | '<video class="h-video-main" playsinline poster="%s" %s>' . |
| 370 | ' <source src="%s" type="video/mp4" />' . |
| 371 | '</video>', |
| 372 | esc_attr($poster_url), |
| 373 | esc_attr( $attributes ), |
| 374 | esc_url( $url ) |
| 375 | ); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | Registry::registerBlock( __DIR__, VideoBlock::class ); |
| 380 |