class-evf-field-address.php
7 years ago
class-evf-field-ai.php
2 years ago
class-evf-field-captcha.php
2 years ago
class-evf-field-checkbox.php
3 years ago
class-evf-field-color.php
3 years ago
class-evf-field-country.php
7 years ago
class-evf-field-credit-card.php
2 years ago
class-evf-field-date-time.php
2 years ago
class-evf-field-divider.php
4 years ago
class-evf-field-email.php
2 years ago
class-evf-field-file-upload.php
6 years ago
class-evf-field-first-name.php
2 years ago
class-evf-field-hidden.php
7 years ago
class-evf-field-html.php
7 years ago
class-evf-field-image-upload.php
7 years ago
class-evf-field-last-name.php
2 years ago
class-evf-field-likert.php
2 years ago
class-evf-field-lookup.php
2 years ago
class-evf-field-number.php
2 years ago
class-evf-field-password.php
7 years ago
class-evf-field-payment-authorize-net.php
2 years ago
class-evf-field-payment-checkbox.php
7 years ago
class-evf-field-payment-coupon.php
2 years ago
class-evf-field-payment-quantity.php
5 years ago
class-evf-field-payment-radio.php
7 years ago
class-evf-field-payment-single.php
7 years ago
class-evf-field-payment-subtotal.php
3 years ago
class-evf-field-payment-total.php
6 years ago
class-evf-field-phone.php
7 years ago
class-evf-field-privacy-policy.php
4 years ago
class-evf-field-progress.php
3 years ago
class-evf-field-radio.php
3 years ago
class-evf-field-range-slider.php
6 years ago
class-evf-field-rating.php
7 years ago
class-evf-field-repeater.php
2 years ago
class-evf-field-reset.php
3 years ago
class-evf-field-scale-rating.php
2 years ago
class-evf-field-select.php
3 years ago
class-evf-field-signature.php
7 years ago
class-evf-field-text.php
2 years ago
class-evf-field-textarea.php
3 years ago
class-evf-field-title.php
6 years ago
class-evf-field-url.php
2 years ago
class-evf-field-wysiwyg.php
4 years ago
class-evf-field-yes-no.php
2 years ago
class-evf-field-ai.php
299 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AI field |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.9.9 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_AI Class. |
| 13 | */ |
| 14 | class EVF_Field_AI extends EVF_Form_Fields { |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * Primary class constructor. |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | |
| 22 | if ( ! class_exists( '\EverestForms\AI' ) ) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | $this->name = esc_html__( 'AI', 'everest-forms' ); |
| 27 | $this->type = 'ai'; |
| 28 | $this->icon = 'evf-icon evf-icon-ai'; |
| 29 | $this->order = 240; |
| 30 | $this->group = 'advanced'; |
| 31 | $this->settings = array( |
| 32 | 'basic-options' => array( |
| 33 | 'field_options' => array( |
| 34 | 'label', |
| 35 | 'meta', |
| 36 | 'description', |
| 37 | ), |
| 38 | ), |
| 39 | 'advanced-options' => array( |
| 40 | 'field_options' => array( |
| 41 | 'ai_chatbot', |
| 42 | 'ai_prompt', |
| 43 | 'ai_type', |
| 44 | 'label_hide', |
| 45 | 'css', |
| 46 | ), |
| 47 | ), |
| 48 | ); |
| 49 | parent::__construct(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Hook in tabs. |
| 54 | */ |
| 55 | public function init_hooks() { |
| 56 | add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * AI chatbot. |
| 61 | * |
| 62 | * @param array $field Field data. |
| 63 | */ |
| 64 | public function ai_chatbot( $field ) { |
| 65 | $value = ! empty( $field['ai_chatbot'] ) ? esc_attr( $field['ai_chatbot'] ) : ''; |
| 66 | $ai_prompt_chatbot = $this->field_element( |
| 67 | 'checkbox', |
| 68 | $field, |
| 69 | array( |
| 70 | 'slug' => 'ai_chatbot', |
| 71 | 'value' => $value, |
| 72 | 'desc' => esc_html__( 'Enable Chatbot', 'everest-forms' ), |
| 73 | /* translators: %1$s - ai settings docs url */ |
| 74 | 'tooltip' => sprintf( esc_html__( 'Check this option to enable chatbot. <a href="%1$s" target="_blank">Learn More</a>', 'everest-forms' ), esc_url( 'https://docs.everestforms.net/docs/ai/#5-toc-title' ) ), |
| 75 | ), |
| 76 | false |
| 77 | ); |
| 78 | $ai_prompt_chatbot = $this->field_element( |
| 79 | 'row', |
| 80 | $field, |
| 81 | array( |
| 82 | 'slug' => 'ai_chatbot', |
| 83 | 'content' => $ai_prompt_chatbot, |
| 84 | ), |
| 85 | false |
| 86 | ); |
| 87 | $args = array( |
| 88 | 'slug' => 'ai_chatbot', |
| 89 | 'content' => $ai_prompt_chatbot, |
| 90 | ); |
| 91 | $this->field_element( 'row', $field, $args ); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * AI Prompt field option. |
| 98 | * |
| 99 | * @param array $field Field data. |
| 100 | */ |
| 101 | public function ai_prompt( $field ) { |
| 102 | $ai_prompt = ! empty( $field['ai_input'] ) ? sanitize_text_field( $field['ai_input'] ) : ''; |
| 103 | $ai_chatbot_input = ! empty( $field['ai_chatbot_input'] ) ? sanitize_text_field( $field['ai_chatbot_input'] ) : ''; |
| 104 | $ai_prompt_label = $this->field_element( |
| 105 | 'label', |
| 106 | $field, |
| 107 | array( |
| 108 | 'slug' => 'ai_input', |
| 109 | 'value' => esc_html__( 'Prompt', 'everest-forms' ), |
| 110 | /* translators: %1$s - ai settings docs url */ |
| 111 | 'tooltip' => sprintf( esc_html__( 'Enter a question or choose a field in the prompt to generate a response. <a href="%1$s" target="_blank">Learn More</a>', 'everest-forms' ), esc_url( 'https://docs.everestforms.net/docs/ai/#7-toc-title' ) ), |
| 112 | ), |
| 113 | false |
| 114 | ); |
| 115 | $ai_prompt_input = $this->field_element( |
| 116 | 'textarea', |
| 117 | $field, |
| 118 | array( |
| 119 | 'slug' => 'ai_input', |
| 120 | 'value' => $ai_prompt, |
| 121 | 'placeholder' => 'Enter a question or choose a field in the prompt to generate a response', |
| 122 | ), |
| 123 | false |
| 124 | ); |
| 125 | $ai_prompt_input .= '<a href="#" class="evf-toggle-smart-tag-display" data-type="fields"><span class="dashicons dashicons-editor-code"></span></a>'; |
| 126 | $ai_prompt_input .= '<div class="evf-smart-tag-lists" style="display: none">'; |
| 127 | $ai_prompt_input .= '<div class="smart-tag-title other-tag-title">Available fields</div><ul class="evf-fields"></ul></div>'; |
| 128 | $args = array( |
| 129 | 'slug' => 'ai_input', |
| 130 | 'content' => $ai_prompt_label . $ai_prompt_input, |
| 131 | 'class' => isset( $field['ai_chatbot'] ) ? 'hidden' : '', |
| 132 | ); |
| 133 | $this->field_element( 'row', $field, $args ); |
| 134 | |
| 135 | $ai_prompt_label = $this->field_element( |
| 136 | 'label', |
| 137 | $field, |
| 138 | array( |
| 139 | 'slug' => 'ai_chatbot_input', |
| 140 | 'value' => esc_html__( 'Field Mapping', 'everest-forms' ), |
| 141 | /* translators: %1$s - ai settings docs url */ |
| 142 | 'tooltip' => sprintf( esc_html__( 'Click on <> and map the field for your question. <a href="%1$s" target="_blank">Learn More</a>', 'everest-forms' ), esc_url( 'https://docs.everestforms.net/docs/ai/#5-toc-title' ) ), |
| 143 | ), |
| 144 | false |
| 145 | ); |
| 146 | $ai_prompt_input = $this->field_element( |
| 147 | 'textarea', |
| 148 | $field, |
| 149 | array( |
| 150 | 'slug' => 'ai_chatbot_input', |
| 151 | 'value' => $ai_chatbot_input, |
| 152 | 'placeholder' => 'Enter a question or choose a field in the prompt to generate a response.', |
| 153 | ), |
| 154 | false |
| 155 | ); |
| 156 | $ai_prompt_input .= '<a href="#" class="evf-toggle-smart-tag-display" data-type="ai-fields"><span class="dashicons dashicons-editor-code"></span></a>'; |
| 157 | $ai_prompt_input .= '<div class="evf-smart-tag-lists" style="display: none">'; |
| 158 | $ai_prompt_input .= '<div class="smart-tag-title other-tag-title">Available fields</div><ul class="evf-fields-ai"></ul></div>'; |
| 159 | $args = array( |
| 160 | 'slug' => 'ai_chatbot_input', |
| 161 | 'content' => $ai_prompt_label . $ai_prompt_input, |
| 162 | 'class' => isset( $field['ai_chatbot'] ) ? '' : 'hidden', |
| 163 | ); |
| 164 | $this->field_element( 'row', $field, $args ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Ai type field option. |
| 169 | * |
| 170 | * @param array $field Field data. |
| 171 | */ |
| 172 | public function ai_type( $field ) { |
| 173 | $ai_type = ! empty( $field['ai_type'] ) ? esc_attr( $field['ai_type'] ) : 'hidden'; |
| 174 | $ai_format_label = $this->field_element( |
| 175 | 'label', |
| 176 | $field, |
| 177 | array( |
| 178 | 'slug' => 'ai_type', |
| 179 | 'value' => esc_html__( 'Field Type', 'everest-forms' ), |
| 180 | /* translators: %1$s - ai settings docs url */ |
| 181 | 'tooltip' => esc_html__( 'Please select the field type.', 'everest-forms' ), |
| 182 | ), |
| 183 | false |
| 184 | ); |
| 185 | if ( 'hidden' === $ai_type ) { |
| 186 | $ai_format_select = $this->field_element( |
| 187 | 'select', |
| 188 | $field, |
| 189 | array( |
| 190 | 'slug' => 'ai_type', |
| 191 | 'value' => $ai_type, |
| 192 | 'options' => array( |
| 193 | 'hidden' => esc_html__( 'Hidden', 'everest-forms' ), |
| 194 | ), |
| 195 | ), |
| 196 | false |
| 197 | ); |
| 198 | } else { |
| 199 | $ai_format_select = $this->field_element( |
| 200 | 'select', |
| 201 | $field, |
| 202 | array( |
| 203 | 'slug' => 'ai_type', |
| 204 | 'value' => $ai_type, |
| 205 | 'options' => array( |
| 206 | 'textarea' => esc_html__( 'Textarea', 'everest-forms' ), |
| 207 | 'html' => esc_html__( 'HTML', 'everest-forms' ), |
| 208 | ), |
| 209 | ), |
| 210 | false |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | $args = array( |
| 215 | 'slug' => 'ai_type', |
| 216 | 'content' => $ai_format_label . $ai_format_select, |
| 217 | ); |
| 218 | $this->field_element( 'row', $field, $args ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Field preview inside the builder. |
| 223 | * |
| 224 | * @since 1.6.1 |
| 225 | * |
| 226 | * @param array $field Field data and settings. |
| 227 | */ |
| 228 | public function field_preview( $field ) { |
| 229 | // Label. |
| 230 | $this->field_preview_option( 'label', $field ); |
| 231 | |
| 232 | // Default value. |
| 233 | $default_value = isset( $field['default_value'] ) && ! empty( $field['default_value'] ) ? $field['default_value'] : ''; |
| 234 | |
| 235 | // Primary input. |
| 236 | echo '<input type="text" value="' . esc_attr( $default_value ) . '" class="widefat" disabled>'; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Define additional field properties. |
| 241 | * |
| 242 | * @since 1.0.0 |
| 243 | * |
| 244 | * @param array $properties Field properties. |
| 245 | * @param array $field Field settings. |
| 246 | * @param array $form_data Form data and settings. |
| 247 | * |
| 248 | * @return array of additional field properties. |
| 249 | */ |
| 250 | public function field_properties( $properties, $field, $form_data ) { |
| 251 | $ai_type = ! empty( $field['ai_type'] ) ? $field['ai_type'] : 'hidden'; |
| 252 | if ( isset( $field['ai_chatbot'] ) ) { |
| 253 | $properties['inputs']['primary']['attr']['ai_chatbot'] = $field['ai_chatbot']; |
| 254 | } |
| 255 | if ( 'hidden' === $ai_type ) { |
| 256 | $properties['label']['attr']['style'] = 'display:none'; |
| 257 | } |
| 258 | return $properties; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Field display on the form front-end. |
| 263 | * |
| 264 | * @since 1.0.0 |
| 265 | * |
| 266 | * @param array $field Field Data. |
| 267 | * @param array $field_atts Field attributes. |
| 268 | * @param array $form_data All Form Data. |
| 269 | */ |
| 270 | public function field_display( $field, $field_atts, $form_data ) { |
| 271 | $value = ''; |
| 272 | $primary = $field['properties']['inputs']['primary']; |
| 273 | $ai_type = ! empty( $field['ai_type'] ) ? $field['ai_type'] : 'hidden'; |
| 274 | switch ( $ai_type ) { |
| 275 | case 'hidden': |
| 276 | printf( |
| 277 | '<input type="hidden" %s>', |
| 278 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ) |
| 279 | ); |
| 280 | break; |
| 281 | case 'textarea': |
| 282 | printf( |
| 283 | '<textarea %s %s >%s</textarea>', |
| 284 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 285 | esc_attr( $primary['required'] ), |
| 286 | esc_html( $value ) |
| 287 | ); |
| 288 | break; |
| 289 | case 'html': |
| 290 | printf( |
| 291 | '<div %s>%s</div>', |
| 292 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 293 | esc_html( $value ) |
| 294 | ); |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 |