-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeless-for-woocommerce.php
243 lines (203 loc) · 7.21 KB
/
nodeless-for-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
<?php
/**
* Plugin Name: Nodeless For Woocommerce
* Plugin URI: https://wordpress.org/plugins/nodelessio-for-woocommerce/
* Description: Nodeless.io is a bitcoin payment service which allows you to receive payments in Bitcoin.
* Author: Nodeless
* Author URI: https://nodeless.io
* Text Domain: nodeless-for-woocommerce
* Domain Path: /languages
* Version: 1.0.1
* Requires PHP: 8.0
* Tested up to: 6.1
* Requires at least: 5.6
* WC requires at least: 6.0.0
* WC tested up to: 7.4
*/
use NodelessIO\WC\Admin\Notice;
use NodelessIO\WC\Gateway\DefaultGateway;
use NodelessIO\WC\Helper\SatsMode;
use NodelessIO\WC\Helper\ApiHelper;
use NodelessIO\WC\Helper\Logger;
defined( 'ABSPATH' ) || exit();
define( 'NODELESS_VERSION', '1.0.1' );
define( 'NODELESS_VERSION_KEY', 'nodeless_version' );
define( 'NODELESS_PLUGIN_FILE_PATH', plugin_dir_path( __FILE__ ) );
define( 'NODELESS_PLUGIN_URL', plugin_dir_url(__FILE__ ) );
define( 'NODELESS_PLUGIN_ID', 'nodeless-for-woocommerce' );
class NodelessIOWCPlugin {
private static $instance;
public function __construct() {
$this->includes();
add_action('woocommerce_thankyou_nodeless', [$this, 'orderStatusThankYouPage'], 10, 1);
if (is_admin()) {
// Register our custom global settings page.
add_filter(
'woocommerce_get_settings_pages',
function ($settings) {
$settings[] = new \NodelessIO\WC\Admin\GlobalSettings();
return $settings;
}
);
$this->dependenciesNotification();
$this->notConfiguredNotification();
}
}
public function includes(): void {
$autoloader = NODELESS_PLUGIN_FILE_PATH . 'vendor/autoload.php';
if (file_exists($autoloader)) {
/** @noinspection PhpIncludeInspection */
require_once $autoloader;
}
// Make sure WP internal functions are available.
if ( ! function_exists('is_plugin_active') ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Setup other dependencies.
// Make SATS as currency available.
if (get_option('nodeless_sats_mode') === 'yes') {
SatsMode::instance();
}
}
public static function initPaymentGateways($gateways): array {
// We always load the default gateway that covers all payment methods available on NodelessIO.
$gateways[] = DefaultGateway::class;
return $gateways;
}
public static function enqueueScripts(): void {
// Load CSS.
wp_register_style('nodeless_payment', plugins_url('assets/css/nodeless-style.css',__FILE__ ));
wp_enqueue_style('nodeless_payment');
}
/**
* Displays notice (and link to config page) on admin dashboard if the plugin is not configured yet.
*/
public function notConfiguredNotification(): void {
if (!ApiHelper::getConfig()) {
$message = sprintf(
esc_html__(
'Plugin not configured yet, please %1$sconfigure the plugin here%2$s',
'nodeless-for-woocommerce'
),
'<a href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=nodeless_settings')) . '">',
'</a>'
);
Notice::addNotice('error', $message);
}
}
/**
* Checks and displays notice on admin dashboard if PHP version is too low or WooCommerce not installed.
*/
public function dependenciesNotification() {
// Check PHP version.
if ( version_compare( PHP_VERSION, '8.0', '<' ) ) {
$versionMessage = sprintf( __( 'Your PHP version is %s but Nodeless.io Payment plugin requires version 8.0+.', 'nodeless-for-woocommerce' ), PHP_VERSION );
Notice::addNotice('error', $versionMessage);
}
// Check if WooCommerce is installed.
if ( ! is_plugin_active('woocommerce/woocommerce.php') ) {
$wcMessage = __('WooCommerce seems to be not installed. Make sure you do before you activate NodelessIO Payment Gateway.', 'nodeless-for-woocommerce');
Notice::addNotice('error', $wcMessage);
}
// Check if PHP cURL is available.
if ( ! function_exists('curl_init') ) {
$curlMessage = __('The PHP cURL extension is not installed. Make sure it is available otherwise this plugin will not work.', 'nodeless-for-woocommerce');
Notice::addNotice('error', $curlMessage);
}
}
/**
* Override order thank you page.
*/
public function orderStatusThankYouPage($order_id)
{
if (!$order = wc_get_order($order_id)) {
return;
}
$title = _x('Payment Status', 'nodeless-for-woocommerce');
$orderData = $order->get_data();
$status = $orderData['status'];
switch ($status)
{
case 'on-hold':
$statusDesc = _x('Waiting for payment settlement', 'nodeless-for-woocommerce');
break;
case 'processing':
$statusDesc = _x('Payment successful, order processing.', 'nodeless-for-woocommerce');
break;
case 'completed':
$statusDesc = _x('Order completed', 'nodeless-for-woocommerce');
break;
case 'failed':
$statusDesc = _x('Payment failed', 'nodeless-for-woocommerce');
break;
default:
$statusDesc = _x(ucfirst($status), 'nodeless-for-woocommerce');
break;
}
echo "
<section class='woocommerce-order-payment-status'>
<h2 class='woocommerce-order-payment-status-title'>{$title}</h2>
<p><strong>{$statusDesc}</strong></p>
</section>
";
}
/**
* Gets the main plugin loader instance.
*
* Ensures only one instance can be loaded.
*/
public static function instance(): \NodelessIOWCPlugin {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
// Start everything up.
function init_nodeless() {
\NodelessIOWCPlugin::instance();
}
/**
* Bootstrap stuff on init.
*/
add_action('init', function() {
// Adding textdomain and translation support.
load_plugin_textdomain('nodeless-for-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
// Flush rewrite rules only once after activation.
if ( ! get_option('nodeless_permalinks_flushed') ) {
flush_rewrite_rules(false);
update_option('nodeless_permalinks_flushed', 1);
}
});
// Action links on plugin overview.
add_filter( 'plugin_action_links_nodeless-for-woocommerce/nodeless-for-woocommerce.php', function ( $links ) {
// Settings link.
$settings_url = esc_url( add_query_arg(
[
'page' => 'wc-settings',
'tab' => 'nodeless_settings'
],
get_admin_url() . 'admin.php'
) );
$settings_link = "<a href='$settings_url'>" . __( 'Settings', 'nodeless-for-woocommerce' ) . '</a>';
$logs_link = "<a target='_blank' href='" . Logger::getLogFileUrl() . "'>" . __('Debug log', 'nodeless-for-woocommerce') . "</a>";
//$docs_link = "<a target='_blank' href='". esc_url('https://docs.nodeless.io/WooCommerce/') . "'>" . __('Docs', 'nodeless-for-woocommerce') . "</a>";
$support_link = "<a target='_blank' href='". esc_url('https://chat.nodeless.io/') . "'>" . __('Support Chat', 'nodeless-for-woocommerce') . "</a>";
array_unshift(
$links,
$settings_link,
$logs_link,
//$docs_link,
$support_link
);
return $links;
} );
// Installation routine.
register_activation_hook( __FILE__, function() {
update_option('nodeless_permalinks_flushed', 0);
update_option( NODELESS_VERSION_KEY, NODELESS_VERSION );
});
// Initialize payment gateways and plugin.
add_filter( 'woocommerce_payment_gateways', [ 'NodelessIOWCPlugin', 'initPaymentGateways' ] );
add_action( 'plugins_loaded', 'init_nodeless', 0 );
add_action( 'wp_enqueue_scripts', [ 'NodelessIOWCPlugin', 'enqueueScripts' ] );