Skip to content

Commit

Permalink
Merge pull request #4115 from impress-org/issue/4113
Browse files Browse the repository at this point in the history
fix: improve logic for donor and user profile disconnection #4113
  • Loading branch information
ravinderk committed May 22, 2019
2 parents b3d09b4 + c017772 commit aa3e97a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
9 changes: 9 additions & 0 deletions includes/admin/admin-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@ function _give_register_admin_notices() {
) );
break;

case 'disconnect-user' :
Give()->notices->register_notice( array(
'id' => 'give-disconnect-user',
'type' => 'updated',
'description' => __( 'User has been successfully disconnected from donor.', 'give' ),
'show' => true,
) );
break;

case 'profile-updated' :
Give()->notices->register_notice( array(
'id' => 'give-profile-updated',
Expand Down
17 changes: 14 additions & 3 deletions includes/admin/donors/donor-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,32 @@ function give_disconnect_donor_user_id( $args ) {
'user_id' => 0,
);

$redirect_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' ) . $donor_id;
$is_donor_updated = $donor->update( $donor_args );

if ( $is_donor_updated ) {

$output['success'] = true;
if ( ! $donor->update( $donor_args ) ) {
update_user_meta( $user_id, '_give_is_donor_disconnected', true );
update_user_meta( $user_id, '_give_disconnected_donor_id', $donor->id );
$donor->update_meta( '_give_disconnected_user_id', $user_id );

// Delete the cache if any exists.
Give_Cache::delete( "give_cache_is_user_donor_disconnection_email_sent_{$user_id}" );

$redirect_url = add_query_arg(
'give-messages[]',
'disconnect-user',
$redirect_url
);

$output['success'] = true;

} else {
$output['success'] = false;
give_set_error( 'give-disconnect-user-fail', __( 'Failed to disconnect user from donor.', 'give' ) );
}

$output['redirect'] = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' ) . $donor_id;
$output['redirect'] = $redirect_url;

/**
* Fires after disconnecting user ID from a donor.
Expand Down
14 changes: 11 additions & 3 deletions includes/admin/donors/donor-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,17 @@ function give_connect_user_donor_profile( $donor, $donor_data, $address ) {
if ( $donor->update( $donor_data ) ) {

// Create and Update Donor First Name and Last Name in Meta Fields.
$donor->update_meta( '_give_donor_first_name', $donor_data['first_name'] );
$donor->update_meta( '_give_donor_last_name', $donor_data['last_name'] );
$donor->update_meta( '_give_donor_title_prefix', $donor_data['title'] );
if ( ! empty( $donor_data['first_name'] ) ) {
$donor->update_meta( '_give_donor_first_name', $donor_data['first_name'] );
}

if ( ! empty( $donor_data['last_name'] ) ) {
$donor->update_meta( '_give_donor_last_name', $donor_data['last_name'] );
}

if ( ! empty( $donor_data['title'] ) ) {
$donor->update_meta( '_give_donor_title_prefix', $donor_data['title'] );
}

// Fetch disconnected user id, if exists.
$disconnected_user_id = $donor->get_meta( '_give_disconnected_user_id', true );
Expand Down
10 changes: 6 additions & 4 deletions includes/emails/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ function give_get_email_names( $user_info, $payment = false ) {
* @param int $donor_id Donor ID.
*
* @since 1.8.14
*
* @return bool
*/
function give_admin_email_user_donor_disconnection( $user_id, $donor_id ) {

Expand All @@ -218,20 +220,20 @@ function give_admin_email_user_donor_disconnection( $user_id, $donor_id ) {

// Bail Out, if user id doesn't exists.
if ( empty( $user_id ) ) {
return;
return false;
}

// Bail Out, if donor id doesn't exists.
if ( empty( $donor_id ) ) {
return;
return false;
}

$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );

$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );

/* translators: %s: payment id */
$subject = __( 'Attention: User tries to login whose Donor profile is disconnected!', 'give' );
$subject = __( 'Attention: User tried to login whose Donor profile is disconnected!', 'give' );

/**
* Filters the Donor-User Disconnection notification subject.
Expand Down Expand Up @@ -268,6 +270,6 @@ function give_admin_email_user_donor_disconnection( $user_id, $donor_id ) {
$emails->__set( 'headers', $headers );
$emails->__set( 'heading', __( 'User - Donor Profile Disconnection', 'give' ) );

$emails->send( give_get_admin_notice_emails(), $subject, give_do_email_tags( $message ) );
return $emails->send( give_get_admin_notice_emails(), $subject, give_do_email_tags( $message, array() ) );

}
32 changes: 24 additions & 8 deletions includes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ function give_donation_history( $atts, $content = false ) {

// Set Donation History Shortcode Arguments in session variable.
Give()->session->set( 'give_donation_history_args', $donation_history_args );

$get_data = give_clean( filter_input_array( INPUT_GET ) );

// If payment_key query arg exists, return receipt instead of donation history.
if (
! empty( $get_data['donation_id'] ) ||
Expand Down Expand Up @@ -267,14 +267,14 @@ function give_receipt_shortcode( $atts ) {
'company_name' => false,
'status_notice' => true,
), $atts, 'give_receipt' );

ob_start();

$donation_id = false;
$receipt_type = false;
$get_data = give_clean( filter_input_array( INPUT_GET ) );
$session = give_get_purchase_session();

if ( ! empty( $get_data['donation_id'] ) ) {
$donation_id = $get_data['donation_id'];
} else if ( ! empty( $get_data['action'] ) && 'view_in_browser' === $get_data['action'] ) {
Expand All @@ -285,7 +285,7 @@ function give_receipt_shortcode( $atts ) {
} else if ( ! empty( $give_receipt_args['id'] ) ) {
$donation_id = $give_receipt_args['id'];
}

// Display donation receipt placeholder while loading receipt via AJAX.
if ( ! wp_doing_ajax() ) {
give_get_template_part( 'receipt/placeholder' );
Expand Down Expand Up @@ -325,11 +325,27 @@ function give_profile_editor_shortcode( $atts ) {

ob_start();

$user_id = get_current_user_id();
$donor_id = get_user_meta( $user_id, '_give_disconnected_donor_id', true ); // This is disconnected donor id.
$is_email_sent = Give_Cache::get( "give_cache_is_user_donor_disconnection_email_sent_{$user_id}" );

// Restrict access to donor profile, if donor and user are disconnected.
$is_donor_disconnected = get_user_meta( get_current_user_id(), '_give_is_donor_disconnected', true );
$is_donor_disconnected = get_user_meta( $user_id, '_give_is_donor_disconnected', true );

if ( is_user_logged_in() && $is_donor_disconnected ) {

// Display notice that Donor and User profiles are disconnected.
Give()->notices->print_frontend_notice( __( 'Your Donor and User profile are no longer connected. Please contact the site administrator.', 'give' ), true, 'error' );

// Send email to admin to notify about the disconnection, if the user is active and login to user account.
if ( ! $is_email_sent ) {

// Send Email.
give_admin_email_user_donor_disconnection( $user_id, $donor_id );

// Set Cache with expiration of 24 hours to ensure that email are not sent on every refresh.
Give_Cache::set( "give_cache_is_user_donor_disconnection_email_sent_{$user_id}", true, strtotime( '+1 day', current_time( 'timestamp', 1 ) ) );
}
return false;
}

Expand Down Expand Up @@ -836,7 +852,7 @@ function give_form_grid_shortcode( $atts ) {
);
$form_args['tax_query'][] = $tax_query;
}

/**
* Filter to modify WP Query for Total Goal.
*
Expand Down

0 comments on commit aa3e97a

Please sign in to comment.