-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-humcore-deposits-subject-rest-controller.php
128 lines (100 loc) · 2.99 KB
/
class-humcore-deposits-subject-rest-controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* REST controller used by select2 to query terms.
*
* @package HumCORE
*/
/**
* Controller.
*/
class Humcore_Deposits_Subject_REST_Controller extends WP_REST_Controller {
/**
* Constructor.
*
* @since alpha
* @access public
*/
public function __construct() {
$this->namespace = 'humcore-deposits-subject/v1';
$this->rest_base = '/terms';
}
/**
* Registers the routes for the objects of the controller.
*
* @since alpha
* @access public
*
* @see register_rest_route()
*/
public function register_routes() {
register_rest_route(
$this->namespace, $this->rest_base, [
'methods' => 'GET',
'callback' => [ $this, 'get_terms' ],
]
);
}
/**
* Sort query results relative to user input:
* ( case-insensitive )
* 1 complete word matches
* 2 matches at beginning of term
* 3 matches elsewhere in term
*
* humcore_deposits_subject_list() already uses natcasesort(), this does additional sorting
*
* @param array $matched_terms natcasesort()ed array of term objects containing properties 'id' & 'text' to be sorted.
* @param string $user_input search query.
* @return array $matched_terms sorted terms.
*/
public static function sort_matched_terms( array $matched_terms, string $user_input ) {
$sorted_terms = [];
// Pull out matches at beginning of term first (complete word matches are first alphabetically).
foreach ( $matched_terms as $i => $matched_term ) {
if ( 0 === strpos( strtolower( $matched_term->text ), strtolower( $user_input ) ) ) {
$sorted_terms[] = $matched_term;
unset( $matched_terms[ $i ] );
}
}
// Append the remaining terms to the sorted terms.
$sorted_terms = array_merge( $sorted_terms, $matched_terms );
return $sorted_terms;
}
/**
* Get list of terms that match $_GET['q']
*
* @param WP_REST_Request $data request data (expected to contain a query in the 'q' parameter).
* @return WP_REST_Response
*/
public function get_terms( WP_REST_Request $data ) {
$start_time = microtime();
$params = $data->get_query_params();
$user_input = $params['q'];
$response = new WP_REST_Response;
$cache_key = 'humcore_deposits_subject_terms_' . sanitize_title( $user_input );
$matched_terms = wp_cache_get( $cache_key );
if ( ! $matched_terms ) {
$all_terms = humcore_deposits_subject_list();
$matched_terms = [];
// Populate array of matches.
foreach ( $all_terms as $term_id => $term ) {
if ( false !== strpos( strtolower( $term ), strtolower( $user_input ) ) ) {
$matched_term = new stdClass;
$matched_term->id = $term_id;
$matched_term->text = $term;
$matched_terms[] = $matched_term;
}
}
$matched_terms = self::sort_matched_terms( $matched_terms, $user_input );
wp_cache_set( $cache_key, $matched_terms, null, 300 );
}
// Formatted for select2 consumption.
$response->set_data(
[
'results' => $matched_terms,
// 'time' => microtime() - $start_time, // for debugging
]
);
return $response;
}
}