Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save yuriinalivaiko/0b298c9ba98f41dbc0da89d435301f6c to your computer and use it in GitHub Desktop.

Select an option

Save yuriinalivaiko/0b298c9ba98f41dbc0da89d435301f6c to your computer and use it in GitHub Desktop.
This code adds custom email template "Profile Completeness - Profile is complete" to be used in the "Ultimate Member - Profile Completeness" extension.
<?php
/**
* Add custom email template "Profile Completeness - Profile is complete".
*
* @param array $emails Emails list.
* @return array
*/
function um_email_notification_profile_is_complete( $emails ) {
// New email templates.
$custom_emails = array(
'profile_is_complete' => array(
'key' => 'profile_is_complete',
'title' => __( 'Profile Completeness - Profile is complete', 'ultimate-member' ),
'description' => __( 'Whether to send an email to an admin when a member\'s profile is complete', 'ultimate-member' ),
'recipient' => 'admin',
'default_active' => true,
'subject' => '[{site_name}] Profile is complete',
'body' => '{display_name} has just completed a profile on {site_name}. <br />To view their profile click here: {user_profile_link} <br /><br />Old role: {old_role}<br />New role: {new_role}',
),
);
foreach ( $custom_emails as $slug => $custom_email ) {
// Default settings.
if ( '' === UM()->options()->get( $slug . '_on' ) ) {
$email_on = empty( $custom_email['default_active'] ) ? 0 : 1;
UM()->options()->update( $slug . '_on', $email_on );
}
if ( '' === UM()->options()->get( $slug . '_sub' ) ) {
$email_sub = empty( $custom_email['subject'] ) ? $slug : $custom_email['subject'];
UM()->options()->update( $slug . '_sub', $email_sub );
}
// Template file.
$located = UM()->mail()->locate_template( $slug );
if ( ! file_exists( $located ) ) {
wp_mkdir_p( dirname( $located ) );
file_put_contents( $located, $custom_email['body'] );
}
$emails[ $slug ] = $custom_email;
}
return $emails;
}
add_filter( 'um_email_notifications', 'um_email_notification_profile_is_complete' );
/**
* Send an email to an admin when the member's profile is complete.
*
* @param array $result
* @param array $role_data
* @return array
*/
function um_email_notification_profile_is_complete_send( $result, $role_data ) {
if ( (int) $result['progress'] >= (int) $role_data['profilec_pct'] ) {
$user_id = get_current_user_id();
$userdata = get_userdata( $user_id );
$old_role = implode( ', ', $userdata->roles );
$new_role = empty( $role_data['profilec_upgrade_role'] ) ? $old_role : $role_data['profilec_upgrade_role'];
$args = array(
'tags' => array(
'{old_role}',
'{new_role}',
),
'tags_replace' => array(
$old_role,
$new_role,
),
);
$emails = um_multi_admin_email();
if ( ! empty( $emails ) ) {
foreach ( $emails as $email ) {
UM()->mail()->send( $email, 'profile_is_complete', $args );
}
}
}
return $result;
}
add_filter( 'um_profile_completeness_get_progress_result', 'um_email_notification_profile_is_complete_send', 20, 2 );
@yuriinalivaiko
Copy link
Author

yuriinalivaiko commented Jul 10, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment