Request cache. */ private static $cache = array(); /** @return array Every form setting with its default. */ public static function defaults() { $admin = get_option( 'admin_email' ); return apply_filters( 'fwren/form_defaults', array( 'confirmation' => array( 'type' => 'message', // message | page | url. 'message' => __( 'Thank you! Your submission has been received.', 'fieldwren' ), 'page_id' => 0, 'url' => '', 'new_tab' => false, 'hide_form' => true, 'reset_form' => true, ), 'conditional_confirmations' => array(), 'notifications' => array( array( 'id' => 'admin', 'enabled' => true, 'name' => __( 'Admin notification', 'fieldwren' ), 'to_type' => 'email', // email | field | routing. 'to' => '{wp.admin_email}', 'to_field' => '', 'routing' => array(), 'cc' => '', 'bcc' => '', 'reply_to' => '{inputs.email}', 'from_name' => '', 'from_email' => '', 'subject' => __( 'New submission: {form_title}', 'fieldwren' ), 'message' => '{all_data}', 'email_template' => '', // '' = Settings → Email default, 'classic', or an email template id. 'attach_uploads' => false, 'send_on' => 'submit', // submit | confirmed | approved | paid. 'conditions' => array( 'enabled' => false, 'match' => 'all', 'rules' => array() ), ), ), 'restrictions' => array( 'limit_entries' => array( 'enabled' => false, 'count' => 100, 'period' => 'total', 'message' => __( 'This form is no longer accepting submissions.', 'fieldwren' ) ), 'schedule' => array( 'enabled' => false, 'start' => '', 'end' => '', 'pending_message' => __( 'This form opens soon.', 'fieldwren' ), 'expired_message' => __( 'This form is closed.', 'fieldwren' ) ), 'login_required' => array( 'enabled' => false, 'message' => __( 'Please log in to submit this form.', 'fieldwren' ) ), 'per_user' => array( 'enabled' => false, 'count' => 1, 'by' => 'ip', 'message' => __( 'You have already submitted this form.', 'fieldwren' ) ), 'deny_empty' => array( 'enabled' => true, 'message' => __( 'The form is empty.', 'fieldwren' ) ), 'blocklist' => array( 'enabled' => false, 'keywords' => '', 'ips' => '', 'emails' => '', 'message' => __( 'Your submission was blocked.', 'fieldwren' ) ), ), 'spam' => array( 'honeypot' => true, 'time_trap' => 2, 'captcha' => 'global', // global | none | recaptcha_v2 | recaptcha_v3 | hcaptcha | turnstile. 'akismet' => false, 'rate_limit' => array( 'enabled' => true, 'count' => 10, 'minutes' => 10 ), ), 'layout' => array( 'custom_css' => '', 'custom_js' => '', 'ajax' => true, 'scroll_to_error' => true, ), 'steps' => array( 'progress' => 'bar', // bar | steps | none. 'show_titles' => true, 'persist' => false, 'scroll_top' => true, ), 'save_resume' => array( 'enabled' => false, 'button_text' => __( 'Save and continue later', 'fieldwren' ), 'email_link' => true, 'expiry_days' => 30, 'message' => __( 'Your progress has been saved. Use this link to continue: {resume_link}', 'fieldwren' ), 'auto_save' => false, ), 'double_optin' => array( 'enabled' => false, 'email_field' => 'email', 'subject' => __( 'Please confirm your submission', 'fieldwren' ), 'message' => __( "Hi,\n\nPlease confirm your submission to {form_title} by clicking this link:\n\n{confirmation_link}\n\nThank you.", 'fieldwren' ), 'pending_message' => __( 'Almost done! Please check your inbox and click the confirmation link.', 'fieldwren' ), 'confirmed_message' => __( 'Thanks! Your submission is confirmed.', 'fieldwren' ), 'delete_after_days' => 7, 'skip_for_logged_in' => true, ), 'admin_approval' => array( 'enabled' => false, 'notify_email' => $admin, 'pending_message' => __( 'Thanks! Your submission is awaiting review.', 'fieldwren' ), 'approved_email' => true, 'declined_email' => false, ), 'entries' => array( 'store' => true, 'store_ip' => true, 'retention_days' => 0, 'delete_uploads' => true, ), 'landing' => array( 'enabled' => false, 'title' => '', 'description' => '', 'logo' => '', 'background' => '#f3f4f6', 'color' => '#2563eb', 'layout' => 'center', // center | split | card. 'image' => '', ), 'conversational' => array( 'enabled' => false, 'start_title' => '', 'start_text' => '', 'start_button'=> __( 'Start', 'fieldwren' ), ), 'quiz' => array( 'enabled' => false, 'show_result' => true, 'grading' => array(), 'randomize' => false, 'result_message' => __( 'You scored {quiz.score} out of {quiz.total} ({quiz.percent}%).', 'fieldwren' ), ), 'payment' => array( 'currency' => 'USD', 'currency_position'=> 'left', 'thousand_sep' => ',', 'decimal_sep' => '.', 'decimals' => 2, 'receipt' => true, ), 'analytics' => array( 'enabled' => true ), 'privacy' => array( 'anonymize_ip' => false, 'export_erase' => true, ), ) ); } /** * Settings of a form merged over defaults. * * @param int $form_id Form id. * @return array */ public static function settings( $form_id ) { $form_id = (int) $form_id; if ( isset( self::$cache[ $form_id ] ) ) { return self::$cache[ $form_id ]; } $raw = get_post_meta( $form_id, '_fwren_settings', true ); $saved = is_string( $raw ) && '' !== $raw ? json_decode( $raw, true ) : ( is_array( $raw ) ? $raw : array() ); $s = self::merge( self::defaults(), is_array( $saved ) ? $saved : array() ); self::$cache[ $form_id ] = apply_filters( 'fwren/form_settings', $s, $form_id ); return self::$cache[ $form_id ]; } /** * Save settings (whole array). * * @param int $form_id Form id. * @param array $settings Settings. */ public static function save_settings( $form_id, array $settings ) { unset( self::$cache[ (int) $form_id ] ); update_post_meta( $form_id, '_fwren_settings', wp_slash( wp_json_encode( $settings ) ) ); } /** @return array Integration / post / user feeds of a form. */ public static function feeds( $form_id ) { $raw = get_post_meta( $form_id, '_fwren_feeds', true ); $feeds = is_string( $raw ) && '' !== $raw ? json_decode( $raw, true ) : array(); return is_array( $feeds ) ? $feeds : array(); } /** Save feeds. */ public static function save_feeds( $form_id, array $feeds ) { update_post_meta( $form_id, '_fwren_feeds', wp_slash( wp_json_encode( array_values( $feeds ) ) ) ); } /** * Recursive merge where saved scalars and lists replace defaults, and saved associative * arrays are merged key by key. */ public static function merge( array $defaults, array $saved ) { foreach ( $saved as $k => $v ) { if ( is_array( $v ) && isset( $defaults[ $k ] ) && is_array( $defaults[ $k ] ) && self::is_assoc( $defaults[ $k ] ) && self::is_assoc( $v ) ) { $defaults[ $k ] = self::merge( $defaults[ $k ], $v ); } else { $defaults[ $k ] = $v; } } return $defaults; } /** @return bool */ private static function is_assoc( array $a ) { return array() === $a ? true : array_keys( $a ) !== range( 0, count( $a ) - 1 ); } /** @return bool Whether the post is a form. */ public static function exists( $form_id ) { return $form_id && Plugin::POST_TYPE === get_post_type( $form_id ); } /** Duplicate a form (blocks, settings, feeds). */ public static function duplicate( $form_id ) { $post = get_post( $form_id ); if ( ! $post || Plugin::POST_TYPE !== $post->post_type ) { return 0; } $new = wp_insert_post( array( 'post_type' => Plugin::POST_TYPE, 'post_status' => 'publish', /* translators: %s form title */ 'post_title' => sprintf( __( '%s (copy)', 'fieldwren' ), $post->post_title ), 'post_content' => wp_slash( $post->post_content ), ) ); if ( $new && ! is_wp_error( $new ) ) { update_post_meta( $new, '_fwren_settings', wp_slash( (string) get_post_meta( $form_id, '_fwren_settings', true ) ) ); update_post_meta( $new, '_fwren_feeds', wp_slash( (string) get_post_meta( $form_id, '_fwren_feeds', true ) ) ); Schema::compile( $new, $post->post_content ); return (int) $new; } return 0; } /** @return array Export package of a form. */ public static function export( $form_id ) { $post = get_post( $form_id ); return array( 'plugin' => 'fieldwren', 'version' => FWREN_VERSION, 'title' => $post ? $post->post_title : '', 'content' => $post ? $post->post_content : '', 'settings' => self::settings( $form_id ), 'feeds' => self::feeds( $form_id ), ); } /** Import one exported form. @return int New form id. */ public static function import( array $pkg ) { if ( empty( $pkg['content'] ) && empty( $pkg['title'] ) ) { return 0; } $id = wp_insert_post( array( 'post_type' => Plugin::POST_TYPE, 'post_status' => 'publish', 'post_title' => sanitize_text_field( $pkg['title'] ?? __( 'Imported form', 'fieldwren' ) ), 'post_content' => wp_slash( (string) ( $pkg['content'] ?? '' ) ), ) ); if ( ! $id || is_wp_error( $id ) ) { return 0; } if ( ! empty( $pkg['settings'] ) && is_array( $pkg['settings'] ) ) { self::save_settings( $id, $pkg['settings'] ); } if ( ! empty( $pkg['feeds'] ) && is_array( $pkg['feeds'] ) ) { self::save_feeds( $id, $pkg['feeds'] ); } Schema::compile( $id, (string) ( $pkg['content'] ?? '' ) ); return (int) $id; } }