PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.11.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.11.3
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 / admin / reports / class-charitable-export-donations.php

class-charitable-export-donations.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.11.3, at includes/admin/reports/class-charitable-export-donations.php

283 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class that is responsible for generating a CSV export of donations.
4 *
5 * @package Charitable/Classes/Charitable_Export_Donations
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.6.45
11 */
12
13 /* Exit if accessed directly */
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Export_Donations' ) ) :
19
20 /**
21 * Charitable_Export_Donations
22 *
23 * @since 1.0.0
24 */
25 class Charitable_Export_Donations extends Charitable_Export {
26
27 /* The type of export. */
28 const EXPORT_TYPE = 'donations';
29
30 /**
31 * Default arguments.
32 *
33 * @since 1.0.0
34 *
35 * @var mixed[]
36 */
37 protected $defaults;
38
39 /**
40 * List of donation statuses.
41 *
42 * @since 1.0.0
43 *
44 * @var string[]
45 */
46 protected $statuses;
47
48 /**
49 * Exportable donation fields.
50 *
51 * @since 1.5.0
52 *
53 * @var array
54 */
55 protected $fields;
56
57 /**
58 * Set Charitable_Donation objects.
59 *
60 * @since 1.5.0
61 *
62 * @var Charitable_Donation[]
63 */
64 protected $donations = array();
65
66 /**
67 * Format money fields.
68 *
69 * @since 1.8.1.4
70 *
71 * @var mixed[]
72 */
73 protected $format_money_fields = array();
74
75 /**
76 * Create class object.
77 *
78 * @since 1.0.0
79 * @since 1.8.1.4 Added formatting money fields.
80 *
81 * @param mixed[] $args Arguments for the report.
82 */
83 public function __construct( $args ) {
84 $this->defaults = array(
85 'start_date' => '',
86 'end_date' => '',
87 'campaign_id' => 'all',
88 'status' => 'all',
89 );
90
91 $this->statuses = charitable_get_valid_donation_statuses();
92 $this->fields = array_map( array( $this, 'get_field_label' ), charitable()->donation_fields()->get_export_fields() );
93
94 add_filter( 'charitable_export_data_key_value', array( $this, 'suppliment_charitable_export_data' ), 10, 3 );
95
96 $this->format_money_fields = apply_filters(
97 'charitable_export_donations_format_money_fields',
98 array(
99 'amount',
100 'subtotal',
101 'total',
102 'total_donation_with_fees',
103 )
104 );
105
106 parent::__construct( $args );
107 }
108
109 /**
110 * Suppliments the data exported for campaigns with custom field data.
111 *
112 * @param mixed[]|false $custom_field_data Optional. Custom field data to add to the exported data. Default false.
113 * @param string|false $key Optional. The key to use for the custom field data. Default false.
114 * @param mixed[] $data Optional. The exported data. Default empty array.
115 *
116 * @return mixed[] The exported data with custom field data added.
117 */
118 public function suppliment_charitable_export_data( $custom_field_data = false, $key = false, $data = array() ) {
119
120 if ( in_array( $key, $this->format_money_fields, true ) ) {
121 return html_entity_decode( trim( charitable_format_money( $custom_field_data ) ) );
122 }
123
124 return $custom_field_data;
125 }
126
127
128 /**
129 * Filter the date and time fields.
130 *
131 * @since 1.0.0
132 *
133 * @param mixed $value The value to set.
134 * @param string $key The key to set.
135 * @param array $data The set of data.
136 * @return mixed
137 */
138 public function set_custom_field_data( $value, $key, $data ) {
139 if ( array_key_exists( $key, $this->fields ) ) {
140 $donation = $this->get_donation( $data['donation_id'] );
141 $value = charitable_get_sanitized_donation_field_value( $donation->get( $key ), $key );
142 }
143
144 return $value;
145 }
146
147 /**
148 * Return the CSV column headers.
149 *
150 * The columns are set as a key=>label array, where the key is used to retrieve the data for that column.
151 *
152 * @since 1.0.0
153 *
154 * @return string[]
155 */
156 protected function get_csv_columns() {
157 $non_field_columns = array(
158 'campaign_id' => '',
159 'campaign_name' => '',
160 'amount' => '',
161 );
162
163 $default_columns = array(
164 'donation_id' => __( 'Donation ID', 'charitable' ),
165 'campaign_id' => __( 'Campaign ID', 'charitable' ),
166 'campaign_name' => __( 'Campaign Title', 'charitable' ),
167 'first_name' => __( 'First Name', 'charitable' ),
168 'last_name' => __( 'Last Name', 'charitable' ),
169 'email' => __( 'Email', 'charitable' ),
170 'address' => __( 'Address', 'charitable' ),
171 'address_2' => __( 'Address 2', 'charitable' ),
172 'city' => __( 'City', 'charitable' ),
173 'state' => __( 'State', 'charitable' ),
174 'postcode' => __( 'Postcode', 'charitable' ),
175 'country' => __( 'Country', 'charitable' ),
176 'phone' => __( 'Phone Number', 'charitable' ),
177 'donor_address' => __( 'Address Formatted', 'charitable' ),
178 'amount' => __( 'Donation Amount', 'charitable' ),
179 'date' => __( 'Date of Donation', 'charitable' ),
180 'time' => __( 'Time of Donation', 'charitable' ),
181 'status_label' => __( 'Donation Status', 'charitable' ),
182 'gateway_label' => __( 'Donation Gateway', 'charitable' ),
183 'test_mode' => __( 'Made in Test Mode', 'charitable' ),
184 'contact_consent' => __( 'Contact Consent', 'charitable' ),
185 );
186
187 /**
188 * Filter the list of columns in the export.
189 *
190 * As of Charitable 1.5, the recommended way to add or remove columns to the
191 * donation export is through the Donation Fields API. This filter is primarily
192 * provided for backwards compatibility and also provides a way to change export
193 * column headers without changing the label for the Donation Field.
194 *
195 * @since 1.0.0
196 *
197 * @param array $columns List of columns.
198 * @param array $args Export args.
199 */
200 $filtered = apply_filters( 'charitable_export_donations_columns', $default_columns, $this->args );
201
202 return $this->get_merged_fields( $default_columns, $this->fields, $non_field_columns, $filtered );
203 }
204
205 /**
206 * Return a Donation object for the given ID.
207 *
208 * @since 1.5.0
209 *
210 * @param int $donation_id The donation ID.
211 * @return Charitable_Donation
212 */
213 protected function get_donation( $donation_id ) {
214 if ( ! array_key_exists( $donation_id, $this->donations ) ) {
215 $this->donations[ $donation_id ] = charitable_get_donation( $donation_id );
216 }
217
218 return $this->donations[ $donation_id ];
219 }
220
221 /**
222 * Get the data to be exported.
223 *
224 * @since 1.0.0
225 *
226 * @return array
227 */
228 protected function get_data() {
229 $query_args = array();
230
231 if ( strlen( $this->args['start_date'] ) ) {
232 $query_args['start_date'] = charitable_sanitize_date_alt_format( $this->args['start_date'], 'Y-m-d 00:00:00' );
233 }
234
235 if ( strlen( $this->args['end_date'] ) ) {
236 $query_args['end_date'] = charitable_sanitize_date_alt_format( $this->args['end_date'], 'Y-m-d 23:59:59' );
237 }
238
239 if ( 'all' !== $this->args['campaign_id'] ) {
240 $query_args['campaign_id'] = $this->args['campaign_id'];
241 }
242
243 if ( 'all' !== $this->args['status'] ) {
244 $query_args['status'] = $this->args['status'];
245 }
246
247 /**
248 * Filter name with misspelling.
249 *
250 * @deprecated 1.7.0
251 *
252 * @since 1.3.5
253 */
254 $query_args = apply_filters( 'chairtable_export_donations_query_args', $query_args, $this->args ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- 'chairtable_export_donations_query_args' is a legacy hook (note: typo in hook name) that cannot be renamed.
255
256 /**
257 * Filter donations query arguments.
258 *
259 * @since 1.3.5
260 *
261 * @param array $query_args The query arguments.
262 * @param array $args The export arguments.
263 */
264 $query_args = apply_filters( 'charitable_export_donations_query_args', $query_args, $this->args );
265
266 return charitable_get_table( 'campaign_donations' )->get_donations_report( $query_args );
267 }
268
269 /**
270 * Return the field label for a registered Donation Field.
271 *
272 * @since 1.5.0
273 *
274 * @param Charitable_Donation_Field $field Instance of `Charitable_Donation_Field`.
275 * @return string
276 */
277 protected function get_field_label( Charitable_Donation_Field $field ) {
278 return $field->label;
279 }
280 }
281
282 endif;
283