-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbillplzWoo.php
240 lines (202 loc) · 8.36 KB
/
billplzWoo.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
<?php
/**
* Plugin Name: Billplz for WooCommerce
* Plugin URI: https://wordpress.org/plugins/billplz-for-woocommerce/
* Description: Billplz. Fair payment platform.
* Author: Billplz Sdn Bhd
* Author URI: http://github.com/billplz/billplz-for-woocommerce
* Version: 3.28.10
* Requires PHP: 7.0
* Requires at least: 4.6
* Requires Plugins: woocommerce
* License: GPLv3
* Text Domain: bfw
* Domain Path: /languages/
* WC requires at least: 3.0
* WC tested up to: 9.1.4
*/
defined('ABSPATH') || exit;
class Woocommerce_Billplz {
private static $instance;
public $notices = array();
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
private function __clone() {}
protected function __construct() {
$this->define_constants();
add_action('admin_init', array(&$this, 'check_environment'));
add_action('admin_notices', array(&$this, 'admin_notices'), 15);
add_action('plugins_loaded', array(&$this, 'init'));
add_filter('option_woocommerce_billplz_settings', array(&$this, 'patch_keys_constant'), 10, 2);
add_action('before_woocommerce_init', array(&$this, 'wc_hpos_compatibility'));
add_action('woocommerce_blocks_loaded', array(&$this, 'blocks_support'));
}
private function define_constants() {
$this->define( 'BFW_MIN_WOOCOMMERCE_VER', '3.0' );
$this->define( 'BFW_MIN_PHP_VER', '7.0' );
$this->define( 'BFW_PLUGIN_FILE', __FILE__ );
$this->define( 'BFW_PLUGIN_URL', plugin_dir_url(BFW_PLUGIN_FILE));
$this->define( 'BFW_PLUGIN_DIR', dirname(BFW_PLUGIN_FILE) );
$this->define( 'BFW_BASENAME', plugin_basename(BFW_PLUGIN_FILE) );
$this->define( 'BFW_PLUGIN_VER', '3.28.10' );
}
public function check_environment() {
$environment_warning = self::get_environment_warning();
if ($environment_warning && is_plugin_active(BFW_BASENAME)) {
deactivate_plugins(BFW_BASENAME);
$this->add_admin_notice('bad_environment', 'error', $environment_warning);
if (isset($_GET['activate'])) {
unset($_GET['activate']);
}
}
$is_woocommerce_active = class_exists('WooCommerce');
if (is_admin() && current_user_can('activate_plugins') && !$is_woocommerce_active) {
$this->add_admin_notice('prompt_bfw_activate', 'error', sprintf(__('<strong>Activation Error:</strong> You must have the <a href="%s" target="_blank">WooCommerce</a> plugin installed and activated for Billplz to activate.', 'bfw'), 'https://woocommerce.com'));
deactivate_plugins(BFW_BASENAME);
if (isset($_GET['activate'])) {
unset($_GET['activate']);
}
return false;
}
if (defined('WC_VERSION') && version_compare(WC_VERSION, BFW_MIN_WOOCOMMERCE_VER, '<')) {
$this->add_admin_notice('prompt_woocommerce_version_update', 'error', sprintf(__('<strong>Activation Error:</strong> You must have the <a href="%s" target="_blank">WooCommerce</a> core version %s+ for the Billplz for WooCommerce add-on to activate.', 'bfw'), 'https://woocommerce.com', BFW_MIN_WOOCOMMERCE_VER));
deactivate_plugins(BFW_BASENAME);
if (isset($_GET['activate'])) {
unset($_GET['activate']);
}
return false;
}
}
public function init() {
if (self::get_environment_warning()) {
return;
}
$this->load_plugin_textdomain();
$this->includes();
}
public function patch_keys_constant($value, $option_name){
if ($option_name != 'woocommerce_billplz_settings'){
return $value;
}
if (defined('BFW_API_KEY')) {
$value['api_key'] = BFW_API_KEY;
}
if (defined('BFW_COLLECTION_ID')) {
$value['collection_id'] = BFW_COLLECTION_ID;
}
if (defined('BFW_PAYMENT_ORDER_COLLECTION_ID')) {
$value['payment_order_collection_id'] = BFW_PAYMENT_ORDER_COLLECTION_ID;
}
if (defined('BFW_X_SIGNATURE')) {
$value['x_signature'] = BFW_X_SIGNATURE;
}
return $value;
}
public function load_plugin_textdomain()
{
load_plugin_textdomain('bfw', false, BFW_PLUGIN_DIR . '/languages/');
}
public function add_admin_notice($slug, $class, $message)
{
$this->notices[$slug] = array(
'class' => $class,
'message' => $message,
);
}
public function admin_notices()
{
$allowed_tags = array(
'a' => array(
'href' => array(),
'title' => array(),
'class' => array(),
'id' => array()
),
'br' => array(),
'em' => array(),
'span' => array(
'class' => array(),
),
'strong' => array(),
);
foreach ((array) $this->notices as $notice_key => $notice) {
echo "<div class='" . esc_attr($notice['class']) . "'><p>";
echo wp_kses($notice['message'], $allowed_tags);
echo '</p></div>';
}
}
private function includes() {
if (!class_exists('WooCommerce')) {
return false;
}
// database model must load first to be used by others
include BFW_PLUGIN_DIR . '/database/model.php';
include BFW_PLUGIN_DIR . '/database/upgrade.php';
if (is_admin()) {
include BFW_PLUGIN_DIR . '/includes/admin/bfw_action_links.php';
include BFW_PLUGIN_DIR . '/includes/admin/bfw_delete_order.php';
include BFW_PLUGIN_DIR . '/includes/admin/bfw_requery_meta_box.php';
include BFW_PLUGIN_DIR . '/includes/admin/bfw_requery_button.php';
include BFW_PLUGIN_DIR . '/includes/admin/bfw_requery_button_in_order_page.php';
}
// ensure are able to be loaded regardless of admin status as it may loaded with or without admin flag
include BFW_PLUGIN_DIR . '/includes/admin/bfw_settings.php';
include BFW_PLUGIN_DIR . '/includes/helpers/billplz_api.php';
include BFW_PLUGIN_DIR . '/includes/helpers/billplz_wpconnect.php';
include BFW_PLUGIN_DIR . '/includes/helpers/billplz_payment_option.php';
include BFW_PLUGIN_DIR . '/includes/wc_billplz_gateway.php';
include BFW_PLUGIN_DIR . '/includes/wc_bill_inquiry.php';
include BFW_PLUGIN_DIR . '/includes/bfw_order_refund.php';
}
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
public static function activation_check() {
$environment_warning = self::get_environment_warning(true);
if ($environment_warning) {
deactivate_plugins(BFW_BASENAME);
wp_die($environment_warning);
}
}
public static function get_environment_warning($during_activation = false) {
if (version_compare(phpversion(), BFW_MIN_PHP_VER, '<')) {
if ($during_activation) {
$message = __('The plugin could not be activated. The minimum PHP version required for this plugin is %1$s. You are running %2$s. Please contact your web host to upgrade your server\'s PHP version.', 'bfw');
} else {
$message = __('The plugin has been deactivated. The minimum PHP version required for this plugin is %1$s. You are running %2$s.', 'bfw');
}
return sprintf($message, BFW_MIN_PHP_VER, phpversion());
}
if (!class_exists('WC_Payment_Gateway')) {
if ($during_activation) {
return __('The plugin could not be activated. Billplz for WooCommerce depends on the last version of <a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> to work.', 'bfw');
}
return __('The plugin has been deactivated. Billplz for WooCommerce depends on the last version of <a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> to work.', 'bfw');
}
return false;
}
public function wc_hpos_compatibility() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
public function blocks_support() {
if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
include BFW_PLUGIN_DIR . '/includes/wc_billplz_blocks_support.php';
add_action(
'woocommerce_blocks_payment_method_type_registration',
function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
$payment_method_registry->register( new WC_Billplz_Blocks_Support() );
}
);
}
}
}
$GLOBALS['woocommerce_billplz'] = Woocommerce_Billplz::get_instance();
register_activation_hook(__FILE__, array('Woocommerce_Billplz', 'activation_check'));