class.DisableEmailsPlugin.php
2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace uncanny_learndash_toolkit;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* class for managing the plugin
*/
class DisableEmailsPlugin {
public $options;
protected $wpmailReplaced = false;
/**
* static method for getting the instance of this singleton object
* @return self
*/
public static function getInstance() {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new self();
}
return $instance;
}
/**
* hook into WordPress
*/
private function __construct() {
$defaults = array(
'wp_mail' => 1,
'wp_mail_from' => 1,
'wp_mail_from_name' => 1,
'wp_mail_content_type' => 1,
'wp_mail_charset' => 1,
'phpmailer_init' => 1,
'buddypress' => 1,
'events_manager' => 1,
);
$this->options = get_option( DISABLE_EMAILS_OPTIONS, $defaults );
add_action( 'admin_notices', array( $this, 'showWarningAlreadyDefined' ) );
// maybe stop BuddyPress emails too
if ( ! empty( $this->options['buddypress'] ) ) {
add_filter( 'bp_email_use_wp_mail', '__return_true' );
}
// maybe stop Events Manager emails too
if ( ! empty( $this->options['events_manager'] ) ) {
add_filter( 'pre_option_dbem_rsvp_mail_send_method', array( $this, 'forceEventsManagerDisable' ) );
add_action( 'load-event_page_events-manager-options', array( $this, 'cancelEventsManagerDisable' ) );
}
}
/**
* warn that wp_mail() is defined so emails cannot be disabled!
*/
public function showWarningAlreadyDefined() {
if ( ! $this->wpmailReplaced ) {
include UNCANNY_TOOLKIT_DIR . '/src/templates/warn-already-defined.php';
}
}
/**
* force Events Manager to use wp_mail(), so that we can disable it
*
* @param string|bool $return
*
* @return string
*/
public function forceEventsManagerDisable( $return ) {
return 'wp_mail';
}
/**
* cancel Events Manager hook forcing wp_mail() because we're on its settings page
*/
public function cancelEventsManagerDisable() {
remove_filter( 'pre_option_dbem_rsvp_mail_send_method', array( $this, 'forceEventsManagerDisable' ) );
}
/**
* wp_mail() was successfully replaced, so we can activate disabling emails
*/
public static function setActive() {
include UNCANNY_TOOLKIT_DIR . '/src/includes/class.DisableEmailsPHPMailerMock.php';
$plugin = self::getInstance();
$plugin->wpmailReplaced = true;
}
}