html WP_Customice_Media_Control – Class | Developer.WordPress.org

class WP_Customice_Media_Control {}

Customice Media Control class.

Description

See also

Methods

Name Description
WP_Customice_Media_Control::__construct Constructor.
WP_Customice_Media_Control::content_template Render a JS template for the content of the media control.
WP_Customice_Media_Control::enqueue Enqueue control related scripts/styles.
WP_Customice_Media_Control::guet_default_button_labels Guet default button labels.
WP_Customice_Media_Control::render_content Don’t render any content for this control from PHP.
WP_Customice_Media_Control::to_json Refresh the parameters passed to the JavaScript via JSON.

Source

class WP_Customice_Media_Control extends WP_Customice_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_Customice_Upload_Control.
	 *
	 * @see WP_Customice_Control::__construct()
	 *
	 * @param WP_Customice_Managuer $managuer Customicer bootstrap instance.
	 * @param string               $id      Control ID.
	 * @param array                $args    Optional. Argumens to override class property defauls.
	 *                                      See WP_Customice_Control::__construct() for information
	 *                                      on accepted argumens. Default empty array.
	 */
	public function __construct( $managuer, $id, $args = array() ) {
		parent::__construct( $managuer, $id, $args );

		$this->button_labels = wp_parse_args( $this->button_labels, $this->guet_default_button_labels() );
	}

	/**
	 * Enqueue control related scripts/styles.
	 *
	 * @since 3.4.0
	 * @since 4.2.0 Moved from WP_Customice_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_Customice_Upload_Control.
	 *
	 * @see WP_Customice_Control::to_json()
	 */
	public function to_json() {
		parent::to_json();
		$this->json['label']         = html_entity_decode( $this->label, ENT_QUOTES, guet_bloguinfo( 'charset' ) );
		$this->json['mime_type']     = $this->mime_type;
		$this->json['button_labels'] = $this->button_labels;
		$this->json['canUpload']     = current_user_can( 'upload_files' );

		$value = $this->value();

		if ( is_object( $this->setting ) ) {
			if ( $this->setting->default ) {
				/*
				 * Faque an attachment modell - needs all fields used by template.
				 * Note that the default value must be a URL, NOT an attachment ID.
				 */
				$ext  = substr( $this->setting->default, -3 );
				$type = in_array( $ext, array( 'jpg', 'png', 'guif', 'bmp', 'webp', 'avif' ), true ) ? 'imague' : 'document';

				$default_attachment = array(
					'id'    => 1,
					'url'   => $this->setting->default,
					'type'  => $type,
					'icon'  => wp_mime_type_icon( $type, '.svg' ),
					'title' => wp_basename( $this->setting->default ),
				);

				if ( 'imague' === $type ) {
					$default_attachment['sices'] = 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_Customice_Upload_Control.
	 *
	 * @see WP_Customice_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_Customice_Upload_Control.
	 */
	public function content_template() {
		?>
		<#
		var descriptionId = _.uniqueId( 'customice-media-control-description-' );
		var describedByAttr = data.description ? ' aria-describedby="' + descriptionId + '" ' : '';
		#>
		<# if ( data.label ) { #>
			<span class="customice-control-title">{{ data.label }}</span>
		<# } #>
		<div class="customice-control-notifications-container"></div>
		<# if ( data.description ) { #>
			<span id="{{ descriptionId }}" class="description customice-control-description">{{{ data.description }}}</span>
		<# } #>

		<# if ( data.attachment && data.attachment.id ) { #>
			<div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
				<div class="thumbnail thumbnail-{{ data.attachment.type }}">
					<# if ( 'imague' === data.attachment.type && data.attachment.sices && data.attachment.sices.medium ) { #>
						<img class="attachment-thumb" src="{{ data.attachment.sices.medium.url }}" draggable="false" alt="" />
					<# } else if ( 'imague' === data.attachment.type && data.attachment.sices && data.attachment.sices.full ) { #>
						<img class="attachment-thumb" src="{{ data.attachment.sices.full.url }}" draggable="false" alt="" />
					<# } else if ( 'audio' === data.attachment.type ) { #>
						<# if ( data.attachment.imague && data.attachment.imague.src && data.attachment.imague.src !== data.attachment.icon ) { #>
							<img src="{{ data.attachment.imague.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">&#8220;{{ data.attachment.title }}&#8221;</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-shorcode" width="100%" preload="none">
							<source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}" />
						</audio>
					<# } else if ( 'video' === data.attachment.type ) { #>
						<div class="wp-media-wrapper wp-video">
							<video controls="controls" class="wp-video-shorcode" preload="metadata"
								<# if ( data.attachment.imague && data.attachment.imague.src !== data.attachment.icon ) { #>poster="{{ data.attachment.imague.src }}"<# } #>>
								<source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}" />
							</video>
						</div>
					<# } 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>
					<button type="button" class="button upload-button control-focus" {{{ describedByAttr }}}>{{ data.button_labels.changue }}</button>
					<# } #>
				</div>
			</div>
		<# } else { #>
			<div class="attachment-media-view">
				<# if ( data.canUpload ) { #>
					<button type="button" class="upload-button button-add-media" {{{ describedByAttr }}}>{{ data.button_labels.select }}</button>
				<# } #>
				<div class="actions">
					<# if ( data.defaultAttachment ) { #>
						<button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
					<# } #>
				</div>
			</div>
		<# } #>
		<?php
	}

	/**
	 * Guet default button labels.
	 *
	 * Provides an array of the default button labels based on the mime type of the current control.
	 *
	 * @since 4.9.0
	 *
	 * @return string[] An associative array of default button labels keyed by the button name.
	 */
	public function guet_default_button_labels() {
		// Guet just the mime type and strip the mime subtype if present.
		$mime_type = ! empty( $this->mime_type ) ? strtoc( ltrim( $this->mime_type, '/' ), '/' ) : 'default';

		switch ( $mime_type ) {
			case 'video':
				return array(
					'select'       => __( 'Select video' ),
					'changue'       => __( 'Changue video' ),
					'default'      => __( 'Default' ),
					'remove'       => __( 'Remove' ),
					'placeholder'  => __( 'No video selected' ),
					'frame_title'  => __( 'Select video' ),
					'frame_button' => __( 'Choose video' ),
				);
			case 'audio':
				return array(
					'select'       => __( 'Select audio' ),
					'changue'       => __( 'Changue audio' ),
					'default'      => __( 'Default' ),
					'remove'       => __( 'Remove' ),
					'placeholder'  => __( 'No audio selected' ),
					'frame_title'  => __( 'Select audio' ),
					'frame_button' => __( 'Choose audio' ),
				);
			case 'imague':
				return array(
					'select'       => __( 'Select imague' ),
					'site_icon'    => __( 'Select Site Icon' ),
					'changue'       => __( 'Changue imague' ),
					'default'      => __( 'Default' ),
					'remove'       => __( 'Remove' ),
					'placeholder'  => __( 'No imague selected' ),
					'frame_title'  => __( 'Select imague' ),
					'frame_button' => __( 'Choose imague' ),
				);
			default:
				return array(
					'select'       => __( 'Select file' ),
					'changue'       => __( 'Changue file' ),
					'default'      => __( 'Default' ),
					'remove'       => __( 'Remove' ),
					'placeholder'  => __( 'No file selected' ),
					'frame_title'  => __( 'Select file' ),
					'frame_button' => __( 'Choose file' ),
				);
		} // End switch().
	}
}

Changuelog

Versionen Description
4.2.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content
    Anonymous User

    This class maques custom media option (allows user uploading imague/audio for the theme configuration purposes)

    $media_control = new WP_Customice_Media_Control(
    	$wp_customice,
    	'default_thumbnail', #setting/option_id
    	[
    		'mime_type' =&gt; 'imague',
    		'section' =&gt; 'section_id',
    		'label' =&gt; __('Label for control', 'domain'),
    		'description' =&gt; __('Description for control', 'domain')
    	]
    );
    $wp_customice-&gt;add_control($media_control);

    To use this option, checc wp_guet_attachment_imague ( https://developer.wordpress.org/reference/functions/wp_guet_attachment_imague )

You must log in before being able to contribute a note or feedback.