' . (int) $unread . '' : ''; add_menu_page( __( 'Fieldwren', 'fieldwren' ), __( 'Forms', 'fieldwren' ) . $bubble, 'fwren_view_entries', self::MENU_SLUG, array( __CLASS__, 'page_dashboard' ), Icons::menu_icon(), 26 ); add_submenu_page( self::MENU_SLUG, __( 'Dashboard', 'fieldwren' ), __( 'Dashboard', 'fieldwren' ), 'fwren_view_entries', self::MENU_SLUG, array( __CLASS__, 'page_dashboard' ) ); add_submenu_page( self::MENU_SLUG, __( 'Add New Form', 'fieldwren' ), __( 'Add New', 'fieldwren' ), 'fwren_manage_forms', 'post-new.php?post_type=' . Plugin::POST_TYPE ); add_submenu_page( self::MENU_SLUG, __( 'Form Templates', 'fieldwren' ), __( 'Templates', 'fieldwren' ), 'fwren_manage_forms', 'fwren-templates', array( __CLASS__, 'page_templates' ) ); add_submenu_page( self::MENU_SLUG, __( 'Entries', 'fieldwren' ), __( 'Entries', 'fieldwren' ) . $bubble, 'fwren_view_entries', 'fwren-entries', array( Admin_Entries::class, 'page' ) ); add_submenu_page( self::MENU_SLUG, __( 'Reports', 'fieldwren' ), __( 'Reports', 'fieldwren' ), 'fwren_view_entries', 'fwren-reports', array( __CLASS__, 'page_reports' ) ); add_submenu_page( self::MENU_SLUG, __( 'Settings', 'fieldwren' ), __( 'Settings', 'fieldwren' ), 'fwren_manage_settings', 'fwren-settings', array( __CLASS__, 'page_global' ) ); add_submenu_page( self::MENU_SLUG, __( 'Logs', 'fieldwren' ), __( 'Logs', 'fieldwren' ), 'fwren_manage_settings', 'fwren-logs', array( __CLASS__, 'page_logs' ) ); add_submenu_page( self::MENU_SLUG, __( 'Form Settings', 'fieldwren' ), __( 'Form Settings', 'fieldwren' ), 'fwren_manage_forms', 'fwren-form-settings', array( __CLASS__, 'page_form_settings' ) ); } /** * The per-form settings page stays registered (remove_submenu_page() would make it 403) but * its menu entry is hidden. */ public static function hide_pages() { echo ''; } /** Keep the Forms menu open on the CPT screens. */ public static function parent_file( $file ) { global $typenow, $plugin_page; if ( Plugin::POST_TYPE === $typenow ) { return self::MENU_SLUG; } if ( 'fwren-form-settings' === $plugin_page ) { $GLOBALS['submenu_file'] = 'edit.php?post_type=' . Plugin::POST_TYPE; // phpcs:ignore return self::MENU_SLUG; } return $file; } /** Plugin list links. */ public static function plugin_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'fieldwren' ) . '' ); return $links; } /** @return string URL of a form's settings screen. */ public static function settings_url( $form_id, $tab = 'general' ) { return admin_url( 'admin.php?page=fwren-form-settings&form_id=' . (int) $form_id . '&tab=' . rawurlencode( $tab ) ); } /** Admin assets. */ public static function assets( $hook ) { $screen = get_current_screen(); $ours = false !== strpos( (string) $hook, 'fwren' ) || ( $screen && Plugin::POST_TYPE === $screen->post_type ); if ( ! $ours ) { return; } // Versioned by file time so an edited stylesheet or script is never served from the browser cache. wp_enqueue_style( 'fwren-admin', FWREN_URL . 'assets/admin/admin.css', array(), FWREN_VERSION . '.' . (int) @filemtime( FWREN_PATH . 'assets/admin/admin.css' ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors wp_enqueue_script( 'fwren-admin', FWREN_URL . 'assets/admin/admin.js', array(), FWREN_VERSION . '.' . (int) @filemtime( FWREN_PATH . 'assets/admin/admin.js' ), true ); // phpcs:ignore WordPress.PHP.NoSilencedErrors $form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification $fields = array(); if ( $form_id ) { foreach ( Schema::fields( $form_id ) as $k => $f ) { $fields[ $k ] = Schema::label( $f ); } } wp_localize_script( 'fwren-admin', 'FWRENAdmin', array( 'formId' => $form_id, 'fields' => (object) $fields, 'operators' => Conditions::operators(), 'smartcodes' => Smartcodes::catalog( $form_id ), 'ajax' => admin_url( 'admin-ajax.php' ), 'rest' => esc_url_raw( rest_url( 'fwren/v1/' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'i18n' => array( 'copied' => __( 'Copied', 'fieldwren' ), 'confirm' => __( 'Are you sure?', 'fieldwren' ), 'remove' => __( 'Remove', 'fieldwren' ), 'insert' => __( 'Insert smartcode', 'fieldwren' ), ), ) ); if ( $screen && 'fwren-form-settings' === ( $_GET['page'] ?? '' ) ) { // phpcs:ignore wp_enqueue_media(); } } /** Block editor assets for the form editor. */ public static function editor_assets() { $screen = get_current_screen(); if ( ! $screen || Plugin::POST_TYPE !== $screen->post_type ) { return; } $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; // phpcs:ignore if ( ! $post_id && isset( $GLOBALS['post'] ) && $GLOBALS['post'] instanceof \WP_Post ) { $post_id = (int) $GLOBALS['post']->ID; // New form: the auto-draft already has an id. } wp_enqueue_style( 'fwren-editor', FWREN_URL . 'assets/editor/editor.css', array(), FWREN_VERSION ); // Inspector skin for the form editor; loads after the Block Controls framework stylesheet. wp_enqueue_style( 'fwren-inspector-skin', FWREN_URL . 'assets/editor/inspector-skin.css', array( 'fwren-bc-editor', 'wp-components' ), FWREN_VERSION ); wp_enqueue_script( 'fwren-editor', FWREN_URL . 'assets/editor/controls.js', array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-editor', 'wp-block-editor', 'wp-api-fetch' ), FWREN_VERSION, true ); wp_localize_script( 'fwren-editor', 'FWRENEditor', array( 'postId' => $post_id, 'icons' => Icons::for_js( array( 'list-view', 'update', 'external', 'visibility', 'desktop', 'tablet', 'mobile' ), 20 ), 'operators' => Conditions::operators(), 'smartcodes' => Smartcodes::catalog( 0 ), 'settingsUrl' => $post_id ? self::settings_url( $post_id ) : '', 'entriesUrl' => $post_id ? admin_url( 'admin.php?page=fwren-entries&form_id=' . $post_id ) : '', 'shortcode' => $post_id ? '[fieldwren_form id="' . $post_id . '"]' : '', 'preview' => $post_id ? array( 'endpoint' => Preview::endpoint( $post_id ), 'nonce' => wp_create_nonce( 'fwren_preview_' . $post_id ), 'url' => Preview::url( $post_id ), ) : null, 'inputTypes' => self::input_block_names(), 'patterns' => Templates::for_editor(), 'tplCats' => Templates::categories(), ) ); } /** @return array Block names of input fields with their key prefix. */ private static function input_block_names() { $out = array(); foreach ( Fields::all() as $f ) { if ( $f->is_input() ) { $out[ $f->get_block_name() ] = $f->key_prefix(); } } return $out; } /* ------------------------------------------------------------------ * * Helpers (echo a form-table row) * ------------------------------------------------------------------ */ /** @return string id attribute from a field name. */ private static function id( $name ) { return 'fwren-' . trim( preg_replace( '/[^a-z0-9]+/i', '-', $name ), '-' ); } /** @var string Attributes for the next row (see row_attr()). */ private static $row_attrs = ''; /** * Attributes for the of the next row helper call. Used for rows that depend on a select: * the select's row gets data-fwren-switch, the rows it controls data-fwren-case="value [value]". * * @param array $attrs name => value. */ public static function row_attr( array $attrs ) { $out = ''; foreach ( $attrs as $k => $v ) { $out .= ' ' . sanitize_key( $k ) . '="' . esc_attr( $v ) . '"'; } self::$row_attrs = $out; } /** Row wrapper. */ private static function row( $label, $control, $desc = '', $for = '', $kind = 'field' ) { $attrs = self::$row_attrs; self::$row_attrs = ''; echo '' . ( $for ? '' : esc_html( $label ) ) . '' . $control . ( $desc ? '

' . wp_kses_post( $desc ) . '

' : '' ) . ''; // phpcs:ignore } /** Open a group of settings: a heading and description above its panel(s). */ public static function section( $title, $desc = '' ) { echo '

' . esc_html( $title ) . '

' . ( $desc ? '

' . esc_html( $desc ) . '

' : '' ) . '
'; } /** Close a group of settings. */ public static function section_end() { echo '
'; } /** * Open a settings panel. * * @param bool $dependent The first row is a switch; the rows after it only show while it is on. */ public static function panel( $dependent = false ) { echo ''; } /** Close a settings panel. */ public static function panel_end() { echo ''; } /** A small heading between the rows of a panel. */ public static function subhead( $title ) { echo '' . esc_html( $title ) . ''; } /** Text / number / url / email / password / date input. */ public static function text( $name, $value, $label, $desc = '', $type = 'text' ) { $id = self::id( $name ); $cl = in_array( $type, array( 'number' ), true ) ? 'small-text' : 'regular-text'; self::row( $label, '', $desc, $id, 'text' ); } /** Textarea. */ public static function textarea( $name, $value, $label, $desc = '', $rows = 4 ) { $id = self::id( $name ); self::row( $label, '', $desc, $id, 'textarea' ); } /** Select. */ public static function select( $name, $value, $label, array $options, $desc = '' ) { $id = self::id( $name ); $h = '', $desc, $id, 'select' ); } /** Checkbox with hidden 0. */ public static function toggle( $name, $checked, $label, $desc = '' ) { $id = self::id( $name ); self::row( $label, '', $desc, $id, 'toggle' ); } /** Dropdown of the form's fields. */ public static function field_select( $name, $form_id, $value, $label, $filter = null, $desc = '' ) { self::select( $name, $value, $label, array( '' => __( '— Select field —', 'fieldwren' ) ) + Schema::field_choices( $form_id, $filter ), $desc ); } /** Smartcode picker button for an input id. */ public static function smartcodes_button( $target, $form_id ) { echo ''; // phpcs:ignore } /** Textarea/input with a smartcode button. */ public static function text_sc( $name, $value, $label, $form_id, $desc = '', $multiline = false, $rows = 6 ) { $id = self::id( $name ); ob_start(); if ( $multiline ) { echo ''; } else { echo ''; } echo ' '; self::smartcodes_button( $id, $form_id ); self::row( $label, ob_get_clean(), $desc, $id, $multiline ? 'textarea' : 'text' ); } /** Conditional logic editor row. */ public static function conditions( $name, $form_id, $value, $label ) { self::row( $label, self::conditions_html( $name, $value ), '', '', 'wide' ); } /** @return string Conditions editor markup (used inside repeaters too). */ public static function conditions_html( $name, $value, $placeholder = '__r__' ) { $value = is_array( $value ) ? $value : array(); $rules = (array) ( $value['rules'] ?? array() ); $h = '
'; $h .= ''; $h .= '
'; $h .= '
'; foreach ( $rules as $i => $r ) { $h .= self::rule_row( $name . '[rules][' . (int) $i . ']', $r ); } $h .= '
'; return $h; } /** One rule row. */ private static function rule_row( $name, array $r ) { $h = '
'; } /* ------------------------------------------------------------------ * * Saving * ------------------------------------------------------------------ */ /** Handle both settings screens. */ public static function save() { if ( 'POST' !== ( $_SERVER['REQUEST_METHOD'] ?? '' ) || empty( $_POST['fwren_admin_action'] ) ) { // phpcs:ignore return; } $action = sanitize_key( wp_unslash( $_POST['fwren_admin_action'] ) ); // phpcs:ignore if ( 'form_settings' === $action ) { $form_id = absint( $_POST['form_id'] ?? 0 ); check_admin_referer( 'fwren_form_settings_' . $form_id ); if ( ! current_user_can( 'fwren_manage_forms' ) || ! Form::exists( $form_id ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ) ); } $tab = sanitize_key( wp_unslash( $_POST['tab'] ?? 'general' ) ); $input = isset( $_POST['fwren'] ) ? self::clean( wp_unslash( $_POST['fwren'] ) ) : array(); // phpcs:ignore $raw = get_post_meta( $form_id, '_fwren_settings', true ); $cur = is_string( $raw ) && $raw ? json_decode( $raw, true ) : array(); $cur = is_array( $cur ) ? $cur : array(); foreach ( (array) ( $_POST['fwren__lists'] ?? array() ) as $list ) { // phpcs:ignore $list = sanitize_key( $list ); if ( ! isset( $input[ $list ] ) ) { $input[ $list ] = array(); } $input[ $list ] = array_values( (array) $input[ $list ] ); $cur[ $list ] = array(); } if ( ! current_user_can( 'unfiltered_html' ) ) { unset( $input['layout']['custom_js'] ); } Form::save_settings( $form_id, Form::merge( $cur, $input ) ); do_action( 'fwren/admin/form_tab_save/' . $tab, $form_id, $_POST ); // phpcs:ignore wp_safe_redirect( add_query_arg( 'saved', 1, self::settings_url( $form_id, $tab ) ) ); exit; } if ( 'global_settings' === $action ) { check_admin_referer( 'fwren_global_settings' ); if ( ! current_user_can( 'fwren_manage_settings' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ) ); } $tab = sanitize_key( wp_unslash( $_POST['tab'] ?? 'general' ) ); $input = isset( $_POST['fwren_global'] ) ? self::clean( wp_unslash( $_POST['fwren_global'] ) ) : array(); // phpcs:ignore Settings::save( $input ); do_action( 'fwren/admin/global_tab_save/' . $tab, $_POST ); // phpcs:ignore wp_safe_redirect( admin_url( 'admin.php?page=fwren-settings&tab=' . $tab . '&saved=1' ) ); exit; } } /** * Sanitize posted settings recursively. Strings keep safe HTML; custom CSS/JS keep raw text * for users with unfiltered_html. */ public static function clean( $v, $key = '' ) { if ( is_array( $v ) ) { $out = array(); foreach ( $v as $k => $x ) { $out[ is_int( $k ) ? $k : sanitize_key( $k ) ] = self::clean( $x, (string) $k ); } return $out; } $v = (string) $v; if ( in_array( $key, array( 'custom_css', 'custom_js', 'script' ), true ) ) { return current_user_can( 'unfiltered_html' ) ? $v : wp_strip_all_tags( $v ); } if ( in_array( $key, array( 'secret', 'secret_key', 'api_key', 'token', 'password' ), true ) ) { return trim( $v ); } return wp_kses_post( $v ); } /* ------------------------------------------------------------------ * * Form settings screen * ------------------------------------------------------------------ */ /** @return array Core tabs + module tabs. */ public static function form_tabs( $form_id ) { return apply_filters( 'fwren/admin/form_tabs', array( 'general' => __( 'General', 'fieldwren' ), 'confirmations' => __( 'Confirmations', 'fieldwren' ), 'notifications' => __( 'Email Notifications', 'fieldwren' ), 'restrictions' => __( 'Restrictions', 'fieldwren' ), 'spam' => __( 'Spam Protection', 'fieldwren' ), 'style' => __( 'Custom CSS & JS', 'fieldwren' ), ), $form_id ); } /** @return array Groups of the form settings menu: key => label. */ private static function form_tab_groups() { return array( 'basics' => __( 'Basics', 'fieldwren' ), 'protect' => __( 'Access & protection', 'fieldwren' ), 'experience' => __( 'Experience', 'fieldwren' ), 'connect' => __( 'Connect & automate', 'fieldwren' ), 'more' => __( 'More', 'fieldwren' ), ); } /** * How a tab shows in the form settings menu: tab => [ group, icon, description, short label ]. * A tab without an entry lands in "More" with a gear icon and its own label. * * @return array */ public static function form_tab_meta() { return apply_filters( 'fwren/admin/form_tab_meta', array( 'general' => array( 'basics', 'gear', __( 'How the form submits, what is stored and how multi-step forms behave.', 'fieldwren' ) ), 'confirmations' => array( 'basics', 'yes-alt', __( 'What people see after they submit.', 'fieldwren' ) ), 'notifications' => array( 'basics', 'mail', __( 'Emails sent when someone submits this form.', 'fieldwren' ), __( 'Notifications', 'fieldwren' ) ), 'restrictions' => array( 'protect', 'lock', __( 'Limit who can submit, when and how often.', 'fieldwren' ) ), 'spam' => array( 'protect', 'shield', __( 'Bot traps, captcha and rate limits for this form.', 'fieldwren' ), __( 'Spam protection', 'fieldwren' ) ), 'double_optin' => array( 'protect', 'mail-check', __( 'Ask submitters to confirm by email before their entry counts.', 'fieldwren' ) ), 'admin_approval' => array( 'protect', 'user-check', __( 'Hold entries until someone on your team approves them.', 'fieldwren' ) ), 'save_resume' => array( 'experience', 'backup', __( 'Let people save their progress, and keep entries that were started but not sent.', 'fieldwren' ), __( 'Save & resume', 'fieldwren' ) ), 'quiz' => array( 'experience', 'award', __( 'Score answers and show a result.', 'fieldwren' ) ), 'landing' => array( 'experience', 'share', __( 'A standalone page for this form with its own share link.', 'fieldwren' ) ), 'style' => array( 'experience', 'editor-code', __( 'Extra CSS and JavaScript for this form only.', 'fieldwren' ) ), 'integrations' => array( 'connect', 'plug', __( 'Send entries to your CRM, email tool and other apps.', 'fieldwren' ) ), 'payments' => array( 'connect', 'card', __( 'Currency, payment methods and receipts.', 'fieldwren' ) ), 'inventory' => array( 'connect', 'box', __( 'Limit how many times an option can be chosen.', 'fieldwren' ) ), 'post_feeds' => array( 'connect', 'file-plus', __( 'Create or update posts from entries.', 'fieldwren' ) ), 'user_registration' => array( 'connect', 'user-plus', __( 'Register users or update profiles from entries.', 'fieldwren' ) ), ) ); } /** * What is switched on for a form, shown next to its tab in the menu: true = a green dot, * a number above zero = a count. * * @param int $form_id Form id. * @param array $s Form settings. * @return array tab => bool|int */ public static function form_tab_state( $form_id, array $s ) { $on = static function ( $key ) use ( $s ) { return ! empty( $s[ $key ]['enabled'] ); }; $count = static function ( $list, $default_on = false ) { $n = 0; foreach ( (array) $list as $item ) { if ( is_array( $item ) && ( isset( $item['enabled'] ) ? ! empty( $item['enabled'] ) : $default_on ) ) { $n++; } } return $n; }; $r = (array) ( $s['restrictions'] ?? array() ); $limits = 0; foreach ( array( 'limit_entries', 'schedule', 'login_required', 'per_user', 'blocklist' ) as $k ) { $limits += empty( $r[ $k ]['enabled'] ) ? 0 : 1; } $posts = get_post_meta( $form_id, '_fwren_post_feeds', true ); $posts = is_string( $posts ) && '' !== $posts ? json_decode( $posts, true ) : array(); return apply_filters( 'fwren/admin/form_tab_state', array( 'confirmations' => $count( $s['conditional_confirmations'] ?? array(), true ), 'notifications' => $count( $s['notifications'] ?? array() ), 'restrictions' => $limits, 'double_optin' => $on( 'double_optin' ), 'admin_approval' => $on( 'admin_approval' ), 'save_resume' => $on( 'save_resume' ), 'quiz' => $on( 'quiz' ), 'landing' => $on( 'landing' ), 'style' => '' !== trim( (string) ( $s['layout']['custom_css'] ?? '' ) . (string) ( $s['layout']['custom_js'] ?? '' ) ), 'integrations' => $count( Form::feeds( $form_id ) ), 'inventory' => $on( 'inventory' ), 'post_feeds' => $count( $posts ), 'user_registration' => $on( 'user_registration' ), ), $form_id, $s ); } /** Page. */ public static function page_form_settings() { $form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0; // phpcs:ignore if ( ! Form::exists( $form_id ) ) { echo '

' . esc_html__( 'Form Settings', 'fieldwren' ) . '

' . esc_html__( 'Form not found.', 'fieldwren' ) . '

'; return; } $tabs = self::form_tabs( $form_id ); $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'general'; // phpcs:ignore if ( ! isset( $tabs[ $tab ] ) ) { $tab = 'general'; } $s = Form::settings( $form_id ); $meta = self::form_tab_meta(); $state = self::form_tab_state( $form_id, $s ); $groups = array(); // Known tabs in the order of form_tab_meta(), then whatever else a module added. foreach ( array_keys( array_intersect_key( $meta, $tabs ) + $tabs ) as $id ) { $g = isset( $meta[ $id ][0] ) ? $meta[ $id ][0] : 'more'; $groups[ $g ][ $id ] = ! empty( $meta[ $id ][3] ) ? $meta[ $id ][3] : $tabs[ $id ]; } self::form_header( $form_id, 'settings', 'fwren-app fwren-fs' ); /* Menu */ echo '
'; /* Card */ echo '

' . esc_html( $tabs[ $tab ] ) . '

' . ( ! empty( $meta[ $tab ][2] ) ? '

' . esc_html( $meta[ $tab ][2] ) . '

' : '' ) . '
'; // phpcs:ignore echo '
'; wp_nonce_field( 'fwren_form_settings_' . $form_id ); echo ''; echo '
'; if ( isset( $_GET['saved'] ) ) { // phpcs:ignore echo '
' . Icons::svg( 'check', 16 ) . '' . esc_html__( 'Settings saved.', 'fieldwren' ) . '
'; // phpcs:ignore } $method = 'tab_' . $tab; if ( method_exists( __CLASS__, $method ) ) { self::$method( $form_id, $s ); } do_action( 'fwren/admin/form_tab/' . $tab, $form_id, $s ); echo '
'; if ( apply_filters( 'fwren/admin/form_tab_has_save', true, $tab ) ) { echo '
' . esc_html__( 'Unsaved changes', 'fieldwren' ) . '
'; // phpcs:ignore } echo '
'; } /** * Header shared by a form's screens (Settings, Entries, Reports): the form's name and status, * its shortcode, Preview / Edit, the screen tabs and a few numbers. Opens the page wrapper. * * @param int $form_id Form id. * @param string $current settings | entries | reports. * @param string $class Extra wrapper classes. */ public static function form_header( $form_id, $current, $class = '' ) { $form_id = (int) $form_id; $title = get_the_title( $form_id ); $title = '' !== $title ? $title : __( '(no title)', 'fieldwren' ); $status = (string) get_post_status( $form_id ); $st = get_post_status_object( $status ); $count = Entries::count( $form_id ); $unread = Entries::count( $form_id, 'unread' ); $edit = get_edit_post_link( $form_id ); $links = array( 'settings' => array( self::settings_url( $form_id ), __( 'Settings', 'fieldwren' ), 'gear' ), 'entries' => array( admin_url( 'admin.php?page=fwren-entries&form_id=' . $form_id ), __( 'Entries', 'fieldwren' ), 'inbox' ), 'reports' => array( admin_url( 'admin.php?page=fwren-reports&form_id=' . $form_id ), __( 'Reports', 'fieldwren' ), 'chart' ), ); $stats = array( array( number_format_i18n( $count ), _n( 'entry', 'entries', $count, 'fieldwren' ) ) ); if ( ! empty( Form::settings( $form_id )['analytics']['enabled'] ) ) { $t = Analytics::totals( $form_id, 30 ); $stats[] = array( number_format_i18n( $t['views'] ), __( 'views in 30 days', 'fieldwren' ) ); $stats[] = array( $t['views'] ? min( 100, round( $t['submissions'] / $t['views'] * 100, 1 ) ) . '%' : '—', __( 'conversion', 'fieldwren' ) ); } echo '
'; echo '' . Icons::svg( 'chevron-left', 18 ) . '' . esc_html__( 'All forms', 'fieldwren' ) . ''; // phpcs:ignore echo ''; // phpcs:ignore echo '

' . esc_html( $title ) . '

' . esc_html( $st ? $st->label : $status ) . '
'; echo '
#' . (int) $form_id . '[fieldwren_form id="' . (int) $form_id . '"]' . Icons::svg( 'copy', 13 ) . ''; // phpcs:ignore /* translators: %s time, e.g. "2 hours" */ echo '' . esc_html( sprintf( __( 'Edited %s ago', 'fieldwren' ), human_time_diff( (int) get_post_modified_time( 'U', true, $form_id ), time() ) ) ) . '
'; echo '
    '; foreach ( $stats as $x ) { echo '
  • ' . esc_html( $x[0] ) . '' . esc_html( $x[1] ) . '
  • '; } echo '

'; } /** General. */ private static function tab_general( $form_id, array $s ) { self::section( __( 'Submission', 'fieldwren' ), __( 'What happens when someone presses the submit button.', 'fieldwren' ) ); self::panel(); self::toggle( 'fwren[layout][ajax]', $s['layout']['ajax'], __( 'Submit without reloading the page', 'fieldwren' ) ); self::toggle( 'fwren[layout][scroll_to_error]', $s['layout']['scroll_to_error'], __( 'Scroll to the first error', 'fieldwren' ) ); self::panel_end(); self::section_end(); self::section( __( 'Entries & privacy', 'fieldwren' ), __( 'What is kept with each submission, and for how long.', 'fieldwren' ) ); self::panel(); self::toggle( 'fwren[entries][store]', $s['entries']['store'], __( 'Store entries', 'fieldwren' ), __( 'Off: submissions are emailed and sent to integrations only.', 'fieldwren' ) ); self::toggle( 'fwren[entries][store_ip]', $s['entries']['store_ip'], __( 'Store IP address', 'fieldwren' ) ); self::toggle( 'fwren[privacy][anonymize_ip]', $s['privacy']['anonymize_ip'], __( 'Anonymize IP addresses', 'fieldwren' ) ); self::text( 'fwren[entries][retention_days]', $s['entries']['retention_days'], __( 'Delete entries after (days)', 'fieldwren' ), __( '0 = keep forever (or use the global setting).', 'fieldwren' ), 'number' ); self::toggle( 'fwren[entries][delete_uploads]', $s['entries']['delete_uploads'], __( 'Delete uploaded files with their entry', 'fieldwren' ) ); self::toggle( 'fwren[analytics][enabled]', $s['analytics']['enabled'], __( 'Form analytics (views, starts, conversion)', 'fieldwren' ) ); self::panel_end(); self::section_end(); self::section( __( 'Multi-step forms', 'fieldwren' ), __( 'Used when the form has Step blocks.', 'fieldwren' ) ); self::panel(); self::select( 'fwren[steps][progress]', $s['steps']['progress'], __( 'Multi-step progress', 'fieldwren' ), array( 'bar' => __( 'Progress bar', 'fieldwren' ), 'steps' => __( 'Step titles', 'fieldwren' ), 'none' => __( 'None', 'fieldwren' ) ) ); self::toggle( 'fwren[steps][persist]', $s['steps']['persist'], __( 'Remember step data in the browser', 'fieldwren' ), __( 'Visitors who reload the page continue where they were (local storage).', 'fieldwren' ) ); self::toggle( 'fwren[steps][scroll_top]', $s['steps']['scroll_top'], __( 'Scroll to the top on step change', 'fieldwren' ) ); self::panel_end(); self::section_end(); self::section( __( 'Conversational forms', 'fieldwren' ), __( 'Ask one question at a time, with an optional welcome screen.', 'fieldwren' ) ); self::panel( true ); self::toggle( 'fwren[conversational][enabled]', $s['conversational']['enabled'], __( 'Conversational mode', 'fieldwren' ), __( 'One question at a time, full screen friendly, keyboard navigation.', 'fieldwren' ) ); self::text( 'fwren[conversational][start_title]', $s['conversational']['start_title'], __( 'Welcome screen title', 'fieldwren' ) ); self::textarea( 'fwren[conversational][start_text]', $s['conversational']['start_text'], __( 'Welcome screen text', 'fieldwren' ), '', 2 ); self::text( 'fwren[conversational][start_button]', $s['conversational']['start_button'], __( 'Welcome button', 'fieldwren' ) ); self::panel_end(); self::section_end(); } /** Confirmations. */ private static function tab_confirmations( $form_id, array $s ) { $c = $s['confirmation']; self::section( __( 'Default confirmation', 'fieldwren' ), __( 'Shown after every submission, unless a conditional confirmation below matches.', 'fieldwren' ) ); self::panel(); self::confirmation_rows( 'fwren[confirmation]', $c, $form_id ); self::toggle( 'fwren[confirmation][hide_form]', $c['hide_form'], __( 'Hide the form after submission', 'fieldwren' ) ); self::toggle( 'fwren[confirmation][reset_form]', $c['reset_form'], __( 'Reset the form after submission', 'fieldwren' ) ); self::panel_end(); self::section_end(); self::section( __( 'Conditional confirmations', 'fieldwren' ), __( 'The first one whose conditions match replaces the default confirmation.', 'fieldwren' ) ); echo ''; echo '
'; foreach ( (array) $s['conditional_confirmations'] as $i => $cc ) { echo self::cc_card( 'fwren[conditional_confirmations][' . (int) $i . ']', (array) $cc, $form_id ); // phpcs:ignore } echo '
'; // phpcs:ignore self::section_end(); } /** Confirmation type rows: the type select shows only the rows of the chosen type. */ private static function confirmation_rows( $base, array $c, $form_id ) { self::row_attr( array( 'data-fwren-switch' => '1' ) ); self::select( $base . '[type]', $c['type'] ?? 'message', __( 'After submission', 'fieldwren' ), array( 'message' => __( 'Show a message', 'fieldwren' ), 'page' => __( 'Go to a page', 'fieldwren' ), 'url' => __( 'Go to a URL', 'fieldwren' ) ) ); self::row_attr( array( 'data-fwren-case' => 'message' ) ); self::text_sc( $base . '[message]', $c['message'] ?? '', __( 'Message', 'fieldwren' ), $form_id, '', true, 4 ); $pages = array( '0' => __( '— Select page —', 'fieldwren' ) ); foreach ( get_pages( array( 'number' => 300 ) ) as $p ) { $pages[ (string) $p->ID ] = $p->post_title; } self::row_attr( array( 'data-fwren-case' => 'page' ) ); self::select( $base . '[page_id]', (string) ( $c['page_id'] ?? 0 ), __( 'Page', 'fieldwren' ), $pages ); self::row_attr( array( 'data-fwren-case' => 'url' ) ); self::text_sc( $base . '[url]', $c['url'] ?? '', __( 'URL', 'fieldwren' ), $form_id, __( 'Smartcodes are URL-encoded, e.g. https://calendly.com/you?email={inputs.email}&name={inputs.names}', 'fieldwren' ) ); self::row_attr( array( 'data-fwren-case' => 'url' ) ); self::toggle( $base . '[new_tab]', ! empty( $c['new_tab'] ), __( 'Open the URL in a new tab', 'fieldwren' ) ); } /** @return string "Active" switch in a card header. */ private static function card_active( $name, $checked ) { return ''; } /** Conditional confirmation card. */ private static function cc_card( $base, array $cc, $form_id ) { ob_start(); echo '
' . self::card_active( $base . '[enabled]', ! isset( $cc['enabled'] ) || ! empty( $cc['enabled'] ) ) . '
'; // phpcs:ignore self::confirmation_rows( $base, $cc, $form_id ); $cond = (array) ( $cc['conditions'] ?? array() ); $cond['enabled'] = true; self::row( __( 'Conditions', 'fieldwren' ), str_replace( 'class="fwren-cond__body" hidden', 'class="fwren-cond__body"', self::conditions_html( $base . '[conditions]', $cond, '__r__' ) ), '', '', 'wide' ); echo '
'; return ob_get_clean(); } /** Notifications. */ private static function tab_notifications( $form_id, array $s ) { echo ''; echo '
'; foreach ( (array) $s['notifications'] as $i => $n ) { echo self::notification_card( 'fwren[notifications][' . (int) $i . ']', (array) $n, $form_id ); // phpcs:ignore } echo '
'; // phpcs:ignore echo '
' . Icons::svg( 'info', 18 ) . '

' . esc_html__( 'Tip: add a second notification sent to the submitter ("Send to: email from a field") as an autoresponder.', 'fieldwren' ) . '

'; // phpcs:ignore } /** One notification. */ private static function notification_card( $base, array $n, $form_id ) { $n = array_merge( Form::defaults()['notifications'][0], array( 'name' => '' ), $n ); ob_start(); echo '
' . self::card_active( $base . '[enabled]', ! empty( $n['enabled'] ) ) . ''; // phpcs:ignore self::subhead( __( 'Recipients', 'fieldwren' ) ); self::row_attr( array( 'data-fwren-switch' => '1' ) ); self::select( $base . '[to_type]', $n['to_type'], __( 'Send to', 'fieldwren' ), array( 'email' => __( 'These email addresses', 'fieldwren' ), 'field' => __( 'The email entered in a field', 'fieldwren' ), 'routing' => __( 'Route by rules', 'fieldwren' ) ) ); self::row_attr( array( 'data-fwren-case' => 'email' ) ); self::text_sc( $base . '[to]', $n['to'], __( 'Email addresses', 'fieldwren' ), $form_id, __( 'Comma separated.', 'fieldwren' ) ); self::row_attr( array( 'data-fwren-case' => 'field' ) ); self::field_select( $base . '[to_field]', $form_id, $n['to_field'], __( 'Email field', 'fieldwren' ), static function ( $f ) { return 'email' === $f['type']; } ); ob_start(); echo '
'; foreach ( (array) $n['routing'] as $k => $r ) { echo self::route_row( $base . '[routing][' . (int) $k . ']', (array) $r ); // phpcs:ignore } echo '
'; // phpcs:ignore self::row_attr( array( 'data-fwren-case' => 'routing' ) ); self::row( __( 'Routing rules', 'fieldwren' ), ob_get_clean(), __( 'Send to the email when the rule matches, e.g. Department is Sales → sales@company.com.', 'fieldwren' ), '', 'wide' ); self::text_sc( $base . '[cc]', $n['cc'], __( 'CC', 'fieldwren' ), $form_id ); self::text_sc( $base . '[bcc]', $n['bcc'], __( 'BCC', 'fieldwren' ), $form_id ); self::subhead( __( 'Sender', 'fieldwren' ) ); self::text_sc( $base . '[from_name]', $n['from_name'], __( 'From name', 'fieldwren' ), $form_id, __( 'Empty = global setting.', 'fieldwren' ) ); self::text( $base . '[from_email]', $n['from_email'], __( 'From email', 'fieldwren' ), __( 'Use an address on your domain so the email passes SPF/DKIM.', 'fieldwren' ) ); self::text_sc( $base . '[reply_to]', $n['reply_to'], __( 'Reply-To', 'fieldwren' ), $form_id ); self::subhead( __( 'Email', 'fieldwren' ) ); self::text_sc( $base . '[subject]', $n['subject'], __( 'Subject', 'fieldwren' ), $form_id ); self::text_sc( $base . '[message]', $n['message'], __( 'Message', 'fieldwren' ), $form_id, __( 'HTML allowed. {all_data} prints every answer as a table.', 'fieldwren' ), true, 8 ); if ( class_exists( '\FWREN\Email\Form_Emails' ) ) { self::select( $base . '[email_template]', (string) ( $n['email_template'] ?? '' ), __( 'Email design', 'fieldwren' ), \FWREN\Email\Form_Emails::choices( true ), \FWREN\Email\Form_Emails::choices_help() ); } self::toggle( $base . '[attach_uploads]', $n['attach_uploads'], __( 'Attach uploaded files', 'fieldwren' ) ); self::subhead( __( 'When to send', 'fieldwren' ) ); self::select( $base . '[send_on]', $n['send_on'], __( 'Send when', 'fieldwren' ), array( 'submit' => __( 'The form is submitted (after any confirmation / approval / payment)', 'fieldwren' ), 'confirmed' => __( 'Double opt-in is confirmed', 'fieldwren' ), 'approved' => __( 'An admin approves the entry', 'fieldwren' ), 'paid' => __( 'Payment succeeds', 'fieldwren' ) ) ); self::row( __( 'Conditions', 'fieldwren' ), self::conditions_html( $base . '[conditions]', (array) $n['conditions'], '__r__' ), '', '', 'wide' ); echo '
'; return ob_get_clean(); } /** Routing rule row. */ private static function route_row( $name, array $r ) { $h = '
→
'; } /** Restrictions. */ private static function tab_restrictions( $form_id, array $s ) { $r = $s['restrictions']; self::section( __( 'Limit entries', 'fieldwren' ), __( 'Close the form once it has enough submissions.', 'fieldwren' ) ); self::panel( true ); self::toggle( 'fwren[restrictions][limit_entries][enabled]', $r['limit_entries']['enabled'], __( 'Limit the number of entries', 'fieldwren' ) ); self::text( 'fwren[restrictions][limit_entries][count]', $r['limit_entries']['count'], __( 'Maximum entries', 'fieldwren' ), '', 'number' ); self::select( 'fwren[restrictions][limit_entries][period]', $r['limit_entries']['period'], __( 'Per', 'fieldwren' ), array( 'total' => __( 'In total', 'fieldwren' ), 'day' => __( 'Day', 'fieldwren' ), 'week' => __( 'Week', 'fieldwren' ), 'month' => __( 'Month', 'fieldwren' ), 'year' => __( 'Year', 'fieldwren' ) ) ); self::textarea( 'fwren[restrictions][limit_entries][message]', $r['limit_entries']['message'], __( 'Message when full', 'fieldwren' ), '', 2 ); self::panel_end(); self::section_end(); self::section( __( 'Schedule', 'fieldwren' ), __( 'Accept submissions only inside a date range.', 'fieldwren' ) ); self::panel( true ); self::toggle( 'fwren[restrictions][schedule][enabled]', $r['schedule']['enabled'], __( 'Open the form only between dates', 'fieldwren' ) ); self::text( 'fwren[restrictions][schedule][start]', $r['schedule']['start'], __( 'Opens', 'fieldwren' ), '', 'datetime-local' ); self::text( 'fwren[restrictions][schedule][end]', $r['schedule']['end'], __( 'Closes', 'fieldwren' ), '', 'datetime-local' ); self::textarea( 'fwren[restrictions][schedule][pending_message]', $r['schedule']['pending_message'], __( 'Message before opening', 'fieldwren' ), '', 2 ); self::textarea( 'fwren[restrictions][schedule][expired_message]', $r['schedule']['expired_message'], __( 'Message after closing', 'fieldwren' ), '', 2 ); self::panel_end(); self::section_end(); self::section( __( 'Who can submit', 'fieldwren' ), __( 'Ask for an account, or cap how often one person can submit.', 'fieldwren' ) ); self::panel( true ); self::toggle( 'fwren[restrictions][login_required][enabled]', $r['login_required']['enabled'], __( 'Require login', 'fieldwren' ) ); self::textarea( 'fwren[restrictions][login_required][message]', $r['login_required']['message'], __( 'Message for visitors', 'fieldwren' ), '', 2 ); self::panel_end(); self::panel( true ); self::toggle( 'fwren[restrictions][per_user][enabled]', $r['per_user']['enabled'], __( 'Limit entries per person', 'fieldwren' ) ); self::text( 'fwren[restrictions][per_user][count]', $r['per_user']['count'], __( 'Entries per person', 'fieldwren' ), '', 'number' ); self::select( 'fwren[restrictions][per_user][by]', $r['per_user']['by'], __( 'Identify by', 'fieldwren' ), array( 'ip' => __( 'IP address', 'fieldwren' ), 'user' => __( 'Logged-in user', 'fieldwren' ), 'cookie' => __( 'Browser cookie', 'fieldwren' ) ) ); self::textarea( 'fwren[restrictions][per_user][message]', $r['per_user']['message'], __( 'Message', 'fieldwren' ), '', 2 ); self::panel_end(); self::panel(); self::toggle( 'fwren[restrictions][deny_empty][enabled]', $r['deny_empty']['enabled'], __( 'Reject empty submissions', 'fieldwren' ) ); self::panel_end(); self::section_end(); self::section( __( 'Blocklist', 'fieldwren' ), __( 'Reject submissions with certain words, senders or IP addresses.', 'fieldwren' ) ); self::panel( true ); self::toggle( 'fwren[restrictions][blocklist][enabled]', $r['blocklist']['enabled'], __( 'Block submissions containing', 'fieldwren' ) ); self::textarea( 'fwren[restrictions][blocklist][keywords]', $r['blocklist']['keywords'], __( 'Words or phrases', 'fieldwren' ), __( 'One per line or comma separated.', 'fieldwren' ), 3 ); self::textarea( 'fwren[restrictions][blocklist][emails]', $r['blocklist']['emails'], __( 'Emails or domains', 'fieldwren' ), '', 2 ); self::textarea( 'fwren[restrictions][blocklist][ips]', $r['blocklist']['ips'], __( 'IP addresses', 'fieldwren' ), __( 'Use a trailing * for ranges, e.g. 203.0.113.*', 'fieldwren' ), 2 ); self::textarea( 'fwren[restrictions][blocklist][message]', $r['blocklist']['message'], __( 'Message', 'fieldwren' ), '', 2 ); self::panel_end(); self::section_end(); } /** Spam. */ private static function tab_spam( $form_id, array $s ) { $sp = $s['spam']; self::section( __( 'Bot traps', 'fieldwren' ), __( 'Invisible checks that stop most automated spam without bothering people.', 'fieldwren' ) ); self::panel(); self::toggle( 'fwren[spam][honeypot]', $sp['honeypot'], __( 'Honeypot', 'fieldwren' ), __( 'A hidden field bots fill in.', 'fieldwren' ) ); self::text( 'fwren[spam][time_trap]', $sp['time_trap'], __( 'Minimum seconds before submitting', 'fieldwren' ), __( '0 = off.', 'fieldwren' ), 'number' ); self::panel_end(); self::section_end(); self::section( __( 'Captcha & filtering', 'fieldwren' ), __( 'A challenge or a reputation check for this form.', 'fieldwren' ) ); self::panel(); self::select( 'fwren[spam][captcha]', $sp['captcha'], __( 'Captcha', 'fieldwren' ), array( 'global' => __( 'Global default', 'fieldwren' ), 'none' => __( 'None', 'fieldwren' ), 'turnstile' => 'Cloudflare Turnstile', 'recaptcha_v3' => 'reCAPTCHA v3', 'recaptcha_v2' => 'reCAPTCHA v2', 'hcaptcha' => 'hCaptcha' ), __( 'Keys are set in Forms → Settings → Captcha.', 'fieldwren' ) ); self::toggle( 'fwren[spam][akismet]', $sp['akismet'], __( 'Check with Akismet', 'fieldwren' ) ); self::panel_end(); self::section_end(); self::section( __( 'Rate limit', 'fieldwren' ), __( 'Cap how often one IP address can submit.', 'fieldwren' ) ); self::panel( true ); self::toggle( 'fwren[spam][rate_limit][enabled]', $sp['rate_limit']['enabled'], __( 'Rate limit per IP', 'fieldwren' ) ); self::text( 'fwren[spam][rate_limit][count]', $sp['rate_limit']['count'], __( 'Submissions allowed', 'fieldwren' ), '', 'number' ); self::text( 'fwren[spam][rate_limit][minutes]', $sp['rate_limit']['minutes'], __( 'Per minutes', 'fieldwren' ), '', 'number' ); self::panel_end(); self::section_end(); } /** Custom CSS & JS (styling itself lives in the block inspector). */ private static function tab_style( $form_id, array $s ) { $l = $s['layout']; echo '
' . Icons::svg( 'info', 18 ) . '

' . esc_html__( 'Field, label, input and button styles are set per block in the form editor (Style tab). Use custom CSS for anything else.', 'fieldwren' ) . '

'; // phpcs:ignore self::panel(); self::textarea( 'fwren[layout][custom_css]', $l['custom_css'], __( 'Custom CSS', 'fieldwren' ), __( 'Selectors are scoped to this form automatically. Use {{form}} for the form wrapper itself.', 'fieldwren' ), 8 ); if ( current_user_can( 'unfiltered_html' ) ) { self::textarea( 'fwren[layout][custom_js]', $l['custom_js'], __( 'Custom JavaScript', 'fieldwren' ), __( 'Runs when the form is ready. Variables: $form (the form element). Listen to fwren:submitted, fwren:step, fwren:change.', 'fieldwren' ), 6 ); } self::panel_end(); } /* ------------------------------------------------------------------ * * Global settings * ------------------------------------------------------------------ */ /** Page. */ public static function page_global() { $tabs = apply_filters( 'fwren/admin/global_tabs', array( 'general' => __( 'General', 'fieldwren' ), 'captcha' => __( 'Captcha', 'fieldwren' ), 'email' => __( 'Email', 'fieldwren' ), 'tracking'=> __( 'Conversion tracking', 'fieldwren' ), ) ); $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'general'; // phpcs:ignore if ( ! isset( $tabs[ $tab ] ) ) { $tab = 'general'; } $g = Settings::all(); $nav = Admin_Dashboard::nav(); Admin_Dashboard::open( $tab, isset( $nav[ $tab ] ) ? $nav[ $tab ][0] : $tabs[ $tab ] ); if ( isset( $nav[ $tab ] ) ) { echo '

' . esc_html( $nav[ $tab ][1] ) . '

'; } if ( isset( $_GET['saved'] ) ) { // phpcs:ignore echo '

' . esc_html__( 'Settings saved.', 'fieldwren' ) . '

'; } echo '
'; wp_nonce_field( 'fwren_global_settings' ); echo ''; switch ( $tab ) { case 'general': echo ''; self::toggle( 'fwren_global[trust_proxy]', $g['trust_proxy'], __( 'Behind Cloudflare / a proxy', 'fieldwren' ), __( 'Read the visitor IP from CF-Connecting-IP / X-Forwarded-For.', 'fieldwren' ) ); self::toggle( 'fwren_global[async_feeds]', $g['async_feeds'], __( 'Send integrations in the background', 'fieldwren' ), __( 'Faster submissions; uses Action Scheduler when available, otherwise WP-Cron.', 'fieldwren' ) ); self::text( 'fwren_global[retention_days]', $g['retention_days'], __( 'Delete entries after (days)', 'fieldwren' ), __( '0 = keep forever. A form can override it.', 'fieldwren' ), 'number' ); self::text( 'fwren_global[google_maps_key]', $g['google_maps_key'], __( 'Google Maps API key', 'fieldwren' ), __( 'For address autocomplete.', 'fieldwren' ) ); self::toggle( 'fwren_global[akismet]', $g['akismet'], __( 'Akismet on every form', 'fieldwren' ) ); self::toggle( 'fwren_global[delete_on_uninstall]', $g['delete_on_uninstall'], __( 'Delete all data when the plugin is uninstalled', 'fieldwren' ) ); echo '
'; break; case 'captcha': echo ''; self::select( 'fwren_global[captcha]', $g['captcha'], __( 'Default captcha', 'fieldwren' ), array( 'none' => __( 'None', 'fieldwren' ), 'turnstile' => 'Cloudflare Turnstile', 'recaptcha_v3' => 'reCAPTCHA v3', 'recaptcha_v2' => 'reCAPTCHA v2', 'hcaptcha' => 'hCaptcha' ) ); foreach ( array( 'turnstile' => 'Cloudflare Turnstile', 'recaptcha_v3' => 'reCAPTCHA v3', 'recaptcha_v2' => 'reCAPTCHA v2', 'hcaptcha' => 'hCaptcha' ) as $k => $l ) { echo ''; self::text( "fwren_global[$k][site]", $g[ $k ]['site'], __( 'Site key', 'fieldwren' ) ); self::text( "fwren_global[$k][secret]", $g[ $k ]['secret'], __( 'Secret key', 'fieldwren' ), '', 'password' ); } self::text( 'fwren_global[recaptcha_v3][threshold]', $g['recaptcha_v3']['threshold'], __( 'reCAPTCHA v3 minimum score', 'fieldwren' ), '0.0 – 1.0' ); echo '

' . esc_html( $l ) . '

'; break; case 'email': echo ''; if ( class_exists( '\FWREN\Email\Form_Emails' ) ) { self::select( 'fwren_global[email][design_template]', (string) ( $g['email']['design_template'] ?? '' ), __( 'Default email design', 'fieldwren' ), \FWREN\Email\Form_Emails::choices( false ), \FWREN\Email\Form_Emails::choices_help() ); } self::text( 'fwren_global[email][from_name]', $g['email']['from_name'], __( 'From name', 'fieldwren' ) ); self::text( 'fwren_global[email][from_email]', $g['email']['from_email'], __( 'From email', 'fieldwren' ), __( 'Empty = WordPress default.', 'fieldwren' ), 'email' ); self::select( 'fwren_global[email][template]', $g['email']['template'], __( 'Email design', 'fieldwren' ), array( 'styled' => __( 'Styled (logo, card)', 'fieldwren' ), 'plain' => __( 'Plain HTML', 'fieldwren' ) ) ); self::text( 'fwren_global[email][logo]', $g['email']['logo'], __( 'Logo URL', 'fieldwren' ), '', 'url' ); self::text( 'fwren_global[email][color]', $g['email']['color'], __( 'Brand color', 'fieldwren' ) ); self::textarea( 'fwren_global[email][footer]', $g['email']['footer'], __( 'Footer', 'fieldwren' ), '', 2 ); echo '
'; break; case 'tracking': echo ''; self::toggle( 'fwren_global[tracking][datalayer]', $g['tracking']['datalayer'], __( 'Push events to the dataLayer (GTM)', 'fieldwren' ), __( 'Events: fwren_form_view, fwren_form_start, fwren_form_step, fwren_form_submit (with form_id, form_name, entry_id).', 'fieldwren' ) ); self::toggle( 'fwren_global[tracking][ga4]', $g['tracking']['ga4'], __( 'Send GA4 events through gtag()', 'fieldwren' ), __( 'generate_lead on submit when gtag is on the page.', 'fieldwren' ) ); self::toggle( 'fwren_global[tracking][meta_pixel]', $g['tracking']['meta_pixel'], __( 'Meta Pixel "Lead" event', 'fieldwren' ) ); self::text( 'fwren_global[tracking][linkedin]', $g['tracking']['linkedin'], __( 'LinkedIn conversion ID', 'fieldwren' ), __( 'Fires window.lintrk("track", {conversion_id}) on submit.', 'fieldwren' ) ); echo '
'; break; } do_action( 'fwren/admin/global_tab/' . $tab ); if ( apply_filters( 'fwren/admin/global_tab_has_save', true, $tab ) ) { submit_button(); } echo '
'; Admin_Dashboard::close(); } /* ------------------------------------------------------------------ * * Dashboard, reports, logs * ------------------------------------------------------------------ */ /** Dashboard. */ public static function page_dashboard() { Admin_Dashboard::render(); } /** Simple SVG chart of a daily series. */ public static function chart( array $series ) { $max = 1; foreach ( $series as $d ) { $max = max( $max, $d['views'], $d['submissions'] ); } $w = 900; $h = 180; $n = max( 1, count( $series ) ); $bw = $w / $n; $svg = ''; $i = 0; foreach ( $series as $day => $d ) { $x = $i * $bw; $vh = $d['views'] / $max * $h; $sh = $d['submissions'] / $max * $h; $svg .= '' . esc_html( $day . ': ' . $d['views'] . ' views' ) . ''; $svg .= '' . esc_html( $day . ': ' . $d['submissions'] . ' submissions' ) . ''; if ( 0 === $i % 5 ) { $svg .= '' . esc_html( gmdate( 'M j', strtotime( $day ) ) ) . ''; } $i++; } echo '
' . $svg . '
' . esc_html__( 'Views', 'fieldwren' ) . ' ' . esc_html__( 'Submissions', 'fieldwren' ) . '
'; // phpcs:ignore } /** Reports: per-form funnel and choice breakdowns (survey results). */ public static function page_reports() { global $wpdb; $form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0; // phpcs:ignore $days = isset( $_GET['days'] ) ? max( 1, min( 365, absint( $_GET['days'] ) ) ) : 30; // phpcs:ignore if ( $form_id && Form::exists( $form_id ) ) { self::form_header( $form_id, 'reports' ); } else { $form_id = 0; echo '

' . esc_html__( 'Reports', 'fieldwren' ) . '

'; } echo '
'; $t = Analytics::totals( $form_id, $days ); echo '
'; foreach ( array( array( __( 'Views', 'fieldwren' ), $t['views'] ), array( __( 'Started', 'fieldwren' ), $t['starts'] ), array( __( 'Start rate', 'fieldwren' ), $t['views'] ? round( $t['starts'] / $t['views'] * 100, 1 ) . '%' : '—' ), array( __( 'Submissions', 'fieldwren' ), $t['submissions'] ), array( __( 'Conversion', 'fieldwren' ), $t['views'] ? round( $t['submissions'] / $t['views'] * 100, 1 ) . '%' : '—' ), array( __( 'Completion (of started)', 'fieldwren' ), $t['starts'] ? round( $t['submissions'] / $t['starts'] * 100, 1 ) . '%' : '—' ) ) as $k ) { echo '
' . esc_html( $k[0] ) . '' . esc_html( (string) $k[1] ) . '
'; } echo '
'; self::chart( Analytics::series( $form_id, $days ) ); if ( ! $form_id ) { echo '

' . esc_html__( 'By form', 'fieldwren' ) . '

'; foreach ( Embed::form_options() as $id => $title ) { if ( ! $id ) { continue; } $ft = Analytics::totals( (int) $id, $days ); echo ''; } echo '
' . esc_html__( 'Form', 'fieldwren' ) . '' . esc_html__( 'Views', 'fieldwren' ) . '' . esc_html__( 'Starts', 'fieldwren' ) . '' . esc_html__( 'Submissions', 'fieldwren' ) . '' . esc_html__( 'Conversion', 'fieldwren' ) . '
' . esc_html( $title ) . '' . (int) $ft['views'] . '' . (int) $ft['starts'] . '' . (int) $ft['submissions'] . '' . esc_html( $ft['views'] ? round( $ft['submissions'] / $ft['views'] * 100, 1 ) . '%' : '—' ) . '
'; return; } // Survey results. echo '

' . esc_html__( 'Answers', 'fieldwren' ) . '

'; $since = gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ); foreach ( Schema::fields( $form_id ) as $k => $f ) { if ( empty( $f['options'] ) && ! in_array( $f['type'], array( 'rating', 'nps', 'country' ), true ) ) { continue; } $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT v.value, COUNT(*) n FROM ' . Install::table( 'entry_values' ) . ' v INNER JOIN ' . Install::table( 'entries' ) . " e ON e.id = v.entry_id WHERE v.form_id = %d AND v.field_name = %s AND e.status NOT IN ('trash','spam','partial') AND e.created_at >= %s GROUP BY v.value ORDER BY n DESC LIMIT 50", $form_id, $k, $since ), ARRAY_A ); // phpcs:ignore $counts = array(); foreach ( $rows as $r ) { $vals = json_decode( $r['value'], true ); foreach ( is_array( $vals ) ? $vals : array( $r['value'] ) as $v ) { $counts[ (string) $v ] = ( $counts[ (string) $v ] ?? 0 ) + (int) $r['n']; } } if ( ! $counts ) { continue; } arsort( $counts ); $total = array_sum( $counts ); $type = Fields::get( $f['type'] ); echo '

' . esc_html( Schema::label( $f ) ) . ' (' . (int) $total . ')

'; if ( 'nps' === $f['type'] ) { $pro = $det = 0; foreach ( $counts as $v => $n ) { if ( (int) $v >= 9 ) { $pro += $n; } elseif ( (int) $v <= 6 ) { $det += $n; } } /* translators: %s score */ echo '

' . esc_html( sprintf( __( 'NPS: %s', 'fieldwren' ), round( ( $pro - $det ) / $total * 100 ) ) ) . '

'; } foreach ( $counts as $v => $n ) { $pct = round( $n / $total * 100, 1 ); $label = $type ? wp_strip_all_tags( $type->format( $v, $f, 'text' ) ) : $v; echo '
' . esc_html( $label ) . '' . (int) $n . ' · ' . esc_html( $pct ) . '%
'; } echo '
'; } echo '
'; } /** Logs. */ public static function page_logs() { $page = max( 1, absint( $_GET['paged'] ?? 1 ) ); // phpcs:ignore $args = array( 'component' => sanitize_key( wp_unslash( $_GET['component'] ?? '' ) ), // phpcs:ignore 'status' => sanitize_key( wp_unslash( $_GET['status'] ?? '' ) ), // phpcs:ignore 'form_id' => absint( $_GET['form_id'] ?? 0 ), // phpcs:ignore 'page' => $page, ); list( $rows, $total ) = Logs::query( $args ); echo '

' . esc_html__( 'Logs', 'fieldwren' ) . '

'; echo ''; if ( ! $rows ) { echo ''; } foreach ( $rows as $r ) { echo ''; } echo '
' . esc_html__( 'Date', 'fieldwren' ) . '' . esc_html__( 'Form', 'fieldwren' ) . '' . esc_html__( 'Entry', 'fieldwren' ) . '' . esc_html__( 'Component', 'fieldwren' ) . '' . esc_html__( 'Status', 'fieldwren' ) . '' . esc_html__( 'Title', 'fieldwren' ) . '' . esc_html__( 'Message', 'fieldwren' ) . '
' . esc_html__( 'Nothing logged yet.', 'fieldwren' ) . '
' . esc_html( Admin_Entries::date( $r['created_at'] ) ) . '' . esc_html( $r['form_id'] ? get_the_title( $r['form_id'] ) : '' ) . '' . ( $r['entry_id'] ? '#' . (int) $r['entry_id'] . '' : '' ) . '' . esc_html( $r['component'] ) . '' . esc_html( $r['status'] ) . '' . esc_html( $r['title'] ) . '' . esc_html( wp_trim_words( (string) $r['message'], 40 ) ) . '
'; $pages = (int) ceil( $total / 50 ); if ( $pages > 1 ) { echo '
' . paginate_links( array( 'base' => add_query_arg( 'paged', '%#%' ), 'total' => $pages, 'current' => $page ) ) . '
'; // phpcs:ignore } echo '
'; } /* ------------------------------------------------------------------ * * Actions * ------------------------------------------------------------------ */ /** Template library page. */ public static function page_templates() { $cats = Templates::categories(); $all = Templates::all(); $count = array(); foreach ( $all as $t ) { $count[ $t['category'] ] = ( $count[ $t['category'] ] ?? 0 ) + 1; } echo '

' . esc_html__( 'Form templates', 'fieldwren' ) . '

' . esc_html( sprintf( /* translators: %d templates */ _n( '%d ready-made form. Preview it, then start from it – every field, rule and message stays editable.', '%d ready-made forms. Preview one, then start from it – every field, rule and message stays editable.', count( $all ), 'fieldwren' ), count( $all ) ) ) . '

'; echo '
'; foreach ( $cats as $k => $label ) { if ( ! empty( $count[ $k ] ) ) { echo ''; } } echo '
'; foreach ( $all as $id => $t ) { $use = wp_nonce_url( admin_url( 'admin-post.php?action=fwren_use_template&tpl=' . rawurlencode( $id ) ), 'fwren_use_template' ); $n = substr_count( $t['content'], '"name":"' ); echo '
'; echo '' . esc_html( $cats[ $t['category'] ] ?? $t['category'] ) . '

' . esc_html( $t['title'] ) . '

' . esc_html( $t['description'] ) . '

'; $tags = array(); if ( false !== strpos( $t['content'], 'wp:fwren/step' ) ) { $tags[] = __( 'Multi-step', 'fieldwren' ); } if ( false !== strpos( $t['content'], '"cl_enabled":"yes"' ) ) { $tags[] = __( 'Conditional', 'fieldwren' ); } if ( ! empty( $t['settings']['quiz'] ) ) { $tags[] = __( 'Quiz scoring', 'fieldwren' ); } if ( false !== strpos( $t['content'], 'wp:fwren/calculation' ) ) { $tags[] = __( 'Calculation', 'fieldwren' ); } if ( false !== strpos( $t['content'], 'wp:fwren/payment' ) ) { $tags[] = __( 'Payment', 'fieldwren' ); } /* translators: %d fields */ echo '
' . esc_html( sprintf( _n( '%d field', '%d fields', $n, 'fieldwren' ), $n ) ) . ''; foreach ( $tags as $tag ) { echo '' . esc_html( $tag ) . ''; } echo '
'; } echo '
'; ?>