@@ -443,13 +443,17 @@ private function formatMail($date, $photo, $object, $getImage = true, $extract =
443
443
}
444
444
$ image = false ;
445
445
if ($ getImage ) {
446
- $ image = $ this ->getImage ( $ object ->Body );
446
+ $ image = $ this ->getFirstImage (Gdn_Format:: to ( $ object ->Body , $ object -> Format ) );
447
447
}
448
448
449
449
// Get content extract.
450
450
$ extractText = false ;
451
451
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);
453
457
}
454
458
// Comment headline.
455
459
$ commentText = false ;
@@ -826,7 +830,7 @@ private function sendMessage($recipientUserID, $messages, $buttonAnchor = '') {
826
830
/**
827
831
* Return first embedded image in dicussion/comment body.
828
832
*
829
- * @param object $body standard
833
+ * @param string $body Html.
830
834
*
831
835
* @return string html to include image in notification (or empty string)
832
836
*/
@@ -841,7 +845,7 @@ public function getImage($body) {
841
845
return '' ;
842
846
}
843
847
$ image = substr ($ image , 0 , $ i );
844
-
848
+
845
849
$ imageUrl = $ image ;
846
850
$ i = stripos ($ imageUrl , "src= " );
847
851
if ($ i === false ) {
@@ -868,6 +872,35 @@ public function getImage($body) {
868
872
return $ imageUrl ;
869
873
}
870
874
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
+
871
904
/**
872
905
* Test method for ensuring successfull refactoring.
873
906
*
@@ -876,7 +909,7 @@ public function getImage($body) {
876
909
*
877
910
* @return void.
878
911
*/
879
- public function vanillaController_dnt_create ($ sender , $ args ) {
912
+ public function vanillaController_dnTestMessage_create ($ sender , $ args ) {
880
913
// Test data for formatMessage/formatMail
881
914
$ date = 'yesterday ' ; // notification related date.
882
915
$ photo = '' ; // originator photo.
@@ -889,6 +922,23 @@ public function vanillaController_dnt_create($sender, $args) {
889
922
decho ($ this ->formatMessage ($ date , $ photo , $ object , $ getImage , $ extract , $ headline , $ story ), 'message ' , true );
890
923
decho ($ this ->formatMail ($ date , $ photo , $ object , $ getImage , $ extract , $ headline , $ story ), 'mail ' , true );
891
924
}
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&utm_medium=cta&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
+ }
892
942
}
893
943
894
944
if (!function_exists ('touchConfig ' )) {
0 commit comments