Make WordPress Core

Changeset 40810


Ignore:
Timestamp:
05/20/2017 04:17:33 AM (9 years ago)
Author:
westonruter
Message:

Widgets: Introduce isHostedVideo method on VideoWidgetControl to allow plugins to extend for recognizing services beyond YouTube and Vimeo.

Also update jshint configuration in Gruntfile to include the widget scripts among the JS files linted.

Props timmydcrawford.
See #39994.
Fixes #40808.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Gruntfile.js

    r40631 r40810  
    330330                                cwd: SOURCE_DIR,
    331331                                src: [
    332                                         'wp-admin/js/*.js',
     332                                        'wp-admin/js/**/*.js',
    333333                                        'wp-includes/js/*.js',
    334334                                        // Built scripts.
  • trunk/src/wp-admin/js/widgets/media-video-widget.js

    r40656 r40810  
    136136
    137137                /**
     138                 * Whether a url is a supported external host.
     139                 *
     140                 * @param {String} url - Video url.
     141                 * @returns {boolean} Whether url is a supported video host.
     142                 */
     143                isHostedVideo: function isHostedVideo( url ) {
     144                        var parsedUrl = document.createElement( 'a' );
     145                        parsedUrl.href = url;
     146                        return /vimeo|youtu\.?be/.test( parsedUrl.host );
     147                },
     148
     149                /**
    138150                 * Render preview.
    139151                 *
     
    151163
    152164                        if ( ! attachmentId && attachmentUrl ) {
    153                                 parsedUrl = document.createElement( 'a' );
    154                                 parsedUrl.href = attachmentUrl;
    155                                 isHostedEmbed = /vimeo|youtu\.?be/.test( parsedUrl.host );
     165                                isHostedEmbed = control.isHostedVideo( attachmentUrl );
    156166                        }
    157167
  • trunk/tests/qunit/wp-admin/js/widgets/test-media-video-widget.js

    r40640 r40810  
    3333                equal( mappedProps.loop, false, 'mapMediaToModelProps should set loop' );
    3434                equal( mappedProps.preload, 'meta', 'mapMediaToModelProps should set preload' );
     35
     36                // Test isHostedVideo().
     37                equal( videoWidgetControlInstance.isHostedVideo( 'https://www.youtube.com/watch?v=OQSNhk5ICTI' ), true, 'isHostedVideo should return true for full YouTube url.' );
     38                equal( videoWidgetControlInstance.isHostedVideo( 'https://youtu.be/OQSNhk5ICTI' ), true, 'isHostedVideo should return true for shortened youtube url' );
     39                equal( videoWidgetControlInstance.isHostedVideo( 'https://vimeo.com/190372437' ), true, 'isHostedVideo should return true for vimeo url.' );
     40                equal( videoWidgetControlInstance.isHostedVideo( 'https://wordpress.org/' ), false, 'isHostedVideo should return false for non-supported video url.' );
    3541        });
    3642
Note: See TracChangeset for help on using the changeset viewer.