Skip to:
Content

BuddyPress.org

Changeset 10477


Ignore:
Timestamp:
01/27/2016 09:05:41 PM (9 years ago)
Author:
djpaul
Message:

Emails: add default filters for email headers and tokens.

See #6592. Props timersys, mercime, boonebgorges, hnla, DJPaul.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-filters.php

    r10474 r10477  
    10891089
    10901090/**
     1091 * Add custom headers to outgoing emails.
     1092 *
     1093 * @since 2.5.0
     1094 *
     1095 * @param array $headers
     1096 * @param string $property Name of property. Unused.
     1097 * @param string $transform Return value transformation. Unused.
     1098 * @param BP_Email $email Email object reference.
     1099 * @return array
     1100 */
     1101function bp_email_set_default_headers( $headers, $property, $transform, $email ) {
     1102    $headers['X-BuddyPress']      = bp_get_version();
     1103    $headers['X-BuddyPress-Type'] = $email->get( 'type' );
     1104
     1105    return $headers;
     1106}
     1107add_filter( 'bp_email_get_headers', 'bp_email_set_default_headers', 6, 4 );
     1108
     1109/**
     1110 * Add default email tokens.
     1111 *
     1112 * @since 2.5.0
     1113 *
     1114 * @param array $tokens Email tokens.
     1115 * @param string $property_name Unused.
     1116 * @param string $transform Unused.
     1117 * @param BP_Email $email Email being sent.
     1118 * @return array
     1119 */
     1120function bp_email_set_default_tokens( $tokens, $property_name, $transform, $email ) {
     1121    $tokens['site.admin-email'] = bp_get_option( 'admin_email' );
     1122    $tokens['site.url']         = home_url();
     1123
     1124    // These options are escaped with esc_html on the way into the database in sanitize_option().
     1125    $tokens['site.description'] = wp_specialchars_decode( bp_get_option( 'blogdescription' ), ENT_QUOTES );
     1126    $tokens['site.name']        = wp_specialchars_decode( bp_get_option( 'blogname' ), ENT_QUOTES );
     1127
     1128    // Default values for tokens set conditionally below.
     1129    $tokens['email.preheader'] = '';
     1130    $tokens['recipient.email'] = '';
     1131    $tokens['recipient.name']  = '';
     1132    $tokens['unsubscribe']     = '';
     1133
     1134
     1135    // Who is the email going to?
     1136    $recipient = $email->get( 'to' );
     1137    if ( $recipient ) {
     1138        $recipient = array_shift( $recipient );
     1139        $user_obj  = $recipient->get_user( 'search-email' );
     1140
     1141        $tokens['recipient.address'] = $recipient->get_address();
     1142        $tokens['recipient.name']    = $recipient->get_name();
     1143
     1144        if ( $user_obj ) {
     1145            // Unsubscribe link.
     1146            $tokens['unsubscribe'] = esc_url( sprintf(
     1147                '%s%s/notifications/',
     1148                bp_core_get_user_domain( $user_obj->ID ),
     1149                function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings'
     1150            ) );
     1151        }
     1152    }
     1153
     1154    // Email preheader.
     1155    $post = $email->get_post_object();
     1156    if ( $post ) {
     1157        $tokens['email.preheader'] = sanitize_text_field( get_post_meta( $post->ID, 'bp_email_preheader', true ) );
     1158    }
     1159
     1160    return $tokens;
     1161}
     1162add_filter( 'bp_email_get_tokens', 'bp_email_set_default_tokens', 6, 4 );
     1163
     1164/**
    10911165 * Find and render the template for Email posts (the Customizer and admin previews).
    10921166 *
Note: See TracChangeset for help on using the changeset viewer.