-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpluggable.php
80 lines (64 loc) · 2.73 KB
/
pluggable.php
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
<?php
declare(strict_types=1);
/*
* This file is part of Ymir WordPress plugin.
*
* (c) Carl Alexander <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Ymir\Plugin\Email\Email;
use Ymir\Plugin\Email\EmailClientInterface;
use Ymir\Plugin\Plugin;
use Ymir\Plugin\Support\Collection;
/**
* Pluggable functions used by the Ymir plugin.
*/
global $pagenow, $ymir;
if ($ymir->isEmailSendingEnabled() && function_exists('wp_mail') && !in_array($pagenow, ['plugins.php', 'update-core.php'], true)) {
add_filter('ymir_admin_notices', function ($notices) {
if ($notices instanceof Collection) {
$notices[] = [
'message' => 'Sending emails using SES is disabled because the "wp_mail" function was already overridden by another plugin.',
'type' => 'warning',
];
}
return $notices;
});
} elseif ($ymir->isEmailSendingEnabled() && !$ymir->isUsingVanityDomain() && !function_exists('wp_mail')) {
/**
* Send email using the cloud provider email client.
*/
function wp_mail($to, $subject, $message, $headers = '', $attachments = []): bool
{
try {
global $ymir;
if (!$ymir instanceof Plugin) {
throw new \RuntimeException('Ymir plugin isn\'t active');
}
$client = $ymir->getContainer()->offsetGet('email_client');
$email = $ymir->getContainer()->offsetGet('email');
if (!$client instanceof EmailClientInterface) {
throw new \RuntimeException('Unable to get the email client');
} elseif (!$email instanceof Email) {
throw new \RuntimeException('Unable to create an email object');
}
$attributes = apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments'));
$email->to($attributes['to'] ?? $to);
$email->subject($attributes['subject'] ?? $subject);
$email->body($attributes['message'] ?? $message);
$email->headers($attributes['headers'] ?? $headers);
$email->attachments($attributes['attachments'] ?? $attachments);
$client->sendEmail($email);
return true;
} catch (\Exception $exception) {
$errorData = compact('to', 'subject', 'message', 'headers', 'attachments');
if ($exception instanceof phpmailerException) {
$errorData['phpmailer_exception_code'] = $exception->getCode();
}
do_action('wp_mail_failed', new WP_Error('wp_mail_failed', $exception->getMessage(), $errorData));
return false;
}
}
}