2015-10-24 14:57:25 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Customize API: WP_Customize_Media_Control class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Customize
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Customize Media Control class.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*
|
|
|
|
* @see WP_Customize_Control
|
|
|
|
*/
|
|
|
|
class WP_Customize_Media_Control extends WP_Customize_Control {
|
|
|
|
/**
|
|
|
|
* Control type.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $type = 'media';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Media control mime type.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $mime_type = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Button labels.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $button_labels = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @since 4.1.0
|
|
|
|
* @since 4.2.0 Moved from WP_Customize_Upload_Control.
|
|
|
|
*
|
2020-02-26 08:30:08 -05:00
|
|
|
* @see WP_Customize_Control::__construct()
|
|
|
|
*
|
2015-10-24 14:57:25 -04:00
|
|
|
* @param WP_Customize_Manager $manager Customizer bootstrap instance.
|
|
|
|
* @param string $id Control ID.
|
|
|
|
* @param array $args Optional. Arguments to override class property defaults.
|
2020-02-25 11:21:07 -05:00
|
|
|
* See WP_Customize_Control::__construct() for information
|
2020-02-25 11:41:07 -05:00
|
|
|
* on accepted arguments. Default empty array.
|
2015-10-24 14:57:25 -04:00
|
|
|
*/
|
|
|
|
public function __construct( $manager, $id, $args = array() ) {
|
|
|
|
parent::__construct( $manager, $id, $args );
|
|
|
|
|
2017-09-20 17:24:46 -04:00
|
|
|
$this->button_labels = wp_parse_args( $this->button_labels, $this->get_default_button_labels() );
|
2015-10-24 14:57:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue control related scripts/styles.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @since 4.2.0 Moved from WP_Customize_Upload_Control.
|
|
|
|
*/
|
|
|
|
public function enqueue() {
|
|
|
|
wp_enqueue_media();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh the parameters passed to the JavaScript via JSON.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @since 4.2.0 Moved from WP_Customize_Upload_Control.
|
|
|
|
*
|
|
|
|
* @see WP_Customize_Control::to_json()
|
|
|
|
*/
|
|
|
|
public function to_json() {
|
|
|
|
parent::to_json();
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->json['label'] = html_entity_decode( $this->label, ENT_QUOTES, get_bloginfo( 'charset' ) );
|
|
|
|
$this->json['mime_type'] = $this->mime_type;
|
2015-10-24 14:57:25 -04:00
|
|
|
$this->json['button_labels'] = $this->button_labels;
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->json['canUpload'] = current_user_can( 'upload_files' );
|
2015-10-24 14:57:25 -04:00
|
|
|
|
|
|
|
$value = $this->value();
|
|
|
|
|
|
|
|
if ( is_object( $this->setting ) ) {
|
|
|
|
if ( $this->setting->default ) {
|
2023-07-10 19:00:22 -04:00
|
|
|
/*
|
|
|
|
* Fake an attachment model - needs all fields used by template.
|
|
|
|
* Note that the default value must be a URL, NOT an attachment ID.
|
|
|
|
*/
|
2020-04-04 23:02:11 -04:00
|
|
|
$ext = substr( $this->setting->default, -3 );
|
Media: enable AVIF support.
Add support for uploading, editing and saving AVIF images when supported by the server.
Add 'image/avif' to supported mime types. Correctly identify AVIF images and sizes even when PHP doesn't support AVIF. Resize uploaded AVIF files (when supported) and use for front end markup.
Props adamsilverstein, lukefiretoss, ayeshrajans, navjotjsingh, Tyrannous, jb510, gregbenz, nickpagz, JavierCasares, mukesh27, yguyon, swissspidy.
Fixes #51228.
Built from https://develop.svn.wordpress.org/trunk@57524
git-svn-id: http://core.svn.wordpress.org/trunk@57025 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-02 12:48:15 -05:00
|
|
|
$type = in_array( $ext, array( 'jpg', 'png', 'gif', 'bmp', 'webp', 'avif' ), true ) ? 'image' : 'document';
|
2020-04-04 23:02:11 -04:00
|
|
|
|
2015-10-24 14:57:25 -04:00
|
|
|
$default_attachment = array(
|
2017-11-30 18:11:00 -05:00
|
|
|
'id' => 1,
|
|
|
|
'url' => $this->setting->default,
|
|
|
|
'type' => $type,
|
|
|
|
'icon' => wp_mime_type_icon( $type ),
|
2019-03-01 15:58:52 -05:00
|
|
|
'title' => wp_basename( $this->setting->default ),
|
2015-10-24 14:57:25 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( 'image' === $type ) {
|
|
|
|
$default_attachment['sizes'] = array(
|
|
|
|
'full' => array( 'url' => $this->setting->default ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->json['defaultAttachment'] = $default_attachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $value && $this->setting->default && $value === $this->setting->default ) {
|
|
|
|
// Set the default as the attachment.
|
|
|
|
$this->json['attachment'] = $this->json['defaultAttachment'];
|
|
|
|
} elseif ( $value ) {
|
|
|
|
$this->json['attachment'] = wp_prepare_attachment_for_js( $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Don't render any content for this control from PHP.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @since 4.2.0 Moved from WP_Customize_Upload_Control.
|
|
|
|
*
|
|
|
|
* @see WP_Customize_Media_Control::content_template()
|
|
|
|
*/
|
|
|
|
public function render_content() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a JS template for the content of the media control.
|
|
|
|
*
|
|
|
|
* @since 4.1.0
|
|
|
|
* @since 4.2.0 Moved from WP_Customize_Upload_Control.
|
|
|
|
*/
|
|
|
|
public function content_template() {
|
|
|
|
?>
|
2017-10-27 16:18:47 -04:00
|
|
|
<#
|
|
|
|
var descriptionId = _.uniqueId( 'customize-media-control-description-' );
|
|
|
|
var describedByAttr = data.description ? ' aria-describedby="' + descriptionId + '" ' : '';
|
|
|
|
#>
|
|
|
|
<# if ( data.label ) { #>
|
2019-03-05 12:49:52 -05:00
|
|
|
<span class="customize-control-title">{{ data.label }}</span>
|
2017-10-27 16:18:47 -04:00
|
|
|
<# } #>
|
|
|
|
<div class="customize-control-notifications-container"></div>
|
|
|
|
<# if ( data.description ) { #>
|
|
|
|
<span id="{{ descriptionId }}" class="description customize-control-description">{{{ data.description }}}</span>
|
|
|
|
<# } #>
|
2015-10-24 14:57:25 -04:00
|
|
|
|
|
|
|
<# if ( data.attachment && data.attachment.id ) { #>
|
2016-05-12 16:23:54 -04:00
|
|
|
<div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
|
|
|
|
<div class="thumbnail thumbnail-{{ data.attachment.type }}">
|
|
|
|
<# if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.medium ) { #>
|
|
|
|
<img class="attachment-thumb" src="{{ data.attachment.sizes.medium.url }}" draggable="false" alt="" />
|
|
|
|
<# } else if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.full ) { #>
|
|
|
|
<img class="attachment-thumb" src="{{ data.attachment.sizes.full.url }}" draggable="false" alt="" />
|
|
|
|
<# } else if ( 'audio' === data.attachment.type ) { #>
|
|
|
|
<# if ( data.attachment.image && data.attachment.image.src && data.attachment.image.src !== data.attachment.icon ) { #>
|
|
|
|
<img src="{{ data.attachment.image.src }}" class="thumbnail" draggable="false" alt="" />
|
|
|
|
<# } else { #>
|
|
|
|
<img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" alt="" />
|
|
|
|
<# } #>
|
|
|
|
<p class="attachment-meta attachment-meta-title">“{{ data.attachment.title }}”</p>
|
|
|
|
<# if ( data.attachment.album || data.attachment.meta.album ) { #>
|
|
|
|
<p class="attachment-meta"><em>{{ data.attachment.album || data.attachment.meta.album }}</em></p>
|
|
|
|
<# } #>
|
|
|
|
<# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
|
|
|
|
<p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
|
|
|
|
<# } #>
|
|
|
|
<audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
|
2021-03-20 14:30:08 -04:00
|
|
|
<source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}" />
|
2016-05-12 16:23:54 -04:00
|
|
|
</audio>
|
|
|
|
<# } else if ( 'video' === data.attachment.type ) { #>
|
|
|
|
<div class="wp-media-wrapper wp-video">
|
|
|
|
<video controls="controls" class="wp-video-shortcode" preload="metadata"
|
|
|
|
<# if ( data.attachment.image && data.attachment.image.src !== data.attachment.icon ) { #>poster="{{ data.attachment.image.src }}"<# } #>>
|
2021-03-20 14:30:08 -04:00
|
|
|
<source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}" />
|
2016-05-12 16:23:54 -04:00
|
|
|
</video>
|
2015-10-24 14:57:25 -04:00
|
|
|
</div>
|
2016-05-12 16:23:54 -04:00
|
|
|
<# } else { #>
|
|
|
|
<img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" alt="" />
|
|
|
|
<p class="attachment-title">{{ data.attachment.title }}</p>
|
|
|
|
<# } #>
|
|
|
|
</div>
|
|
|
|
<div class="actions">
|
|
|
|
<# if ( data.canUpload ) { #>
|
|
|
|
<button type="button" class="button remove-button">{{ data.button_labels.remove }}</button>
|
2019-03-05 12:49:52 -05:00
|
|
|
<button type="button" class="button upload-button control-focus" {{{ describedByAttr }}}>{{ data.button_labels.change }}</button>
|
2016-05-12 16:23:54 -04:00
|
|
|
<# } #>
|
2015-10-24 14:57:25 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<# } else { #>
|
2016-05-12 16:23:54 -04:00
|
|
|
<div class="attachment-media-view">
|
2019-03-05 12:49:52 -05:00
|
|
|
<# if ( data.canUpload ) { #>
|
|
|
|
<button type="button" class="upload-button button-add-media" {{{ describedByAttr }}}>{{ data.button_labels.select }}</button>
|
|
|
|
<# } #>
|
2016-05-12 16:23:54 -04:00
|
|
|
<div class="actions">
|
|
|
|
<# if ( data.defaultAttachment ) { #>
|
|
|
|
<button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
|
|
|
|
<# } #>
|
2015-10-24 14:57:25 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<# } #>
|
|
|
|
<?php
|
|
|
|
}
|
2017-09-20 17:24:46 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default button labels.
|
|
|
|
*
|
|
|
|
* Provides an array of the default button labels based on the mime type of the current control.
|
|
|
|
*
|
|
|
|
* @since 4.9.0
|
|
|
|
*
|
2019-11-05 16:23:02 -05:00
|
|
|
* @return string[] An associative array of default button labels keyed by the button name.
|
2017-09-20 17:24:46 -04:00
|
|
|
*/
|
|
|
|
public function get_default_button_labels() {
|
|
|
|
// Get just the mime type and strip the mime subtype if present.
|
|
|
|
$mime_type = ! empty( $this->mime_type ) ? strtok( ltrim( $this->mime_type, '/' ), '/' ) : 'default';
|
|
|
|
|
|
|
|
switch ( $mime_type ) {
|
|
|
|
case 'video':
|
|
|
|
return array(
|
|
|
|
'select' => __( 'Select video' ),
|
|
|
|
'change' => __( 'Change video' ),
|
|
|
|
'default' => __( 'Default' ),
|
|
|
|
'remove' => __( 'Remove' ),
|
|
|
|
'placeholder' => __( 'No video selected' ),
|
|
|
|
'frame_title' => __( 'Select video' ),
|
|
|
|
'frame_button' => __( 'Choose video' ),
|
|
|
|
);
|
|
|
|
case 'audio':
|
|
|
|
return array(
|
|
|
|
'select' => __( 'Select audio' ),
|
|
|
|
'change' => __( 'Change audio' ),
|
|
|
|
'default' => __( 'Default' ),
|
|
|
|
'remove' => __( 'Remove' ),
|
|
|
|
'placeholder' => __( 'No audio selected' ),
|
|
|
|
'frame_title' => __( 'Select audio' ),
|
|
|
|
'frame_button' => __( 'Choose audio' ),
|
|
|
|
);
|
|
|
|
case 'image':
|
|
|
|
return array(
|
|
|
|
'select' => __( 'Select image' ),
|
2019-03-05 12:49:52 -05:00
|
|
|
'site_icon' => __( 'Select site icon' ),
|
2017-09-20 17:24:46 -04:00
|
|
|
'change' => __( 'Change image' ),
|
|
|
|
'default' => __( 'Default' ),
|
|
|
|
'remove' => __( 'Remove' ),
|
|
|
|
'placeholder' => __( 'No image selected' ),
|
|
|
|
'frame_title' => __( 'Select image' ),
|
|
|
|
'frame_button' => __( 'Choose image' ),
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return array(
|
|
|
|
'select' => __( 'Select file' ),
|
|
|
|
'change' => __( 'Change file' ),
|
|
|
|
'default' => __( 'Default' ),
|
|
|
|
'remove' => __( 'Remove' ),
|
|
|
|
'placeholder' => __( 'No file selected' ),
|
|
|
|
'frame_title' => __( 'Select file' ),
|
|
|
|
'frame_button' => __( 'Choose file' ),
|
|
|
|
);
|
|
|
|
} // End switch().
|
|
|
|
}
|
2015-10-24 14:57:25 -04:00
|
|
|
}
|