bp_email_set_default_tokens( array $tokens, string $property_name, string $transform, BP_Email $email )

Add default email tokens.

Description

Parameters

$tokens

(Required) Email tokens.

$property_name

(Required) Unused.

$transform

(Required) Unused.

$email

(Required) Email being sent.

Return

(array)

Source

File: bp-core/bp-core-filters.php

1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
function bp_email_set_default_tokens( $tokens, $property_name, $transform, $email ) {
    $tokens['site.admin-email'] = bp_get_option( 'admin_email' );
    $tokens['site.url']         = home_url();
    $tokens['email.subject']    = $email->get_subject();
 
    // These options are escaped with esc_html on the way into the database in sanitize_option().
    $tokens['site.description'] = wp_specialchars_decode( bp_get_option( 'blogdescription' ), ENT_QUOTES );
    $tokens['site.name']        = wp_specialchars_decode( bp_get_option( 'blogname' ), ENT_QUOTES );
 
    // Default values for tokens set conditionally below.
    $tokens['email.preheader']     = '';
    $tokens['recipient.email']     = '';
    $tokens['recipient.name']      = '';
    $tokens['recipient.avatar']    = '';
    $tokens['recipient.username']  = '';
 
    // Who is the email going to?
    $recipient = $email->get( 'to' );
    if ( $recipient ) {
        $recipient = array_shift( $recipient );
        $user_obj  = $recipient->get_user( 'search-email' );
 
        $tokens['recipient.email']  = $recipient->get_address();
        $tokens['recipient.name']   = $recipient->get_name();
        $tokens['recipient.avatar'] = $recipient->get_avatar();
 
        if ( ! $user_obj && $tokens['recipient.email'] ) {
            $user_obj = get_user_by( 'email', $tokens['recipient.email'] );
        }
 
        if ( $user_obj ) {
            $tokens['recipient.username'] = $user_obj->user_login;
 
            if ( bp_is_active( 'settings' ) && empty( $tokens['unsubscribe'] ) ) {
                $tokens['unsubscribe'] = esc_url( sprintf(
                    '%s%s/notifications/',
                    bp_core_get_user_domain( $user_obj->ID ),
                    bp_get_settings_slug()
                ) );
            }
        }
    }
 
    // Set default unsubscribe link if not passed.
    if ( empty( $tokens['unsubscribe'] ) ) {
        $tokens['unsubscribe'] = wp_login_url();
    }
 
    // Email preheader.
    $post = $email->get_post_object();
    if ( $post ) {
        $tokens['email.preheader'] = sanitize_text_field( get_post_meta( $post->ID, 'bp_email_preheader', true ) );
    }
 
    return $tokens;
}

Changelog

Changelog
Version Description
BuddyPress 2.5.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.