PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.2
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.0.2, at classes/class-export.php

252 lines 6.1 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' );
115 break;
116
117 case 'summary':
118 $row_out[ $column_name ] = $record->summary;
119 break;
120
121 case 'user_id':
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 'connector':
127 $row_out[ $column_name ] = $record->connector;
128 break;
129
130 case 'context':
131 $row_out[ $column_name ] = $record->context;
132 break;
133
134 case 'action':
135 $row_out[ $column_name ] = $record->{$column_name};
136 break;
137
138 case 'blog_id':
139 $row_out[ $column_name ] = $record->blog_id;
140 break;
141
142 case 'ip':
143 $row_out[ $column_name ] = $record->{$column_name};
144 break;
145 }
146 }
147
148 return $row_out;
149 }
150
151 /**
152 * Increase pagination limit for CSV Output
153 *
154 * @return int
155 */
156 public function disable_paginate() {
157 return 10000;
158 }
159
160 /**
161 * Expand columns for CSV Output
162 *
163 * @param array $columns Columns currently registered to the list table being exported.
164 * @return array New columns for exporting.
165 */
166 public function expand_columns( $columns ) {
167 $new_columns = array(
168 'date' => $columns['date'],
169 'summary' => $columns['summary'],
170 'user_id' => $columns['user_id'],
171 'connector' => __( 'Connector', 'stream' ),
172 'context' => $columns['context'],
173 'action' => $columns['action'],
174 'ip' => $columns['ip'],
175 );
176
177 if ( is_multisite() && $this->plugin->is_network_activated() ) {
178 $new_columns['blog_id'] = __( 'Blog ID', 'stream' );
179 }
180
181 return $new_columns;
182 }
183
184 /**
185 * Registers all available exporters
186 *
187 * @return void
188 */
189 public function register_exporters() {
190 $exporters = array(
191 'csv',
192 'json',
193 );
194
195 $classes = array();
196 foreach ( $exporters as $exporter ) {
197 include_once $this->plugin->locations['dir'] . '/exporters/class-exporter-' . $exporter . '.php';
198 $class_name = sprintf( '\WP_Stream\Exporter_%s', str_replace( '-', '_', $exporter ) );
199 if ( ! class_exists( $class_name ) ) {
200 continue;
201 }
202 $class = new $class_name();
203 if ( ! property_exists( $class, 'slug' ) ) {
204 continue;
205 }
206 $classes[ $class->slug ] = $class;
207 }
208
209 /**
210 * Allows for adding additional exporters via classes that extend Exporter.
211 *
212 * @param array $classes An array of Exporter objects. In the format exporter_slug => Exporter_Class()
213 */
214 $this->exporters = apply_filters( 'wp_stream_exporters', $classes );
215
216 // Ensure that all exporters extend Exporter.
217 foreach ( $this->exporters as $key => $exporter ) {
218 if ( ! $this->is_valid_exporter( $exporter ) ) {
219 unset( $this->exporters[ $key ] );
220 }
221 }
222 }
223
224 /**
225 * Checks whether an exporter class is valid
226 *
227 * @param Exporter $exporter The class to check.
228 * @return bool
229 */
230 public function is_valid_exporter( $exporter ) {
231 if ( ! is_a( $exporter, 'WP_Stream\Exporter' ) ) {
232 return false;
233 }
234
235 if ( ! method_exists( $exporter, 'is_dependency_satisfied' ) || ! $exporter->is_dependency_satisfied() ) {
236 return false;
237 }
238
239 return true;
240 }
241
242
243 /**
244 * Returns an array with all available exporters
245 *
246 * @return array
247 */
248 public function get_exporters() {
249 return $this->exporters;
250 }
251 }
252