Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This code snippet will allow searches on the All Reviews admin page
- * to include results for the name, email, and IP address of reviews.
- * @site-reviews v5.19
- */
- /**
- * @return bool
- */
- function site_reviews_is_admin_reviews_query(WP_Query $query) {
- global $pagenow;
- return function_exists('glsr')
- && glsr()->isAdmin()
- && glsr()->post_type === $query->get('post_type')
- && $query->is_main_query()
- && 'edit.php' === $pagenow;
- }
- /**
- * @param string $search
- * @return string
- * @filter posts_search
- */
- function site_reviews_modify_posts_search($search, WP_Query $query) {
- if (!site_reviews_is_admin_reviews_query($query) || empty($query->query_vars['search_terms'])) {
- return $search;
- }
- global $wpdb;
- $newsearch = '';
- $searchand = '';
- $exclusionPrefix = apply_filters('wp_query_search_exclusion_prefix', '-');
- $n = !empty($query->query_vars['exact']) ? '' : '%';
- foreach ($query->query_vars['search_terms'] as $term) {
- if (\GeminiLabs\SiteReviews\Helpers\Str::startsWith([$exclusionPrefix], $term)) {
- $like_op = 'NOT LIKE';
- $andor_op = 'AND';
- $term = \GeminiLabs\SiteReviews\Helpers\Str::removePrefix($term, $exclusionPrefix);
- } else {
- $like_op = 'LIKE';
- $andor_op = 'OR';
- }
- $like = $n.$wpdb->esc_like($term).$n;
- $newsearch .= $wpdb->prepare("{$searchand}(
- ({$wpdb->posts}.post_title $like_op %s)
- $andor_op
- ({$wpdb->posts}.post_excerpt $like_op %s)
- $andor_op
- ({$wpdb->posts}.post_content $like_op %s)
- $andor_op
- ({$wpdb->prefix}glsr_ratings.name $like_op %s)
- $andor_op
- ({$wpdb->prefix}glsr_ratings.email $like_op %s)
- $andor_op
- ({$wpdb->prefix}glsr_ratings.ip_address $like_op %s)
- )", $like, $like, $like, $like, $like, $like);
- $searchand = ' AND ';
- }
- if (!empty($newsearch)) {
- $search = " AND ({$newsearch}) ";
- if (!is_user_logged_in()) {
- $search .= " AND ({$wpdb->posts}.post_password = '') ";
- }
- }
- return $search;
- }
- add_filter('posts_search', 'site_reviews_modify_posts_search', 10, 2);
- /**
- * @param string $join
- * @return string
- * @filter posts_join_paged
- */
- function site_reviews_modify_posts_join($join, WP_Query $query) {
- if (!site_reviews_is_admin_reviews_query($query) || empty($query->query_vars['search_terms'])) {
- return $join;
- }
- global $wpdb;
- $join .= " LEFT JOIN {$wpdb->prefix}glsr_ratings ON {$wpdb->posts}.ID = {$wpdb->prefix}glsr_ratings.review_id ";
- return $join;
- }
- add_filter('posts_join_paged', 'site_reviews_modify_posts_join', 10, 2);
Add Comment
Please, Sign In to add comment