Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created February 1, 2019 16:11
Show Gist options
  • Save lgedeon/cbeca414a2815333a8584d5b6b1d3a7c to your computer and use it in GitHub Desktop.
Save lgedeon/cbeca414a2815333a8584d5b6b1d3a7c to your computer and use it in GitHub Desktop.
Export a CSV report of all media in a WordPress Site
<?php
/**
* Media Library Report class
*
* Adds a Media Library Report option to WP Admin / Tools / Export.
*/
class Media_Library_Report {
/**
* Set up all the hooks.
*
* @return void
*/
public function init() {
add_action( 'export_filters', array( $this, 'add_export_option' ) );
add_filter( 'export_args', array( $this, 'export_args' ) );
add_action( 'export_wp', array( $this, 'export' ) );
add_action( 'admin_init', array( $this, 'register_importer' ) );
}
/**
* Add an option to export report.
*/
public function add_export_option() {
?>
<p><label><input type="radio" name="content" value="media-library-report" /> <?php esc_html_e( 'Media Library Report (csv)', 'plugin' ); ?></label></p>
<?php
}
/**
* Prevent the normal xml file from loading and set up for the report.
*
* @param array $args The export args being filtered.
*
* @return array
*/
public function export_args( $args ) {
if ( 'media-library-report' === filter_input( INPUT_GET, 'content', FILTER_SANITIZE_STRING ) ) {
return array( 'media-library-report' => true );
}
return $args;
}
/**
* Get array of arrays to export.
*
* @return array
*/
public function get_media_attachments() {
$rows = [];
$query_args = [
'post_type' => 'attachment',
'posts_per_page' => '-1',
'post_status' => 'any',
'suppress_filters' => false,
];
add_filter( 'pre_get_posts', [ $this, 'reset_query' ], 999 );
$query = new WP_Query( $query_args );
remove_filter( 'pre_get_posts', [ $this, 'reset_query' ], 999 );
foreach ( $query->posts as $post ) {
$rows[] = [
'post_title' => $post->post_title,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'ione_image_credit_meta' => get_post_meta( $post->ID, '_image_credit_meta', true ),
'_wp_attachment_image_alt' => get_post_meta( $post->ID, '_wp_attachment_image_alt', true ),
'_ione_media_id' => get_post_meta( $post->ID, '_plugin_media_id', true ),
'_wp_attached_file' => get_post_meta( $post->ID, '_wp_attached_file', true ),
];
}
return $rows;
}
/**
* Clear out meta queries set elsewhere in the code.
*
* Can be extended in the future to clear out other modifications to our query.
*
* @param WP_Query $query Query set in get_media_attachments().
*/
public function reset_query( $query ) {
// Only reset what you have to for this query. May have to var_export to find out what has been added here that should not have been.
$query->set( 'meta_key', '' );
$query->set( 'meta_query', new WP_Meta_Query() );
}
/**
* Do the export.
*
* @param array $args The export arguments.
*/
public function export( $args ) {
$this->get_media_attachments();
if ( ! empty( $args['media-library-report'] ) ) {
$sitename = sanitize_key( get_bloginfo( 'name' ) ) ?: 'nameless-site';
$this->do_headers( sprintf( '%s-media-library-report-%s.csv', $sitename, date( 'Y-m-d' ) ) );
$this->output_array_as_csv( $this->get_media_attachments() );
exit();
}
}
/**
* Set headers including filename.
*
* @param string $filename Export csv with this filename.
*/
public function do_headers( $filename ) {
header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Description: Media Library Report' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );
}
/**
* Output an array of arrays as csv content.
*
* Assumes headers are already sent and that no other content is being sent.
*
* Each inner array must have the same number of fields and at least the first array
* must have keys that are used column headers.
*
* @param array $rows Array of arrays containing data to output.
*/
public function output_array_as_csv( $rows ) {
$file = fopen( 'php://output', 'w' );
fputcsv( $file, array_keys( reset( $rows ) ) );
foreach ( $rows as $row ) {
fputcsv( $file, $row );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment