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

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