
<?php
/**
 * Elementor Integration.
 *
 * Registers the WooCommerce Button Text Changer widget.
 *
 * @package WooCommerce_Button_Text_Changer
 */

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

/**
 * Elementor integration class.
 */
final class WBTC_Elementor {

	/**
	 * Singleton instance.
	 *
	 * @var WBTC_Elementor|null
	 */
	private static $instance = null;

	/**
	 * Get singleton instance.
	 *
	 * @return WBTC_Elementor
	 */
	public static function instance() {

		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Constructor.
	 */
	private function __construct() {

		/*
		 * Register custom Elementor category.
		 */
		add_action(
			'elementor/elements/categories_registered',
			array(
				$this,
				'register_category',
			)
		);

		/*
		 * Register custom widget.
		 */
		add_action(
			'elementor/widgets/register',
			array(
				$this,
				'register_widgets',
			)
		);
	}

	/**
	 * Register custom Elementor category.
	 *
	 * @param object $elements_manager Elementor elements manager.
	 * @return void
	 */
	public function register_category(
		$elements_manager
	) {

		$elements_manager->add_category(
			'wbtc-elements',
			array(
				'title' => __(
					'WooCommerce Button Changer',
					'button-text-changer-for-woocommerce'
				),
				'icon'  => 'eicon-button',
			)
		);
	}

	/**
	 * Register Elementor widgets.
	 *
	 * @param object $widgets_manager Elementor widgets manager.
	 * @return void
	 */
	public function register_widgets(
		$widgets_manager
	) {

		$widget_file =
			WBTC_PATH .
			'elementor/widgets/class-woocommerce-button-widget.php';

		/*
		 * Stop safely if the widget file does not exist.
		 */
		if ( ! file_exists( $widget_file ) ) {
			return;
		}

		require_once $widget_file;

		/*
		 * Register the widget only if the class exists.
		 */
		if (
			class_exists(
				'WBTC_WooCommerce_Button_Widget'
			)
		) {

			$widgets_manager->register(
				new \WBTC_WooCommerce_Button_Widget()
			);
		}
	}
}