PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.5
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.5
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / abstracts / abstract-class-charitable-export.php

abstract-class-charitable-export.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.5, at includes/abstracts/abstract-class-charitable-export.php

273 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Abstract class defining Export model.
4 *
5 * @package Charitable/Classes/Charitable_Export
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.6.40
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Export' ) ) :
19
20 /**
21 * Charitable_Export
22 *
23 * @since 1.0.0
24 */
25 abstract class Charitable_Export {
26
27 /* The type of export. */
28 const EXPORT_TYPE = '';
29
30 /**
31 * The CSV's columns.
32 *
33 * @since 1.0.0
34 *
35 * @var string[]
36 */
37 protected $columns;
38
39 /**
40 * Optional array of arguments.
41 *
42 * @since 1.0.0
43 *
44 * @var mixed[]
45 */
46 protected $args;
47
48 /**
49 * Array of default arguments.
50 *
51 * @since 1.0.0
52 *
53 * @var mixed[]
54 */
55 protected $defaults = array();
56
57 /**
58 * Create class object.
59 *
60 * @since 1.0.0
61 *
62 * @param mixed[] $args Mixed arguments.
63 */
64 public function __construct( $args = array() ) {
65 $this->columns = $this->get_csv_columns();
66 $this->args = $this->parse_args( $args );
67
68 $this->export();
69 }
70
71 /**
72 * Returns whether the current user can export data.
73 *
74 * @since 1.0.0
75 *
76 * @return boolean
77 */
78 public function can_export() {
79 return (bool) apply_filters( 'charitable_export_capability', current_user_can( 'export_charitable_reports' ), $this );
80 }
81
82 /**
83 * Filter a specific field.
84 *
85 * By default, this will simply return the value.
86 *
87 * @since 1.6.36
88 *
89 * @param mixed $value The value to set.
90 * @param string $key The key to set.
91 * @param array $data The set of data.
92 * @return mixed
93 */
94 public function set_custom_field_data( $value, $key, $data ) {
95 return $value;
96 }
97
98 /**
99 * Parse the arguments.
100 *
101 * @since 1.6.36
102 *
103 * @return array
104 */
105 protected function parse_args( $args ) {
106 return wp_parse_args( $args, $this->defaults );
107 }
108
109 /**
110 * Export the CSV file.
111 *
112 * @since 1.0.0
113 *
114 * @return void
115 */
116 protected function export() {
117 $data = array_map( array( $this, 'map_data' ), $this->get_data() );
118
119 $this->print_headers();
120
121 /* Create a file pointer connected to the output stream */
122 $output = fopen( 'php://output', 'w' );
123
124 /* Print first row headers. */
125 fputcsv( $output, array_values( $this->columns ) );
126
127 /* Print the data */
128 foreach ( $data as $row ) {
129 fputcsv( $output, $row );
130 }
131
132 fclose( $output );
133
134 exit();
135 }
136
137 /**
138 * Receives a row of data and maps it to the keys defined in the columns.
139 *
140 * @since 1.0.0
141 *
142 * @param object|array $data
143 * @return mixed
144 */
145 protected function map_data( $data ) {
146 /* Cast the data to array */
147 if ( ! is_array( $data ) ) {
148 $data = (array) $data;
149 }
150
151 $row = array();
152
153 foreach ( $this->columns as $key => $label ) {
154 $value = isset( $data[ $key ] ) ? $data[ $key ] : '';
155
156 /**
157 * Filter the value to be exported for a specific cell.
158 *
159 * Note that this filter applies to all Charitable exports
160 * (both campaigns and donations).
161 *
162 * @since 1.0.0
163 *
164 * @param mixed $value The default value.
165 * @param string $key The field/column we are getting a value for.
166 * @param mixed $data The raw input data.
167 */
168 $value = apply_filters( 'charitable_export_data_key_value', $this->set_custom_field_data( $value, $key, $data ), $key, $data );
169
170 if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
171 /**
172 * Filter the value to be exported for a specific cell.
173 *
174 * Use this filter if you only want to change a specific
175 * type of export.
176 *
177 * @since 1.6.40
178 *
179 * @param mixed $value The default value.
180 * @param string $key The field/column we are getting a value for.
181 * @param mixed $data The raw input data.
182 */
183 $value = apply_filters( 'charitable_export_' . static::EXPORT_TYPE . '_data_key_value', $value, $key, $data );
184 }
185
186 $row[] = $value;
187 }
188
189 return $row;
190 }
191
192 /**
193 * Print the CSV document headers.
194 *
195 * @since 1.0.0
196 *
197 * @return void
198 */
199 protected function print_headers() {
200 ignore_user_abort( true );
201
202 if ( ! charitable_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
203 set_time_limit( 0 );
204 }
205
206 /* Check for PHP 5.3+ */
207 if ( function_exists( 'get_called_class' ) ) {
208 $class = get_called_class();
209 $export = $class::EXPORT_TYPE;
210 } else {
211 $export = '';
212 }
213
214 nocache_headers();
215 header( 'Content-Type: text/csv; charset=utf-8' );
216 header( 'Content-Disposition: attachment; filename=charitable-export-' . $export . '-' . date( 'm-d-Y' ) . '.csv' );
217 header( 'Expires: 0' );
218 }
219
220 /**
221 * Get merged fields.
222 *
223 * @since 1.6.0
224 *
225 * @param array $default_columns The default set of columns to include in the export.
226 * @param array $fields A set of fields retrieved from the Campaigns/Donations Fields API.
227 * @param array $non_field_columns Extra fields not include in the Fields API.
228 * @param array $filtered The default columns after they have been passed through a filter.
229 * @return array
230 */
231 protected function get_merged_fields( $default_columns, $fields, $non_field_columns, $filtered ) {
232 /* Get all fields that were removed either by the filter or the Fields API. */
233 $removed = array_merge(
234 array_diff_key( $default_columns, $fields, $non_field_columns ), /* Fields API */
235 array_diff_key( $default_columns, $filtered ) /* Filter */
236 );
237
238 /* Get all fields that were added either by the filter or the Fields API. */
239 $added = array_merge(
240 array_diff_key( $fields, $default_columns ), /* Fields API */
241 array_diff_key( $filtered, $default_columns ) /* Filter */
242 );
243
244 /* Get all of the default columns that were not removed. */
245 $columns = array_diff_key( $default_columns, $removed );
246
247 /* Finally, merge with all added columns and return. */
248 return array_merge( $columns, $added );
249 }
250
251 /**
252 * Return the CSV column headers.
253 *
254 * The columns are set as a key=>label array, where the key is used to retrieve the data for that column.
255 *
256 * @since 1.0.0
257 *
258 * @return string[]
259 */
260 abstract protected function get_csv_columns();
261
262 /**
263 * Get the data to be exported.
264 *
265 * @since 1.0.0
266 *
267 * @return array
268 */
269 abstract protected function get_data();
270 }
271
272 endif;
273