Last active
June 4, 2023 21:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! function_exists( 'um_email_locate_template' ) ) { | |
/** | |
* Locate a template and return the path for inclusion. | |
*/ | |
function um_email_locate_template( $template_name ) { | |
$blog_id = is_multisite() ? '/' . get_current_blog_id() : ''; | |
$template = locate_template( | |
array( | |
trailingslashit( 'ultimate-member/email' . $blog_id ) . $template_name . '.php', | |
trailingslashit( 'ultimate-member/email' ) . $template_name . '.php', | |
) | |
); | |
if ( ! $template ) { | |
$template = wp_normalize_path( STYLESHEETPATH . '/ultimate-member/email/' . $template_name . '.php' ); | |
} | |
return apply_filters( 'um_locate_email_template', $template, $template_name ); | |
} | |
} | |
/** | |
* 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 ( ! array_key_exists( $slug.'_on', UM()->options()->options ) ) { | |
UM()->options()->options[ $slug.'_on' ] = empty( $custom_email['default_active'] ) ? 0 : 1; | |
UM()->options()->options[ $slug.'_sub' ] = $custom_email['subject']; | |
} | |
// Template file. | |
$located = um_email_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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Method
UM()->mail()->locate_template()
has been marked private. Use custom functionum_email_locate_template
instead.