Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRAoW committed Feb 12, 2018
0 parents commit d0e98d6
Show file tree
Hide file tree
Showing 14 changed files with 651 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/vendor/
15 changes: 15 additions & 0 deletions Config/config.php
@@ -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',
],
],
],
];
41 changes: 41 additions & 0 deletions EventListener/EmailSubscriber.php
@@ -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);
}
}
26 changes: 26 additions & 0 deletions Feed/Feed.php
@@ -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;
}

}
23 changes: 23 additions & 0 deletions Helpers/ParamsHelper.php
@@ -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;
}

}
18 changes: 18 additions & 0 deletions MauticRssToEmailBundle.php
@@ -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
{
}
63 changes: 63 additions & 0 deletions Parser/FeedParser.php
@@ -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;
}
}
79 changes: 79 additions & 0 deletions Parser/ItemTag.php
@@ -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;
}
}
71 changes: 71 additions & 0 deletions Parser/ItemsParser.php
@@ -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;
}
}

0 comments on commit d0e98d6

Please sign in to comment.