| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla\Filter; |
| 4 |
|
| 5 |
class Autocomplete |
| 6 |
{ |
| 7 |
public function add_hooks() |
| 8 |
{ |
| 9 |
add_action('wp_ajax_boxzilla_autocomplete', array( $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'], array( 'page', 'post', 'category', 'post_type', 'post_tag' ))) ? $_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 |
|
| 28 |
default: |
| 29 |
case 'post': |
| 30 |
case 'page': |
| 31 |
echo $this->list_posts($q, $type); |
| 32 |
break; |
| 33 |
|
| 34 |
case 'category': |
| 35 |
echo $this->list_categories($q); |
| 36 |
break; |
| 37 |
|
| 38 |
case 'post_type': |
| 39 |
echo $this->list_post_types($q); |
| 40 |
break; |
| 41 |
|
| 42 |
case 'post_tag': |
| 43 |
echo $this->list_tags($q); |
| 44 |
break; |
| 45 |
} |
| 46 |
|
| 47 |
die(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @param string $query |
| 52 |
* @param string $post_type |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
protected function list_posts($query, $post_type = 'post') |
| 57 |
{ |
| 58 |
global $wpdb; |
| 59 |
$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 . '%%'); |
| 60 |
$post_slugs = $wpdb->get_col($sql); |
| 61 |
return join($post_slugs, PHP_EOL); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @param string $query |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
protected function list_categories($query) |
| 70 |
{ |
| 71 |
$terms = get_terms('category', array( 'name__like' => $query, 'fields' => 'names', 'hide_empty' => false )); |
| 72 |
return join($terms, PHP_EOL); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @param string $query |
| 77 |
* |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
protected function list_tags($query) |
| 81 |
{ |
| 82 |
$terms = get_terms('post_tag', array( 'name__like' => $query, 'fields' => 'names', 'hide_empty' => false )); |
| 83 |
return join($terms, PHP_EOL); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* @param string $query |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
protected function list_post_types($query) |
| 93 |
{ |
| 94 |
$post_types = get_post_types(array( 'public' => true ), 'names'); |
| 95 |
$matched_post_types = array_filter($post_types, function ($name) use ($query) { |
| 96 |
return strpos($name, $query) === 0; |
| 97 |
}); |
| 98 |
|
| 99 |
return join($matched_post_types, PHP_EOL); |
| 100 |
} |
| 101 |
} |
| 102 |
|