Plugin Directory

Changeset 2011966


Ignore:
Timestamp:
01/14/2019 11:35:39 AM (6 years ago)
Author:
lucastidio
Message:

Async widget load, code cleanup

Location:
tidio-live-chat/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tidio-live-chat/trunk/readme.txt

    r1987129 r2011966  
    1111
    1212== Description ==
    13                        
     13
    1414Tidio Live Chat is a live chat service which allows you to communicate with your customers easily, also with the help of chatbots. It is designed specifically for the WordPress community. Once you install the plugin, you will be ready to take full advantage of the benefits our service provides. **A free plan is available!**
    1515
     
    103103
    104104= Can I install TidioChat on my plain html site? =
    105 Yes you can 
     105Yes you can
    106106
    107107= Is it possible to initiate a conversation before the visitor does? =
     
    122122== Changelog ==
    123123
     124= 3.4.0 =
     125* Add asynchronous loading of chat widget option
     126* Code cleanup
     127
    124128= 3.3.2 =
    125129* Change of the widget script placement in Wordpress themes to improve the rendering performance.
    126130* Use admin email as a default email for a Tidio account.
    127131
     132= 3.3.2 =
     133* Change of the widget script placement in Wordpress themes to improve the rendering performance.
     134* Use admin email as a default email for a Tidio account.
     135
    128136= 3.3.1 =
    129137* Refreshed texts within the plugin to be more intuitive.
     
    142150* Cleanup with changes after last modifications.
    143151
    144 = 3.0.1 = 
     152= 3.0.1 =
    145153* (added) curl support
    146154
    147 = 3.0 = 
     155= 3.0 =
    148156* (added) new design
    149157* (added) integration with tidio chat account
    150158
    151 = 2.1.2 = 
     159= 2.1.2 =
    152160* (added) compatibility with other tidio plugins
    153161
    154 = 2.1 = 
     162= 2.1 =
    155163* (added) compatibility with tidio one api
    156164
    157 = 2.0.5 = 
     165= 2.0.5 =
    158166* (added) compatibility with tidio one
    159167
    160168= 2.0.2 =
    161 * (added) support to sites with disabled allow_url_fopen flag 
     169* (added) support to sites with disabled allow_url_fopen flag
    162170
    163171= 2.0.1 =
  • tidio-live-chat/trunk/tidio-elements.php

    r1929077 r2011966  
    55 * Plugin URI: http://www.tidiochat.com
    66 * Description: Tidio Live Chat - Live chat for your website. No logging in, no signing up - integrates with your website in less than 20 seconds.
    7  * Version: 3.3.3
     7 * Version: 3.4.0
    88 * Author: Tidio Ltd.
    99 * Author URI: http://www.tidiochat.com
    1010 * License: GPL2
    1111 */
    12 define('TIDIOCHAT_VERSION', '3.3.3');
    13 
    14 class TidioLiveChat {
    15 
    16     private $scriptUrl = '//code.tidio.co/';
    17     private static $apiUrl = 'https://api-v2.tidio.co';
    18     private static $chatUrl = 'https://www.tidiochat.com';
    19 
    20     public function __construct() {
    21 
     12define('TIDIOCHAT_VERSION', '3.4.0');
     13
     14class TidioLiveChat
     15{
     16    const SCRIPT_URL = '//code.tidio.co/';
     17    const API_URL = 'https://api-v2.tidio.co';
     18    const CHAT_URL = 'https://www.tidiochat.com';
     19    const PUBLIC_KEY_OPTION = 'tidio-one-public-key';
     20    const PRIVATE_KEY_OPTION = 'tidio-one-private-key';
     21    const ASYNC_LOAD_OPTION = 'tidio-async-load';
     22    const CLEAR_ACCOUNT_DATA_ACTION = 'tidio-chat-reset';
     23    const TIDIO_PLUGIN_NAME = 'tidio-live-chat';
     24    const TOGGLE_ASYNC_ACTION = 'tidio-chat-toggle-async';
     25
     26    public function __construct()
     27    {
    2228        if (!empty($_GET['tidio_chat_version'])) {
    2329            echo TIDIOCHAT_VERSION;
     
    2632
    2733        /* Before add link to menu - check is user trying to unninstal */
    28         if (is_admin() && !empty($_GET['tidio_one_clear_cache'])) {
    29             delete_option('tidio-one-public-key');
    30             delete_option('tidio-one-private-key');
    31         }
    32 
    33         add_action('admin_menu', array($this, 'addAdminMenuLink'));
    34 
    35         if(get_option('tidio-one-public-key')){
     34        if (is_admin() && current_user_can('activate_plugins') && !empty($_GET['tidio_one_clear_cache'])) {
     35            delete_option(TidioLiveChat::PUBLIC_KEY_OPTION);
     36            delete_option(TidioLiveChat::PRIVATE_KEY_OPTION);
     37        }
     38
     39        if (get_option(TidioLiveChat::PUBLIC_KEY_OPTION)) {
    3640            add_action('admin_footer', array($this, 'adminJS'));
    3741        }
    3842
    3943        if (!is_admin()) {
    40             add_action('wp_enqueue_scripts', array($this, 'enqueueScripts'), 1000);
    41         }else{
     44            if (get_option(TidioLiveChat::ASYNC_LOAD_OPTION)) {
     45                add_action('wp_footer', array($this, 'enqueueScriptsAsync'), PHP_INT_MAX);
     46            } else {
     47                add_action('wp_enqueue_scripts', array($this, 'enqueueScriptsSync'), 1000);
     48            }
     49        } else if (current_user_can('activate_plugins')) {
     50            add_action('admin_menu', array($this, 'addAdminMenuLink'));
    4251            add_action('admin_enqueue_scripts', array($this, 'enqueueAdminScripts'));
    43         }
    44 
    45         add_action('wp_ajax_tidio_chat_save_keys', array($this, 'ajaxTidioChatSaveKeys'));
    46 
    47         /* Ajax functions to set up existing tidio account  */
    48         add_action('wp_ajax_get_project_keys', array($this, 'ajaxGetProjectKeys'));
    49         add_action('wp_ajax_get_private_key', array($this, 'ajaxGetPrivateKey'));
    50 
    51         add_filter('plugin_action_links', array($this, 'pluginActionLinks'), 10, 2);
    52         add_action('admin_post_tidio-chat-reset', array($this, 'uninstall'));
    53     }
    54 
    55     public function pluginActionLinks($links, $file) {
    56         if (strpos($file, 'tidio-elements.php') !== false && get_option('tidio-one-private-key')) {
    57             $links[] = '<a href="'.admin_url('admin-post.php').'?action=tidio-chat-reset">'.esc_html__( 'Clear Account Data' , 'tidio-live-chat').'</a>';
    58         }
    59 
    60         return $links;
    61     }
    62 
    63     public function ajaxGetProjectKeys(){
    64         update_option('tidio-one-public-key', $_POST['public_key']);
    65         update_option('tidio-one-private-key', $_POST['private_key']);
    66         echo self::getRedirectUrl($_POST['private_key']);
     52
     53            add_action('wp_ajax_tidio_chat_save_keys', array($this, 'ajaxTidioChatSaveKeys'));
     54            add_action('wp_ajax_get_project_keys', array($this, 'ajaxGetProjectKeys'));
     55            add_action('wp_ajax_get_private_key', array($this, 'ajaxGetPrivateKey'));
     56
     57            add_action('admin_post_' . TidioLiveChat::CLEAR_ACCOUNT_DATA_ACTION . '', array($this, 'uninstall'));
     58            add_action('admin_post_' . TidioLiveChat::TOGGLE_ASYNC_ACTION . '', array($this, 'toggleAsync'));
     59
     60            add_filter('plugin_action_links', array($this, 'pluginActionLinks'), 10, 2);
     61        }
     62    }
     63
     64    public static function activate()
     65    {
     66        update_option(TidioLiveChat::ASYNC_LOAD_OPTION, true);
     67    }
     68
     69    public static function ajaxGetPrivateKey()
     70    {
     71        $privateKey = TidioLiveChat::getPrivateKey();
     72        if (!$privateKey || $privateKey == 'false') {
     73            echo 'error';
     74            exit();
     75        }
     76        echo TidioLiveChat::getRedirectUrl($privateKey);
    6777        exit();
    6878    }
    6979
    70     // Ajax - Create an new project
    71 
    72     public function ajaxTidioChatSaveKeys() {
    73 
    74         if (!is_admin()) {
    75             exit;
    76         }
    77 
    78         if (empty($_POST['private_key']) || empty($_POST['public_key'])) {
    79             exit;
    80         }
    81 
    82         update_option('tidio-one-public-key', $_POST['public_key']);
    83         update_option('tidio-one-private-key', $_POST['private_key']);
    84 
    85         echo '1';
    86         exit;
    87     }
    88 
    89     // Front End Scripts
    90     public function enqueueScripts() {
    91         wp_enqueue_script('tidio-chat', $this->scriptUrl . self::getPublicKey() . '.js', array(), TIDIOCHAT_VERSION, true);
    92     }
    93 
    94     // Admin scripts and style enquee
    95     public function enqueueAdminScripts(){
    96         wp_enqueue_script('tidio-chat-admin', plugins_url('media/js/options.js', __FILE__), array(), TIDIOCHAT_VERSION, true);
    97         wp_enqueue_style('tidio-chat-admin-style', plugins_url('media/css/options.css', __FILE__), array(), TIDIOCHAT_VERSION);
    98     }
    99 
    100     // Admin JavaScript
    101     public function adminJS() {
    102         $privateKey = self::getPrivateKey();
    103         $redirectUrl = '';
    104 
    105         if ($privateKey && $privateKey != 'false') {
    106             $redirectUrl = self::getRedirectUrl($privateKey);
    107         } else {
    108             $redirectUrl = admin_url('admin-ajax.php?action=tidio_chat_redirect');
    109         }
    110 
    111         echo "<script>jQuery('a[href=\"admin.php?page=tidio-chat\"]').attr('href', '" . $redirectUrl . "').attr('target', '_blank') </script>";
    112     }
    113 
    114     // Menu Pages
    115 
    116     public function addAdminMenuLink() {
    117         $optionPage = add_menu_page(
    118             'Tidio Chat', 'Tidio Chat', 'manage_options', 'tidio-chat', array($this, 'addAdminPage'), content_url() . '/plugins/tidio-live-chat/media/img/icon.png'
    119         );
    120     }
    121 
    122     public function addAdminPage() {
    123         // Set class property
    124         $dir = plugin_dir_path(__FILE__);
    125         include $dir . 'options.php';
    126     }
    127 
    128     // Uninstall
    129 
    130     public function uninstall() {
    131         delete_option('tidio-one-public-key');
    132         delete_option('tidio-one-private-key');
    133         wp_redirect( admin_url('plugins.php') );
    134         die();
    135     }
    136 
    137     // Get Private Key
    138 
    139     public static function getPrivateKey() {
    140         self::syncPrivateKey();
    141 
    142         $privateKey = get_option('tidio-one-private-key');
     80    public static function getPrivateKey()
     81    {
     82        TidioLiveChat::syncPrivateKey();
     83
     84        $privateKey = get_option(TidioLiveChat::PRIVATE_KEY_OPTION);
    14385
    14486        if ($privateKey) {
     
    14789
    14890        try {
    149             $data = self::getContent(self::getAccessUrl());
    150         } catch(Exception $e){
     91            $data = TidioLiveChat::getContent(TidioLiveChat::getAccessUrl());
     92        } catch (Exception $e) {
    15193            $data = null;
    15294        }
    15395        //
    15496        if (!$data) {
    155             update_option('tidio-one-private-key', 'false');
     97            update_option(TidioLiveChat::PRIVATE_KEY_OPTION, 'false');
    15698            return false;
    15799        }
     
    159101        @$data = json_decode($data, true);
    160102        if (!$data || !$data['status']) {
    161             update_option('tidio-one-private-key', 'false');
     103            update_option(TidioLiveChat::PRIVATE_KEY_OPTION, 'false');
    162104            return false;
    163105        }
    164106
    165         update_option('tidio-one-private-key', $data['value']['private_key']);
    166         update_option('tidio-one-public-key', $data['value']['public_key']);
     107        update_option(TidioLiveChat::PRIVATE_KEY_OPTION, $data['value']['private_key']);
     108        update_option(TidioLiveChat::PUBLIC_KEY_OPTION, $data['value']['public_key']);
     109        update_option(TidioLiveChat::ASYNC_LOAD_OPTION, true);
    167110
    168111        return $data['value']['private_key'];
    169112    }
    170113
    171     public static function getContent($url){
    172 
    173         if(function_exists('curl_version')){ // load trought curl
     114    public static function syncPrivateKey()
     115    {
     116        if (get_option(TidioLiveChat::PUBLIC_KEY_OPTION)) {
     117            return false;
     118        }
     119
     120        $publicKey = get_option('tidio-chat-external-public-key');
     121        $privateKey = get_option('tidio-chat-external-private-key');
     122
     123        if (!$publicKey || !$privateKey) {
     124            return false;
     125        }
     126
     127        // sync old variables with new one
     128
     129        update_option(TidioLiveChat::PUBLIC_KEY_OPTION, $publicKey);
     130        update_option(TidioLiveChat::PRIVATE_KEY_OPTION, $privateKey);
     131
     132        return true;
     133    }
     134
     135    public static function getContent($url)
     136    {
     137
     138        if (function_exists('curl_version')) { // load trought curl
    174139            $ch = curl_init();
    175140
     
    185150            return file_get_contents($url);
    186151        }
    187 
    188     }
    189 
    190     // Sync private key with old version
    191 
    192     public static function syncPrivateKey() {
    193         if (get_option('tidio-one-public-key')) {
    194             return false;
    195         }
    196 
    197         $publicKey = get_option('tidio-chat-external-public-key');
    198         $privateKey = get_option('tidio-chat-external-private-key');
    199 
    200         if (!$publicKey || !$privateKey) {
    201             return false;
    202         }
    203 
    204         // sync old variables with new one
    205 
    206         update_option('tidio-one-public-key', $publicKey);
    207         update_option('tidio-one-private-key', $privateKey);
    208 
    209         return true;
    210     }
    211 
    212     // Get Access Url
    213 
    214     public static function getAccessUrl() {
    215         return self::$apiUrl . '/access/external/create?url=' . urlencode(site_url()) . '&platform=wordpress&email=' . urlencode(get_option('admin_email')) . '&_ip=' . $_SERVER['REMOTE_ADDR'];
    216     }
    217 
    218     public static function getRedirectUrl($privateKey) {
    219         return self::$chatUrl . '/access?' . http_build_query(
     152    }
     153
     154    public static function getAccessUrl()
     155    {
     156        return TidioLiveChat::API_URL . '/access/external/create?url=' . urlencode(site_url()) . '&platform=wordpress&email=' . urlencode(get_option('admin_email')) . '&_ip=' . $_SERVER['REMOTE_ADDR'];
     157    }
     158
     159    public static function getRedirectUrl($privateKey)
     160    {
     161        return TidioLiveChat::CHAT_URL . '/access?' . http_build_query(
    220162                array(
    221163                    'privateKey' => $privateKey,
     
    227169    }
    228170
    229     public static function ajaxGetPrivateKey(){
    230         $privateKey = self::getPrivateKey();
    231         if(!$privateKey || $privateKey=='false'){
    232             echo 'error';
    233             exit();
    234         }
    235         echo self::getRedirectUrl($privateKey);
     171    public function pluginActionLinks($links, $file)
     172    {
     173        if (strpos($file, basename(__FILE__)) !== false) {
     174            if (get_option(TidioLiveChat::PRIVATE_KEY_OPTION)) {
     175                $links[] = '<a href="' . admin_url('admin-post.php') . '?action=' . TidioLiveChat::CLEAR_ACCOUNT_DATA_ACTION . '">' . esc_html__('Clear Account Data',
     176                        TidioLiveChat::TIDIO_PLUGIN_NAME) . '</a>';
     177                if (get_option(TidioLiveChat::ASYNC_LOAD_OPTION)) {
     178                    $toggleAsyncLabel = '✓';
     179                    $onclickPart = 'onclick="return confirm(\'Disabling asynchronous loading of the chat widget may affect the page loading time of your website. Are you sure you want to disable the asynchronous loading?\');"';
     180                } else {
     181                    $toggleAsyncLabel = '✘';
     182                    $onclickPart = '';
     183                }
     184                $links[] = '<a href="' . admin_url('admin-post.php') . '?action=' . TidioLiveChat::TOGGLE_ASYNC_ACTION . '" ' . $onclickPart . '>' . esc_html__($toggleAsyncLabel . ' Asynchronous loading',
     185                        TidioLiveChat::TIDIO_PLUGIN_NAME) . '</a>';
     186            }
     187        }
     188
     189        return $links;
     190    }
     191
     192    public function toggleAsync()
     193    {
     194        update_option(TidioLiveChat::ASYNC_LOAD_OPTION, !get_option(TidioLiveChat::ASYNC_LOAD_OPTION));
     195        wp_redirect(admin_url('plugins.php'));
     196        die();
     197    }
     198
     199    public function ajaxGetProjectKeys()
     200    {
     201        update_option(TidioLiveChat::PUBLIC_KEY_OPTION, $_POST['public_key']);
     202        update_option(TidioLiveChat::PRIVATE_KEY_OPTION, $_POST['private_key']);
     203        echo TidioLiveChat::getRedirectUrl($_POST['private_key']);
    236204        exit();
    237205    }
    238206
    239     // Get Public Key
    240 
    241     public static function getPublicKey() {
    242         $publicKey = get_option('tidio-one-public-key');
     207    public function ajaxTidioChatSaveKeys()
     208    {
     209        if (!is_admin()) {
     210            exit;
     211        }
     212
     213        if (empty($_POST['private_key']) || empty($_POST['public_key'])) {
     214            exit;
     215        }
     216
     217        update_option(TidioLiveChat::PUBLIC_KEY_OPTION, $_POST['public_key']);
     218        update_option(TidioLiveChat::PRIVATE_KEY_OPTION, $_POST['private_key']);
     219
     220        echo '1';
     221        exit;
     222    }
     223
     224    public function enqueueScriptsAsync()
     225    {
     226        $publicKey = TidioLiveChat::getPublicKey();
     227        $widgetUrl = TidioLiveChat::SCRIPT_URL . $publicKey . '.js';
     228        $asyncScript = <<<SRC
     229<script type='text/javascript'>
     230document.tidioChatCode = "$publicKey";
     231(function() {
     232  function asyncLoad() {
     233    var tidioScript = document.createElement("script");
     234    tidioScript.type = "text/javascript";
     235    tidioScript.async = true;
     236    tidioScript.src = "{$widgetUrl}";
     237    document.body.appendChild(tidioScript);
     238  }
     239  if (window.attachEvent) {
     240    window.attachEvent("onload", asyncLoad);
     241  } else {
     242    window.addEventListener("load", asyncLoad, false);
     243  }
     244})();
     245</script>
     246SRC;
     247        echo $asyncScript;
     248    }
     249
     250    public static function getPublicKey()
     251    {
     252        $publicKey = get_option(TidioLiveChat::PUBLIC_KEY_OPTION);
    243253
    244254        if ($publicKey) {
     
    246256        }
    247257
    248         self::getPrivateKey();
    249 
    250         return get_option('tidio-one-public-key');
    251     }
    252 
     258        TidioLiveChat::getPrivateKey();
     259
     260        return get_option(TidioLiveChat::PUBLIC_KEY_OPTION);
     261    }
     262
     263    public function enqueueScriptsSync()
     264    {
     265        wp_enqueue_script('tidio-chat', TidioLiveChat::SCRIPT_URL . TidioLiveChat::getPublicKey() . '.js', array(),
     266            TIDIOCHAT_VERSION,
     267            true);
     268        wp_add_inline_script('tidio-chat', 'document.tidioChatCode = "' . TidioLiveChat::getPublicKey() . '";',
     269            'before');
     270    }
     271
     272    public function enqueueAdminScripts()
     273    {
     274        wp_enqueue_script('tidio-chat-admin', plugins_url('media/js/options.js', __FILE__), array(), TIDIOCHAT_VERSION,
     275            true);
     276        wp_enqueue_style('tidio-chat-admin-style', plugins_url('media/css/options.css', __FILE__), array(),
     277            TIDIOCHAT_VERSION);
     278    }
     279
     280    public function adminJS()
     281    {
     282        $privateKey = TidioLiveChat::getPrivateKey();
     283        $redirectUrl = '';
     284
     285        if ($privateKey && $privateKey != 'false') {
     286            $redirectUrl = TidioLiveChat::getRedirectUrl($privateKey);
     287        } else {
     288            $redirectUrl = admin_url('admin-ajax.php?action=tidio_chat_redirect');
     289        }
     290
     291        echo "<script>jQuery('a[href=\"admin.php?page=tidio-chat\"]').attr('href', '" . $redirectUrl . "').attr('target', '_blank') </script>";
     292    }
     293
     294    public function addAdminMenuLink()
     295    {
     296        add_menu_page(
     297            'Tidio Chat', 'Tidio Chat', 'manage_options', 'tidio-chat', array($this, 'addAdminPage'),
     298            content_url() . '/plugins/' . TidioLiveChat::TIDIO_PLUGIN_NAME . '/media/img/icon.png'
     299        );
     300    }
     301
     302    public function addAdminPage()
     303    {
     304        // Set class property
     305        $dir = plugin_dir_path(__FILE__);
     306        include $dir . 'options.php';
     307    }
     308
     309    public function uninstall()
     310    {
     311        delete_option(TidioLiveChat::PUBLIC_KEY_OPTION);
     312        delete_option(TidioLiveChat::PRIVATE_KEY_OPTION);
     313        delete_option(TidioLiveChat::ASYNC_LOAD_OPTION);
     314        wp_redirect(admin_url('plugins.php'));
     315        die();
     316    }
    253317}
    254318
    255 $tidioLiveChat = new TidioLiveChat();
    256 
     319add_action('init', 'initialize_tidio');
     320
     321function initialize_tidio()
     322{
     323    $tidioLiveChat = new TidioLiveChat();
     324}
     325
     326register_activation_hook(__FILE__, array('TidioLiveChat', 'activate'));
Note: See TracChangeset for help on using the changeset viewer.