| @@ -1,117 +1,95 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Boxzilla\Filter; |
| 4 | 4 | |
| 5 | -class Autocomplete | |
| 6 | -{ | |
| 7 | - public function init() | |
| 8 | - { | |
| 9 | - add_action('wp_ajax_boxzilla_autocomplete', [ $this, 'ajax' ]); | |
| 10 | - } | |
| 5 | +class Autocomplete { | |
| 11 | 6 | |
| 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'; | |
| 7 | + public function add_hooks() { | |
| 8 | + add_action( 'wp_ajax_boxzilla_autocomplete', array( $this, 'ajax' ) ); | |
| 9 | + } | |
| 19 | 10 | |
| 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 | - } | |
| 11 | + /** | |
| 12 | + * AJAX listener for autocomplete | |
| 13 | + */ | |
| 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'; | |
| 25 | 17 | |
| 26 | - switch ($type) { | |
| 27 | - default: | |
| 28 | - case 'post': | |
| 29 | - case 'page': | |
| 30 | - echo $this->list_posts($q, $type); | |
| 31 | - break; | |
| 18 | + // do nothing if supplied 'q' parameter is omitted or empty | |
| 19 | + // or less than 2 characters long | |
| 20 | + if( empty( $q ) || strlen( $q ) < 2 ) { | |
| 21 | + die(); | |
| 22 | + } | |
| 32 | 23 | |
| 33 | - case 'category': | |
| 34 | - echo $this->list_categories($q); | |
| 35 | - break; | |
| 24 | + switch( $type ) { | |
| 36 | 25 | |
| 37 | - case 'post_type': | |
| 38 | - echo $this->list_post_types($q); | |
| 39 | - break; | |
| 26 | + default: | |
| 27 | + case 'post': | |
| 28 | + case 'page': | |
| 29 | + echo $this->list_posts( $q, $type ); | |
| 30 | + break; | |
| 40 | 31 | |
| 41 | - case 'post_tag': | |
| 42 | - echo $this->list_tags($q); | |
| 43 | - break; | |
| 44 | - } | |
| 32 | + case 'category': | |
| 33 | + echo $this->list_categories( $q ); | |
| 34 | + break; | |
| 45 | 35 | |
| 46 | - die(); | |
| 47 | - } | |
| 36 | + case 'post_type': | |
| 37 | + echo $this->list_post_types( $q ); | |
| 38 | + break; | |
| 48 | 39 | |
| 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 | - } | |
| 40 | + case 'post_tag': | |
| 41 | + echo $this->list_tags( $q ); | |
| 42 | + break; | |
| 43 | + } | |
| 62 | 44 | |
| 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 | - } | |
| 45 | + die(); | |
| 46 | + } | |
| 80 | 47 | |
| 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 | - } | |
| 48 | + /** | |
| 49 | + * @param string $query | |
| 50 | + * @param string $post_type | |
| 51 | + * | |
| 52 | + * @return string | |
| 53 | + */ | |
| 54 | + protected function list_posts( $query, $post_type = 'post' ) { | |
| 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 . '%%' ); | |
| 57 | + $post_slugs = $wpdb->get_col( $sql ); | |
| 58 | + return join( $post_slugs, PHP_EOL ); | |
| 59 | + } | |
| 98 | 60 | |
| 61 | + /** | |
| 62 | + * @param string $query | |
| 63 | + * | |
| 64 | + * @return string | |
| 65 | + */ | |
| 66 | + protected function list_categories( $query ) { | |
| 67 | + $terms = get_terms( 'category', array( 'name__like' => $query, 'fields' => 'names', 'hide_empty' => false ) ); | |
| 68 | + return join( $terms, PHP_EOL ); | |
| 69 | + } | |
| 99 | 70 | |
| 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 | - ); | |
| 71 | + /** | |
| 72 | + * @param string $query | |
| 73 | + * | |
| 74 | + * @return string | |
| 75 | + */ | |
| 76 | + protected function list_tags( $query ) { | |
| 77 | + $terms = get_terms( 'post_tag', array( 'name__like' => $query, 'fields' => 'names', 'hide_empty' => false ) ); | |
| 78 | + return join( $terms, PHP_EOL ); | |
| 79 | + } | |
| 114 | 80 | |
| 115 | - return join(PHP_EOL, $matched_post_types); | |
| 116 | - } | |
| 117 | -} | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * @param string $query | |
| 84 | + * | |
| 85 | + * @return string | |
| 86 | + */ | |
| 87 | + 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 | + }); | |
| 92 | + | |
| 93 | + return join( $matched_post_types, PHP_EOL ); | |
| 94 | + } | |
| 95 | +} | |