setup_vars(); $this->hooks(); $this->check_credentials(); } public function setup_vars(){ $this->wsOptions = get_option( 'wp_smtp_options' ); } public function hooks() { register_activation_hook( __FILE__ , array( $this,'wp_smtp_activate' ) ); register_deactivation_hook( __FILE__, array( $this, 'wp_smtp_deactivate' ) ); add_filter( 'plugin_action_links', array( $this, 'wp_smtp_settings_link' ), 10, 2 ); add_action( 'init', array( $this,'load_textdomain' ) ); add_action( 'phpmailer_init', array( $this,'wp_smtp' ) ); add_action( 'admin_notices', array( $this, 'retype_credentials_notice' ) ); add_action( 'wp_smtp_admin_update', array( $this, 'check_credentials' ) ); add_action( 'wp_loaded', array( $this, 'wp_smtp_form_actions' ), 15 ); add_action( 'wp_loaded', array( $this, 'setup_vars' ), 20 ); add_action( 'wp_loaded', array( $this, 'load_admin_requirements'), 30 ); } public function load_admin_requirements() { new WPSMTP\Admin(); new WPSMTP\Process(); } public function wp_smtp_activate(){ $wsOptions = array(); $wsOptions["from"] = ""; $wsOptions["fromname"] = ""; $wsOptions["host"] = ""; $wsOptions["smtpsecure"] = ""; $wsOptions["port"] = ""; $wsOptions["smtpauth"] = "yes"; $wsOptions["username"] = ""; $wsOptions["password"] = ""; $wsOptions["deactivate"] = ""; add_option( 'wp_smtp_options', $wsOptions ); \WPSMTP\Table::install(); } public function wp_smtp_deactivate() { if( $this->wsOptions['deactivate'] == 'yes' ) { delete_option( 'wp_smtp_options' ); delete_option( 'wp_smtp_encrypted' ); } } public function load_textdomain() { load_plugin_textdomain( 'wp-smtp', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' ); } public function wp_smtp( $phpmailer ) { if( ! is_email($this->wsOptions["from"] ) || empty( $this->wsOptions["host"] ) ) { return; } $phpmailer->Mailer = "smtp"; $phpmailer->From = $this->wsOptions["from"]; $phpmailer->FromName = $this->wsOptions["fromname"]; $phpmailer->Sender = $phpmailer->From; $phpmailer->AddReplyTo($phpmailer->From,$phpmailer->FromName); $phpmailer->Host = $this->wsOptions["host"]; $phpmailer->SMTPSecure = $this->wsOptions["smtpsecure"]; $phpmailer->Port = $this->wsOptions["port"]; $phpmailer->SMTPAuth = ($this->wsOptions["smtpauth"]=="yes") ? TRUE : FALSE; if( $phpmailer->SMTPAuth ){ $phpmailer->Username = base64_decode( $this->wsOptions["username"] ); $phpmailer->Password = base64_decode( $this->wsOptions["password"] ); } } public function wp_smtp_settings_link($action_links,$plugin_file) { if( $plugin_file == plugin_basename( __FILE__ ) ) { $ws_settings_link = '' . __("Logs") . ''; array_unshift($action_links,$ws_settings_link); $ws_settings_link = '' . __("Settings") . ''; array_unshift($action_links,$ws_settings_link); } return $action_links; } /** * Check for credentials * * @param array $options WP SMTP options * * @return mixed * @since 1.2.5 */ public function check_credentials( $options = array(), $pass_ajax = false ) { if ( ! is_admin() || ( ! $pass_ajax && defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { return; } $encription = get_option( 'wp_smtp_status' ); // Connecting to host can be a resource heavy task, so we only do it if we need to. if ( 'encrypted' === $encription ) { return true; } // Connecting to host can be a resource heavy task, so we only do it if we need to. if ( 'not_encrypted' === $encription ) { add_action( 'admin_notices', array( $this, 'retype_credentials_notice' ) ); add_action( 'wp_smtp_admin_notices', array( $this, 'retype_credentials_wp_smtp_notice' ) ); return false; } if ( empty( $options ) ) { $options = get_option( 'wp_smtp_options' ); } if ( ! isset( $options['username'] ) || ! isset( $options['password'] ) || ! isset( $options['host'] ) || ! isset( $options['port'] ) || ! isset( $options['smtpauth'] ) || ! isset( $options['smtpsecure'] ) || '' === $options['username'] || '' === $options['password'] || '' === $options['host'] || '' === $options['port'] || '' === $options['smtpauth'] ) { return false; } global $phpmailer; // (Re)create it, if it's gone missing. if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) { require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php'; require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php'; require_once ABSPATH . WPINC . '/PHPMailer/Exception.php'; $phpmailer = new PHPMailer\PHPMailer\PHPMailer( true ); } // Set the timeout to 15 seconds, so if it doesn't connect to not let the user in standby. $smtp = $phpmailer->getSMTPInstance(); $smtp->Timeout = 15; $smtp->Timelimit = 15; $phpmailer->Timeout = 15; $phpmailer->Timelimit = 15; $phpmailer->Mailer = "smtp"; $phpmailer->Host = $options['host']; $phpmailer->SMTPAuth = 'yes' === $options['smtpauth']; // Ask it to use authenticate using the Username and Password properties $phpmailer->Port = $options['port']; $phpmailer->SMTPKeepAlive = false; if ( $phpmailer->SMTPAuth ) { $phpmailer->Username = base64_decode( $options['username'] ); $phpmailer->Password = base64_decode( $options['password'] ); } $phpmailer->SMTPSecure = $options['smtpsecure']; // preferable but optional try { if ( $phpmailer->smtpConnect() ) { update_option( 'wp_smtp_status', 'encrypted' ); return true; } else { update_option( 'wp_smtp_status', 'not_encrypted' ); return false; } } catch ( Exception $e ) { update_option( 'wp_smtp_status', 'not_encrypted' ); return false; } } /** * Add notice to retype credentials and info about the server * * @return void * @since 1.2.5 */ public function retype_credentials_notice() { $status = get_option( 'wp_smtp_status' ); if ( ! $status || 'not_encrypted' !== $status) { return; } ?>
' . esc_html__( 'We recently made some changes regarding how we save the username and password in the database, namely we are encrypting them, so that might be the reason for the connection error. Re-entering and saving them should solve the issue.', 'wp-smtp' ) . '
'; } ?>