class-wp-customize-media-control.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. * Customize Media Control class.
  11. *
  12. * @since 4.2.0
  13. *
  14. * @see WP_Customize_Control
  15. */
  16. class WP_Customize_Media_Control extends WP_Customize_Control {
  17. /**
  18. * Control type.
  19. *
  20. * @since 4.2.0
  21. * @var string
  22. */
  23. public $type = 'media';
  24. /**
  25. * Media control mime type.
  26. *
  27. * @since 4.2.0
  28. * @var string
  29. */
  30. public $mime_type = '';
  31. /**
  32. * Button labels.
  33. *
  34. * @since 4.2.0
  35. * @var array
  36. */
  37. public $button_labels = array();
  38. /**
  39. * Constructor.
  40. *
  41. * @since 4.1.0
  42. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  43. *
  44. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  45. * @param string $id Control ID.
  46. * @param array $args Optional. Arguments to override class property defaults.
  47. */
  48. public function __construct( $manager, $id, $args = array() ) {
  49. parent::__construct( $manager, $id, $args );
  50. $this->button_labels = wp_parse_args( $this->button_labels, $this->get_default_button_labels() );
  51. }
  52. /**
  53. * Enqueue control related scripts/styles.
  54. *
  55. * @since 3.4.0
  56. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  57. */
  58. public function enqueue() {
  59. wp_enqueue_media();
  60. }
  61. /**
  62. * Refresh the parameters passed to the JavaScript via JSON.
  63. *
  64. * @since 3.4.0
  65. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  66. *
  67. * @see WP_Customize_Control::to_json()
  68. */
  69. public function to_json() {
  70. parent::to_json();
  71. $this->json['label'] = html_entity_decode( $this->label, ENT_QUOTES, get_bloginfo( 'charset' ) );
  72. $this->json['mime_type'] = $this->mime_type;
  73. $this->json['button_labels'] = $this->button_labels;
  74. $this->json['canUpload'] = current_user_can( 'upload_files' );
  75. $value = $this->value();
  76. if ( is_object( $this->setting ) ) {
  77. if ( $this->setting->default ) {
  78. // Fake an attachment model - needs all fields used by template.
  79. // Note that the default value must be a URL, NOT an attachment ID.
  80. $type = in_array( substr( $this->setting->default, -3 ), array( 'jpg', 'png', 'gif', 'bmp' ) ) ? 'image' : 'document';
  81. $default_attachment = array(
  82. 'id' => 1,
  83. 'url' => $this->setting->default,
  84. 'type' => $type,
  85. 'icon' => wp_mime_type_icon( $type ),
  86. 'title' => wp_basename( $this->setting->default ),
  87. );
  88. if ( 'image' === $type ) {
  89. $default_attachment['sizes'] = array(
  90. 'full' => array( 'url' => $this->setting->default ),
  91. );
  92. }
  93. $this->json['defaultAttachment'] = $default_attachment;
  94. }
  95. if ( $value && $this->setting->default && $value === $this->setting->default ) {
  96. // Set the default as the attachment.
  97. $this->json['attachment'] = $this->json['defaultAttachment'];
  98. } elseif ( $value ) {
  99. $this->json['attachment'] = wp_prepare_attachment_for_js( $value );
  100. }
  101. }
  102. }
  103. /**
  104. * Don't render any content for this control from PHP.
  105. *
  106. * @since 3.4.0
  107. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  108. *
  109. * @see WP_Customize_Media_Control::content_template()
  110. */
  111. public function render_content() {}
  112. /**
  113. * Render a JS template for the content of the media control.
  114. *
  115. * @since 4.1.0
  116. * @since 4.2.0 Moved from WP_Customize_Upload_Control.
  117. */
  118. public function content_template() {
  119. ?>
  120. <#
  121. var descriptionId = _.uniqueId( 'customize-media-control-description-' );
  122. var describedByAttr = data.description ? ' aria-describedby="' + descriptionId + '" ' : '';
  123. #>
  124. <# if ( data.label ) { #>
  125. <span class="customize-control-title">{{ data.label }}</span>
  126. <# } #>
  127. <div class="customize-control-notifications-container"></div>
  128. <# if ( data.description ) { #>
  129. <span id="{{ descriptionId }}" class="description customize-control-description">{{{ data.description }}}</span>
  130. <# } #>
  131. <# if ( data.attachment && data.attachment.id ) { #>
  132. <div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
  133. <div class="thumbnail thumbnail-{{ data.attachment.type }}">
  134. <# if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.medium ) { #>
  135. <img class="attachment-thumb" src="{{ data.attachment.sizes.medium.url }}" draggable="false" alt="" />
  136. <# } else if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.full ) { #>
  137. <img class="attachment-thumb" src="{{ data.attachment.sizes.full.url }}" draggable="false" alt="" />
  138. <# } else if ( 'audio' === data.attachment.type ) { #>
  139. <# if ( data.attachment.image && data.attachment.image.src && data.attachment.image.src !== data.attachment.icon ) { #>
  140. <img src="{{ data.attachment.image.src }}" class="thumbnail" draggable="false" alt="" />
  141. <# } else { #>
  142. <img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" alt="" />
  143. <# } #>
  144. <p class="attachment-meta attachment-meta-title">&#8220;{{ data.attachment.title }}&#8221;</p>
  145. <# if ( data.attachment.album || data.attachment.meta.album ) { #>
  146. <p class="attachment-meta"><em>{{ data.attachment.album || data.attachment.meta.album }}</em></p>
  147. <# } #>
  148. <# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
  149. <p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
  150. <# } #>
  151. <audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
  152. <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
  153. </audio>
  154. <# } else if ( 'video' === data.attachment.type ) { #>
  155. <div class="wp-media-wrapper wp-video">
  156. <video controls="controls" class="wp-video-shortcode" preload="metadata"
  157. <# if ( data.attachment.image && data.attachment.image.src !== data.attachment.icon ) { #>poster="{{ data.attachment.image.src }}"<# } #>>
  158. <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
  159. </video>
  160. </div>
  161. <# } else { #>
  162. <img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" alt="" />
  163. <p class="attachment-title">{{ data.attachment.title }}</p>
  164. <# } #>
  165. </div>
  166. <div class="actions">
  167. <# if ( data.canUpload ) { #>
  168. <button type="button" class="button remove-button">{{ data.button_labels.remove }}</button>
  169. <button type="button" class="button upload-button control-focus" {{{ describedByAttr }}}>{{ data.button_labels.change }}</button>
  170. <# } #>
  171. </div>
  172. </div>
  173. <# } else { #>
  174. <div class="attachment-media-view">
  175. <# if ( data.canUpload ) { #>
  176. <button type="button" class="upload-button button-add-media" {{{ describedByAttr }}}>{{ data.button_labels.select }}</button>
  177. <# } #>
  178. <div class="actions">
  179. <# if ( data.defaultAttachment ) { #>
  180. <button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
  181. <# } #>
  182. </div>
  183. </div>
  184. <# } #>
  185. <?php
  186. }
  187. /**
  188. * Get default button labels.
  189. *
  190. * Provides an array of the default button labels based on the mime type of the current control.
  191. *
  192. * @since 4.9.0
  193. *
  194. * @return array An associative array of default button labels.
  195. */
  196. public function get_default_button_labels() {
  197. // Get just the mime type and strip the mime subtype if present.
  198. $mime_type = ! empty( $this->mime_type ) ? strtok( ltrim( $this->mime_type, '/' ), '/' ) : 'default';
  199. switch ( $mime_type ) {
  200. case 'video':
  201. return array(
  202. 'select' => __( 'Select video' ),
  203. 'change' => __( 'Change video' ),
  204. 'default' => __( 'Default' ),
  205. 'remove' => __( 'Remove' ),
  206. 'placeholder' => __( 'No video selected' ),
  207. 'frame_title' => __( 'Select video' ),
  208. 'frame_button' => __( 'Choose video' ),
  209. );
  210. case 'audio':
  211. return array(
  212. 'select' => __( 'Select audio' ),
  213. 'change' => __( 'Change audio' ),
  214. 'default' => __( 'Default' ),
  215. 'remove' => __( 'Remove' ),
  216. 'placeholder' => __( 'No audio selected' ),
  217. 'frame_title' => __( 'Select audio' ),
  218. 'frame_button' => __( 'Choose audio' ),
  219. );
  220. case 'image':
  221. return array(
  222. 'select' => __( 'Select image' ),
  223. 'site_icon' => __( 'Select site icon' ),
  224. 'change' => __( 'Change image' ),
  225. 'default' => __( 'Default' ),
  226. 'remove' => __( 'Remove' ),
  227. 'placeholder' => __( 'No image selected' ),
  228. 'frame_title' => __( 'Select image' ),
  229. 'frame_button' => __( 'Choose image' ),
  230. );
  231. default:
  232. return array(
  233. 'select' => __( 'Select file' ),
  234. 'change' => __( 'Change file' ),
  235. 'default' => __( 'Default' ),
  236. 'remove' => __( 'Remove' ),
  237. 'placeholder' => __( 'No file selected' ),
  238. 'frame_title' => __( 'Select file' ),
  239. 'frame_button' => __( 'Choose file' ),
  240. );
  241. } // End switch().
  242. }
  243. }