<?php
/**
 * Elementor and Beaver Builder integrations for Square payment links.
 *
 * @package SHC4WC
 */

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

class SHC4WC_Page_Builders {
	/** @var self|null */
	private static $instance = null;

	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		add_action( 'elementor/widgets/register', array( $this, 'register_elementor_widgets' ) );
		add_action( 'elementor/widgets/widgets_registered', array( $this, 'register_elementor_widgets' ) );
		add_action( 'fl_builder_loaded', array( $this, 'load_beaver_builder_module' ) );
	}

	/**
	 * @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager.
	 */
	public function register_elementor_widgets( $widgets_manager ) {
		static $registered = false;
		if ( $registered ) {
			return;
		}
		$registered = true;
		if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
			return;
		}

		require_once SHC4WC_PLUGIN_DIR . 'includes/elementor/class-shc4wc-elementor-payment-link.php';

		$widgets_manager->register( new SHC4WC_Elementor_Payment_Link() );
	}

	public function load_beaver_builder_module() {
		if ( ! class_exists( 'FLBuilder' ) || ! class_exists( 'FLBuilderModule' ) ) {
			return;
		}

		require_once SHC4WC_PLUGIN_DIR . 'includes/beaver-builder/modules/payment-link/payment-link.php';
	}
}
