Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically disable any plugin declaring wp_mail first #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,67 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = [] ) {
return $phpmailer->wpmail($to, $subject, $message, $headers, $attachments);
}

} else {
// Disable whatever plugin has added its own wp_mail function.

require_once( ABSPATH . 'wp-admin/includes/plugin.php' );

/**
* Given a filename, figure out what plugin it is from.
*
* @param string $filename The file path we're trying to determine the plugin for.
*
* @return string|null The plugin file name, e.g. "disable-emails/disable-emails.php".
*/
$get_plugin_file_from_path = function ( $filename ) {

// If the file is outside the plugins dir, whats's up? MU plugins?
if ( ! stristr( $filename, WP_PLUGIN_DIR ) ) {
return null;
}

// Remove the path/to/wp-content/plugins.
$plugin_file = trim( substr( $filename, strlen( realpath( WP_PLUGIN_DIR ) ) ), DIRECTORY_SEPARATOR );

$plugins = get_plugins();

// This will only work if wp_mail is defined in the plugin's main file.
if ( array_key_exists( $plugin_file, $plugins ) ) {

return $plugin_file;
}

// Get the first part of the file path to the first /.
$plugin_slug = substr( $plugin_file, 0, strpos( $plugin_file, DIRECTORY_SEPARATOR ) );

// If the file is in the plugin's folder.
foreach ( $plugins as $plugin_file_name => $plugin ) {

if ( stristr( $plugin_file_name, $plugin_slug ) ) {
return $plugin_file_name;
}
}

return null;
};

$wp_mail_reflector = new \ReflectionFunction( 'wp_mail' );
$wp_mail_filename = $wp_mail_reflector->getFileName();

$built_in_wp_mail_filename = 'wp-includes/pluggable.php';

// If wp_mail has been overridden.
if ( substr( $wp_mail_filename, - 1 * strlen( $built_in_wp_mail_filename ) ) !== $built_in_wp_mail_filename ) {

$plugin_file = $get_plugin_file_from_path( $wp_mail_filename );

if ( null !== $plugin_file ) {

deactivate_plugins( $plugin_file );

}
}

}

/**
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Disable Emails
Contributors: webaware
Contributors: webaware, BrianHenryIE
Plugin Name: Disable Emails
Plugin URI: https://shop.webaware.com.au/downloads/disable-emails/
Author URI: https://shop.webaware.com.au/
Expand Down