-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIssuersField.php
580 lines (461 loc) · 14.9 KB
/
IssuersField.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
<?php
/**
* Issuers field
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2022 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay\Extensions\GravityForms
*/
namespace Pronamic\WordPress\Pay\Extensions\GravityForms;
use GF_Field_Select;
use Pronamic\WordPress\Pay\Core\Gateway;
use Pronamic\WordPress\Pay\Core\PaymentMethods;
use Pronamic\WordPress\Pay\Plugin;
/**
* Title: WordPress pay extension Gravity Forms issuers field
* Description:
* Copyright: 2005-2022 Pronamic
* Company: Pronamic
*
* @author Remco Tolsma
* @version 2.1.10
* @since 1.4.7
*
* @property int $pronamicPayConfigId Added by admin.js.
* @property int $formId https://github.com/wp-premium/gravityforms/blob/2.3.2/includes/fields/class-gf-field.php#L1044
* @property array $choices https://github.com/wp-premium/gravityforms/search?q=%22%24this-%3Echoices%22
*/
class IssuersField extends GF_Field_Select {
/**
* Type
*
* @var string
*/
const TYPE = Fields::ISSUERS_FIELD_TYPE;
/**
* Type
*
* @var string
*/
public $type = Fields::ISSUERS_FIELD_TYPE;
/**
* Constructs and initializes issuers field.
*
* @param array $properties Properties.
*/
public function __construct( $properties = array() ) {
parent::__construct( $properties );
// Actions.
if ( ! has_action( 'gform_editor_js_set_default_values', array( __CLASS__, 'editor_js_set_default_values' ) ) ) {
add_action( 'gform_editor_js_set_default_values', array( __CLASS__, 'editor_js_set_default_values' ) );
}
if (
! isset( $this->formId )
&&
wp_doing_ajax()
&&
filter_has_var( INPUT_POST, 'form_id' )
&&
'rg_add_field' === filter_input( INPUT_POST, 'action' )
&&
false !== strpos( filter_input( INPUT_POST, 'field' ), self::TYPE )
) {
$this->formId = filter_input( INPUT_POST, 'form_id', FILTER_SANITIZE_NUMBER_INT );
}
// Choices.
if ( isset( $this->formId ) ) {
$this->set_choices( $this->formId );
}
// Set default display mode.
if ( ! isset( $this->pronamicPayDisplayMode ) ) {
$this->pronamicPayDisplayMode = 'select';
}
// Add display mode CSS classes.
if ( false === strpos( $this->cssClass, 'pronamic_pay_display_icons' ) && 'icons' === substr( $this->pronamicPayDisplayMode, 0, 5 ) ) {
$this->cssClass .= ' pronamic_pay_display_icons';
}
if ( false === strpos( $this->cssClass, 'gf_list_2col' ) && in_array( $this->pronamicPayDisplayMode, array( 'icons-64', 'icons-125' ), true ) ) {
$this->cssClass .= ' gf_list_2col';
}
}
/**
* Get form editor field settings for this field.
*
* @link https://github.com/wp-premium/gravityforms/blob/2.0.3/includes/fields/class-gf-field-select.php#L16-L35
* @link https://github.com/wp-premium/gravityforms/blob/2.0.3/includes/fields/class-gf-field.php#L144-L151
* @return array
*/
public function get_form_editor_field_settings() {
return array(
'conditional_logic_field_setting',
'error_message_setting',
'enable_enhanced_ui_setting',
'label_setting',
'label_placement_setting',
'admin_label_setting',
'size_setting',
'placeholder_setting',
'description_setting',
'css_class_setting',
'rules_setting',
'pronamic_pay_config_field_setting',
'pronamic_pay_display_field_setting',
);
}
/**
* Get the iDEAL gateway for this field.
*
* @return null|Gateway
*/
private function get_gateway() {
$gateway = null;
if ( isset( $this->pronamicPayConfigId ) && ! empty( $this->pronamicPayConfigId ) ) {
$gateway = Plugin::get_gateway( $this->pronamicPayConfigId );
}
if ( ! $gateway ) {
$feeds = FeedsDB::get_feeds_by_form_id( $this->formId );
foreach ( $feeds as $feed ) {
// Check if feed is active.
if ( '0' === get_post_meta( $feed->id, '_pronamic_pay_gf_feed_active', true ) ) {
continue;
}
$gateway = Plugin::get_gateway( $feed->config_id );
if ( $gateway ) {
$issuers = $gateway->get_transient_issuers();
if ( empty( $issuers ) ) {
continue;
}
return $gateway;
}
}
}
return $gateway;
}
/**
* Set the issuer choices for this issuers field.
*
* @param int $form_id Gravity Forms form ID.
*/
private function set_choices( $form_id ) {
$this->choices = array();
// Prevent HTTP requests in forms list.
if ( \doing_filter( 'gform_form_actions' ) ) {
return;
}
$gateway = $this->get_gateway();
if ( ! $gateway ) {
return;
}
// Always use iDEAL payment method for issuer field.
$gateway->set_payment_method( PaymentMethods::IDEAL );
$field = $gateway->get_issuer_field();
if ( null === $field ) {
return;
}
foreach ( $field['choices'] as $group ) {
foreach ( $group['options'] as $value => $label ) {
$this->choices[] = array(
'value' => $value,
'text' => $label,
);
}
}
}
/**
* Get the field input.
*
* @link https://github.com/wp-premium/gravityforms/blob/2.0.3/includes/fields/class-gf-field-select.php#L41-L60
*
* @link https://github.com/wp-premium/gravityforms/blob/2.0.3/includes/fields/class-gf-field.php#L182-L193
* @link https://github.com/wp-premium/gravityforms/blob/2.3.2/includes/fields/class-gf-field.php#L228-L239
*
* @param array $form The Form Object currently being processed.
* @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
* @param null|array $entry Null or the Entry Object currently being edited.
*
* @return string
*/
public function get_field_input( $form, $value = '', $entry = null ) {
// Error handling.
if ( is_wp_error( $this->error ) ) {
return $this->error->get_error_message();
}
// Input.
$input = parent::get_field_input( $form, $value, $entry );
$field_css_id = sprintf( '#field_%1$s_%2$s', $this->formId, $this->id );
if ( ! is_admin() && 'icons' === substr( $this->pronamicPayDisplayMode, 0, 5 ) ) {
ob_start();
?>
<div class="ginput_container ginput_container_radio gfield_trigger_change">
<ul class="gfield_radio input_<?php echo esc_attr( $this->formId ); ?>_<?php echo esc_attr( $this->id ); ?>">
<?php
// Icon filename replacements.
$replacements = array(
' bankiers' => '',
' ' => '-',
);
// Icon file and size.
switch ( $this->pronamicPayDisplayMode ) {
case 'icons-24':
$dimensions = array( 24, 24 );
break;
case 'icons-64':
$dimensions = array( 64, 64 );
break;
case 'icons-125':
default:
$dimensions = array( 125, 60 );
}
$images_path = plugin_dir_path( Plugin::$file ) . 'images/';
// Loop issuers.
foreach ( $this->choices as $choice ) {
// Icon file name.
$issuer = strtr( strtolower( $choice['text'] ), $replacements );
if ( false !== stripos( $issuer, 'test' ) || false !== stripos( $issuer, 'simulation' ) ) {
$issuer = 'test';
}
if ( ! is_dir( $images_path . $issuer ) && is_dir( $images_path . $issuer . '-bank' ) ) {
$issuer .= '-bank';
}
$icon_path = sprintf(
'%s/icon-%s.png',
$issuer,
implode( 'x', $dimensions )
);
// Radio input.
$label_content = sprintf( '<span>%s</span>', esc_html( $choice['text'] ) );
if ( file_exists( plugin_dir_path( Plugin::$file ) . 'images/' . $icon_path ) ) {
$icon_url = plugins_url( 'images/' . $icon_path, Plugin::$file );
$label_content = sprintf(
'<img src="%2$s" alt="%1$s" srcset="%3$s 2x, %4$s 3x, %5$s 4x" /><span>%1$s</span>',
esc_html( $choice['text'] ),
esc_url( $icon_url ),
esc_url( str_replace( '.png', '@2x.png', $icon_url ) ),
esc_url( str_replace( '.png', '@3x.png', $icon_url ) ),
esc_url( str_replace( '.png', '@4x.png', $icon_url ) )
);
}
printf(
'<li class="gchoice_%1$s_%2$s_%3$s"><input type="radio" id="choice_%1$s_%2$s_%3$s" name="input_%2$s" value="%3$s" /> <label for="choice_%1$s_%2$s_%3$s">%4$s</label></li>',
esc_attr( $this->formId ),
esc_attr( $this->id ),
esc_attr( $choice['value'] ),
wp_kses_post( $label_content )
);
}
?>
</ul>
</div>
<style>
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li {
display: inline-block;
width: 50%;
margin: 0;
vertical-align: baseline;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li img {
display: block;
width: <?php echo esc_html( $dimensions[0] ); ?>px;
height: <?php echo esc_html( $dimensions[1] ); ?>px;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li label {
display: inline-block;
max-width: 100%;
width: 100%;
margin: 0;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li input[type="radio"] {
display: none;
}
<?php
switch ( $this->pronamicPayDisplayMode ) {
case 'icons-24':
?>
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li label {
margin: 0;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li label {
width: auto;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li label span {
display: inline-block;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li img {
display: inline;
vertical-align: middle;
margin-right: 10px;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li input[type="radio"] {
display: inline-block;
margin-top: 0;
}
<?php
break;
case 'icons-64':
?>
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio {
width: <?php echo esc_html( ( 64 * 2 ) + 126 ); ?>px;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li {
text-align: center;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li label {
margin: 0;
padding-bottom: 10px;
color: #bbb;
font-weight: normal;
border-radius: 4px;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li:hover label {
background: #efefef;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li img {
margin: 10px auto;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li span {
display: table-cell;
width: 127px;
height: 105px;
padding: 2px;
text-align: center;
vertical-align: bottom;
white-space: normal;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li img + span {
display: block;
height: auto;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li input[type="radio"]:checked ~ label {
font-weight: bold;
color: #555;
background: #efefef;
}
<?php
break;
case 'icons-125':
?>
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio {
width: <?php echo esc_html( ( 125 * 2 ) + 25 ); ?>px;
font-size: 16px;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li {
float: left;
width: auto;
margin: 0 4px 4px 0;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li label {
display: block;
height: 68px;
padding: 3px;
border: 1px solid #bbb;
border-radius: 4px;
background: #fff;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li:hover label {
background: #efefef;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li input[type="radio"]:checked ~ label {
padding: 2px;
border-width: 2px;
border-color: #555;
background: #efefef;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li label span {
display: table-cell;
width: 125px;
height: 60px;
text-align: center;
vertical-align: middle;
white-space: normal;
}
.gform_wrapper <?php echo esc_html( $field_css_id ); ?> .gfield_radio li label img + span {
display: none;
}
<?php
}
?>
</style>
<?php
$input = ob_get_clean();
}
if ( is_admin() ) {
$feeds = FeedsDB::get_feeds_by_form_id( $form['id'] );
$new_feed_url = Admin::get_new_feed_url( $form['id'] );
if ( empty( $feeds ) ) {
$link = sprintf(
'<a class="ideal-edit-link" href="%s" target="_blank">%s</a>',
esc_url( $new_feed_url ),
__( 'New Payment Feed', 'pronamic_ideal' )
);
$input = $link . $input;
}
if ( ! empty( $feeds ) && empty( $this->choices ) ) {
// If there are feeds and no choices it's very likely this field is no supported by the gateway.
$error = sprintf(
'<p class="pronamic-pay-error"><strong>%s</strong><br><em>%s</em></p>',
__( 'This field is not supported by your payment gateway.', 'pronamic_ideal' ),
sprintf(
/* translators: %s: new feed URL */
__( 'Please remove it from this form or <a href="%s" target="_blank">add a supported payment gateway</a>.', 'pronamic_ideal' ),
esc_url( $new_feed_url )
)
);
$input = $error . $input;
}
}
return $input;
}
/**
* Get form editor field title.
*
* @link https://github.com/wp-premium/gravityforms/blob/1.9.19/includes/fields/class-gf-field.php#L106-L113
* @link https://github.com/wp-premium/gravityforms/blob/1.9.19/includes/fields/class-gf-field-select.php#L12-L14
* @return string
*/
public function get_form_editor_field_title() {
return esc_attr__( 'Issuer', 'pronamic_ideal' );
}
/**
* Get form editor button.
*
* @link https://github.com/wp-premium/gravityforms/blob/1.9.19/includes/fields/class-gf-field.php#L115-L129
* @return array
*/
public function get_form_editor_button() {
return array(
'group' => 'pronamic_pay_fields',
'text' => __( 'Issuer', 'pronamic_ideal' ),
);
}
/**
* Add button.
*
* @link https://github.com/wp-premium/gravityforms/blob/1.9.19/form_detail.php#L2353-L2368
* @link https://github.com/wp-premium/gravityforms/blob/1.9.19/includes/fields/class-gf-field.php#L617-L652
*
* @param array $field_groups Field groups.
*
* @return array
*/
public function add_button( $field_groups ) {
// We have to make sure the custom pay field group is added, otherwise the button won't be added.
$field_groups = Fields::add_pay_field_group( $field_groups );
return parent::add_button( $field_groups );
}
/**
* Editor JavaScript default field values.
*
* @link https://github.com/wp-premium/gravityforms/blob/2.0.3/js.php#L587-L599
*/
public static function editor_js_set_default_values() {
$label = __( 'Choose a bank for iDEAL payment', 'pronamic_ideal' );
?>
case '<?php echo esc_js( self::TYPE ); ?>' :
if ( ! field.label ) {
field.label = '<?php echo esc_js( $label ); ?>';
}
break;
<?php
}
}