-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrisp.php
213 lines (167 loc) · 6.22 KB
/
crisp.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* @package Crisp
* @version 0.21
* Plugin Name: Crisp
* Plugin URI: http://wordpress.org/plugins/crisp/
* Description: Crisp is a Livechat plugin
* Author: Crisp IM
* Version: 0.21
* Author URI: https://crisp.chat
*
* Text Domain: crisp
* Domain Path: /languages/
*/
add_action('admin_menu', 'crisp_create_menu');
function crisp_create_menu() {
add_menu_page(__('Crisp Settings', 'crisp'), __('Crisp Settings', 'crisp'), 'administrator', __FILE__, 'crisp_plugin_settings_page' , 'https://crisp.chat/favicon.png');
add_action('admin_init', 'register_crisp_plugin_settings' );
add_action('admin_init', 'register_crisp_plugin_onboarding');
}
function register_crisp_plugin_onboarding() {
$onboarding = get_option('crisp_onboarding');
$website_id = get_option('website_id');
if (empty($website_id) && (empty($onboarding) || !$onboarding)) {
update_option("crisp_onboarding", true);
wp_redirect(admin_url('admin.php?page='.plugin_basename(__FILE__)));
}
}
function register_crisp_plugin_settings() {
register_setting( 'crisp-plugin-settings-group', 'website_id' );
add_option('crisp_onboarding', false);
}
function crisp_plugin_settings_page() {
if (isset($_GET["crisp_website_id"]) && !empty($_GET["crisp_website_id"])) {
update_option("website_id", $_GET["crisp_website_id"]);
}
if (isset($_GET["crisp_verify"]) && !empty($_GET["crisp_verify"])) {
update_option("website_verify", $_GET["crisp_verify"]);
}
$website_id = get_option('website_id');
$is_crisp_working = isset($website_id) && !empty($website_id);
$http_callback = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s://" : "://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$add_to_crisp_link = "https://app.crisp.chat/initiate/plugin/aca0046c-356c-428f-8eeb-063014c6a278?payload=$http_callback";
?>
<link rel="stylesheet" href="<?php echo plugins_url("assets/style.css", __FILE__ );?>">
<?php
if ($is_crisp_working) {
?>
<div class="wrap crisp-wrap">
<div class="crisp-modal">
<h2 class="crisp-title"><?php _e('Connected with Crisp.', 'crisp'); ?></h2>
<p class="crisp-subtitle"><?php _e('You can now use Crisp from your homepage.', 'crisp'); ?></p>
<a class="crisp-button crisp-neutral" href="https://app.crisp.chat/settings/website/<?php echo $website_id ?>"><?php _e('Go to my Crisp settings', 'crisp'); ?></a>
<a class="crisp-button crisp" href="https://app.crisp.chat/website/<?php echo $website_id ?>/inbox/"><?php _e('Go to my Inbox', 'crisp'); ?></a>
<a class="crisp-button crisp-neutral" href="<?php echo $add_to_crisp_link; ?>"><?php _e('Reconfigure', 'crisp'); ?></a>
</div>
<p class="crisp-notice"><?php _e('Loving Crisp <b style="color:red">♥</b> ? Rate us on the <a target="_blank" href="https://wordpress.org/support/plugin/crisp/reviews/?filter=5">Wordpress Plugin Directory</a>', 'crisp'); ?></p>
</div>
<?php
} else {
?>
<div class="wrap crisp-wrap">
<div class="crisp-modal">
<h2 class="crisp-title"><?php _e('Connect with Crisp.', 'crisp'); ?></h2>
<p class="crisp-subtitle"><?php _e('This link will redirect you to Crisp and configure your Wordpress. Magic', 'crisp'); ?></p>
<a class="crisp-button crisp" href="<?php echo $add_to_crisp_link; ?>"><?php _e('Connect with Crisp', 'crisp'); ?></a>
</div>
</div>
<?php
}
}
add_action('wp_head', 'crisp_hook_head', 1);
function crisp_sync_wordpress_user() {
$output = "";
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
}
if (!isset($current_user)) {
return "";
}
$website_verify = get_option('website_verify');
$email = esc_js($current_user->user_email);
$nickname = esc_js($current_user->display_name);
if (!empty($email) && empty($website_verify)) {
$output .= '$crisp.push(["set", "user:email", "' . $email . '"]);';
} else if (!empty($email)) {
$hmac = hash_hmac("sha256", $email, $website_verify);
$output .= '$crisp.push(["set", "user:email", ["' . $email . '", "' . $hmac . '"]]);';
}
if (!empty($nickname)) {
$output .= '$crisp.push(["set", "user:nickname", "' . $nickname . '"]);';
}
return $output;
}
function crisp_sync_woocommerce_customer() {
$output = "";
if (!class_exists("WooCommerce") || is_admin()) {
return $output;
}
$customer = WC()->session->get("customer");
if ($customer == NULL) {
return $output;
}
if (isset($customer["phone"]) && !empty($customer["phone"])) {
$output .= '$crisp.push(["set", "user:phone", "' . $customer["phone"] . '"]);';
}
$nickname = "";
if (isset($customer["first_name"]) && !empty($customer["first_name"])) {
$nickname = $customer["first_name"];
}
if (isset($customer["last_name"]) && !empty($customer["last_name"])) {
$nickname .= " ".$customer["last_name"];
}
if (!empty($nickname)) {
$output .= '$crisp.push(["set", "user:nickname", "' . $nickname . '"]);';
}
$data = array();
$data_keys = array(
"company",
"address",
"address_1",
"address_2",
"postcode",
"state",
"country",
"shipping_company",
"shipping_address",
"shipping_address_1",
"shipping_address_2",
"shipping_state",
"shipping_country",
);
foreach ($data_keys as $key) {
if (isset($customer[$key]) && !empty($customer[$key])) {
$data[] = '["'. $key . '", "' . $customer[$key] . '"]';
}
}
if (count($data) > 0) {
$output .= '$crisp.push(["set", "session:data", [[' . implode(",", $data) . ']]]);';
}
return $output;
}
function crisp_hook_head() {
$website_id = get_option('website_id');
$locale = str_replace("_", "-", strtolower(get_locale()));
if (!in_array($locale, array("pt-br", "pt-pr"))) {
$locale = substr($locale, 0, 2);
}
if (!isset($website_id) || empty($website_id)) {
return;
}
$output="<script data-cfasync='false'>
window.\$crisp=[];
CRISP_RUNTIME_CONFIG = {
locale : '$locale'
};
CRISP_WEBSITE_ID = '$website_id';";
$output .= "(function(){
d=document;s=d.createElement('script');
s.src='https://client.crisp.chat/l.js';
s.async=1;d.getElementsByTagName('head')[0].appendChild(s);
})();";
$output .= crisp_sync_wordpress_user();
$output .= crisp_sync_woocommerce_customer();
$output .= "</script>";
echo $output;
}