-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.traktivity.php
162 lines (140 loc) · 3.5 KB
/
stats.traktivity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Stats functions.
*
* @package Traktivity
*/
defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
/**
* Build Stats from existing Traktivity data.
*
* @since 2.2.0
*/
class Traktivity_Stats {
/**
* Constructor
*/
function __construct() {}
/**
* Convert minutes into string of days, hours, and minutes.
*
* @since 2.2.0
*
* @param string|int $minutes Number of minutes.
*
* @return string $runtime Time.
*/
public static function convert_time( $minutes = 0 ) {
$minutes_per_hour = 60;
$minutes_per_day = 24 * $minutes_per_hour;
$minutes_per_year = 365 * $minutes_per_day;
// Get number of years.
$years = floor( $minutes / $minutes_per_year );
// Get number of days.
$days_minutes = $minutes % $minutes_per_year;
$days = floor( $days_minutes / $minutes_per_day );
// Get number of hours.
$hour_minutes = $minutes % $minutes_per_day;
$hours = floor( $hour_minutes / $minutes_per_hour );
// Get the minutes left.
$minutes_left = $hour_minutes % $minutes_per_hour;
$minutes = ceil( $minutes_left );
if ( 0 < $minutes ) {
$display_minutes = sprintf(
/* Translators: %1$d is the number of minutes */
_n(
'%1$d minute',
'%1$d minutes',
$minutes,
'traktivity'
),
$minutes
);
} else {
$display_minutes = '';
}
if ( 0 < $hours ) {
$display_hours = sprintf(
/* Translators: %1$d is the number of hours, %2$d is the number of minutes. */
_n(
'%1$d hour %2$s',
'%1$d hours %2$s',
$hours,
'traktivity'
),
$hours,
$display_minutes
);
} else {
$display_hours = $display_minutes;
}
if ( 0 < $days ) {
$display_days = sprintf(
/* Translators: %1$d is the number of days, %2$s is the number of hours and minutes. */
_n(
'%1$d day %2$s',
'%1$d days %2$s',
$days,
'traktivity'
),
$days,
$display_hours
);
} else {
$display_days = $display_hours;
}
if ( 0 < $years ) {
return sprintf(
/* Translators: %1$d is the number of years, %2$s is the number of days, hours and minutes. */
_n(
'%1$d year %2$s',
'%1$d years %2$s',
$years,
'traktivity'
),
$years,
$display_days
);
} else {
$runtime = $display_days;
}
return $runtime;
}
/**
* Create an option where we store the Total time spent in front of a screen.
*
* @since 2.2.0
*
* @return string $time Total time spent in front of a screen.
*/
public static function total_time_watched() {
$stats = get_option( 'traktivity_stats' );
// If that's the first time we're running this function, let's start with an empty array of stats.
if ( empty( $stats ) ) {
$stats = array();
}
// If the total time is already set, let's stop here.
if ( ! empty( $stats['total_time_watched'] ) ) {
return $stats['total_time_watched'];
}
// Let's pull all trakt_runtime post meta from all Traktivity events.
global $wpdb;
$post_meta = 'trakt_runtime';
$all_runtimes = $wpdb->get_col( $wpdb->prepare( "
SELECT pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status = 'publish'
AND p.post_type = 'traktivity_event'
", $post_meta ) );
if ( ! empty( $all_runtimes ) ) {
$stats['total_time_watched'] = array_sum( $all_runtimes );
// Save the value as an option.
update_option( 'traktivity_stats', $stats );
return $stats['total_time_watched'];
}
// Fallback.
return 0;
}
} // End class.
new Traktivity_Stats();