<?php
/**
 * Plugin Name: Demo Plugin for #26633
 * Description: Registers a new setting for a non-scalar value (i.e. an assoc array) and a control which allows the setting to be updated by pressing a button.
 * Author: Weston Ruter, X-Team
 * Author URI: http://x-team.com/profile/weston-ruter/
 */

function wp26633_customize_register( $wp_customize ) {

	class WP26633_Customize_Control extends WP_Customize_Control {

		public $type = 'wp26633';
		public $label = 'Click Count';

		public function render_content() {
			?>
			<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
			<div class="customize-control-content">
				<p>
					<span class="click-count"></span>
				</p>
				<p>
					<a href="#" class=button>a.button</a>
				</p>
				<p>
					<button class=button type=button>button[type=button]</button>
				</p>
				<p>
					<input class=button type=button value="input[type=button]">
				</p>
			</div>
			<?php
		}
	}

	$wp_customize->add_section( 'wp26633', array(
		'title' => '#26633',
	) );

	$wp_customize->add_setting( 'wp26633', array(
		'default' => 0,
		'transport' => 'postMessage',
	) );

	$control = new WP26633_Customize_Control(
		$wp_customize,
		'wp26633',
		array(
			'section' => 'wp26633',
		)
	);

	$wp_customize->add_control( $control );
	add_action( 'customize_controls_print_footer_scripts', 'wp26633_control_js' );
}

add_action( 'customize_register', 'wp26633_customize_register' );


function wp26633_control_js() {
	wp_print_scripts( array( 'jquery', 'customize-controls' ) );
	?>
	<script>
	(function() {
		wp.customize.controlConstructor.wp26633 = wp.customize.Control.extend({
			ready: function() {
				var control = this;
				control.container.find( '.button, button' ).on( 'click', function () {
					var count = control.setting();
					count += 1;
					control.setting( count );
				} );
				control.setting.bind( function ( to ) {
					control.container.find( '.click-count' ).text( to );
				} );
				control.container.find( '.click-count' ).text( control.setting() );
			}
		});
	}());
	</script>
	<?php
}
