class-wp-widget-media-audio.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Widget API: WP_Widget_Media_Audio class
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 4.8.0
  8. */
  9. /**
  10. * Core class that implements an audio widget.
  11. *
  12. * @since 4.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Media_Audio extends WP_Widget_Media {
  17. /**
  18. * Constructor.
  19. *
  20. * @since 4.8.0
  21. */
  22. public function __construct() {
  23. parent::__construct( 'media_audio', __( 'Audio' ), array(
  24. 'description' => __( 'Displays an audio player.' ),
  25. 'mime_type' => 'audio',
  26. ) );
  27. $this->l10n = array_merge( $this->l10n, array(
  28. 'no_media_selected' => __( 'No audio selected' ),
  29. 'add_media' => _x( 'Add Audio', 'label for button in the audio widget' ),
  30. 'replace_media' => _x( 'Replace Audio', 'label for button in the audio widget; should preferably not be longer than ~13 characters long' ),
  31. 'edit_media' => _x( 'Edit Audio', 'label for button in the audio 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 audio file. 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( 'Audio Widget (%d)', 'Audio Widget (%d)' ),
  39. 'media_library_state_single' => __( 'Audio Widget' ),
  40. 'unsupported_file_type' => __( 'Looks like this isn&#8217;t the correct kind of file. Please link to an audio file instead.' ),
  41. ) );
  42. }
  43. /**
  44. * Get schema for properties of a widget instance (item).
  45. *
  46. * @since 4.8.0
  47. *
  48. * @see WP_REST_Controller::get_item_schema()
  49. * @see WP_REST_Controller::get_additional_fields()
  50. * @link https://core.trac.wordpress.org/ticket/35574
  51. * @return array Schema for properties.
  52. */
  53. public function get_instance_schema() {
  54. $schema = array_merge(
  55. parent::get_instance_schema(),
  56. array(
  57. 'preload' => array(
  58. 'type' => 'string',
  59. 'enum' => array( 'none', 'auto', 'metadata' ),
  60. 'default' => 'none',
  61. 'description' => __( 'Preload' ),
  62. ),
  63. 'loop' => array(
  64. 'type' => 'boolean',
  65. 'default' => false,
  66. 'description' => __( 'Loop' ),
  67. ),
  68. )
  69. );
  70. foreach ( wp_get_audio_extensions() as $audio_extension ) {
  71. $schema[ $audio_extension ] = array(
  72. 'type' => 'string',
  73. 'default' => '',
  74. 'format' => 'uri',
  75. /* translators: %s: audio extension */
  76. 'description' => sprintf( __( 'URL to the %s audio source file' ), $audio_extension ),
  77. );
  78. }
  79. return $schema;
  80. }
  81. /**
  82. * Render the media on the frontend.
  83. *
  84. * @since 4.8.0
  85. *
  86. * @param array $instance Widget instance props.
  87. * @return void
  88. */
  89. public function render_media( $instance ) {
  90. $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
  91. $attachment = null;
  92. if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
  93. $attachment = get_post( $instance['attachment_id'] );
  94. }
  95. if ( $attachment ) {
  96. $src = wp_get_attachment_url( $attachment->ID );
  97. } else {
  98. $src = $instance['url'];
  99. }
  100. echo wp_audio_shortcode(
  101. array_merge(
  102. $instance,
  103. compact( 'src' )
  104. )
  105. );
  106. }
  107. /**
  108. * Enqueue preview scripts.
  109. *
  110. * These scripts normally are enqueued just-in-time when an audio shortcode is used.
  111. * In the customizer, however, widgets can be dynamically added and rendered via
  112. * selective refresh, and so it is important to unconditionally enqueue them in
  113. * case a widget does get added.
  114. *
  115. * @since 4.8.0
  116. */
  117. public function enqueue_preview_scripts() {
  118. /** This filter is documented in wp-includes/media.php */
  119. if ( 'mediaelement' === apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ) ) {
  120. wp_enqueue_style( 'wp-mediaelement' );
  121. wp_enqueue_script( 'wp-mediaelement' );
  122. }
  123. }
  124. /**
  125. * Loads the required media files for the media manager and scripts for media widgets.
  126. *
  127. * @since 4.8.0
  128. */
  129. public function enqueue_admin_scripts() {
  130. parent::enqueue_admin_scripts();
  131. wp_enqueue_style( 'wp-mediaelement' );
  132. wp_enqueue_script( 'wp-mediaelement' );
  133. $handle = 'media-audio-widget';
  134. wp_enqueue_script( $handle );
  135. $exported_schema = array();
  136. foreach ( $this->get_instance_schema() as $field => $field_schema ) {
  137. $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
  138. }
  139. wp_add_inline_script(
  140. $handle,
  141. sprintf(
  142. 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
  143. wp_json_encode( $this->id_base ),
  144. wp_json_encode( $exported_schema )
  145. )
  146. );
  147. wp_add_inline_script(
  148. $handle,
  149. sprintf(
  150. '
  151. wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
  152. wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
  153. ',
  154. wp_json_encode( $this->id_base ),
  155. wp_json_encode( $this->widget_options['mime_type'] ),
  156. wp_json_encode( $this->l10n )
  157. )
  158. );
  159. }
  160. /**
  161. * Render form template scripts.
  162. *
  163. * @since 4.8.0
  164. */
  165. public function render_control_template_scripts() {
  166. parent::render_control_template_scripts()
  167. ?>
  168. <script type="text/html" id="tmpl-wp-media-widget-audio-preview">
  169. <# if ( data.error && 'missing_attachment' === data.error ) { #>
  170. <div class="notice notice-error notice-alt notice-missing-attachment">
  171. <p><?php echo $this->l10n['missing_attachment']; ?></p>
  172. </div>
  173. <# } else if ( data.error ) { #>
  174. <div class="notice notice-error notice-alt">
  175. <p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
  176. </div>
  177. <# } else if ( data.model && data.model.src ) { #>
  178. <?php wp_underscore_audio_template() ?>
  179. <# } #>
  180. </script>
  181. <?php
  182. }
  183. }