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

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.