/
live-chat-suite.php
165 lines (120 loc) · 4.69 KB
/
live-chat-suite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Plugin Name: Live Chat Suite
* Description: Live Chat For Wordpress!
* Version: 1.0.0
* Author: livechatsuite
* Author URI: https://www.livechatsuite.com
* Text Domain: livechatsuite
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* License: GPL-2.0+
*/
if(!defined('LIVECHATSUITE_PLUGIN_VERSION'))
define('LIVECHATSUITE_PLUGIN_VERSION', '1.0.0');
if(!defined('LIVECHATSUITE_URL'))
define('LIVECHATSUITE_URL', plugin_dir_url( __FILE__ ));
if(!defined('LIVECHATSUITE_PATH'))
define('LIVECHATSUITE_PATH', plugin_dir_path( __FILE__ ));
class Livechatsuite
{
/**
*
* @var string
*/
private $_nonce = 'livechatsuite_admin';
/**
*
* @var string
*/
private $option_name = 'livechatsuite_data';
public function __construct()
{
add_action('wp_footer', array($this,'addFooterCode'));
add_action('admin_menu', array($this,'addAdminMenu'));
add_action('wp_ajax_store_admin_data', array($this,'storeAdminData'));
add_action('admin_enqueue_scripts', array($this,'addAdminScripts'));
}
/**
* Returns the saved options data as an array
*
* @return array
*/
private function getData()
{
return get_option($this->option_name, array());
}
/**
* Callback for the Ajax request
*
* Updates the options data
*
* @return void
*/
public function storeAdminData()
{
if (wp_verify_nonce($_POST['_nonce'], $this->_nonce ) === false)
die('oops, Something went wrong, please try again!');
$data = $this->getData();
update_option($this->option_name, $_POST['lcs_site_id']);
echo __('Saved Site ID! Navigate to a wordpress page to see your chat widget.', 'livechatsuite');
die();
}
/**
* Adds Admin Scripts for the Ajax call
*/
public function addAdminScripts()
{
wp_enqueue_style('livechatsuite-admin', LIVECHATSUITE_URL. 'assets/css/admin.css', false, 1.0);
wp_enqueue_script('livechatsuite-admin', LIVECHATSUITE_URL. 'assets/js/admin.js', array(), 1.0);
$admin_options = array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'_nonce' => wp_create_nonce( $this->_nonce ),
);
wp_localize_script('livechatsuite-admin', 'livechatsuite_exchanger', $admin_options);
}
public function addAdminMenu()
{
add_menu_page( __( 'Live Chat Suite', 'livechatsuite' ), __( 'Live Chat Suite', 'livechatsuite' ),
'manage_options','livechatsuite',array($this, 'adminLayout'),
'dashicons-format-status'
);
}
public function adminLayout()
{
?>
<div class="wrap">
<form id="lcs_add_siteid" class="postbox">
<div class="form-group inside">
<h1><?php _e('LiveChat Suite - Give Your Users The Live Chat Experience They Deserve!', 'livechatsuite'); ?></h1>
<?php $this->addFooterCode(true); ?>
<h3><?php _e('Setup LiveChat Suite', 'livechatsuite'); ?></h3>
<?php _e('Congrats! Set up is quick and easy, simple enter your Site ID in the box below and your ready to go!', 'livechatsuite'); ?>
<br> <br>
<br>
<?php _e('Don`t have a Site ID yet? Create an account and <a href="https://www.livechatsuite.com/start" target="_blank">get one here</a> - It only takes 1 minute!', 'livechatsuite'); ?>
<br>
<?php _e('Already have an account? Login and customize your widget <a href="https://www.livechatsuite.com/login" target="_blank">here</a>', 'livechatsuite'); ?>
<table class="form-table">
<tbody><tr><td scope="row">
<label><?php _e( 'Your Site ID:', 'livechatsuite' ); ?></label></td><td>
<input name="lcs_site_id"id="lcs_site_id" class="regular-text"type="text"value="<?php echo get_option('livechatsuite_data');?>"/>
</td></tr></tbody></table> </div>
<div class="inside"><button class="button button-primary" id="save_lcs_id" type="submit">
<?php _e( 'Save', 'livechatsuite' ); ?></button></div></form></div>
<?php
}
/**
* @param $force boolean
* @return void
*/
public function addFooterCode($force = false)
{
$lcs_id=get_option('livechatsuite_data'); if(!empty($lcs_id)){
?>
<!--Start Live Chat Suite Code-->
<script type="text/javascript">(function() {var s = document.createElement('script');s.type = 'text/javascript';s.async = true;s.src = 'https://chat.livechatsuite.com/clientloader.js';var x = document.getElementsByTagName('script')[0];x.parentNode.insertBefore(s, x);})();lcs_data = ({uid: '<?php echo $lcs_id ?>'});</script>
<!--End Live Chat Suite Analytics Code-->
<?php }
}
}
new Livechatsuite();