Skip to content

Commit 078f7f0

Browse files
committed
refactor: simplify FrankenPHP integration — on-demand TLS handles everything
- Remove admin API TLS policy patching (no longer needed) - Remove Psr\Log\LogLevel dependency - on_add_domain/on_remove_domain are now just logging — Caddy's on-demand TLS provisions and expires certs automatically via the ask endpoint - The ask endpoint (REST + standalone PHP) is the only active component
1 parent f775968 commit 078f7f0

1 file changed

Lines changed: 12 additions & 92 deletions

File tree

inc/integrations/providers/frankenphp/class-frankenphp-domain-mapping.php

Lines changed: 12 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace WP_Ultimo\Integrations\Providers\FrankenPHP;
1919

20-
use Psr\Log\LogLevel;
2120
use WP_Ultimo\Integrations\Base_Capability_Module;
2221
use WP_Ultimo\Integrations\Capabilities\Domain_Mapping_Capability;
2322

@@ -187,123 +186,44 @@ private function is_valid_multisite_domain(string $domain): bool {
187186
*
188187
* @param string $domain The domain name being mapped.
189188
* @param int $site_id ID of the site receiving the mapping.
189+
* On-demand TLS handles certificate provisioning automatically on first
190+
* TLS handshake. The ask endpoint validates the domain. No explicit
191+
* provisioning needed — just log for observability.
192+
*
190193
* @return void
191194
*/
192195
public function on_add_domain(string $domain, int $site_id): void {
193196

194-
$this->provision_certificate($domain);
197+
if (function_exists('wu_log_add')) {
198+
wu_log_add('integration-frankenphp', "Domain added: {$domain} (cert will be provisioned on first visit via on-demand TLS)");
199+
}
195200
}
196201

197202
/**
198-
* Called when a mapped domain is removed.
199-
*
200-
* Caddy automatically stops serving certs for domains that fail the
201-
* "ask" check on renewal, so no explicit cleanup is needed.
203+
* {@inheritdoc}
202204
*
203-
* @param string $domain The domain name being removed.
204-
* @param int $site_id ID of the site.
205-
* @return void
205+
* Caddy automatically stops renewing certs for domains that fail the
206+
* ask endpoint check, so no explicit cleanup is needed.
206207
*/
207208
public function on_remove_domain(string $domain, int $site_id): void {
208209

209-
// Caddy handles cleanup automatically via on-demand TLS renewal checks.
210210
if (function_exists('wu_log_add')) {
211211
wu_log_add('integration-frankenphp', "Domain removed: {$domain} (cert will expire naturally)");
212212
}
213213
}
214214

215215
/**
216-
* Called when a new subdomain is added.
217-
*
218-
* Subdomains under the main domain are covered by the wildcard cert
219-
* or on-demand TLS, so no action needed.
220-
*
221-
* @param string $subdomain The subdomain being added.
222-
* @param int $site_id ID of the site.
223-
* @return void
216+
* {@inheritdoc}
224217
*/
225218
public function on_add_subdomain(string $subdomain, int $site_id): void {
226219
}
227220

228221
/**
229-
* Called when a subdomain is removed.
230-
*
231-
* @param string $subdomain The subdomain being removed.
232-
* @param int $site_id ID of the site.
233-
* @return void
222+
* {@inheritdoc}
234223
*/
235224
public function on_remove_subdomain(string $subdomain, int $site_id): void {
236225
}
237226

238-
/**
239-
* Add a Let's Encrypt TLS policy for a domain via Caddy's admin API.
240-
*
241-
* Caddy's Caddyfile adapter merges TLS policies when a catch-all block
242-
* uses a static cert, so named blocks don't get their own ACME policy.
243-
* This method patches the TLS connection policies at runtime to add
244-
* an ACME-backed policy for the domain before the static-cert catch-all.
245-
*
246-
* @param string $domain The domain to provision.
247-
* @return void
248-
*/
249-
private function provision_certificate(string $domain): void {
250-
251-
/** @var FrankenPHP_Integration */
252-
$frankenphp = $this->get_integration();
253-
254-
// Get current TLS connection policies.
255-
$current = $frankenphp->api_call(
256-
'/config/apps/http/servers/srv0/tls_connection_policies',
257-
[],
258-
'GET'
259-
);
260-
261-
if (is_wp_error($current) || ! is_array($current)) {
262-
if (function_exists('wu_log_add')) {
263-
wu_log_add('integration-frankenphp', "Failed to read TLS policies for {$domain}", LogLevel::ERROR);
264-
}
265-
return;
266-
}
267-
268-
// Check if this domain already has a policy.
269-
foreach ($current as $policy) {
270-
$sni = $policy['match']['sni'] ?? [];
271-
if (in_array($domain, $sni, true)) {
272-
if (function_exists('wu_log_add')) {
273-
wu_log_add('integration-frankenphp', "TLS policy already exists for {$domain}");
274-
}
275-
return;
276-
}
277-
}
278-
279-
// Insert a new ACME policy for this domain before the catch-all.
280-
// Policies without certificate_selection use Caddy's default (ACME/Let's Encrypt).
281-
$new_policy = ['match' => ['sni' => [$domain]]];
282-
283-
// Insert before the last entry (the catch-all with no SNI match).
284-
array_splice($current, count($current) - 1, 0, [$new_policy]);
285-
286-
$response = $frankenphp->api_call(
287-
'/config/apps/http/servers/srv0/tls_connection_policies',
288-
$current,
289-
'PATCH'
290-
);
291-
292-
if (is_wp_error($response)) {
293-
if (function_exists('wu_log_add')) {
294-
wu_log_add(
295-
'integration-frankenphp',
296-
"TLS policy error for {$domain}: " . $response->get_error_message(),
297-
LogLevel::ERROR
298-
);
299-
}
300-
} else {
301-
if (function_exists('wu_log_add')) {
302-
wu_log_add('integration-frankenphp', "Let's Encrypt TLS policy added for {$domain}");
303-
}
304-
}
305-
}
306-
307227
/**
308228
* {@inheritdoc}
309229
*/

0 commit comments

Comments
 (0)