| @@ -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', 'post_tag' ) ) ) ? $_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': |
| @@ -52,9 +52,9 @@ | ||
| 52 | 52 | * @return string |
| 53 | 53 | */ |
| 54 | 54 | protected function list_posts( $query, $post_type = 'post' ) { |
| 55 | 55 | global $wpdb; |
| 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 . '%%' ); | |
| 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 . '%%' ); | |
| 57 | 57 | $post_slugs = $wpdb->get_col( $sql ); |
| 58 | 58 | return join( $post_slugs, PHP_EOL ); |
| 59 | 59 | } |
| 60 | 60 | |
| @@ -63,9 +63,16 @@ | ||
| 63 | 63 | * |
| 64 | 64 | * @return string |
| 65 | 65 | */ |
| 66 | 66 | protected function list_categories( $query ) { |
| 67 | - $terms = get_terms( 'category', array( 'name__like' => $query, 'fields' => 'names', 'hide_empty' => false ) ); | |
| 67 | + $terms = get_terms( | |
| 68 | + 'category', | |
| 69 | + array( | |
| 70 | + 'name__like' => $query, | |
| 71 | + 'fields' => 'names', | |
| 72 | + 'hide_empty' => false, | |
| 73 | + ) | |
| 74 | + ); | |
| 68 | 75 | return join( $terms, PHP_EOL ); |
| 69 | 76 | } |
| 70 | 77 | |
| 71 | 78 | /** |
| @@ -73,9 +80,16 @@ | ||
| 73 | 80 | * |
| 74 | 81 | * @return string |
| 75 | 82 | */ |
| 76 | 83 | protected function list_tags( $query ) { |
| 77 | - $terms = get_terms( 'post_tag', array( 'name__like' => $query, 'fields' => 'names', 'hide_empty' => false ) ); | |
| 84 | + $terms = get_terms( | |
| 85 | + 'post_tag', | |
| 86 | + array( | |
| 87 | + 'name__like' => $query, | |
| 88 | + 'fields' => 'names', | |
| 89 | + 'hide_empty' => false, | |
| 90 | + ) | |
| 91 | + ); | |
| 78 | 92 | return join( $terms, PHP_EOL ); |
| 79 | 93 | } |
| 80 | 94 | |
| 81 | 95 | |
| @@ -84,12 +98,15 @@ | ||
| 84 | 98 | * |
| 85 | 99 | * @return string |
| 86 | 100 | */ |
| 87 | 101 | protected function list_post_types( $query ) { |
| 88 | - $post_types = get_post_types( array( 'public' => true ), 'names' ); | |
| 89 | - $matched_post_types = array_filter( $post_types, function( $name ) use( $query ) { | |
| 90 | - return strpos( $name, $query ) === 0; | |
| 91 | - }); | |
| 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 | + ); | |
| 92 | 109 | |
| 93 | 110 | return join( $matched_post_types, PHP_EOL ); |
| 94 | 111 | } |
| 95 | -} | |
| 112 | +} | |