* @copyright 2026 webhead GmbH */ declare( strict_types=1 ); namespace Webhead\WP\SearchCloud\Routing; use WP_REST_Request; use WP_REST_Response; use Webhead\WP\SearchCloud\Capabilities\Plugin_Capabilities; use Webhead\WP\SearchCloud\Connection\Api_Response; use Webhead\WP\SearchCloud\Connection\Search_Cloud_Client; use Webhead\WP\SearchCloud\Indexing\Facet_Config; use Webhead\WP\SearchCloud\Indexing\Language_Config; use Webhead\WP\SearchCloud\Indexing\Woo; use Webhead\WP\SearchCloud\Dependencies\Webhead\WP\Framework\Attributes\Route; use Webhead\WP\SearchCloud\Dependencies\Webhead\WP\Framework\Routing\Rest_API; defined( 'ABSPATH' ) || exit; /** * @since 1.0.0 */ final class Search_Config_Endpoint extends Rest_API { public function __construct( private Search_Cloud_Client $client, private Language_Config $language_config, private Facet_Config $facet_config, private Woo $woo ) {} protected function get_rest_namespace(): string { return 'searcio/v1'; } #[Route( '/search-config', methods: 'GET', permission_callback: 'can_view' )] public function get_config(): WP_REST_Response { return $this->respond( $this->client->get_search_config() ); } #[Route( '/search-config', methods: 'PUT', permission_callback: 'can_manage_settings' )] public function update_config( WP_REST_Request $request ): WP_REST_Response { $payload = $request->get_json_params(); if ( ! is_array( $payload ) ) { return new WP_REST_Response( [ 'message' => __( 'Invalid search configuration payload.', 'searcio' ) ], 400 ); } // The shop's currency goes with every save; the tab has no field for it. $currency = $this->woo->currency(); if ( null !== $currency ) { $payload['currency'] = $currency; } return $this->respond( $this->client->update_search_config( $payload ) ); } public function can_view(): bool { return current_user_can( Plugin_Capabilities::VIEW ); } public function can_manage_settings(): bool { return current_user_can( Plugin_Capabilities::MANAGE_SETTINGS ); } private function respond( Api_Response $response ): WP_REST_Response { if ( $response->transport_error ) { return new WP_REST_Response( [ 'message' => __( 'Could not reach Searcio.', 'searcio' ) ], 502 ); } // Mirror `languages` and `facet_fields` locally on every successful read/write so // `Document_Builder`, the front-end language filter and the search window's filter // order don't need a live API round trip on every sync or page load. if ( $response->ok() ) { $this->language_config->set_enabled_languages( $response->body()['languages'] ?? [] ); $this->facet_config->set_fields( $response->body()['facet_fields'] ?? [] ); } return new WP_REST_Response( $response->body(), $response->status ); } }