Merge pull request #1262 from mailpoet/recently-sent-templates

limitting the recently sent templates to 12 [MAILPOET-1265]
This commit is contained in:
stoletniy
2018-02-13 11:28:42 +03:00
committed by GitHub
3 changed files with 53 additions and 0 deletions

View File

@@ -6,6 +6,9 @@ if(!defined('ABSPATH')) exit;
class NewsletterTemplate extends Model {
public static $_table = MP_NEWSLETTER_TEMPLATES_TABLE;
const RECENTLY_SENT_CATEGORIES = '["recent"]';
const RECENTLY_SENT_COUNT = 12;
function __construct() {
parent::__construct();
@@ -17,6 +20,22 @@ class NewsletterTemplate extends Model {
));
}
static function cleanRecentlySent($data) {
if(!empty($data['categories']) && $data['categories'] === self::RECENTLY_SENT_CATEGORIES) {
$ids = parent::where('categories', self::RECENTLY_SENT_CATEGORIES)
->select('id')
->orderByDesc('id')
->limit(self::RECENTLY_SENT_COUNT)
->findMany();
$ids = array_map(function ($template) {
return $template->id;
}, $ids);
parent::where('categories', self::RECENTLY_SENT_CATEGORIES)
->whereNotIn('id', $ids)
->deleteMany();
}
}
function asArray() {
$template = parent::asArray();
if(isset($template['body'])) {