<?php
/**
 * Customizer custom control: Select field with optgroups support.
 *
 * @package    Zooey
 * @copyright  WebMan Design, Oliver Juhas
 *
 * @since    1.0.0
 * @version  2.0.1
 */

namespace WebManDesign\Zooey\Customize\Control;

use WP_Customize_Control;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

class Select extends WP_Customize_Control {

	/**
	 * Control type.
	 *
	 * @since   1.0.0
	 * @access  public
	 * @var     string
	 */
	public $type = 'select';

	/**
	 * Renders the control wrapper and calls $this->render_content() for the internals.
	 *
	 * @since    1.0.0
	 * @version  2.0.1
	 *
	 * @return  void
	 */
	public function render_content() {

		// Output

			if ( ! empty( $this->choices ) && is_array( $this->choices ) ) :
				?>

				<label>
					<span class="customize-control-title"><?php
						echo esc_html( $this->label );
					?></span>

					<?php if ( $this->description ) : ?>
					<span class="description customize-control-description"><?php
						echo wp_kses( $this->description, '#description' );
					?></span>
					<?php endif; ?>

					<select
						name="<?php echo esc_attr( $this->id ); ?>"
						<?php $this->link(); ?>
					>
						<?php

						foreach ( $this->choices as $value => $label ) {
							if ( 0 === strpos( $value, 'optgroup' ) ) {
								echo '<optgroup label="' . esc_attr( $label ) . '">';
							} elseif ( 0 === strpos( $value, '/optgroup' ) ) {
								echo '</optgroup>';
							} else {
								echo
									'<option
										value="' . esc_attr( $value ) . '" '
									. selected( $this->value(), $value, false ) // Return, do not echo.
									. '>'
									. esc_html( $label )
									. '</option>';
							}
						}

						?>
					</select>
				</label>

				<?php
			endif;

	} // /render_content

}
