Skip to:
Content

BuddyPress.org

Changeset 10490


Ignore:
Timestamp:
02/01/2016 07:25:42 PM (4 years ago)
Author:
djpaul
Message:

Emails: use local instance of WordPress' PHPMailer class.

Relying on the global is okay, but initialising it (if it is unset) touches a WordPress global which doesn't have a direct API. While this is probably safe today, it may not be reliable in the future. After consideration, especially considering BuddyPress' efforts not to introduce or use globals directly, now we instead create a static instance of the PHPMailer object inside the BP_Email class.

The original approach came from WordPress' wp_mail() which, I assume, once found a situation where the global wasn't set.

For #6592

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-phpmailer.php

    r10488 r10490  
    1818
    1919    /**
    20      * Constructor.
    21      *
    22      * @since 2.5.0
    23      */
    24     public function __construct() {
    25         global $phpmailer;
    26 
    27         // We'll try to use the PHPMailer object that might have been created by WordPress.
    28         if ( ! ( $phpmailer instanceof PHPMailer ) ) {
    29             require_once ABSPATH . WPINC . '/class-phpmailer.php';
    30             require_once ABSPATH . WPINC . '/class-smtp.php';
    31             $phpmailer = new PHPMailer( true );
    32         }
    33     }
    34 
    35     /**
    3620     * Send email(s).
    3721     *
     
    4226     */
    4327    public function bp_email( BP_Email $email ) {
    44         global $phpmailer;
     28        static $phpmailer = null;
     29
     30        if ( $phpmailer === null ) {
     31            if ( ! class_exists( 'PHPMailer' ) ) {
     32                require_once ABSPATH . WPINC . '/class-phpmailer.php';
     33                require_once ABSPATH . WPINC . '/class-smtp.php';
     34            }
     35
     36            $phpmailer = new PHPMailer( true );
     37        }
     38
    4539
    4640        /*
Note: See TracChangeset for help on using the changeset viewer.