Add automated emails subjects to segments response

[MAILPOET-2352]
This commit is contained in:
Ján Mikláš
2019-09-18 12:33:41 +02:00
committed by Jack Kitterhing
parent d43ae6d350
commit dad381c74d
2 changed files with 26 additions and 0 deletions

View File

@ -58,6 +58,7 @@ class Segments extends APIEndpoint {
$data[] = $segment
->withSubscribersCount()
->withAutomatedEmailsSubjects()
->asArray();
}

View File

@ -1,12 +1,14 @@
<?php
namespace MailPoet\Models;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Entities\SegmentEntity;
use MailPoet\WP\Functions as WPFunctions;
use MailPoet\WooCommerce\Helper as WCHelper;
/**
* @property array $subscribers_count
* @property array $automated_emails_subjects
* @property string $name
* @property string $type
* @property string $description
@ -119,6 +121,29 @@ class Segment extends Model {
return $this;
}
function withAutomatedEmailsSubjects() {
$automated_emails = NewsletterSegment::tableAlias('relation')
->where('relation.segment_id', $this->id)
->join(
MP_NEWSLETTERS_TABLE,
'newsletters.id = relation.newsletter_id',
'newsletters'
)
->whereIn('newsletters.type', [
NewsletterEntity::TYPE_AUTOMATIC,
NewsletterEntity::TYPE_WELCOME,
NewsletterEntity::TYPE_NOTIFICATION,
])
->select('newsletters.subject')
->findMany();
$this->automated_emails_subjects = array_map(function($email) {
return $email->subject;
}, $automated_emails);
return $this;
}
static function getWPSegment() {
$wp_segment = self::where('type', self::TYPE_WP_USERS)->findOne();