-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhellotext.php
More file actions
139 lines (115 loc) · 3.59 KB
/
hellotext.php
File metadata and controls
139 lines (115 loc) · 3.59 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
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
<?php
/**
* Hellotext
*
* @package Hellotext
*
* @wordpress-plugin
* Plugin Name: Hellotext
* Plugin URI: https://github.com/hellotext/hellotext-wordpress
* Description: Integrates Hellotext tracking to WooCommerce.
* Version: 1.3.1
* Author: Hellotext
* Author URI: https://www.hellotext.com
* License: GPL v2
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: hellotext
* Domain Path: /languages
*/
use Hellotext\Constants;
// TODO: Refactor this to use the APP_ENV variable
if (! isset($_ENV['APP_ENV'])) {
$_ENV['APP_ENV'] = 'production';
}
$TEST = $_ENV['APP_ENV'] === 'test';
$HELLOTEXT_DEV_MODE = $_ENV['APP_ENV'] === 'development';
$HELLOTEXT_API_URL = $HELLOTEXT_DEV_MODE
? $_ENV['HELLOTEXT_API_URL'] ?? ''
: 'https://api.hellotext.com';
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Load Composer autoloader (handles all classes)
require_once __DIR__ . '/vendor/autoload.php';
// Load event handlers (contain functions, not classes)
$event_files = glob(__DIR__ . '/src/Events/*.php');
foreach ($event_files as $file) {
require_once $file;
}
// Load misc files (Settings, Scripts contain functions)
$misc_files = glob(__DIR__ . '/src/Misc/*.php');
foreach ($misc_files as $file) {
require_once $file;
}
// Function on Events/AppRemoved.php
register_deactivation_hook( __FILE__, 'hellotext_deactivate' );
/**
* Check for plugin updates from GitHub.
* Caches result for 24 hours to avoid rate limiting.
*
* @return void
*/
function hellotext_version_check() {
if (!is_admin()) {
return;
}
$cache_key = 'hellotext_version_check';
$cached = get_transient($cache_key);
if (false !== $cached) {
if (isset($cached['message'])) {
echo '<div class="notice notice-warning"><p>' . wp_kses_post($cached['message']) . '</p></div>';
}
return;
}
$response = wp_remote_get(
'https://api.github.com/repos/hellotext/hellotext-wordpress/releases/latest',
['timeout' => 5]
);
$cache_data = [];
if (!is_wp_error($response) && 200 === wp_remote_retrieve_response_code($response)) {
$release = json_decode(wp_remote_retrieve_body($response), true);
if (isset($release['tag_name'])) {
$current = get_plugin_data(__FILE__)['Version'];
$latest = ltrim($release['tag_name'], 'v');
if (version_compare($current, $latest, '<')) {
$cache_data['message'] = sprintf(
'New version of Hellotext available: %s. <a href="%s">View details</a>',
$latest,
$release['html_url']
);
}
}
}
set_transient($cache_key, $cache_data, DAY_IN_SECONDS);
if (isset($cache_data['message'])) {
echo '<div class="notice notice-warning"><p>' . wp_kses_post($cache_data['message']) . '</p></div>';
}
}
add_action('admin_notices', 'hellotext_version_check');
/**
* Load plugin text domain.
*
* @return void
*/
function hellotext_load_textdomain() {
load_plugin_textdomain( 'hellotext', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'hellotext_load_textdomain' );
/**
* Uninstall handler for cleanup.
*
* @return void
*/
function uninstall() {
global $wpdb;
delete_option(Constants::OPTION_BUSINESS_ID);
delete_option(Constants::OPTION_WEBCHAT_ID);
delete_option(Constants::OPTION_WEBCHAT_PLACEMENT);
delete_option(Constants::OPTION_WEBCHAT_BEHAVIOUR);
delete_option(Constants::OPTION_ACCESS_TOKEN);
$api_keys_table = $wpdb->prefix . 'woocommerce_api_keys';
if ($wpdb->get_var("SHOW TABLES LIKE '$api_keys_table'") === $api_keys_table) {
$wpdb->delete($api_keys_table, ['description' => 'Hellotext']);
}
}
register_uninstall_hook(__FILE__, 'uninstall');