Make WordPress Core

Ticket #42039: 42039.diff

File 42039.diff, 5.4 KB (added by obenland, 7 years ago)
  • src/wp-admin/js/widgets/media-video-widget.js

     
    134134                },
    135135
    136136                /**
    137                  * Whether a url is a supported external host.
     137                 * Whether MediaElement.js can directly play the URL from an external providers.
    138138                 *
    139139                 * @param {String} url - Video url.
    140140                 * @returns {boolean} Whether url is a supported video host.
     
    151151                 * @returns {void}
    152152                 */
    153153                renderPreview: function renderPreview() {
    154                         var control = this, previewContainer, previewTemplate, attachmentId, attachmentUrl, poster, isHostedEmbed = false, mime, error;
     154                        var control = this, previewContainer, previewTemplate, attachmentId, attachmentUrl, html = '', canHostedVideoPlayByMEJS = false, isOEmbed = false, mime, error, urlParser, matches;
    155155                        attachmentId = control.model.get( 'attachment_id' );
    156156                        attachmentUrl = control.model.get( 'url' );
    157157                        error = control.model.get( 'error' );
     
    161161                        }
    162162
    163163                        if ( ! attachmentId && attachmentUrl ) {
    164                                 isHostedEmbed = control.isHostedVideo( attachmentUrl );
    165                         }
    166 
    167                         if ( isHostedEmbed ) {
    168                                 control.fetchEmbed();
    169                                 poster = control.oembedResponses[ attachmentUrl ] ? control.oembedResponses[ attachmentUrl ].thumbnail_url : null;
     164                                canHostedVideoPlayByMEJS = control.isHostedVideo( attachmentUrl );
    170165                        }
    171166
    172167                        // Verify the selected attachment mime is supported.
     
    175170                                if ( ! _.contains( _.values( wp.media.view.settings.embedMimes ), mime ) ) {
    176171                                        error = 'unsupported_file_type';
    177172                                }
     173                        } else if ( ! attachmentId ) {
     174                                urlParser = document.createElement( 'a' );
     175                                urlParser.href = attachmentUrl;
     176                                matches = urlParser.pathname.toLowerCase().match( /\.(\w+)$/ );
     177                                if ( matches ) {
     178                                        if ( ! _.contains( _.keys( wp.media.view.settings.embedMimes ), matches[1] ) ) {
     179                                                error = 'unsupported_file_type';
     180                                        }
     181                                } else {
     182                                        isOEmbed = true;
     183                                }
     184                        }
     185
     186                        if ( isOEmbed ) {
     187                                control.fetchEmbed();
     188                                if ( control.oembedResponses[ attachmentUrl ] ) {
     189                                        html = control.oembedResponses[ attachmentUrl ].html.replace( /\swidth="\d+"/, ' width="100%"' ).replace( /\sheight="\d+"/, '' );
     190                                }
    178191                        }
    179192
    180193                        previewContainer = control.$el.find( '.media-widget-preview' );
     
    182195
    183196                        previewContainer.html( previewTemplate({
    184197                                model: {
    185                                         attachment_id: control.model.get( 'attachment_id' ),
     198                                        attachment_id: attachmentId,
    186199                                        src: attachmentUrl,
    187                                         poster: poster
     200                                        html: html
    188201                                },
    189                                 is_hosted_embed: isHostedEmbed,
     202                                is_hosted_embed: canHostedVideoPlayByMEJS,
     203                                is_oembed: isOEmbed,
    190204                                error: error
    191205                        }));
    192206                        wp.mediaelement.initialize();
  • src/wp-admin/js/widgets/media-widgets.js

     
    184184                                                        return;
    185185                                                }
    186186
    187                                                 // If video, test for Vimeo and YouTube, otherwise, renderFail(). This should be removed once #34115 is resolved.
    188                                                 if ( 'video' === this.controller.options.mimeType && ! /vimeo|youtu\.?be/.test( urlParser.host ) ) {
    189                                                         embedLinkView.renderFail();
    190                                                         return;
    191                                                 }
    192 
    193187                                                // Support YouTube embed links.
    194188                                                url = embedLinkView.model.get( 'url' );
    195189                                                re = /https?:\/\/www\.youtube\.com\/embed\/([^/]+)/;
  • src/wp-includes/widgets/class-wp-widget-media-video.php

     
    121121                        return;
    122122                }
    123123
    124                 add_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
     124                $yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
     125                $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#';
     126                if ( ! $attachment && ! preg_match( $yt_pattern, $src ) && ! preg_match( $vimeo_pattern, $src ) ) {
     127                        echo $this->inject_video_max_width_style( wp_oembed_get( $src ) );
     128
     129                } else {
     130                        add_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
     131
     132                        echo wp_video_shortcode(
     133                                array_merge(
     134                                        $instance,
     135                                        compact( 'src' )
     136                                ),
     137                                $instance['content']
     138                        );
    125139
    126                 echo wp_video_shortcode(
    127                         array_merge(
    128                                 $instance,
    129                                 compact( 'src' )
    130                         ),
    131                         $instance['content']
    132                 );
    133 
    134                 remove_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
     140                        remove_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
     141                }
    135142        }
    136143
    137144        /**
     
    227234                                <div class="notice notice-error notice-alt">
    228235                                        <p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
    229236                                </div>
    230                         <# } else if ( data.is_hosted_embed && data.model.poster ) { #>
    231                                 <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link">
    232                                         <img src="{{ data.model.poster }}" />
    233                                 </a>
    234                         <# } else if ( data.is_hosted_embed ) { #>
    235                                 <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link no-poster">
    236                                         <span class="dashicons dashicons-format-video"></span>
    237                                 </a>
     237                        <# } else if ( data.is_oembed ) { #>
     238                                {{{ data.model.html }}}
    238239                        <# } else if ( data.model.src ) { #>
    239240                                <?php wp_underscore_video_template() ?>
    240241                        <# } #>