# contact-forms/trunk/admin/dashboard-widget.php

Contact Forms by Cimatti, version trunk. 81 lines.

- Page: https://pluginprobe.com/plugins/contact-forms/trunk/code/admin/dashboard-widget.php
- Raw: https://pluginprobe.com/plugins/contact-forms/trunk/raw/admin/dashboard-widget.php
- Modified: 2026-07-13T10:04:36+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/contact-forms/trunk/code/admin/dashboard-widget.php#L10-L20`.

```php
<?php
/**
 * Contact Forms dashboard widget.
 *
 * Loaded on demand from accua_contact_forms_dashboard_add_widgets() in
 * contact-forms.php, so this file is only included on the WordPress
 * dashboard screen and only for users who can see the widget.
 */

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Get the statistics shown in the dashboard widget.
 *
 * Results are cached in a transient for 5 minutes so repeated dashboard
 * loads do not run COUNT queries against the submissions tables.
 *
 * @return array{active_forms:int, pages:int, submissions:int, emails:int}
 */
function accua_contact_forms_dashboard_get_stats()
{
  $stats = get_transient('accua_forms_dashboard_stats');
  if (is_array($stats)) {
    return $stats;
  }

  global $wpdb;

  // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom tables; results cached in a transient.
  $stats = array(
    'active_forms' => count(get_option('accua_forms_saved_forms', array())),
    'pages' => (int) $wpdb->get_var("SELECT COUNT(DISTINCT afs_uri) FROM `{$wpdb->prefix}accua_forms_submissions`"),
    'submissions' => (int) $wpdb->get_var("SELECT COUNT(*) FROM `{$wpdb->prefix}accua_forms_submissions` WHERE afs_status >= 0"),
    'emails' => (int) $wpdb->get_var("SELECT COUNT(DISTINCT afsv_value) FROM `{$wpdb->prefix}accua_forms_submissions_values` WHERE afsv_type = 'autoreply_email'"),
  );
  // phpcs:enable

  set_transient('accua_forms_dashboard_stats', $stats, 5 * MINUTE_IN_SECONDS);

  return $stats;
}

/**
 * Dashboard widget handler for Contact Forms stats.
 *
 * Shows form statistics including active forms, pages, submissions, and distinct emails.
 * Only accessible to users with the 'manage_options' capability.
 */
function accua_contact_forms_dashboard_widget_news_handler()
{
  if (!current_user_can('manage_options')) {
    return;
  }

  $stats = accua_contact_forms_dashboard_get_stats();
  $rows = array(
    array($stats['active_forms'], __('Active forms', 'contact-forms')),
    array($stats['pages'], __('Pages', 'contact-forms')),
    array($stats['submissions'], __('Submissions', 'contact-forms')),
    array($stats['emails'], __('Distinct Emails', 'contact-forms')),
  );

  $plugin_data = get_plugin_data(ACCUA_FORMS_FILE, false, false); ?>
  <table>
    <?php foreach ($rows as $row) : ?>
      <tr class="first">
        <td class="first b"><?php echo esc_html(number_format_i18n($row[0])); ?></td>
        <td class="t"><?php echo esc_html($row[1]); ?></td>
      </tr>
    <?php endforeach; ?>
  </table>
  <br />
  <span id="accua-forms-version"><a href="<?php echo esc_url('https://www.cimatti.it/en/wordpress-plugins/contact-forms/'); ?>">
      <?php
      esc_html_e('Version', 'contact-forms');
      echo " " . esc_html($plugin_data['Version']); ?></a> | <a href="<?php echo esc_url(admin_url('admin.php?page=accua_forms')); ?>"><?php esc_html_e('Dashboard', 'contact-forms'); ?></a>
  </span>

<?php
}

```
