-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclass-gfsliderfields.php
334 lines (278 loc) · 11.6 KB
/
class-gfsliderfields.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
<?php
GFForms::include_addon_framework();
class GFSliderFields extends GFAddOn {
protected $_version = GF_SLIDER_FIELDS_VERSION;
protected $_min_gravityforms_version = '1.9';
protected $_slug = 'slider-fields';
protected $_path = 'gravity-slider-fields/gravity-slider-fields.php';
protected $_full_path = __FILE__;
protected $_title = 'Gravity Slider Fields';
protected $_short_title = 'Gravity Slider Fields';
private static $_instance = null;
/**
* Get an instance of this class.
*
* @return GFCustomEntryLimit
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new GFSliderFields();
}
return self::$_instance;
}
/**
* Handles hooks and loading of language files.
*/
public function init() {
parent::init();
load_plugin_textdomain( 'typewheel', false, basename( dirname( __file__ ) ) . '/languages/' );
// creating the slider field
add_action( 'gform_editor_js_set_default_values', array( $this, 'set_defaults' ) );
add_action( 'gform_editor_js', array( $this, 'editor_js' ) );
add_filter( 'gform_field_standard_settings' , array( $this, 'slider_settings' ) , 10, 2 );
// add tooltips
$this->add_tooltip( 'slider_value_relations', sprintf(
'<h6>%s</h6> %s',
__( 'Value Relations', 'typewheel' ),
__( 'Enter descriptive terms that relate to the min and max number values of the slider.', 'typewheel' )
) );
$this->add_tooltip( 'slider_step', sprintf(
'<h6>%s</h6> %s',
__( 'Step', 'typewheel' ),
__( 'Enter a value that each interval or step will take between the min and max of the slider. The full specified value range of the slider (max - min) should be evenly divisible by the step and the step should not exceed a precision of two decimal places. (default: 1)', 'typewheel' )
) );
$this->add_tooltip( 'slider_value_visibility', sprintf(
'<h6>%s</h6> %s',
__( 'Value Visibility', 'typewheel' ),
__( 'Select whether to hide, show on hover & drag, or always show the currently selected value.', 'typewheel' )
) );
$this->add_tooltip( 'slider_connect', sprintf(
'<h6>%s</h6> %s',
__( 'Connecting Elements', 'typewheel' ),
__( 'Select whether to visually connect the handle to the upper or lower edge of the slider.', 'typewheel' )
) );
add_filter( 'gform_custom_merge_tags', array( $this, 'slider_calculation_merge_tags' ), 10, 4 );
// control submission & rendering of form_settings
add_filter( 'gform_pre_submission_filter', array( $this, 'pre_submission_filter' ) );
( isset( $_GET['page'] ) && 'gf_entries' == $_GET['page'] ) ? add_filter( 'gform_admin_pre_render', array( $this, 'pre_submission_filter' ) ) : FALSE;
}
// # SCRIPTS & STYLES -----------------------------------------------------------------------------------------------
/**
* Return the scripts which should be enqueued.
*
* @return array
*/
public function scripts() {
$scripts = array(
array(
'handle' => 'noUiSlider',
'src' => $this->get_base_url() . '/noUiSlider/nouislider.min.js',
'version' => $this->_version,
'deps' => array( 'jquery' ),
'enqueue' => array(
array( 'admin_page' => array( 'form_settings' ) ),
array( 'field_types' => array( 'slider' ) )
)
),
array(
'handle' => 'wNumb',
'src' => $this->get_base_url() . '/wNumb/wNumb.js',
'version' => $this->_version,
'deps' => array( 'jquery' ),
'enqueue' => array(
array( 'admin_page' => array( 'form_settings' ) ),
array( 'field_types' => array( 'slider' ) )
)
),
array(
'handle' => 'slider_fields',
'src' => $this->get_base_url() . '/js/slider.js',
'version' => $this->_version,
'deps' => array( 'jquery', 'noUiSlider', 'wNumb' ),
'enqueue' => array(
array( 'admin_page' => array( 'form_settings' ) ),
array( 'field_types' => array( 'slider' ) )
)
),
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Return the stylesheets which should be enqueued.
*
* @return array
*/
public function styles() {
$styles = array(
array(
'handle' => 'noUiSlider',
'src' => $this->get_base_url() . '/noUiSlider/nouislider.min.css',
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_settings' ) ),
array( 'field_types' => array( 'slider' ) )
)
),
array(
'handle' => 'slider_fields',
'src' => $this->get_base_url() . '/css/slider.css',
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_settings' ) ),
array( 'field_types' => array( 'slider' ) )
)
)
);
return array_merge( parent::styles(), $styles );
}
// # FRONTEND FUNCTIONS --------------------------------------------------------------------------------------------
/**
* Add the text in the plugin settings to the bottom of the form if enabled for this form.
*
* @param string $button The string containing the input tag to be filtered.
* @param array $form The form currently being displayed.
*
* @return string
*/
// # ADMIN FUNCTIONS -----------------------------------------------------------------------------------------------
/**
* Set default values when adding a slider
*
* @since 0.1
*/
function set_defaults() {
?>
case "slider" :
field.label = "Untitled";
field.numberFormat = "decimal_dot";
field.rangeMin = 0;
field.rangeMax = 10;
field.slider_step = 1;
field.slider_value_visibility = "hidden";
field.slider_connect = "none";
break;
<?php
} // end set_defaults
/**
* Execute javascript for proper loading of field
*
* @since 0.1
*/
function editor_js() {
?>
<script type='text/javascript'>
jQuery(document).ready(function($) {
// Bind to the load field settings event to initialize the slider settings
$(document).bind("gform_load_field_settings", function(event, field, form){
jQuery("#slider_min_value_relation").val(field['slider_min_value_relation']);
jQuery("#slider_max_value_relation").val(field['slider_max_value_relation']);
jQuery("#slider_step").val(field['slider_step']);
jQuery("#slider_value_visibility").val(field['slider_value_visibility']);
jQuery("#slider_connect").val(field['slider_connect']);
});
});
</script>
<?php
} // end editor_js
/**
* Render custom options for the field
*
* @since 0.1
*/
function slider_settings( $position, $form_id ) {
// Create settings on position 1550 (right after range option)
if ( 1550 == $position ) {
?>
<li class="slider_value_relations field_setting">
<div style="clear:both;">
<?php _e( 'Value Relations', 'gsf-locale' ); ?>
<?php gform_tooltip( 'slider_value_relations' ); ?>
</div>
<div style="width:50%;float:left"><input type="text" id="slider_min_value_relation" style="width:100%;" onchange="SetFieldProperty('slider_min_value_relation', this.value);" /><label for="slider_min_value_relation"><?php _e( 'Min', 'gsf-locale' ); ?></label></div>
<div style="width:50%;float:left"><input type="text" id="slider_max_value_relation" style="width:100%;" onchange="SetFieldProperty('slider_max_value_relation', this.value);" /><label for="slider_max_value_relation"><?php _e( 'Max', 'gsf-locale' ); ?></label></div>
<br class="clear">
</li>
<li class="slider_step field_setting">
<div style="clear:both;">
<?php _e( 'Step', 'gsf-locale' ); ?>
<?php gform_tooltip( 'slider_step' ); ?>
</div>
<div style="width:25%;"><input type="number" id="slider_step" step=".01" style="width:100%;" onchange="SetFieldProperty('slider_step', this.value);" /></div>
</li>
<li class="slider_value_visibility field_setting">
<div style="clear:both;">
<?php _e( 'Show Value', 'gsf-locale' ); ?>
<?php gform_tooltip( 'slider_value_visibility' ); ?>
</div>
<div style="width:25%;">
<select id="slider_value_visibility" onchange="SetFieldProperty('slider_value_visibility', this.value);">
<option value="hidden"><?php _e( 'Hidden', 'gsf-locale' ); ?></option>
<option value="hover-drag"><?php _e( 'Hover/Drag', 'gsf-locale' ); ?></option>
<option value="show"><?php _e( 'Shown', 'gsf-locale' ); ?></option>
</select>
</div>
</li>
<li class="slider_connect field_setting">
<div style="clear:both;">
<?php _e( 'Connect', 'gsf-locale' ); ?>
<?php gform_tooltip( 'slider_connect' ); ?>
</div>
<div style="width:25%;">
<select id="slider_connect" onchange="SetFieldProperty('slider_connect', this.value);">
<option value="none"><?php _e( 'None', 'gsf-locale' ); ?></option>
<option value="lower"><?php _e( 'Lower', 'gsf-locale' ); ?></option>
<option value="upper"><?php _e( 'Upper', 'gsf-locale' ); ?></option>
</select>
</div>
</li>
<?php
}
} // end slider_settings
/**
* Add merge tags to calculation drop down
*
* @since 1.5
*/
function slider_calculation_merge_tags( $merge_tags, $form_id, $fields, $element_id ) {
// check the type of merge tag dropdown
if ( 'field_calculation_formula' != $element_id ) {
return $merge_tags;
}
foreach ( $fields as $field ) {
// check the field type as we only want to generate merge tags for list fields
if ( 'slider' != $field->get_input_type() ) {
continue;
}
$merge_tags[] = array( 'label' => $field->label, 'tag' => '{' . $field->label . ':' . $field->id . '}' );
}
return $merge_tags;
} // END slider_calculation_merge_tags
/**
* Append min/max relation notes to label in notifications, confirmations and entry detail
*
* @since 0.1
*/
function pre_submission_filter( $form ) {
// Loop through form fields
foreach ( $form['fields'] as &$field ) {
// If a slider is found
if ( 'slider' == $field['type'] ) {
// Set default min/max values, if they do not exist for the field
$min = ( isset( $field['rangeMin'] ) && '' != $field['rangeMin'] ) ? $field['rangeMin'] : 0;
$max = ( isset( $field['rangeMax'] ) && '' != $field['rangeMax'] ) ? $field['rangeMax'] : 10;
// If min/max relations exist, append them to the field label
if ( '' != $field['slider_min_value_relation'] || '' != $field['slider_max_value_relation'] ) {
$field['label'] = $field['label'] . ' (' . GFCommon::format_number( $min, $field['numberFormat'] ) . ': ' . $field['slider_min_value_relation'] . ', ' . GFCommon::format_number( $max, $field['numberFormat'] ) . ': ' . $field['slider_max_value_relation'] . ')';
}
}
}
return $form;
} // pre_submission_filter
public function add_tooltip( $key, $content ) {
$this->tooltips[ $key ] = $content;
add_filter( 'gform_tooltips', array( $this, 'load_tooltips' ) );
}
public function load_tooltips( $tooltips ) {
return array_merge( $tooltips, $this->tooltips );
}
}