Skip to content

Commit

Permalink
Merge pull request #54 from newfold-labs/release/2.1.0
Browse files Browse the repository at this point in the history
Release 2.1.0
  • Loading branch information
abhijitb authored Aug 30, 2024
2 parents d04b267 + 539217a commit 77e132e
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 91 deletions.
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-polyfill'), 'version' => 'df55f6ced101179f1862');
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '9f695924420aef9082a5');
2 changes: 1 addition & 1 deletion build/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 83 additions & 1 deletion build/index.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 62 additions & 58 deletions includes/MultiSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@ class MultiSearchController extends \WP_REST_Controller {
*/
protected $namespace = 'newfold-multi-search/v1';

/**
/**
* The base of this controller's route
*
* @var string
*/
protected $rest_base = 'multi_search';

/**
* The API key for the multi-search service
*
* @var string
*/
protected $apiKey;
* The API key for the multi-search service
*
* @var string
*/
protected $api_key;

/**
* The endpoint for the multi-search service
*
* @var string
*/
protected $endpoint;
/**
* The endpoint for the multi-search service
*
* @var string
*/
protected $endpoint;

/**
* Constructor to initialize the API key and endpoint
*/
public function __construct() {
$this->apiKey = 'B9wvYIokTPPgXEM3isTqsxbDOva21igT';
$this->endpoint = 'https://search.hiive.cloud/multi_search?x-typesense-api-key=' . $this->apiKey;
}
* Constructor to initialize the API key and endpoint
*/
public function __construct() {
$this->api_key = 'B9wvYIokTPPgXEM3isTqsxbDOva21igT';
$this->endpoint = 'https://search.hiive.cloud/multi_search?x-typesense-api-key=' . $this->api_key;
}

/**
/**
* Register the routes for this objects of the controller
*/
public function register_routes() {
Expand All @@ -64,57 +64,62 @@ public function register_routes() {
),
)
);
}
}

public function get_multi_search_result( \WP_REST_Request $request ) {
/**
* Fetch the result from typesense
*
* @param \WP_REST_Request $request the REST request object
*/
public function get_multi_search_result( \WP_REST_Request $request ) {
$brand = sanitize_text_field( $request->get_param( 'brand' ) );
$query = sanitize_text_field( $request->get_param( 'query' ) );
$params = [
'searches' => [
[
'q' => $query,
'query_by' => 'post_title,post_content',
'group_by' => 'post_title',
'group_limit' => 1,
'sort_by' => '_text_match:desc,post_likes:desc',
'filter_by' => 'post_category:=' . $brand,

$params = array(
'searches' => array(
array(
'q' => $query,
'query_by' => 'post_title,post_content',
'group_by' => 'post_title',
'group_limit' => 1,
'sort_by' => '_text_match:desc,post_likes:desc',
'filter_by' => 'post_category:=' . $brand,
'prioritize_token_position' => true,
'limit_hits' => 3,
'per_page' => 3,
'highlight_full_fields' => 'post_title,post_content',
'collection' => 'nfd_help_articles',
'page' => 1,
]
]
];
$args = [
'body' => json_encode( $params ),
'headers' => [
'Content-Type' => 'application/json',
'X-TYPESENSE-API-KEY' => $this->apiKey,
],
];
'limit_hits' => 3,
'per_page' => 3,
'highlight_full_fields' => 'post_title,post_content',
'collection' => 'nfd_help_articles',
'page' => 1,
),
),
);

$args = array(
'body' => wp_json_encode( $params ),
'headers' => array(
'Content-Type' => 'application/json',
'X-TYPESENSE-API-KEY' => $this->api_key,
),
);

$response = wp_remote_post( $this->endpoint, $args );
if ( is_wp_error( $response ) ) {
return new WP_Error( 'request_failed', 'The request failed', array( 'status' => 500 ) );
return new \WP_Error( 'request_failed', 'The request failed', array( 'status' => 500 ) );
}

$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
$data = json_decode( $body, true );
if ( empty( $data ) ) {
return new WP_Error( 'no_data', 'No data found', array( 'status' => 404 ) );
return new \WP_Error( 'no_data', 'No data found', array( 'status' => 404 ) );
}

return rest_ensure_response( $data );
}
}

/**
/**
* Check permissions for routes.
*
* @return \WP_Error
* @return \WP_Error|boolean
*/
public function check_permission() {
if ( ! current_user_can( 'manage_options' ) ) {
Expand All @@ -126,5 +131,4 @@ public function check_permission() {
}
return true;
}

}
}
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"algoliasearch": "4.20.0",
"lodash": "4.17.21",
"lodash.debounce": "4.0.8",
"marked": "14.0.0",
"react-instantsearch-hooks-web": "6.43.0",
"react-loader-spinner": "6.1.6",
"react-router-dom": "6.6.2",
Expand Down
16 changes: 15 additions & 1 deletion src/components/ResultContent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//
import Feedback from './Feedback';
import NoResults from './NoResults';
import { useEffect, useState } from 'react';
import { marked } from 'marked';

export const ResultContent = ( {
content,
Expand All @@ -13,10 +15,22 @@ export const ResultContent = ( {
return <NoResults />;
}

const MarkdownRenderer = ( { markdownText } ) => {
const [ htmlContent, setHtmlContent ] = useState( '' );

useEffect( () => {
// Convert Markdown to HTML whenever markdownText changes
const convertedHTML = marked( markdownText );
setHtmlContent( convertedHTML );
}, [ markdownText ] ); // Dependency array ensures this runs on markdownText change

return <p dangerouslySetInnerHTML={ { __html: htmlContent } } />;
};

if ( content && content.length > 0 ) {
return (
<>
<p dangerouslySetInnerHTML={ { __html: content } } />
<MarkdownRenderer markdownText={ content } />
{ showFeedbackSection && content && content.length > 0 && (
<Feedback postId={ postId } source={ source } />
) }
Expand Down
Loading

0 comments on commit 77e132e

Please sign in to comment.