Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 100
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MultiSearchController
0.00% covered (danger)
0.00%
0 / 100
0.00% covered (danger)
0.00%
0 / 5
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
2
 get_multi_search_result
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
12
 get_tooltip_search_result
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
20
 check_permission
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\HelpCenter;
4
5/**
6 * APIs for getting the result from the Type Sense (Multi Site search)
7 */
8class MultiSearchController extends \WP_REST_Controller {
9
10    /**
11     * The namespace of this controller's route.
12     *
13     * @var string
14     */
15    protected $namespace = 'newfold-multi-search/v1';
16
17    /**
18     * The base of this controller's route
19     *
20     * @var string
21     */
22    protected $rest_base = 'multi_search';
23
24    /**
25     * The API key for the multi-search service
26     *
27     * @var string
28     */
29    protected $api_key;
30
31    /**
32     * The endpoint for the multi-search service
33     *
34     * @var string
35     */
36    protected $endpoint;
37
38    /**
39     * Constructor to initialize the API key and endpoint
40     */
41    public function __construct() {
42        $this->api_key  = 'B9wvYIokTPPgXEM3isTqsxbDOva21igT';
43        $this->endpoint = 'https://search.hiive.cloud/multi_search?x-typesense-api-key=' . $this->api_key;
44    }
45
46    /**
47     * Register the routes for this objects of the controller
48     */
49    public function register_routes() {
50        register_rest_route(
51            $this->namespace,
52            '/' . $this->rest_base,
53            array(
54                array(
55                    'methods'             => \WP_REST_Server::CREATABLE,
56                    'callback'            => array( $this, 'get_multi_search_result' ),
57                    'args'                => array(
58                        'query' => array(
59                            'required' => true,
60                            'type'     => 'string',
61                        ),
62                    ),
63                    'permission_callback' => array( $this, 'check_permission' ),
64                ),
65            )
66        );
67
68        /**
69         * Register the routes for this objects of the controller
70         */
71        register_rest_route(
72            $this->namespace,
73            '/tootip_search',
74            array(
75                array(
76                    'methods'             => \WP_REST_Server::CREATABLE,
77                    'callback'            => array( $this, 'get_tooltip_search_result' ),
78                    'args'                => array(
79                        'postId' => array(
80                            'required' => true,
81                            'type'     => 'string',
82                        ),
83                    ),
84                    'permission_callback' => array( $this, 'check_permission' ),
85                ),
86            )
87        );
88    }
89
90    /**
91     * Fetch the result from typesense
92     *
93     * @param \WP_REST_Request $request the REST request object
94     */
95    public function get_multi_search_result( \WP_REST_Request $request ) {
96        $brand = sanitize_text_field( $request->get_param( 'brand' ) );
97        $query = sanitize_text_field( $request->get_param( 'query' ) );
98
99        $params = array(
100            'searches' => array(
101                array(
102                    'q'                         => $query,
103                    'query_by'                  => 'post_title,post_content',
104                    'group_by'                  => 'post_title',
105                    'group_limit'               => 1,
106                    'sort_by'                   => '_text_match:desc,post_likes:desc',
107                    'filter_by'                 => 'post_category:=' . $brand,
108                    'prioritize_token_position' => true,
109                    'limit_hits'                => 3,
110                    'per_page'                  => 3,
111                    'highlight_full_fields'     => 'post_title,post_content',
112                    'collection'                => 'nfd_help_articles',
113                    'page'                      => 1,
114                ),
115            ),
116        );
117
118        $args = array(
119            'body'    => wp_json_encode( $params ),
120            'headers' => array(
121                'Content-Type'        => 'application/json',
122                'X-TYPESENSE-API-KEY' => $this->api_key,
123            ),
124        );
125
126        $response = wp_remote_post( $this->endpoint, $args );
127        if ( is_wp_error( $response ) ) {
128            return new \WP_Error( 'request_failed', __( 'The request failed', 'wp-module-help-center' ), array( 'status' => 500 ) );
129        }
130
131        $body = wp_remote_retrieve_body( $response );
132        $data = json_decode( $body, true );
133        if ( empty( $data ) ) {
134            return new \WP_Error( 'no_data', __( 'No data found', 'wp-module-help-center' ), array( 'status' => 404 ) );
135        }
136
137        return rest_ensure_response( $data );
138    }
139
140    /**
141     * Fetch the result from typesense
142     *
143     * @param \WP_REST_Request $request the REST request object
144     */
145    public function get_tooltip_search_result( \WP_REST_Request $request ) {
146        $postId = sanitize_text_field( $request->get_param( 'postId' ) );
147
148        $url = USER_INTERACTION_SERVICE_BASE . 'postContent/';
149
150        $args = array(
151            'method'  => 'POST',
152            'headers' => array(
153                'Content-Type' => 'application/json',
154            ),
155            'timeout' => 60,
156            'body'    => wp_json_encode(
157                array(
158                    'postId' => $postId,
159                )
160            ),
161        );
162
163        $response = wp_remote_post( $url, $args );
164        if ( is_wp_error( $response ) ) {
165            return new \WP_Error( 'request_failed', __( 'The request failed', 'wp-module-help-center' ), array( 'status' => 500 ) );
166        }
167
168        $body = wp_remote_retrieve_body( $response );
169        $data = json_decode( $body, true );
170        if ( isset( $data['data']['status'] ) && 404 === $data['data']['status'] ) {
171            return new \WP_Error( 'no_data', __( 'No data found', 'wp-module-help-center' ), array( 'status' => 404 ) );
172        }
173
174        return rest_ensure_response( $data );
175    }
176
177    /**
178     * Check permissions for routes.
179     *
180     * @return \WP_Error|boolean
181     */
182    public function check_permission() {
183        if ( ! current_user_can( 'manage_options' ) ) {
184            return new \WP_Error(
185                'rest_forbidden',
186                __( 'You must be authenticated to make this call', 'wp-module-help-center' ),
187                array( 'status' => 401 )
188            );
189        }
190        return true;
191    }
192}