store = $store ?? static function (): Store { return new WpStore(); }; } /** * The route's URL, for the page's script. * * @return string */ public static function url(): string { return rest_url( RestController::REST_NAMESPACE . self::ROUTE ); } /** * Registers the route. Runs on `rest_api_init`. */ public function register(): void { register_rest_route( RestController::REST_NAMESPACE, self::ROUTE, array( 'methods' => 'GET', 'callback' => array( $this, 'handle' ), 'permission_callback' => array( self::class, 'permission' ), ) ); } /** * Only an administrator who may use FlitAgent. * * @return bool */ public static function permission(): bool { return current_user_can( Admin::CAPABILITY ); } /** * The state. * * @return \WP_REST_Response */ public function handle() { $response = new \WP_REST_Response( $this->state(), 200 ); $response->header( 'Cache-Control', 'no-store' ); return $response; } /** * `{ connected: bool, tools: { chatgpt|claude|manual: "connected"|"not_connected" } }`. * * @return array{connected: bool, tools: array} */ public function state(): array { $store = call_user_func( $this->store ); $connected = null !== $store->connection(); $known = $connected ? ( new Apps( $store ) )->tools() : array(); $tools = array(); foreach ( Apps::TOOLS as $tool ) { $tools[ $tool ] = isset( $known[ $tool ] ) ? self::CONNECTED : self::NOT_CONNECTED; } return array( 'connected' => $connected, 'tools' => $tools, ); } }