| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace cx_newsletter;
- class cron_emails_sender
- extends service
- implements service_interface {
- public function prepare() : self {
- $this->campaigns_repository = new campaigns_repository(
- $this->get_database(),
- $this->get_tables()
- );
- $this->send_log_repository = new send_log_repository(
- $this->get_database(),
- $this->get_tables()
- );
-
- $this->customers_mapper = new customers_mapper(
- $this->get_database(),
- $this->get_tables()
- );
- $this->messages_mapper = new messages_mapper(
- $this->get_database(),
- $this->get_tables()
- );
-
- return $this;
- }
- public function process() : self {
- $next_campaign = $this->campaigns_repository->load_next(
- campaign_type::email
- );
- if ($next_campaign === null) {
- return $this;
- }
- $customers = $this->send_log_repository->load_all(
- $next_campaign,
- $this->get_settings()->get('email_count'),
- false
- );
- if (empty($customers)) {
- $next_campaign->finalize();
- $this->campaigns_repository->save($next_campaign);
- return $this->process();
- }
- foreach ($customers as $customer) {
- if ($this->send($customer, $next_campaign->message)) {
- $this->send_log_repository->mark_send($next_campaign, $customer);
- }
- }
- return $this;
- }
- private function send(customer $to, message $what) : bool {
- if (empty($to->email)) {
- return false;
- }
- if (!$what->is_complete()) {
- return false;
- }
- $headers = [];
- $headers['From'] = $this->get_settings()->get('source_address');
- $headers['Reply-To'] = $this->get_settings()->get('reply_to_address');
- $headers['MIME-Version'] = '1.0';
- $headers['Content-Type'] = 'text/html';
-
- return \wp_mail($to->email, $what->title, $what->content, $headers);
- }
- private campaigns_repository $campaigns_repository;
- private send_log_repository $send_log_repository;
- private customers_mapper $customers_mapper;
- private messages_mapper $messages_mapper;
- private campaign $what;
- }
|