scanner = $scanner; add_action( 'admin_post_adbear_export_csv', array( $this, 'export_csv' ) ); } /** * Export current unused scan result set to CSV. * * @return void */ public function export_csv() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to perform this action.', 'adbear-image-cleaner' ) ); } check_admin_referer( 'adbear_export_csv', 'adbear_export_nonce' ); $rows = $this->scanner->get_unused_results(); $filename = sanitize_file_name( 'adbear-image-cleaner-' . gmdate( 'Ymd-His' ) . '.csv' ); nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); $output = fopen( 'php://output', 'w' ); if ( ! $output ) { wp_die( esc_html__( 'Could not generate CSV output.', 'adbear-image-cleaner' ) ); } fputcsv( $output, array( 'Attachment ID', 'File Name', 'File Path', 'File Size', 'Upload Date', 'Status' ) ); foreach ( $rows as $row ) { fputcsv( $output, array( isset( $row['id'] ) ? (int) $row['id'] : 0, isset( $row['file_name'] ) ? (string) $row['file_name'] : '', isset( $row['file_path'] ) ? (string) $row['file_path'] : '', isset( $row['file_size'] ) ? (int) $row['file_size'] : 0, isset( $row['upload_date'] ) ? (string) $row['upload_date'] : '', isset( $row['status_text'] ) ? (string) $row['status_text'] : '', ) ); } exit; } }