-
Notifications
You must be signed in to change notification settings - Fork 13
/
bootstrap.php
535 lines (473 loc) · 18.4 KB
/
bootstrap.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<?php
/*
Plugin Name: Intercom
Plugin URI: https://wordpress.org/plugins/intercom
Description: Official <a href="https://www.intercom.io">Intercom</a> support for WordPress.
Author: Intercom
Author URI: https://www.intercom.io
Version: 2.6.6
*/
class IdentityVerificationCalculator
{
private $raw_data = array();
private $secret_key = "";
public function __construct($data, $secret_key)
{
$this->raw_data = $data;
$this->secret_key = $secret_key;
}
public function identityVerificationComponent()
{
$secret_key = $this->getSecretKey();
if (empty($secret_key))
{
return $this->emptyIdentityVerificationHashComponent();
}
if (array_key_exists("user_id", $this->getRawData()))
{
return $this->identityVerificationHashComponent("user_id");
}
if (array_key_exists("email", $this->getRawData()))
{
return $this->identityVerificationHashComponent("email");
}
return $this->emptyIdentityVerificationHashComponent();
}
private function emptyIdentityVerificationHashComponent()
{
return array();
}
private function identityVerificationHashComponent($key)
{
$raw_data = $this->getRawData();
return array("user_hash" => hash_hmac("sha256", $raw_data[$key], $this->getSecretKey()));
}
private function getSecretKey()
{
return $this->secret_key;
}
private function getRawData()
{
return $this->raw_data;
}
}
class IntercomSettingsPage
{
private $settings = array();
private $styles = array();
public function __construct($settings)
{
$this->settings = $settings;
$this->styles = $this->setStyles($settings);
}
public function dismissibleMessage($text)
{
return <<<END
<div id="message" class="updated notice is-dismissible">
<p>$text</p>
<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
</div>
END;
}
public function getAuthUrl() {
return "https://wordpress_auth.intercom.io/confirm?state=".get_site_url()."::".wp_create_nonce('intercom-oauth');
}
public function htmlUnclosed()
{
$settings = $this->getSettings();
$styles = $this->getStyles();
$app_id = WordPressEscaper::escAttr($settings['app_id']);
$secret = WordPressEscaper::escAttr($settings['secret']);
$auth_url = $this->getAuthUrl();
$dismissable_message = '';
if (isset($_GET['appId'])) {
// Copying app_id from setup guide
$app_id = WordPressEscaper::escAttr($_GET['appId']);
$dismissable_message = $this->dismissibleMessage("We've copied your new Intercom app id below. click to save changes and then close this window to finish signing up for Intercom.");
}
if (isset($_GET['saved'])) {
$dismissable_message = $this->dismissibleMessage("Your app id has been successfully saved. You can now close this window to finish signing up for Intercom.");
}
if (isset($_GET['authenticated'])) {
$dismissable_message = $this->dismissibleMessage('You successfully authenticated with Intercom');
}
return <<<END
<link rel="stylesheet" property='stylesheet' href="https://marketing.intercomassets.com/assets/redesign-ead0ee66f7c89e2930e04ac1b7e423494c29e8e681382f41d0b6b8a98b4591e1.css">
<style>
#wpcontent {
background-color: #ffffff;
}
</style>
<div class="wrap">
$dismissable_message
<section id="main_content" style="padding-top: 70px;">
<div class="container">
<div class="cta">
<div class="sp__2--lg sp__2--xlg"></div>
<div id="oauth_content" style="$styles[app_id_link_style]">
<div class="t__h1 c__red">Get started with Intercom</div>
<div class="cta__desc">
Chat with visitors to your website in real-time, capture them as leads, and convert them to customers. Install Intercom on your WordPress site in a couple of clicks.
</div>
<div id="get_intercom_btn_container" style="position:relative;margin-top:30px;">
<a href="$auth_url">
<img src="https://static.intercomassets.com/assets/oauth/primary-7edb2ebce84c088063f4b86049747c3a.png" srcset="https://static.intercomassets.com/assets/oauth/primary-7edb2ebce84c088063f4b86049747c3a.png 1x, https://static.intercomassets.com/assets/oauth/primary@2x-0d69ca2141dfdfa0535634610be80994.png 2x, https://static.intercomassets.com/assets/oauth/primary@3x-788ed3c44d63a6aec3927285e920f542.png 3x"/>
</a>
</div>
</div>
<div class="t__h1 c__red" style="$styles[app_id_copy_title]">Intercom setup</div>
<div class="t__h1 c__red" style="$styles[app_id_saved_title]">Intercom app ID saved</div>
<div id="app_id_and_secret_content" style="$styles[app_id_row_style]">
<div class="t__h1 c__red" style="$styles[app_id_copy_hidden]">Intercom has been installed</div>
<div class="cta__desc">
<div style="$styles[app_id_copy_hidden]">
Intercom is now set up and ready to go. You can now chat with your existing and potential new customers, send them targeted messages, and get feedback.
<br/>
<br/>
<a class="c__blue" href="https://app.intercom.com/a/apps/$app_id" target="_blank">Click here to access your Intercom Team Inbox.</a>
<br/>
<br/>
Need help? <a class="c__blue" href="https://docs.intercom.io/for-converting-visitors-to-users" target="_blank">Visit our documentation</a> for best practices, tips, and much more.
<br/>
<br/>
</div>
<div>
<div style="font-size:0.87em;$styles[app_id_copy_hidden]">
Learn more about our products : <a class="c__blue" href="https://www.intercom.com/customer-engagement" target="_blank">Messages</a>, <a class="c__blue" href="https://www.intercom.com/customer-support-software/knowledge-base" target="_blank">Articles</a> and <a class="c__blue" href="https://www.intercom.com/customer-support-software/help-desk" target="_blank">Inbox</a>.
</div>
<form method="post" action="" name="update_settings">
<table class="form-table" align="center" style="margin-top: 16px; width: inherit;">
<tbody>
<tr>
<th scope="row" style="text-align: center; vertical-align: middle;"><label for="intercom_app_id">App ID</label></th>
<td>
<input id="intercom_app_id" $styles[app_id_state] name="app_id" type="text" value="$app_id" class="$styles[app_id_class]">
<button type="submit" class="btn btn__primary cta__submit" style="$styles[button_submit_style]">Save</button>
</td>
</tr>
</tbody>
</table>
END;
}
public function htmlClosed()
{
$settings = $this->getSettings();
$styles = $this->getStyles();
$auth_url = $this->getAuthUrl();
$secret = WordPressEscaper::escAttr($settings['secret']);
$app_id = WordPressEscaper::escAttr($settings['app_id']);
$auth_url_identity_verification = "";
if (empty($secret) && !empty($app_id)) {
$auth_url_identity_verification = $auth_url.'&enable_identity_verification=1';
}
return <<<END
</form>
<div style="$styles[app_id_copy_hidden]">
<div style="$styles[app_secret_link_style]">
<a class="c__blue" href="$auth_url_identity_verification">Authenticate with your Intercom application to enable Identity Verification</a>
</div>
<p style="font-size:0.86em">Identity Verification ensures that conversations between you and your users are kept private.<br/>
<br/>
<a class="c__blue" href="https://docs.intercom.com/configure-intercom-for-your-product-or-site/staying-secure/enable-identity-verification-on-your-web-product" target="_blank">Learn more about Identity Verification</a>
</p>
<br/>
<div style="font-size:0.8em">If the Intercom application associated with your Wordpress is incorrect, please <a class="c__blue" href="$auth_url">click here</a> to reconnect with Intercom, to choose a new application.</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
END;
}
public function html()
{
return $this->htmlUnclosed() . $this->htmlClosed();
}
public function setStyles($settings) {
$styles = array();
$app_id = WordPressEscaper::escAttr($settings['app_id']);
$secret = WordPressEscaper::escAttr($settings['secret']);
$identity_verification = WordPressEscaper::escAttr($settings['identity_verification']);
// Use Case : Identity Verification enabled : checkbox checked and disabled
if($identity_verification) {
$styles['identity_verification_state'] = 'checked disabled';
} else {
$styles['identity_verification_state'] = '';
}
// Use Case : app_id here but Identity Verification disabled
if (empty($secret) && !empty($app_id)) {
$styles['app_secret_row_style'] = 'display: none;';
$styles['app_secret_link_style'] = '';
} else {
$styles['app_secret_row_style'] = '';
$styles['app_secret_link_style'] = 'display: none;';
}
// Copying appId from Intercom Setup Guide for validation
if (isset($_GET['appId'])) {
$app_id = WordPressEscaper::escAttr($_GET['appId']);
$styles['app_id_state'] = 'readonly';
$styles['app_id_class'] = "cta__email";
$styles['button_submit_style'] = '';
$styles['app_id_copy_hidden'] = 'display: none;';
$styles['app_id_copy_title'] = '';
$styles['identity_verification_state'] = 'disabled'; # Prevent from sending POST data about identity_verification when using app_id form
} else {
$styles['app_id_class'] = "";
$styles['button_submit_style'] = 'display: none;';
$styles['app_id_copy_title'] = 'display: none;';
$styles['app_id_state'] = 'disabled'; # Prevent from sending POST data about app_id when using identity_verification form
$styles['app_id_copy_hidden'] = '';
}
//Use Case App_id successfully copied
if (isset($_GET['saved'])) {
$styles['app_id_copy_hidden'] = 'display: none;';
$styles['app_id_saved_title'] = '';
} else {
$styles['app_id_saved_title'] = 'display: none;';
}
// Display 'connect with intercom' button if no app_id provided (copied from setup guide or from Oauth)
if (empty($app_id)) {
$styles['app_id_row_style'] = 'display: none;';
$styles['app_id_link_style'] = '';
} else {
$styles['app_id_row_style'] = '';
$styles['app_id_link_style'] = 'display: none;';
}
return $styles;
}
private function getSettings()
{
return $this->settings;
}
private function getStyles()
{
return $this->styles;
}
}
class IntercomSnippet
{
private $snippet_settings = "";
public function __construct($snippet_settings)
{
$this->snippet_settings = $snippet_settings;
}
public function html()
{
return $this->shutdown_on_logout() . $this->source();
}
private function shutdown_on_logout()
{
return <<<HTML
<script data-cfasync="false">
document.onreadystatechange = function () {
if (document.readyState == "complete") {
var logout_link = document.querySelectorAll('a[href*="wp-login.php?action=logout"]');
if (logout_link) {
for(var i=0; i < logout_link.length; i++) {
logout_link[i].addEventListener( "click", function() {
Intercom('shutdown');
});
}
}
}
};
</script>
HTML;
}
private function source()
{
$snippet_json = $this->snippet_settings->json();
$app_id = $this->snippet_settings->appId();
return <<<HTML
<script data-cfasync="false">
window.intercomSettings = $snippet_json;
</script>
<script data-cfasync="false">(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/$app_id';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s, x);};if(document.readyState==='complete'){l();}else if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script>
HTML;
}
}
class IntercomSnippetSettings
{
private $raw_data = array();
private $secret = NULL;
private $wordpress_user = NULL;
public function __construct($raw_data, $secret = NULL, $wordpress_user = NULL, $constants = array('ICL_LANGUAGE_CODE' => 'language_override'))
{
$this->raw_data = $this->validateRawData($raw_data);
$this->secret = $secret;
$this->wordpress_user = $wordpress_user;
$this->constants = $constants;
}
public function json()
{
return json_encode($this->getRawData());
}
public function appId()
{
$raw_data = $this->getRawData();
return $raw_data["app_id"];
}
private function getRawData()
{
$user = new IntercomUser($this->wordpress_user, $this->raw_data);
$settings = apply_filters("intercom_settings", $user->buildSettings());
$identityVerificationCalculator = new IdentityVerificationCalculator($settings, $this->secret);
$result = array_merge($settings, $identityVerificationCalculator->identityVerificationComponent());
$result = $this->mergeConstants($result);
$result['installation_type'] = 'wordpress';
return $result;
}
private function mergeConstants($settings) {
foreach($this->constants as $key => $value) {
if (defined($key)) {
$const_val = WordPressEscaper::escJS(constant($key));
$settings = array_merge($settings, array($value => $const_val));
}
}
return $settings;
}
private function validateRawData($raw_data)
{
if (!array_key_exists("app_id", $raw_data)) {
throw new Exception("app_id is required");
}
return $raw_data;
}
}
if (getenv('INTERCOM_PLUGIN_TEST') == '1' && !function_exists('apply_filters')) {
function apply_filters($_, $value) {
return $value;
}
}
class WordPressEscaper
{
public static function escAttr($value)
{
if (function_exists('esc_attr')) {
return esc_attr($value);
} else {
if (getenv('INTERCOM_PLUGIN_TEST') == '1') {
return $value;
}
}
}
public static function escJS($value)
{
if (function_exists('esc_js')) {
return esc_js($value);
} else {
if (getenv('INTERCOM_PLUGIN_TEST') == '1') {
return $value;
}
}
}
}
class IntercomUser
{
private $wordpress_user = NULL;
private $settings = array();
public function __construct($wordpress_user, $settings)
{
$this->wordpress_user = $wordpress_user;
$this->settings = $settings;
}
public function buildSettings()
{
if (empty($this->wordpress_user))
{
return $this->settings;
}
if (!empty($this->wordpress_user->user_email))
{
$this->settings["email"] = WordPressEscaper::escJS($this->wordpress_user->user_email);
}
if (!empty($this->wordpress_user->display_name))
{
$this->settings["name"] = WordPressEscaper::escJS($this->wordpress_user->display_name);
}
return $this->settings;
}
}
class Validator
{
private $inputs = array();
private $validation;
public function __construct($inputs, $validation)
{
$this->input = $inputs;
$this->validation = $validation;
}
public function validAppId()
{
return $this->validate($this->input["app_id"]);
}
public function validSecret()
{
return $this->validate($this->input["secret"]);
}
private function validate($x)
{
return call_user_func($this->validation, $x);
}
}
if (getenv('INTERCOM_PLUGIN_TEST') != '1') {
if (!defined('ABSPATH')) exit;
}
function add_intercom_snippet()
{
$options = get_option('intercom');
$snippet_settings = new IntercomSnippetSettings(
array("app_id" => WordPressEscaper::escJS($options['app_id'])),
WordPressEscaper::escJS($options['secret']),
wp_get_current_user()
);
$snippet = new IntercomSnippet($snippet_settings);
echo $snippet->html();
}
function add_intercom_settings_page()
{
add_options_page(
'Intercom Settings',
'Intercom',
'manage_options',
'intercom',
'render_intercom_options_page'
);
}
function render_intercom_options_page()
{
if (!current_user_can('manage_options'))
{
wp_die('You do not have sufficient permissions to access Intercom settings');
}
$options = get_option('intercom');
$settings_page = new IntercomSettingsPage(array("app_id" => $options['app_id'], "secret" => $options['secret']));
echo $settings_page->htmlUnclosed();
wp_nonce_field('intercom-update');
echo $settings_page->htmlClosed();
}
function intercom_settings() {
register_setting('intercom', 'intercom');
if (isset($_GET['state']) && wp_verify_nonce($_GET[ 'state'], 'intercom-oauth') && current_user_can('manage_options') && isset($_GET['app_id']) && isset($_GET['secret']) ) {
$validator = new Validator($_GET, function($x) { return wp_kses(trim($x), array()); });
update_option("intercom", array("app_id" => $validator->validAppId(), "secret" => $validator->validSecret()));
$redirect_to = 'options-general.php?page=intercom&authenticated=1';
wp_safe_redirect(admin_url($redirect_to));
}
if (current_user_can('manage_options') && isset($_POST['app_id']) && isset($_POST[ '_wpnonce']) && wp_verify_nonce($_POST[ '_wpnonce'],'intercom-update')) {
$options = array();
$options["app_id"] = WordPressEscaper::escAttr($_POST['app_id']);
update_option("intercom", $options);
wp_safe_redirect(admin_url('options-general.php?page=intercom&saved=1'));
}
}
if (getenv('INTERCOM_PLUGIN_TEST') != '1') {
add_action('wp_footer', 'add_intercom_snippet');
add_action('admin_menu', 'add_intercom_settings_page');
add_action('network_admin_menu', 'add_intercom_settings_page');
add_action('admin_init', 'intercom_settings');
}