Skip to content

Commit 4bf5aba

Browse files
committed
Admin dashboard changes
* New filter `tptn_admin_dashboard_tabs` to add/remove tabs * Passing 'hide' will hide a tab on first load * Smart chart visualization: Automatically switches to an area chart for datasets with more than 100 data points.
1 parent 4990c28 commit 4bf5aba

8 files changed

+128
-17
lines changed

includes/admin/class-admin.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,28 @@ public function admin_enqueue_scripts() {
194194
'top-ten-chart-js',
195195
TOP_TEN_PLUGIN_URL . 'includes/admin/js/chart.min.js',
196196
array(),
197-
TOP_TEN_VERSION,
197+
'4.4.8',
198198
true
199199
);
200200
wp_register_script(
201201
'top-ten-chart-datalabels-js',
202202
TOP_TEN_PLUGIN_URL . 'includes/admin/js/chartjs-plugin-datalabels.min.js',
203203
array( 'top-ten-chart-js' ),
204-
TOP_TEN_VERSION,
204+
'2.2.0',
205205
true
206206
);
207207
wp_register_script(
208208
'top-ten-luxon',
209209
TOP_TEN_PLUGIN_URL . 'includes/admin/js/luxon.min.js',
210210
array(),
211-
TOP_TEN_VERSION,
211+
'3.5.0',
212212
true
213213
);
214214
wp_register_script(
215215
'top-ten-chartjs-adapter-luxon-js',
216216
TOP_TEN_PLUGIN_URL . 'includes/admin/js/chartjs-adapter-luxon.min.js',
217217
array( 'top-ten-luxon', 'top-ten-chart-js' ),
218-
TOP_TEN_VERSION,
218+
'1.3.1',
219219
true
220220
);
221221
wp_register_script(

includes/admin/class-dashboard.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @link https://webberzone.com
66
* @since 3.0.0
77
*
8-
* @package Top 10
8+
* @package Top_Ten
99
* @subpackage Admin/Dashboard
1010
*/
1111

@@ -99,8 +99,20 @@ public function render_page() {
9999
<ul class="nav-tab-wrapper" style="padding:0; border-bottom: 1px solid #ccc;">
100100
<?php
101101
foreach ( $this->get_tabs() as $tab_id => $tab_name ) {
102+
$tab_class = 'nav-tab';
103+
$tab_style = '';
102104

103-
echo '<li style="padding:0; border:0; margin:0;"><a href="#' . esc_attr( $tab_id ) . '" title="' . esc_attr( $tab_name['title'] ) . '" class="nav-tab">';
105+
// Check if this tab should be hidden initially.
106+
if ( isset( $tab_name['hide'] ) && $tab_name['hide'] ) {
107+
$tab_style = 'display:none;';
108+
}
109+
110+
// Add custom class if specified.
111+
if ( isset( $tab_name['class'] ) ) {
112+
$tab_class .= ' ' . $tab_name['class'];
113+
}
114+
115+
echo '<li style="padding:0; border:0; margin:0;"><a href="#' . esc_attr( $tab_id ) . '" title="' . esc_attr( $tab_name['title'] ) . '" class="' . esc_attr( $tab_class ) . '" style="' . esc_attr( $tab_style ) . '">';
104116
echo esc_html( $tab_name['title'] );
105117
echo '</a></li>';
106118

@@ -321,6 +333,15 @@ public function get_tabs() {
321333
),
322334
);
323335

336+
/**
337+
* Filters the tabs for the admin dashboard.
338+
*
339+
* @since 4.1.0
340+
*
341+
* @param array $tabs Array of tabs.
342+
*/
343+
$tabs = apply_filters( 'tptn_admin_dashboard_tabs', $tabs );
344+
324345
return $tabs;
325346
}
326347

includes/admin/js/admin-scripts.js

+22
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ jQuery(document).ready(function ($) {
5656
$(ui.newTab.find("a")).addClass("nav-tab-active");
5757
}
5858
});
59+
60+
// Wait for the tabs to be fully initialized
61+
setTimeout(function() {
62+
// Select Today tab (second tab) by default and add the nav-tab-active class
63+
var $tabs = $("#post-body-content").tabs();
64+
65+
// First find the Today tab index (usually 1)
66+
var tabIndex = 0;
67+
$(".nav-tab-wrapper li a").each(function(index) {
68+
if ($(this).text().indexOf('Today') >= 0) {
69+
tabIndex = index;
70+
return false; // Break the loop
71+
}
72+
});
73+
74+
// Set the active tab
75+
$tabs.tabs("option", "active", tabIndex);
76+
77+
// Manually add the active class to the Today tab
78+
$(".nav-tab-wrapper li a").removeClass("nav-tab-active");
79+
$(".nav-tab-wrapper li").eq(tabIndex).find("a").addClass("nav-tab-active");
80+
}, 200);
5981
});
6082

6183
// Datepicker.

includes/admin/js/admin-scripts.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/admin/js/chart-data.js

