Move email-related code into the email file

This commit is contained in:
Kelly Dwan 2019-11-15 14:20:52 -05:00
parent e0e8fae44b
commit 69937c72f5
No known key found for this signature in database
GPG key ID: 8BA5575F3D11575D
4 changed files with 118 additions and 118 deletions

View file

@ -5,7 +5,9 @@
namespace WordPressDotOrg\FiveForTheFuture\Email; namespace WordPressDotOrg\FiveForTheFuture\Email;
use WordPressDotOrg\FiveForTheFuture\{ Auth, Contributor };
use const WordPressDotOrg\FiveForTheFuture\PREFIX; use const WordPressDotOrg\FiveForTheFuture\PREFIX;
use const WordPressDotOrg\FiveForTheFuture\PledgeMeta\META_PREFIX;
defined( 'WPINC' ) || die(); defined( 'WPINC' ) || die();
@ -42,3 +44,112 @@ function send_email( $to, $subject, $message, $pledge_id ) {
return $result; return $result;
} }
/**
* Email pledge manager to confirm their email address.
*
* @param int $pledge_id The ID of the pledge.
* @param int $action_page_id The ID of the page that the user will be taken back to, in order to process their
* confirmation request.
*
* @return bool
*/
function send_pledge_confirmation_email( $pledge_id, $action_page_id ) {
$pledge = get_post( $pledge_id );
$message = sprintf(
"Thanks for pledging your organization's time to contribute to the WordPress open source project! Please confirm this email address in order to publish your pledge:\n\n%s",
Auth\get_authentication_url( $pledge_id, 'confirm_pledge_email', $action_page_id )
);
return send_email(
$pledge->{'5ftf_org-pledge-email'},
'Please confirm your email address',
$message,
$pledge_id
);
}
/**
* Send contributors an email to confirm their participation.
*
* @param int $pledge_id
* @param int|null $contributor_id Optional. Send to a specific contributor instead of all.
*/
function send_contributor_confirmation_emails( $pledge_id, $contributor_id = null ) {
$pledge = get_post( $pledge_id );
$subject = "Confirm your {$pledge->post_title} sponsorship";
/*
* Only fetch unconfirmed ones, because we might be resending confirmation emails, and we shouldn't resend to
* confirmed contributors.
*/
$unconfirmed_contributors = Contributor\get_pledge_contributors( $pledge->ID, 'pending', $contributor_id );
foreach ( $unconfirmed_contributors as $contributor ) {
$user = get_user_by( 'login', $contributor->post_title );
/*
* Their first name is ideal, but their username is the best fallback because `nickname`, `display_name`,
* etc are too formal.
*/
$name = $user->first_name ? $user->first_name : '@' . $user->user_nicename;
/*
* This uses w.org login accounts instead of `Auth\get_authentication_url()`, because the reasons for using
* tokens for pledges don't apply to contributors, accounts are more secure, and they provide a better UX
* because there's no expiration.
*/
$message =
"Howdy $name, {$pledge->post_title} has created a Five for the Future pledge on WordPress.org and listed you as one of the contributors that they sponsor to contribute to the WordPress open source project. You can view their pledge at:\n\n" .
get_permalink( $pledge_id ) . "\n\n" .
"To confirm that they're sponsoring your contributions, please review your pledges at:\n\n" .
get_permalink( get_page_by_path( 'my-pledges' ) ) . "\n\n" .
"Please also update your WordPress.org profile to include the number of hours per week that you contribute, and the teams that you contribute to:\n\n" .
"https://profiles.wordpress.org/me/profile/edit/group/5/\n\n" .
"If {$pledge->post_title} isn't sponsoring your contributions, then you can ignore this email, and you won't be listed on their pledge.";
$user = get_user_by( 'login', $contributor->post_title );
send_email( $user->user_email, $subject, $message, $pledge_id );
}
}
/**
* Email the pledge admin a temporary link they can use to manage their pledge.
*
* @param int $pledge_id
*
* @return true|WP_Error
*/
function send_manage_pledge_link( $pledge_id ) {
$admin_email = get_post( $pledge_id )->{ META_PREFIX . 'org-pledge-email' };
if ( ! is_email( $admin_email ) ) {
return new WP_Error( 'invalid_email', 'Invalid email address.' );
}
$subject = __( 'Updating your Pledge', 'wporg-5ftf' );
$message =
'Howdy, please open this link to update your pledge:' . "\n\n" .
Auth\get_authentication_url(
$pledge_id,
'manage_pledge',
get_page_by_path( 'manage-pledge' )->ID,
// The token needs to be reused so that the admin can view the form, submit it, and view the result.
false
);
$result = send_email( $admin_email, $subject, $message, $pledge_id );
if ( ! $result ) {
$result = new WP_Error( 'email_failed', 'Email failed to send' );
}
return $result;
}

