|
5 | 5 | * |
6 | 6 | * Display visual source and target field indicators next to field labels in the form editor. |
7 | 7 | */ |
8 | | -add_filter( 'gform_admin_pre_render', function( $form ) { |
| 8 | +add_filter( 'gform_field_content', function( $content, $field ) { |
9 | 9 |
|
10 | | - if ( ! class_exists( 'GP_Copy_Cat' ) ) { |
11 | | - return $form; |
| 10 | + if ( ! GFCommon::is_form_editor() || ! class_exists( 'GP_Copy_Cat' ) || ! $field->formId ) { |
| 11 | + return $content; |
12 | 12 | } |
13 | 13 |
|
14 | | - $gpcc = new GP_Copy_Cat(); |
15 | | - $gpcc_fields = $gpcc->get_copy_cat_fields( $form ); |
| 14 | + $gpcc = new GP_Copy_Cat( 'fake.php' ); |
| 15 | + $gpcc_fields = $gpcc->get_copy_cat_fields( GFAPI::get_form( $field->formId ) ); |
16 | 16 | if ( empty( $gpcc_fields ) ) { |
17 | | - return $form; |
| 17 | + return $content; |
18 | 18 | } |
19 | 19 |
|
20 | | - add_filter( 'admin_footer', function() { |
21 | | - ?> |
22 | | - <style> |
23 | | - .gpcc-source .gform-field-label:after { |
24 | | - content: 'GPCC: Source'; |
25 | | - color: #274524; |
26 | | - margin: 0 0.5rem; |
27 | | - background-color: #edf8ec; |
28 | | - border: 1px solid #d7e8d5; |
29 | | - border-radius: 40px; |
30 | | - float: right; |
31 | | - font-size: 0.6875rem; |
32 | | - font-weight: 600; |
33 | | - padding: 0.1125rem 0.4625rem; |
34 | | - } |
35 | | - |
36 | | - .gpcc-target .gform-field-label:after { |
37 | | - content: 'GPCC: Target'; |
38 | | - color: #274524; |
39 | | - margin: 0 0.5rem; |
40 | | - background-color: #edf8ec; |
41 | | - border: 1px solid #d7e8d5; |
42 | | - border-radius: 40px; |
43 | | - float: right; |
44 | | - font-size: 0.6875rem; |
45 | | - font-weight: 600; |
46 | | - padding: 0.1125rem 0.4625rem; |
47 | | - } |
48 | | - </style> |
49 | | - <?php |
50 | | - } ); |
| 20 | + if ( ! has_action( 'admin_footer', 'gpcc_field_indicator_styles' ) ) { |
| 21 | + add_filter( 'admin_footer', 'gpcc_field_indicator_styles' ); |
| 22 | + } |
51 | 23 |
|
52 | 24 | $mappings = array(); |
53 | 25 | foreach ( $gpcc_fields as $_mappings ) { |
54 | 26 | $mappings = array_merge( $_mappings ); |
55 | 27 | } |
56 | 28 |
|
57 | | - foreach ( $form['fields'] as &$field ) { |
58 | | - foreach ( $mappings as $mapping ) { |
59 | | - if ( $field->id == $mapping['source'] ) { |
60 | | - $field->cssClass .= ' gpcc-source'; |
61 | | - } |
62 | | - if ( $field->id == $mapping['target'] ) { |
63 | | - $field->cssClass .= ' gpcc-target'; |
64 | | - } |
| 29 | + $spans = array(); |
| 30 | + |
| 31 | + foreach( $mappings as $mapping ) { |
| 32 | + if ( $field->id == $mapping['source'] ) { |
| 33 | + $spans['source'] = '<span class="gpcc-source gw-field-indicator">GPCC: Source</span>'; |
| 34 | + } |
| 35 | + if ( $field->id == $mapping['target'] ) { |
| 36 | + $spans['target'] = '<span class="gpcc-target gw-field-indicator">GPCC: Target</span>'; |
65 | 37 | } |
66 | 38 | } |
67 | 39 |
|
68 | | - return $form; |
69 | | -} ); |
| 40 | + $search = '<\/label>|<\/legend>'; |
| 41 | + $replace = sprintf( '\0 %s', implode( '', $spans ) ); |
| 42 | + $content = preg_replace( "/$search/", $replace, $content, 1 ); |
| 43 | + |
| 44 | + return $content; |
| 45 | +}, 11, 2 ); |
| 46 | + |
| 47 | +function gpcc_field_indicator_styles() { |
| 48 | + ?> |
| 49 | + <style> |
| 50 | + .gw-field-indicator { |
| 51 | + margin: 0 0 0 0.6875rem; |
| 52 | + background-color: #ecedf8; |
| 53 | + border: 1px solid #d5d7e9; |
| 54 | + border-radius: 40px; |
| 55 | + font-size: 0.6875rem; |
| 56 | + font-weight: 600; |
| 57 | + padding: 0.1125rem 0.4625rem; |
| 58 | + vertical-align: text-top; |
| 59 | + } |
| 60 | + .gw-field-indicator + .gw-field-indicator { |
| 61 | + margin-left: 0; |
| 62 | + } |
| 63 | + .gpcc-source, .gpcc-target { |
| 64 | + color: #274524; |
| 65 | + background-color: #edf8ec; |
| 66 | + border-color: #d7e8d5; |
| 67 | + } |
| 68 | + </style> |
| 69 | + <?php |
| 70 | +} |
0 commit comments