← Back
Editing: ConnectionName.php
<?php namespace FluentForm\App\Modules\MCP\Support; defined('ABSPATH') || exit; use FluentForm\App\Modules\MCP\AbilitiesRegistrar; /** * The name an AI app gives this site's Fluent Forms connection in the application-password * snippets: a short product code plus the site address, for example ff-example-com. * * It carries the site address because an app refuses a second connection with a name it * already has, so an owner of two sites could not add both under one fixed name. */ class ConnectionName { /** Short product code that starts the name. */ const PREFIX = 'ff'; /** AI apps build a tool name as mcp__{connection}__{tool} and refuse one longer than this. */ const MAX_TOOL_NAME = 64; /** * The connection name for a site address. An address that does not fit keeps its start and * gets a short hash, so two sites with the same start still differ. A site in a * subdirectory shares its host with its neighbours, so the path counts too. * * @param string|null $homeUrl The site address; null reads home_url(). * @param int|null $longestToolLength null measures the tool names. * @return string */ public static function name($homeUrl = null, $longestToolLength = null) { if (null === $homeUrl) { $homeUrl = home_url(); } if (null === $longestToolLength) { $longestToolLength = self::longestToolNameLength(); } $homeUrl = (string) $homeUrl; // A bare host is accepted too. $withScheme = false === strpos($homeUrl, '//') ? '//' . $homeUrl : $homeUrl; $parts = wp_parse_url($withScheme); $host = is_array($parts) && !empty($parts['host']) ? strtolower($parts['host']) : ''; $path = is_array($parts) && !empty($parts['path']) ? strtolower($parts['path']) : ''; $address = preg_replace('/^www\./', '', $host) . $path; $key = trim((string) preg_replace('/[^a-z0-9]+/', '-', $address), '-'); // mcp__ + name + __ + tool $room = self::MAX_TOOL_NAME - strlen('mcp__') - strlen('__') - (int) $longestToolLength - strlen(self::PREFIX . '-'); if ('' === $key || $room < 1) { return self::PREFIX; } if (strlen($key) <= $room) { return self::PREFIX . '-' . $key; } $hash = substr(md5($key), 0, 6); // No room for a start and a hash: the hash alone still tells sites apart. if ($room < strlen($hash) + 2) { return self::PREFIX . '-' . substr($hash, 0, $room); } $start = rtrim(substr($key, 0, $room - strlen($hash) - 1), '-'); return self::PREFIX . '-' . ('' === $start ? '' : $start . '-') . $hash; } /** * Length of the longest tool name as an app sees it (the ability name with the slash as a * dash). The catalogue lists Pro's tools whether Pro is active or not, so the name does not * change when Pro is switched. * * @return int */ public static function longestToolNameLength() { $longest = 0; foreach (AbilitiesRegistrar::catalogue() as $tool) { $toolName = str_replace('/', '-', (string) $tool['name']); $longest = max($longest, strlen($toolName)); } return $longest; } }
Save File
Cancel