label. */ function stackmime_forms_field_types() { $types = array( 'text' => __( 'Text', 'stackmime-forms' ), 'email' => __( 'Email', 'stackmime-forms' ), 'tel' => __( 'Phone', 'stackmime-forms' ), 'textarea' => __( 'Paragraph', 'stackmime-forms' ), 'select' => __( 'Dropdown', 'stackmime-forms' ), 'radio' => __( 'Single choice', 'stackmime-forms' ), 'choices' => __( 'Checkboxes', 'stackmime-forms' ), 'number' => __( 'Number', 'stackmime-forms' ), 'date' => __( 'Date', 'stackmime-forms' ), 'url' => __( 'Website', 'stackmime-forms' ), 'consent' => __( 'Consent checkbox', 'stackmime-forms' ), 'section' => __( 'Heading and text', 'stackmime-forms' ), ); /** * Field types offered in the builder. Add-ons can add their own types. * * @param array $types type => label. */ return (array) apply_filters( 'stackmime_forms_field_types', $types ); } /** * Types that only display content (no value is submitted), such as headings. */ function stackmime_forms_display_types() { /** * Display-only field types. * * @param string[] $types Types. */ return (array) apply_filters( 'stackmime_forms_display_types', array( 'section' ) ); } /** * Whether a field type only displays content. * * @param string $type Field type. */ function stackmime_forms_is_display_type( $type ) { return in_array( (string) $type, stackmime_forms_display_types(), true ); } /** * Keep extension properties ("x_" keys) on a field or form settings, as plain text. * They let add-ons store their options without the free plugin dropping them when a form is saved. * * @param array $raw Raw values. * @return array Clean x_ values. */ function stackmime_forms_extension_props( $raw ) { $out = array(); foreach ( (array) $raw as $k => $v ) { if ( count( $out ) >= 20 || ! is_string( $k ) || ! preg_match( '/^x_[a-z0-9_]{1,40}$/', $k ) ) { continue; } if ( is_bool( $v ) ) { $out[ $k ] = $v; } elseif ( is_scalar( $v ) ) { $out[ $k ] = mb_substr( sanitize_text_field( (string) $v ), 0, 500 ); } } return $out; } /** * Types that take a list of options. */ function stackmime_forms_option_types() { return array( 'select', 'radio', 'choices' ); } /** * All builder forms: id => definition (fields as an ordered list). */ function stackmime_forms_builder_all() { $forms = get_option( 'stackmime_forms_builder', array() ); return is_array( $forms ) ? $forms : array(); } /** * One builder form, or null. * * @param string $id Form ID. */ function stackmime_forms_builder_get( $id ) { $all = stackmime_forms_builder_all(); return $all[ $id ] ?? null; } /** * Save all builder forms. * * @param array $forms id => definition. */ function stackmime_forms_builder_put( $forms ) { update_option( 'stackmime_forms_builder', $forms, false ); } /** * Register every builder form with the form registry. */ function stackmime_forms_register_builder_forms() { foreach ( stackmime_forms_builder_all() as $id => $def ) { stackmime_forms_register( $id, stackmime_forms_builder_to_registry( $def ) ); } } /** * Turn a stored builder form into a registry definition. * * @param array $def Stored definition. */ function stackmime_forms_builder_to_registry( $def ) { $fields = array(); $summary = array(); $types = stackmime_forms_field_types(); foreach ( (array) ( $def['fields'] ?? array() ) as $field ) { if ( ! isset( $types[ (string) ( $field['type'] ?? '' ) ] ) ) { continue; // A type from an add-on that isn't active: kept in the builder, left out of the form. } $key = (string) $field['key']; $f = array( 'label' => (string) $field['label'], 'type' => (string) $field['type'], 'required' => ! empty( $field['required'] ), 'placeholder' => (string) ( $field['placeholder'] ?? '' ), 'help' => (string) ( $field['help'] ?? '' ), 'width' => 'half' === ( $field['width'] ?? '' ) ? 'half' : 'full', 'options' => (array) ( $field['options'] ?? array() ), 'max_choices' => (int) ( $field['max_choices'] ?? 0 ), ); if ( ! empty( $field['max'] ) ) { $f['max'] = (int) $field['max']; } $f = array_merge( stackmime_forms_extension_props( $field ), $f ); /** * A builder field as the form registry sees it. * * @param array $f Registry field. * @param array $field Stored builder field. */ $fields[ $key ] = (array) apply_filters( 'stackmime_forms_builder_field_args', $f, $field ); if ( ! $summary && 'textarea' === $field['type'] ) { $summary[] = $key; } } $settings = (array) ( $def['settings'] ?? array() ); $args = array( 'title' => (string) ( $def['title'] ?? '' ), 'fields' => $fields, 'summary' => $summary, 'notify_to' => (string) ( $settings['notify_to'] ?? '' ), 'autoreply' => 'on' === ( $settings['autoreply'] ?? '' ) ? true : ( 'off' === ( $settings['autoreply'] ?? '' ) ? false : null ), 'autoreply_subject' => (string) ( $settings['autoreply_subject'] ?? '' ), 'autoreply_body' => (string) ( $settings['autoreply_body'] ?? '' ), 'source' => 'builder', 'x' => stackmime_forms_extension_props( $settings ), ); foreach ( array( 'subject', 'success', 'submit_label' ) as $key ) { if ( '' !== (string) ( $settings[ $key ] ?? '' ) ) { $args[ $key ] = (string) $settings[ $key ]; } } /** * A builder form as the form registry sees it. * * @param array $args Registry arguments. * @param array $def Stored builder form. */ return (array) apply_filters( 'stackmime_forms_builder_form_args', $args, $def ); } /** * Keys a field can't use (they are part of the submission itself). */ function stackmime_forms_reserved_keys() { return array( 'form', 'token', 'page', 'fields', 'entry', 'site', '_sm_hp', 'action', 'id' ); } /** * Clean a form posted by the builder. Everything is checked against an allow-list; unknown keys are dropped. * * @param array $raw Decoded JSON from the builder. * @return array Clean definition: title, fields (list), settings. */ function stackmime_forms_builder_sanitize( $raw ) { $raw = is_array( $raw ) ? $raw : array(); $types = stackmime_forms_field_types(); $title = trim( sanitize_text_field( (string) ( $raw['title'] ?? '' ) ) ); $out = array( 'title' => '' !== $title ? mb_substr( $title, 0, 120 ) : __( 'Untitled form', 'stackmime-forms' ), 'fields' => array(), 'settings' => array(), ); $used = stackmime_forms_reserved_keys(); $n = 0; foreach ( array_slice( (array) ( $raw['fields'] ?? array() ), 0, 60 ) as $field ) { if ( ! is_array( $field ) ) { continue; } $type = (string) ( $field['type'] ?? 'text' ); $dormant = ! isset( $types[ $type ] ); if ( $dormant && ! preg_match( '/^[a-z][a-z0-9_]{1,30}$/', $type ) ) { continue; } ++$n; $label = trim( sanitize_text_field( (string) ( $field['label'] ?? '' ) ) ); $key = sanitize_key( str_replace( '-', '_', (string) ( $field['key'] ?? '' ) ) ); if ( '' === $key ) { $key = sanitize_key( str_replace( array( ' ', '-' ), '_', remove_accents( strtolower( $label ) ) ) ); } $key = '' === $key ? 'field_' . $n : mb_substr( $key, 0, 40 ); if ( preg_match( '/^[0-9]/', $key ) ) { $key = 'f_' . $key; } $base = $key; $i = 2; while ( in_array( $key, $used, true ) ) { $key = $base . '_' . ( $i++ ); } $used[] = $key; $clean = array( 'key' => $key, 'type' => $type, 'label' => '' !== $label ? mb_substr( $label, 0, 200 ) : ( $types[ $type ] ?? $type ), ); if ( $dormant ) { // Type from an add-on that isn't active: keep it as it was so nothing is lost. $clean['help'] = mb_substr( sanitize_textarea_field( (string) ( $field['help'] ?? '' ) ), 0, 500 ); $out['fields'][] = array_merge( $clean, stackmime_forms_extension_props( $field ) ); continue; } $display = stackmime_forms_is_display_type( $type ); if ( ! $display ) { $clean['required'] = ! empty( $field['required'] ); } $clean['help'] = mb_substr( sanitize_textarea_field( (string) ( $field['help'] ?? '' ) ), 0, 500 ); if ( ! $display && ! in_array( $type, array( 'consent', 'radio', 'choices' ), true ) ) { $clean['placeholder'] = mb_substr( sanitize_text_field( (string) ( $field['placeholder'] ?? '' ) ), 0, 120 ); } if ( ! $display ) { $clean['width'] = 'half' === ( $field['width'] ?? '' ) ? 'half' : 'full'; } if ( in_array( $type, array( 'text', 'textarea' ), true ) && ! empty( $field['max'] ) ) { $clean['max'] = min( 'textarea' === $type ? 10000 : 1000, max( 1, absint( $field['max'] ) ) ); } if ( in_array( $type, stackmime_forms_option_types(), true ) ) { $options = array(); $lines = is_array( $field['options'] ?? null ) ? $field['options'] : preg_split( '/\R/', (string) ( $field['options'] ?? '' ) ); foreach ( array_slice( $lines, 0, 100 ) as $line ) { $line = trim( sanitize_text_field( (string) $line ) ); if ( '' === $line ) { continue; } $value = sanitize_key( str_replace( array( ' ', '-' ), '_', remove_accents( strtolower( $line ) ) ) ); $value = '' === $value ? 'option_' . ( count( $options ) + 1 ) : mb_substr( $value, 0, 60 ); while ( isset( $options[ $value ] ) ) { $value .= '_2'; } $options[ $value ] = mb_substr( $line, 0, 200 ); } if ( ! $options ) { $options = array( 'option_1' => __( 'Option 1', 'stackmime-forms' ), 'option_2' => __( 'Option 2', 'stackmime-forms' ), ); } $clean['options'] = $options; if ( 'choices' === $type ) { $clean['max_choices'] = min( 100, absint( $field['max_choices'] ?? 0 ) ); } } $clean = array_merge( stackmime_forms_extension_props( $field ), $clean ); /** * A field after cleaning, before it is saved. Add-ons validate their x_ properties here. * * @param array $clean Clean field. * @param array $field Raw field from the builder. * @param string $type Field type. */ $out['fields'][] = (array) apply_filters( 'stackmime_forms_builder_field', $clean, $field, $type ); } $s = (array) ( $raw['settings'] ?? array() ); $emails = array_filter( array_map( 'sanitize_email', array_map( 'trim', explode( ',', (string) ( $s['notify_to'] ?? '' ) ) ) ), 'is_email' ); $auto = (string) ( $s['autoreply'] ?? 'default' ); $out['settings'] = array( 'submit_label' => mb_substr( trim( sanitize_text_field( (string) ( $s['submit_label'] ?? '' ) ) ), 0, 60 ), 'success' => mb_substr( trim( sanitize_textarea_field( (string) ( $s['success'] ?? '' ) ) ), 0, 500 ), 'notify_to' => implode( ', ', array_slice( $emails, 0, 10 ) ), 'subject' => mb_substr( trim( sanitize_text_field( (string) ( $s['subject'] ?? '' ) ) ), 0, 200 ), 'autoreply' => in_array( $auto, array( 'default', 'on', 'off' ), true ) ? $auto : 'default', 'autoreply_subject' => mb_substr( trim( sanitize_text_field( (string) ( $s['autoreply_subject'] ?? '' ) ) ), 0, 200 ), 'autoreply_body' => mb_substr( trim( sanitize_textarea_field( (string) ( $s['autoreply_body'] ?? '' ) ) ), 0, 5000 ), ); $out['settings'] = array_merge( stackmime_forms_extension_props( $s ), $out['settings'] ); /** * Form settings after cleaning, before they are saved. Add-ons validate their x_ settings here. * * @param array $settings Clean settings. * @param array $raw Raw settings from the builder. */ $out['settings'] = (array) apply_filters( 'stackmime_forms_builder_settings', $out['settings'], $s ); return $out; } /** * A new, unused form ID from a title. * * @param string $title Title. */ function stackmime_forms_new_id( $title ) { $base = sanitize_key( str_replace( array( ' ', '_' ), '-', remove_accents( strtolower( $title ) ) ) ); $base = '' === $base ? 'form' : mb_substr( $base, 0, 40 ); $id = $base; $i = 2; while ( stackmime_forms_get( $id ) || stackmime_forms_builder_get( $id ) ) { $id = $base . '-' . ( $i++ ); } return $id; } /** * Starter templates for new forms. Each is a builder definition without an ID. */ function stackmime_forms_templates() { $name = array( 'type' => 'text', 'key' => 'name', 'label' => __( 'Your name', 'stackmime-forms' ), 'required' => true, 'width' => 'half', ); $email = array( 'type' => 'email', 'key' => 'email', 'label' => __( 'Email', 'stackmime-forms' ), 'required' => true, 'width' => 'half', ); $phone = array( 'type' => 'tel', 'key' => 'phone', 'label' => __( 'Phone', 'stackmime-forms' ), 'width' => 'half', ); $consent = array( 'type' => 'consent', 'key' => 'consent', 'label' => __( 'I agree to be contacted about my enquiry.', 'stackmime-forms' ), 'required' => true, ); return array( 'contact' => array( 'name' => __( 'Contact', 'stackmime-forms' ), 'description' => __( 'Name, email, phone and a message.', 'stackmime-forms' ), 'form' => array( 'title' => __( 'Contact', 'stackmime-forms' ), 'fields' => array( $name, $email, $phone, array( 'type' => 'textarea', 'key' => 'message', 'label' => __( 'How can we help?', 'stackmime-forms' ), 'required' => true, ), ), 'settings' => array( 'submit_label' => __( 'Send message', 'stackmime-forms' ), 'subject' => __( 'New message from {name} (#{entry})', 'stackmime-forms' ), ), ), ), 'quote' => array( 'name' => __( 'Quote request', 'stackmime-forms' ), 'description' => __( 'For services: what they need, budget and timing.', 'stackmime-forms' ), 'form' => array( 'title' => __( 'Quote request', 'stackmime-forms' ), 'fields' => array( $name, array_merge( $phone, array( 'required' => true ) ), array_merge( $email, array( 'required' => false ) ), array( 'type' => 'text', 'key' => 'company', 'label' => __( 'Company', 'stackmime-forms' ), 'width' => 'half', ), array( 'type' => 'select', 'key' => 'service', 'label' => __( 'What do you need?', 'stackmime-forms' ), 'required' => true, 'options' => array( __( 'Service one', 'stackmime-forms' ), __( 'Service two', 'stackmime-forms' ), __( 'Something else', 'stackmime-forms' ) ), ), array( 'type' => 'radio', 'key' => 'timeline', 'label' => __( 'When do you need it?', 'stackmime-forms' ), 'options' => array( __( 'As soon as possible', 'stackmime-forms' ), __( 'Within a month', 'stackmime-forms' ), __( 'Just planning', 'stackmime-forms' ) ), ), array( 'type' => 'textarea', 'key' => 'details', 'label' => __( 'Tell us a little more', 'stackmime-forms' ), ), ), 'settings' => array( 'submit_label' => __( 'Get my quote', 'stackmime-forms' ), 'subject' => __( 'Quote request from {name} (#{entry})', 'stackmime-forms' ), ), ), ), 'callback' => array( 'name' => __( 'Call-back request', 'stackmime-forms' ), 'description' => __( 'A short form for people who prefer a phone call.', 'stackmime-forms' ), 'form' => array( 'title' => __( 'Call-back request', 'stackmime-forms' ), 'fields' => array( $name, array_merge( $phone, array( 'required' => true ) ), array( 'type' => 'select', 'key' => 'best_time', 'label' => __( 'Best time to call', 'stackmime-forms' ), 'options' => array( __( 'Morning', 'stackmime-forms' ), __( 'Afternoon', 'stackmime-forms' ), __( 'Evening', 'stackmime-forms' ) ), ), ), 'settings' => array( 'submit_label' => __( 'Call me back', 'stackmime-forms' ), 'subject' => __( 'Call-back request from {name} (#{entry})', 'stackmime-forms' ), ), ), ), 'booking' => array( 'name' => __( 'Booking request', 'stackmime-forms' ), 'description' => __( 'Preferred date, time and number of people.', 'stackmime-forms' ), 'form' => array( 'title' => __( 'Booking request', 'stackmime-forms' ), 'fields' => array( $name, $email, $phone, array( 'type' => 'date', 'key' => 'date', 'label' => __( 'Preferred date', 'stackmime-forms' ), 'required' => true, 'width' => 'half', ), array( 'type' => 'select', 'key' => 'time', 'label' => __( 'Preferred time', 'stackmime-forms' ), 'width' => 'half', 'options' => array( __( 'Morning', 'stackmime-forms' ), __( 'Afternoon', 'stackmime-forms' ), __( 'Evening', 'stackmime-forms' ) ), ), array( 'type' => 'number', 'key' => 'people', 'label' => __( 'Number of people', 'stackmime-forms' ), 'width' => 'half', ), array( 'type' => 'textarea', 'key' => 'notes', 'label' => __( 'Anything we should know?', 'stackmime-forms' ), ), ), 'settings' => array( 'submit_label' => __( 'Request booking', 'stackmime-forms' ), 'subject' => __( 'Booking request from {name} (#{entry})', 'stackmime-forms' ), ), ), ), 'feedback' => array( 'name' => __( 'Feedback', 'stackmime-forms' ), 'description' => __( 'A rating and comments, with optional contact details.', 'stackmime-forms' ), 'form' => array( 'title' => __( 'Feedback', 'stackmime-forms' ), 'fields' => array( array( 'type' => 'radio', 'key' => 'rating', 'label' => __( 'How did we do?', 'stackmime-forms' ), 'required' => true, 'options' => array( __( 'Excellent', 'stackmime-forms' ), __( 'Good', 'stackmime-forms' ), __( 'Okay', 'stackmime-forms' ), __( 'Poor', 'stackmime-forms' ) ), ), array( 'type' => 'textarea', 'key' => 'comments', 'label' => __( 'Comments', 'stackmime-forms' ), 'required' => true, ), array_merge( $name, array( 'required' => false ) ), array_merge( $email, array( 'required' => false ) ), ), 'settings' => array( 'submit_label' => __( 'Send feedback', 'stackmime-forms' ), 'subject' => __( 'New feedback (#{entry})', 'stackmime-forms' ), ), ), ), 'blank' => array( 'name' => __( 'Blank form', 'stackmime-forms' ), 'description' => __( 'Start from scratch.', 'stackmime-forms' ), 'form' => array( 'title' => __( 'New form', 'stackmime-forms' ), 'fields' => array( $name, $email, $consent ), 'settings' => array(), ), ), ); } /** * Create a builder form from a template. Returns the new ID, or ''. * * @param string $template Template key. * @param string $id Optional fixed ID. */ function stackmime_forms_create_from_template( $template, $id = '' ) { $templates = stackmime_forms_templates(); if ( ! isset( $templates[ $template ] ) ) { return ''; } $def = stackmime_forms_builder_sanitize( $templates[ $template ]['form'] ); $id = $id ? sanitize_key( $id ) : stackmime_forms_new_id( $def['title'] ); $all = stackmime_forms_builder_all(); if ( isset( $all[ $id ] ) ) { return ''; } $all[ $id ] = $def; stackmime_forms_builder_put( $all ); return $id; } /** * First run: give new sites a ready contact form (ID "contact", as in earlier versions). */ function stackmime_forms_seed() { if ( get_option( 'stackmime_forms_seeded' ) ) { return; } update_option( 'stackmime_forms_seeded', 1, false ); if ( ! stackmime_forms_builder_all() ) { stackmime_forms_create_from_template( 'contact', 'contact' ); } }