| @@ -1,571 +1,578 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Form field abstract class | |
| 5 | - * | |
| 6 | - * @since 1.1.0 | |
| 7 | - */ | |
| 8 | -abstract class WeForms_Field_Contract { | |
| 9 | - | |
| 10 | - /** | |
| 11 | - * The field name | |
| 12 | - * | |
| 13 | - * @var string | |
| 14 | - */ | |
| 15 | - protected $name = ''; | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * Type of the field | |
| 19 | - * | |
| 20 | - * @var string | |
| 21 | - */ | |
| 22 | - protected $input_type = ''; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Icon of the field | |
| 26 | - * | |
| 27 | - * @var string | |
| 28 | - */ | |
| 29 | - protected $icon = ''; | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * Get the name of the field | |
| 33 | - * | |
| 34 | - * @return string | |
| 35 | - */ | |
| 36 | - public function get_name() { | |
| 37 | - return $this->name; | |
| 38 | - } | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Get field type | |
| 42 | - * | |
| 43 | - * @return string | |
| 44 | - */ | |
| 45 | - public function get_type() { | |
| 46 | - return $this->input_type; | |
| 47 | - } | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Get the fontawesome icon for this field | |
| 51 | - * | |
| 52 | - * @return string | |
| 53 | - */ | |
| 54 | - public function get_icon() { | |
| 55 | - return $this->icon; | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * Render of the field in the frontend | |
| 60 | - * | |
| 61 | - * @param array $field_settings The field configuration from the db | |
| 62 | - * @param int $form_id The form id | |
| 63 | - * | |
| 64 | - * @return void | |
| 65 | - */ | |
| 66 | - abstract public function render( $field_settings, $form_id ); | |
| 67 | - | |
| 68 | - /** | |
| 69 | - * Get the field option settings for form builder | |
| 70 | - * | |
| 71 | - * @return array | |
| 72 | - */ | |
| 73 | - abstract public function get_options_settings(); | |
| 74 | - | |
| 75 | - /** | |
| 76 | - * Get the field props | |
| 77 | - * | |
| 78 | - * Field props are the field properties that saves in the database | |
| 79 | - * | |
| 80 | - * @return array | |
| 81 | - */ | |
| 82 | - abstract public function get_field_props(); | |
| 83 | - | |
| 84 | - /** | |
| 85 | - * The JS template for using in the form builder | |
| 86 | - * | |
| 87 | - * @return array | |
| 88 | - */ | |
| 89 | - public function get_js_settings() { | |
| 90 | - $settings = [ | |
| 91 | - 'template' => $this->get_type(), | |
| 92 | - 'title' => $this->get_name(), | |
| 93 | - 'icon' => $this->get_icon(), | |
| 94 | - 'pro_feature' => $this->is_pro(), | |
| 95 | - 'settings' => $this->get_options_settings(), | |
| 96 | - 'field_props' => $this->get_field_props(), | |
| 97 | - 'is_full_width' => $this->is_full_width(), | |
| 98 | - ]; | |
| 99 | - | |
| 100 | - if ( $validator = $this->get_validator() ) { | |
| 101 | - $settings['validator'] = $validator; | |
| 102 | - } | |
| 103 | - | |
| 104 | - return apply_filters( 'weforms_field_get_js_settings', $settings ); | |
| 105 | - } | |
| 106 | - | |
| 107 | - /** | |
| 108 | - * Custom field validator if exists | |
| 109 | - * | |
| 110 | - * @return bool|array | |
| 111 | - */ | |
| 112 | - public function get_validator() { | |
| 113 | - return false; | |
| 114 | - } | |
| 115 | - | |
| 116 | - /** | |
| 117 | - * Check if it's a pro feature | |
| 118 | - * | |
| 119 | - * @return bool | |
| 120 | - */ | |
| 121 | - public function is_pro() { | |
| 122 | - return false; | |
| 123 | - } | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * If this field is full width | |
| 127 | - * | |
| 128 | - * Used in form builder preview (hides the label) | |
| 129 | - * | |
| 130 | - * @return bool | |
| 131 | - */ | |
| 132 | - public function is_full_width() { | |
| 133 | - return false; | |
| 134 | - } | |
| 135 | - | |
| 136 | - /** | |
| 137 | - * Conditional property for all fields | |
| 138 | - * | |
| 139 | - * @return array | |
| 140 | - */ | |
| 141 | - public function default_conditional_prop() { | |
| 142 | - return [ | |
| 143 | - 'condition_status' => 'no', | |
| 144 | - 'cond_field' => [], | |
| 145 | - 'cond_operator' => [ '=' ], | |
| 146 | - 'cond_option' => [ __( '- select -', 'weforms' ) ], | |
| 147 | - 'cond_logic' => 'all', | |
| 148 | - ]; | |
| 149 | - } | |
| 150 | - | |
| 151 | - /** | |
| 152 | - * Default attributes of a field | |
| 153 | - * | |
| 154 | - * Child classes should use this default setting and extend it by using `get_field_settings()` function | |
| 155 | - * | |
| 156 | - * @return array | |
| 157 | - */ | |
| 158 | - public function default_attributes() { | |
| 159 | - return [ | |
| 160 | - 'template' => $this->get_type(), | |
| 161 | - 'name' => '', | |
| 162 | - 'label' => $this->get_name(), | |
| 163 | - 'required' => 'no', | |
| 164 | - 'id' => 0, | |
| 165 | - 'width' => 'large', | |
| 166 | - 'css' => '', | |
| 167 | - 'placeholder' => '', | |
| 168 | - 'default' => '', | |
| 169 | - 'size' => 40, | |
| 170 | - 'help' => '', | |
| 171 | - 'is_meta' => 'yes', // wpuf uses it to differentiate meta fields with core fields, maybe removed | |
| 172 | - 'is_new' => true, // introduced by @edi, not sure what it does. Have to remove | |
| 173 | - 'wpuf_cond' => $this->default_conditional_prop(), | |
| 174 | - ]; | |
| 175 | - } | |
| 176 | - | |
| 177 | - /** | |
| 178 | - * Common properties for all kinds of fields | |
| 179 | - * | |
| 180 | - * @param bool $is_meta | |
| 181 | - * | |
| 182 | - * @return array | |
| 183 | - */ | |
| 184 | - public static function get_default_option_settings( $is_meta = true, $exclude = [] ) { | |
| 185 | - $common_properties = [ | |
| 186 | - [ | |
| 187 | - 'name' => 'label', | |
| 188 | - 'title' => __( 'Field Label', 'weforms' ), | |
| 189 | - 'type' => 'text', | |
| 190 | - 'section' => 'basic', | |
| 191 | - 'priority' => 10, | |
| 192 | - 'help_text' => __( 'Enter a title of this field', 'weforms' ), | |
| 193 | - ], | |
| 194 | - | |
| 195 | - [ | |
| 196 | - 'name' => 'help', | |
| 197 | - 'title' => __( 'Help text', 'weforms' ), | |
| 198 | - 'type' => 'text', | |
| 199 | - 'section' => 'basic', | |
| 200 | - 'priority' => 20, | |
| 201 | - 'help_text' => __( 'Give the user some information about this field', 'weforms' ), | |
| 202 | - ], | |
| 203 | - | |
| 204 | - [ | |
| 205 | - 'name' => 'required', | |
| 206 | - 'title' => __( 'Required', 'weforms' ), | |
| 207 | - 'type' => 'radio', | |
| 208 | - 'options' => [ | |
| 209 | - 'yes' => __( 'Yes', 'weforms' ), | |
| 210 | - 'no' => __( 'No', 'weforms' ), | |
| 211 | - ], | |
| 212 | - 'section' => 'basic', | |
| 213 | - 'priority' => 21, | |
| 214 | - 'default' => 'no', | |
| 215 | - 'inline' => true, | |
| 216 | - 'help_text' => __( 'Check this option to mark the field required. A form will not submit unless all required fields are provided.', 'weforms' ), | |
| 217 | - ], | |
| 218 | - | |
| 219 | - [ | |
| 220 | - 'name' => 'width', | |
| 221 | - 'title' => __( 'Field Size', 'weforms' ), | |
| 222 | - 'type' => 'radio', | |
| 223 | - 'options' => [ | |
| 224 | - 'small' => __( 'Small', 'weforms' ), | |
| 225 | - 'medium' => __( 'Medium', 'weforms' ), | |
| 226 | - 'large' => __( 'Large', 'weforms' ), | |
| 227 | - ], | |
| 228 | - 'section' => 'advanced', | |
| 229 | - 'priority' => 21, | |
| 230 | - 'default' => 'large', | |
| 231 | - 'inline' => true, | |
| 232 | - ], | |
| 233 | - | |
| 234 | - [ | |
| 235 | - 'name' => 'css', | |
| 236 | - 'title' => __( 'CSS Class Name', 'weforms' ), | |
| 237 | - 'type' => 'text', | |
| 238 | - 'section' => 'advanced', | |
| 239 | - 'priority' => 22, | |
| 240 | - 'help_text' => __( 'Provide a container class name for this field. Available classes: wpuf-col-half, wpuf-col-half-last, wpuf-col-one-third, wpuf-col-one-third-last', 'weforms' ), | |
| 241 | - ], | |
| 242 | - | |
| 243 | - [ | |
| 244 | - 'name' => 'dynamic', | |
| 245 | - 'title' => '', | |
| 246 | - 'type' => 'dynamic-field', | |
| 247 | - 'section' => 'advanced', | |
| 248 | - 'priority' => 23, | |
| 249 | - 'help_text' => __( 'Check this option to allow field to be populated dynamically using hooks/query string/shortcode', 'weforms' ), | |
| 250 | - ], | |
| 251 | - ]; | |
| 252 | - | |
| 253 | - if ( $is_meta ) { | |
| 254 | - $common_properties[] = [ | |
| 255 | - 'name' => 'name', | |
| 256 | - 'title' => __( 'Meta Key', 'weforms' ), | |
| 257 | - 'type' => 'text-meta', | |
| 258 | - 'section' => 'basic', | |
| 259 | - 'priority' => 11, | |
| 260 | - 'help_text' => __( 'Name of the meta key this field will save to', 'weforms' ), | |
| 261 | - ]; | |
| 262 | - } | |
| 263 | - | |
| 264 | - if ( count( $exclude ) ) { | |
| 265 | - foreach ( $common_properties as $key => &$option ) { | |
| 266 | - if ( in_array( $option['name'], $exclude ) ) { | |
| 267 | - unset( $common_properties[$key] ); | |
| 268 | - } | |
| 269 | - } | |
| 270 | - } | |
| 271 | - | |
| 272 | - return $common_properties; | |
| 273 | - } | |
| 274 | - | |
| 275 | - /** | |
| 276 | - * Common properties of a text input field | |
| 277 | - * | |
| 278 | - * @param bool $word_restriction | |
| 279 | - * | |
| 280 | - * @return array | |
| 281 | - */ | |
| 282 | - public static function get_default_text_option_settings( $word_restriction = false ) { | |
| 283 | - $properties = [ | |
| 284 | - [ | |
| 285 | - 'name' => 'placeholder', | |
| 286 | - 'title' => __( 'Placeholder text', 'weforms' ), | |
| 287 | - 'type' => 'text-with-tag', | |
| 288 | - 'tag_filter' => 'no_fields', // we don't want to show any fields with merge tags, just basic tags | |
| 289 | - 'section' => 'advanced', | |
| 290 | - 'priority' => 10, | |
| 291 | - 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ), | |
| 292 | - ], | |
| 293 | - | |
| 294 | - [ | |
| 295 | - 'name' => 'default', | |
| 296 | - 'title' => __( 'Default value', 'weforms' ), | |
| 297 | - 'type' => 'text-with-tag', | |
| 298 | - 'tag_filter' => 'no_fields', | |
| 299 | - 'section' => 'advanced', | |
| 300 | - 'priority' => 11, | |
| 301 | - 'help_text' => __( 'The default value this field will have', 'weforms' ), | |
| 302 | - ], | |
| 303 | - | |
| 304 | - [ | |
| 305 | - 'name' => 'size', | |
| 306 | - 'title' => __( 'Size', 'weforms' ), | |
| 307 | - 'type' => 'text', | |
| 308 | - 'variation' => 'number', | |
| 309 | - 'section' => 'advanced', | |
| 310 | - 'priority' => 20, | |
| 311 | - 'help_text' => __( 'Size of this input field', 'weforms' ), | |
| 312 | - ], | |
| 313 | - ]; | |
| 314 | - | |
| 315 | - if ( $word_restriction ) { | |
| 316 | - $properties[] = [ | |
| 317 | - 'name' => 'word_restriction', | |
| 318 | - 'title' => __( 'Word Restriction', 'weforms' ), | |
| 319 | - 'type' => 'text', | |
| 320 | - 'section' => 'advanced', | |
| 321 | - 'priority' => 15, | |
| 322 | - 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ), | |
| 323 | - ]; | |
| 324 | - } | |
| 325 | - | |
| 326 | - return apply_filters( 'wpuf-form-builder-common-text-fields-properties', $properties ); | |
| 327 | - } | |
| 328 | - | |
| 329 | - /** | |
| 330 | - * Option data for option based fields | |
| 331 | - * | |
| 332 | - * @param bool $is_multiple | |
| 333 | - * | |
| 334 | - * @return array | |
| 335 | - */ | |
| 336 | - public function get_default_option_dropdown_settings( $is_multiple = false ) { | |
| 337 | - return [ | |
| 338 | - 'name' => 'options', | |
| 339 | - 'title' => __( 'Options', 'weforms' ), | |
| 340 | - 'type' => 'option-data', | |
| 341 | - 'is_multiple' => $is_multiple, | |
| 342 | - 'section' => 'basic', | |
| 343 | - 'priority' => 12, | |
| 344 | - 'help_text' => __( 'Add options for the form field', 'weforms' ), | |
| 345 | - ]; | |
| 346 | - } | |
| 347 | - | |
| 348 | - /** | |
| 349 | - * Common properties of a textarea field | |
| 350 | - * | |
| 351 | - * @return array | |
| 352 | - */ | |
| 353 | - public function get_default_textarea_option_settings() { | |
| 354 | - return [ | |
| 355 | - [ | |
| 356 | - 'name' => 'rows', | |
| 357 | - 'title' => __( 'Rows', 'weforms' ), | |
| 358 | - 'type' => 'text', | |
| 359 | - 'section' => 'advanced', | |
| 360 | - 'priority' => 10, | |
| 361 | - 'help_text' => __( 'Number of rows in textarea', 'weforms' ), | |
| 362 | - ], | |
| 363 | - | |
| 364 | - [ | |
| 365 | - 'name' => 'cols', | |
| 366 | - 'title' => __( 'Columns', 'weforms' ), | |
| 367 | - 'type' => 'text', | |
| 368 | - 'section' => 'advanced', | |
| 369 | - 'priority' => 11, | |
| 370 | - 'help_text' => __( 'Number of columns in textarea', 'weforms' ), | |
| 371 | - ], | |
| 372 | - | |
| 373 | - [ | |
| 374 | - 'name' => 'placeholder', | |
| 375 | - 'title' => __( 'Placeholder text', 'weforms' ), | |
| 376 | - 'type' => 'text', | |
| 377 | - 'section' => 'advanced', | |
| 378 | - 'priority' => 12, | |
| 379 | - 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ), | |
| 380 | - 'dependencies' => [ | |
| 381 | - 'rich' => 'no', | |
| 382 | - ], | |
| 383 | - ], | |
| 384 | - | |
| 385 | - [ | |
| 386 | - 'name' => 'default', | |
| 387 | - 'title' => __( 'Default value', 'weforms' ), | |
| 388 | - 'type' => 'text', | |
| 389 | - 'section' => 'advanced', | |
| 390 | - 'priority' => 13, | |
| 391 | - 'help_text' => __( 'The default value this field will have', 'weforms' ), | |
| 392 | - ], | |
| 393 | - | |
| 394 | - [ | |
| 395 | - 'name' => 'rich', | |
| 396 | - 'title' => __( 'Textarea', 'weforms' ), | |
| 397 | - 'type' => 'radio', | |
| 398 | - 'options' => [ | |
| 399 | - 'no' => __( 'Normal', 'weforms' ), | |
| 400 | - 'yes' => __( 'Rich textarea', 'weforms' ), | |
| 401 | - 'teeny' => __( 'Teeny Rich textarea', 'weforms' ), | |
| 402 | - ], | |
| 403 | - 'section' => 'advanced', | |
| 404 | - 'priority' => 14, | |
| 405 | - 'default' => 'no', | |
| 406 | - ], | |
| 407 | - | |
| 408 | - [ | |
| 409 | - 'name' => 'word_restriction', | |
| 410 | - 'title' => __( 'Word Restriction', 'weforms' ), | |
| 411 | - 'type' => 'text', | |
| 412 | - 'section' => 'advanced', | |
| 413 | - 'priority' => 15, | |
| 414 | - 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ), | |
| 415 | - ], | |
| 416 | - ]; | |
| 417 | - } | |
| 418 | - | |
| 419 | - /* | |
| 420 | - |-------------------------------------------------------------------------- | |
| 421 | - | Field helper methods | |
| 422 | - |-------------------------------------------------------------------------- | |
| 423 | - | | |
| 424 | - | Various helper method for rendering form fields | |
| 425 | - */ | |
| 426 | - | |
| 427 | - public function print_list_attributes( $field ) { | |
| 428 | - $label = isset( $field['label'] ) ? $field['label'] : ''; | |
| 429 | - $el_name = !empty( $field['name'] ) ? $field['name'] : ''; | |
| 430 | - $class_name = !empty( $field['css'] ) ? ' ' . $field['css'] : ''; | |
| 431 | - $field_size = !empty( $field['width'] ) ? ' field-size-' . $field['width'] : ''; | |
| 432 | - | |
| 433 | - printf( 'class="wpuf-el %s%s%s" data-label="%s"', esc_attr( $el_name ), esc_attr( $class_name ), esc_attr( $field_size ), | |
| 434 | - esc_attr( $label ) ); | |
| 435 | - } | |
| 436 | - | |
| 437 | - /** | |
| 438 | - * Prints form input label | |
| 439 | - * | |
| 440 | - * @param array $attr | |
| 441 | - * @param int $form_id | |
| 442 | - */ | |
| 443 | - public function print_label( $field, $form_id = 0 ) { | |
| 444 | - ?> | |
| 445 | - <div class="wpuf-label"> | |
| 446 | - <label for="<?php echo isset( $field['name'] ) ? esc_attr( $field['name'] ) . '_' . esc_attr( $form_id ) : 'cls'; ?>"><?php echo esc_html( $field['label'] ) . wp_kses_post( $this->required_mark( $field ) ) ; ?></label> | |
| 447 | - </div> | |
| 448 | - <?php | |
| 449 | - } | |
| 450 | - | |
| 451 | - /** | |
| 452 | - * Check if a field is required | |
| 453 | - * | |
| 454 | - * @param array $field | |
| 455 | - * | |
| 456 | - * @return bool | |
| 457 | - */ | |
| 458 | - public function is_required( $field ) { | |
| 459 | - if ( isset( $field['required'] ) && $field['required'] == 'yes' ) { | |
| 460 | - return true; | |
| 461 | - } | |
| 462 | - | |
| 463 | - return false; | |
| 464 | - } | |
| 465 | - | |
| 466 | - /** | |
| 467 | - * Prints required field asterisk | |
| 468 | - * | |
| 469 | - * @param array $attr | |
| 470 | - * | |
| 471 | - * @return string | |
| 472 | - */ | |
| 473 | - public function required_mark( $field ) { | |
| 474 | - if ( $this->is_required( $field ) ) { | |
| 475 | - return ' <span class="required">*</span>'; | |
| 476 | - } | |
| 477 | - } | |
| 478 | - | |
| 479 | - /** | |
| 480 | - * Prints help text for a field | |
| 481 | - * | |
| 482 | - * @param array $field | |
| 483 | - */ | |
| 484 | - public function help_text( $field ) { | |
| 485 | - if ( empty( $field['help'] ) ) { | |
| 486 | - return; | |
| 487 | - } | |
| 488 | - ?> | |
| 489 | - <span class="wpuf-help"><?php echo esc_attr( $field['help'] ); ?></span> | |
| 490 | - <?php | |
| 491 | - } | |
| 492 | - | |
| 493 | - /** | |
| 494 | - * Push logic to conditional array for processing | |
| 495 | - * | |
| 496 | - * @param array $form_field | |
| 497 | - * @param int $form_id | |
| 498 | - * | |
| 499 | - * @return void | |
| 500 | - */ | |
| 501 | - public function conditional_logic( $form_field, $form_id ) { | |
| 502 | - if ( !isset( $form_field['wpuf_cond']['condition_status'] ) || $form_field['wpuf_cond']['condition_status'] != 'yes' ) { | |
| 503 | - return; | |
| 504 | - } | |
| 505 | - | |
| 506 | - $cond_inputs = $form_field['wpuf_cond']; | |
| 507 | - $cond_inputs['condition_status'] = isset( $cond_inputs['condition_status'] ) ? $cond_inputs['condition_status'] : ''; | |
| 508 | - | |
| 509 | - if ( $cond_inputs['condition_status'] == 'yes' ) { | |
| 510 | - $cond_inputs['type'] = $form_field['template']; | |
| 511 | - $cond_inputs['name'] = $form_field['name']; | |
| 512 | - $cond_inputs['form_id'] = $form_id; | |
| 513 | - $condition = json_encode( $cond_inputs ); | |
| 514 | - } else { | |
| 515 | - $condition = ''; | |
| 516 | - } ?> | |
| 517 | - <?php | |
| 518 | - $script = "wpuf_conditional_items.push({$condition});"; | |
| 519 | - wp_add_inline_script( 'wpuf-form', $script ); | |
| 520 | - ?> | |
| 521 | - <?php | |
| 522 | - } | |
| 523 | - | |
| 524 | - /** | |
| 525 | - * Prepare entry default, can be replaced through field classes | |
| 526 | - * | |
| 527 | - * @param $field | |
| 528 | - * | |
| 529 | - * @return mixed | |
| 530 | - */ | |
| 531 | - public function prepare_entry( $field, $args = [] ) { | |
| 532 | - if( empty( $_POST['_wpnonce'] ) ) { | |
| 533 | - wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); | |
| 534 | - } | |
| 535 | - | |
| 536 | - if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { | |
| 537 | - wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); | |
| 538 | - } | |
| 539 | - | |
| 540 | - $args = ! empty( $args ) ? $args : weforms_clean( $_POST ); | |
| 541 | - $value = !empty( $args[$field['name']] ) ? $args[$field['name']] : ''; | |
| 542 | - | |
| 543 | - if ( is_array( $value ) ) { | |
| 544 | - $entry_value = implode( WeForms::$field_separator, $args[$field['name']] ); | |
| 545 | - } else { | |
| 546 | - $entry_value = trim( $value ); | |
| 547 | - } | |
| 548 | - | |
| 549 | - return $entry_value; | |
| 550 | - } | |
| 551 | - | |
| 552 | - /** | |
| 553 | - * Function to check word restriction | |
| 554 | - * | |
| 555 | - * @param $word_nums number of words allowed | |
| 556 | - */ | |
| 557 | - public function check_word_restriction_func( $word_nums, $rich_text, $field_name ) { | |
| 558 | - // bail out if it is dashboard | |
| 559 | - if ( is_admin() ) { | |
| 560 | - return; | |
| 561 | - } ?> | |
| 562 | - <script type="text/javascript"> | |
| 563 | - ;(function($) { | |
| 564 | - $(document).ready( function(){ | |
| 565 | - WP_User_Frontend.editorLimit.bind(<?php printf( '%d, "%s", "%s"', esc_attr( $word_nums ), esc_attr( $field_name ), esc_attr( $rich_text ) ); ?>); | |
| 566 | - }); | |
| 567 | - })(jQuery); | |
| 568 | - </script> | |
| 569 | - <?php | |
| 570 | - } | |
| 571 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Form field abstract class | |
| 5 | + * | |
| 6 | + * @since 1.1.0 | |
| 7 | + */ | |
| 8 | +abstract class WeForms_Field_Contract { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * The field name | |
| 12 | + * | |
| 13 | + * @var string | |
| 14 | + */ | |
| 15 | + protected $name = ''; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Type of the field | |
| 19 | + * | |
| 20 | + * @var string | |
| 21 | + */ | |
| 22 | + protected $input_type = ''; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Icon of the field | |
| 26 | + * | |
| 27 | + * @var string | |
| 28 | + */ | |
| 29 | + protected $icon = ''; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Get the name of the field | |
| 33 | + * | |
| 34 | + * @return string | |
| 35 | + */ | |
| 36 | + public function get_name() { | |
| 37 | + return $this->name; | |
| 38 | + } | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Get field type | |
| 42 | + * | |
| 43 | + * @return string | |
| 44 | + */ | |
| 45 | + public function get_type() { | |
| 46 | + return $this->input_type; | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Get the fontawesome icon for this field | |
| 51 | + * | |
| 52 | + * @return string | |
| 53 | + */ | |
| 54 | + public function get_icon() { | |
| 55 | + return $this->icon; | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Render of the field in the frontend | |
| 60 | + * | |
| 61 | + * @param array $field_settings The field configuration from the db | |
| 62 | + * @param integer $form_id The form id | |
| 63 | + * | |
| 64 | + * @return void | |
| 65 | + */ | |
| 66 | + abstract function render( $field_settings, $form_id ); | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Get the field option settings for form builder | |
| 70 | + * | |
| 71 | + * @return array | |
| 72 | + */ | |
| 73 | + abstract function get_options_settings(); | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Get the field props | |
| 77 | + * | |
| 78 | + * Field props are the field properties that saves in the database | |
| 79 | + * | |
| 80 | + * @return array | |
| 81 | + */ | |
| 82 | + abstract function get_field_props(); | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * The JS template for using in the form builder | |
| 86 | + * | |
| 87 | + * @return array | |
| 88 | + */ | |
| 89 | + public function get_js_settings() { | |
| 90 | + $settings = array( | |
| 91 | + 'template' => $this->get_type(), | |
| 92 | + 'title' => $this->get_name(), | |
| 93 | + 'icon' => $this->get_icon(), | |
| 94 | + 'pro_feature' => $this->is_pro(), | |
| 95 | + 'settings' => $this->get_options_settings(), | |
| 96 | + 'field_props' => $this->get_field_props(), | |
| 97 | + 'is_full_width' => $this->is_full_width() | |
| 98 | + ); | |
| 99 | + | |
| 100 | + if ( $validator = $this->get_validator() ) { | |
| 101 | + $settings['validator'] = $validator; | |
| 102 | + } | |
| 103 | + | |
| 104 | + return apply_filters( 'weforms_field_get_js_settings', $settings ); | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Custom field validator if exists | |
| 109 | + * | |
| 110 | + * @return boolean|array | |
| 111 | + */ | |
| 112 | + public function get_validator() { | |
| 113 | + return false; | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * Check if it's a pro feature | |
| 118 | + * | |
| 119 | + * @return boolean | |
| 120 | + */ | |
| 121 | + public function is_pro() { | |
| 122 | + return false; | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * If this field is full width | |
| 127 | + * | |
| 128 | + * Used in form builder preview (hides the label) | |
| 129 | + * | |
| 130 | + * @return boolean | |
| 131 | + */ | |
| 132 | + public function is_full_width() { | |
| 133 | + return false; | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * Conditional property for all fields | |
| 138 | + * | |
| 139 | + * @return array | |
| 140 | + */ | |
| 141 | + public function default_conditional_prop() { | |
| 142 | + return array( | |
| 143 | + 'condition_status' => 'no', | |
| 144 | + 'cond_field' => array(), | |
| 145 | + 'cond_operator' => array( '=' ), | |
| 146 | + 'cond_option' => array( __( '- select -', 'weforms' ) ), | |
| 147 | + 'cond_logic' => 'all' | |
| 148 | + ); | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * Default attributes of a field | |
| 153 | + * | |
| 154 | + * Child classes should use this default setting and extend it by using `get_field_settings()` function | |
| 155 | + * | |
| 156 | + * @return array | |
| 157 | + */ | |
| 158 | + public function default_attributes() { | |
| 159 | + return array( | |
| 160 | + 'template' => $this->get_type(), | |
| 161 | + 'name' => '', | |
| 162 | + 'label' => $this->get_name(), | |
| 163 | + 'required' => 'no', | |
| 164 | + 'id' => 0, | |
| 165 | + 'width' => 'large', | |
| 166 | + 'css' => '', | |
| 167 | + 'placeholder' => '', | |
| 168 | + 'default' => '', | |
| 169 | + 'size' => 40, | |
| 170 | + 'help' => '', | |
| 171 | + 'is_meta' => 'yes', // wpuf uses it to differentiate meta fields with core fields, maybe removed | |
| 172 | + 'is_new' => true, // introduced by @edi, not sure what it does. Have to remove | |
| 173 | + 'wpuf_cond' => $this->default_conditional_prop() | |
| 174 | + ); | |
| 175 | + } | |
| 176 | + | |
| 177 | + /** | |
| 178 | + * Common properties for all kinds of fields | |
| 179 | + * | |
| 180 | + * @param boolean $is_meta | |
| 181 | + * | |
| 182 | + * @return array | |
| 183 | + */ | |
| 184 | + public static function get_default_option_settings( $is_meta = true, $exclude = array() ) { | |
| 185 | + $common_properties = array( | |
| 186 | + array( | |
| 187 | + 'name' => 'label', | |
| 188 | + 'title' => __( 'Field Label', 'weforms' ), | |
| 189 | + 'type' => 'text', | |
| 190 | + 'section' => 'basic', | |
| 191 | + 'priority' => 10, | |
| 192 | + 'help_text' => __( 'Enter a title of this field', 'weforms' ), | |
| 193 | + ), | |
| 194 | + | |
| 195 | + array( | |
| 196 | + 'name' => 'help', | |
| 197 | + 'title' => __( 'Help text', 'weforms' ), | |
| 198 | + 'type' => 'text', | |
| 199 | + 'section' => 'basic', | |
| 200 | + 'priority' => 20, | |
| 201 | + 'help_text' => __( 'Give the user some information about this field', 'weforms' ), | |
| 202 | + ), | |
| 203 | + | |
| 204 | + array( | |
| 205 | + 'name' => 'required', | |
| 206 | + 'title' => __( 'Required', 'weforms' ), | |
| 207 | + 'type' => 'radio', | |
| 208 | + 'options' => array( | |
| 209 | + 'yes' => __( 'Yes', 'weforms' ), | |
| 210 | + 'no' => __( 'No', 'weforms' ), | |
| 211 | + ), | |
| 212 | + 'section' => 'basic', | |
| 213 | + 'priority' => 21, | |
| 214 | + 'default' => 'no', | |
| 215 | + 'inline' => true, | |
| 216 | + 'help_text' => __( 'Check this option to mark the field required. A form will not submit unless all required fields are provided.', 'weforms' ), | |
| 217 | + ), | |
| 218 | + | |
| 219 | + array( | |
| 220 | + 'name' => 'width', | |
| 221 | + 'title' => __( 'Field Size', 'weforms' ), | |
| 222 | + 'type' => 'radio', | |
| 223 | + 'options' => array( | |
| 224 | + 'small' => __( 'Small', 'weforms' ), | |
| 225 | + 'medium' => __( 'Medium', 'weforms' ), | |
| 226 | + 'large' => __( 'Large', 'weforms' ), | |
| 227 | + ), | |
| 228 | + 'section' => 'advanced', | |
| 229 | + 'priority' => 21, | |
| 230 | + 'default' => 'large', | |
| 231 | + 'inline' => true, | |
| 232 | + ), | |
| 233 | + | |
| 234 | + array( | |
| 235 | + 'name' => 'css', | |
| 236 | + 'title' => __( 'CSS Class Name', 'weforms' ), | |
| 237 | + 'type' => 'text', | |
| 238 | + 'section' => 'advanced', | |
| 239 | + 'priority' => 22, | |
| 240 | + 'help_text' => __( 'Provide a container class name for this field. Available classes: wpuf-col-half, wpuf-col-half-last, wpuf-col-one-third, wpuf-col-one-third-last', 'weforms' ), | |
| 241 | + ), | |
| 242 | + | |
| 243 | + array( | |
| 244 | + 'name' => 'dynamic', | |
| 245 | + 'title' => '', | |
| 246 | + 'type' => 'dynamic-field', | |
| 247 | + 'section' => 'advanced', | |
| 248 | + 'priority' => 23, | |
| 249 | + 'help_text' => __( 'Check this option to allow field to be populated dynamically using hooks/query string/shortcode', 'weforms' ), | |
| 250 | + ), | |
| 251 | + ); | |
| 252 | + | |
| 253 | + if ( $is_meta ) { | |
| 254 | + $common_properties[] = array( | |
| 255 | + 'name' => 'name', | |
| 256 | + 'title' => __( 'Meta Key', 'weforms' ), | |
| 257 | + 'type' => 'text-meta', | |
| 258 | + 'section' => 'basic', | |
| 259 | + 'priority' => 11, | |
| 260 | + 'help_text' => __( 'Name of the meta key this field will save to', 'weforms' ), | |
| 261 | + ); | |
| 262 | + } | |
| 263 | + | |
| 264 | + if ( count( $exclude ) ) { | |
| 265 | + foreach ( $common_properties as $key => &$option ) { | |
| 266 | + | |
| 267 | + if ( in_array( $option['name'] , $exclude) ) { | |
| 268 | + unset( $common_properties[$key] ); | |
| 269 | + } | |
| 270 | + } | |
| 271 | + | |
| 272 | + } | |
| 273 | + | |
| 274 | + return $common_properties; | |
| 275 | + } | |
| 276 | + | |
| 277 | + /** | |
| 278 | + * Common properties of a text input field | |
| 279 | + * | |
| 280 | + * @param boolean $word_restriction | |
| 281 | + * | |
| 282 | + * @return array | |
| 283 | + */ | |
| 284 | + public static function get_default_text_option_settings( $word_restriction = false ) { | |
| 285 | + $properties = array( | |
| 286 | + array( | |
| 287 | + 'name' => 'placeholder', | |
| 288 | + 'title' => __( 'Placeholder text', 'weforms' ), | |
| 289 | + 'type' => 'text-with-tag', | |
| 290 | + 'tag_filter' => 'no_fields', // we don't want to show any fields with merge tags, just basic tags | |
| 291 | + 'section' => 'advanced', | |
| 292 | + 'priority' => 10, | |
| 293 | + 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ), | |
| 294 | + ), | |
| 295 | + | |
| 296 | + array( | |
| 297 | + 'name' => 'default', | |
| 298 | + 'title' => __( 'Default value', 'weforms' ), | |
| 299 | + 'type' => 'text-with-tag', | |
| 300 | + 'tag_filter' => 'no_fields', | |
| 301 | + 'section' => 'advanced', | |
| 302 | + 'priority' => 11, | |
| 303 | + 'help_text' => __( 'The default value this field will have', 'weforms' ), | |
| 304 | + ), | |
| 305 | + | |
| 306 | + array( | |
| 307 | + 'name' => 'size', | |
| 308 | + 'title' => __( 'Size', 'weforms' ), | |
| 309 | + 'type' => 'text', | |
| 310 | + 'variation' => 'number', | |
| 311 | + 'section' => 'advanced', | |
| 312 | + 'priority' => 20, | |
| 313 | + 'help_text' => __( 'Size of this input field', 'weforms' ), | |
| 314 | + ), | |
| 315 | + ); | |
| 316 | + | |
| 317 | + if ( $word_restriction ) { | |
| 318 | + $properties[] = array( | |
| 319 | + 'name' => 'word_restriction', | |
| 320 | + 'title' => __( 'Word Restriction', 'weforms' ), | |
| 321 | + 'type' => 'text', | |
| 322 | + 'section' => 'advanced', | |
| 323 | + 'priority' => 15, | |
| 324 | + 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ), | |
| 325 | + ); | |
| 326 | + } | |
| 327 | + | |
| 328 | + return apply_filters( 'wpuf-form-builder-common-text-fields-properties', $properties ); | |
| 329 | + } | |
| 330 | + | |
| 331 | + /** | |
| 332 | + * Option data for option based fields | |
| 333 | + * | |
| 334 | + * @param boolean $is_multiple | |
| 335 | + * | |
| 336 | + * @return array | |
| 337 | + */ | |
| 338 | + public function get_default_option_dropdown_settings( $is_multiple = false ) { | |
| 339 | + return array( | |
| 340 | + 'name' => 'options', | |
| 341 | + 'title' => __( 'Options', 'weforms' ), | |
| 342 | + 'type' => 'option-data', | |
| 343 | + 'is_multiple' => $is_multiple, | |
| 344 | + 'section' => 'basic', | |
| 345 | + 'priority' => 12, | |
| 346 | + 'help_text' => __( 'Add options for the form field', 'weforms' ), | |
| 347 | + ); | |
| 348 | + } | |
| 349 | + | |
| 350 | + /** | |
| 351 | + * Common properties of a textarea field | |
| 352 | + * | |
| 353 | + * @return array | |
| 354 | + */ | |
| 355 | + public function get_default_textarea_option_settings() { | |
| 356 | + return array( | |
| 357 | + array( | |
| 358 | + 'name' => 'rows', | |
| 359 | + 'title' => __( 'Rows', 'weforms' ), | |
| 360 | + 'type' => 'text', | |
| 361 | + 'section' => 'advanced', | |
| 362 | + 'priority' => 10, | |
| 363 | + 'help_text' => __( 'Number of rows in textarea', 'weforms' ), | |
| 364 | + ), | |
| 365 | + | |
| 366 | + array( | |
| 367 | + 'name' => 'cols', | |
| 368 | + 'title' => __( 'Columns', 'weforms' ), | |
| 369 | + 'type' => 'text', | |
| 370 | + 'section' => 'advanced', | |
| 371 | + 'priority' => 11, | |
| 372 | + 'help_text' => __( 'Number of columns in textarea', 'weforms' ), | |
| 373 | + ), | |
| 374 | + | |
| 375 | + array( | |
| 376 | + 'name' => 'placeholder', | |
| 377 | + 'title' => __( 'Placeholder text', 'weforms' ), | |
| 378 | + 'type' => 'text', | |
| 379 | + 'section' => 'advanced', | |
| 380 | + 'priority' => 12, | |
| 381 | + 'help_text' => __( 'Text for HTML5 placeholder attribute', 'weforms' ), | |
| 382 | + 'dependencies' => array( | |
| 383 | + 'rich' => 'no' | |
| 384 | + ) | |
| 385 | + ), | |
| 386 | + | |
| 387 | + array( | |
| 388 | + 'name' => 'default', | |
| 389 | + 'title' => __( 'Default value', 'weforms' ), | |
| 390 | + 'type' => 'text', | |
| 391 | + 'section' => 'advanced', | |
| 392 | + 'priority' => 13, | |
| 393 | + 'help_text' => __( 'The default value this field will have', 'weforms' ), | |
| 394 | + ), | |
| 395 | + | |
| 396 | + array( | |
| 397 | + 'name' => 'rich', | |
| 398 | + 'title' => __( 'Textarea', 'weforms' ), | |
| 399 | + 'type' => 'radio', | |
| 400 | + 'options' => array( | |
| 401 | + 'no' => __( 'Normal', 'weforms' ), | |
| 402 | + 'yes' => __( 'Rich textarea', 'weforms' ), | |
| 403 | + 'teeny' => __( 'Teeny Rich textarea', 'weforms' ), | |
| 404 | + ), | |
| 405 | + 'section' => 'advanced', | |
| 406 | + 'priority' => 14, | |
| 407 | + 'default' => 'no', | |
| 408 | + ), | |
| 409 | + | |
| 410 | + array( | |
| 411 | + 'name' => 'word_restriction', | |
| 412 | + 'title' => __( 'Word Restriction', 'weforms' ), | |
| 413 | + 'type' => 'text', | |
| 414 | + 'section' => 'advanced', | |
| 415 | + 'priority' => 15, | |
| 416 | + 'help_text' => __( 'Numebr of words the author to be restricted in', 'weforms' ), | |
| 417 | + ), | |
| 418 | + ); | |
| 419 | + } | |
| 420 | + | |
| 421 | + /* | |
| 422 | + |-------------------------------------------------------------------------- | |
| 423 | + | Field helper methods | |
| 424 | + |-------------------------------------------------------------------------- | |
| 425 | + | | |
| 426 | + | Various helper method for rendering form fields | |
| 427 | + */ | |
| 428 | + | |
| 429 | + public function print_list_attributes( $field ) { | |
| 430 | + $label = isset( $field['label'] ) ? $field['label'] : ''; | |
| 431 | + $el_name = !empty( $field['name'] ) ? $field['name'] : ''; | |
| 432 | + $class_name = !empty( $field['css'] ) ? ' ' . $field['css'] : ''; | |
| 433 | + $field_size = !empty( $field['width'] ) ? ' field-size-' . $field['width'] : ''; | |
| 434 | + | |
| 435 | + printf( 'class="wpuf-el %s%s%s" data-label="%s"', $el_name, $class_name, $field_size, $label ); | |
| 436 | + } | |
| 437 | + | |
| 438 | + /** | |
| 439 | + * Prints form input label | |
| 440 | + * | |
| 441 | + * @param array $attr | |
| 442 | + * @param integer $form_id | |
| 443 | + */ | |
| 444 | + function print_label( $field, $form_id = 0 ) { | |
| 445 | + ?> | |
| 446 | + <div class="wpuf-label"> | |
| 447 | + <label for="<?php echo isset( $field['name'] ) ? $field['name'] . '_' . $form_id : 'cls'; ?>"><?php echo $field['label'] . $this->required_mark( $field ); ?></label> | |
| 448 | + </div> | |
| 449 | + <?php | |
| 450 | + } | |
| 451 | + | |
| 452 | + /** | |
| 453 | + * Check if a field is required | |
| 454 | + * | |
| 455 | + * @param array $field | |
| 456 | + * | |
| 457 | + * @return boolean | |
| 458 | + */ | |
| 459 | + public function is_required( $field ) { | |
| 460 | + if ( isset( $field['required'] ) && $field['required'] == 'yes' ) { | |
| 461 | + return true; | |
| 462 | + } | |
| 463 | + | |
| 464 | + return false; | |
| 465 | + } | |
| 466 | + | |
| 467 | + /** | |
| 468 | + * Prints required field asterisk | |
| 469 | + * | |
| 470 | + * @param array $attr | |
| 471 | + * @return string | |
| 472 | + */ | |
| 473 | + function required_mark( $field ) { | |
| 474 | + if ( $this->is_required( $field ) ) { | |
| 475 | + return ' <span class="required">*</span>'; | |
| 476 | + } | |
| 477 | + } | |
| 478 | + | |
| 479 | + /** | |
| 480 | + * Prints help text for a field | |
| 481 | + * | |
| 482 | + * @param array $field | |
| 483 | + */ | |
| 484 | + function help_text( $field ) { | |
| 485 | + if ( empty( $field['help'] ) ) { | |
| 486 | + return; | |
| 487 | + } | |
| 488 | + ?> | |
| 489 | + <span class="wpuf-help"><?php echo stripslashes( $field['help'] ); ?></span> | |
| 490 | + <?php | |
| 491 | + } | |
| 492 | + | |
| 493 | + /** | |
| 494 | + * Push logic to conditional array for processing | |
| 495 | + * | |
| 496 | + * @param array $form_field | |
| 497 | + * @param integer $form_id | |
| 498 | + * | |
| 499 | + * @return void | |
| 500 | + */ | |
| 501 | + function conditional_logic( $form_field, $form_id ) { | |
| 502 | + | |
| 503 | + if ( !isset( $form_field['wpuf_cond']['condition_status'] ) || $form_field['wpuf_cond']['condition_status'] != 'yes' ) { | |
| 504 | + return; | |
| 505 | + } | |
| 506 | + | |
| 507 | + $cond_inputs = $form_field['wpuf_cond']; | |
| 508 | + $cond_inputs['condition_status'] = isset( $cond_inputs['condition_status'] ) ? $cond_inputs['condition_status'] : ''; | |
| 509 | + | |
| 510 | + if ( $cond_inputs['condition_status'] == 'yes') { | |
| 511 | + $cond_inputs['type'] = $form_field['template']; | |
| 512 | + $cond_inputs['name'] = $form_field['name']; | |
| 513 | + $cond_inputs['form_id'] = $form_id; | |
| 514 | + $condition = json_encode( $cond_inputs ); | |
| 515 | + | |
| 516 | + } else { | |
| 517 | + $condition = ''; | |
| 518 | + } | |
| 519 | + | |
| 520 | + // for section break | |
| 521 | +// if ( $form_field['template'] == 'section_break' ) { | |
| 522 | +// $cond_inputs['name'] = $form_field['name'] .'_'. $form_field['id']; | |
| 523 | +// $condition = json_encode( $cond_inputs ); | |
| 524 | +// } | |
| 525 | + | |
| 526 | + ?> | |
| 527 | + <script type="text/javascript"> | |
| 528 | + wpuf_conditional_items.push(<?php echo $condition; ?>); | |
| 529 | + </script> | |
| 530 | + <?php | |
| 531 | + } | |
| 532 | + | |
| 533 | + /** | |
| 534 | + * Prepare entry default, can be replaced through field classes | |
| 535 | + * | |
| 536 | + * @param $field | |
| 537 | + * | |
| 538 | + * @return mixed | |
| 539 | + */ | |
| 540 | + public function prepare_entry( $field ) { | |
| 541 | + | |
| 542 | + $value = !empty( $_POST[$field['name']] ) ? $_POST[$field['name']] : ''; | |
| 543 | + | |
| 544 | + if ( is_array( $value ) ) { | |
| 545 | + | |
| 546 | + $entry_value = implode( WeForms::$field_separator, $_POST[$field['name']] ); | |
| 547 | + | |
| 548 | + } else { | |
| 549 | + $entry_value = trim( $value ); | |
| 550 | + } | |
| 551 | + | |
| 552 | + return $entry_value; | |
| 553 | + } | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + /** | |
| 558 | + * Function to check word restriction | |
| 559 | + * | |
| 560 | + * @param $word_nums number of words allowed | |
| 561 | + */ | |
| 562 | + function check_word_restriction_func($word_nums, $rich_text, $field_name) { | |
| 563 | + // bail out if it is dashboard | |
| 564 | + if ( is_admin() ) { | |
| 565 | + return; | |
| 566 | + } | |
| 567 | + ?> | |
| 568 | + <script type="text/javascript"> | |
| 569 | + ;(function($) { | |
| 570 | + $(document).ready( function(){ | |
| 571 | + WP_User_Frontend.editorLimit.bind(<?php printf( '%d, "%s", "%s"', $word_nums, $field_name, $rich_text ); ?>); | |
| 572 | + }); | |
| 573 | + })(jQuery); | |
| 574 | + </script> | |
| 575 | + <?php | |
| 576 | + | |
| 577 | + } | |
| 578 | +} | |