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

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