Customize: Move control's fallback selection of default content template to `renderContent` method to align with sections and panels.

* Only use default control content template when a more specific template doesn't exist.
* Remove extraneous whitespace from being output in `WP_Customize_Control::render()` method.
* Move Custom Header template printing to `customize_controls_print_footer_scripts` action.

See #30738.


git-svn-id: https://develop.svn.wordpress.org/trunk@41935 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2017-10-19 03:03:19 +00:00
parent b1f20b6763
commit 1c04006909
4 changed files with 38 additions and 32 deletions

View File

@ -3362,7 +3362,7 @@
},
initialize: function( id, options ) {
var control = this, deferredSettingIds = [], settings, gatherSettings, standardTypes;
var control = this, deferredSettingIds = [], settings, gatherSettings;
control.params = _.extend( {}, control.defaults );
@ -3401,30 +3401,8 @@
control.container = $( control.selector ); // Likely dead, per above. See #28709.
}
standardTypes = [
'button',
'checkbox',
'date',
'datetime-local',
'email',
'month',
'number',
'password',
'radio',
'range',
'search',
'select',
'tel',
'time',
'text',
'textarea',
'week',
'url'
];
if ( control.params.templateId ) {
control.templateSelector = control.params.templateId;
} else if ( _.contains( standardTypes, control.params.type ) && control.container.is( ':empty' ) ) {
control.templateSelector = 'customize-control-default-content';
} else {
control.templateSelector = 'customize-control-' + control.params.type + '-content';
}
@ -3886,12 +3864,39 @@
* @since 4.1.0
*/
renderContent: function () {
var template,
control = this;
var control = this, template, standardTypes, templateId;
standardTypes = [
'button',
'checkbox',
'date',
'datetime-local',
'email',
'month',
'number',
'password',
'radio',
'range',
'search',
'select',
'tel',
'time',
'text',
'textarea',
'week',
'url'
];
templateId = control.templateSelector;
// Use default content template when a standard HTML type is used and there isn't a more specific template existing.
if ( templateId === 'customize-control-' + control.params.type + '-content' && _.contains( standardTypes, control.params.type ) && ! document.getElementById( 'tmpl-' + templateId ) ) {
templateId = 'customize-control-default-content';
}
// Replace the container element's content with the control.
if ( 0 !== $( '#tmpl-' + control.templateSelector ).length ) {
template = wp.template( control.templateSelector );
if ( document.getElementById( 'tmpl-' + templateId ) ) {
template = wp.template( templateId );
if ( template && control.container ) {
control.container.html( template( control.params ) );
}

View File

@ -421,9 +421,9 @@ class WP_Customize_Control {
$id = 'customize-control-' . str_replace( array( '[', ']' ), array( '-', '' ), $this->id );
$class = 'customize-control customize-control-' . $this->type;
?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
<?php $this->render_content(); ?>
</li><?php
printf( '<li id="%s" class="%s">', esc_attr( $id ), esc_attr( $class ) );
$this->render_content();
echo '</li>';
}
/**

View File

@ -77,6 +77,8 @@ class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
return;
}
add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_header_image_template' ) );
// Process default headers and uploaded headers.
$custom_image_header->process_default_headers();
$this->default_headers = $custom_image_header->get_default_header_images();
@ -157,7 +159,6 @@ class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
/**
*/
public function render_content() {
$this->print_header_image_template();
$visibility = $this->get_current_image_src() ? '' : ' style="display:none" ';
$width = absint( get_theme_support( 'custom-header', 'width' ) );
$height = absint( get_theme_support( 'custom-header', 'height' ) );

View File

@ -421,7 +421,7 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase {
$params = $control->json();
$this->assertEquals( 'widget_form', $params['type'] );
$this->assertRegExp( '#^<li[^>]+>\s+</li>$#', $params['content'] );
$this->assertRegExp( '#^<li[^>]+>\s*</li>$#', $params['content'] );
$this->assertRegExp( '#^<div[^>]*class=\'widget\'[^>]*#s', $params['widget_control'] );
$this->assertContains( '<div class="widget-content"></div>', $params['widget_control'] );
$this->assertNotContains( '<input class="widefat"', $params['widget_control'] );