← Back
Editing: HubConnect.php
<?php namespace FluentForm\App\Modules\MCP\Support; defined('ABSPATH') || exit; use FluentForm\App\Modules\MCP\MCPInit; /** * FluentHub's one-URL connection, for the card on the MCP settings screen. * * FluentHub is read and switched on through two filters only, never through its classes: * the filters are its stable surface. The contract is fluent-toolkit * docs/mcp-product-connect.md, version 1. */ class HubConnect { /** The contract version this code reads. Another version gives no card. */ const CONTRACT_VERSION = 1; /** * What the card needs, or null when there is no card: FluentHub is not active, too old to * answer, or answers in a version this code does not know. * * @return array|null */ public static function state() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- FluentHub's hook $state = apply_filters('fluent_kit/mcp_hub_state', null, MCPInit::SERVER_ID); return self::normalize($state); } /** * The card's fields from FluentHub's answer, cleaned; null for an answer this code cannot read. * * @param mixed $state The raw answer of fluent_kit/mcp_hub_state. * @return array|null */ public static function normalize($state) { if (!is_array($state) || !isset($state['version']) || self::CONTRACT_VERSION !== (int) $state['version'] || empty($state['hub']) || !is_array($state['hub']) || empty($state['sign_in']) || !is_array($state['sign_in']) || empty($state['product']) || !is_array($state['product']) ) { return null; } // FluentHub's own wording, printed as given, so this screen and FluentHub's say the same. $clients = []; foreach ((array) self::get($state, 'clients', []) as $client) { if (!is_array($client) || empty($client['key']) || empty($client['snippet'])) { continue; } $clients[] = [ 'key' => sanitize_key($client['key']), 'label' => sanitize_text_field((string) self::get($client, 'label', $client['key'])), 'note' => sanitize_text_field((string) self::get($client, 'note', '')), 'snippet' => (string) $client['snippet'], ]; } return [ // Nothing to copy means the one URL is not usable from this screen. 'ready' => !empty($state['ready']) && !empty($clients), 'hub_enabled' => !empty($state['hub']['enabled']), 'sign_in_enabled' => !empty($state['sign_in']['enabled']), 'endpoint_url' => esc_url_raw((string) self::get($state['hub'], 'endpoint_url', '')), 'hub_reason' => sanitize_key((string) self::get($state['hub'], 'reason', '')), 'sign_in_reason' => sanitize_key((string) self::get($state['sign_in'], 'reason', '')), 'included' => !empty($state['product']['included']), 'user_can_connect' => !empty($state['user_can_connect']), 'can_manage' => !empty($state['can_manage']), 'manage_url' => esc_url_raw((string) self::get($state, 'manage_url', '')), 'profile_url' => esc_url_raw((string) self::get($state, 'profile_url', '')), 'clients' => $clients, ]; } /** * Turn on the FluentHub MCP server. Nothing here turns anything off: FluentHub's hook has no * such argument. The hook checks manage_options; the caller is a POST route that checked a * nonce and the admin policy. * * @param mixed $result For tests: an answer in place of the filter's. * @return array status (done|forbidden|unavailable), message, and hub (the fresh card) when done. */ public static function enable($result = null) { if (null === $result) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- FluentHub's hook $result = apply_filters('fluent_kit/mcp_hub_enable', null, MCPInit::SERVER_ID, 'hub'); } // null: FluentHub is not active, or is a release without this call. if (!is_array($result)) { return [ 'status' => 'unavailable', 'message' => __('FluentHub is not active on this site.', 'fluentform'), ]; } $status = (string) self::get($result, 'status', ''); $reason = (string) self::get($result, 'reason', ''); if ('done' === $status) { $fresh = isset($result['state']) && is_array($result['state']) ? $result['state'] : null; return [ 'status' => 'done', 'message' => __('The FluentHub MCP server is on.', 'fluentform'), 'hub' => null === $fresh ? self::state() : self::normalize($fresh), ]; } if ('forbidden' === $status) { $refusal = 'product_refused' === $reason ? sanitize_text_field((string) self::get($result, 'message', '')) : ''; return [ 'status' => 'forbidden', 'message' => '' !== $refusal ? $refusal : __('Only a site administrator can turn this on.', 'fluentform'), ]; } $messages = [ 'adapter_missing' => __('FluentHub needs a newer MCP adapter before this can be turned on.', 'fluentform'), 'adapter_outdated' => __('FluentHub needs a newer MCP adapter before this can be turned on.', 'fluentform'), 'unknown_product' => __('FluentHub does not know this plugin.', 'fluentform'), 'not_active' => __('This plugin is not active on the site.', 'fluentform'), 'no_handler' => __('This plugin cannot be turned on from here. Use its own MCP settings.', 'fluentform'), ]; return [ 'status' => 'unavailable', 'message' => isset($messages[$reason]) ? $messages[$reason] : __('This could not be turned on. Open FluentHub to check its MCP settings.', 'fluentform'), ]; } private static function get(array $data, $key, $fallback) { return array_key_exists($key, $data) ? $data[$key] : $fallback; } }
Save File
Cancel