| 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) 2023, 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 |
* Format money fields. |
| 76 |
* |
| 77 |
* @since 1.8.1.4 |
| 78 |
* |
| 79 |
* @var mixed[] |
| 80 |
*/ |
| 81 |
protected $format_money_fields = array(); |
| 82 |
|
| 83 |
/** |
| 84 |
* Create class object. |
| 85 |
* |
| 86 |
* @since 1.6.0 |
| 87 |
* |
| 88 |
* @param mixed[] $args Arguments for the report. |
| 89 |
*/ |
| 90 |
public function __construct( $args ) { |
| 91 |
/** |
| 92 |
* Filter the default campaign export arguments. |
| 93 |
* |
| 94 |
* @since 1.6.36 |
| 95 |
* @since 1.8.1.4 Added formatting money fields. |
| 96 |
* |
| 97 |
* @param array $defaults Default arguments. |
| 98 |
*/ |
| 99 |
$this->defaults = apply_filters( |
| 100 |
'charitable_export_campaigns_default_args', |
| 101 |
array( |
| 102 |
'start_date_from' => '', |
| 103 |
'start_date_to' => '', |
| 104 |
'end_date_from' => '', |
| 105 |
'end_date_to' => '', |
| 106 |
'status' => '', |
| 107 |
'start_date' => '', // Kept for backwards compatibility, @since 1.6.36. |
| 108 |
'end_date' => '', // Kept for backwards compatibility, @since 1.6.36. |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
$this->fields = array_map( array( $this, 'get_field_label' ), charitable()->campaign_fields()->get_export_fields() ); |
| 113 |
|
| 114 |
add_filter( 'charitable_export_data_key_value', array( $this, 'suppliment_charitable_export_data' ), 10, 3 ); |
| 115 |
|
| 116 |
$this->format_money_fields = apply_filters( |
| 117 |
'charitable_export_campaigns_format_money_fields', |
| 118 |
array( |
| 119 |
'donated_amount', |
| 120 |
'goal', |
| 121 |
'minimum_donation_amount', |
| 122 |
) |
| 123 |
); |
| 124 |
|
| 125 |
parent::__construct( $args ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Suppliments the data exported for campaigns with custom field data. |
| 130 |
* |
| 131 |
* @since 1.6.0 |
| 132 |
* |
| 133 |
* @param mixed[]|false $custom_field_data Optional. Custom field data to add to the exported data. Default false. |
| 134 |
* @param string|false $key Optional. The key to use for the custom field data. Default false. |
| 135 |
* @param mixed[] $data Optional. The exported data. Default empty array. |
| 136 |
* |
| 137 |
* @return mixed[] The exported data with custom field data added. |
| 138 |
*/ |
| 139 |
public function suppliment_charitable_export_data( $custom_field_data = false, $key = false, $data = array() ) { |
| 140 |
|
| 141 |
if ( false === $custom_field_data && ! empty( $data ) && intval( $data[0] ) !== 0 ) { |
| 142 |
$the_post = get_post( $data[0] ); |
| 143 |
if ( $key === 'ID' ) { |
| 144 |
return $the_post->ID; |
| 145 |
} |
| 146 |
if ( $key === 'post_title' ) { |
| 147 |
return $the_post->post_title; |
| 148 |
} |
| 149 |
if ( $key === 'campaign_creator_id' || $key === 'post_author' ) { |
| 150 |
return intval( $the_post->post_author ); |
| 151 |
} |
| 152 |
if ( $key === 'campaign_creator_name' ) { |
| 153 |
return the_author_meta( 'display_name', $the_post->post_author ); |
| 154 |
} |
| 155 |
if ( $key === 'post_date' ) { |
| 156 |
return ! empty( $the_post->post_date ) ? gmdate( 'F j, Y g:i a', strtotime( $the_post->post_date ) ) : ''; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
if ( in_array( $key, $this->format_money_fields, true ) ) { |
| 161 |
return html_entity_decode( trim( charitable_format_money( $custom_field_data ) ) ); |
| 162 |
} |
| 163 |
|
| 164 |
return $custom_field_data; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Filter the cell values. |
| 169 |
* |
| 170 |
* @since 1.6.0 |
| 171 |
* |
| 172 |
* @param mixed $value The value to set. |
| 173 |
* @param string $key The key to set. |
| 174 |
* @param array $data The data array, which just contains the campaign ID. |
| 175 |
* @return mixed |
| 176 |
*/ |
| 177 |
public function set_custom_field_data( $value, $key, $data ) { |
| 178 |
$campaign_id = current( $data ); |
| 179 |
|
| 180 |
if ( array_key_exists( $key, $this->fields ) ) { |
| 181 |
$value = $this->get_campaign( $campaign_id )->get( $key ); |
| 182 |
} |
| 183 |
|
| 184 |
return $value; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Parse the arguments. |
| 189 |
* |
| 190 |
* @since 1.6.36 |
| 191 |
* @param array $args Arguments for the report. |
| 192 |
* |
| 193 |
* @return array |
| 194 |
*/ |
| 195 |
protected function parse_args( $args ) { |
| 196 |
$parsed = wp_parse_args( $args, $this->defaults ); |
| 197 |
|
| 198 |
if ( empty( $parsed['start_date_from'] ) && strlen( $parsed['start_date'] ) ) { |
| 199 |
$parsed['start_date_from'] = $parsed['start_date']; |
| 200 |
} |
| 201 |
|
| 202 |
if ( empty( $parsed['start_date_to'] ) && strlen( $parsed['end_date'] ) ) { |
| 203 |
$parsed['start_date_to'] = $parsed['end_date']; |
| 204 |
} |
| 205 |
|
| 206 |
unset( |
| 207 |
$parsed['start_date'], |
| 208 |
$parsed['end_date'] |
| 209 |
); |
| 210 |
|
| 211 |
return $parsed; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Return the CSV column headers. |
| 216 |
* |
| 217 |
* The columns are set as a key=>label array, where the key is used to retrieve the data for that column. |
| 218 |
* |
| 219 |
* @since 1.6.0 |
| 220 |
* |
| 221 |
* @return string[] |
| 222 |
*/ |
| 223 |
protected function get_csv_columns() { |
| 224 |
$default_columns = array( |
| 225 |
'ID' => __( 'Campaign ID', 'charitable' ), |
| 226 |
'post_title' => __( 'Title', 'charitable' ), |
| 227 |
'post_date' => __( 'Date Created', 'charitable' ), |
| 228 |
'end_date' => __( 'End Date', 'charitable' ), |
| 229 |
'goal' => __( 'Goal', 'charitable' ), |
| 230 |
'donated_amount' => __( 'Amount Donated', 'charitable' ), |
| 231 |
'percent_donated_raw' => __( 'Percentage of Goal Raised', 'charitable' ), |
| 232 |
'status' => __( 'Status', 'charitable' ), |
| 233 |
'campaign_creator_name' => __( 'Campaign Creator', 'charitable' ), |
| 234 |
'campaign_creator_id' => __( 'Campaign Creator ID', 'charitable' ), |
| 235 |
'campaign_creator_email' => __( 'Campaign Creator Email', 'charitable' ), |
| 236 |
); |
| 237 |
|
| 238 |
/** |
| 239 |
* Filter the list of columns in the export. |
| 240 |
* |
| 241 |
* The recommended way to add or remove columns to the campaign export |
| 242 |
* is through the Campaign Fields API. This filter is provided as a way |
| 243 |
* to change export column headers without changing the label for the |
| 244 |
* Campaign Field. |
| 245 |
* |
| 246 |
* @since 1.6.0 |
| 247 |
* |
| 248 |
* @param array $columns List of columns. |
| 249 |
* @param array $args Export args. |
| 250 |
*/ |
| 251 |
$filtered = apply_filters( 'charitable_export_campaigns_columns', $default_columns, $this->args ); |
| 252 |
|
| 253 |
return $this->get_merged_fields( $default_columns, $this->fields, array(), $filtered ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Return a Campaign object for the given ID. |
| 258 |
* |
| 259 |
* @since 1.6.0 |
| 260 |
* |
| 261 |
* @param int $campaign_id The campaign ID. |
| 262 |
* @return Charitable_Campaign |
| 263 |
*/ |
| 264 |
protected function get_campaign( $campaign_id ) { |
| 265 |
if ( ! array_key_exists( $campaign_id, $this->campaigns ) ) { |
| 266 |
$this->campaigns[ $campaign_id ] = charitable_get_campaign( $campaign_id ); |
| 267 |
} |
| 268 |
|
| 269 |
return $this->campaigns[ $campaign_id ]; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Return a User object for a campaign creator. |
| 274 |
* |
| 275 |
* @since 1.6.0 |
| 276 |
* |
| 277 |
* @param int $campaign_id The campaign ID. |
| 278 |
* @return Charitable_User |
| 279 |
*/ |
| 280 |
public function get_campaign_creator( $campaign_id ) { |
| 281 |
$creator = $this->get_campaign( $campaign_id )->get_campaign_creator(); |
| 282 |
|
| 283 |
if ( ! array_key_exists( $creator, $this->creators ) ) { |
| 284 |
$this->creators[ $creator ] = charitable_get_user( $creator ); |
| 285 |
} |
| 286 |
|
| 287 |
return $this->creators[ $creator ]; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Get the data to be exported. |
| 292 |
* |
| 293 |
* @since 1.6.0 |
| 294 |
* |
| 295 |
* @return array |
| 296 |
*/ |
| 297 |
protected function get_data() { |
| 298 |
$query_args = array( |
| 299 |
'fields' => 'ids', |
| 300 |
'posts_per_page' => -1, |
| 301 |
'meta_query' => array(), // phpcs:ignore |
| 302 |
'date_query' => array(), |
| 303 |
'perm' => 'readable', |
| 304 |
); |
| 305 |
|
| 306 |
if ( strlen( $this->args['start_date_from'] ) || strlen( $this->args['start_date_to'] ) ) { |
| 307 |
$query_args['date_query']['inclusive'] = true; |
| 308 |
|
| 309 |
if ( strlen( $this->args['start_date_from'] ) ) { |
| 310 |
$query_args['date_query']['after'] = charitable_sanitize_date_alt_format( $this->args['start_date_from'], 'Y-m-d' ); |
| 311 |
} |
| 312 |
|
| 313 |
if ( strlen( $this->args['start_date_to'] ) ) { |
| 314 |
$query_args['date_query']['before'] = charitable_sanitize_date_alt_format( $this->args['start_date_to'], 'Y-m-d' ); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if ( strlen( $this->args['end_date_from'] ) ) { |
| 319 |
$query_args['meta_query'][] = array( |
| 320 |
'key' => '_campaign_end_date', |
| 321 |
'value' => charitable_sanitize_date_alt_format( $this->args['end_date_from'], 'Y-m-d 00:00:00' ), |
| 322 |
'compare' => '>=', |
| 323 |
'type' => 'datetime', |
| 324 |
); |
| 325 |
} |
| 326 |
|
| 327 |
if ( strlen( $this->args['end_date_to'] ) ) { |
| 328 |
$query_args['meta_query'][] = array( |
| 329 |
'key' => '_campaign_end_date', |
| 330 |
'value' => charitable_sanitize_date_alt_format( $this->args['end_date_to'], 'Y-m-d 00:00:00' ), |
| 331 |
'compare' => '<=', |
| 332 |
'type' => 'datetime', |
| 333 |
); |
| 334 |
} |
| 335 |
|
| 336 |
if ( ! empty( $this->args['status'] ) ) { |
| 337 |
switch ( $this->args['status'] ) { |
| 338 |
case 'any': |
| 339 |
$query_args['post_status'] = array( 'draft', 'pending', 'private', 'publish' ); |
| 340 |
break; |
| 341 |
|
| 342 |
case 'active': |
| 343 |
$query_args['post_status'] = 'publish'; |
| 344 |
$query_args['meta_query'] = array( // phpcs:ignore |
| 345 |
'relation' => 'OR', |
| 346 |
array( |
| 347 |
'key' => '_campaign_end_date', |
| 348 |
'value' => gmdate( 'Y-m-d H:i:s' ), |
| 349 |
'compare' => '>', |
| 350 |
'type' => 'datetime', |
| 351 |
), |
| 352 |
array( |
| 353 |
'key' => '_campaign_end_date', |
| 354 |
'value' => 0, |
| 355 |
'compare' => '=', |
| 356 |
), |
| 357 |
); |
| 358 |
break; |
| 359 |
|
| 360 |
case 'finish': |
| 361 |
$query_args['post_status'] = 'publish'; |
| 362 |
$query_args['meta_query'] = array( // phpcs:ignore |
| 363 |
array( |
| 364 |
'key' => '_campaign_end_date', |
| 365 |
'value' => gmdate( 'Y-m-d H:i:s' ), |
| 366 |
'compare' => '<=', |
| 367 |
'type' => 'datetime', |
| 368 |
), |
| 369 |
); |
| 370 |
break; |
| 371 |
|
| 372 |
default: |
| 373 |
$query_args['post_status'] = $this->args['status']; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
if ( ! current_user_can( 'edit_others_campaigns' ) ) { // phpcs:ignore |
| 378 |
$query_args['author'] = get_current_user_id(); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Filter the campaigns export query arguments. |
| 383 |
* |
| 384 |
* @since 1.6.0 |
| 385 |
* |
| 386 |
* @param array $query_args The query arguments. |
| 387 |
* @param array $args The export arguments. |
| 388 |
*/ |
| 389 |
$query_args = apply_filters( 'charitable_export_campaigns_query_args', $query_args, $this->args ); |
| 390 |
|
| 391 |
return Charitable_Campaigns::query( $query_args )->posts; |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Return the field label for a registered Campaign Field. |
| 396 |
* |
| 397 |
* @since 1.6.0 |
| 398 |
* |
| 399 |
* @param Charitable_Campaign_Field $field Instance of `Charitable_Campaign_Field`. |
| 400 |
* @return string |
| 401 |
*/ |
| 402 |
protected function get_field_label( Charitable_Campaign_Field $field ) { |
| 403 |
return $field->label; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
endif; |
| 408 |
|