WP_Customize_Media_Control

Customize Media Control class.

Defined (1)

The class is defined in the following location(s).

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