Skip to content

Commit 88a4cc4

Browse files
committed
Implement a getFirstImage method
1 parent 5befdad commit 88a4cc4

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

DelayedNotificationsPlugin.php

+55-5
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,17 @@ private function formatMail($date, $photo, $object, $getImage = true, $extract =
443443
}
444444
$image = false;
445445
if ($getImage) {
446-
$image = $this->getImage($object->Body);
446+
$image = $this->getFirstImage(Gdn_Format::to($object->Body, $object->Format));
447447
}
448448

449449
// Get content extract.
450450
$extractText = false;
451451
if ($extract) {
452-
$extractText = $this->getExtract($object->Body, $extract);
452+
$extractText = sliceString(
453+
Gdn::formatService()->renderExcerpt($object->Body, $object->Format),
454+
$exract
455+
);
456+
// $extractText = $this->getExtract($body, $extract);
453457
}
454458
// Comment headline.
455459
$commentText = false;
@@ -826,7 +830,7 @@ private function sendMessage($recipientUserID, $messages, $buttonAnchor = '') {
826830
/**
827831
* Return first embedded image in dicussion/comment body.
828832
*
829-
* @param object $body standard
833+
* @param string $body Html.
830834
*
831835
* @return string html to include image in notification (or empty string)
832836
*/
@@ -841,7 +845,7 @@ public function getImage($body) {
841845
return '';
842846
}
843847
$image = substr($image, 0, $i);
844-
848+
845849
$imageUrl = $image;
846850
$i = stripos($imageUrl, "src=");
847851
if ($i === false) {
@@ -868,6 +872,35 @@ public function getImage($body) {
868872
return $imageUrl;
869873
}
870874

875+
/**
876+
* Return url of first embedded image.
877+
*
878+
* @param string $body Html.
879+
*
880+
* @return string|false The url of the first image of false.
881+
*/
882+
public function getFirstImage($body) {
883+
// Query body for images.
884+
$dom = new DomDocument;
885+
$dom->loadHTML($body);
886+
$xpath = new DomXPath($dom);
887+
$images = $xpath->query("//img");
888+
// Return if there are no images in body.
889+
if (count($images) == 0) {
890+
return false;
891+
}
892+
// Get image url and size.
893+
$imageUrl = $images->item(0)->getAttribute('src');
894+
list($width, $height) = getimagesize($imageUrl);
895+
$minImageSize = Gdn::config('Plugins.DelayedNotifications.MinImageSize', 20);
896+
// Ignore small images.
897+
if ($width < $minImageSize || $height < $minImageSize) {
898+
return false;
899+
}
900+
901+
return $imageUrl;
902+
}
903+
871904
/**
872905
* Test method for ensuring successfull refactoring.
873906
*
@@ -876,7 +909,7 @@ public function getImage($body) {
876909
*
877910
* @return void.
878911
*/
879-
public function vanillaController_dnt_create($sender, $args) {
912+
public function vanillaController_dnTestMessage_create($sender, $args) {
880913
// Test data for formatMessage/formatMail
881914
$date = 'yesterday'; // notification related date.
882915
$photo = ''; // originator photo.
@@ -889,6 +922,23 @@ public function vanillaController_dnt_create($sender, $args) {
889922
decho($this->formatMessage($date, $photo, $object, $getImage, $extract, $headline, $story), 'message', true);
890923
decho($this->formatMail($date, $photo, $object, $getImage, $extract, $headline, $story), 'mail', true);
891924
}
925+
926+
public function vanillaController_dnTestImage_create($ender) {
927+
$body = '<header class="Header Header-branding">
928+
<div class="Container">
929+
<a href="/" class="Header-logo">
930+
<img src="https://us.v-cdn.net/5018160/uploads/fb3af9601a44d13eb2b42f9e02fe924b.png" alt="Vanilla Forums">
931+
</a>
932+
<div class="Header-spacer"></div>
933+
<a class="Header-brandLink" href="https://blog.vanillaforums.com">Blog</a>
934+
<a class="Header-brandLink" href="https://docs.vanillaforums.com">Documentation</a>
935+
<a class="Header-cta" href="http://pages.vanillaforums.com/demo-request-vanilla-forums?utm_source=vanilladocs&amp;utm_medium=cta&amp;utm_campaign=demo-request">Try Vanilla Cloud</a>
936+
</div>
937+
</header>';
938+
939+
decho($this->getImage($body), 'image', true);
940+
decho($this->getFirstImage($body), 'first image', true);
941+
}
892942
}
893943

894944
if (!function_exists('touchConfig')) {

views/mail.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<col />
3232
</colgroup>
3333
<tr>
34-
<?php if($image): ?>
34+
<?php if ($image): ?>
3535
<td width="78px" valign="top" align="right">
3636
<span style="border-radius: 4px;padding: 0px 5px;vertical-align: top;display: table-cell;">
3737
<img width="120px" style="display:block; border-radius:6px; border:solid 1px rgba(0,0,0,.08);vertical-align: top;" src="<?= $image ?>" />

0 commit comments

Comments
 (0)