| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that is responsible for generating a CSV export of donations. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Export_Donations |
| 6 |
* @version 1.0.0 |
| 7 |
* @author Eric Daams |
| 8 |
* @copyright Copyright (c) 2019, Studio 164a |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
/* Exit if accessed directly */ |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Export_Donations' ) ) : |
| 18 |
|
| 19 |
/* Include Charitable_Export base class. */ |
| 20 |
if ( ! class_exists( 'Charitable_Export' ) ) { |
| 21 |
require_once( charitable()->get_path( 'includes' ) . 'abstracts/abstract-class-charitable-export.php' ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Charitable_Export_Donations |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
class Charitable_Export_Donations extends Charitable_Export { |
| 30 |
|
| 31 |
/* The type of export. */ |
| 32 |
const EXPORT_TYPE = 'donations'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Default arguments. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* |
| 39 |
* @var mixed[] |
| 40 |
*/ |
| 41 |
protected $defaults; |
| 42 |
|
| 43 |
/** |
| 44 |
* List of donation statuses. |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* |
| 48 |
* @var string[] |
| 49 |
*/ |
| 50 |
protected $statuses; |
| 51 |
|
| 52 |
/** |
| 53 |
* Exportable donation fields. |
| 54 |
* |
| 55 |
* @since 1.5.0 |
| 56 |
* |
| 57 |
* @var array |
| 58 |
*/ |
| 59 |
protected $fields; |
| 60 |
|
| 61 |
/** |
| 62 |
* Set Charitable_Donation objects. |
| 63 |
* |
| 64 |
* @since 1.5.0 |
| 65 |
* |
| 66 |
* @var Charitable_Donation[] |
| 67 |
*/ |
| 68 |
protected $donations = array(); |
| 69 |
|
| 70 |
/** |
| 71 |
* Create class object. |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* |
| 75 |
* @param mixed[] $args Arguments for the report. |
| 76 |
*/ |
| 77 |
public function __construct( $args ) { |
| 78 |
$this->defaults = array( |
| 79 |
'start_date' => '', |
| 80 |
'end_date' => '', |
| 81 |
'campaign_id' => 'all', |
| 82 |
'status' => 'all', |
| 83 |
); |
| 84 |
|
| 85 |
$this->statuses = charitable_get_valid_donation_statuses(); |
| 86 |
$this->fields = array_map( array( $this, 'get_field_label' ), charitable()->donation_fields()->get_export_fields() ); |
| 87 |
|
| 88 |
add_filter( 'charitable_export_data_key_value', array( $this, 'set_custom_field_data' ), 10, 3 ); |
| 89 |
|
| 90 |
parent::__construct( $args ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Filter the date and time fields. |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* |
| 98 |
* @param mixed $value The value to set. |
| 99 |
* @param string $key The key to set. |
| 100 |
* @param array $data The set of data. |
| 101 |
* @return mixed |
| 102 |
*/ |
| 103 |
public function set_custom_field_data( $value, $key, $data ) { |
| 104 |
if ( array_key_exists( $key, $this->fields ) ) { |
| 105 |
$donation = $this->get_donation( $data['donation_id'] ); |
| 106 |
$value = charitable_get_sanitized_donation_field_value( $donation->get( $key ), $key ); |
| 107 |
} |
| 108 |
|
| 109 |
return $value; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Return the CSV column headers. |
| 114 |
* |
| 115 |
* The columns are set as a key=>label array, where the key is used to retrieve the data for that column. |
| 116 |
* |
| 117 |
* @since 1.0.0 |
| 118 |
* |
| 119 |
* @return string[] |
| 120 |
*/ |
| 121 |
protected function get_csv_columns() { |
| 122 |
$non_field_columns = array( |
| 123 |
'campaign_id' => '', |
| 124 |
'campaign_name' => '', |
| 125 |
'amount' => '', |
| 126 |
); |
| 127 |
|
| 128 |
$default_columns = array( |
| 129 |
'donation_id' => __( 'Donation ID', 'charitable' ), |
| 130 |
'campaign_id' => __( 'Campaign ID', 'charitable' ), |
| 131 |
'campaign_name' => __( 'Campaign Title', 'charitable' ), |
| 132 |
'first_name' => __( 'First Name', 'charitable' ), |
| 133 |
'last_name' => __( 'Last Name', 'charitable' ), |
| 134 |
'email' => __( 'Email', 'charitable' ), |
| 135 |
'address' => __( 'Address', 'charitable' ), |
| 136 |
'address_2' => __( 'Address 2', 'charitable' ), |
| 137 |
'city' => __( 'City', 'charitable' ), |
| 138 |
'state' => __( 'State', 'charitable' ), |
| 139 |
'postcode' => __( 'Postcode', 'charitable' ), |
| 140 |
'country' => __( 'Country', 'charitable' ), |
| 141 |
'phone' => __( 'Phone Number', 'charitable' ), |
| 142 |
'donor_address' => __( 'Address Formatted', 'charitable' ), |
| 143 |
'amount' => __( 'Donation Amount', 'charitable' ), |
| 144 |
'date' => __( 'Date of Donation', 'charitable' ), |
| 145 |
'time' => __( 'Time of Donation', 'charitable' ), |
| 146 |
'status_label' => __( 'Donation Status', 'charitable' ), |
| 147 |
'gateway_label' => __( 'Donation Gateway', 'charitable' ), |
| 148 |
'test_mode' => __( 'Made in Test Mode', 'charitable' ), |
| 149 |
'contact_consent' => __( 'Contact Consent', 'charitable' ), |
| 150 |
); |
| 151 |
|
| 152 |
/** |
| 153 |
* Filter the list of columns in the export. |
| 154 |
* |
| 155 |
* As of Charitable 1.5, the recommended way to add or remove columns to the |
| 156 |
* donation export is through the Donation Fields API. This filter is primarily |
| 157 |
* provided for backwards compatibility and also provides a way to change export |
| 158 |
* column headers without changing the label for the Donation Field. |
| 159 |
* |
| 160 |
* @since 1.0.0 |
| 161 |
* |
| 162 |
* @param array $columns List of columns. |
| 163 |
* @param array $args Export args. |
| 164 |
*/ |
| 165 |
$filtered = apply_filters( 'charitable_export_donations_columns', $default_columns, $this->args ); |
| 166 |
|
| 167 |
return $this->get_merged_fields( $default_columns, $this->fields, $non_field_columns, $filtered ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Return a Donation object for the given ID. |
| 172 |
* |
| 173 |
* @since 1.5.0 |
| 174 |
* |
| 175 |
* @param int $donation_id The donation ID. |
| 176 |
* @return Charitable_Donation |
| 177 |
*/ |
| 178 |
protected function get_donation( $donation_id ) { |
| 179 |
if ( ! array_key_exists( $donation_id, $this->donations ) ) { |
| 180 |
$this->donations[ $donation_id ] = charitable_get_donation( $donation_id ); |
| 181 |
} |
| 182 |
|
| 183 |
return $this->donations[ $donation_id ]; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get the data to be exported. |
| 188 |
* |
| 189 |
* @since 1.0.0 |
| 190 |
* |
| 191 |
* @return array |
| 192 |
*/ |
| 193 |
protected function get_data() { |
| 194 |
$query_args = array(); |
| 195 |
|
| 196 |
if ( strlen( $this->args['start_date'] ) ) { |
| 197 |
$query_args['start_date'] = charitable_sanitize_date( $this->args['start_date'], 'Y-m-d 00:00:00' ); |
| 198 |
} |
| 199 |
|
| 200 |
if ( strlen( $this->args['end_date'] ) ) { |
| 201 |
$query_args['end_date'] = charitable_sanitize_date( $this->args['end_date'], 'Y-m-d 23:59:59' ); |
| 202 |
} |
| 203 |
|
| 204 |
if ( 'all' != $this->args['campaign_id'] ) { |
| 205 |
$query_args['campaign_id'] = $this->args['campaign_id']; |
| 206 |
} |
| 207 |
|
| 208 |
if ( 'all' != $this->args['status'] ) { |
| 209 |
$query_args['status'] = $this->args['status']; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Filter name with misspelling. |
| 214 |
* |
| 215 |
* @deprecated 1.7.0 |
| 216 |
* |
| 217 |
* @since 1.3.5 |
| 218 |
*/ |
| 219 |
$query_args = apply_filters( 'chairtable_export_donations_query_args', $query_args, $this->args ); |
| 220 |
|
| 221 |
/** |
| 222 |
* Filter donations query arguments. |
| 223 |
* |
| 224 |
* @since 1.3.5 |
| 225 |
* |
| 226 |
* @param array $query_args The query arguments. |
| 227 |
* @param array $args The export arguments. |
| 228 |
*/ |
| 229 |
$query_args = apply_filters( 'charitable_export_donations_query_args', $query_args, $this->args ); |
| 230 |
|
| 231 |
return charitable_get_table( 'campaign_donations' )->get_donations_report( $query_args ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Return the field label for a registered Donation Field. |
| 236 |
* |
| 237 |
* @since 1.5.0 |
| 238 |
* |
| 239 |
* @param Charitable_Donation_Field $field Instance of `Charitable_Donation_Field`. |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
protected function get_field_label( Charitable_Donation_Field $field ) { |
| 243 |
return $field->label; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
endif; |
| 248 |
|