Created
August 9, 2015 20:14
-
-
Save norcross/7cb96c7fe1e855648a94 to your computer and use it in GitHub Desktop.
a basic CSV export example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Devin CSV Export | |
* | |
* Contains our export processing | |
*/ | |
/* | |
Copyright 2015 Reaktiv Studios | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; version 2 of the License (GPL v2) only. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
if ( ! class_exists( 'Devin_CSV_Export' ) ) { | |
// Start up the engine | |
class Devin_CSV_Export | |
{ | |
/** | |
* [__construct description] | |
*/ | |
public function __construct() { | |
add_action( 'admin_menu', array( $this, 'export_menu' ) ); | |
add_action( 'admin_init', array( $this, 'export_init' ) ); | |
} | |
/** | |
* call the menu item to add the export page | |
* | |
* @return [type] [description] | |
*/ | |
public function export_menu() { | |
add_users_page( 'Devin CSV Export', 'Devin CSV Export', 'manage_options', 'devin-csv-export', array( __class__, 'csv_export_page' ) ); | |
} | |
/** | |
* the actual export page | |
* @return [type] [description] | |
*/ | |
public static function csv_export_page() { | |
// wrap the form | |
echo '<div class="wrap custom-sort-wrap">'; | |
// set the form title | |
echo '<h2>' . esc_html( get_admin_page_title() ) . '</h2>'; | |
// begin the form portion itself | |
echo '<form method="post">'; | |
echo wp_nonce_field( 'csv_export_nonce', 'csv_export_nonce', false, false ); | |
echo '<button type="submit" class="button button-primary">Export Data</button>'; | |
echo '<input type="hidden" name="csv-export-trigger" value="go">'; | |
echo '</form>'; | |
echo '</div>'; | |
} | |
/** | |
* trigger our export function | |
* | |
* @return PA_Calculator_Logging | |
*/ | |
public function export_init() { | |
// bail if no access | |
if ( ! current_user_can( 'manage_options' ) ) { | |
return; | |
} | |
// bail if no page reference | |
if ( empty( $_POST['csv-export-trigger'] ) || 'go' !== $_POST['csv-export-trigger'] ) { | |
return; | |
} | |
// check nonce and bail if missing | |
if ( empty( $_POST['csv_export_nonce'] ) || ! wp_verify_nonce( $_POST['csv_export_nonce'], 'csv_export_nonce' ) ) { | |
return; | |
} | |
// fetch the user export data | |
if ( false === $data = self::export_query() ) { | |
return; | |
} | |
// run the export | |
self::process_export( $data ); | |
} | |
/** | |
* the actual export process | |
* | |
* @return | |
*/ | |
public static function process_export( $data = array() ) { | |
// build the file name | |
$file = 'devin_csv_export_' . strftime( '%Y-%m-%d' ); | |
header( 'Pragma: public' ); | |
header( 'Cache-control: max-age=0' ); | |
header( 'Content-Type: text/csv; charset=utf8' ); | |
header( 'Content-Disposition: attachment; filename=' . $file . '.csv' ); | |
// create the file and echo it | |
echo self::csv_create( $data ); | |
// and bail | |
exit(); | |
} | |
/** | |
* create the CSV data | |
* | |
* @return | |
*/ | |
public static function csv_create( $data = array(), $col_sep = ',', $row_sep = "\n", $qut = '"' ) { | |
// bail with no data | |
if ( empty( $data ) ) { | |
return false; | |
} | |
// set an empty | |
$build = ''; | |
// add the header | |
$build .= self::build_export_header(); | |
// Data rows | |
foreach ( $data as $item ) { | |
$field1 = $item['field1']; | |
$field2 = $item['field2']; | |
$field3 = $item['field3']; | |
// set a temp | |
$row = ''; | |
// set columns | |
$row .= "$col_sep$qut$field1$qut"; // first field | |
$row .= "$col_sep$qut$field2$qut"; // second field | |
$row .= "$col_sep$qut$field3$qut"; // third field | |
// add the field | |
$build .= substr( $row, 1 ) . $row_sep; | |
} | |
// return the build file | |
return $build; | |
} | |
/** | |
* Build the header row for a CSV export | |
* | |
* @return string | |
*/ | |
public static function build_export_header() { | |
// Header row | |
$output = '"Field1","Field2","Field3","Field4"'."\n"; | |
// return the header row | |
return $output; | |
} | |
/** | |
* fetch the students for the export | |
* | |
* @return [type] [description] | |
*/ | |
public static function export_query() { | |
// QUERY YOUR DATA HOWEVER YOU NEED TO | |
} | |
// end class | |
} | |
// end exists check | |
} | |
// Instantiate our class | |
new Devin_CSV_Export(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment