-
Notifications
You must be signed in to change notification settings - Fork 1
/
wcp-admin.php
356 lines (314 loc) · 12.6 KB
/
wcp-admin.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
<?php
/**
* Class defining admin functions
*
* @package (wcp\)
*/
// Exit if accessed directly
defined('ABSPATH') || die('Access Restricted!');
// Include everything.
include_once(dirname(__FILE__) . '/wcp-include-all.php');
//==============================================================================
// Global vars.
global $g_wcp_plugin_directory_url;
$g_wcp_plugin_directory_url = plugins_url('', __FILE__);
global $g_wcp_cron_script_url;
$g_wcp_cron_script_url = get_site_url() . '/wp-cron.php';
//==============================================================================
// Global default settings
global $g_wcp_config_defaults;
$g_wcp_config_defaults = array(
'assigned_address_expires_in_mins' => 3 * 60, // 3 hours to pay for order and receive necessary number of confirmations.
'funds_received_value_expires_in_mins' => '2', // 'received_funds_checked_at' is fresh (considered to be a valid value) if it was last checked within 'funds_received_value_expires_in_mins' minutes.
'max_blockchains_api_failures' => '3', // Return error after this number of sequential failed attempts to retrieve blockchain data.
'max_unusable_generated_addresses' => '20', // Return error after this number of unusable (non-empty) wallet addresses were sequentially generated.
'blockchain_api_timeout_secs' => '20', // Connection and request timeouts for get operations dealing with blockchain requests.
'exchange_rate_api_timeout_secs' => '10', // Connection and request timeouts for get operations dealing with exchange rate API requests.
'soft_cron_job_schedule_name' => 'minutes_5', // WP cron job frequency.
'database_schema_version' => '1.4',
);
global $g_wcp_btc_user_defaults;
$g_wcp_btc_user_defaults = array(
'confs_num' => '6',
'exchange_reference_rate' => 'Coingecko',
'exchange_multiplier' => '1.00',
'exchange_rate_cache_time' => '20',
'starting_index_for_new_addresses' => '5',
'autocomplete_paid_orders' => '1',
);
global $g_wcp_fair_user_defaults;
$g_wcp_fair_user_defaults = array(
'exchange_reference_rate' => 'FreeVision',
'exchange_multiplier' => '1.00',
'exchange_rate_cache_time' => '20',
'starting_index_for_new_addresses' => '5',
'autocomplete_paid_orders' => '1',
);
global $g_wcp_general_defaults;
$g_wcp_general_defaults = array(
'enable_soft_cron' => '1',
'delete_db_tables_on_uninstall' => '1',
'reuse_expired_addresses' => '1', // True - may reduce anonymouty of store customers (someone may click/generate bunch of fake orders to list many addresses that in a future will be used by real customers).
// False - better anonymouty but may leave many addresses in wallet unused (and hence will require very high 'gap limit') due to many unpaid order clicks.
// In this case it is recommended to regenerate new wallet after 'gap limit' reaches 1000.
);
//==============================================================================
/**
* Returns plugin wide settings
*
* @return array array containing all the settings, some settings are arrays with more values
*/
function wcp__get_settings()
{
global $g_wcp_plugin_directory_url;
global $g_wcp_config_defaults;
global $g_wcp_btc_user_defaults;
global $g_wcp_fair_user_defaults;
global $g_wcp_general_defaults;
$wcp_settings = get_option(WCP_SETTINGS_NAME);
if (!is_array($wcp_settings)) {
$wcp_settings = $g_wcp_config_defaults;
}
return $wcp_settings;
$btc_settings = get_option(WCP_BTC_SETTINGS);
if (!is_array($btc_settings)) {
$btc_settings = $g_wcp_btc_user_defaults;
}
return $btc_settings;
$fair_settings = get_option(WCP_FAIR_SETTINGS);
if (!is_array($fair_settings)) {
$fair_settings = $g_wcp_fair_user_defaults;
}
return $fair_settings;
$general_settings = get_option(WCP_GENERAL_SETTINGS);
if (!is_array($general_settings)) {
$general_settings = $g_wcp_general_defaults;
}
return $general_settings;
}
//==============================================================================
/**
* Returns Bitcoin (BTC) specific settings
*
* @return array array containing all Bitcoin (BTC) settings, some settings are arrays with more values
*/
function wcp__get_btc_settings()
{
global $g_wcp_plugin_directory_url;
global $g_wcp_btc_user_defaults;
$btc_settings = get_option(WCP_BTC_SETTINGS);
if (!is_array($btc_settings)) {
$btc_settings = $g_wcp_btc_user_defaults;
}
return $btc_settings;
}
//==============================================================================
/**
* Returns FairCoin (FAIR) specific settings
*
* @return array array containing all FairCoin (FAIR) settings, some settings are arrays with more values
*/
function wcp__get_fair_settings()
{
global $g_wcp_plugin_directory_url;
global $g_wcp_fair_user_defaults;
$fair_settings = get_option(WCP_FAIR_SETTINGS);
if (!is_array($fair_settings)) {
$fair_settings = $g_wcp_fair_user_defaults;
}
return $fair_settings;
}
//==============================================================================
/**
* Returns Settings saved on general tab of admin page
*
* @return array array containing all general settings, some settings are arrays with more values
*/
function wcp__get_general_settings()
{
global $g_wcp_plugin_directory_url;
global $g_wcp_general_defaults;
$general_settings = get_option(WCP_GENERAL_SETTINGS);
if (!is_array($general_settings)) {
$general_settings = $g_wcp_general_defaults;
}
return $general_settings;
}
//==============================================================================
/**
* Updates the plugin wide settings
* This can be
*
* @return [type] [description]
*/
function wcp__update_settings()
{
global $g_wcp_config_defaults;
global $g_wcp_btc_user_defaults;
global $g_wcp_fair_user_defaults;
global $g_wcp_general_defaults;
// Load current settings and overwrite them with whatever values are present on submitted form
$wcp_settings = wcp__get_settings();
foreach ($g_wcp_general_defaults as $k => $v) {
if (!isset($wcp_settings[$k])) {
$wcp_settings[$k] = '';
} // Force set to something.
// if no old value is present and no new value is given
// we want to set the settings to the default
$value = $v;
if (isset($_POST[$k])) {
// we have a new value that we want to set
$value = $_POST[$k];
} elseif ($wcp_settings[$k] != '') {
// there is a value and we do not want to change it.
continue;
}
WCP__update_individual_wcp_setting($wcp_settings[$k], $value);
}
$btc_settings = wcp__get_btc_settings();
foreach ($g_wcp_btc_user_defaults as $k => $v) {
if (!isset($btc_settings[$k])) {
$btc_settings[$k] = '';
} // Force set to something.
// if no old value is present and no new value is given
// we want to set the settings to the default
$value = $v;
if (isset($_POST[$k])) {
// we have a new value that we want to set
$value = $_POST[$k];
} elseif ($btc_settings[$k] != '') {
// there is a value and we do not want to change it.
continue;
}
WCP__update_individual_wcp_setting($btc_settings[$k], $value);
}
$fair_settings = wcp__get_fair_settings();
foreach ($g_wcp_fair_user_defaults as $k => $v) {
if (!isset($fair_settings[$k])) {
$fair_settings[$k] = '';
} // Force set to something.
// if no old value is present and no new value is given
// we want to set the settings to the default
$value = $v;
if (isset($_POST[$k])) {
// we have a new value that we want to set
$value = $_POST[$k];
} elseif ($fair_settings[$k] != '') {
// there is a value and we do not want to change it.
continue;
}
WCP__update_individual_wcp_setting($fair_settings[$k], $value);
}
$general_settings = wcp__get_general_settings();
foreach ($g_wcp_general_defaults as $k => $v) {
if (!isset($general_settings[$k])) {
$general_settings[$k] = '';
} // Force set to something.
// if no old value is present and no new value is given
// we want to set the settings to the default
$value = $v;
if (isset($_POST[$k])) {
// we have a new value that we want to set
$value = $_POST[$k];
} elseif ($general_settings[$k] != '') {
// there is a value and we do not want to change it.
continue;
}
WCP__update_individual_wcp_setting($general_settings[$k], $value);
}
update_option(WCP_SETTINGS_NAME, $wcp_settings);
update_option(WCP_BTC_SETTINGS, $btc_settings);
update_option(WCP_FAIR_SETTINGS, $fair_settings);
update_option(WCP_GENERAL_SETTINGS, $general_settings);
}
//==============================================================================
// Takes care of recursive updating
function WCP__update_individual_wcp_setting(&$wcp_current_setting, $wcp_new_setting)
{
if (is_string($wcp_new_setting)) {
$wcp_current_setting = WCP__stripslashes($wcp_new_setting);
} elseif (is_array($wcp_new_setting)) { // Note: new setting may not exist yet in current setting: curr[t5] - not set yet, while new[t5] set.
// Need to do recursive
foreach ($wcp_new_setting as $k => $v) {
if (!isset($wcp_current_setting[$k])) {
$wcp_current_setting[$k] = '';
} // If not set yet - force set it to something.
WCP__update_individual_wcp_setting($wcp_current_setting[$k], $v);
}
} else {
$wcp_current_setting = $wcp_new_setting;
}
}
//==============================================================================
// Reset settings only for one screen
function WCP__reset_partial_settings()
{
global $g_wcp_config_defaults;
// Load current settings and overwrite ones that are present on submitted form with defaults
$wcp_settings = wcp__get_settings();
foreach ($_POST as $k => $v) {
if (isset($g_wcp_config_defaults[$k])) {
if (!isset($wcp_settings[$k])) {
$wcp_settings[$k] = '';
} // Force set to something.
WCP__update_individual_wcp_setting($wcp_settings[$k], $g_wcp_config_defaults[$k]);
}
}
update_option(WCP_SETTINGS_NAME, $wcp_settings);
}
//==============================================================================
// Resets all settings to default
function WCP__reset_all_settings()
{
global $g_wcp_config_defaults;
update_option(WCP_SETTINGS_NAME, $g_wcp_config_defaults);
}
//==============================================================================
// Updates the exchange rate cache
function WCP__update_cache($exchange_rate, $exchange_reference_rate)
{
// Save new currency exchange rate info in cache
$wcp_settings = wcp__get_settings();
$currency_code = get_woocommerce_currency();
if (!isset($wcp_settings['exchange_rates'])) {
$wcp_settings['exchange_rates'] = array();
}
if (!isset($wcp_settings['exchange_rates'][$currency_code])) {
$wcp_settings['exchange_rates'][$currency_code] = array();
}
if (!isset($wcp_settings['exchange_rates'][$currency_code][$exchange_reference_rate])) {
$wcp_settings['exchange_rates'][$currency_code][$exchange_reference_rate] = array();
}
$wcp_settings['exchange_rates'][$currency_code][$exchange_reference_rate]['time-last-checked'] = time();
$wcp_settings['exchange_rates'][$currency_code][$exchange_reference_rate]['exchange_rate'] = $exchange_rate;
update_option(WCP_SETTINGS_NAME, $wcp_settings);
}
//==============================================================================
// Creates required tables in database
/*
----------------------------------
: Table 'btc_addresses' :
----------------------------------
status "unused" - never been used address with last known zero balance
"assigned" - order was placed and this address was assigned for payment
"revalidate" - assigned/expired, unused or unknown address suddenly got non-zero balance in it. Revalidate it for possible late order payment against meta_data.
"used" - order was placed and this address and payment in full was received. Address will not be used again.
"xused" - address was used (touched with funds) by unknown entity outside of this application. No metadata is present for this address, will not be able to correlated it with any order.
"unknown" - new address was generated but cannot retrieve balance due to blockchain API failure.
*/
function WCP__create_database_tables()
{
$create_tables = array('TableFAIR', 'TableBTC');
foreach ($create_tables as $table) {
$table::create_database_tables();
}
}
//==============================================================================
// NOTE: Irreversibly deletes all plugin tables and data
function WCP__delete_database_tables()
{
$create_tables = array('TableFAIR', 'TableBTC');
foreach ($create_tables as $table) {
$table::delete_database_tables();
}
}