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

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

347 lines 8.8 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 campaigns.
4 *
5 * @package Charitable/Classes/Charitable_Export_Campaigns
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.6.0
10 * @version 1.6.39
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Export_Campaigns' ) ) :
18
19 /**
20 * Charitable_Export_Campaigns
21 *
22 * @since 1.6.0
23 */
24 class Charitable_Export_Campaigns extends Charitable_Export {
25
26 /* The type of export. */
27 const EXPORT_TYPE = 'campaigns';
28
29 /**
30 * Default arguments.
31 *
32 * @since 1.6.0
33 *
34 * @var mixed[]
35 */
36 protected $defaults;
37
38 /**
39 * List of donation statuses.
40 *
41 * @since 1.6.0
42 *
43 * @var string[]
44 */
45 protected $statuses;
46
47 /**
48 * Exportable campaign fields.
49 *
50 * @since 1.6.0
51 *
52 * @var array
53 */
54 protected $fields;
55
56 /**
57 * Set Charitable_Campaign objects.
58 *
59 * @since 1.6.0
60 *
61 * @var Charitable_Campaign[]
62 */
63 protected $campaigns = array();
64
65 /**
66 * Set Charitable_User objects.
67 *
68 * @since 1.6.0
69 *
70 * @var Charitable_User[]
71 */
72 protected $creators = array();
73
74 /**
75 * Create class object.
76 *
77 * @since 1.6.0
78 *
79 * @param mixed[] $args Arguments for the report.
80 */
81 public function __construct( $args ) {
82 /**
83 * Filter the default campaign export arguments.
84 *
85 * @since 1.6.36
86 *
87 * @param array $defaults Default arguments.
88 */
89 $this->defaults = apply_filters(
90 'charitable_export_campaigns_default_args',
91 array(
92 'start_date_from' => '',
93 'start_date_to' => '',
94 'end_date_from' => '',
95 'end_date_to' => '',
96 'status' => '',
97 'start_date' => '', // Kept for backwards compatibility, @since 1.6.36.
98 'end_date' => '', // Kept for backwards compatibility, @since 1.6.36.
99 )
100 );
101
102 $this->fields = array_map( array( $this, 'get_field_label' ), charitable()->campaign_fields()->get_export_fields() );
103
104 parent::__construct( $args );
105 }
106
107 /**
108 * Filter the cell values.
109 *
110 * @since 1.6.0
111 *
112 * @param mixed $value The value to set.
113 * @param string $key The key to set.
114 * @param array $data The data array, which just contains the campaign ID.
115 * @return mixed
116 */
117 public function set_custom_field_data( $value, $key, $data ) {
118 $campaign_id = current( $data );
119
120 if ( array_key_exists( $key, $this->fields ) ) {
121 $value = $this->get_campaign( $campaign_id )->get( $key );
122 }
123
124 return $value;
125 }
126
127 /**
128 * Parse the arguments.
129 *
130 * @since 1.6.36
131 *
132 * @return array
133 */
134 protected function parse_args( $args ) {
135 $parsed = wp_parse_args( $args, $this->defaults );
136
137 if ( empty( $parsed['start_date_from'] ) && strlen( $parsed['start_date'] ) ) {
138 $parsed['start_date_from'] = $parsed['start_date'];
139 }
140
141 if ( empty( $parsed['start_date_to'] ) && strlen( $parsed['end_date'] ) ) {
142 $parsed['start_date_to'] = $parsed['end_date'];
143 }
144
145 unset(
146 $parsed['start_date'],
147 $parsed['end_date']
148 );
149
150 return $parsed;
151 }
152
153 /**
154 * Return the CSV column headers.
155 *
156 * The columns are set as a key=>label array, where the key is used to retrieve the data for that column.
157 *
158 * @since 1.6.0
159 *
160 * @return string[]
161 */
162 protected function get_csv_columns() {
163 $default_columns = array(
164 'ID' => __( 'Campaign ID', 'charitable' ),
165 'post_title' => __( 'Title', 'charitable' ),
166 'post_date' => __( 'Date Created', 'charitable' ),
167 'end_date' => __( 'End Date', 'charitable' ),
168 'goal' => __( 'Goal', 'charitable' ),
169 'donated_amount' => __( 'Amount Donated', 'charitable' ),
170 'percent_donated_raw' => __( 'Percentage of Goal Raised', 'charitable' ),
171 'status' => __( 'Status', 'charitable' ),
172 'campaign_creator_name' => __( 'Campaign Creator', 'charitable' ),
173 'campaign_creator_id' => __( 'Campaign Creator ID', 'charitable' ),
174 'campaign_creator_email' => __( 'Campaign Creator Email', 'charitable' ),
175 );
176
177 /**
178 * Filter the list of columns in the export.
179 *
180 * The recommended way to add or remove columns to the campaign export
181 * is through the Campaign Fields API. This filter is provided as a way
182 * to change export column headers without changing the label for the
183 * Campaign Field.
184 *
185 * @since 1.6.0
186 *
187 * @param array $columns List of columns.
188 * @param array $args Export args.
189 */
190 $filtered = apply_filters( 'charitable_export_campaigns_columns', $default_columns, $this->args );
191
192 return $this->get_merged_fields( $default_columns, $this->fields, array(), $filtered );
193 }
194
195 /**
196 * Return a Campaign object for the given ID.
197 *
198 * @since 1.6.0
199 *
200 * @param int $campaign_id The campaign ID.
201 * @return Charitable_Campaign
202 */
203 protected function get_campaign( $campaign_id ) {
204 if ( ! array_key_exists( $campaign_id, $this->campaigns ) ) {
205 $this->campaigns[ $campaign_id ] = charitable_get_campaign( $campaign_id );
206 }
207
208 return $this->campaigns[ $campaign_id ];
209 }
210
211 /**
212 * Return a User object for a campaign creator.
213 *
214 * @since 1.6.0
215 *
216 * @param int $campaign_id The campaign ID.
217 * @return Charitable_User
218 */
219 public function get_campaign_creator( $campaign_id ) {
220 $creator = $this->get_campaign( $campaign_id )->get_campaign_creator();
221
222 if ( ! array_key_exists( $creator, $this->creators ) ) {
223 $this->creators[ $creator ] = charitable_get_user( $creator );
224 }
225
226 return $this->creators[ $creator ];
227 }
228
229 /**
230 * Get the data to be exported.
231 *
232 * @since 1.6.0
233 *
234 * @return array
235 */
236 protected function get_data() {
237 $query_args = array(
238 'fields' => 'ids',
239 'posts_per_page' => -1,
240 'meta_query' => array(),
241 'date_query' => array(),
242 'perm' => 'readable',
243 );
244
245 if ( strlen( $this->args['start_date_from'] ) || strlen( $this->args['start_date_to'] ) ) {
246 $query_args['date_query']['inclusive'] = true;
247
248 if ( strlen( $this->args['start_date_from'] ) ) {
249 $query_args['date_query']['after'] = charitable_sanitize_date( $this->args['start_date_from'], 'Y-m-d' );
250 }
251
252 if ( strlen( $this->args['start_date_to'] ) ) {
253 $query_args['date_query']['before'] = charitable_sanitize_date( $this->args['start_date_to'], 'Y-m-d' );
254 }
255 }
256
257 if ( strlen( $this->args['end_date_from'] ) ) {
258 $query_args['meta_query'][] = array(
259 'key' => '_campaign_end_date',
260 'value' => charitable_sanitize_date( $this->args['end_date_from'], 'Y-m-d 00:00:00' ),
261 'compare' => '>=',
262 'type' => 'datetime',
263 );
264 }
265
266 if ( strlen( $this->args['end_date_to'] ) ) {
267 $query_args['meta_query'][] = array(
268 'key' => '_campaign_end_date',
269 'value' => charitable_sanitize_date( $this->args['end_date_to'], 'Y-m-d 00:00:00' ),
270 'compare' => '<=',
271 'type' => 'datetime',
272 );
273 }
274
275 if ( ! empty( $this->args['status'] ) ) {
276 switch ( $this->args['status'] ) {
277 case 'any':
278 $query_args['post_status'] = array( 'draft', 'pending', 'private', 'publish' );
279 break;
280
281 case 'active':
282 $query_args['post_status'] = 'publish';
283 $query_args['meta_query'] = array(
284 'relation' => 'OR',
285 array(
286 'key' => '_campaign_end_date',
287 'value' => date( 'Y-m-d H:i:s' ),
288 'compare' => '>',
289 'type' => 'datetime',
290 ),
291 array(
292 'key' => '_campaign_end_date',
293 'value' => 0,
294 'compare' => '=',
295 ),
296 );
297 break;
298
299 case 'finish':
300 $query_args['post_status'] = 'publish';
301 $query_args['meta_query'] = array(
302 array(
303 'key' => '_campaign_end_date',
304 'value' => date( 'Y-m-d H:i:s' ),
305 'compare' => '<=',
306 'type' => 'datetime',
307 ),
308 );
309 break;
310
311 default:
312 $query_args['post_status'] = $this->args['status'];
313 }
314 }
315
316 if ( ! current_user_can( 'edit_others_campaigns' ) ) {
317 $query_args['author'] = get_current_user_id();
318 }
319
320 /**
321 * Filter the campaigns export query arguments.
322 *
323 * @since 1.6.0
324 *
325 * @param array $query_args The query arguments.
326 * @param array $args The export arguments.
327 */
328 $query_args = apply_filters( 'charitable_export_campaigns_query_args', $query_args, $this->args );
329
330 return Charitable_Campaigns::query( $query_args )->posts;
331 }
332
333 /**
334 * Return the field label for a registered Campaign Field.
335 *
336 * @since 1.6.0
337 *
338 * @param Charitable_Campaign_Field $field Instance of `Charitable_Campaign_Field`.
339 * @return string
340 */
341 protected function get_field_label( Charitable_Campaign_Field $field ) {
342 return $field->label;
343 }
344 }
345
346 endif;
347