forked from happyfoxinc/hfchat-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhappyfox-chat.php
More file actions
87 lines (76 loc) · 2.7 KB
/
happyfox-chat.php
File metadata and controls
87 lines (76 loc) · 2.7 KB
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
<?php
/**
* Plugin Name: HappyFox Chat
* Plugin URI: https://happyfoxchat.com/wordpress
* Description: This plugin adds the HappyFox Chat widget to your Wordpress blog
* Version: 1.0.0
* Author: HappyFox Inc.
* Author URI: http://happyfoxchat.com
* License: MIT
*/
add_action('init', 'hfc_setup_widget');
add_action('admin_init', 'hfc_register_settings' );
add_action('admin_menu', 'hfc_admin_menu');
add_action('wp_footer', 'hfc_add_visitor_widget' );
function hfc_register_settings() {
register_setting('happyfox-chat-settings', 'hfc_api_key');
register_setting('happyfox-chat-settings', 'hfc_embed_token');
register_setting('happyfox-chat-settings', 'hfc_access_token');
}
function hfc_admin_menu() {
add_menu_page('HappyFox Chat Settings', 'HappyFox Chat', 'administrator', 'happyfox-chat-settings', 'happyfox_chat_settings_page', 'dashicons-format-chat');
wp_enqueue_style('happyfox-chat-settings', WP_PLUGIN_URL . '/happyfox-chat/css/style.css');
}
function happyfox_chat_settings_page() {
include('happyfox-chat-settings.php');
}
function hfc_setup_widget() {
if( !session_id() )
session_start();
$error = "";
if (isset( $_POST['hfc_api_key_submission'] ) && $_POST['hfc_api_key_submission'] == '1') {
$url = 'https://happyfoxchat.com/api/v1/integrations/wordpress/widget-info?apiKey='. $_POST['hfc_api_key'];
$result = wp_remote_get($url);
try {
$json = json_decode($result['body']);
if(isset($json->embedToken)) {
update_option('hfc_api_key', $_POST['hfc_api_key']);
update_option('hfc_embed_token', $json->embedToken);
update_option('hfc_access_token', $json->accessToken);
echo "Success";
} else {
$error = "Please check your API key";
}
} catch( Exception $ex ) {
$error = "Please check your API key";
}
$_SESSION['error'] = $error;
}
}
function hfc_add_visitor_widget() {
$embed_token = get_option('hfc_embed_token');
$access_token = get_option('hfc_access_token');
if($embed_token && $access_token) {
echo <<<HTML
<!--Start of HappyFox Live Chat Script-->
<script>
window.HFCHAT_CONFIG = {
EMBED_TOKEN: "{$embed_token}",
ACCESS_TOKEN: "{$access_token}",
HOST_URL: "https://www.happyfoxchat.com",
ASSETS_URL: "https://d1l7z5ofrj6ab8.cloudfront.net/visitor"
};
(function() {
var scriptTag = document.createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.async = true;
scriptTag.src = window.HFCHAT_CONFIG.ASSETS_URL + '/js/widget-loader.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(scriptTag, s);
})();
</script>
<!--End of HappyFox Live Chat Script-->
HTML;
}
}
?>