-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_send_util_oof.php
78 lines (67 loc) · 2.5 KB
/
message_send_util_oof.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
<?php
/**
* Provides utility functions, related to the sending of email messages
*
* @category Plugin for Roundcube Webmail
* @author Stanimir Bozhilov <[email protected]>
* @license TODO
*/
class message_send_util_oof
{
/**
* A handler for the "message_before_send" hook
* We use it to turn a hidden JSON-LD from a "div" into a "script" tag
* (used when composing messages with structured data via our plugin)
*
* @param $args array The hook arguments that we receive
*
* @return array The (potentially) modified hook arguments
*/
public static function on_message_before_send_hook_oof($args)
{
// Add OOF structured data to the message if there's a vacation notice
$args = oof_util::add_oof_on_send($args);
// Move structured data from a hidden div to a script tag
$args = self::fix_structured_data_before_send_oof($args);
return $args;
}
/**
* Helper function for moving structured data from a hidden div to a script tag
*
* @param array $args The arguments of the "message_before_send" hook
*
* @return array The updated arguments of the "message_before_send" hook
*/
private static function fix_structured_data_before_send_oof($args)
{
$jsonDivOpeningTag = '<div id="jsonDivBeforeSend" style="display: none;">';
$jsonDivClosingTag = '</div>';
$message = $args['message'];
$htmlBody = $message->getHTMLBody();
// Extract the "div" that contains the hidden JSON-LD
$startDivTag = strpos($htmlBody, $jsonDivOpeningTag);
$endDivTag = strpos($htmlBody, $jsonDivClosingTag);
$extractedJsonLd = '';
if ($startDivTag && $endDivTag) {
$extractedJsonLd = substr(
$htmlBody,
$startDivTag + strlen($jsonDivOpeningTag),
$endDivTag - ($startDivTag + strlen($jsonDivOpeningTag))
);
}
// Turn the "div" with JSON-LD into a "script" tag with JSON-LD
if (isset($extractedJsonLd) && !empty($extractedJsonLd)) {
$htmlBody = str_replace(
$jsonDivOpeningTag . $extractedJsonLd . $jsonDivClosingTag,
'<script type="application/ld+json">'
. $extractedJsonLd
. '</script>',
$htmlBody
);
$message->setHTMLBody($htmlBody);
}
// Send the now modified message
$args['message'] = $message;
return $args;
}
}