activities
4 months ago
agents
4 months ago
analytics
4 months ago
bookings
1 week ago
calendar
4 months ago
configs
4 months ago
customers
1 week ago
locations
4 months ago
orders
1 day ago
services
4 months ago
abstract-ability.php
1 day ago
class-latepoint-abilities.php
1 day ago
class-latepoint-abilities.php
181 lines
| 1 | <?php |
| 2 | /** |
| 3 | * LatePoint Abilities — Main loader & hook wiring. |
| 4 | * |
| 5 | * Registers the LatePoint category and all abilities with the WordPress |
| 6 | * Abilities API (requires WordPress 6.9+). The guarded loader in latepoint.php |
| 7 | * ensures this file is only included when the API is available. |
| 8 | * |
| 9 | * @package LatePoint\Abilities |
| 10 | * @since 5.3.0 |
| 11 | */ |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | class LatePointAbilities { |
| 18 | |
| 19 | /** @var string[] Config class map: module slug => class name */ |
| 20 | private static array $config_modules = [ |
| 21 | 'bookings' => 'LatePointAbilitiesBookings', |
| 22 | 'customers' => 'LatePointAbilitiesCustomers', |
| 23 | 'services' => 'LatePointAbilitiesServices', |
| 24 | 'agents' => 'LatePointAbilitiesAgents', |
| 25 | 'orders' => 'LatePointAbilitiesOrders', |
| 26 | 'locations' => 'LatePointAbilitiesLocations', |
| 27 | 'calendar' => 'LatePointAbilitiesCalendar', |
| 28 | 'analytics' => 'LatePointAbilitiesAnalytics', |
| 29 | 'activities' => 'LatePointAbilitiesActivities', |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * Boot: include module files and wire hooks. |
| 34 | */ |
| 35 | public static function init(): void { |
| 36 | self::include_modules(); |
| 37 | |
| 38 | // Reset LatePoint's current user on every request to ensure it reflects the latest WP user state. |
| 39 | // This is crucial for accurate permission checks in abilities, especially when user roles or capabilities have changed during the request lifecycle. |
| 40 | add_action( 'set_current_user', [ OsAuthHelper::class, 'reset_current_user' ], 1 ); |
| 41 | |
| 42 | add_action( 'wp_abilities_api_categories_init', [ __CLASS__, 'register_category' ] ); |
| 43 | add_action( 'wp_abilities_api_init', [ __CLASS__, 'register_all' ] ); |
| 44 | |
| 45 | // Wire the dedicated MCP server once all plugins are fully loaded. We use a |
| 46 | // late plugins_loaded priority (99, matching SureForms) so the MCP Adapter's |
| 47 | // classes are reliably available to mcp_adapter_enabled() regardless of plugin |
| 48 | // load order — important when another active plugin bundles its own adapter copy. |
| 49 | add_action( 'plugins_loaded', [ __CLASS__, 'maybe_register_mcp_server' ], 99999 ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Attach the dedicated MCP server registration when the adapter is active. |
| 54 | */ |
| 55 | public static function maybe_register_mcp_server(): void { |
| 56 | if ( self::mcp_adapter_enabled() ) { |
| 57 | add_action( 'mcp_adapter_init', [ __CLASS__, 'register_mcp_server' ] ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Whether the dedicated LatePoint MCP server should be registered. |
| 63 | * |
| 64 | * Requires the WordPress Abilities API and the MCP Adapter (WP\MCP\Plugin) |
| 65 | * to be available, and the MCP server toggle to be on. |
| 66 | */ |
| 67 | public static function mcp_adapter_enabled(): bool { |
| 68 | return function_exists( 'wp_register_ability' ) |
| 69 | && class_exists( 'WP\\MCP\\Plugin' ) |
| 70 | && OsSettingsHelper::is_on( 'latepoint_mcp_server' ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Register a dedicated LatePoint MCP server with the MCP Adapter. |
| 75 | * |
| 76 | * Endpoint: {site_url}/wp-json/latepoint/v1/mcp — exposes every registered |
| 77 | * latepoint/* ability as a tool. register_all() only registers abilities the |
| 78 | * Enable Edit / Enable Delete toggles allow, so the published tool set follows |
| 79 | * those toggles automatically (read-only when both are off; create/update and |
| 80 | * delete tools appear as those toggles are enabled). |
| 81 | * |
| 82 | * @param \WP\MCP\Core\McpAdapter $adapter The MCP adapter instance. |
| 83 | */ |
| 84 | public static function register_mcp_server( $adapter ): void { |
| 85 | $tools = []; |
| 86 | foreach ( wp_get_abilities() as $ability ) { |
| 87 | $name = $ability->get_name(); |
| 88 | if ( 0 === strpos( $name, 'latepoint/' ) ) { |
| 89 | $tools[] = $name; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Filters the tool ids exposed by the dedicated LatePoint MCP server. |
| 95 | * |
| 96 | * Defaults to every registered latepoint/* ability (already gated by the |
| 97 | * Enable Edit / Enable Delete toggles). Return a subset to publish fewer |
| 98 | * tools to AI clients. |
| 99 | * |
| 100 | * @param string[] $tools Ability ids exposed by the dedicated MCP server. |
| 101 | */ |
| 102 | $tools = apply_filters( 'latepoint_mcp_server_tool_ids', $tools ); |
| 103 | if ( empty( $tools ) ) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | $transport_class = class_exists( '\\WP\\MCP\\Transport\\HttpTransport' ) |
| 108 | ? \WP\MCP\Transport\HttpTransport::class |
| 109 | : \WP\MCP\Transport\Http\RestTransport::class; |
| 110 | |
| 111 | $adapter->create_server( |
| 112 | 'latepoint', |
| 113 | 'latepoint/v1', |
| 114 | 'mcp', |
| 115 | OsSettingsHelper::get_brand_name() . ' ' . __( 'MCP Server', 'latepoint' ), |
| 116 | sprintf( |
| 117 | /* translators: %s: brand name. */ |
| 118 | __( '%s MCP Server for managing bookings, customers, services, agents, locations, and orders.', 'latepoint' ), |
| 119 | OsSettingsHelper::get_brand_name() |
| 120 | ), |
| 121 | LATEPOINT_VERSION, |
| 122 | [ $transport_class ], |
| 123 | \WP\MCP\Infrastructure\ErrorHandling\ErrorLogMcpErrorHandler::class, |
| 124 | \WP\MCP\Infrastructure\Observability\NullMcpObservabilityHandler::class, |
| 125 | $tools, |
| 126 | [], |
| 127 | [] |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Include abstract base and all config module files. |
| 133 | */ |
| 134 | private static function include_modules(): void { |
| 135 | $base = LATEPOINT_ABSPATH . 'lib/abilities/'; |
| 136 | require_once $base . 'abstract-ability.php'; |
| 137 | foreach ( array_keys( self::$config_modules ) as $slug ) { |
| 138 | require_once $base . 'configs/class-latepoint-abilities-' . $slug . '.php'; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Register the top-level LatePoint category. |
| 144 | */ |
| 145 | public static function register_category(): void { |
| 146 | wp_register_ability_category( |
| 147 | 'latepoint', |
| 148 | [ |
| 149 | 'label' => OsSettingsHelper::get_brand_name(), |
| 150 | 'description' => __( 'Appointment scheduling — bookings, customers, services, and staff.', 'latepoint' ), |
| 151 | ] |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Register all abilities from every module. |
| 157 | * |
| 158 | * Read-only abilities register whenever the master toggle is on. |
| 159 | * Mutating abilities require the write toggle; destructive ones |
| 160 | * require the delete toggle. |
| 161 | */ |
| 162 | public static function register_all(): void { |
| 163 | $allow_write = OsSettingsHelper::is_on( 'latepoint_abilities_api_edit' ); |
| 164 | $allow_delete = OsSettingsHelper::is_on( 'latepoint_abilities_api_delete' ); |
| 165 | |
| 166 | foreach ( self::$config_modules as $class ) { |
| 167 | foreach ( $class::get_abilities() as $ability ) { |
| 168 | if ( $ability->is_destructive() && ! $allow_delete ) { |
| 169 | continue; |
| 170 | } |
| 171 | if ( ! $ability->is_read_only() && ! $ability->is_destructive() && ! $allow_write ) { |
| 172 | continue; |
| 173 | } |
| 174 | wp_register_ability( $ability->get_id(), $ability->to_definition() ); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | LatePointAbilities::init(); |
| 181 |