| @@ -14,8 +14,11 @@ | ||
| 14 | 14 | $this->table = 'mail_override'; |
| 15 | 15 | $this->pkey = 'id'; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | + /** | |
| 19 | + * Get mails depending on filters (search, ordering, pagination) | |
| 20 | + */ | |
| 18 | 21 | public function getMatchingElements(array $settings = []): array |
| 19 | 22 | { |
| 20 | 23 | $query = 'SELECT override.*, mail.name, mail.subject FROM #__acym_mail_override AS override JOIN #__acym_mail AS mail ON override.mail_id = mail.id'; |
| 21 | 24 | $queryCount = 'SELECT COUNT(override.mail_id) AS total, SUM(override.active) AS totalActive FROM #__acym_mail_override AS override INNER JOIN #__acym_mail AS mail ON override.mail_id = mail.id'; |
| @@ -87,8 +90,11 @@ | ||
| 87 | 90 | |
| 88 | 91 | return acym_loadObjectList($query, $key); |
| 89 | 92 | } |
| 90 | 93 | |
| 94 | + /** | |
| 95 | + * Returns the override email corresponding to the base email content provided | |
| 96 | + */ | |
| 91 | 97 | public function getMailByBaseContent(string $subject, string $body): ?object |
| 92 | 98 | { |
| 93 | 99 | $translatepressIsActive = acym_isExtensionActive('translatepress-multilingual/index.php'); |
| 94 | 100 | $activeOverrides = $this->getActiveOverrides('name'); |
| @@ -108,8 +114,10 @@ | ||
| 108 | 114 | foreach (['subject', 'body'] as $part) { |
| 109 | 115 | $identifier = 'base_'.$part; |
| 110 | 116 | if (empty($oneOverride->$identifier)) continue; |
| 111 | 117 | |
| 118 | + // The identifier is the default email's text, for example "User coucou registered to the site https://www.acymailing.com" | |
| 119 | + // We use it to check if the current email matches | |
| 112 | 120 | $decodedValue = json_decode($oneOverride->$identifier, true); |
| 113 | 121 | if (empty($decodedValue)) continue; |
| 114 | 122 | |
| 115 | 123 | $oneOverride->$identifier = ''; |
| @@ -123,8 +131,11 @@ | ||
| 123 | 131 | } |
| 124 | 132 | } |
| 125 | 133 | } |
| 126 | 134 | |
| 135 | + // Replace the %s / %1$s by (.*) to get the params from the email content | |
| 136 | + // So: User %1$s registered to the site %2$s | |
| 137 | + // Becomes: User (.*) registered to the site (.*) | |
| 127 | 138 | $oneOverride->$identifier = preg_replace( |
| 128 | 139 | [ |
| 129 | 140 | '/%([0-9].?\$)?s/', |
| 130 | 141 | '/\\\{[A-Z_]+\\\}/i', |
| @@ -152,10 +163,12 @@ | ||
| 152 | 163 | } |
| 153 | 164 | } |
| 154 | 165 | } |
| 155 | 166 | |
| 167 | + // The content didn't match the identifier | |
| 156 | 168 | if (!$matches) continue; |
| 157 | 169 | |
| 170 | + // We found the override | |
| 158 | 171 | $mailClass = new MailClass(); |
| 159 | 172 | $mail = $mailClass->getOneById($oneOverride->mail_id); |
| 160 | 173 | |
| 161 | 174 | if ($oneOverride->source == 'woocommerce' && $translatepressIsActive) { |
| @@ -167,8 +180,9 @@ | ||
| 167 | 180 | } |
| 168 | 181 | $mail->subject = $subjectTranslated; |
| 169 | 182 | } |
| 170 | 183 | |
| 184 | + // Include the found parameters | |
| 171 | 185 | $mail->parameters = $parameters; |
| 172 | 186 | |
| 173 | 187 | if (empty($oneOverride->base_body)) { |
| 174 | 188 | $mail->body = $body; |