← All changes
|
includes/admin/reports/class-charitable-export-campaigns.php
+151
-50
1.6.7
→
1.8.13
View file →
| @@ -2,13 +2,13 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Class that is responsible for generating a CSV export of campaigns. |
| 4 | 4 | * |
| 5 | 5 | * @package Charitable/Classes/Charitable_Export_Campaigns |
| 6 | - * @author Eric Daams | |
| 7 | - * @copyright Copyright (c) 2018, Studio 164a | |
| 6 | + * @author David Bisset | |
| 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 | - * @version 1.6.0 | |
| 10 | + * @version 1.6.39 | |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 14 | exit; |
| @@ -15,13 +15,8 @@ | ||
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | if ( ! class_exists( 'Charitable_Export_Campaigns' ) ) : |
| 18 | 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 | 19 | /** |
| 25 | 20 | * Charitable_Export_Campaigns |
| 26 | 21 | * |
| 27 | 22 | * @since 1.6.0 |
| @@ -76,8 +71,17 @@ | ||
| 76 | 71 | */ |
| 77 | 72 | protected $creators = array(); |
| 78 | 73 | |
| 79 | 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 | + /** | |
| 80 | 84 | * Create class object. |
| 81 | 85 | * |
| 82 | 86 | * @since 1.6.0 |
| 83 | 87 | * |
| @@ -83,32 +87,97 @@ | ||
| 83 | 87 | * |
| 84 | 88 | * @param mixed[] $args Arguments for the report. |
| 85 | 89 | */ |
| 86 | 90 | public function __construct( $args ) { |
| 87 | - $this->defaults = array( | |
| 88 | - 'start_date' => '', | |
| 89 | - 'end_date' => '', | |
| 90 | - 'status' => '', | |
| 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 | + ) | |
| 91 | 110 | ); |
| 92 | 111 | |
| 93 | 112 | $this->fields = array_map( array( $this, 'get_field_label' ), charitable()->campaign_fields()->get_export_fields() ); |
| 94 | 113 | |
| 95 | - add_filter( 'charitable_export_data_key_value', array( $this, 'set_custom_field_data' ), 10, 3 ); | |
| 114 | + add_filter( 'charitable_export_data_key_value', array( $this, 'suppliment_charitable_export_data' ), 10, 3 ); | |
| 96 | 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 | + | |
| 97 | 125 | parent::__construct( $args ); |
| 98 | 126 | } |
| 99 | 127 | |
| 100 | 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 | + /** | |
| 101 | 168 | * Filter the cell values. |
| 102 | 169 | * |
| 103 | 170 | * @since 1.6.0 |
| 104 | 171 | * |
| 105 | - * @param mixed $value The value to set. | |
| 106 | - * @param string $key The key to set. | |
| 107 | - * @param int $campaign_id The campaign ID. | |
| 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. | |
| 108 | 175 | * @return mixed |
| 109 | 176 | */ |
| 110 | - public function set_custom_field_data( $value, $key, $campaign_id ) { | |
| 177 | + public function set_custom_field_data( $value, $key, $data ) { | |
| 178 | + $campaign_id = current( $data ); | |
| 179 | + | |
| 111 | 180 | if ( array_key_exists( $key, $this->fields ) ) { |
| 112 | 181 | $value = $this->get_campaign( $campaign_id )->get( $key ); |
| 113 | 182 | } |
| 114 | 183 | |
| @@ -115,8 +184,35 @@ | ||
| 115 | 184 | return $value; |
| 116 | 185 | } |
| 117 | 186 | |
| 118 | 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 | + /** | |
| 119 | 215 | * Return the CSV column headers. |
| 120 | 216 | * |
| 121 | 217 | * The columns are set as a key=>label array, where the key is used to retrieve the data for that column. |
| 122 | 218 | * |
| @@ -201,35 +297,56 @@ | ||
| 201 | 297 | protected function get_data() { |
| 202 | 298 | $query_args = array( |
| 203 | 299 | 'fields' => 'ids', |
| 204 | 300 | 'posts_per_page' => -1, |
| 301 | + 'meta_query' => array(), // phpcs:ignore | |
| 302 | + 'date_query' => array(), | |
| 303 | + 'perm' => 'readable', | |
| 205 | 304 | ); |
| 206 | 305 | |
| 207 | - if ( strlen( $this->args['start_date'] ) || strlen( $this->args['end_date'] ) ) { | |
| 208 | - $date_query = array( | |
| 209 | - 'inclusive' => true, | |
| 210 | - ); | |
| 306 | + if ( strlen( $this->args['start_date_from'] ) || strlen( $this->args['start_date_to'] ) ) { | |
| 307 | + $query_args['date_query']['inclusive'] = true; | |
| 211 | 308 | |
| 212 | - if ( strlen( $this->args['start_date'] ) ) { | |
| 213 | - $date_query['after'] = charitable_sanitize_date( $this->args['start_date'], 'Y-m-d' ); | |
| 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' ); | |
| 214 | 311 | } |
| 215 | 312 | |
| 216 | - if ( strlen( $this->args['end_date'] ) ) { | |
| 217 | - $date_query['before'] = charitable_sanitize_date( $this->args['end_date'], 'Y-m-d' ); | |
| 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' ); | |
| 218 | 315 | } |
| 316 | + } | |
| 219 | 317 | |
| 220 | - $query_args['date_query'] = $date_query; | |
| 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 | + ); | |
| 221 | 325 | } |
| 222 | 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 | + | |
| 223 | 336 | if ( ! empty( $this->args['status'] ) ) { |
| 224 | 337 | switch ( $this->args['status'] ) { |
| 338 | + case 'any': | |
| 339 | + $query_args['post_status'] = array( 'draft', 'pending', 'private', 'publish' ); | |
| 340 | + break; | |
| 341 | + | |
| 225 | 342 | case 'active': |
| 226 | 343 | $query_args['post_status'] = 'publish'; |
| 227 | - $query_args['meta_query'] = array( | |
| 344 | + $query_args['meta_query'] = array( // phpcs:ignore | |
| 228 | 345 | 'relation' => 'OR', |
| 229 | 346 | array( |
| 230 | 347 | 'key' => '_campaign_end_date', |
| 231 | - 'value' => date( 'Y-m-d H:i:s' ), | |
| 348 | + 'value' => gmdate( 'Y-m-d H:i:s' ), | |
| 232 | 349 | 'compare' => '>', |
| 233 | 350 | 'type' => 'datetime', |
| 234 | 351 | ), |
| 235 | 352 | array( |
| @@ -241,12 +358,12 @@ | ||
| 241 | 358 | break; |
| 242 | 359 | |
| 243 | 360 | case 'finish': |
| 244 | 361 | $query_args['post_status'] = 'publish'; |
| 245 | - $query_args['meta_query'] = array( | |
| 362 | + $query_args['meta_query'] = array( // phpcs:ignore | |
| 246 | 363 | array( |
| 247 | 364 | 'key' => '_campaign_end_date', |
| 248 | - 'value' => date( 'Y-m-d H:i:s' ), | |
| 365 | + 'value' => gmdate( 'Y-m-d H:i:s' ), | |
| 249 | 366 | 'compare' => '<=', |
| 250 | 367 | 'type' => 'datetime', |
| 251 | 368 | ), |
| 252 | 369 | ); |
| @@ -256,8 +373,12 @@ | ||
| 256 | 373 | $query_args['post_status'] = $this->args['status']; |
| 257 | 374 | } |
| 258 | 375 | } |
| 259 | 376 | |
| 377 | + if ( ! current_user_can( 'edit_others_campaigns' ) ) { // phpcs:ignore | |
| 378 | + $query_args['author'] = get_current_user_id(); | |
| 379 | + } | |
| 380 | + | |
| 260 | 381 | /** |
| 261 | 382 | * Filter the campaigns export query arguments. |
| 262 | 383 | * |
| 263 | 384 | * @since 1.6.0 |
| @@ -267,28 +388,8 @@ | ||
| 267 | 388 | */ |
| 268 | 389 | $query_args = apply_filters( 'charitable_export_campaigns_query_args', $query_args, $this->args ); |
| 269 | 390 | |
| 270 | 391 | return Charitable_Campaigns::query( $query_args )->posts; |
| 271 | - } | |
| 272 | - | |
| 273 | - /** | |
| 274 | - * Receives a row of data and maps it to the keys defined in the columns. | |
| 275 | - * | |
| 276 | - * @since 1.6.0 | |
| 277 | - * | |
| 278 | - * @param int $data The campaign ID. | |
| 279 | - * @return mixed | |
| 280 | - */ | |
| 281 | - protected function map_data( $data ) { | |
| 282 | - $row = array(); | |
| 283 | - | |
| 284 | - foreach ( $this->columns as $key => $label ) { | |
| 285 | - $value = isset( $data[ $key ] ) ? $data[ $key ] : ''; | |
| 286 | - $value = apply_filters( 'charitable_export_data_key_value', $value, $key, $data ); | |
| 287 | - $row[] = $value; | |
| 288 | - } | |
| 289 | - | |
| 290 | - return $row; | |
| 291 | 392 | } |
| 292 | 393 | |
| 293 | 394 | /** |
| 294 | 395 | * Return the field label for a registered Campaign Field. |