__( 'Submit', 'fillaro' ), 'submitAlign' => 'full', 'storeEntries' => true, 'confirmation' => array( 'type' => 'message', 'message' => __( 'Thanks! We got your message and will reply soon.', 'fillaro' ), 'redirectUrl' => '', 'pageId' => 0, ), 'notifications' => array( 'admin' => array( 'enabled' => true, 'to' => '{admin_email}', 'subject' => __( 'New entry: {form_title}', 'fillaro' ), 'body' => '{all_fields}', 'replyTo' => '', ), 'user' => array( 'enabled' => false, 'emailField' => '', 'subject' => __( 'We received your message', 'fillaro' ), 'body' => __( "Hi,\n\nThanks for contacting us. Here is a copy of what you sent:\n\n{all_fields}", 'fillaro' ), ), ), 'spam' => array( 'honeypot' => true, 'minTime' => 2, ), 'webhook' => array( 'enabled' => false, 'url' => '', ), ); /** * Filters default form settings. Add-ons add their own keys here. * * @param array $defaults Settings. */ return apply_filters( 'fillaro/default_form_settings', $defaults ); } /** * Sanitize a list of fields. * * @param mixed $raw Raw fields. * @return array */ public static function sanitize_fields( $raw ) { $registry = Plugin::instance()->fields; $clean = array(); $keys = array(); if ( ! is_array( $raw ) ) { return $clean; } foreach ( array_slice( array_values( $raw ), 0, self::MAX_FIELDS ) as $field ) { if ( ! is_array( $field ) || empty( $field['type'] ) || ! is_string( $field['type'] ) ) { continue; } $type = $registry->get( $field['type'] ); if ( ! $type ) { continue; } $key = isset( $field['key'] ) && is_string( $field['key'] ) ? self::sanitize_key( $field['key'] ) : ''; if ( '' === $key ) { $key = $type->type(); } $base = $key; $n = 2; while ( isset( $keys[ $key ] ) ) { $key = $base . '_' . $n; ++$n; } $keys[ $key ] = true; $classes = isset( $field['cssClass'] ) && is_string( $field['cssClass'] ) ? implode( ' ', array_filter( array_map( 'sanitize_html_class', preg_split( '/\s+/', $field['cssClass'] ) ) ) ) : ''; $common = array( 'key' => $key, 'type' => $type->type(), 'label' => isset( $field['label'] ) ? sanitize_text_field( (string) $field['label'] ) : '', 'placeholder' => isset( $field['placeholder'] ) ? sanitize_text_field( (string) $field['placeholder'] ) : '', 'help' => isset( $field['help'] ) ? sanitize_text_field( (string) $field['help'] ) : '', 'required' => ! empty( $field['required'] ) && $type->has_value(), 'hideLabel' => ! empty( $field['hideLabel'] ), 'width' => ( isset( $field['width'] ) && in_array( $field['width'], array( 'full', 'half', 'third' ), true ) ) ? $field['width'] : 'full', 'cssClass' => $classes, 'defaultValue' => isset( $field['defaultValue'] ) && is_scalar( $field['defaultValue'] ) ? sanitize_text_field( (string) $field['defaultValue'] ) : '', ); /** * Filters one sanitized field. Add-ons clean their own keys here, * for example conditional logic rules. * * @param array $clean Clean field. * @param array $field Raw field. * @param FieldType $type Field type. */ $clean[] = apply_filters( 'fillaro/sanitize_field', array_merge( $common, $type->sanitize_config( $field ) ), $field, $type ); } return $clean; } /** * Field key: lowercase letters, numbers and underscores, max 64 chars. * * @param string $key Raw key. * @return string */ public static function sanitize_key( $key ) { $key = strtolower( preg_replace( '/[^A-Za-z0-9_]+/', '_', $key ) ); $key = trim( preg_replace( '/_+/', '_', $key ), '_' ); if ( '' !== $key && ctype_digit( $key[0] ) ) { $key = 'f_' . $key; } return substr( $key, 0, 64 ); } /** * Sanitize form settings. * * @param mixed $raw Raw settings. * @return array */ public static function sanitize_settings( $raw ) { $s = Arr::merge_deep( $raw, self::default_settings() ); $text = static function ( $v, $max = 500 ) { return is_scalar( $v ) ? substr( sanitize_text_field( (string) $v ), 0, $max ) : ''; }; $email = static function ( $v ) { return is_scalar( $v ) ? sanitize_textarea_field( (string) $v ) : ''; }; $clean = array( 'submitText' => '' !== $text( $s['submitText'], 80 ) ? $text( $s['submitText'], 80 ) : __( 'Submit', 'fillaro' ), 'submitAlign' => in_array( $s['submitAlign'], array( 'full', 'left', 'center', 'right' ), true ) ? $s['submitAlign'] : 'full', 'storeEntries' => (bool) $s['storeEntries'], 'confirmation' => array( 'type' => in_array( $s['confirmation']['type'], array( 'message', 'redirect', 'page' ), true ) ? $s['confirmation']['type'] : 'message', 'message' => is_string( $s['confirmation']['message'] ) ? wp_kses_post( $s['confirmation']['message'] ) : '', 'redirectUrl' => is_string( $s['confirmation']['redirectUrl'] ) ? esc_url_raw( $s['confirmation']['redirectUrl'], array( 'http', 'https' ) ) : '', 'pageId' => absint( $s['confirmation']['pageId'] ), ), 'notifications' => array( 'admin' => array( 'enabled' => (bool) $s['notifications']['admin']['enabled'], 'to' => $text( $s['notifications']['admin']['to'] ), 'subject' => $text( $s['notifications']['admin']['subject'], 200 ), 'body' => $email( $s['notifications']['admin']['body'] ), 'replyTo' => is_string( $s['notifications']['admin']['replyTo'] ) ? self::sanitize_key( $s['notifications']['admin']['replyTo'] ) : '', ), 'user' => array( 'enabled' => (bool) $s['notifications']['user']['enabled'], 'emailField' => is_string( $s['notifications']['user']['emailField'] ) ? self::sanitize_key( $s['notifications']['user']['emailField'] ) : '', 'subject' => $text( $s['notifications']['user']['subject'], 200 ), 'body' => $email( $s['notifications']['user']['body'] ), ), ), 'spam' => array( 'honeypot' => (bool) $s['spam']['honeypot'], 'minTime' => max( 0, min( 30, (int) $s['spam']['minTime'] ) ), ), 'webhook' => array( 'enabled' => (bool) $s['webhook']['enabled'], 'url' => is_string( $s['webhook']['url'] ) ? esc_url_raw( $s['webhook']['url'], array( 'https', 'http' ) ) : '', ), ); /** * Filters sanitized form settings. Add-ons sanitize their own keys here. * * @param array $clean Clean settings. * @param array $raw Raw settings merged with defaults. */ return apply_filters( 'fillaro/sanitize_form_settings', $clean, $s ); } }