Skip to content

Commit f6aa25e

Browse files
authored
Merge pull request #162 from danog/newsletter
Newsletter
2 parents 9b652f6 + d5ac40b commit f6aa25e

25 files changed

Lines changed: 495 additions & 55 deletions

‎.htaccess‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ RewriteRule ^(playground|r|demo|try|sandbox)/?$ /play [R=301,L,NC,QSA]
108108
RewriteRule ^quickstart/?$ https://psalm.dev/docs/running_psalm/installation/ [R=301,L]
109109
RewriteRule ^contribute/?$ /views/contribute.php [L,NC]
110110
RewriteRule ^support/?$ /views/support.php [L,NC]
111+
RewriteRule ^newsletter/?$ /views/newsletter.php [L,NC]
112+
RewriteRule ^newsletter/check-your-email/?$ /views/newsletter_check.php [L,NC]
111113
RewriteRule ^plugins/?$ /views/plugins.php [L,NC]
112114
RewriteRule ^$ /views/index.php [L,NC]
113115
RewriteRule ^schema/config$ /vendor/vimeo/psalm/config.xsd [L,NC]

‎assets/css/mkdocs_material_extra.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎assets/css/site.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎assets/js/newsletter.js‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// The newsletter forms (includes/subscribe.php, and the docs theme's): Cloudflare Turnstile is loaded once
2+
// someone starts filling in a form, and newsletter.daniil.it/subscribe sends errors back to /newsletter as
3+
// ?error=<code>. In the docs, a dismissable box invites readers to subscribe (see showPopup).
4+
(function () {
5+
var TURNSTILE = "https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=psalmNewsletterTurnstile";
6+
var STORAGE_KEY = "psalm-newsletter";
7+
// After a dismissal, the docs box comes back after this long
8+
var DISMISSED_FOR = 60 * 24 * 60 * 60 * 1000;
9+
10+
var messages = {
11+
email: "Please enter a valid email address.",
12+
consent: "Please accept the privacy policy to subscribe.",
13+
captcha: "The anti-spam check didn't pass: please try again.",
14+
form: "The form couldn't be read: please try again.",
15+
unavailable: "The subscription couldn't be saved right now: please try again later.",
16+
};
17+
18+
function storage(value) {
19+
try {
20+
if (value === undefined)
21+
return JSON.parse(localStorage.getItem(STORAGE_KEY) || "null");
22+
localStorage.setItem(STORAGE_KEY, JSON.stringify(value));
23+
} catch (e) {
24+
// Private windows and blocked storage: the box just shows again
25+
}
26+
return null;
27+
}
28+
29+
var turnstileState = "idle";
30+
var waiting = [];
31+
32+
window.psalmNewsletterTurnstile = function () {
33+
turnstileState = "ready";
34+
waiting.splice(0).forEach(render);
35+
};
36+
37+
// Renders the form's widget, loading Turnstile first if needed
38+
function render(form) {
39+
if (form.newsletterWidget !== undefined)
40+
return;
41+
if (turnstileState !== "ready") {
42+
if (waiting.indexOf(form) === -1)
43+
waiting.push(form);
44+
if (turnstileState === "idle") {
45+
turnstileState = "loading";
46+
var script = document.createElement("script");
47+
script.src = TURNSTILE;
48+
script.async = true;
49+
script.onerror = function () {
50+
turnstileState = "idle";
51+
};
52+
document.head.appendChild(script);
53+
}
54+
return;
55+
}
56+
var box = form.querySelector(".newsletter__turnstile");
57+
form.newsletterWidget = window.turnstile.render(box, {
58+
sitekey: box.getAttribute("data-sitekey"),
59+
size: "flexible",
60+
callback: function () {
61+
if (form.newsletterPending) {
62+
form.newsletterPending = false;
63+
form.submit();
64+
}
65+
},
66+
});
67+
}
68+
69+
function setUp(form) {
70+
if (form.newsletterReady)
71+
return;
72+
form.newsletterReady = true;
73+
form.addEventListener("focusin", function () {
74+
render(form);
75+
});
76+
// Until Turnstile has a token, wait for it and submit then
77+
form.addEventListener("submit", function (event) {
78+
var token = form.querySelector("[name=cf-turnstile-response]");
79+
if (!token || !token.value) {
80+
event.preventDefault();
81+
form.newsletterPending = true;
82+
render(form);
83+
return;
84+
}
85+
storage({ subscribed: Date.now() });
86+
});
87+
}
88+
89+
function showError() {
90+
var code = new URLSearchParams(location.search).get("error");
91+
var form = document.querySelector("form.newsletter--page");
92+
if (!code || !form || !Object.prototype.hasOwnProperty.call(messages, code))
93+
return;
94+
var p = document.createElement("p");
95+
p.className = "newsletter__error";
96+
p.setAttribute("role", "alert");
97+
p.textContent = messages[code];
98+
form.parentNode.insertBefore(p, form);
99+
}
100+
101+
// The docs box: shown once the reader has spent some time on the docs, unless they subscribed or
102+
// dismissed it recently
103+
function showPopup() {
104+
var template = document.getElementById("newsletter-popup");
105+
if (!template)
106+
return;
107+
var state = storage();
108+
if (state && (state.subscribed || (state.dismissed && Date.now() - state.dismissed < DISMISSED_FOR)))
109+
return;
110+
111+
var shown = false;
112+
function show() {
113+
if (shown)
114+
return;
115+
shown = true;
116+
window.removeEventListener("scroll", onScroll);
117+
var popup = template.content.firstElementChild.cloneNode(true);
118+
popup.querySelector(".newsletter-popup__close").addEventListener("click", function () {
119+
storage({ dismissed: Date.now() });
120+
popup.remove();
121+
});
122+
document.body.appendChild(popup);
123+
setUp(popup.querySelector("form.newsletter"));
124+
}
125+
function onScroll() {
126+
var scrolled = window.scrollY + window.innerHeight;
127+
if (scrolled >= document.documentElement.scrollHeight * 0.6)
128+
show();
129+
}
130+
window.addEventListener("scroll", onScroll, { passive: true });
131+
setTimeout(show, 45000);
132+
}
133+
134+
function init() {
135+
Array.prototype.forEach.call(document.querySelectorAll("form.newsletter"), setUp);
136+
showError();
137+
showPopup();
138+
}
139+
140+
if (document.readyState === "loading")
141+
document.addEventListener("DOMContentLoaded", init);
142+
else
143+
init();
144+
})();

