← Back
Editing: RestRouteHint.php
<?php namespace WPForms\Integrations\Abilities; use WP_REST_Request; use WP_REST_Response; use WP_REST_Server; use WPForms\Integrations\IntegrationInterface; /** * Point REST requests to unknown WPForms routes at the Abilities API. * * AI clients often guess routes such as `/wpforms/v1/forms/1`, which do not exist because * WPForms data is served through abilities, so their 404 names the routes that do. * * @since 2.0.5.3 */ class RestRouteHint implements IntegrationInterface { /** * Prefix of the request routes that receive the hint. * * @since 2.0.5.3 */ private const ROUTE_PREFIX = '/wpforms/v1/'; /** * REST namespace of the Abilities API. * * @since 2.0.5.3 */ private const ABILITIES_REST_NAMESPACE = 'wp-abilities/v1'; /** * Indicate if the current integration is allowed to load. * * @since 2.0.5.3 * * @return bool */ public function allow_load(): bool { // The hint points to the Abilities API, which exists only in WordPress 6.9+. return function_exists( 'wp_register_ability' ); } /** * Load the integration. * * @since 2.0.5.3 */ public function load(): void { $this->hooks(); } /** * Register hooks. * * @since 2.0.5.3 */ private function hooks(): void { add_filter( 'rest_post_dispatch', [ $this, 'filter_rest_post_dispatch' ], 10, 3 ); } /** * Add the Abilities API hint to a request that matches no WPForms REST route. * * @since 2.0.5.3 * * @param WP_REST_Response|mixed $response Result to send to the client. * @param WP_REST_Server $server Server instance. * @param WP_REST_Request $request Request used to generate the response. * * @return WP_REST_Response|mixed */ public function filter_rest_post_dispatch( $response, $server, $request ) { if ( ! $this->is_unmatched_wpforms_route( $response, $server, $request ) ) { return $response; } $data = $response->get_data(); $data['message'] = $this->get_hint_message(); if ( ! isset( $data['data'] ) || ! is_array( $data['data'] ) ) { $data['data'] = []; } $data['data']['abilities_url'] = rest_url( self::ABILITIES_REST_NAMESPACE . '/abilities' ); $response->set_data( $data ); return $response; } /** * Whether the response is core's `rest_no_route` 404 for a WPForms route on a server with the Abilities API. * * @since 2.0.5.3 * * @param WP_REST_Response|mixed $response Result to send to the client. * @param WP_REST_Server $server Server instance. * @param WP_REST_Request $request Request used to generate the response. * * @return bool */ private function is_unmatched_wpforms_route( $response, $server, $request ): bool { if ( ! $response instanceof WP_REST_Response || $response->get_status() !== 404 ) { return false; } $data = $response->get_data(); if ( ! is_array( $data ) || ( $data['code'] ?? '' ) !== 'rest_no_route' ) { return false; } if ( stripos( (string) $request->get_route(), self::ROUTE_PREFIX ) !== 0 ) { return false; } // The hint names an Abilities API route, so give it only when that route is registered. return in_array( self::ABILITIES_REST_NAMESPACE, $server->get_namespaces(), true ); } /** * Get the message that points to the Abilities API routes. * * @since 2.0.5.3 * * @return string */ private function get_hint_message(): string { return sprintf( /* translators: %1$s - REST route that lists the abilities, %2$s - REST route that runs an ability. */ __( 'No WPForms REST route matches this request. WPForms data is served through the Abilities API: list the available abilities with %1$s, then run one at %2$s.', 'wpforms-lite' ), 'GET /' . self::ABILITIES_REST_NAMESPACE . '/abilities', '/' . self::ABILITIES_REST_NAMESPACE . '/abilities/' . Abilities::ABILITY_NAMESPACE . '/<ability>/run' ); } }
Save File
Cancel