-
Notifications
You must be signed in to change notification settings - Fork 39
/
class-gfsimpleaddon.php
472 lines (416 loc) · 13.5 KB
/
class-gfsimpleaddon.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
<?php
GFForms::include_addon_framework();
class GFSimpleAddOn extends GFAddOn {
protected $_version = GF_SIMPLE_ADDON_VERSION;
protected $_min_gravityforms_version = '1.9';
protected $_slug = 'simpleaddon';
protected $_path = 'simpleaddon/simpleaddon.php';
protected $_full_path = __FILE__;
protected $_title = 'Gravity Forms Simple Add-On';
protected $_short_title = 'Simple Add-On';
private static $_instance = null;
/**
* Get an instance of this class.
*
* @return GFSimpleAddOn
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new GFSimpleAddOn();
}
return self::$_instance;
}
/**
* Handles hooks and loading of language files.
*/
public function init() {
parent::init();
add_filter( 'gform_submit_button', array( $this, 'form_submit_button' ), 10, 2 );
add_action( 'gform_after_submission', array( $this, 'after_submission' ), 10, 2 );
}
// # SCRIPTS & STYLES -----------------------------------------------------------------------------------------------
/**
* Return the scripts which should be enqueued.
*
* @return array
*/
public function scripts() {
$scripts = array(
array(
'handle' => 'my_script_js',
'src' => $this->get_base_url() . '/js/my_script.js',
'version' => $this->_version,
'deps' => array( 'jquery' ),
'strings' => array(
'first' => esc_html__( 'First Choice', 'simpleaddon' ),
'second' => esc_html__( 'Second Choice', 'simpleaddon' ),
'third' => esc_html__( 'Third Choice', 'simpleaddon' )
),
'enqueue' => array(
array(
'admin_page' => array( 'form_settings' ),
'tab' => 'simpleaddon'
)
)
),
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Return the stylesheets which should be enqueued.
*
* @return array
*/
public function styles() {
$styles = array(
array(
'handle' => 'my_styles_css',
'src' => $this->get_base_url() . '/css/my_styles.css',
'version' => $this->_version,
'enqueue' => array(
array( 'field_types' => array( 'poll' ) )
)
)
);
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
*/
function form_submit_button( $button, $form ) {
$settings = $this->get_form_settings( $form );
if ( isset( $settings['enabled'] ) && true == $settings['enabled'] ) {
$text = $this->get_plugin_setting( 'mytextbox' );
$button = "<div>{$text}</div>" . $button;
}
return $button;
}
// # ADMIN FUNCTIONS -----------------------------------------------------------------------------------------------
/**
* Creates a custom page for this add-on.
*/
public function plugin_page() {
echo 'This page appears in the Forms menu';
}
/**
* Configures the settings which should be rendered on the add-on settings tab.
*
* @return array
*/
public function plugin_settings_fields() {
return array(
array(
'title' => esc_html__( 'Simple Add-On Settings', 'simpleaddon' ),
'fields' => array(
array(
'name' => 'mytextbox',
'tooltip' => esc_html__( 'This is the tooltip', 'simpleaddon' ),
'label' => esc_html__( 'This is the label', 'simpleaddon' ),
'type' => 'text',
'class' => 'small',
'feedback_callback' => array( $this, 'is_valid_setting' ),
)
)
)
);
}
/**
* Configures the settings which should be rendered on the Form Settings > Simple Add-On tab.
*
* @return array
*/
public function form_settings_fields( $form ) {
return array(
array(
'title' => esc_html__( 'Simple Form Settings', 'simpleaddon' ),
'fields' => array(
array(
'label' => esc_html__( 'My checkbox', 'simpleaddon' ),
'type' => 'checkbox',
'name' => 'enabled',
'tooltip' => esc_html__( 'This is the tooltip', 'simpleaddon' ),
'choices' => array(
array(
'label' => esc_html__( 'Enabled', 'simpleaddon' ),
'name' => 'enabled',
),
),
),
array(
'label' => esc_html__( 'My checkboxes', 'simpleaddon' ),
'type' => 'checkbox',
'name' => 'checkboxgroup',
'tooltip' => esc_html__( 'This is the tooltip', 'simpleaddon' ),
'choices' => array(
array(
'label' => esc_html__( 'First Choice', 'simpleaddon' ),
'name' => 'first',
),
array(
'label' => esc_html__( 'Second Choice', 'simpleaddon' ),
'name' => 'second',
),
array(
'label' => esc_html__( 'Third Choice', 'simpleaddon' ),
'name' => 'third',
),
),
),
array(
'label' => esc_html__( 'My Radio Buttons', 'simpleaddon' ),
'type' => 'radio',
'name' => 'myradiogroup',
'tooltip' => esc_html__( 'This is the tooltip', 'simpleaddon' ),
'choices' => array(
array(
'label' => esc_html__( 'First Choice', 'simpleaddon' ),
),
array(
'label' => esc_html__( 'Second Choice', 'simpleaddon' ),
),
array(
'label' => esc_html__( 'Third Choice', 'simpleaddon' ),
),
),
),
array(
'label' => esc_html__( 'My Horizontal Radio Buttons', 'simpleaddon' ),
'type' => 'radio',
'horizontal' => true,
'name' => 'myradiogrouph',
'tooltip' => esc_html__( 'This is the tooltip', 'simpleaddon' ),
'choices' => array(
array(
'label' => esc_html__( 'First Choice', 'simpleaddon' ),
),
array(
'label' => esc_html__( 'Second Choice', 'simpleaddon' ),
),
array(
'label' => esc_html__( 'Third Choice', 'simpleaddon' ),
),
),
),
array(
'label' => esc_html__( 'My Dropdown', 'simpleaddon' ),
'type' => 'select',
'name' => 'mydropdown',
'tooltip' => esc_html__( 'This is the tooltip', 'simpleaddon' ),
'choices' => array(
array(
'label' => esc_html__( 'First Choice', 'simpleaddon' ),
'value' => 'first',
),
array(
'label' => esc_html__( 'Second Choice', 'simpleaddon' ),
'value' => 'second',
),
array(
'label' => esc_html__( 'Third Choice', 'simpleaddon' ),
'value' => 'third',
),
),
),
array(
'label' => esc_html__( 'My Text Box', 'simpleaddon' ),
'type' => 'text',
'name' => 'mytext',
'tooltip' => esc_html__( 'This is the tooltip', 'simpleaddon' ),
'class' => 'medium',
'feedback_callback' => array( $this, 'is_valid_setting' ),
),
array(
'label' => esc_html__( 'My Text Area', 'simpleaddon' ),
'type' => 'textarea',
'name' => 'mytextarea',
'tooltip' => esc_html__( 'This is the tooltip', 'simpleaddon' ),
'class' => 'medium merge-tag-support mt-position-right',
),
array(
'label' => esc_html__( 'My Hidden Field', 'simpleaddon' ),
'type' => 'hidden',
'name' => 'myhidden',
),
array(
'label' => esc_html__( 'My Custom Field', 'simpleaddon' ),
'type' => 'my_custom_field_type',
'name' => 'my_custom_field',
'args' => array(
'text' => array(
'label' => esc_html__( 'A textbox sub-field', 'simpleaddon' ),
'name' => 'subtext',
'default_value' => 'change me',
),
'checkbox' => array(
'label' => esc_html__( 'A checkbox sub-field', 'simpleaddon' ),
'name' => 'my_custom_field_check',
'choices' => array(
array(
'label' => esc_html__( 'Activate', 'simpleaddon' ),
'name' => 'subcheck',
'default_value' => true,
),
),
),
),
),
array(
'label' => esc_html__( 'Simple condition', 'simpleaddon' ),
'type' => 'custom_logic_type',
'name' => 'custom_logic',
),
array(
'label' => esc_html__( 'Field Select', 'simpleaddon' ),
'type' => 'field_select',
'name' => 'my_field',
),
),
),
);
}
/**
* Define the markup for the my_custom_field_type type field.
*
* @param array $field The field properties.
* @param bool|true $echo Should the setting markup be echoed.
*/
public function settings_my_custom_field_type( $field, $echo = true ) {
echo '<div>' . esc_html__( 'My custom field contains a few settings:', 'simpleaddon' ) . '</div>';
// get the text field settings from the main field and then render the text field
$text_field = $field['args']['text'];
$this->settings_text( $text_field );
// get the checkbox field settings from the main field and then render the checkbox field
$checkbox_field = $field['args']['checkbox'];
$this->settings_checkbox( $checkbox_field );
}
// # SIMPLE CONDITION EXAMPLE --------------------------------------------------------------------------------------
/**
* Define the markup for the custom_logic_type type field.
*
* @param array $field The field properties.
* @param bool|true $echo Should the setting markup be echoed.
*/
public function settings_custom_logic_type( $field, $echo = true ) {
// Get the setting name.
$name = $field['name'];
// Define the properties for the checkbox to be used to enable/disable access to the simple condition settings.
$checkbox_field = array(
'name' => $name,
'type' => 'checkbox',
'choices' => array(
array(
'label' => esc_html__( 'Enabled', 'simpleaddon' ),
'name' => $name . '_enabled',
),
),
'onclick' => "if(this.checked){jQuery('#{$name}_condition_container').show();} else{jQuery('#{$name}_condition_container').hide();}",
);
// Determine if the checkbox is checked, if not the simple condition settings should be hidden.
$is_enabled = $this->get_setting( $name . '_enabled' ) == '1';
$container_style = ! $is_enabled ? "style='display:none;'" : '';
// Put together the field markup.
$str = sprintf( "%s<div id='%s_condition_container' %s>%s</div>",
$this->settings_checkbox( $checkbox_field, false ),
$name,
$container_style,
$this->simple_condition( $name )
);
echo $str;
}
/**
* Build an array of choices containing fields which are compatible with conditional logic.
*
* @return array
*/
public function get_conditional_logic_fields() {
$form = $this->get_current_form();
$fields = array();
foreach ( $form['fields'] as $field ) {
if ( $field->is_conditional_logic_supported() ) {
$inputs = $field->get_entry_inputs();
if ( $inputs ) {
$choices = array();
foreach ( $inputs as $input ) {
if ( rgar( $input, 'isHidden' ) ) {
continue;
}
$choices[] = array(
'value' => $input['id'],
'label' => GFCommon::get_label( $field, $input['id'], true )
);
}
if ( ! empty( $choices ) ) {
$fields[] = array( 'choices' => $choices, 'label' => GFCommon::get_label( $field ) );
}
} else {
$fields[] = array( 'value' => $field->id, 'label' => GFCommon::get_label( $field ) );
}
}
}
return $fields;
}
/**
* Evaluate the conditional logic.
*
* @param array $form The form currently being processed.
* @param array $entry The entry currently being processed.
*
* @return bool
*/
public function is_custom_logic_met( $form, $entry ) {
if ( $this->is_gravityforms_supported( '2.0.7.4' ) ) {
// Use the helper added in Gravity Forms 2.0.7.4.
return $this->is_simple_condition_met( 'custom_logic', $form, $entry );
}
// Older version of Gravity Forms, use our own method of validating the simple condition.
$settings = $this->get_form_settings( $form );
$name = 'custom_logic';
$is_enabled = rgar( $settings, $name . '_enabled' );
if ( ! $is_enabled ) {
// The setting is not enabled so we handle it as if the rules are met.
return true;
}
// Build the logic array to be used by Gravity Forms when evaluating the rules.
$logic = array(
'logicType' => 'all',
'rules' => array(
array(
'fieldId' => rgar( $settings, $name . '_field_id' ),
'operator' => rgar( $settings, $name . '_operator' ),
'value' => rgar( $settings, $name . '_value' ),
),
)
);
return GFCommon::evaluate_conditional_logic( $logic, $form, $entry );
}
/**
* Performing a custom action at the end of the form submission process.
*
* @param array $entry The entry currently being processed.
* @param array $form The form currently being processed.
*/
public function after_submission( $entry, $form ) {
// Evaluate the rules configured for the custom_logic setting.
$result = $this->is_custom_logic_met( $form, $entry );
if ( $result ) {
// Do something awesome because the rules were met.
}
}
// # HELPERS -------------------------------------------------------------------------------------------------------
/**
* The feedback callback for the 'mytextbox' setting on the plugin settings page and the 'mytext' setting on the form settings page.
*
* @param string $value The setting value.
*
* @return bool
*/
public function is_valid_setting( $value ) {
return strlen( $value ) < 10;
}
}