MailerLog.php
120 lines
| 1 | <?php |
| 2 | namespace MailPoet\Mailer; |
| 3 | |
| 4 | use MailPoet\Models\Setting; |
| 5 | |
| 6 | if(!defined('ABSPATH')) exit; |
| 7 | |
| 8 | class MailerLog { |
| 9 | const SETTING_NAME = 'mta_log'; |
| 10 | const STATUS_PAUSED = 'paused'; |
| 11 | const RETRY_ATTEMPTS_LIMIT = 3; |
| 12 | const RETRY_INTERVAL = 120; // seconds |
| 13 | |
| 14 | static function getMailerLog($mailer_log = false) { |
| 15 | if($mailer_log) return $mailer_log; |
| 16 | $mailer_log = Setting::getValue(self::SETTING_NAME); |
| 17 | if(!$mailer_log) { |
| 18 | $mailer_log = self::createMailerLog(); |
| 19 | } |
| 20 | return $mailer_log; |
| 21 | } |
| 22 | |
| 23 | static function createMailerLog() { |
| 24 | $mailer_log = array( |
| 25 | 'sent' => null, |
| 26 | 'started' => time(), |
| 27 | 'status' => null, |
| 28 | 'retry_attempt' => null, |
| 29 | 'retry_at' => null, |
| 30 | 'error' => null |
| 31 | ); |
| 32 | Setting::setValue(self::SETTING_NAME, $mailer_log); |
| 33 | return $mailer_log; |
| 34 | } |
| 35 | |
| 36 | static function resetMailerLog() { |
| 37 | return self::createMailerLog(); |
| 38 | } |
| 39 | |
| 40 | static function updateMailerLog($mailer_log) { |
| 41 | Setting::setValue(self::SETTING_NAME, $mailer_log); |
| 42 | return $mailer_log; |
| 43 | } |
| 44 | |
| 45 | static function enforceExecutionRequirements($mailer_log = false) { |
| 46 | $mailer_log = self::getMailerLog($mailer_log); |
| 47 | if($mailer_log['retry_attempt'] === self::RETRY_ATTEMPTS_LIMIT) { |
| 48 | $mailer_log = self::pauseSending($mailer_log); |
| 49 | } |
| 50 | if($mailer_log['status'] === self::STATUS_PAUSED) { |
| 51 | throw new \Exception(__('Sending has been paused.', 'mailpoet')); |
| 52 | } |
| 53 | if(!is_null($mailer_log['retry_at'])) { |
| 54 | if(time() <= $mailer_log['retry_at']) { |
| 55 | throw new \Exception(__('Sending is waiting to be retried.', 'mailpoet')); |
| 56 | } else { |
| 57 | $mailer_log['retry_at'] = null; |
| 58 | self::updateMailerLog($mailer_log); |
| 59 | } |
| 60 | } |
| 61 | // ensure that sending frequency has not been reached |
| 62 | if(self::isSendingLimitReached($mailer_log)) { |
| 63 | throw new \Exception(__('Sending frequency limit has been reached.', 'mailpoet')); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static function pauseSending($mailer_log) { |
| 68 | $mailer_log['status'] = self::STATUS_PAUSED; |
| 69 | $mailer_log['retry_attempt'] = null; |
| 70 | $mailer_log['retry_at'] = null; |
| 71 | return self::updateMailerLog($mailer_log); |
| 72 | } |
| 73 | |
| 74 | static function resumeSending() { |
| 75 | return self::resetMailerLog(); |
| 76 | } |
| 77 | |
| 78 | static function processSendingError($operation, $error_message) { |
| 79 | $mailer_log = self::getMailerLog(); |
| 80 | (int)$mailer_log['retry_attempt']++; |
| 81 | $mailer_log['retry_at'] = time() + self::RETRY_INTERVAL; |
| 82 | $mailer_log['error'] = array( |
| 83 | 'operation' => $operation, |
| 84 | 'error_message' => $error_message |
| 85 | ); |
| 86 | self::updateMailerLog($mailer_log); |
| 87 | return self::enforceExecutionRequirements(); |
| 88 | } |
| 89 | |
| 90 | static function incrementSentCount() { |
| 91 | $mailer_log = self::getMailerLog(); |
| 92 | // clear previous retry count, errors, etc. |
| 93 | if($mailer_log['error']) { |
| 94 | $mailer_log = self::clearSendingErrorLog($mailer_log); |
| 95 | } |
| 96 | (int)$mailer_log['sent']++; |
| 97 | return self::updateMailerLog($mailer_log); |
| 98 | } |
| 99 | |
| 100 | static function clearSendingErrorLog($mailer_log) { |
| 101 | $mailer_log['retry_attempt'] = null; |
| 102 | $mailer_log['retry_at'] = null; |
| 103 | $mailer_log['error'] = null; |
| 104 | return self::updateMailerLog($mailer_log); |
| 105 | } |
| 106 | |
| 107 | static function isSendingLimitReached($mailer_log = false) { |
| 108 | $mailer_config = Mailer::getMailerConfig(); |
| 109 | // do not enforce sending limit for MailPoet's sending method |
| 110 | if($mailer_config['method'] === Mailer::METHOD_MAILPOET) return false; |
| 111 | $mailer_log = self::getMailerLog($mailer_log); |
| 112 | $elapsed_time = time() - (int)$mailer_log['started']; |
| 113 | if($mailer_log['sent'] === $mailer_config['frequency_limit']) { |
| 114 | if($elapsed_time <= $mailer_config['frequency_interval']) return true; |
| 115 | // reset mailer log if enough time has passed since the limit was reached |
| 116 | self::resetMailerLog(); |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | } |