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
534 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['tax_query']['relation'] = 'AND'; |
| 194 | } |
| 195 | |
| 196 | if ( ! empty( $give_forms_categories ) ) { |
| 197 | $args['tax_query'][] = array( |
| 198 | 'taxonomy' => 'give_forms_category', |
| 199 | 'field' => 'term_id', |
| 200 | 'terms' => $give_forms_categories, |
| 201 | 'operator' => 'AND', |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | if ( ! empty( $give_forms_tags ) ) { |
| 206 | $args['tax_query'][] = array( |
| 207 | 'taxonomy' => 'give_forms_tag', |
| 208 | 'field' => 'term_id', |
| 209 | 'terms' => $give_forms_tags, |
| 210 | 'operator' => 'AND', |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | return $args; |
| 215 | } |
| 216 | |
| 217 | add_filter( 'give_ajax_form_search_args', 'give_export_donation_form_search_args' ); |
| 218 | |
| 219 | /** |
| 220 | * Add Donation standard fields in export donation page |
| 221 | * |
| 222 | * @since 2.1 |
| 223 | */ |
| 224 | function give_export_donation_standard_fields() { |
| 225 | ?> |
| 226 | <tr> |
| 227 | <td scope="row" class="row-title"> |
| 228 | <label><?php _e( 'Standard Columns:', 'give' ); ?></label> |
| 229 | </td> |
| 230 | <td> |
| 231 | <div class="give-clearfix"> |
| 232 | <ul class="give-export-option"> |
| 233 | <li class="give-export-option-fields give-export-option-payment-fields"> |
| 234 | <ul class="give-export-option-payment-fields-ul"> |
| 235 | |
| 236 | <li class="give-export-option-label give-export-option-donation-label"> |
| 237 | <span> |
| 238 | <?php _e( 'Donation Payment Fields', 'give' ); ?> |
| 239 | </span> |
| 240 | </li> |
| 241 | |
| 242 | <li class="give-export-option-start"> |
| 243 | <label for="give-export-donation-id"> |
| 244 | <input type="checkbox" checked |
| 245 | name="give_give_donations_export_option[donation_id]" |
| 246 | id="give-export-donation-id"><?php _e( 'Donation ID', 'give' ); ?> |
| 247 | </label> |
| 248 | </li> |
| 249 | |
| 250 | <?php |
| 251 | if ( give_is_setting_enabled( give_get_option( 'sequential-ordering_status', 'disabled' ) ) ) { |
| 252 | ?> |
| 253 | <li> |
| 254 | <label for="give-export-seq-id"> |
| 255 | <input type="checkbox" checked |
| 256 | name="give_give_donations_export_option[seq_id]" |
| 257 | id="give-export-seq-id"><?php _e( 'Donation Number', 'give' ); ?> |
| 258 | </label> |
| 259 | </li> |
| 260 | <?php |
| 261 | } |
| 262 | ?> |
| 263 | |
| 264 | <li> |
| 265 | <label for="give-export-donation-sum"> |
| 266 | <input type="checkbox" checked |
| 267 | name="give_give_donations_export_option[donation_total]" |
| 268 | id="give-export-donation-sum"><?php _e( 'Donation Total', 'give' ); ?> |
| 269 | </label> |
| 270 | </li> |
| 271 | |
| 272 | <li> |
| 273 | <label for="give-export-donation-currency_code"> |
| 274 | <input type="checkbox" checked |
| 275 | name="give_give_donations_export_option[currency_code]" |
| 276 | id="give-export-donation-currency_code"><?php _e( 'Currency Code', 'give' ); ?> |
| 277 | </label> |
| 278 | </li> |
| 279 | |
| 280 | <li> |
| 281 | <label for="give-export-donation-currency_symbol"> |
| 282 | <input type="checkbox" checked |
| 283 | name="give_give_donations_export_option[currency_symbol]" |
| 284 | id="give-export-donation-currency_symbol"><?php _e( 'Currency Symbol', 'give' ); ?> |
| 285 | </label> |
| 286 | </li> |
| 287 | |
| 288 | <li> |
| 289 | <label for="give-export-donation-status"> |
| 290 | <input type="checkbox" checked |
| 291 | name="give_give_donations_export_option[donation_status]" |
| 292 | id="give-export-donation-status"><?php _e( 'Donation Status', 'give' ); ?> |
| 293 | </label> |
| 294 | </li> |
| 295 | |
| 296 | <li> |
| 297 | <label for="give-export-donation-date"> |
| 298 | <input type="checkbox" checked |
| 299 | name="give_give_donations_export_option[donation_date]" |
| 300 | id="give-export-donation-date"><?php _e( 'Donation Date', 'give' ); ?> |
| 301 | </label> |
| 302 | </li> |
| 303 | |
| 304 | <li> |
| 305 | <label for="give-export-donation-time"> |
| 306 | <input type="checkbox" checked |
| 307 | name="give_give_donations_export_option[donation_time]" |
| 308 | id="give-export-donation-time"><?php _e( 'Donation Time', 'give' ); ?> |
| 309 | </label> |
| 310 | </li> |
| 311 | |
| 312 | <li> |
| 313 | <label for="give-export-payment-gateway"> |
| 314 | <input type="checkbox" checked |
| 315 | name="give_give_donations_export_option[payment_gateway]" |
| 316 | id="give-export-payment-gateway"><?php _e( 'Payment Gateway', 'give' ); ?> |
| 317 | </label> |
| 318 | </li> |
| 319 | |
| 320 | <?php |
| 321 | /* |
| 322 | * Action to add extra columns in standard payment fields |
| 323 | * |
| 324 | * @since 2.1 |
| 325 | */ |
| 326 | do_action( 'give_export_donation_standard_payment_fields' ); |
| 327 | ?> |
| 328 | </ul> |
| 329 | </li> |
| 330 | |
| 331 | <li class="give-export-option-fields give-export-option-form-fields"> |
| 332 | <ul class="give-export-option-form-fields-ul"> |
| 333 | |
| 334 | <li class="give-export-option-label give-export-option-Form-label"> |
| 335 | <span> |
| 336 | <?php _e( 'Donation Form Fields', 'give' ); ?> |
| 337 | </span> |
| 338 | </li> |
| 339 | |
| 340 | |
| 341 | <li class="give-export-option-start"> |
| 342 | <label for="give-export-donation-form-id"> |
| 343 | <input type="checkbox" checked |
| 344 | name="give_give_donations_export_option[form_id]" |
| 345 | id="give-export-donation-form-id"><?php _e( 'Donation Form ID', 'give' ); ?> |
| 346 | </label> |
| 347 | </li> |
| 348 | |
| 349 | <li> |
| 350 | <label for="give-export-donation-form-title"> |
| 351 | <input type="checkbox" checked |
| 352 | name="give_give_donations_export_option[form_title]" |
| 353 | id="give-export-donation-form-title"><?php _e( 'Donation Form Title', 'give' ); ?> |
| 354 | </label> |
| 355 | </li> |
| 356 | |
| 357 | <li> |
| 358 | <label for="give-export-donation-form-level-id"> |
| 359 | <input type="checkbox" checked |
| 360 | name="give_give_donations_export_option[form_level_id]" |
| 361 | id="give-export-donation-form-level-id"><?php _e( 'Donation Form Level ID', 'give' ); ?> |
| 362 | </label> |
| 363 | </li> |
| 364 | |
| 365 | <li> |
| 366 | <label for="give-export-donation-form-level-title"> |
| 367 | <input type="checkbox" checked |
| 368 | name="give_give_donations_export_option[form_level_title]" |
| 369 | id="give-export-donation-form-level-title"><?php _e( 'Donation Form Level Title', 'give' ); ?> |
| 370 | </label> |
| 371 | </li> |
| 372 | |
| 373 | <?php |
| 374 | /* |
| 375 | * Action to add extra columns in standard form fields |
| 376 | * |
| 377 | * @since 2.1 |
| 378 | */ |
| 379 | do_action( 'give_export_donation_standard_form_fields' ); |
| 380 | ?> |
| 381 | </ul> |
| 382 | </li> |
| 383 | |
| 384 | <li class="give-export-option-fields give-export-option-donor-fields"> |
| 385 | <ul class="give-export-option-donor-fields-ul"> |
| 386 | |
| 387 | <li class="give-export-option-label give-export-option-donor-label"> |
| 388 | <span> |
| 389 | <?php _e( 'Donor Fields', 'give' ); ?> |
| 390 | </span> |
| 391 | </li> |
| 392 | |
| 393 | <li class="give-export-option-start"> |
| 394 | <label for="give-export-first-name"> |
| 395 | <input type="checkbox" checked |
| 396 | name="give_give_donations_export_option[first_name]" |
| 397 | id="give-export-first-name"><?php _e( 'Donor\'s First Name', 'give' ); ?> |
| 398 | </label> |
| 399 | </li> |
| 400 | |
| 401 | <li> |
| 402 | <label for="give-export-last-name"> |
| 403 | <input type="checkbox" checked |
| 404 | name="give_give_donations_export_option[last_name]" |
| 405 | id="give-export-last-name"><?php _e( 'Donor\'s Last Name', 'give' ); ?> |
| 406 | </label> |
| 407 | </li> |
| 408 | |
| 409 | <li> |
| 410 | <label for="give-export-email"> |
| 411 | <input type="checkbox" checked |
| 412 | name="give_give_donations_export_option[email]" |
| 413 | id="give-export-email"><?php _e( 'Donor\'s Email', 'give' ); ?> |
| 414 | </label> |
| 415 | </li> |
| 416 | |
| 417 | <li> |
| 418 | <label for="give-export-company"> |
| 419 | <input type="checkbox" checked |
| 420 | name="give_give_donations_export_option[company]" |
| 421 | id="give-export-company"><?php _e( 'Company Name', 'give' ); ?> |
| 422 | </label> |
| 423 | </li> |
| 424 | |
| 425 | <li> |
| 426 | <label for="give-export-address"> |
| 427 | <input type="checkbox" checked |
| 428 | name="give_give_donations_export_option[address]" |
| 429 | id="give-export-address"><?php _e( 'Donor\'s Billing Address', 'give' ); ?> |
| 430 | </label> |
| 431 | </li> |
| 432 | |
| 433 | <li> |
| 434 | <label for="give-export-userid"> |
| 435 | <input type="checkbox" checked |
| 436 | name="give_give_donations_export_option[userid]" |
| 437 | id="give-export-userid"><?php _e( 'User ID', 'give' ); ?> |
| 438 | </label> |
| 439 | </li> |
| 440 | |
| 441 | <li> |
| 442 | <label for="give-export-donorid"> |
| 443 | <input type="checkbox" checked |
| 444 | name="give_give_donations_export_option[donorid]" |
| 445 | id="give-export-donorid"><?php _e( 'Donor ID', 'give' ); ?> |
| 446 | </label> |
| 447 | </li> |
| 448 | |
| 449 | <li> |
| 450 | <label for="give-export-donor-ip"> |
| 451 | <input type="checkbox" checked |
| 452 | name="give_give_donations_export_option[donor_ip]" |
| 453 | id="give-export-donor-ip"><?php _e( 'Donor IP Address', 'give' ); ?> |
| 454 | </label> |
| 455 | </li> |
| 456 | |
| 457 | <?php |
| 458 | /* |
| 459 | * Action to add extra columns in standard donor fields |
| 460 | * |
| 461 | * @since 2.1 |
| 462 | */ |
| 463 | do_action( 'give_export_donation_standard_donor_fields' ); |
| 464 | ?> |
| 465 | </ul> |
| 466 | </li> |
| 467 | |
| 468 | <?php |
| 469 | /** |
| 470 | * Action to add custom export column. |
| 471 | * |
| 472 | * @since 2.1.4 |
| 473 | */ |
| 474 | do_action( 'give_export_donation_add_custom_column' ); |
| 475 | ?> |
| 476 | </ul> |
| 477 | </div> |
| 478 | </td> |
| 479 | </tr> |
| 480 | <?php |
| 481 | } |
| 482 | |
| 483 | add_action( 'give_export_donation_fields', 'give_export_donation_standard_fields', 10 ); |
| 484 | |
| 485 | /** |
| 486 | * Add Donation Custom fields in export donation page |
| 487 | * |
| 488 | * @since 2.1 |
| 489 | */ |
| 490 | function give_export_donation_custom_fields() { |
| 491 | ?> |
| 492 | <tr |
| 493 | class="give-hidden give-export-donations-hide give-export-donations-standard-fields"> |
| 494 | <td scope="row" class="row-title"> |
| 495 | <label><?php _e( 'Custom Field Columns:', 'give' ); ?></label> |
| 496 | </td> |
| 497 | <td class="give-field-wrap"> |
| 498 | <div class="give-clearfix"> |
| 499 | <ul class="give-export-option-ul"></ul> |
| 500 | <p class="give-field-description"><?php _e( 'The following fields may have been created by custom code, or another plugin.', 'give' ); ?></p> |
| 501 | </div> |
| 502 | </td> |
| 503 | </tr> |
| 504 | <?php |
| 505 | } |
| 506 | |
| 507 | add_action( 'give_export_donation_fields', 'give_export_donation_custom_fields', 30 ); |
| 508 | |
| 509 | |
| 510 | /** |
| 511 | * Add Donation hidden fields in export donation page |
| 512 | * |
| 513 | * @since 2.1 |
| 514 | */ |
| 515 | function give_export_donation_hidden_fields() { |
| 516 | ?> |
| 517 | |
| 518 | <tr class="give-hidden give-export-donations-hide give-export-donations-hidden-fields"> |
| 519 | <td scope="row" class="row-title"> |
| 520 | <label><?php _e( 'Hidden Custom Field Columns:', 'give' ); ?></label> |
| 521 | </td> |
| 522 | <td class="give-field-wrap"> |
| 523 | <div class="give-clearfix"> |
| 524 | <ul class="give-export-option-ul"></ul> |
| 525 | <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> |
| 526 | </div> |
| 527 | </td> |
| 528 | </tr> |
| 529 | <?php |
| 530 | } |
| 531 | |
| 532 | add_action( 'give_export_donation_fields', 'give_export_donation_hidden_fields', 40 ); |
| 533 | |
| 534 |