1 <?php
2 /**
3 * Export Class
4 *
5 * This is the base class for all export methods. Each data export type (customers, payments, etc) extend this class
6 *
7 * @package EDD
8 * @subpackage Admin/Reports
9 * @copyright Copyright (c) 2013, Pippin Williamson
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.4.4
12 */
13
14 // Exit if accessed directly
15 if ( ! defined( 'ABSPATH' ) ) exit;
16
17 /**
18 * EDD_Export Class
19 *
20 * @since 1.4.4
21 */
22 class EDD_Export {
23 /**
24 * Our export type. Used for export-type specific filters/actions
25 * @var string
26 * @since 1.4.4
27 */
28 public $export_type = 'default';
29
30 /**
31 * Can we export?
32 *
33 * @access public
34 * @since 1.4.4
35 * @return bool Whether we can export or not
36 */
37 public function can_export() {
38 return (bool) apply_filters( 'edd_export_capability', current_user_can( 'manage_options' ) );
39 }
40
41 /**
42 * Set the export headers
43 *
44 * @access public
45 * @since 1.4.4
46 * @return void
47 */
48 public function headers() {
49 ignore_user_abort( true );
50
51 if ( ! edd_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) )
52 set_time_limit( 0 );
53
54 nocache_headers();
55 header( 'Content-Type: text/csv; charset=utf-8' );
56 header( 'Content-Disposition: attachment; filename=edd-export-' . $this->export_type . '-' . date( 'm-d-Y' ) . '.csv' );
57 header( "Expires: 0" );
58 }
59
60 /**
61 * Set the CSV columns
62 *
63 * @access public
64 * @since 1.4.4
65 * @return array $cols All the columns
66 */
67 public function csv_cols() {
68 $cols = array(
69 'id' => __( 'ID', 'edd' ),
70 'date' => __( 'Date', 'edd' )
71 );
72 return $cols;
73 }
74
75 /**
76 * Retrieve the CSV columns
77 *
78 * @access public
79 * @since 1.4.4
80 * @return array $cols Array of the columns
81 */
82 public function get_csv_cols() {
83 $cols = $this->csv_cols();
84 return apply_filters( 'edd_export_csv_cols_' . $this->export_type, $cols );
85 }
86
87 /**
88 * Output the CSV columns
89 *
90 * @access public
91 * @since 1.4.4
92 * @uses EDD_Export::get_csv_cols()
93 * @return void
94 */
95 public function csv_cols_out() {
96 $cols = $this->get_csv_cols();
97 $i = 1;
98 foreach( $cols as $col_id => $column ) {
99 echo '"' . $column . '"';
100 echo $i == count( $cols ) ? '' : ',';
101 $i++;
102 }
103 echo "\r\n";
104 }
105
106 /**
107 * Get the data being exported
108 *
109 * @access public
110 * @since 1.4.4
111 * @return array $data Data for Export
112 */
113 public function get_data() {
114 // Just a sample data array
115 $data = array(
116 0 => array(
117 'id' => '',
118 'data' => date( 'F j, Y' )
119 ),
120 1 => array(
121 'id' => '',
122 'data' => date( 'F j, Y' )
123 )
124 );
125
126 $data = apply_filters( 'edd_export_get_data', $data );
127 $data = apply_filters( 'edd_export_get_data_' . $this->export_type, $data );
128
129 return $data;
130 }
131
132 /**
133 * Output the CSV rows
134 *
135 * @access public
136 * @since 1.4.4
137 * @return void
138 */
139 public function csv_rows_out() {
140 $data = $this->get_data();
141
142 $cols = $this->get_csv_cols();
143
144 // Output each row
145 foreach ( $data as $row ) {
146 $i = 1;
147 foreach ( $row as $col_id => $column ) {
148 // Make sure the column is valid
149 if ( array_key_exists( $col_id, $cols ) ) {
150 echo '"' . $column . '"';
151 echo $i == count( $cols ) + 1 ? '' : ',';
152 }
153
154 $i++;
155 }
156 echo "\r\n";
157 }
158 }
159
160 /**
161 * Perform the export
162 *
163 * @access public
164 * @since 1.4.4
165 * @uses EDD_Export::can_export()
166 * @uses EDD_Export::headers()
167 * @uses EDD_Export::csv_cols_out()
168 * @uses EDD_Export::csv_rows_out()
169 * @return void
170 */
171 public function export() {
172 if ( ! $this->can_export() )
173 wp_die( __( 'You do not have permission to export data.', 'edd' ), __( 'Error', 'edd' ) );
174
175 // Set headers
176 $this->headers();
177
178 // Output CSV columns (headers)
179 $this->csv_cols_out();
180
181 // Output CSV rows
182 $this->csv_rows_out();
183
184 exit;
185 }
186 }