← All changes
|
modules/videopress/class.videopress-player.php
+68
-43
13.8.3
→
16.3-a.5
View file →
| @@ -1,7 +1,12 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | +use Automattic\Jetpack\VideoPress\Inline_Player; | |
| 2 | 3 | use Automattic\Jetpack\VideoPress\Jwt_Token_Bridge; |
| 3 | 4 | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit( 0 ); | |
| 7 | +} | |
| 8 | + | |
| 4 | 9 | /** |
| 5 | 10 | * VideoPress playback module markup generator. |
| 6 | 11 | * |
| 7 | 12 | * @since 1.3 |
| @@ -47,8 +52,15 @@ | ||
| 47 | 52 | */ |
| 48 | 53 | public static $shown = array(); |
| 49 | 54 | |
| 50 | 55 | /** |
| 56 | + * Fallback video title. | |
| 57 | + * | |
| 58 | + * @var ?string | |
| 59 | + */ | |
| 60 | + protected $title; | |
| 61 | + | |
| 62 | + /** | |
| 51 | 63 | * Initiate a player object based on shortcode values and possible blog-level option overrides |
| 52 | 64 | * |
| 53 | 65 | * @since 1.3 |
| 54 | 66 | * @param string $guid VideoPress unique identifier. |
| @@ -96,11 +108,9 @@ | ||
| 96 | 108 | $cached_video = wp_cache_get( $cache_key, 'video' ); |
| 97 | 109 | } |
| 98 | 110 | if ( empty( $cached_video ) ) { |
| 99 | 111 | $video = new VideoPress_Video( $guid, $maxwidth ); |
| 100 | - if ( empty( $video ) ) { | |
| 101 | - return; | |
| 102 | - } elseif ( isset( $video->error ) ) { | |
| 112 | + if ( isset( $video->error ) ) { | |
| 103 | 113 | $this->video = $video->error; |
| 104 | 114 | return; |
| 105 | 115 | } elseif ( is_wp_error( $video ) ) { |
| 106 | 116 | $this->video = $video; |
| @@ -510,18 +520,18 @@ | ||
| 510 | 520 | $data['locale'] = $locale; |
| 511 | 521 | unset( $locale ); |
| 512 | 522 | |
| 513 | 523 | $guid = $this->video->guid; |
| 514 | - $guid_js = wp_json_encode( $guid ); | |
| 524 | + $guid_js = wp_json_encode( $guid, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); | |
| 515 | 525 | $html .= '<script type="text/javascript">' . PHP_EOL; |
| 516 | 526 | $html .= 'jQuery(document).ready(function() {'; |
| 517 | 527 | |
| 518 | - $html .= 'if ( !jQuery.VideoPress.data[' . wp_json_encode( $guid ) . '] ) { jQuery.VideoPress.data[' . wp_json_encode( $guid ) . '] = new Array(); }' . PHP_EOL; | |
| 519 | - $html .= 'jQuery.VideoPress.data[' . wp_json_encode( $guid ) . '][' . self::$shown[ $guid ] . ']=' . wp_json_encode( $data ) . ';' . PHP_EOL; | |
| 528 | + $html .= 'if ( !jQuery.VideoPress.data[' . $guid_js . '] ) { jQuery.VideoPress.data[' . $guid_js . '] = new Array(); }' . PHP_EOL; | |
| 529 | + $html .= 'jQuery.VideoPress.data[' . $guid_js . '][' . self::$shown[ $guid ] . ']=' . wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';' . PHP_EOL; | |
| 520 | 530 | unset( $data ); |
| 521 | 531 | |
| 522 | - $jq_container = wp_json_encode( '#' . $this->video_container_id ); | |
| 523 | - $jq_placeholder = wp_json_encode( '#' . $video_placeholder_id ); | |
| 532 | + $jq_container = wp_json_encode( '#' . $this->video_container_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); | |
| 533 | + $jq_placeholder = wp_json_encode( '#' . $video_placeholder_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); | |
| 524 | 534 | $player_config = "{width:{$width},height:{$height},"; |
| 525 | 535 | if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true ) { |
| 526 | 536 | $player_config .= 'freedom:"true",'; |
| 527 | 537 | } |
| @@ -539,11 +549,11 @@ | ||
| 539 | 549 | $html .= 'swfobject.embedSWF(' . implode( |
| 540 | 550 | ',', |
| 541 | 551 | array( |
| 542 | 552 | 'jQuery.VideoPress.video.flash.player_uri', |
| 543 | - wp_json_encode( $this->video_container_id ), | |
| 544 | - wp_json_encode( $width ), | |
| 545 | - wp_json_encode( $height ), | |
| 553 | + wp_json_encode( $this->video_container_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ), | |
| 554 | + wp_json_encode( $width, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ), | |
| 555 | + wp_json_encode( $height, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ), | |
| 546 | 556 | 'jQuery.VideoPress.video.flash.min_version', |
| 547 | 557 | 'jQuery.VideoPress.video.flash.expressinstall', // attempt to upgrade the Flash player if less than min_version. requires a 310x137 container or larger but we will always try to include |
| 548 | 558 | '{guid:' . $guid_js . '}', // FlashVars |
| 549 | 559 | 'jQuery.VideoPress.video.flash.params', |
| @@ -552,11 +562,11 @@ | ||
| 552 | 562 | ) |
| 553 | 563 | ) . ');'; |
| 554 | 564 | $html .= '} else {' . PHP_EOL; |
| 555 | 565 | $html .= "if ( jQuery.VideoPress.video.prepare({$guid_js},{$player_config}," . self::$shown[ $guid ] . ') ) {' . PHP_EOL; |
| 556 | - $html .= 'if ( jQuery(' . $jq_container . ').data( "player" ) === "flash" ){jQuery.VideoPress.video.play(jQuery(' . wp_json_encode( '#' . $this->video_container_id ) . '));}else{'; | |
| 557 | - $html .= 'jQuery(' . $jq_placeholder . ').html(' . wp_json_encode( $this->html_age_date() ) . ');' . PHP_EOL; | |
| 558 | - $html .= 'jQuery(' . wp_json_encode( '#' . $video_placeholder_id . ' input[type=submit]' ) . ').one("click", function(event){jQuery.VideoPress.requirements.isSufficientAge(jQuery(' . $jq_container . '),' . absint( $this->video->age_rating ) . ')});' . PHP_EOL; | |
| 566 | + $html .= 'if ( jQuery(' . $jq_container . ').data( "player" ) === "flash" ){jQuery.VideoPress.video.play(jQuery(' . wp_json_encode( '#' . $this->video_container_id, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . '));}else{'; | |
| 567 | + $html .= 'jQuery(' . $jq_placeholder . ').html(' . wp_json_encode( $this->html_age_date(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ');' . PHP_EOL; | |
| 568 | + $html .= 'jQuery(' . wp_json_encode( '#' . $video_placeholder_id . ' input[type="submit"]', JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ').one("click", function(event){jQuery.VideoPress.requirements.isSufficientAge(jQuery(' . $jq_container . '),' . absint( $this->video->age_rating ) . ')});' . PHP_EOL; | |
| 559 | 569 | $html .= '}}}' . PHP_EOL; |
| 560 | 570 | } else { |
| 561 | 571 | $html .= "if ( jQuery.VideoPress.video.prepare({$guid_js}, {$player_config}," . self::$shown[ $guid ] . ') ) {' . PHP_EOL; |
| 562 | 572 | if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true ) { |
| @@ -625,22 +635,10 @@ | ||
| 625 | 635 | // Must not use iframes for IE11 due to a fullscreen bug |
| 626 | 636 | if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && stristr( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ), 'Trident/7.0; rv:11.0' ) ) { |
| 627 | 637 | $iframe_embed = false; |
| 628 | 638 | } else { |
| 629 | - | |
| 630 | - /** | |
| 631 | - * Filter the VideoPress iframe embed | |
| 632 | - * | |
| 633 | - * This filter allows you to control whether the videos will be embedded using an iframe. | |
| 634 | - * Set this to false in order to use an in-page embed rather than an iframe. | |
| 635 | - * | |
| 636 | - * @module videopress | |
| 637 | - * | |
| 638 | - * @since 3.7.0 | |
| 639 | - * | |
| 640 | - * @param boolean $videopress_player_use_iframe | |
| 641 | - */ | |
| 642 | - $iframe_embed = apply_filters( 'jetpack_videopress_player_use_iframe', true ); | |
| 639 | + // The site setting and the `jetpack_videopress_player_use_iframe` filter decide; see Inline_Player::is_enabled(). | |
| 640 | + $iframe_embed = ! Inline_Player::is_enabled(); | |
| 643 | 641 | } |
| 644 | 642 | |
| 645 | 643 | if ( ! array_key_exists( 'hd', $this->options ) ) { |
| 646 | 644 | $this->options['hd'] = (bool) get_option( 'video_player_high_quality', false ); |
| @@ -697,15 +695,17 @@ | ||
| 697 | 695 | foreach ( $videopress_options as $option => $value ) { |
| 698 | 696 | if ( ! in_array( $option, array( 'width', 'height' ), true ) ) { |
| 699 | 697 | |
| 700 | 698 | // add_query_arg ignores false as a value, so replacing it with 0 |
| 699 | + // @phan-suppress-next-line PhanPluginSimplifyExpressionBool -- Probably it could, but semantically let's keep it as-is. | |
| 701 | 700 | $iframe_url = add_query_arg( $option, ( false === $value ) ? 0 : $value, $iframe_url ); |
| 702 | 701 | } |
| 703 | 702 | } |
| 704 | 703 | |
| 705 | - $cover = $videopress_options['cover'] ? ' data-resize-to-parent="true"' : ''; | |
| 706 | - $js_url = 'https://s0.wp.com/wp-content/plugins/video/assets/js/next/videopress-iframe.js'; | |
| 707 | - // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript | |
| 704 | + $cover = $videopress_options['cover'] ? ' data-resize-to-parent="true"' : ''; | |
| 705 | + | |
| 706 | + wp_enqueue_script( 'videopress-iframe', 'https://videopress.com/videopress-iframe.js', array(), JETPACK__VERSION, true ); | |
| 707 | + | |
| 708 | 708 | return "<iframe title='" . __( 'VideoPress Video Player', 'jetpack' ) |
| 709 | 709 | . "' aria-label='" . __( 'VideoPress Video Player', 'jetpack' ) |
| 710 | 710 | . "' width='" . esc_attr( $videopress_options['width'] ) |
| 711 | 711 | . "' height='" . esc_attr( $videopress_options['height'] ) |
| @@ -711,21 +711,46 @@ | ||
| 711 | 711 | . "' height='" . esc_attr( $videopress_options['height'] ) |
| 712 | 712 | . "' src='" . esc_attr( $iframe_url ) |
| 713 | 713 | . "' frameborder='0' allowfullscreen" |
| 714 | 714 | . $cover |
| 715 | - . " allow='clipboard-write'></iframe>" | |
| 716 | - . "<script src='" . esc_attr( $js_url ) . "'></script>"; | |
| 715 | + . " allow='clipboard-write; presentation'></iframe>"; | |
| 717 | 716 | |
| 718 | 717 | } else { |
| 719 | - $videopress_options = wp_json_encode( $videopress_options ); | |
| 720 | - $js_url = 'https://s0.wp.com/wp-content/plugins/video/assets/js/videojs/videopress.js'; | |
| 718 | + $attributes = array( | |
| 719 | + 'autoplay' => $videopress_options['autoPlay'] ?? false, | |
| 720 | + 'controls' => $videopress_options['controls'] ?? true, | |
| 721 | + 'loop' => $videopress_options['loop'] ?? false, | |
| 722 | + 'muted' => $videopress_options['muted'] ?? false, | |
| 723 | + 'playsinline' => $videopress_options['playsinline'] ?? false, | |
| 724 | + 'useAverageColor' => $videopress_options['useAverageColor'] ?? true, | |
| 725 | + 'cover' => $videopress_options['cover'], | |
| 726 | + 'hd' => $videopress_options['hd'], | |
| 727 | + 'at' => $videopress_options['at'] ?? 0, | |
| 728 | + 'preload' => $videopress_options['preloadContent'] ?? 'metadata', | |
| 729 | + 'defaultLangCode' => $videopress_options['defaultLangCode'] ?? '', | |
| 730 | + ); | |
| 731 | + $ratio = $videopress_options['width'] > 0 | |
| 732 | + ? ( $videopress_options['height'] / $videopress_options['width'] ) * 100 | |
| 733 | + : null; | |
| 721 | 734 | |
| 722 | - return "<div id='{$video_container_id}'></div> | |
| 723 | - <script src='{$js_url}'></script> | |
| 724 | - <script> | |
| 725 | - videopress('{$this->video->guid}', document.querySelector('#{$video_container_id}'), {$videopress_options}); | |
| 726 | - </script>"; | |
| 727 | - // phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedScript | |
| 735 | + $guid = (string) $this->video->guid; | |
| 736 | + | |
| 737 | + // The video data already carries the poster frame VideoPress serves for this site. | |
| 738 | + $poster = ! empty( $this->video->poster_frame_uri ) && is_string( $this->video->poster_frame_uri ) | |
| 739 | + ? $this->video->poster_frame_uri | |
| 740 | + : Inline_Player::get_poster_url( $guid ); | |
| 741 | + | |
| 742 | + return "<div id='" . esc_attr( $video_container_id ) . "'>" | |
| 743 | + . Inline_Player::render( | |
| 744 | + $guid, | |
| 745 | + Inline_Player::get_player_options( $attributes ), | |
| 746 | + $ratio, | |
| 747 | + array( | |
| 748 | + 'poster' => $poster, | |
| 749 | + 'title' => isset( $this->video->title ) ? (string) $this->video->title : '', | |
| 750 | + ) | |
| 751 | + ) | |
| 752 | + . '</div>'; | |
| 728 | 753 | } |
| 729 | 754 | } |
| 730 | 755 | |
| 731 | 756 | /** |
| @@ -851,9 +876,9 @@ | ||
| 851 | 876 | } |
| 852 | 877 | |
| 853 | 878 | $embed = array( |
| 854 | 879 | 'id' => $this->video_id, |
| 855 | - 'src' => esc_url_raw( $this->video->players->swf->url . '&' . http_build_query( $this->get_flash_variables(), null, '&' ), array( 'http', 'https' ) ), | |
| 880 | + 'src' => esc_url_raw( $this->video->players->swf->url . '&' . http_build_query( $this->get_flash_variables(), '', '&' ), array( 'http', 'https' ) ), | |
| 856 | 881 | 'type' => 'application/x-shockwave-flash', |
| 857 | 882 | 'width' => $this->video->calculated_width, |
| 858 | 883 | 'height' => $this->video->calculated_height, |
| 859 | 884 | ); |
| @@ -887,9 +912,9 @@ | ||
| 887 | 912 | if ( isset( $this->video->title ) ) { |
| 888 | 913 | $thumbnail_html .= esc_attr( $this->video->title ); |
| 889 | 914 | } |
| 890 | 915 | $thumbnail_html .= '" src="' . esc_url( $this->video->poster_frame_uri, array( 'http', 'https' ) ) . '" width="' . $this->video->calculated_width . '" height="' . $this->video->calculated_height . '" />'; |
| 891 | - $flash_vars = esc_attr( http_build_query( $this->get_flash_variables(), null, '&' ) ); | |
| 916 | + $flash_vars = esc_attr( http_build_query( $this->get_flash_variables(), '', '&' ) ); | |
| 892 | 917 | $flash_params = ''; |
| 893 | 918 | foreach ( $this->get_flash_parameters() as $attribute => $value ) { |
| 894 | 919 | $flash_params .= '<param name="' . esc_attr( $attribute ) . '" value="' . esc_attr( $value ) . '" />'; |
| 895 | 920 | } |