Make WordPress Core


Ignore:
Timestamp:
10/24/2014 04:31:54 PM (10 years ago)
Author:
johnbillion
Message:

Add the ability for a customizer control to render its controls via a JavaScript template. Switches the default color picker control to a JavaScript template. See #29572. Props celloexpressions

File:
1 edited

Legend:

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

    r29903 r30014  
    218218        }
    219219
    220         $this->json['type'] = $this->type;
    221         $this->json['active'] = $this->active();
     220        $this->json['type']        = $this->type;
     221        $this->json['label']       = $this->label;
     222        $this->json['description'] = $this->description;
     223        $this->json['active']      = $this->active();
    222224    }
    223225
     
    336338     * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
    337339     * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
     340     *
     341     * Control content can alternately be rendered in JS. See {@see WP_Customize_Control::print_template()}.
    338342     *
    339343     * @since 3.4.0
     
    444448        }
    445449    }
     450
     451    /**
     452     * Render the control's JS template.
     453     *
     454     * This function is only run for control types that have been registered with {@see WP_Customize_Manager::register_control_type()}.
     455     *
     456     * In the future, this will also print the template for the control's container element and be overridable.
     457     *
     458     * @since 4.1.0
     459     */
     460    final public function print_template() {
     461            ?>
     462            <script type="text/html" id="tmpl-customize-control-<?php echo $this->type; ?>-content">
     463                    <?php $this->content_template(); ?>
     464            </script>
     465            <?php
     466    }
     467
     468    /**
     469     * An Underscore (JS) template for this control's content (but not its container).
     470     *
     471     * Class variables for this control class are available in the `data` JS object;
     472     * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
     473     *
     474     * @see WP_Customize_Control::print_template()
     475     *
     476     * @since 4.1.0
     477     */
     478    protected function content_template() {}
     479
    446480}
    447481
     
    500534        parent::to_json();
    501535        $this->json['statuses'] = $this->statuses;
    502     }
    503 
    504     /**
    505      * Render the control's content.
    506      *
    507      * @since 3.4.0
    508      */
    509     public function render_content() {
    510         $this_default = $this->setting->default;
    511         $default_attr = '';
    512         if ( $this_default ) {
    513             if ( false === strpos( $this_default, '#' ) )
    514                 $this_default = '#' . $this_default;
    515             $default_attr = ' data-default-color="' . esc_attr( $this_default ) . '"';
    516         }
    517         // The input's value gets set by JS. Don't fill it.
     536        $this->json['defaultValue'] = $this->setting->default;
     537    }
     538
     539    /**
     540     * Don't render the control content from PHP, as it's rendered via JS on load.
     541     *
     542     * @since 3.4.0
     543     */
     544    public function render_content() {}
     545
     546    /**
     547     * Render a JS template for the content of the color picker control.
     548     *
     549     * @since 4.1.0
     550     */
     551    public function content_template() {
    518552        ?>
     553        <# var defaultValue = '';
     554        if ( data.defaultValue ) {
     555            if ( '#' !== data.defaultValue.substring( 0, 1 ) ) {
     556                defaultValue = '#' + data.defaultValue;
     557            }
     558            defaultValue = ' data-default-color=' + defaultValue; // Quotes added automatically.
     559        } #>
    519560        <label>
    520             <?php if ( ! empty( $this->label ) ) : ?>
    521                 <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
    522             <?php endif;
    523             if ( ! empty( $this->description ) ) : ?>
    524                 <span class="description customize-control-description"><?php echo $this->description; ?></span>
    525             <?php endif; ?>
    526 
     561            <# if ( data.label ) { #>
     562                <span class="customize-control-title">{{ data.label }}</span>
     563            <# } #>
     564            <# if ( data.description ) { #>
     565                <span class="description customize-control-description">{{ data.description }}</span>
     566            <# } #>
    527567            <div class="customize-control-content">
    528                 <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value' ); ?>"<?php echo $default_attr; ?> />
     568                <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value' ); ?>" {{ defaultValue }} />
    529569            </div>
    530570        </label>
Note: See TracChangeset for help on using the changeset viewer.