.*)', array( 'methods' => array( 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' ), 'callback' => array( self::class, 'woocommerce_required_error' ), 'permission_callback' => '__return_true', ) ); } } /** * Covers the one request shape register_routes()'s own catch-all cannot * match: the bare namespace root with no trailing path segment at all * (e.g. GET /wp-json/aipt/v1, no trailing slash). * * WP_REST_Server::register_route() auto-registers a namespace-index * route ('/aipt/v1', mapped to core's own get_namespace_index()) as a * side effect the first time any route is registered under a namespace * -- which is exactly what register_routes() above triggers. That * auto-added route is matched before our own catch-all (whose pattern * requires a literal '/' after the namespace), so a bare request would * otherwise still reach core's generic 200 index listing instead of this * class's documented 424 contract. The rest_pre_dispatch filter runs * before any route matching, so it is the standard way to intercept a * request ahead of that side effect. * * @param mixed $response Pre-calculated dispatch result, or null to proceed with normal routing. * @param \WP_REST_Server $server REST server instance (unused). * @param \WP_REST_Request $request Request being dispatched. * @return mixed */ public static function intercept_bare_namespace_index( $response, $server, $request ) { if ( null !== $response ) { return $response; } $route = rtrim( $request->get_route(), '/' ); foreach ( self::NAMESPACES as $namespace ) { if ( '/' . $namespace === $route ) { return self::woocommerce_required_error(); } } return $response; } /** * The plugin's own documented contract for every aipt/v1 and wp/v2/aipt * route when WooCommerce is inactive, matching every real controller's * check_woocommerce_active() gate. * * @return \WP_Error */ public static function woocommerce_required_error(): \WP_Error { return new \WP_Error( 'woocommerce_required', __( 'WooCommerce is required for this feature.', 'ai-product-tools' ), array( 'status' => 424 ) ); } }