/wp-includes/customize/class-wp-customize-media-control.php

https://bitbucket.org/skyarch-iijima/wordpress · PHP · 263 lines · 162 code · 17 blank · 84 comment · 9 complexity · 1860299cb2fcb56823b70966536198c3 MD5 · raw file

  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' => 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 selectButtonId = _.uniqueId( 'customize-media-control-button-' );
  122. var descriptionId = _.uniqueId( 'customize-media-control-description-' );
  123. var describedByAttr = data.description ? ' aria-describedby="' + descriptionId + '" ' : '';
  124. #>
  125. <# if ( data.label ) { #>
  126. <label class="customize-control-title" for="{{ selectButtonId }}">{{ data.label }}</label>
  127. <# } #>
  128. <div class="customize-control-notifications-container"></div>
  129. <# if ( data.description ) { #>
  130. <span id="{{ descriptionId }}" class="description customize-control-description">{{{ data.description }}}</span>
  131. <# } #>
  132. <# if ( data.attachment && data.attachment.id ) { #>
  133. <div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
  134. <div class="thumbnail thumbnail-{{ data.attachment.type }}">
  135. <# if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.medium ) { #>
  136. <img class="attachment-thumb" src="{{ data.attachment.sizes.medium.url }}" draggable="false" alt="" />
  137. <# } else if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.full ) { #>
  138. <img class="attachment-thumb" src="{{ data.attachment.sizes.full.url }}" draggable="false" alt="" />
  139. <# } else if ( 'audio' === data.attachment.type ) { #>
  140. <# if ( data.attachment.image && data.attachment.image.src && data.attachment.image.src !== data.attachment.icon ) { #>
  141. <img src="{{ data.attachment.image.src }}" class="thumbnail" draggable="false" alt="" />
  142. <# } else { #>
  143. <img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" alt="" />
  144. <# } #>
  145. <p class="attachment-meta attachment-meta-title">&#8220;{{ data.attachment.title }}&#8221;</p>
  146. <# if ( data.attachment.album || data.attachment.meta.album ) { #>
  147. <p class="attachment-meta"><em>{{ data.attachment.album || data.attachment.meta.album }}</em></p>
  148. <# } #>
  149. <# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
  150. <p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
  151. <# } #>
  152. <audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
  153. <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
  154. </audio>
  155. <# } else if ( 'video' === data.attachment.type ) { #>
  156. <div class="wp-media-wrapper wp-video">
  157. <video controls="controls" class="wp-video-shortcode" preload="metadata"
  158. <# if ( data.attachment.image && data.attachment.image.src !== data.attachment.icon ) { #>poster="{{ data.attachment.image.src }}"<# } #>>
  159. <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
  160. </video>
  161. </div>
  162. <# } else { #>
  163. <img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" alt="" />
  164. <p class="attachment-title">{{ data.attachment.title }}</p>
  165. <# } #>
  166. </div>
  167. <div class="actions">
  168. <# if ( data.canUpload ) { #>
  169. <button type="button" class="button remove-button">{{ data.button_labels.remove }}</button>
  170. <button type="button" class="button upload-button control-focus" id="{{ selectButtonId }}" {{{ describedByAttr }}}>{{ data.button_labels.change }}</button>
  171. <# } #>
  172. </div>
  173. </div>
  174. <# } else { #>
  175. <div class="attachment-media-view">
  176. <div class="placeholder">
  177. {{ data.button_labels.placeholder }}
  178. </div>
  179. <div class="actions">
  180. <# if ( data.defaultAttachment ) { #>
  181. <button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
  182. <# } #>
  183. <# if ( data.canUpload ) { #>
  184. <button type="button" class="button upload-button" id="{{ selectButtonId }}" {{{ describedByAttr }}}>{{ data.button_labels.select }}</button>
  185. <# } #>
  186. </div>
  187. </div>
  188. <# } #>
  189. <?php
  190. }
  191. /**
  192. * Get default button labels.
  193. *
  194. * Provides an array of the default button labels based on the mime type of the current control.
  195. *
  196. * @since 4.9.0
  197. *
  198. * @return array An associative array of default button labels.
  199. */
  200. public function get_default_button_labels() {
  201. // Get just the mime type and strip the mime subtype if present.
  202. $mime_type = ! empty( $this->mime_type ) ? strtok( ltrim( $this->mime_type, '/' ), '/' ) : 'default';
  203. switch ( $mime_type ) {
  204. case 'video':
  205. return array(
  206. 'select' => __( 'Select video' ),
  207. 'change' => __( 'Change video' ),
  208. 'default' => __( 'Default' ),
  209. 'remove' => __( 'Remove' ),
  210. 'placeholder' => __( 'No video selected' ),
  211. 'frame_title' => __( 'Select video' ),
  212. 'frame_button' => __( 'Choose video' ),
  213. );
  214. case 'audio':
  215. return array(
  216. 'select' => __( 'Select audio' ),
  217. 'change' => __( 'Change audio' ),
  218. 'default' => __( 'Default' ),
  219. 'remove' => __( 'Remove' ),
  220. 'placeholder' => __( 'No audio selected' ),
  221. 'frame_title' => __( 'Select audio' ),
  222. 'frame_button' => __( 'Choose audio' ),
  223. );
  224. case 'image':
  225. return array(
  226. 'select' => __( 'Select image' ),
  227. 'change' => __( 'Change image' ),
  228. 'default' => __( 'Default' ),
  229. 'remove' => __( 'Remove' ),
  230. 'placeholder' => __( 'No image selected' ),
  231. 'frame_title' => __( 'Select image' ),
  232. 'frame_button' => __( 'Choose image' ),
  233. );
  234. default:
  235. return array(
  236. 'select' => __( 'Select file' ),
  237. 'change' => __( 'Change file' ),
  238. 'default' => __( 'Default' ),
  239. 'remove' => __( 'Remove' ),
  240. 'placeholder' => __( 'No file selected' ),
  241. 'frame_title' => __( 'Select file' ),
  242. 'frame_button' => __( 'Choose file' ),
  243. );
  244. } // End switch().
  245. }
  246. }