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

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