consent-logs.php
5.25 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
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie_Notice_Consent_Logs class.
*
* @class Cookie_Notice_Consent_Logs
*/
class Cookie_Notice_Consent_Logs {
/**
* Class constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ], 11 );
add_action( 'wp_ajax_cn_get_consent_logs_by_date', [ $this, 'get_consent_logs_by_date' ] );
}
/**
* Load default scripts and styles.
*
* @return void
*/
public function admin_enqueue_scripts() {
// get main instance
$cn = Cookie_Notice();
// get cookie compliance status
$status = $cn->get_status();
// pagination script
wp_enqueue_script( 'cookie-notice-admin-pagination', COOKIE_NOTICE_URL . '/assets/pagination/pagination.min.js', [ 'jquery' ], $cn->defaults['version'] );
wp_enqueue_style( 'cookie-notice-admin-pagination', COOKIE_NOTICE_URL . '/assets/pagination/pagination.css', [], $cn->defaults['version'] );
}
/**
* Get consent by date via AJAX request.
*
* @return void
*/
public function get_consent_logs_by_date() {
// valid nonce?
if ( ! check_ajax_referer( 'cn-get-consent-logs', 'nonce' ) )
wp_send_json_error();
// check data
if ( ! isset( $_POST['action'], $_POST['date'], $_POST['nonce'] ) )
wp_send_json_error();
// check capability
if ( ! current_user_can( apply_filters( 'cn_manage_cookie_notice_cap', 'manage_options' ) ) )
wp_send_json_error();
// sanitize date
$date = preg_replace( '[^\d-]', '', $_POST['date'] );
// get datetime
$dt = DateTime::createFromFormat( 'Y-m-d', $date );
// valid date?
if ( $dt && $dt->format( 'Y-m-d' ) === $date ) {
$data = Cookie_Notice()->welcome_api->get_consent_logs_by_date( $date );
if ( is_array( $data ) )
wp_send_json_success( $this->get_consent_logs_table( $data ) );
else
wp_send_json_error( $data );
}
wp_send_json_error();
}
/**
* Get single row template.
*
* @return string
*/
public function get_consent_logs_table( $data ) {
$html = '
<table class="wp-list-table widefat fixed striped table-view-list toplevel_page_cookie-notice">
<thead>
<tr>
<th id="cn_consent_id">' . esc_html__( 'Consent ID', 'cookie-notice' ) . '</th>
<th id="cn_consent_id">' . esc_html__( 'Consent Level', 'cookie-notice' ) . '</th>
<th id="cn_consent_id">' . esc_html__( 'Categories', 'cookie-notice' ) . '</th>
<th id="cn_consent_id">' . esc_html__( 'Duration', 'cookie-notice' ) . '</th>
<th id="cn_consent_id">' . esc_html__( 'Time', 'cookie-notice' ) . '</th>
</tr>
</thead>
<tbody>';
// no data?
if ( empty( $data ) ) {
$html .= '
<tr>
<td colspan="5">' . esc_html__( 'No consent logs found.', 'cookie-notice' ) . '</td>
</tr>';
} else {
// set current timezone
$current_timezone = new DateTimeZone( Cookie_Notice()->dashboard->timezone_string() );
foreach ( $data as $no => $consent_log ) {
$categories = [];
if ( $consent_log->ev_essential )
$categories[] = esc_html__( 'Essential', 'cookie-notice' );
if ( $consent_log->ev_functional )
$categories[] = esc_html__( 'Functional', 'cookie-notice' );
if ( $consent_log->ev_analytics )
$categories[] = esc_html__( 'Analytics', 'cookie-notice' );
if ( $consent_log->ev_marketing )
$categories[] = esc_html__( 'Marketing', 'cookie-notice' );
// get current date
$timestamp = new DateTime( $consent_log->timestamp );
$timestamp->setTimezone( $current_timezone );
// get deuration in days
$duration = (int) $consent_log->ev_eventdetails_expiry;
if ( $duration === 30 )
$duration = __( '1 month', 'cookie-notice' );
elseif ( $duration === 90 )
$duration = __( '3 months', 'cookie-notice' );
elseif ( $duration === 182 )
$duration = __( '6 months', 'cookie-notice' );
elseif ( $duration === 365 )
$duration = __( '1 year', 'cookie-notice' );
elseif ( $duration === 730 )
$duration = __( '2 years', 'cookie-notice' );
$html .= '
<tr' . ( $no > 9 ? ' style="display: none"' : '' ) . '>
<td>' . esc_html( $consent_log->ev_eventdetails_consentid ) . '</td>
<td>' . sprintf( esc_html__( 'Level %d', 'cookie-notice' ), $consent_log->ev_consentlevel ) . '</td>
<td>' . implode( ', ', $categories ) . '</td>
<td>' . esc_html( $duration ) . '</td>
<td>' . esc_html( $timestamp->format( get_option( 'time_format' ) ) ) . '</td>
</tr>';
}
}
$html .= '
</tbody>
</table>';
return $html;
}
/**
* Get single row template.
*
* @return string
*/
public function get_single_row_template() {
return '
<tr id="" class="cn-consent-log-details">
<th></th>
<td colspan="5">
<div class="cn-consent-logs-data loading">
<span class="spinner is-active"></span>
</div>
</td>
</tr>';
}
/**
* Get error template.
*
* @return string
*/
public function get_error_template() {
return '<p class="description">' . esc_html__( 'We were unable to download consent logs due to an error. Please try again later.', 'cookie-notice' ) . '</p>';
}
}