Created
November 9, 2015 19:11
-
-
Save montrealist/b0bc89b5b308ae2c6c45 to your computer and use it in GitHub Desktop.
WordPress plugin to generate CSV from postmeta table data (in this case - average star ratings from the Starstruck plugin
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 | |
/** | |
* CSV Exporter bootstrap file | |
* | |
* This file is read by WordPress to generate the plugin information in the plugin | |
* admin area. This file also includes all of the dependencies used by the plugin, | |
* registers the activation and deactivation functions, and defines a function | |
* that starts the plugin. | |
* | |
* @since 1.0.0 | |
* @package EPC CSV Export | |
* | |
* @wordpress-plugin | |
* Plugin Name: Epic CSV Export | |
* Description: exports CSVs | |
* Version: 1.0.0 | |
* Author: Max Kay | |
* Author URI: https://ca.linkedin.com/in/maxkay | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* Text Domain: epc-csv-export | |
* Domain Path: /languages | |
*/ | |
class EPCCSVExport { | |
/** | |
* Constructor | |
*/ | |
public function __construct() { | |
if (isset($_GET['report'])) { | |
$filename = 'report-' . date('Y-m-d--H-i-s') . '.csv'; | |
$csv = $this->generate_csv(); | |
header("Pragma: public"); | |
header("Expires: 0"); | |
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); | |
header("Cache-Control: private", false); | |
header("Content-Type: application/octet-stream"); | |
header("Content-Disposition: attachment; filename={$filename};"); | |
header("Content-Transfer-Encoding: binary"); | |
echo $csv; | |
exit; | |
} | |
// Add extra menu items for admins | |
add_action('admin_menu', array($this, 'admin_menu')); | |
// Create end-points | |
add_filter('query_vars', array($this, 'query_vars')); | |
add_action('parse_request', array($this, 'parse_request')); | |
} | |
/** | |
* Add extra menu items for admins | |
*/ | |
public function admin_menu() { | |
// error_log( 'admin_menu'); | |
add_menu_page('Export Ratings', 'Export Ratings', 'manage_options', 'download_report', array($this, 'download_report')); | |
} | |
/** | |
* Allow for custom query variables | |
*/ | |
public function query_vars($query_vars) { | |
// error_log( 'query_vars' ); | |
$query_vars[] = 'download_report'; | |
return $query_vars; | |
} | |
/** | |
* Parse the request | |
*/ | |
public function parse_request(&$wp) { | |
// error_log( 'parse_request' ); | |
if (array_key_exists('download_report', $wp->query_vars)) { | |
$this->download_report(); | |
exit; | |
} | |
} | |
/** | |
* Export Star Ratings | |
*/ | |
public function download_report() { | |
echo '<div class="wrap">'; | |
echo '<div id="icon-tools" class="icon32"> | |
</div>'; | |
echo '<h2>Download CSV Report</h2>'; | |
echo '<p><a href="?page=download_report&report=users">'. __('Export the star ratings to CSV','epc-csv-export') . '</a></p>'; | |
} | |
/** | |
* Converting data to CSV | |
*/ | |
function generate_csv() { | |
// error_log( 'generate_csv2: ' ); | |
$csv_output = ''; | |
if ( !is_admin() ) { | |
return; | |
} | |
// if ( ! isset( $_GET['bbg_export'] ) ) { | |
// return; | |
// } | |
$header_row = array( | |
0 => 'ID', | |
1 => 'Post Title', | |
2 => 'Average Rating', | |
3 => 'Total Votes' | |
); | |
$fn = function($v) { return __($v,'epc-csv-export'); }; | |
$arr = array_map($fn, $header_row ); | |
$csv_output = implode(',', $arr); | |
$csv_output .= "\n"; | |
$data_rows = array(); | |
global $wpdb, $bp; | |
$users = $wpdb->get_results( "SELECT p.ID id, p.post_title title, rating.meta_value rating, rating2.meta_value rating2 | |
FROM {$wpdb->posts} p | |
JOIN {$wpdb->postmeta} rating | |
ON p.ID = rating.post_id AND rating.meta_key = '_starstruck_avg' | |
JOIN {$wpdb->postmeta} rating2 | |
ON p.ID = rating2.post_id AND rating2.meta_key = '_starstruck_total' | |
WHERE | |
p.post_type = 'product' | |
ORDER BY p.post_title ASC" ); | |
if(count($users) > 0){ | |
foreach ( $users as $u ) { | |
$row = array(); | |
$row[0] = $u->id; | |
$row[1] = $u->title; | |
$row[2] = $u->rating; | |
$row[3] = $u->rating2; | |
$result = array_values($row); | |
$result = implode(", ", $result); | |
$csv_output .= $result."\n"; | |
} | |
} | |
// error_log( 'csv_output: '. print_r( $csv_output, true ) ); | |
return $csv_output; | |
} | |
} | |
// Instantiate a singleton of this plugin | |
$csvExport = new EPCCSVExport(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment