| @@ -1,151 +1,112 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Boxzilla\Filter; |
| 4 | 4 | |
| 5 | -if (! defined('ABSPATH')) { | |
| 6 | - exit; | |
| 7 | -} | |
| 5 | +class Autocomplete { | |
| 8 | 6 | |
| 9 | -class Autocomplete | |
| 10 | -{ | |
| 11 | - private const MAX_RESULTS = 20; | |
| 7 | + public function init() { | |
| 8 | + add_action( 'wp_ajax_boxzilla_autocomplete', array( $this, 'ajax' ) ); | |
| 9 | + } | |
| 12 | 10 | |
| 13 | - public function init(): void | |
| 14 | - { | |
| 15 | - add_action('wp_ajax_boxzilla_autocomplete', [ $this, 'ajax' ], 10, 0); | |
| 16 | - } | |
| 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' ), true ) ) ? $_GET['type'] : 'post'; | |
| 17 | 17 | |
| 18 | - /** | |
| 19 | - * AJAX listener for autocomplete | |
| 20 | - */ | |
| 21 | - public function ajax(): void | |
| 22 | - { | |
| 23 | - if (! current_user_can('edit_box')) { | |
| 24 | - wp_die('', '', [ 'response' => 403 ]); | |
| 25 | - } | |
| 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 | + } | |
| 26 | 23 | |
| 27 | - $q = isset($_GET['q']) ? sanitize_text_field(wp_unslash($_GET['q'])) : ''; | |
| 28 | - $allowed_types = [ 'page', 'post', 'category', 'post_type', 'post_tag' ]; | |
| 29 | - $type = isset($_GET['type']) ? sanitize_key(wp_unslash($_GET['type'])) : ''; | |
| 30 | - if (! in_array($type, $allowed_types, true)) { | |
| 31 | - $type = 'post'; | |
| 32 | - } | |
| 24 | + switch ( $type ) { | |
| 33 | 25 | |
| 34 | - // do nothing if supplied 'q' parameter is omitted or empty | |
| 35 | - // or less than 2 characters long | |
| 36 | - if (empty($q) || strlen($q) < 2) { | |
| 37 | - wp_die(); | |
| 38 | - } | |
| 26 | + default: | |
| 27 | + case 'post': | |
| 28 | + case 'page': | |
| 29 | + echo $this->list_posts( $q, $type ); | |
| 30 | + break; | |
| 39 | 31 | |
| 40 | - switch ($type) { | |
| 41 | - default: | |
| 42 | - case 'post': | |
| 43 | - case 'page': | |
| 44 | - echo esc_html($this->list_posts($q, $type)); | |
| 45 | - break; | |
| 32 | + case 'category': | |
| 33 | + echo $this->list_categories( $q ); | |
| 34 | + break; | |
| 46 | 35 | |
| 47 | - case 'category': | |
| 48 | - echo esc_html($this->list_categories($q)); | |
| 49 | - break; | |
| 36 | + case 'post_type': | |
| 37 | + echo $this->list_post_types( $q ); | |
| 38 | + break; | |
| 50 | 39 | |
| 51 | - case 'post_type': | |
| 52 | - echo esc_html($this->list_post_types($q)); | |
| 53 | - break; | |
| 40 | + case 'post_tag': | |
| 41 | + echo $this->list_tags( $q ); | |
| 42 | + break; | |
| 43 | + } | |
| 54 | 44 | |
| 55 | - case 'post_tag': | |
| 56 | - echo esc_html($this->list_tags($q)); | |
| 57 | - break; | |
| 58 | - } | |
| 45 | + die(); | |
| 46 | + } | |
| 59 | 47 | |
| 60 | - wp_die(); | |
| 61 | - } | |
| 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 | + } | |
| 62 | 60 | |
| 63 | - /** | |
| 64 | - * @param string $query | |
| 65 | - * @param string $post_type | |
| 66 | - * | |
| 67 | - * @return string | |
| 68 | - */ | |
| 69 | - protected function list_posts($query, $post_type = 'post') | |
| 70 | - { | |
| 71 | - global $wpdb; | |
| 72 | - $like = $wpdb->esc_like($query) . '%'; | |
| 73 | - $limit = self::MAX_RESULTS; | |
| 61 | + /** | |
| 62 | + * @param string $query | |
| 63 | + * | |
| 64 | + * @return string | |
| 65 | + */ | |
| 66 | + protected function list_categories( $query ) { | |
| 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 ); | |
| 76 | + } | |
| 74 | 77 | |
| 75 | - $post_slugs = $wpdb->get_col( | |
| 76 | - $wpdb->prepare( | |
| 77 | - "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 ORDER BY p.post_name ASC LIMIT %d", | |
| 78 | - $post_type, | |
| 79 | - $like, | |
| 80 | - $like, | |
| 81 | - $limit | |
| 82 | - ) | |
| 83 | - ); | |
| 84 | - return join(PHP_EOL, $post_slugs); | |
| 85 | - } | |
| 78 | + /** | |
| 79 | + * @param string $query | |
| 80 | + * | |
| 81 | + * @return string | |
| 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 | + } | |
| 86 | 94 | |
| 87 | - /** | |
| 88 | - * @param string $query | |
| 89 | - * | |
| 90 | - * @return string | |
| 91 | - */ | |
| 92 | - protected function list_categories($query) | |
| 93 | - { | |
| 94 | - $terms = get_terms([ | |
| 95 | - 'taxonomy' => 'category', | |
| 96 | - 'name__like' => $query, | |
| 97 | - 'number' => self::MAX_RESULTS, | |
| 98 | - 'fields' => 'names', | |
| 99 | - 'hide_empty' => false, | |
| 100 | - ]); | |
| 101 | 95 | |
| 102 | - if (is_wp_error($terms)) { | |
| 103 | - return ''; | |
| 104 | - } | |
| 96 | + /** | |
| 97 | + * @param string $query | |
| 98 | + * | |
| 99 | + * @return string | |
| 100 | + */ | |
| 101 | + protected function list_post_types( $query ) { | |
| 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 | + ); | |
| 105 | 109 | |
| 106 | - return join(PHP_EOL, $terms); | |
| 107 | - } | |
| 108 | - | |
| 109 | - /** | |
| 110 | - * @param string $query | |
| 111 | - * | |
| 112 | - * @return string | |
| 113 | - */ | |
| 114 | - protected function list_tags($query) | |
| 115 | - { | |
| 116 | - $terms = get_terms([ | |
| 117 | - 'taxonomy' => 'post_tag', | |
| 118 | - 'name__like' => $query, | |
| 119 | - 'number' => self::MAX_RESULTS, | |
| 120 | - 'fields' => 'names', | |
| 121 | - 'hide_empty' => false, | |
| 122 | - ]); | |
| 123 | - | |
| 124 | - if (is_wp_error($terms)) { | |
| 125 | - return ''; | |
| 126 | - } | |
| 127 | - | |
| 128 | - return join(PHP_EOL, $terms); | |
| 129 | - } | |
| 130 | - | |
| 131 | - | |
| 132 | - /** | |
| 133 | - * @param string $query | |
| 134 | - * | |
| 135 | - * @return string | |
| 136 | - */ | |
| 137 | - protected function list_post_types($query) | |
| 138 | - { | |
| 139 | - $post_types = get_post_types([ 'public' => true ], 'names'); | |
| 140 | - $matched_post_types = array_filter( | |
| 141 | - $post_types, | |
| 142 | - function ($name) use ($query) { | |
| 143 | - return strpos($name, $query) === 0; | |
| 144 | - } | |
| 145 | - ); | |
| 146 | - | |
| 147 | - $matched_post_types = array_slice($matched_post_types, 0, self::MAX_RESULTS); | |
| 148 | - | |
| 149 | - return join(PHP_EOL, $matched_post_types); | |
| 150 | - } | |
| 110 | + return join( $matched_post_types, PHP_EOL ); | |
| 111 | + } | |
| 151 | 112 | } |