date( 'm' ) ) { $date = strtotime( $str . ' ' . date( 'Y', strtotime( '-1 year' ) ) ); } else { $date = strtotime( $str . ' ' . date( 'Y' ) ); } $r[ 'year' ] = date( 'Y', $date ); $r[ 'month' ] = date( 'm', $date ); break; default : $date = strtotime( $str ); $r[ 'year' ] = date( 'Y', $date ); $r[ 'month' ] = date( 'm', $date ); $r[ 'day' ] = date( 'd', $date ); } return $r; } /* * Validate search query and return search results with AJAX */ function admin_search_ajax() { if ( ! is_user_logged_in() ) { return; } $return = array( 'error' => true, 'message' => __( 'No search 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' ] ) ) ); $q = apply_filters( 'admin_search_query', $q ); if ( $q && strlen( $q ) > 2 ) { $return[ 'error' ] = false; foreach ( admin_search_setting( 'post_types' ) as $post_type ) { $author = NULL; $date = NULL; $date_range = 'on'; foreach ( array( __( 'posts by %author%', 'admin-search' ), __( 'posted by %author%', 'admin-search' ), __( 'authored by %author%', 'admin-search' ), __( 'author is %author%', 'admin-search' ), __( 'author %author%', 'admin-search' ), __( 'made by %author%', 'admin-search' ) ) as $author_q ) { if ( preg_match( '/^' . str_replace( '%author%', '(.+)', $author_q ) . '$/', $q, $match ) ) { $author = $match[ 1 ]; break; } } if ( $author == NULL ) { foreach ( array( __( 'posted before %date%', 'admin-search' ), __( 'published before %date%', 'admin-search' ) ) as $date_q ) { if ( preg_match( '/^' . str_replace( '%date%', '(.+)', $date_q ) . '$/', $q, $match ) ) { $date = admin_search_convert_date_str( $match[ 1 ] ); $date_range = 'before'; break; } } if ( $date == NULL ) { foreach ( array( __( 'posted after %date%', 'admin-search' ), __( 'published after %date%', 'admin-search' ) ) as $date_q ) { if ( preg_match( '/^' . str_replace( '%date%', '(.+)', $date_q ) . '$/', $q, $match ) ) { $date = admin_search_convert_date_str( $match[ 1 ] ); $date_range = 'after'; break; } } } if ( $date == NULL ) { foreach ( array( __( 'posted on %date%', 'admin-search' ), __( 'posted in %date%', 'admin-search' ), __( 'posted %date%', 'admin-search' ), __( 'published on %date%', 'admin-search' ), __( 'published in %date%', 'admin-search' ), __( 'published %date%', 'admin-search' ) ) as $date_q ) { if ( preg_match( '/^' . str_replace( '%date%', '(.+)', $date_q ) . '$/', $q, $match ) ) { $date = admin_search_convert_date_str( $match[ 1 ] ); break; } } } } $posts_args = array( 'post_type' => $post_type, 'post_status' => 'any', 'posts_per_page' => admin_search_setting( 'result_count' ) ); if ( $author != NULL ) { if ( is_numeric( $author ) ) { $posts_args[ 'author' ] = $author; } else { $posts_args[ 'author_name' ] = $author; } } else if ( $date != NULL ) { switch ( $date_range ) { case 'on' : $posts_args[ 'date_query' ] = $date; break; case 'before' : $posts_args[ 'date_query' ]['before'] = $date; break; case 'after' : $posts_args[ 'date_query' ]['after'] = $date; break; } } else { $posts_args[ 's' ] = $q; } $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' ][ 'name' ] = $post_type -> name; $return[ 'results' ][ $post_type -> name ][ 'post_type' ][ 'label' ] = $post_type -> label; } $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() == 'publish' ? NULL : __( ucfirst( get_post_status() ), 'admin-search' ) ); 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; } } wp_reset_query(); } foreach ( admin_search_setting( 'taxonomies' ) as $taxonomy ) { $terms_args = array( 'name__like' => $q, 'taxonomy' => $taxonomy, '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' ); } } } if ( admin_search_setting( 'include_users' ) ) { if ( current_user_can( 'edit_user' ) ) { $user_roles = new WP_Roles(); $role_names = $user_roles -> get_names(); $role_q = $q; if ( in_array( $role_q, array( 'admin', 'admins' ) ) ) { $role_q = 'administrator'; } if ( isset( $role_names[ preg_replace( '/s$/', '', $role_q ) ] ) ) { $user_args = array( 'role' => preg_replace( '/s$/', '', $role_q ) ); } else { $user_args = array( 'search' => '*' . $q . '*', 'search_columns' => array( 'user_login', 'user_nicename', 'user_email', 'display_name', ) ); } $user_args = apply_filters( 'admin_search_users_query', $user_args ); $users = new WP_User_Query( $user_args ); $users = $users -> get_results(); if ( $users ) { if ( ! isset( $return[ 'results' ][ 'user' ] ) ) { $return[ 'results' ][ 'user' ][ 'post_type' ] = array( 'name' => 'user', 'label' => __( 'Users', 'admin-search' ) ); } foreach ( $users as $user ) { $return[ 'results' ][ 'user' ][ 'posts' ][] = array( 'id' => $user -> ID, 'edit_post_link' => get_edit_user_link( $user -> ID ), 'title' => $user -> data -> user_login, 'date' => NULL, 'status' => $role_names[ $user -> roles[ 0 ] ] ); } } } } 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 $sub_q ) { if ( strpos( strtolower( $sub_q ), $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' ], 'status' => $admin_result[ 'status' ] ); } } } if ( isset( $return[ 'results' ][ 'admin' ] ) && ! isset( $return[ 'results' ][ 'admin' ][ 'post_type' ] ) ) { $return[ 'results' ][ 'admin' ][ 'post_type' ] = array( 'name' => 'admin', 'label' => __( 'Admin Pages', 'admin-search' ) ); } } if ( admin_search_setting( 'external_websites' ) ) { $return[ 'results' ][ 'external_sites' ][ 'post_type' ] = array( 'name' => 'external_sites', 'label' => __( 'Other Websites', 'admin-search' ) ); foreach ( explode( "\n", admin_search_setting( 'external_websites' ) ) as $external_website ) { $return[ 'results' ][ 'external_sites' ][ 'posts' ][] = array( 'id' => '', 'edit_post_link' => str_replace( '%q%', urlencode( $q ), $external_website ), 'title' => sprintf( __( 'View results on %s', 'admin-search' ), parse_url( $external_website, PHP_URL_HOST ) ), 'status' => NULL ); } } /* 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' );