← Back
Editing: HubAccess.php
<?php namespace FluentForm\App\Modules\MCP\Support; defined('ABSPATH') || exit; use FluentForm\App\Modules\Acl\Acl; use FluentForm\App\Modules\MCP\MCPInit; /** * Who reaches the Fluent Forms tools: through FluentHub's one URL, and in the tool list of the * fluentform server. Both run only while Fluent Forms' MCP is on (MCPInit::init()). */ class HubAccess { /** * The permissions the tools check. A user who holds none of them is not admitted to the hub, * because every call would fail. A test keeps this list equal to the tools' declarations. */ const TOOL_PERMISSIONS = [ 'fluentform_dashboard_access', 'fluentform_forms_manager', 'fluentform_entries_viewer', 'fluentform_manage_entries', 'fluentform_view_payments', 'fluentform_settings_manager', ]; public static function register() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- FluentHub's hook add_filter('fluent_kit/mcp_hub_product_user_can_connect', [self::class, 'admitUser'], 10, 3); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- the MCP adapter's hook add_filter('mcp_adapter_tools_list', [self::class, 'listToolsForUser'], 10, 2); } /** * FluentHub admits a user who is not a site administrator to a product only when the product * says yes. Answers for the fluentform slug only, and never lowers a true it is handed. * Form scope does not count here: a manager limited to some forms is admitted, and each tool * keeps them to those forms. * * @param mixed $allowed * @param string $slug * @param \WP_User|null $user * @return mixed */ public static function admitUser($allowed, $slug, $user = null) { if ($allowed || MCPInit::SERVER_ID !== $slug) { return $allowed; } return Acl::userHasAnyPermission($user, self::TOOL_PERMISSIONS); } /** * Show a client of the fluentform server only the tools its user may run. Every Fluent Forms * permission check runs without input (form scope is checked inside the tool), so the * ability's own check decides, including a check that needs two permissions. * * @param array $tools * @param object $server * @return array */ public static function listToolsForUser($tools, $server = null) { $ours = is_object($server) && method_exists($server, 'get_server_id') && MCPInit::SERVER_ID === $server->get_server_id(); if (!is_array($tools) || !$ours || !function_exists('wp_has_ability')) { return $tools; } $prefix = MCPInit::SERVER_ID . '-'; return array_values(array_filter($tools, function ($tool) use ($prefix) { if (!is_object($tool) || !method_exists($tool, 'getName')) { return true; } // The adapter names a tool after its ability, with the slash as a dash. $name = (string) $tool->getName(); if (0 !== strpos($name, $prefix)) { return true; } // wp_get_ability() warns on an unknown name, so ask wp_has_ability() first. // An unknown tool stays listed; the call decides. $abilityName = MCPInit::SERVER_ID . '/' . substr($name, strlen($prefix)); $ability = wp_has_ability($abilityName) ? wp_get_ability($abilityName) : null; if (!$ability || !method_exists($ability, 'check_permissions')) { return true; } return true === $ability->check_permissions(); })); } }
Save File
Cancel