admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE ), 'noResults' => __( 'No matches found.', 'deadbolt' ), 'searching' => __( 'Searching…', 'deadbolt' ), 'remove' => __( 'Remove', 'deadbolt' ), ] ); } // End enqueue_fields_assets() /** * Get the sanitized search term from the request. * * @return string */ private static function term() : string { return isset( $_GET[ 'term' ] ) ? sanitize_text_field( wp_unslash( $_GET[ 'term' ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- the nonce is verified by Helpers::verify_ajax() before this runs. } // End term() /** * AJAX: search users by name, login or email. * * @return void */ public function ajax_search_users() : void { Helpers::verify_ajax( self::NONCE, self::user_search_capability() ); $term = self::term(); if ( strlen( $term ) < 2 ) { wp_send_json_success( [] ); } $users = get_users( [ 'search' => '*' . $term . '*', 'search_columns' => [ 'user_login', 'user_email', 'display_name', 'user_nicename' ], 'number' => self::LIMIT, 'orderby' => 'display_name', 'fields' => [ 'ID', 'display_name', 'user_login' ], 'count_total' => false, ] ); $results = array_map( fn( $user ) => [ 'id' => (int) $user->ID, 'text' => $user->display_name . ' (' . $user->user_login . ')' ], $users ); wp_send_json_success( $results ); } // End ajax_search_users() /** * AJAX: search posts in the enabled post types, by title or ID. * * @return void */ public function ajax_search_posts() : void { Helpers::verify_ajax( self::NONCE ); $term = self::term(); $post_types = array_keys( Settings::get( 'post_types' ) ); if ( '' === $term || empty( $post_types ) ) { wp_send_json_success( [] ); } $args = [ 'post_type' => $post_types, 'post_status' => [ 'publish', 'private', 'draft', 'pending', 'future' ], 'posts_per_page' => self::LIMIT, 'fields' => 'ids', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ]; if ( ctype_digit( $term ) ) { $args[ 'post__in' ] = [ absint( $term ) ]; } else { $args[ 's' ] = $term; } $results = array_map( fn( $post_id ) => [ 'id' => $post_id, 'text' => Templates::post_label( $post_id ) ], get_posts( $args ) ); wp_send_json_success( $results ); } // End ajax_search_posts() } // End class Pickers Pickers::instance();