false`, `exclude_from_search => true`, and the import screen exists precisely because some of that * text is internal (a price list, a policy draft, notes copied from a supplier). A search box on a public help * page must not become the hole that publishes it. * * That is also why the query goes through `WP_Query` rather than a hand-written `$wpdb` LIKE: WP_Query applies * every rule WordPress already has about what a visitor may see — post status, password, private terms, * post-type visibility — instead of an allow-list that has to be kept correct by hand. * * @package AI House */ defined( 'ABSPATH' ) || exit; /** * Class MYBOT_REST_Search */ class MYBOT_REST_Search { /** * The shortest query worth searching for, in characters. */ const MIN_LENGTH = 2; /** * The longest query accepted, in characters. */ const MAX_LENGTH = 120; /** * Registers the route. * * @return void */ public function register() { register_rest_route( MYBOT_REST_NAMESPACE, '/search', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'search' ), 'permission_callback' => array( 'MYBOT_REST_Controller', 'public_permission' ), 'args' => array( 'q' => array( 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'limit' => array( 'required' => false, 'type' => 'integer', 'default' => 6, 'sanitize_callback' => 'absint', ), ), ) ); } /** * Runs the search. * * @param WP_REST_Request $request The request. * @return WP_REST_Response|WP_Error */ public function search( $request ) { $allowed = mybot()->security->enforce_rate_limit(); if ( is_wp_error( $allowed ) ) { return $allowed; } $query = trim( (string) $request->get_param( 'q' ) ); /* * A one-character query matches most of the site and costs a full scan; the limit is the difference * between a search box and a way to make the server work for free. */ if ( mb_strlen( $query ) < self::MIN_LENGTH ) { return new WP_REST_Response( array( 'success' => true, 'items' => array(), 'total' => 0, 'message' => __( 'Type at least two characters.', 'aihouse' ), ), 200 ); } if ( mb_strlen( $query ) > self::MAX_LENGTH ) { $query = mb_substr( $query, 0, self::MAX_LENGTH ); } $limit = (int) $request->get_param( 'limit' ); $limit = ( $limit > 0 && $limit <= 20 ) ? $limit : 6; /* * `post_status => publish` and the readable post types are set HERE, not in a filter: a filter that * could widen them would be a documented way to publish a draft through the search box. */ $results = new WP_Query( array( 's' => $query, 'post_status' => 'publish', 'post_type' => $this->searchable_types(), 'posts_per_page' => $limit, 'no_found_rows' => true, 'ignore_sticky_posts' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, ) ); $items = array(); foreach ( $results->posts as $post ) { $items[] = array( 'title' => wp_strip_all_tags( get_the_title( $post ) ), 'url' => esc_url_raw( get_permalink( $post ) ), 'excerpt' => $this->excerpt( $post ), 'type' => $this->type_label( $post ), ); } /** * Filters the items a help-page search returns. * * @param array $items The items. * @param WP_Query $results The query. * @param string $query The visitor's query. */ $items = apply_filters( 'mybot_help_search_items', $items, $results, $query ); return new WP_REST_Response( array( 'success' => true, 'items' => array_values( (array) $items ), 'total' => count( (array) $items ), 'query' => $query, ), 200 ); } /** * The post types a visitor may read, minus the knowledge base. * * @return string[] */ private function searchable_types() { $types = get_post_types( array( 'public' => true, 'exclude_from_search' => false, ), 'names' ); unset( $types['attachment'] ); return array_values( $types ); } /** * A short plain-text excerpt for one result. * * Plain text, never HTML: the script writes it with `textContent`, so markup would be shown to the visitor * as characters — and `wp_strip_all_tags` is what makes that true even when a plugin adds shortcodes to * the excerpt. * * @param WP_Post $post The post. * @return string */ private function excerpt( $post ) { $text = has_excerpt( $post ) ? $post->post_excerpt : $post->post_content; $text = wp_strip_all_tags( strip_shortcodes( (string) $text ) ); $text = wp_trim_words( $text, 22, '…' ); return trim( $text ); } /** * The label shown next to a result ("Page", "Post"), in the site's language. * * @param WP_Post $post The post. * @return string */ private function type_label( $post ) { $object = get_post_type_object( (string) $post->post_type ); if ( ! $object ) { return ''; } return wp_strip_all_tags( (string) $object->labels->singular_name ); } }