WordPress 4.4
[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                 $this->button_labels = array(
59                         'select'       => __( 'Select File' ),
60                         'change'       => __( 'Change File' ),
61                         'default'      => __( 'Default' ),
62                         'remove'       => __( 'Remove' ),
63                         'placeholder'  => __( 'No file selected' ),
64                         'frame_title'  => __( 'Select File' ),
65                         'frame_button' => __( 'Choose File' ),
66                 );
67         }
68
69         /**
70          * Enqueue control related scripts/styles.
71          *
72          * @since 3.4.0
73          * @since 4.2.0 Moved from WP_Customize_Upload_Control.
74          */
75         public function enqueue() {
76                 wp_enqueue_media();
77         }
78
79         /**
80          * Refresh the parameters passed to the JavaScript via JSON.
81          *
82          * @since 3.4.0
83          * @since 4.2.0 Moved from WP_Customize_Upload_Control.
84          *
85          * @see WP_Customize_Control::to_json()
86          */
87         public function to_json() {
88                 parent::to_json();
89                 $this->json['label'] = html_entity_decode( $this->label, ENT_QUOTES, get_bloginfo( 'charset' ) );
90                 $this->json['mime_type'] = $this->mime_type;
91                 $this->json['button_labels'] = $this->button_labels;
92                 $this->json['canUpload'] = current_user_can( 'upload_files' );
93
94                 $value = $this->value();
95
96                 if ( is_object( $this->setting ) ) {
97                         if ( $this->setting->default ) {
98                                 // Fake an attachment model - needs all fields used by template.
99                                 // Note that the default value must be a URL, NOT an attachment ID.
100                                 $type = in_array( substr( $this->setting->default, -3 ), array( 'jpg', 'png', 'gif', 'bmp' ) ) ? 'image' : 'document';
101                                 $default_attachment = array(
102                                         'id' => 1,
103                                         'url' => $this->setting->default,
104                                         'type' => $type,
105                                         'icon' => wp_mime_type_icon( $type ),
106                                         'title' => basename( $this->setting->default ),
107                                 );
108
109                                 if ( 'image' === $type ) {
110                                         $default_attachment['sizes'] = array(
111                                                 'full' => array( 'url' => $this->setting->default ),
112                                         );
113                                 }
114
115                                 $this->json['defaultAttachment'] = $default_attachment;
116                         }
117
118                         if ( $value && $this->setting->default && $value === $this->setting->default ) {
119                                 // Set the default as the attachment.
120                                 $this->json['attachment'] = $this->json['defaultAttachment'];
121                         } elseif ( $value ) {
122                                 $this->json['attachment'] = wp_prepare_attachment_for_js( $value );
123                         }
124                 }
125         }
126
127         /**
128          * Don't render any content for this control from PHP.
129          *
130          * @since 3.4.0
131          * @since 4.2.0 Moved from WP_Customize_Upload_Control.
132          *
133          * @see WP_Customize_Media_Control::content_template()
134          */
135         public function render_content() {}
136
137         /**
138          * Render a JS template for the content of the media control.
139          *
140          * @since 4.1.0
141          * @since 4.2.0 Moved from WP_Customize_Upload_Control.
142          */
143         public function content_template() {
144                 ?>
145                 <label for="{{ data.settings['default'] }}-button">
146                         <# if ( data.label ) { #>
147                                 <span class="customize-control-title">{{ data.label }}</span>
148                         <# } #>
149                         <# if ( data.description ) { #>
150                                 <span class="description customize-control-description">{{{ data.description }}}</span>
151                         <# } #>
152                 </label>
153
154                 <# if ( data.attachment && data.attachment.id ) { #>
155                         <div class="current">
156                                 <div class="container">
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>
192                                 </div>
193                         </div>
194                         <div class="actions">
195                                 <# if ( data.canUpload ) { #>
196                                 <button type="button" class="button remove-button"><?php echo $this->button_labels['remove']; ?></button>
197                                 <button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button"><?php echo $this->button_labels['change']; ?></button>
198                                 <div style="clear:both"></div>
199                                 <# } #>
200                         </div>
201                 <# } else { #>
202                         <div class="current">
203                                 <div class="container">
204                                         <div class="placeholder">
205                                                 <div class="inner">
206                                                         <span>
207                                                                 <?php echo $this->button_labels['placeholder']; ?>
208                                                         </span>
209                                                 </div>
210                                         </div>
211                                 </div>
212                         </div>
213                         <div class="actions">
214                                 <# if ( data.defaultAttachment ) { #>
215                                         <button type="button" class="button default-button"><?php echo $this->button_labels['default']; ?></button>
216                                 <# } #>
217                                 <# if ( data.canUpload ) { #>
218                                 <button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button"><?php echo $this->button_labels['select']; ?></button>
219                                 <# } #>
220                                 <div style="clear:both"></div>
221                         </div>
222                 <# } #>
223                 <?php
224         }
225 }