3
* Widget API: WP_Widget_Media_Video class
11
* Core class that implements a video widget.
17
class WP_Widget_Media_Video extends WP_Widget_Media {
25
public function __construct() {
26
parent::__construct( 'media_video', __( 'Video' ), array(
27
'description' => __( 'Displays a video from the media library or from YouTube, Vimeo, or another provider.' ),
28
'mime_type' => 'video',
31
$this->l10n = array_merge( $this->l10n, array(
32
'no_media_selected' => __( 'No video selected' ),
33
'add_media' => _x( 'Add Video', 'label for button in the video widget' ),
34
'replace_media' => _x( 'Replace Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
35
'edit_media' => _x( 'Edit Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
36
'missing_attachment' => sprintf(
37
/* translators: placeholder is URL to media library */
38
__( 'We can’t find that video. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ),
39
esc_url( admin_url( 'upload.php' ) )
41
/* translators: %d is widget count */
42
'media_library_state_multi' => _n_noop( 'Video Widget (%d)', 'Video Widget (%d)' ),
43
'media_library_state_single' => __( 'Video Widget' ),
44
/* translators: placeholder is a list of valid video file extensions */
45
'unsupported_file_type' => sprintf( __( 'Sorry, we can’t display the video file type selected. Please select a supported video file (%1$s) or stream (YouTube or Vimeo) instead.' ), '<code>.' . implode( '</code>, <code>.', wp_get_video_extensions() ) . '</code>' ),
50
* Get schema for properties of a widget instance (item).
55
* @see WP_REST_Controller::get_item_schema()
56
* @see WP_REST_Controller::get_additional_fields()
57
* @link https://core.trac.wordpress.org/ticket/35574
58
* @return array Schema for properties.
60
public function get_instance_schema() {
61
$schema = array_merge(
62
parent::get_instance_schema(),
66
'enum' => array( 'none', 'auto', 'metadata' ),
67
'default' => 'metadata',
68
'description' => __( 'Preload' ),
69
'should_preview_update' => false,
74
'description' => __( 'Loop' ),
75
'should_preview_update' => false,
80
'sanitize_callback' => 'wp_kses_post',
81
'description' => __( 'Tracks (subtitles, captions, descriptions, chapters, or metadata)' ),
82
'should_preview_update' => false,
87
foreach ( wp_get_video_extensions() as $video_extension ) {
88
$schema[ $video_extension ] = array(
92
/* translators: placeholder is video extension */
93
'description' => sprintf( __( 'URL to the %s video source file' ), $video_extension ),
101
* Render the media on the frontend.
106
* @param array $instance Widget instance props.
110
public function render_media( $instance ) {
111
$instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
114
if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
115
$attachment = get_post( $instance['attachment_id'] );
119
$src = wp_get_attachment_url( $attachment->ID );
122
// Manually add the loop query argument.
123
$loop = $instance['loop'] ? '1' : '0';
124
$src = empty( $instance['url'] ) ? $instance['url'] : add_query_arg( 'loop', $loop, $instance['url'] );
127
if ( empty( $src ) ) {
131
add_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
133
echo wp_video_shortcode(
141
remove_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
145
* Inject max-width and remove height for videos too constrained to fit inside sidebars on frontend.
150
* @param string $html Video shortcode HTML output.
151
* @return string HTML Output.
153
public function inject_video_max_width_style( $html ) {
154
$html = preg_replace( '/\sheight="\d+"/', '', $html );
155
$html = preg_replace( '/\swidth="\d+"/', '', $html );
156
$html = preg_replace( '/(?<=width:)\s*\d+px(?=;?)/', '100%', $html );
161
* Enqueue preview scripts.
163
* These scripts normally are enqueued just-in-time when a video shortcode is used.
164
* In the customizer, however, widgets can be dynamically added and rendered via
165
* selective refresh, and so it is important to unconditionally enqueue them in
166
* case a widget does get added.
171
public function enqueue_preview_scripts() {
172
/** This filter is documented in wp-includes/media.php */
173
if ( 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' ) ) {
174
wp_enqueue_style( 'wp-mediaelement' );
175
wp_enqueue_script( 'wp-mediaelement' );
178
// Enqueue script needed by Vimeo; see wp_video_shortcode().
179
wp_enqueue_script( 'froogaloop' );
183
* Loads the required scripts and styles for the widget control.
188
public function enqueue_admin_scripts() {
189
parent::enqueue_admin_scripts();
191
$handle = 'media-video-widget';
192
wp_enqueue_script( $handle );
194
$exported_schema = array();
195
foreach ( $this->get_instance_schema() as $field => $field_schema ) {
196
$exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
198
wp_add_inline_script(
201
'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
202
wp_json_encode( $this->id_base ),
203
wp_json_encode( $exported_schema )
207
wp_add_inline_script(
211
wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
212
wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
214
wp_json_encode( $this->id_base ),
215
wp_json_encode( $this->widget_options['mime_type'] ),
216
wp_json_encode( $this->l10n )
222
* Render form template scripts.
227
public function render_control_template_scripts() {
228
parent::render_control_template_scripts()
230
<script type="text/html" id="tmpl-wp-media-widget-video-preview">
231
<# if ( data.error && 'missing_attachment' === data.error ) { #>
232
<div class="notice notice-error notice-alt notice-missing-attachment">
233
<p><?php echo $this->l10n['missing_attachment']; ?></p>
235
<# } else if ( data.error && 'unsupported_file_type' === data.error ) { #>
236
<div class="notice notice-error notice-alt notice-missing-attachment">
237
<p><?php echo $this->l10n['unsupported_file_type']; ?></p>
239
<# } else if ( data.error ) { #>
240
<div class="notice notice-error notice-alt">
241
<p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
243
<# } else if ( data.is_hosted_embed && data.model.poster ) { #>
244
<a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link">
245
<img src="{{ data.model.poster }}" />
247
<# } else if ( data.is_hosted_embed ) { #>
248
<a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link no-poster">
249
<span class="dashicons dashicons-format-video"></span>
251
<# } else if ( data.model.src ) { #>
252
<?php wp_underscore_video_template() ?>