Make WordPress Core


Ignore:
Timestamp:
03/10/2015 06:01:42 PM (12 years ago)
Author:
ocean90
Message:

Customizer: Introduce WP_Customize_Media_Control.

WP_Customize_Media_Control is a new base class for all Customizer media controls. If used directly it supports the ID of an attachment instead of an URL like WP_Customize_Upload_Control.

props celloexpressions.
fixes #29215.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-control.php

    r31661 r31698  
    522522         */
    523523        final public function print_template() {
    524                 ?>
    525                 <script type="text/html" id="tmpl-customize-control-<?php echo $this->type; ?>-content">
    526                         <?php $this->content_template(); ?>
    527                 </script>
    528                 <?php
     524                ?>
     525                <script type="text/html" id="tmpl-customize-control-<?php echo $this->type; ?>-content">
     526                        <?php $this->content_template(); ?>
     527                </script>
     528                <?php
    529529        }
    530530
    … …  
    639639
    640640/**
    641  * Customize Upload Control class.
    642  *
    643  * @since 3.4.0
     641 * Customize Media Control class.
     642 *
     643 * @since 4.2.0
    644644 *
    645645 * @see WP_Customize_Control
    646646 */
    647 class WP_Customize_Upload_Control extends WP_Customize_Control {
    648         public $type          = 'upload';
     647class WP_Customize_Media_Control extends WP_Customize_Control {
     648        public $type          = 'media';
    649649        public $mime_type     = '';
    650650        public $button_labels = array();
    651         public $removed = ''; // unused
    652         public $context; // unused
    653         public $extensions = array(); // unused
    654651
    655652        /**
    … …  
    657654         *
    658655         * @since 4.1.0
     656         * @since 4.2.0 Moved from WP_Customize_Upload_Control.
    659657         *
    660658         * @param WP_Customize_Manager $manager {@see WP_Customize_Manager} instance.
    … …  
    678676         *
    679677         * @since 3.4.0
     678         * @since 4.2.0 Moved from WP_Customize_Upload_Control.
    680679         */
    681680        public function enqueue() {
    … …  
    687686         *
    688687         * @since 3.4.0
     688         * @since 4.2.0 Moved from WP_Customize_Upload_Control.
     689         *
    689690         * @uses WP_Customize_Control::to_json()
    690691         */
    … …  
    699700                        if ( $this->setting->default ) {
    700701                                // Fake an attachment model - needs all fields used by template.
     702                                // Note that the default value must be a URL, NOT an attachment ID.
    701703                                $type = in_array( substr( $this->setting->default, -3 ), array( 'jpg', 'png', 'gif', 'bmp' ) ) ? 'image' : 'document';
    702704                                $default_attachment = array(
    … …  
    721723                                $this->json['attachment'] = $this->json['defaultAttachment'];
    722724                        } elseif ( $value ) {
    723                                 // Get the attachment model for the existing file.
    724                                 $attachment_id = attachment_url_to_postid( $value );
    725                                 if ( $attachment_id ) {
    726                                         $this->json['attachment'] = wp_prepare_attachment_for_js( $attachment_id );
    727                                 }
     725                                $this->json['attachment'] = wp_prepare_attachment_for_js( $value );
    728726                        }
    729727                }
    … …  
    733731         * Don't render any content for this control from PHP.
    734732         *
    735          * @see WP_Customize_Upload_Control::content_template()
    736          * @since 3.4.0
     733         * @since 3.4.0
     734         * @since 4.2.0 Moved from WP_Customize_Upload_Control.
     735         *
     736         * @see WP_Customize_Media_Control::content_template()
    737737         */
    738738        public function render_content() {}
    739739
    740740        /**
    741          * Render a JS template for the content of the upload control.
     741         * Render a JS template for the content of the media control.
    742742         *
    743743         * @since 4.1.0
     744         * @since 4.2.0 Moved from WP_Customize_Upload_Control.
    744745         */
    745746        public function content_template() {
    … …  
    824825
    825826/**
     827 * Customize Upload Control Class.
     828 *
     829 * @since 3.4.0
     830 *
     831 * @see WP_Customize_Media_Control
     832 */
     833class WP_Customize_Upload_Control extends WP_Customize_Media_Control {
     834        public $type          = 'upload';
     835        public $mime_type     = '';
     836        public $button_labels = array();
     837        public $removed = ''; // unused
     838        public $context; // unused
     839        public $extensions = array(); // unused
     840
     841        /**
     842         * Refresh the parameters passed to the JavaScript via JSON.
     843         *
     844         * @since 3.4.0
     845         *
     846         * @uses WP_Customize_Media_Control::to_json()
     847         */
     848        public function to_json() {
     849                parent::to_json();
     850
     851                $value = $this->value();
     852                if ( $value ) {
     853                        // Get the attachment model for the existing file.
     854                        $attachment_id = attachment_url_to_postid( $value );
     855                        if ( $attachment_id ) {
     856                                $this->json['attachment'] = wp_prepare_attachment_for_js( $attachment_id );
     857                        }
     858                }
     859        }
     860}
     861
     862/**
    826863 * Customize Image Control class.
    827864 *
Note: See TracChangeset for help on using the changeset viewer.