-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
23 changed files
with
585 additions
and
91 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 |
---|---|---|
@@ -1,4 +1,16 @@ | ||
# ryssbowh/craft-emails Changelog | ||
|
||
## 1.0.1 - 09/01/2022 | ||
|
||
### Fixed | ||
- Compress email logs setting | ||
- Email duplication in email shots | ||
|
||
### Added | ||
- "View emails" button on email shots dashboard | ||
- Mailchimp lists integration through API | ||
|
||
## 1.0.0 - 08/01/2022 | ||
|
||
### Added | ||
- Initial release |
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
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
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
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
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,29 @@ | ||
<?php | ||
|
||
namespace Ryssbowh\CraftEmails\Models; | ||
|
||
use yii\base\DynamicModel; | ||
|
||
class MailchimpList extends DynamicModel | ||
{ | ||
/** | ||
* @var MailchimpMember[] | ||
*/ | ||
public $members = []; | ||
|
||
/** | ||
* Get all subscribed members emails of this list | ||
* | ||
* @return string[] | ||
*/ | ||
public function getEmails(): array | ||
{ | ||
$emails = []; | ||
foreach ($this->members as $member) { | ||
if ($member->status == 'subscribed') { | ||
$emails[$member->email_address] = $member->full_name; | ||
} | ||
} | ||
return $emails; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace Ryssbowh\CraftEmails\Models; | ||
|
||
use yii\base\DynamicModel; | ||
|
||
class MailchimpMember extends DynamicModel | ||
{ | ||
} |
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
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
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,104 @@ | ||
<?php | ||
|
||
namespace Ryssbowh\CraftEmails\Services; | ||
|
||
use DrewM\MailChimp\MailChimp; | ||
use Ryssbowh\CraftEmails\Emails; | ||
use Ryssbowh\CraftEmails\Models\MailchimpList; | ||
use Ryssbowh\CraftEmails\Models\MailchimpMember; | ||
use Ryssbowh\CraftEmails\exceptions\MailchimpException; | ||
use craft\base\Component; | ||
|
||
class MailchimpService extends Component | ||
{ | ||
const CACHE_KEY = 'emails.mailchimp_lists'; | ||
|
||
/** | ||
* @var MailChimp | ||
*/ | ||
protected $_mailchimp; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $_lists; | ||
|
||
/** | ||
* Is the api up and running | ||
* | ||
* @return boolean | ||
*/ | ||
public function isEnabled(): bool | ||
{ | ||
return !is_null($this->api); | ||
} | ||
|
||
/** | ||
* Clear mailchimp caches | ||
*/ | ||
public function clearCaches() | ||
{ | ||
\Craft::$app->cache->delete(self::CACHE_KEY); | ||
} | ||
|
||
/** | ||
* Get all lists | ||
* | ||
* @return array | ||
*/ | ||
public function getLists(): array | ||
{ | ||
if (!$this->isEnabled()) { | ||
return []; | ||
} | ||
if ($this->_lists === null) { | ||
$cached = \Craft::$app->cache->get(self::CACHE_KEY); | ||
if ($cached === false) { | ||
$lists = $this->api->get('lists'); | ||
$cached = []; | ||
foreach ($lists['lists'] ?? [] as $attributes) { | ||
$list = new MailchimpList($attributes); | ||
$list->members = []; | ||
$details = $this->api->get('lists/' . $list->id . '/members'); | ||
foreach ($details['members'] as $member) { | ||
$list->members[] = new MailchimpMember($member); | ||
} | ||
$cached[$attributes['id']] = $list; | ||
} | ||
$duration = Emails::$plugin->settings->mailchimpCacheDuration; | ||
\Craft::$app->cache->set(self::CACHE_KEY, $cached, $duration * 60); | ||
} | ||
$this->_lists = $cached; | ||
} | ||
return $this->_lists; | ||
} | ||
|
||
/** | ||
* Get a list by id | ||
* | ||
* @param string $id | ||
* @return MailchimpList | ||
*/ | ||
public function getList(string $id): MailchimpList | ||
{ | ||
if (isset($this->lists[$id])) { | ||
return $this->lists[$id]; | ||
} | ||
throw MailchimpException::noList($id); | ||
} | ||
|
||
/** | ||
* Get api instance | ||
* | ||
* @return ?MailChimp | ||
*/ | ||
public function getApi(): ?MailChimp | ||
{ | ||
if (is_null($this->_mailchimp)) { | ||
if (Emails::$plugin->settings->mailchimpApiKey) { | ||
$this->_mailchimp = new MailChimp(\Craft::parseEnv(Emails::$plugin->settings->mailchimpApiKey)); | ||
} | ||
} | ||
return $this->_mailchimp; | ||
} | ||
} |
Oops, something went wrong.