PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
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 1.7.0.9 All 211 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.8.13, at includes/abstracts/abstract-class-charitable-export.php

284 lines 6.9 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) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.8.0.3
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 ) { // phpcs:ignore
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 * @version 1.8.5 - Added filter for columns and data.
114 *
115 * @return void
116 */
117 protected function export() {
118 $data = array_map( array( $this, 'map_data' ), $this->get_data() );
119
120 /**
121 * Last chance for filtering the data to be exported.
122 *
123 * @since 1.8.5
124 *
125 * @param array $data The data to be exported.
126 */
127 $data = apply_filters( 'charitable_export_data', $data );
128 $columns = apply_filters( 'charitable_export_columns', $this->columns );
129
130 $this->print_headers();
131
132 /* Create a file pointer connected to the output stream */
133 $output = fopen( 'php://output', 'w' );
134
135 /* Print first row headers. */
136 fputcsv( $output, array_values( $columns ) );
137
138 /* Print the data */
139 foreach ( $data as $row ) {
140 fputcsv( $output, $row );
141 }
142
143 fclose( $output ); // phpcs:ignore
144
145 exit();
146 }
147
148 /**
149 * Receives a row of data and maps it to the keys defined in the columns.
150 *
151 * @since 1.0.0
152 *
153 * @param object|array $data Data.
154 * @return mixed
155 */
156 protected function map_data( $data ) {
157 /* Cast the data to array */
158 if ( ! is_array( $data ) ) {
159 $data = (array) $data;
160 }
161
162 $row = array();
163
164 foreach ( $this->columns as $key => $label ) {
165 $value = isset( $data[ $key ] ) ? $data[ $key ] : '';
166
167 /**
168 * Filter the value to be exported for a specific cell.
169 *
170 * Note that this filter applies to all Charitable exports
171 * (both campaigns and donations).
172 *
173 * @since 1.0.0
174 *
175 * @param mixed $value The default value.
176 * @param string $key The field/column we are getting a value for.
177 * @param mixed $data The raw input data.
178 */
179 $value = apply_filters( 'charitable_export_data_key_value', $this->set_custom_field_data( $value, $key, $data ), $key, $data );
180
181 if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
182 /**
183 * Filter the value to be exported for a specific cell.
184 *
185 * Use this filter if you only want to change a specific
186 * type of export.
187 *
188 * @since 1.6.40
189 *
190 * @param mixed $value The default value.
191 * @param string $key The field/column we are getting a value for.
192 * @param mixed $data The raw input data.
193 */
194 $value = apply_filters( 'charitable_export_' . static::EXPORT_TYPE . '_data_key_value', $value, $key, $data );
195 }
196
197 $row[] = $value;
198 }
199
200 return $row;
201 }
202
203 /**
204 * Print the CSV document headers.
205 *
206 * @since 1.0.0
207 *
208 * @return void
209 */
210 protected function print_headers() {
211 ignore_user_abort( true );
212
213 if ( ! charitable_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
214 set_time_limit( 0 ); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged
215 }
216
217 /* Check for PHP 5.3+ */
218 if ( function_exists( 'get_called_class' ) ) {
219 $class = get_called_class();
220 $export = $class::EXPORT_TYPE;
221 } else {
222 $export = '';
223 }
224
225 nocache_headers();
226 header( 'Content-Type: text/csv; charset=utf-8' );
227 header( 'Content-Disposition: attachment; filename=charitable-export-' . $export . '-' . date( 'm-d-Y' ) . '.csv' ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
228 header( 'Expires: 0' );
229 }
230
231 /**
232 * Get merged fields.
233 *
234 * @since 1.6.0
235 *
236 * @param array $default_columns The default set of columns to include in the export.
237 * @param array $fields A set of fields retrieved from the Campaigns/Donations Fields API.
238 * @param array $non_field_columns Extra fields not include in the Fields API.
239 * @param array $filtered The default columns after they have been passed through a filter.
240 * @return array
241 */
242 protected function get_merged_fields( $default_columns, $fields, $non_field_columns, $filtered ) {
243 /* Get all fields that were removed either by the filter or the Fields API. */
244 $removed = array_merge(
245 array_diff_key( $default_columns, $fields, $non_field_columns ), /* Fields API */
246 array_diff_key( $default_columns, $filtered ) /* Filter */
247 );
248
249 /* Get all fields that were added either by the filter or the Fields API. */
250 $added = array_merge(
251 array_diff_key( $fields, $default_columns ), /* Fields API */
252 array_diff_key( $filtered, $default_columns ) /* Filter */
253 );
254
255 /* Get all of the default columns that were not removed. */
256 $columns = array_diff_key( $default_columns, $removed );
257
258 /* Finally, merge with all added columns and return. */
259 return array_merge( $columns, $added );
260 }
261
262 /**
263 * Return the CSV column headers.
264 *
265 * The columns are set as a key=>label array, where the key is used to retrieve the data for that column.
266 *
267 * @since 1.0.0
268 *
269 * @return string[]
270 */
271 abstract protected function get_csv_columns();
272
273 /**
274 * Get the data to be exported.
275 *
276 * @since 1.0.0
277 *
278 * @return array
279 */
280 abstract protected function get_data();
281 }
282
283 endif;
284