| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules; |
| 4 |
|
| 5 |
defined('ABSPATH') or die; |
| 6 |
|
| 7 |
use FluentForm\App\Helpers\Helper; |
| 8 |
use FluentForm\App\Models\Submission; |
| 9 |
|
| 10 |
class DashboardWidgetModule |
| 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 |
|
| 29 |
public function showStat() |
| 30 |
{ |
| 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([ |
| 39 |
'fluentform_forms.title', |
| 40 |
'fluentform_submissions.form_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"), |
| 44 |
]) |
| 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(); |
| 50 |
|
| 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) { |
| 56 |
echo 'You can see your submission stats here'; |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$this->printStats($stats); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
private function printStats($stats) |
| 65 |
{ |
| 66 |
?> |
| 67 |
<ul class="ff_dashboard_stats"> |
| 68 |
<?php foreach ($stats as $stat): ?> |
| 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> |
| 76 |
<?php endforeach; ?> |
| 77 |
</ul> |
| 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> |
| 85 |
<?php endif; ?> |
| 86 |
<style> |
| 87 |
ul.ff_dashboard_stats { |
| 88 |
margin: 0; |
| 89 |
padding: 0; |
| 90 |
list-style: none; |
| 91 |
} |
| 92 |
|
| 93 |
ul.ff_dashboard_stats li { |
| 94 |
padding: 8px 12px; |
| 95 |
border-bottom: 1px solid #eeeeee; |
| 96 |
margin: 0 -12px; |
| 97 |
cursor: pointer; |
| 98 |
} |
| 99 |
|
| 100 |
ul.ff_dashboard_stats li:hover { |
| 101 |
background: #fafafa; |
| 102 |
border-bottom: 1px solid #eeeeee; |
| 103 |
} |
| 104 |
|
| 105 |
ul.ff_dashboard_stats li:hover a { |
| 106 |
color: black; |
| 107 |
} |
| 108 |
|
| 109 |
ul.ff_dashboard_stats li:nth-child(2n+2) { |
| 110 |
background: #f9f9f9; |
| 111 |
} |
| 112 |
|
| 113 |
ul.ff_dashboard_stats li span.ff_total { |
| 114 |
float: right; |
| 115 |
} |
| 116 |
|
| 117 |
ul.ff_dashboard_stats li a { |
| 118 |
display: block; |
| 119 |
color: #0073aa; |
| 120 |
font-weight: 500; |
| 121 |
font-size: 105%; |
| 122 |
} |
| 123 |
|
| 124 |
.ff_recommended_plugin { |
| 125 |
padding: 15px 0px 0px; |
| 126 |
} |
| 127 |
|
| 128 |
.ff_recommended_plugin a { |
| 129 |
font-weight: bold; |
| 130 |
font-size: 110%; |
| 131 |
} |
| 132 |
</style> |
| 133 |
<?php |
| 134 |
} |
| 135 |
|
| 136 |
private function getInstallUrl($plugin) |
| 137 |
{ |
| 138 |
return wp_nonce_url( |
| 139 |
self_admin_url('update.php?action=install-plugin&plugin=' . $plugin), |
| 140 |
'install-plugin_' . $plugin |
| 141 |
); |
| 142 |
} |
| 143 |
} |
| 144 |
|