| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Form field manager class |
| 5 |
* |
| 6 |
* @since 1.1.0 |
| 7 |
*/ |
| 8 |
class WeForms_Field_Manager { |
| 9 |
|
| 10 |
/** |
| 11 |
* The fields |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private $fields = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* Going to store WeForms_Notification instance for tmp. Will refactor |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private $notification = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Get all the registered fields |
| 26 |
* |
| 27 |
* @return array |
| 28 |
*/ |
| 29 |
public function get_fields() { |
| 30 |
if ( !empty( $this->fields ) ) { |
| 31 |
return $this->fields; |
| 32 |
} |
| 33 |
|
| 34 |
$this->register_field_types(); |
| 35 |
|
| 36 |
return $this->fields; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Register the field types |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
private function register_field_types() { |
| 45 |
require_once __DIR__ . '/fields/class-abstract-fields.php'; |
| 46 |
require_once __DIR__ . '/fields/class-field-text.php'; |
| 47 |
require_once __DIR__ . '/fields/class-field-name.php'; |
| 48 |
require_once __DIR__ . '/fields/class-field-email.php'; |
| 49 |
require_once __DIR__ . '/fields/class-field-textarea.php'; |
| 50 |
require_once __DIR__ . '/fields/class-field-column.php'; |
| 51 |
require_once __DIR__ . '/fields/class-field-checkbox.php'; |
| 52 |
require_once __DIR__ . '/fields/class-field-radio.php'; |
| 53 |
require_once __DIR__ . '/fields/class-field-dropdown.php'; |
| 54 |
require_once __DIR__ . '/fields/class-field-multidropdown.php'; |
| 55 |
require_once __DIR__ . '/fields/class-field-url.php'; |
| 56 |
require_once __DIR__ . '/fields/class-field-sectionbreak.php'; |
| 57 |
require_once __DIR__ . '/fields/class-field-html.php'; |
| 58 |
require_once __DIR__ . '/fields/class-field-hidden.php'; |
| 59 |
require_once __DIR__ . '/fields/class-field-image.php'; |
| 60 |
require_once __DIR__ . '/fields/class-field-recaptcha.php'; |
| 61 |
require_once __DIR__ . '/fields/class-field-humanpresence.php'; |
| 62 |
require_once __DIR__ . '/fields/class-field-date.php'; |
| 63 |
|
| 64 |
$fields = [ |
| 65 |
'text_field' => new WeForms_Form_Field_Text(), |
| 66 |
'name_field' => new WeForms_Form_Field_Name(), |
| 67 |
'date_field' => new WeForms_Form_Field_Date_Free(), |
| 68 |
'email_address' => new WeForms_Form_Field_Email(), |
| 69 |
'textarea_field' => new WeForms_Form_Field_Textarea(), |
| 70 |
'radio_field' => new WeForms_Form_Field_Radio(), |
| 71 |
'checkbox_field' => new WeForms_Form_Field_Checkbox(), |
| 72 |
'column_field' => new WeForms_Form_Field_Column(), |
| 73 |
'dropdown_field' => new WeForms_Form_Field_Dropdown(), |
| 74 |
'multiple_select' => new WeForms_Form_Field_MultiDropdown(), |
| 75 |
'website_url' => new WeForms_Form_Field_URL(), |
| 76 |
'section_break' => new WeForms_Form_Field_SectionBreak(), |
| 77 |
'custom_html' => new WeForms_Form_Field_HTML(), |
| 78 |
'custom_hidden_field' => new WeForms_Form_Field_Hidden(), |
| 79 |
'image_upload' => new WeForms_Form_Field_Image(), |
| 80 |
'recaptcha' => new WeForms_Form_Field_reCaptcha(), |
| 81 |
'humanpresence' => new WeForms_Form_Field_HumanPresence(), |
| 82 |
]; |
| 83 |
|
| 84 |
$this->fields = apply_filters( 'weforms_form_fields', $fields ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get field groups |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public function get_field_groups() { |
| 93 |
$groups = [ |
| 94 |
[ |
| 95 |
'title' => __( 'Custom Fields', 'weforms' ), |
| 96 |
'id' => 'custom-fields', |
| 97 |
'fields' => apply_filters( 'weforms_field_groups_custom', |
| 98 |
[ |
| 99 |
'name_field', |
| 100 |
'text_field', |
| 101 |
'textarea_field', |
| 102 |
'dropdown_field', |
| 103 |
'multiple_select', |
| 104 |
'radio_field', |
| 105 |
'checkbox_field', |
| 106 |
'website_url', |
| 107 |
'email_address', |
| 108 |
'custom_hidden_field', |
| 109 |
'image_upload', |
| 110 |
] |
| 111 |
), |
| 112 |
], |
| 113 |
|
| 114 |
[ |
| 115 |
'title' => __( 'Others', 'weforms' ), |
| 116 |
'id' => 'others', |
| 117 |
'fields' => apply_filters( 'weforms_field_groups_others', |
| 118 |
[ |
| 119 |
'column_field', |
| 120 |
'section_break', |
| 121 |
'custom_html', |
| 122 |
'recaptcha', |
| 123 |
'humanpresence' |
| 124 |
] |
| 125 |
), |
| 126 |
], |
| 127 |
]; |
| 128 |
|
| 129 |
return apply_filters( 'weforms_field_groups', $groups ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get fields JS setting for the form builder |
| 134 |
* |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
public function get_js_settings() { |
| 138 |
$fields = $this->get_fields(); |
| 139 |
$js_array = []; |
| 140 |
|
| 141 |
if ( $fields ) { |
| 142 |
foreach ( $fields as $type => $object ) { |
| 143 |
$js_array[ $type ] = $object->get_js_settings(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return $js_array; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Render the form fields |
| 152 |
* |
| 153 |
* @param int $form_id |
| 154 |
* @param array $fields |
| 155 |
* @param array $atts |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public function render_fields( $fields, $form_id, $atts = [] ) { |
| 160 |
if ( !$fields ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
$this->notification = new WeForms_Notification(); |
| 165 |
$this->notification->set_merge_tags(); |
| 166 |
|
| 167 |
$fields = apply_filters( 'weforms_render_fields', $fields, $form_id ); |
| 168 |
|
| 169 |
foreach ( $fields as $field ) { |
| 170 |
if ( !$field_object = $this->field_exists( $field['template'] ) ) { |
| 171 |
if ( defined( 'WP_DEBUG' && WP_DEBUG ) ) { |
| 172 |
echo '<h4 style="color: red;"><em>' . esc_attr( $field['template'] ). '</em> field not found.</h4>'; |
| 173 |
} |
| 174 |
|
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
$field = $this->dynamic_fields( $field, $form_id, $atts ); |
| 179 |
$field = $this->replace_tags( $field, $form_id, $atts ); |
| 180 |
$field_object->render( $field, $form_id ); |
| 181 |
$field_object->conditional_logic( $field, $form_id ); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Filter dynamic fields |
| 187 |
* |
| 188 |
* @param array $form_field |
| 189 |
* @param int $form_id |
| 190 |
* |
| 191 |
* @return void |
| 192 |
*/ |
| 193 |
public function dynamic_fields( $form_field, $form_id, $atts = [] ) { |
| 194 |
if ( !isset( $form_field['dynamic'] ) || empty( $form_field['dynamic']['status'] ) || empty( $form_field['dynamic']['param_name'] ) ) { |
| 195 |
return $form_field; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
$param_name = $form_field['dynamic']['param_name']; |
| 200 |
|
| 201 |
if ( isset( $form_field['options'] ) ) { |
| 202 |
$form_field['options'] = apply_filters( 'weforms_field_options_' . $param_name, $form_field['options'], $form_id ); |
| 203 |
|
| 204 |
if ( isset( $_GET[ $param_name ] ) && is_array( $_GET[ $param_name ] ) ) { |
| 205 |
$form_field['default'] = array_merge( $form_field['options'], sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ) ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
foreach ( [ 'default', 'selected' ] as $key => $default_key ) { |
| 210 |
if ( isset( $form_field[ $default_key ] ) ) { |
| 211 |
$form_field[ $default_key ] = apply_filters( 'weforms_field_default_value_' . $param_name, $form_field[ $default_key ], $form_id ); |
| 212 |
|
| 213 |
if ( isset( $atts[ $param_name ] ) ) { |
| 214 |
$form_field[ $default_key ] = $atts[ $param_name ]; |
| 215 |
} |
| 216 |
|
| 217 |
if ( isset( $_GET[ $param_name ] ) ) { |
| 218 |
if ( is_array( $form_field[ $default_key ] ) == is_array( sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ) ) ) { |
| 219 |
$form_field[ $default_key ] = sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ); |
| 220 |
} |
| 221 |
|
| 222 |
if ( ! is_array( $form_field[ $default_key ] ) == ! is_array( sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ) ) ) { |
| 223 |
$form_field[ $default_key ] = sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return $form_field; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Replace merge tags |
| 234 |
* |
| 235 |
* @param array $form_field |
| 236 |
* @param int $form_id |
| 237 |
* |
| 238 |
* @return void |
| 239 |
*/ |
| 240 |
public function replace_tags( $form_field, $form_id, $atts = [] ) { |
| 241 |
$fields = [ |
| 242 |
'default', |
| 243 |
'placeholder', |
| 244 |
'first_name' => [ 'placeholder', 'default' ], |
| 245 |
'middle_name' => [ 'placeholder', 'default' ], |
| 246 |
'last_name' => [ 'placeholder', 'default' ], |
| 247 |
]; |
| 248 |
|
| 249 |
foreach ( $fields as $key => $field ) { |
| 250 |
if ( is_array( $field ) ) { |
| 251 |
foreach ( $field as $k => $sub_field ) { |
| 252 |
if ( !empty( $form_field[ $key ] ) && !empty( $form_field[ $key ][ $sub_field ] ) ) { |
| 253 |
$form_field[ $key ][ $sub_field ] = $this->notification->replace_tags( $form_field[ $key ][ $sub_field ] ); |
| 254 |
} |
| 255 |
} |
| 256 |
} else { |
| 257 |
if ( !empty( $form_field[ $field ] ) && is_string( $form_field[ $field ] ) ) { |
| 258 |
$form_field[ $field ] = $this->notification->replace_tags( $form_field[ $field ] ); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return $form_field; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Check if a field exists |
| 268 |
* |
| 269 |
* @param string $field_type |
| 270 |
* |
| 271 |
* @return bool |
| 272 |
*/ |
| 273 |
public function field_exists( $field_type ) { |
| 274 |
if ( array_key_exists( $field_type, $this->get_fields() ) ) { |
| 275 |
return $this->fields[ $field_type ]; |
| 276 |
} |
| 277 |
|
| 278 |
return false; |
| 279 |
} |
| 280 |
} |
| 281 |
|