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

242 lines 6.5 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) 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.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 * Create class object.
68 *
69 * @since 1.0.0
70 *
71 * @param mixed[] $args Arguments for the report.
72 */
73 public function __construct( $args ) {
74 $this->defaults = array(
75 'start_date' => '',
76 'end_date' => '',
77 'campaign_id' => 'all',
78 'status' => 'all',
79 );
80
81 $this->statuses = charitable_get_valid_donation_statuses();
82 $this->fields = array_map( array( $this, 'get_field_label' ), charitable()->donation_fields()->get_export_fields() );
83
84 parent::__construct( $args );
85 }
86
87 /**
88 * Filter the date and time fields.
89 *
90 * @since 1.0.0
91 *
92 * @param mixed $value The value to set.
93 * @param string $key The key to set.
94 * @param array $data The set of data.
95 * @return mixed
96 */
97 public function set_custom_field_data( $value, $key, $data ) {
98 if ( array_key_exists( $key, $this->fields ) ) {
99 $donation = $this->get_donation( $data['donation_id'] );
100 $value = charitable_get_sanitized_donation_field_value( $donation->get( $key ), $key );
101 }
102
103 return $value;
104 }
105
106 /**
107 * Return the CSV column headers.
108 *
109 * The columns are set as a key=>label array, where the key is used to retrieve the data for that column.
110 *
111 * @since 1.0.0
112 *
113 * @return string[]
114 */
115 protected function get_csv_columns() {
116 $non_field_columns = array(
117 'campaign_id' => '',
118 'campaign_name' => '',
119 'amount' => '',
120 );
121
122 $default_columns = array(
123 'donation_id' => __( 'Donation ID', 'charitable' ),
124 'campaign_id' => __( 'Campaign ID', 'charitable' ),
125 'campaign_name' => __( 'Campaign Title', 'charitable' ),
126 'first_name' => __( 'First Name', 'charitable' ),
127 'last_name' => __( 'Last Name', 'charitable' ),
128 'email' => __( 'Email', 'charitable' ),
129 'address' => __( 'Address', 'charitable' ),
130 'address_2' => __( 'Address 2', 'charitable' ),
131 'city' => __( 'City', 'charitable' ),
132 'state' => __( 'State', 'charitable' ),
133 'postcode' => __( 'Postcode', 'charitable' ),
134 'country' => __( 'Country', 'charitable' ),
135 'phone' => __( 'Phone Number', 'charitable' ),
136 'donor_address' => __( 'Address Formatted', 'charitable' ),
137 'amount' => __( 'Donation Amount', 'charitable' ),
138 'date' => __( 'Date of Donation', 'charitable' ),
139 'time' => __( 'Time of Donation', 'charitable' ),
140 'status_label' => __( 'Donation Status', 'charitable' ),
141 'gateway_label' => __( 'Donation Gateway', 'charitable' ),
142 'test_mode' => __( 'Made in Test Mode', 'charitable' ),
143 'contact_consent' => __( 'Contact Consent', 'charitable' ),
144 );
145
146 /**
147 * Filter the list of columns in the export.
148 *
149 * As of Charitable 1.5, the recommended way to add or remove columns to the
150 * donation export is through the Donation Fields API. This filter is primarily
151 * provided for backwards compatibility and also provides a way to change export
152 * column headers without changing the label for the Donation Field.
153 *
154 * @since 1.0.0
155 *
156 * @param array $columns List of columns.
157 * @param array $args Export args.
158 */
159 $filtered = apply_filters( 'charitable_export_donations_columns', $default_columns, $this->args );
160
161 return $this->get_merged_fields( $default_columns, $this->fields, $non_field_columns, $filtered );
162 }
163
164 /**
165 * Return a Donation object for the given ID.
166 *
167 * @since 1.5.0
168 *
169 * @param int $donation_id The donation ID.
170 * @return Charitable_Donation
171 */
172 protected function get_donation( $donation_id ) {
173 if ( ! array_key_exists( $donation_id, $this->donations ) ) {
174 $this->donations[ $donation_id ] = charitable_get_donation( $donation_id );
175 }
176
177 return $this->donations[ $donation_id ];
178 }
179
180 /**
181 * Get the data to be exported.
182 *
183 * @since 1.0.0
184 *
185 * @return array
186 */
187 protected function get_data() {
188 $query_args = array();
189
190 if ( strlen( $this->args['start_date'] ) ) {
191 $query_args['start_date'] = charitable_sanitize_date( $this->args['start_date'], 'Y-m-d 00:00:00' );
192 }
193
194 if ( strlen( $this->args['end_date'] ) ) {
195 $query_args['end_date'] = charitable_sanitize_date( $this->args['end_date'], 'Y-m-d 23:59:59' );
196 }
197
198 if ( 'all' !== $this->args['campaign_id'] ) {
199 $query_args['campaign_id'] = $this->args['campaign_id'];
200 }
201
202 if ( 'all' != $this->args['status'] ) {
203 $query_args['status'] = $this->args['status'];
204 }
205
206 /**
207 * Filter name with misspelling.
208 *
209 * @deprecated 1.7.0
210 *
211 * @since 1.3.5
212 */
213 $query_args = apply_filters( 'chairtable_export_donations_query_args', $query_args, $this->args );
214
215 /**
216 * Filter donations query arguments.
217 *
218 * @since 1.3.5
219 *
220 * @param array $query_args The query arguments.
221 * @param array $args The export arguments.
222 */
223 $query_args = apply_filters( 'charitable_export_donations_query_args', $query_args, $this->args );
224
225 return charitable_get_table( 'campaign_donations' )->get_donations_report( $query_args );
226 }
227
228 /**
229 * Return the field label for a registered Donation Field.
230 *
231 * @since 1.5.0
232 *
233 * @param Charitable_Donation_Field $field Instance of `Charitable_Donation_Field`.
234 * @return string
235 */
236 protected function get_field_label( Charitable_Donation_Field $field ) {
237 return $field->label;
238 }
239 }
240
241 endif;
242