| @@ -1,58 +1,88 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentForm\App\Modules; |
| 4 | 4 | |
| 5 | +defined('ABSPATH') or die; | |
| 6 | + | |
| 7 | +use FluentForm\App\Helpers\Helper; | |
| 8 | +use FluentForm\App\Models\Submission; | |
| 9 | + | |
| 5 | 10 | class DashboardWidgetModule |
| 6 | 11 | { |
| 12 | + /** | |
| 13 | + * Constructor - automatically registers cache invalidation hooks | |
| 14 | + */ | |
| 15 | + public function __construct() | |
| 16 | + { | |
| 17 | + $app = \FluentForm\Framework\Foundation\App::getInstance(); | |
| 18 | + | |
| 19 | + // Clear dashboard widget cache when a new submission is created or status changes | |
| 20 | + $app->addAction('fluentform/submission_inserted', function () { | |
| 21 | + delete_transient('fluentform_dashboard_stats_v1'); | |
| 22 | + }); | |
| 23 | + | |
| 24 | + $app->addAction('fluentform/before_submission_status_change', function () { | |
| 25 | + delete_transient('fluentform_dashboard_stats_v1'); | |
| 26 | + }); | |
| 27 | + } | |
| 28 | + | |
| 7 | 29 | public function showStat() |
| 8 | 30 | { |
| 9 | - global $wpdb; | |
| 10 | - $stats = wpFluent()->table('fluentform_submissions') | |
| 11 | - ->select([ | |
| 31 | + // Check cache first to avoid expensive query on every dashboard load | |
| 32 | + $cacheKey = 'fluentform_dashboard_stats_v1'; | |
| 33 | + $stats = get_transient($cacheKey); | |
| 34 | + | |
| 35 | + if (false === $stats) { | |
| 36 | + global $wpdb; | |
| 37 | + | |
| 38 | + $stats = Submission::select([ | |
| 12 | 39 | 'fluentform_forms.title', |
| 13 | 40 | 'fluentform_submissions.form_id', |
| 14 | - wpFluent()->raw('count(' . $wpdb->prefix . 'fluentform_submissions.id) as total'), | |
| 15 | - wpFluent()->raw('max(' . $wpdb->prefix . 'fluentform_submissions.id) as max_id'), | |
| 41 | + wpFluent()->raw('COUNT(' . $wpdb->prefix . 'fluentform_submissions.id) as total'), | |
| 42 | + wpFluent()->raw('MAX(' . $wpdb->prefix . 'fluentform_submissions.id) as max_id'), | |
| 43 | + wpFluent()->raw("SUM(CASE WHEN {$wpdb->prefix}fluentform_submissions.status = 'unread' THEN 1 ELSE 0 END) as unread_count"), | |
| 16 | 44 | ]) |
| 17 | - ->orderBy('max_id', 'DESC') | |
| 18 | - ->groupBy('fluentform_submissions.form_id') | |
| 19 | - ->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_submissions.form_id') | |
| 20 | - ->limit(10) | |
| 21 | - ->get(); | |
| 45 | + ->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_submissions.form_id') | |
| 46 | + ->groupBy('fluentform_submissions.form_id') | |
| 47 | + ->orderBy('max_id', 'DESC') | |
| 48 | + ->limit(10) | |
| 49 | + ->get(); | |
| 22 | 50 | |
| 23 | - if (!$stats) { | |
| 51 | + // Cache for 5 minutes to reduce database load | |
| 52 | + set_transient($cacheKey, $stats, 10 * MINUTE_IN_SECONDS); | |
| 53 | + } | |
| 54 | + | |
| 55 | + if (!$stats || count($stats) === 0) { | |
| 24 | 56 | echo 'You can see your submission stats here'; |
| 25 | 57 | return; |
| 26 | 58 | } |
| 27 | 59 | |
| 28 | - foreach ($stats as $stat) { | |
| 29 | - $stat->unreadCount = $this->getUnreadCount($stat->form_id); | |
| 30 | - } | |
| 31 | - | |
| 32 | 60 | $this->printStats($stats); |
| 33 | - return; | |
| 34 | 61 | } |
| 35 | 62 | |
| 63 | + | |
| 36 | 64 | private function printStats($stats) |
| 37 | 65 | { |
| 38 | 66 | ?> |
| 39 | 67 | <ul class="ff_dashboard_stats"> |
| 40 | 68 | <?php foreach ($stats as $stat): ?> |
| 41 | - <li> | |
| 42 | - <a href="<?php echo admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $stat->form_id); ?>"> | |
| 43 | - <?php echo $stat->title; ?> | |
| 44 | - <span class="ff_total"><?php echo $stat->unreadCount; ?>/<?php echo $stat->total; ?></span> | |
| 45 | - </a> | |
| 46 | - </li> | |
| 69 | + <li> | |
| 70 | + <a | |
| 71 | + href="<?php echo esc_url(admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $stat->form_id)); ?>"> | |
| 72 | + <?php echo esc_html($stat->title); ?> | |
| 73 | + <span class="ff_total"><?php echo esc_attr($stat->unread_count); ?>/<?php echo esc_attr($stat->total); ?></span> | |
| 74 | + </a> | |
| 75 | + </li> | |
| 47 | 76 | <?php endforeach; ?> |
| 48 | 77 | </ul> |
| 49 | - <?php if ( !defined('ENHANCED_BLOCKS_VERSION') && !defined('FLUENTFORMPRO') ) : ?> | |
| 50 | - <div class="ff_recommended_plugin"> | |
| 51 | - Recommended Plugin: <b>Enhanced Blocks – Page Builder Blocks for Gutenberg</b> <br /> | |
| 52 | - <a href="<?php echo $this->getInstallUrl('enhanced-blocks'); ?>">Install</a> | |
| 53 | - | <a target="_blank" rel="noopener" href="https://wordpress.org/plugins/enhanced-blocks/">Learn More</a> | |
| 54 | - </div> | |
| 78 | + <?php if (!defined('FLUENTCRM') && !defined('FLUENTFORMPRO')) : ?> | |
| 79 | + <div class="ff_recommended_plugin"> | |
| 80 | + Recommended Plugin: <b>FluentCRM - Email Marketing Automation For WordPress</b> <br /> | |
| 81 | + <a | |
| 82 | + href="<?php echo esc_url($this->getInstallUrl('fluent-crm')); ?>">Install</a> | |
| 83 | + | <a target="_blank" rel="noopener" href="https://wordpress.org/plugins/fluent-crm/">Learn More</a> | |
| 84 | + </div> | |
| 55 | 85 | <?php endif; ?> |
| 56 | 86 | <style> |
| 57 | 87 | ul.ff_dashboard_stats { |
| 58 | 88 | margin: 0; |
| @@ -93,8 +123,9 @@ | ||
| 93 | 123 | |
| 94 | 124 | .ff_recommended_plugin { |
| 95 | 125 | padding: 15px 0px 0px; |
| 96 | 126 | } |
| 127 | + | |
| 97 | 128 | .ff_recommended_plugin a { |
| 98 | 129 | font-weight: bold; |
| 99 | 130 | font-size: 110%; |
| 100 | 131 | } |
| @@ -107,14 +138,6 @@ | ||
| 107 | 138 | return wp_nonce_url( |
| 108 | 139 | self_admin_url('update.php?action=install-plugin&plugin=' . $plugin), |
| 109 | 140 | 'install-plugin_' . $plugin |
| 110 | 141 | ); |
| 111 | - } | |
| 112 | - | |
| 113 | - private function getUnreadCount($formId) | |
| 114 | - { | |
| 115 | - return wpFluent()->table('fluentform_submissions') | |
| 116 | - ->where('status', 'unread') | |
| 117 | - ->where('form_id', $formId) | |
| 118 | - ->count(); | |
| 119 | 142 | } |
| 120 | 143 | } |