balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-wp-customize-media-control.php (9384B)


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