Beispiel #1
0
/**
 * Add support for pre-2.5 email actions.
 *
 * @since 2.5.0
 *
 * @param bool|WP_Error $delivery_status Bool if the email was sent or not.
 *                                       If a WP_Error, there was a failure.
 * @param BP_Email $email Email object reference.
 * @return mixed
 */
function bp_core_deprecated_email_actions($delivery_status, $email)
{
    $pre_2_5_emails = array('activity-comment', 'activity-comment-author', 'core-user-registration', 'core-user-registration-with-blog', 'friends-request', 'friends-request-accepted', 'groups-details-updated', 'groups-invitation', 'groups-member-promoted', 'groups-membership-request', 'groups-membership-request-accepted', 'groups-membership-request-rejected', 'messages-unread', 'settings-verify-email-change');
    remove_action('bp_send_email_success', 'bp_core_deprecated_email_actions', 20);
    $email_content = $email->get('content');
    $email_subject = $email->get('subject');
    $email_type = $email->get('type');
    $tokens = $email->get('tokens');
    add_action('bp_send_email_success', 'bp_core_deprecated_email_actions', 20, 2);
    // Backpat for pre-2.5 emails only.
    if (!in_array($email_type, $pre_2_5_emails, true)) {
        return;
    }
    if ($email_type === 'activity-comment') {
        /**
         * Fires after the sending of a reply to an update email notification.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email. $params argument unset and deprecated.
         *
         * @param int    $user_id       ID of the original activity item author.
         * @param string $email_subject Email notification subject text.
         * @param string $email_content Email notification message text.
         * @param int    $comment_id    ID for the newly received comment.
         * @param int    $commenter_id  ID of the user who made the comment.
         * @param array  $deprecated    Removed in 2.5; now an empty array.
         */
        do_action('bp_activity_sent_reply_to_update_email', $tokens['original_activity.user_id'], $email_subject, $email_content, $tokens['comment.id'], $tokens['commenter.id'], array());
    } elseif ($email_type === 'activity-comment-author') {
        /**
         * Fires after the sending of a reply to a reply email notification.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email. $params argument unset and deprecated.
         *
         * @param int    $user_id       ID of the parent activity item author.
         * @param string $email_subject Email notification subject text.
         * @param string $email_content Email notification message text.
         * @param int    $comment_id    ID for the newly received comment.
         * @param int    $commenter_id  ID of the user who made the comment.
         * @param array  $deprecated    Removed in 2.5; now an empty array.
         */
        do_action('bp_activity_sent_reply_to_reply_email', $tokens['parent-comment-user.id'], $email_subject, $email_content, $tokens['comment.id'], $tokens['commenter.id'], array());
    } elseif ($email_type === 'core-user-registration') {
        if (!empty($tokens['user.id'])) {
            /**
             * Fires after the sending of activation email to a newly registered user.
             *
             * @since 1.5.0
             *
             * @param string $email_subject   Subject for the sent email.
             * @param string $email_content   Message for the sent email.
             * @param int    $user_id         ID of the new user.
             * @param string $recipient_email Email address of the new user.
             * @param string $key             Activation key.
             */
            do_action('bp_core_sent_user_validation_email', $email_subject, $email_content, $tokens['user.id'], $tokens['recipient.email'], $tokens['key']);
        } else {
            /**
             * Fires after the sending of the notification to new users for successful registration without blog.
             *
             * @since 1.5.0
             * @deprecated 2.5.0 Use the filters in BP_Email. $meta argument unset and deprecated.
             *
             * @param string $admin_email        Admin Email address for the site.
             * @param string $email_subject      Subject used in the notification email.
             * @param string $email_content      Message used in the notification email.
             * @param string $recipient_username The user's login name.
             * @param string $recipient_email    The user's email address.
             * @param string $key                The activation key created in wpmu_signup_blog().
             * @param array  $meta               Removed in 2.5; now an empty array.
             */
            do_action('bp_core_sent_user_signup_email', bp_get_option('admin_email'), $email_subject, $email_content, $tokens['recipient.username'], $tokens['recipient.email'], $tokens['key'], array());
        }
    } elseif ($email_type === 'core-user-registration-with-blog') {
        /**
         * Fires after the sending of the notification to new users for successful registration with blog.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email. $meta argument unset and deprecated.
         *
         * @param string $admin_email        Admin Email address for the site.
         * @param string $email_subject      Subject used in the notification email.
         * @param string $email_content      Message used in the notification email.
         * @param string $domain             The new blog domain.
         * @param string $path               The new blog path.
         * @param string $title              The site title.
         * @param string $recipient_username The user's login name.
         * @param string $recipient_email    The user's email address.
         * @param string $key                The activation key created in wpmu_signup_blog().
         * @param array  $meta               Removed in 2.5; now an empty array.
         */
        do_action('bp_core_sent_blog_signup_email', bp_get_option('admin_email'), $email_subject, $email_content, $tokens['domain'], $tokens['path'], $tokens['title'], $tokens['recipient.username'], $tokens['recipient.email'], $tokens['key_blog'], array());
    } elseif ($email_type === 'friends-request') {
        /**
         * Fires after the new friend request email is sent.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $friend_id     ID of the request recipient.
         * @param string $email_subject Text for the friend request subject field.
         * @param string $email_content Text for the friend request message field.
         * @param int    $friendship_id ID of the friendship object.
         * @param int    $initiator_id  ID of the friendship requester.
         */
        do_action('bp_friends_sent_request_email', $tokens['friend.id'], $email_subject, $email_content, $tokens['friendship.id'], $tokens['initiator.id']);
    } elseif ($email_type === 'friends-request-accepted') {
        /**
         * Fires after the friend request accepted email is sent.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $initiator_id  ID of the friendship requester.
         * @param string $email_subject Text for the friend request subject field.
         * @param string $email_content Text for the friend request message field.
         * @param int    $friendship_id ID of the friendship object.
         * @param int    $friend_id     ID of the request recipient.
         */
        do_action('bp_friends_sent_accepted_email', $tokens['initiator.id'], $email_subject, $email_content, $tokens['friendship.id'], $tokens['friend.id']);
    } elseif ($email_type === 'groups-invitation') {
        /**
         * Fires after the notification is sent that a member has been invited to a group.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int             $invited_user_id  ID of the user who was invited.
         * @param string          $email_subject    Email notification subject text.
         * @param string          $email_content    Email notification message text.
         * @param BP_Groups_Group $group            Group object.
         */
        do_action('bp_groups_sent_invited_email', $tokens['inviter.id'], $email_subject, $email_content, $tokens['group']);
    } elseif ($email_type === 'groups-member-promoted') {
        /**
         * Fires after the notification is sent that a member has been promoted.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $user_id       ID of the user who was promoted.
         * @param string $email_subject Email notification subject text.
         * @param string $email_content Email notification message text.
         * @param int    $group_id      ID of the group that the user is a member of.
         */
        do_action('bp_groups_sent_promoted_email', $tokens['user.id'], $email_subject, $email_content, $tokens['group.id']);
    } elseif ($email_type === 'groups-membership-request') {
        /**
         * Fires after the notification is sent that a member has requested group membership.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $admin_id           ID of the group administrator.
         * @param string $email_subject      Email notification subject text.
         * @param string $email_content      Email notification message text.
         * @param int    $requesting_user_id ID of the user requesting membership.
         * @param int    $group_id           ID of the group receiving membership request.
         * @param int    $membership_id      ID of the group membership object.
         */
        do_action('bp_groups_sent_membership_request_email', $tokens['admin.id'], $email_subject, $email_content, $tokens['requesting-user.id'], $tokens['group.id'], $tokens['membership.id']);
    } elseif ($email_type === 'groups-membership-request-accepted' || $email_type === 'groups-membership-request-rejected') {
        /**
         * Fires after the notification is sent that a membership has been approved.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use the filters in BP_Email.
         *
         * @param int    $requesting_user_id ID of the user whose membership was approved.
         * @param string $email_subject      Email notification subject text.
         * @param string $email_content      Email notification message text.
         * @param int    $group_id           ID of the group that was joined.
         */
        do_action('bp_groups_sent_membership_approved_email', $tokens['requesting-user.id'], $email_subject, $email_content, $tokens['group.id']);
    }
}
 /**
  * Send email(s).
  *
  * @since 2.5.0
  *
  * @param BP_Email $email Email to send.
  * @return bool|WP_Error Returns true if email send, else a descriptive WP_Error.
  */
 public function bp_email(BP_Email $email)
 {
     static $phpmailer = null;
     if ($phpmailer === null) {
         if (!class_exists('PHPMailer')) {
             require_once ABSPATH . WPINC . '/class-phpmailer.php';
             require_once ABSPATH . WPINC . '/class-smtp.php';
         }
         $phpmailer = new PHPMailer(true);
     }
     /*
      * Resets.
      */
     $phpmailer->clearAllRecipients();
     $phpmailer->clearAttachments();
     $phpmailer->clearCustomHeaders();
     $phpmailer->clearReplyTos();
     $phpmailer->Sender = '';
     /*
      * Set up.
      */
     $phpmailer->IsMail();
     $phpmailer->CharSet = bp_get_option('blog_charset');
     $phpmailer->Hostname = self::get_hostname();
     /*
      * Content.
      */
     $phpmailer->Subject = $email->get_subject('replace-tokens');
     $content_plaintext = PHPMailer::normalizeBreaks($email->get_content_plaintext('replace-tokens'));
     if ($email->get('content_type') === 'html') {
         $phpmailer->msgHTML($email->get_template('add-content'), '', 'wp_strip_all_tags');
         $phpmailer->AltBody = $content_plaintext;
     } else {
         $phpmailer->IsHTML(false);
         $phpmailer->Body = $content_plaintext;
     }
     $recipient = $email->get_from();
     try {
         $phpmailer->SetFrom($recipient->get_address(), $recipient->get_name(), false);
     } catch (phpmailerException $e) {
     }
     $recipient = $email->get_reply_to();
     try {
         $phpmailer->addReplyTo($recipient->get_address(), $recipient->get_name());
     } catch (phpmailerException $e) {
     }
     $recipients = $email->get_to();
     foreach ($recipients as $recipient) {
         try {
             $phpmailer->AddAddress($recipient->get_address(), $recipient->get_name());
         } catch (phpmailerException $e) {
         }
     }
     $recipients = $email->get_cc();
     foreach ($recipients as $recipient) {
         try {
             $phpmailer->AddCc($recipient->get_address(), $recipient->get_name());
         } catch (phpmailerException $e) {
         }
     }
     $recipients = $email->get_bcc();
     foreach ($recipients as $recipient) {
         try {
             $phpmailer->AddBcc($recipient->get_address(), $recipient->get_name());
         } catch (phpmailerException $e) {
         }
     }
     $headers = $email->get_headers();
     foreach ($headers as $name => $content) {
         $phpmailer->AddCustomHeader($name, $content);
     }
     /**
      * Fires after PHPMailer is initialised.
      *
      * @since 2.5.0
      *
      * @param PHPMailer $phpmailer The PHPMailer instance.
      */
     do_action('bp_phpmailer_init', $phpmailer);
     /** This filter is documented in wp-includes/pluggable.php */
     do_action_ref_array('phpmailer_init', array(&$phpmailer));
     try {
         return $phpmailer->Send();
     } catch (phpmailerException $e) {
         return new WP_Error($e->getCode(), $e->getMessage(), $email);
     }
 }
