Contributors: Reset profile hours when pledge deactivated.

See https://github.com/WordPress/five-for-the-future/issues/169
This commit is contained in:
Ian Dunn 2022-04-28 15:05:44 -07:00
parent 834c62c0d0
commit c6d7bbb7c1
No known key found for this signature in database
GPG key ID: 99B971B50343CBCB
6 changed files with 338 additions and 4 deletions

View file

@ -166,6 +166,22 @@ function add_pledge_contributors( $pledge_id, $contributors ) {
return $results;
}
/**
* Remove all of the contributors for the given pledge.
*
* Some contributors are sponsored by multiple companies. They'll have a `5ftf_contributor` post for each company,
* but only the post associated with the given pledge should be removed.
*/
function remove_pledge_contributors( int $pledge_id ) : void {
$contributors = get_pledge_contributors( $pledge_id, 'all' );
foreach ( $contributors as $status_group ) {
foreach ( $status_group as $contributor ) {
remove_contributor( $contributor->ID );
}
}
}
/**
* Remove a contributor post from a pledge.
*
@ -186,6 +202,21 @@ function remove_contributor( $contributor_post_id ) {
Email\send_contributor_removed_email( $pledge_id, $contributor );
}
$has_additional_sponsors = get_posts( array(
'post_type' => CPT_ID,
'title' => $contributor->post_title,
'post_status' => 'publish',
) );
// `pending` contributors never confirmed they were associated with the company, so their profile data isn't
// tied to the pledge, and shouldn't be reset. If a user has multiple sponsors, we don't know which hours are
// sponsored by which company, so just leave them all.
if ( 'publish' === $old_status && ! $has_additional_sponsors ) {
$user = get_user_by( 'login', $contributor->post_title );
XProfile\reset_contribution_data( $user->ID );
}
/**
* Action: Fires when a contributor is removed from a pledge.
*

View file

@ -128,13 +128,23 @@ function send_contributor_confirmation_emails( $pledge_id, $contributor_id = nul
function send_contributor_removed_email( $pledge_id, $contributor ) {
$pledge = get_post( $pledge_id );
$subject = "Removed from {$pledge->post_title} Five for the Future pledge";
$message = "Howdy {$contributor->post_title},\n\n";
$message .= sprintf(
'This email is to notify you that your WordPress.org contributor profile is no longer linked to %1$ss Five for the Future pledge. If this is unexpected news, its best to reach out directly to %1$s with questions. Have a great day!',
$user = get_user_by( 'login', $contributor->post_title );
$message = sprintf( '
Howdy %1$s,
This email is to notify you that your WordPress.org contributor profile is no longer linked to %2$ss Five for the Future pledge. If this is unexpected news, its best to reach out directly to %2$s with questions.
If they were the only sponsor linked to your account, then the "Hours Per Week" and "Contributor Teams" fields on your profile have been reset, so that teams have accurate data. If you still plan on contributing without sponsorship, please revisit your profile and enter your new hours and teams.
https://profiles.wordpress.org/me/profile/edit/group/5/
Have a great day!',
$contributor->post_title,
$pledge->post_title
);
$message = str_replace( "\t", '', trim( $message ) );
$user = get_user_by( 'login', $contributor->post_title );
send_email( $user->user_email, $subject, $message, $pledge_id );
}

View file

@ -387,6 +387,10 @@ function deactivate( $pledge_id, $notify = false, $reason = '' ) {
do_action( FiveForTheFuture\PREFIX . '_deactivated_pledge', $pledge_id, $notify, $reason, $result );
if ( ! is_wp_error( $result ) ) {
Contributor\remove_pledge_contributors( $pledge_id );
}
return $result;
}

View file

@ -170,3 +170,21 @@ function get_contributor_user_data( $user_id ) {
return $formatted_data;
}
/**
* Reset the 5ftF data on a user's profile.
*/
function reset_contribution_data( $user_id ) : void {
global $wpdb;
$wpdb->query( $wpdb->prepare( '
DELETE FROM `bpmain_bp_xprofile_data`
WHERE
user_id = %d AND
field_id IN ( %d, %d, %d )',
$user_id,
FIELD_IDS['sponsored'],
FIELD_IDS['hours_per_week'],
FIELD_IDS['team_names'],
) );
}