-
Notifications
You must be signed in to change notification settings - Fork 22
/
common.php
8070 lines (6596 loc) · 265 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();
}
use \Gravity_Forms\Gravity_Forms\Messages\Dismissable_Messages;
use \Gravity_Forms\Gravity_Forms\Orders\Factories\GF_Order_Factory;
use \Gravity_Forms\Gravity_Forms\Orders\Summaries\GF_Order_Summary;
use \Gravity_Forms\Gravity_Forms\Setup_Wizard\GF_Setup_Wizard_Service_Provider;
use \Gravity_Forms\Gravity_Forms\Setup_Wizard\Endpoints\GF_Setup_Wizard_Endpoint_Save_Prefs;
/**
* Class GFCommon
*
* Includes common methods accessed throughout Gravity Forms and add-ons.
*/
class GFCommon {
private static $plugins;
private static $license_info;
/**
* An array of event start times set by GFCommon::timer_start().
*
* @since 2.7.1
*
* @var float[]
*/
private static $start_times = array();
// 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]{0,3}(?:,?[0-9]{3})*(?:\.[0-9]{1,2})?)$/", $value ) || preg_match( "/^(-?[0-9]{0,3}(?:\.?[0-9]{3})*(?:,[0-9]{2})?)$/", $value );
}
}
/**
* Determines if the current page is the block editor.
*
* @since 2.7
*
* @return bool Returns true if the current page is the block editor. Returns false otherwise.
*/
public static function is_block_editor_page() {
if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) {
return true;
}
if ( ! function_exists( 'get_current_screen' ) ) {
return false;
}
$current_screen = get_current_screen();
if ( is_callable( array( $current_screen, 'is_block_editor' ) ) && $current_screen->is_block_editor() ) {
return true;
}
return false;
}
/**
* Removes the currency symbol from the supplied value.
*
* @since unknown
* @since 2.4.18 Updated to support the currency code being passed for the $currency param.
*
* @param string $value The value to be cleaned.
* @param null|array|string $currency Null to use the default currency, an array of currency properties, or the currency code.
*
* @return string
*/
public static function remove_currency_symbol( $value, $currency = null ) {
if ( empty( $value ) ) {
return $value;
}
if ( ! is_array( $currency ) ) {
$code = empty( $currency ) ? GFCommon::get_currency() : $currency;
if ( empty( $code ) ) {
$code = 'USD';
}
$currency = RGCurrency::get_currency( $code );
}
if ( ! empty( $currency['symbol_left'] ) ) {
$value = str_replace( $currency['symbol_left'], '', $value );
}
if ( ! empty( $currency['symbol_right'] ) ) {
$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 ) {
$dir = untrailingslashit( $dir );
if ( ! is_dir( $dir ) || ! wp_is_writable( $dir ) || is_link( $dir ) ) {
GFCommon::log_debug( __METHOD__ . '(): Path ' . $dir . ' is not a valid path or is not writable' );
return;
}
if ( ! ( $dp = opendir( $dir ) ) ) {
GFCommon::log_debug( __METHOD__ . '(): Unable to open directory: ' . $dir );
return;
}
// ignores all errors
set_error_handler( '__return_false', E_ALL );
//creates an empty index.html file
$index_file_path = $dir . '/index.html';
GFCommon::log_debug( __METHOD__ . '(): Adding file: ' . $index_file_path );
if ( $f = fopen( $index_file_path, '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>
<IfModule headers_module>
Header set X-Robots-Tag "noindex"
</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 );
}
/**
* Decode JSON string to array.
*
* @since 2.5
*
* @param string $value JSON string.
*
* @return array|string
*/
public static function maybe_decode_json( $value ) {
if ( self::is_json( $value ) ) {
return json_decode( $value, ARRAY_A );
}
return $value;
}
/**
* Determines if provided string is a JSON object.
*
* @since 2.5
*
* @param string $string JSON string.
*
* @return bool
*/
public static function is_json( $string ) {
if ( is_string( $string ) && in_array( substr( $string, 0, 1 ), array( '{', '[' ) ) && is_array( json_decode( $string, ARRAY_A ) ) ) {
return true;
}
return false;
}
/**
* Strips HTML tags using wp_strip_all_tags from a string that may contain unicode.
*
* @since 2.8.5
*
* @param string $string JSON string.
*
* @return string
*/
public static function strip_all_tags_from_json_string( $string ) {
$decoded_json = json_decode( $string, true );
$reencoded_json = json_encode( $decoded_json );
return wp_strip_all_tags( $reencoded_json );
}
//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, without trailing slash.
*
* @since unknown
* @since 2.6.2 Updated to use GF_PLUGIN_DIR_PATH.
*
* @return string
*/
public static function get_base_path() {
return untrailingslashit( GF_PLUGIN_DIR_PATH );
}
/**
* Returns the URL to an image within the plugin's image directory.
*
* @since 2.5
*
* @param string $image_path The image path, including subdirectories if needed.
*
* @return string
*/
public static function get_image_url( $image_path ) {
$base = untrailingslashit( self::get_base_url() );
return sprintf( '%s/images/%s', $base, $image_path );
}
/**
* Returns the path to an image within the plugin's image directory.
*
* @since 2.5
*
* @param string $image_path The image path, including subdirectories if needed.
*
* @return string
*/
public static function get_image_path( $image_path ) {
$base = untrailingslashit( self::get_base_path() );
return sprintf( '%s/images/%s', $base, $image_path );
}
/**
* Returns the URL to an font file within the plugin's fonts directory.
*
* @since 2.5
*
* @param string $font_path The font path, including subdirectories if needed.
*
* @return string
*/
public static function get_font_url( $font_path ) {
$base = untrailingslashit( self::get_base_url() );
return sprintf( '%s/fonts/%s', $base, $font_path );
}
/**
* Returns the path to an font file within the plugin's fonts directory.
*
* @since 2.5
*
* @param string $font_path The font path, including subdirectories if needed.
*
* @return string
*/
public static function get_font_path( $font_path ) {
$base = untrailingslashit( self::get_base_path() );
return sprintf( '%s/fonts/%s', $base, $font_path );
}
/**
* Returns an array of files/directories which match the supplied pattern.
*
* @since 2.4.15
*
* @param string $pattern The pattern to be appended to the base path when performing the search.
* @param string $base_path The base path. Defaults to the plugin's root folder.
*
* @return array|false
*/
public static function glob( $pattern, $base_path = '' ) {
if ( empty( $base_path ) ) {
$base_path = self::get_base_path();
}
// Escape any brackets in the base path.
$base_path = str_replace( array( '[', ']' ), array( '\[', '\]' ), $base_path );
$base_path = str_replace( array( '\[', '\]' ), array( '[[]', '[]]' ), $base_path );
return glob( $base_path . $pattern );
}
/**
* Requires and returns an array of files which match the supplied pattern.
*
* @since 2.4.15
*
* @param string $pattern The pattern to be appended to the base path when performing the search.
* @param string $base_path The base path. Defaults to the plugin's root folder.
*
* @return array|false
*/
public static function glob_require_once( $pattern, $base_path = '' ) {
$files = self::glob( $pattern, $base_path );
if ( is_array( $files ) ) {
foreach ( $files as $file ) {
require_once $file;
}
}
return $files;
}
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://docs.gravityforms.com/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 = preg_match( "/^(https?:\/\/)/i", $url );
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://docs.gravityforms.com/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( trim( $email ) );
}
public static function is_valid_email_list( $email_list ) {
$emails = explode( ',', $email_list );
if ( ! is_array( $emails ) ) {
return false;
}
// Trim values.
$emails = array_map( 'trim', $emails );
foreach ( $emails as $email ) {
if ( ! self::is_valid_email( $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 esc_attr_e( $group_label ); ?>">
<?php } ?>
<?php foreach ( $tags as $tag ) { ?>
<option value="<?php esc_attr_e( $tag['tag'] ); ?>"><?php esc_html_e( $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 : rgget( 'id' );
$form_id = absint( $form_id );
$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 data-js-reload="gforms-calculation-variables" id="<?php echo esc_attr( $element_id ); ?>_variable_select" class="<?php echo esc_attr( $class ); ?>" onchange="<?php echo $onchange; ?>">
<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