true,
'message' => __( 'No query supplied', 'admin-search' ),
'results' => array()
);
if ( isset( $_GET[ 'q' ] ) && is_string( $_GET[ 'q' ] ) && $_GET[ 'q' ] ) {
$return[ 'message' ] = __( 'Query too short', 'admin-search' );
$q = trim( strtolower( strip_tags( $_GET[ 'q' ] ) ) );
if ( $q && strlen( $_GET[ 'q' ] ) > 2 ) {
$return[ 'error' ] = false;
if ( admin_search_setting( 'include_admin_pages' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'admin-results.php';
foreach ( $admin_results as $admin_result ) {
if ( current_user_can( $admin_result[ 'capability' ] ) ) {
$contains_value = false;
foreach ( $admin_result[ 'q' ] as $subq ) {
if ( strpos( __( $subq ), $q ) !== false ) {
$contains_value = true; break;
}
}
if ( $contains_value ) {
$return[ 'results' ][ 'admin' ][ 'posts' ][] = array(
'id' => '',
'edit_post_link' => $admin_result[ 'url' ],
'title' => $admin_result[ 'title' ]
);
}
}
}
if ( isset( $return[ 'results' ][ 'admin' ] ) && ! isset( $return[ 'results' ][ 'admin' ][ 'post_type' ] ) ) {
$return[ 'results' ][ 'admin' ][ 'post_type' ] = array(
'name' => 'admin',
'label' => __( 'Administration', 'admin-search' )
);
}
}
$posts_args = array(
's' => $q,
'post_type' => admin_search_setting( 'post_types' ),
'post_status' => 'any',
'posts_per_page' => admin_search_setting( 'result_count' )
);
$posts_args = apply_filters( 'admin_search_posts_query', $posts_args );
$posts = new WP_Query( $posts_args );
if ( $posts -> have_posts() ) {
while ( $posts -> have_posts() ) {
$posts -> the_post();
$post_type = get_post_type_object( get_post_type( get_the_id() ) );
if ( ! isset( $return[ 'results' ][ $post_type -> name ] ) ) {
$return[ 'results' ][ $post_type -> name ][ 'post_type' ] = $post_type;
}
$title = get_the_title();
if ( admin_search_setting( 'highlight_query' ) ) {
$title = preg_replace( "/({$q})/i", '$1', $title );
}
$result = array(
'id' => get_the_id(),
'edit_post_link' => get_edit_post_link(),
'title' => $title,
'date' => get_the_date(),
'status' => get_post_status()
);
if ( $post_type -> name === 'attachment' ) {
$result[ 'attachment' ] = wp_get_attachment_image( get_the_id(), 'medium' );
$result[ 'mime_type' ] = get_post_mime_type();
$result[ 'mime_icon' ] = wp_mime_type_icon( get_the_id() );
}
$return[ 'results' ][ $post_type -> name ][ 'posts' ][] = $result;
}
}
if ( admin_search_setting( 'include_terms' ) ) {
$terms_args = array(
'name__like' => $q,
'hide_empty' => false,
'number' => admin_search_setting( 'result_count' )
);
$terms_args = apply_filters( 'admin_search_terms_query', $terms_args );
$terms = get_terms( $terms_args );
if ( $terms ) {
foreach ( $terms as $term ) {
$post_type = get_taxonomy( $term -> taxonomy );
if ( ! isset( $return[ 'results' ][ $post_type -> name ] ) ) {
$return[ 'results' ][ $post_type -> name ][ 'post_type' ] = $post_type;
}
$title = $term -> name;
if ( admin_search_setting( 'highlight_query' ) ) {
$title = preg_replace( "/({$q})/i", '$1', $title );
}
$return[ 'results' ][ $post_type -> name ][ 'posts' ][] = array(
'id' => $term -> term_id,
'edit_post_link' => get_edit_term_link( $term -> term_id ),
'title' => $title
);
}
}
}
if ( admin_search_setting( 'include_comments' ) ) {
$comments_args = array(
'search' => $q,
'number' => admin_search_setting( 'result_count' ),
);
$comments_args = apply_filters( 'admin_search_comments_query', $comments_args );
$comments = new WP_Comment_Query;
$comments = $comments -> query( $comments_args );
if ( $comments ) {
if ( ! isset( $return[ 'results' ][ 'comment' ] ) ) {
$return[ 'results' ][ 'comment' ][ 'post_type' ] = array(
'name' => 'comment',
'label' => __( 'Comments', 'admin-search' )
);
}
foreach ( $comments as $comment ) {
$return[ 'results' ][ 'comment' ][ 'posts' ][] = array(
'id' => $comment -> comment_ID,
'edit_post_link' => get_edit_comment_link( $comment -> comment_ID ),
'title' => sprintf( __( 'Comment by %s on %s' , 'admin-search' ), $comment -> comment_author, get_the_title( $comment -> comment_post_ID ) ),
'date' => date( get_option( 'date_format' ), strtotime( $comment -> comment_date ) ),
'status' => $comment -> comment_approved ? 'published' : 'pending'
);
}
}
}
/*
Ensure attachments are the last results returned
*/
if ( isset( $return[ 'results' ][ 'attachment' ] ) ) {
$attachments = $return[ 'results' ][ 'attachment' ];
unset( $return[ 'results' ][ 'attachment' ] );
$return[ 'results' ][ 'attachment' ] = $attachments;
}
/*
Calculate and return total result count
*/
$return[ 'total_results' ] = 0;
foreach ( $return[ 'results' ] as $key => $value ) {
$return[ 'total_results' ] += count( $return[ 'results' ][ $key ][ 'posts' ] );
}
if ( $return[ 'total_results' ] > 0 ) {
$return[ 'message' ] = __( 'Results found', 'admin-search' );
} else {
$return[ 'message' ] = __( 'No results found', 'admin-search' );
}
}
}
wp_send_json( $return );
wp_die();
}
add_action( 'wp_ajax_admin_search_ajax', 'admin_search_ajax' );
add_action( 'wp_ajax_nopriv_admin_search_ajax', 'admin_search_ajax' );