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

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