Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d0e98d6
Showing
14 changed files
with
651 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
return [ | ||
'name' => 'RSS To Email', | ||
'description' => 'Enables RSS feed in mail', | ||
'version' => '1.0', | ||
'author' => 'Right Amount of Weird', | ||
'services' => [ | ||
'events' => [ | ||
'mautic.plugin.rsstoemail.subscriber' => [ | ||
'class' => 'MauticPlugin\MauticRssToEmailBundle\EventListener\EmailSubscriber', | ||
], | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
namespace MauticPlugin\MauticRssToEmailBundle\EventListener; | ||
|
||
use MauticPlugin\MauticRssToEmailBundle\Parser\Parser; | ||
use Mautic\CoreBundle\EventListener\CommonSubscriber; | ||
use Mautic\EmailBundle\EmailEvents; | ||
use Mautic\EmailBundle\Event\EmailSendEvent; | ||
|
||
/** | ||
* Class EmailSubscriber | ||
*/ | ||
class EmailSubscriber extends CommonSubscriber | ||
{ | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
EmailEvents::EMAIL_ON_SEND => array('onEmailGenerate', 300), | ||
EmailEvents::EMAIL_ON_DISPLAY => array('onEmailGenerate', 300), | ||
); | ||
} | ||
|
||
/** | ||
* Search and replace tokens with content | ||
* | ||
* @param EmailSendEvent $event | ||
*/ | ||
public function onEmailGenerate(EmailSendEvent $event) | ||
{ | ||
// // Get content | ||
$content = $event->getContent(); | ||
|
||
$parser = new Parser($content); | ||
$content = $parser->getContent(); | ||
|
||
$event->setContent($content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
namespace MauticPlugin\MauticRssToEmailBundle\Feed; | ||
|
||
use SimplePie; | ||
|
||
class Feed | ||
{ | ||
protected $feed; | ||
|
||
public function __construct($url) | ||
{ | ||
$feed = new SimplePie(); | ||
$feed->set_feed_url($url); | ||
$feed->enable_cache(false); | ||
$feed->init(); | ||
$feed->handle_content_type(); | ||
|
||
$this->feed = $feed; | ||
} | ||
|
||
public function getFeed() | ||
{ | ||
return $this->feed; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
namespace MauticPlugin\MauticRssToEmailBundle\Helpers; | ||
|
||
class ParamsHelper | ||
{ | ||
public static function parse($paramsString) | ||
{ | ||
$params = []; | ||
preg_match_all('/ ?([^=]+)="([^"]+)"/', $paramsString, $paramMatches); | ||
|
||
if (!empty($paramMatches[1])) { | ||
foreach ($paramMatches[1] as $index => $paramKey) { | ||
$key = $paramKey; | ||
$value = $paramMatches[2][$index]; | ||
|
||
$params[$key] = $value; | ||
} | ||
} | ||
|
||
return $params; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* @copyright 2018 Chris Schrijver. All rights reserved | ||
* @author Chris Schrijver | ||
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
namespace MauticPlugin\MauticRssToEmailBundle; | ||
|
||
use Mautic\PluginBundle\Bundle\PluginBundleBase; | ||
|
||
/** | ||
* Class MauticRssToEmailBundle. | ||
*/ | ||
class MauticRssToEmailBundle extends PluginBundleBase | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
namespace MauticPlugin\MauticRssToEmailBundle\Parser; | ||
|
||
use MauticPlugin\MauticRssToEmailBundle\Traits\ParamsTrait; | ||
|
||
class FeedParser | ||
{ | ||
use ParamsTrait; | ||
|
||
public $feed; | ||
protected $content; | ||
|
||
public function __construct($content, $feed) | ||
{ | ||
$this->feed = $feed; | ||
|
||
$content = $this->parseInfo($content); | ||
|
||
$itemsParser = new ItemsParser($content, $this->feed); | ||
$content = $itemsParser->getContent(); | ||
|
||
$this->setContent($content); | ||
} | ||
|
||
public function parseInfo($content) | ||
{ | ||
preg_match_all('/{feedinfo:([^}]+)}/', $content, $tags); | ||
|
||
$feed = $this->feed->getFeed(); | ||
|
||
if (!empty($tags[1])) { | ||
foreach ($tags[1] as $tagIndex => $tag) { | ||
$value = "Unknown ({$tag})"; | ||
|
||
switch ($tag) { | ||
case 'title': | ||
$value = $feed->get_title(); | ||
break; | ||
case 'url': | ||
$value = $feed->feed_url; | ||
break; | ||
case 'description': | ||
$value = $feed->get_description(); | ||
break; | ||
} | ||
|
||
$content = str_replace($tags[0][$tagIndex], $value, $content); | ||
} | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
public function setContent($content) | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
public function getContent() | ||
{ | ||
return $this->content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
namespace MauticPlugin\MauticRssToEmailBundle\Parser; | ||
|
||
use MauticPlugin\MauticRssToEmailBundle\Traits\ParamsTrait; | ||
|
||
class ItemTag | ||
{ | ||
use ParamsTrait; | ||
|
||
public $tag; | ||
public $feedItem; | ||
|
||
public function __construct($tag, $params, $feedItem) | ||
{ | ||
$this->tag = $tag; | ||
$this->setParams($params); | ||
|
||
$this->feedItem = $feedItem; | ||
} | ||
|
||
public function getValue() | ||
{ | ||
$feedItem = $this->feedItem; | ||
|
||
$value = "Unknown ({$this->tag})"; | ||
|
||
switch ($this->tag) { | ||
case 'title': | ||
$value = $feedItem->get_title(); | ||
break; | ||
case 'content': | ||
$value = $feedItem->get_description(); | ||
break; | ||
case 'content_full': | ||
$value = $feedItem->get_content(true); | ||
break; | ||
case 'content_text': | ||
$value = strip_tags($feedItem->get_description()); | ||
break; | ||
case 'content_full_text': | ||
$value = strip_tags($feedItem->get_content(true)); | ||
break; | ||
case 'description': | ||
$value = $feedItem->get_description(); | ||
break; | ||
case 'date': | ||
$format = 'j F Y, g:i a'; | ||
if (!empty($this->getParam('format'))) { | ||
$format = $this->getParam('format'); | ||
} | ||
|
||
$value = $feedItem->get_date($format); | ||
break; | ||
case 'author': | ||
$author = $feedItem->get_author(); | ||
$value = ''; | ||
|
||
if (!is_null($author)) { | ||
$value = $author->get_link(); | ||
} | ||
|
||
$value = $feedItem->get_author()->get_name(); | ||
break; | ||
case 'categories': | ||
$value = implode(', ', $feedItem->get_categories()); | ||
break; | ||
case 'image': | ||
$enclosure = $feedItem->get_enclosure(); | ||
$value = ''; | ||
|
||
if (!is_null($enclosure)) { | ||
$value = $enclosure->get_link(); | ||
} | ||
break; | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
namespace MauticPlugin\MauticRssToEmailBundle\Parser; | ||
|
||
use MauticPlugin\MauticRssToEmailBundle\Helpers\ParamsHelper; | ||
use MauticPlugin\MauticRssToEmailBundle\Traits\ParamsTrait; | ||
|
||
class ItemsParser | ||
{ | ||
use ParamsTrait; | ||
|
||
protected $content; | ||
|
||
public function __construct($content, $feed) | ||
{ | ||
preg_match('/{feeditems([^}]*)}(.*){\/feeditems}/ms', $content, $feedItemMatches); | ||
|
||
if (!empty($feedItemMatches)) { | ||
$params = $this->parseParams($feedItemMatches[1]); | ||
$itemTemplate = $feedItemMatches[2]; | ||
|
||
$itemsContent = ''; | ||
|
||
$maxImages = $this->getParam('count'); | ||
|
||
$item_i = 0; | ||
foreach ($feed->getFeed()->get_items() as $feedItem) { | ||
if (!empty($maxImages) && $maxImages == $item_i) break; | ||
|
||
$itemsContent .= $this->parseItem($itemTemplate, $feedItem); | ||
|
||
$item_i++; | ||
} | ||
|
||
$content = str_replace($feedItemMatches[0], $itemsContent, $content); | ||
} | ||
|
||
$this->setContent($content); | ||
} | ||
|
||
public function parseItem($itemTemplate, $feedItem) | ||
{ | ||
preg_match_all('/{feeditem:([^ }]+)( [^}]+)?}/', $itemTemplate, $tags); | ||
|
||
if (!empty($tags[1])) { | ||
|
||
foreach ($tags[1] as $tagIndex => $tag) { | ||
$params = []; | ||
|
||
if (isset($tags[2][$tagIndex]) && !empty(trim($tags[2][$tagIndex]))) { | ||
$params = ParamsHelper::parse($tags[2][$tagIndex]); | ||
} | ||
|
||
$itemTag = new ItemTag($tag, $params, $feedItem); | ||
|
||
$itemTemplate = str_replace($tags[0][$tagIndex], $itemTag->getValue(), $itemTemplate); | ||
} | ||
} | ||
|
||
return $itemTemplate; | ||
} | ||
|
||
public function setContent($content) | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
public function getContent() | ||
{ | ||
return $this->content; | ||
} | ||
} |
Oops, something went wrong.