Created
January 7, 2022 16:02
-
-
Save michaeluno/25ddf4119be929cfe5e4a4b561703837 to your computer and use it in GitHub Desktop.
This is a WordPress plugin that demonstrates how dynamic CSV data are downloaded using the export button with Admin Page Framework.
This file contains 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 | |
/** | |
* Plugin Name: Admin Page Framework - CSV Download | |
* Description: Demonstrates how a CSV file is downloaded using the export button. | |
* Version: 0.0.1 | |
* Author: Michael Uno | |
*/ | |
namespace AdminPageFrameworkCSVDownload; | |
class Load { | |
public function __construct() { | |
add_action( 'plugins_loaded', array( $this, 'load' ) ); | |
} | |
public function load() { | |
if ( ! class_exists( 'AdminPageFramework' ) ) { | |
return; | |
} | |
include( __DIR__ . '/AdminPage.php' ); | |
new AdminPage; | |
include( __DIR__ . '/CSVGenerator.php' ); | |
new CSVGenerator; | |
} | |
} | |
new Load; |
This file contains 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 | |
namespace AdminPageFrameworkCSVDownload; | |
class AdminPage extends \AdminPageFramework { | |
public function setUp() { | |
$this->setRootMenuPage( 'Settings' ); | |
// Add the sub menus and the pages | |
$this->addSubMenuItems( | |
array( | |
'title' => 'CSV Download', // the page and menu title | |
'page_slug' => 'apf_csv_download', // the page slug | |
) | |
); | |
$this->addInPageTabs( | |
'apf_csv_download', | |
array( | |
'tab_slug' => 'sample', // avoid hyphen(dash), dots, and white spaces | |
'title' => __( 'Sample', 'admin-page-framework-csv-download' ), | |
), | |
array( | |
'tab_slug' => 'download', | |
'title' => __( 'Download', 'admin-page-framework-csv-download' ), | |
) | |
); | |
} | |
/** | |
* @callback add_action() do_{page slug}_{tab slug} | |
*/ | |
public function do_apf_csv_download_sample() { | |
echo "<h2>Sample</h2>" | |
. "<p>This is a sample tab.</p>"; | |
} | |
/** | |
* @callback add_action() load_{page slug}_{tab slug} | |
*/ | |
public function load_apf_csv_download_download() { | |
$this->addSettingFields( | |
array( | |
'field_id' => 'export', | |
'save' => false, | |
'type' => 'export', | |
'value' => __( 'Download', 'admin-page-framework-csv-download' ), | |
'description' => __( 'Export data as CSV.', 'admin-page-framework-csv-download' ), | |
'file_name' => 'test.csv', | |
'show_title_column' => false, | |
) | |
); | |
} | |
} |
This file contains 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 | |
namespace AdminPageFrameworkCSVDownload; | |
class CSVGenerator { | |
/** | |
* Sets up properties and hooks. | |
*/ | |
public function __construct() { | |
$_sPageSlug = 'apf_csv_download'; | |
$_sTabSlug = 'download'; | |
add_filter( "export_{$_sPageSlug}_{$_sTabSlug}", array( $this, 'replyToFilterExportData' ), 10, 0 ); | |
add_filter( "export_name_{$_sPageSlug}_{$_sTabSlug}", array( $this, 'replyToGetExportFileName' ), 10, 0 ); | |
add_filter( "export_format_{$_sPageSlug}_{$_sTabSlug}", array( $this, 'replyToGetFormat' ), 10, 0 ); | |
} | |
/** | |
* @return string Force the output to be text. | |
*/ | |
public function replyToGetFormat() { | |
return 'text'; | |
} | |
/** | |
* @callback add_filter() export_{page slug}_{tab slug} | |
* @return string | |
*/ | |
public function replyToFilterExportData() { | |
// Simulate some data | |
$_aData = array ( | |
array( 'Year', 'Make', 'Model', 'Description', 'Price' ), | |
array( 1997, 'Ford', 'E350', 'ac, abs, moon', '3000.00' ), | |
array( 1999, 'Chevy', 'E350', 'Venture "Extended Edition"', '4900.00' ), | |
array( 1999, 'Chevy', 'E350', 'Venture "Extended Edition, Very Large"', '5000.00' ), | |
array( 1996, 'Jeep', 'Grand Cherokee', 'MUST SELL! air, moon roof, loaded', '4799.00' ), | |
); | |
return $this->getCSVFromList( $_aData ); | |
} | |
public function getCSVFromList( array $aList, $sDelimiter=',', $sEnclosure='"', $sEscapeChar= "\\" ) { | |
$_rFile = fopen('php://memory', 'r+' ); // r+ | |
foreach( $aList as $_aFields ) { | |
fputcsv( $_rFile, $_aFields, $sDelimiter, $sEnclosure, $sEscapeChar ); | |
} | |
rewind( $_rFile ); | |
$_bsCSV = stream_get_contents( $_rFile ); | |
return false === $_bsCSV | |
? '' | |
: rtrim( $_bsCSV ); | |
} | |
/** | |
* @return string | |
*/ | |
public function replyToGetExportFileName() { | |
$_sToday = date( 'Ymd', time() ); | |
return "apf-csv-download-{$_sToday}.csv"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment