
<?php
/**
 * WooCommerce Button Elementor Widget.
 *
 * @package WooCommerce_Button_Text_Changer
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/*
 * Make sure Elementor is available.
 */
if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
	return;
}

/**
 * WooCommerce Button Elementor Widget.
 */
class WBTC_WooCommerce_Button_Widget extends \Elementor\Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @return string
	 */
	public function get_name() {
		return 'wbtc-woocommerce-button';
	}

	/**
	 * Get widget title.
	 *
	 * @return string
	 */
	public function get_title() {
		return esc_html__(
			'WooCommerce Button',
			'button-text-changer-for-woocommerce'
		);
	}

	/**
	 * Get widget icon.
	 *
	 * @return string
	 */
	public function get_icon() {
		return 'eicon-button';
	}

	/**
	 * Get widget categories.
	 *
	 * @return array
	 */
	public function get_categories() {
		return array(
			'wbtc-elements',
		);
	}

	/**
	 * Get widget keywords.
	 *
	 * @return array
	 */
	public function get_keywords() {
		return array(
			'woocommerce',
			'button',
			'cart',
			'checkout',
			'add to cart',
			'button text',
		);
	}

	/**
	 * Register widget controls.
	 *
	 * @return void
	 */
	protected function register_controls() {

		$this->start_controls_section(
			'wbtc_button_section',
			array(
				'label' => esc_html__(
					'Button Settings',
					'button-text-changer-for-woocommerce'
				),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'button_type',
			array(
				'label'   => esc_html__(
					'Button Type',
					'button-text-changer-for-woocommerce'
				),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'add_to_cart',
				'options' => array(
					'add_to_cart' => esc_html__(
						'Add to Cart',
						'button-text-changer-for-woocommerce'
					),
					'view_cart' => esc_html__(
						'View Cart',
						'button-text-changer-for-woocommerce'
					),
					'update_cart' => esc_html__(
						'Update Cart',
						'button-text-changer-for-woocommerce'
					),
					'checkout' => esc_html__(
						'Proceed to Checkout',
						'button-text-changer-for-woocommerce'
					),
					'place_order' => esc_html__(
						'Place Order',
						'button-text-changer-for-woocommerce'
					),
					'read_more' => esc_html__(
						'Read More',
						'button-text-changer-for-woocommerce'
					),
				),
			)
		);

		$this->add_control(
			'button_text',
			array(
				'label'       => esc_html__(
					'Button Text',
					'button-text-changer-for-woocommerce'
				),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'placeholder' => esc_html__(
					'Enter button text',
					'button-text-changer-for-woocommerce'
				),
				'default'     => '',
				'label_block' => true,
				'description' => esc_html__(
					'Leave empty to use the global text configured in Button Text Changer settings.',
					'button-text-changer-for-woocommerce'
				),
			)
		);

		$this->add_control(
			'button_url',
			array(
				'label'       => esc_html__(
					'Button URL',
					'button-text-changer-for-woocommerce'
				),
				'type'        => \Elementor\Controls_Manager::URL,
				'placeholder' => 'https://example.com',
				'default'     => array(
					'url' => '',
				),
				'label_block' => true,
			)
		);

		$this->end_controls_section();

		/*
		 * Style controls.
		 */
		$this->start_controls_section(
			'wbtc_style_section',
			array(
				'label' => esc_html__(
					'Button Style',
					'button-text-changer-for-woocommerce'
				),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'button_size',
			array(
				'label'   => esc_html__(
					'Size',
					'button-text-changer-for-woocommerce'
				),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'default',
				'options' => array(
					'default' => esc_html__(
						'Default',
						'button-text-changer-for-woocommerce'
					),
					'small' => esc_html__(
						'Small',
						'button-text-changer-for-woocommerce'
					),
					'medium' => esc_html__(
						'Medium',
						'button-text-changer-for-woocommerce'
					),
					'large' => esc_html__(
						'Large',
						'button-text-changer-for-woocommerce'
					),
				),
			)
		);

		$this->add_control(
			'button_alignment',
			array(
				'label'   => esc_html__(
					'Alignment',
					'button-text-changer-for-woocommerce'
				),
				'type'    => \Elementor\Controls_Manager::CHOOSE,
				'options' => array(
					'left' => array(
						'title' => esc_html__(
							'Left',
							'button-text-changer-for-woocommerce'
						),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__(
							'Center',
							'button-text-changer-for-woocommerce'
						),
						'icon'  => 'eicon-text-align-center',
					),
					'right' => array(
						'title' => esc_html__(
							'Right',
							'button-text-changer-for-woocommerce'
						),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default' => 'left',
				'toggle'  => true,
				'selectors' => array(
					'{{WRAPPER}} .wbtc-elementor-button-wrapper' =>
						'text-align: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Get global button text.
	 *
	 * @param string $button_type Button setting key.
	 * @return string
	 */
	private function get_global_button_text( $button_type ) {

		if (
			! class_exists(
				'WBTC_Button_Text_Changer'
			)
		) {
			return '';
		}

		$manager =
			WBTC_Button_Text_Changer::instance();

		return $manager->get_button_text(
			$button_type
		);
	}

	/**
	 * Get fallback button label.
	 *
	 * @param string $button_type Button setting key.
	 * @return string
	 */
	private function get_default_button_text( $button_type ) {

		$defaults = array(
			'add_to_cart' => __(
				'Add to Cart',
				'button-text-changer-for-woocommerce'
			),
			'view_cart' => __(
				'View Cart',
				'button-text-changer-for-woocommerce'
			),
			'update_cart' => __(
				'Update Cart',
				'button-text-changer-for-woocommerce'
			),
			'checkout' => __(
				'Proceed to Checkout',
				'button-text-changer-for-woocommerce'
			),
			'place_order' => __(
				'Place Order',
				'button-text-changer-for-woocommerce'
			),
			'read_more' => __(
				'Read More',
				'button-text-changer-for-woocommerce'
			),
		);

		return isset( $defaults[ $button_type ] )
			? $defaults[ $button_type ]
			: $defaults['add_to_cart'];
	}

	/**
	 * Render widget output.
	 *
	 * @return void
	 */
	protected function render() {

		$settings = $this->get_settings_for_display();

		$button_type =
			! empty( $settings['button_type'] )
				? sanitize_key(
					$settings['button_type']
				)
				: 'add_to_cart';

		$button_text =
			isset( $settings['button_text'] )
				? trim(
					sanitize_text_field(
						$settings['button_text']
					)
				)
				: '';

		/*
		 * Use global setting when the widget text is empty.
		 */
		if ( '' === $button_text ) {

			$button_text =
				$this->get_global_button_text(
					$button_type
				);
		}

		/*
		 * Use WooCommerce/default label when
		 * no global setting exists.
		 */
		if ( '' === $button_text ) {

			$button_text =
				$this->get_default_button_text(
					$button_type
				);
		}

		$url = '';

		if (
			isset( $settings['button_url']['url'] )
			&&
			'' !== $settings['button_url']['url']
		) {

			$url = esc_url(
				$settings['button_url']['url']
			);
		} else {

			/*
			 * Provide useful WooCommerce defaults.
			 */
			switch ( $button_type ) {

				case 'view_cart':
					$url = function_exists( 'wc_get_cart_url' )
						? wc_get_cart_url()
						: '';

					break;

				case 'checkout':
					$url = function_exists( 'wc_get_checkout_url' )
						? wc_get_checkout_url()
						: '';

					break;

				default:
					$url = '';
			}
		}

		$size =
			! empty( $settings['button_size'] )
				? sanitize_html_class(
					$settings['button_size']
				)
				: 'default';

		$button_classes = array(
			'elementor-button',
			'wbtc-elementor-button',
			'wbtc-button-type-' . $button_type,
		);

		if ( 'default' !== $size ) {
			$button_classes[] =
				'wbtc-button-size-' . $size;
		}

		$button_class =
			implode(
				' ',
				array_map(
					'sanitize_html_class',
					$button_classes
				)
			);

		?>

		<div class="wbtc-elementor-button-wrapper">

			<?php if ( '' !== $url ) : ?>

				<a
					class="<?php echo esc_attr( $button_class ); ?>"
					href="<?php echo esc_url( $url ); ?>"
					<?php
					if (
						! empty(
							$settings['button_url']['is_external']
						)
					) {
						echo ' target="_blank"';
					}
					?>
					<?php
					if (
						! empty(
							$settings['button_url']['nofollow']
						)
					) {
						echo ' rel="nofollow"';
					}
					?>
				>
					<?php echo esc_html( $button_text ); ?>
				</a>

			<?php else : ?>

				<button
					type="button"
					class="<?php echo esc_attr( $button_class ); ?>"
				>
					<?php echo esc_html( $button_text ); ?>
				</button>

			<?php endif; ?>

		</div>

		<?php
	}
}