notification = $notification; if (!$this->hasRequiredVariables()) { throw new Exception( 'Mailable should include the following template variables: ' . implode(',', static::getRequiredVariables()) ); } return $this; } /** * Setup footer with unsubscribe link if notification is deliberately set with self::allowUnsubscribe() * * @param null|mixed $localeKey */ protected function setupUnsubscribeFooter(string $locale, $context, $localeKey = null): void { if (!isset($this->notification)) { return; } $this->footer = $this->renameContextVariables($this->setFooterText($locale, $localeKey)); $notificationManager = new NotificationManager(); /** @var NotificationManager $notificationManager */ $request = Application::get()->getRequest(); $this->addData([ self::$unsubscribeUrl => $notificationManager->getUnsubscribeNotificationUrl( $request, $this->notification, $context ), ]); } /** * Adds unsubscribe headers, see pkp/pkp-lib#6627 */ public function headers(): Headers { $unsubscribeUrl = $this->viewData[self::$unsubscribeUrl] ?? null; return new Headers( null, [], $unsubscribeUrl ? [ 'List-Unsubscribe-Post' => 'List-Unsubscribe=One-Click', 'List-Unsubscribe' => '<' . $unsubscribeUrl . '>', ] : [] ); } /** * @return bool whether mailable contains variables requires for the footer */ protected function hasRequiredVariables(): bool { $included = []; $requiredVariables = static::getRequiredVariables(); foreach ($requiredVariables as $requiredVariable) { foreach ($this->getVariables() as $variable) { if (is_a($variable, $requiredVariable, true)) { $included[] = $requiredVariable; break; } } } return count($included) === count($requiredVariables); } /** * Replace email template variables in the locale string, so they correspond to the application, * e.g., contextName => journalName/pressName/serverName */ protected function renameContextVariables(string $footer): string { $map = [ '{$' . PKPContextEmailVariable::CONTEXT_NAME . '}' => '{$' . ContextEmailVariable::CONTEXT_NAME . '}', '{$' . PKPContextEmailVariable::CONTEXT_URL . '}' => '{$' . ContextEmailVariable::CONTEXT_URL . '}', '{$' . PKPContextEmailVariable::CONTEXT_SIGNATURE . '}' => '{$' . ContextEmailVariable::CONTEXT_SIGNATURE . '}', ]; return str_replace(array_keys($map), array_values($map), $footer); } /** * Set the message to be displayed in the footer */ protected function setFooterText(string $locale, string $localeKey = null): string { if (is_null($localeKey)) { $localeKey = $this->defaultUnsubscribeLocaleKey; } return __($localeKey, [], $locale); } protected static function getRequiredVariables(): array { return static::$requiredVariables; } }