Skip to content

Commit be1a4c6

Browse files
committed
first viable version
0 parents commit be1a4c6

File tree

6 files changed

+221
-0
lines changed

6 files changed

+221
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## 1.0.0 / 2018-11-19
4+
5+
- Initial release

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Matt Mirus
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Gravity Forms Data Attributes
2+
3+
- [Usage](#usage)
4+
- [Filters](#filters)
5+
- [Installation](#installation)
6+
- [Screenshots](#screenshots)
7+
8+
...
9+
10+
## Usage
11+
12+
...
13+
14+
## Filters
15+
16+
...
17+
18+
## Installation
19+
20+
There are three options for installing this plugin:
21+
22+
1. With composer from [Packagist](https://packagist.org/packages/mmirus/gravity-forms-data-attributes): `composer require mmirus/gravity-forms-data-attributes`
23+
2. With [GitHub Updater](https://github.com/afragen/github-updater)
24+
3. By downloading the latest release ZIP from this repository and installing it like any normal WordPress plugin
25+
26+
## Screenshots
27+
28+
_description_
29+
30+
![Alt Text](/screenshots/file.png)

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "mmirus/gravity-forms-data-attributes",
3+
"description": "Display the content of a field in the title bar of your Advanced Custom Fields flexible content sections.",
4+
"type": "wordpress-plugin",
5+
"license": "MIT",
6+
"homepage": "https://github.com/mmirus/gravity-forms-data-attributes",
7+
"authors": [
8+
{
9+
"name": "Matt Mirus",
10+
"email": "matt@mattmirus.com",
11+
"homepage": "https://github.com/mmirus"
12+
}
13+
],
14+
"keywords": [
15+
"wordpress",
16+
"advanced custom fields",
17+
"acf"
18+
],
19+
"support": {
20+
"issues": "https://github.com/mmirus/gravity-forms-data-attributes/issues"
21+
},
22+
"require": {
23+
"php": ">=5.3.0",
24+
"composer/installers": "^1.6"
25+
}
26+
}

gravity-forms-data-attributes.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/*
3+
Plugin Name: Gravity Forms Data Attributes
4+
Plugin URI: https://github.com/mmirus/gravity-forms-data-attributes
5+
Description: Add custom data attributes to your form inputs
6+
Author: Matt Mirus
7+
Author URI: https://github.com/mmirus
8+
Version: 1.0.0
9+
GitHub Plugin URI: https://github.com/mmirus/gravity-forms-data-attributes
10+
*/
11+
12+
add_action('gform_field_standard_settings', function ($position, $form_id) {
13+
if ($position === 1350) :
14+
?>
15+
<li class="enable_data_attrs_setting field_setting">
16+
<input type="checkbox" id="field_enable_data_attrs_value" onclick="ToggleDataAttrs();" />
17+
<label for="field_enable_data_attrs_value" class="inline">
18+
<?php esc_html_e('Enable Data Attributes', 'gravityforms'); ?>
19+
</label>
20+
21+
<div id="gform_data_attrs" style="display:none;">
22+
<br>
23+
<label for="field_data_attrs" class="section_label">
24+
<?php esc_html_e('Data Attribute Names', 'gravityforms'); ?>
25+
<?php gform_tooltip('form_field_data_attrs') ?>
26+
</label>
27+
<textarea id="field_data_attrs" class="fieldwidth-3 fieldheight-2" oninput="SetFieldProperty('dataAttrsField', this.value);"></textarea>
28+
<em style="display:block;">You must save the form after changing this setting.</em>
29+
30+
<div id="gform_data_attr_inputs"></div>
31+
</div>
32+
</li>
33+
<?php
34+
endif;
35+
}, 10, 2);
36+
37+
add_action('gform_editor_js', function () {
38+
?>
39+
<script type='text/javascript'>
40+
// show/hide data attributes textarea
41+
function ToggleDataAttrs(isInit){
42+
var speed = isInit ? "" : "slow";
43+
44+
if(jQuery("#field_enable_data_attrs_value").is(":checked")){
45+
jQuery("#gform_data_attrs").show(speed);
46+
47+
SetFieldProperty('enableDataAttrsField', true);
48+
}
49+
else{
50+
jQuery("#gform_data_attrs").hide(speed);
51+
SetFieldProperty('enableDataAttrsField', false);
52+
SetFieldProperty('dataAttrsField', '');
53+
}
54+
}
55+
56+
// make custom settings availalbe to all field types
57+
for (i in fieldSettings) {
58+
fieldSettings[i] += ', .enable_data_attrs_setting, .data_attrs_setting';
59+
}
60+
61+
// initialize our custom settings on the field settings load event
62+
jQuery(document).on('gform_load_field_settings', function(event, field, form){
63+
jQuery('#field_enable_data_attrs_value').attr('checked', field.enableDataAttrsField == true);
64+
ToggleDataAttrs(true);
65+
jQuery('#field_data_attrs').val(field.dataAttrsField);
66+
67+
if (['checkbox', 'radio'].includes(field.type)) return;
68+
69+
var dataAttrsInputContainer = jQuery('#gform_data_attr_inputs');
70+
71+
var dataAttrs = field.dataAttrsField
72+
73+
if (!dataAttrs) return '';
74+
75+
dataAttrs = dataAttrs.split("\n").map(function(name) {
76+
return {
77+
name: name,
78+
value: field[name] || ''
79+
};
80+
});
81+
82+
var inputs = dataAttrs.map(function(dataAttr) {
83+
return "<br><label class='section_label'>" + dataAttr.name + "</label><input type='text' id='" + dataAttr.name + "' value='" + dataAttr.value + "' class='field-" + dataAttr.name + " field-data-attr' data-attr-name='" + dataAttr.name + "' /><br>";
84+
}).join('');
85+
86+
dataAttrsInputContainer.append(inputs);
87+
});
88+
89+
// save data attribute values (general)
90+
jQuery('#gform_data_attr_inputs').on('input propertychange', '.field-data-attr', function () {
91+
var $this = jQuery(this);
92+
93+
field = GetSelectedField();
94+
field[$this.data('attrName')] = $this.val();
95+
});
96+
97+
// save data attribute values ()checkbox and radio fields)
98+
jQuery('.choices_setting').on('input propertychange', '.field-choice-data-attr', function () {
99+
var $this = jQuery(this);
100+
var i = $this.closest('li.field-choice-row').data('index');
101+
102+
field = GetSelectedField();
103+
field.choices[i][$this.data('attrName')] = $this.val();
104+
});
105+
106+
// add data attribute fields to checkbox / radio choices
107+
gform.addFilter('gform_append_field_choice_option', function (str, field, i) {
108+
var inputType = GetInputType(field);
109+
var custom = field.choices[i].custom ? field.choices[i].custom : '';
110+
111+
var dataAttrs = field.dataAttrsField
112+
113+
if (!dataAttrs) return '';
114+
115+
dataAttrs = dataAttrs.split("\n").map(function(name) {
116+
return {
117+
name: name,
118+
value: field.choices[i][name] || ''
119+
};
120+
});
121+
122+
// TODO remove inline styles from below?
123+
var inputs = dataAttrs.map(function(dataAttr) {
124+
var id = inputType + "_choice_" + dataAttr.name + "_" + i;
125+
return "<label style='width:155px;margin:10px 0 0;'>" + dataAttr.name + " <input type='text' id='" + id + "' value='" + dataAttr.value + "' class='field-choice-input field-choice-" + dataAttr.name + " field-choice-data-attr' data-attr-name='" + dataAttr.name + "' /></label>";
126+
}).join('');
127+
128+
return "<div style='display:flex; flex-wrap:wrap; margin-left:35px;'>" + inputs + "</div>";
129+
});
130+
</script>
131+
<?php
132+
133+
});
134+
135+
add_filter('gform_tooltips', function ($tooltips) {
136+
$tooltips['form_field_data_attrs'] = "<h6>Data Attribute Names</h6><p>Enter the names of the data attributes you wish to enable, one per line.</p><p>You must save the form after changing this setting.</p>";
137+
return $tooltips;
138+
});

0 commit comments

Comments
 (0)