# contact-forms/2.3.5/admin/dashboard-page.php

Contact Forms by Cimatti, version 2.3.5. 798 lines.

- Page: https://pluginprobe.com/plugins/contact-forms/2.3.5/code/admin/dashboard-page.php
- Raw: https://pluginprobe.com/plugins/contact-forms/2.3.5/raw/admin/dashboard-page.php
- Modified: 2026-08-05T09:37:24+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/2.3.5/code/admin/dashboard-page.php#L10-L20`.

```php
<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Render a link with expandable text when it exceeds 80 characters.
 */
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Internal helper with intentional underscore prefix
function _accua_forms_dashboard_expandable_link( $text, $url ) {
    return '<a href="' . esc_url( $url ) . '" target="_blank" title="' . esc_attr( $text ) . '">' . esc_html( $text ) . '</a>';
}

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Internal helper function with underscore prefix
function _accua_forms_dashboard_page(){
  /* global vars */
  global $hook_suffix;
  /* enable add_meta_boxes function in this page. */
  do_action( 'add_meta_boxes', $hook_suffix, 10, 2 ); ?>

  <div class="fx-settings-meta-box-wrap">
    <div id="postbox-container-1" class="postbox-container">
    </div><!-- .fx-settings-meta-box-wrap -->
  </div>
  <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
  <?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );

  // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Dashboard statistics queries, dynamic field filtering
  global $wpdb;
  $email_fields = array();
  $num_submissions = $wpdb->get_var("SELECT count(*) as num_row
      FROM {$wpdb->prefix}accua_forms_submissions WHERE afs_status >= 0");
  if ($num_submissions) {
    $num_posts = $wpdb->get_var("SELECT count(DISTINCT afs_post_id ) as num_row
      FROM {$wpdb->prefix}accua_forms_submissions WHERE afs_post_id <> 0 AND afs_status >= 0");
    $num_forms = $wpdb->get_var("SELECT count(DISTINCT afs_form_id ) as num_row
      FROM {$wpdb->prefix}accua_forms_submissions WHERE afs_status >= 0");
    $num_submissions_per_lead_status = $wpdb->get_results("SELECT afs_lead_status AS status, COUNT(*) AS n
                   FROM `{$wpdb->prefix}accua_forms_submissions`
                   WHERE afs_status >= 0
                   GROUP BY afs_lead_status", OBJECT_K);

    //seleziono i campi email
    $avail_fields = get_option('accua_forms_avail_fields', array());
    foreach ($avail_fields as $key => $single_field) {
      if ($single_field['type'] == 'email' || $single_field['type'] == 'autoreply_email') {
        $email_fields[] = $wpdb->prepare('%s', $key);
      }
    }
    if ($email_fields) {
      $email_fields_string = implode(',', $email_fields);
      $num_emails = $wpdb->get_var("SELECT COUNT(DISTINCT (`afsv_value`)) as num_row
      FROM {$wpdb->prefix}accua_forms_submissions, {$wpdb->prefix}accua_forms_submissions_values
      WHERE `afs_id` = `afsv_sub_id`
      AND afsv_field_id IN ({$email_fields_string})
      AND afs_status >= 0");
    } else {
      $num_emails = 0;
    }
  }


  $forms_data = get_option('accua_forms_saved_forms', array());

  $all_public_post_types = function_exists('accua_forms_get_public_post_types') ? accua_forms_get_public_post_types() : array();
  $submitted_posts = $wpdb->get_results("SELECT DISTINCT s.afs_post_id AS post_id, p.post_type, p.post_title
    FROM {$wpdb->prefix}accua_forms_submissions s
    INNER JOIN {$wpdb->posts} p ON p.ID = s.afs_post_id
    WHERE s.afs_post_id > 0
    AND s.afs_status >= 0
    AND p.post_type <> 'attachment'
    ORDER BY p.post_type ASC, p.post_title ASC, s.afs_post_id DESC", ARRAY_A);

  $submitted_post_types = array();
  foreach ($submitted_posts as $submitted_post) {
    if (isset($all_public_post_types[$submitted_post['post_type']])) {
      $submitted_post_types[$submitted_post['post_type']] = $all_public_post_types[$submitted_post['post_type']];
    }
  }

  $filter_post_type = '';
  // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
  if (isset($_GET['ptype'])) {
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
    $requested_post_type = sanitize_key(wp_unslash($_GET['ptype']));
    if (isset($submitted_post_types[$requested_post_type])) {
      $filter_post_type = $requested_post_type;
    }
  }

  $id_posts = array();
  foreach ($submitted_posts as $submitted_post) {
    if ($filter_post_type !== '' && $submitted_post['post_type'] !== $filter_post_type) {
      continue;
    }
    $id_posts[] = $submitted_post;
  }

  $ptype_param = '';
  if ($filter_post_type !== '') {
    $ptype_param = $wpdb->prepare("AND EXISTS (
      SELECT 1
      FROM {$wpdb->posts} p_filter
      WHERE p_filter.ID = afs_post_id
      AND p_filter.post_type = %s
    )", $filter_post_type);
  }

  $fid = '';
  $fid_param = '';
  // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
  if (isset($_GET['fid'])) {
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
    $fid = sanitize_text_field(wp_unslash($_GET['fid']));
    if (isset($forms_data[$fid])) {
      $fid_param = $wpdb->prepare('AND afs_form_id = %s', $fid);
    } else {
      $fid = '';
    }
  }

  $filter_post = -1;
  $pid_param = '';
  // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
  if (isset($_GET['pid'])) {
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
    $filter_post = (int) $_GET['pid'];
    if ($filter_post > 0 && get_post($filter_post)) {
      if ($filter_post_type !== '' && get_post_type($filter_post) !== $filter_post_type) {
        $filter_post = -1;
      }
    }
    if ($filter_post > 0) {
      $pid_param = $wpdb->prepare('AND afs_post_id = %d', $filter_post);
    } else {
      $filter_post = -1;
    }
  }


  // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
  if (isset($_GET['period'])) {
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
    $period = (int) $_GET['period'];
    if($period <= 0 && $period != -1){
      $period = 24; //default
    }
  } else{
    $period = 24; //default
  }

  $period2 = 24;
  // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
  if (isset($_GET['period2'])) {
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filter for dashboard view
    $period2 = (int) $_GET['period2'];
    if ($period2 <= 0 && $period2 != -1) {
      $period2 = 24; //default
    }
  }

  $label_my = array();
  $submissions = array();
  $unique_submissions = array();
  $origin_label_my = array();

  if ($email_fields) {
    //costruisco il grafico
    $query_grafico = $wpdb->get_results("SELECT YEAR( afs_submitted ) AS `year` , MONTH( afs_submitted ) AS `month` , COUNT( DISTINCT (`afs_id`) ) AS `submissions` , COUNT( DISTINCT (`afsv_value`)) AS `unique_submissions`
    FROM `{$wpdb->prefix}accua_forms_submissions`
      LEFT JOIN `{$wpdb->prefix}accua_forms_submissions_values` ON `afs_id` = `afsv_sub_id`
    WHERE afsv_field_id IN ({$email_fields_string})
    AND afs_status >= 0
    $fid_param
    $ptype_param
    $pid_param
    GROUP BY `year` , `month`
    ORDER BY `year` DESC , `month` DESC");

    if ($query_grafico) {
      /* cicliamo per ogni mese in modo da riempire con 0 i mesi senza compilazioni */
      for ($i = (int) wp_date('Y'); $i >= $query_grafico[count($query_grafico) - 1]->year; $i--) {
        $start_month = 12;
        $end_month = 1;
        if ($i == (int) wp_date('Y')) $start_month = (int) wp_date('n');
        if ($i == $query_grafico[count($query_grafico) - 1]->year) $end_month = $query_grafico[count($query_grafico) - 1]->month;
        for ($j = $start_month; $j >= $end_month; $j--) {
          $find = false;
          foreach ($query_grafico as $result) {
            if (!$find && (int)$result->year == $i && (int)$result->month == $j) {
              $label_my[] = $j . '/' . $i;
              $submissions[] = (int)$result->submissions;
              $unique_submissions[] = (int)$result->unique_submissions;
              $find = true;
            }
          }
          if (!$find) {
            $label_my[] = $j . '/' . $i;
            $submissions[] = 0;
            $unique_submissions[] = 0;

          }
        }
      }
      $origin_label_my = $label_my;
      $origin_submissions = $submissions;
      $origin_unique_submissions = $unique_submissions;

      if ($period != -1) {
        $label_my = array_slice($label_my, 0, $period);
        $submissions = array_slice($submissions, 0, $period);
        $unique_submissions = array_slice($unique_submissions, 0, $period);
      }

      $label_my = array_reverse($label_my);
      $submissions = array_reverse($submissions);
      $unique_submissions = array_reverse($unique_submissions);
    }
  }

  // Second chart: submissions and distinct pages per month (no form/page filter)
  $label_my2 = array();
  $submissions2 = array();
  $unique_posts2 = array();
  $origin_label_my2 = array();
  $origin_submissions2 = array();
  $origin_unique_posts2 = array();

  $query_grafico2 = $wpdb->get_results("SELECT YEAR(afs_submitted) AS `year`, MONTH(afs_submitted) AS `month`, COUNT(DISTINCT afs_id) AS `submissions`, COUNT(DISTINCT afs_post_id) AS `unique_posts`
    FROM `{$wpdb->prefix}accua_forms_submissions`
    WHERE afs_status >= 0 AND afs_post_id > 0
    $ptype_param
    $pid_param
    GROUP BY `year`, `month`
    ORDER BY `year` DESC, `month` DESC");

  if ($query_grafico2) {
    for ($i = (int) wp_date('Y'); $i >= $query_grafico2[count($query_grafico2) - 1]->year; $i--) {
      $start_month2 = 12;
      $end_month2 = 1;
      if ($i == (int) wp_date('Y')) $start_month2 = (int) wp_date('n');
      if ($i == $query_grafico2[count($query_grafico2) - 1]->year) $end_month2 = $query_grafico2[count($query_grafico2) - 1]->month;
      for ($j = $start_month2; $j >= $end_month2; $j--) {
        $find2 = false;
        foreach ($query_grafico2 as $result) {
          if (!$find2 && (int)$result->year == $i && (int)$result->month == $j) {
            $label_my2[] = $j . '/' . $i;
            $submissions2[] = (int)$result->submissions;
            $unique_posts2[] = (int)$result->unique_posts;
            $find2 = true;
          }
        }
        if (!$find2) {
          $label_my2[] = $j . '/' . $i;
          $submissions2[] = 0;
          $unique_posts2[] = 0;
        }
      }
    }
    $origin_label_my2 = $label_my2;
    $origin_submissions2 = $submissions2;
    $origin_unique_posts2 = $unique_posts2;

    if ($period2 != -1) {
      $label_my2 = array_slice($label_my2, 0, $period2);
      $submissions2 = array_slice($submissions2, 0, $period2);
      $unique_posts2 = array_slice($unique_posts2, 0, $period2);
    }

    $label_my2 = array_reverse($label_my2);
    $submissions2 = array_reverse($submissions2);
    $unique_posts2 = array_reverse($unique_posts2);
  } ?>

  <div id="accua_dashboard_page" class="accua_forms_admin_page wrap">
    <h1><?php esc_html_e('Contact Forms - Dashboard', 'contact-forms'); ?></h1>
    <div class="metabox-holder">
      <div class="postbox">
        <div class="postbox-header"><h2><?php esc_html_e('Monthly number of forms submitted', 'contact-forms'); ?></h2></div>
        <div class="inside" id="dashboard_right_now">

          <form method="get">
            <input type="hidden" name="page" value="accua_forms" />

            <label><?php esc_html_e('Period: ', 'contact-forms'); ?></label>
            <select name="period">
              <option value='3' <?php if($period == 3){ echo 'selected="selected"'; } ?>><?php esc_html_e('last', 'contact-forms'); ?> 3 <?php esc_html_e('months', 'contact-forms'); ?></option>
              <option value='6' <?php if($period == 6){ echo 'selected="selected"'; } ?>><?php esc_html_e('last', 'contact-forms'); ?> 6 <?php esc_html_e('months', 'contact-forms'); ?></option>
              <option value='12' <?php if($period == 12){ echo 'selected="selected"'; } ?>><?php esc_html_e('last', 'contact-forms'); ?> 12 <?php esc_html_e('months', 'contact-forms'); ?></option>
              <option value='24' <?php if($period == 24 || !$period ){ echo 'selected="selected"'; } ?>><?php esc_html_e('last', 'contact-forms'); ?> 24 <?php esc_html_e('months', 'contact-forms'); ?></option>
              <option value='-1' <?php if($period == -1 ){ echo 'selected="selected"'; } ?>><?php esc_html_e('All', 'contact-forms'); ?></option>
            </select>

            <label><?php esc_html_e('Form: ', 'contact-forms'); ?></label>
            <select name="fid">
              <?php
              $selected = ($fid === '') ? 'selected="selected"' : '';
              echo '<option value="" ' . esc_attr($selected) . '>' . esc_html__('Submissions from all forms', 'contact-forms') . '</option>';
              foreach($forms_data as $ffid => $form) {
                $selected = ($fid == $ffid) ? 'selected="selected"' : '';
                $title = isset($form['title']) ? trim($form['title']) : '';
                if ($title === '') {
                  $title = $ffid;
                }
                echo '<option value="' . esc_attr($ffid) . '" ' . esc_attr($selected) . '>' . esc_html($title) . '</option>';
              }
              ?>
            </select> 

            <?php if ($period2 != 24) { ?>
              <input type="hidden" name="period2" value="<?php echo esc_attr($period2); ?>" />
            <?php } ?>
            <input type="submit" class="button button-secondary" value="<?php esc_attr_e('Apply', 'contact-forms') ?>" />
          </form>


          <form method="get" action="" style="margin-bottom:70px;">
            <div class="div-inside-form-slider">
              <div>
                <input type="range" min="1" max="<?php echo count($origin_label_my); ?>" value="24" class="slider" name="slide_period" id="slide_period" step="1">
                <p>Last <span id="slide_period_value" class="span-value"></span>  months</p>
              </div>
            </div>
          </form>

          <?php
          if (!empty($query_grafico)) {

            echo "<div class='fixed-height-chart' style='height: 350px'>";
            echo '<canvas id="ContactChart"></canvas>';
            echo "</div>";

            $script_grafico = "
             <script> 
             jQuery(document).ready(function(){
             /* slider */
                 var slider_periodo = document.getElementById('slide_period');
                 var output_slider_periodo = document.getElementById('slide_period_value');
                 output_slider_periodo.innerHTML = slider_periodo.value;
                 
                  var labels = " . _accua_forms_json_encode($label_my) . ";  
                  const dataa = {
                    labels: labels,
                    datasets: [
                        {
                          label: 'Submissions', 
                          data: " . _accua_forms_json_encode($submissions) . ",
                          backgroundColor: 'rgba(0, 0, 0, 0.1)',                
                        borderWidth: 1,
                        },
                        {
                          label: 'Unique submissions',
                          data: " . _accua_forms_json_encode($unique_submissions) . ",
                          backgroundColor: 'rgba(255, 114, 5, 1)',                  
                          borderWidth: 1,
                          xAxisID: 'axis1',
                        }              
                    ]
                  };  
          
         const config = {
            type: 'bar',
            data: dataa,                 
            options: { 
              responsive: true,
              scales:{
                x: { 
                    ticks:{ 
                        display: false
                    },
                    grid: {
                        offset:false
                    }
                } 
              },
              maintainAspectRatio: false, //per avere altezza fissa
              plugins: {
                title: {
                  display: false,
                },
              },
              interaction: {
                intersect: false,
              }
            }
          };
          
          var ctx = document.getElementById('ContactChart').getContext('2d');
          var myChart = new Chart(ctx, config);
          //console.log('chart initialized', myChart);
           
           function update_mychart_submissions(item, index) {
                myChart.data.datasets[0].data[index] = item; 
           }
           function update_mychart_unique_submissions(item, index) {
                myChart.data.datasets[1].data[index] = item; 
           }
           
           slider_periodo.oninput = function() {                    
                slider_periodo_val = this.value;
                output_slider_periodo.innerHTML = slider_periodo_val;
                
                /* mi devo passare gli array nel js e tagliarli a seconda del punto in cui sono nello slider */
                label = " . _accua_forms_json_encode($origin_label_my) . ";    
                submissions = " . _accua_forms_json_encode($origin_submissions) . ";
                unique_submission = " . _accua_forms_json_encode($origin_unique_submissions) . ";
                
                label = label.slice(0, slider_periodo_val).reverse();
                submissions = submissions.slice(0, slider_periodo_val).reverse();
                unique_submission = unique_submission.slice(0, slider_periodo_val).reverse();
                
                submissions.forEach(update_mychart_submissions);
                unique_submission.forEach(update_mychart_unique_submissions);
                myChart.data.labels = label
                myChart.update();            
           } 
           
           });
          </script>
          ";
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Chart.js configuration with pre-escaped values
            echo $script_grafico;
          } else {
            echo '<div style="height: 350px"><p>', esc_html__('No forms submitted', 'contact-forms'), '</p></div>';
          } ?>
        </div>

      </div>


      <div class="postbox">
        <div class="postbox-header"><h2><?php esc_html_e('Monthly submissions by page', 'contact-forms'); ?></h2></div>
        <div class="inside" id="dashboard_right_now_posts">

          <form method="get">
            <input type="hidden" name="page" value="accua_forms" />
            <?php if ($fid !== '') echo '<input type="hidden" name="fid" value="' . esc_attr($fid) . '" />'; ?>
            <?php if ($period != 24) echo '<input type="hidden" name="period" value="' . esc_attr($period) . '" />'; ?>

            <label><?php esc_html_e('Period: ', 'contact-forms'); ?></label>
            <select name="period2">
              <option value='3' <?php if($period2 == 3){ echo 'selected="selected"'; } ?>><?php esc_html_e('last', 'contact-forms'); ?> 3 <?php esc_html_e('months', 'contact-forms'); ?></option>
              <option value='6' <?php if($period2 == 6){ echo 'selected="selected"'; } ?>><?php esc_html_e('last', 'contact-forms'); ?> 6 <?php esc_html_e('months', 'contact-forms'); ?></option>
              <option value='12' <?php if($period2 == 12){ echo 'selected="selected"'; } ?>><?php esc_html_e('last', 'contact-forms'); ?> 12 <?php esc_html_e('months', 'contact-forms'); ?></option>
              <option value='24' <?php if($period2 == 24 || !$period2 ){ echo 'selected="selected"'; } ?>><?php esc_html_e('last', 'contact-forms'); ?> 24 <?php esc_html_e('months', 'contact-forms'); ?></option>
              <option value='-1' <?php if($period2 == -1 ){ echo 'selected="selected"'; } ?>><?php esc_html_e('All', 'contact-forms'); ?></option>
            </select>

            <label><?php esc_html_e('Content type: ', 'contact-forms'); ?></label>
            <select name="ptype" id="accua-post-type-filter-2">
              <option value=""><?php esc_html_e('Show all content types', 'contact-forms'); ?></option>
              <?php foreach ($submitted_post_types as $post_type_slug => $post_type_label) { ?>
                <option value="<?php echo esc_attr($post_type_slug); ?>" <?php selected($filter_post_type, $post_type_slug); ?>><?php echo esc_html($post_type_label); ?></option>
              <?php } ?>
            </select>

            <label><?php esc_html_e('Content: ', 'contact-forms'); ?></label>
            <select name="pid" id="accua-post-filter-2">
              <option value="-1" <?php selected($filter_post, -1); ?>><?php esc_html_e('Show all contents', 'contact-forms'); ?></option>
              <?php foreach ($submitted_posts as $submitted_post) {
                $post_type_label = isset($submitted_post_types[$submitted_post['post_type']]) ? $submitted_post_types[$submitted_post['post_type']] : $submitted_post['post_type'];
                $post_title = $submitted_post['post_title'] !== '' ? $submitted_post['post_title'] : '#' . $submitted_post['post_id'];
                $option_label = '[' . $post_type_label . '] ' . $post_title;
                ?>
                <option value="<?php echo esc_attr($submitted_post['post_id']); ?>" data-post-type="<?php echo esc_attr($submitted_post['post_type']); ?>" <?php selected($filter_post, (int) $submitted_post['post_id']); ?>><?php echo esc_html($option_label); ?></option>
              <?php } ?>
            </select>

            <input type="submit" class="button button-secondary" value="<?php esc_attr_e('Apply', 'contact-forms') ?>" />
          </form>

          <form method="get" action="" style="margin-bottom:70px;">
            <div class="div-inside-form-slider">
              <div>
                <input type="range" min="1" max="<?php echo !empty($origin_label_my2) ? count($origin_label_my2) : 24; ?>" value="24" class="slider" name="slide_period2" id="slide_period2" step="1">
                <p>Last <span id="slide_period2_value" class="span-value"></span> months</p>
              </div>
            </div>
          </form>

          <?php
          if (!empty($query_grafico2)) {

            echo "<div class='fixed-height-chart' style='height: 350px'>";
            echo '<canvas id="ContactChart2"></canvas>';
            echo "</div>";

            $script_grafico2 = "
             <script>
             jQuery(document).ready(function(){
             /* slider */
                 var slider_periodo2 = document.getElementById('slide_period2');
                 var output_slider_periodo2 = document.getElementById('slide_period2_value');
                 output_slider_periodo2.innerHTML = slider_periodo2.value;
                 
                  var labels2 = " . _accua_forms_json_encode($label_my2) . ";
                  const dataa2 = {
                    labels: labels2,
                    datasets: [
                        {
                          label: 'Submissions',
                          data: " . _accua_forms_json_encode($submissions2) . ",
                          backgroundColor: 'rgba(21, 202, 255, 0.35)',
                          borderColor: 'rgba(21, 202, 255, 1)',
                          borderWidth: 1,
                        },
                        {
                          label: 'Unique pages',
                          data: " . _accua_forms_json_encode($unique_posts2) . ",
                          backgroundColor: 'rgba(46, 204, 113, 1)',
                          borderColor: 'rgba(46, 204, 113, 1)',
                          borderWidth: 1,
                          xAxisID: 'axis1',
                        }
                    ]
                  };
          
         const config2 = {
            type: 'bar',
            data: dataa2,
            options: {
              responsive: true,
              scales:{
                x: {
                    ticks:{
                        display: false
                    },
                    grid: {
                        offset:false
                    }
                }
              },
              maintainAspectRatio: false,
              plugins: {
                title: {
                  display: false,
                },
              },
              interaction: {
                intersect: false,
              }
            }
          };
          
          var ctx2 = document.getElementById('ContactChart2').getContext('2d');
          var myChart2 = new Chart(ctx2, config2);
           
           function update_mychart2_submissions(item, index) {
                myChart2.data.datasets[0].data[index] = item;
           }
           function update_mychart2_unique_posts(item, index) {
                myChart2.data.datasets[1].data[index] = item;
           }
           
           slider_periodo2.oninput = function() {
                slider_periodo2_val = this.value;
                output_slider_periodo2.innerHTML = slider_periodo2_val;
                
                label2 = " . _accua_forms_json_encode($origin_label_my2) . ";
                subs2 = " . _accua_forms_json_encode($origin_submissions2) . ";
                uposts2 = " . _accua_forms_json_encode($origin_unique_posts2) . ";
                
                label2 = label2.slice(0, slider_periodo2_val).reverse();
                subs2 = subs2.slice(0, slider_periodo2_val).reverse();
                uposts2 = uposts2.slice(0, slider_periodo2_val).reverse();
                
                subs2.forEach(update_mychart2_submissions);
                uposts2.forEach(update_mychart2_unique_posts);
                myChart2.data.labels = label2;
                myChart2.update();
           }
           
           });
          </script>
          ";
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Chart.js configuration with pre-escaped values
            echo $script_grafico2;
            echo '<script>
            jQuery(function($) {
              function bindDependentPostSelect(postTypeSelector, postSelector) {
                var $postType = $(postTypeSelector);
                var $post = $(postSelector);

                if (!$postType.length || !$post.length) {
                  return;
                }

                function updateOptions() {
                  var selectedType = $postType.val();
                  var hasVisibleSelection = false;

                  $post.find("option").each(function() {
                    var $option = $(this);
                    var optionType = $option.data("post-type");
                    var visible = !selectedType || !optionType || optionType === selectedType;

                    $option.prop("hidden", !visible);
                    $option.prop("disabled", !visible);

                    if (visible && $option.is(":selected")) {
                      hasVisibleSelection = true;
                    }
                  });

                  if (!hasVisibleSelection) {
                    $post.val("-1");
                  }
                }

                $postType.on("change", updateOptions);
                updateOptions();
              }

              bindDependentPostSelect("#accua-post-type-filter", "#accua-post-filter");
              bindDependentPostSelect("#accua-post-type-filter-2", "#accua-post-filter-2");
            });
            </script>';
          } else {
            echo '<div style="height: 350px"><p>', esc_html__('No forms submitted', 'contact-forms'), '</p></div>';
          } ?>
        </div>

      </div>


      <div class="postbox">
        <div class="postbox-header"><h2><?php esc_html_e('Last 10 submissions', 'contact-forms'); ?></h2></div>
        <div class="inside">
        <?php
        // Sortable columns
        $allowed_orderby = array(
            'afs_submitted'   => 's.`afs_submitted`',
            'email'           => '`email`',
            'afs_referrer'    => 's.`afs_referrer`',
            'afs_uri'         => 's.`afs_uri`',
            'afs_lead_status' => 's.`afs_lead_status`',
        );
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only sort parameter
        $orderby_raw = isset( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : '';
        $orderby_key = isset( $allowed_orderby[ $orderby_raw ] ) ? $orderby_raw : 'afs_submitted';
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only sort parameter
        $order_raw = isset( $_GET['order'] ) ? sanitize_key( wp_unslash( $_GET['order'] ) ) : 'desc';
        $order = in_array( strtoupper( $order_raw ), array( 'ASC', 'DESC' ), true ) ? strtoupper( $order_raw ) : 'DESC';
        $order_clause = $allowed_orderby[ $orderby_key ] . ' ' . $order;

        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Safe: $order_clause built from allowlist
        $data_last_submissions = $wpdb->get_results(
            "SELECT s.`afs_id`, s.`afs_submitted`, s.`afs_uri`, s.`afs_referrer`, s.`afs_lead_status`,
                    COALESCE(v.`afsv_value`, '') AS `email`
             FROM `{$wpdb->prefix}accua_forms_submissions` s
             LEFT JOIN `{$wpdb->prefix}accua_forms_submissions_values` v
                    ON v.`afsv_sub_id` = s.`afs_id` AND v.`afsv_field_id` = 'email'
             WHERE s.`afs_status` >= 0
             ORDER BY {$order_clause} LIMIT 10"
        );

        if ($data_last_submissions) {
          $base_url = admin_url( 'admin.php?page=accua_forms' );
          $columns = array(
              'afs_submitted'   => __( 'Submitted', 'contact-forms' ),
              'email'           => __( 'Email', 'contact-forms' ),
              'afs_referrer'    => __( 'Referrer', 'contact-forms' ),
              'afs_uri'         => __( 'Page', 'contact-forms' ),
              'afs_lead_status' => __( 'Lead Status', 'contact-forms' ),
          );
          ?>
          <table class="widefat fixed striped accua-last-submissions">
            <colgroup>
              <col style="width:12%;">
              <col style="width:17%;">
              <col style="width:19%;">
              <col style="width:23%;">
              <col style="width:14%;">
              <col style="width:15%;">
            </colgroup>
            <thead>
            <tr>
              <?php foreach ( $columns as $col_key => $col_label ) {
                  $is_current = ( $orderby_key === $col_key );
                  $next_order = ( $is_current && $order === 'ASC' ) ? 'desc' : 'asc';
                  $class = $is_current ? 'sorted ' . strtolower( $order ) : 'sortable desc';
                  $sort_url = add_query_arg( array( 'orderby' => $col_key, 'order' => $next_order ), $base_url );
                  ?>
                  <th scope="col" class="manage-column column-<?php echo esc_attr( $col_key ); ?> <?php echo esc_attr( $class ); ?>">
                    <a href="<?php echo esc_url( $sort_url ); ?>">
                      <span><?php echo esc_html( $col_label ); ?></span>
                      <span class="sorting-indicators">
                        <span class="sorting-indicator asc" aria-hidden="true"></span>
                        <span class="sorting-indicator desc" aria-hidden="true"></span>
                      </span>
                    </a>
                  </th>
              <?php } ?>
              <th scope="col" class="manage-column column-actions"><?php esc_html_e( 'Actions', 'contact-forms' ); ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($data_last_submissions as $row) {
              $view_url = admin_url( 'admin.php?page=accua_forms_submissions_list&sid=' . absint( $row->afs_id ) );
              $trash_url = wp_nonce_url( admin_url( 'admin.php?page=accua_forms_submissions_list&action=delete&submission[]=' . absint( $row->afs_id ) ), 'bulk-submissions' );
              $date = new DateTime( $row->afs_submitted );
              $date_formatted = ( get_locale() === 'it_IT' ) ? $date->format( 'j M Y' ) : $date->format( 'M j, Y' );
              ?>
              <tr>
                <td><?php echo esc_html( $date_formatted ); ?></td>
                <td><?php echo esc_html( $row->email ); ?></td>
                <td class="column-afs_referrer"><?php
                  if ( $row->afs_referrer ) {
                    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Helper returns fully escaped HTML
                    echo _accua_forms_dashboard_expandable_link( $row->afs_referrer, $row->afs_referrer );
                  }
                ?></td>
                <td class="column-afs_uri"><?php
                  if ( $row->afs_uri ) {
                    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Helper returns fully escaped HTML
                    echo _accua_forms_dashboard_expandable_link( $row->afs_uri, home_url( $row->afs_uri ) );
                  }
                ?></td>
                <td class="column-afs_lead_status"><?php
                  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Function returns escaped HTML select element
                  echo accua_forms_select_lead_status( $row->afs_id, $row->afs_lead_status );
                ?></td>
                <td>
                  <a href="<?php echo esc_url( $view_url ); ?>"><?php esc_html_e( 'Open', 'contact-forms' ); ?></a>
                  &nbsp;|&nbsp;
                  <a href="<?php echo esc_url( $trash_url ); ?>" class="submitdelete" onclick="return confirm('<?php echo esc_js( __( 'Are you sure you want to trash this submission?', 'contact-forms' ) ); ?>');"><?php echo esc_html_x( 'Trash', 'row action', 'contact-forms' ); ?></a>
                  &nbsp;|&nbsp;
                  <?php if ( (int) $row->afs_lead_status === -1 ) {
                    $unspam_url = wp_nonce_url( admin_url( 'admin.php?page=accua_forms_submissions_list&lead_status=-1&action=unspam&submission[]=' . absint( $row->afs_id ) ), 'bulk-submissions' );
                    ?>
                    <a href="<?php echo esc_url( $unspam_url ); ?>"><?php esc_html_e( 'Not spam', 'contact-forms' ); ?></a>
                  <?php } else {
                    $spam_url = wp_nonce_url( admin_url( 'admin.php?page=accua_forms_submissions_list&action=spam&submission[]=' . absint( $row->afs_id ) ), 'bulk-submissions' );
                    ?>
                    <a href="<?php echo esc_url( $spam_url ); ?>" class="submitdelete"><?php echo esc_html_x( 'Spam', 'row action', 'contact-forms' ); ?></a>
                  <?php } ?>
                </td>
              </tr>
            <?php } ?>
            </tbody>
          </table>
          <?php
          // "Go to" quick links to the submissions list views (same as the list page's .subsubsub)
          $subs_list_url = admin_url( 'admin.php?page=accua_forms_submissions_list' );
          $spam_n = ! empty( $num_submissions_per_lead_status[-1]->n ) ? absint( $num_submissions_per_lead_status[-1]->n ) : 0;
          // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- No user input
          $trash_n = (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$wpdb->prefix}accua_forms_submissions` WHERE afs_status < 0" );

          $goto_items = array();
          $goto_items[] = '<li><a href="' . esc_url( $subs_list_url ) . '">' . esc_html__( 'Active', 'contact-forms' ) . ' (' . number_format_i18n( max( 0, (int) $num_submissions - $spam_n ) ) . ')</a></li>';
          foreach ( accua_forms_get_lead_statuses() as $ls_id => $ls_label ) {
            $ls_id = (int) $ls_id;
            if ( $ls_id === -1 || empty( $num_submissions_per_lead_status[ $ls_id ]->n ) ) {
              continue;
            }
            $goto_items[] = '<li><a href="' . esc_url( add_query_arg( 'lead_status', $ls_id, $subs_list_url ) ) . '">' . esc_html( $ls_label ) . ' (' . number_format_i18n( absint( $num_submissions_per_lead_status[ $ls_id ]->n ) ) . ')</a></li>';
          }
          $goto_items[] = '<li><a href="' . esc_url( add_query_arg( 'lead_status', '-1', $subs_list_url ) ) . '">' . esc_html__( 'Spam', 'contact-forms' ) . ' (' . number_format_i18n( $spam_n ) . ')</a></li>';
          $goto_items[] = '<li><a href="' . esc_url( add_query_arg( 'del', '1', $subs_list_url ) ) . '">' . esc_html__( 'Trash', 'contact-forms' ) . ' (' . number_format_i18n( $trash_n ) . ')</a></li>';
          ?>
          <ul class="subsubsub">
            <li><?php esc_html_e( 'Go to:', 'contact-forms' ); ?>&nbsp;</li>
            <?php
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Items are escaped above
            echo implode( " |\n", $goto_items );
            ?>
          </ul>
          <div class="clear"></div>
        <?php } ?>
        </div>
      </div>

      <?php if ($num_submissions) { ?>
      <div class="postbox">
        <div class="postbox-header"><h2><?php esc_html_e('Total forms submitted', 'contact-forms'); ?></h2></div>
        <div class="inside">
        <table>
          <tr class="first"><td class="first b"><?php echo esc_html($num_forms); ?></td><td class="t"><?php esc_html_e('Forms', 'contact-forms'); ?></td></tr>
          <tr class="first"><td class="first b"><?php echo esc_html($num_posts); ?></td><td class="t"><?php esc_html_e('Pages', 'contact-forms'); ?></td></tr>
          <tr class="first"><td class="first b"><?php echo esc_html($num_submissions); ?></td><td class="t"><?php esc_html_e('Submissions', 'contact-forms'); ?></td></tr>
          <tr class="first"><td class="first b"><?php echo esc_html($num_emails); ?></td><td class="t"><?php esc_html_e('Distinct Emails', 'contact-forms'); ?></td></tr>
        </table>
        <h3><?php esc_html_e('Submissions per lead status', 'contact-forms'); ?></h3>
        <table>
          <?php
          $lead_statuses = accua_forms_get_lead_statuses();
          foreach ($lead_statuses as $k => $v) {
            if (isset($num_submissions_per_lead_status[$k])) {
              echo '<tr class="first"><td class="first b">'.absint($num_submissions_per_lead_status[$k]->n).'</td><td class="t">'.esc_html($v).'</td></tr>';
            }
          }
          ?>
        </table>
        </div>
      </div>
      <?php } ?>
    </div>
  <?php
  // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
}
```