‎assets/sass/mkdocs_material_extra.scss‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@import 'tiempos_text.scss';
22
@import 'national.scss';
33
@import 'view_transitions.scss';
4+
@import 'newsletter.scss';
45

56
body {
67
font-family: 'Tiempos Text', 'Times New Roman', serif;

‎assets/sass/newsletter.scss‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// The newsletter forms (includes/subscribe.php and the docs theme) and the docs' box (assets/js/newsletter.js)
2+
.newsletter {
3+
font-family: "National 2", "Helvetica Neue", "Helvetica", sans-serif;
4+
font-size: 15px;
5+
line-height: 1.4;
6+
text-align: left;
7+
8+
p {
9+
margin: 0 0 .6em;
10+
}
11+
12+
.newsletter__title {
13+
font-weight: bold;
14+
font-size: 18px;
15+
}
16+
17+
.newsletter__fields {
18+
display: flex;
19+
flex-wrap: wrap;
20+
gap: 8px;
21+
align-items: flex-end;
22+
23+
label {
24+
flex: 1 1 200px;
25+
display: flex;
26+
flex-direction: column;
27+
gap: 4px;
28+
font-size: 13px;
29+
color: #555;
30+
}
31+
}
32+
33+
input[type=email],
34+
input[type=text] {
35+
font: inherit;
36+
padding: 0 10px;
37+
height: 40px;
38+
border: 1px solid #c9c4c4;
39+
border-radius: 3px;
40+
background: #fff;
41+
color: #000;
42+
box-sizing: border-box;
43+
width: 100%;
44+
}
45+
46+
button {
47+
font: inherit;
48+
font-weight: bold;
49+
height: 40px;
50+
padding: 0 18px;
51+
border: 0;
52+
border-radius: 3px;
53+
background: #cfeabb;
54+
color: #2e5d0b;
55+
cursor: pointer;
56+
57+
&:hover {
58+
background: #b9e09c;
59+
}
60+
}
61+
62+
.newsletter__consent {
63+
display: block;
64+
margin-top: 8px;
65+
font-size: 13px;
66+
color: #555;
67+
}
68+
69+
.newsletter__turnstile:not(:empty) {
70+
margin-top: 8px;
71+
}
72+
}
73+
74+
// Honeypot: bots fill it, people don't see it
75+
.newsletter__hp {
76+
position: absolute !important;
77+
left: -10000px;
78+
width: 1px;
79+
height: 1px;
80+
overflow: hidden;
81+
}
82+
83+
.newsletter__error {
84+
padding: 10px 14px;
85+
border-left: 3px solid #c42020;
86+
background: #fbeaea;
87+
color: #7a1414;
88+
}
89+
90+
.newsletter--page,
91+
.newsletter--box {
92+
margin: 2em 0;
93+
}
94+
95+
.newsletter--box {
96+
padding: 18px 20px;
97+
background: #f1f0f0;
98+
border-radius: 4px;
99+
}
100+
101+
.newsletter--compact {
102+
max-width: 480px;
103+
margin: 0 auto 24px;
104+
}
105+
106+
.footer_newsletter > p {
107+
margin-bottom: 8px;
108+
}
109+
110+
.newsletter-popup {
111+
position: fixed;
112+
right: 16px;
113+
bottom: 16px;
114+
z-index: 10;
115+
width: 340px;
116+
max-width: calc(100vw - 32px);
117+
box-sizing: border-box;
118+
padding: 18px 20px;
119+
background: #fff;
120+
border-radius: 6px;
121+
box-shadow: 0 6px 24px rgba(0, 0, 0, .2);
122+
123+
.newsletter__title {
124+
padding-right: 24px;
125+
}
126+
}
127+
128+
.newsletter-popup__close {
129+
position: absolute;
130+
top: 6px;
131+
right: 8px;
132+
border: 0;
133+
background: none;
134+
font-size: 24px;
135+
line-height: 1;
136+
color: #777;
137+
cursor: pointer;
138+
139+
&:hover {
140+
color: #000;
141+
}
142+
}
143+
144+
// In articles and pages, aligned with the text, whose paragraphs are indented
145+
div.post {
146+
.newsletter--page,
147+
.newsletter--box,
148+
.newsletter__error {
149+
margin-left: 20px;
150+
margin-right: 20px;
151+
}
152+
153+
.newsletter p {
154+
padding: 0;
155+
font-size: inherit;
156+
line-height: inherit;
157+
margin: 0 0 .6em;
158+
}
159+
160+
p.newsletter__error {
161+
padding: 10px 14px;
162+
}
163+
}

‎assets/sass/site.scss‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@import 'front.scss';
77
@import 'post.scss';
88
@import 'footer.scss';
9+
@import 'newsletter.scss';
910
@import 'plugins.scss';
1011
@import 'view_transitions.scss';
1112

‎includes/footer.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
<?php require_once __DIR__ . '/subscribe.php'; ?>
12
<footer>
2-
<p>Follow Psalm news: <a target="_blank" href="https://t.me/psalmphp">Telegram</a> — <a target="_blank" href="https://discord.gg/RwH8rEpxt9">Discord</a> — <a target="_blank" rel="me" href="https://phpc.social/@psalm">Mastodon</a> — <a target="_blank" href="https://x.com/psalmphp">X</a></p>
3+
<?php if (!NewsletterForm::rendered()): ?>
4+
<div class="footer_newsletter">
5+
<p>Get new Psalm articles by email:</p>
6+
<?= NewsletterForm::render('compact') ?>
7+
</div>
8+
<?php endif ?>
9+
<p>Follow Psalm news: <a href="/newsletter">Newsletter</a> — <a target="_blank" href="https://t.me/psalmphp">Telegram</a> — <a target="_blank" href="https://discord.gg/RwH8rEpxt9">Discord</a> — <a target="_blank" rel="me" href="https://phpc.social/@psalm">Mastodon</a> — <a target="_blank" href="https://x.com/psalmphp">X</a></p>
310
<p>Official community: <a target="_blank" href="https://psalmphp.t.me">Telegram</a> — <a target="_blank" href="https://discord.gg/RwH8rEpxt9">Discord</a></p>
411
</footer>

‎includes/meta.php‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
<?php
2-
/**
3-
* Shared <head> tags. Pages may set before including:
4-
* - $meta_description: text for <meta name="description">
5-
* - $canonical_url: canonical URL, or false to omit it. When unset, it is https://psalm.dev plus
6-
* the request path without query, repeated slashes or a trailing slash (pages with fixed
7-
* URLs set it explicitly, since routes are case-insensitive)
8-
*/
92
require_once __DIR__ . '/asset.php';
103

11-
if (!isset($canonical_url)) {
12-
// Not parse_url(): it reads "//plugins" as a host and returns no path
13-
$canonical_path = rtrim((string) preg_replace('#/+#', '/', strtok($_SERVER['REQUEST_URI'] ?? '/', '?')), '/');
14-
$canonical_url = 'https://psalm.dev' . ($canonical_path === '' ? '/' : $canonical_path);
15-
}
4+
/**
5+
* Prints the shared <head> tags.
6+
*
7+
* @param string|null $description text for <meta name="description">
8+
* @param string|false|null $canonical canonical URL, or false to omit it. When null, it is https://psalm.dev
9+
* plus the request path without query, repeated slashes or a trailing slash (pages with fixed
10+
* URLs pass it explicitly, since routes are case-insensitive)
11+
*/
12+
function page_meta(?string $description = null, string|false|null $canonical = null): void
13+
{
14+
if ($canonical === null) {
15+
// Not parse_url(): it reads "//plugins" as a host and returns no path
16+
$canonical_path = rtrim((string) preg_replace('#/+#', '/', (string) strtok($_SERVER['REQUEST_URI'] ?? '/', '?')), '/');
17+
$canonical = 'https://psalm.dev' . ($canonical_path === '' ? '/' : $canonical_path);
18+
}
1619
?>
17-
<?php if (!empty($meta_description)): ?>
18-
<meta name="description" content="<?= htmlspecialchars($meta_description, ENT_QUOTES) ?>" />
20+
<?php if ($description !== null && $description !== ''): ?>
21+
<meta name="description" content="<?= htmlspecialchars($description, ENT_QUOTES) ?>" />
1922
<?php endif ?>
20-
<?php if ($canonical_url): ?>
21-
<link rel="canonical" href="<?= htmlspecialchars($canonical_url, ENT_QUOTES) ?>" />
23+
<?php if ($canonical !== false && $canonical !== ''): ?>
24+
<link rel="canonical" href="<?= htmlspecialchars($canonical, ENT_QUOTES) ?>" />
2225
<?php endif ?>
2326
<meta property="og:site_name" content="Psalm" />
2427
<meta property="og:image" content="https://psalm.dev/psalm_preview.png" />
@@ -30,3 +33,5 @@
3033
<link rel="icon" type="image/png" href="/favicon.png">
3134
<meta name="viewport" content="width=device-width, initial-scale=1">
3235
<?php require __DIR__ . '/speculation_rules.php'; ?>
36+
<?php
37+
}

0 commit comments

Comments
 (0)