forked from ymirapp/wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluggable.php
70 lines (55 loc) · 2.35 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
<?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;
/**
* Pluggable functions used by the Ymir plugin.
*/
global $pagenow, $ymir;
if ($ymir->isSesEnabled() && function_exists('wp_mail') && !in_array($pagenow, ['plugins.php', 'update-core.php'], true)) {
exit('"wp_mail" function already overridden by another plugin');
} elseif ($ymir->isSesEnabled() && !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;
}
}
}