geminilabs

[site-reviews] Include name, email, and IP Address when searching reviews in admin

Jan 6th, 2022 (edited)
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. /**
  2.  * This code snippet will allow searches on the All Reviews admin page
  3.  * to include results for the name, email, and IP address of reviews.
  4.  * @site-reviews v5.19
  5.  */
  6.  
  7. /**
  8.  * @return bool
  9.  */
  10. function site_reviews_is_admin_reviews_query(WP_Query $query) {
  11.     global $pagenow;
  12.     return function_exists('glsr')
  13.         && glsr()->isAdmin()
  14.         && glsr()->post_type === $query->get('post_type')
  15.         && $query->is_main_query()
  16.         && 'edit.php' === $pagenow;
  17. }
  18.  
  19. /**
  20.  * @param string $search
  21.  * @return string
  22.  * @filter posts_search
  23.  */
  24. function site_reviews_modify_posts_search($search, WP_Query $query) {
  25.     if (!site_reviews_is_admin_reviews_query($query) || empty($query->query_vars['search_terms'])) {
  26.         return $search;
  27.     }
  28.     global $wpdb;
  29.     $newsearch = '';
  30.     $searchand = '';
  31.     $exclusionPrefix = apply_filters('wp_query_search_exclusion_prefix', '-');
  32.     $n = !empty($query->query_vars['exact']) ? '' : '%';
  33.     foreach ($query->query_vars['search_terms'] as $term) {
  34.         if (\GeminiLabs\SiteReviews\Helpers\Str::startsWith([$exclusionPrefix], $term)) {
  35.             $like_op = 'NOT LIKE';
  36.             $andor_op = 'AND';
  37.             $term = \GeminiLabs\SiteReviews\Helpers\Str::removePrefix($term, $exclusionPrefix);
  38.         } else {
  39.             $like_op = 'LIKE';
  40.             $andor_op = 'OR';
  41.         }
  42.         $like = $n.$wpdb->esc_like($term).$n;
  43.         $newsearch .= $wpdb->prepare("{$searchand}(
  44.            ({$wpdb->posts}.post_title $like_op %s)
  45.            $andor_op
  46.            ({$wpdb->posts}.post_excerpt $like_op %s)
  47.            $andor_op
  48.            ({$wpdb->posts}.post_content $like_op %s)
  49.            $andor_op
  50.            ({$wpdb->prefix}glsr_ratings.name $like_op %s)
  51.            $andor_op
  52.            ({$wpdb->prefix}glsr_ratings.email $like_op %s)
  53.            $andor_op
  54.            ({$wpdb->prefix}glsr_ratings.ip_address $like_op %s)
  55.        )", $like, $like, $like, $like, $like, $like);
  56.         $searchand = ' AND ';
  57.     }
  58.     if (!empty($newsearch)) {
  59.         $search = " AND ({$newsearch}) ";
  60.         if (!is_user_logged_in()) {
  61.             $search .= " AND ({$wpdb->posts}.post_password = '') ";
  62.         }
  63.     }
  64.     return $search;
  65. }
  66. add_filter('posts_search', 'site_reviews_modify_posts_search', 10, 2);
  67.  
  68. /**
  69.  * @param string $join
  70.  * @return string
  71.  * @filter posts_join_paged
  72.  */
  73. function site_reviews_modify_posts_join($join, WP_Query $query) {
  74.     if (!site_reviews_is_admin_reviews_query($query) || empty($query->query_vars['search_terms'])) {
  75.         return $join;
  76.     }
  77.     global $wpdb;
  78.     $join .= " LEFT JOIN {$wpdb->prefix}glsr_ratings ON {$wpdb->posts}.ID = {$wpdb->prefix}glsr_ratings.review_id ";
  79.     return $join;
  80. }
  81. add_filter('posts_join_paged', 'site_reviews_modify_posts_join', 10, 2);
  82.  
Add Comment
Please, Sign In to add comment