*/ public static function search( string $term, string $post_type = 'page' ): array { $term = trim( $term ); $args = array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => self::LIMIT, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'ignore_sticky_posts' => true, 'suppress_filters' => false, ); if ( '' === $term ) { $args['orderby'] = 'modified'; $args['order'] = 'DESC'; } else { $args['s'] = $term; $args['search_columns'] = array( 'post_title' ); $args['orderby'] = 'relevance'; } $posts = get_posts( $args ); if ( ctype_digit( $term ) ) { $by_id = get_post( (int) $term ); if ( $by_id && $post_type === $by_id->post_type && 'publish' === $by_id->post_status ) { array_unshift( $posts, $by_id ); } } $results = array(); foreach ( $posts as $post ) { $results[ $post->ID ] = self::item( $post ); } return array_values( $results ); } /** * One result: ID, title, URL, and the page's path (e.g. /about/team/) * so pages with the same title can be told apart. * * @param \WP_Post $post Post. * @return array{id:int,title:string,path:string,url:string} */ public static function item( \WP_Post $post ): array { $permalink = (string) get_permalink( $post ); $home = (string) home_url(); return array( 'id' => (int) $post->ID, 'title' => self::title( $post ), 'path' => str_starts_with( $permalink, $home ) ? (string) substr( $permalink, strlen( $home ) ) : $permalink, 'url' => $permalink, ); } /** * A post's title for display, with a fallback for untitled ones. * * @param \WP_Post $post Post. * @return string */ public static function title( \WP_Post $post ): string { $title = trim( wp_strip_all_tags( get_the_title( $post ) ) ); /* translators: %d: post ID */ return '' !== $title ? html_entity_decode( $title, ENT_QUOTES, 'UTF-8' ) : sprintf( __( '(no title) #%d', 'all-in-one-utilities' ), $post->ID ); } /** * The stored value, if it's still a published post of $post_type. * * @param mixed $value Raw value. * @param string $post_type Post type. * @return int Post ID, or 0. */ public static function sanitize( mixed $value, string $post_type = 'page' ): int { $id = absint( $value ); $post = $id ? get_post( $id ) : null; return ( $post && $post_type === $post->post_type && 'publish' === $post->post_status ) ? $id : 0; } /** * AJAX: search for the type-ahead. * * @return void */ public static function ajax_search(): void { check_ajax_referer( self::NONCE ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } $post_type = isset( $_POST['post_type'] ) ? sanitize_key( wp_unslash( $_POST['post_type'] ) ) : 'page'; $term = isset( $_POST['term'] ) ? sanitize_text_field( wp_unslash( $_POST['term'] ) ) : ''; if ( ! post_type_exists( $post_type ) || ! is_post_type_viewable( $post_type ) ) { wp_send_json_error( null, 400 ); } wp_send_json_success( array( 'items' => self::search( $term, $post_type ) ) ); } /** * Render the field. * * @param Field $field Field. * @param mixed $value Stored post ID. * @param string $name Name attr. * @param string $id Id attr (the search box, so the row's label points at it). * @return void */ public static function render( Field $field, mixed $value, string $name, string $id ): void { $post_type = (string) ( $field->args['post_type'] ?? 'page' ); $none_label = (string) ( $field->args['none_label'] ?? __( 'None', 'all-in-one-utilities' ) ); $selected = self::sanitize( $value, $post_type ); $post = $selected ? get_post( $selected ) : null; $current = $post ? self::item( $post ) : null; $list_id = $id . '-results'; $type = get_post_type_object( $post_type ); $plural = $type ? strtolower( (string) $type->labels->name ) : $post_type; ?>
' . esc_html( (string) $field->args['description'] ) . '
'; } } }