2019-10-25 22:07:09 +03:00
< ? php
/**
2019-11-20 17:40:45 +02:00
* Helper functions for sending emails .
2019-10-25 22:07:09 +03:00
*/
namespace WordPressDotOrg\FiveForTheFuture\Email ;
2019-11-20 17:40:45 +02:00
use WordPressDotOrg\FiveForTheFuture\ { Auth , Contributor };
use const WordPressDotOrg\FiveForTheFuture\PREFIX ;
use const WordPressDotOrg\FiveForTheFuture\PledgeMeta\META_PREFIX ;
2019-12-03 22:09:35 +02:00
use WP_Error ;
2019-10-25 22:07:09 +03:00
2019-11-20 17:40:45 +02:00
defined ( 'WPINC' ) || die ();
2019-11-07 21:53:39 +02:00
2019-10-25 22:07:09 +03:00
/**
* Wrap `wp_mail()` with shared functionality .
*
* @ param string $to
* @ param string $subject
* @ param string $message
2019-11-01 03:39:57 +02:00
* @ param int $pledge_id
2019-10-25 22:07:09 +03:00
*
* @ return bool
*/
2019-11-01 03:39:57 +02:00
function send_email ( $to , $subject , $message , $pledge_id ) {
2019-10-25 22:07:09 +03:00
$headers = array (
'From: WordPress - Five for the Future <donotreply@wordpress.org>' ,
'Reply-To: support@wordcamp.org' ,
);
2019-11-01 03:39:57 +02:00
$result = wp_mail ( $to , $subject , $message , $headers );
/**
* Broadcast the results of an attempt to send an email .
*
* @ param string $to
* @ param string $subject
* @ param string $message
* @ param array $headers
* @ param bool $result
* @ param int $pledge_id
*/
2019-11-20 17:40:45 +02:00
do_action ( PREFIX . '_email_result' , $to , $subject , $message , $headers , $result , $pledge_id );
2019-11-01 03:39:57 +02:00
return $result ;
2019-10-25 22:07:09 +03:00
}
/**
2019-11-20 17:40:45 +02:00
* Email pledge manager to confirm their email address .
2019-10-25 22:07:09 +03:00
*
2019-11-20 17:40:45 +02:00
* @ 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 .
2019-10-25 22:07:09 +03:00
*
2019-11-20 17:40:45 +02:00
* @ return bool
2019-10-25 22:07:09 +03:00
*/
2019-11-20 17:40:45 +02:00
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
2019-10-25 22:07:09 +03:00
);
2019-11-20 17:40:45 +02:00
}
/**
* 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 " ;
2019-10-25 22:07:09 +03:00
/*
2019-11-20 17:40:45 +02:00
* Only fetch unconfirmed ones , because we might be resending confirmation emails , and we shouldn ' t resend to
* confirmed contributors .
2019-10-25 22:07:09 +03:00
*/
2019-11-20 17:40:45 +02:00
$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 " .
2019-10-25 22:07:09 +03:00
2019-11-20 17:40:45 +02:00
" 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 " .
2019-10-25 22:07:09 +03:00
2019-11-20 17:40:45 +02:00
" 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. " ;
send_email ( $user -> user_email , $subject , $message , $pledge_id );
}
2019-10-25 22:07:09 +03:00
}
2019-12-17 00:33:43 +02:00
/**
* Send the removed contributor an email to notify them after removal .
*
* @ param int $pledge_id
* @ param WP_Post $contributor
*/
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$s’ s Five for the Future pledge. If this is unexpected news, it’ s best to reach out directly to %1$s with questions. Have a great day!' ,
$pledge -> post_title
);
$user = get_user_by ( 'login' , $contributor -> post_title );
send_email ( $user -> user_email , $subject , $message , $pledge_id );
}
2019-10-25 22:07:09 +03:00
/**
2019-11-20 17:40:45 +02:00
* Email the pledge admin a temporary link they can use to manage their pledge .
2019-10-25 22:07:09 +03:00
*
2019-11-20 17:40:45 +02:00
* @ param int $pledge_id
2019-10-25 22:07:09 +03:00
*
2019-11-20 17:40:45 +02:00
* @ return true | WP_Error
2019-10-25 22:07:09 +03:00
*/
2019-11-20 17:40:45 +02:00
function send_manage_pledge_link ( $pledge_id ) {
$admin_email = get_post ( $pledge_id ) -> { META_PREFIX . 'org-pledge-email' };
2019-10-25 22:07:09 +03:00
2019-11-20 17:40:45 +02:00
if ( ! is_email ( $admin_email ) ) {
return new WP_Error ( 'invalid_email' , 'Invalid email address.' );
2019-10-25 22:07:09 +03:00
}
2019-12-21 00:20:24 +02:00
$subject = __ ( 'Next steps to update your Pledge' , 'wporg-5ftf' );
$message = sprintf (
" Hi there! \n \n Thanks for confirming your email! Here is the link to update your pledge: \n \n %s \n \n If you have any questions about how to update your pledge, or about Five for the Future in general, please visit %s. \n \n Thanks for participating in Five for the Future. Your contribution ensures that WordPress continues to thrive! " ,
2019-11-20 17:40:45 +02:00
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
2019-12-21 00:20:24 +02:00
),
home_url ()
);
2019-10-25 23:47:59 +03:00
2019-11-20 17:40:45 +02:00
$result = send_email ( $admin_email , $subject , $message , $pledge_id );
2019-10-25 22:07:09 +03:00
2019-11-20 17:40:45 +02:00
if ( ! $result ) {
$result = new WP_Error ( 'email_failed' , 'Email failed to send' );
2019-10-25 22:07:09 +03:00
}
2019-11-20 17:40:45 +02:00
return $result ;
2019-10-25 22:07:09 +03:00
}
2019-12-10 21:07:48 +02:00
/**
* Email pledge manager to notify that the pledge has been removed .
*
* @ param WP_Post $pledge The pledge object , used to add the title now that the pledge itself has been deleted .
*
* @ return bool
*/
function send_pledge_deactivation_email ( $pledge ) {
$message = sprintf (
" Your organization, %s, has been removed from the Five for the Future listing. \n \n " .
'Please reply to this email if this was a mistake.' ,
$pledge -> post_title
);
return send_email (
$pledge -> { '5ftf_org-pledge-email' },
'Pledge removed from Five for the Future' ,
$message ,
$pledge -> ID
);
}