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