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

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