From 37a36421838fa9205a05f41a77d856c64808a5e0 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 24 Oct 2014 16:31:54 +0000 Subject: [PATCH] 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 git-svn-id: https://develop.svn.wordpress.org/trunk@30014 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/customize.php | 3 + src/wp-admin/js/customize-controls.js | 23 +++++- .../class-wp-customize-control.php | 82 ++++++++++++++----- .../class-wp-customize-manager.php | 36 ++++++++ 4 files changed, 122 insertions(+), 22 deletions(-) diff --git a/src/wp-admin/customize.php b/src/wp-admin/customize.php index 7828ee4e64..ed5255818e 100644 --- a/src/wp-admin/customize.php +++ b/src/wp-admin/customize.php @@ -180,6 +180,9 @@ do_action( 'customize_controls_print_scripts' );
render_control_templates(); + /** * Print Customizer control scripts in the footer. * diff --git a/src/wp-admin/js/customize-controls.js b/src/wp-admin/js/customize-controls.js index fad223ee4a..812f4fac4f 100644 --- a/src/wp-admin/js/customize-controls.js +++ b/src/wp-admin/js/customize-controls.js @@ -60,7 +60,10 @@ } control.setting = control.settings['default'] || null; - control.ready(); + control.renderContent( function() { + // Don't call ready() until the content has rendered. + control.ready(); + } ); }) ); control.elements = []; @@ -149,6 +152,24 @@ this.setting.bind( update ); update( this.setting() ); + }, + + /** + * Render the control from its JS template, if it exists. + * + * The control's container must alreasy exist in the DOM. + */ + renderContent: function( callback ) { + var template, + selector = 'customize-control-' + this.params.type + '-content', + callback = callback || function(){}; + if ( 0 !== $( '#tmpl-' + selector ).length ) { + template = wp.template( selector ); + if ( template && this.container ) { + this.container.append( template( this.params ) ); + } + } + callback(); } }); diff --git a/src/wp-includes/class-wp-customize-control.php b/src/wp-includes/class-wp-customize-control.php index 7937d2dc5f..cbb7cdde5d 100644 --- a/src/wp-includes/class-wp-customize-control.php +++ b/src/wp-includes/class-wp-customize-control.php @@ -217,8 +217,10 @@ class WP_Customize_Control { $this->json['settings'][ $key ] = $setting->id; } - $this->json['type'] = $this->type; - $this->json['active'] = $this->active(); + $this->json['type'] = $this->type; + $this->json['label'] = $this->label; + $this->json['description'] = $this->description; + $this->json['active'] = $this->active(); } /** @@ -336,6 +338,8 @@ class WP_Customize_Control { * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`. * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly. * + * Control content can alternately be rendered in JS. See {@see WP_Customize_Control::print_template()}. + * * @since 3.4.0 */ protected function render_content() { @@ -443,6 +447,36 @@ class WP_Customize_Control { break; } } + + /** + * Render the control's JS template. + * + * This function is only run for control types that have been registered with {@see WP_Customize_Manager::register_control_type()}. + * + * In the future, this will also print the template for the control's container element and be overridable. + * + * @since 4.1.0 + */ + final public function print_template() { + ?> + + json['statuses'] = $this->statuses; + $this->json['defaultValue'] = $this->setting->default; } /** - * Render the control's content. + * Don't render the control content from PHP, as it's rendered via JS on load. * * @since 3.4.0 */ - public function render_content() { - $this_default = $this->setting->default; - $default_attr = ''; - if ( $this_default ) { - if ( false === strpos( $this_default, '#' ) ) - $this_default = '#' . $this_default; - $default_attr = ' data-default-color="' . esc_attr( $this_default ) . '"'; - } - // The input's value gets set by JS. Don't fill it. - ?> -