Wordpress 4.6
[autoinstalls/wordpress.git] / wp-includes / customize / class-wp-customize-media-control.php
1 <?php
2 /**
3  * Customize API: WP_Customize_Media_Control class
4  *
5  * @package WordPress
6  * @subpackage Customize
7  * @since 4.4.0
8  */
9
10 /**
11  * Customize Media Control class.
12  *
13  * @since 4.2.0
14  *
15  * @see WP_Customize_Control
16  */
17 class WP_Customize_Media_Control extends WP_Customize_Control {
18         /**
19          * Control type.
20          *
21          * @since 4.2.0
22          * @access public
23          * @var string
24          */
25         public $type = 'media';
26
27         /**
28          * Media control mime type.
29          *
30          * @since 4.2.0
31          * @access public
32          * @var string
33          */
34         public $mime_type = '';
35
36         /**
37          * Button labels.
38          *
39          * @since 4.2.0
40          * @access public
41          * @var array
42          */
43         public $button_labels = array();
44
45         /**
46          * Constructor.
47          *
48          * @since 4.1.0
49          * @since 4.2.0 Moved from WP_Customize_Upload_Control.
50          *
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.
54          */
55         public function __construct( $manager, $id, $args = array() ) {
56                 parent::__construct( $manager, $id, $args );
57
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' ),
67                         ) );
68                 }
69         }
70
71         /**
72          * Enqueue control related scripts/styles.
73          *
74          * @since 3.4.0
75          * @since 4.2.0 Moved from WP_Customize_Upload_Control.
76          */
77         public function enqueue() {
78                 wp_enqueue_media();
79         }
80
81         /**
82          * Refresh the parameters passed to the JavaScript via JSON.
83          *
84          * @since 3.4.0
85          * @since 4.2.0 Moved from WP_Customize_Upload_Control.
86          *
87          * @see WP_Customize_Control::to_json()
88          */
89         public function to_json() {
90                 parent::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' );
95
96                 $value = $this->value();
97
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(
104                                         'id' => 1,
105                                         'url' => $this->setting->default,
106                                         'type' => $type,
107                                         'icon' => wp_mime_type_icon( $type ),
108                                         'title' => basename( $this->setting->default ),
109                                 );
110
111                                 if ( 'image' === $type ) {
112                                         $default_attachment['sizes'] = array(
113                                                 'full' => array( 'url' => $this->setting->default ),
114                                         );
115                                 }
116
117                                 $this->json['defaultAttachment'] = $default_attachment;
118                         }
119
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 );
125                         }
126                 }
127         }
128
129         /**
130          * Don't render any content for this control from PHP.
131          *
132          * @since 3.4.0
133          * @since 4.2.0 Moved from WP_Customize_Upload_Control.
134          *
135          * @see WP_Customize_Media_Control::content_template()
136          */
137         public function render_content() {}
138
139         /**
140          * Render a JS template for the content of the media control.
141          *
142          * @since 4.1.0
143          * @since 4.2.0 Moved from WP_Customize_Upload_Control.
144          */
145         public function content_template() {
146                 ?>
147                 <label for="{{ data.settings['default'] }}-button">
148                         <# if ( data.label ) { #>
149                                 <span class="customize-control-title">{{ data.label }}</span>
150                         <# } #>
151                         <# if ( data.description ) { #>
152                                 <span class="description customize-control-description">{{{ data.description }}}</span>
153                         <# } #>
154                 </label>
155
156                 <# if ( data.attachment && data.attachment.id ) { #>
157                         <div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
158                                 <div class="thumbnail thumbnail-{{ data.attachment.type }}">
159                                         <# if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.medium ) { #>
160                                                 <img class="attachment-thumb" src="{{ data.attachment.sizes.medium.url }}" draggable="false" alt="" />
161                                         <# } else if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.full ) { #>
162                                                 <img class="attachment-thumb" src="{{ data.attachment.sizes.full.url }}" draggable="false" alt="" />
163                                         <# } else if ( 'audio' === data.attachment.type ) { #>
164                                                 <# if ( data.attachment.image && data.attachment.image.src && data.attachment.image.src !== data.attachment.icon ) { #>
165                                                         <img src="{{ data.attachment.image.src }}" class="thumbnail" draggable="false" alt="" />
166                                                 <# } else { #>
167                                                         <img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" alt="" />
168                                                 <# } #>
169                                                 <p class="attachment-meta attachment-meta-title">&#8220;{{ data.attachment.title }}&#8221;</p>
170                                                 <# if ( data.attachment.album || data.attachment.meta.album ) { #>
171                                                 <p class="attachment-meta"><em>{{ data.attachment.album || data.attachment.meta.album }}</em></p>
172                                                 <# } #>
173                                                 <# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
174                                                 <p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
175                                                 <# } #>
176                                                 <audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
177                                                         <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
178                                                 </audio>
179                                         <# } else if ( 'video' === data.attachment.type ) { #>
180                                                 <div class="wp-media-wrapper wp-video">
181                                                         <video controls="controls" class="wp-video-shortcode" preload="metadata"
182                                                                 <# if ( data.attachment.image && data.attachment.image.src !== data.attachment.icon ) { #>poster="{{ data.attachment.image.src }}"<# } #>>
183                                                                 <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
184                                                         </video>
185                                                 </div>
186                                         <# } else { #>
187                                                 <img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" alt="" />
188                                                 <p class="attachment-title">{{ data.attachment.title }}</p>
189                                         <# } #>
190                                 </div>
191                                 <div class="actions">
192                                         <# if ( data.canUpload ) { #>
193                                         <button type="button" class="button remove-button">{{ data.button_labels.remove }}</button>
194                                         <button type="button" class="button upload-button control-focus" id="{{ data.settings['default'] }}-button">{{ data.button_labels.change }}</button>
195                                         <div style="clear:both"></div>
196                                         <# } #>
197                                 </div>
198                         </div>
199                 <# } else { #>
200                         <div class="attachment-media-view">
201                                 <div class="placeholder">
202                                                 {{ data.button_labels.placeholder }}
203                                 </div>
204                                 <div class="actions">
205                                         <# if ( data.defaultAttachment ) { #>
206                                                 <button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
207                                         <# } #>
208                                         <# if ( data.canUpload ) { #>
209                                         <button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button">{{ data.button_labels.select }}</button>
210                                         <# } #>
211                                         <div style="clear:both"></div>
212                                 </div>
213                         </div>
214                 <# } #>
215                 <?php
216         }
217 }