/**
 * Add default email tokens.
 *
 * @since 2.5.0
 *
 * @param array $tokens Email tokens.
 * @param string $property_name Unused.
 * @param string $transform Unused.
 * @param BP_Email $email Email being sent.
 * @return array
 */
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();
    // 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.username'] = '';
    $tokens['unsubscribe'] = site_url('wp-login.php');
    // 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();
        if (!$user_obj && $tokens['recipient.email']) {
            $user_obj = get_user_by('email', $tokens['recipient.email']);
        }
        if ($user_obj) {
            // Unsubscribe link.
            $tokens['unsubscribe'] = esc_url(sprintf('%s%s/notifications/', bp_core_get_user_domain($user_obj->ID), function_exists('bp_get_settings_slug') ? bp_get_settings_slug() : 'settings'));
            $tokens['recipient.username'] = $user_obj->user_login;
        }
    }
    // 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;
}
 public function test_invalid_headers()
 {
     $email = new BP_Email('activity-at-message');
     $default_headers = $email->get_headers();
     $headers = array('custom:header' => 'custom:value');
     $email->set_headers($headers);
     $this->assertNotSame($headers + $default_headers, $email->get('headers'));
     $this->assertSame(array('customheader' => 'customvalue') + $default_headers, $email->get('headers'));
 }