*/ public function search( string $search ): array { $search = trim( $search ); if ( '' === $search ) { return array(); } $ids = get_posts( array( 'post_type' => $this->post_types(), 'post_status' => self::STATUSES, 's' => $search, 'search_columns' => array( 'post_title' ), 'posts_per_page' => self::LIMIT, 'orderby' => 'relevance', 'fields' => 'ids', 'no_found_rows' => true, 'suppress_filters' => false, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); if ( ctype_digit( $search ) ) { array_unshift( $ids, (int) $search ); } return $this->describe( $ids ); } /** * Name each ID, in the order given, dropping any that no longer point at offerable content. * * @param int[] $ids Post IDs. * @return array */ public function describe( array $ids ): array { $types = $this->post_types(); $result = array(); foreach ( array_unique( array_map( 'absint', $ids ) ) as $id ) { $post = $id > 0 ? get_post( $id ) : null; if ( ! $post || ! in_array( $post->post_type, $types, true ) ) { continue; } $type = get_post_type_object( $post->post_type ); $title = trim( wp_strip_all_tags( get_the_title( $post ) ) ); $result[] = array( 'id' => (int) $post->ID, /* translators: %d: post ID. */ 'title' => '' !== $title ? $title : sprintf( __( '(no title) #%d', 'adpix-analytics' ), $post->ID ), 'type' => $type ? (string) $type->labels->singular_name : $post->post_type, ); } return $result; } /** * Post types a visitor can land on. * * @return string[] */ private function post_types(): array { $types = get_post_types( array( 'public' => true ) ); // Attachments are public but have no page of their own on most sites worth measuring. unset( $types['attachment'] ); return array_values( $types ); } }