| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla\Filter; |
| 4 |
|
| 5 |
class Autocomplete |
| 6 |
{ |
| 7 |
public function init() |
| 8 |
{ |
| 9 |
add_action('wp_ajax_boxzilla_autocomplete', [ $this, 'ajax' ]); |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* AJAX listener for autocomplete |
| 14 |
*/ |
| 15 |
public function ajax() |
| 16 |
{ |
| 17 |
$q = ( isset($_GET['q']) ) ? sanitize_text_field($_GET['q']) : ''; |
| 18 |
$type = ( isset($_GET['type']) && in_array($_GET['type'], [ 'page', 'post', 'category', 'post_type', 'post_tag' ], true) ) ? $_GET['type'] : 'post'; |
| 19 |
|
| 20 |
// do nothing if supplied 'q' parameter is omitted or empty |
| 21 |
// or less than 2 characters long |
| 22 |
if (empty($q) || strlen($q) < 2) { |
| 23 |
die(); |
| 24 |
} |
| 25 |
|
| 26 |
switch ($type) { |
| 27 |
default: |
| 28 |
case 'post': |
| 29 |
case 'page': |
| 30 |
echo $this->list_posts($q, $type); |
| 31 |
break; |
| 32 |
|
| 33 |
case 'category': |
| 34 |
echo $this->list_categories($q); |
| 35 |
break; |
| 36 |
|
| 37 |
case 'post_type': |
| 38 |
echo $this->list_post_types($q); |
| 39 |
break; |
| 40 |
|
| 41 |
case 'post_tag': |
| 42 |
echo $this->list_tags($q); |
| 43 |
break; |
| 44 |
} |
| 45 |
|
| 46 |
die(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param string $query |
| 51 |
* @param string $post_type |
| 52 |
* |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
protected function list_posts($query, $post_type = 'post') |
| 56 |
{ |
| 57 |
global $wpdb; |
| 58 |
$sql = $wpdb->prepare("SELECT p.post_name FROM $wpdb->posts p WHERE p.post_type = %s AND p.post_status = 'publish' AND ( p.post_title LIKE %s OR p.post_name LIKE %s ) GROUP BY p.post_name", $post_type, $query . '%%', $query . '%%'); |
| 59 |
$post_slugs = $wpdb->get_col($sql); |
| 60 |
return join(PHP_EOL, $post_slugs); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @param string $query |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
protected function list_categories($query) |
| 69 |
{ |
| 70 |
$terms = get_terms( |
| 71 |
'category', |
| 72 |
[ |
| 73 |
'name__like' => $query, |
| 74 |
'fields' => 'names', |
| 75 |
'hide_empty' => false, |
| 76 |
] |
| 77 |
); |
| 78 |
return join(PHP_EOL, $terms); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @param string $query |
| 83 |
* |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
protected function list_tags($query) |
| 87 |
{ |
| 88 |
$terms = get_terms( |
| 89 |
'post_tag', |
| 90 |
[ |
| 91 |
'name__like' => $query, |
| 92 |
'fields' => 'names', |
| 93 |
'hide_empty' => false, |
| 94 |
] |
| 95 |
); |
| 96 |
return join(PHP_EOL, $terms); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* @param string $query |
| 102 |
* |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
protected function list_post_types($query) |
| 106 |
{ |
| 107 |
$post_types = get_post_types([ 'public' => true ], 'names'); |
| 108 |
$matched_post_types = array_filter( |
| 109 |
$post_types, |
| 110 |
function ($name) use ($query) { |
| 111 |
return strpos($name, $query) === 0; |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
return join(PHP_EOL, $matched_post_types); |
| 116 |
} |
| 117 |
} |
| 118 |
|