[ 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  = substr( $this->setting->default, -3 );
  96                  $type = in_array( $ext, array( 'jpg', 'png', 'gif', 'bmp', 'webp', 'avif' ), true ) ? 'image' : 'document';
  97  
  98                  $default_attachment = array(
  99                      'id'    => 1,
 100                      'url'   => $this->setting->default,
 101                      'type'  => $type,
 102                      'icon'  => wp_mime_type_icon( $type, '.svg' ),
 103                      'title' => wp_basename( $this->setting->default ),
 104                  );
 105  
 106                  if ( 'image' === $type ) {
 107                      $default_attachment['sizes'] = array(
 108                          'full' => array( 'url' => $this->setting->default ),
 109                      );
 110                  }
 111  
 112                  $this->json['defaultAttachment'] = $default_attachment;
 113              }
 114  
 115              if ( $value && $this->setting->default && $value === $this->setting->default ) {
 116                  // Set the default as the attachment.
 117                  $this->json['attachment'] = $this->json['defaultAttachment'];
 118              } elseif ( $value ) {
 119                  $this->json['attachment'] = wp_prepare_attachment_for_js( $value );
 120              }
 121          }
 122      }
 123  
 124      /**
 125       * Don't render any content for this control from PHP.
 126       *
 127       * @since 3.4.0
 128       * @since 4.2.0 Moved from WP_Customize_Upload_Control.
 129       *
 130       * @see WP_Customize_Media_Control::content_template()
 131       */
 132  	public function render_content() {}
 133  
 134      /**
 135       * Render a JS template for the content of the media control.
 136       *
 137       * @since 4.1.0
 138       * @since 4.2.0 Moved from WP_Customize_Upload_Control.
 139       */
 140  	public function content_template() {
 141          ?>
 142          <#
 143          var descriptionId = _.uniqueId( 'customize-media-control-description-' );
 144          var describedByAttr = data.description ? ' aria-describedby="' + descriptionId + '" ' : '';
 145          #>
 146          <# if ( data.label ) { #>
 147              <span class="customize-control-title">{{ data.label }}</span>
 148          <# } #>
 149          <div class="customize-control-notifications-container"></div>
 150          <# if ( data.description ) { #>
 151              <span id="{{ descriptionId }}" class="description customize-control-description">{{{ data.description }}}</span>
 152          <# } #>
 153  
 154          <# if ( data.attachment && data.attachment.id ) { #>
 155              <div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
 156                  <div class="thumbnail thumbnail-{{ data.attachment.type }}">
 157                      <# if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.medium ) { #>
 158                          <img class="attachment-thumb" src="{{ data.attachment.sizes.medium.url }}" draggable="false" alt="" />
 159                      <# } else if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.full ) { #>
 160                          <img class="attachment-thumb" src="{{ data.attachment.sizes.full.url }}" draggable="false" alt="" />
 161                      <# } else if ( 'audio' === data.attachment.type ) { #>
 162                          <# if ( data.attachment.image && data.attachment.image.src && data.attachment.image.src !== data.attachment.icon ) { #>
 163                              <img src="{{ data.attachment.image.src }}" class="thumbnail" draggable="false" alt="" />
 164                          <# } else { #>
 165                              <img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" alt="" />
 166                          <# } #>
 167                          <p class="attachment-meta attachment-meta-title">&#8220;{{ data.attachment.title }}&#8221;</p>
 168                          <# if ( data.attachment.album || data.attachment.meta.album ) { #>
 169                          <p class="attachment-meta"><em>{{ data.attachment.album || data.attachment.meta.album }}</em></p>
 170                          <# } #>
 171                          <# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
 172                          <p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
 173                          <# } #>
 174                          <audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
 175                              <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}" />
 176                          </audio>
 177                      <# } else if ( 'video' === data.attachment.type ) { #>
 178                          <div class="wp-media-wrapper wp-video">
 179                              <video controls="controls" class="wp-video-shortcode" preload="metadata"
 180                                  <# if ( data.attachment.image && data.attachment.image.src !== data.attachment.icon ) { #>poster="{{ data.attachment.image.src }}"<# } #>>
 181                                  <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}" />
 182                              </video>
 183                          </div>
 184                      <# } else { #>
 185                          <img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" alt="" />
 186                          <p class="attachment-title">{{ data.attachment.title }}</p>
 187                      <# } #>
 188                  </div>
 189                  <div class="actions">
 190                      <# if ( data.canUpload ) { #>
 191                      <button type="button" class="button remove-button">{{ data.button_labels.remove }}</button>
 192                      <button type="button" class="button upload-button control-focus" {{{ describedByAttr }}}>{{ data.button_labels.change }}</button>
 193                      <# } #>
 194                  </div>
 195              </div>
 196          <# } else { #>
 197              <div class="attachment-media-view">
 198                  <# if ( data.canUpload ) { #>
 199                      <button type="button" class="upload-button button-add-media" {{{ describedByAttr }}}>{{ data.button_labels.select }}</button>
 200                  <# } #>
 201                  <div class="actions">
 202                      <# if ( data.defaultAttachment ) { #>
 203                          <button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
 204                      <# } #>
 205                  </div>
 206              </div>
 207          <# } #>
 208          <?php
 209      }
 210  
 211      /**
 212       * Get default button labels.
 213       *
 214       * Provides an array of the default button labels based on the mime type of the current control.
 215       *
 216       * @since 4.9.0
 217       *
 218       * @return string[] An associative array of default button labels keyed by the button name.
 219       */
 220  	public function get_default_button_labels() {
 221          // Get just the mime type and strip the mime subtype if present.
 222          $mime_type = ! empty( $this->mime_type ) ? strtok( ltrim( $this->mime_type, '/' ), '/' ) : 'default';
 223  
 224          switch ( $mime_type ) {
 225              case 'video':
 226                  return array(
 227                      'select'       => __( 'Select video' ),
 228                      'change'       => __( 'Change video' ),
 229                      'default'      => __( 'Default' ),
 230                      'remove'       => __( 'Remove' ),
 231                      'placeholder'  => __( 'No video selected' ),
 232                      'frame_title'  => __( 'Select video' ),
 233                      'frame_button' => __( 'Choose video' ),
 234                  );
 235              case 'audio':
 236                  return array(
 237                      'select'       => __( 'Select audio' ),
 238                      'change'       => __( 'Change audio' ),
 239                      'default'      => __( 'Default' ),
 240                      'remove'       => __( 'Remove' ),
 241                      'placeholder'  => __( 'No audio selected' ),
 242                      'frame_title'  => __( 'Select audio' ),
 243                      'frame_button' => __( 'Choose audio' ),
 244                  );
 245              case 'image':
 246                  return array(
 247                      'select'       => __( 'Select image' ),
 248                      'site_icon'    => __( 'Select Site Icon' ),
 249                      'change'       => __( 'Change image' ),
 250                      'default'      => __( 'Default' ),
 251                      'remove'       => __( 'Remove' ),
 252                      'placeholder'  => __( 'No image selected' ),
 253                      'frame_title'  => __( 'Select image' ),
 254                      'frame_button' => __( 'Choose image' ),
 255                  );
 256              default:
 257                  return array(
 258                      'select'       => __( 'Select file' ),
 259                      'change'       => __( 'Change file' ),
 260                      'default'      => __( 'Default' ),
 261                      'remove'       => __( 'Remove' ),
 262                      'placeholder'  => __( 'No file selected' ),
 263                      'frame_title'  => __( 'Select file' ),
 264                      'frame_button' => __( 'Choose file' ),
 265                  );
 266          } // End switch().
 267      }
 268  }


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref