'; } /* ------------------------------------------------------------------ * * Shell * ------------------------------------------------------------------ */ /** @return array Pages kept registered (reachable by URL and the app menu) but left out of the Forms submenu. */ public static function hidden_pages() { return apply_filters( 'fwren/admin/hidden_submenus', array( 'post-new.php?post_type=' . Plugin::POST_TYPE, 'fwren-reports', 'fwren-logs', 'fwren-form-settings', 'fwren-global-styles', 'fwren-coupons', 'fwren-tools' ) ); } /** Hooks. */ public static function init() { add_action( 'admin_menu', array( __CLASS__, 'adopt_pages' ), 1000 ); add_action( 'admin_head', array( __CLASS__, 'trim_menu' ), 0 ); add_filter( 'parent_file', array( __CLASS__, 'highlight' ), 20 ); } /** * Slim the Forms submenu to Dashboard, All Forms, Templates, Entries, Payments, Settings. * Runs on admin_head – after WordPress has checked access to the current page (removing * entries during admin_menu makes those pages 403) and before the menu is printed. */ public static function trim_menu() { global $submenu; if ( empty( $submenu[ Admin::MENU_SLUG ] ) ) { return; } $hide = self::hidden_pages(); $order = array_flip( array( Admin::MENU_SLUG, 'edit.php?post_type=' . Plugin::POST_TYPE, 'fwren-templates', 'fwren-entries', 'edit.php?post_type=fwren_popup', 'edit.php?post_type=fwrene_template', 'fwren-payments', 'fwren-settings' ) ); $items = array_values( array_filter( $submenu[ Admin::MENU_SLUG ], static function ( $i ) use ( $hide ) { return ! in_array( $i[2], $hide, true ); } ) ); usort( $items, static function ( $a, $b ) use ( $order ) { return ( $order[ $a[2] ] ?? 99 ) <=> ( $order[ $b[2] ] ?? 99 ); } ); $submenu[ Admin::MENU_SLUG ] = $items; // phpcs:ignore WordPress.WP.GlobalVariablesOverride } /** Hidden pages highlight the submenu entry they now live under. */ public static function highlight( $file ) { global $plugin_page; $map = array( 'fwren-reports' => Admin::MENU_SLUG, 'fwren-logs' => 'fwren-settings', 'fwren-tools' => 'fwren-settings', 'fwren-global-styles' => 'fwren-settings', 'fwren-coupons' => 'fwren-settings', ); if ( 'fwren-reports' === $plugin_page && ! empty( $_GET['form_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification $map['fwren-reports'] = 'edit.php?post_type=' . Plugin::POST_TYPE; } if ( $plugin_page && isset( $map[ $plugin_page ] ) ) { $GLOBALS['submenu_file'] = $map[ $plugin_page ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride return Admin::MENU_SLUG; } return $file; } /** Render Reports, Logs, Tools and Coupons inside the app shell. */ public static function adopt_pages() { $pages = array( 'fwren-reports' => array( 'reports', array( Admin::class, 'page_reports' ) ), 'fwren-logs' => array( 'logs', array( Admin::class, 'page_logs' ) ), 'fwren-tools' => array( 'tools', array( '\FWREN\Modules\Importer', 'page' ) ), 'fwren-coupons' => array( 'coupons', array( '\FWREN\Payments\Admin_Screens', 'page_coupons' ) ), ); foreach ( $pages as $slug => $p ) { $hook = get_plugin_page_hookname( $slug, Admin::MENU_SLUG ); if ( ! is_callable( $p[1] ) || ! has_action( $hook ) ) { continue; } remove_all_actions( $hook ); add_action( $hook, static function () use ( $p ) { self::embed( $p[0], $p[1] ); } ); } } /** * Print a page's own output inside the shell; its first

becomes the shell title. * * @param string $key Nav key. * @param callable $callback Original page callback. */ public static function embed( $key, $callback ) { // A single form's report has its own per-form header and tabs. if ( 'reports' === $key && ! empty( $_GET['form_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification call_user_func( $callback ); return; } ob_start(); call_user_func( $callback ); $html = (string) ob_get_clean(); $nav = self::nav(); $title = $nav[ $key ][0] ?? ''; if ( preg_match( '#]*>(.*?)

#s', $html, $m ) ) { $found = trim( wp_strip_all_tags( $m[1] ) ); // Sub-screens (edit coupon…) keep their own heading; list screens use the menu label. if ( '' !== $found && ( '' === $title || ! empty( $_GET['action'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification $title = $found; } $html = preg_replace( '#]*>.*?#s', '', $html, 1 ); } self::open( $key, $title ); if ( isset( $nav[ $key ] ) ) { echo '

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

'; } echo $html; // phpcs:ignore WordPress.Security.EscapeOutput -- the page's own escaped output. self::close(); } /** @return array Navigation: key => [ title, description, url, icon, capability ]. */ public static function nav() { $s = admin_url( 'admin.php?page=fwren-settings&tab=' ); $set = 'fwren_manage_settings'; $items = array( 'dashboard' => array( __( 'Dashboard', 'fieldwren' ), __( 'Overview, stats and quick start', 'fieldwren' ), admin_url( 'admin.php?page=fwren' ), 'home', 'fwren_view_entries' ), 'reports' => array( __( 'Reports', 'fieldwren' ), __( 'Funnels and survey answers per form', 'fieldwren' ), admin_url( 'admin.php?page=fwren-reports' ), 'chart', 'fwren_view_entries' ), 'general' => array( __( 'General', 'fieldwren' ), __( 'Proxy, retention, maps and data', 'fieldwren' ), $s . 'general', 'gear', $set ), 'captcha' => array( __( 'Spam protection', 'fieldwren' ), __( 'Turnstile, reCAPTCHA and hCaptcha keys', 'fieldwren' ), $s . 'captcha', 'shield', $set ), 'email' => array( __( 'Email', 'fieldwren' ), __( 'Sender, logo and email design', 'fieldwren' ), $s . 'email', 'mail', $set ), 'tracking' => array( __( 'Conversion tracking', 'fieldwren' ), __( 'GTM, GA4, Meta and LinkedIn events', 'fieldwren' ), $s . 'tracking', 'target', $set ), ); // Module tabs (integrations, payments…) keep their place in the menu. $known = array_keys( $items ); $extra = apply_filters( 'fwren/admin/global_tabs', array() ); $meta = array( 'integrations' => array( __( 'Connect CRMs, email tools and apps', 'fieldwren' ), 'plug' ), 'payments' => array( __( 'Gateways, test mode and webhooks', 'fieldwren' ), 'card' ), ); foreach ( (array) $extra as $id => $label ) { if ( in_array( $id, $known, true ) ) { continue; } $items[ $id ] = array( $label, $meta[ $id ][0] ?? __( 'Module settings', 'fieldwren' ), $s . $id, $meta[ $id ][1] ?? 'gear', $set ); if ( 'payments' === $id && class_exists( '\FWREN\Payments\Admin_Screens' ) ) { $items['coupons'] = array( __( 'Coupons', 'fieldwren' ), __( 'Discount codes for paid forms', 'fieldwren' ), admin_url( 'admin.php?page=fwren-coupons' ), 'tag', $set ); } } if ( class_exists( '\FWREN_BC\Kit' ) ) { $items['styles'] = array( __( 'Global styles', 'fieldwren' ), __( 'Colours and fonts every form uses', 'fieldwren' ), admin_url( 'admin.php?page=fwren-global-styles' ), 'palette', 'manage_options' ); } if ( class_exists( '\FWREN\Modules\Importer' ) ) { $items['tools'] = array( __( 'Import & export', 'fieldwren' ), __( 'Move forms, import from Fluent Forms', 'fieldwren' ), admin_url( 'admin.php?page=fwren-tools' ), 'download', 'fwren_manage_forms' ); } $items['logs'] = array( __( 'Activity log', 'fieldwren' ), __( 'Emails, integrations and payment events', 'fieldwren' ), admin_url( 'admin.php?page=fwren-logs' ), 'list', $set ); $items = apply_filters( 'fwren/admin/app_nav', $items ); return array_filter( $items, static function ( $n ) { return empty( $n[4] ) || current_user_can( $n[4] ); } ); } /** Open the shell. */ public static function open( $active, $title, $actions = '' ) { echo '

' . esc_html( $title ) . '

' . $actions . '
'; // phpcs:ignore } /** Close the shell. */ public static function close() { echo '
'; // Narrow screens show the menu as a scrolling row: bring the current item into view. echo ''; } /* ------------------------------------------------------------------ * * Dashboard * ------------------------------------------------------------------ */ /** @return string "+12%" style change between two numbers. */ private static function delta( $now, $before ) { if ( ! $before ) { return $now ? '' . esc_html__( 'new', 'fieldwren' ) . '' : '—'; } $pct = round( ( $now - $before ) / $before * 100 ); $cls = $pct > 0 ? 'is-up' : ( $pct < 0 ? 'is-down' : '' ); return '' . ( $pct > 0 ? '▲ ' : ( $pct < 0 ? '▼ ' : '' ) ) . abs( $pct ) . '%'; } /** Render. */ public static function render() { global $wpdb; $user = wp_get_current_user(); $forms = (int) wp_count_posts( Plugin::POST_TYPE )->publish; $series = Analytics::series( 0, 60 ); $now = array( 'views' => 0, 'starts' => 0, 'submissions' => 0 ); $prev = $now; $i = 0; foreach ( $series as $d ) { foreach ( $now as $k => $v ) { if ( $i >= 30 ) { $now[ $k ] += $d[ $k ]; } else { $prev[ $k ] += $d[ $k ]; } } $i++; } $conv = $now['views'] ? round( $now['submissions'] / $now['views'] * 100, 1 ) : 0; $conv_prev = $prev['views'] ? round( $prev['submissions'] / $prev['views'] * 100, 1 ) : 0; $unread = Entries::count( 0, 'unread' ); $total = Entries::count( 0 ); $new_url = admin_url( 'post-new.php?post_type=' . Plugin::POST_TYPE ); $tpl_url = admin_url( 'admin.php?page=fwren-templates' ); self::open( 'dashboard', __( 'Dashboard', 'fieldwren' ), '
' . self::icon( 'layout', 18 ) . esc_html__( 'Templates', 'fieldwren' ) . '' . self::icon( 'plus', 18 ) . esc_html__( 'New form', 'fieldwren' ) . '
' ); /* Hero */ $tpl_count = count( Templates::all() ); $fields = count( Fields::block_names() ); $ints = class_exists( '\FWREN\Integrations\Manager' ) && method_exists( '\FWREN\Integrations\Manager', 'all' ) ? count( (array) \FWREN\Integrations\Manager::all() ) : 37; echo '
' . esc_html( sprintf( /* translators: %s name */ __( 'Hi %s 👋', 'fieldwren' ), $user->display_name ) ) . ''; echo '

' . esc_html__( 'Build forms people actually finish.', 'fieldwren' ) . '

' . esc_html__( 'Design in the block editor, add logic and payments, and follow every lead from view to entry.', 'fieldwren' ) . '

'; $pills = array( array( 'indigo', sprintf( /* translators: %d */ __( '%d templates', 'fieldwren' ), $tpl_count ) ), array( 'sky', sprintf( /* translators: %d */ __( '%d form blocks', 'fieldwren' ), $fields ) ), array( 'violet', __( 'Conditional logic', 'fieldwren' ) ), array( 'amber', __( 'Multi-step', 'fieldwren' ) ), array( 'emerald', sprintf( /* translators: %d */ __( '%d integrations', 'fieldwren' ), $ints ) ), array( 'rose', __( 'Payments', 'fieldwren' ) ), ); echo '
'; foreach ( $pills as $p ) { echo '' . esc_html( $p[1] ) . ''; } echo '
'; // phpcs:ignore echo '
'; // phpcs:ignore /* Stats */ $stats = array( array( 'indigo', 'form', __( 'Published forms', 'fieldwren' ), number_format_i18n( $forms ), '' ), array( 'sky', 'eye', __( 'Views · 30 days', 'fieldwren' ), number_format_i18n( $now['views'] ), self::delta( $now['views'], $prev['views'] ) ), array( 'emerald', 'inbox', __( 'Submissions · 30 days', 'fieldwren' ), number_format_i18n( $now['submissions'] ), self::delta( $now['submissions'], $prev['submissions'] ) ), array( 'violet', 'target', __( 'Conversion rate', 'fieldwren' ), $now['views'] ? $conv . '%' : '—', self::delta( $conv, $conv_prev ) ), array( 'rose', 'bell', __( 'Unread entries', 'fieldwren' ), number_format_i18n( $unread ), '' . esc_html__( 'Review', 'fieldwren' ) . ' →' ), ); echo '
'; foreach ( $stats as $s ) { echo '
' . self::icon( $s[1], 20 ) . '
' . esc_html( $s[2] ) . '' . esc_html( $s[3] ) . '' . $s[4] . '
'; // phpcs:ignore } echo '
'; /* Chart + checklist */ echo '

' . esc_html__( 'Performance', 'fieldwren' ) . '

' . esc_html__( 'Views', 'fieldwren' ) . ' ' . esc_html__( 'Submissions', 'fieldwren' ) . '' . esc_html__( 'Full report', 'fieldwren' ) . ' →
'; self::area_chart( array_slice( $series, 30, 30, true ) ); echo '
'; $s_all = Settings::all(); $has_cap = 'none' !== ( $s_all['captcha'] ?? 'none' ); $embedded = (bool) $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type NOT IN ('fwren_form','revision') AND (post_content LIKE '%wp:fwren/form%' OR post_content LIKE '%[fieldwren_form%') LIMIT 1" ); // phpcs:ignore $feeds = (bool) $wpdb->get_var( "SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = '_fwren_feeds' AND meta_value NOT IN ('', '[]') LIMIT 1" ); // phpcs:ignore $styled = false !== get_option( 'fwren_bc_kit' ); $steps = array( array( $forms > 0, __( 'Create your first form', 'fieldwren' ), $new_url ), array( $embedded, __( 'Add it to a page', 'fieldwren' ), admin_url( 'edit.php?post_type=page' ) ), array( $has_cap, __( 'Turn on spam protection', 'fieldwren' ), admin_url( 'admin.php?page=fwren-settings&tab=captcha' ) ), array( $styled, __( 'Set your brand colours & fonts', 'fieldwren' ), admin_url( 'admin.php?page=fwren-global-styles' ) ), array( $feeds, __( 'Connect an integration', 'fieldwren' ), admin_url( 'admin.php?page=fwren-settings&tab=integrations' ) ), array( $total > 0, __( 'Receive your first entry', 'fieldwren' ), admin_url( 'admin.php?page=fwren-entries' ) ), ); $done = count( array_filter( wp_list_pluck( $steps, 0 ) ) ); $pct = (int) round( $done / count( $steps ) * 100 ); echo '

' . esc_html__( 'Getting started', 'fieldwren' ) . '

' . esc_html( $done . '/' . count( $steps ) ) . '
    '; foreach ( $steps as $st ) { echo '
  • ' . ( $st[0] ? self::icon( 'check', 14 ) : '' ) . '' . esc_html( $st[1] ) . '
  • '; // phpcs:ignore } echo '
'; /* Top forms + recent entries */ $top = array(); foreach ( Embed::form_options() as $id => $title ) { if ( ! $id ) { continue; } $t = Analytics::totals( (int) $id, 30 ); $top[] = array( (int) $id, get_the_title( $id ), $t['views'], $t['submissions'], Entries::count( (int) $id ) ); } usort( $top, static function ( $a, $b ) { return $b[3] <=> $a[3] ?: $b[4] <=> $a[4] ?: $b[2] <=> $a[2]; } ); $top = array_slice( $top, 0, 6 ); echo '

' . esc_html__( 'Top forms', 'fieldwren' ) . '

' . esc_html__( 'All forms', 'fieldwren' ) . ' →
'; if ( ! $top ) { echo '
' . esc_html__( 'No forms yet.', 'fieldwren' ) . ' ' . esc_html__( 'Create one', 'fieldwren' ) . '
'; } else { echo ''; foreach ( $top as $f ) { $c = $f[2] ? min( 100, round( $f[3] / $f[2] * 100, 1 ) ) : 0; echo ''; } echo '
' . esc_html__( 'Form', 'fieldwren' ) . '' . esc_html__( 'Views', 'fieldwren' ) . '' . esc_html__( 'Entries', 'fieldwren' ) . '' . esc_html__( 'Conversion', 'fieldwren' ) . '
' . esc_html( $f[1] ? $f[1] : '#' . $f[0] ) . '' . (int) $f[2] . '' . (int) $f[4] . '' . esc_html( $f[2] ? $c . '%' : '—' ) . '
'; } echo '
'; list( $rows ) = Entries::query( array( 'per_page' => 6 ) ); echo '

' . esc_html__( 'Recent entries', 'fieldwren' ) . '

' . esc_html__( 'View all', 'fieldwren' ) . ' →
'; if ( ! $rows ) { echo '
' . esc_html__( 'Entries show up here as soon as someone submits a form.', 'fieldwren' ) . '
'; } echo '
    '; $tones = array( 'indigo', 'sky', 'emerald', 'violet', 'amber', 'rose' ); foreach ( $rows as $r ) { $name = ''; $mail = ''; foreach ( $r['response'] as $k => $v ) { if ( ! $mail && is_string( $v ) && is_email( $v ) ) { $mail = $v; } if ( ! $name && 'names' === $k ) { $name = is_array( $v ) ? trim( implode( ' ', array_filter( array_map( 'strval', $v ) ) ) ) : (string) $v; } } $who = $name ? $name : ( $mail ? $mail : sprintf( /* translators: %d */ __( 'Entry #%d', 'fieldwren' ), $r['serial'] ) ); $initial = strtoupper( function_exists( 'mb_substr' ) ? mb_substr( $who, 0, 1 ) : substr( $who, 0, 1 ) ); $ago = human_time_diff( strtotime( $r['created_at'] . ' UTC' ), time() ); echo '
  • ' . esc_html( $initial ) . '' . esc_html( $who ) . '' . esc_html( get_the_title( $r['form_id'] ) ) . ( $mail && $name ? ' · ' . esc_html( $mail ) : '' ) . '' . esc_html( sprintf( /* translators: %s time */ __( '%s ago', 'fieldwren' ), $ago ) ) . '
  • '; } echo '
'; /* Quick actions */ $quick = array( array( 'plus', 'indigo', __( 'New form', 'fieldwren' ), __( 'Start blank in the block editor', 'fieldwren' ), $new_url ), array( 'layout', 'sky', __( 'Templates', 'fieldwren' ), sprintf( /* translators: %d */ __( '%d ready-made forms', 'fieldwren' ), $tpl_count ), $tpl_url ), array( 'inbox', 'emerald', __( 'Entries', 'fieldwren' ), __( 'Search, export and follow up', 'fieldwren' ), admin_url( 'admin.php?page=fwren-entries' ) ), array( 'chart', 'violet', __( 'Reports', 'fieldwren' ), __( 'Funnels and survey answers', 'fieldwren' ), admin_url( 'admin.php?page=fwren-reports' ) ), array( 'palette', 'amber', __( 'Global styles', 'fieldwren' ), __( 'Brand colours and fonts', 'fieldwren' ), admin_url( 'admin.php?page=fwren-global-styles' ) ), array( 'plug', 'rose', __( 'Integrations', 'fieldwren' ), __( 'CRMs, email and webhooks', 'fieldwren' ), admin_url( 'admin.php?page=fwren-settings&tab=integrations' ) ), ); echo '
'; foreach ( $quick as $q ) { echo '' . self::icon( $q[0], 20 ) . '' . esc_html( $q[2] ) . '' . esc_html( $q[3] ) . ''; // phpcs:ignore } echo '
'; /* Popular templates */ $pick = array( 'quote', 'multistep-brief', 'appointment', 'quiz-plan-finder' ); $cats = Templates::categories(); echo '

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

' . esc_html__( 'Browse all', 'fieldwren' ) . ' →
'; foreach ( $pick as $i => $id ) { $t = Templates::get( $id ); if ( ! $t ) { continue; } $use = wp_nonce_url( admin_url( 'admin-post.php?action=fwren_use_template&tpl=' . rawurlencode( $id ) ), 'fwren_use_template' ); echo '
' . esc_html( $cats[ $t['category'] ] ?? '' ) . '' . esc_html( $t['title'] ) . '

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

' . esc_html__( 'Preview', 'fieldwren' ) . '' . esc_html__( 'Use', 'fieldwren' ) . ' →
'; } echo '
'; self::close(); } /** * Smooth area chart (views area + submissions line) for a day => [views, submissions] series. * * @param array $series Series. */ public static function area_chart( array $series ) { $w = 640; $h = 200; $pad = 6; $n = max( 2, count( $series ) ); $max = 1; foreach ( $series as $d ) { $max = max( $max, $d['views'], $d['submissions'] ); } $max = $max * 1.15; $pts = static function ( $key ) use ( $series, $w, $h, $pad, $n, $max ) { $out = array(); $i = 0; foreach ( $series as $d ) { $out[] = array( round( $i / ( $n - 1 ) * $w, 2 ), round( $h - $pad - $d[ $key ] / $max * ( $h - 2 * $pad ), 2 ) ); $i++; } return $out; }; $path = static function ( array $p ) { $d = 'M' . $p[0][0] . ' ' . $p[0][1]; for ( $i = 1, $c = count( $p ); $i < $c; $i++ ) { $mx = ( $p[ $i - 1 ][0] + $p[ $i ][0] ) / 2; $d .= ' C' . $mx . ' ' . $p[ $i - 1 ][1] . ' ' . $mx . ' ' . $p[ $i ][1] . ' ' . $p[ $i ][0] . ' ' . $p[ $i ][1]; } return $d; }; $v = $pts( 'views' ); $s = $pts( 'submissions' ); $area = $path( $v ) . ' L' . $w . ' ' . $h . ' L0 ' . $h . ' Z'; $labels = array_keys( $series ); echo '
'; for ( $g = 1; $g <= 3; $g++ ) { echo ''; } echo ''; echo '
' . esc_html( $labels ? wp_date( 'M j', strtotime( reset( $labels ) ) ) : '' ) . '' . esc_html( $labels ? wp_date( 'M j', strtotime( $labels[ (int) floor( count( $labels ) / 2 ) ] ) ) : '' ) . '' . esc_html__( 'Today', 'fieldwren' ) . '
'; } }