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

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