← All changes
|
includes/admin/reports/class-charitable-export-donations.php
+67
-17
1.6.7
→
1.8.13
View file →
| @@ -1,13 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Class that is responsible for generating a CSV export of donations. |
| 4 | 4 | * |
| 5 | - * @package Charitable/Classes/Charitable_Export_Donations | |
| 6 | - * @version 1.0.0 | |
| 7 | - * @author Eric Daams | |
| 8 | - * @copyright Copyright (c) 2018, Studio 164a | |
| 9 | - * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License | |
| 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 | |
| 10 | 11 | */ |
| 11 | 12 | |
| 12 | 13 | /* Exit if accessed directly */ |
| 13 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -15,17 +16,12 @@ | ||
| 15 | 16 | } |
| 16 | 17 | |
| 17 | 18 | if ( ! class_exists( 'Charitable_Export_Donations' ) ) : |
| 18 | 19 | |
| 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 | 20 | /** |
| 25 | 21 | * Charitable_Export_Donations |
| 26 | 22 | * |
| 27 | - * @since 1.0.0 | |
| 23 | + * @since 1.0.0 | |
| 28 | 24 | */ |
| 29 | 25 | class Charitable_Export_Donations extends Charitable_Export { |
| 30 | 26 | |
| 31 | 27 | /* The type of export. */ |
| @@ -67,11 +63,21 @@ | ||
| 67 | 63 | */ |
| 68 | 64 | protected $donations = array(); |
| 69 | 65 | |
| 70 | 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 | + /** | |
| 71 | 76 | * Create class object. |
| 72 | 77 | * |
| 73 | 78 | * @since 1.0.0 |
| 79 | + * @since 1.8.1.4 Added formatting money fields. | |
| 74 | 80 | * |
| 75 | 81 | * @param mixed[] $args Arguments for the report. |
| 76 | 82 | */ |
| 77 | 83 | public function __construct( $args ) { |
| @@ -84,14 +90,58 @@ | ||
| 84 | 90 | |
| 85 | 91 | $this->statuses = charitable_get_valid_donation_statuses(); |
| 86 | 92 | $this->fields = array_map( array( $this, 'get_field_label' ), charitable()->donation_fields()->get_export_fields() ); |
| 87 | 93 | |
| 88 | - add_filter( 'charitable_export_data_key_value', array( $this, 'set_custom_field_data' ), 10, 3 ); | |
| 94 | + add_filter( 'charitable_export_data_key_value', array( $this, 'suppliment_charitable_export_data' ), 10, 3 ); | |
| 89 | 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 | + | |
| 90 | 106 | parent::__construct( $args ); |
| 91 | 107 | } |
| 92 | 108 | |
| 93 | 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 | + /* | |
| 122 | + * Core 'subtotal' and 'total' have no value_callback, so they arrive here as | |
| 123 | + * raw database amounts (period as the decimal separator, e.g. "101.78"). Other | |
| 124 | + * money fields ('amount', and addon-provided totals such as Fee Relief's | |
| 125 | + * 'total_donation_with_fees') arrive already localized. On comma-decimal locales, | |
| 126 | + * running a raw DB amount through charitable_format_money() without the db_format | |
| 127 | + * flag makes the "." read as a thousands separator and get stripped | |
| 128 | + * (101.78 -> "10 178,00"). So pass db_format = true only for the raw DB amounts. | |
| 129 | + */ | |
| 130 | + $db_format_fields = apply_filters( | |
| 131 | + 'charitable_export_donations_db_format_money_fields', | |
| 132 | + array( 'subtotal', 'total' ) | |
| 133 | + ); | |
| 134 | + $db_format = in_array( $key, $db_format_fields, true ); | |
| 135 | + | |
| 136 | + return html_entity_decode( trim( charitable_format_money( $custom_field_data, false, $db_format ) ) ); | |
| 137 | + } | |
| 138 | + | |
| 139 | + return $custom_field_data; | |
| 140 | + } | |
| 141 | + | |
| 142 | + | |
| 143 | + /** | |
| 94 | 144 | * Filter the date and time fields. |
| 95 | 145 | * |
| 96 | 146 | * @since 1.0.0 |
| 97 | 147 | * |
| @@ -193,20 +243,20 @@ | ||
| 193 | 243 | protected function get_data() { |
| 194 | 244 | $query_args = array(); |
| 195 | 245 | |
| 196 | 246 | if ( strlen( $this->args['start_date'] ) ) { |
| 197 | - $query_args['start_date'] = charitable_sanitize_date( $this->args['start_date'], 'Y-m-d 00:00:00' ); | |
| 247 | + $query_args['start_date'] = charitable_sanitize_date_alt_format( $this->args['start_date'], 'Y-m-d 00:00:00' ); | |
| 198 | 248 | } |
| 199 | 249 | |
| 200 | 250 | if ( strlen( $this->args['end_date'] ) ) { |
| 201 | - $query_args['end_date'] = charitable_sanitize_date( $this->args['end_date'], 'Y-m-d 23:59:59' ); | |
| 251 | + $query_args['end_date'] = charitable_sanitize_date_alt_format( $this->args['end_date'], 'Y-m-d 23:59:59' ); | |
| 202 | 252 | } |
| 203 | 253 | |
| 204 | - if ( 'all' != $this->args['campaign_id'] ) { | |
| 254 | + if ( 'all' !== $this->args['campaign_id'] ) { | |
| 205 | 255 | $query_args['campaign_id'] = $this->args['campaign_id']; |
| 206 | 256 | } |
| 207 | 257 | |
| 208 | - if ( 'all' != $this->args['status'] ) { | |
| 258 | + if ( 'all' !== $this->args['status'] ) { | |
| 209 | 259 | $query_args['status'] = $this->args['status']; |
| 210 | 260 | } |
| 211 | 261 | |
| 212 | 262 | /** |
| @@ -215,9 +265,9 @@ | ||
| 215 | 265 | * @deprecated 1.7.0 |
| 216 | 266 | * |
| 217 | 267 | * @since 1.3.5 |
| 218 | 268 | */ |
| 219 | - $query_args = apply_filters( 'chairtable_export_donations_query_args', $query_args, $this->args ); | |
| 269 | + $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. | |
| 220 | 270 | |
| 221 | 271 | /** |
| 222 | 272 | * Filter donations query arguments. |
| 223 | 273 | * |