/
blockonomics-woocommerce.php
executable file
·600 lines (558 loc) · 30 KB
/
blockonomics-woocommerce.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
/**
* Plugin Name: WordPress Bitcoin Payments - Blockonomics
* Plugin URI: https://github.com/blockonomics/woocommerce-plugin
* Description: Accept Bitcoin Payments on your WooCommerce-powered website with Blockonomics
* Version: 3.2
* Author: Blockonomics
* Author URI: https://www.blockonomics.co
* License: MIT
* Text Domain: blockonomics-bitcoin-payments
* Domain Path: /languages/
* WC tested up to: 4.7.1
*/
/* Copyright 2017 Blockonomics Inc.
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
require_once ABSPATH . 'wp-admin/includes/plugin.php';
/**
* Initialize hooks needed for the payment gateway
*/
function blockonomics_woocommerce_init()
{
if (!class_exists('WC_Payment_Gateway')) {
return;
}
require_once plugin_dir_path(__FILE__) . 'php' . DIRECTORY_SEPARATOR . 'WC_Gateway_Blockonomics.php';
include_once plugin_dir_path(__FILE__) . 'php' . DIRECTORY_SEPARATOR . 'Blockonomics.php';
add_action('admin_menu', 'add_page');
add_action('init', 'woocommerce_handle_blockonomics_return');
add_action('init', 'load_plugin_translations');
add_action('woocommerce_order_details_after_order_table', 'nolo_custom_field_display_cust_order_meta', 10, 1);
add_action('woocommerce_email_customer_details', 'nolo_bnomics_woocommerce_email_customer_details', 10, 1);
add_action('admin_enqueue_scripts', 'blockonomics_load_admin_scripts' );
add_action('restrict_manage_posts', 'filter_orders' , 20 );
add_filter('request', 'filter_orders_by_address_or_txid' );
add_filter('woocommerce_payment_gateways', 'woocommerce_add_blockonomics_gateway');
add_filter('clean_url', 'bnomics_async_scripts', 11, 1 );
/**
* Add Styles to Blockonomics Admin Page
**/
function blockonomics_load_admin_scripts($hook){
if ( $hook === 'settings_page_blockonomics_options') {
wp_enqueue_style('bnomics-admin-style', plugin_dir_url(__FILE__) . "css/blockonomics_options.css", '', get_plugin_data( __FILE__ )['Version']);
}
}
/**
* Adding new filter to WooCommerce orders
**/
function filter_orders() {
global $typenow;
if ( 'shop_order' === $typenow ) {
?>
<input size='26' value="<?php if(isset( $_GET['filter_by'] )) echo(esc_attr($_GET['filter_by'])); ?>" type='name' placeholder='Filter by crypto address/txid' name='filter_by'>
<?php
}
}
function filter_orders_by_address_or_txid( $vars ) {
global $typenow;
if ( 'shop_order' === $typenow && isset( $_GET['filter_by'] ) && ! empty( $_GET['filter_by'])){
$vars['meta_value'] = wc_clean( $_GET['filter_by'] );
}
return $vars;
}
/**
* Add this Gateway to WooCommerce
**/
function woocommerce_add_blockonomics_gateway($methods)
{
$methods[] = 'WC_Gateway_Blockonomics';
return $methods;
}
function load_plugin_translations()
{
load_plugin_textdomain('blockonomics-bitcoin-payments', false, dirname(plugin_basename(__FILE__)) . '/languages/');
}
function woocommerce_handle_blockonomics_return()
{
if (!isset($_GET['return_from_blockonomics'])) {
return;
}
if (isset($_GET['cancelled'])) {
$order = new WC_Order($_GET['order']['custom']);
if ($order->status != 'completed') {
$order->update_status('failed', __('Customer cancelled blockonomics payment', 'blockonomics-bitcoin-payments'));
}
}
// Blockonomics order param interferes with woocommerce
unset($_GET['order']);
unset($_REQUEST['order']);
if (isset($_GET['order_key'])) {
$_GET['order'] = $_GET['order_key'];
}
}
// Add entry in the settings menu
function add_page()
{
$blockonomics = new Blockonomics;
if (isset($_POST['generateSecret']))
{
generate_secret(true);
}
$api_key = $blockonomics->get_api_key();
// get_api_key() will return api key or temp api key
// if both are null, generate new blockonomics guest account with temporary wallet
// temp wallet will be used with temp api key
if (!$api_key)
{
generate_secret();
$callback_url = get_callback_url();
$response = $blockonomics->get_temp_api_key($callback_url);
if ($response->response_code != 200)
{
$message = __('Error while generating temporary APIKey: '. isset($response->message) ? $response->message : '', 'blockonomics-bitcoin-payments');
display_admin_message($message, 'error');
}
else
{
update_option("blockonomics_temp_api_key", isset($response->apikey) ? $response->apikey : '');
}
}
add_options_page(
'Blockonomics', 'Blockonomics', 'manage_options',
'blockonomics_options', 'show_options'
);
}
function display_admin_message($msg, $type)
{
add_settings_error('option_notice', 'option_notice', $msg, $type);
}
function get_started_message($domain = '', $label_class = 'bnomics-options-intendation', $message = 'To configure')
{
echo
"<label class=$label_class>".
__("$message, click <b> Get Started for Free </b> on ", 'blockonomics-bitcoin-payments').
'<a href="https://'.$domain.'blockonomics.co/merchants" target="_blank">'.
__('https://'.$domain.'blockonomics.co/merchants', 'blockonomics-bitcoin-payments').
'</a>
</label>';
}
function success_message()
{
echo '<td colspan="2"class="notice notice-success bnomics-test-setup-message">'.__("Success", 'blockonomics-bitcoin-payments').'</td>';
}
function error_message($error)
{
echo
'<td colspan="2" class="notice notice-error bnomics-test-setup-message">'.$error.'.<br/>'.
__("Please consult ", 'blockonomics-bitcoin-payments').
'<a href="http://help.blockonomics.co/support/solutions/articles/33000215104-unable-to-generate-new-address" target="_blank">'.
__("this troubleshooting article", 'blockonomics-bitcoin-payments').'</a>.
</td>';
}
function generate_secret($force_generate = false)
{
$callback_secret = get_option("blockonomics_callback_secret");
if (!$callback_secret || $force_generate) {
$callback_secret = sha1(openssl_random_pseudo_bytes(20));
update_option("blockonomics_callback_secret", $callback_secret);
}
}
function get_callback_url()
{
$callback_secret = get_option('blockonomics_callback_secret');
$callback_url = WC()->api_request_url('WC_Gateway_Blockonomics');
$callback_url = add_query_arg('secret', $callback_secret, $callback_url);
return $callback_url;
}
function show_options()
{
if (isset($_GET['tab']) && $_GET['tab'] == "currencies" && isset($_GET['settings-updated']) ? $_GET['settings-updated'] : '' == 'true')
{
$blockonomics = new Blockonomics;
$setup_errors = $blockonomics->testSetup();
$btc_error = isset($setup_errors['btc']) ? $setup_errors['btc'] : 'false';
$bch_error = isset($setup_errors['bch']) ? $setup_errors['bch'] : 'false';
$withdraw_requested = $blockonomics->make_withdraw();
}
?>
<script type="text/javascript">
function gen_secret() {
document.generateSecretForm.submit();
}
function check_form(tab) {
const urlParams = new URLSearchParams(window.location.href);
const currentTab = urlParams.get('tab') ?? 'settings';
if (currentTab == tab){
return;
}
if (document.getElementById('blockonomics_form_updated').value == 'true' || document.getElementById('blockonomics_api_updated').value == 'true'){
if(validateBlockonomicsForm()){
save_form_then_redirect(tab);
}
} else {
window.location.href = "options-general.php?page=blockonomics_options&tab="+tab;
}
}
function save_form_then_redirect(tab) {
const xhr = new XMLHttpRequest();
xhr.open("POST", "options.php");
xhr.onload = function(event){
window.location.href = "options-general.php?page=blockonomics_options&tab="+tab;
};
const formData = new FormData(document.myform);
xhr.send(formData);
document.getElementById('myform').innerHTML = "Saving Settings...";
}
function value_changed() {
document.getElementById('blockonomics_api_updated').value = 'true';
add_asterisk("settings");
}
function add_asterisk(tab) {
document.getElementById('blockonomics_form_updated').value = 'true';
document.getElementById(tab+'_nav_bar').style.background = "#e6d9cb";
document.getElementById(tab+'_nav_bar').textContent = tab.charAt(0).toUpperCase() + tab.slice(1)+"*";
}
function validateBlockonomicsForm() {
if(document.getElementById("blockonomics_api_key")){
newApiKey = document.getElementById("blockonomics_api_key").value;
apiKeyChanged = newApiKey != "<?php echo get_option("blockonomics_api_key")?>";
if (apiKeyChanged && newApiKey.length != 43) {
alert("ERROR: Invalid APIKey");
return false
}
}
return true;
}
function show_advanced() {
document.getElementById("advanced_title").style.display = 'none';
document.getElementById("advanced_window").style.display = 'block';
}
function show_basic() {
document.getElementById("advanced_title").style.display = 'block';
document.getElementById("advanced_window").style.display = 'none';
}
</script>
<div class="wrap">
<h1><?php echo __('Blockonomics', 'blockonomics-bitcoin-payments')?></h1>
<?php
if (isset($withdraw_requested)):?>
<div class="bnomics-width-withdraw">
<td colspan='2' class="bnomics-options-no-padding bnomics-width">
<p class='notice notice-<?php echo $withdraw_requested[1]?>'>
<?php echo $withdraw_requested[0].'.' ?>
</p>
</td>
</div>
<?php endif; ?>
<?php
if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
} else {
$active_tab = 'settings';
}
?>
<form method="post" name="myform" id="myform" onsubmit="return validateBlockonomicsForm()" action="options.php">
<h2 class="nav-tab-wrapper">
<a onclick="check_form('settings')" id='settings_nav_bar' class="nav-tab <?php echo $active_tab == 'settings' ? 'nav-tab-active' : ''; ?>"><?php echo __('Settings', 'blockonomics-bitcoin-payments')?></a>
<a onclick="check_form('currencies')" id='currencies_nav_bar' class="nav-tab <?php echo $active_tab == 'currencies' ? 'nav-tab-active' : ''; ?>"><?php echo __('Currencies', 'blockonomics-bitcoin-payments')?></a>
</h2>
<input type="hidden" name="blockonomics_form_updated" id="blockonomics_form_updated" value="false">
<input type="hidden" name="blockonomics_api_updated" id="blockonomics_api_updated" value="false">
<?php wp_nonce_field('update-options');
switch ( $active_tab ){
case 'settings' :?>
<div class="bnomics-width">
<h4><?php echo __('API Key', 'blockonomics-bitcoin-payments')?></h4>
<input class="bnomics-options-input" onchange="value_changed()" size="130" type="text" id="blockonomics_api_key" name="blockonomics_api_key" value="<?php echo get_option('blockonomics_api_key'); ?>" />
<?php get_started_message('', '', 'To get your API Key');?>
<h4><?php echo __('Callback URL', 'blockonomics-bitcoin-payments')?>
<a href="javascript:gen_secret()" id="generate-callback" class="bnomics-options-callback-icon" title="Generate New Callback URL"></a>
</h4>
<input class="bnomics-options-input" size="130" type="text" value="<?php echo get_callback_url();?>" disabled/>
<p id="advanced_title" class="bnomics-options-bold"><a href="javascript:show_advanced()"><?php echo __('Advanced Settings', 'blockonomics-bitcoin-payments')?> ▼</a></p>
<div id="advanced_window" style="display:none">
<p class="bnomics-options-bold"><a href="javascript:show_basic()"><?php echo __('Advanced Settings', 'blockonomics-bitcoin-payments')?> ▲</a></p>
<table class="form-table">
<tr valign="top"><th scope="row"><?php echo __('Time period of countdown timer on payment page (in minutes)', 'blockonomics-bitcoin-payments')?></th>
<td>
<select onchange="add_asterisk('settings')" name="blockonomics_timeperiod" />
<option value="10" <?php selected(get_option('blockonomics_timeperiod'), 10); ?>>10</option>
<option value="15" <?php selected(get_option('blockonomics_timeperiod'), 15); ?>>15</option>
<option value="20" <?php selected(get_option('blockonomics_timeperiod'), 20); ?>>20</option>
<option value="25" <?php selected(get_option('blockonomics_timeperiod'), 25); ?>>25</option>
<option value="30" <?php selected(get_option('blockonomics_timeperiod'), 30); ?>>30</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Extra Currency Rate Margin % (Increase live fiat to BTC rate by small percent)', 'blockonomics-bitcoin-payments')?></th>
<td><input onchange="add_asterisk('settings')" type="number" min="0" max="4" step="0.01" name="blockonomics_margin" value="<?php echo esc_attr( get_option('blockonomics_margin', 0) ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Underpayment Slack % (Allow payments that are off by a small percentage)', 'blockonomics-bitcoin-payments')?></th>
<td><input onchange="add_asterisk('settings')" type="number" min="0" max="10" step="0.01" name="blockonomics_underpayment_slack" value="<?php echo esc_attr( get_option('blockonomics_underpayment_slack', 0) ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Display Payment Page in Lite Mode (Enable this if you are having problems in rendering checkout page)', 'blockonomics-bitcoin-payments')?></th>
<td><input onchange="add_asterisk('settings')" type="checkbox" name="blockonomics_lite" value="1" <?php checked("1", get_option('blockonomics_lite')); ?> /></td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('No Javascript checkout page (Enable this if you have majority customer that use tor like browser that block Javascript)', 'blockonomics-bitcoin-payments')?></th>
<td><input onchange="add_asterisk('settings')" type="checkbox" name="blockonomics_nojs" value="1" <?php checked("1", get_option('blockonomics_nojs')); ?> /></td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Network Confirmations required for payment to complete)', 'blockonomics-bitcoin-payments')?></th>
<td><select onchange="add_asterisk('settings')" name="blockonomics_network_confirmation">
<option value="2" <?php selected(get_option('blockonomics_network_confirmation'), 2); ?>><?php echo __('2 (Recommended)', 'blockonomics-bitcoin-payments')?></option>
<option value="1" <?php selected(get_option('blockonomics_network_confirmation'), 1); ?>>1</option>
<option value="0" <?php selected(get_option('blockonomics_network_confirmation'), 0); ?>>0</option>
</select></td>
</tr>
</table>
</div>
<p class="submit">
<input type="submit" class="button-primary" value="<?php echo __("Save", 'blockonomics-bitcoin-payments')?>"/>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="blockonomics_api_key,blockonomics_timeperiod,blockonomics_margin,blockonomics_gen_callback,blockonomics_api_updated,blockonomics_underpayment_slack,blockonomics_lite,blockonomics_nojs,blockonomics_network_confirmation" />
</p>
</form>
<form method="POST" name="generateSecretForm">
<p class="submit">
<input type="hidden" name="generateSecret" value="true">
</p>
</form>
</div>
<?php
break;
case 'currencies' :?>
<table width="100%" cellspacing="0" cellpadding="0" class="form-table bnomics-options-intendation bnomics-width">
<h2>
<input onchange="add_asterisk('currencies')" type="checkbox" name="blockonomics_btc" value="1"<?php checked("1", get_option('blockonomics_btc', true)); ?>" />
<?php echo __('Bitcoin (BTC)', 'blockonomics-bitcoin-payments')?>
</h2>
<?php
get_started_message();
$btc_enabled = get_option("blockonomics_btc");
if ($btc_enabled || get_option("blockonomics_btc") === false):
$total_received = get_option('blockonomics_temp_withdraw_amount') / 1.0e8;
$api_key = get_option("blockonomics_api_key");
$temp_api_key = get_option("blockonomics_temp_api_key");
if ($temp_api_key): ?>
<th class="blockonomics-narrow-th" scope="row"><b><?php echo __('Temporary Destination', 'blockonomics-bitcoin-payments')?></b></th>
<td colspan="2" class="bnomics-options-no-padding">
<label><b><?php echo __("Blockonomics Wallet (Balance: $total_received BTC)", 'blockonomics-bitcoin-payments')?></b></label>
<label><?php echo __("Our temporary wallet receives your payments until your configure your own wallet. Withdraw to your wallet is triggered automatically when configuration is done", 'blockonomics-bitcoin-payments')?></label>
</td>
</tr>
<?php endif;
endif;
if (get_option('blockonomics_btc') == '1' && isset($btc_error)):
if ($btc_error):
error_message($btc_error);
else:
success_message();
endif;
endif; ?>
</table>
<table class="form-table bnomics-options-intendation bnomics-width">
<h2>
<input onchange="add_asterisk('currencies')" type="checkbox" name="blockonomics_bch" value="1"<?php checked("1", get_option('blockonomics_bch')); ?>" />
<?php echo __("Bitcoin Cash (BCH)", 'blockonomics-bitcoin-payments')?>
</h2>
<?php
get_started_message('bch.');
$bch_enabled = get_option("blockonomics_bch");
if ($bch_enabled == '1' && isset($bch_error)):
if ($bch_error):
error_message($bch_error);
else:
success_message();
endif;
endif; ?>
</table>
<div class="bnomics-options-small-margin-top">
<input type="submit" class="button-primary" value="<?php echo __("Test Setup", 'blockonomics-bitcoin-payments')?>" />
<input type="hidden" name="page_options" value="blockonomics_bch, blockonomics_btc" />
<input type="hidden" name="action" value="update" />
</div>
</form>
<?php
break;
}
?>
</div>
<?php
}
function bnomics_display_tx_info($order, $email=false)
{
$blockonomics = new Blockonomics();
$active_cryptos = $blockonomics->getActiveCurrencies();
foreach ($active_cryptos as $crypto) {
$txid = get_post_meta($order->get_id(), 'blockonomics_'.$crypto['code'].'_txid', true);
$address = get_post_meta($order->get_id(), $crypto['code'].'_address', true);
if ($txid && $address) {
if ($crypto['code'] == 'btc') {
$base_url = Blockonomics::BASE_URL;
}else{
$base_url = Blockonomics::BCH_BASE_URL;
}
echo '<b>'.__('Payment Details', 'blockonomics-bitcoin-payments').'</b><p><strong>'.__('Transaction', 'blockonomics-bitcoin-payments').':</strong> <a href =\''. $base_url ."/api/tx?txid=$txid&addr=$address'>".substr($txid, 0, 10). '</a></p>';
if (!$email) {
echo '<p>'.__('Your order will be processed on confirmation of above transaction by the bitcoin network.', 'blockonomics-bitcoin-payments').'</p>';
}
}
}
}
function nolo_custom_field_display_cust_order_meta($order)
{
bnomics_display_tx_info($order);
}
function nolo_bnomics_woocommerce_email_customer_details($order)
{
bnomics_display_tx_info($order, true);
}
function bnomics_enqueue_stylesheets(){
wp_enqueue_style('bnomics-style', plugin_dir_url(__FILE__) . "css/order.css", '', get_plugin_data( __FILE__ )['Version']);
}
function bnomics_enqueue_scripts(){
wp_enqueue_script( 'angular', plugins_url('js/angular.min.js#deferload', __FILE__), '', get_plugin_data( __FILE__ )['Version'] );
wp_enqueue_script( 'angular-resource', plugins_url('js/angular-resource.min.js#deferload', __FILE__), '', get_plugin_data( __FILE__ )['Version'] );
wp_enqueue_script( 'app', plugins_url('js/app.js#deferload', __FILE__), '', get_plugin_data( __FILE__ )['Version'] );
wp_enqueue_script( 'angular-qrcode', plugins_url('js/angular-qrcode.js#deferload', __FILE__), '', get_plugin_data( __FILE__ )['Version'] );
wp_enqueue_script( 'vendors', plugins_url('js/vendors.min.js#deferload', __FILE__), '', get_plugin_data( __FILE__ )['Version'] );
wp_enqueue_script( 'reconnecting-websocket', plugins_url('js/reconnecting-websocket.min.js#deferload', __FILE__), '', get_plugin_data( __FILE__ )['Version'] );
}
// Async load
function bnomics_async_scripts($url)
{
if ( strpos( $url, '#deferload') === false )
return $url;
else if ( is_admin() )
return str_replace( '#deferload', '', $url );
else
return str_replace( '#deferload', '', $url )."' defer='defer";
}
}
// After all plugins have been loaded, initialize our payment gateway plugin
add_action('plugins_loaded', 'blockonomics_woocommerce_init', 0);
register_activation_hook( __FILE__, 'blockonomics_activation_hook' );
add_action('admin_notices', 'blockonomics_plugin_activation');
global $blockonomics_db_version;
$blockonomics_db_version = '1.0';
function blockonomics_create_table() {
// Create blockonomics_orders table
// https://codex.wordpress.org/Creating_Tables_with_Plugins
global $wpdb;
global $blockonomics_db_version;
$table_name = $wpdb->prefix . 'blockonomics_orders';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
order_id int NOT NULL,
status int NOT NULL,
crypto varchar(3) NOT NULL,
address varchar(191) NOT NULL,
timestamp int,
time_remaining int,
satoshi int,
currency varchar(3),
value longtext,
txid text,
PRIMARY KEY (address),
KEY orderkey (order_id,crypto)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
update_option( 'blockonomics_db_version', $blockonomics_db_version );
}
function blockonomics_activation_hook() {
if(!is_plugin_active('woocommerce/woocommerce.php'))
{
trigger_error(__( 'Wordpress Bitcoin Payments - Blockonomics requires WooCommerce plugin to be installed and active.', 'blockonomics-bitcoin-payments' ).'<br>', E_USER_ERROR);
}
set_transient( 'blockonomics_activation_hook_transient', true, 3);
}
// Since WP 3.1 the activation function registered with register_activation_hook() is not called when a plugin is updated.
function blockonomics_update_db_check() {
global $wpdb;
global $blockonomics_db_version;
$installed_ver = get_site_option( 'blockonomics_db_version' );
if ( $installed_ver != $blockonomics_db_version ) {
// Example function to demonstrate table changes between upgrade versions
// if ($installed_ver < 1.0) {
// $wpdb->query("ALTER TABLE $table_name DROP transaction;");
// }
blockonomics_create_table();
}
}
add_action( 'plugins_loaded', 'blockonomics_update_db_check' );
register_activation_hook( __FILE__, 'blockonomics_create_table' );
//Show message when plugin is activated
function blockonomics_plugin_activation() {
if(!is_plugin_active('woocommerce/woocommerce.php'))
{
$html = '<div class="error">';
$html .= '<p>';
$html .= __( 'Wordpress Bitcoin Payments - Blockonomics failed to load. Please activate WooCommerce plugin.', 'blockonomics-bitcoin-payments' );
$html .= '</p>';
$html .= '</div>';
echo $html;
}
if( get_transient( 'blockonomics_activation_hook_transient' ) ){
$html = '<div class="updated">';
$html .= '<p>';
$html .= __( 'Congrats, you are now accepting BTC payments! You can configure Blockonomics <a href="options-general.php?page=blockonomics_options">on this page</a>.', 'blockonomics-bitcoin-payments' );
$html .= '</p>';
$html .= '</div>';
echo $html;
delete_transient( 'fx-admin-notice-example' );
}
}
// On uninstallation, clear every option the plugin has set
register_uninstall_hook( __FILE__, 'blockonomics_uninstall_hook' );
function blockonomics_uninstall_hook() {
delete_option('blockonomics_callback_secret');
delete_option('blockonomics_api_key');
delete_option('blockonomics_temp_api_key');
delete_option('blockonomics_temp_withdraw_amount');
delete_option('blockonomics_margin');
delete_option('blockonomics_timeperiod');
delete_option('blockonomics_api_updated');
delete_option('blockonomics_bch');
delete_option('blockonomics_btc');
delete_option('blockonomics_underpayment_slack');
delete_option('blockonomics_lite');
delete_option('blockonomics_nojs');
delete_option('blockonomics_network_confirmation');
global $wpdb;
$table_name = $wpdb->prefix . 'blockonomics_orders';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
delete_option("blockonomics_db_version");
}
function blockonomics_plugin_add_settings_link( $links ) {
$settings_link = '<a href="options-general.php?page=blockonomics_options">' . __( 'Settings' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
$plugin = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$plugin", 'blockonomics_plugin_add_settings_link' );