View file

@ -49,7 +49,7 @@ function render_form_new() {
$pledge_id = filter_input( INPUT_GET, 'pledge_id', FILTER_VALIDATE_INT ); $pledge_id = filter_input( INPUT_GET, 'pledge_id', FILTER_VALIDATE_INT );
$complete = true; $complete = true;
Pledge\send_pledge_confirmation_email( $pledge_id, get_post()->ID ); Email\send_pledge_confirmation_email( $pledge_id, get_post()->ID );
} }
ob_start(); ob_start();
@ -138,62 +138,12 @@ function process_pledge_confirmation_email( $pledge_id, $action, $unverified_tok
'ID' => $pledge_id, 'ID' => $pledge_id,
'post_status' => 'publish', 'post_status' => 'publish',
) ); ) );
send_contributor_confirmation_emails( $pledge_id ); Email\send_contributor_confirmation_emails( $pledge_id );
} }
return $email_confirmed; return $email_confirmed;
} }
/**
* Send contributors an email to confirm their participation.
*
* @param int $pledge_id
* @param int|null $contributor_id Optional. Send to a specific contributor instead of all.
*/
function send_contributor_confirmation_emails( $pledge_id, $contributor_id = null ) {
$pledge = get_post( $pledge_id );
$subject = "Confirm your {$pledge->post_title} sponsorship";
/*
* Only fetch unconfirmed ones, because we might be resending confirmation emails, and we shouldn't resend to
* confirmed contributors.
*/
$unconfirmed_contributors = Contributor\get_pledge_contributors( $pledge->ID, 'pending', $contributor_id );
foreach ( $unconfirmed_contributors as $contributor ) {
$user = get_user_by( 'login', $contributor->post_title );
/*
* Their first name is ideal, but their username is the best fallback because `nickname`, `display_name`,
* etc are too formal.
*/
$name = $user->first_name ? $user->first_name : '@' . $user->user_nicename;
/*
* This uses w.org login accounts instead of `Auth\get_authentication_url()`, because the reasons for using
* tokens for pledges don't apply to contributors, accounts are more secure, and they provide a better UX
* because there's no expiration.
*/
$message =
"Howdy $name, {$pledge->post_title} has created a Five for the Future pledge on WordPress.org and listed you as one of the contributors that they sponsor to contribute to the WordPress open source project. You can view their pledge at:\n\n" .
get_permalink( $pledge_id ) . "\n\n" .
"To confirm that they're sponsoring your contributions, please review your pledges at:\n\n" .
get_permalink( get_page_by_path( 'my-pledges' ) ) . "\n\n" .
"Please also update your WordPress.org profile to include the number of hours per week that you contribute, and the teams that you contribute to:\n\n" .
"https://profiles.wordpress.org/me/profile/edit/group/5/\n\n" .
"If {$pledge->post_title} isn't sponsoring your contributions, then you can ignore this email, and you won't be listed on their pledge.";
$user = get_user_by( 'login', $contributor->post_title );
Email\send_email( $user->user_email, $subject, $message, $pledge_id );
}
}
/** /**
* Render the form(s) for managing existing pledges. * Render the form(s) for managing existing pledges.
* *
@ -262,7 +212,7 @@ function process_manage_link_request() {
if ( $valid_admin_email && $valid_admin_email === $unverified_admin_email ) { if ( $valid_admin_email && $valid_admin_email === $unverified_admin_email ) {
$verified_pledge_id = $unverified_pledge_id; // The addresses will only match is the pledge ID is valid. $verified_pledge_id = $unverified_pledge_id; // The addresses will only match is the pledge ID is valid.
$message_sent = send_manage_pledge_link( $verified_pledge_id ); $message_sent = Email\send_manage_pledge_link( $verified_pledge_id );
if ( $message_sent ) { if ( $message_sent ) {
$result = __( "Thanks! We've emailed you a link you can open in order to update your pledge.", 'wporg-5ftf' ); $result = __( "Thanks! We've emailed you a link you can open in order to update your pledge.", 'wporg-5ftf' );
@ -281,42 +231,6 @@ function process_manage_link_request() {
return $result; return $result;
} }
/**
* Email the pledge admin a temporary link they can use to manage their pledge.
*
* @param int $pledge_id
*
* @return true|WP_Error
*/
function send_manage_pledge_link( $pledge_id ) {
$admin_email = get_post( $pledge_id )->{ PledgeMeta\META_PREFIX . 'org-pledge-email' };
if ( ! is_email( $admin_email ) ) {
return new WP_Error( 'invalid_email', 'Invalid email address.' );
}
$subject = __( 'Updating your Pledge', 'wporg-5ftf' );
$message =
'Howdy, please open this link to update your pledge:' . "\n\n" .
Auth\get_authentication_url(
$pledge_id,
'manage_pledge',
get_page_by_path( 'manage-pledge' )->ID,
// The token needs to be reused so that the admin can view the form, submit it, and view the result.
false
);
$result = Email\send_email( $admin_email, $subject, $message, $pledge_id );
if ( ! $result ) {
$result = new WP_Error( 'email_failed', 'Email failed to send' );
}
return $result;
}
/** /**
* Process a submission from the Manage Existing Pledge form. * Process a submission from the Manage Existing Pledge form.
* *

View file

@ -6,7 +6,7 @@
namespace WordPressDotOrg\FiveForTheFuture\PledgeMeta; namespace WordPressDotOrg\FiveForTheFuture\PledgeMeta;
use WordPressDotOrg\FiveForTheFuture; use WordPressDotOrg\FiveForTheFuture;
use WordPressDotOrg\FiveForTheFuture\{ Pledge, PledgeForm, Contributor, XProfile }; use WordPressDotOrg\FiveForTheFuture\{ Contributor, Email, Pledge, PledgeForm, XProfile };
use WP_Post, WP_Error; use WP_Post, WP_Error;
defined( 'WPINC' ) || die(); defined( 'WPINC' ) || die();
@ -274,14 +274,14 @@ function save_pledge( $pledge_id, $pledge ) {
save_pledge_meta( $pledge_id, $submitted_meta ); save_pledge_meta( $pledge_id, $submitted_meta );
if ( filter_input( INPUT_POST, 'resend-pledge-confirmation' ) ) { if ( filter_input( INPUT_POST, 'resend-pledge-confirmation' ) ) {
Pledge\send_pledge_confirmation_email( Email\send_pledge_confirmation_email(
filter_input( INPUT_GET, 'resend-pledge-id', FILTER_VALIDATE_INT ), filter_input( INPUT_GET, 'resend-pledge-id', FILTER_VALIDATE_INT ),
get_page_by_path( 'for-organizations' )->ID get_page_by_path( 'for-organizations' )->ID
); );
} }
if ( filter_input( INPUT_POST, 'resend-contributor-confirmation' ) ) { if ( filter_input( INPUT_POST, 'resend-contributor-confirmation' ) ) {
PledgeForm\send_contributor_confirmation_emails( Email\send_contributor_confirmation_emails(
$pledge_id, $pledge_id,
filter_input( INPUT_GET, 'resend-contributor-id', FILTER_VALIDATE_INT ) filter_input( INPUT_GET, 'resend-contributor-id', FILTER_VALIDATE_INT )
); );

View file

@ -189,37 +189,12 @@ function create_new_pledge( $name ) {
// The pledge's meta data is saved at this point via `save_pledge_meta()`, which is a `save_post` callback. // The pledge's meta data is saved at this point via `save_pledge_meta()`, which is a `save_post` callback.
if ( ! is_wp_error( $pledge_id ) ) { if ( ! is_wp_error( $pledge_id ) ) {
send_pledge_confirmation_email( $pledge_id, get_post()->ID ); Email\send_pledge_confirmation_email( $pledge_id, get_post()->ID );
} }
return $pledge_id; return $pledge_id;
} }
/**
* Email pledge manager to confirm their email address.
*
* @param int $pledge_id The ID of the pledge.
* @param int $action_page_id The ID of the page that the user will be taken back to, in order to process their
* confirmation request.
*
* @return bool
*/
function send_pledge_confirmation_email( $pledge_id, $action_page_id ) {
$pledge = get_post( $pledge_id );
$message = sprintf(
"Thanks for pledging your organization's time to contribute to the WordPress open source project! Please confirm this email address in order to publish your pledge:\n\n%s",
Auth\get_authentication_url( $pledge_id, 'confirm_pledge_email', $action_page_id )
);
return Email\send_email(
$pledge->{'5ftf_org-pledge-email'},
'Please confirm your email address',
$message,
$pledge_id
);
}
/** /**
* Filter query for archive & search pages to ensure we're only showing the expected data. * Filter query for archive & search pages to ensure we're only showing the expected data.
* *