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