array( 'name' => __( 'Forms', 'fillnova' ), 'singular_name' => __( 'Form', 'fillnova' ), ), 'public' => false, 'show_ui' => false, 'show_in_rest' => false, 'rewrite' => false, 'query_var' => false, 'supports' => array( 'title' ), 'capability_type' => 'post', ) ); } /** * Default settings for a form. * * @return array */ public static function default_settings() { return array( 'mode' => 'standard', // standard | multistep | conversational. 'submit_text' => __( 'Submit', 'fillnova' ), 'next_text' => __( 'Next', 'fillnova' ), 'prev_text' => __( 'Back', 'fillnova' ), 'success_type' => 'message', // message | redirect. 'success_message' => __( 'Thank you! Your response has been received.', 'fillnova' ), 'redirect_url' => '', 'show_progress' => true, 'store_entries' => true, 'honeypot' => true, 'captcha' => 'none', 'entry_limit' => 0, 'limit_message' => __( 'This form is no longer accepting responses.', 'fillnova' ), 'require_login' => false, 'admin_notify' => true, 'admin_to' => '', 'admin_subject' => __( 'New entry: {form_title}', 'fillnova' ), 'admin_message' => '{all_fields}', 'reply_to_field' => '', 'user_notify' => false, 'user_email_field'=> '', 'user_subject' => __( 'We received your submission', 'fillnova' ), 'user_message' => __( "Hi,\n\nThanks for reaching out. Here is a copy of your submission:\n\n{all_fields}", 'fillnova' ), 'theme' => 'modern', // modern | minimal | bold | glass. 'primary_color' => '#087A68', 'radius' => 10, 'label_position' => 'top', 'form_bg' => 'none', // none | white | soft | dark. 'field_size' => 'md', // sm | md | lg. 'button_width' => 'auto', // auto | 25 | 50 | 75 | full. 'button_align' => 'left', // left | center | right. 'button_size' => 'auto', // auto (match fields) | sm | md | lg. 'description' => '', 'welcome_title' => '', 'welcome_text' => '', 'welcome_button' => __( 'Start', 'fillnova' ), ); } /** * Empty schema. * * @return array */ public static function empty_schema() { return array( 'fields' => array(), 'settings' => self::default_settings(), ); } /** * Get a form. * * @param int $form_id Form ID. * @return array|null { id, title, status, fields, settings } */ public static function get( $form_id ) { $post = get_post( absint( $form_id ) ); if ( ! $post || self::POST_TYPE !== $post->post_type ) { return null; } $raw = get_post_meta( $post->ID, self::META_KEY, true ); $schema = is_string( $raw ) ? json_decode( $raw, true ) : $raw; if ( ! is_array( $schema ) ) { $schema = self::empty_schema(); } $schema['settings'] = wp_parse_args( isset( $schema['settings'] ) ? (array) $schema['settings'] : array(), self::default_settings() ); $schema['fields'] = isset( $schema['fields'] ) && is_array( $schema['fields'] ) ? $schema['fields'] : array(); $form = array( 'id' => $post->ID, 'title' => $post->post_title, 'status' => $post->post_status, 'fields' => $schema['fields'], 'settings' => $schema['settings'], 'created' => $post->post_date, 'modified' => $post->post_modified, ); /** * Filter a loaded form (fields + settings) before it is rendered or processed. * * @param array $form Form. * @param int $form_id Form ID. */ return apply_filters( 'fillnova_form', $form, $post->ID ); } /** * Create or update a form. * * @param array $data { id?, title, fields, settings, status? }. * @return int|WP_Error */ public static function save( $data ) { $id = isset( $data['id'] ) ? absint( $data['id'] ) : 0; $title = isset( $data['title'] ) ? sanitize_text_field( $data['title'] ) : ''; $status = isset( $data['status'] ) && in_array( $data['status'], array( 'publish', 'draft' ), true ) ? $data['status'] : 'publish'; $schema = self::sanitize_schema( array( 'fields' => isset( $data['fields'] ) ? $data['fields'] : array(), 'settings' => isset( $data['settings'] ) ? $data['settings'] : array(), ) ); $postarr = array( 'post_type' => self::POST_TYPE, 'post_title' => '' !== $title ? $title : __( 'Untitled form', 'fillnova' ), 'post_status' => $status, ); if ( $id ) { $existing = get_post( $id ); if ( ! $existing || self::POST_TYPE !== $existing->post_type ) { return new WP_Error( 'fillnova_not_found', __( 'Form not found.', 'fillnova' ) ); } $postarr['ID'] = $id; $result = wp_update_post( $postarr, true ); } else { // Free plan: cap the number of forms. Pro is unlimited. $limit = fillnova_free_form_limit(); if ( ! fillnova_is_pro() && $limit > 0 ) { $counts = (array) wp_count_posts( self::POST_TYPE ); $total = (int) ( $counts['publish'] ?? 0 ) + (int) ( $counts['draft'] ?? 0 ); if ( $total >= $limit ) { return new WP_Error( 'fillnova_form_limit', sprintf( /* translators: %d: number of forms allowed on the free plan. */ __( 'The free plan includes up to %d forms. Upgrade to Fillnova Pro for unlimited forms.', 'fillnova' ), $limit ), array( 'status' => 402, 'upgrade' => fillnova_upgrade_url() ) ); } } $result = wp_insert_post( $postarr, true ); } if ( is_wp_error( $result ) ) { return $result; } update_post_meta( $result, self::META_KEY, wp_slash( wp_json_encode( $schema ) ) ); do_action( 'fillnova_form_saved', $result, $schema ); return (int) $result; } /** * Duplicate a form. * * @param int $form_id Form ID. * @return int|WP_Error */ public static function duplicate( $form_id ) { $form = self::get( $form_id ); if ( ! $form ) { return new WP_Error( 'fillnova_not_found', __( 'Form not found.', 'fillnova' ) ); } /* translators: %s: form title */ $form['title'] = sprintf( __( '%s (copy)', 'fillnova' ), $form['title'] ); unset( $form['id'] ); return self::save( $form ); } /** * Delete a form and its entries. * * @param int $form_id Form ID. */ public static function delete( $form_id ) { $post = get_post( absint( $form_id ) ); if ( $post && self::POST_TYPE === $post->post_type ) { Fillnova_Entries::delete_by_form( $post->ID ); wp_delete_post( $post->ID, true ); } } /** * List forms. * * @param array $args WP_Query args overrides. * @return WP_Post[] */ public static function all( $args = array() ) { return get_posts( wp_parse_args( $args, array( 'post_type' => self::POST_TYPE, 'post_status' => array( 'publish', 'draft' ), 'posts_per_page' => -1, 'orderby' => 'modified', 'order' => 'DESC', ) ) ); } /** * Sanitize a whole schema. * * @param array $schema Schema. * @return array */ public static function sanitize_schema( $schema ) { $out = array( 'fields' => array(), 'settings' => self::sanitize_settings( isset( $schema['settings'] ) ? (array) $schema['settings'] : array() ), ); $fields = isset( $schema['fields'] ) && is_array( $schema['fields'] ) ? $schema['fields'] : array(); $seen = array(); $remap = array(); foreach ( $fields as $field ) { $field = (array) $field; $clean = self::sanitize_field( $field ); if ( ! $clean ) { continue; } if ( isset( $seen[ $clean['id'] ] ) ) { $clean['id'] = fillnova_uid(); } $raw = isset( $field['id'] ) && is_scalar( $field['id'] ) ? sanitize_key( (string) $field['id'] ) : ''; if ( '' !== $raw && $raw !== $clean['id'] && ! isset( $remap[ $raw ] ) ) { $remap[ $raw ] = $clean['id']; } $seen[ $clean['id'] ] = true; $out['fields'][] = $clean; } // IDs that had to change (AI output, imports) must not break logic, formulas or merge tags. if ( $remap ) { $out = self::remap_ids( $out, $remap ); } return $out; } /** * Turn any field ID into the f_xxx format, keeping it readable. Empty string when unusable. * * @param mixed $id Raw ID. * @return string */ public static function normalize_id( $id ) { if ( ! is_scalar( $id ) ) { return ''; } $id = sanitize_key( (string) $id ); if ( preg_match( '/^f_[a-z0-9_]{2,40}$/', $id ) ) { return $id; } $slug = trim( preg_replace( '/[^a-z0-9_]/', '', str_replace( '-', '_', preg_replace( '/^f_/', '', $id ) ) ), '_' ); $slug = substr( $slug, 0, 40 ); if ( strlen( $slug ) < 2 ) { return ''; } return 'f_' . $slug; } /** * Rewrite references to renamed field IDs everywhere in a schema. * * @param array $schema Sanitized schema. * @param array $remap old => new. * @return array */ protected static function remap_ids( $schema, $remap ) { $search = array(); $replace = array(); $fsearch = array(); $freplace = array(); foreach ( $remap as $old => $new ) { $search[] = '{field:' . $old . '}'; $replace[] = '{field:' . $new . '}'; $fsearch[] = '{' . $old . '}'; $freplace[] = '{' . $new . '}'; } $walk = function ( $value, $key = '' ) use ( &$walk, $remap, $search, $replace, $fsearch, $freplace ) { if ( is_array( $value ) ) { foreach ( $value as $k => $v ) { $value[ $k ] = $walk( $v, $k ); } return $value; } if ( ! is_string( $value ) ) { return $value; } if ( is_string( $key ) && ( 'field' === $key || '_field' === substr( $key, -6 ) ) && isset( $remap[ $value ] ) ) { return $remap[ $value ]; } $value = str_replace( $search, $replace, $value ); // Bare {id} references only exist in calculation formulas. if ( 'formula' === $key ) { $value = str_replace( $fsearch, $freplace, $value ); } return $value; }; return $walk( $schema ); } /** * Sanitize form settings. * * @param array $settings Settings. * @return array */ public static function sanitize_settings( $settings ) { $defaults = self::default_settings(); $settings = wp_parse_args( $settings, $defaults ); $out = array(); $bools = array( 'show_progress', 'store_entries', 'honeypot', 'require_login', 'admin_notify', 'user_notify' ); $texts = array( 'submit_text', 'next_text', 'prev_text', 'admin_subject', 'user_subject', 'limit_message', 'reply_to_field', 'user_email_field', 'welcome_title', 'welcome_button' ); $areas = array( 'success_message', 'admin_message', 'user_message', 'description', 'welcome_text' ); foreach ( $bools as $k ) { $out[ $k ] = filter_var( $settings[ $k ], FILTER_VALIDATE_BOOLEAN ); } foreach ( $texts as $k ) { $out[ $k ] = sanitize_text_field( (string) $settings[ $k ] ); } foreach ( $areas as $k ) { $out[ $k ] = wp_kses_post( (string) $settings[ $k ] ); } $out['mode'] = in_array( $settings['mode'], array( 'standard', 'multistep', 'conversational' ), true ) ? $settings['mode'] : 'standard'; $out['success_type'] = in_array( $settings['success_type'], array( 'message', 'redirect' ), true ) ? $settings['success_type'] : 'message'; $out['redirect_url'] = esc_url_raw( (string) $settings['redirect_url'] ); $out['captcha'] = in_array( $settings['captcha'], array( 'none', 'recaptcha', 'hcaptcha', 'turnstile' ), true ) ? $settings['captcha'] : 'none'; $out['entry_limit'] = absint( $settings['entry_limit'] ); $out['admin_to'] = implode( ',', array_filter( array_map( 'sanitize_email', explode( ',', (string) $settings['admin_to'] ) ) ) ); $out['theme'] = in_array( $settings['theme'], array( 'modern', 'minimal', 'bold', 'glass' ), true ) ? $settings['theme'] : 'modern'; $out['primary_color'] = sanitize_hex_color( $settings['primary_color'] ) ? sanitize_hex_color( $settings['primary_color'] ) : $defaults['primary_color']; $out['radius'] = min( 40, absint( $settings['radius'] ) ); $out['label_position'] = in_array( $settings['label_position'], array( 'top', 'left', 'floating' ), true ) ? $settings['label_position'] : 'top'; $out['form_bg'] = in_array( $settings['form_bg'], array( 'none', 'white', 'soft', 'dark' ), true ) ? $settings['form_bg'] : 'none'; $out['field_size'] = in_array( $settings['field_size'], array( 'sm', 'md', 'lg' ), true ) ? $settings['field_size'] : 'md'; $out['button_width'] = in_array( (string) $settings['button_width'], array( 'auto', '25', '50', '75', 'full' ), true ) ? (string) $settings['button_width'] : 'auto'; $out['button_align'] = in_array( $settings['button_align'], array( 'left', 'center', 'right' ), true ) ? $settings['button_align'] : 'left'; $out['button_size'] = in_array( $settings['button_size'], array( 'auto', 'sm', 'md', 'lg' ), true ) ? $settings['button_size'] : 'auto'; /** * Allow Pro / add-ons to sanitize extra settings (integrations, payments...). */ return apply_filters( 'fillnova_sanitize_settings', $out, $settings ); } /** * Sanitize a single field. * * @param array $field Field. * @return array|null */ public static function sanitize_field( $field ) { $type = isset( $field['type'] ) ? sanitize_key( $field['type'] ) : ''; if ( ! Fillnova_Fields::known( $type ) ) { return null; } if ( Fillnova_Fields::locked( $type ) ) { // Pro field while Pro is inactive: keep it untouched (structure only) so nothing is lost. return array_merge( (array) json_decode( wp_json_encode( $field ), true ), array( 'type' => $type, 'id' => isset( $field['id'] ) ? sanitize_key( $field['id'] ) : fillnova_uid() ) ); } $id = isset( $field['id'] ) ? self::normalize_id( $field['id'] ) : ''; if ( '' === $id ) { $id = fillnova_uid(); } $out = array( 'id' => $id, 'type' => $type, ); $keys = self::field_keys(); foreach ( $keys as $key => $kind ) { if ( ! array_key_exists( $key, $field ) ) { continue; } $v = $field[ $key ]; switch ( $kind ) { case 'text': $out[ $key ] = sanitize_text_field( (string) $v ); break; case 'textarea': $out[ $key ] = sanitize_textarea_field( (string) $v ); break; case 'html': $out[ $key ] = wp_kses_post( (string) $v ); break; case 'bool': $out[ $key ] = filter_var( $v, FILTER_VALIDATE_BOOLEAN ); break; case 'int': $out[ $key ] = intval( $v ); break; case 'float': $out[ $key ] = '' === $v || null === $v ? '' : (float) $v; break; case 'url': $out[ $key ] = esc_url_raw( (string) $v ); break; case 'key': $out[ $key ] = sanitize_key( (string) $v ); break; case 'formula': $out[ $key ] = preg_replace( '/[^0-9a-z_{}\s\.\+\-\*\/\(\)%,]/i', '', (string) $v ); break; case 'options': $out[ $key ] = self::sanitize_options( $v ); break; case 'conditions': $out[ $key ] = Fillnova_Logic::sanitize( $v ); break; case 'fields': $sub = array(); foreach ( (array) $v as $child ) { $c = self::sanitize_field( (array) $child ); if ( $c && 'repeater' !== $c['type'] ) { $sub[] = $c; } } $out[ $key ] = $sub; break; } } if ( isset( $out['width'] ) && ! in_array( $out['width'], array( 'full', 'half', 'third' ), true ) ) { $out['width'] = 'full'; } return apply_filters( 'fillnova_sanitize_field', $out, $field ); } /** * Known field keys and their sanitizers. * * @return array */ public static function field_keys() { return apply_filters( 'fillnova_field_keys', array( 'label' => 'text', 'placeholder' => 'text', 'help' => 'text', 'default' => 'text', 'required' => 'bool', 'hide_label' => 'bool', 'width' => 'key', 'css_class' => 'text', 'content' => 'html', 'options' => 'options', 'multiple' => 'bool', 'layout' => 'key', 'min' => 'float', 'max' => 'float', 'step' => 'float', 'minlength' => 'int', 'maxlength' => 'int', 'rows' => 'int', 'mask' => 'text', 'accept' => 'text', 'max_size' => 'float', 'max_files' => 'int', 'stars' => 'int', 'consent_text' => 'html', 'level' => 'key', 'subfields' => 'text', 'conditions' => 'conditions', 'prefix' => 'text', 'suffix' => 'text', 'unique' => 'bool', 'error_message'=> 'text', 'date_format' => 'text', ) ); } /** * Sanitize choice options. * * @param mixed $options Options. * @return array */ public static function sanitize_options( $options ) { $out = array(); foreach ( (array) $options as $opt ) { if ( is_string( $opt ) ) { $opt = array( 'label' => $opt ); } $opt = (array) $opt; $label = isset( $opt['label'] ) ? sanitize_text_field( (string) $opt['label'] ) : ''; if ( '' === $label ) { continue; } $item = array( 'label' => $label, 'value' => isset( $opt['value'] ) && '' !== $opt['value'] ? sanitize_text_field( (string) $opt['value'] ) : $label, ); if ( isset( $opt['calc'] ) && '' !== $opt['calc'] ) { $item['calc'] = (float) $opt['calc']; } if ( ! empty( $opt['correct'] ) ) { $item['correct'] = true; } if ( ! empty( $opt['image'] ) ) { $item['image'] = esc_url_raw( (string) $opt['image'] ); } $out[] = $item; } return $out; } }