3 * Customize API: WP_Customize_Media_Control class
6 * @subpackage Customize
11 * Customize Media Control class.
15 * @see WP_Customize_Control
17 class WP_Customize_Media_Control extends WP_Customize_Control {
25 public $type = 'media';
28 * Media control mime type.
34 public $mime_type = '';
43 public $button_labels = array();
49 * @since 4.2.0 Moved from WP_Customize_Upload_Control.
51 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
52 * @param string $id Control ID.
53 * @param array $args Optional. Arguments to override class property defaults.
55 public function __construct( $manager, $id, $args = array() ) {
56 parent::__construct( $manager, $id, $args );
58 if ( ! ( $this instanceof WP_Customize_Image_Control ) ) {
59 $this->button_labels = wp_parse_args( $this->button_labels, array(
60 'select' => __( 'Select File' ),
61 'change' => __( 'Change File' ),
62 'default' => __( 'Default' ),
63 'remove' => __( 'Remove' ),
64 'placeholder' => __( 'No file selected' ),
65 'frame_title' => __( 'Select File' ),
66 'frame_button' => __( 'Choose File' ),
72 * Enqueue control related scripts/styles.
75 * @since 4.2.0 Moved from WP_Customize_Upload_Control.
77 public function enqueue() {
82 * Refresh the parameters passed to the JavaScript via JSON.
85 * @since 4.2.0 Moved from WP_Customize_Upload_Control.
87 * @see WP_Customize_Control::to_json()
89 public function to_json() {
91 $this->json['label'] = html_entity_decode( $this->label, ENT_QUOTES, get_bloginfo( 'charset' ) );
92 $this->json['mime_type'] = $this->mime_type;
93 $this->json['button_labels'] = $this->button_labels;
94 $this->json['canUpload'] = current_user_can( 'upload_files' );
96 $value = $this->value();
98 if ( is_object( $this->setting ) ) {
99 if ( $this->setting->default ) {
100 // Fake an attachment model - needs all fields used by template.
101 // Note that the default value must be a URL, NOT an attachment ID.
102 $type = in_array( substr( $this->setting->default, -3 ), array( 'jpg', 'png', 'gif', 'bmp' ) ) ? 'image' : 'document';
103 $default_attachment = array(
105 'url' => $this->setting->default,
107 'icon' => wp_mime_type_icon( $type ),
108 'title' => basename( $this->setting->default ),
111 if ( 'image' === $type ) {
112 $default_attachment['sizes'] = array(
113 'full' => array( 'url' => $this->setting->default ),
117 $this->json['defaultAttachment'] = $default_attachment;
120 if ( $value && $this->setting->default && $value === $this->setting->default ) {
121 // Set the default as the attachment.
122 $this->json['attachment'] = $this->json['defaultAttachment'];
123 } elseif ( $value ) {
124 $this->json['attachment'] = wp_prepare_attachment_for_js( $value );
130 * Don't render any content for this control from PHP.
133 * @since 4.2.0 Moved from WP_Customize_Upload_Control.
135 * @see WP_Customize_Media_Control::content_template()
137 public function render_content() {}
140 * Render a JS template for the content of the media control.
143 * @since 4.2.0 Moved from WP_Customize_Upload_Control.
145 public function content_template() {
147 <label for="{{ data.settings['default'] }}-button">
148 <# if ( data.label ) { #>
149 <span class="customize-control-title">{{ data.label }}</span>
151 <# if ( data.description ) { #>
152 <span class="description customize-control-description">{{{ data.description }}}</span>
156 <# if ( data.attachment && data.attachment.id ) { #>
157 <div class="current">
158 <div class="container">
159 <div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
160 <div class="thumbnail thumbnail-{{ data.attachment.type }}">
161 <# if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.medium ) { #>
162 <img class="attachment-thumb" src="{{ data.attachment.sizes.medium.url }}" draggable="false" alt="" />
163 <# } else if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.full ) { #>
164 <img class="attachment-thumb" src="{{ data.attachment.sizes.full.url }}" draggable="false" alt="" />
165 <# } else if ( 'audio' === data.attachment.type ) { #>
166 <# if ( data.attachment.image && data.attachment.image.src && data.attachment.image.src !== data.attachment.icon ) { #>
167 <img src="{{ data.attachment.image.src }}" class="thumbnail" draggable="false" alt="" />
169 <img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" alt="" />
171 <p class="attachment-meta attachment-meta-title">“{{ data.attachment.title }}”</p>
172 <# if ( data.attachment.album || data.attachment.meta.album ) { #>
173 <p class="attachment-meta"><em>{{ data.attachment.album || data.attachment.meta.album }}</em></p>
175 <# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
176 <p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
178 <audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
179 <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
181 <# } else if ( 'video' === data.attachment.type ) { #>
182 <div class="wp-media-wrapper wp-video">
183 <video controls="controls" class="wp-video-shortcode" preload="metadata"
184 <# if ( data.attachment.image && data.attachment.image.src !== data.attachment.icon ) { #>poster="{{ data.attachment.image.src }}"<# } #>>
185 <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
189 <img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" alt="" />
190 <p class="attachment-title">{{ data.attachment.title }}</p>
196 <div class="actions">
197 <# if ( data.canUpload ) { #>
198 <button type="button" class="button remove-button">{{ data.button_labels.remove }}</button>
199 <button type="button" class="button upload-button control-focus" id="{{ data.settings['default'] }}-button">{{ data.button_labels.change }}</button>
200 <div style="clear:both"></div>
204 <div class="current">
205 <div class="container">
206 <div class="placeholder">
209 {{ data.button_labels.placeholder }}
215 <div class="actions">
216 <# if ( data.defaultAttachment ) { #>
217 <button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
219 <# if ( data.canUpload ) { #>
220 <button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button">{{ data.button_labels.select }}</button>
222 <div style="clear:both"></div>