-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.php
executable file
·149 lines (122 loc) · 4.84 KB
/
index.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
<?php
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
/*
* Plugin Name: KOMOJU Payments
* Plugin URI: https://github.com/komoju/komoju-woocommerce
* Description: Extends WooCommerce with KOMOJU gateway.
* Author: KOMOJU
* Author URI: https://komoju.com
* Version: 3.1.5
* WC requires at least: 6.0
* WC tested up to: 9.4.1
*/
add_action('before_woocommerce_init', function () {
if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('cart_checkout_blocks', __FILE__, true);
Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
}
});
add_action('plugins_loaded', 'woocommerce_komoju_init', 0);
function woocommerce_komoju_init()
{
/*
* Localisation
*/
load_plugin_textdomain('komoju-woocommerce', false, dirname(plugin_basename(__FILE__)) . '/languages');
/**
* Add the Gateway to WooCommerce
**/
function woocommerce_add_komoju_gateway($methods)
{
require_once 'class-wc-gateway-komoju.php';
require_once 'includes/class-wc-gateway-komoju-single-slug.php';
$methods[] = new WC_Gateway_Komoju();
$komoju_payment_methods = get_option('komoju_woocommerce_payment_methods');
if (gettype($komoju_payment_methods) == 'array') {
foreach ($komoju_payment_methods as $payment_method) {
$methods[] = new WC_Gateway_Komoju_Single_Slug($payment_method);
}
}
return $methods;
}
/**
* Add the KOMOJU settings page to WooCommerce
**/
function woocommerce_add_komoju_settings_page($settings)
{
require_once 'class-wc-gateway-komoju.php';
require_once 'class-wc-settings-page-komoju.php';
$settings[] = new WC_Settings_Page_Komoju();
return $settings;
}
/**
* Add the KOMOJU Fields JS
**/
function woocommerce_komoju_load_scripts()
{
if (!is_checkout()) {
return;
}
$komoju_fields_js = get_option('komoju_woocommerce_fields_url');
if (!$komoju_fields_js) {
$komoju_fields_js = 'https://multipay.komoju.com/fields.js';
}
wp_enqueue_script('komoju-fields', $komoju_fields_js);
}
function woocommerce_komoju_load_script_as_module($tag, $handle, $src)
{
if ($handle !== 'komoju-fields') {
return $tag;
}
return '<script type="module" src="' . esc_attr($src) . '"></script>';
}
function woocommerce_komoju_handle_http_request()
{
// Force WC to load our gateway, causing WC_Gateway_Komoju_IPN_Handler to get instantiated.
WC()->payment_gateways()->payment_gateways();
// When WC_Gateway_Komoju_IPN_Handler is instantiated, this filter should be registered.
$handled = apply_filters('invoke_komoju_ipn_handler', false);
// Catch unexpected case where the filter is NOT registered
if (!$handled) {
header('X-Komoju-Error: komoju gateway not loaded');
wp_die(
'gateway (and thus IPN handler) not loaded',
'KOMOJU WooCommerce plugin',
['status' => 500]
);
}
}
add_filter('woocommerce_payment_gateways', 'woocommerce_add_komoju_gateway');
add_filter('woocommerce_get_settings_pages', 'woocommerce_add_komoju_settings_page');
add_action('woocommerce_api_wc_gateway_komoju', 'woocommerce_komoju_handle_http_request');
add_action('wp_enqueue_scripts', 'woocommerce_komoju_load_scripts');
add_filter('script_loader_tag', 'woocommerce_komoju_load_script_as_module', 10, 3);
add_action('plugins_loaded', 'woocommerce_komoju_blocks');
function woocommerce_komoju_blocks()
{
if (!class_exists('WC_Payment_Gateway')) {
return;
}
require_once 'includes/class-wc-gateway-komoju-block.php';
}
add_action('woocommerce_blocks_loaded', 'register_komoju_payment_method_type');
function register_komoju_payment_method_type()
{
if (!class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
return;
}
add_action(
'woocommerce_blocks_payment_method_type_registration',
function (PaymentMethodRegistry $payment_method_registry) {
$gateways = WC()->payment_gateways()->payment_gateways();
if ($gateways) {
foreach ($gateways as $gateway) {
if ($gateway->enabled == 'yes' && $gateway instanceof WC_Gateway_Komoju_Single_Slug) {
$payment_method_registry->register(new WC_Gateway_Komoju_Blocks($gateway));
}
}
}
}
);
}
}