form_fields, true ); if ( ! is_array( $structure ) || ! isset( $structure['fields'] ) || ! is_array( $structure['fields'] ) ) { return; } $defaults = fluentformLoadFile( 'Services/FormBuilder/DefaultElements.php' ); $message = $defaults['general']['textarea'] ?? array(); if ( ! $message ) { return; } $fields_by_name = array(); $changed = false; foreach ( $structure['fields'] as $field ) { $name = (string) ( $field['attributes']['name'] ?? '' ); if ( ! in_array( $name, array( 'email', 'whatsapp', 'message' ), true ) ) { $changed = true; continue; } if ( isset( $fields_by_name[ $name ] ) ) { $changed = true; continue; } $fields_by_name[ $name ] = $field; } if ( isset( $fields_by_name['email'] ) ) { $fields_by_name['email']['settings']['validation_rules']['required']['value'] = true; } if ( isset( $fields_by_name['whatsapp'] ) ) { $whatsapp = $fields_by_name['whatsapp']; $whatsapp['settings']['validation_rules']['required']['value'] = true; $whatsapp['settings']['temp_mask'] = 'custom'; $whatsapp['settings']['data-mask-reverse'] = 'no'; $whatsapp['attributes']['data-mask'] = '(00) 00000-0000'; $fields_by_name['whatsapp'] = $whatsapp; } if ( ! isset( $fields_by_name['whatsapp'] ) ) { $whatsapp = $defaults['general']['input_mask'] ?? array(); if ( $whatsapp ) { $whatsapp['uniqElKey'] = 'el_gso_contact_whatsapp'; $whatsapp['attributes']['name'] = 'whatsapp'; $whatsapp['attributes']['placeholder'] = '(31) 99999-9999'; $whatsapp['attributes']['data-mask'] = '(00) 00000-0000'; $whatsapp['settings']['label'] = __( 'WhatsApp', 'gustavoo-portfolio' ); $whatsapp['settings']['admin_field_label'] = __( 'WhatsApp', 'gustavoo-portfolio' ); $whatsapp['settings']['mobile_keyboard_type'] = 'tel'; $whatsapp['settings']['temp_mask'] = 'custom'; $whatsapp['settings']['data-mask-reverse'] = 'no'; $whatsapp['settings']['validation_rules']['required']['value'] = true; $fields_by_name['whatsapp'] = $whatsapp; } } if ( ! isset( $fields_by_name['message'] ) || 'textarea' !== (string) ( $fields_by_name['message']['element'] ?? '' ) ) { $message['uniqElKey'] = 'el_gso_contact_message'; $message['attributes']['name'] = 'message'; $message['attributes']['placeholder'] = __( 'Descreva o motivo do contato e o que você quer construir.', 'gustavoo-portfolio' ); $message['attributes']['rows'] = 5; $message['settings']['label'] = __( 'Como posso ajudar?', 'gustavoo-portfolio' ); $message['settings']['admin_field_label'] = __( 'Motivo do contato', 'gustavoo-portfolio' ); $message['settings']['validation_rules']['required']['value'] = true; $fields_by_name['message'] = $message; } $fields = array(); foreach ( array( 'email', 'whatsapp', 'message' ) as $required_name ) { if ( isset( $fields_by_name[ $required_name ] ) ) { $fields[] = $fields_by_name[ $required_name ]; } } $changed = wp_json_encode( $structure['fields'] ) !== wp_json_encode( $fields ); if ( ! $changed ) { return; } $structure['fields'] = $fields; $form_fields = wp_json_encode( $structure, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); if ( class_exists( '\FluentForm\App\Services\Form\FormService' ) ) { ( new \FluentForm\App\Services\Form\FormService() )->update( array( 'form_id' => $form->id, 'title' => $form->title, 'status' => $form->status, 'formFields' => $form_fields, ) ); } else { $form->form_fields = $form_fields; $form->save(); } } add_action( 'init', 'gustavoo_portfolio_ensure_contact_form_fields', 30 ); /** * Preserve contact fields in submissions while an older form schema is being * migrated. Fluent Forms only whitelists fields known by its saved schema. * * @param array $form_data Sanitized submission data. * @param int $form_id Submitted form ID. * @param array $input_configs Fluent Forms input configuration. * @return array */ function gustavoo_portfolio_preserve_contact_submission_fields( $form_data, $form_id, $input_configs ) { $contact_form_id = absint( gustavoo_portfolio_get_setting( 'contact_form_id' ) ); if ( ! $contact_form_id || $contact_form_id !== absint( $form_id ) ) { return $form_data; } if ( isset( $_POST['whatsapp'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Fluent Forms validates the submission. $form_data['whatsapp'] = sanitize_text_field( wp_unslash( $_POST['whatsapp'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing } if ( isset( $_POST['message'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Fluent Forms validates the submission. $form_data['message'] = sanitize_textarea_field( wp_unslash( $_POST['message'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing } return $form_data; } add_filter( 'fluentform/insert_response_data', 'gustavoo_portfolio_preserve_contact_submission_fields', 10, 3 ); /** * Render a Fluent Forms form from a numeric, administrator-controlled ID. * * The theme deliberately never stores or executes arbitrary shortcodes. Contact * synchronization is configured through the FluentCRM feed inside Fluent Forms. * * @param int $form_id Form ID. * @param string $context Form context, used for classes and filters. * @return string */ function gustavoo_portfolio_render_fluent_form( $form_id, $context = 'default' ) { $form_id = absint( $form_id ); $context = sanitize_html_class( $context ); if ( $form_id && shortcode_exists( 'fluentform' ) ) { if ( 'contact' === $context ) { gustavoo_portfolio_ensure_contact_form_fields(); } $shortcode = sprintf( '[fluentform id="%d"]', $form_id ); $markup = do_shortcode( $shortcode ); // Older persisted forms may still contain only the e-mail field. Keep the // requested fields visible inside the real Fluent Forms
' ) ) { $missing_fields = ''; if ( false === stripos( $markup, 'name="whatsapp"' ) ) { $missing_fields .= '