Skip to content

Commit 8b76712

Browse files
committed
migrate overview page from Statify Extended Evaluation
* Use API endpoints to fetch data * build monthly and yearly chart/table re-using dashboard JS
1 parent 0377051 commit 8b76712

File tree

9 files changed

+473
-120
lines changed

9 files changed

+473
-120
lines changed

css/dashboard.css

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @group Front page */
22

3-
#statify_chart {
3+
.statify-chart {
44
color: #a7aaad;
55
flex: 0 0 100%;
66
height: 140px;
@@ -10,34 +10,40 @@
1010
text-align: center;
1111
}
1212

13-
#statify_chart * {
13+
.statify-chart * {
1414
direction: ltr;
1515
}
1616

17-
body.rtl #statify_chart * {
17+
body.rtl .statify-chart * {
1818
direction: rtl;
1919
}
2020

21-
#statify_chart .spinner {
21+
.statify-chart .spinner {
2222
float: none;
2323
margin: 1em;
2424
}
2525

26-
#statify_chart .ct-line {
26+
.statify-chart .ct-line {
2727
stroke: #3582c4;
2828
stroke-width: 2px;
2929
}
3030

31-
#statify_chart .ct-point {
31+
.statify-chart .ct-point {
3232
fill: #fff;
3333
stroke: #3582c4;
3434
stroke-width: 1.5px;
3535
}
3636

37-
#statify_chart .ct-area {
37+
.statify-chart .ct-area {
3838
fill: #3582c4;
3939
}
4040

41+
.statify-chart .ct-label.ct-horizontal.ct-end {
42+
align-items: center;
43+
justify-content: center;
44+
margin-left: -50%;
45+
}
46+
4147
.statify-chartist-tooltip {
4248
border: 1px solid #000;
4349
background-color: #fff;
@@ -55,6 +61,24 @@ body.rtl #statify_chart * {
5561
color: #3582c4;
5662
}
5763

64+
65+
.statify-table th,
66+
.statify-table td:last-child {
67+
font-weight: 700;
68+
}
69+
70+
.statify-table tr.placeholder {
71+
background-color: #fff;
72+
}
73+
74+
.statify-table tr.placeholder td span {
75+
background-color: #f5f5f5;
76+
border-radius: 1em;
77+
display: inline-block;
78+
width: 80%;
79+
}
80+
81+
5882
#statify_dashboard .postbox-title-action a,
5983
#statify_dashboard .settings-link a {
6084
display: block;
@@ -139,4 +163,21 @@ body.rtl #statify_chart * {
139163
line-height: 28px;
140164
}
141165

166+
.statify-chart-container {
167+
background: #fff;
168+
margin: 1em 0;
169+
padding: 1em;
170+
}
171+
172+
.statify-chart-container .statify-chart {
173+
height: 280px;
174+
margin: 10px 20px;
175+
}
176+
177+
.statify-chart-title {
178+
text-align: center;
179+
font-size: 1.5em;
180+
padding: 0.5em 0;
181+
}
182+
142183
/* @end group */

inc/class-statify-api.php

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ class Statify_Api extends Statify {
2222
*
2323
* @var string
2424
*/
25-
const REST_NAMESPACE = 'statify/v1';
25+
const REST_NAMESPACE = 'statify/v1';
2626
const REST_ROUTE_TRACK = 'track';
2727
const REST_ROUTE_STATS = 'stats';
28+
const REST_ROUTE_STATS_EXTENDED = 'stats/extended';
2829

2930
/**
3031
* Initialize REST API routes.
@@ -55,6 +56,16 @@ public static function init(): void {
5556
)
5657
);
5758

59+
register_rest_route(
60+
self::REST_NAMESPACE,
61+
self::REST_ROUTE_STATS_EXTENDED,
62+
array(
63+
'methods' => WP_REST_Server::READABLE,
64+
'callback' => array( __CLASS__, 'get_extended' ),
65+
'permission_callback' => array( __CLASS__, 'user_can_see_stats' ),
66+
)
67+
);
68+
5869
add_filter( 'rest_authentication_errors', array( __CLASS__, 'check_authentication' ), 5 );
5970
}
6071

@@ -103,7 +114,7 @@ public static function track_visit( WP_REST_Request $request ): WP_REST_Response
103114
if ( null !== $referrer ) {
104115
$referrer = filter_var( $referrer, FILTER_SANITIZE_URL );
105116
}
106-
$target = $request->get_param( 'target' );
117+
$target = $request->get_param( 'target' );
107118
if ( null !== $target ) {
108119
$target = filter_var( $target, FILTER_SANITIZE_URL );
109120
}
@@ -124,7 +135,7 @@ public static function track_visit( WP_REST_Request $request ): WP_REST_Response
124135
public static function get_stats( WP_REST_Request $request ): WP_REST_Response {
125136
$refresh = '1' === $request->get_param( 'refresh' );
126137

127-
$stats = Statify_Dashboard::get_stats( $refresh );
138+
$stats = Statify_Dashboard::get_stats( $refresh );
128139

129140
$visits = $stats['visits'];
130141
$stats['visits'] = array();
@@ -151,4 +162,54 @@ public static function get_stats( WP_REST_Request $request ): WP_REST_Response {
151162

152163
return new WP_REST_Response( $stats );
153164
}
165+
166+
167+
/**
168+
* Get extended statistics.
169+
*
170+
* @param WP_REST_Request $request The request.
171+
*
172+
* @return WP_REST_Response The response.
173+
*
174+
* @since 2.0.0
175+
*/
176+
public static function get_extended( WP_REST_Request $request ): WP_REST_Response {
177+
$scope = $request->get_param( 'scope' );
178+
$post = $request->get_param( 'post' );
179+
180+
$status = 200;
181+
if ( 'year' === $scope ) {
182+
$stats = Statify_Evaluation::get_views_for_all_years( $post );
183+
} elseif ( 'month' === $scope ) {
184+
$visits = Statify_Evaluation::get_views_for_all_months( $post );
185+
$stats = array( 'visits' => array() );
186+
$last_ym = 0;
187+
foreach ( $visits as $ym => $v ) {
188+
$ym = explode( '-', $ym );
189+
$year = intval( $ym[0] );
190+
$month = intval( $ym[1] );
191+
$year_month = $year * 12 + $month;
192+
for ( $ym = $last_ym + 1; $last_ym > 0 && $ym < $year_month; $ym++ ) {
193+
// Fill gaps.
194+
$y = intval( $ym / 12 );
195+
if ( ! isset( $stats['visits'][ $y ] ) ) {
196+
$stats['visits'][ $y ] = array();
197+
}
198+
$stats['visits'][ $y ][ $ym % 12 ] = 0;
199+
}
200+
if ( ! isset( $stats['visits'][ $year ] ) ) {
201+
$stats['visits'][ $year ] = array();
202+
}
203+
$stats['visits'][ $year ][ $month ] = $v;
204+
$last_ym = $year_month;
205+
}
206+
} elseif ( 'day' === $scope ) {
207+
$stats = Statify_Evaluation::get_views_for_all_days( $post );
208+
} else {
209+
$stats = array( 'error' => 'invalid scope (allowed: year, month, day)' );
210+
$status = 400;
211+
}
212+
213+
return new WP_REST_Response( $stats, $status );
214+
}
154215
}

