ver. 0.279: Newsletter frontend migration, Languages facade elimination, bug fix newsletter_unsubscribe
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
namespace Domain\Newsletter;
|
||||
|
||||
use Domain\Settings\SettingsRepository;
|
||||
use Domain\Article\ArticleRepository;
|
||||
|
||||
class NewsletterRepository
|
||||
{
|
||||
@@ -9,11 +10,29 @@ class NewsletterRepository
|
||||
|
||||
private $db;
|
||||
private SettingsRepository $settingsRepository;
|
||||
private ?ArticleRepository $articleRepository;
|
||||
private ?NewsletterPreviewRenderer $previewRenderer;
|
||||
|
||||
public function __construct($db, ?SettingsRepository $settingsRepository = null)
|
||||
{
|
||||
public function __construct(
|
||||
$db,
|
||||
?SettingsRepository $settingsRepository = null,
|
||||
?ArticleRepository $articleRepository = null,
|
||||
?NewsletterPreviewRenderer $previewRenderer = null
|
||||
) {
|
||||
$this->db = $db;
|
||||
$this->settingsRepository = $settingsRepository ?? new SettingsRepository($db);
|
||||
$this->articleRepository = $articleRepository;
|
||||
$this->previewRenderer = $previewRenderer;
|
||||
}
|
||||
|
||||
private function getArticleRepository(): ArticleRepository
|
||||
{
|
||||
return $this->articleRepository ?? ($this->articleRepository = new ArticleRepository($this->db));
|
||||
}
|
||||
|
||||
private function getPreviewRenderer(): NewsletterPreviewRenderer
|
||||
{
|
||||
return $this->previewRenderer ?? ($this->previewRenderer = new NewsletterPreviewRenderer());
|
||||
}
|
||||
|
||||
public function getSettings(): array
|
||||
@@ -289,4 +308,138 @@ class NewsletterRepository
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
// ── Frontend methods ──
|
||||
|
||||
public function unsubscribe(string $hash): bool
|
||||
{
|
||||
$id = $this->db->get('pp_newsletter', 'id', ['hash' => $hash]);
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->db->delete('pp_newsletter', ['id' => $id]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function confirmSubscription(string $hash): bool
|
||||
{
|
||||
$id = $this->db->get('pp_newsletter', 'id', ['AND' => ['hash' => $hash, 'status' => 0]]);
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->db->update('pp_newsletter', ['status' => 1], ['id' => $id]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getHashByEmail(string $email): ?string
|
||||
{
|
||||
$hash = $this->db->get('pp_newsletter', 'hash', ['email' => $email]);
|
||||
return $hash ? (string)$hash : null;
|
||||
}
|
||||
|
||||
public function removeByEmail(string $email): bool
|
||||
{
|
||||
if (!$this->db->get('pp_newsletter', 'id', ['email' => $email])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$this->db->delete('pp_newsletter', ['email' => $email]);
|
||||
}
|
||||
|
||||
public function signup(string $email, string $serverName, bool $ssl, array $settings): bool
|
||||
{
|
||||
if (!\S::email_check($email)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->db->get('pp_newsletter', 'id', ['email' => $email])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$hash = md5(time() . $email);
|
||||
|
||||
$text = ($settings['newsletter_header'] ?? '');
|
||||
$text .= $this->templateByName('#potwierdzenie-zapisu-do-newslettera');
|
||||
$text .= ($settings['newsletter_footer'] ?? '');
|
||||
|
||||
$base = $ssl ? 'https' : 'http';
|
||||
$link = '/newsletter/confirm/hash=' . $hash;
|
||||
$text = str_replace('[LINK]', $link, $text);
|
||||
$text = str_replace('[WYPISZ_SIE]', '', $text);
|
||||
|
||||
$text = preg_replace(
|
||||
"-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i",
|
||||
"$1" . $base . "://" . $serverName . "$2$4",
|
||||
$text
|
||||
);
|
||||
$text = preg_replace(
|
||||
"-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i",
|
||||
"$1" . $base . "://" . $serverName . "$2$4",
|
||||
$text
|
||||
);
|
||||
|
||||
$lang = \S::get_session('lang-' . \S::get_session('current-lang'));
|
||||
$subject = $lang['potwierdz-zapisanie-sie-do-newslettera'] ?? 'Newsletter';
|
||||
\S::send_email($email, $subject, $text);
|
||||
|
||||
$this->db->insert('pp_newsletter', ['email' => $email, 'hash' => $hash, 'status' => 0]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sendQueued(int $limit, string $serverName, bool $ssl, string $unsubscribeLabel): bool
|
||||
{
|
||||
$settingsDetails = $this->settingsRepository->getSettings();
|
||||
|
||||
$results = $this->db->query('SELECT * FROM pp_newsletter_send ORDER BY id ASC LIMIT ' . (int)$limit);
|
||||
$results = $results ? $results->fetchAll() : [];
|
||||
|
||||
if (!is_array($results) || empty($results)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$renderer = $this->getPreviewRenderer();
|
||||
$articleRepo = $this->getArticleRepository();
|
||||
|
||||
foreach ($results as $row) {
|
||||
$dates = explode(' - ', $row['dates']);
|
||||
|
||||
$articles = [];
|
||||
if (isset($dates[0], $dates[1])) {
|
||||
$articles = $articleRepo->articlesByDateAdd($dates[0], $dates[1]);
|
||||
}
|
||||
|
||||
$text = $renderer->render(
|
||||
is_array($articles) ? $articles : [],
|
||||
$settingsDetails,
|
||||
$this->templateDetails((int)$row['id_template']),
|
||||
(string)$row['dates']
|
||||
);
|
||||
|
||||
$base = $ssl ? 'https' : 'http';
|
||||
|
||||
$text = preg_replace(
|
||||
"-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i",
|
||||
"$1" . $base . "://" . $serverName . "$2$4",
|
||||
$text
|
||||
);
|
||||
$text = preg_replace(
|
||||
"-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i",
|
||||
"$1" . $base . "://" . $serverName . "$2$4",
|
||||
$text
|
||||
);
|
||||
|
||||
$hash = $this->getHashByEmail($row['email']);
|
||||
$link = $base . "://" . $serverName . '/newsletter/unsubscribe/hash=' . $hash;
|
||||
$text = str_replace('[WYPISZ_SIE]', '<a href="' . $link . '">' . $unsubscribeLabel . '</a>', $text);
|
||||
|
||||
\S::send_email($row['email'], 'Newsletter ze strony: ' . $serverName, $text);
|
||||
|
||||
$this->db->delete('pp_newsletter_send', ['id' => $row['id']]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user