]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/customize/class-wp-customize-media-control.php
Wordpress 4.5.3
[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="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="" />
168                                                                 <# } else { #>
169                                                                         <img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" alt="" />
170                                                                 <# } #>
171                                                                 <p class="attachment-meta attachment-meta-title">&#8220;{{ data.attachment.title }}&#8221;</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>
174                                                                 <# } #>
175                                                                 <# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
176                                                                 <p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
177                                                                 <# } #>
178                                                                 <audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
179                                                                         <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
180                                                                 </audio>
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 }}"/>
186                                                                         </video>
187                                                                 </div>
188                                                         <# } else { #>
189                                                                 <img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" alt="" />
190                                                                 <p class="attachment-title">{{ data.attachment.title }}</p>
191                                                         <# } #>
192                                                 </div>
193                                         </div>
194                                 </div>
195                         </div>
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>
201                                 <# } #>
202                         </div>
203                 <# } else { #>
204                         <div class="current">
205                                 <div class="container">
206                                         <div class="placeholder">
207                                                 <div class="inner">
208                                                         <span>
209                                                                 {{ data.button_labels.placeholder }}
210                                                         </span>
211                                                 </div>
212                                         </div>
213                                 </div>
214                         </div>
215                         <div class="actions">
216                                 <# if ( data.defaultAttachment ) { #>
217                                         <button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
218                                 <# } #>
219                                 <# if ( data.canUpload ) { #>
220                                 <button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button">{{ data.button_labels.select }}</button>
221                                 <# } #>
222                                 <div style="clear:both"></div>
223                         </div>
224                 <# } #>
225                 <?php
226         }
227 }