-
-
Notifications
You must be signed in to change notification settings - Fork 321
Introduce EmailTemplate classes
#3632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tadhgboyle
wants to merge
25
commits into
develop
Choose a base branch
from
tb/email-template-classes
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
1515b29
Introduce `EmailTemplate` classes
tadhgboyle 0157186
Merge branch 'develop' into tb/email-template-classes
tadhgboyle 8b99733
wip
tadhgboyle 4c2d0a5
convert test email
tadhgboyle ef61116
convert forgot password
tadhgboyle 14c2fa9
wip
tadhgboyle dfddc1a
convert test email to sendRaw
tadhgboyle a9bb236
refactor email class
tadhgboyle 2b7e13c
remove global placeholders
tadhgboyle f1ccc4b
Merge branch 'develop' into tb/email-template-classes
tadhgboyle 02ee708
wip
tadhgboyle 6b1f735
remove email message editing + previewing
tadhgboyle 94b445a
unused language keys
tadhgboyle 79932f3
convert email error type column to mailer
tadhgboyle 7488b31
move away from type ints
tadhgboyle 9afce6e
unused terms
tadhgboyle a8315ab
fixes
tadhgboyle 60fb6d5
fix notification settings in cli install
tadhgboyle 05ff9f7
fix forgot password flow messages
tadhgboyle 5f14e07
rename content event
tadhgboyle b50564c
unused term
tadhgboyle a239f45
Apply fixes from StyleCI (#3640)
tadhgboyle 7592d7b
WIP
partydragen eb75dae
Revert "WIP"
partydragen 42c6767
todo
tadhgboyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,14 @@ | ||
| <?php | ||
|
|
||
| class AlertTemplate | ||
| { | ||
| public function __construct( | ||
| public LanguageKey|string $title, | ||
| public LanguageKey|string|null $content = null, | ||
| public ?string $link = null, | ||
| ) { | ||
| if ($this->link === null && $this->content === null) { | ||
| throw new InvalidArgumentException('Either link or content must be provided'); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
| <?php | ||
|
|
||
| abstract class EmailTemplate | ||
| { | ||
| /** | ||
| * @var array<string, LanguageKey|string> Placeholders for this email template | ||
| */ | ||
| private array $_placeholders = []; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->addPlaceholder('[Sitename]', Output::getClean(SITE_NAME)); | ||
| $this->addPlaceholder('[Greeting]', new LanguageKey('emails', 'greeting')); | ||
| $this->addPlaceholder('[Thanks]', new LanguageKey('emails', 'thanks')); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the snake_case representation of the email template name, | ||
| * derived from the class name with "EmailTemplate" removed. | ||
| * For example: RegisterEmailTemplate -> "register", ForgotPasswordEmailTemplate -> "forgot_password". | ||
| */ | ||
| private function name(): string | ||
| { | ||
| $baseName = str_replace('EmailTemplate', '', static::class); | ||
|
|
||
| return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $baseName)); | ||
| } | ||
|
|
||
| abstract public function subject(): LanguageKey|string; | ||
|
|
||
| /** | ||
| * Add a custom placeholder/variable for email messages. | ||
| * | ||
| * @param string $key The key to use for the placeholder, should be enclosed in square brackets. | ||
| * @param string|Closure(Language, string): string $value The value to replace the placeholder with. | ||
| */ | ||
| final public function addPlaceholder(string $key, $value): void | ||
| { | ||
| $this->_placeholders[$key] = $value; | ||
| } | ||
|
|
||
| final public function renderContent(string $languageCode): string | ||
| { | ||
| $placeholderKeys = array_keys($this->_placeholders); | ||
| $placeholderValues = []; | ||
|
|
||
| foreach ($this->_placeholders as $placeholder) { | ||
| if ($placeholder instanceof LanguageKey) { | ||
| $placeholderValues[] = $placeholder->translate($languageCode); | ||
| } else { | ||
| $placeholderValues[] = $placeholder; | ||
| } | ||
| } | ||
|
|
||
| return str_replace( | ||
| $placeholderKeys, | ||
| $placeholderValues, | ||
| file_get_contents($this->getPath()), | ||
| ); | ||
| } | ||
|
|
||
| private function getPath(): string | ||
| { | ||
| $name = $this->name(); | ||
|
|
||
| $customPath = implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'custom', 'templates', TEMPLATE, 'email', $name . '.html']); | ||
| if (file_exists($customPath)) { | ||
| return $customPath; | ||
| } | ||
|
|
||
| $defaultPath = implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'custom', 'templates', 'DefaultRevamp', 'email', $name . '.html']); | ||
tadhgboyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (file_exists($defaultPath)) { | ||
| return $defaultPath; | ||
| } | ||
|
|
||
| throw new Exception('Email template not found: ' . $name); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
38 changes: 38 additions & 0 deletions
38
core/migrations/20250503090934_convert_email_error_type_to_string.php
This file contains hidden or 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,38 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Phinx\Migration\AbstractMigration; | ||
|
|
||
| final class ConvertEmailErrorTypeToString extends AbstractMigration | ||
| { | ||
| private const CONVERSION_MAP = [ | ||
| 1 => RegisterEmailTemplate::class, | ||
| 3 => ForgotPasswordEmailTemplate::class, | ||
| 5 => ForumTopicReplyEmailTemplate::class, | ||
| 6 => MassMessageEmailTemplate::class, | ||
| ]; | ||
|
|
||
| public function change(): void | ||
| { | ||
| $this->table('nl2_email_errors') | ||
| ->renameColumn('type', 'mailer') | ||
| ->changeColumn('mailer', 'string', ['limit' => 255]) | ||
| ->update(); | ||
|
|
||
| $email_errors = DB::getInstance()->query('SELECT * FROM nl2_email_errors')->results(); | ||
|
|
||
| foreach ($email_errors as $error) { | ||
| $type = $error->mailer; | ||
| if (isset(self::CONVERSION_MAP[$type])) { | ||
| DB::getInstance()->update('email_errors', $error->id, [ | ||
| 'mailer' => self::CONVERSION_MAP[$type], | ||
| ]); | ||
| } else { | ||
| DB::getInstance()->update('email_errors', $error->id, [ | ||
| 'mailer' => 'unknown', | ||
| ]); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to keep support for setting email, Such as Store and Forms module will send emails to quests based on the email they entered