-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-bootstrap-gravityforms.php
137 lines (119 loc) · 4.23 KB
/
class-bootstrap-gravityforms.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
<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Bootstrap_Gravity_Forms' ) ) {
/**
* 'Bootstrap_Gravity_Forms.
*/
class Bootstrap_Gravity_Forms {
/**
* Construct
*
* @access public
* @return void
*/
function __construct() {
add_filter( 'gform_field_content', array( $this, 'add_bootstrap_classes_to_fields' ), 10, 5 );
add_filter( 'gform_submit_button', array( $this, 'add_bootstrap_classes_to_submit' ), 10, 2 );
// add_filter( 'gform_field_container', array( $this, 'bootstrapify_container' ), 10, 6 );
}
/**
* Add Bootstrap Classes to Form Fields.
*
* @access public
* @param mixed $content Content.
* @param mixed $field Field.
* @param mixed $value Value.
* @param mixed $lead_id Lead ID.
* @param mixed $form_id Form ID.
* @return void
*/
function add_bootstrap_classes_to_fields( $content, $field, $value, $lead_id, $form_id ) {
// Bootstrapify it..
$class = 'form-control';
if( $field->type == 'website' || $field->type == 'email' ){
$class .= ' medium';
}
switch( $field->type ){
// Forms that by default have classes.
case 'text':
case 'textarea':
case 'select':
case 'multiselect':
case 'number':
case 'phone':
case 'post_title':
case 'post_content':
case 'post_excerpt':
case 'post_tags':
case 'post_custom_field':
case 'quantity':
$content = str_replace( 'small', $class . ' small', $content );
$content = str_replace( 'medium', $class . ' medium', $content );
$content = str_replace( 'large', $class . ' large', $content );
break;
// Forms that do not have classes (by default).
case 'address':
case 'post_category':
case 'option':
case 'time':
// Bottom one will be hit for the else.
$content = str_replace( '<select', '<select class="' . $class . '"', $content );
case 'website':
case 'email':
case 'name':
$content = str_replace( 'input type=', 'input class="' . $class . '" type=', $content );
break;
case 'radio':
case 'checkbox':
$content = str_replace( '<li', '<div', $content );
$content = str_replace( 'class=\'', 'class=\'form-check ', $content );
$content = str_replace( '</li>', '</div>', $content );
$content = str_replace( '<label', '<label class="form-check-label"', $content );
$content = str_replace( '<input', '<input class="form-check-input" style="margin-left: -1.25rem;"', $content );
break;
case 'date':
$content = str_replace( 'datepicker', $class . ' datepicker', $content );
break;
case 'post_image':
$content = str_replace( 'ginput_container_post_image', 'ginput_container_post_image custom-file', $content );
$content = str_replace( 'ginput_complex', '', $content );
$content = str_replace( '</span', '<label class="custom-file-label">Choose File</label></span', $content );
$content = str_replace( 'medium', 'medium custom-file-input', $content );
break;
case 'fileupload':
$content = str_replace( 'ginput_container_fileupload', 'ginput_container_fileupload custom-file', $content );
$content = str_replace( '<span', '<label class="custom-file-label">Choose File</label><span', $content );
$content = str_replace( 'medium', 'medium custom-file-input', $content );
break;
case 'list':
$content = str_replace( '<input type', '<input class="form-control" type', $content );
break;
case 'product':
case 'shipping':
case 'total':
// Other cases...
break;
}
return $content;
}
/**
* Add Bootstrap classes to Submit Button.
*
* @access public
* @param mixed $button Button.
* @param mixed $form Form.
* @return void
*/
function add_bootstrap_classes_to_submit( $button, $form ) {
return '<input type="submit" class="btn btn-primary btn-gform" id="gform_submit_button_' . $form['id'] . '" value="' . $form['button']['text'] . '">';
}
function bootstrapify_container( $field_container, $field, $form, $css_class, $style, $field_content ){
// $field_container = str_replace( '{FIELD_CONTENT}', '<div class="form-group row">{FIELD_CONTENT}</div>', $field_container );
return $field_container;
}
}
new Bootstrap_Gravity_Forms();
}