forked from wp-premium/gravityforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
5825 lines (4693 loc) · 196 KB
/
common.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
/**
* Class GFCommon
*
* Includes common methods accessed throughout Gravity Forms and add-ons.
*/
class GFCommon {
// deprecated; set to GFForms::$version in GFForms::init() for backwards compat
public static $version = null;
public static $tab_index = 1;
public static $errors = array();
public static $messages = array();
public static $email_boundary = '394c21ef2c7143749256c37c3b5b7ee0';
/**
* An array of dismissible messages to display on the page.
*
* @var array $dismissible_messages
*/
public static $dismissible_messages = array();
public static function get_selection_fields( $form, $selected_field_id ) {
$str = '';
foreach ( $form['fields'] as $field ) {
$input_type = RGFormsModel::get_input_type( $field );
$field_label = RGFormsModel::get_label( $field );
if ( $input_type == 'checkbox' || $input_type == 'radio' || $input_type == 'select' ) {
$selected = $field->id == $selected_field_id ? "selected='selected'" : '';
$str .= "<option value='" . $field->id . "' " . $selected . '>' . $field_label . '</option>';
}
}
return $str;
}
public static function is_numeric( $value, $number_format = '' ) {
if ( $number_format == 'currency' ) {
$number_format = self::is_currency_decimal_dot() ? 'decimal_dot' : 'decimal_comma';
$value = self::remove_currency_symbol( $value );
}
switch ( $number_format ) {
case 'decimal_dot' :
return preg_match( "/^(-?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]+)?)$/", $value );
break;
case 'decimal_comma' :
return preg_match( "/^(-?[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9]+)?)$/", $value );
break;
default :
return preg_match( "/^(-?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)$/", $value ) || preg_match( "/^(-?[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9]{2})?)$/", $value );
}
}
public static function remove_currency_symbol( $value, $currency = null ) {
if ( $currency == null ) {
$code = GFCommon::get_currency();
if ( empty( $code ) ) {
$code = 'USD';
}
$currency = RGCurrency::get_currency( $code );
}
$value = str_replace( $currency['symbol_left'], '', $value );
$value = str_replace( $currency['symbol_right'], '', $value );
//some symbols can't be easily matched up, so this will catch any of them
$value = preg_replace( '/[^,.\d]/', '', $value );
return $value;
}
public static function is_currency_decimal_dot( $currency = null ) {
if ( $currency == null ) {
$code = GFCommon::get_currency();
if ( empty( $code ) ) {
$code = 'USD';
}
$currency = RGCurrency::get_currency( $code );
}
return rgar( $currency, 'decimal_separator' ) == '.';
}
public static function trim_all( $text ) {
$text = trim( $text );
do {
$prev_text = $text;
$text = str_replace( ' ', ' ', $text );
} while ( $text != $prev_text );
return $text;
}
public static function format_number( $number, $number_format, $currency = '', $include_thousands_sep = false ) {
if ( ! is_numeric( $number ) ) {
return $number;
}
//replacing commas with dots and dots with commas
if ( $number_format == 'currency' ) {
if ( empty( $currency ) ) {
$currency = GFCommon::get_currency();
}
$currency = new RGCurrency( $currency );
$number = $currency->to_money( $number );
} else {
if ( $number_format == 'decimal_comma' ) {
$dec_point = ',';
$thousands_sep = $include_thousands_sep ? '.' : '';
} else {
$dec_point = '.';
$thousands_sep = $include_thousands_sep ? ',' : '';
}
$is_negative = $number < 0;
$number = explode( '.', $number );
$number[0] = number_format( absint( $number[0] ), 0, '', $thousands_sep );
$number = implode( $dec_point, $number );
if ( $is_negative ) {
$number = '-' . $number;
}
}
return $number;
}
public static function recursive_add_index_file( $dir ) {
if ( ! is_dir( $dir ) || is_link( $dir ) ) {
return;
}
if ( ! ( $dp = opendir( $dir ) ) ) {
return;
}
// ignores all errors
set_error_handler( create_function( '', 'return 0;' ), E_ALL );
//creates an empty index.html file
if ( $f = fopen( $dir . '/index.html', 'w' ) ) {
fclose( $f );
}
// restores error handler
restore_error_handler();
while ( ( false !== $file = readdir( $dp ) ) ) {
if ( is_dir( "$dir/$file" ) && $file != '.' && $file != '..' ) {
self::recursive_add_index_file( "$dir/$file" );
}
}
closedir( $dp );
}
public static function add_htaccess_file() {
$upload_root = GFFormsModel::get_upload_root();
if ( ! is_dir( $upload_root ) ) {
return;
}
if ( ! wp_is_writable( $upload_root ) ) {
return;
}
$htaccess_file = $upload_root . '.htaccess';
if ( file_exists( $htaccess_file ) ) {
@unlink( $htaccess_file );
}
$txt = '# Disable parsing of PHP for some server configurations. This file may be removed or modified on certain server configurations by using by the gform_upload_root_htaccess_rules filter. Please consult your system administrator before removing this file.
<Files *>
SetHandler none
SetHandler default-handler
Options -ExecCGI
RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
</Files>
<IfModule mod_php5.c>
php_flag engine off
</IfModule>';
$rules = explode( "\n", $txt );
/**
* A filter to allow the modification/disabling of parsing certain PHP within Gravity Forms
*
* @since 1.9.2
*
* @param mixed $rules The Rules of what to parse or not to parse
*/
$rules = apply_filters( 'gform_upload_root_htaccess_rules', $rules );
if ( ! empty( $rules ) ) {
if ( ! function_exists( 'insert_with_markers' ) ) {
require_once( ABSPATH . 'wp-admin/includes/misc.php' );
}
insert_with_markers( $htaccess_file, 'Gravity Forms', $rules );
}
}
public static function clean_number( $number, $number_format = '' ) {
if ( rgblank( $number ) ) {
return $number;
}
$decimal_char = '';
if ( $number_format == 'decimal_dot' ) {
$decimal_char = '.';
} else if ( $number_format == 'decimal_comma' ) {
$decimal_char = ',';
} else if ( $number_format == 'currency' ) {
$currency = RGCurrency::get_currency( GFCommon::get_currency() );
$decimal_char = $currency['decimal_separator'];
}
$float_number = '';
$clean_number = '';
$is_negative = false;
//Removing all non-numeric characters
$array = str_split( $number );
foreach ( $array as $char ) {
if ( ( $char >= '0' && $char <= '9' ) || $char == ',' || $char == '.' ) {
$clean_number .= $char;
} else if ( $char == '-' ) {
$is_negative = true;
}
}
//Removing thousand separators but keeping decimal point
$array = str_split( $clean_number );
for ( $i = 0, $count = sizeof( $array ); $i < $count; $i ++ ) {
$char = $array[ $i ];
if ( $char >= '0' && $char <= '9' ) {
$float_number .= $char;
} else if ( empty( $decimal_char ) && ( $char == '.' || $char == ',' ) && strlen( $clean_number ) - $i <= 3 ) {
$float_number .= '.';
} else if ( $decimal_char == $char ) {
$float_number .= '.';
}
}
if ( $is_negative ) {
$float_number = '-' . $float_number;
}
return $float_number;
}
public static function json_encode( $value ) {
return json_encode( $value );
}
public static function json_decode( $str, $is_assoc = true ) {
return json_decode( $str, $is_assoc );
}
//Returns the url of the plugin's root folder
public static function get_base_url() {
return plugins_url( '', __FILE__ );
}
//Returns the physical path of the plugin's root folder
public static function get_base_path() {
return dirname( __FILE__ );
}
public static function get_email_fields( $form ) {
$fields = array();
foreach ( $form['fields'] as $field ) {
if ( $field->type == 'email' || $field->inputType == 'email' ) {
$fields[] = $field;
}
}
return $fields;
}
public static function truncate_middle( $text, $max_length ) {
if ( strlen( $text ) <= $max_length ) {
return $text;
}
$middle = intval( $max_length / 2 );
return self::safe_substr( $text, 0, $middle ) . '...' . self::safe_substr( $text, strlen( $text ) - $middle, $middle );
}
public static function is_invalid_or_empty_email( $email ) {
return empty( $email ) || ! self::is_valid_email( $email );
}
/**
* Validates URLs.
*
* @since 2.0.7.12 Filters added to allow for using custom validation.
* @access public
*
* @used-by GFFormSettings::handle_confirmation_edit_submission()
* @used-by GF_Field_Post_Image::get_value_save_entry()
* @used-by GF_Field_Website::get_value_entry_detail()
* @used-by GF_Field_Website::validate()
*
* @param string $url The URL to validate.
*
* @return bool True if valid. False otherwise.
*/
public static function is_valid_url( $url ) {
$url = trim( $url );
/***
* Enables and disables RFC URL validation. Defaults to true.
*
* When RFC is enabled, URLs will be validated against the RFC standard.
* When disabled, a simple and generic URL validation will be performed.
*
* @since 2.0.7.12
* @see https://www.gravityhelp.com/documentation/article/gform_rfc_url_validation/
*
* @param bool true If RFC validation should be enabled. Defaults to true. Set to false to disable RFC validation.
*/
$use_rfc = apply_filters( 'gform_rfc_url_validation', true );
$is_valid = ( strpos( $url, 'http://' ) === 0 || strpos( $url, 'https://' ) === 0 );
if ( $use_rfc ) {
$is_valid = $is_valid && filter_var( $url, FILTER_VALIDATE_URL ) !== false;
}
/***
* Filters the result of URL validations, allowing for custom validation to be performed.
*
* @since 2.0.7.12
* @see https://www.gravityhelp.com/documentation/article/gform_is_valid_url/
*
* @param bool $is_valid True if valid. False otherwise.
* @param string $url The URL being validated.
*/
$is_valid = apply_filters( 'gform_is_valid_url', $is_valid, $url );
return $is_valid;
}
public static function is_valid_email( $email ) {
return is_email( $email );
}
public static function is_valid_email_list( $email_list ) {
$emails = explode( ',', $email_list );
if ( ! is_array( $emails ) ) {
return false;
}
foreach ( $emails as $email ) {
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
return false;
}
}
return true;
}
public static function get_label( $field, $input_id = 0, $input_only = false, $allow_admin_label = true ) {
return RGFormsModel::get_label( $field, $input_id, $input_only, $allow_admin_label );
}
public static function get_input( $field, $id ) {
return RGFormsModel::get_input( $field, $id );
}
public static function insert_variables( $fields, $element_id, $hide_all_fields = false, $callback = '', $onchange = '', $max_label_size = 40, $exclude = null, $args = '', $class_name = '' ) {
if ( $fields == null ) {
$fields = array();
}
if ( $exclude == null ) {
$exclude = array();
}
$exclude = apply_filters( 'gform_merge_tag_list_exclude', $exclude, $element_id, $fields );
$merge_tags = self::get_merge_tags( $fields, $element_id, $hide_all_fields, $exclude, $args );
$onchange = empty( $onchange ) ? "InsertVariable('{$element_id}', '{$callback}');" : $onchange;
$class = trim( $class_name . ' gform_merge_tags' );
?>
<select id="<?php echo esc_attr( $element_id ); ?>_variable_select" onchange="<?php echo $onchange ?>" class="<?php echo esc_attr( $class ) ?>">
<option value=''><?php esc_html_e( 'Insert Merge Tag', 'gravityforms' ); ?></option>
<?php foreach ( $merge_tags as $group => $group_tags ) {
$group_label = rgar( $group_tags, 'label' );
$tags = rgar( $group_tags, 'tags' );
if ( empty( $group_tags['tags'] ) ) {
continue;
}
if ( $group_label ) {
?>
<optgroup label="<?php echo $group_label; ?>">
<?php } ?>
<?php foreach ( $tags as $tag ) { ?>
<option value="<?php echo $tag['tag']; ?>"><?php echo $tag['label']; ?></option>
<?php
}
if ( $group_label ) {
?>
</optgroup>
<?php
}
} ?>
</select>
<?php
}
/**
* This function is used by the gfMergeTags JS object to get the localized label for non-field merge tags as well as
* for backwards compatibility with the gform_custom_merge_tags hook. Lastly, this plugin is used by the soon-to-be
* deprecated insert_variables() function as the new gfMergeTags object has not yet been applied to the Post Content
* Template setting.
*
* @param GF_Field[] $fields
* @param $element_id
* @param bool $hide_all_fields
* @param array $exclude_field_types
* @param string $option
*
* @return array
*/
public static function get_merge_tags( $fields, $element_id, $hide_all_fields = false, $exclude_field_types = array(), $option = '' ) {
if ( $fields == null ) {
$fields = array();
}
if ( $exclude_field_types == null ) {
$exclude_field_types = array();
}
$required_fields = $optional_fields = $pricing_fields = array();
$ungrouped = $required_group = $optional_group = $pricing_group = $other_group = array();
if ( ! $hide_all_fields ) {
$ungrouped[] = array(
'tag' => '{all_fields}',
'label' => esc_html__( 'All Submitted Fields', 'gravityforms' )
);
}
// group fields by required, optional, and pricing
foreach ( $fields as $field ) {
if ( $field->displayOnly ) {
continue;
}
$input_type = RGFormsModel::get_input_type( $field );
// skip field types that should be excluded
if ( is_array( $exclude_field_types ) && in_array( $input_type, $exclude_field_types ) ) {
continue;
}
if ( $field->isRequired ) {
switch ( $input_type ) {
case 'name' :
if ( $field->nameFormat == 'extended' ) {
$prefix = GFCommon::get_input( $field, $field->id . '.2' );
$suffix = GFCommon::get_input( $field, $field->id . '.8' );
$optional_field = $field;
$optional_field['inputs'] = array( $prefix, $suffix );
//Add optional name fields to the optional list
$optional_fields[] = $optional_field;
//Remove optional name field from required list
unset( $field->inputs[0] );
unset( $field->inputs[3] );
}
$required_fields[] = $field;
break;
default:
$required_fields[] = $field;
}
} else {
$optional_fields[] = $field;
}
if ( self::is_pricing_field( $field->type ) ) {
$pricing_fields[] = $field;
}
}
if ( ! empty( $required_fields ) ) {
foreach ( $required_fields as $field ) {
$required_group = array_merge( $required_group, self::get_field_merge_tags( $field, $option ) );
}
}
if ( ! empty( $optional_fields ) ) {
foreach ( $optional_fields as $field ) {
$optional_group = array_merge( $optional_group, self::get_field_merge_tags( $field, $option ) );
}
}
if ( ! empty( $pricing_fields ) ) {
if ( ! $hide_all_fields ) {
$pricing_group[] = array(
'tag' => '{pricing_fields}',
'label' => esc_html__( 'All Pricing Fields', 'gravityforms' )
);
}
foreach ( $pricing_fields as $field ) {
$pricing_group = array_merge( $pricing_group, self::get_field_merge_tags( $field, $option ) );
}
}
$other_group[] = array( 'tag' => '{ip}', 'label' => esc_html__( 'User IP Address', 'gravityforms' ) );
$other_group[] = array(
'tag' => '{date_mdy}',
'label' => esc_html__( 'Date', 'gravityforms' ) . ' (mm/dd/yyyy)'
);
$other_group[] = array(
'tag' => '{date_dmy}',
'label' => esc_html__( 'Date', 'gravityforms' ) . ' (dd/mm/yyyy)'
);
$other_group[] = array(
'tag' => '{embed_post:ID}',
'label' => esc_html__( 'Embed Post/Page Id', 'gravityforms' )
);
$other_group[] = array(
'tag' => '{embed_post:post_title}',
'label' => esc_html__( 'Embed Post/Page Title', 'gravityforms' )
);
$other_group[] = array( 'tag' => '{embed_url}', 'label' => esc_html__( 'Embed URL', 'gravityforms' ) );
$other_group[] = array( 'tag' => '{entry_id}', 'label' => esc_html__( 'Entry Id', 'gravityforms' ) );
$other_group[] = array( 'tag' => '{entry_url}', 'label' => esc_html__( 'Entry URL', 'gravityforms' ) );
$other_group[] = array( 'tag' => '{form_id}', 'label' => esc_html__( 'Form Id', 'gravityforms' ) );
$other_group[] = array( 'tag' => '{form_title}', 'label' => esc_html__( 'Form Title', 'gravityforms' ) );
$other_group[] = array( 'tag' => '{user_agent}', 'label' => esc_html__( 'HTTP User Agent', 'gravityforms' ) );
$other_group[] = array( 'tag' => '{referer}', 'label' => esc_html__( 'HTTP Referer URL', 'gravityforms' ) );
if ( self::has_post_field( $fields ) ) {
$other_group[] = array( 'tag' => '{post_id}', 'label' => esc_html__( 'Post Id', 'gravityforms' ) );
$other_group[] = array(
'tag' => '{post_edit_url}',
'label' => esc_html__( 'Post Edit URL', 'gravityforms' )
);
}
$other_group[] = array(
'tag' => '{user:display_name}',
'label' => esc_html__( 'User Display Name', 'gravityforms' )
);
$other_group[] = array( 'tag' => '{user:user_email}', 'label' => esc_html__( 'User Email', 'gravityforms' ) );
$other_group[] = array( 'tag' => '{user:user_login}', 'label' => esc_html__( 'User Login', 'gravityforms' ) );
$form_id = isset( $fields[0] ) ? $fields[0]->formId : 0;
$custom_group = apply_filters( 'gform_custom_merge_tags', array(), $form_id, $fields, $element_id );
$merge_tags = array(
'ungrouped' => array(
'label' => false,
'tags' => $ungrouped,
),
'required' => array(
'label' => esc_html__( 'Required form fields', 'gravityforms' ),
'tags' => $required_group,
),
'optional' => array(
'label' => esc_html__( 'Optional form fields', 'gravityforms' ),
'tags' => $optional_group,
),
'pricing' => array(
'label' => esc_html__( 'Pricing form fields', 'gravityforms' ),
'tags' => $pricing_group,
),
'other' => array(
'label' => esc_html__( 'Other', 'gravityforms' ),
'tags' => $other_group,
),
'custom' => array(
'label' => esc_html__( 'Custom', 'gravityforms' ),
'tags' => $custom_group,
)
);
return $merge_tags;
}
/**
* @param GF_Field $field
* @param string $option
*
* @return string
*/
public static function get_field_merge_tags( $field, $option = '' ) {
$merge_tags = array();
$tag_args = RGFormsModel::get_input_type( $field ) == 'list' ? ":{$option}" : ''; //args currently only supported by list field
$inputs = $field->get_entry_inputs();
if ( is_array( $inputs ) ) {
if ( RGFormsModel::get_input_type( $field ) == 'checkbox' ) {
$value = '{' . esc_html( GFCommon::get_label( $field, $field->id ) ) . ':' . $field->id . "{$tag_args}}";
$merge_tags[] = array(
'tag' => $value,
'label' => esc_html( GFCommon::get_label( $field, $field->id ) )
);
}
foreach ( $field->inputs as $input ) {
if ( RGFormsModel::get_input_type( $field ) == 'creditcard' ) {
//only include the credit card type (field_id.4) and number (field_id.1)
if ( $input['id'] == $field->id . '.1' || $input['id'] == $field->id . '.4' ) {
$value = '{' . esc_html( GFCommon::get_label( $field, $input['id'] ) ) . ':' . $input['id'] . "{$tag_args}}";
$merge_tags[] = array(
'tag' => $value,
'label' => esc_html( GFCommon::get_label( $field, $input['id'] ) )
);
}
} else {
$value = '{' . esc_html( GFCommon::get_label( $field, $input['id'] ) ) . ':' . $input['id'] . "{$tag_args}}";
$merge_tags[] = array(
'tag' => $value,
'label' => esc_html( GFCommon::get_label( $field, $input['id'] ) )
);
}
}
} else {
$value = '{' . esc_html( GFCommon::get_label( $field ) ) . ':' . $field->id . "{$tag_args}}";
$merge_tags[] = array(
'tag' => $value,
'label' => esc_html( GFCommon::get_label( $field ) )
);
}
return $merge_tags;
}
public static function insert_field_variable( $field, $max_label_size = 40, $args = '' ) {
$tag_args = RGFormsModel::get_input_type( $field ) == 'list' ? ":{$args}" : ''; //args currently only supported by list field
if ( is_array( $field->inputs ) ) {
if ( RGFormsModel::get_input_type( $field ) == 'checkbox' ) {
?>
<option value='<?php echo '{' . esc_html( GFCommon::get_label( $field, $field->id ) ) . ':' . $field->id . "{$tag_args}}" ?>'><?php echo esc_html( GFCommon::get_label( $field, $field->id ) ) ?></option>
<?php
}
foreach ( $field->inputs as $input ) {
?>
<option value='<?php echo '{' . esc_html( GFCommon::get_label( $field, $input['id'] ) ) . ':' . $input['id'] . "{$tag_args}}" ?>'><?php echo esc_html( GFCommon::get_label( $field, $input['id'] ) ) ?></option>
<?php
}
} else {
?>
<option value='<?php echo '{' . esc_html( GFCommon::get_label( $field ) ) . ':' . $field->id . "{$tag_args}}" ?>'><?php echo esc_html( GFCommon::get_label( $field ) ) ?></option>
<?php
}
}
public static function insert_post_content_variables( $fields, $element_id, $callback, $max_label_size = 25 ) {
// TODO: replace with class-powered merge tags
$insert_variables_onchange = sprintf( "InsertPostContentVariable('%s', '%s');", esc_js( $element_id ), esc_js( $callback ) );
self::insert_variables( $fields, $element_id, true, '', $insert_variables_onchange, $max_label_size, null, '', 'gform_content_template_merge_tags' );
?>
<select id="<?php echo $element_id ?>_image_size_select" onchange="InsertPostImageVariable('<?php echo esc_js( $element_id ); ?>', '<?php echo esc_js( $element_id ); ?>'); SetCustomFieldTemplate();" style="display:none;">
<option value=""><?php esc_html_e( 'Select image size', 'gravityforms' ) ?></option>
<option value="thumbnail"><?php esc_html_e( 'Thumbnail', 'gravityforms' ) ?></option>
<option value="thumbnail:left"><?php esc_html_e( 'Thumbnail - Left Aligned', 'gravityforms' ) ?></option>
<option value="thumbnail:center"><?php esc_html_e( 'Thumbnail - Centered', 'gravityforms' ) ?></option>
<option value="thumbnail:right"><?php esc_html_e( 'Thumbnail - Right Aligned', 'gravityforms' ) ?></option>
<option value="medium"><?php esc_html_e( 'Medium', 'gravityforms' ) ?></option>
<option value="medium:left"><?php esc_html_e( 'Medium - Left Aligned', 'gravityforms' ) ?></option>
<option value="medium:center"><?php esc_html_e( 'Medium - Centered', 'gravityforms' ) ?></option>
<option value="medium:right"><?php esc_html_e( 'Medium - Right Aligned', 'gravityforms' ) ?></option>
<option value="large"><?php esc_html_e( 'Large', 'gravityforms' ) ?></option>
<option value="large:left"><?php esc_html_e( 'Large - Left Aligned', 'gravityforms' ) ?></option>
<option value="large:center"><?php esc_html_e( 'Large - Centered', 'gravityforms' ) ?></option>
<option value="large:right"><?php esc_html_e( 'Large - Right Aligned', 'gravityforms' ) ?></option>
<option value="full"><?php esc_html_e( 'Full Size', 'gravityforms' ) ?></option>
<option value="full:left"><?php esc_html_e( 'Full Size - Left Aligned', 'gravityforms' ) ?></option>
<option value="full:center"><?php esc_html_e( 'Full Size - Centered', 'gravityforms' ) ?></option>
<option value="full:right"><?php esc_html_e( 'Full Size - Right Aligned', 'gravityforms' ) ?></option>
</select>
<?php
}
public static function insert_calculation_variables( $fields, $element_id, $onchange = '', $callback = '', $max_label_size = 40 ) {
if ( $fields == null ) {
$fields = array();
}
$onchange = empty( $onchange ) ? sprintf( "InsertVariable('%s', '%s');", esc_js( $element_id ), esc_js( $callback ) ) : $onchange;
$class = 'gform_merge_tags';
?>
<select id="<?php echo esc_attr( $element_id ); ?>_variable_select" onchange="<?php echo $onchange ?>" class="<?php echo esc_attr( $class ) ?>">
<option value=''><?php esc_html_e( 'Insert Merge Tag', 'gravityforms' ); ?></option>
<optgroup label="<?php esc_attr_e( 'Allowable form fields', 'gravityforms' ); ?>">
<?php
foreach ( $fields as $field ) {
if ( ! self::is_valid_for_calcuation( $field ) ) {
continue;
}
if ( RGFormsModel::get_input_type( $field ) == 'checkbox' ) {
foreach ( $field->inputs as $input ) {
?>
<option value='<?php echo esc_attr( '{' . esc_html( GFCommon::get_label( $field, $input['id'] ) ) . ':' . $input['id'] . '}' ); ?>'><?php echo esc_html( GFCommon::get_label( $field, $input['id'] ) ) ?></option>
<?php
}
} else {
self::insert_field_variable( $field, $max_label_size );
}
}
?>
</optgroup>
<?php
$form_id = isset( $forms[0] ) ? $fields[0]->formId : 0;
$custom_merge_tags = apply_filters( 'gform_custom_merge_tags', array(), $form_id, $fields, $element_id );
if ( is_array( $custom_merge_tags ) && ! empty( $custom_merge_tags ) ) {
?>
<optgroup label="<?php esc_attr_e( 'Custom', 'gravityforms' ); ?>">
<?php foreach ( $custom_merge_tags as $custom_merge_tag ) { ?>
<option value='<?php echo esc_attr( rgar( $custom_merge_tag, 'tag' ) ); ?>'><?php echo esc_html( rgar( $custom_merge_tag, 'label' ) ); ?></option>
<?php } ?>
</optgroup>
<?php } ?>
</select>
<?php
}
private static function get_post_image_variable( $media_id, $arg1, $arg2, $is_url = false ) {
if ( $is_url ) {
$image = wp_get_attachment_image_src( $media_id, $arg1 );
if ( $image ) {
list( $src, $width, $height ) = $image;
}
return $src;
}
switch ( $arg1 ) {
case 'title' :
$media = get_post( $media_id );
return $media->post_title;
case 'caption' :
$media = get_post( $media_id );
return $media->post_excerpt;
case 'description' :
$media = get_post( $media_id );
return $media->post_content;
default :
$img = wp_get_attachment_image( $media_id, $arg1, false, array( 'class' => "size-{$arg1} align{$arg2} wp-image-{$media_id}" ) );
return $img;
}
}
public static function replace_variables_post_image( $text, $post_images, $lead ) {
preg_match_all( '/{[^{]*?:(\d+)(:([^:]*?))?(:([^:]*?))?(:url)?}/mi', $text, $matches, PREG_SET_ORDER );
if ( is_array( $matches ) ) {
foreach ( $matches as $match ) {
$input_id = $match[1];
// Ignore fields that are not post images.
if ( ! isset( $post_images[ $input_id ] ) ) {
continue;
}
// Reading alignment and 'url' parameters.
// Format could be {image:5:medium:left:url} or {image:5:medium:url}.
$size_meta = empty( $match[3] ) ? 'full' : $match[3];
$align = empty( $match[5] ) ? 'none' : $match[5];
if ( $align == 'url' ) {
$align = 'none';
$is_url = true;
} else {
$is_url = rgar( $match, 6 ) == ':url';
}
$media_id = $post_images[ $input_id ];
$value = is_wp_error( $media_id ) ? '' : self::get_post_image_variable( $media_id, $size_meta, $align, $is_url );
$text = str_replace( $match[0], $value, $text );
}
}
return $text;
}
public static function implode_non_blank( $separator, $array ) {
if ( ! is_array( $array ) ) {
return '';
}
$ary = array();
foreach ( $array as $item ) {
if ( ! rgblank( $item ) ) {
$ary[] = $item;
}
}
return implode( $separator, $ary );
}
public static function format_variable_value( $value, $url_encode, $esc_html, $format, $nl2br = true ) {
if ( $esc_html ) {
$value = esc_html( $value );
}
if ( $format == 'html' && $nl2br ) {
$value = nl2br( $value );
}
if ( $url_encode ) {
$value = urlencode( $value );
}
return $value;
}
public static function replace_variables( $text, $form, $lead, $url_encode = false, $esc_html = true, $nl2br = true, $format = 'html', $aux_data = array() ) {
$data = array_merge( array( 'entry' => $lead ), $aux_data );
/**
* Filter data that will be used to replace merge tags.
*
* @since 2.1.1.11 Added the Entry Object as the 4th parameter.
*
* @param $data array Array of key/value pairs, where key is used as merge tag and value is an array of data available to the merge tag.
* @param $text string String of text which will be searched for merge tags.
* @param $form array Current form object.
* @param $lead array The current Entry Object.
*
* @see https://www.gravityhelp.com/documentation/article/gform_merge_tag_data/
*/
$data = apply_filters( 'gform_merge_tag_data', $data, $text, $form, $lead );
$lead = $data['entry'];
$text = $nl2br ? nl2br( $text ) : $text;
$text = apply_filters( 'gform_pre_replace_merge_tags', $text, $form, $lead, $url_encode, $esc_html, $nl2br, $format );
if ( strpos( $text, '{' ) === false ) {
return $text;
}
// Replacing conditional merge tag variables: [gravityforms action="conditional" merge_tag="{Other Services:4}" ....
preg_match_all( '/merge_tag\s*=\s*["|\']({[^{]*?:(\d+(\.\d+)?)(:(.*?))?})["|\']/mi', $text, $matches, PREG_SET_ORDER );
if ( is_array( $matches ) ) {
foreach ( $matches as $match ) {
$input_id = $match[2];
$text = self::replace_field_variable( $text, $form, $lead, $url_encode, $esc_html, $nl2br, $format, $input_id, $match, true );
}
}
// Process dynamic merge tags based on auxiliary data.
$aux_tags = array_keys( $data );
$pattern = sprintf( '/{(%s):(.+?)}/', implode( '|', $aux_tags ) );
preg_match_all( $pattern, $text, $matches, PREG_SET_ORDER );
foreach ( $matches as $match ) {
list( $search, $tag, $prop ) = $match;
if ( is_callable( $data[ $tag ] ) ) {
$data[ $tag ] = call_user_func( $data[ $tag ], $lead, $form );
}
$object = $data[ $tag ];
$replace = rgars( $object, $prop );
$text = str_replace( $search, $replace, $text );
}
// Replacing field variables: {FIELD_LABEL:FIELD_ID} {My Field:2}.
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );
if ( is_array( $matches ) ) {
foreach ( $matches as $match ) {
$input_id = $match[1];
$text = self::replace_field_variable( $text, $form, $lead, $url_encode, $esc_html, $nl2br, $format, $input_id, $match );
}
}
$matches = array();
preg_match_all( "/{all_fields(:(.*?))?}/", $text, $matches, PREG_SET_ORDER );
foreach ( $matches as $match ) {
$options = explode( ',', rgar( $match, 2 ) );
$use_value = in_array( 'value', $options );
$display_empty = in_array( 'empty', $options );
$use_admin_label = in_array( 'admin', $options );
//all submitted fields using text
if ( strpos( $text, $match[0] ) !== false ) {
$text = str_replace( $match[0], self::get_submitted_fields( $form, $lead, $display_empty, ! $use_value, $format, $use_admin_label, 'all_fields', rgar( $match, 2 ) ), $text );
}
}
// All submitted fields including empty fields.
if ( strpos( $text, '{all_fields_display_empty}' ) !== false ) {
$text = str_replace( '{all_fields_display_empty}', self::get_submitted_fields( $form, $lead, true, true, $format, false, 'all_fields_display_empty' ), $text );
}
// Pricing fields.
$pricing_matches = array();
preg_match_all( "/{pricing_fields(:(.*?))?}/", $text, $pricing_matches, PREG_SET_ORDER );
foreach ( $pricing_matches as $match ) {
$options = explode( ',', rgar( $match, 2 ) );
$use_value = in_array( 'value', $options );
$use_admin_label = in_array( 'admin', $options );
// All submitted pricing fields using text.
if ( strpos( $text, $match[0] ) !== false ) {
$pricing_fields = self::get_submitted_pricing_fields( $form, $lead, $format, ! $use_value, $use_admin_label );
if ( $format == 'html' ) {
$text = str_replace(
$match[0],
'<table width="99%" border="0" cellpadding="1" cellspacing="0" bgcolor="#EAEAEA">
<tr><td>
<table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">' . $pricing_fields . '</table>
</td></tr>
</table>',
$text
);
} else {
$text = str_replace( $match[0], $pricing_fields, $text );
}
}