I'm developing my first WP plugin to extract data from a WordPress DB.
I want it to be possible to define the date range in order to limit what's gonna be extracted or extract it through invoices.
Can anyone have a look and tell me what's the problem or what I have to do?
That would be great, thanks!
/**
* Constructor
*/
function __construct() {
if (isset($_GET['report'])) {
$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=\"report.csv\";");
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
*/
function admin_menu() {
add_menu_page('Extract Data', 'Extract Data', 'manage_options', 'export_data', array($this, 'export_data'));
}
/**
* Allow for custom query variables
*/
function query_vars($query_vars) {
$query_vars[] = 'export_data';
return $query_vars;
}
/**
* Parse the request
*/
function parse_request(&$wp) {
if (array_key_exists('export_data', $wp->query_vars)) {
$this->export_data();
exit;
}
}
/**
* Download report
*/
function export_data() {
ob_start();?>
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h2> WordPress Extract Data</h2>
<p>
<h3>Extract data throught defining the start/end date or through invoices</h3>
</p>
<div id="calendar_box">
<h4>Search by date</h4>
<form method="POST" action="" enctype="multipart/form-data">
Start Date:
<input type="text" name="date_picker1" id="date_picker1" />
<?php
echo '<img src="' . plugins_url( 'img/calendar.jpg', __FILE__ ) . '" />';
?>
<div id="calendar1" style="position: absolute; top: 40px;left: 5px; z-index:1;"></div>
End Date:
<input type="text" name="date_picker2" id="date_picker2" />
<?php
echo '<img src="' . plugins_url( 'img/calendar.jpg', __FILE__ ) . '" />';
?>
<div id="calendar2" style="position: absolute; top: 40px;left: 220px; z-index:1;"></div>
<br>
<p class="submit">
<a href="?page=export_data&report=users"><input type="submit" class="button-primary" value="Export Report" /></a>
</p>
</form>
</div>
<br>
or
<br>
<div id="invoices_box">
<h4>Search by invoice numbers</h4>
<form method="POST" action="" enctype="multipart/form-data">
<input type="text" name="invoices" id="invoices" />
<br>
<p class="submit">
<input type="submit" class="button-primary" value="Export Report" />
</p>
</form>
</div>
</div>
<?php
echo ob_get_clean();
}
/**
* Converting data to CSV
*/
function generate_csv() {
global $wpdb;
$csv_output = '';
$table = 'wp_test_payments';
$startdate = $_POST['date_picker1'];
$finaldate = $_POST['date_picker2'];
$invoices = $_POST['invoices'];
$rowq1 = $wpdb->num_rows($MyQuery1);
$rowq2 = $wpdb->num_rows($MyQuery2);
/*
*Build your Query
*/
$MyQuery1 = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $table . ' WHERE date BETWEEN ' . $startdate . ' AND '.$finaldate.' '));
$MyQuery2 = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $table . ' WHERE payment_id = ' . $invoices . ' '));
$i = 0;
if ( $rowq1 = 0 AND $rowq2 = 0) {
$Error = $wpdb->print_error();
die("You have to select one of the variables.");
}
if ($rowq1 > 0) {
while ($row = $wpdb->get_results($MyQuery1)) {
$csv_output = $csv_output . $row['Field'] . ",";
$i++;
}
}
if ($rowq2 > 0) {
while ($row = $wpdb->get_results($MyQuery2)) {
$csv_output = $csv_output . $row['Field'] . ",";
$i++;
}
}
$csv_output .= "\n";
$values = ($rowq1 = 0 XOR $rowq2 = 0);
while ($rowr = $wpdb->get_results($values)) {
for ($j = 0; $j < $i; $j++) {
$csv_output .= $rowr[$j] . ",";
}
$csv_output .= "\n";
}
return $csv_output;
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire