* @author : Premio <contact@premio.io>
use CHT\includes\CHT_Widget;
if (defined('ABSPATH') === false) {
require_once 'class-social-icons.php';
* Admin base class for the Chaty plugin.
protected static $response = null;
* Constructor for the class
public function __construct()
$plugin = CHT_Widget::get_instance();
$this->pluginSlug = $plugin->get_plugin_slug();
$this->socials = CHT_Social_Icons::get_instance()->get_icons_list();
$this->colors = CHT_Social_Icons::get_instance()->get_colors();
$this->chatwayIcons = CHT_Social_Icons::get_instance()->get_chatway_icons();
$this->token = $this->get_token();
$this->upgradeSlug = $this->pluginSlug.'-upgrade';
add_action('admin_menu', [$this, 'cht_admin_setting_page']);
// Adds all of the options for the administrative settings
add_action('admin_init', [$this, 'cht_register_inputs']);
add_action('admin_init', [$this, 'change_menu_text']);
add_action('admin_head', [$this, 'cht_inline_css_admin']);
add_action('init', [$this, 'chaty_text']);
// add_action('updated_option', array($this, 'cht_clear_all_caches'));
add_action('wp_ajax_wcp_admin_send_message_to_owner', [$this, 'wcp_admin_send_message_to_owner']);
// ADD Upgrade link to plugin
add_filter('plugin_action_links_'.CHT_PLUGIN_BASE, [$this, 'plugin_action_links']);
add_action('admin_footer', [$this, 'add_deactivate_modal']);
add_action('wp_ajax_chaty_plugin_deactivate', [$this, 'chaty_plugin_deactivate']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_styles'], 99);
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts'], 99);
add_action("wp_ajax_chaty_update_popup_status", [$this, 'update_popup_status']);
add_action("wp_ajax_update_channel_setting", [$this, 'update_channel_setting']);
add_action('wp_ajax_hide_chaty_cta', array($this, 'hide_chaty_cta'));
add_action("admin_head", [$this, "admin_head"]);
add_action("admin_init", [$this, "check_for_redirection"]);
add_action('wp_ajax_get_chatway_status', [$this, 'get_chatway_status']);
add_filter('check_for_chatway', [$this, 'check_for_chatway']);
add_filter('check_for_chatway_status', [$this, 'check_for_chatway_status']);
// add need help in footer
add_action('admin_footer', array($this, 'chaty_admin_footer_need_help_content'));
function get_chatway_status()
echo $this->check_for_chatway_status();
public function check_for_chatway()
$plugin = 'chatway-live-chat/chatway.php';
return is_plugin_active($plugin);
public function check_for_chatway_status()
if ( !function_exists( 'get_plugins' ) || !function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$all_plugins = get_plugins();
$plugin = 'chatway-live-chat/chatway.php';
if (!isset($all_plugins[$plugin])) {
} else if(!is_plugin_active($plugin)){
} else if(!get_option( 'chatway_user_identifier', false )) {
$token = get_option( 'chatway_token', '' );
$user_id = get_option( 'chatway_user_identifier', '' );
if( empty( $token ) || empty( $user_id ) ) {
$response = wp_safe_remote_get(
'https://prod-api.chatway.app/api/profile?include[]=',
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token
if (is_wp_error($response)) {
$response = json_decode(wp_remote_retrieve_body($response), true);
if(isset($response['message']) && $response['message'] == 'Unauthenticated.') {
delete_option( 'chatway_token' );
delete_option( 'chatway_user_identifier' );
} else if(!isset($response['data']['attributes']['registration_step']) || $response['data']['attributes']['registration_step'] != 'ONBOARDED') {
* Checks for redirection and performs the necessary actions if certain conditions are met.
function check_for_redirection() {
if(isset($_GET['page']) && $_GET['page'] == 'cht-manage-live-chat') {
wp_redirect(admin_url('admin.php?page=chatway'));
if(!defined("DOING_AJAX")) {
$chaty_status = get_option("cht_redirect");
delete_option("cht_redirect");
wp_redirect(admin_url("admin.php?page=chaty-app"));
$page = isset($_GET['page']) ? $_GET['page'] : "";
if (in_array($page, ["widget-analytics", "chaty-contact-form-feed", "recommended-chaty-plugins", "chaty-app-upgrade"])) {
$is_shown = \CHT_SIGNUP_CLASS::check_modal_status();
wp_redirect(admin_url("admin.php?page=chaty-app"));
* Add custom CSS to the wp-admin head section
#adminmenu .toplevel_page_chaty-app > ul > li:last-child {
#adminmenu .toplevel_page_chaty-app > ul > li:last-child a {
background-color: #B78DEB;
transition: all 0.2s linear;
box-shadow: 0px 6px 8px 0px #B78DEB3D;
#adminmenu .toplevel_page_chaty-app > ul > li:last-child a:hover, #adminmenu .toplevel_page_chaty-app > ul > li:last-child a.current {
box-shadow: 0px 6px 8px 0px #B78DEB3D;
background-color: #9565d0;
#adminmenu .toplevel_page_chaty-app > ul > li:last-child a span {
background-color: #c5a4ef;
transition: all 0.2s linear;
#adminmenu .toplevel_page_chaty-app > ul > li:last-child a:hover span {
background-color: #B78DEB;
#adminmenu .toplevel_page_chaty-app > ul > li:last-child a span svg {
* This function is used to hide the Chaty Call-To-Action (CTA) based on the provided input.
* It checks the validity of the request by verifying the nonce value.
* If the request is valid, it sets the "hide_chaty_cta" option to "yes" and returns a success response.
* If the request is not valid, it returns an error response.
* @global array $_POST The input data received via POST request
* @global WP_REST_Response $response The JSON response object
function hide_chaty_cta()
$response['data'] = array();
$response['message'] = "";
$postData = filter_input_array(INPUT_POST);
if (!isset($postData['nonce']) || empty($postData['nonce'])) {
$response['message'] = esc_html__("Your request is not valid", 'chaty');
$nonce = esc_attr($postData['nonce']);
if(!wp_verify_nonce($nonce, 'hide_chaty_cta')) {
$response['message'] = esc_html__("Your request is not valid", 'chaty');
add_option("hide_chaty_cta", "yes");
wp_send_json($response); die;
* Add custom links to the plugin action links on the plugin page
* @param array $links An array of plugin action links.
* @return array The modified plugin action links.
public function plugin_action_links($links)
$links['need_help'] = '<a target="_blank" href="https://wordpress.org/support/plugin/chaty/" >'.__('Need help?', 'chaty').'</a>';
$links['go_pro'] = '<a style="color: #FF5983; font-weight: bold; display: inline-block; border: solid 1px #FF5983; border-radius: 4px; padding: 0 5px;" href="'.CHT_PRO_URL.'" class="chaty-plugins-gopro">'.esc_attr__('Upgrade', 'chaty').'</a>';
}//end plugin_action_links()
* Deactivate the Chaty plugin.
public function chaty_plugin_deactivate()
$response['message'] = "";
$reason = filter_input(INPUT_POST, 'reason');
$nonce = filter_input(INPUT_POST, 'nonce');
$response['message'] = "Please provide reason";
} else if (empty($nonce)) {
$response['message'] = esc_attr__("Your request is not valid", 'chaty');
} else if (!current_user_can("manage_options")) {
$response['message'] = esc_attr__("Your request is not valid", 'chaty');
if (!wp_verify_nonce($nonce, 'chaty_deactivate_nonce')) {
$response['message'] = esc_attr__("Your request is not valid", 'chaty');
if ($errorCounter == 0) {
$email = "none@none.none";
$emailId = filter_input(INPUT_POST, 'email_id');
if (isset($emailId) && !empty($emailId) && filter_var($emailId, FILTER_VALIDATE_EMAIL)) {
$user_name = $current_user->first_name." ".$current_user->last_name;
// sending message to Crisp
$messageData['key'] = "Plugin";
$messageData['value'] = "Chaty";
$postMessage[] = $messageData;
$messageData['key'] = "Plugin Version";
$messageData['value'] = CHT_VERSION;
$postMessage[] = $messageData;
$messageData['key'] = "Domain";
$messageData['value'] = $domain;
$postMessage[] = $messageData;
$messageData['key'] = "Email";
$messageData['value'] = $email;
$postMessage[] = $messageData;
$messageData['key'] = "WordPress Version";
$messageData['value'] = esc_attr(get_bloginfo('version'));
$postMessage[] = $messageData;
$messageData['key'] = "PHP Version";
$messageData['value'] = PHP_VERSION;
$postMessage[] = $messageData;
$messageData['key'] = "Message";
$messageData['value'] = $reason;
$postMessage[] = $messageData;
'message' => $postMessage,
// Sending message to Crisp API
$apiResponse = wp_safe_remote_post("https://premioapps.com/premio/send-message-api.php", ['body' => $apiParams, 'timeout' => 15, 'sslverify' => true]);
if (is_wp_error($apiResponse)) {
wp_safe_remote_post("https://premioapps.com/premio/send-message-api.php", ['body' => $apiParams, 'timeout' => 15, 'sslverify' => false]);
}//end chaty_plugin_deactivate()
* @param mixed $value The value to be sanitized
* @return mixed The sanitized value
public static function chaty_sanitize_options($value)
$value = stripslashes($value);
$value = filter_var($value);
$value = htmlspecialchars($value);
}//end chaty_sanitize_options()
* Add deactivate modal to the plugin's settings page
* This function is responsible for checking if the current user has the capability to manage options. If the user
* has the capability, it checks if the current page is the plugins.php page. If it is, it includes the chaty-deactivate-form.php
* file in order to display the deactivate modal.
public function add_deactivate_modal()
if (current_user_can("manage_options")) {
if ('plugins.php' !== $pagenow) {
include CHT_DIR.'/views/admin/chaty-deactivate-form.php';
}//end add_deactivate_modal()
* Remove underscores and replace them with spaces in a given text
* @param string $text The text to remove underscores from
* @return string The modified text with spaces instead of underscores
public function del_space($text)
return str_replace('_', ' ', $text);
* Generate inline CSS for admin menu styling
* @return string Inline CSS for admin menu styling
public function cht_inline_css_admin()
#toplevel_page_chaty-app img:hover, #toplevel_page_chaty-app img {