consent-logs-list-table.php
5.16 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie_Notice_Consent_Logs_List_Table class.
*
* @class Cookie_Notice_Consent_Logs_List_Table
*/
class Cookie_Notice_Consent_Logs_List_Table extends WP_List_Table {
/**
* Prepare the items for the table to process.
*
* @return void
*/
public function prepare_items() {
// get main instance
$cn = Cookie_Notice();
// get consent logs
if ( is_multisite() && $cn->is_network_admin() && $cn->is_plugin_network_active() && $cn->network_options['global_override'] )
$analytics = get_site_option( 'cookie_notice_app_analytics', [] );
else
$analytics = get_option( 'cookie_notice_app_analytics', [] );
// get date format
$format = get_option( 'date_format' );
// get 30 days of default data
$logs = $this->fill_missing_dates( $format );
// any data?
if ( ! empty( $analytics['consentActivities'] ) && is_array( $analytics['consentActivities'] ) ) {
foreach ( $analytics['consentActivities'] as $index => $entry ) {
// get date in digits only
$digits = (int) str_replace( '-', '', substr( $entry->eventdt, 0, 10 ) );
// current data?
if ( array_key_exists( $digits, $logs ) ) {
$logs[$digits]['level_' . (int) $entry->consentlevel] = (int) $entry->totalrecd;
$logs[$digits]['total'] += (int) $entry->totalrecd;
}
}
krsort( $logs, SORT_NUMERIC );
}
$this->_column_headers = [ $this->get_columns(), [], $this->get_sortable_columns(), 'date' ];
usort( $logs, [ $this, 'usort_reorder' ] );
$this->items = $logs;
}
/**
* Fill missing dates.
*
* @param string $format
* @return array
*/
private function fill_missing_dates( $format ) {
$empty_logs = [];
// get current date
$d = new DateTime();
// go back 30 days
$d->modify( '-30 days' );
// update dates for last 30 days
for ( $i = 1; $i <= 31; $i++ ) {
$date = $d->format( 'Y-m-d' );
$digits = (int) str_replace( '-', '', $date );
$empty_logs[$digits] = [
'slug' => $digits,
'date' => date_i18n( $format, strtotime( $date ) ),
'date_iso' => $date,
'level_1' => 0,
'level_2' => 0,
'level_3' => 0,
'total' => 0
];
$d->modify( '+1 days' );
}
return $empty_logs;
}
/**
* Sort consent logs.
*
* @param int $first
* @param int $second
* @return array
*/
public function usort_reorder( $first, $second ) {
// get orderby
$orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_key( $_GET['orderby'] ) : 'date';
// skip invalid orderby
if ( ! array_key_exists( $orderby, $this->get_sortable_columns() ) )
return 0;
// get order
$order = ( ! empty( $_GET['order'] ) ) ? sanitize_key( $_GET['order'] ) : 'desc';
// use numeric value for dates
if ( $orderby === 'date' )
$orderby = 'slug';
// determine sort order
if ( $first[$orderby] === $second[$orderby] )
$result = 0;
else
$result = ( $first[$orderby] < $second[$orderby] ) ? -1 : 1;
return ( $order === 'asc' ) ? $result : -$result;
}
/**
* Override the parent columns method. Defines the columns to use in your listing table.
*
* @return array
*/
public function get_columns() {
$columns = [
'cb' => '',
'date' => __( 'Date', 'cookie-notice' ),
'level_1' => __( 'Level 1', 'cookie-notice' ),
'level_2' => __( 'Level 2', 'cookie-notice' ),
'level_3' => __( 'Level 3', 'cookie-notice' ),
'total' => __( 'Total', 'cookie-notice' )
];
return $columns;
}
/**
* Define the sortable columns
*
* @return array
*/
public function get_sortable_columns() {
return [
'date' => [ 'date', false ],
'level_1' => [ 'level_1', true ],
'level_2' => [ 'level_2', true ],
'level_3' => [ 'level_3', true ],
'total' => [ 'total', true ]
];
}
/**
* Display single row.
*
* @param array $item
* @return void
*/
public function single_row( $item ) {
echo '
<tr id="cn_consent_log_' . esc_attr( $item['slug'] ) . '" class="cn-consent-log" data-date="' . esc_attr( $item['date_iso'] ) . '">';
$this->single_row_columns( $item );
echo '
</tr>';
}
/**
* Define what data to show on each column of the table.
*
* @param array $item
* @param string $column_name
* @return string
*/
public function column_default( $item, $column_name ) {
return esc_html( $item[$column_name] );
}
/**
* Define what data to show on cb column of the table.
*
* @param array $item
* @return string
*/
function column_cb( $item ) {
return '
<label for="cn-consent-log-' . esc_attr( $item['slug'] ) . '" class="cn-consent-log-item">
<input id="cn-consent-log-' . esc_attr( $item['slug'] ) . '" type="checkbox">
<span class="cn-consent-log-head' . ( $item['total'] === 0 ? ' disabled' : '' ) . '"></span>
</label>';
}
/**
* Display bulk actions.
*
* @return array
*/
public function get_bulk_actions() {
return [];
}
/**
* Display empty result.
*
* @return void
*/
public function no_items() {
echo __( 'No consent logs found.', 'cookie-notice' );
}
}