[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7