Skip to content

Instantly share code, notes, and snippets.

@steffendre
Last active May 15, 2018 09:18
Show Gist options
  • Save steffendre/37bdf1f2d98eabe74d8fee8dc899d790 to your computer and use it in GitHub Desktop.
Save steffendre/37bdf1f2d98eabe74d8fee8dc899d790 to your computer and use it in GitHub Desktop.
PMPro Approval Addon customization (temporary gist)
/**
* Send an email to admin and membership approvers when a user has signed up for a membership level that requires approval.
*
*/
public static function pmpro_after_change_membership_level( $level_id, $user_id ){
//check if level requires approval, if not stop executing this function and don't send email.
if( !PMPro_Approvals::requiresApproval( $level_id ) ){
return;
}
$mail_recipients = array();
//get admin email address to email admin.
$mail_recipients[] = get_bloginfo( 'admin_email' );
// This meta field is added via PMPro register helper field.
// At the time this function is fired, the data is not (yet) saved in the database.
// So question is if there is an action which if fired after saving meta data, where we can hook this function.
$user_society = get_user_meta( $user_id, 'society', true );
$approvers = array();
if (! '' == $user_society ) {
$approvers = get_users( array(
'meta_key' => 'society',
'meta_value' => $user_society,
'role' => 'pmpro_approver',
));
}
// Get email addresses of approver users
$approvers = array_map(
function( $approver ){
return $approver->user_email;
},
$approvers
);
// Add approvers to mail recipients
$mail_recipients = array_merge( $mail_recipients, $approvers );
$admin_approval_email = new PMProEmail();
$admin_approval_email->email = $mail_recipients;
$admin_approval_email->subject = __( 'A user is pending approval for webinar access', 'pmpro-approvals' );
$admin_approval_email->template = 'admin_notification_approved'; //Update email template for admins.
$admin_approval_email->body .= file_get_contents( dirname( __FILE__ ) . "/email/admin_notification.html" );
$admin_approval_email->body .= '<h1 style="margin: 0 0 10px 0; font-family: sans-serif; font-size: 18px; line-height: 125%; color: #333333; font-weight: normal;">Dear Approver,</h1>';
$admin_approval_email->body .= '<p><a href="' . get_admin_url() . 'admin.php?page=pmpro-approvals&user_id=' . $user_id . '">View user details</a><p>';
$admin_approval_email->body .= '<p>Kind Regards,<br />Customer Desk</p>';
$admin_approval_email->body .= '</td></tr></table>';
$admin_approval_email->sendEmail();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment