WP_REST_Server::READABLE, 'callback' => array( $this, 'get_search' ), 'permission_callback' => $this->require_cap( 'agyl_manage' ), 'args' => array( 'q' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), 'limit' => array( 'type' => 'integer', 'default' => 8, 'minimum' => 1, 'maximum' => 20, ), 'lang' => array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), ), ) ); } /** * GET /kb/search. * * @param WP_REST_Request $request Request. */ public function get_search( WP_REST_Request $request ): WP_REST_Response { $query = trim( (string) $request['q'] ); $limit = max( 1, min( 20, (int) $request['limit'] ) ); $lang = (string) $request['lang']; $results = $this->retriever->search( $query, array( 'lang' => $lang, 'limit' => $limit, ) ); foreach ( $results as &$row ) { if ( mb_strlen( $row['content'] ) > self::CONTENT_TRUNCATE ) { $row['content'] = mb_substr( $row['content'], 0, self::CONTENT_TRUNCATE ) . '…'; } } unset( $row ); return $this->respond( array( 'query' => $query, 'count' => count( $results ), 'results' => $results, ) ); } }