Skip to content

Commit

Permalink
Update replace tokens signature
Browse files Browse the repository at this point in the history
  • Loading branch information
cannycookie committed Apr 6, 2024
1 parent 2228a86 commit 86e60ba
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Contracts/TokenReplacementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

interface TokenReplacementInterface
{
public function replaceTokens($content, $models);
public function replaceTokens(string $content,array $models);
}
26 changes: 20 additions & 6 deletions src/DefaultTokenHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@

use Illuminate\Support\Facades\View;
use Visualbuilder\EmailTemplates\Contracts\TokenReplacementInterface;
use Visualbuilder\EmailTemplates\Models\EmailTemplate;


class DefaultTokenHelper implements TokenReplacementInterface
{
public function replaceTokens($content, $models)
/**
* Replace tokens in the content with actual values from the models.
*
* @param string $content The content with tokens to be replaced
* @param array $models The models containing the values for the tokens
*
* @return string The content with replaced tokens
*/
public function replaceTokens(string $content, $models): string
{
// Replace singular tokens.
// These are for password reset and email verification
Expand Down Expand Up @@ -44,7 +53,9 @@ public function replaceTokens($content, $models)
}
}

//Insert user-consent
/**
* User Consent Email tokens
*/
if(isset($models->consentOptions)){
$content = str_replace('##consent-options##',view('vendor.user-consent.mails.accept-notification', ['consentOptions' => $models->consentOptions])->render(),$content);

Expand Down Expand Up @@ -84,13 +95,16 @@ public function replaceTokens($content, $models)
}
}

$button = $this->buildEmailButton($content);
$content = self::replaceButtonToken($content, $button);
if(isset($models->emailTemplate)){
$button = $this->buildEmailButton($content, $models->emailTemplate);
$content = self::replaceButtonToken($content, $button);
}


return $content;
}

private function buildEmailButton($content)
private function buildEmailButton($content, $emailTemplate)
{
$title = $url = '';
if (preg_match('/(?<=##button).*?(?=#)/', $content, $matches)) {
Expand All @@ -105,7 +119,7 @@ private function buildEmailButton($content)
return View::make('vb-email-templates::email.parts._button', [
'url' => $url,
'title' => $title,
'data' => ['theme' => $this->theme->colours],
'data' => ['theme' => $emailTemplate->theme->colours],
])
->render();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/TokenHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected static function getFacadeAccessor()
return \Visualbuilder\EmailTemplates\Contracts\TokenReplacementInterface::class;
}

public static function replace($content, $models)
public static function replace(string $content, $models): string
{
return static::resolveFacadeInstance(static::getFacadeAccessor())->replaceTokens($content, $models);
}
Expand Down
28 changes: 14 additions & 14 deletions src/Models/EmailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,17 @@ public function getBase64EmailPreviewData()
*/
public function getEmailPreviewData()
{
$model = self::createEmailPreviewData();
$models = self::createEmailPreviewData();

// preparing logo
$logo = $this->resolveLogoUrl($this->logo);

return [
'user' => $model->user,
'content' => TokenHelper::replace($this->content, $model),
'subject' => TokenHelper::replace($this->subject, $model),
'preHeaderText' => TokenHelper::replace($this->preheader, $model),
'title' => TokenHelper::replace($this->title, $model),
'content' => TokenHelper::replace($this->content, $models),
'subject' => TokenHelper::replace($this->subject, $models),
'preHeaderText' => TokenHelper::replace($this->preheader, $models),
'title' => TokenHelper::replace($this->title, $models),
'theme' => $this->theme->colours,
'logo' => $logo,
];
Expand All @@ -179,19 +179,19 @@ public function getEmailPreviewData()
*/
public static function createEmailPreviewData()
{
$model = (object) [];
$models = (object) [];

$userModel = config('filament-email-templates.recipients')[0];
//Setup some data for previewing email template
$model->user = $userModel::first();

$model->tokenUrl = URL::to('/');
$model->verificationUrl = URL::to('/');
$model->expiresAt = now();
$models->tokenUrl = URL::to('/');
$models->verificationUrl = URL::to('/');
$models->expiresAt = now();
/* Not used in preview but need to add something */
$model->plainText = Str::random(32);
$models->plainText = Str::random(32);

return $model;
return $models;
}

/**
Expand Down Expand Up @@ -241,10 +241,10 @@ public function getMailableExistsAttribute(): bool
public function getMailableClass()
{
$className = Str::studly($this->key);
$directory = str_replace('/', '\\', config('filament-email-templates.mailable_directory', 'Mail/Visualbuilder/EmailTemplates')); // Convert slashes to namespace format
$fullClassName = "\\App\\{$directory}\\{$className}";
$directory = str_replace('/', '\\', config('filament-email-templates.mailable_directory', 'Mail/Visualbuilder/EmailTemplates'));
$fullClassName = "App\\" . rtrim($directory, '\\') . "\\{$className}";

if (! class_exists($fullClassName)) {
if (!class_exists($fullClassName)) {
throw new \Exception("Mailable class {$fullClassName} does not exist.");
}

Expand Down
20 changes: 11 additions & 9 deletions src/Traits/BuildGenericEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

trait BuildGenericEmail
{

public $emailTemplate;
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$template = EmailTemplate::findEmailByKey($this->template, App::currentLocale());
$this->emailTemplate = EmailTemplate::findEmailByKey($this->template, App::currentLocale());

if($this->attachment ?? false) {
$this->attach(
Expand All @@ -30,19 +32,19 @@ public function build()


// preparing logo
$logo = $template->resolveLogoUrl($template->logo);
$logo = $this->emailTemplate->resolveLogoUrl($this->emailTemplate->logo);

$data = [
'content' => TokenHelper::replace($template->content, $this),
'preHeaderText' => TokenHelper::replace($template->preheader, $this),
'title' => TokenHelper::replace($template->title, $this),
'theme' => $template->theme->colours,
'content' => TokenHelper::replace($this->emailTemplate->content, $this),
'preHeaderText' => TokenHelper::replace($this->emailTemplate->preheader, $this),
'title' => TokenHelper::replace($this->emailTemplate->title, $this),
'theme' => $this->emailTemplate->theme->colours,
'logo' => $logo,
];

return $this->from($template->from['email'], $template->from['name'])
->view($template->view_path)
->subject(TokenHelper::replace($template->subject, $this))
return $this->from($this->emailTemplate->from['email'], $this->emailTemplate->from['name'])
->view($this->emailTemplate->view_path)
->subject(TokenHelper::replace($this->emailTemplate->subject, $this))
->to($this->sendTo)
->with(['data' => $data]);
}
Expand Down

0 comments on commit 86e60ba

Please sign in to comment.