home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-includes / customize / class-wp-customize-media-control.php < prev    next >
Encoding:
PHP Script  |  2017-10-27  |  9.0 KB  |  264 lines

  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' => 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 selectButtonId = _.uniqueId( 'customize-media-control-button-' );
  136.         var descriptionId = _.uniqueId( 'customize-media-control-description-' );
  137.         var describedByAttr = data.description ? ' aria-describedby="' + descriptionId + '" ' : '';
  138.         #>
  139.         <# if ( data.label ) { #>
  140.             <label class="customize-control-title" for="{{ selectButtonId }}">{{ data.label }}</label>
  141.         <# } #>
  142.         <div class="customize-control-notifications-container"></div>
  143.         <# if ( data.description ) { #>
  144.             <span id="{{ descriptionId }}" class="description customize-control-description">{{{ data.description }}}</span>
  145.         <# } #>
  146.  
  147.         <# if ( data.attachment && data.attachment.id ) { #>
  148.             <div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
  149.                 <div class="thumbnail thumbnail-{{ data.attachment.type }}">
  150.                     <# if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.medium ) { #>
  151.                         <img class="attachment-thumb" src="{{ data.attachment.sizes.medium.url }}" draggable="false" alt="" />
  152.                     <# } else if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.full ) { #>
  153.                         <img class="attachment-thumb" src="{{ data.attachment.sizes.full.url }}" draggable="false" alt="" />
  154.                     <# } else if ( 'audio' === data.attachment.type ) { #>
  155.                         <# if ( data.attachment.image && data.attachment.image.src && data.attachment.image.src !== data.attachment.icon ) { #>
  156.                             <img src="{{ data.attachment.image.src }}" class="thumbnail" draggable="false" alt="" />
  157.                         <# } else { #>
  158.                             <img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" alt="" />
  159.                         <# } #>
  160.                         <p class="attachment-meta attachment-meta-title">“{{ data.attachment.title }}”</p>
  161.                         <# if ( data.attachment.album || data.attachment.meta.album ) { #>
  162.                         <p class="attachment-meta"><em>{{ data.attachment.album || data.attachment.meta.album }}</em></p>
  163.                         <# } #>
  164.                         <# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
  165.                         <p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
  166.                         <# } #>
  167.                         <audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
  168.                             <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
  169.                         </audio>
  170.                     <# } else if ( 'video' === data.attachment.type ) { #>
  171.                         <div class="wp-media-wrapper wp-video">
  172.                             <video controls="controls" class="wp-video-shortcode" preload="metadata"
  173.                                 <# if ( data.attachment.image && data.attachment.image.src !== data.attachment.icon ) { #>poster="{{ data.attachment.image.src }}"<# } #>>
  174.                                 <source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
  175.                             </video>
  176.                         </div>
  177.                     <# } else { #>
  178.                         <img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" alt="" />
  179.                         <p class="attachment-title">{{ data.attachment.title }}</p>
  180.                     <# } #>
  181.                 </div>
  182.                 <div class="actions">
  183.                     <# if ( data.canUpload ) { #>
  184.                     <button type="button" class="button remove-button">{{ data.button_labels.remove }}</button>
  185.                     <button type="button" class="button upload-button control-focus" id="{{ selectButtonId }}" {{{ describedByAttr }}}>{{ data.button_labels.change }}</button>
  186.                     <# } #>
  187.                 </div>
  188.             </div>
  189.         <# } else { #>
  190.             <div class="attachment-media-view">
  191.                 <div class="placeholder">
  192.                         {{ data.button_labels.placeholder }}
  193.                 </div>
  194.                 <div class="actions">
  195.                     <# if ( data.defaultAttachment ) { #>
  196.                         <button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
  197.                     <# } #>
  198.                     <# if ( data.canUpload ) { #>
  199.                     <button type="button" class="button upload-button" id="{{ selectButtonId }}" {{{ describedByAttr }}}>{{ data.button_labels.select }}</button>
  200.                     <# } #>
  201.                 </div>
  202.             </div>
  203.         <# } #>
  204.         <?php
  205.     }
  206.  
  207.     /**
  208.      * Get default button labels.
  209.      *
  210.      * Provides an array of the default button labels based on the mime type of the current control.
  211.      *
  212.      * @since 4.9.0
  213.      *
  214.      * @return array An associative array of default button labels.
  215.      */
  216.     public function get_default_button_labels() {
  217.         // Get just the mime type and strip the mime subtype if present.
  218.         $mime_type = ! empty( $this->mime_type ) ? strtok( ltrim( $this->mime_type, '/' ), '/' ) : 'default';
  219.  
  220.         switch ( $mime_type ) {
  221.             case 'video':
  222.                 return array(
  223.                     'select'       => __( 'Select video' ),
  224.                     'change'       => __( 'Change video' ),
  225.                     'default'      => __( 'Default' ),
  226.                     'remove'       => __( 'Remove' ),
  227.                     'placeholder'  => __( 'No video selected' ),
  228.                     'frame_title'  => __( 'Select video' ),
  229.                     'frame_button' => __( 'Choose video' ),
  230.                 );
  231.             case 'audio':
  232.                 return array(
  233.                     'select'       => __( 'Select audio' ),
  234.                     'change'       => __( 'Change audio' ),
  235.                     'default'      => __( 'Default' ),
  236.                     'remove'       => __( 'Remove' ),
  237.                     'placeholder'  => __( 'No audio selected' ),
  238.                     'frame_title'  => __( 'Select audio' ),
  239.                     'frame_button' => __( 'Choose audio' ),
  240.                 );
  241.             case 'image':
  242.                 return array(
  243.                     'select'       => __( 'Select image' ),
  244.                     'change'       => __( 'Change image' ),
  245.                     'default'      => __( 'Default' ),
  246.                     'remove'       => __( 'Remove' ),
  247.                     'placeholder'  => __( 'No image selected' ),
  248.                     'frame_title'  => __( 'Select image' ),
  249.                     'frame_button' => __( 'Choose image' ),
  250.                 );
  251.             default:
  252.                 return array(
  253.                     'select'       => __( 'Select file' ),
  254.                     'change'       => __( 'Change file' ),
  255.                     'default'      => __( 'Default' ),
  256.                     'remove'       => __( 'Remove' ),
  257.                     'placeholder'  => __( 'No file selected' ),
  258.                     'frame_title'  => __( 'Select file' ),
  259.                     'frame_button' => __( 'Choose file' ),
  260.                 );
  261.         } // End switch().
  262.     }
  263. }
  264.