Completed
Pull Request — develop (#1237)
by Naveen
02:49
created

Faceted_Search_Template_Endpoint   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register_template_route() 0 18 2
A get_template() 0 6 2
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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_template', array() );
49
		return array_key_exists( $template_id, $templates ) ? $templates[$template_id] : '';
50
	}
51
}