+71-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,47 @@ function updateChart() {
1616
date.push(data[i].date);
1717
visits.push(data[i].visits);
1818
}
19+
20+
// Determine chart type based on number of datapoints
21+
var chartType = "bar";
22+
var chartFill = false;
23+
var backgroundColor = "#70c4e1";
24+
var pointBackgroundColor = "#70c4e1";
25+
var borderWidth = 0;
26+
var pointBorderColor = "#70c4e1";
27+
var pointRadius = 0;
28+
var showDataLabels = true;
29+
30+
// If we have more than 50 datapoints, switch to area chart for better visualization
31+
if (date.length > 100) {
32+
chartType = "line";
33+
chartFill = true;
34+
backgroundColor = "rgba(112, 196, 225, 0.8)"; // Transparent blue
35+
pointBackgroundColor = "#70c4e1"; // Solid blue dots
36+
borderWidth = 2;
37+
pointBorderColor = "#ffffff"; // White border around dots
38+
pointRadius = 3;
39+
showDataLabels = false; // Hide data labels for area chart to avoid clutter
40+
}
41+
42+
// Update chart configuration if type needs to change
43+
if (window.top10chart.config.type !== chartType) {
44+
window.top10chart.config.type = chartType;
45+
window.top10chart.data.datasets.forEach((dataset) => {
46+
dataset.fill = chartFill;
47+
dataset.backgroundColor = backgroundColor;
48+
dataset.pointBackgroundColor = pointBackgroundColor;
49+
dataset.borderWidth = borderWidth;
50+
dataset.pointBorderColor = pointBorderColor;
51+
dataset.pointRadius = pointRadius;
52+
});
53+
54+
// Update data labels plugin display setting
55+
if (window.top10chart.options && window.top10chart.options.plugins && window.top10chart.options.plugins.datalabels) {
56+
window.top10chart.options.plugins.datalabels.display = showDataLabels;
57+
}
58+
}
59+
1960
window.top10chart.data.labels = date;
2061
window.top10chart.data.datasets.forEach((dataset) => {
2162
dataset.data = visits;
@@ -46,19 +87,46 @@ jQuery(document).ready(function ($) {
4687
visits.push(data[i].visits);
4788
}
4889

90+
// Determine chart type based on number of datapoints
91+
var chartType = "bar";
92+
var chartFill = false;
93+
var backgroundColor = "#70c4e1";
94+
var pointBackgroundColor = "#70c4e1";
95+
var borderWidth = 0;
96+
var pointBorderColor = "#70c4e1";
97+
var pointRadius = 0;
98+
var showDataLabels = true;
99+
100+
// If we have more than 50 datapoints, switch to area chart for better visualization
101+
if (date.length > 50) {
102+
chartType = "line";
103+
chartFill = true;
104+
backgroundColor = "rgba(112, 196, 225, 0.2)"; // Transparent blue
105+
pointBackgroundColor = "#70c4e1"; // Solid blue dots
106+
borderWidth = 2;
107+
pointBorderColor = "#ffffff"; // White border around dots
108+
pointRadius = 3;
109+
showDataLabels = false; // Hide data labels for area chart to avoid clutter
110+
}
111+
49112
var ctx = $("#visits");
50113
var config = {
51-
type: "bar",
114+
type: chartType,
52115
data: {
53116
labels: date,
54117
datasets: [
55118
{
56119
label: tptn_chart_data.datasetlabel,
57-
backgroundColor: "#70c4e1",
120+
backgroundColor: backgroundColor,
58121
borderColor: "#70c4e1",
59122
hoverBackgroundColor: "#ffbf00",
60123
hoverBorderColor: "#ffbf00",
61124
data: visits,
125+
fill: chartFill,
126+
pointBackgroundColor: pointBackgroundColor,
127+
pointBorderColor: pointBorderColor,
128+
pointRadius: pointRadius,
129+
borderWidth: borderWidth,
62130
},
63131
],
64132
},
@@ -77,6 +145,7 @@ jQuery(document).ready(function ($) {
77145
color: "#000000",
78146
anchor: "end",
79147
align: "top",
148+
display: showDataLabels,
80149
},
81150
},
82151
scales: {

includes/admin/js/chart-data.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/admin/js/chart.min.js

-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.txt

+6
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ When you enabled the scheduled maintenance, Top 10 will create a cron job that w
151151

152152
= 4.1.0 =
153153

154+
* Features:
155+
* Import data from WordPress Popular Posts plugin.
156+
* Admin Dashboard:
157+
* Smart chart visualization: Automatically switches to an area chart for datasets with more than 100 data points.
158+
* [Pro] Clicking on a column in the Popular Posts chart will display the most popular posts for the selected day.
159+
154160
* Modifications:
155161
* Updated ChartJS and replaced Moment adapter with Luxon.
156162
* An admin notice is now displayed when any Top 10 table is missing. The plugin will also automatically recreate the missing tables.

0 commit comments

Comments
 (0)