VideoWidget.php
553 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Elementor Presto Player video widget. |
| 4 | * |
| 5 | * @package PrestoPlayer |
| 6 | */ |
| 7 | |
| 8 | namespace PrestoPlayer\Integrations\Elementor; |
| 9 | |
| 10 | use Elementor\Utils; |
| 11 | use Elementor\Widget_Base; |
| 12 | use Elementor\Controls_Manager; |
| 13 | use PrestoPlayer\Models\Preset; |
| 14 | use PrestoPlayer\Blocks\VimeoBlock; |
| 15 | use PrestoPlayer\Blocks\YouTubeBlock; |
| 16 | use PrestoPlayer\Blocks\SelfHostedBlock; |
| 17 | use Elementor\Modules\DynamicTags\Module as TagsModule; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; // Exit if accessed directly. |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Presto Player video widget. |
| 25 | * |
| 26 | * Presto Player widget that displays a video player. |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class VideoWidget extends Widget_Base { |
| 31 | |
| 32 | /** |
| 33 | * Whether the premium version is active. |
| 34 | * |
| 35 | * @var bool |
| 36 | */ |
| 37 | private $is_premium = false; |
| 38 | |
| 39 | /** |
| 40 | * Plugin version string. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | private $version = ''; |
| 45 | |
| 46 | /** |
| 47 | * Set whether the premium version is active. |
| 48 | * |
| 49 | * @param bool $pro Whether premium is active. |
| 50 | * @return void |
| 51 | */ |
| 52 | public function setPremium( $pro ) { |
| 53 | $this->is_premium = $pro; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Set the plugin version. |
| 58 | * |
| 59 | * @param string $version Plugin version string. |
| 60 | * @return void |
| 61 | */ |
| 62 | public function setVersion( $version ) { |
| 63 | $this->version = $version; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get widget name. |
| 68 | * |
| 69 | * Retrieve video widget name. |
| 70 | * |
| 71 | * @since 1.0.0 |
| 72 | * @access public |
| 73 | * |
| 74 | * @return string Widget name. |
| 75 | */ |
| 76 | public function get_name() { |
| 77 | return 'presto_video'; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get widget title. |
| 82 | * |
| 83 | * Retrieve video widget title. |
| 84 | * |
| 85 | * @since 1.0.0 |
| 86 | * @access public |
| 87 | * |
| 88 | * @return string Widget title. |
| 89 | */ |
| 90 | public function get_title() { |
| 91 | return __( 'Presto Video', 'presto-player' ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get widget icon. |
| 96 | * |
| 97 | * Retrieve video widget icon. |
| 98 | * |
| 99 | * @since 1.0.0 |
| 100 | * @access public |
| 101 | * |
| 102 | * @return string Widget icon. |
| 103 | */ |
| 104 | public function get_icon() { |
| 105 | return 'eicon-youtube'; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get widget categories. |
| 110 | * |
| 111 | * Retrieve the list of categories the video widget belongs to. |
| 112 | * |
| 113 | * Used to determine where to display the widget in the editor. |
| 114 | * |
| 115 | * @since 2.0.0 |
| 116 | * @access public |
| 117 | * |
| 118 | * @return array Widget categories. |
| 119 | */ |
| 120 | public function get_categories() { |
| 121 | return array( 'basic' ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get widget keywords. |
| 126 | * |
| 127 | * Retrieve the list of keywords the widget belongs to. |
| 128 | * |
| 129 | * @since 2.1.0 |
| 130 | * @access public |
| 131 | * |
| 132 | * @return array Widget keywords. |
| 133 | */ |
| 134 | public function get_keywords() { |
| 135 | return array( 'video', 'player', 'embed', 'youtube', 'vimeo' ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Build the fake (disabled) toggle control HTML for non-premium users. |
| 140 | * |
| 141 | * @param string $label Control label text. |
| 142 | * @return string Toggle control HTML markup. |
| 143 | */ |
| 144 | public function fake_toggle_html( $label = '' ) { |
| 145 | return '<div class="elementor-control-muted_preview elementor-control-type-switcher elementor-label-inline elementor-control-separator-default"> |
| 146 | <div class="elementor-control-content"> |
| 147 | <div class="elementor-control-field"> |
| 148 | <label for="elementor-control-default-c513" class="elementor-control-title">' . esc_html( $label ) . ' <i class="eicon-pro-icon"></i></label> |
| 149 | <div class="elementor-control-input-wrapper"> |
| 150 | <label class="elementor-switch elementor-control-unit-2"> |
| 151 | <input id="elementor-control-default-c513" type="checkbox" data-setting="muted_preview" class="elementor-switch-input" value="yes" disabled> |
| 152 | <span class="elementor-switch-label" data-on="Yes" data-off="No"></span> |
| 153 | <span class="elementor-switch-handle"></span> |
| 154 | </label> |
| 155 | </div> |
| 156 | </div> |
| 157 | </div> |
| 158 | </div>'; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Register video widget controls. |
| 163 | * |
| 164 | * Adds different input fields to allow the user to change and customize the widget settings. |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | * @access protected |
| 168 | */ |
| 169 | protected function _register_controls() { |
| 170 | $this->start_controls_section( |
| 171 | 'section_video', |
| 172 | array( |
| 173 | 'label' => __( 'Video', 'presto-player' ), |
| 174 | ) |
| 175 | ); |
| 176 | |
| 177 | $this->add_control( |
| 178 | 'video_type', |
| 179 | array( |
| 180 | 'label' => __( 'Source', 'presto-player' ), |
| 181 | 'type' => Controls_Manager::SELECT, |
| 182 | 'default' => 'youtube', |
| 183 | 'options' => array( |
| 184 | 'youtube' => __( 'YouTube', 'presto-player' ), |
| 185 | 'vimeo' => __( 'Vimeo', 'presto-player' ), |
| 186 | 'hosted' => __( 'Self Hosted', 'presto-player' ), |
| 187 | ), |
| 188 | ) |
| 189 | ); |
| 190 | |
| 191 | $this->add_control( |
| 192 | 'youtube_url', |
| 193 | array( |
| 194 | 'label' => __( 'Link', 'presto-player' ), |
| 195 | 'type' => Controls_Manager::TEXT, |
| 196 | 'dynamic' => array( |
| 197 | 'active' => true, |
| 198 | 'categories' => array( |
| 199 | TagsModule::POST_META_CATEGORY, |
| 200 | TagsModule::URL_CATEGORY, |
| 201 | ), |
| 202 | ), |
| 203 | 'placeholder' => __( 'Enter your URL', 'presto-player' ) . ' (YouTube)', |
| 204 | 'default' => 'https://www.youtube.com/watch?v=XHOmBV4js_E', |
| 205 | 'label_block' => true, |
| 206 | 'condition' => array( |
| 207 | 'video_type' => 'youtube', |
| 208 | ), |
| 209 | ) |
| 210 | ); |
| 211 | |
| 212 | $this->add_control( |
| 213 | 'vimeo_url', |
| 214 | array( |
| 215 | 'label' => __( 'Link', 'presto-player' ), |
| 216 | 'type' => Controls_Manager::TEXT, |
| 217 | 'dynamic' => array( |
| 218 | 'active' => true, |
| 219 | 'categories' => array( |
| 220 | TagsModule::POST_META_CATEGORY, |
| 221 | TagsModule::URL_CATEGORY, |
| 222 | ), |
| 223 | ), |
| 224 | 'placeholder' => __( 'Enter your URL', 'presto-player' ) . ' (Vimeo)', |
| 225 | 'default' => 'https://vimeo.com/235215203', |
| 226 | 'label_block' => true, |
| 227 | 'condition' => array( |
| 228 | 'video_type' => 'vimeo', |
| 229 | ), |
| 230 | ) |
| 231 | ); |
| 232 | |
| 233 | $this->add_control( |
| 234 | 'hosted_url', |
| 235 | array( |
| 236 | 'label' => __( 'Add/Select Video', 'presto-player' ), |
| 237 | 'type' => Controls_Manager::MEDIA, |
| 238 | 'dynamic' => array( |
| 239 | 'active' => true, |
| 240 | 'categories' => array( |
| 241 | TagsModule::MEDIA_CATEGORY, |
| 242 | ), |
| 243 | ), |
| 244 | 'media_type' => 'video', |
| 245 | 'condition' => array( |
| 246 | 'video_type' => 'hosted', |
| 247 | ), |
| 248 | ) |
| 249 | ); |
| 250 | |
| 251 | $this->add_control( |
| 252 | 'external_url', |
| 253 | array( |
| 254 | 'label' => __( 'URL', 'presto-player' ), |
| 255 | 'type' => Controls_Manager::URL, |
| 256 | 'autocomplete' => false, |
| 257 | 'options' => false, |
| 258 | 'label_block' => true, |
| 259 | 'show_label' => false, |
| 260 | 'dynamic' => array( |
| 261 | 'active' => true, |
| 262 | 'categories' => array( |
| 263 | TagsModule::POST_META_CATEGORY, |
| 264 | TagsModule::URL_CATEGORY, |
| 265 | ), |
| 266 | ), |
| 267 | 'media_type' => 'video', |
| 268 | 'placeholder' => __( 'Enter your URL', 'presto-player' ), |
| 269 | 'condition' => array( |
| 270 | 'video_type' => 'hosted', |
| 271 | 'insert_url' => 'yes', |
| 272 | ), |
| 273 | ) |
| 274 | ); |
| 275 | |
| 276 | $this->add_control( |
| 277 | 'video_options', |
| 278 | array( |
| 279 | 'label' => __( 'Video Options', 'presto-player' ), |
| 280 | 'type' => Controls_Manager::HEADING, |
| 281 | 'separator' => 'before', |
| 282 | ) |
| 283 | ); |
| 284 | |
| 285 | if ( ! $this->is_premium ) { |
| 286 | $this->add_control( |
| 287 | 'important_note', |
| 288 | array( |
| 289 | 'type' => \Elementor\Controls_Manager::RAW_HTML, |
| 290 | 'raw' => $this->fake_toggle_html( __( 'Muted Preview', 'presto-player' ) ), |
| 291 | 'condition' => array( |
| 292 | 'autoplay' => '', |
| 293 | ), |
| 294 | ) |
| 295 | ); |
| 296 | } else { |
| 297 | $this->add_control( |
| 298 | 'muted_preview', |
| 299 | array( |
| 300 | 'label' => __( 'Muted Preview', 'presto-player' ), |
| 301 | 'type' => Controls_Manager::SWITCHER, |
| 302 | 'condition' => array( |
| 303 | 'autoplay' => '', |
| 304 | ), |
| 305 | ) |
| 306 | ); |
| 307 | } |
| 308 | |
| 309 | $this->add_control( |
| 310 | 'autoplay', |
| 311 | array( |
| 312 | 'label' => __( 'Autoplay', 'presto-player' ), |
| 313 | 'type' => Controls_Manager::SWITCHER, |
| 314 | 'condition' => array( |
| 315 | 'muted_preview' => '', |
| 316 | ), |
| 317 | ) |
| 318 | ); |
| 319 | |
| 320 | $this->add_control( |
| 321 | 'play-inline', |
| 322 | array( |
| 323 | 'label' => __( 'Play inline', 'presto-player' ), |
| 324 | 'type' => Controls_Manager::SWITCHER, |
| 325 | ) |
| 326 | ); |
| 327 | |
| 328 | $this->add_control( |
| 329 | 'poster', |
| 330 | array( |
| 331 | 'label' => __( 'Poster', 'presto-player' ), |
| 332 | 'type' => Controls_Manager::MEDIA, |
| 333 | |
| 334 | ) |
| 335 | ); |
| 336 | |
| 337 | $this->end_controls_section(); |
| 338 | |
| 339 | $this->start_controls_section( |
| 340 | 'section_video_style', |
| 341 | array( |
| 342 | 'label' => __( 'Video Preset', 'presto-player' ), |
| 343 | 'tab' => Controls_Manager::TAB_STYLE, |
| 344 | ) |
| 345 | ); |
| 346 | |
| 347 | $options = $this->get_preset_options(); |
| 348 | |
| 349 | $this->add_control( |
| 350 | 'preset', |
| 351 | array( |
| 352 | 'label' => __( 'Select A Preset', 'presto-player' ), |
| 353 | 'type' => Controls_Manager::SELECT, |
| 354 | 'options' => $options['options'], |
| 355 | 'default' => $options['default_id'], |
| 356 | 'frontend_available' => true, |
| 357 | ) |
| 358 | ); |
| 359 | |
| 360 | $this->end_controls_section(); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Get the available preset options for the preset select control. |
| 365 | * |
| 366 | * @return array { |
| 367 | * Preset options data. |
| 368 | * |
| 369 | * @type array $options Map of preset ID to preset name. |
| 370 | * @type int $default_id The default preset ID. |
| 371 | * } |
| 372 | */ |
| 373 | protected function get_preset_options() { |
| 374 | $presets = new Preset(); |
| 375 | $presets = $presets->all(); |
| 376 | |
| 377 | $preset_options = array(); |
| 378 | $default_id = 0; |
| 379 | if ( ! empty( $presets ) ) { |
| 380 | foreach ( $presets as $preset ) { |
| 381 | if ( 'default' === $preset->slug ) { |
| 382 | $default_id = $preset->id; |
| 383 | } |
| 384 | $preset_options[ $preset->id ] = $preset->name; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return array( |
| 389 | 'options' => $preset_options, |
| 390 | 'default_id' => $default_id, |
| 391 | ); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Render video widget output on the frontend. |
| 396 | * |
| 397 | * Written in PHP and used to generate the final HTML. |
| 398 | * |
| 399 | * @since 1.0.0 |
| 400 | * @access protected |
| 401 | */ |
| 402 | protected function render() { |
| 403 | $settings = $this->get_settings_for_display(); |
| 404 | |
| 405 | switch ( $settings['video_type'] ) { |
| 406 | case 'hosted': |
| 407 | $hosted = new SelfHostedBlock( $this->is_premium, $this->version ); |
| 408 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Block html() escapes all values internally; player markup must not be re-escaped. |
| 409 | echo $hosted->html( |
| 410 | array( |
| 411 | 'id' => $settings['hosted_url']['id'], |
| 412 | 'src' => $settings['hosted_url']['url'], |
| 413 | 'preset' => $settings['preset'], |
| 414 | 'autoplay' => $settings['autoplay'], |
| 415 | 'playsInline' => $settings['play-inline'], |
| 416 | 'poster' => ! empty( $settings['poster']['url'] ) ? $settings['poster']['url'] : '', |
| 417 | 'chapters' => array(), |
| 418 | ), |
| 419 | '' |
| 420 | ); |
| 421 | // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 422 | break; |
| 423 | case 'youtube': |
| 424 | $youtube = new YouTubeBlock( $this->is_premium, $this->version ); |
| 425 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Block html() escapes all values internally; player markup must not be re-escaped. |
| 426 | echo $youtube->html( |
| 427 | array( |
| 428 | 'src' => $settings['youtube_url'], |
| 429 | 'preset' => $settings['preset'], |
| 430 | 'autoplay' => $settings['autoplay'], |
| 431 | 'playsInline' => $settings['play-inline'], |
| 432 | 'poster' => ! empty( $settings['poster']['url'] ) ? $settings['poster']['url'] : '', |
| 433 | 'chapters' => array(), |
| 434 | ), |
| 435 | '' |
| 436 | ); |
| 437 | // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 438 | break; |
| 439 | case 'vimeo': |
| 440 | $vimeo = new VimeoBlock( $this->is_premium, $this->version ); |
| 441 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Block html() escapes all values internally; player markup must not be re-escaped. |
| 442 | echo $vimeo->html( |
| 443 | array( |
| 444 | 'src' => $settings['vimeo_url'], |
| 445 | 'preset' => $settings['preset'], |
| 446 | 'autoplay' => $settings['autoplay'], |
| 447 | 'playsInline' => $settings['play-inline'], |
| 448 | 'poster' => ! empty( $settings['poster']['url'] ) ? $settings['poster']['url'] : '', |
| 449 | 'chapters' => array(), |
| 450 | ), |
| 451 | '' |
| 452 | ); |
| 453 | // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | |
| 459 | /** |
| 460 | * Build the HTML attribute parameters for a self-hosted video element. |
| 461 | * |
| 462 | * @since 2.1.0 |
| 463 | * @access private |
| 464 | * |
| 465 | * @return array Map of attribute name to attribute value. |
| 466 | */ |
| 467 | private function get_hosted_params() { |
| 468 | $settings = $this->get_settings_for_display(); |
| 469 | |
| 470 | $video_params = array(); |
| 471 | |
| 472 | foreach ( array( 'autoplay', 'loop', 'controls' ) as $option_name ) { |
| 473 | if ( $settings[ $option_name ] ) { |
| 474 | $video_params[ $option_name ] = ''; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if ( $settings['mute'] ) { |
| 479 | $video_params['muted'] = 'muted'; |
| 480 | } |
| 481 | |
| 482 | if ( $settings['play_on_mobile'] ) { |
| 483 | $video_params['playsinline'] = ''; |
| 484 | } |
| 485 | |
| 486 | if ( ! $settings['download_button'] ) { |
| 487 | $video_params['controlsList'] = 'nodownload'; |
| 488 | } |
| 489 | |
| 490 | if ( $settings['poster']['url'] ) { |
| 491 | $video_params['poster'] = $settings['poster']['url']; |
| 492 | } |
| 493 | |
| 494 | return $video_params; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Get the resolved self-hosted video URL, including time fragments. |
| 499 | * |
| 500 | * @since 2.1.0 |
| 501 | * @access private |
| 502 | * |
| 503 | * @return string Video URL, or empty string when none is set. |
| 504 | */ |
| 505 | private function get_hosted_video_url() { |
| 506 | $settings = $this->get_settings_for_display(); |
| 507 | |
| 508 | if ( ! empty( $settings['insert_url'] ) ) { |
| 509 | $video_url = $settings['external_url']['url']; |
| 510 | } else { |
| 511 | $video_url = $settings['hosted_url']['url']; |
| 512 | } |
| 513 | |
| 514 | if ( empty( $video_url ) ) { |
| 515 | return ''; |
| 516 | } |
| 517 | |
| 518 | if ( $settings['start'] || $settings['end'] ) { |
| 519 | $video_url .= '#t='; |
| 520 | } |
| 521 | |
| 522 | if ( $settings['start'] ) { |
| 523 | $video_url .= $settings['start']; |
| 524 | } |
| 525 | |
| 526 | if ( $settings['end'] ) { |
| 527 | $video_url .= ',' . $settings['end']; |
| 528 | } |
| 529 | |
| 530 | return $video_url; |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Render the self-hosted video element markup. |
| 535 | * |
| 536 | * @since 2.1.0 |
| 537 | * @access private |
| 538 | * |
| 539 | * @return void |
| 540 | */ |
| 541 | private function render_hosted_video() { |
| 542 | $video_url = $this->get_hosted_video_url(); |
| 543 | if ( empty( $video_url ) ) { |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | $video_params = $this->get_hosted_params(); |
| 548 | ?> |
| 549 | <video class="presto-player-video" src="<?php echo esc_url( $video_url ); ?>" <?php echo Utils::render_html_attributes( $video_params ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor\Utils::render_html_attributes() escapes each attribute value. ?>></video> |
| 550 | <?php |
| 551 | } |
| 552 | } |
| 553 |