[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/customize/ -> class-wp-customize-media-control.php (source)

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


Generated : Sun Jun 21 08:20:10 2026 Cross-referenced by PHPXref