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