<?php

class DCBP_Public
{

	private $plugin_name;
	private $version;

	public function __construct($plugin_name, $version)
	{

		$this->plugin_name = $plugin_name;
		$this->version = $version;
	}


	public function enqueue_scripts_item() {
		// Get the token from the theme options
		$dt_token = carbon_get_theme_option('dartamas_token');
	
		// Register the external script from sipOnline
		wp_register_script('dartamas_script', 'https://my.siponline.ir/callButton.js', array(), '1.0.0', true);
	
		// Check if the token exists
		if ($dt_token) {
			// Enqueue the registered script
			wp_enqueue_script('dartamas_script');
	
			// Create the inline script
			$dt_script = 'document.getElementById("'.$dt_token.'").setAttribute("data-position", "bottom_right");
						  document.getElementById("'.$dt_token.'").setAttribute("data-margin", "20");
						  document.getElementById("'.$dt_token.'").setAttribute("data-url", "sipOnline");';
	
			// Add the inline script to the previously declared script
			wp_add_inline_script('dartamas_script', $dt_script);
		}
	}
	
	
	public function enqueue_scripts()
	{
		if (class_exists('FLBuilderModel') && FLBuilderModel::is_builder_active()) {
			return;
		}

		if (class_exists('\Elementor\Plugin') && \Elementor\Plugin::$instance->preview->is_preview()) {
			return;
		}	
		
		$dt_token = carbon_get_theme_option('dartamas_token');		
		if ($dt_token) {
			$dt_selected_page_only = carbon_get_theme_option('dartamas_selected_page_only');
			$dt_selected_page_post = carbon_get_the_post_meta('dartamas_selected_page_post');
			if($dt_selected_page_post == true){
				?>
					<script id="<?php echo esc_js($dt_token); ?>" src="https://my.siponline.ir/callButton.js" data-position="bottom_right" data-margin="20" data-url="sipOnline"></script>
				<?php

			}
			
				if ($dt_selected_page_only == true) {
					$page = get_post() ?? null;
					$pageId [] =$page->ID;
					$dt_exclude_pages = carbon_get_theme_option('dartamas_exclude_pages');
					if (array_intersect($pageId, $dt_exclude_pages)) {
							return;
					}
					else
					?>
					<script id="<?php echo esc_js($dt_token); ?>" src="https://my.siponline.ir/callButton.js" data-position="bottom_right" data-margin="20" data-url="sipOnline"></script>
					<?php
				}
				else
				{
					$page = get_post() ?? null;
					$pageId [] =$page->ID;
					$dt_selected_pages = carbon_get_theme_option('dartamas_selected_pages');
					if (!array_intersect($pageId, $dt_selected_pages)) {
						return;
					}
					else
					?>
					<script id="<?php echo esc_js($dt_token); ?>" src="https://my.siponline.ir/callButton.js" data-position="bottom_right" data-margin="20" data-url="sipOnline"></script>
					<?php
				}
			
		}
	}
	
		
}
