| 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 |
// Records are only viewable by users with the Stream view capability; |
| 50 |
// don't rely on the nonce alone for authorization. |
| 51 |
if ( ! current_user_can( $this->plugin->admin->view_cap ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$nonce = wp_stream_filter_input( INPUT_GET, 'stream_record_actions_nonce' ); |
| 56 |
if ( ! wp_verify_nonce( $nonce, 'stream_record_actions_nonce' ) ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$action = wp_stream_filter_input( INPUT_GET, 'record-actions' ); |
| 61 |
if ( strpos( $action, 'export-' ) !== 0 ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$output_type = str_replace( 'export-', '', $action ); |
| 66 |
if ( ! array_key_exists( $output_type, $this->get_exporters() ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$this->plugin->admin->register_list_table(); |
| 71 |
$list_table = $this->plugin->admin->list_table; |
| 72 |
$list_table->prepare_items(); |
| 73 |
add_filter( 'stream_records_per_page', array( $this, 'disable_paginate' ) ); |
| 74 |
add_filter( 'wp_stream_list_table_columns', array( $this, 'expand_columns' ), 10, 1 ); |
| 75 |
|
| 76 |
$records = $list_table->get_records(); |
| 77 |
$columns = $list_table->get_columns(); |
| 78 |
$output = array(); |
| 79 |
foreach ( $records as $item ) { |
| 80 |
$output[] = $this->build_record( $item, $columns ); |
| 81 |
} |
| 82 |
|
| 83 |
$exporters = $this->get_exporters(); |
| 84 |
$exporter = $exporters[ $output_type ]; |
| 85 |
$exporter->output_file( $output, $columns ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Add Export options to record actions menu |
| 90 |
* |
| 91 |
* @param array $action_menu_items Export types. |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function actions_menu_export_items( $action_menu_items ) { |
| 96 |
foreach ( $this->get_exporters() as $exporter ) { |
| 97 |
$action = 'export-' . $exporter->slug; |
| 98 |
/* translators: %s: an export format (e.g. "CSV") */ |
| 99 |
$action_menu_items[ $action ] = sprintf( __( 'Export as %s', 'stream' ), $exporter->name ); |
| 100 |
} |
| 101 |
|
| 102 |
return $action_menu_items; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Extracts data from Records |
| 107 |
* |
| 108 |
* @param array $item Post to extract data from. |
| 109 |
* @param array $columns Columns being extracted. |
| 110 |
* @return array Numerically-indexed array with extracted data. |
| 111 |
*/ |
| 112 |
public function build_record( $item, $columns ) { |
| 113 |
$record = new Record( $item ); |
| 114 |
|
| 115 |
$row_out = array(); |
| 116 |
foreach ( array_keys( $columns ) as $column_name ) { |
| 117 |
switch ( $column_name ) { |
| 118 |
case 'date': |
| 119 |
$created = gmdate( 'Y-m-d H:i:s', strtotime( $record->created ) ); |
| 120 |
$row_out[ $column_name ] = get_date_from_gmt( $created, 'Y/m/d h:i:s A T' ); |
| 121 |
break; |
| 122 |
|
| 123 |
case 'summary': |
| 124 |
$row_out[ $column_name ] = $record->summary; |
| 125 |
break; |
| 126 |
|
| 127 |
case 'user': |
| 128 |
$user = new Author( (int) $record->user_id, (array) $record->user_meta ); |
| 129 |
$row_out[ $column_name ] = $user->get_display_name(); |
| 130 |
break; |
| 131 |
|
| 132 |
case 'user_id': |
| 133 |
$row_out[ $column_name ] = $record->user_id; |
| 134 |
break; |
| 135 |
|
| 136 |
case 'connector': |
| 137 |
$row_out[ $column_name ] = $record->connector; |
| 138 |
break; |
| 139 |
|
| 140 |
case 'context': |
| 141 |
$row_out[ $column_name ] = $record->context; |
| 142 |
break; |
| 143 |
|
| 144 |
case 'object_id': |
| 145 |
$row_out[ $column_name ] = $record->object_id; |
| 146 |
break; |
| 147 |
|
| 148 |
case 'action': |
| 149 |
$row_out[ $column_name ] = $record->{$column_name}; |
| 150 |
break; |
| 151 |
|
| 152 |
case 'blog_id': |
| 153 |
$row_out[ $column_name ] = $record->blog_id; |
| 154 |
break; |
| 155 |
|
| 156 |
case 'ip': |
| 157 |
$row_out[ $column_name ] = $record->{$column_name}; |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return $row_out; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Increase pagination limit for CSV Output |
| 167 |
* |
| 168 |
* @return int |
| 169 |
*/ |
| 170 |
public function disable_paginate() { |
| 171 |
|
| 172 |
/** |
| 173 |
* Filter to change how many records are exported. |
| 174 |
* Increasing this too much could cause your export to time out. |
| 175 |
* |
| 176 |
* @return int The number of records to export. |
| 177 |
*/ |
| 178 |
return apply_filters( 'wp_stream_export_limit', 10000 ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Expand columns for CSV Output |
| 183 |
* |
| 184 |
* @param array $columns Columns currently registered to the list table being exported. |
| 185 |
* @return array New columns for exporting. |
| 186 |
*/ |
| 187 |
public function expand_columns( $columns ) { |
| 188 |
$new_columns = array( |
| 189 |
'date' => $columns['date'], |
| 190 |
'summary' => $columns['summary'], |
| 191 |
'user' => $columns['user_id'], |
| 192 |
'user_id' => __( 'User ID', 'stream' ), |
| 193 |
'connector' => __( 'Connector', 'stream' ), |
| 194 |
'context' => $columns['context'], |
| 195 |
'object_id' => __( 'Object ID', 'stream' ), |
| 196 |
'action' => $columns['action'], |
| 197 |
'ip' => $columns['ip'], |
| 198 |
); |
| 199 |
|
| 200 |
if ( is_multisite() && $this->plugin->is_network_activated() ) { |
| 201 |
$new_columns['blog_id'] = __( 'Blog ID', 'stream' ); |
| 202 |
} |
| 203 |
|
| 204 |
return $new_columns; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Registers all available exporters |
| 209 |
* |
| 210 |
* @return void |
| 211 |
*/ |
| 212 |
public function register_exporters() { |
| 213 |
$exporters = array( |
| 214 |
'csv', |
| 215 |
'json', |
| 216 |
); |
| 217 |
|
| 218 |
$classes = array(); |
| 219 |
foreach ( $exporters as $exporter ) { |
| 220 |
include_once $this->plugin->locations['dir'] . '/exporters/class-exporter-' . $exporter . '.php'; |
| 221 |
$class_name = sprintf( '\WP_Stream\Exporter_%s', str_replace( '-', '_', $exporter ) ); |
| 222 |
if ( ! class_exists( $class_name ) ) { |
| 223 |
continue; |
| 224 |
} |
| 225 |
$class = new $class_name(); |
| 226 |
if ( ! property_exists( $class, 'slug' ) ) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
$classes[ $class->slug ] = $class; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Allows for adding additional exporters via classes that extend Exporter. |
| 234 |
* |
| 235 |
* @param array $classes An array of Exporter objects. In the format exporter_slug => Exporter_Class() |
| 236 |
*/ |
| 237 |
$this->exporters = apply_filters( 'wp_stream_exporters', $classes ); |
| 238 |
|
| 239 |
// Ensure that all exporters extend Exporter. |
| 240 |
foreach ( $this->exporters as $key => $exporter ) { |
| 241 |
if ( ! $this->is_valid_exporter( $exporter ) ) { |
| 242 |
unset( $this->exporters[ $key ] ); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Checks whether an exporter class is valid |
| 249 |
* |
| 250 |
* @param Exporter $exporter The class to check. |
| 251 |
* @return bool |
| 252 |
*/ |
| 253 |
public function is_valid_exporter( $exporter ) { |
| 254 |
if ( ! is_a( $exporter, 'WP_Stream\Exporter' ) ) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
if ( ! method_exists( $exporter, 'is_dependency_satisfied' ) || ! $exporter->is_dependency_satisfied() ) { |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
/** |
| 267 |
* Returns an array with all available exporters |
| 268 |
* |
| 269 |
* @return array |
| 270 |
*/ |
| 271 |
public function get_exporters() { |
| 272 |
return $this->exporters; |
| 273 |
} |
| 274 |
} |
| 275 |
|