PluginProbe
Stream – Activity Log & Audit Trail / 4.1.0
Stream – Activity Log & Audit Trail v4.1.0
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-export.php

class-export.php in Stream – Activity Log & Audit Trail 4.1.0, at classes/class-export.php

269 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Loads & Manages exporter classes.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - Export
12 */
13 class Export {
14 /**
15 * Holds instance of plugin object
16 *
17 * @var Plugin
18 */
19 public $plugin;
20
21 /**
22 * Hold registered exporters
23 *
24 * @var array
25 */
26 protected $exporters = array();
27
28 /**
29 * Class constructor
30 *
31 * @param Plugin $plugin The plugin object.
32 */
33 public function __construct( $plugin ) {
34 $this->plugin = $plugin;
35
36 if ( 'wp_stream' === wp_stream_filter_input( INPUT_GET, 'page' ) ) {
37 add_action( 'admin_init', array( $this, 'render_download' ) );
38 add_action( 'wp_stream_record_actions_menu', array( $this, 'actions_menu_export_items' ) );
39 $this->register_exporters();
40 }
41 }
42
43 /**
44 * Outputs download file to user based on selected exporter
45 *
46 * @return void
47 */
48 public function render_download() {
49 $nonce = wp_stream_filter_input( INPUT_GET, 'stream_record_actions_nonce' );
50 if ( ! wp_verify_nonce( $nonce, 'stream_record_actions_nonce' ) ) {
51 return;
52 }
53
54 $action = wp_stream_filter_input( INPUT_GET, 'record-actions' );
55 if ( strpos( $action, 'export-' ) !== 0 ) {
56 return;
57 }
58
59 $output_type = str_replace( 'export-', '', $action );
60 if ( ! array_key_exists( $output_type, $this->get_exporters() ) ) {
61 return;
62 }
63
64 $this->plugin->admin->register_list_table();
65 $list_table = $this->plugin->admin->list_table;
66 $list_table->prepare_items();
67 add_filter( 'stream_records_per_page', array( $this, 'disable_paginate' ) );
68 add_filter( 'wp_stream_list_table_columns', array( $this, 'expand_columns' ), 10, 1 );
69
70 $records = $list_table->get_records();
71 $columns = $list_table->get_columns();
72 $output = array();
73 foreach ( $records as $item ) {
74 $output[] = $this->build_record( $item, $columns );
75 }
76
77 $exporters = $this->get_exporters();
78 $exporter = $exporters[ $output_type ];
79 $exporter->output_file( $output, $columns );
80 }
81
82 /**
83 * Add Export options to record actions menu
84 *
85 * @param array $action_menu_items Export types.
86 *
87 * @return array
88 */
89 public function actions_menu_export_items( $action_menu_items ) {
90 foreach ( $this->get_exporters() as $exporter ) {
91 $action = 'export-' . $exporter->slug;
92 /* translators: %s: an export format (e.g. "CSV") */
93 $action_menu_items[ $action ] = sprintf( __( 'Export as %s', 'stream' ), $exporter->name );
94 }
95
96 return $action_menu_items;
97 }
98
99 /**
100 * Extracts data from Records
101 *
102 * @param array $item Post to extract data from.
103 * @param array $columns Columns being extracted.
104 * @return array Numerically-indexed array with extracted data.
105 */
106 public function build_record( $item, $columns ) {
107 $record = new Record( $item );
108
109 $row_out = array();
110 foreach ( array_keys( $columns ) as $column_name ) {
111 switch ( $column_name ) {
112 case 'date':
113 $created = gmdate( 'Y-m-d H:i:s', strtotime( $record->created ) );
114 $row_out[ $column_name ] = get_date_from_gmt( $created, 'Y/m/d h:i:s A T' );
115 break;
116
117 case 'summary':
118 $row_out[ $column_name ] = $record->summary;
119 break;
120
121 case 'user':
122 $user = new Author( (int) $record->user_id, (array) $record->user_meta );
123 $row_out[ $column_name ] = $user->get_display_name();
124 break;
125
126 case 'user_id':
127 $row_out[ $column_name ] = $record->user_id;
128 break;
129
130 case 'connector':
131 $row_out[ $column_name ] = $record->connector;
132 break;
133
134 case 'context':
135 $row_out[ $column_name ] = $record->context;
136 break;
137
138 case 'object_id':
139 $row_out[ $column_name ] = $record->object_id;
140 break;
141
142 case 'action':
143 $row_out[ $column_name ] = $record->{$column_name};
144 break;
145
146 case 'blog_id':
147 $row_out[ $column_name ] = $record->blog_id;
148 break;
149
150 case 'ip':
151 $row_out[ $column_name ] = $record->{$column_name};
152 break;
153 }
154 }
155
156 return $row_out;
157 }
158
159 /**
160 * Increase pagination limit for CSV Output
161 *
162 * @return int
163 */
164 public function disable_paginate() {
165
166 /**
167 * Filter to change how many records are exported.
168 * Increasing this too much could cause your export to time out.
169 *
170 * @return int The number of records to export.
171 */
172 return apply_filters( 'wp_stream_export_limit', 10000 );
173 }
174
175 /**
176 * Expand columns for CSV Output
177 *
178 * @param array $columns Columns currently registered to the list table being exported.
179 * @return array New columns for exporting.
180 */
181 public function expand_columns( $columns ) {
182 $new_columns = array(
183 'date' => $columns['date'],
184 'summary' => $columns['summary'],
185 'user' => $columns['user_id'],
186 'user_id' => __( 'User ID', 'stream' ),
187 'connector' => __( 'Connector', 'stream' ),
188 'context' => $columns['context'],
189 'object_id' => __( 'Object ID', 'stream' ),
190 'action' => $columns['action'],
191 'ip' => $columns['ip'],
192 );
193
194 if ( is_multisite() && $this->plugin->is_network_activated() ) {
195 $new_columns['blog_id'] = __( 'Blog ID', 'stream' );
196 }
197
198 return $new_columns;
199 }
200
201 /**
202 * Registers all available exporters
203 *
204 * @return void
205 */
206 public function register_exporters() {
207 $exporters = array(
208 'csv',
209 'json',
210 );
211
212 $classes = array();
213 foreach ( $exporters as $exporter ) {
214 include_once $this->plugin->locations['dir'] . '/exporters/class-exporter-' . $exporter . '.php';
215 $class_name = sprintf( '\WP_Stream\Exporter_%s', str_replace( '-', '_', $exporter ) );
216 if ( ! class_exists( $class_name ) ) {
217 continue;
218 }
219 $class = new $class_name();
220 if ( ! property_exists( $class, 'slug' ) ) {
221 continue;
222 }
223 $classes[ $class->slug ] = $class;
224 }
225
226 /**
227 * Allows for adding additional exporters via classes that extend Exporter.
228 *
229 * @param array $classes An array of Exporter objects. In the format exporter_slug => Exporter_Class()
230 */
231 $this->exporters = apply_filters( 'wp_stream_exporters', $classes );
232
233 // Ensure that all exporters extend Exporter.
234 foreach ( $this->exporters as $key => $exporter ) {
235 if ( ! $this->is_valid_exporter( $exporter ) ) {
236 unset( $this->exporters[ $key ] );
237 }
238 }
239 }
240
241 /**
242 * Checks whether an exporter class is valid
243 *
244 * @param Exporter $exporter The class to check.
245 * @return bool
246 */
247 public function is_valid_exporter( $exporter ) {
248 if ( ! is_a( $exporter, 'WP_Stream\Exporter' ) ) {
249 return false;
250 }
251
252 if ( ! method_exists( $exporter, 'is_dependency_satisfied' ) || ! $exporter->is_dependency_satisfied() ) {
253 return false;
254 }
255
256 return true;
257 }
258
259
260 /**
261 * Returns an array with all available exporters
262 *
263 * @return array
264 */
265 public function get_exporters() {
266 return $this->exporters;
267 }
268 }
269