name = esc_html__( 'AI', 'ai-contact-form' ); $this->type = 'ai'; $this->icon = 'evf-icon evf-icon-ai'; $this->order = 240; $this->group = 'advanced'; $this->settings = array( 'basic-options' => array( 'field_options' => array( 'label', 'meta', 'description', ), ), 'advanced-options' => array( 'field_options' => array( 'ai_chatbot', 'ai_prompt', 'ai_type', 'label_hide', 'css', ), ), ); parent::__construct(); } /** * Hook in tabs. */ public function init_hooks() { add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); } /** * AI chatbot. * * @param array $field Field data. */ public function ai_chatbot( $field ) { $value = ! empty( $field['ai_chatbot'] ) ? esc_attr( $field['ai_chatbot'] ) : ''; $ai_prompt_chatbot = $this->field_element( 'checkbox', $field, array( 'slug' => 'ai_chatbot', 'value' => $value, 'desc' => esc_html__( 'Enable Chatbot', 'ai-contact-form' ), /* translators: %1$s - ai settings docs url */ 'tooltip' => sprintf( esc_html__( 'Check this option to enable chatbot. Learn More', 'ai-contact-form' ), esc_url( 'https://docs.everestforms.net/docs/ai/#5-toc-title' ) ), ), false ); $ai_prompt_chatbot = $this->field_element( 'row', $field, array( 'slug' => 'ai_chatbot', 'content' => $ai_prompt_chatbot, ), false ); $args = array( 'slug' => 'ai_chatbot', 'content' => $ai_prompt_chatbot, ); $this->field_element( 'row', $field, $args ); } /** * AI Prompt field option. * * @param array $field Field data. */ public function ai_prompt( $field ) { $ai_prompt = ! empty( $field['ai_input'] ) ? sanitize_text_field( $field['ai_input'] ) : ''; $ai_chatbot_input = ! empty( $field['ai_chatbot_input'] ) ? sanitize_text_field( $field['ai_chatbot_input'] ) : ''; $ai_prompt_label = $this->field_element( 'label', $field, array( 'slug' => 'ai_input', 'value' => esc_html__( 'Prompt', 'ai-contact-form' ), /* translators: %1$s - ai settings docs url */ 'tooltip' => sprintf( esc_html__( 'Enter a question or choose a field in the prompt to generate a response. Learn More', 'ai-contact-form' ), esc_url( 'https://docs.everestforms.net/docs/ai/#7-toc-title' ) ), ), false ); $ai_prompt_input = $this->field_element( 'textarea', $field, array( 'slug' => 'ai_input', 'value' => $ai_prompt, 'placeholder' => 'Enter a question or choose a field in the prompt to generate a response', ), false ); $ai_prompt_input .= ''; $ai_prompt_input .= ''; $args = array( 'slug' => 'ai_input', 'content' => $ai_prompt_label . $ai_prompt_input, 'class' => isset( $field['ai_chatbot'] ) ? 'hidden' : '', ); $this->field_element( 'row', $field, $args ); $ai_prompt_label = $this->field_element( 'label', $field, array( 'slug' => 'ai_chatbot_input', 'value' => esc_html__( 'Field Mapping', 'ai-contact-form' ), /* translators: %1$s - ai settings docs url */ 'tooltip' => sprintf( esc_html__( 'Click on <> and map the field created for the question input. Learn More', 'ai-contact-form' ), esc_url( 'https://docs.everestforms.net/docs/ai/#5-toc-title' ) ), ), false ); $ai_prompt_input = $this->field_element( 'textarea', $field, array( 'slug' => 'ai_chatbot_input', 'value' => $ai_chatbot_input, 'placeholder' => 'Enter a question or choose a field in the prompt to generate a response.', ), false ); $ai_prompt_input .= ''; $ai_prompt_input .= ''; $args = array( 'slug' => 'ai_chatbot_input', 'content' => $ai_prompt_label . $ai_prompt_input, 'class' => isset( $field['ai_chatbot'] ) ? '' : 'hidden', ); $this->field_element( 'row', $field, $args ); } /** * Ai type field option. * * @param array $field Field data. */ public function ai_type( $field ) { $ai_type = ! empty( $field['ai_type'] ) ? esc_attr( $field['ai_type'] ) : 'hidden'; $ai_format_label = $this->field_element( 'label', $field, array( 'slug' => 'ai_type', 'value' => esc_html__( 'Field Type', 'everest-forms-open-ai' ), 'tooltip' => esc_html__( 'Please select the field type.', 'everest-forms-open-ai' ), ), false ); if ( 'hidden' === $ai_type ) { $ai_format_select = $this->field_element( 'select', $field, array( 'slug' => 'ai_type', 'value' => $ai_type, 'options' => array( 'hidden' => esc_html__( 'Hidden', 'ai-contact-form' ), ), ), false ); } else { $ai_format_select = $this->field_element( 'select', $field, array( 'slug' => 'ai_type', 'value' => $ai_type, 'options' => array( 'textarea' => esc_html__( 'Textarea', 'ai-contact-form' ), 'html' => esc_html__( 'HTML', 'ai-contact-form' ), ), ), false ); } $args = array( 'slug' => 'ai_type', 'content' => $ai_format_label . $ai_format_select, ); $this->field_element( 'row', $field, $args ); } /** * Field preview inside the builder. * * @since 1.6.1 * * @param array $field Field data and settings. */ public function field_preview( $field ) { // Label. $this->field_preview_option( 'label', $field ); // Default value. $default_value = isset( $field['default_value'] ) && ! empty( $field['default_value'] ) ? $field['default_value'] : ''; // Primary input. echo ''; } /** * Define additional field properties. * * @since 1.0.0 * * @param array $properties Field properties. * @param array $field Field settings. * @param array $form_data Form data and settings. * * @return array of additional field properties. */ public function field_properties( $properties, $field, $form_data ) { $ai_type = ! empty( $field['ai_type'] ) ? $field['ai_type'] : 'hidden'; if ( isset( $field['ai_chatbot'] ) ) { $properties['inputs']['primary']['attr']['ai_chatbot'] = $field['ai_chatbot']; } if ( 'hidden' === $ai_type ) { $properties['label']['attr']['style'] = 'display:none'; } return $properties; } /** * Field display on the form front-end. * * @since 1.0.0 * * @param array $field Field Data. * @param array $field_atts Field attributes. * @param array $form_data All Form Data. */ public function field_display( $field, $field_atts, $form_data ) { $value = ''; $primary = $field['properties']['inputs']['primary']; $ai_type = ! empty( $field['ai_type'] ) ? $field['ai_type'] : 'hidden'; switch ( $ai_type ) { case 'hidden': printf( '', $this->ai_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ) ); break; case 'textarea': printf( '', $this->ai_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), esc_attr( $primary['required'] ), esc_html( $value ) ); break; case 'html': printf( '
%s
', $this->ai_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), esc_html( $value ) ); break; } } /** * Formats, sanitizes, and returns/echos HTML element ID, classes, attributes, * * @param string $id Element ID. * @param array $class Class args. * @param array $datas Data args. * @param array $atts Attributes. * @param bool $echo True to echo else return. * * @return string */ private function ai_html_attributes( $id = '', $class = array(), $datas = array(), $atts = array(), $echo = false ) { $id = trim( $id ); $parts = array(); if ( ! empty( $id ) ) { $id = sanitize_html_class( $id ); if ( ! empty( $id ) ) { $parts[] = 'id="' . $id . '"'; } } if ( ! empty( $class ) ) { $class = $this->ai_sanitize_classes( $class, true ); if ( ! empty( $class ) ) { $parts[] = 'class="' . $class . '"'; } } if ( ! empty( $datas ) ) { foreach ( $datas as $data => $val ) { $parts[] = 'data-' . sanitize_html_class( $data ) . '="' . esc_attr( $val ) . '"'; } } if ( ! empty( $atts ) ) { foreach ( $atts as $att => $val ) { if ( '0' === $val || ! empty( $val ) ) { if ( $att[0] === '[' ) { //phpcs:ignore // Handle special case for bound attributes in AMP. $escaped_att = '[' . sanitize_html_class( trim( $att, '[]' ) ) . ']'; } else { $escaped_att = sanitize_html_class( $att ); } $parts[] = $escaped_att . '="' . esc_attr( $val ) . '"'; } } } $output = implode( ' ', $parts ); if ( $echo ) { echo esc_html( trim( $output ) ); } else { return trim( $output ); } } /** * Sanitize string of CSS classes. * * @param array|string $classes Class names. * @param bool $convert True will convert strings to array and vice versa. * * @return string|array */ private function ai_sanitize_classes( $classes, $convert = false ) { $css = array(); $array = is_array( $classes ); if ( ! empty( $classes ) ) { if ( ! $array ) { $classes = explode( ' ', trim( $classes ) ); } foreach ( $classes as $class ) { $css[] = sanitize_html_class( $class ); } } if ( $array ) { return $convert ? implode( ' ', $css ) : $css; } else { return $convert ? $css : implode( ' ', $css ); } } }