|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Widgets\Faceted_Search; |
|
4
|
|
|
|
|
5
|
|
|
use WP_REST_Server; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @since ?.??.? |
|
9
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class Faceted_Search_Template_Endpoint { |
|
12
|
|
|
|
|
13
|
|
|
public function __construct() { |
|
14
|
|
|
add_action( 'rest_api_init', array( $this, 'register_template_route' ) ); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function register_template_route() { |
|
18
|
|
|
|
|
19
|
|
|
register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search/template/', array( |
|
20
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
21
|
|
|
'callback' => array( $this, 'get_template' ), |
|
22
|
|
|
/** |
|
23
|
|
|
* We want this endpoint to be publicly accessible |
|
24
|
|
|
*/ |
|
25
|
|
|
'permission_callback' => '__return_true', |
|
26
|
|
|
'args' => array( |
|
27
|
|
|
'template_id' => array( |
|
28
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
29
|
|
|
return is_string( $param ) && $param; |
|
30
|
|
|
}, |
|
31
|
|
|
), |
|
32
|
|
|
) |
|
33
|
|
|
) ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Faceted search widget makes call to this endpoint to get the template. |
|
38
|
|
|
* Takes the request, checks if template id is registered via filter, |
|
39
|
|
|
* if not it returns empty. |
|
40
|
|
|
* |
|
41
|
|
|
* @param $request \WP_REST_Request |
|
42
|
|
|
* |
|
43
|
|
|
* @return string Returns the template string. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function get_template( $request ) { |
|
46
|
|
|
$data = $request->get_params(); |
|
47
|
|
|
$template_id = (string) $data['template_id']; |
|
48
|
|
|
$templates = apply_filters( 'wordlift_faceted_search_templates', array() ); |
|
49
|
|
|
$template = array_key_exists( $template_id, $templates ) ? $templates[$template_id] : ''; |
|
50
|
|
|
return array( 'template' => $template ); |
|
51
|
|
|
} |
|
52
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.