inc/class-statify-dashboard.php

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
*/
1919
class Statify_Dashboard extends Statify {
2020

21-
/**
22-
* Plugin version.
23-
*
24-
* @since 1.4.0
25-
* @var string
26-
*/
27-
protected static $_plugin_version;
28-
2921
/**
3022
* Dashboard widget initialize
3123
*
@@ -48,9 +40,6 @@ public static function init(): void {
4840
wp_normalize_path( sprintf( '%s/lang', STATIFY_DIR ) )
4941
);
5042

51-
// Plugin version.
52-
self::_get_version();
53-
5443
// Add dashboard widget.
5544
wp_add_dashboard_widget(
5645
'statify_dashboard',
@@ -60,82 +49,12 @@ public static function init(): void {
6049
);
6150

6251
// Init CSS.
63-
add_action( 'admin_print_styles', array( __CLASS__, 'add_style' ) );
52+
add_action( 'admin_print_styles', array( 'Statify', 'add_style' ) );
6453

6554
// Init JS.
66-
add_action( 'admin_print_scripts', array( __CLASS__, 'add_js' ) );
67-
}
68-
69-
/**
70-
* Print CSS
71-
*
72-
* @since 0.1.0
73-
* @version 1.4.0
74-
*/
75-
public static function add_style(): void {
76-
77-
// Register CSS.
78-
wp_register_style(
79-
'chartist_css',
80-
plugins_url( '/css/chartist.min.css', STATIFY_FILE ),
81-
array(),
82-
self::$_plugin_version
83-
);
84-
wp_register_style(
85-
'chartist_tooltip_css',
86-
plugins_url( '/css/chartist-plugin-tooltip.min.css', STATIFY_FILE ),
87-
array(),
88-
self::$_plugin_version
89-
);
90-
wp_register_style(
91-
'statify',
92-
plugins_url( '/css/dashboard.min.css', STATIFY_FILE ),
93-
array(),
94-
self::$_plugin_version
95-
);
96-
97-
// Load CSS.
98-
wp_enqueue_style( 'chartist_css' );
99-
wp_enqueue_style( 'chartist_tooltip_css' );
100-
wp_enqueue_style( 'statify' );
101-
}
102-
103-
/**
104-
* Print JavaScript
105-
*
106-
* @since 0.1.0
107-
* @version 1.4.0
108-
*/
109-
public static function add_js(): void {
110-
111-
// Register JS.
112-
wp_register_script(
113-
'chartist_js',
114-
plugins_url( 'js/chartist.min.js', STATIFY_FILE ),
115-
array(),
116-
self::$_plugin_version,
117-
true
118-
);
119-
wp_register_script(
120-
'chartist_tooltip_js',
121-
plugins_url( 'js/chartist-plugin-tooltip.min.js', STATIFY_FILE ),
122-
array( 'chartist_js' ),
123-
self::$_plugin_version,
124-
true
125-
);
126-
wp_register_script(
127-
'statify_chart_js',
128-
plugins_url( 'js/dashboard.min.js', STATIFY_FILE ),
129-
array( 'wp-api-fetch', 'chartist_tooltip_js', 'wp-i18n' ),
130-
self::$_plugin_version,
131-
true
132-
);
133-
134-
// Sets translated strings for the script.
135-
wp_set_script_translations( 'statify_chart_js', 'statify' );
55+
add_action( 'admin_print_scripts', array( 'Statify', 'add_js' ) );
13656
}
13757

138-
13958
/**
14059
* Print widget frontview.
14160
*
@@ -228,22 +147,6 @@ private static function _save_widget_options(): void {
228147
update_option( 'statify', $options );
229148
}
230149

231-
232-
/**
233-
* Set plugin version from plugin meta data
234-
*
235-
* @since 1.4.0
236-
* @version 1.4.0
237-
*/
238-
private static function _get_version(): void {
239-
240-
// Get plugin meta.
241-
$meta = get_plugin_data( STATIFY_FILE );
242-
243-
self::$_plugin_version = $meta['Version'];
244-
}
245-
246-
247150
/**
248151
* Get stats from cache
249152
*

0 commit comments

Comments
 (0)