Plugin Directory

Changeset 2628223


Ignore:
Timestamp:
11/11/2021 05:54:04 PM (4 years ago)
Author:
darklrd
Message:

iFlyChat backend update

Location:
iflychat/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • iflychat/trunk/iflychat.php

    r2100434 r2628223  
    11<?php
    22/**
     3 *
     4 * Plugin Name: iFlyChat
     5 * Plugin URI: http://wordpress.org/extend/plugins/iflychat/
     6 * Description: One on one chat, Multiple chatrooms, Embedded chatrooms
     7 * Author: iFlyChat Team
     8 * Version: 4.7.0
     9 * Author URI: https://iflychat.com/
     10 *
    311 * @package iflychat
    4  * @version 4.6.4
    5  */
     12 * @version 4.7.0
     13 *
     14 * Exit if accessed directly
     15 */
     16
     17if ( ! defined( 'ABSPATH' ) ) {
     18    die( 'Access Denied' );
     19}
     20
     21if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
     22    require_once ABSPATH . '/wp-admin/includes/plugin.php';
     23}
     24
     25if ( '' === session_id() ) {
     26    session_start();
     27}
     28
     29if ( ! defined( 'DRUPALCHAT_EXTERNAL_HOST' ) ) {
     30    define( 'DRUPALCHAT_EXTERNAL_HOST', 'http://api.iflychat.com' );
     31}
     32
     33if ( ! defined( 'DRUPALCHAT_EXTERNAL_PORT' ) ) {
     34    define( 'DRUPALCHAT_EXTERNAL_PORT', '80' );
     35}
     36
     37if ( ! defined( 'DRUPALCHAT_EXTERNAL_A_HOST' ) ) {
     38    define( 'DRUPALCHAT_EXTERNAL_A_HOST', 'https://api.iflychat.com' );
     39}
     40
     41if ( ! defined( 'DRUPALCHAT_EXTERNAL_A_PORT' ) ) {
     42    define( 'DRUPALCHAT_EXTERNAL_A_PORT', '443' );
     43}
     44
     45if ( ! defined( 'DRUPALCHAT_EXTERNAL_CDN_HOST' ) ) {
     46    define( 'DRUPALCHAT_EXTERNAL_CDN_HOST', 'cdn.iflychat.com' );
     47}
     48
     49if ( ! defined( 'CHATCAMP_EXTERNAL_HOST' ) ) {
     50    define( 'CHATCAMP_EXTERNAL_HOST', 'http://api.chatcamp.io' );
     51}
     52
     53if ( ! defined( 'CHATCAMP_EXTERNAL_PORT' ) ) {
     54    define( 'CHATCAMP_EXTERNAL_PORT', '80' );
     55}
     56
     57if ( ! defined( 'CHATCAMP_EXTERNAL_A_HOST' ) ) {
     58    define( 'CHATCAMP_EXTERNAL_A_HOST', 'https://api.chatcamp.io' );
     59}
     60
     61if ( ! defined( 'CHATCAMP_EXTERNAL_A_PORT' ) ) {
     62    define( 'CHATCAMP_EXTERNAL_A_PORT', '443' );
     63}
     64
     65if ( ! defined( 'CHATCAMP_EXTERNAL_CDN_HOST' ) ) {
     66    define( 'CHATCAMP_EXTERNAL_CDN_HOST', 'cdn.chatcamp.io' );
     67}
     68
     69define( 'IFLYCHAT_PLUGIN_VERSION', 'WP-4.7.0' );
     70if ( ! defined( 'IFLYCHAT_DEBUG' ) ) {
     71    define( 'IFLYCHAT_DEBUG', false );
     72}
     73
     74
     75/* define constants */
     76if ( ! defined( 'IFLYCHAT_DIR' ) ) {
     77    define( 'IFLYCHAT_DIR', plugin_dir_path( __FILE__ ) );
     78}
     79if ( ! defined( 'IFLYCHAT_URL' ) ) {
     80    define( 'IFLYCHAT_URL', plugin_dir_url( __FILE__ ) );
     81}
     82
     83$iflychat_engine = true;
     84
     85/**
     86 * Function to get Session Token.
     87 *
     88 * @since 4.7.0
     89 * @return String $session_token as String.
     90 */
     91function iflychat_get_hash_session() {
     92    $data = uniqid( mp_rand(), true );
     93    $hash = base64_encode( hash( 'sha256', $data, true ) ); //phpcs:ignore
     94
     95    $session_token = strtr(
     96        $hash,
     97        array(
     98            '+' => '-',
     99            '/' => '_',
     100            '=' => '',
     101        )
     102    );
     103    return $session_token;
     104}
     105
     106/**
     107 * Function to get User ID.
     108 *
     109 * @since 4.7.0
     110 * @return String user ID as String.
     111 */
     112function iflychat_get_user_id() {
     113    $current_user = wp_get_current_user();
     114    if ( $current_user->ID ) {
     115        return strval( $current_user->ID );
     116    } else {
     117        return false;
     118    }
     119}
     120
     121/**
     122 * Function to get User's Display Name.
     123 *
     124 * @since 4.7.0
     125 * @return String user's Display Name as String or false.
     126 */
     127function iflychat_get_user_name() {
     128    $current_user   = wp_get_current_user();
     129    $hook_user_name = apply_filters( 'iflychat_get_username_filter', '', $current_user->ID );
     130
     131    if ( ! empty( $hook_user_name ) ) {
     132        return $hook_user_name;
     133    }
     134
     135    if ( $current_user->ID ) {
     136        if ( ! isset( $current_user->display_name ) || '' === trim( $current_user->display_name ) || ( '2' === iflychat_get_option( 'iflychat_use_display_name' ) ) ) {
     137            return $current_user->user_login;
     138        } else {
     139            return $current_user->display_name;
     140        }
     141    } else {
     142        return false;
     143    }
     144}
     145
     146
     147/**
     148 * Function to load script Async.
     149 *
     150 * @since 4.7.0
     151 * @param String $url String.
     152 * @return String $url String.
     153 */
     154function iflychat_async_scripts( $url ) {
     155    if ( false === strpos( $url, '#asyncload' ) ) {
     156        return $url;
     157    } elseif ( is_admin() ) {
     158        return str_replace( '#asyncload', '', $url ) . "' async='async";
     159    } else {
     160        return str_replace( '#asyncload', '', $url ) . "' async='async";
     161    }
     162}
     163add_filter( 'clean_url', 'iflychat_async_scripts' );
     164
     165/**
     166 * Function to Initialise the plugin.
     167 *
     168 * @since 4.7.0
     169 */
     170function iflychat_init() {
     171    $user_data = false;
     172    if ( iflychat_check_access() ) {
     173        $_iflychat_protocol = isset( $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
     174
     175        wp_enqueue_script( 'iflychat-ajax', IFLYCHAT_URL . 'js/iflychat.js', array( 'jquery' ), filemtime( IFLYCHAT_DIR . 'js/iflychat.js' ), true );
     176        wp_localize_script( 'iflychat-ajax', 'iflychat_chatcamp_check', iflychat_chatcamp_check() ? '1' : '0' );
     177        wp_localize_script( 'iflychat-ajax', 'iflychat_app_id', iflychat_get_option( 'iflychat_app_id' ) );
     178        wp_localize_script( 'iflychat-ajax', 'iflychat_external_cdn_host', iflychat_chatcamp_check() ? CHATCAMP_EXTERNAL_CDN_HOST : DRUPALCHAT_EXTERNAL_CDN_HOST );
     179        if ( is_user_logged_in() ) {
     180            //phpcs:ignore
     181            // $user_data = json_encode( _iflychat_get_user_auth() );
     182            $user_data = wp_json_encode( _iflychat_get_user_auth() );
     183        }
     184
     185        if ( '1' === iflychat_get_option( 'iflychat_session_caching' ) && isset( $_SESSION['user_data'] ) && $user_data === $_SESSION['user_data'] ) {
     186            //phpcs:ignore
     187            //if(iflychat_get_option('iflychat_enable_friends') == '1'){
     188            if ( isset( $_SESSION['token'] ) && ! empty( $_SESSION['token'] ) ) {
     189                wp_localize_script( 'iflychat-ajax', 'iflychat_auth_token', $_SESSION['token'] );
     190            }
     191        }
     192        if ( is_user_logged_in() ) {
     193            wp_localize_script( 'iflychat-ajax', 'iflychat_auth_url', admin_url( 'admin-ajax.php', $_iflychat_protocol ) );
     194        }
     195
     196        if ( '1' === iflychat_get_option( 'iflychat_popup_chat' ) ) {
     197            wp_enqueue_script( 'iflychat-popup', IFLYCHAT_URL . 'js/iflychat-popup.js', array(), filemtime( IFLYCHAT_DIR . 'js/iflychat-popup.js' ), true );
     198        } elseif ( '2' === iflychat_get_option( 'iflychat_popup_chat' ) && ! is_admin() ) {
     199            wp_enqueue_script( 'iflychat-popup', IFLYCHAT_URL . 'js/iflychat-popup.js', array(), filemtime( IFLYCHAT_DIR . 'js/iflychat-popup.js' ), true );
     200        } elseif ( ( '3' === iflychat_get_option( 'iflychat_popup_chat' ) || '4' === iflychat_get_option( 'iflychat_popup_chat' ) ) && iflychat_path_check() ) {
     201            wp_enqueue_script( 'iflychat-popup', IFLYCHAT_URL . 'js/iflychat-popup.js', array(), filemtime( IFLYCHAT_DIR . 'js/iflychat-popup.js' ), true );
     202        }
     203    }
     204}
     205
     206// phpcs:ignore
    6207/*
    7 Plugin Name: iFlyChat
    8 Plugin URI: http://wordpress.org/extend/plugins/iflychat/
    9 Description: One on one chat, Multiple chatrooms, Embedded chatrooms
    10 Author: iFlyChat Team
    11 Version: 4.6.4
    12 Author URI: https://iflychat.com/
     208Already Commented
     209add_filter('iflychat_get_user_groups_filter','iflychat_get_user_groups');
     210function iflychat_get_user_groups(){
     211$current_user =  wp_get_current_user();
     212$arr = array();
     213if ($current_user->ID % 2 == 0 ) {
     214    $arr['A'] = "A";
     215} else {
     216    $arr['B'] = "B";
     217}
     218return (array)$arr;
     219};
    13220*/
    14221
    15 if (!function_exists('is_plugin_active_for_network')) {
    16     require_once(ABSPATH . '/wp-admin/includes/plugin.php');
    17 }
    18 if(session_id() == ''){
    19     session_start();
    20 }
    21 
    22 if(!defined('DRUPALCHAT_EXTERNAL_HOST')){
    23   define('DRUPALCHAT_EXTERNAL_HOST', 'http://api.iflychat.com');
    24 }
    25 
    26 if(!defined('DRUPALCHAT_EXTERNAL_PORT')){
    27   define('DRUPALCHAT_EXTERNAL_PORT', '80');
    28 }
    29 
    30 if(!defined('DRUPALCHAT_EXTERNAL_A_HOST')){
    31   define('DRUPALCHAT_EXTERNAL_A_HOST', 'https://api.iflychat.com');
    32 }
    33 
    34 if(!defined('DRUPALCHAT_EXTERNAL_A_PORT')){
    35   define('DRUPALCHAT_EXTERNAL_A_PORT', '443');
    36 }
    37 
    38 if(!defined('DRUPALCHAT_EXTERNAL_CDN_HOST')){
    39   define('DRUPALCHAT_EXTERNAL_CDN_HOST', 'cdn.iflychat.com');
    40 }
    41 
    42 if(!defined('CHATCAMP_EXTERNAL_HOST')){
    43   define('CHATCAMP_EXTERNAL_HOST', 'http://api.chatcamp.io');
    44 }
    45 
    46 if(!defined('CHATCAMP_EXTERNAL_PORT')){
    47   define('CHATCAMP_EXTERNAL_PORT', '80');
    48 }
    49 
    50 if(!defined('CHATCAMP_EXTERNAL_A_HOST')){
    51   define('CHATCAMP_EXTERNAL_A_HOST', 'https://api.chatcamp.io');
    52 }
    53 
    54 if(!defined('CHATCAMP_EXTERNAL_A_PORT')){
    55   define('CHATCAMP_EXTERNAL_A_PORT', '443');
    56 }
    57 
    58 if(!defined('CHATCAMP_EXTERNAL_CDN_HOST')){
    59   define('CHATCAMP_EXTERNAL_CDN_HOST', 'cdn.chatcamp.io');
    60 }
    61 
    62 define('IFLYCHAT_PLUGIN_VERSION', 'WP-4.6.4');
    63 if (!defined('IFLYCHAT_DEBUG')) {
    64   define('IFLYCHAT_DEBUG',          false);
    65 }
    66 $iflychat_engine = TRUE;
    67 
    68 function iflychat_get_hash_session()
    69 {
    70     $data = uniqid(mt_rand(), TRUE);
    71     $hash = base64_encode(hash('sha256', $data, TRUE));
    72     return strtr($hash, array('+' => '-', '/' => '_', '=' => ''));
    73 }
    74 
    75 function iflychat_get_user_id()
    76 {
    77   $current_user =  wp_get_current_user();
    78     global $wpdb;
    79     if ($current_user->ID) {
    80         return strval($current_user->ID);
    81     } else {
    82         return false;
    83     }
    84 }
    85 
    86 function iflychat_get_user_name()
    87 {
    88   $current_user =  wp_get_current_user();
    89   $hook_user_name = apply_filters('iflychat_get_username_filter', '',$current_user->ID);
    90   if (!empty($hook_user_name)) {
    91     return $hook_user_name;
    92   }
    93     if ($current_user->ID) {
    94         if (empty($current_user->display_name) || (iflychat_get_option('iflychat_use_display_name') == '2')) {
    95             return $current_user->user_login;
    96         } else {
    97             return $current_user->display_name;
    98         }
    99     } else {
    100         return false;
    101     }
    102 }
    103 
    104 
    105 // Async load
    106 function iflychat_async_scripts($url)
    107 {
    108     if ( strpos( $url, '#asyncload') === false )
    109         return $url;
    110     else if ( is_admin() )
    111         return str_replace( '#asyncload', '', $url )."' async='async";
    112     else
    113         return str_replace( '#asyncload', '', $url )."' async='async";
    114 }
    115 add_filter( 'clean_url', 'iflychat_async_scripts');
    116 
    117 
    118 function iflychat_init()
    119 {
    120     $user_data = false;
    121     if(iflychat_check_access()){
    122            
    123         $_iflychat_protocol = isset($_SERVER["HTTPS"]) ? 'https://' : 'http://';
    124 
    125         wp_enqueue_script('iflychat-ajax', plugin_dir_url( __FILE__ ) . 'js/iflychat.js', array('jquery'), false, true);
    126         wp_localize_script('iflychat-ajax', 'iflychat_chatcamp_check', iflychat_chatcamp_check()?"1":"0");
    127         wp_localize_script('iflychat-ajax', 'iflychat_app_id', iflychat_get_option('iflychat_app_id'));
    128         wp_localize_script('iflychat-ajax', 'iflychat_external_cdn_host', iflychat_chatcamp_check()?CHATCAMP_EXTERNAL_CDN_HOST:DRUPALCHAT_EXTERNAL_CDN_HOST);
    129         if (is_user_logged_in()) {
    130             $user_data = json_encode(_iflychat_get_user_auth());
    131         }
    132         if(iflychat_get_option('iflychat_session_caching') == '1' && isset($_SESSION['user_data']) && $_SESSION['user_data'] == $user_data){
    133             //if(iflychat_get_option('iflychat_enable_friends') == '1'){
    134             if (isset($_SESSION['token']) && !empty($_SESSION['token'])) {
    135                 wp_localize_script('iflychat-ajax', 'iflychat_auth_token', $_SESSION['token']);
    136             }
    137         }
    138         if (is_user_logged_in()) {
    139             wp_localize_script('iflychat-ajax', 'iflychat_auth_url', admin_url('admin-ajax.php', $_iflychat_protocol));
    140         }
    141        
    142         if(iflychat_get_option('iflychat_popup_chat') == '1'){
    143             wp_enqueue_script('iflychat-popup', plugin_dir_url( __FILE__ ) . 'js/iflychat-popup.js', array(), false, true);
    144         }
    145         else if(iflychat_get_option('iflychat_popup_chat') == '2' && !is_admin()){
    146             wp_enqueue_script('iflychat-popup', plugin_dir_url( __FILE__ ) . 'js/iflychat-popup.js', array(), false, true);
    147         }
    148         else if((iflychat_get_option('iflychat_popup_chat') == '3' || iflychat_get_option('iflychat_popup_chat') == '4') && iflychat_path_check()){
    149             wp_enqueue_script('iflychat-popup', plugin_dir_url( __FILE__ ) . 'js/iflychat-popup.js', array(), false, true);
    150         }
    151        
    152 
    153     }
    154 }
    155 
    156 
    157 // add_filter('iflychat_get_user_groups_filter','iflychat_get_user_groups');
    158 // function iflychat_get_user_groups(){
    159 //$current_user =  wp_get_current_user();
    160 // $arr = array();
    161 // if ($current_user->ID % 2 == 0 ) {
    162 //    $arr['A'] = "A";
    163 // } else {
    164 //    $arr['B'] = "B";
    165 // }
    166 //
    167 //    return (array)$arr;
    168 // };
    169 
    170 /**
    171  * function to get user_details
    172  */
    173 function _iflychat_get_user_auth()
    174 {
    175   $current_user =  wp_get_current_user();
    176   $admin_check = FALSE;
    177   $user_data = array();
    178   if (iflychat_check_chat_admin()) {
    179       $chat_role = "admin";
    180   } else if(iflychat_check_chat_moderator()){
    181       $chat_role = "moderator";
    182   }else{
    183       $chat_role = "participant";
    184   }
    185   $role = array();
    186   foreach ($current_user->roles as $rkey => $rvalue) {
    187       $role[$rvalue] = $rvalue;
    188   }
    189   if (iflychat_get_user_id() && iflychat_get_user_name()) {
    190     $user_data = array(
    191       'user_id' => iflychat_get_user_id(),
    192       'user_name' => iflychat_get_user_name(),
    193       'user_roles' => $role,
    194       'chat_role' => $chat_role,
    195       'user_list_filter' => 'all',
    196       'user_status' => TRUE,
    197     );
    198   }
    199 
    200   $user_data['user_avatar_url'] = iflychat_get_user_pic_url();
    201   $user_data['user_profile_url'] = iflychat_get_user_profile_url();
    202 
    203   //Added allRoles if chat_role is admin or moderator
    204   if ($chat_role == 'admin' || $chat_role == 'moderator') {
    205     global $wp_roles;
    206     $user_data['user_site_roles'] = $wp_roles->get_names();
    207   }
    208 
    209   $hook_user_groups = apply_filters('iflychat_get_user_groups_filter', array(),$current_user->ID);
    210   $hook_user_friends = apply_filters('iflychat_get_user_friends_filter', array(),$current_user->ID);
    211   $hook_user_roles = apply_filters('iflychat_get_user_roles_filter', array(),$current_user->ID);
    212   if ((iflychat_get_option('iflychat_enable_friends') == '2') && function_exists('friends_get_friend_user_ids')) { // filtering based on buddypress friends
    213       //echo 'enable friends';
    214       $user_data['user_list_filter'] = 'friend';
    215       $final_list = array();
    216       $final_list['1']['name'] = 'friend';
    217       $final_list['1']['plural'] = 'friends';
    218       $final_list['1']['valid_uids'] = friends_get_friend_user_ids(iflychat_get_user_id());
    219       $user_data['user_relationships'] = $final_list;
    220   }else {
    221       $user_data['user_list_filter'] = 'all';
    222   }
    223   if (!empty($hook_user_friends)) {
    224       if (iflychat_get_option('iflychat_enable_friends') != '2') {
    225           iflychat_update_option('iflychat_enable_friends', '2');
    226       }
    227       $user_data['user_list_filter'] = 'friend';
    228       $final_list = array();
    229       $final_list['1']['name'] = 'friend';
    230       $final_list['1']['plural'] = 'friends';
    231       $final_list['1']['valid_uids'] = $hook_user_friends;
    232       $user_data['user_relationships'] = $final_list;
    233   }
    234   if (!empty($hook_user_groups)) {
    235      // echo 'hook_user_groups';
    236       $user_data['user_list_filter'] = 'group';
    237       $user_data['user_groups'] = $hook_user_groups;
    238       if (iflychat_get_option('iflychat_enable_user_groups') != '1') {
    239           iflychat_update_option('iflychat_enable_user_groups', '1');
    240       }
    241   }
    242   if (empty($hook_user_groups)) {
    243       if (iflychat_get_option('iflychat_enable_user_groups') != '2') {
    244           iflychat_update_option('iflychat_enable_user_groups', '2');
    245       }
    246   }
    247   if (!empty($hook_user_roles)) {
    248     $user_data['user_roles'] = $hook_user_roles;
    249   }
    250 
    251 
    252   return $user_data;
    253 }
    254 
    255 
    256 function _iflychat_get_auth()
    257 {   $current_user =  wp_get_current_user();
    258     global $wp_version;
    259     $data = array();
    260     if (iflychat_get_option('iflychat_api_key') == " ") {
    261         return null;
    262     }
    263     $admin_check = FALSE;
    264     if (iflychat_check_chat_admin()) {
    265         $chat_role = "admin";
    266     } else if(iflychat_check_chat_moderator()){
    267         $chat_role = "moderator";
    268     }else{
    269         $chat_role = "participant";
    270     }
    271     $role = array();
    272     foreach ($current_user->roles as $rkey => $rvalue) {
    273         $role[$rvalue] = $rvalue;
    274     }
    275 
    276   if (iflychat_get_user_id() && iflychat_get_user_name()) {
    277     $data = array(
    278       'api_key' => iflychat_get_option('iflychat_api_key'),
    279       'app_id' =>  iflychat_get_option('iflychat_app_id')? iflychat_get_option('iflychat_app_id'):'',
    280       'version' => IFLYCHAT_PLUGIN_VERSION,
    281     );
    282   }
    283 
    284     $user_data = _iflychat_get_user_auth();
    285     $data = array_merge($data, $user_data);
    286     $_SESSION['user_data'] = json_encode($user_data);
    287 
    288     $options = array(
    289         'method' => 'POST',
    290         'body' => $data,
    291         'timeout' => 15,
    292         'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
    293         'sslverify' => false,
    294     );
    295 
    296     if(iflychat_chatcamp_check()) {
    297         $options['body'] = json_encode(array('id' => $data['user_id']));
    298         $options['headers'] = array('Content-Type' => 'application/json', 'x-api-key' => $data['api_key']);
    299         $result = wp_remote_head(CHATCAMP_EXTERNAL_A_HOST . ':' . CHATCAMP_EXTERNAL_A_PORT . '/api/2.0/users.get', $options);
    300         if (!is_wp_error($result) && $result['response']['code'] == 200) {
    301             $result = json_decode($result['body']);
    302             if (is_user_logged_in()) {
    303                 $_SESSION['token'] = $result->access_token;
    304                 $result->current_user = $current_user;
    305                 $result->test_data = $data;
    306                 $should_update = false;
    307                 $should_update = iflychat_chatcamp_user_should_update($data, $result->user);
    308                 if($should_update === true){
    309                     $options['body'] = json_encode(iflychat_chatcamp_process_user_data($data));
    310                     $result = wp_remote_head(CHATCAMP_EXTERNAL_A_HOST . ':' . CHATCAMP_EXTERNAL_A_PORT . '/api/2.0/users.update', $options);
    311                      if (!is_wp_error($result) && $result['response']['code'] == 200) {
    312                         $result = json_decode($result['body']);
    313                         if (is_user_logged_in()) {
    314                             $_SESSION['token'] = $result->access_token;
    315                             $result->current_user = $current_user;
    316                             // print_r($result);exit;
    317                         }
    318                          return $result;
    319                     }
    320                     else if(!is_wp_error($result) && $result['response']['code'] != 200){
    321                         return $result['response'];
    322                     }
    323                     else {
    324                         $error = array(
    325                             'code' => $result->get_error_code(),
    326                             'message' => $result->get_error_message()
    327                         );
    328                         return $error;
    329                     }
    330                 }
    331             }
    332             return $result;
    333         }
    334         else if($result['response']['code'] == 404){
    335             $options['body'] = json_encode(iflychat_chatcamp_process_user_data($data));
    336             $result = wp_remote_head(CHATCAMP_EXTERNAL_A_HOST . ':' . CHATCAMP_EXTERNAL_A_PORT . '/api/2.0/users.create', $options);
    337             if (!is_wp_error($result) && $result['response']['code'] == 200) {
    338                 $result = json_decode($result['body']);
    339                 if (is_user_logged_in()) {
    340                     $_SESSION['token'] = $result->access_token;
    341                     $result->current_user = $current_user;
    342                     // print_r($result);exit;
    343                 }
    344                 return $result;
    345             }
    346             else if(!is_wp_error($result) && $result['response']['code'] != 200){
    347                 return $result['response'];
    348               } else {
    349                 $error = array(
    350                   'code' => $result->get_error_code(),
    351                   'message' => $result->get_error_message()
    352                 );
    353                 return $error;
    354               }
    355         }
    356         else if(!is_wp_error($result) && $result['response']['code'] != 200){
    357           return $result['response'];
    358         } else {
    359           $error = array(
    360             'code' => $result->get_error_code(),
    361             'message' => $result->get_error_message()
    362           );
    363           return $error;
    364         }
    365     }
    366     else {
    367         $result = wp_remote_head(iflychat_get_host(TRUE) . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/api/1.1/token/generate', $options);
    368         if (!is_wp_error($result) && $result['response']['code'] == 200) {
    369             $result = json_decode($result['body']);
    370             if (is_user_logged_in()) {
    371                 $_SESSION['token'] = $result->key;
    372                 // print_r($result);exit;
    373             }
    374             return $result;
    375         }
    376         else if(!is_wp_error($result) && $result['response']['code'] != 200){
    377           return $result['response'];
    378         } else {
    379           $error = array(
    380             'code' => $result->get_error_code(),
    381             'message' => $result->get_error_message()
    382           );
    383           return $error;
    384         }
    385     }
    386 
    387    
    388 }
    389 
    390 function iflychat_mobile_auth()
    391 {
    392     if (iflychat_get_option('iflychat_enable_mobile_sdk_integration', '2') == '1') {
    393         $uid = wp_authenticate_username_password(null, $_POST['username'], $_POST['password']);
    394         $id = ($uid->data->ID);
    395         if ($id) {
    396             $user = wp_set_current_user($id, $_POST['username']);
    397             $result = json_encode(_iflychat_get_auth($_POST['username']));
    398             header("Content-Type: application/json");
    399             echo $result;
    400         } else {
    401             header('HTTP/1.1 403 Access Denied');
    402             echo 'Access Denied';
    403         }
    404     } else {
    405         header('HTTP/1.1 403 Access Denied');
    406         echo "Please Enable Mobile SDK Integration";
    407     }
    408     exit;
    409 }
    410 
    411 function iflychat_submit_uth()
    412 {
    413 
    414     $json = NULL;
    415     $json = _iflychat_get_auth();
    416     //print_r(gettype($json));
    417 
    418     $response = json_encode($json);
    419     //print_r(gettype($response));
    420      header("Content-Type: application/json");
    421      echo $response;
    422      exit;
    423 }
    424 
    425 function iflychat_install()
    426 {
    427     global $wpdb;
    428 }
    429 
    430 function iflychat_uninstall()
    431 {
    432     //delete_option('iflychat_api_key');
    433     global $wpdb;
    434 }
    435 
    436 function iflychat_set_options()
    437 {
    438     $options = array(
    439         'app_id' => array(
    440             'name' => 'iflychat_app_id',
    441             'default' => ' ',
    442             'desc' => '<b>APP ID</b> (register at <a href="https://iflychat.com">iFlyChat.com</a> to get it)',
    443             'input_type' => 'text'
    444         ),
    445         'api_key' => array(
    446             'name' => 'iflychat_api_key',
    447             'default' => ' ',
    448             'desc' => '<b>API key</b> (register at <a href="https://iflychat.com">iFlyChat.com</a> to get it)',
    449             'input_type' => 'text'
    450         ),
    451         'use_display_name' => array (
    452             'name' => 'iflychat_use_display_name',
    453             'default' => '1',
    454             'desc' => 'Specify whether to use display name or username for logged-in user',
    455             'input_type' => 'dropdown',
    456             'data' => array(
    457                 '1' => 'Display Name',
    458                 '2' => 'Username')
    459         ),
    460         'embed_chat' => array (
    461             'name' => 'iflychat_embed_chat',
    462             'desc' => 'Show embed chat',
    463             'default' => 'View Tutorial',
    464             'input_type' => 'button',
    465             'link' => 'https://iflychat.com/embedded-chatroom-example-public-chatroom'
    466         ),
    467         'enable_friends' => array (
    468             'name' => 'iflychat_enable_friends',
    469             'default' => '1',
    470             'desc' => 'Show only friends in online user list',
    471             'input_type' => 'dropdown',
    472             'data' => array(
    473                 '1' => 'No',
    474                 '2' => 'BuddyPress Friends')
    475         ),
    476         'popup_chat' => array (
    477             'name' => 'iflychat_popup_chat',
    478             'default' => '1',
    479             'desc' => 'Show Popup Chat',
    480             'input_type' => 'dropdown',
    481             'data' => array(
    482                 '1' => 'Everywhere',
    483                 '2' => 'Frontend Only',
    484                 '3' => 'Everywhere except those listed',
    485                 '4' => 'Only the listed pages',
    486                 '5' => 'Disable')
    487         ),
    488         'path_pages' => array (
    489             'name' => 'iflychat_path_pages',
    490             'default' => '',
    491             'desc' => "Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are <b>/2012/10/my-post</b> for a single post and <b>/2012/*</b> for a group of posts. The path should always start with a forward slash(/).",
    492             'input_type' => 'textarea'
    493         ),
    494         'chat_moderators_array' => array (
    495           'name' => 'iflychat_chat_moderators_array',
    496           'default' => '',
    497           'desc' => "Specify WordPress username of users who should be chat moderators (separated by comma)",
    498           'input_type' => 'textarea'
    499         ),
    500         'chat_admins_array' => array (
    501           'name' => 'iflychat_chat_admins_array',
    502           'default' => '',
    503           'desc' => "Specify WordPress username of users who should be chat admininstrators (separated by comma)",
    504           'input_type' => 'textarea'
    505         ),
    506         'session_caching' => array (
    507             'name' => 'iflychat_session_caching',
    508             'default' => '2',
    509             'desc' => 'Enable Session Caching',
    510             'input_type' => 'dropdown',
    511             'data' => array(
    512                 '1' => 'Yes',
    513                 '2' => 'No')
    514         ),
    515 
    516     );
    517 
    518     return $options;
    519 
    520 }
    521 
    522 //create settings page
    523 function iflychat_settings()
    524 {
    525     wp_enqueue_script( 'iflychat-admin', plugin_dir_url( __FILE__ ) . 'js/iflychat.admin.script.js', array('jquery'));
    526     $active_tab = isset($_GET['tab']) ? $_GET['tab'] : 'plugin_settings';
    527     $i = 0;
    528     ?>
    529     <div class="wrap">
    530     <h1><?php _e('iFlyChat Settings', 'iflychat_settings'); ?></h1>
    531     <h2 class="nav-tab-wrapper">
    532         <a href="?page=iflychat_settings&tab=plugin_settings"
    533            class="nav-tab <?php echo $active_tab == 'plugin_settings' ? 'nav-tab-active' : ''; ?>">Plugin Settings</a>
    534         <a href="?page=iflychat_settings&tab=app_settings"
    535            class="nav-tab <?php echo $active_tab == 'app_settings' ? 'nav-tab-active' : ''; ?>">App Settings</a>
    536     </h2>
    537 
    538         <?php
    539         if ($active_tab == 'plugin_settings') {
    540             // $result = null;
    541             // if(!iflychat_chatcamp_check()) {
    542             $result = _iflychat_get_auth();
    543            
    544            
    545           if(gettype($result) == 'array'){
    546 
    547             if($result['code'] == 403){?>
    548               <div id="message" class="error"><p><strong><?php _e('Invalid API Key.', 'iflychat_settings'); ?></strong></p></div>
    549               <?php
    550             }else if($result['code'] == 503){?>
    551               <div id="message" class="error"><p><strong><?php _e('503 Error. Service Unavailable.', 'iflychat_settings'); ?></strong></p></div>
    552               <?php
    553             }else{?>
    554               <div id="message" class="error"><p><strong><?php _e('Error Message - '.$result['message'].'.Error Code - '.$result['code'], 'wp_error'); ?></strong></p></div>
    555               <?php
    556             }
    557           }
    558         if(iflychat_validate_fields()){
    559           if (isset($_GET['updated']) && $_GET['updated'] == 'true') {
    560             ?>
    561             <div id="message" class="updated fade"><p><strong><?php _e('Settings Updated', 'iflychat_settings'); ?></strong></p></div>
    562             <?php
    563           }
    564         }else{
    565           ?>
    566           <div id="message" class="error"><p><strong><?php _e('Invalid APP ID.', 'iflychat_settings'); ?></strong></p></div>
    567           <?php
    568         }
    569         ?>
    570         <form method="post" action="<?php
    571         if(is_multisite() && is_plugin_active_for_network(plugin_basename(__FILE__ ))) {
    572             echo esc_url('edit.php?action=iflychat_network_settings');
    573         }
    574         else {
    575             echo esc_url('options.php');
    576         }
    577         ?>">
    578             <div>
    579                 <?php settings_fields('iflychat-settings'); ?>
    580             </div>
    581 
    582             <?php
    583             $options = iflychat_set_options();
    584             ?>
    585             <table class="form-table">
    586                 <?php foreach($options as $option){ ?>
    587                     <?php
    588                     //if option type is a dropdown, do this
    589                     if ( $option['input_type'] == 'dropdown'){ ?>
    590                         <tr valign="top">
    591                             <th scope="row"><?php _e($option['desc'], 'iflychat_settings'); ?></th>
    592                             <td><select id="<?php echo $option['name']; ?>" name="<?php echo $option['name']; ?>">
    593                                     <?php foreach($option['data'] as $opt => $value){ ?>
    594                                         <option <?php if(iflychat_get_option($option['name']) == $opt){ echo 'selected="selected"';}?> name="<?php echo $option['name']; ?>" value="<?php echo $opt; ?>"><?php echo $value ; ?></option>
    595                                     <?php } //endforeach ?>
    596                                 </select>
    597                             </td>
    598                         </tr>
    599                         <?php
    600                         //if option type is text, do this
    601                     }elseif ( $option['input_type'] == 'text'){ ?>
    602                         <tr valign="top">
    603                             <th scope="row"><?php _e($option['desc'], 'iflychat_settings'); ?></th>
    604                             <td><input id="<?php echo $option['name']; ?>" name="<?php echo $option['name']; ?>" value="<?php echo iflychat_get_option($option['name']); ?>" size="64" />
    605                             </td>
    606                         </tr>
    607                         <?php
    608                         //if option type is text, do this
    609                     }elseif ( $option['input_type'] == 'textarea'){ ?>
    610                         <tr valign="top" id="<?php if($option['name'] === 'iflychat_path_pages') echo $option['name'] ?>">
    611                             <th scope="row"><?php _e($option['desc'], 'iflychat_settings'); ?></th>
    612                             <td><textarea  cols="80" rows="6" name="<?php echo $option['name']; ?>"><?php echo iflychat_get_option($option['name']); ?>
    613                                     </textarea>
    614                             </td>
    615                         </tr>
    616                         <?php
    617                     }elseif ($option['input_type'] == 'button') { ?>
    618                       <tr valign="top">
    619                         <th scope="row"><?php _e($option['desc'], 'iflychat_settings'); ?></th>
    620                         <td><a target="_blank" href="<?php echo $option['link']; ?>"><input type="button" value="<?php echo $option['default']; ?>"</a>
    621                         </td>
    622                       </tr>
    623                       <?php
    624 
    625                     }else{} //endif
    626 
    627                 } //endforeach ?>
    628 
    629       </table>
    630       <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Update', 'iflychat_settings'); ?>" /></p>
    631     </form>
    632     </div>
    633     <br />
    634     <hr />
    635     <br />
    636     <h3><?php echo 'Debug Information'; ?></h3>
    637     <p>
    638       <?php echo 'Having problems with iFlyChat? Check out our installation guide'; ?>
    639       <a href="https://iflychat.com/installation/wordpress-chat-plugin" target="_blank"><?php echo 'here'; ?></a>
    640       <?php echo '. You can also open a support ticket and we will look into it immediately. Please include the debug information given below.'; ?>
    641       <a href="https://iflychat.com/contact" target="_blank"><?php echo 'Contact support'; ?></a>
    642     </p>
    643     <textarea style="width:90%; height:200px;">
    644 URL: <?php echo esc_url( get_option('siteurl') ); ?>
    645 
    646 PHP Version: <?php echo esc_html( phpversion() ); ?>
    647 
    648 Wordpress Version: <?php global $wp_version; echo esc_html( $wp_version ); ?>
    649 
    650 Active Theme: <?php
    651       if ( !function_exists('wp_get_theme') ) {
    652         $theme =  wp_get_themes();
    653         echo esc_html( $theme['Name'] . ' ' . $theme['Version'] );
    654       } else {
    655         $theme = wp_get_theme();
    656         echo esc_html( $theme->Name . ' ' . $theme->Version );
    657       }
    658       ?>
    659 
    660 URL Open Method: <?php echo esc_html( iflychat_url_method() ); ?>
    661 
    662 Plugin Version: <?php echo esc_html( IFLYCHAT_PLUGIN_VERSION ); ?>
    663 
    664 Settings: iflychat_is_installed: <?php echo esc_html( iflychat_is_installed().' | ' ); ?>
    665       <?php foreach (iflychat_options() as $opt) {
    666         echo esc_html( ' '.$opt.': '.get_option($opt).' | ' );
    667       }
    668       ?>
    669 
    670 Plugins: <?php
    671       foreach (get_plugins() as $key => $plugin) {
    672         $isactive = '';
    673         if (is_plugin_active($key)) {
    674           $isactive = '(active)';
    675         }
    676         echo esc_html( ' '.$plugin['Name'].' '.$plugin['Version'].' '.$isactive.' | ' );
    677       }
    678       ?>
    679         </textarea><br/>
    680     <?php
    681   } else {
    682     $dashboardUrl = "https://dashboard.chatcamp.io";
    683     if(!iflychat_chatcamp_check()) {
    684         $iflychat_host = DRUPALCHAT_EXTERNAL_A_HOST;
    685         $host = explode("/", $iflychat_host);
    686         $host_name = $host[2];
    687         if (isset($_SESSION['token']) && !empty($_SESSION['token'])) {
    688         $token = $_SESSION['token'];
    689         } else {
    690         $token = _iflychat_get_auth()->key;
    691         }   
    692         $dashboardUrl = "https://cdn.iflychat.com/apps/dashboard/#/settings/app?sessid=" . $token . "&hostName=" . $host_name . "&hostPort=" . DRUPALCHAT_EXTERNAL_A_PORT;
    693     }
    694     ?>
    695     <br/>
    696     <input type="button" class="button-primary" value="Open App Dashboard in new tab" onclick="window.open('<?php echo $dashboardUrl ?>')">
    697     <?php
    698   }
    699 
    700         ?>
    701     </form>
    702     <?php
    703 }
    704 //check if iflychat is installed
     222/**
     223 * Function to get user_details
     224 *
     225 * @since 4.7.0
     226 */
     227function _iflychat_get_user_auth() {
     228    $current_user = wp_get_current_user();
     229    $admin_check  = false;
     230    $user_data    = array();
     231
     232    if ( iflychat_check_chat_admin() ) {
     233        $chat_role = 'admin';
     234    } elseif ( iflychat_check_chat_moderator() ) {
     235        $chat_role = 'moderator';
     236    } else {
     237        $chat_role = 'participant';
     238    }
     239    $role = array();
     240    if ( count( $current_user->roles ) > 0 ) {
     241        foreach ( $current_user->roles as $rkey => $rvalue ) {
     242            $role[ $rvalue ] = $rvalue;
     243        }
     244    }
     245
     246    if ( iflychat_get_user_id() && iflychat_get_user_name() ) {
     247        $user_data = array(
     248            'user_id'          => iflychat_get_user_id(),
     249            'user_name'        => iflychat_get_user_name(),
     250            'user_roles'       => $role,
     251            'chat_role'        => $chat_role,
     252            'user_list_filter' => 'all',
     253            'user_status'      => true,
     254        );
     255    }
     256
     257    $user_data['user_avatar_url']  = iflychat_get_user_pic_url();
     258    $user_data['user_profile_url'] = iflychat_get_user_profile_url();
     259
     260    // Added allRoles if chat_role is admin or moderator.
     261    if ( 'admin' === $chat_role || 'moderator' === $chat_role ) {
     262        global $wp_roles;
     263        $user_data['user_site_roles'] = $wp_roles->get_names();
     264    }
     265
     266    $hook_user_groups  = apply_filters( 'iflychat_get_user_groups_filter', array(), $current_user->ID );
     267    $hook_user_friends = apply_filters( 'iflychat_get_user_friends_filter', array(), $current_user->ID );
     268    $hook_user_roles   = apply_filters( 'iflychat_get_user_roles_filter', array(), $current_user->ID );
     269    if ( ( '2' === iflychat_get_option( 'iflychat_enable_friends' ) ) && function_exists( 'friends_get_friend_user_ids' ) ) { // filtering based on buddypress friends.
     270        $user_data['user_list_filter']   = 'friend';
     271        $final_list                      = array();
     272        $final_list['1']['name']         = 'friend';
     273        $final_list['1']['plural']       = 'friends';
     274        $final_list['1']['valid_uids']   = friends_get_friend_user_ids( iflychat_get_user_id() );
     275        $user_data['user_relationships'] = $final_list;
     276    } else {
     277        $user_data['user_list_filter'] = 'all';
     278    }
     279    if ( ! empty( $hook_user_friends ) ) {
     280        if ( '2' !== iflychat_get_option( 'iflychat_enable_friends' ) ) {
     281            iflychat_update_option( 'iflychat_enable_friends', '2' );
     282        }
     283        $user_data['user_list_filter']   = 'friend';
     284        $final_list                      = array();
     285        $final_list['1']['name']         = 'friend';
     286        $final_list['1']['plural']       = 'friends';
     287        $final_list['1']['valid_uids']   = $hook_user_friends;
     288        $user_data['user_relationships'] = $final_list;
     289    }
     290
     291    if ( ! empty( $hook_user_groups ) ) {
     292        $user_data['user_list_filter'] = 'group';
     293        $user_data['user_groups']      = $hook_user_groups;
     294        if ( '1' !== iflychat_get_option( 'iflychat_enable_user_groups' ) ) {
     295            iflychat_update_option( 'iflychat_enable_user_groups', '1' );
     296        }
     297    }
     298    if ( empty( $hook_user_groups ) ) {
     299        if ( '2' !== iflychat_get_option( 'iflychat_enable_user_groups' ) ) {
     300            iflychat_update_option( 'iflychat_enable_user_groups', '2' );
     301        }
     302    }
     303
     304    if ( ! empty( $hook_user_roles ) ) {
     305        $user_data['user_roles'] = $hook_user_roles;
     306    }
     307    return $user_data;
     308}
     309
     310/**
     311 * Function to get User authentication
     312 *
     313 * @since 4.7.0
     314 */
     315function _iflychat_get_auth() {
     316    $current_user = wp_get_current_user();
     317    global $wp_version;
     318    $data = array();
     319
     320    if ( '' === trim( iflychat_get_option( 'iflychat_api_key' ) ) ) {
     321        return null;
     322    }
     323
     324    $admin_check = false;
     325    if ( iflychat_check_chat_admin() ) {
     326        $chat_role = 'admin';
     327    } elseif ( iflychat_check_chat_moderator() ) {
     328        $chat_role = 'moderator';
     329    } else {
     330        $chat_role = 'participant';
     331    }
     332
     333    $role = array();
     334    if ( count( $current_user->roles ) > 0 ) {
     335        foreach ( $current_user->roles as $rkey => $rvalue ) {
     336            $role[ $rvalue ] = $rvalue;
     337        }
     338    }
     339
     340    if ( iflychat_get_user_id() && iflychat_get_user_name() ) {
     341        $data = array(
     342            'api_key' => iflychat_get_option( 'iflychat_api_key' ),
     343            'app_id'  => iflychat_get_option( 'iflychat_app_id' ) ? iflychat_get_option( 'iflychat_app_id' ) : '',
     344            'version' => IFLYCHAT_PLUGIN_VERSION,
     345        );
     346    }
     347
     348    $user_data             = _iflychat_get_user_auth();
     349    $data                  = array_merge( $data, $user_data );
     350    $_SESSION['user_data'] = wp_json_encode( $user_data );
     351
     352    $options = array(
     353        'method'    => 'POST',
     354        'body'      => $data,
     355        'timeout'   => 15,
     356        'headers'   => array(
     357            'Content-Type' => 'application/x-www-form-urlencoded',
     358        ),
     359        'sslverify' => false,
     360    );
     361
     362    if ( iflychat_chatcamp_check() ) {
     363
     364        $options['body'] = wp_json_encode(
     365            array(
     366                'id' => $data['user_id'],
     367            )
     368        );
     369
     370        $options['headers'] = array(
     371            'Content-Type' => 'application/json',
     372            'x-api-key'    => $data['api_key'],
     373        );
     374
     375        $result = wp_remote_head( CHATCAMP_EXTERNAL_A_HOST . ':' . CHATCAMP_EXTERNAL_A_PORT . '/api/2.0/users.get', $options );
     376        if ( ! is_wp_error( $result ) && 200 === $result['response']['code'] ) {
     377            $result = json_decode( $result['body'] );
     378            if ( is_user_logged_in() ) {
     379                $_SESSION['token']    = $result->access_token;
     380                $result->current_user = $current_user;
     381                $result->test_data    = $data;
     382                $should_update        = false;
     383                $should_update        = iflychat_chatcamp_user_should_update( $data, $result->user );
     384                if ( true === $should_update ) {
     385                    $options['body'] = wp_json_encode( iflychat_chatcamp_process_user_data( $data ) );
     386                    $result          = wp_remote_head( CHATCAMP_EXTERNAL_A_HOST . ':' . CHATCAMP_EXTERNAL_A_PORT . '/api/2.0/users.update', $options );
     387                    if ( ! is_wp_error( $result ) && 200 === $result['response']['code'] ) {
     388                        $result = json_decode( $result['body'] );
     389                        if ( is_user_logged_in() ) {
     390                            $_SESSION['token']    = $result->access_token;
     391                            $result->current_user = $current_user;
     392                        }
     393                        return $result;
     394                    } elseif ( ! is_wp_error( $result ) && 200 !== $result['response']['code'] ) {
     395                        return $result['response'];
     396                    } else {
     397                        $error = array(
     398                            'code'    => $result->get_error_code(),
     399                            'message' => $result->get_error_message(),
     400                        );
     401                        return $error;
     402                    }
     403                }
     404            }
     405            return $result;
     406        } elseif ( 404 === $result['response']['code'] ) {
     407            $options['body'] = wp_json_encode( iflychat_chatcamp_process_user_data( $data ) );
     408            $result          = wp_remote_head( CHATCAMP_EXTERNAL_A_HOST . ':' . CHATCAMP_EXTERNAL_A_PORT . '/api/2.0/users.create', $options );
     409            if ( ! is_wp_error( $result ) && 200 === $result['response']['code'] ) {
     410                $result = json_decode( $result['body'] );
     411                if ( is_user_logged_in() ) {
     412                    $_SESSION['token']    = $result->access_token;
     413                    $result->current_user = $current_user;
     414                }
     415                return $result;
     416            } elseif ( ! is_wp_error( $result ) && 200 !== $result['response']['code'] ) {
     417                return $result['response'];
     418            } else {
     419                $error = array(
     420                    'code'    => $result->get_error_code(),
     421                    'message' => $result->get_error_message(),
     422                );
     423                return $error;
     424            }
     425        } elseif ( ! is_wp_error( $result ) && 200 !== $result['response']['code'] ) {
     426            return $result['response'];
     427        } else {
     428            $error = array(
     429                'code'    => $result->get_error_code(),
     430                'message' => $result->get_error_message(),
     431            );
     432            return $error;
     433        }
     434    } else {
     435        $result = wp_remote_head( iflychat_get_host( true ) . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/api/1.1/token/generate', $options );
     436        if ( ! is_wp_error( $result ) && 200 === $result['response']['code'] ) {
     437            $result = json_decode( $result['body'] );
     438            if ( is_user_logged_in() ) {
     439                $_SESSION['token'] = $result->key;
     440            }
     441            return $result;
     442        } elseif ( ! is_wp_error( $result ) && 200 !== $result['response']['code'] ) {
     443            return $result['response'];
     444        } else {
     445            $error = array(
     446                'code'    => $result->get_error_code(),
     447                'message' => $result->get_error_message(),
     448            );
     449            return $error;
     450        }
     451    }
     452}
     453
     454/**
     455 * Function to get mobile authentication
     456 *
     457 * @since 4.7.0
     458 */
     459function iflychat_mobile_auth() {
     460    if ( '1' === iflychat_get_option( 'iflychat_enable_mobile_sdk_integration', '2' ) ) {
     461
     462        $username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : ''; //phpcs:ignore
     463        $password = isset( $_POST['password'] ) ? sanitize_text_field( wp_unslash( $_POST['password'] ) ) : ''; //phpcs:ignore
     464
     465        $uid = wp_authenticate_username_password( null, $username, $password );
     466        $id  = ( $uid->data->ID );
     467        if ( '' !== trim( $id ) ) {
     468            $user   = wp_set_current_user( $id, $username );
     469            $result = wp_json_encode( _iflychat_get_auth( $username ) );
     470            header( 'Content-Type: application/json' );
     471            echo $result; //phpcs:ignore
     472        } else {
     473            header( 'HTTP/1.1 403 Access Denied' );
     474            echo esc_html( 'Access Denied' );
     475        }
     476    } else {
     477        header( 'HTTP/1.1 403 Access Denied' );
     478        echo esc_html( 'Please Enable Mobile SDK Integration' );
     479    }
     480    exit;
     481}
     482
     483/**
     484 * Function to Submit chat
     485 *
     486 * @since 4.7.0
     487 */
     488function iflychat_submit_uth() {
     489
     490    $json = null;
     491    $json = _iflychat_get_auth();
     492
     493    $response = wp_json_encode( $json );
     494    header( 'Content-Type: application/json' );
     495    echo $response; //phpcs:ignore
     496    exit;
     497}
     498
     499/**
     500 * Function to Install plugin
     501 *
     502 * @since 4.7.0
     503 */
     504function iflychat_install() {
     505    global $wpdb;
     506}
     507
     508/**
     509 * Function to uninstall plugin
     510 *
     511 * @since 4.7.0
     512 */
     513function iflychat_uninstall() {
     514    global $wpdb;
     515}
     516
     517/**
     518 * Function to Set Settings option
     519 *
     520 * @since 4.7.0
     521 */
     522function iflychat_set_options() {
     523    $options = array(
     524        'app_id'                => array(
     525            'name'          => 'iflychat_app_id',
     526            'default'       => ' ',
     527            'desc'          => '<b>APP ID</b> (register at <a href="https://iflychat.com">iFlyChat.com</a> to get it)',
     528            'input_type'    => 'text',
     529            'sanitize_data' => array(
     530                'type'              => 'string',
     531                'sanitize_callback' => 'sanitize_text_field',
     532            ),
     533        ),
     534        'api_key'               => array(
     535            'name'          => 'iflychat_api_key',
     536            'default'       => ' ',
     537            'desc'          => '<b>API key</b> (register at <a href="https://iflychat.com">iFlyChat.com</a> to get it)',
     538            'input_type'    => 'text',
     539            'sanitize_data' => array(
     540                'type'              => 'string',
     541                'sanitize_callback' => 'sanitize_text_field',
     542            ),
     543        ),
     544        'use_display_name'      => array(
     545            'name'          => 'iflychat_use_display_name',
     546            'default'       => '1',
     547            'desc'          => 'Specify whether to use display name or username for logged-in user',
     548            'input_type'    => 'dropdown',
     549            'data'          => array(
     550                '1' => 'Display Name',
     551                '2' => 'Username',
     552            ),
     553            'sanitize_data' => array(
     554                'type'              => 'string',
     555                'sanitize_callback' => 'sanitize_text_field',
     556            ),
     557        ),
     558        'embed_chat'            => array(
     559            'name'          => 'iflychat_embed_chat',
     560            'desc'          => 'Show embed chat',
     561            'default'       => 'View Tutorial',
     562            'input_type'    => 'button',
     563            'link'          => 'https://iflychat.com/embedded-chatroom-example-public-chatroom',
     564            'sanitize_data' => array(
     565                'type'              => 'string',
     566                'sanitize_callback' => 'sanitize_text_field',
     567            ),
     568        ),
     569        'enable_friends'        => array(
     570            'name'          => 'iflychat_enable_friends',
     571            'default'       => '1',
     572            'desc'          => 'Show only friends in online user list',
     573            'input_type'    => 'dropdown',
     574            'data'          => array(
     575                '1' => 'No',
     576                '2' => 'BuddyPress Friends',
     577            ),
     578            'sanitize_data' => array(
     579                'type'              => 'string',
     580                'sanitize_callback' => 'sanitize_text_field',
     581            ),
     582        ),
     583        'popup_chat'            => array(
     584            'name'          => 'iflychat_popup_chat',
     585            'default'       => '1',
     586            'desc'          => 'Show Popup Chat',
     587            'input_type'    => 'dropdown',
     588            'data'          => array(
     589                '1' => 'Everywhere',
     590                '2' => 'Frontend Only',
     591                '3' => 'Everywhere except those listed',
     592                '4' => 'Only the listed pages',
     593                '5' => 'Disable',
     594            ),
     595            'sanitize_data' => array(
     596                'type'              => 'string',
     597                'sanitize_callback' => 'sanitize_text_field',
     598            ),
     599        ),
     600        'path_pages'            => array(
     601            'name'          => 'iflychat_path_pages',
     602            'default'       => '',
     603            'desc'          => "Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are <b>/2012/10/my-post</b> for a single post and <b>/2012/*</b> for a group of posts. The path should always start with a forward slash(/).",
     604            'input_type'    => 'textarea',
     605            'sanitize_data' => array(
     606                'type'              => 'string',
     607                'sanitize_callback' => 'sanitize_textarea_field',
     608            ),
     609        ),
     610        'chat_moderators_array' => array(
     611            'name'          => 'iflychat_chat_moderators_array',
     612            'default'       => '',
     613            'desc'          => 'Specify WordPress username of users who should be chat moderators (separated by comma)',
     614            'input_type'    => 'textarea',
     615            'sanitize_data' => array(
     616                'type'              => 'string',
     617                'sanitize_callback' => 'sanitize_textarea_field',
     618            ),
     619        ),
     620        'chat_admins_array'     => array(
     621            'name'          => 'iflychat_chat_admins_array',
     622            'default'       => '',
     623            'desc'          => 'Specify WordPress username of users who should be chat admininstrators (separated by comma)',
     624            'input_type'    => 'textarea',
     625            'sanitize_data' => array(
     626                'type'              => 'string',
     627                'sanitize_callback' => 'sanitize_textarea_field',
     628            ),
     629        ),
     630        'session_caching'       => array(
     631            'name'          => 'iflychat_session_caching',
     632            'default'       => '2',
     633            'desc'          => 'Enable Session Caching',
     634            'input_type'    => 'dropdown',
     635            'data'          => array(
     636                '1' => 'Yes',
     637                '2' => 'No',
     638            ),
     639            'sanitize_data' => array(
     640                'type'              => 'string',
     641                'sanitize_callback' => 'sanitize_text_field',
     642            ),
     643        ),
     644    );
     645
     646    return $options;
     647
     648}
     649
     650/**
     651 * Function to create Settings page
     652 *
     653 * @since 4.7.0
     654 */
     655function iflychat_settings() {
     656    wp_enqueue_script( 'iflychat-admin', IFLYCHAT_URL . 'js/iflychat.admin.script.js', array(), filemtime( IFLYCHAT_DIR . 'js/iflychat.admin.script.js' ), true );
     657
     658    $active_tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : 'plugin_settings'; //phpcs:ignore
     659    $i          = 0;
     660    ?>
     661    <div class="wrap">
     662        <h1><?php esc_html_e( 'iFlyChat Settings', 'iflychat_settings' ); ?></h1>
     663        <h2 class="nav-tab-wrapper">
     664            <a href="?page=iflychat_settings&tab=plugin_settings" class="nav-tab <?php echo esc_attr( 'plugin_settings' === $active_tab ) ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Plugin Settings', 'iflychat_settings' ); ?></a>
     665            <a href="?page=iflychat_settings&tab=app_settings" class="nav-tab <?php echo esc_attr( 'app_settings' === $active_tab ) ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'App Settings', 'iflychat_settings' ); ?></a>
     666        </h2>
     667        <?php
     668        if ( 'plugin_settings' === $active_tab ) {
     669            $result = _iflychat_get_auth();
     670            if ( is_array( $result ) ) {
     671
     672                if ( 403 === $result['code'] ) {
     673                    ?>
     674                    <div id="message" class="error"><p><strong><?php esc_html_e( 'Invalid API Key.', 'iflychat_settings' ); ?></strong></p></div>
     675                    <?php
     676                } elseif ( 503 === $result['code'] ) {
     677                    ?>
     678                    <div id="message" class="error"><p><strong><?php esc_html_e( '503 Error. Service Unavailable.', 'iflychat_settings' ); ?></strong></p></div>
     679                    <?php
     680                } else {
     681                    $msg = 'Error Message - ' . $result['message'] . '. Error Code - ' . $result['code'];
     682                    ?>
     683                    <div id="message" class="error"><p><strong><?php echo esc_attr( $msg ); ?></strong></p></div>
     684                    <?php
     685                }
     686            }
     687
     688            if ( iflychat_validate_fields() ) {
     689                if ( isset( $_GET['updated'] ) && 'true' === trim( sanitize_text_field( wp_unslash( $_GET['updated'] ) ) ) ) { //phpcs:ignore
     690                    ?>
     691                    <div id="message" class="updated fade"><p><strong><?php esc_html_e( 'Settings Updated', 'iflychat_settings' ); ?></strong></p></div>
     692                    <?php
     693                }
     694            } else {
     695                ?>
     696                <div id="message" class="error"><p><strong><?php esc_html_e( 'Invalid APP ID.', 'iflychat_settings' ); ?></strong></p></div>
     697                <?php
     698            }
     699            $action_url = '';
     700            if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
     701                $action_url = 'edit.php?action=iflychat_network_settings';
     702            } else {
     703                $action_url = 'options.php';
     704            }
     705            ?>
     706            <form method="post" action="<?php echo esc_url( $action_url ); ?>">
     707                <div>
     708                    <?php
     709                    settings_fields( 'iflychat-settings' );
     710                    wp_nonce_field( 'iflychat_network_settings_form', 'iflychat_network_settings_form_nonce' );
     711                    ?>
     712                </div>
     713
     714                <?php
     715                $options = iflychat_set_options();
     716                ?>
     717                <table class="form-table">
     718                    <?php
     719                    if ( count( $options ) > 0 ) {
     720                        foreach ( $options as $option ) {
     721                            $opt_name = iflychat_get_option( $option['name'] );
     722
     723                            if ( 'dropdown' === $option['input_type'] ) {
     724                                ?>
     725                                <tr valign="top">
     726                                    <th scope="row"><?php echo wp_kses_post( $option['desc'] ); ?></th>
     727                                    <td>
     728                                        <select id="<?php echo esc_attr( $option['name'] ); ?>" name="<?php echo esc_attr( $option['name'] ); ?>">
     729                                            <?php
     730                                            foreach ( $option['data'] as $opt => $value ) {
     731                                                ?>
     732                                                    <option <?php echo esc_attr( trim( $opt_name ) === trim( $opt ) ? 'selected="selected"' : '' ); ?> value="<?php echo esc_attr( $opt ); ?>"><?php echo esc_attr( $value ); ?></option>
     733                                                <?php
     734                                            } //endforeach
     735                                            ?>
     736                                        </select>
     737                                    </td>
     738                                </tr>
     739                                <?php
     740                                // If option type is text, do this.
     741                            } elseif ( 'text' === $option['input_type'] ) {
     742                                ?>
     743                                <tr valign="top">
     744                                    <th scope="row"><?php echo wp_kses_post( $option['desc'] ); ?></th>
     745                                    <td><input id="<?php echo esc_attr( $option['name'] ); ?>" name="<?php echo esc_attr( $option['name'] ); ?>" value="<?php echo esc_attr( $opt_name ); ?>" size="64" />
     746                                    </td>
     747                                </tr>
     748                                <?php
     749                                // If option type is text, do this.
     750                            } elseif ( 'textarea' === $option['input_type'] ) {
     751                                ?>
     752                                <tr valign="top" id="<?php echo esc_attr( 'iflychat_path_pages' === $option['name'] ? $option['name'] : '' ); ?>">
     753                                    <th scope="row"><?php echo wp_kses_post( $option['desc'] ); ?></th>
     754                                    <td><textarea  cols="80" rows="6" name="<?php echo esc_attr( $option['name'] ); ?>"><?php echo esc_attr( trim( $opt_name ) ); ?>
     755                                            </textarea>
     756                                    </td>
     757                                </tr>
     758                                <?php
     759                            } elseif ( 'button' === $option['input_type'] ) {
     760                                ?>
     761                                <tr valign="top">
     762                                    <th scope="row"><?php echo wp_kses_post( $option['desc'] ); ?></th>
     763                                    <td><a target="_blank" href="<?php echo esc_url( $option['link'] ); ?>"><input type="button" value="<?php echo esc_attr( $option['default'] ); ?>"></a>
     764                                    </td>
     765                                </tr>
     766                                <?php
     767                            }
     768                        }//endforeach
     769                    }
     770                    ?>
     771                </table>
     772                <p class="submit"><input type="submit" class="button-primary" value="<?php esc_html_e( 'Update', 'iflychat_settings' ); ?>" /></p>
     773            </form>     
     774            <br />
     775            <hr />
     776            <br />
     777            <h3><?php esc_html_e( 'Debug Information', 'iflychat_settings' ); ?></h3>
     778            <p>
     779                <?php esc_html_e( 'Having problems with iFlyChat? Check out our installation guide', 'iflychat_settings' ); ?>
     780                <a href="https://iflychat.com/installation/wordpress-chat-plugin" target="_blank"><?php esc_html_e( 'here', 'iflychat_settings' ); ?></a>
     781                <?php esc_html_e( '. You can also open a support ticket and we will look into it immediately. Please include the debug information given below.', 'iflychat_settings' ); ?>
     782                <a href="https://iflychat.com/contact" target="_blank"><?php esc_html_e( 'Contact support', 'iflychat_settings' ); ?></a>
     783            </p>
     784            <?php
     785            global $wp_version;
     786
     787            if ( ! function_exists( 'wp_get_theme' ) ) {
     788                $theme      = wp_get_themes();
     789                $theme_name = esc_html( $theme['Name'] . ' ' . $theme['Version'] );
     790            } else {
     791                $theme      = wp_get_theme();
     792                $theme_name = esc_html( $theme->Name . ' ' . $theme->Version ); // phpcs:ignore
     793            }
     794            ?>
     795            <div style="font-size: 15px;">
     796                <?php echo esc_html( 'URL : ' ); ?><?php echo esc_url( get_option( 'siteurl' ) ); ?><br/>
     797                <?php echo esc_html( 'PHP Version : ' ); ?><?php echo esc_html( phpversion() ); ?><br/>
     798                <?php echo esc_html( 'WordPress Version : ' ); ?><?php echo esc_html( $wp_version ); ?><br/>
     799                <?php echo esc_html( 'Active Theme : ' ); ?><?php echo esc_attr( $theme_name ); ?><br/>
     800                <?php echo esc_html( 'URL Open Method : ' ); ?><?php echo esc_html( iflychat_url_method() ); ?><br/>
     801                <?php echo esc_html( 'Plugin Version : ' ); ?><?php echo esc_html( IFLYCHAT_PLUGIN_VERSION ); ?><br/>
     802                <?php echo esc_html( 'Settings: iflychat_is_installed: ' ); ?><?php echo esc_html( iflychat_is_installed() . ' | ' ); ?>
     803                <?php
     804                foreach ( iflychat_options() as $opt ) {
     805                    echo esc_html( ' ' . $opt . ' : ' . get_option( $opt ) . ' | ' );
     806                }
     807                ?>
     808                <br/>
     809                <?php echo esc_html( 'Plugins : ' ); ?>
     810                <?php
     811                foreach ( get_plugins() as $key => $plugin ) {
     812                    $isactive = '';
     813                    if ( is_plugin_active( $key ) ) {
     814                        $isactive = '(active)';
     815                    }
     816                    echo esc_html( ' ' . $plugin['Name'] . ' ' . $plugin['Version'] . ' ' . $isactive . ' | ' );
     817                }
     818                ?>
     819            </div>
     820            <br/>
     821            <?php
     822        } else {
     823            $dashboard_url = 'https://dashboard.chatcamp.io';
     824            if ( ! iflychat_chatcamp_check() ) {
     825                $iflychat_host = DRUPALCHAT_EXTERNAL_A_HOST;
     826
     827                $host      = explode( '/', $iflychat_host );
     828                $host_name = isset( $host[2] ) ? $host[2] : '';
     829                if ( isset( $_SESSION['token'] ) && ! empty( $_SESSION['token'] ) ) {
     830                    $token = $_SESSION['token'];
     831                } else {
     832                    $token = _iflychat_get_auth()->key;
     833                }
     834                $dashboard_url = 'https://cdn.iflychat.com/apps/dashboard/#/settings/app?sessid=' . $token . '&hostName=' . $host_name . '&hostPort=' . DRUPALCHAT_EXTERNAL_A_PORT;
     835            }
     836            ?>
     837            <br/>
     838            <input type="button" class="button-primary" value="Open App Dashboard in new tab" onclick="window.open( '<?php echo esc_url( $dashboard_url ); ?>')">
     839            <?php
     840        }
     841        ?>
     842    </div>
     843    <?php
     844}
     845
     846
     847/**
     848 * Function to check if iflychat is installed
     849 *
     850 * @since 4.7.0
     851 */
    705852function iflychat_is_installed() {
    706   $iflychat_api_key = get_option('iflychat_api_key');
    707   $iflychat_app_id = get_option('iflychat_app_id');
    708   if ( strlen( $iflychat_api_key ) > 0 && strlen( $iflychat_app_id ) > 0 ) {
    709     return true;
    710   }
    711   else {
    712     return false;
    713   }
    714 }
     853    $iflychat_api_key = get_option( 'iflychat_api_key' );
     854    $iflychat_app_id  = get_option( 'iflychat_app_id' );
     855    if ( strlen( trim( $iflychat_api_key ) ) > 0 && strlen( trim( $iflychat_app_id ) ) > 0 ) {
     856        return true;
     857    } else {
     858        return false;
     859    }
     860}
     861
     862/**
     863 * Function to array of settings options.
     864 *
     865 * @since 4.7.0
     866 * @return Array settings options as Array.
     867 */
    715868function iflychat_options() {
    716   return array(
    717     'iflychat_app_id',
    718     'iflychat_api_key',
    719     'iflychat_use_display_name',
    720     'iflychat_enable_friends',
    721     'iflychat_popup_chat',
    722     'iflychat_path_pages',
    723     'iflychat_chat_moderators_array',
    724     'iflychat_chat_admins_array',
    725     'iflychat_session_caching'
    726   );
    727 }
     869    return array(
     870        'iflychat_app_id',
     871        'iflychat_api_key',
     872        'iflychat_use_display_name',
     873        'iflychat_enable_friends',
     874        'iflychat_popup_chat',
     875        'iflychat_path_pages',
     876        'iflychat_chat_moderators_array',
     877        'iflychat_chat_admins_array',
     878        'iflychat_session_caching',
     879    );
     880}
     881
     882/**
     883 * Function to get url method.
     884 *
     885 * @since 4.7.0
     886 * @return String URL method as String.
     887 */
    728888function iflychat_url_method() {
    729   if(function_exists('curl_init')) {
    730     return 'curl';
    731   } else if(ini_get('allow_url_fopen') && function_exists('stream_get_contents')) {
    732     return 'fopen';
    733   } else {
    734     return 'fsockopen';
    735   }
    736 }
    737 //add settings page
    738 function iflychat_settings_page()
    739 {
    740     if (is_multisite() && is_plugin_active_for_network(plugin_basename(__FILE__))) {
    741         add_submenu_page('settings.php', 'iFlyChat Settings', 'iFlyChat Settings', 'manage_options', 'iflychat_settings', 'iflychat_settings');
    742     } else {
    743         add_options_page('iFlyChat Settings', 'iFlyChat Settings', 'manage_options', 'iflychat_settings', 'iflychat_settings');
    744     }
    745 }
    746 //register settings loops through options
    747 function iflychat_register_settings()
    748 {
    749     $options = iflychat_set_options(); //get options array
    750 
    751     foreach($options as $option){
    752         register_setting('iflychat-settings', $option['name']);
    753         //register each setting with option's 'name'
    754 
    755         if (iflychat_get_option($option['name']) === false) {
    756             iflychat_add_option($option['name'], $option['default'], '', 'yes'); //set option defaults
    757         }
    758     }
    759 
    760 //    if (iflychat_get_option('iflychat_promote_plugin') === false) {
    761 //        iflychat_add_option('iflychat_promote_plugin', '0', '', 'yes');
    762 //    }
    763 //
    764 //    if (iflychat_get_option('iflychat_ext_d_i') === false) {
    765 //        iflychat_add_option('iflychat_ext_d_i', '1', '', 'yes');
    766 //    }
     889    if ( function_exists( 'curl_init' ) ) {
     890        return 'curl';
     891    } elseif ( ini_get( 'allow_url_fopen' ) && function_exists( 'stream_get_contents' ) ) {
     892        return 'fopen';
     893    } else {
     894        return 'fsockopen';
     895    }
     896}
     897
     898/**
     899 * Function to add settings page.
     900 *
     901 * @since 4.7.0
     902 */
     903function iflychat_settings_page() {
     904    if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
     905        add_submenu_page( 'settings.php', 'iFlyChat Settings', 'iFlyChat Settings', 'manage_options', 'iflychat_settings', 'iflychat_settings' );
     906    } else {
     907        add_options_page( 'iFlyChat Settings', 'iFlyChat Settings', 'manage_options', 'iflychat_settings', 'iflychat_settings' );
     908    }
     909}
     910
     911/**
     912 * Function to register settings loops through options.
     913 *
     914 * @since 4.7.0
     915 */
     916function iflychat_register_settings() {
     917    $options = iflychat_set_options();
     918    foreach ( $options as $option ) {
     919            register_setting(
     920                'iflychat-settings',
     921                $option['name'],
     922                $option['sanitize_data'],
     923            );
     924
     925        // register each setting with option's 'name'.
     926        if ( iflychat_get_option( $option['name'] ) === false ) {
     927            iflychat_add_option( $option['name'], $option['default'], '', 'yes' ); // set option defaults.
     928        }
     929    }
     930
     931    // phpcs:ignore
     932    /*
     933    Commented Already
     934    if (iflychat_get_option('iflychat_promote_plugin') === false) {
     935    iflychat_add_option('iflychat_promote_plugin', '0', '', 'yes');
     936    }
     937
     938    if (iflychat_get_option('iflychat_ext_d_i') === false) {
     939    iflychat_add_option('iflychat_ext_d_i', '1', '', 'yes');
     940    }
     941    */
    767942
    768943}
    769944add_action( 'admin_init', 'iflychat_register_settings' );
    770945
    771 function iflychat_validate_fields(){
    772     $app_id = iflychat_get_option('iflychat_app_id');
    773     if(iflychat_chatcamp_check() || (strlen($app_id) == 36 && $app_id[14] == '4')){
    774         return true;
    775         //$errors->add( 'iflychat_app_id_error', __( '<strong>ERROR</strong>: Invalid APP ID.' ) );
    776     }else{
    777         return false;
    778     }
    779     //return $errors;
    780 }
    781 
    782 
    783 if (is_multisite() && is_plugin_active_for_network(plugin_basename(__FILE__))) {
    784     add_action('network_admin_menu', 'iflychat_settings_page');
     946/**
     947 * Function to Validate form fields.
     948 *
     949 * @since 4.7.0
     950 * @return Boolean true or false as Boolean.
     951 */
     952function iflychat_validate_fields() {
     953    $app_id = iflychat_get_option( 'iflychat_app_id' );
     954    if ( iflychat_chatcamp_check() || ( 36 === strlen( $app_id ) && '4' === $app_id[14] ) ) {
     955        return true;
     956    } else {
     957        return false;
     958    }
     959}
     960
     961if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
     962    add_action( 'network_admin_menu', 'iflychat_settings_page' );
    785963} else {
    786     add_action('admin_menu', 'iflychat_settings_page');
    787 }
    788 
    789 
    790 function iflychat_network_settings()
    791 {
    792 
    793     if (($_POST['option_page'] == "iflychat-settings") && ($_POST['action'] == "update")) {
    794           foreach ((array)$_POST as $key => $value) {
    795               if (substr($key, 0, 9) === "iflychat_") {
    796                 update_site_option($key, $value);
    797               }
    798           }
    799 
    800 
    801     }
    802     // redirect to settings page in network
    803     wp_redirect(
    804         add_query_arg(
    805             array('page' => 'iflychat_settings', 'updated' => 'true'),
    806             network_admin_url('settings.php')
    807         )
    808     );
    809     exit;
    810 }
    811 
    812 if (is_multisite() && is_plugin_active_for_network(plugin_basename(__FILE__))) {
    813     add_action('network_admin_edit_iflychat_network_settings', 'iflychat_network_settings');
    814 }
    815 
    816 add_action('init', 'iflychat_init');
    817 add_action('wp_ajax_nopriv_iflychat-mobile-auth', 'iflychat_mobile_auth');
    818 add_action('wp_ajax_iflychat-mobile-auth', 'iflychat_mobile_auth');
    819 add_action('wp_ajax_nopriv_iflychat-get', 'iflychat_submit_uth');
    820 add_action('wp_ajax_iflychat-get', 'iflychat_submit_uth');
    821 add_action('wp_ajax_nopriv_iflychat-offline-msg', 'iflychat_send_offline_message');
    822 add_action('wp_ajax_iflychat-offline-msg', 'iflychat_send_offline_message');
    823 add_action('wp_ajax_nopriv_iflychat-change-guest-name', 'iflychat_change_guest_name');
    824 add_action('wp_login', 'iflychat_user_login');
    825 add_action('wp_logout', 'iflychat_user_logout');
    826 add_shortcode('iflychat_inbox', 'iflychat_get_inbox');
    827 add_shortcode('iflychat_message_thread', 'iflychat_get_message_thread');
    828 add_shortcode('iflychat_embed', 'iflychat_get_embed_code');
    829 register_activation_hook(__FILE__, 'iflychat_install');
    830 register_deactivation_hook(__FILE__, 'iflychat_uninstall');
    831 function iflychat_match_path($path, $patterns)
    832 {
    833     $to_replace = array(
    834         '/(\r\n?|\n)/',
    835         '/\\\\\*/',
    836     );
    837     $replacements = array(
    838         '|',
    839         '.*',
    840     );
    841     $patterns_quoted = preg_quote($patterns, '/');
    842     $regexps[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
    843     return (bool)preg_match($regexps[$patterns], $path);
    844 }
    845 
    846 function iflychat_path_check()
    847 {
    848     $page_match = FALSE;
    849 
    850     // if (trim(iflychat_get_option('iflychat_path_pages')) != '') {
    851         if (function_exists('mb_strtolower')) {
    852             $pages = mb_strtolower(iflychat_get_option('iflychat_path_pages'));
    853             $path = mb_strtolower($_SERVER['REQUEST_URI']);
    854         } else {
    855             $pages = strtolower(iflychat_get_option('iflychat_path_pages'));
    856             $path = strtolower($_SERVER['REQUEST_URI']);
    857         }
    858         $page_match = iflychat_match_path($path, $pages);
    859         if(iflychat_get_option('iflychat_popup_chat') == '3') $page_match = !$page_match;
    860         if(iflychat_get_option('iflychat_popup_chat') == '4') $page_match = $page_match;
    861         //$page_match = (iflychat_get_option('iflychat_popup_chat') == '3') ? (!$page_match) : $page_match;
    862     // } else
    863     if (iflychat_get_option('iflychat_popup_chat') == '1') {
    864         $page_match = TRUE;
    865     }
    866     return $page_match;
    867 }
    868 
    869 function iflychat_mail_set_content_type()
    870 {
    871     return "text/html";
    872 }
    873 
    874 function iflychat_send_offline_message()
    875 {
    876     if (isset($_POST['drupalchat_m_contact_details']) && isset($_POST['drupalchat_m_message'])) {
    877         global $user;
    878         $drupalchat_offline_mail = array();
    879         $drupalchat_offline_mail['subject'] = 'iFlyChat: Message from Customer';
    880         $drupalchat_offline_mail['contact_details'] = '<p>' . iflychat_get_option('iflychat_support_chat_offline_message_contact') . ': ' . ($_POST['drupalchat_m_contact_details']) . '</p>';
    881         $drupalchat_offline_mail['message'] = '<p>' . iflychat_get_option('iflychat_support_chat_offline_message_label') . ': ' . ($_POST['drupalchat_m_message']) . '</p>';
    882         $drupalchat_offline_mail['message'] = $drupalchat_offline_mail['contact_details'] . '<br><br>' . $drupalchat_offline_mail['message'];
    883         add_filter('wp_mail_content_type', 'iflychat_mail_set_content_type');
    884         $result = wp_mail(iflychat_get_option('iflychat_support_chat_offline_message_email'), $drupalchat_offline_mail['subject'], $drupalchat_offline_mail['message']);
    885     }
    886     $response = json_encode($result);
    887     header("Content-Type: application/json");
    888     echo $response;
    889     exit;
    890 }
    891 
    892 function iflychat_check_chat_admin()
    893 {
    894   $current_user =  wp_get_current_user();
    895     if (current_user_can('activate_plugins')) {
    896         return TRUE;
    897     }
    898     $a = iflychat_get_option('iflychat_chat_admins_array');
    899     if (!empty($a) && ($current_user->ID)) {
    900         $a_names = explode(",", $a);
    901         foreach ($a_names as $an) {
    902             $aa = trim($an);
    903             if ($aa == $current_user->user_login) {
    904                 return TRUE;
    905                 break;
    906             }
    907         }
    908     }
    909     return FALSE;
    910 }
    911 
    912 function iflychat_check_chat_moderator()
    913 {
    914   $current_user =  wp_get_current_user();
    915    
    916     $a = iflychat_get_option('iflychat_chat_moderators_array');
    917    
    918     if (!empty($a) && ($current_user->ID)) {
    919         $a_names = explode(",", $a);
    920         foreach ($a_names as $an) {
    921             $aa = trim($an);
    922             if ($aa == $current_user->user_login) {
    923                 return TRUE;
    924                 break;
    925             }
    926         }
    927     }
    928     return FALSE;
    929 }
    930 
    931 function iflychat_token_destroy()
    932 {
    933     $data = array(
    934         'api_key' => iflychat_get_option('iflychat_api_key')
    935     );
    936     $options = array(
    937         'method' => 'POST',
    938         'body' => $data,
    939         'timeout' => 15,
    940         'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
    941         'sslverify' => false,
    942     );
    943     $result = wp_remote_head(iflychat_get_host(TRUE) . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/api/1.1/token/'
    944         . $_SESSION['token'] . '/delete', $options);
    945 //if(!is_wp_error($result) && $result['response']['code'] == 200) {
    946     session_unset();
    947 //}
    948 }
    949 
    950 function iflychat_user_login()
    951 {
    952     setcookie("iflychat_key", "", time() - 3600, "/");
    953     setcookie("iflychat_css", "", time() - 3600, "/");
    954     setcookie("iflychat_time", "", time() - 3600, "/");
    955 }
    956 
    957 function iflychat_user_logout()
    958 {
    959     setcookie("iflychat_key", "", time() - 3600, "/");
    960     setcookie("iflychat_css", "", time() - 3600, "/");
    961     setcookie("iflychat_time", "", time() - 3600, "/");
    962     iflychat_token_destroy();
    963 }
    964 
    965 function iflychat_get_inbox()
    966 {
    967     $data = array(
    968         'uid' => iflychat_get_user_id(),
    969         'api_key' => iflychat_get_option('iflychat_api_key'),
    970     );
    971     $options = array(
    972         'method' => 'POST',
    973         'body' => $data,
    974         'timeout' => 15,
    975         'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
    976         'sslverify' => false,
    977     );
    978     $result = wp_remote_head(iflychat_get_host(TRUE) . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/r/', $options);
    979     $output = '';
    980     if (!is_wp_error($result) && $result['response']['code'] == 200) {
    981         $query = json_decode($result['body']);
    982         $timezone_offet = iflychat_get_option('gmt_offset');
    983         $date_format = iflychat_get_option('date_format');
    984         $time_format = iflychat_get_option('time_format');
    985         foreach ($query as $record) {
    986             $rt = $record->timestamp + ($timezone_offet * 3600);
    987             $output .= '<div style="display:block;border-bottom: 1px solid #ccc; padding: 10px;"><div style="font-size:130%; display: inline;">' . $record->name . '</div><div style="float:right;color:#AAA; font-size: 70%;">' . date("{$date_format} {$time_format}", $rt) . '</div><div style="display: block; padding: 10px;">' . $record->message . '</div></div>';
    988         }
    989     }
    990     return $output;
    991 }
    992 
    993 function iflychat_get_message_thread($atts)
    994 {
    995     extract(shortcode_atts(array(
    996         'room_id' => '0',
    997         'id' => '0',
    998     ), $atts));
    999     if (($room_id[0] == 'c') && ($room_id[1] == '-')) {
    1000         $room_id = substr($room_id, 2);
    1001     } else if ($id != '0') {
    1002         if (($id[0] == 'c') && ($id[1] == '-')) {
    1003             $room_id = substr($id, 2);
    1004         } else {
    1005             $room_id = $id;
    1006         }
    1007     }
    1008     $data = array(
    1009         'uid1' => iflychat_get_user_id(),
    1010         'uid2' => ('c-' . $room_id),
    1011         'api_key' => iflychat_get_option('iflychat_api_key'),
    1012     );
    1013     $options = array(
    1014         'method' => 'POST',
    1015         'body' => $data,
    1016         'timeout' => 15,
    1017         'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
    1018         'sslverify' => false,
    1019     );
    1020     $result = wp_remote_head(iflychat_get_host(TRUE) . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/q/', $options);
    1021     $output = '';
    1022     if (!is_wp_error($result) && $result['response']['code'] == 200) {
    1023         $query = json_decode($result['body']);
    1024         $timezone_offet = iflychat_get_option('gmt_offset');
    1025         $date_format = iflychat_get_option('date_format');
    1026         $time_format = iflychat_get_option('time_format');
    1027         foreach ($query as $record) {
    1028             $rt = $record->timestamp + ($timezone_offet * 3600);
    1029             $output .= '<div style="display:block;border-bottom: 1px solid #ccc; padding: 1% 0% 1% 0%;"></div><div style="display:block; padding-top: 1%; padding-bottom: 0%"><div style="font-size:100%; display: inline;"><a href="#">' . $record->from_name . '</a></div><div style="float:right;font-size: 70%;">' . date("{$date_format} {$time_format}", $rt) . '</div><div style="display: block; padding-top: 1%; padding-bottom: 0%">' . $record->message . '</div></div>';
    1030         }
    1031     }
    1032     return $output;
    1033 }
    1034 
    1035 function iflychat_get_embed_code($atts)
    1036 {
    1037 
    1038     global $iflychat_engine;
    1039 
    1040     extract(shortcode_atts(array(
    1041         'room_id' => '0',
    1042         'id' => '0',
    1043         'hide_user_list' => 'no',
    1044         'hide_popup_chat' => 'no',
    1045         'height' => '550px',
    1046     ), $atts));
    1047 
    1048     $output = '';
    1049 
    1050     if ($iflychat_engine) {
    1051 
    1052         $output = '<style>.drupalchat-embed-chatroom-content {height: ' . $height . ' !important;}';
    1053         if ($hide_user_list == "yes") {
    1054             $output .= '#drupalchat-embed-user-list {display:none !important;}.drupalchat-embed-chatroom-content {width:95% !important;}';
    1055         }
    1056         $output .= '</style>';
    1057         $output .= '<script type="text/javascript">if(typeof(iflyembed) === "undefined") {iflyembed = {};iflyembed.settings = {};iflyembed.settings.ifly = {};}iflyembed.settings.ifly.embed = "1";iflyembed.settings.ifly.ur_hy = "1";iflyembed.settings.ifly.embed_msg = "Type your message here. Press Enter to send.";iflyembed.settings.ifly.embed_online_user_text = "Online Users";</script>';
    1058         if (($room_id[0] == 'c') && ($room_id[1] == '-')) {
    1059             $room_id = substr($room_id, 2);
    1060         } else if ($id != '0') {
    1061             if (($id[0] == 'c') && ($id[1] == '-')) {
    1062                 $room_id = substr($id, 2);
    1063             } else {
    1064                 $room_id = $id;
    1065             }
    1066         }
    1067         $output .= '<div id="drupalchat-embed-chatroom-' . $room_id . '" class="drupalchat-embed-chatroom-container';
    1068         if ($hide_popup_chat == "yes") {
    1069             $output .= ' drupalchat-hide-popup-chat';
    1070         }
    1071         $output .= '"></div>';
    1072     } else if (iflychat_check_chat_admin()) {
    1073         $output .= '<div style="background-color:#eee;color:red;padding:5px;">iFlyChat is NOT set to load on this page. Please check the path visibility settings on iFlyChat plugin settings page. In case of any query, please create a support ticket <a href="https://iflychat.com/contact" target="_blank">here</a>. This error message is shown only to chat admins.</div>';
    1074     }
    1075     return $output;
    1076 }
    1077 
    1078 function iflychat_get_user_pic_url()
    1079 {
    1080      $current_user = wp_get_current_user();
    1081     $url = 'http://www.gravatar.com/avatar/' . (($current_user->ID) ? (md5(strtolower($current_user->user_email))) : ('00000000000000000000000000000000')) . '?d=mm&size=24';
    1082     $hook_url = apply_filters('iflychat_get_user_avatar_url_filter', '',$current_user->ID);
    1083     if (!empty($hook_url)) {
    1084         return $hook_url;
    1085     }
    1086     if (function_exists("bp_core_fetch_avatar") && ($current_user->ID > 0)) {
    1087         $url = iflychat_get_avatar_url_from_html(bp_core_fetch_avatar(array('item_id' => iflychat_get_user_id(), 'html' => false)));
    1088     } else if (function_exists("user_avatar_fetch_avatar") && ($current_user->ID > 0)) {
    1089         $local_url = user_avatar_fetch_avatar(array('html' => false, 'item_id' => $current_user->ID));
    1090         if ($local_url) {
    1091             $url = $local_url;
    1092         }
    1093     } else if (function_exists("userpro_profile_data") && ($current_user->ID > 0)) {
    1094         $user_id = $current_user->ID;
    1095         $url = userpro_profile_data('profilepicture', $user_id);
    1096     } else if (function_exists("um_get_avatar_url") && ($current_user->ID > 0)) {
    1097         $user_id = $current_user->ID;
    1098         $url = um_get_avatar_url(get_avatar($user_id, $size = 96));
    1099     } else if (function_exists("get_wp_user_avatar_src") && ($current_user->ID > 0)) {
    1100         $url = get_wp_user_avatar_src(iflychat_get_user_id());
    1101     } else if (function_exists("get_simple_local_avatar") && ($current_user->ID > 0)) {
    1102         $source = get_simple_local_avatar(iflychat_get_user_id());
    1103         $source = explode('src="', $source);
    1104         if (isset($source[1])) {
    1105             $source = explode('"', $source[1]);
    1106         } else {
    1107             $source = explode("src='", $source[0]);
    1108             if (isset($source[1])) {
    1109                 $source = explode("'", $source[1]);
    1110             } else {
    1111                 $source[0] = 'http://www.gravatar.com/avatar/' . (($current_user->ID) ? (md5(strtolower($current_user->user_email))) : ('00000000000000000000000000000000')) . '?d=mm&size=24';
    1112             }
    1113         }
    1114         $url = $source[0];
    1115     } else if ($current_user->ID > 0) {
    1116         if (false && function_exists("get_avatar_url")) {
    1117             $url = get_avatar_url(iflychat_get_user_id());
    1118         } else {
    1119             $url = iflychat_get_avatar_url_from_html(get_avatar(iflychat_get_user_id()));
    1120         }
    1121     }
    1122 
    1123     $pos = strpos($url, ':');
    1124     if ($pos !== false) {
    1125         $url = substr($url, $pos + 1);
    1126     }
    1127     return $url;
    1128 }
    1129 
    1130 function iflychat_get_user_profile_url()
    1131 {
    1132     global $userpro;
    1133     $current_user =  wp_get_current_user();
    1134 
    1135     $upl = 'javascript:void(0)';
    1136     $hook_upl = apply_filters('iflychat_get_user_profile_url_filter', 'javascript:void(0)',$current_user->ID);
    1137     if ($hook_upl == $upl) {
    1138         if (function_exists("bp_core_get_userlink") && ($current_user->ID > 0)) {
    1139             $upl = bp_core_get_userlink($current_user->ID, false, true);
    1140         } else if (function_exists("um_user_profile_url") && ($current_user->ID > 0)) {
    1141             $upl = um_user_profile_url($current_user->ID, false, true);
    1142         } else if (($current_user->ID > 0) && ($userpro != null)) {
    1143             $upl = ($userpro->permalink($current_user->ID));
    1144         }
    1145         return $upl;
    1146     } else {
    1147         return $hook_upl;
    1148     }
    1149 }
    1150 
    1151 function iflychat_get_option($name)
    1152 {
    1153     if (is_multisite() && is_plugin_active_for_network(plugin_basename(__FILE__))) {
    1154         return get_site_option($name);
    1155     } else {
    1156         return get_option($name);
    1157     }
    1158 }
    1159 
    1160 function iflychat_add_option($name, $value, $v2, $v3)
    1161 {
    1162     if (is_multisite() && is_plugin_active_for_network(plugin_basename(__FILE__))) {
    1163         return add_site_option($name, $value, $v2, $v3);
    1164     } else {
    1165         return add_option($name, $value, $v2, $v3);
    1166     }
    1167 }
    1168 
    1169 function iflychat_update_option($name, $value)
    1170 {
    1171     if (is_multisite() && is_plugin_active_for_network(plugin_basename(__FILE__))) {
    1172         return update_site_option($name, $value);
    1173     } else {
    1174         return update_option($name, $value);
    1175     }
    1176 }
    1177 
    1178 function iflychat_check_access()
    1179 {
    1180   global $current_user;
    1181     $flag = apply_filters('iflychat_check_access_filter', true, $current_user->ID);
    1182     if ($flag == true) {
    1183         return true;
    1184     } else {
    1185         return false;
    1186     }
    1187     exit;
    1188 }
    1189 
    1190 function iflychat_get_avatar_url_from_html($source)
    1191 {
    1192     $source = explode('src="', $source);
    1193     if (isset($source[1])) {
    1194         $source = explode('"', $source[1]);
    1195     } else {
    1196         $source = explode("src='", $source[0]);
    1197         if (isset($source[1])) {
    1198             $source = explode("'", $source[1]);
    1199         } else {
    1200             //$source[0] = '';
    1201         }
    1202     }
    1203     return $source[0];
    1204 }
    1205 
    1206 function iflychat_get_host($https = FALSE)
    1207 {
    1208     if (iflychat_get_option('iflychat_show_admin_list') == '1') {
    1209         if ($https) {
    1210             return 'https://support1.iflychat.com';
    1211         } else {
    1212             return 'http://support1.iflychat.com';
    1213         }
    1214     } else {
    1215         if ($https) {
    1216             return DRUPALCHAT_EXTERNAL_A_HOST;
    1217         } else {
    1218             return DRUPALCHAT_EXTERNAL_HOST;
    1219         }
    1220     }
    1221 }
    1222 
    1223 function iflychat_process_stop_word_list($words)
    1224 {
    1225     $new_arr = array_map('trim', explode(',', $words));
    1226     $final = implode(",", $new_arr);
    1227     return $final;
    1228 }
    1229 
     964    add_action( 'admin_menu', 'iflychat_settings_page' );
     965}
     966
     967/**
     968 * Function to Update site options.
     969 *
     970 * @since 4.7.0
     971 */
     972function iflychat_network_settings() {
     973    if ( isset( $_POST['iflychat_network_settings_form_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['iflychat_network_settings_form_nonce'] ) ), 'iflychat_network_settings_form' ) ) {
     974
     975        $option_page = ( isset( $_POST['option_page'] ) ? sanitize_text_field( wp_unslash( $_POST['option_page'] ) ) : '' );
     976        $action      = ( isset( $_POST['action'] ) ? sanitize_text_field( wp_unslash( $_POST['action'] ) ) : '' );
     977
     978        if ( ( 'iflychat-settings' === $option_page ) && ( 'update' === $action ) ) {
     979            foreach ( (array) $_POST as $key => $value ) { // phpcs:ignore
     980                if ( substr( $key, 0, 9 ) === 'iflychat_' ) {
     981                    update_site_option( $key, trim( $value ) );
     982                }
     983            }
     984        }
     985        // redirect to settings page in network.
     986        wp_safe_redirect(
     987            add_query_arg(
     988                array(
     989                    'page'    => 'iflychat_settings',
     990                    'updated' => 'true',
     991                ),
     992                network_admin_url( 'settings.php' ),
     993            )
     994        );
     995        exit;
     996    }
     997}
     998
     999if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
     1000    add_action( 'network_admin_edit_iflychat_network_settings', 'iflychat_network_settings' );
     1001}
     1002
     1003add_action( 'init', 'iflychat_init' );
     1004add_action( 'wp_ajax_nopriv_iflychat-mobile-auth', 'iflychat_mobile_auth' );
     1005add_action( 'wp_ajax_iflychat-mobile-auth', 'iflychat_mobile_auth' );
     1006add_action( 'wp_ajax_nopriv_iflychat-get', 'iflychat_submit_uth' );
     1007add_action( 'wp_ajax_iflychat-get', 'iflychat_submit_uth' );
     1008add_action( 'wp_ajax_nopriv_iflychat-offline-msg', 'iflychat_send_offline_message' );
     1009add_action( 'wp_ajax_iflychat-offline-msg', 'iflychat_send_offline_message' );
     1010add_action( 'wp_ajax_nopriv_iflychat-change-guest-name', 'iflychat_change_guest_name' );
     1011add_action( 'wp_login', 'iflychat_user_login' );
     1012add_action( 'wp_logout', 'iflychat_user_logout' );
     1013add_shortcode( 'iflychat_inbox', 'iflychat_get_inbox' );
     1014add_shortcode( 'iflychat_message_thread', 'iflychat_get_message_thread' );
     1015add_shortcode( 'iflychat_embed', 'iflychat_get_embed_code' );
     1016register_activation_hook( __FILE__, 'iflychat_install' );
     1017register_deactivation_hook( __FILE__, 'iflychat_uninstall' );
     1018
     1019/**
     1020 * Function to check path match or not.
     1021 *
     1022 * @since 4.7.0
     1023 * @param String $path path name as string.
     1024 * @param String $patterns path name as string.
     1025 * @return Boolean true or false as Boolean.
     1026 */
     1027function iflychat_match_path( $path, $patterns ) {
     1028    $to_replace   = array(
     1029        '/(\r\n?|\n)/',
     1030        '/\\\\\*/',
     1031    );
     1032    $replacements = array(
     1033        '|',
     1034        '.*',
     1035    );
     1036
     1037    $patterns_quoted      = preg_quote( $patterns, '/' );
     1038    $regexps[ $patterns ] = '/^(' . preg_replace( $to_replace, $replacements, $patterns_quoted ) . ')$/';
     1039    return (bool) preg_match( $regexps[ $patterns ], $path );
     1040}
     1041
     1042/**
     1043 * Function to check path check.
     1044 *
     1045 * @since 4.7.0
     1046 * @return Boolean $page_match true or false Boolean.
     1047 */
     1048function iflychat_path_check() {
     1049    $page_match = false;
     1050
     1051    $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
     1052
     1053    if ( function_exists( 'mb_strtolower' ) ) {
     1054        $pages = mb_strtolower( iflychat_get_option( 'iflychat_path_pages' ) );
     1055        $path  = mb_strtolower( $request_uri );
     1056    } else {
     1057        $pages = strtolower( iflychat_get_option( 'iflychat_path_pages' ) );
     1058        $path  = strtolower( $request_uri );
     1059    }
     1060    $page_match = iflychat_match_path( $path, $pages );
     1061
     1062    if ( '3' === iflychat_get_option( 'iflychat_popup_chat' ) ) {
     1063        $page_match = ! $page_match;
     1064    }
     1065    if ( '4' === iflychat_get_option( 'iflychat_popup_chat' ) ) {
     1066        $page_match = $page_match;
     1067    }
     1068    if ( '1' === iflychat_get_option( 'iflychat_popup_chat' ) ) {
     1069        $page_match = true;
     1070    }
     1071    return $page_match;
     1072}
     1073
     1074/**
     1075 * Function to return content type.
     1076 *
     1077 * @since 4.7.0
     1078 * @return string content type as string.
     1079 */
     1080function iflychat_mail_set_content_type() {
     1081    return 'text/html';
     1082}
     1083
     1084/**
     1085 * Function to send ofline message.
     1086 *
     1087 * @since 4.7.0
     1088 */
     1089function iflychat_send_offline_message() {
     1090
     1091    // phpcs:ignore
     1092    $contact_details = isset( $_POST['drupalchat_m_contact_details'] ) ? sanitize_textarea_field( wp_unslash( $_POST['drupalchat_m_contact_details'] ) ) : '';
     1093    // phpcs:ignore
     1094    $drupalchat_m_message = isset( $_POST['drupalchat_m_message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['drupalchat_m_message'] ) ) : '';
     1095
     1096    if ( isset( $contact_details ) && isset( $drupalchat_m_message ) ) { // phpcs:ignore
     1097        global $user;
     1098        $drupalchat_offline_mail                    = array();
     1099        $drupalchat_offline_mail['subject']         = 'iFlyChat: Message from Customer';
     1100        $drupalchat_offline_mail['contact_details'] = '<p>' . iflychat_get_option( 'iflychat_support_chat_offline_message_contact' ) . ': ' . ( $contact_details ) . '</p>';
     1101        $drupalchat_offline_mail['message']         = '<p>' . iflychat_get_option( 'iflychat_support_chat_offline_message_label' ) . ': ' . ( $drupalchat_m_message ) . '</p>';
     1102        $drupalchat_offline_mail['message']         = $drupalchat_offline_mail['contact_details'] . '<br><br>' . $drupalchat_offline_mail['message'];
     1103        add_filter( 'wp_mail_content_type', 'iflychat_mail_set_content_type' );
     1104        $result = wp_mail( iflychat_get_option( 'iflychat_support_chat_offline_message_email' ), $drupalchat_offline_mail['subject'], $drupalchat_offline_mail['message'] );
     1105    }
     1106
     1107    $response = wp_json_encode( $result );
     1108    header( 'Content-Type: application/json' );
     1109    echo $response; // phpcs:ignore
     1110    exit;
     1111}
     1112
     1113/**
     1114 * Function to check admin chat.
     1115 *
     1116 * @since 4.7.0
     1117 * @return Boolean admin chat true or false as Boolean.
     1118 */
     1119function iflychat_check_chat_admin() {
     1120
     1121    $current_user = wp_get_current_user();
     1122    if ( current_user_can( 'activate_plugins' ) ) {
     1123        return true;
     1124    }
     1125
     1126    $a = iflychat_get_option( 'iflychat_chat_admins_array' );
     1127    if ( ! empty( $a ) && ( $current_user->ID ) ) {
     1128        $a_names = explode( ',', $a );
     1129        foreach ( $a_names as $an ) {
     1130            $aa = trim( $an );
     1131            if ( $aa === $current_user->user_login ) {
     1132                return true;
     1133                break; // phpcs:ignore
     1134            }
     1135        }
     1136    }
     1137    return false;
     1138}
     1139
     1140/**
     1141 * Function to check chat is moderator.
     1142 *
     1143 * @since 4.7.0
     1144 * @return Boolean chat moderator true or false as Boolean.
     1145 */
     1146function iflychat_check_chat_moderator() {
     1147    $current_user = wp_get_current_user();
     1148
     1149    $a = iflychat_get_option( 'iflychat_chat_moderators_array' );
     1150    if ( ! empty( $a ) && ( $current_user->ID ) ) {
     1151        $a_names = explode( ',', $a );
     1152        foreach ( $a_names as $an ) {
     1153            $aa = trim( $an );
     1154            if ( $aa === $current_user->user_login ) {
     1155                return true;
     1156                break;// phpcs:ignore
     1157            }
     1158        }
     1159    }
     1160    return false;
     1161}
     1162
     1163/**
     1164 * Function to destroy session ( session unset ).
     1165 *
     1166 * @since 4.7.0
     1167 */
     1168function iflychat_token_destroy() {
     1169    $data = array(
     1170        'api_key' => iflychat_get_option( 'iflychat_api_key' ),
     1171    );
     1172
     1173    $options = array(
     1174        'method'    => 'POST',
     1175        'body'      => $data,
     1176        'timeout'   => 15,
     1177        'headers'   => array( 'Content-Type' => 'application/x-www-form-urlencoded' ),
     1178        'sslverify' => false,
     1179    );
     1180
     1181    $result = wp_remote_head(
     1182        iflychat_get_host( true ) . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/api/1.1/token/'
     1183        . $_SESSION['token'] . '/delete',
     1184        $options
     1185    );
     1186    session_unset();
     1187}
     1188
     1189/**
     1190 * Function to set cookie for user login.
     1191 *
     1192 * @since 4.7.0
     1193 */
     1194function iflychat_user_login() {
     1195    setcookie( 'iflychat_key', '', time() - 3600, '/' );
     1196    setcookie( 'iflychat_css', '', time() - 3600, '/' );
     1197    setcookie( 'iflychat_time', '', time() - 3600, '/' );
     1198}
     1199
     1200/**
     1201 * Function to set cookie and destroy token for user logout.
     1202 *
     1203 * @since 4.7.0
     1204 */
     1205function iflychat_user_logout() {
     1206    setcookie( 'iflychat_key', '', time() - 3600, '/' );
     1207    setcookie( 'iflychat_css', '', time() - 3600, '/' );
     1208    setcookie( 'iflychat_time', '', time() - 3600, '/' );
     1209    iflychat_token_destroy();
     1210}
     1211
     1212/**
     1213 * Function to get Inbox.
     1214 *
     1215 * @since 4.7.0
     1216 * @return string $output messages content as string.
     1217 */
     1218function iflychat_get_inbox() {
     1219    $data = array(
     1220        'uid'     => iflychat_get_user_id(),
     1221        'api_key' => iflychat_get_option( 'iflychat_api_key' ),
     1222    );
     1223
     1224    $options = array(
     1225        'method'    => 'POST',
     1226        'body'      => $data,
     1227        'timeout'   => 15,
     1228        'headers'   => array( 'Content-Type' => 'application/x-www-form-urlencoded' ),
     1229        'sslverify' => false,
     1230    );
     1231
     1232    $result = wp_remote_head(
     1233        iflychat_get_host( true ) . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/r/',
     1234        $options
     1235    );
     1236    $output = '';
     1237    if ( ! is_wp_error( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) {
     1238        $query          = json_decode( $result['body'] );
     1239        $timezone_offet = iflychat_get_option( 'gmt_offset' );
     1240        $date_format    = iflychat_get_option( 'date_format' );
     1241        $time_format    = iflychat_get_option( 'time_format' );
     1242        foreach ( $query as $record ) {
     1243            $rt      = $record->timestamp + ( $timezone_offet * 3600 );
     1244            $output .= '<div style="display:block;border-bottom: 1px solid #ccc; padding: 10px;"><div style="font-size:130%; display: inline;">' . $record->name . '</div><div style="float:right;color:#AAA; font-size: 70%;">' . date( "{$date_format} {$time_format}", $rt ) . '</div><div style="display: block; padding: 10px;">' . $record->message . '</div></div>'; // phpcs:ignore
     1245        }
     1246    }
     1247    return $output;
     1248}
     1249
     1250
     1251/**
     1252 * Function to get Message thread.
     1253 *
     1254 * @since 4.7.0
     1255 * @param Array $atts attributes as Array.
     1256 * @return string $output messages content as string.
     1257 */
     1258function iflychat_get_message_thread( $atts ) {
     1259    // phpcs:ignore
     1260    extract(
     1261        shortcode_atts(
     1262            array(
     1263                'room_id' => '0',
     1264                'id'      => '0',
     1265            ),
     1266            $atts
     1267        )
     1268    );
     1269
     1270    if ( isset( $room_id[0] ) && ( 'c' === $room_id[0] ) && isset( $room_id[1] ) && ( '-' === $room_id[1] ) ) {
     1271        $room_id = substr( $room_id, 2 );
     1272    } elseif ( '0' !== $id ) {
     1273        if ( ( 'c' === $id[0] ) && ( '-' === $id[1] ) ) {
     1274            $room_id = substr( $id, 2 );
     1275        } else {
     1276            $room_id = $id;
     1277        }
     1278    }
     1279
     1280    $data = array(
     1281        'uid1'    => iflychat_get_user_id(),
     1282        'uid2'    => ( 'c-' . $room_id ),
     1283        'api_key' => iflychat_get_option( 'iflychat_api_key' ),
     1284    );
     1285
     1286    $options = array(
     1287        'method'    => 'POST',
     1288        'body'      => $data,
     1289        'timeout'   => 15,
     1290        'headers'   => array( 'Content-Type' => 'application/x-www-form-urlencoded' ),
     1291        'sslverify' => false,
     1292    );
     1293
     1294    $result = wp_remote_head(
     1295        iflychat_get_host( true ) . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/q/',
     1296        $options
     1297    );
     1298
     1299    $output = '';
     1300
     1301    if ( ! is_wp_error( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) {
     1302        $query          = json_decode( $result['body'] );
     1303        $timezone_offet = iflychat_get_option( 'gmt_offset' );
     1304        $date_format    = iflychat_get_option( 'date_format' );
     1305        $time_format    = iflychat_get_option( 'time_format' );
     1306        foreach ( $query as $record ) {
     1307            $rt      = $record->timestamp + ( $timezone_offet * 3600 );
     1308            $output .= '<div style="display:block;border-bottom: 1px solid #ccc; padding: 1% 0% 1% 0%;"></div><div style="display:block; padding-top: 1%; padding-bottom: 0%"><div style="font-size:100%; display: inline;"><a href="#">' . $record->from_name . '</a></div><div style="float:right;font-size: 70%;">' . date( "{$date_format} {$time_format}", $rt ) . '</div><div style="display: block; padding-top: 1%; padding-bottom: 0%">' . $record->message . '</div></div>';// phpcs:ignore
     1309        }
     1310    }
     1311    return $output;
     1312}
     1313
     1314/**
     1315 * Function to get Embed code.
     1316 *
     1317 * @since 4.7.0
     1318 * @param Array $atts attributes as Array.
     1319 * @return string $output messages content as string.
     1320 */
     1321function iflychat_get_embed_code( $atts ) {
     1322
     1323    global $iflychat_engine;
     1324
     1325    extract( // phpcs:ignore
     1326        shortcode_atts(
     1327            array(
     1328                'room_id'         => '0',
     1329                'id'              => '0',
     1330                'hide_user_list'  => 'no',
     1331                'hide_popup_chat' => 'no',
     1332                'height'          => '550px',
     1333            ),
     1334            $atts
     1335        )
     1336    );
     1337
     1338    $output = '';
     1339
     1340    if ( $iflychat_engine ) {
     1341
     1342        $output = '<style>.drupalchat-embed-chatroom-content {height: ' . $height . ' !important;}';
     1343        if ( 'yes' === $hide_user_list ) {
     1344            $output .= '#drupalchat-embed-user-list {display:none !important;}.drupalchat-embed-chatroom-content {width:95% !important;}';
     1345        }
     1346        $output .= '</style>';
     1347        $output .= '<script type="text/javascript">if(typeof(iflyembed) === "undefined") {iflyembed = {};iflyembed.settings = {};iflyembed.settings.ifly = {};}iflyembed.settings.ifly.embed = "1";iflyembed.settings.ifly.ur_hy = "1";iflyembed.settings.ifly.embed_msg = "Type your message here. Press Enter to send.";iflyembed.settings.ifly.embed_online_user_text = "Online Users";</script>';
     1348        if ( isset( $room_id[0] ) && ( 'c' === $room_id[0] ) && isset( $room_id[1] ) && ( '-' === $room_id[1] ) ) {
     1349            $room_id = substr( $room_id, 2 );
     1350        } elseif ( '0' !== $id ) {
     1351            if ( ( 'c' === $id[0] ) && ( '-' === $id[1] ) ) {
     1352                $room_id = substr( $id, 2 );
     1353            } else {
     1354                $room_id = $id;
     1355            }
     1356        }
     1357        $output .= '<div id="drupalchat-embed-chatroom-' . $room_id . '" class="drupalchat-embed-chatroom-container';
     1358        if ( 'yes' === $hide_popup_chat ) {
     1359            $output .= ' drupalchat-hide-popup-chat';
     1360        }
     1361        $output .= '"></div>';
     1362    } elseif ( iflychat_check_chat_admin() ) {
     1363        $output .= '<div style="background-color:#eee;color:red;padding:5px;">iFlyChat is NOT set to load on this page. Please check the path visibility settings on iFlyChat plugin settings page. In case of any query, please create a support ticket <a href="https://iflychat.com/contact" target="_blank">here</a>. This error message is shown only to chat admins.</div>';
     1364    }
     1365    return $output;
     1366}
     1367
     1368
     1369/**
     1370 * Function to get Embed code.
     1371 *
     1372 * @since 4.7.0
     1373 * @return string $url URL as string.
     1374 */
     1375function iflychat_get_user_pic_url() {
     1376    $current_user = wp_get_current_user();
     1377    $url          = 'http://www.gravatar.com/avatar/' . ( ( $current_user->ID ) ? ( md5( strtolower( $current_user->user_email ) ) ) : ( '00000000000000000000000000000000' ) ) . '?d=mm&size=24';
     1378    $hook_url     = apply_filters( 'iflychat_get_user_avatar_url_filter', '', $current_user->ID );
     1379    if ( ! empty( $hook_url ) ) {
     1380        return $hook_url;
     1381    }
     1382
     1383    if ( function_exists( 'bp_core_fetch_avatar' ) && ( $current_user->ID > 0 ) ) {
     1384        $url = iflychat_get_avatar_url_from_html(
     1385            bp_core_fetch_avatar(
     1386                array(
     1387                    'item_id' => iflychat_get_user_id(),
     1388                    'html'    => false,
     1389                )
     1390            )
     1391        );
     1392
     1393    } elseif ( function_exists( 'user_avatar_fetch_avatar' ) && ( $current_user->ID > 0 ) ) {
     1394        $local_url = user_avatar_fetch_avatar(
     1395            array(
     1396                'html'    => false,
     1397                'item_id' => $current_user->ID,
     1398            ),
     1399        );
     1400
     1401        if ( $local_url ) {
     1402            $url = $local_url;
     1403        }
     1404    } elseif ( function_exists( 'userpro_profile_data' ) && ( $current_user->ID > 0 ) ) {
     1405        $user_id = $current_user->ID;
     1406        $url     = userpro_profile_data( 'profilepicture', $user_id );
     1407    } elseif ( function_exists( 'um_get_avatar_url' ) && ( $current_user->ID > 0 ) ) {
     1408        $user_id = $current_user->ID;
     1409        $url     = um_get_avatar_url( get_avatar( $user_id, $size = 96 ) );
     1410    } elseif ( function_exists( 'get_wp_user_avatar_src' ) && ( $current_user->ID > 0 ) ) {
     1411        $url = get_wp_user_avatar_src( iflychat_get_user_id() );
     1412    } elseif ( function_exists( 'get_simple_local_avatar' ) && ( $current_user->ID > 0 ) ) {
     1413        $source = get_simple_local_avatar( iflychat_get_user_id() );
     1414        $source = explode( 'src="', $source );
     1415        if ( isset( $source[1] ) ) {
     1416            $source = explode( '"', $source[1] );
     1417        } else {
     1418            $source = explode( "src='", $source[0] );
     1419            if ( isset( $source[1] ) ) {
     1420                $source = explode( "'", $source[1] );
     1421            } else {
     1422                $source[0] = 'http://www.gravatar.com/avatar/' . ( ( $current_user->ID ) ? ( md5( strtolower( $current_user->user_email ) ) ) : ( '00000000000000000000000000000000' ) ) . '?d=mm&size=24';
     1423            }
     1424        }
     1425        $url = $source[0];
     1426    } elseif ( $current_user->ID > 0 ) {
     1427        if ( false && function_exists( 'get_avatar_url' ) ) {
     1428            $url = get_avatar_url( iflychat_get_user_id() );
     1429        } else {
     1430            $url = iflychat_get_avatar_url_from_html( get_avatar( iflychat_get_user_id() ) );
     1431        }
     1432    }
     1433
     1434    $pos = strpos( $url, ':' );
     1435    if ( false !== $pos ) {
     1436        $url = substr( $url, $pos + 1 );
     1437    }
     1438    return $url;
     1439}
     1440
     1441
     1442/**
     1443 * Function to get user profile url.
     1444 *
     1445 * @since 4.7.0
     1446 * @return string url as string.
     1447 */
     1448function iflychat_get_user_profile_url() {
     1449    global $userpro;
     1450    $current_user = wp_get_current_user();
     1451
     1452    $upl      = 'javascript:void(0)';
     1453    $hook_upl = apply_filters( 'iflychat_get_user_profile_url_filter', 'javascript:void(0)', $current_user->ID );
     1454    if ( $hook_upl === $upl ) {
     1455        if ( function_exists( 'bp_core_get_userlink' ) && ( $current_user->ID > 0 ) ) {
     1456            $upl = bp_core_get_userlink( $current_user->ID, false, true );
     1457        } elseif ( function_exists( 'um_user_profile_url' ) && ( $current_user->ID > 0 ) ) {
     1458            $upl = um_user_profile_url( $current_user->ID, false, true );
     1459        } elseif ( ( $current_user->ID > 0 ) && ( null !== $userpro ) ) {
     1460            $upl = ( $userpro->permalink( $current_user->ID ) );
     1461        }
     1462        return $upl;
     1463    } else {
     1464        return $hook_upl;
     1465    }
     1466}
     1467
     1468/**
     1469 * Function to get option by name.
     1470 *
     1471 * @since 4.7.0
     1472 * @param string $name as option name string.
     1473 * @return string option value string.
     1474 */
     1475function iflychat_get_option( $name ) {
     1476    if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
     1477        return get_site_option( $name );
     1478    } else {
     1479        return get_option( $name );
     1480    }
     1481}
     1482
     1483/**
     1484 * Function to Add a new option.
     1485 *
     1486 * @since 4.7.0
     1487 * @param string  $name as option name string.
     1488 * @param string  $value as option value string.
     1489 * @param string  $v2 as string (Optional) Description. Not used anymore default ''.
     1490 * @param Boolean $v3 as true false.
     1491 * @return Boolean option insert Boolean.
     1492 */
     1493function iflychat_add_option( $name, $value, $v2, $v3 ) {
     1494    if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
     1495        return add_site_option( $name, $value, $v2, $v3 );
     1496    } else {
     1497        return add_option( $name, $value, '', $v3 );
     1498    }
     1499}
     1500
     1501/**
     1502 * Function to update a option.
     1503 *
     1504 * @since 4.7.0
     1505 * @param string $name as option name string.
     1506 * @param string $value as option value string.
     1507 * @return Boolean option update Boolean.
     1508 */
     1509function iflychat_update_option( $name, $value ) {
     1510    if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
     1511        return update_site_option( $name, $value );
     1512    } else {
     1513        return update_option( $name, $value );
     1514    }
     1515}
     1516
     1517/**
     1518 * Function to check user access.
     1519 *
     1520 * @since 4.7.0
     1521 * @return Boolean Access true or false Boolean.
     1522 */
     1523function iflychat_check_access() {
     1524    global $current_user;
     1525    $flag = apply_filters( 'iflychat_check_access_filter', true, $current_user->ID );
     1526    if ( true === $flag ) {
     1527        return true;
     1528    } else {
     1529        return false;
     1530    }
     1531    exit;
     1532}
     1533
     1534/**
     1535 * Function to get avtar url from html.
     1536 *
     1537 * @since 4.7.0
     1538 * @param String $source html as String.
     1539 * @return String avtar url as String.
     1540 */
     1541function iflychat_get_avatar_url_from_html( $source ) {
     1542    $source = explode( 'src="', $source );
     1543    if ( isset( $source[1] ) ) {
     1544        $source = explode( '"', $source[1] );
     1545    } else {
     1546        $source = explode( "src='", $source[0] );
     1547        if ( isset( $source[1] ) ) {
     1548            $source = explode( "'", $source[1] );
     1549        }
     1550    }
     1551    return $source[0];
     1552}
     1553
     1554/**
     1555 * Function to return host name (url) .
     1556 *
     1557 * @since 4.7.0
     1558 * @param Boolean $https is tru or false as boolean.
     1559 * @return String url as String.
     1560 */
     1561function iflychat_get_host( $https = false ) {
     1562    if ( '1' === iflychat_get_option( 'iflychat_show_admin_list' ) ) {
     1563        if ( $https ) {
     1564            return 'https://support1.iflychat.com';
     1565        } else {
     1566            return 'http://support1.iflychat.com';
     1567        }
     1568    } else {
     1569        if ( $https ) {
     1570            return DRUPALCHAT_EXTERNAL_A_HOST;
     1571        } else {
     1572            return DRUPALCHAT_EXTERNAL_HOST;
     1573        }
     1574    }
     1575}
     1576
     1577/**
     1578 * Function to get string.
     1579 *
     1580 * @since 4.7.0
     1581 * @param String $words as String.
     1582 * @return String $final word as String.
     1583 */
     1584function iflychat_process_stop_word_list( $words ) {
     1585    $new_arr = array_map( 'trim', explode( ',', $words ) );
     1586    $final   = implode( ',', $new_arr );
     1587    return $final;
     1588}
     1589
     1590/**
     1591 * Function to check chat camp.
     1592 *
     1593 * @since 4.7.0
     1594 * @return Boolean true or false as Boolean.
     1595 */
    12301596function iflychat_chatcamp_check() {
    1231     // return true;
    1232     $app_id = iflychat_get_option('iflychat_app_id');
    1233     if(!empty($app_id)) {
    1234         if(strpos($app_id, '-') === false && strlen($app_id) == 19) {
    1235             return true;
    1236         }
    1237     }
    1238     return false;
    1239 }
    1240 
    1241 function iflychat_chatcamp_process_user_data($data) {
    1242     $data['x-api-key'] = $data['api_key'];
    1243     $data['id'] = $data['user_id'];
    1244     $data['display_name'] = $data['user_name'];
    1245     $data['avatar_url'] = $data['user_avatar_url'];
    1246     $data['profile_url'] = $data['user_profile_url'];
    1247     $data['check_access_token'] = true;
    1248     $data['access_token'] = iflychat_get_hash_session();
    1249     $data['metadata'] = array("cc_user_roles" => json_encode($data['user_roles']));
    1250     return $data;
    1251 }
    1252 
    1253 function iflychat_chatcamp_user_should_update($new, $old) {
    1254     $update = false;
    1255     if($new['user_name'] != $old->display_name){
    1256         $update = true;
    1257     }
    1258 
    1259     if($new['user_avatar_url'] != $old->avatar_url){
    1260         $update = true;
    1261     }
    1262 
    1263     if($new['user_profile_url'] != $old->profile_url){
    1264         $update = true;
    1265     }
    1266 
    1267     if(json_encode($new['user_roles']) !== $old->metadata->cc_user_roles){
    1268         $update = true;
    1269     }
    1270    
    1271     return $update;
     1597    $app_id = iflychat_get_option( 'iflychat_app_id' );
     1598    if ( ! empty( $app_id ) ) {
     1599        if ( false === strpos( $app_id, '-' ) && 19 === strlen( $app_id ) ) {
     1600            return true;
     1601        }
     1602    }
     1603    return false;
     1604}
     1605
     1606/**
     1607 * Function to add chatcamp userdata.
     1608 *
     1609 * @since 4.7.0
     1610 * @param Array $data users data as Array.
     1611 * @return Array $data users data as Array.
     1612 */
     1613function iflychat_chatcamp_process_user_data( $data ) {
     1614    $data['x-api-key']          = $data['api_key'];
     1615    $data['id']                 = $data['user_id'];
     1616    $data['display_name']       = $data['user_name'];
     1617    $data['avatar_url']         = $data['user_avatar_url'];
     1618    $data['profile_url']        = $data['user_profile_url'];
     1619    $data['check_access_token'] = true;
     1620    $data['access_token']       = iflychat_get_hash_session();
     1621    $data['metadata']           = array( 'cc_user_roles' => wp_json_encode( $data['user_roles'] ) );
     1622    return $data;
     1623}
     1624
     1625/**
     1626 * Function to check user update or not.
     1627 *
     1628 * @since 4.7.0
     1629 * @param Array  $new users data as Array.
     1630 * @param Object $old users data as Object.
     1631 * @return Boolean $update true or false as Boolean.
     1632 */
     1633function iflychat_chatcamp_user_should_update( $new, $old ) {
     1634    $update = false;
     1635    if ( isset( $new['user_name'] ) && $new['user_name'] !== $old->display_name ) {
     1636        $update = true;
     1637    }
     1638
     1639    if ( isset( $new['user_avatar_url'] ) && $new['user_avatar_url'] !== $old->avatar_url ) {
     1640        $update = true;
     1641    }
     1642
     1643    if ( isset( $new['user_profile_url'] ) && $new['user_profile_url'] !== $old->profile_url ) {
     1644        $update = true;
     1645    }
     1646
     1647    if ( isset( $new['user_roles'] ) && wp_json_encode( $new['user_roles'] ) !== $old->metadata->cc_user_roles ) {
     1648        $update = true;
     1649    }
     1650    return $update;
    12721651}
    12731652
  • iflychat/trunk/readme.txt

    r2286536 r2628223  
    44Requires at least: 3.0
    55Tested up to: 5.3
    6 Stable tag: 4.6.4
     6Stable tag: 4.7.0
    77Tags: buddypress, chat, chat room, community, embed chat, friends, group chat, html5 chat, one to one chat, live chat, popup chat, mobile chat, multisite, wordpress chat, wp chat, ultimate member
    88License: GPLv2 or later
     
    138138== Changelog ==
    139139
     140= 4.7.0 =
     141
     14211/11/2021: iFlyChat new backend update.
     143
    140144= 4.6.4 =
    141145
     
    449453== Upgrade Notice ==
    450454
     455= 4.7.0 =
     456After updating, go to iFlyChat Settings page and click on Update button.
     457
    451458= 4.6.4 =
    452459After updating, go to iFlyChat Settings page and click on Update button.
Note: See TracChangeset for help on using the changeset viewer.