- Implements exclusion of duplicate posts from ALC
This commit is contained in:
@@ -82,6 +82,7 @@ class Initializer {
|
|||||||
$newsletter_option_fields = Env::$db_prefix . 'newsletter_option_fields';
|
$newsletter_option_fields = Env::$db_prefix . 'newsletter_option_fields';
|
||||||
$newsletter_option = Env::$db_prefix . 'newsletter_option';
|
$newsletter_option = Env::$db_prefix . 'newsletter_option';
|
||||||
$newsletter_links = Env::$db_prefix . 'newsletter_links';
|
$newsletter_links = Env::$db_prefix . 'newsletter_links';
|
||||||
|
$newsletter_posts = Env::$db_prefix . 'newsletter_posts';
|
||||||
$statistics_newsletters = Env::$db_prefix . 'statistics_newsletters';
|
$statistics_newsletters = Env::$db_prefix . 'statistics_newsletters';
|
||||||
$statistics_clicks = Env::$db_prefix . 'statistics_clicks';
|
$statistics_clicks = Env::$db_prefix . 'statistics_clicks';
|
||||||
$statistics_opens = Env::$db_prefix . 'statistics_opens';
|
$statistics_opens = Env::$db_prefix . 'statistics_opens';
|
||||||
@@ -101,6 +102,7 @@ class Initializer {
|
|||||||
define('MP_NEWSLETTER_SEGMENT_TABLE', $newsletter_segment);
|
define('MP_NEWSLETTER_SEGMENT_TABLE', $newsletter_segment);
|
||||||
define('MP_NEWSLETTER_OPTION_FIELDS_TABLE', $newsletter_option_fields);
|
define('MP_NEWSLETTER_OPTION_FIELDS_TABLE', $newsletter_option_fields);
|
||||||
define('MP_NEWSLETTER_LINKS_TABLE', $newsletter_links);
|
define('MP_NEWSLETTER_LINKS_TABLE', $newsletter_links);
|
||||||
|
define('MP_NEWSLETTER_POSTS_TABLE', $newsletter_posts);
|
||||||
define('MP_NEWSLETTER_OPTION_TABLE', $newsletter_option);
|
define('MP_NEWSLETTER_OPTION_TABLE', $newsletter_option);
|
||||||
define('MP_STATISTICS_NEWSLETTERS_TABLE', $statistics_newsletters);
|
define('MP_STATISTICS_NEWSLETTERS_TABLE', $statistics_newsletters);
|
||||||
define('MP_STATISTICS_CLICKS_TABLE', $statistics_clicks);
|
define('MP_STATISTICS_CLICKS_TABLE', $statistics_clicks);
|
||||||
|
@@ -23,6 +23,7 @@ class Migrator {
|
|||||||
'newsletter_option',
|
'newsletter_option',
|
||||||
'newsletter_segment',
|
'newsletter_segment',
|
||||||
'newsletter_links',
|
'newsletter_links',
|
||||||
|
'newsletter_posts',
|
||||||
'forms',
|
'forms',
|
||||||
'statistics_newsletters',
|
'statistics_newsletters',
|
||||||
'statistics_clicks',
|
'statistics_clicks',
|
||||||
@@ -253,6 +254,18 @@ class Migrator {
|
|||||||
return $this->sqlify(__FUNCTION__, $attributes);
|
return $this->sqlify(__FUNCTION__, $attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function newsletter_posts() {
|
||||||
|
$attributes = array(
|
||||||
|
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||||
|
'newsletter_id mediumint(9) NOT NULL,',
|
||||||
|
'post_id mediumint(9) NOT NULL,',
|
||||||
|
'created_at TIMESTAMP NOT NULL DEFAULT 0,',
|
||||||
|
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||||
|
'PRIMARY KEY (id)',
|
||||||
|
);
|
||||||
|
return $this->sqlify(__FUNCTION__, $attributes);
|
||||||
|
}
|
||||||
|
|
||||||
function forms() {
|
function forms() {
|
||||||
$attributes = array(
|
$attributes = array(
|
||||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||||
|
12
lib/Models/NewsletterPost.php
Normal file
12
lib/Models/NewsletterPost.php
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Models;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class NewsletterPost extends Model {
|
||||||
|
public static $_table = MP_NEWSLETTER_POSTS_TABLE;
|
||||||
|
|
||||||
|
function __construct() {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
}
|
@@ -1,12 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Newsletter;
|
namespace MailPoet\Newsletter;
|
||||||
|
|
||||||
|
use MailPoet\Models\NewsletterPost;
|
||||||
use MailPoet\Newsletter\Editor\Transformer;
|
use MailPoet\Newsletter\Editor\Transformer;
|
||||||
|
use MailPoet\Util\Helpers;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class AutomatedLatestContent {
|
class AutomatedLatestContent {
|
||||||
function getPosts($args) {
|
function getPosts($args, $newsletter_id = false, $posts_to_exclude = array()) {
|
||||||
|
if($newsletter_id) {
|
||||||
|
$existing_posts = NewsletterPost::where('newsletter_id', $newsletter_id)
|
||||||
|
->findArray();
|
||||||
|
if(count($existing_posts)) {
|
||||||
|
$existing_posts = Helpers::arrayColumn($existing_posts, 'post_id');
|
||||||
|
$posts_to_exclude = array_merge($posts_to_exclude, $existing_posts);
|
||||||
|
}
|
||||||
|
}
|
||||||
$parameters = array(
|
$parameters = array(
|
||||||
'posts_per_page' => (isset($args['amount'])) ? (int) $args['amount'] : 10,
|
'posts_per_page' => (isset($args['amount'])) ? (int) $args['amount'] : 10,
|
||||||
'post_type' => (isset($args['contentType'])) ? $args['contentType'] : 'post',
|
'post_type' => (isset($args['contentType'])) ? $args['contentType'] : 'post',
|
||||||
@@ -21,7 +31,10 @@ class AutomatedLatestContent {
|
|||||||
$parameters['post__in'] = $args['posts'];
|
$parameters['post__in'] = $args['posts'];
|
||||||
}
|
}
|
||||||
$parameters['tax_query'] = $this->constructTaxonomiesQuery($args);
|
$parameters['tax_query'] = $this->constructTaxonomiesQuery($args);
|
||||||
return get_posts($parameters);
|
$WP_posts = array_map(function($post) use ($posts_to_exclude) {
|
||||||
|
return (!in_array($post->ID, $posts_to_exclude)) ? $post : false;
|
||||||
|
}, get_posts($parameters));
|
||||||
|
return array_filter($WP_posts);
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformPosts($args, $posts) {
|
function transformPosts($args, $posts) {
|
||||||
|
@@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace MailPoet\Newsletter\Renderer\Blocks;
|
|
||||||
|
|
||||||
class AutomatedLatestContent {
|
|
||||||
static function render($element, $column_count) {
|
|
||||||
$ALC = new \MailPoet\Newsletter\AutomatedLatestContent();
|
|
||||||
$posts = $ALC->getPosts($element);
|
|
||||||
$transformed_posts = array('blocks' => $ALC->transformPosts($element, $posts));
|
|
||||||
$renderer = new Renderer();
|
|
||||||
return $renderer->render($transformed_posts, $column_count);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -4,6 +4,15 @@ namespace MailPoet\Newsletter\Renderer\Blocks;
|
|||||||
use MailPoet\Newsletter\Renderer\StylesHelper;
|
use MailPoet\Newsletter\Renderer\StylesHelper;
|
||||||
|
|
||||||
class Renderer {
|
class Renderer {
|
||||||
|
public $newsletter;
|
||||||
|
public $posts;
|
||||||
|
|
||||||
|
function __construct($newsletter, $posts = false) {
|
||||||
|
$this->newsletter = $newsletter;
|
||||||
|
$this->posts = array();
|
||||||
|
$this->ALC = new \MailPoet\Newsletter\AutomatedLatestContent();
|
||||||
|
}
|
||||||
|
|
||||||
function render($data, $column_count) {
|
function render($data, $column_count) {
|
||||||
$block_content = '';
|
$block_content = '';
|
||||||
array_map(function($block) use (&$block_content, &$columns, $column_count) {
|
array_map(function($block) use (&$block_content, &$columns, $column_count) {
|
||||||
@@ -20,8 +29,37 @@ class Renderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createElementFromBlockType($block, $column_count) {
|
function createElementFromBlockType($block, $column_count) {
|
||||||
|
if ($block['type'] === 'automatedLatestContent') {
|
||||||
|
$content = $this->processAutomatedLatestContent($block, $column_count);
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
$block = StylesHelper::applyTextAlignment($block);
|
$block = StylesHelper::applyTextAlignment($block);
|
||||||
$block_class = __NAMESPACE__ . '\\' . ucfirst($block['type']);
|
$block_class = __NAMESPACE__ . '\\' . ucfirst($block['type']);
|
||||||
return (class_exists($block_class)) ? $block_class::render($block, $column_count) : '';
|
if (!class_exists($block_class)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return $block_class::render($block, $column_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
function processAutomatedLatestContent($args, $column_count) {
|
||||||
|
$posts_to_exclude = $this->getPosts();
|
||||||
|
$ALCPosts = $this->ALC->getPosts($args, $this->newsletter['id'], $posts_to_exclude);
|
||||||
|
foreach($ALCPosts as $post) {
|
||||||
|
$posts_to_exclude[] = $post->ID;
|
||||||
|
}
|
||||||
|
$transformed_posts = array(
|
||||||
|
'blocks' => $this->ALC->transformPosts($args, $ALCPosts)
|
||||||
|
);
|
||||||
|
$this->setPosts($posts_to_exclude);
|
||||||
|
$rendered_posts = $this->render($transformed_posts, $column_count);
|
||||||
|
return $rendered_posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPosts() {
|
||||||
|
return $this->posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setPosts($posts) {
|
||||||
|
return $this->posts = $posts;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -4,35 +4,31 @@ namespace MailPoet\Newsletter\Renderer;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class Renderer {
|
class Renderer {
|
||||||
public $template = 'Template.html';
|
|
||||||
public $blocks_renderer;
|
public $blocks_renderer;
|
||||||
public $columns_renderer;
|
public $columns_renderer;
|
||||||
public $DOM_parser;
|
public $DOM_parser;
|
||||||
public $CSS_inliner;
|
public $CSS_inliner;
|
||||||
public $newsletter;
|
public $newsletter;
|
||||||
|
const NEWSLETTER_TEMPLATE = 'Template.html';
|
||||||
|
|
||||||
function __construct($newsletter) {
|
function __construct(array $newsletter) {
|
||||||
$this->blocks_renderer = new Blocks\Renderer();
|
$this->newsletter = $newsletter;
|
||||||
|
$this->blocks_renderer = new Blocks\Renderer($this->newsletter);
|
||||||
$this->columns_renderer = new Columns\Renderer();
|
$this->columns_renderer = new Columns\Renderer();
|
||||||
$this->DOM_parser = new \pQuery();
|
$this->DOM_parser = new \pQuery();
|
||||||
$this->CSS_inliner = new \MailPoet\Util\CSS();
|
$this->CSS_inliner = new \MailPoet\Util\CSS();
|
||||||
$this->newsletter = $newsletter;
|
$this->template = file_get_contents(dirname(__FILE__) . '/' . self::NEWSLETTER_TEMPLATE);
|
||||||
$this->template = file_get_contents(dirname(__FILE__) . '/' . $this->template);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
$newsletter_data = (is_array($this->newsletter['body'])) ?
|
$newsletter = $this->newsletter;
|
||||||
$this->newsletter['body'] :
|
$rendered_body = $this->renderBody($newsletter['body']['content']);
|
||||||
json_decode($this->newsletter['body'], true);
|
$rendered_styles = $this->renderStyles($newsletter['body']['globalStyles']);
|
||||||
$newsletter_body = $this->renderBody($newsletter_data['content']);
|
|
||||||
$newsletter_styles = $this->renderStyles($newsletter_data['globalStyles']);
|
|
||||||
$newsletter_subject = $this->newsletter['subject'];
|
|
||||||
$newsletter_preheader = $this->newsletter['preheader'];
|
|
||||||
$template = $this->injectContentIntoTemplate($this->template, array(
|
$template = $this->injectContentIntoTemplate($this->template, array(
|
||||||
$newsletter_subject,
|
$newsletter['subject'],
|
||||||
$newsletter_styles,
|
$rendered_styles,
|
||||||
$newsletter_preheader,
|
$newsletter['preheader'],
|
||||||
$newsletter_body
|
$rendered_body
|
||||||
));
|
));
|
||||||
$template = $this->inlineCSSStyles($template);
|
$template = $this->inlineCSSStyles($template);
|
||||||
$template = $this->postProcessTemplate($template);
|
$template = $this->postProcessTemplate($template);
|
||||||
@@ -43,7 +39,7 @@ class Renderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function renderBody($content) {
|
function renderBody($content) {
|
||||||
$content = array_map(function($content_block) {
|
$rendered_content = array_map(function($content_block) {
|
||||||
$column_count = count($content_block['blocks']);
|
$column_count = count($content_block['blocks']);
|
||||||
$column_data = $this->blocks_renderer->render(
|
$column_data = $this->blocks_renderer->render(
|
||||||
$content_block,
|
$content_block,
|
||||||
@@ -55,7 +51,7 @@ class Renderer {
|
|||||||
$column_data
|
$column_data
|
||||||
);
|
);
|
||||||
}, $content['blocks']);
|
}, $content['blocks']);
|
||||||
return implode('', $content);
|
return implode('', $rendered_content);
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderStyles($styles) {
|
function renderStyles($styles) {
|
||||||
@@ -80,9 +76,9 @@ class Renderer {
|
|||||||
return $css;
|
return $css;
|
||||||
}
|
}
|
||||||
|
|
||||||
function injectContentIntoTemplate($template, $data) {
|
function injectContentIntoTemplate($template, $content) {
|
||||||
return preg_replace_callback('/{{\w+}}/', function($matches) use (&$data) {
|
return preg_replace_callback('/{{\w+}}/', function($matches) use (&$content) {
|
||||||
return array_shift($data);
|
return array_shift($content);
|
||||||
}, $template);
|
}, $template);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user