core
4 months ago
class-autocomplete.php
6 months ago
class-convert-values.php
5 months ago
class-has-meta.php
5 months ago
class-has-terms.php
5 months ago
class-manager.php
5 months ago
class-source.php
5 months ago
class-autocomplete.php
126 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Source; |
| 4 | |
| 5 | use SearchRegex\Sql; |
| 6 | use WP_Term; |
| 7 | |
| 8 | /** |
| 9 | * Provides autocomplete functions |
| 10 | * |
| 11 | * @psalm-immutable |
| 12 | */ |
| 13 | class Autocomplete { |
| 14 | /** |
| 15 | * Autocomplete a user name |
| 16 | * |
| 17 | * @param string $value Value. |
| 18 | * @return list<object{id: string, value: string}> |
| 19 | */ |
| 20 | public static function get_user( $value ) { |
| 21 | global $wpdb; |
| 22 | |
| 23 | return $wpdb->get_results( $wpdb->prepare( "SELECT ID as id,display_name as value FROM {$wpdb->users} WHERE display_name LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', Source::AUTOCOMPLETE_LIMIT ) ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Autocomplete a post title |
| 28 | * |
| 29 | * @param string $value Value. |
| 30 | * @param Sql\Value $id_column ID column. |
| 31 | * @param Sql\Value $search_column Search column. |
| 32 | * @return list<object{id: string, value: string}> |
| 33 | */ |
| 34 | public static function get_post( $value, Sql\Value $id_column, Sql\Value $search_column ) { |
| 35 | global $wpdb; |
| 36 | |
| 37 | $type_sql = "AND post_status != 'inherit'"; // Ignore attachments |
| 38 | |
| 39 | $column = $id_column->get_value(); |
| 40 | $search = $search_column->get_value(); |
| 41 | |
| 42 | // phpcs:ignore |
| 43 | return $wpdb->get_results( $wpdb->prepare( "SELECT {$column} as id,post_title as value FROM {$wpdb->posts} WHERE {$search} LIKE %s {$type_sql} LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', Source::AUTOCOMPLETE_LIMIT ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Autocomplete a term (tag or category) |
| 48 | * |
| 49 | * @param string $type Term term (post_tag or category). |
| 50 | * @param string $value Value. |
| 51 | * @return list<object{id: int, value: string}> |
| 52 | */ |
| 53 | private static function get_terms( $type, $value ) { |
| 54 | $results = []; |
| 55 | $terms = \get_terms( |
| 56 | [ |
| 57 | 'taxonomy' => $type, |
| 58 | 'hide_empty' => false, |
| 59 | 'number' => Source::AUTOCOMPLETE_LIMIT, |
| 60 | 'search' => $value, |
| 61 | ] |
| 62 | ); |
| 63 | |
| 64 | if ( ! is_array( $terms ) ) { |
| 65 | return []; |
| 66 | } |
| 67 | |
| 68 | foreach ( $terms as $term ) { |
| 69 | $results[] = (object) [ |
| 70 | 'id' => $term->term_id, |
| 71 | 'value' => $term->name, |
| 72 | ]; |
| 73 | } |
| 74 | |
| 75 | return $results; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Autocomplete a category |
| 80 | * |
| 81 | * @param string $value Value. |
| 82 | * @return list<object{id: int, value: string}> |
| 83 | */ |
| 84 | public static function get_category( $value ) { |
| 85 | return self::get_terms( 'category', $value ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Autocomplete a tag |
| 90 | * |
| 91 | * @param string $value Value. |
| 92 | * @return list<object{id: int, value: string}> |
| 93 | */ |
| 94 | public static function get_tag( $value ) { |
| 95 | return self::get_terms( 'post_tag', $value ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Autocomplete meta data |
| 100 | * |
| 101 | * @param Sql\Value $table Meta table. |
| 102 | * @param string $value Value. |
| 103 | * @return list<object{id: string, value: string}> |
| 104 | */ |
| 105 | public static function get_meta( Sql\Value $table, $value ) { |
| 106 | global $wpdb; |
| 107 | |
| 108 | $table = $wpdb->prefix . $table->get_value(); |
| 109 | |
| 110 | // phpcs:ignore |
| 111 | return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT meta_key as id,meta_key as value FROM {$table} WHERE meta_key LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', Source::AUTOCOMPLETE_LIMIT ) ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Autocomplete a comment |
| 116 | * |
| 117 | * @param string $value Value. |
| 118 | * @return list<object{id: string, value: string}> |
| 119 | */ |
| 120 | public static function get_comment( $value ) { |
| 121 | global $wpdb; |
| 122 | |
| 123 | return $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID as id,comment_content as value FROM {$wpdb->comments} WHERE comment_content LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', Source::AUTOCOMPLETE_LIMIT ) ); |
| 124 | } |
| 125 | } |
| 126 |