class-wp-widget-media-video.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Widget API: WP_Widget_Media_Video class
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 4.8.0
  8. */
  9. /**
  10. * Core class that implements a video widget.
  11. *
  12. * @since 4.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Media_Video extends WP_Widget_Media {
  17. /**
  18. * Constructor.
  19. *
  20. * @since 4.8.0
  21. */
  22. public function __construct() {
  23. parent::__construct( 'media_video', __( 'Video' ), array(
  24. 'description' => __( 'Displays a video from the media library or from YouTube, Vimeo, or another provider.' ),
  25. 'mime_type' => 'video',
  26. ) );
  27. $this->l10n = array_merge( $this->l10n, array(
  28. 'no_media_selected' => __( 'No video selected' ),
  29. 'add_media' => _x( 'Add Video', 'label for button in the video widget' ),
  30. 'replace_media' => _x( 'Replace Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
  31. 'edit_media' => _x( 'Edit Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
  32. 'missing_attachment' => sprintf(
  33. /* translators: %s: URL to media library */
  34. __( 'We can&#8217;t find that video. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
  35. esc_url( admin_url( 'upload.php' ) )
  36. ),
  37. /* translators: %d: widget count */
  38. 'media_library_state_multi' => _n_noop( 'Video Widget (%d)', 'Video Widget (%d)' ),
  39. 'media_library_state_single' => __( 'Video Widget' ),
  40. /* translators: %s: a list of valid video file extensions */
  41. 'unsupported_file_type' => sprintf( __( 'Sorry, we can&#8217;t load the video at the supplied URL. Please check that the URL is for a supported video file (%s) or stream (e.g. YouTube and Vimeo).' ), '<code>.' . implode( '</code>, <code>.', wp_get_video_extensions() ) . '</code>' ),
  42. ) );
  43. }
  44. /**
  45. * Get schema for properties of a widget instance (item).
  46. *
  47. * @since 4.8.0
  48. *
  49. * @see WP_REST_Controller::get_item_schema()
  50. * @see WP_REST_Controller::get_additional_fields()
  51. * @link https://core.trac.wordpress.org/ticket/35574
  52. * @return array Schema for properties.
  53. */
  54. public function get_instance_schema() {
  55. $schema = array_merge(
  56. parent::get_instance_schema(),
  57. array(
  58. 'preload' => array(
  59. 'type' => 'string',
  60. 'enum' => array( 'none', 'auto', 'metadata' ),
  61. 'default' => 'metadata',
  62. 'description' => __( 'Preload' ),
  63. 'should_preview_update' => false,
  64. ),
  65. 'loop' => array(
  66. 'type' => 'boolean',
  67. 'default' => false,
  68. 'description' => __( 'Loop' ),
  69. 'should_preview_update' => false,
  70. ),
  71. 'content' => array(
  72. 'type' => 'string',
  73. 'default' => '',
  74. 'sanitize_callback' => 'wp_kses_post',
  75. 'description' => __( 'Tracks (subtitles, captions, descriptions, chapters, or metadata)' ),
  76. 'should_preview_update' => false,
  77. ),
  78. )
  79. );
  80. foreach ( wp_get_video_extensions() as $video_extension ) {
  81. $schema[ $video_extension ] = array(
  82. 'type' => 'string',
  83. 'default' => '',
  84. 'format' => 'uri',
  85. /* translators: %s: video extension */
  86. 'description' => sprintf( __( 'URL to the %s video source file' ), $video_extension ),
  87. );
  88. }
  89. return $schema;
  90. }
  91. /**
  92. * Render the media on the frontend.
  93. *
  94. * @since 4.8.0
  95. *
  96. * @param array $instance Widget instance props.
  97. *
  98. * @return void
  99. */
  100. public function render_media( $instance ) {
  101. $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
  102. $attachment = null;
  103. if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
  104. $attachment = get_post( $instance['attachment_id'] );
  105. }
  106. $src = $instance['url'];
  107. if ( $attachment ) {
  108. $src = wp_get_attachment_url( $attachment->ID );
  109. }
  110. if ( empty( $src ) ) {
  111. return;
  112. }
  113. $youtube_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
  114. $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#';
  115. if ( $attachment || preg_match( $youtube_pattern, $src ) || preg_match( $vimeo_pattern, $src ) ) {
  116. add_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
  117. echo wp_video_shortcode(
  118. array_merge(
  119. $instance,
  120. compact( 'src' )
  121. ),
  122. $instance['content']
  123. );
  124. remove_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
  125. } else {
  126. echo $this->inject_video_max_width_style( wp_oembed_get( $src ) );
  127. }
  128. }
  129. /**
  130. * Inject max-width and remove height for videos too constrained to fit inside sidebars on frontend.
  131. *
  132. * @since 4.8.0
  133. *
  134. * @param string $html Video shortcode HTML output.
  135. * @return string HTML Output.
  136. */
  137. public function inject_video_max_width_style( $html ) {
  138. $html = preg_replace( '/\sheight="\d+"/', '', $html );
  139. $html = preg_replace( '/\swidth="\d+"/', '', $html );
  140. $html = preg_replace( '/(?<=width:)\s*\d+px(?=;?)/', '100%', $html );
  141. return $html;
  142. }
  143. /**
  144. * Enqueue preview scripts.
  145. *
  146. * These scripts normally are enqueued just-in-time when a video shortcode is used.
  147. * In the customizer, however, widgets can be dynamically added and rendered via
  148. * selective refresh, and so it is important to unconditionally enqueue them in
  149. * case a widget does get added.
  150. *
  151. * @since 4.8.0
  152. */
  153. public function enqueue_preview_scripts() {
  154. /** This filter is documented in wp-includes/media.php */
  155. if ( 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' ) ) {
  156. wp_enqueue_style( 'wp-mediaelement' );
  157. wp_enqueue_script( 'mediaelement-vimeo' );
  158. wp_enqueue_script( 'wp-mediaelement' );
  159. }
  160. }
  161. /**
  162. * Loads the required scripts and styles for the widget control.
  163. *
  164. * @since 4.8.0
  165. */
  166. public function enqueue_admin_scripts() {
  167. parent::enqueue_admin_scripts();
  168. $handle = 'media-video-widget';
  169. wp_enqueue_script( $handle );
  170. $exported_schema = array();
  171. foreach ( $this->get_instance_schema() as $field => $field_schema ) {
  172. $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
  173. }
  174. wp_add_inline_script(
  175. $handle,
  176. sprintf(
  177. 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
  178. wp_json_encode( $this->id_base ),
  179. wp_json_encode( $exported_schema )
  180. )
  181. );
  182. wp_add_inline_script(
  183. $handle,
  184. sprintf(
  185. '
  186. wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
  187. wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
  188. ',
  189. wp_json_encode( $this->id_base ),
  190. wp_json_encode( $this->widget_options['mime_type'] ),
  191. wp_json_encode( $this->l10n )
  192. )
  193. );
  194. }
  195. /**
  196. * Render form template scripts.
  197. *
  198. * @since 4.8.0
  199. */
  200. public function render_control_template_scripts() {
  201. parent::render_control_template_scripts()
  202. ?>
  203. <script type="text/html" id="tmpl-wp-media-widget-video-preview">
  204. <# if ( data.error && 'missing_attachment' === data.error ) { #>
  205. <div class="notice notice-error notice-alt notice-missing-attachment">
  206. <p><?php echo $this->l10n['missing_attachment']; ?></p>
  207. </div>
  208. <# } else if ( data.error && 'unsupported_file_type' === data.error ) { #>
  209. <div class="notice notice-error notice-alt notice-missing-attachment">
  210. <p><?php echo $this->l10n['unsupported_file_type']; ?></p>
  211. </div>
  212. <# } else if ( data.error ) { #>
  213. <div class="notice notice-error notice-alt">
  214. <p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
  215. </div>
  216. <# } else if ( data.is_oembed && data.model.poster ) { #>
  217. <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link">
  218. <img src="{{ data.model.poster }}" />
  219. </a>
  220. <# } else if ( data.is_oembed ) { #>
  221. <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link no-poster">
  222. <span class="dashicons dashicons-format-video"></span>
  223. </a>
  224. <# } else if ( data.model.src ) { #>
  225. <?php wp_underscore_video_template() ?>
  226. <# } #>
  227. </script>
  228. <?php
  229. }
  230. }