__('Single line text', 'ssf-forms'), 'email' => __('Email', 'ssf-forms'), 'tel' => __('Phone', 'ssf-forms'), 'url' => __('Website', 'ssf-forms'), 'textarea' => __('Paragraph', 'ssf-forms'), 'number' => __('Number', 'ssf-forms'), 'select' => __('Dropdown', 'ssf-forms'), 'radio' => __('Radio choices', 'ssf-forms'), 'checkbox' => __('Checkbox choices', 'ssf-forms'), 'date' => __('Date', 'ssf-forms'), 'time' => __('Time', 'ssf-forms'), 'hidden' => __('Hidden fixed value', 'ssf-forms'), 'section' => __('Section', 'ssf-forms'), 'consent' => __('Consent', 'ssf-forms'), ]); } public function supports_form($form) { // Registered field types are supplied by this plugin or an active extension. // Missing implementations must not silently discard submitted answers. $types = $this->types(); foreach ($form['fields'] ?? [] as $field) { if (!isset($types[$field['type'] ?? ''])) { return false; } } return true; } public function schema($input) { if (!is_array($input)) { return $this->error(__('A form object is required.', 'ssf-forms')); } if (strlen(wp_json_encode($input)) > 524288) { return new \WP_Error( 'ssffw_schema_size', __('The form configuration is too large.', 'ssf-forms'), [ 'status' => 413, ], ); } $title = sanitize_text_field($input['title'] ?? ''); if ($title === '' || $this->length($title) > 200) { return $this->error(__('Enter a form title of up to 200 characters.', 'ssf-forms')); } $raw_fields = $input['fields'] ?? []; if (!is_array($raw_fields) || count($raw_fields) > 100) { return $this->error(__('A form may contain up to 100 fields.', 'ssf-forms')); } $fields = []; $ids = []; $types = $this->types(); foreach ($raw_fields as $raw) { if (!is_array($raw)) { return $this->error(__('Each field must be an object.', 'ssf-forms')); } if (!is_string($raw['id'] ?? null)) { return $this->error(__('Each field needs a text ID.', 'ssf-forms')); } $id = $raw['id']; if (!preg_match('/^[a-z][a-z0-9_]{0,63}$/D', $id) || isset($ids[$id])) { return $this->error( __( 'Field IDs must be unique lowercase letters, numbers and underscores, beginning with a letter.', 'ssf-forms', ), ); } $type = sanitize_key($raw['type'] ?? 'text'); if (!isset($types[$type])) { return $this->error(__('This form uses a field type that is unavailable.', 'ssf-forms')); } $field = self::clean_structure($raw); $field['id'] = $id; $field['type'] = $type; $field['label'] = sanitize_text_field($raw['label'] ?? ''); if ($field['label'] === '' || $this->length($field['label']) > 200) { return $this->error(__('Every field needs a label of up to 200 characters.', 'ssf-forms')); } $field['required'] = !empty($raw['required']); $field['placeholder'] = $this->trim_text($raw['placeholder'] ?? '', 200); $field['help'] = $this->trim_text($raw['help'] ?? '', 1000); $field['default'] = is_scalar($raw['default'] ?? '') ? $this->trim_text($raw['default'] ?? '', 10000) : ''; $field['options'] = []; if (isset($raw['options'])) { if (!is_array($raw['options']) || count($raw['options']) > 200) { return $this->error(__('Choice fields may contain up to 200 options.', 'ssf-forms')); } foreach ($raw['options'] as $option) { if (!is_scalar($option)) { return $this->error(__('Choice options must be text.', 'ssf-forms')); } $option = $this->trim_text($option, 200); if ($option !== '') { $field['options'][] = $option; } } $field['options'] = array_values(array_unique($field['options'])); } if (in_array($type, ['select', 'radio', 'checkbox'], true) && !$field['options']) { return $this->error(__('Choice fields need at least one option.', 'ssf-forms')); } foreach (['min', 'max'] as $bound) { if (isset($raw[$bound]) && $raw[$bound] !== '') { if (!is_numeric($raw[$bound]) || !is_finite((float) $raw[$bound])) { return $this->error(__('Field bounds must be finite numbers.', 'ssf-forms')); } $field[$bound] = 0 + $raw[$bound]; } else { unset($field[$bound]); } } if (isset($field['min'], $field['max']) && $field['min'] > $field['max']) { return $this->error(__('A field minimum cannot exceed its maximum.', 'ssf-forms')); } if (!empty($raw['condition'])) { $condition = $raw['condition']; if ( !is_array($condition) || !in_array( $condition['operator'] ?? '', ['equals', 'not_equals', 'contains', 'not_empty'], true, ) ) { return $this->error(__('A field condition is invalid.', 'ssf-forms')); } $field['condition'] = [ 'field' => sanitize_key($condition['field'] ?? ''), 'operator' => $condition['operator'], 'value' => $this->trim_text($condition['value'] ?? '', 200), ]; } else { unset($field['condition']); } $fields[] = $field; $ids[$id] = count($fields) - 1; } foreach ($fields as $field) { if (!isset($field['condition'])) { continue; } $dependency = $field['condition']['field']; if (!isset($ids[$dependency]) || $dependency === $field['id']) { return $this->error( __('Conditions must refer to another field in this form.', 'ssf-forms'), ); } $visited = [$field['id'] => true]; while (isset($ids[$dependency])) { if (isset($visited[$dependency])) { return $this->error(__('Field conditions cannot contain a cycle.', 'ssf-forms')); } $visited[$dependency] = true; $next = $fields[$ids[$dependency]]['condition']['field'] ?? null; if (!$next) { break; } $dependency = $next; } } $settings = is_array($input['settings'] ?? null) ? self::clean_structure($input['settings']) : []; $settings['submit_label'] = $this->trim_text( $settings['submit_label'] ?? __('Send submission', 'ssf-forms'), 100, ); $settings['success_message'] = $this->trim_text( $settings['success_message'] ?? __('Your submission has been saved. Thank you.', 'ssf-forms'), 2000, ); $settings['notification_enabled'] = !empty($settings['notification_enabled']); $settings['notification_to'] = $this->recipients( $settings['notification_to'] ?? get_option('admin_email'), ); if (is_wp_error($settings['notification_to'])) { return $settings['notification_to']; } $settings['notification_to'] = implode(', ', $settings['notification_to']); $settings['notification_subject'] = str_replace( ["\r", "\n"], '', $this->trim_text( $settings['notification_subject'] ?? __('New submission: {form_title}', 'ssf-forms'), 180, ), ); $settings['retention_days'] = min(3650, max(0, (int) ($settings['retention_days'] ?? 90))); if (!isset($settings['integrations']) || !is_array($settings['integrations'])) { $settings['integrations'] = []; } $appearance = is_array($settings['appearance'] ?? null) ? $settings['appearance'] : []; foreach (['accent', 'background', 'text', 'border'] as $key) { $appearance[$key] = sanitize_hex_color($appearance[$key] ?? '') ?: ''; } $appearance['radius'] = min(32, max(0, (int) ($appearance['radius'] ?? 10))); $appearance['width'] = min(1200, max(240, (int) ($appearance['width'] ?? 720))); $settings['appearance'] = $appearance; $form = [ 'id' => absint($input['id'] ?? 0), 'title' => $title, 'status' => ($input['status'] ?? '') === 'publish' ? 'publish' : 'draft', 'version' => max(1, absint($input['version'] ?? 1)), 'fields' => $fields, 'settings' => $settings, ]; try { $form = apply_filters('ssffw_validate_schema', $form); } catch (\Throwable $error) { return $this->error( __('An optional field extension could not validate this form.', 'ssf-forms'), ); } return is_wp_error($form) || is_array($form) ? $form : $this->error(__('A form extension returned an invalid schema.', 'ssf-forms')); } public function recipients($value) { $recipients = is_array($value) ? $value : preg_split('/[,;]+/', (string) $value); $clean = []; foreach ($recipients as $email) { if (!is_scalar($email)) { return $this->error(__('Notification recipients must be email addresses.', 'ssf-forms')); } $email = trim((string) $email); if ($email === '') { continue; } if (!is_email($email) || preg_match('/[\r\n]/', $email)) { return $this->error(__('Enter valid notification email addresses.', 'ssf-forms')); } $clean[] = sanitize_email($email); } if (count($clean) > 10) { return $this->error(__('Use at most ten notification recipients.', 'ssf-forms')); } return array_values(array_unique($clean)); } public function submission($form, $values, $context = []) { if (!$this->supports_form($form)) { return new \WP_Error( 'ssffw_unavailable_fields', __('This form requires an unavailable extension.', 'ssf-forms'), ['status' => 409], ); } if (!is_array($values) || count($values) > 150) { return $this->error(__('The submitted values are invalid.', 'ssf-forms')); } // Reduce user input to declared fields before any extension can observe it. $values = $this->submission_input($form, $values); if (is_wp_error($values)) { return $values; } $context = $this->submission_context($context, $values); try { $values = apply_filters('ssffw_before_submit', $values, $form, $context); } catch (\Throwable $error) { return new \WP_Error( 'ssffw_extension_validation', __( 'This form could not validate its optional fields. Please contact the website owner.', 'ssf-forms', ), ['status' => 503], ); } if (is_wp_error($values)) { return $values; } if (!is_array($values)) { return $this->error(__('A submission extension returned invalid values.', 'ssf-forms')); } $normalized = []; $errors = []; foreach ($form['fields'] as $field) { if (!$this->is_visible($field, $form, $values) || $field['type'] === 'section') { continue; } $id = $field['id']; $raw = $field['type'] === 'hidden' ? $field['default'] ?? '' : $values[$id] ?? null; try { $extension = apply_filters( 'ssffw_validate_field', null, $field, $raw, $form, $values, $context, ); } catch (\Throwable $error) { return new \WP_Error( 'ssffw_extension_validation', __( 'This form could not validate its optional fields. Please contact the website owner.', 'ssf-forms', ), ['status' => 503], ); } if (is_wp_error($extension)) { $errors[$id] = $extension->get_error_message(); continue; } if (is_array($extension) && array_key_exists('value', $extension)) { $normalized[$id] = $extension['value']; if (!empty($extension['errors'])) { $errors[$id] = is_array($extension['errors']) ? implode(' ', $extension['errors']) : (string) $extension['errors']; } continue; } $result = $this->field($field, $raw); if (is_wp_error($result)) { $errors[$id] = $result->get_error_message(); } else { $normalized[$id] = $result; } } if ($errors) { return new \WP_Error( 'ssffw_validation', __('Please check the highlighted fields.', 'ssf-forms'), [ 'status' => 422, 'fields' => $errors, ], ); } return $normalized; } /** Sanitize a bounded value tree while preserving scalar/array types for field validation. */ private function input_value($value, $depth = 0) { if ($depth > 8) { return $this->error(__('The submitted values are too deeply nested.', 'ssf-forms')); } if (is_array($value)) { if (count($value) > 200) { return $this->error(__('The submitted value contains too many items.', 'ssf-forms')); } $result = []; foreach ($value as $key => $item) { // Do not silently merge invalid keys after sanitization. if (!is_int($key) && (!is_string($key) || sanitize_key($key) !== $key)) { return $this->error(__('The submitted field keys are invalid.', 'ssf-forms')); } $clean = $this->input_value($item, $depth + 1); if (is_wp_error($clean)) { return $clean; } $result[$key] = $clean; } return $result; } if (is_string($value)) { if ($this->length($value) > 10000) { return $this->error(__('This answer is too long.', 'ssf-forms')); } return sanitize_textarea_field($value); } if (is_float($value) && !is_finite($value)) { return $this->error(__('Enter a finite number.', 'ssf-forms')); } if (is_scalar($value) || $value === null) { return $value; } return $this->error(__('The submitted value has an invalid type.', 'ssf-forms')); } public function submission_input($form, $values) { $clean = []; foreach ($form['fields'] as $field) { $id = $field['id']; $raw = $field['type'] === 'hidden' ? $field['default'] ?? '' : $values[$id] ?? null; $clean[$id] = $this->input_value($raw); if (is_wp_error($clean[$id])) { return $clean[$id]; } } $result = []; $errors = []; $builtins = [ 'text', 'email', 'tel', 'url', 'textarea', 'number', 'select', 'radio', 'checkbox', 'date', 'time', 'hidden', 'consent', ]; foreach ($form['fields'] as $field) { if (!$this->is_visible($field, $form, $clean) || $field['type'] === 'section') { continue; } $value = $clean[$field['id']]; if (in_array($field['type'], $builtins, true)) { $value = $this->field($field, $value); } if (is_wp_error($value)) { $errors[$field['id']] = $value->get_error_message(); } else { $result[$field['id']] = $value; } } if ($errors) { return new \WP_Error( 'ssffw_validation', __('Please check the highlighted fields.', 'ssf-forms'), [ 'status' => 422, 'fields' => $errors, ], ); } return $result; } /** Keep the legacy request-shaped extension API, with only intentional sanitized parameters. */ public function submission_context($context, $values) { $clean = [ 'user_id' => get_current_user_id(), 'submission_id' => sanitize_text_field($context['submission_id'] ?? ''), ]; if (($context['request'] ?? null) instanceof \WP_REST_Request) { $original = $context['request']; $request = new \WP_REST_Request( 'POST', '/ssffw/v1/forms/' . absint($original['id']) . '/submit', ); $request->set_url_params(['id' => absint($original['id'])]); $extra = $original->get_param('context'); $request->set_body_params([ 'values' => $values, 'submission_id' => $clean['submission_id'], 'context' => is_array($extra) ? self::clean_structure($extra) : [], ]); $clean['request'] = $request; } return $clean; } public function is_visible($field, $form, $values, $visited = []) { if (empty($field['condition'])) { return true; } if (isset($visited[$field['id']])) { return false; } $visited[$field['id']] = true; $condition = $field['condition']; $parent = null; foreach ($form['fields'] as $candidate) { if ($candidate['id'] === $condition['field']) { $parent = $candidate; break; } } if (!$parent || !$this->is_visible($parent, $form, $values, $visited)) { return false; } $value = $parent['type'] === 'hidden' ? $parent['default'] ?? '' : $values[$condition['field']] ?? ''; $expected = (string) ($condition['value'] ?? ''); $equal = is_array($value) ? in_array($expected, array_map('strval', array_filter($value, 'is_scalar')), true) : is_scalar($value) && (string) $value === $expected; switch ($condition['operator']) { case 'equals': return $equal; case 'not_equals': return !$equal; case 'contains': return is_array($value) ? $equal : is_scalar($value) && strpos((string) $value, $expected) !== false; case 'not_empty': return is_array($value) ? count($value) > 0 : is_scalar($value) && trim((string) $value) !== ''; } return false; } private function field($field, $raw) { $type = $field['type']; if ($type === 'checkbox') { if ($raw === null || $raw === '') { $raw = []; } if (!is_array($raw) || count($raw) > 200) { return $this->error(__('Select valid choices.', 'ssf-forms')); } $value = []; foreach ($raw as $choice) { if (!is_scalar($choice) || !in_array((string) $choice, $field['options'], true)) { return $this->error(__('Select valid choices.', 'ssf-forms')); } $value[] = (string) $choice; } $value = array_values(array_unique($value)); if (!empty($field['required']) && !$value) { return $this->error(__('Select at least one choice.', 'ssf-forms')); } return $value; } if ($type === 'consent') { $value = in_array($raw, [true, 1, '1', 'on', 'yes'], true); if (!empty($field['required']) && !$value) { return $this->error(__('Your confirmation is required.', 'ssf-forms')); } return $value; } if (!is_scalar($raw) && $raw !== null) { return $this->error(__('Enter a single value.', 'ssf-forms')); } $raw = trim((string) ($raw ?? '')); if (!empty($field['required']) && $raw === '') { return $this->error(__('This field is required.', 'ssf-forms')); } if ($raw === '') { return ''; } if ($this->length($raw) > 10000) { return $this->error(__('This answer is too long.', 'ssf-forms')); } if ($type === 'email') { if (!is_email($raw)) { return $this->error(__('Enter a valid email address.', 'ssf-forms')); } return sanitize_email($raw); } if ($type === 'url') { if ( !filter_var($raw, FILTER_VALIDATE_URL) || !in_array(strtolower((string) wp_parse_url($raw, PHP_URL_SCHEME)), ['http', 'https'], true) ) { return $this->error(__('Enter an http or https website address.', 'ssf-forms')); } return esc_url_raw($raw, ['http', 'https']); } if ($type === 'number') { if (!is_numeric($raw) || !is_finite((float) $raw)) { return $this->error(__('Enter a finite number.', 'ssf-forms')); } $number = 0 + $raw; if ( (isset($field['min']) && $number < $field['min']) || (isset($field['max']) && $number > $field['max']) ) { return $this->error(__('Enter a number within the allowed range.', 'ssf-forms')); } return $number; } if (in_array($type, ['select', 'radio'], true) && !in_array($raw, $field['options'], true)) { return $this->error(__('Select an available choice.', 'ssf-forms')); } if ($type === 'date') { $date = \DateTimeImmutable::createFromFormat('!Y-m-d', $raw); if (!$date || $date->format('Y-m-d') !== $raw) { return $this->error(__('Enter a valid date.', 'ssf-forms')); } } if ($type === 'time' && !preg_match('/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/D', $raw)) { return $this->error(__('Enter a valid time.', 'ssf-forms')); } $value = $type === 'textarea' ? sanitize_textarea_field($raw) : sanitize_text_field($raw); if (!empty($field['required']) && $value === '') { return $this->error(__('This field is required.', 'ssf-forms')); } if (in_array($type, ['text', 'textarea', 'tel', 'hidden'], true)) { if ( (isset($field['min']) && $this->length($value) < $field['min']) || (isset($field['max']) && $this->length($value) > $field['max']) ) { return $this->error(__('Enter text within the allowed length.', 'ssf-forms')); } } if ($type === 'tel' && $this->length($value) > 60) { return $this->error(__('Enter a shorter phone number.', 'ssf-forms')); } return $value; } public function public_form($form) { $public = $form; $allowed = ['submit_label', 'success_message', 'appearance']; $public['settings'] = array_intersect_key($form['settings'] ?? [], array_flip($allowed)); return $public; } public function export_form($form) { if (!isset($form['settings']) || !is_array($form['settings'])) { $form['settings'] = []; } $form['id'] = 0; $form['status'] = 'draft'; $form['settings']['notification_enabled'] = false; $form['settings']['notification_to'] = ''; $form['settings']['integrations'] = []; foreach ( [ 'notification_routes', 'routing', 'webhook', 'webhook_url', 'webhook_secret', 'secret', 'api_key', ] as $key ) { unset($form['settings'][$key]); } return apply_filters('ssffw_export_form', $form); } public static function clean_structure($value, $depth = 0) { if ($depth > 8) { return null; } if (is_array($value)) { $clean = []; foreach (array_slice($value, 0, 500, true) as $key => $item) { $clean_key = is_int($key) ? $key : sanitize_key($key); $clean[$clean_key] = self::clean_structure($item, $depth + 1); } return $clean; } if (is_bool($value) || is_int($value) || is_float($value) || $value === null) { return $value; } if (!is_scalar($value)) { return null; } $text = function_exists('mb_substr') ? mb_substr((string) $value, 0, 20000) : substr((string) $value, 0, 20000); return sanitize_textarea_field($text); } private function length($text) { return function_exists('mb_strlen') ? mb_strlen($text) : strlen($text); } private function trim_text($text, $length) { return is_scalar($text) ? (function_exists('mb_substr') ? mb_substr(sanitize_text_field((string) $text), 0, $length) : substr(sanitize_text_field((string) $text), 0, $length)) : ''; } private function error($message) { return new \WP_Error('ssffw_invalid', $message, ['status' => 400]); } }