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