forked from ThemeFuse/Unyson-Mailer-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-fw-extension-mailer.php
More file actions
135 lines (117 loc) · 2.86 KB
/
class-fw-extension-mailer.php
File metadata and controls
135 lines (117 loc) · 2.86 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php if (!defined('FW')) die('Forbidden');
class FW_Extension_Mailer extends FW_Extension
{
private $is_configured_cache = null;
/**
* @var FW_Ext_Mailer_Send_Method[]
*/
private $send_methods;
/**
* @internal
*/
protected function _init()
{
if (is_admin()) {
add_action(
'fw_extension_settings_form_render:'. $this->get_name(),
array($this, '_action_extension_settings_form_render')
);
}
}
/**
* @internal
*/
public function _action_extension_settings_form_render()
{
wp_enqueue_script(
'fw_option_email_settings',
$this->get_uri('/static/js/scripts.js'),
array('jquery'),
false,
true
);
}
public function send($to, $subject, $message, $data = array())
{
$send_method = $this->get_send_method(
$this->get_db_settings_option('method')
);
if (!$send_method) {
return array(
'status' => 0,
'message' => __('Invalid send method', 'fw')
);
}
if (is_wp_error(
$send_method_configuration = $send_method->prepare_settings_options_values(
$this->get_db_settings_option($send_method->get_id())
)
)) {
return array(
'status' => 0,
'message' => $send_method_configuration->get_error_message()
);
}
$email = new FW_Ext_Mailer_Email();
$email->set_to($to);
$email->set_subject($subject);
$email->set_body($message);
$result = $send_method->send(
$email,
$this->get_db_settings_option($send_method->get_id()),
$data
);
return is_wp_error($result)
? array(
'status' => 0,
'message' => $result->get_error_message()
)
: array(
'status' => 1,
'message' => __('The message has been successfully sent!', 'fw')
);
}
/**
* Check if extension settings options are valid
* @return bool
*/
public function is_configured()
{
if (is_null($this->is_configured_cache)) {
$send_method = $this->get_send_method(
$this->get_db_settings_option('method')
);
if ($send_method) {
$this->is_configured_cache = !is_wp_error(
$send_method->prepare_settings_options_values(
$this->get_db_settings_option($send_method->get_id())
)
);
} else {
$this->is_configured_cache = false;
}
}
return $this->is_configured_cache;
}
public function get_send_methods()
{
if (empty($this->send_methods)) {
require_once dirname(__FILE__) . '/includes/classes/class-fw-ext-mailer-email.php';
require_once dirname(__FILE__) . '/includes/classes/class-fw-ext-mailer-send-method.php';
$this->send_methods = array();
foreach (apply_filters('fw_ext_mailer_send_methods', array()) as $send_method) {
$this->send_methods[ $send_method->get_id() ] = $send_method;
}
}
return $this->send_methods;
}
public function get_send_method($method_id)
{
$this->get_send_methods(); // init cache
if (isset($this->send_methods[$method_id])) {
return $this->send_methods[$method_id];
} else {
return null;
}
}
}