class-batch-export-donors.php
8 years ago
class-batch-export-forms.php
8 years ago
class-batch-export.php
8 years ago
class-core-settings-export.php
8 years ago
class-export-earnings.php
8 years ago
class-export.php
8 years ago
class-give-export-donations.php
8 years ago
export-actions.php
8 years ago
export-functions.php
8 years ago
give-export-donations-exporter.php
8 years ago
give-export-donations-functions.php
8 years ago
pdf-reports.php
8 years ago
give-export-donations-functions.php
538 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Give Export Donations Functions |
| 4 | */ |
| 5 | |
| 6 | |
| 7 | /** |
| 8 | * AJAX |
| 9 | * |
| 10 | * @see http://wordpress.stackexchange.com/questions/58834/echo-all-meta-keys-of-a-custom-post-type |
| 11 | * |
| 12 | * @return string |
| 13 | */ |
| 14 | function give_export_donations_get_custom_fields() { |
| 15 | |
| 16 | global $wpdb; |
| 17 | $post_type = 'give_payment'; |
| 18 | $responses = array(); |
| 19 | |
| 20 | $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : ''; |
| 21 | |
| 22 | if ( empty( $form_id ) ) { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | $args = array( |
| 27 | 'give_forms' => array( $form_id ), |
| 28 | 'posts_per_page' => - 1, |
| 29 | 'fields' => 'ids', |
| 30 | ); |
| 31 | $donation_list = implode( ',', (array) give_get_payments( $args ) ); |
| 32 | |
| 33 | $query_and = sprintf( |
| 34 | "AND $wpdb->posts.ID IN (%s) |
| 35 | AND $wpdb->paymentmeta.meta_key != '' |
| 36 | AND $wpdb->paymentmeta.meta_key NOT RegExp '(^[_0-9].+$)'", |
| 37 | $donation_list |
| 38 | ); |
| 39 | |
| 40 | $query = " |
| 41 | SELECT DISTINCT($wpdb->paymentmeta.meta_key) |
| 42 | FROM $wpdb->posts |
| 43 | LEFT JOIN $wpdb->paymentmeta |
| 44 | ON $wpdb->posts.ID = $wpdb->paymentmeta.payment_id |
| 45 | WHERE $wpdb->posts.post_type = '%s' |
| 46 | " . $query_and; |
| 47 | |
| 48 | $meta_keys = $wpdb->get_col( $wpdb->prepare( $query, $post_type ) ); |
| 49 | |
| 50 | if ( ! empty( $meta_keys ) ) { |
| 51 | $responses['standard_fields'] = array_values( $meta_keys ); |
| 52 | } |
| 53 | |
| 54 | $query_and = sprintf( |
| 55 | "AND $wpdb->posts.ID IN (%s) |
| 56 | AND $wpdb->paymentmeta.meta_key != '' |
| 57 | AND $wpdb->paymentmeta.meta_key NOT RegExp '^[^_]'", |
| 58 | $donation_list |
| 59 | ); |
| 60 | |
| 61 | $query = " |
| 62 | SELECT DISTINCT($wpdb->paymentmeta.meta_key) |
| 63 | FROM $wpdb->posts |
| 64 | LEFT JOIN $wpdb->paymentmeta |
| 65 | ON $wpdb->posts.ID = $wpdb->paymentmeta.payment_id |
| 66 | WHERE $wpdb->posts.post_type = '%s' |
| 67 | " . $query_and; |
| 68 | |
| 69 | $hidden_meta_keys = $wpdb->get_col( $wpdb->prepare( $query, $post_type ) ); |
| 70 | |
| 71 | /** |
| 72 | * Filter to modify hidden keys that are going to be ignore when displaying the hidden keys |
| 73 | * |
| 74 | * @since 2.1 |
| 75 | * |
| 76 | * @param array $ignore_hidden_keys Hidden keys that are going to be ignore |
| 77 | * @param array $form_id Donation form id |
| 78 | * |
| 79 | * @return array $ignore_hidden_keys Hidden keys that are going to be ignore |
| 80 | */ |
| 81 | $ignore_hidden_keys = apply_filters( 'give_export_donations_ignore_hidden_keys', array( |
| 82 | '_give_payment_meta', |
| 83 | '_give_payment_gateway', |
| 84 | '_give_payment_form_title', |
| 85 | '_give_payment_form_id', |
| 86 | '_give_payment_price_id', |
| 87 | '_give_payment_user_id', |
| 88 | '_give_payment_user_email', |
| 89 | '_give_payment_user_ip', |
| 90 | '_give_payment_customer_id', |
| 91 | '_give_payment_total', |
| 92 | '_give_completed_date', |
| 93 | '_give_donation_company', |
| 94 | '_give_donor_billing_first_name', |
| 95 | '_give_donor_billing_last_name', |
| 96 | '_give_payment_donor_email', |
| 97 | '_give_payment_donor_id', |
| 98 | '_give_payment_date', |
| 99 | '_give_donor_billing_address1', |
| 100 | '_give_donor_billing_address2', |
| 101 | '_give_donor_billing_city', |
| 102 | '_give_donor_billing_zip', |
| 103 | '_give_donor_billing_state', |
| 104 | '_give_donor_billing_country', |
| 105 | '_give_payment_import', |
| 106 | '_give_payment_currency', |
| 107 | '_give_payment_import_id', |
| 108 | '_give_payment_donor_ip', |
| 109 | ), |
| 110 | $form_id |
| 111 | ); |
| 112 | |
| 113 | // Unset ignored hidden keys. |
| 114 | foreach ( $ignore_hidden_keys as $key ) { |
| 115 | if ( ( $key = array_search( $key, $hidden_meta_keys ) ) !== false ) { |
| 116 | unset( $hidden_meta_keys[ $key ] ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if ( ! empty( $hidden_meta_keys ) ) { |
| 121 | $responses['hidden_fields'] = array_values( $hidden_meta_keys ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Filter to modify custom fields when select donation forms, |
| 126 | * |
| 127 | * @since 2.1 |
| 128 | * |
| 129 | * @param array $responses Contain all the fields that need to be display when donation form is display |
| 130 | * @param int $form_id Donation Form ID |
| 131 | * |
| 132 | * @return array $responses |
| 133 | */ |
| 134 | wp_send_json( (array) apply_filters( 'give_export_donations_get_custom_fields', $responses, $form_id ) ); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | add_action( 'wp_ajax_give_export_donations_get_custom_fields', 'give_export_donations_get_custom_fields' ); |
| 139 | |
| 140 | /** |
| 141 | * Register the payments batch exporter |
| 142 | * |
| 143 | * @since 1.0 |
| 144 | */ |
| 145 | function give_register_export_donations_batch_export() { |
| 146 | add_action( 'give_batch_export_class_include', 'give_export_donations_include_export_class', 10, 1 ); |
| 147 | } |
| 148 | |
| 149 | add_action( 'give_register_batch_exporter', 'give_register_export_donations_batch_export', 10 ); |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * Includes the Give Export Donations Custom Exporter Class. |
| 154 | * |
| 155 | * @param $class Give_Export_Donations_CSV |
| 156 | */ |
| 157 | function give_export_donations_include_export_class( $class ) { |
| 158 | if ( 'Give_Export_Donations_CSV' === $class ) { |
| 159 | require_once GIVE_PLUGIN_DIR . 'includes/admin/tools/export/give-export-donations-exporter.php'; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Create column key. |
| 166 | * |
| 167 | * @param $string |
| 168 | * |
| 169 | * @return string |
| 170 | */ |
| 171 | function give_export_donations_create_column_key( $string ) { |
| 172 | return sanitize_key( str_replace( ' ', '_', $string ) ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Filter to modify donation search form when search through AJAX |
| 177 | * |
| 178 | * @since 2.1 |
| 179 | * |
| 180 | * @param $args |
| 181 | * |
| 182 | * @return array |
| 183 | */ |
| 184 | function give_export_donation_form_search_args( $args ) { |
| 185 | if ( empty( $_POST['fields'] ) ) { |
| 186 | return $args; |
| 187 | } |
| 188 | |
| 189 | $fields = isset( $_POST['fields'] ) ? $_POST['fields'] : null; |
| 190 | parse_str( $fields ); |
| 191 | |
| 192 | if ( ! empty( $give_forms_categories ) || ! empty( $give_forms_tags ) ) { |
| 193 | $args['posts_per_page'] = -1; |
| 194 | } |
| 195 | |
| 196 | if ( ! empty( $give_forms_categories ) && ! empty( $give_forms_tags ) ) { |
| 197 | $args['tax_query']['relation'] = 'AND'; |
| 198 | } |
| 199 | |
| 200 | if ( ! empty( $give_forms_categories ) ) { |
| 201 | $args['tax_query'][] = array( |
| 202 | 'taxonomy' => 'give_forms_category', |
| 203 | 'field' => 'term_id', |
| 204 | 'terms' => $give_forms_categories, |
| 205 | 'operator' => 'AND', |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | if ( ! empty( $give_forms_tags ) ) { |
| 210 | $args['tax_query'][] = array( |
| 211 | 'taxonomy' => 'give_forms_tag', |
| 212 | 'field' => 'term_id', |
| 213 | 'terms' => $give_forms_tags, |
| 214 | 'operator' => 'AND', |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | return $args; |
| 219 | } |
| 220 | |
| 221 | add_filter( 'give_ajax_form_search_args', 'give_export_donation_form_search_args' ); |
| 222 | |
| 223 | /** |
| 224 | * Add Donation standard fields in export donation page |
| 225 | * |
| 226 | * @since 2.1 |
| 227 | */ |
| 228 | function give_export_donation_standard_fields() { |
| 229 | ?> |
| 230 | <tr> |
| 231 | <td scope="row" class="row-title"> |
| 232 | <label><?php _e( 'Standard Columns:', 'give' ); ?></label> |
| 233 | </td> |
| 234 | <td> |
| 235 | <div class="give-clearfix"> |
| 236 | <ul class="give-export-option"> |
| 237 | <li class="give-export-option-fields give-export-option-payment-fields"> |
| 238 | <ul class="give-export-option-payment-fields-ul"> |
| 239 | |
| 240 | <li class="give-export-option-label give-export-option-donation-label"> |
| 241 | <span> |
| 242 | <?php _e( 'Donation Payment Fields', 'give' ); ?> |
| 243 | </span> |
| 244 | </li> |
| 245 | |
| 246 | <li class="give-export-option-start"> |
| 247 | <label for="give-export-donation-id"> |
| 248 | <input type="checkbox" checked |
| 249 | name="give_give_donations_export_option[donation_id]" |
| 250 | id="give-export-donation-id"><?php _e( 'Donation ID', 'give' ); ?> |
| 251 | </label> |
| 252 | </li> |
| 253 | |
| 254 | <?php |
| 255 | if ( give_is_setting_enabled( give_get_option( 'sequential-ordering_status', 'disabled' ) ) ) { |
| 256 | ?> |
| 257 | <li> |
| 258 | <label for="give-export-seq-id"> |
| 259 | <input type="checkbox" checked |
| 260 | name="give_give_donations_export_option[seq_id]" |
| 261 | id="give-export-seq-id"><?php _e( 'Donation Number', 'give' ); ?> |
| 262 | </label> |
| 263 | </li> |
| 264 | <?php |
| 265 | } |
| 266 | ?> |
| 267 | |
| 268 | <li> |
| 269 | <label for="give-export-donation-sum"> |
| 270 | <input type="checkbox" checked |
| 271 | name="give_give_donations_export_option[donation_total]" |
| 272 | id="give-export-donation-sum"><?php _e( 'Donation Total', 'give' ); ?> |
| 273 | </label> |
| 274 | </li> |
| 275 | |
| 276 | <li> |
| 277 | <label for="give-export-donation-currency_code"> |
| 278 | <input type="checkbox" checked |
| 279 | name="give_give_donations_export_option[currency_code]" |
| 280 | id="give-export-donation-currency_code"><?php _e( 'Currency Code', 'give' ); ?> |
| 281 | </label> |
| 282 | </li> |
| 283 | |
| 284 | <li> |
| 285 | <label for="give-export-donation-currency_symbol"> |
| 286 | <input type="checkbox" checked |
| 287 | name="give_give_donations_export_option[currency_symbol]" |
| 288 | id="give-export-donation-currency_symbol"><?php _e( 'Currency Symbol', 'give' ); ?> |
| 289 | </label> |
| 290 | </li> |
| 291 | |
| 292 | <li> |
| 293 | <label for="give-export-donation-status"> |
| 294 | <input type="checkbox" checked |
| 295 | name="give_give_donations_export_option[donation_status]" |
| 296 | id="give-export-donation-status"><?php _e( 'Donation Status', 'give' ); ?> |
| 297 | </label> |
| 298 | </li> |
| 299 | |
| 300 | <li> |
| 301 | <label for="give-export-donation-date"> |
| 302 | <input type="checkbox" checked |
| 303 | name="give_give_donations_export_option[donation_date]" |
| 304 | id="give-export-donation-date"><?php _e( 'Donation Date', 'give' ); ?> |
| 305 | </label> |
| 306 | </li> |
| 307 | |
| 308 | <li> |
| 309 | <label for="give-export-donation-time"> |
| 310 | <input type="checkbox" checked |
| 311 | name="give_give_donations_export_option[donation_time]" |
| 312 | id="give-export-donation-time"><?php _e( 'Donation Time', 'give' ); ?> |
| 313 | </label> |
| 314 | </li> |
| 315 | |
| 316 | <li> |
| 317 | <label for="give-export-payment-gateway"> |
| 318 | <input type="checkbox" checked |
| 319 | name="give_give_donations_export_option[payment_gateway]" |
| 320 | id="give-export-payment-gateway"><?php _e( 'Payment Gateway', 'give' ); ?> |
| 321 | </label> |
| 322 | </li> |
| 323 | |
| 324 | <?php |
| 325 | /* |
| 326 | * Action to add extra columns in standard payment fields |
| 327 | * |
| 328 | * @since 2.1 |
| 329 | */ |
| 330 | do_action( 'give_export_donation_standard_payment_fields' ); |
| 331 | ?> |
| 332 | </ul> |
| 333 | </li> |
| 334 | |
| 335 | <li class="give-export-option-fields give-export-option-form-fields"> |
| 336 | <ul class="give-export-option-form-fields-ul"> |
| 337 | |
| 338 | <li class="give-export-option-label give-export-option-Form-label"> |
| 339 | <span> |
| 340 | <?php _e( 'Donation Form Fields', 'give' ); ?> |
| 341 | </span> |
| 342 | </li> |
| 343 | |
| 344 | |
| 345 | <li class="give-export-option-start"> |
| 346 | <label for="give-export-donation-form-id"> |
| 347 | <input type="checkbox" checked |
| 348 | name="give_give_donations_export_option[form_id]" |
| 349 | id="give-export-donation-form-id"><?php _e( 'Donation Form ID', 'give' ); ?> |
| 350 | </label> |
| 351 | </li> |
| 352 | |
| 353 | <li> |
| 354 | <label for="give-export-donation-form-title"> |
| 355 | <input type="checkbox" checked |
| 356 | name="give_give_donations_export_option[form_title]" |
| 357 | id="give-export-donation-form-title"><?php _e( 'Donation Form Title', 'give' ); ?> |
| 358 | </label> |
| 359 | </li> |
| 360 | |
| 361 | <li> |
| 362 | <label for="give-export-donation-form-level-id"> |
| 363 | <input type="checkbox" checked |
| 364 | name="give_give_donations_export_option[form_level_id]" |
| 365 | id="give-export-donation-form-level-id"><?php _e( 'Donation Form Level ID', 'give' ); ?> |
| 366 | </label> |
| 367 | </li> |
| 368 | |
| 369 | <li> |
| 370 | <label for="give-export-donation-form-level-title"> |
| 371 | <input type="checkbox" checked |
| 372 | name="give_give_donations_export_option[form_level_title]" |
| 373 | id="give-export-donation-form-level-title"><?php _e( 'Donation Form Level Title', 'give' ); ?> |
| 374 | </label> |
| 375 | </li> |
| 376 | |
| 377 | <?php |
| 378 | /* |
| 379 | * Action to add extra columns in standard form fields |
| 380 | * |
| 381 | * @since 2.1 |
| 382 | */ |
| 383 | do_action( 'give_export_donation_standard_form_fields' ); |
| 384 | ?> |
| 385 | </ul> |
| 386 | </li> |
| 387 | |
| 388 | <li class="give-export-option-fields give-export-option-donor-fields"> |
| 389 | <ul class="give-export-option-donor-fields-ul"> |
| 390 | |
| 391 | <li class="give-export-option-label give-export-option-donor-label"> |
| 392 | <span> |
| 393 | <?php _e( 'Donor Fields', 'give' ); ?> |
| 394 | </span> |
| 395 | </li> |
| 396 | |
| 397 | <li class="give-export-option-start"> |
| 398 | <label for="give-export-first-name"> |
| 399 | <input type="checkbox" checked |
| 400 | name="give_give_donations_export_option[first_name]" |
| 401 | id="give-export-first-name"><?php _e( 'Donor\'s First Name', 'give' ); ?> |
| 402 | </label> |
| 403 | </li> |
| 404 | |
| 405 | <li> |
| 406 | <label for="give-export-last-name"> |
| 407 | <input type="checkbox" checked |
| 408 | name="give_give_donations_export_option[last_name]" |
| 409 | id="give-export-last-name"><?php _e( 'Donor\'s Last Name', 'give' ); ?> |
| 410 | </label> |
| 411 | </li> |
| 412 | |
| 413 | <li> |
| 414 | <label for="give-export-email"> |
| 415 | <input type="checkbox" checked |
| 416 | name="give_give_donations_export_option[email]" |
| 417 | id="give-export-email"><?php _e( 'Donor\'s Email', 'give' ); ?> |
| 418 | </label> |
| 419 | </li> |
| 420 | |
| 421 | <li> |
| 422 | <label for="give-export-company"> |
| 423 | <input type="checkbox" checked |
| 424 | name="give_give_donations_export_option[company]" |
| 425 | id="give-export-company"><?php _e( 'Company Name', 'give' ); ?> |
| 426 | </label> |
| 427 | </li> |
| 428 | |
| 429 | <li> |
| 430 | <label for="give-export-address"> |
| 431 | <input type="checkbox" checked |
| 432 | name="give_give_donations_export_option[address]" |
| 433 | id="give-export-address"><?php _e( 'Donor\'s Billing Address', 'give' ); ?> |
| 434 | </label> |
| 435 | </li> |
| 436 | |
| 437 | <li> |
| 438 | <label for="give-export-userid"> |
| 439 | <input type="checkbox" checked |
| 440 | name="give_give_donations_export_option[userid]" |
| 441 | id="give-export-userid"><?php _e( 'User ID', 'give' ); ?> |
| 442 | </label> |
| 443 | </li> |
| 444 | |
| 445 | <li> |
| 446 | <label for="give-export-donorid"> |
| 447 | <input type="checkbox" checked |
| 448 | name="give_give_donations_export_option[donorid]" |
| 449 | id="give-export-donorid"><?php _e( 'Donor ID', 'give' ); ?> |
| 450 | </label> |
| 451 | </li> |
| 452 | |
| 453 | <li> |
| 454 | <label for="give-export-donor-ip"> |
| 455 | <input type="checkbox" checked |
| 456 | name="give_give_donations_export_option[donor_ip]" |
| 457 | id="give-export-donor-ip"><?php _e( 'Donor IP Address', 'give' ); ?> |
| 458 | </label> |
| 459 | </li> |
| 460 | |
| 461 | <?php |
| 462 | /* |
| 463 | * Action to add extra columns in standard donor fields |
| 464 | * |
| 465 | * @since 2.1 |
| 466 | */ |
| 467 | do_action( 'give_export_donation_standard_donor_fields' ); |
| 468 | ?> |
| 469 | </ul> |
| 470 | </li> |
| 471 | |
| 472 | <?php |
| 473 | /** |
| 474 | * Action to add custom export column. |
| 475 | * |
| 476 | * @since 2.1.4 |
| 477 | */ |
| 478 | do_action( 'give_export_donation_add_custom_column' ); |
| 479 | ?> |
| 480 | </ul> |
| 481 | </div> |
| 482 | </td> |
| 483 | </tr> |
| 484 | <?php |
| 485 | } |
| 486 | |
| 487 | add_action( 'give_export_donation_fields', 'give_export_donation_standard_fields', 10 ); |
| 488 | |
| 489 | /** |
| 490 | * Add Donation Custom fields in export donation page |
| 491 | * |
| 492 | * @since 2.1 |
| 493 | */ |
| 494 | function give_export_donation_custom_fields() { |
| 495 | ?> |
| 496 | <tr |
| 497 | class="give-hidden give-export-donations-hide give-export-donations-standard-fields"> |
| 498 | <td scope="row" class="row-title"> |
| 499 | <label><?php _e( 'Custom Field Columns:', 'give' ); ?></label> |
| 500 | </td> |
| 501 | <td class="give-field-wrap"> |
| 502 | <div class="give-clearfix"> |
| 503 | <ul class="give-export-option-ul"></ul> |
| 504 | <p class="give-field-description"><?php _e( 'The following fields may have been created by custom code, or another plugin.', 'give' ); ?></p> |
| 505 | </div> |
| 506 | </td> |
| 507 | </tr> |
| 508 | <?php |
| 509 | } |
| 510 | |
| 511 | add_action( 'give_export_donation_fields', 'give_export_donation_custom_fields', 30 ); |
| 512 | |
| 513 | |
| 514 | /** |
| 515 | * Add Donation hidden fields in export donation page |
| 516 | * |
| 517 | * @since 2.1 |
| 518 | */ |
| 519 | function give_export_donation_hidden_fields() { |
| 520 | ?> |
| 521 | |
| 522 | <tr class="give-hidden give-export-donations-hide give-export-donations-hidden-fields"> |
| 523 | <td scope="row" class="row-title"> |
| 524 | <label><?php _e( 'Hidden Custom Field Columns:', 'give' ); ?></label> |
| 525 | </td> |
| 526 | <td class="give-field-wrap"> |
| 527 | <div class="give-clearfix"> |
| 528 | <ul class="give-export-option-ul"></ul> |
| 529 | <p class="give-field-description"><?php _e( 'The following hidden custom fields contain data created by Give Core, a Give Add-on, another plugin, etc.<br/>Hidden fields are generally used for programming logic, but you may contain data you would like to export.', 'give' ); ?></p> |
| 530 | </div> |
| 531 | </td> |
| 532 | </tr> |
| 533 | <?php |
| 534 | } |
| 535 | |
| 536 | add_action( 'give_export_donation_fields', 'give_export_donation_hidden_fields', 40 ); |
| 537 | |
| 538 |