Skip to content

Commit

Permalink
feature: add donor phone number exports (#7394)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaHungDinh authored May 30, 2024
1 parent 11e183e commit a3b8cd1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
4 changes: 3 additions & 1 deletion includes/admin/tools/export/export-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ function give_do_ajax_export() {
* Note: This function is for internal purposes only.
* Use filter "give_export_donors_get_default_columns" instead.
*
* @unreleased add donor_phone_number column.
* @since 2.2.6
*
* @return array
Expand All @@ -154,7 +155,8 @@ function give_export_donors_get_default_columns() {
'address' => __( 'Address', 'give' ),
'userid' => __( 'User ID', 'give' ),
'donor_created_date' => __( 'Donor Created Date', 'give' ),
'donations' => __( 'Number of donations', 'give' ),
'donor_phone_number' => __( 'Donor Phone Number', 'give' ),
'donations' => __( 'Number of donations', 'give' ),
'donation_sum' => __( 'Total Donated', 'give' ),
];

Expand Down
11 changes: 11 additions & 0 deletions includes/admin/tools/export/give-export-donations-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function csv_cols() {
/**
* CSV file columns.
*
* @unreleased add phone column.
* @since 2.1
*
* @param array $columns
Expand Down Expand Up @@ -199,6 +200,9 @@ private function get_cols( $columns ) {
$cols['address_zip'] = __( 'Zip', 'give' );
$cols['address_country'] = __( 'Country', 'give' );
break;
case 'phone':
$cols['phone'] = __( 'Donor Phone Number', 'give' );
break;
case 'comment':
$cols['comment'] = __( 'Donor Comment', 'give' );
break;
Expand Down Expand Up @@ -314,6 +318,7 @@ public function get_donation_argument( $args = array() ) {
*
* @access public
*
* @unreleased add donor phone.
* @since 2.1
*
* @global object $wpdb Used to query the database using the WordPress database API.
Expand Down Expand Up @@ -374,6 +379,10 @@ public function get_data() {
$data[ $i ]['address_country'] = isset( $address['country'] ) ? $address['country'] : '';
}

if ( ! empty( $columns['phone'] ) ) {
$data[ $i ]['phone'] = $payment_meta['_give_payment_donor_phone'];
}

if ( ! empty( $columns['comment'] ) ) {
$comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id );
$data[ $i ]['comment'] = ! empty( $comment ) ? $comment->comment_content : '';
Expand Down Expand Up @@ -674,13 +683,15 @@ public function print_csv_rows() {
* Escapes CSV cell data to protect against CSV injection.
* @link https://owasp.org/www-community/attacks/CSV_Injection
*
* @unreleased sanitize + prefix
* @since 2.25.2
*
* @param mixed|string $cellData
*
* @return mixed|string
*/
protected function escape_csv_cell_data($cellData) {
$cellData = str_replace('+', '', $cellData);
$firstCharacter = substr($cellData, 0, 1);
if( in_array($firstCharacter, array('=', '+', '-', '@')) ) {
$cellData = "'" . $cellData;
Expand Down
31 changes: 20 additions & 11 deletions includes/admin/tools/export/give-export-donations-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ function give_export_donations_get_custom_fields() {
$donation_list = implode( ',', (array) give_get_payments( $args ) );

$query_and = sprintf(
"AND $wpdb->posts.ID IN (%s)
AND $wpdb->donationmeta.meta_key != ''
"AND $wpdb->posts.ID IN (%s)
AND $wpdb->donationmeta.meta_key != ''
AND $wpdb->donationmeta.meta_key NOT RegExp '(^[_0-9].+$)'",
$donation_list
);

$query = "
SELECT DISTINCT($wpdb->donationmeta.meta_key)
FROM $wpdb->posts
LEFT JOIN $wpdb->donationmeta
SELECT DISTINCT($wpdb->donationmeta.meta_key)
FROM $wpdb->posts
LEFT JOIN $wpdb->donationmeta
ON $wpdb->posts.ID = {$wpdb->donationmeta}.{$donationmeta_table_key}
WHERE $wpdb->posts.post_type = '%s'
" . $query_and;
Expand All @@ -56,17 +56,17 @@ function give_export_donations_get_custom_fields() {
}

$query_and = sprintf(
"AND $wpdb->posts.ID IN (%s)
AND $wpdb->donationmeta.meta_key != ''
"AND $wpdb->posts.ID IN (%s)
AND $wpdb->donationmeta.meta_key != ''
AND $wpdb->donationmeta.meta_key NOT RegExp '^[^_]'",
$donation_list
);

$query = "
SELECT DISTINCT($wpdb->donationmeta.meta_key)
FROM $wpdb->posts
LEFT JOIN $wpdb->donationmeta
ON $wpdb->posts.ID = {$wpdb->donationmeta}.{$donationmeta_table_key}
SELECT DISTINCT($wpdb->donationmeta.meta_key)
FROM $wpdb->posts
LEFT JOIN $wpdb->donationmeta
ON $wpdb->posts.ID = {$wpdb->donationmeta}.{$donationmeta_table_key}
WHERE $wpdb->posts.post_type = '%s'
" . $query_and;

Expand Down Expand Up @@ -229,6 +229,7 @@ function give_export_donation_form_search_args( $args ) {
/**
* Add Donation standard fields in export donation page
*
* @unreleased add Donor Phone Number to donor fields.
* @since 2.1
*/
function give_export_donation_standard_fields() {
Expand Down Expand Up @@ -472,6 +473,14 @@ function give_export_donation_standard_fields() {
</label>
</li>

<li>
<label for="give-export-phone">
<input type="checkbox" checked
name="give_give_donations_export_option[phone]"
id="give-export-address"><?php _e( 'Donor\'s Phone Number', 'give' ); ?>
</label>
</li>

<li>
<label for="give-export-comment">
<input type="checkbox" checked
Expand Down
4 changes: 4 additions & 0 deletions src/Exports/DonorsExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function set_properties($request)
}

/**
* @unreleased Include donor phone.
* @since 2.29.0 Include donor created date
* @since 2.21.2
* @since 3.3.0 Filter donors by form ID
Expand All @@ -67,6 +68,7 @@ public function get_data(): array
['donors.email', 'email'],
['donors.user_id', 'userid'],
['donors.date_created', 'donor_created_date'],
['donors.phone', 'donor_phone_number'],
['donors.purchase_count', 'donations'],
['donors.purchase_value', 'donation_sum']
);
Expand Down Expand Up @@ -155,6 +157,7 @@ protected function filterExportData(array $exportData): array
}

/**
* @unreleased Include donor_phone_number col.
* @since 2.29.0 Include donor created col
* @since 2.21.2
*/
Expand All @@ -174,6 +177,7 @@ public function csv_cols(): array
],
'userid' => __('User ID', 'give'),
'donor_created_date' => __('Donor Created', 'give'),
'donor_phone_number' => __('Donor Phone Number', 'give'),
'donations' => __('Number of donations', 'give'),
'donation_sum' => __('Total Donated', 'give'),
], $this->postedData['give_export_columns'])
Expand Down

0 comments on commit a3b8cd1

Please sign in to comment.