'Quote request', * 'fields' => array( * 'name' => array( 'label' => 'Name', 'type' => 'text', 'required' => true ), * 'phone' => array( 'label' => 'Phone', 'type' => 'tel', 'required' => true ), * ), * ) ); * } ); * * Field types: text, email, tel, url, textarea, select, radio, choices, consent, date, number and section * (a heading and text between fields, never submitted). Unknown fields are always rejected. * "choices" takes several options at once (sent comma separated); "max_choices" limits how many. * "width" is full (default) or half: two half-width fields sit side by side on wide screens. * Forms made in the builder (StackMime Forms → Forms) are registered the same way. * Optional per form: autoreply_subject and autoreply_body (placeholders: {name}, {entry}, any field, {site}). * * @package StackMime\Forms */ defined( 'ABSPATH' ) || exit; $GLOBALS['stackmime_forms_registry'] = array(); /** * Register a form. * * @param string $id Form ID (letters, numbers, dashes). * @param array $args Definition. */ function stackmime_forms_register( $id, $args ) { $id = sanitize_key( $id ); $args = wp_parse_args( $args, array( 'title' => ucfirst( $id ), 'fields' => array(), 'notify_to' => '', /* translators: Default notification subject. Keep the {placeholders}. */ 'subject' => __( 'New {form} from {name} (#{entry})', 'stackmime-forms' ), 'summary' => array(), 'autoreply' => null, // Optional per-form auto-reply text (e.g. another language). Empty = the text under Settings. 'autoreply_subject' => '', 'autoreply_body' => '', 'success' => __( 'Thanks! We\'ve received your message and will be in touch shortly.', 'stackmime-forms' ), 'submit_label' => __( 'Send', 'stackmime-forms' ), 'source' => 'code', ) ); foreach ( $args['fields'] as $key => $field ) { $args['fields'][ $key ] = wp_parse_args( $field, array( 'label' => $key, 'type' => 'text', 'required' => false, 'max' => 'textarea' === ( $field['type'] ?? '' ) ? 3000 : 200, 'options' => array(), 'max_choices' => 0, 'placeholder' => '', 'width' => 'full', 'help' => '', ) ); } $GLOBALS['stackmime_forms_registry'][ $id ] = $args; } /** * Get one form definition, or null. * * @param string $id Form ID. */ function stackmime_forms_get( $id ) { $all = stackmime_forms_all(); return $all[ $id ] ?? null; } /** * All registered forms. */ function stackmime_forms_all() { return $GLOBALS['stackmime_forms_registry']; } add_action( 'init', function () { // Forms made in the builder first, then forms registered in code by themes and plugins (code wins on the same ID). stackmime_forms_register_builder_forms(); do_action( 'stackmime_forms_init' ); }, 5 ); /** * The first field of a type in a form (e.g. the email field used for Reply-To and the auto-reply), or ''. * * @param array $form Form definition. * @param string $type Field type. * @param string $hint Preferred key fragment, e.g. 'name'. */ function stackmime_forms_field_of_type( $form, $type, $hint = '' ) { $first = ''; foreach ( $form['fields'] as $key => $field ) { if ( $type !== $field['type'] ) { continue; } if ( '' === $hint || false !== strpos( (string) $key, $hint ) ) { return (string) $key; } if ( '' === $first ) { $first = (string) $key; } } return $first; } /** * Name, email and phone of the person who sent an entry, whatever the field keys are. * * @param array|null $form Form definition (null when the form no longer exists). * @param array $fields Entry values. * @return array{name: string, email: string, phone: string} */ function stackmime_forms_entry_contact( $form, $fields ) { $keys = array( 'name' => 'name', 'email' => 'email', 'phone' => 'phone', ); if ( $form ) { $keys['name'] = stackmime_forms_field_of_type( $form, 'text', 'name' ); $keys['email'] = stackmime_forms_field_of_type( $form, 'email' ); $keys['phone'] = stackmime_forms_field_of_type( $form, 'tel' ); } $out = array(); foreach ( $keys as $what => $key ) { $out[ $what ] = ( '' !== $key && isset( $fields[ $key ] ) && is_scalar( $fields[ $key ] ) ) ? trim( (string) $fields[ $key ] ) : ''; } if ( '' !== $out['email'] && ! is_email( $out['email'] ) ) { $out['email'] = ''; } return $out; }