Block.php
1 month ago
BlockFinder.php
1 year ago
DynamicData.php
3 weeks ago
HasOneRelationship.php
1 year ago
Integration.php
1 year ago
Utility.php
3 weeks ago
Block.php
747 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Block support for Presto Player. |
| 4 | * |
| 5 | * @package PrestoPlayer |
| 6 | */ |
| 7 | |
| 8 | namespace PrestoPlayer\Support; |
| 9 | |
| 10 | use PrestoPlayer\Models\Video; |
| 11 | use PrestoPlayer\Models\Player; |
| 12 | use PrestoPlayer\Models\Preset; |
| 13 | use PrestoPlayer\Models\AudioPreset; |
| 14 | use PrestoPlayer\Models\Setting; |
| 15 | use PrestoPlayer\Support\DynamicData; |
| 16 | use PrestoPlayer\Integrations\LearnDash\LearnDash; |
| 17 | use PrestoPlayer\Services\PreloadService; |
| 18 | |
| 19 | /** |
| 20 | * Base block class |
| 21 | */ |
| 22 | class Block { |
| 23 | |
| 24 | /** |
| 25 | * The block name (slug) |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $name = ''; |
| 30 | |
| 31 | /** |
| 32 | * The template name |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $template_name = 'video'; |
| 37 | |
| 38 | /** |
| 39 | * Attributes |
| 40 | * |
| 41 | * @var array |
| 42 | */ |
| 43 | protected $attributes = array( |
| 44 | 'color' => array( |
| 45 | 'type' => 'string', |
| 46 | 'default' => '#00b3ff', |
| 47 | ), |
| 48 | 'blockAlignment' => array( |
| 49 | 'type' => 'string', |
| 50 | ), |
| 51 | 'autoplay' => array( |
| 52 | 'type' => 'boolean', |
| 53 | ), |
| 54 | 'id' => array( |
| 55 | 'type' => 'number', |
| 56 | ), |
| 57 | 'src' => array( |
| 58 | 'type' => 'string', |
| 59 | ), |
| 60 | 'imageID' => array( |
| 61 | 'type' => 'number', |
| 62 | ), |
| 63 | 'poster' => array( |
| 64 | 'type' => 'string', |
| 65 | ), |
| 66 | 'content' => array( |
| 67 | 'type' => 'boolean', |
| 68 | ), |
| 69 | 'pip' => array( |
| 70 | 'type' => 'boolean', |
| 71 | 'default' => true, |
| 72 | ), |
| 73 | 'fullscreen' => array( |
| 74 | 'type' => 'boolean', |
| 75 | 'default' => true, |
| 76 | ), |
| 77 | 'captions' => array( |
| 78 | 'type' => 'boolean', |
| 79 | 'default' => false, |
| 80 | ), |
| 81 | 'hideControls' => array( |
| 82 | 'type' => 'boolean', |
| 83 | 'default' => true, |
| 84 | ), |
| 85 | 'playLarge' => array( |
| 86 | 'type' => 'boolean', |
| 87 | 'default' => true, |
| 88 | ), |
| 89 | 'chapters' => array( |
| 90 | 'type' => 'array', |
| 91 | 'default' => array(), |
| 92 | ), |
| 93 | 'overlays' => array( |
| 94 | 'type' => 'array', |
| 95 | 'default' => array(), |
| 96 | ), |
| 97 | 'speed' => array( |
| 98 | 'type' => 'boolean', |
| 99 | 'default' => true, |
| 100 | ), |
| 101 | ); |
| 102 | |
| 103 | /** |
| 104 | * Default attributes for the block. |
| 105 | * |
| 106 | * @var array |
| 107 | */ |
| 108 | protected $default_attributes = array( |
| 109 | 'playsInline' => true, |
| 110 | ); |
| 111 | |
| 112 | /** |
| 113 | * Constructor. |
| 114 | * |
| 115 | * @param bool $is_premium Whether the plugin is premium. |
| 116 | * @param int $version Plugin version. |
| 117 | */ |
| 118 | public function __construct( bool $is_premium = false, $version = 1 ) { |
| 119 | do_action( 'presto_player_before_block_output', array( $this, 'middleware' ) ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Register the block type |
| 124 | * |
| 125 | * @return void |
| 126 | */ |
| 127 | public function register() { |
| 128 | add_action( 'init', array( $this, 'registerBlockType' ) ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get additional attributes for the block. |
| 133 | * |
| 134 | * @return array |
| 135 | */ |
| 136 | public function additionalAttributes() { |
| 137 | return array(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the block title from block.json |
| 142 | * |
| 143 | * @return string |
| 144 | */ |
| 145 | public function getBlockTitle() { |
| 146 | // Try to get the title from block.json metadata. |
| 147 | $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( "presto-player/{$this->name}" ); |
| 148 | if ( $block_type && ! empty( $block_type->title ) ) { |
| 149 | return $block_type->title; |
| 150 | } |
| 151 | return ''; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Register dynamic block type. |
| 156 | * |
| 157 | * @return void |
| 158 | */ |
| 159 | public function registerBlockType() { |
| 160 | register_block_type( |
| 161 | "presto-player/$this->name", |
| 162 | array( |
| 163 | 'attributes' => wp_parse_args( $this->additionalAttributes(), $this->attributes ), |
| 164 | 'render_callback' => array( $this, 'html' ), |
| 165 | ) |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Middleware to run before outputting template. |
| 171 | * |
| 172 | * @param array $attributes Block attributes. |
| 173 | * @param string $content Block content. |
| 174 | * @return boolean Whether the block should load. |
| 175 | */ |
| 176 | public function middleware( $attributes, $content ) { |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Sanitize attributes function. |
| 182 | * |
| 183 | * @param array $attributes Block attributes. |
| 184 | * @param array $default_config Default configuration. |
| 185 | * @return array Sanitized attributes. |
| 186 | */ |
| 187 | public function sanitizeAttributes( $attributes, $default_config ) { |
| 188 | return array(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Allow overriding attributes. |
| 193 | * |
| 194 | * @param array $attributes Block attributes. |
| 195 | * @return array |
| 196 | */ |
| 197 | public function overrideAttributes( $attributes ) { |
| 198 | return apply_filters( 'presto_video_block_attributes_override', $attributes, $this ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Must sanitize attributes. |
| 203 | * |
| 204 | * @param array $attributes Block attributes. |
| 205 | * @return array Sanitized attributes. |
| 206 | */ |
| 207 | private function _sanitizeAttibutes( $attributes ) { |
| 208 | |
| 209 | // attribute overrides. |
| 210 | $attributes = $this->overrideAttributes( $attributes ); |
| 211 | |
| 212 | // Apply default attributes if not set. |
| 213 | $attributes = $this->applyAttributeDefaults( $attributes ); |
| 214 | |
| 215 | // video id. |
| 216 | $id = ! empty( $attributes['id'] ) ? $attributes['id'] : 0; |
| 217 | |
| 218 | if ( 'audio' === $this->name ) { |
| 219 | $preset = $this->getAudioPreset( ! empty( $attributes['preset'] ) ? $attributes['preset'] : 0 ); |
| 220 | $preset->type = 'audio'; |
| 221 | } else { |
| 222 | $preset = $this->getPreset( ! empty( $attributes['preset'] ) ? $attributes['preset'] : 0, $attributes ); |
| 223 | } |
| 224 | |
| 225 | // Apply server-side overrides that depend on block attributes. |
| 226 | $branding = $this->getBranding( $preset ); |
| 227 | $class = $this->getClasses( $attributes ); |
| 228 | $player_class = $this->getPlayerClasses( $id, $preset, $attributes ); |
| 229 | $styles = $this->getPlayerStyles( $preset, $branding, $attributes ); |
| 230 | $css = $this->getCSS( $id ); |
| 231 | $src = ! empty( $attributes['src'] ) ? $attributes['src'] : ''; |
| 232 | |
| 233 | // use title or source. |
| 234 | if ( empty( $attributes['title'] ) ) { |
| 235 | $video = $id ? ( new Video( $id ) ) : false; |
| 236 | $attributes['title'] = $video ? $video->title : $src; |
| 237 | } |
| 238 | |
| 239 | // Strip unsafe schemes (javascript:, data:, vbscript:) from overlay link URLs before they reach the player. |
| 240 | $overlays = ! empty( $attributes['overlays'] ) ? (array) $attributes['overlays'] : array(); |
| 241 | foreach ( $overlays as $k => $overlay ) { |
| 242 | if ( ! empty( $overlay['link']['url'] ) ) { |
| 243 | $overlays[ $k ]['link']['url'] = esc_url_raw( $overlay['link']['url'] ); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Default config. |
| 248 | $default_config = apply_filters( |
| 249 | 'presto_player/block/default_attributes', |
| 250 | array( |
| 251 | 'type' => $this->name, |
| 252 | 'name' => $this->getBlockTitle(), |
| 253 | 'css' => wp_kses_post( $css ), |
| 254 | 'class' => $class, |
| 255 | 'is_hls' => $this->isHls( $src ), |
| 256 | 'styles' => $styles, |
| 257 | 'skin' => $preset->skin, |
| 258 | 'playerClass' => $player_class, |
| 259 | 'id' => $id, |
| 260 | 'src' => $src, |
| 261 | 'autoplay' => ! empty( $attributes['autoplay'] ), |
| 262 | 'playsInline' => ! empty( $attributes['playsInline'] ), |
| 263 | 'poster' => ! empty( $attributes['poster'] ) ? $attributes['poster'] : '', |
| 264 | 'branding' => $branding, |
| 265 | 'youtube' => array( |
| 266 | 'noCookie' => (bool) Setting::get( 'youtube', 'nocookie' ), |
| 267 | 'channelId' => sanitize_text_field( Setting::get( 'youtube', 'channel_id' ) ), |
| 268 | 'show_count' => ! empty( $preset->action_bar['show_count'] ), |
| 269 | ), |
| 270 | 'preload' => ! empty( $attributes['preload'] ) ? $attributes['preload'] : '', |
| 271 | 'tracks' => ! empty( $attributes['tracks'] ) ? (array) $attributes['tracks'] : array(), |
| 272 | 'preset' => $preset ? $preset->toArray() : array(), |
| 273 | 'chapters' => ! empty( $attributes['chapters'] ) ? $attributes['chapters'] : array(), |
| 274 | 'overlays' => DynamicData::replaceItems( $overlays, 'text' ), |
| 275 | 'blockAttributes' => $attributes, |
| 276 | 'videoAttributes' => array(), |
| 277 | 'provider' => $this->name, |
| 278 | 'analytics' => Setting::get( 'analytics', 'enable', false ), |
| 279 | 'automations' => Setting::get( 'performance', 'automations', true ), |
| 280 | 'title' => ! empty( $attributes['title'] ) ? html_entity_decode( $attributes['title'] ) : '', |
| 281 | ), |
| 282 | $attributes |
| 283 | ); |
| 284 | |
| 285 | return wp_parse_args( |
| 286 | $this->sanitizeAttributes( $attributes, $default_config ), |
| 287 | $default_config |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get CSS from settings. |
| 293 | * Is it an HLS playlist. |
| 294 | * |
| 295 | * @param string $src src parameter. |
| 296 | * @return boolean |
| 297 | */ |
| 298 | public function isHls( $src ) { |
| 299 | $src = ! empty( $src ) ? $src : ''; |
| 300 | return \strpos( $src, '.m3u8' ) !== false; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Get CSS from settings. |
| 305 | * Validates before output. |
| 306 | * |
| 307 | * @param integer $id the video id. |
| 308 | * @return string |
| 309 | */ |
| 310 | public function getCSS( $id ) { |
| 311 | return apply_filters( |
| 312 | 'presto_player/player/css', |
| 313 | Utility::sanitizeCSS( |
| 314 | Setting::get( 'branding', 'player_css' ), |
| 315 | $id |
| 316 | ) |
| 317 | ); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Gets the preset. |
| 322 | * |
| 323 | * @param integer $id Preset ID. |
| 324 | * @param array $attributes Block attributes. |
| 325 | * @return \PrestoPlayer\Models\Preset |
| 326 | */ |
| 327 | public function getPreset( $id, $attributes = array() ) { |
| 328 | $preset = new Preset( ! empty( $id ) ? $id : 0 ); |
| 329 | $preset_id = $preset->id; |
| 330 | |
| 331 | if ( empty( $preset_id ) ) { |
| 332 | $preset = $preset->findWhere( array( 'slug' => 'default' ) ); |
| 333 | } |
| 334 | |
| 335 | // replace watermark text. |
| 336 | if ( ! empty( $preset->watermark['enabled'] ) ) { |
| 337 | $watermark_text = array( |
| 338 | 'text' => DynamicData::replaceText( $preset->watermark['text'] ), |
| 339 | ); |
| 340 | |
| 341 | $preset->watermark = wp_parse_args( $watermark_text, $preset->watermark ); |
| 342 | } |
| 343 | |
| 344 | // If lazy load is enabled, disable it if muted preview or autoplay is enabled. |
| 345 | if ( $preset->lazy_load_youtube ) { |
| 346 | $has_muted_preview = isset( $attributes['mutedPreview']['enabled'] ) && ! empty( $attributes['mutedPreview']['enabled'] ); |
| 347 | $has_autoplay = ! empty( $attributes['autoplay'] ); |
| 348 | $preset->lazy_load_youtube = ! ( $has_muted_preview || $has_autoplay ); |
| 349 | } |
| 350 | |
| 351 | return apply_filters( 'presto_player/presto_player_presets/data', $preset, 'video' ); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Gets the audio preset. |
| 356 | * |
| 357 | * @param integer $id Preset ID. |
| 358 | * @return \PrestoPlayer\Models\AudioPreset |
| 359 | */ |
| 360 | public function getAudioPreset( $id ) { |
| 361 | $preset = new AudioPreset( ! empty( $id ) ? $id : 0 ); |
| 362 | $preset_id = $preset->id; |
| 363 | |
| 364 | if ( empty( $preset_id ) ) { |
| 365 | $preset = $preset->findWhere( array( 'slug' => 'default' ) ); |
| 366 | } |
| 367 | |
| 368 | return apply_filters( 'presto_player/presto_player_presets/data', $preset, 'audio' ); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Get player branding. |
| 373 | * |
| 374 | * @param \PrestoPlayer\Models\Preset $preset the preset. |
| 375 | * @return array |
| 376 | */ |
| 377 | public function getBranding( $preset ) { |
| 378 | $branding = Player::getBranding(); |
| 379 | |
| 380 | // sanitize with sensible defaults. |
| 381 | $branding['color'] = ! empty( $branding['color'] ) ? sanitize_hex_color( $branding['color'] ) : 'rgba(43,51,63,.7)'; |
| 382 | $branding['logo_width'] = ! empty( $branding['logo_width'] ) ? $branding['logo_width'] : 150; |
| 383 | if ( isset( $branding['logo'] ) ) { |
| 384 | $branding['logo'] = ! empty( $branding['logo'] && ! $preset->hide_logo ) ? $branding['logo'] : ''; |
| 385 | } |
| 386 | |
| 387 | return $branding; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Get block classes. |
| 392 | * |
| 393 | * @param array $attributes the block attributes. |
| 394 | * @return string |
| 395 | */ |
| 396 | public function getClasses( $attributes ) { |
| 397 | $block_alignment = isset( $attributes['align'] ) ? sanitize_text_field( $attributes['align'] ) : ''; |
| 398 | return ! empty( $block_alignment ) ? 'align' . $block_alignment : ''; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Get player classes. |
| 403 | * |
| 404 | * @param integer $id the video id. |
| 405 | * @param \PrestoPlayer\Models\Preset $preset the preset. |
| 406 | * @param array $attributes the block attributes. |
| 407 | * @return string |
| 408 | */ |
| 409 | public function getPlayerClasses( $id, $preset, $attributes ) { |
| 410 | $skin = $preset->skin; |
| 411 | $player_class = 'presto-video-id-' . (int) $id; |
| 412 | $player_class .= ' presto-preset-id-' . (int) $preset->id; |
| 413 | |
| 414 | if ( ! empty( $skin ) ) { |
| 415 | $player_class .= ' skin-' . sanitize_text_field( $skin ); |
| 416 | } |
| 417 | |
| 418 | $caption_style = $preset->caption_style; |
| 419 | if ( ! empty( $caption_style ) ) { |
| 420 | $player_class .= ' caption-style-' . sanitize_html_class( $caption_style ); |
| 421 | } |
| 422 | |
| 423 | if ( ! empty( $attributes['className'] ) ) { |
| 424 | $player_class .= ' ' . (string) $attributes['className']; |
| 425 | } |
| 426 | |
| 427 | return $player_class; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Get player styles. |
| 432 | * |
| 433 | * @param \PrestoPlayer\Models\Preset $preset the preset. |
| 434 | * @param array $branding the branding object. |
| 435 | * @param array $attributes the block attributes. |
| 436 | * @return string |
| 437 | */ |
| 438 | public function getPlayerStyles( $preset, $branding, $attributes = array() ) { |
| 439 | |
| 440 | // Set brand color. |
| 441 | $background_color = ( ! empty( $preset->background_color ) ? sanitize_hex_color( $preset->background_color ) : 'var(--presto-player-highlight-color, ' . sanitize_hex_color( $branding['color'] ) . ')' ); |
| 442 | $styles = '--plyr-color-main: ' . $background_color . '; '; |
| 443 | |
| 444 | // video. |
| 445 | if ( $preset->caption_background ) { |
| 446 | $styles .= '--plyr-captions-background: ' . sanitize_hex_color( $preset->caption_background ) . '; '; |
| 447 | } |
| 448 | if ( $preset->border_radius ) { |
| 449 | $styles .= '--presto-player-border-radius: ' . (int) $preset->border_radius . 'px; '; |
| 450 | } |
| 451 | |
| 452 | if ( $branding['logo_width'] ) { |
| 453 | $styles .= '--presto-player-logo-width: ' . (int) $branding['logo_width'] . 'px; '; |
| 454 | } |
| 455 | if ( ! empty( $preset->email_collection['border_radius'] ) ) { |
| 456 | $styles .= '--presto-player-email-border-radius: ' . (int) $preset->email_collection['border_radius'] . 'px; '; |
| 457 | } |
| 458 | |
| 459 | // audio. |
| 460 | if ( 'audio' === $preset->type ) { |
| 461 | if ( $preset->background_color ) { |
| 462 | $styles .= '--plyr-audio-controls-background: ' . sanitize_hex_color( $preset->background_color ) . ';'; |
| 463 | } else { |
| 464 | $styles .= '--plyr-audio-controls-background: ' . sanitize_hex_color( $branding['color'] ) . ';'; |
| 465 | } |
| 466 | |
| 467 | if ( $preset->control_color ) { |
| 468 | $styles .= '--plyr-audio-control-color: ' . sanitize_hex_color( $preset->control_color ) . ';'; |
| 469 | $styles .= '--plyr-range-thumb-background: ' . sanitize_hex_color( $preset->control_color ) . ';'; |
| 470 | $styles .= '--plyr-range-fill-background: ' . sanitize_hex_color( $preset->control_color ) . ';'; |
| 471 | $styles .= '--plyr-audio-progress-buffered-background: ' . Utility::hex2rgba( sanitize_hex_color( $preset->control_color ), 0.35 ) . ';'; |
| 472 | $styles .= '--plyr-range-thumb-shadow: 0 1px 1px ' . Utility::hex2rgba( sanitize_hex_color( $preset->control_color ), 0.15 ) . ', 0 0 0 1px ' . Utility::hex2rgba( sanitize_hex_color( $preset->control_color ), 0.2 ) . ';'; |
| 473 | } else { |
| 474 | $styles .= '--plyr-audio-control-color: #ffffff;'; |
| 475 | $styles .= '--plyr-range-thumb-background: #ffffff;'; |
| 476 | $styles .= '--plyr-range-fill-background: #ffffff;'; |
| 477 | $styles .= '--plyr-audio-progress-buffered-background: ' . Utility::hex2rgba( sanitize_hex_color( sanitize_hex_color( '#dcdcdc' ) ), 0.35 ) . ';'; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // Set aspect ratio css variable. |
| 482 | if ( ! empty( $attributes['ratio'] ) ) { |
| 483 | $styles .= '--presto-player-aspect-ratio: ' . str_replace( ':', '/', esc_attr( $attributes['ratio'] ) ) . ';'; |
| 484 | } |
| 485 | |
| 486 | return $styles; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Get block attributes. |
| 491 | * |
| 492 | * @param array $attributes the block attributes. |
| 493 | * @return array |
| 494 | */ |
| 495 | public function getAttributes( $attributes ) { |
| 496 | return $this->_sanitizeAttibutes( $attributes ); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Dynamic block output. |
| 501 | * |
| 502 | * @param array $attributes the block attributes. |
| 503 | * @param string $content the post content. |
| 504 | * @return string |
| 505 | */ |
| 506 | public function html( $attributes, $content ) { |
| 507 | global $presto_player_instance; |
| 508 | if ( null === $presto_player_instance ) { |
| 509 | $presto_player_instance = 0; |
| 510 | } |
| 511 | ++$presto_player_instance; |
| 512 | |
| 513 | // html middleware. |
| 514 | $load = $this->middleware( $attributes, $content ); |
| 515 | |
| 516 | if ( is_feed() ) { |
| 517 | return $this->getFeedHtml( $attributes ); |
| 518 | } |
| 519 | |
| 520 | if ( LearnDash::isEnabled() ) { |
| 521 | if ( ! LearnDash::shouldVideoLoad() ) { |
| 522 | return false; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // let integrations filter loading capabilities. |
| 527 | if ( ! apply_filters( 'presto_player_load_video', $load, $attributes, $content, $this->name ) ) { |
| 528 | // allow a custom fallback. |
| 529 | $fallback = apply_filters( 'presto_player_load_video_fallback', false, $attributes, $content, $this ); |
| 530 | if ( $fallback ) { |
| 531 | return wp_kses_post( $fallback ); |
| 532 | } |
| 533 | return $this->getFallbackHTMLForUnauthorizeAccess(); |
| 534 | } |
| 535 | |
| 536 | // get template data. |
| 537 | $data = apply_filters( 'presto_player_block_data', $this->getAttributes( $attributes ), $this ); |
| 538 | |
| 539 | // need and id and src. |
| 540 | if ( empty( $data['id'] ) && empty( $data['src'] ) ) { |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | // Preload component resources. |
| 545 | $preload_service = new PreloadService(); |
| 546 | $preload_service->add( array( 'presto-player' ) ); |
| 547 | switch ( $this->name ) { |
| 548 | case 'bunny': |
| 549 | $preload_service->add( array( 'presto-bunny' ) ); |
| 550 | break; |
| 551 | case 'youtube': |
| 552 | $preload_service->add( array( 'presto-youtube' ) ); |
| 553 | break; |
| 554 | case 'self-hosted': |
| 555 | $preload_service->add( array( 'presto-video' ) ); |
| 556 | break; |
| 557 | case 'vimeo': |
| 558 | $preload_service->add( array( 'presto-vimeo' ) ); |
| 559 | break; |
| 560 | case 'audio': |
| 561 | $preload_service->add( array( 'presto-audio' ) ); |
| 562 | break; |
| 563 | default: |
| 564 | break; |
| 565 | } |
| 566 | $preload_service->bootstrap(); |
| 567 | |
| 568 | // TODO: child template system. |
| 569 | ob_start(); |
| 570 | |
| 571 | if ( ! empty( $data['id'] ) ) { |
| 572 | echo '<!--presto-player:video_id=' . (int) $data['id'] . '-->'; |
| 573 | } |
| 574 | |
| 575 | if ( file_exists( PRESTO_PLAYER_PLUGIN_DIR . "templates/{$this->template_name}.php" ) ) { |
| 576 | include PRESTO_PLAYER_PLUGIN_DIR . "templates/{$this->template_name}.php"; |
| 577 | } |
| 578 | |
| 579 | $this->iframeFallback( $data ); |
| 580 | |
| 581 | // output schema markup for optimized seo. |
| 582 | $this->outputVideoSchemaMarkup( $this->getSchema( $data ) ); |
| 583 | |
| 584 | $template = ob_get_contents(); |
| 585 | ob_end_clean(); |
| 586 | |
| 587 | return $template; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Get json data for video schema. |
| 592 | * https://developers.google.com/search/docs/appearance/structured-data/video#video-object. |
| 593 | * |
| 594 | * @param array $data the block data. |
| 595 | * |
| 596 | * @return array|bool |
| 597 | */ |
| 598 | public function getSchema( $data ) { |
| 599 | |
| 600 | if ( ! apply_filters( 'presto_player_video_schema_enabled', true, $data ) ) { |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | if ( isset( $data ) && empty( $data['id'] ) ) { |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | if ( 'audio' === $data['type'] ) { |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | $visibility = $data['blockAttributes']['visibility'] ?? false; |
| 613 | if ( $visibility && 'private' === $visibility ) { |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | $title = $data['title'] ?? get_the_title(); |
| 618 | if ( empty( $title ) ) { |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | $poster = $data['poster'] ?? ''; |
| 623 | if ( empty( $poster ) ) { |
| 624 | return false; |
| 625 | } |
| 626 | |
| 627 | $video = new Video( (int) $data['id'] ); |
| 628 | |
| 629 | return apply_filters( |
| 630 | 'presto_player_video_schema', |
| 631 | array( |
| 632 | // required. |
| 633 | '@context' => 'https://schema.org', |
| 634 | '@type' => 'VideoObject', |
| 635 | 'name' => wp_kses_post( $title ), |
| 636 | 'thumbnailUrl' => esc_url( $poster ), |
| 637 | 'uploadDate' => wp_date( 'c', strtotime( $video->getCreatedAt() ) ), |
| 638 | // recommended. |
| 639 | 'contentUrl' => esc_url( $data['src'] ?? '' ), |
| 640 | ), |
| 641 | $data |
| 642 | ); |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Output video schema markup. |
| 647 | * |
| 648 | * @param array $data the block data. |
| 649 | * |
| 650 | * @return void|bool |
| 651 | */ |
| 652 | public function outputVideoSchemaMarkup( $data ) { |
| 653 | |
| 654 | if ( empty( $data ) ) { |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | ?> |
| 659 | <script type="application/ld+json"> |
| 660 | <?php |
| 661 | echo wp_json_encode( $data ); |
| 662 | ?> |
| 663 | </script> |
| 664 | <?php |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Adds an iframe fallback script to the page in case js loading fails. |
| 669 | * |
| 670 | * This function checks if the video provider is YouTube or Vimeo and adds |
| 671 | * a filter to load an iframe fallback script if necessary. This ensures |
| 672 | * that the video can still be displayed even if JavaScript fails to load. |
| 673 | * |
| 674 | * @param array $data An array containing video data, including the 'provider' key. |
| 675 | * |
| 676 | * @return void This function doesn't return a value, but may add a filter. |
| 677 | */ |
| 678 | public function iframeFallback( $data ) { |
| 679 | // must be vimeo or youtube. |
| 680 | if ( in_array( $data['provider'], array( 'youtube', 'vimeo' ), true ) ) { |
| 681 | add_filter( 'presto_player/scripts/load_iframe_fallback', '__return_true' ); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * This function return HTML for unauthorized access or curtain. |
| 687 | * |
| 688 | * @return string. |
| 689 | */ |
| 690 | public function getFallbackHTMLForUnauthorizeAccess() { |
| 691 | // Get the branding CSS variable. |
| 692 | $data = $this->getAttributes( array() ); |
| 693 | ob_start(); |
| 694 | if ( file_exists( PRESTO_PLAYER_PLUGIN_DIR . 'templates/unauthorized.php' ) ) { |
| 695 | include PRESTO_PLAYER_PLUGIN_DIR . 'templates/unauthorized.php'; |
| 696 | } |
| 697 | $template = ob_get_contents(); |
| 698 | ob_end_clean(); |
| 699 | return $template; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * Return fallback html for feeds. |
| 704 | * |
| 705 | * @param array $atts array of attributes. |
| 706 | */ |
| 707 | public function getFeedHtml( $atts ) { |
| 708 | if ( is_feed() ) { |
| 709 | ob_start(); |
| 710 | ?> |
| 711 | |
| 712 | <?php if ( in_array( $this->name, array( 'self-hosted', 'bunny' ), true ) && ! empty( $atts['src'] ) ) { ?> |
| 713 | <video controls preload="none"> |
| 714 | <source src="<?php echo esc_url( $atts['src'] ); ?>" /> |
| 715 | </video> |
| 716 | <?php } ?> |
| 717 | |
| 718 | <?php if ( 'audio' === $this->name && ! empty( $atts['src'] ) ) { ?> |
| 719 | <audio controls preload="none"> |
| 720 | <source src="<?php echo esc_url( $atts['src'] ); ?>" /> |
| 721 | </audio> |
| 722 | <?php } ?> |
| 723 | |
| 724 | <?php if ( 'youtube' === $this->name && ! empty( $atts['video_id'] ) ) { ?> |
| 725 | <?php echo wp_kses_post( wp_oembed_get( 'https://www.youtube.com/watch?v=' . esc_attr( $atts['video_id'] ) ) ); ?> |
| 726 | <?php } ?> |
| 727 | |
| 728 | <?php if ( 'vimeo' === $this->name && ! empty( $atts['video_id'] ) ) { ?> |
| 729 | <?php echo wp_kses_post( wp_oembed_get( 'https://vimeo.com/' . esc_attr( $atts['video_id'] ) ) ); ?> |
| 730 | <?php } ?> |
| 731 | |
| 732 | <?php |
| 733 | return ob_get_clean(); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Applies a default value to the attribute if attribute is not set. |
| 739 | * |
| 740 | * @param array $attributes array of attributes. |
| 741 | * @return array The merged attributes after applying defaults. |
| 742 | */ |
| 743 | public function applyAttributeDefaults( $attributes ) { |
| 744 | return wp_parse_args( $attributes, $this->default_attributes ); |
| 745 | } |
| 746 | } |
| 747 |