use CHT\admin\CHT_Admin_Base;
use CHT\admin\CHT_PRO_Social_Icons;
use CHT\admin\CHT_Social_Icons;
if (defined('ABSPATH') === false) {
$adminBase = CHT_ADMIN_INC.'/class-admin-base.php';
$socialIcons = CHT_ADMIN_INC.'/class-social-icons.php';
require_once $socialIcons;
* Class for managing frontend functionalities.
class CHT_Frontend extends CHT_Admin_Base
* Holds the number of widgets.
* Whether the font is enabled or not.
* @var bool $hasFont True if the font is enabled, false otherwise.
* Whether the font is enabled or not.
* @var bool $hasFont True if the email field is added, false otherwise.
public $hasEmail = false;
* Whether the font is enabled or not.
* @var bool $hasEmoji True if the emoji is enabled, false otherwise.
public $hasEmoji = false;
public $hasChatway = false;
public $chatwayIcons = [];
* Initializes the class properties and sets up the necessary actions and filters.
public function __construct()
$this->socials = CHT_Social_Icons::get_instance()->get_icons_list();
$this->chatwayIcons = CHT_Social_Icons::get_instance()->get_chatway_icons();
add_action('wp_ajax_choose_social', [$this, 'choose_social_handler']);
add_action('wp_ajax_get_chaty_settings', [$this, 'get_chaty_settings']);
// Return setting for a social media in html.
add_action('wp_ajax_chaty_front_form_save_data', [$this, 'chaty_front_form_save_data']);
add_action('wp_ajax_nopriv_chaty_front_form_save_data', [$this, 'chaty_front_form_save_data']);
add_action('wp_ajax_remove_chaty_widget', [$this, 'remove_chaty_widget']);
add_action('wp_ajax_rename_chaty_widget', array($this, 'rename_chaty_widget')); // rename social media widget
// Remove social media widget.
add_action('wp_ajax_change_chaty_widget_status', [$this, 'change_chaty_widget_status']);
add_action('wp_ajax_update_chaty_view', [$this, 'update_chaty_view']);
add_action('wp_ajax_nopriv_update_chaty_view', [$this, 'update_chaty_view']);
// Remove social media widget.
$inEditors = $this->check_for_editors();
add_action('wp_enqueue_scripts', [$this, 'cht_front_end_css_and_js']);
* Update chaty view function
* This function updates the chaty view count when triggered.
public function update_chaty_view() {
if(isset($_POST['token'])) {
$token = sanitize_text_field($_POST['token']);
if(wp_verify_nonce($token, "update_chaty_view")) {
if(!get_option("chaty_views")) {
add_option("chaty_views", 1);
public function rename_chaty_widget() {
if (current_user_can('manage_options')) {
$widget_index = sanitize_text_field($_POST['widget_index']);
$widget_nonce = sanitize_text_field($_POST['widget_nonce']);
$widget_title = sanitize_text_field($_POST['widget_title']);
if (isset($widget_index) && !empty($widget_index) && !empty($widget_nonce) && wp_verify_nonce($widget_nonce, "chaty_remove_" . $widget_index)) {
$index = trim($index, "_");
update_option("cht_widget_title", $widget_title);
update_option("cht_widget_title_" . $index, $widget_title);
echo esc_url(admin_url("admin.php?page=chaty-app"));
* Checks if the current page is being edited in a page builder.
* @return int Returns 1 if the page is being edited in a page builder, 0 otherwise.
function check_for_editors()
$isElementor = isset($_GET['elementor-preview']) ? 1 : 0;
$isCtBuilder = isset($_GET['ct_builder']) ? 1 : 0;
$isDiviTheme = isset($_GET['et_fb']) ? 1 : 0;
$isZionBuilder = isset($_GET['zionbuilder-preview']) ? 1 : 0;
$isSiteOrigin = isset($_GET['siteorigin_panels_live_editor']) ? 1 : 0;
$flBuilder = isset($_GET['fl_builder']) ? 1 : 0;
return ($isCtBuilder || $isElementor || $isDiviTheme || $isZionBuilder || $isSiteOrigin || $flBuilder) ? 1 : 0;
}//end check_for_editors()
* To remove a chaty widget
public function remove_chaty_widget()
if (current_user_can('manage_options')) {
$widgetIndex = filter_input(INPUT_POST, 'widget_index');
$widgetNonce = filter_input(INPUT_POST, 'widget_nonce');
if (isset($widgetIndex) && !empty($widgetIndex) && !empty($widgetNonce) && wp_verify_nonce($widgetNonce, "chaty_remove_".$widgetIndex)) {
delete_option("cht_active");
delete_option("chaty_icons_view");
delete_option("chaty_icons_view");
delete_option("cht_cta_text_color");
delete_option("cht_cta_bg_color");
delete_option("cht_pending_messages");
delete_option("cht_number_of_messages");
delete_option("cht_number_color");
delete_option("cht_number_bg_color");
delete_option("cht_cta_switcher");
delete_option("chaty_attention_effect");
delete_option("chaty_default_state");
delete_option("chaty_trigger_on_time");
delete_option("chaty_trigger_time");
delete_option("chaty_trigger_on_exit");
delete_option("chaty_trigger_on_scroll");
delete_option("chaty_trigger_on_page_scroll");
delete_option("cht_close_button");
delete_option("cht_close_button_text");
delete_option("chaty_updated_on");
delete_option("cht_widget_title");
delete_option("cht_widget_font");
delete_option("cta_type");
delete_option("cta_heading_text");
delete_option("cta_body_text");
delete_option("cta_header_text_color");
delete_option("cta_header_bg_color");
foreach ($this->socials as $social) {
delete_option('cht_social_'.$social['slug']);
update_option('cht_devices', $options);
update_option('cht_position', 'right');
update_option('cht_cta', 'Contact us');
update_option('cht_numb_slug', ',Phone,Whatsapp');
update_option('cht_social_whatsapp', '');
update_option('cht_social_phone', '');
update_option('cht_widget_size', '54');
update_option('widget_icon', 'chat-base');
update_option('cht_widget_img', '');
update_option('cht_color', '#A886CD');
echo esc_url(admin_url("admin.php?page=chaty-app"));
}//end remove_chaty_widget()
* Change the status of the Chaty widget
public function change_chaty_widget_status()
if (current_user_can('manage_options')) {
$widgetIndex = filter_input(INPUT_POST, 'widget_index');
$widgetNonce = filter_input(INPUT_POST, 'widget_nonce');
if (isset($widgetIndex) && !empty($widgetIndex) && !empty($widgetNonce) && wp_verify_nonce($widgetNonce, "chaty_remove_".$widgetIndex)) {
$widgetIndex = trim($widgetIndex, "_");
if (empty($widgetIndex) || $widgetIndex == 0) {
$widgetIndex = "_".$widgetIndex;
$status = get_option("cht_active".$widgetIndex);
update_option("cht_active".$widgetIndex, 0);
update_option("cht_active".$widgetIndex, 1);
}//end change_chaty_widget_status()
function chaty_front_form_save_data()
$postData = filter_input_array(INPUT_POST);
$widgetIndex = $postData['widget'];
if(empty($widgetIndex)) {
if (isset($postData['nonce']) && isset($postData['widget']) && wp_verify_nonce($postData['nonce'], "chaty_widget_nonce".$widgetIndex)) {
$name = isset($postData['name']) ? $postData['name'] : "";
$phone = isset($postData['phone']) ? $postData['phone'] : "";
$email = isset($postData['email']) ? $postData['email'] : "";
$message = isset($postData['message']) ? $postData['message'] : "";
$refURL = isset($postData['ref_url']) ? $postData['ref_url'] : "";
$widget = $postData['widget'];
$channel = $postData['channel'];
$value = get_option('cht_social_'.$channel);
// get saved settings for button
$fieldSetting = isset($value['name']) ? $value['name'] : [];
if (isset($fieldSetting['is_active']) && $fieldSetting['is_active'] == "yes" && isset($fieldSetting['is_required']) && $fieldSetting['is_required'] == "yes" && empty($name)) {
'field' => 'chaty-field-name',
'message' => esc_html__("this field is required", 'chaty'),
$fieldSetting = isset($value['phone']) ? $value['phone'] : [];
if (isset($fieldSetting['is_active']) && $fieldSetting['is_active'] == "yes" && isset($fieldSetting['is_required']) && $fieldSetting['is_required'] == "yes" && empty($phone)) {
'field' => 'chaty-field-phone',
'message' => esc_html__("this field is required", 'chaty'),
$fieldSetting = isset($value['email']) ? $value['email'] : [];
if (isset($fieldSetting['is_active']) && $fieldSetting['is_active'] == "yes" && isset($fieldSetting['is_required']) && $fieldSetting['is_required'] == "yes") {
'field' => 'chaty-field-email',
'message' => esc_html__("this field is required", 'chaty'),
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
'field' => 'chaty-field-email',
'message' => esc_html__("email address is not valid", 'chaty'),
$fieldSetting = isset($value['message']) ? $value['message'] : [];
if (isset($fieldSetting['is_active']) && $fieldSetting['is_active'] == "yes" && isset($fieldSetting['is_required']) && $fieldSetting['is_required'] == "yes" && empty($message)) {
'field' => 'chaty-field-message',
'message' => esc_html__("this field is required", 'chaty'),
$widget = trim($widget, "_");
$response['message'] = esc_attr($value['thanks_message']);
$response['redirect_action'] = esc_attr($value['redirect_action']);
$response['redirect_link'] = esc_url($value['redirect_link']);
$response['link_in_new_tab'] = esc_attr($value['link_in_new_tab']);
$response['close_form_after'] = esc_attr($value['close_form_after']);
$response['close_form_after_seconds'] = esc_attr($value['close_form_after_seconds']);
$currentDate = gmdate("Y-m-d H:i:s");
$chatyTable = $wpdb->prefix.'chaty_contact_form_leads';
$fieldSetting = isset($value['name']) ? $value['name'] : [];
if (isset($fieldSetting['is_active']) && $fieldSetting['is_active'] == "yes") {
$insert['name'] = esc_sql(sanitize_text_field($name));
$fieldSetting = isset($value['email']) ? $value['email'] : [];
if (isset($fieldSetting['is_active']) && $fieldSetting['is_active'] == "yes") {
$insert['email'] = esc_sql(sanitize_text_field($email));
$fieldSetting = isset($value['phone']) ? $value['phone'] : [];
if (isset($fieldSetting['is_active']) && $fieldSetting['is_active'] == "yes") {
$insert['phone_number'] = esc_sql(sanitize_text_field($phone));
$fieldSetting = isset($value['message']) ? $value['message'] : [];
if (isset($fieldSetting['is_active']) && $fieldSetting['is_active'] == "yes") {
$insert['message'] = esc_sql(sanitize_text_field($message));
$insert['ref_page'] = esc_url(esc_sql(sanitize_text_field($refURL)));
$insert['ip_address'] = "";
$insert['widget_id'] = esc_sql(sanitize_text_field($widget));
$insert['created_on'] = esc_sql($currentDate);
$wpdb->insert($chatyTable, $insert);
$showFirst = get_option("show_first_chaty_lead_box");
if ($showFirst === false) {
add_option("show_first_chaty_lead_box", 1);
$response['errors'] = $errors;
$response['message'] = "Invalid request, Please try again";
$response['message'] = "Invalid request, Please try again";
echo wp_json_encode($response);
}//end chaty_front_form_save_data()
* To add front-end CSS and JS for the chat widget.
function cht_front_end_css_and_js()
if ($this->canInsertWidget()) :
// Initialize widget if widget is enable for current page
$social = $this->get_social_icon_list();
$chtActive = get_option("cht_active");
// $bgColor = $this->get_current_color();
// get custom background color for widget
$defColor = get_option('cht_color');
$customColor = get_option('cht_custom_color');
// checking for custom color
if (!empty($customColor)) {
delete_option("cht_custom_color");
update_option("cht_color", $defColor);
$bgColor = strtoupper($color);
// get total active channels
$cta = nl2br(get_option('cht_cta'));
// $cta = str_replace(array("\r", "\n"), "", $cta);
$cta = str_replace("'", "'", $cta);
$cta = str_replace("'", "'", $cta);
$cta = esc_attr(wp_unslash($cta));
$cta = html_entity_decode($cta);
$positionSide = get_option('positionSide');
$chtBottomSpacing = get_option('cht_bottom_spacing');
// get widget position from bottom
$chtSideSpacing = get_option('cht_side_spacing');
// get widget position from left/Right
$chtWidgetSize = get_option('cht_widget_size');
$positionSide = empty($positionSide) ? 'right' : $positionSide;
// Initialize widget position if not exists
$chtSideSpacing = ($chtSideSpacing) ? $chtSideSpacing : '25';
// Initialize widget from left/Right if not exists
$chtWidgetSize = ($chtWidgetSize) ? $chtWidgetSize : '54';
// Initialize widget size if not exists
$position = get_option('cht_position');
$position = ($position) ? $position : 'right';
// Initialize widget position if not exists
$total = ($chtSideSpacing + $chtWidgetSize + $chtSideSpacing);
$chtBottomSpacing = ($chtBottomSpacing) ? $chtBottomSpacing : '25';
// Initialize widget bottom position if not exists
$chtSideSpacing = ($chtSideSpacing) ? $chtSideSpacing : '25';
// Initialize widget left/Right position if not exists
$imageUrl = plugin_dir_url("")."chaty-pro/admin/assets/images/chaty-default.png";
// Initialize default image
$analytics = get_option("cht_google_analytics");
// check for google analytics enable or not
$analytics = empty($analytics) ? 0 : $analytics;
// Initialize google analytics flag to 0 if not data not exists
$text = get_option("cht_close_button_text");
$close_text = ($text === false) ? "Hide" : $text;
$imageData = wp_get_attachment_image_src($imageId, "full");
if (!empty($imageData) && is_array($imageData)) {
$imageUrl = $imageData[0];
// change close button image if exists
$fontFamily = get_option('cht_widget_font');
if ($fontFamily == "System Stack") {
$fontFamily = "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif";