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

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