Skip to content

Commit 4b4eb0f

Browse files
committed
better wp_mail overrides check
1 parent 729a9d4 commit 4b4eb0f

1 file changed

Lines changed: 49 additions & 7 deletions

File tree

classes/suggested-tasks/providers/interactive/class-email-sending.php

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,18 @@ class Email_Sending extends Interactive {
7878
protected $email_error = '';
7979

8080
/**
81-
* Whether phpmailer is filtered.
81+
* Whether wp_mail is filtered.
8282
*
8383
* @var bool
8484
*/
85-
protected $is_phpmailer_filtered = false;
85+
protected $is_wp_mail_filtered = false;
86+
87+
/**
88+
* Whether wp_mail is overridden.
89+
*
90+
* @var bool
91+
*/
92+
protected $is_wp_mail_overridden = false;
8693

8794
/**
8895
* Initialize the task provider.
@@ -101,7 +108,8 @@ public function init() {
101108
add_action( 'wp_mail_failed', [ $this, 'set_email_error' ] );
102109

103110
// By now all plugins should be loaded and hopefully add actions registered, so we can check if phpmailer is filtered.
104-
\add_action( 'init', [ $this, 'check_if_phpmailer_is_filtered' ], PHP_INT_MAX );
111+
\add_action( 'init', [ $this, 'check_if_wp_mail_is_filtered' ], PHP_INT_MAX );
112+
\add_action( 'init', [ $this, 'check_if_wp_mail_has_override' ], PHP_INT_MAX );
105113

106114
$this->email_subject = \esc_html__( 'Test email from Progress Planner', 'progress-planner' );
107115
// translators: %s is the admin URL.
@@ -155,13 +163,47 @@ public function enqueue_scripts() {
155163
}
156164

157165
/**
158-
* Check if phpmailer is filtered.
166+
* Check if wp_mail is filtered.
159167
*
160168
* @return void
161169
*/
162-
public function check_if_phpmailer_is_filtered() {
170+
public function check_if_wp_mail_is_filtered() {
163171
global $wp_filter;
164-
$this->is_phpmailer_filtered = isset( $wp_filter['phpmailer_init'] ) && ! empty( $wp_filter['phpmailer_init']->callbacks ) ? true : false;
172+
173+
$filters_to_check = [
174+
'phpmailer_init',
175+
'pre_wp_mail',
176+
];
177+
178+
foreach ( $filters_to_check as $filter ) {
179+
$has_filter = isset( $wp_filter[ $filter ] ) && ! empty( $wp_filter[ $filter ]->callbacks ) ? true : false;
180+
$this->is_wp_mail_filtered = $this->is_wp_mail_filtered || $has_filter;
181+
}
182+
}
183+
184+
/**
185+
* Check if wp_mail has an override.
186+
*
187+
* @return void
188+
*/
189+
public function check_if_wp_mail_has_override() {
190+
191+
// Just in case, since it will trigger PHP fatal error if the function doesn't exist.
192+
if ( function_exists( 'wp_mail' ) ) {
193+
$ref = new \ReflectionFunction( 'wp_mail' );
194+
$file_path = $ref->getFileName();
195+
196+
$this->is_wp_mail_overridden = $file_path && $file_path !== ABSPATH . 'wp-includes/pluggable.php';
197+
}
198+
}
199+
200+
/**
201+
* Whether there is an email override.
202+
*
203+
* @return bool
204+
*/
205+
protected function is_there_sending_email_override() {
206+
return $this->is_wp_mail_filtered || $this->is_wp_mail_overridden;
165207
}
166208

167209
/**
@@ -367,7 +409,7 @@ public function the_popover_content() {
367409
<?php /* Email not received, showing troubleshooting */ ?>
368410
<div class="prpl-columns-wrapper-flex prpl-sending-email-step" id="prpl-sending-email-troubleshooting-step" style="display: none;">
369411
<div class="prpl-column prpl-column-content">
370-
<?php if ( $this->is_phpmailer_filtered ) : ?>
412+
<?php if ( $this->is_there_sending_email_override() ) : ?>
371413
<p><?php \esc_html_e( 'Your website is using a plugin that filters emails.', 'progress-planner' ); ?></p>
372414
<?php else : ?>
373415
<p><?php \esc_html_e( 'Your website is not using a plugin that filters emails.', 'progress-planner' ); ?></p>

0 commit comments

Comments
 (0)