-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWC_Buckaroo_Settings_Page.php
491 lines (460 loc) · 18.7 KB
/
WC_Buckaroo_Settings_Page.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
<?php
if (!defined('ABSPATH')) {
exit;
}
if (class_exists('WC_Buckaroo_Settings_Page', false)) {
return new WC_Buckaroo_Settings_Page(
new WC_Gateway_Buckaroo_MasterSettings()
);
}
/**
* WC_Buckaroo_Settings_Page.
*/
class WC_Buckaroo_Settings_Page extends WC_Settings_Page
{
protected $gateway;
/**
* Constructor.
*/
public function __construct(WC_Settings_API $gateway)
{
$this->gateway = $gateway;
$this->id = 'buckaroo_settings';
$this->label = __('Buckaroo Settings', 'wc-buckaroo-bpe-gateway');
parent::__construct();
add_action(
'woocommerce_admin_field_buckaroo_payment_list', [$this, "render_payment_list"]
);
add_action(
'woocommerce_admin_field_buckaroo_button', [$this, "render_button_field"]
);
add_action(
'woocommerce_admin_field_buckaroo_hidden', [$this, "render_hidden_field"]
);
add_action(
'woocommerce_admin_field_buckaroo_file', [$this, "render_file_field"]
);
add_action(
'woocommerce_admin_field_buckaroo_submeniu', [$this, "render_submeniu_field"]
);
}
/**
* @inheritDoc
*/
protected function get_own_sections()
{
return array(
'' => __('General', 'wc-buckaroo-bpe-gateway'),
'methods' => __('Payment methods', 'wc-buckaroo-bpe-gateway'),
'report' => __('Report', 'wc-buckaroo-bpe-gateway')
);
}
/**
* Version lower than 5.5 section compatibility
*
* @return void
*/
public function get_sections()
{
return $this->get_own_sections();
}
/**
* @inheritDoc
*/
public function output()
{
global $current_section, $hide_save_button;
if (version_compare(WOOCOMMERCE_VERSION, '5.5.0', '<')) {
if ($current_section === '') {
WC_Admin_Settings::output_fields($this->get_settings_for_default_section());
}
}
parent::output();
if ($current_section === 'report') {
(new Buckaroo_Report_Page())->output_report();
$hide_save_button = true;
}
if ($current_section === 'methods') {
$this->render_gateway_list();
$hide_save_button = true;
}
if ($current_section === 'logs' && isset($_GET['log_file'])) {
(new Buckaroo_Report_Page())->display_log_file(
sanitize_text_field($_GET['log_file'])
);
$hide_save_button = true;
}
}
public function save()
{
if (version_compare(WOOCOMMERCE_VERSION, '5.5.0', '<')) {
$this->save_settings_for_current_section();
}
parent::save();
}
/**
* @inheritDoc
* */
public function save_settings_for_current_section()
{
global $current_section;
if ($current_section === '') {
$this->gateway->process_admin_options();
$this->getErrors();
//update certificate list with new values
$this->gateway->initCerificateFields();
}
}
/**
* Display any form validation errors to the page
*
* @return void
*/
public function getErrors()
{
$errors = $this->gateway->get_errors();
if (count($errors)) {
foreach ($errors as $error) {
WC_Admin_Settings::add_error($error);
}
}
}
/**
* @inheritDoc
* */
public function get_settings_for_default_section()
{
$settings = array_merge(
array(
array(
'title' => __(
'Buckaroo settings', 'wc-buckaroo-bpe-gateway'
),
'type'=>'buckaroo_submeniu',
'links'=> [
[
"name" => __('Documentation', 'wc-buckaroo-bpe-gateway'),
"href" => 'https://support.buckaroo.nl/categorieen/plugins/woocommerce'
],
[
"name" => __('FAQ', 'wc-buckaroo-bpe-gateway'),
"href" => 'https://support.buckaroo.nl/categorieen/plugins/woocommerce/faq-woocommerce'
]
]
),
array(
'type' => 'title',
'id' => 'buckaroo-page',
'desc' => __(
'Integrate more then 30+ international payment methods in your WooCommerce webshop. Simply enable them into your WooCommerce webshop with the Buckaroo Payments plugin.
Please go to the <a href="https://plaza.buckaroo.nl/Configuration/Website/Index/">signup page</a> to create a Buckaroo account and start receiving payments.</br>
Contact <a href="mailto:[email protected]">[email protected]</a> if you have any questions about this plugin.',
'wc-buckaroo-bpe-gateway'
),
),
array(
'type' => 'buckaroo_payment_list'
),
array(
'title' => __(
'General settings', 'wc-buckaroo-bpe-gateway'
),
'type' => 'title',
'id' => 'buckaroo-general-settings',
),
),
$this->get_master_settings()
);
$settings[] = array(
'type' => 'sectionend',
'id' => 'buckaroo-page',
);
return $settings;
}
/**
* Get master fields
*
* @return array
*/
public function get_master_settings()
{
$fields = [];
foreach ($this->gateway->form_fields as $id => $field) {
$type =$field['type'];
if (in_array($field['type'], ['button', 'hidden', 'file'])) {
$type = "buckaroo_".$field['type'];
}
$field = array_merge(
$field,
array(
'id' => $this->gateway->get_field_key($id),
'desc' => $field['description'],
'value' => $this->gateway->get_option($id),
'type' => $type
)
);
unset($field['description']);
$fields[] = $field;
}
return $fields;
}
public function render_payment_list()
{
$gateways = $this->getBuckarooGateways();
$containerHeight = ceil(count($gateways) / 3) * 45
?>
<ul class="buckaroo-payment-list" style="height:<?php echo esc_attr($containerHeight); ?>px">
<?php foreach ($gateways as $gateway) {
$method_title = $gateway->get_method_title() ? $gateway->get_method_title() : $gateway->get_title();
?>
<li>
<?php
if ($gateway->icon !== null) {
?>
<img class="buckaroo-payment-list-icon" src="<?php echo esc_url($gateway->icon); ?>">
<?php
}
echo wp_kses_post(str_replace("Buckaroo ", "", $method_title));
if (wc_string_to_bool($gateway->enabled)) {
if ($gateway->mode === 'live') {
echo '<b class="buckaroo-payment-status buckaroo-payment-enabled-live">'.esc_html__('enabled (live)', 'wc-buckaroo-bpe-gateway').'</b>';
} else {
echo '<b class="buckaroo-payment-status buckaroo-payment-enabled-test">'.esc_html__('enabled (test)', 'wc-buckaroo-bpe-gateway').'</b>';
}
} else {
echo '<b class="buckaroo-payment-status buckaroo-payment-disabled">'.esc_html__('disabled', 'wc-buckaroo-bpe-gateway').'</b>';
}
?>
<a href="<?php echo esc_url(admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . strtolower($gateway->id)));?>">
<?php echo esc_html__('edit', 'wc-buckaroo-bpe-gateway'); ?>
</a>
</li>
<?php
}?>
</ul>
<?php
}
/**
* Add custom file field
*
* @param array $value
*
* @return void
*/
public function render_file_field($value)
{
?>
<tr><td>
<input
name="<?php echo esc_attr($value['id']); ?>"
id="<?php echo esc_attr($value['id']); ?>"
type="file"
value="<?php echo esc_attr($value['value']); ?>"
class="<?php echo esc_attr($value['class']); ?>"
/>
</td><tr>
<?php
}
public function render_submeniu_field($value)
{
?>
<h2><?php echo esc_html($value['title']); ?></h2>
<tr>
<td>
<ul class="subsubsub" style="width:100%;margin-bottom:10px">
<?php foreach ($value['links'] as $key => $link) {
$endSlash = $key === count($value['links']) - 1 ? '' : '|';
echo '<li><b><a href="'.esc_url($link['href']).'"> '.esc_html($link['name']).' </a></b>'.esc_html($endSlash);
}
?>
</ul>
</td>
<tr>
<?php
}
/**
* Add custom hidden field
*
* @param array $value
*
* @return void
*/
public function render_hidden_field($value)
{
?>
<tr>
<input
name="<?php echo esc_attr($value['id']); ?>"
id="<?php echo esc_attr($value['id']); ?>"
type="hidden"
value="<?php echo esc_attr($value['value']); ?>"
class="<?php echo esc_attr($value['class']); ?>"
/>
</tr>
<?php
}
/**
* Add custom button field
*
* @param array $value
*
* @return void
*/
public function render_button_field($value)
{
$custom_attributes = array();
$field_description = WC_Admin_Settings::get_field_description($value);
$description = $field_description['description'];
$tooltip_html = $field_description['tooltip_html'];
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr($value['id']); ?>"><?php echo esc_html($value['title']); ?>
<?php
echo wp_kses($tooltip_html, array("span"=>array("class"=>true, "data-tip"=>true)));
?>
</label>
</th>
<td class="forminp forminp-<?php echo esc_attr(sanitize_title($value['type'])); ?>">
<input
name="<?php echo esc_attr($value['id']); ?>"
id="<?php echo esc_attr($value['id']); ?>"
type="button"
style="<?php echo esc_attr($value['css']); ?>"
value="<?php echo esc_attr($value['value']); ?>"
class="<?php echo esc_attr($value['class']); ?> input-text regular-input "
placeholder="<?php echo esc_attr($value['placeholder']); ?>"
<?php
if (!empty($value['custom_attributes']) && is_array($value['custom_attributes'])) {
foreach ($value['custom_attributes'] as $attribute => $attribute_value) {
echo esc_attr($attribute) . '="' . esc_attr($attribute_value) . '"';
}
}
?>
/>
<?php
echo esc_html($value['suffix']);
echo wp_kses($description, array("p"=>array("class" =>true, "style"=>true)));
?>
</td>
</tr>
<?php
}
/**
* Filter gateways to display only our gateways
*
* @return array
*/
protected function getBuckarooGateways()
{
$gateways = WC()->payment_gateways->payment_gateways();
$gateways = array_filter(
$gateways,
function ($gateway) {
return $gateway instanceof WC_Gateway_Buckaroo;
}
);
return $this->sortGatewaysAlfa($gateways);
}
/**
* Sort payment gateway alphabetically by name
*
* @param array $gateway
*
* @return array
*/
protected function sortGatewaysAlfa($gateways)
{
uasort(
$gateways,
function ($a, $b) {
return strcmp(
strtolower(str_replace("Buckaroo ", "", $a->get_method_title())),
strtoLower(str_replace("Buckaroo ", "", $b->get_method_title()))
);
}
);
return $gateways;
}
/**
* Render the gateway list
*
* @return void
*/
protected function render_gateway_list()
{
?>
<h2><?php echo esc_html__('Payment methods', 'wc-buckaroo-bpe-gateway'); ?></h2>
<p>
<?php
echo esc_html__('Buckaroo payment methods are listed below and can be accessed, enabled or disabled.', 'wc-buckaroo-bpe-gateway');
?>
</p>
<tr valign="top">
<td class="wc_payment_gateways_wrapper" colspan="2">
<table class="wc_gateways widefat" cellspacing="0" aria-describedby="payment_gateways_options-description">
<thead>
<tr>
<?php
$columns = array(
'name' => __('Method', 'wc-buckaroo-bpe-gateway'),
'status' => __('Enabled', 'wc-buckaroo-bpe-gateway'),
'action' => '',
);
foreach ( $columns as $key => $column ) {
echo '<th class="' . esc_attr($key) . '">' . esc_html($column) . '</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ( $this->getBuckarooGateways() as $gateway ) {
echo '<tr data-gateway_id="' . esc_attr($gateway->id) . '">';
foreach ( $columns as $key => $column ) {
$method_title = $gateway->get_method_title() ? $gateway->get_method_title() : $gateway->get_title();
$custom_title = $gateway->get_title();
$width = '';
if (in_array($key, array( 'status', 'action' ), true)) {
$width = '1%';
}
echo '<td class="' . esc_attr($key) . '" width="' . esc_attr($width) . '">';
switch ( $key ) {
case 'name':
echo '<a href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . strtolower($gateway->id))) . '" class="wc-payment-gateway-method-title">' . wp_kses_post(str_replace("Buckaroo ", "", $method_title)) . '</a>';
if ($method_title !== $custom_title) {
echo '<span class="wc-payment-gateway-method-name"> – ' . wp_kses_post($custom_title) . '</span>';
}
break;
case 'action':
if (wc_string_to_bool($gateway->enabled)) {
/* Translators: %s Payment gateway name. */
echo '<a class="button alignright" aria-label="' . esc_attr(sprintf(__('Manage the "%s" payment method', 'wc-buckaroo-bpe-gateway'), $method_title)) . '" href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . strtolower($gateway->id))) . '">' . esc_html__('Manage', 'wc-buckaroo-bpe-gateway') . '</a>';
} else {
/* Translators: %s Payment gateway name. */
echo '<a class="button alignright" aria-label="' . esc_attr(sprintf(__('Set up the "%s" payment method', 'wc-buckaroo-bpe-gateway'), $method_title)) . '" href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . strtolower($gateway->id))) . '">' . esc_html__('Set up', 'wc-buckaroo-bpe-gateway') . '</a>';
}
break;
case 'status':
echo '<a class="wc-payment-gateway-method-toggle-enabled" href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . strtolower($gateway->id))) . '">';
if (wc_string_to_bool($gateway->enabled)) {
/* Translators: %s Payment gateway name. */
echo '<span class="woocommerce-input-toggle woocommerce-input-toggle--enabled" aria-label="' . esc_attr(sprintf(__('The "%s" payment method is currently enabled', 'wc-buckaroo-bpe-gateway'), $method_title)) . '">' . esc_attr__('Yes', 'wc-buckaroo-bpe-gateway') . '</span>';
} else {
/* Translators: %s Payment gateway name. */
echo '<span class="woocommerce-input-toggle woocommerce-input-toggle--disabled" aria-label="' . esc_attr(sprintf(__('The "%s" payment method is currently disabled', 'wc-buckaroo-bpe-gateway'), $method_title)) . '">' . esc_attr__('No', 'wc-buckaroo-bpe-gateway') . '</span>';
}
echo '</a>';
break;
}
echo '</td>';
}
echo '</tr>';
}
?>
</tbody>
</table>
</td>
</tr>
<?php
}
}