<?php
/**
 * Business Interface Customizer widgets class.
 *
 * @package     Business Interface
 * @author      WPInterface Team
 * @since       1.0.0
 */

/**
 * Do not allow direct script access.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Business_Interface_Customizer_Widget_Button' ) ) :

	/**
	 * Business Interface Customizer widget class
	 */
	class Business_Interface_Customizer_Widget_Button extends Business_Interface_Customizer_Widget {

		/**
		 * Menu Location for this widget
		 *
		 * @since 1.0.0
		 * @var string
		 */
		public $styles = array();

		/**
		 * Primary class constructor.
		 *
		 * @since 1.0.0
		 * @param array $args An array of the values for this widget.
		 */
		public function __construct( $args = array() ) {

			$values = array(
				'text'       => '',
				'url'        => '',
				'target'     => '_self',
				'class'      => '',
				'style'      => '',
				'visibility' => 'all',
			);

			$args['values'] = isset( $args['values'] ) ? wp_parse_args( $args['values'], $values ) : $values;

			$args['values']['text']       = wp_kses( $args['values']['text'], business_interface_get_allowed_html_tags() );
			$args['values']['url']        = esc_url_raw( $args['values']['url'] );
			$args['values']['target']     = sanitize_text_field( $args['values']['target'] );
			$args['values']['class']      = sanitize_text_field( $args['values']['class'] );
			$args['values']['style']      = sanitize_text_field( $args['values']['style'] );
			$args['values']['visibility'] = isset( $args['values']['visibility'] ) ? sanitize_text_field( $args['values']['visibility'] ) : 'hide-mobile-tablet';

			parent::__construct( $args );

			$this->name        = __( 'Button', 'business-interface' );
			$this->description = __( 'A button with custom link.', 'business-interface' );
			$this->icon        = 'dashicons dashicons-admin-links';
			$this->type        = 'button';
			$this->styles      = isset( $args['styles'] ) ? $args['styles'] : array();
		}

		/**
		 * Displays the form for this widget on the Widgets page of the WP Admin area.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function form() {
			?>
			<!-- Text -->
			<p>
				<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-text"><?php esc_html_e( 'Text', 'business-interface' ); ?>:</label>
				<input 
					type="text" 
					id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-text" 
					name="widget-button[<?php echo esc_attr( $this->number ); ?>][text]" 
					data-option-name="text" 
					value="<?php echo esc_html( $this->values['text'] ); ?>" 
					placeholder="<?php esc_attr_e( 'Button Text', 'business-interface' ); ?>"/>
			</p>

			<!-- URL -->
			<p>
				<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-url"><?php esc_html_e( 'URL', 'business-interface' ); ?>:</label>
				<input 
					type="text" 
					id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-url" 
					name="widget-button[<?php echo esc_attr( $this->number ); ?>][url]" 
					data-option-name="url" 
					value="<?php echo esc_html( $this->values['url'] ); ?>" 
					placeholder="<?php esc_attr_e( 'Button URL', 'business-interface' ); ?>" />
			</p>

			<!-- Target -->
			<p>
				<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target"><?php esc_html_e( 'Open link in', 'business-interface' ); ?>:</label>
				<span class="buttonset">
					<input 
						class="switch-input screen-reader-text" 
						type="radio" 
						value="_self" 
						name="widget-button[<?php echo esc_attr( $this->number ); ?>][target]" 
						id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target-_self" 
						<?php checked( '_self', $this->values['target'], true ); ?> 
						data-option-name="target">
						<label 
							class="switch-label" 
							for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target-_self">
							<?php esc_html_e( 'Same Tab', 'business-interface' ); ?>
						</label>
					</input>
					<input 
						class="switch-input screen-reader-text" 
						type="radio" 
						value="_blank" 
						name="widget-button[<?php echo esc_attr( $this->number ); ?>][target]" 
						id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target-_blank" 
						<?php checked( '_blank', $this->values['target'], true ); ?> 
						data-option-name="target">
						<label 
							class="switch-label" 
							for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-target-_blank">
							<?php esc_html_e( 'New Tab', 'business-interface' ); ?>
						</label>
					</input>
				</span>
			</p>

			<?php if ( ! empty( $this->styles ) ) { ?>
				<!-- Styles -->
				<p class="business-interface-widget-button-style">
					<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-style">
						<?php esc_html_e( 'Style', 'business-interface' ); ?>:
					</label>
					<select id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-style" name="widget-button[<?php echo esc_attr( $this->number ); ?>][style]" data-option-name="style">
						<?php foreach ( $this->styles as $key => $value ) { ?>
							<option 
								value="<?php echo esc_attr( $key ); ?>" 
								<?php selected( $key, $this->values['style'], true ); ?>>
								<?php echo esc_html( $value ); ?>
							</option>
						<?php } ?>
					</select>
				</p>
			<?php } ?>

			<!-- Class -->
			<p>
				<label for="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-class"><?php esc_html_e( 'Additional class', 'business-interface' ); ?>:</label>
				<input 
					type="text" 
					id="widget-button-<?php echo esc_attr( $this->id ); ?>-<?php echo esc_attr( $this->number ); ?>-class" 
					name="widget-button[<?php echo esc_attr( $this->number ); ?>][class]" 
					data-option-name="class" 
					value="<?php echo esc_html( $this->values['class'] ); ?>" />
			</p>
			<?php
		}
	}
endif;
