PluginProbe ʕ •ᴥ•ʔ
MainWP Child Reports / 2.2
MainWP Child Reports v2.2
0.0.1 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9.1 1.9.2 1.9.3 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1 2.1.1 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.3 2.3.1 trunk
mainwp-child-reports / classes / class-export.php
mainwp-child-reports / classes Last commit date
class-admin.php 2 years ago class-author.php 5 years ago class-cli.php 5 years ago class-connector.php 2 years ago class-connectors.php 3 years ago class-date-interval.php 5 years ago class-db-driver-wpdb.php 2 years ago class-db-driver.php 5 years ago class-db.php 3 years ago class-export.php 5 years ago class-exporter.php 5 years ago class-filter-input.php 3 years ago class-form-generator.php 5 years ago class-install.php 3 years ago class-list-table.php 4 years ago class-live-update.php 3 years ago class-log.php 4 years ago class-mainwp-child-report-helper.php 5 years ago class-network.php 3 years ago class-plugin.php 3 years ago class-preview-list-table.php 5 years ago class-query.php 4 years ago class-record.php 5 years ago class-settings.php 2 years ago class-uninstall.php 2 years ago
class-export.php
259 lines
1 <?php
2 /** MainWP Child Reports export. */
3 namespace WP_MainWP_Stream;
4
5 /**
6 * Class Export.
7 * @package WP_MainWP_Stream
8 */
9 class Export {
10 /**
11 * Hold Plugin class
12 *
13 * @var Plugin
14 */
15 public $plugin;
16
17 /**
18 * Hold registered exporters
19 *
20 * @var array
21 */
22 protected $exporters = array();
23
24 /**
25 * Export constructor
26 *
27 * Run each time the class is called.
28 *
29 * @param Plugin $plugin The plugin object.
30 */
31 public function __construct( $plugin ) {
32 $this->plugin = $plugin;
33
34 if ( 'wp_mainwp_stream' === wp_mainwp_stream_filter_input( INPUT_GET, 'page' ) ) {
35 add_action( 'admin_init', array( $this, 'render_download' ) );
36 add_action( 'wp_mainwp_stream_record_actions_menu', array( $this, 'actions_menu_export_items' ) );
37 $this->register_exporters();
38 }
39 }
40
41 /**
42 * Outputs download file to user based on selected exporter
43 *
44 * @return void
45 */
46 public function render_download() {
47 $nonce = wp_mainwp_stream_filter_input( INPUT_GET, 'stream_record_actions_nonce' );
48 if ( ! wp_verify_nonce( $nonce, 'stream_record_actions_nonce' ) ) {
49 return;
50 }
51
52 $action = wp_mainwp_stream_filter_input( INPUT_GET, 'record-actions' );
53 if ( strpos( $action, 'export-' ) !== 0 ) {
54 return;
55 }
56
57 $output_type = str_replace( 'export-', '', $action );
58 if ( ! array_key_exists( $output_type, $this->get_exporters() ) ) {
59 return;
60 }
61
62 $this->plugin->admin->register_list_table();
63 $list_table = $this->plugin->admin->list_table;
64 $list_table->prepare_items();
65 add_filter( 'mainwp_stream_records_per_page', array( $this, 'disable_paginate' ) );
66 add_filter( 'wp_mainwp_stream_list_table_columns', array( $this, 'expand_columns' ), 10, 1 );
67
68 $records = $list_table->get_records();
69 $columns = $list_table->get_columns();
70 $output = array();
71 foreach ( $records as $item ) {
72 $output[] = $this->build_record( $item, $columns );
73 }
74
75 $exporters = $this->get_exporters();
76 $exporter = $exporters[ $output_type ];
77 $exporter->output_file( $output, $columns );
78 }
79
80 /**
81 * Add Export options to record actions menu.
82 *
83 * @param array $action_menu_items Action menu items.
84 *
85 * @return array $action_menu_items Return Action Menu Items.
86 *
87 * @uses \WP_MainWP_Stream\Author::get_exporters()
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: Placeholder refers to an export format (e.g. "CSV").
93 $action_menu_items[ $action ] = sprintf( __( 'Export as %s', 'mainwp-child-reports' ), $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 *
105 * @return array Numerically-indexed array with extracted data.
106 *
107 * @uses \WP_MainWP_Stream\Author
108 * @uses \WP_MainWP_Stream\Record
109 */
110 public function build_record( $item, $columns ) {
111 $record = new Record( $item );
112
113 $row_out = array();
114 foreach ( array_keys( $columns ) as $column_name ) {
115 switch ( $column_name ) {
116 case 'date':
117 $created = date( 'Y-m-d H:i:s', strtotime( $record->created ) );
118 $row_out[ $column_name ] = get_date_from_gmt( $created, 'Y/m/d h:i:s A' );
119 break;
120
121 case 'summary':
122 $row_out[ $column_name ] = $record->summary;
123 break;
124
125 case 'user_id':
126 $user = new Author( (int) $record->user_id, (array) $record->user_meta );
127 $row_out[ $column_name ] = $user->get_display_name();
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 'action':
139 $row_out[ $column_name ] = $record->{$column_name};
140 break;
141
142 case 'blog_id':
143 $row_out[ $column_name ] = $record->blog_id;
144 break;
145
146 case 'ip':
147 $row_out[ $column_name ] = $record->{$column_name};
148 break;
149 }
150 }
151
152 return $row_out;
153 }
154
155 /**
156 * Increase pagination limit for CSV Output
157 *
158 * @param int $records_per_page Old limit for records_per_page.
159 * @return int
160 */
161 public function disable_paginate( $records_per_page ) {
162 return 10000;
163 }
164
165 /**
166 * Expand columns for CSV Output
167 *
168 * @param array $columns Columns currently registered to the list table being exported.
169 * @return array New columns for exporting.
170 */
171 public function expand_columns( $columns ) {
172 $new_columns = array(
173 'date' => $columns['date'],
174 'summary' => $columns['summary'],
175 'user_id' => $columns['user_id'],
176 'connector' => __( 'Connector', 'mainwp-child-reports' ),
177 'context' => $columns['context'],
178 'action' => $columns['action'],
179 'ip' => $columns['ip'],
180 );
181
182 if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
183 $new_columns['blog_id'] = __( 'Blog ID', 'mainwp-child-reports' );
184 }
185
186 return $new_columns;
187 }
188
189 /**
190 * Registers all available exporters
191 *
192 * @return void
193 */
194 public function register_exporters() {
195 $exporters = array(
196 'csv',
197 'json',
198 );
199
200 $classes = array();
201 foreach ( $exporters as $exporter ) {
202 include_once $this->plugin->locations['dir'] . '/exporters/class-exporter-' . $exporter . '.php';
203 $class_name = sprintf( '\WP_MainWP_Stream\Exporter_%s', str_replace( '-', '_', $exporter ) );
204 if ( ! class_exists( $class_name ) ) {
205 continue;
206 }
207 $class = new $class_name();
208 if ( ! property_exists( $class, 'slug' ) ) {
209 continue;
210 }
211 $classes[ $class->slug ] = $class;
212 }
213
214 /**
215 * Allows for adding additional exporters via classes that extend Exporter.
216 *
217 * @param array $classes An array of Exporter objects. In the format exporter_slug => Exporter_Class()
218 */
219 $this->exporters = apply_filters( 'wp_mainwp_stream_exporters', $classes );
220
221 // Ensure that all exporters extend Exporter
222 foreach ( $this->exporters as $key => $exporter ) {
223 if ( ! $this->is_valid_exporter( $exporter ) ) {
224 unset( $this->exporters[ $key ] );
225 }
226 }
227 }
228
229 /**
230 * Checks whether an exporter class is valid
231 *
232 * @param Exporter $exporter The class to check.
233 * @return bool
234 *
235 * @uses \WP_MainWP_Stream\Exporter
236 */
237 public function is_valid_exporter( $exporter ) {
238 if ( ! is_a( $exporter, 'WP_MainWP_Stream\Exporter' ) ) {
239 return false;
240 }
241
242 if ( ! method_exists( $exporter, 'is_dependency_satisfied' ) || ! $exporter->is_dependency_satisfied() ) {
243 return false;
244 }
245
246 return true;
247 }
248
249
250 /**
251 * Returns an array with all available exporters
252 *
253 * @return array
254 */
255 public function get_exporters() {
256 return $this->exporters;
257 }
258 }
259