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
namespace TotalPoll\Admin\Ajax; ! defined( 'ABSPATH' ) && exit();
use TotalPoll\Contracts\Log\Repository; use TotalPoll\Log\Model; use TotalPollVendors\TotalCore\Contracts\Http\Request; use TotalPollVendors\TotalCore\Export\ColumnTypes\DateColumn; use TotalPollVendors\TotalCore\Export\ColumnTypes\TextColumn; use TotalPollVendors\TotalCore\Export\Spreadsheet; use TotalPollVendors\TotalCore\Export\Writer; use TotalPollVendors\TotalCore\Export\Writers\CsvWriter; use TotalPollVendors\TotalCore\Export\Writers\HTMLWriter; use TotalPollVendors\TotalCore\Export\Writers\JsonWriter;
/** * Class Log * @package TotalPoll\Admin\Ajax * @since 1.0.0 */ class Log { /** * @var Request $request */ protected $request; /** * @var Repository $log */ protected $log; /** * @var array $criteria */ protected $criteria = [];
/** * Log constructor. * * @param Request $request * @param Repository $log */ public function __construct( Request $request, Repository $log ) { $this->request = $request; $this->log = $log;
$this->criteria = [ 'page' => absint( $this->request->request( 'page', 1 ) ), 'poll' => $this->request->request( 'poll', null ), 'from' => $this->request->request( 'from', null ), 'to' => $this->request->request( 'to', null ), 'format' => $this->request->request( 'format', null ), ]; }
/** * Get AJAX endpoint. * @action-callback wp_ajax_totalpoll_log_list */ public function fetch() { $args = [ 'conditions' => [ 'date' => [] ], 'page' => $this->criteria['page'] ];
if ( $this->criteria['poll'] ): $args['conditions']['poll_id'] = $this->criteria['poll']; endif;
if ( $this->criteria['from'] && strptime( $this->criteria['from'], '%Y-%m-%d' ) ): $args['conditions']['date'][] = [ 'operator' => '>=', 'value' => "{$this->criteria['from']} 00:00:00" ]; endif;
if ( $this->criteria['to'] && strptime( $this->criteria['to'], '%Y-%m-%d' ) ): $args['conditions']['date'][] = [ 'operator' => '<=', 'value' => "{$this->criteria['to']} 23:59:59" ]; endif;
$entries = $this->log->get( $args ); /** * Filters the list of log entries sent to log browser. * * @param Model[] $entries Array of log entries models. * @param array $criteria Array of criteria. * * @return array * @since 4.0.0 */ $entries = apply_filters( 'totalpoll/filters/admin/log/fetch', $entries, $this->criteria );
wp_send_json( [ 'entries' => $entries, 'lastPage' => count( $entries ) === 0 || count( $entries ) < 30 ] ); }
/** * Download AJAX endpoint. * @action-callback wp_ajax_totalpoll_log_download */ public function download() { $args = [ 'conditions' => [ 'date' => [] ], 'perPage' => - 1 ];
if ( $this->criteria['poll'] ): $args['conditions']['poll_id'] = $this->criteria['poll']; endif;
if ( $this->criteria['from'] && strptime( $this->criteria['from'], '%Y-%m-%d 00:00:00' ) ): $args['conditions']['date'][] = [ 'operator' => '>=', 'value' => "{$this->criteria['from']} 00:00:00" ]; endif;
if ( $this->criteria['to'] && strptime( $this->criteria['to'], '%Y-%m-%d 23:59:59' ) ): $args['conditions']['date'][] = [ 'operator' => '<=', 'value' => "{$this->criteria['to']} 23:59:59" ]; endif;
$entries = (array) $this->log->get( $args );
/** * Filters the list of log entries to be exported. * * @param Model[] $entries Array of log entries models. * * @return array * @since 4.0.0 */ $entries = apply_filters( 'totalpoll/filters/admin/log/export/entries', $entries );
$export = new Spreadsheet();
$export->addColumn( new TextColumn( 'Status' ) ); $export->addColumn( new TextColumn( 'Action' ) ); $export->addColumn( new DateColumn( 'Date' ) ); $export->addColumn( new TextColumn( 'IP' ) ); $export->addColumn( new TextColumn( 'Browser' ) ); $export->addColumn( new TextColumn( 'Poll' ) ); $export->addColumn( new TextColumn( 'User ID' ) ); $export->addColumn( new TextColumn( 'User login' ) ); $export->addColumn( new TextColumn( 'User name' ) ); $export->addColumn( new TextColumn( 'User email' ) ); $export->addColumn( new TextColumn( 'Entry' ) ); $export->addColumn( new TextColumn( 'Details' ) );
/** * Fires after setup essential columns and before populating data. Useful for define new columns. * * @param Spreadsheet $export Spreadsheet object. * @param array $entries Array of log entries. * * @since 4.0.0 */ do_action( 'totalpoll/actions/admin/log/export/columns', $export, $entries );
foreach ( $entries as $entry ): /** * Filters a row of exported log entries. * * @param array $row Array of values. * @param Model $entry Log entry model. * * @return array * @since 4.0.0 */ $row = apply_filters( 'totalpoll/filters/admin/log/export/row', [ $entry->getStatus(), $entry->getAction(), $entry->getDate(), $entry->getIp(), $entry->getUseragent(), $entry->getPoll()->getTitle(), $entry->getUserId() ?: 'N/A', $entry->getUser()->user_login ?: 'N/A', $entry->getUser()->display_name ?: 'N/A', $entry->getUser()->user_email ?: 'N/A', json_encode( $entry->getEntry() ), $this->criteria['format'] !== 'json' ? json_encode( $entry->getDetails() ) : $entry->getDetails(), ], $entry, $this );
$export->addRow( $row ); endforeach;
if ( empty( $this->criteria['format'] ) ): $this->criteria['format'] = 'default'; endif;
$format = $this->criteria['format'];
if ( $format === 'csv' ): // CSV $writer = new CsvWriter(); elseif ( $format === 'json' ): // JSON $writer = new JsonWriter(); else: // Fallback to HTML $writer = new HTMLWriter(); endif;
/** * Filters the file writer for a specific format when exporting log entries. * * @param Writer $writer Writer object. * * @return Writer * @since 4.0.0 */ $writer = apply_filters( "totalpoll/filters/admin/log/export/writer/{$format}", $writer );
$writer->includeColumnHeaders = true;
$export->download( $writer, 'totalpoll-export-log-' . date( 'Y-m-d H:i:s' ) );
exit; }
}
|