Repository navigation
Expand file tree
/
Copy pathclass-export.php
More file actions
274 lines (234 loc) · 6.76 KB
/
Copy pathclass-export.php
File metadata and controls
274 lines (234 loc) · 6.76 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* Loads & Manages exporter classes.
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class - Export
*/
class Export {
/**
* Holds instance of plugin object
*
* @var Plugin
*/
public $plugin;
/**
* Hold registered exporters
*
* @var array
*/
protected $exporters = array();
/**
* Class constructor
*
* @param Plugin $plugin The plugin object.
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
if ( 'wp_stream' === wp_stream_filter_input( INPUT_GET, 'page' ) ) {
add_action( 'admin_init', array( $this, 'render_download' ) );
add_action( 'wp_stream_record_actions_menu', array( $this, 'actions_menu_export_items' ) );
$this->register_exporters();
}
}
/**
* Outputs download file to user based on selected exporter
*
* @return void
*/
public function render_download() {
// Records are only viewable by users with the Stream view capability;
// don't rely on the nonce alone for authorization.
if ( ! current_user_can( $this->plugin->admin->view_cap ) ) {
return;
}
$nonce = wp_stream_filter_input( INPUT_GET, 'stream_record_actions_nonce' );
if ( ! wp_verify_nonce( $nonce, 'stream_record_actions_nonce' ) ) {
return;
}
$action = wp_stream_filter_input( INPUT_GET, 'record-actions' );
if ( strpos( $action, 'export-' ) !== 0 ) {
return;
}
$output_type = str_replace( 'export-', '', $action );
if ( ! array_key_exists( $output_type, $this->get_exporters() ) ) {
return;
}
$this->plugin->admin->records->register_list_table();
$list_table = $this->plugin->admin->list_table;
$list_table->prepare_items();
add_filter( 'stream_records_per_page', array( $this, 'disable_paginate' ) );
add_filter( 'wp_stream_list_table_columns', array( $this, 'expand_columns' ), 10, 1 );
$records = $list_table->get_records();
$columns = $list_table->get_columns();
$output = array();
foreach ( $records as $item ) {
$output[] = $this->build_record( $item, $columns );
}
$exporters = $this->get_exporters();
$exporter = $exporters[ $output_type ];
$exporter->output_file( $output, $columns );
}
/**
* Add Export options to record actions menu
*
* @param array $action_menu_items Export types.
*
* @return array
*/
public function actions_menu_export_items( $action_menu_items ) {
foreach ( $this->get_exporters() as $exporter ) {
$action = 'export-' . $exporter->slug;
/* translators: %s: an export format (e.g. "CSV") */
$action_menu_items[ $action ] = sprintf( __( 'Export as %s', 'stream' ), $exporter->name );
}
return $action_menu_items;
}
/**
* Extracts data from Records
*
* @param array $item Post to extract data from.
* @param array $columns Columns being extracted.
* @return array Numerically-indexed array with extracted data.
*/
public function build_record( $item, $columns ) {
$record = new Record( $item );
$row_out = array();
foreach ( array_keys( $columns ) as $column_name ) {
switch ( $column_name ) {
case 'date':
$created = gmdate( 'Y-m-d H:i:s', strtotime( $record->created ) );
$row_out[ $column_name ] = get_date_from_gmt( $created, 'Y/m/d h:i:s A T' );
break;
case 'summary':
$row_out[ $column_name ] = $record->summary;
break;
case 'user':
$user = new Author( (int) $record->user_id, (array) $record->user_meta );
$row_out[ $column_name ] = $user->get_display_name();
break;
case 'user_id':
$row_out[ $column_name ] = $record->user_id;
break;
case 'connector':
$row_out[ $column_name ] = $record->connector;
break;
case 'context':
$row_out[ $column_name ] = $record->context;
break;
case 'object_id':
$row_out[ $column_name ] = $record->object_id;
break;
case 'action':
$row_out[ $column_name ] = $record->{$column_name};
break;
case 'blog_id':
$row_out[ $column_name ] = $record->blog_id;
break;
case 'ip':
$row_out[ $column_name ] = $record->{$column_name};
break;
}
}
return $row_out;
}
/**
* Increase pagination limit for CSV Output
*
* @return int
*/
public function disable_paginate() {
/**
* Filter to change how many records are exported.
* Increasing this too much could cause your export to time out.
*
* @param int $limit The number of records to export.
*/
return apply_filters( 'wp_stream_export_limit', 10000 );
}
/**
* Expand columns for CSV Output
*
* @param array $columns Columns currently registered to the list table being exported.
* @return array New columns for exporting.
*/
public function expand_columns( $columns ) {
$new_columns = array(
'date' => $columns['date'],
'summary' => $columns['summary'],
'user' => $columns['user_id'],
'user_id' => __( 'User ID', 'stream' ),
'connector' => __( 'Connector', 'stream' ),
'context' => $columns['context'],
'object_id' => __( 'Object ID', 'stream' ),
'action' => $columns['action'],
'ip' => $columns['ip'],
);
if ( is_multisite() && $this->plugin->is_network_activated() ) {
$new_columns['blog_id'] = __( 'Blog ID', 'stream' );
}
return $new_columns;
}
/**
* Registers all available exporters
*
* @return void
*/
public function register_exporters() {
$exporters = array(
'csv',
'json',
);
$classes = array();
foreach ( $exporters as $exporter ) {
include_once $this->plugin->locations['dir'] . '/exporters/class-exporter-' . $exporter . '.php';
$class_name = sprintf( '\WP_Stream\Exporter_%s', str_replace( '-', '_', $exporter ) );
if ( ! class_exists( $class_name ) ) {
continue;
}
$class = new $class_name();
if ( ! property_exists( $class, 'slug' ) ) {
continue;
}
$classes[ $class->slug ] = $class;
}
/**
* Allows for adding additional exporters via classes that extend Exporter.
*
* @param array $classes An array of Exporter objects. In the format exporter_slug => Exporter_Class()
*/
$this->exporters = apply_filters( 'wp_stream_exporters', $classes );
// Ensure that all exporters extend Exporter.
foreach ( $this->exporters as $key => $exporter ) {
if ( ! $this->is_valid_exporter( $exporter ) ) {
unset( $this->exporters[ $key ] );
}
}
}
/**
* Checks whether an exporter class is valid
*
* @param Exporter $exporter The class to check.
* @return bool
*/
public function is_valid_exporter( $exporter ) {
if ( ! is_a( $exporter, 'WP_Stream\Exporter' ) ) {
return false;
}
if ( ! method_exists( $exporter, 'is_dependency_satisfied' ) || ! $exporter->is_dependency_satisfied() ) {
return false;
}
return true;
}
/**
* Returns an array with all available exporters
*
* @return array
*/
public function get_exporters() {
return $this->exporters;
}
}