← All changes
|
includes/admin/reports/class-charitable-export-campaigns.php
+68
-7
1.7.0.12
→
1.8.13
View file →
| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | * Class that is responsible for generating a CSV export of campaigns. |
| 4 | 4 | * |
| 5 | 5 | * @package Charitable/Classes/Charitable_Export_Campaigns |
| 6 | 6 | * @author David Bisset |
| 7 | - * @copyright Copyright (c) 2022, WP Charitable LLC | |
| 7 | + * @copyright Copyright (c) 2023, WP Charitable LLC | |
| 8 | 8 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 | 9 | * @since 1.6.0 |
| 10 | 10 | * @version 1.6.39 |
| 11 | 11 | */ |
| @@ -71,8 +71,17 @@ | ||
| 71 | 71 | */ |
| 72 | 72 | protected $creators = array(); |
| 73 | 73 | |
| 74 | 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 | + /** | |
| 75 | 84 | * Create class object. |
| 76 | 85 | * |
| 77 | 86 | * @since 1.6.0 |
| 78 | 87 | * |
| @@ -82,8 +91,9 @@ | ||
| 82 | 91 | /** |
| 83 | 92 | * Filter the default campaign export arguments. |
| 84 | 93 | * |
| 85 | 94 | * @since 1.6.36 |
| 95 | + * @since 1.8.1.4 Added formatting money fields. | |
| 86 | 96 | * |
| 87 | 97 | * @param array $defaults Default arguments. |
| 88 | 98 | */ |
| 89 | 99 | $this->defaults = apply_filters( |
| @@ -100,12 +110,62 @@ | ||
| 100 | 110 | ); |
| 101 | 111 | |
| 102 | 112 | $this->fields = array_map( array( $this, 'get_field_label' ), charitable()->campaign_fields()->get_export_fields() ); |
| 103 | 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 | + | |
| 104 | 125 | parent::__construct( $args ); |
| 105 | 126 | } |
| 106 | 127 | |
| 107 | 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 | + /** | |
| 108 | 168 | * Filter the cell values. |
| 109 | 169 | * |
| 110 | 170 | * @since 1.6.0 |
| 111 | 171 | * |
| @@ -127,8 +187,9 @@ | ||
| 127 | 187 | /** |
| 128 | 188 | * Parse the arguments. |
| 129 | 189 | * |
| 130 | 190 | * @since 1.6.36 |
| 191 | + * @param array $args Arguments for the report. | |
| 131 | 192 | * |
| 132 | 193 | * @return array |
| 133 | 194 | */ |
| 134 | 195 | protected function parse_args( $args ) { |
| @@ -236,9 +297,9 @@ | ||
| 236 | 297 | protected function get_data() { |
| 237 | 298 | $query_args = array( |
| 238 | 299 | 'fields' => 'ids', |
| 239 | 300 | 'posts_per_page' => -1, |
| 240 | - 'meta_query' => array(), | |
| 301 | + 'meta_query' => array(), // phpcs:ignore | |
| 241 | 302 | 'date_query' => array(), |
| 242 | 303 | 'perm' => 'readable', |
| 243 | 304 | ); |
| 244 | 305 | |
| @@ -279,13 +340,13 @@ | ||
| 279 | 340 | break; |
| 280 | 341 | |
| 281 | 342 | case 'active': |
| 282 | 343 | $query_args['post_status'] = 'publish'; |
| 283 | - $query_args['meta_query'] = array( | |
| 344 | + $query_args['meta_query'] = array( // phpcs:ignore | |
| 284 | 345 | 'relation' => 'OR', |
| 285 | 346 | array( |
| 286 | 347 | 'key' => '_campaign_end_date', |
| 287 | - 'value' => date( 'Y-m-d H:i:s' ), | |
| 348 | + 'value' => gmdate( 'Y-m-d H:i:s' ), | |
| 288 | 349 | 'compare' => '>', |
| 289 | 350 | 'type' => 'datetime', |
| 290 | 351 | ), |
| 291 | 352 | array( |
| @@ -297,12 +358,12 @@ | ||
| 297 | 358 | break; |
| 298 | 359 | |
| 299 | 360 | case 'finish': |
| 300 | 361 | $query_args['post_status'] = 'publish'; |
| 301 | - $query_args['meta_query'] = array( | |
| 362 | + $query_args['meta_query'] = array( // phpcs:ignore | |
| 302 | 363 | array( |
| 303 | 364 | 'key' => '_campaign_end_date', |
| 304 | - 'value' => date( 'Y-m-d H:i:s' ), | |
| 365 | + 'value' => gmdate( 'Y-m-d H:i:s' ), | |
| 305 | 366 | 'compare' => '<=', |
| 306 | 367 | 'type' => 'datetime', |
| 307 | 368 | ), |
| 308 | 369 | ); |
| @@ -312,9 +373,9 @@ | ||
| 312 | 373 | $query_args['post_status'] = $this->args['status']; |
| 313 | 374 | } |
| 314 | 375 | } |
| 315 | 376 | |
| 316 | - if ( ! current_user_can( 'edit_others_campaigns' ) ) { | |
| 377 | + if ( ! current_user_can( 'edit_others_campaigns' ) ) { // phpcs:ignore | |
| 317 | 378 | $query_args['author'] = get_current_user_id(); |
| 318 | 379 | } |
| 319 | 380 | |
| 320 | 381 | /** |