Advertisement
geminilabs

[site-reviews] Restrict visible reviews on the "All Reviews" page to the assigned_user

Jul 2nd, 2021 (edited)
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.72 KB | None | 0 0
  1. /**
  2.  * Restrict visible reviews on the "All Reviews" page to the assigned_user
  3.  * @updated 2023-12-12
  4.  */
  5.  
  6. /**
  7.  * Helper function to get the review counts for the different post statuses
  8.  * restricted to reviews which have been assigned to the current logged in user
  9.  * when the user does not have the "edit_others_site-reviews" capability.
  10.  * @return object
  11.  */
  12. function glsr_get_restricted_review_counts() {
  13.     if (!glsr_is_query_restricted_to_assigned_users()) {
  14.         return wp_count_posts('site-review', 'readable');
  15.     }
  16.     $userId = get_current_user_id();
  17.     $cache_key = sprintf('site-review_readable_%s', $userId);
  18.     $counts = wp_cache_get($cache_key, 'counts');
  19.     if (false === $counts) {
  20.         global $wpdb;
  21.         $results = $wpdb->get_results("
  22.            SELECT p.post_status, COUNT(*) AS count
  23.            FROM {$wpdb->posts} AS p
  24.            INNER JOIN {$wpdb->prefix}glsr_ratings AS r ON (p.ID = r.review_id)
  25.            INNER JOIN {$wpdb->prefix}glsr_assigned_users AS au ON (r.ID = au.rating_id)
  26.            WHERE p.post_type = 'site-review' AND (au.user_id = {$userId} OR p.post_author = {$userId})
  27.            GROUP BY p.post_status
  28.        ", ARRAY_A);
  29.         $results = wp_list_pluck($results, 'count', 'post_status');
  30.         $counts = array_map('intval', $results);
  31.     }
  32.     $defaults = array_fill_keys(get_post_stati(), 0);
  33.     $counts = wp_parse_args((array) $counts, $defaults);
  34.     wp_cache_set($cache_key, $counts, 'counts');
  35.     return (object) $counts;
  36. }
  37.  
  38. /**
  39.  * Helper function to check if the reviews need to be restricted to those
  40.  * which have been assigned to the current logged in user
  41.  * @return bool
  42.  */
  43. function glsr_is_query_restricted_to_assigned_users() {
  44.     if (!is_admin()
  45.         || !function_exists('glsr')
  46.         || !function_exists('get_current_screen')
  47.         || !isset(get_current_screen()->id)
  48.         || get_current_screen()->id !== 'edit-site-review'
  49.         || current_user_can('edit_others_site-reviews')) {
  50.         return false;
  51.     }
  52.     return true;
  53. }
  54.  
  55. /**
  56.  * Filters the SQL Joins of the WP_Query
  57.  * @param string $joins
  58.  * @return string
  59.  */
  60. add_filter('posts_join', function ($join) {
  61.     if (glsr_is_query_restricted_to_assigned_users()) {
  62.         global $wpdb;
  63.         $join .= " INNER JOIN {$wpdb->prefix}glsr_ratings ON ({$wpdb->posts}.ID = {$wpdb->prefix}glsr_ratings.review_id)";
  64.         $join .= " INNER JOIN {$wpdb->prefix}glsr_assigned_users ON ({$wpdb->prefix}glsr_ratings.ID = {$wpdb->prefix}glsr_assigned_users.rating_id)";
  65.     }
  66.     return $join;
  67. });
  68.  
  69. /**
  70.  * Filters the SQL Where of the WP_Query
  71.  * @param string $where
  72.  * @return string
  73.  */
  74. add_filter('posts_where', function ($where) {
  75.     if (glsr_is_query_restricted_to_assigned_users()) {
  76.         global $wpdb;
  77.         $userId = get_current_user_id();
  78.         $where .= " AND {$wpdb->prefix}glsr_assigned_users.user_id = {$userId}";
  79.     }
  80.     return $where;
  81. });
  82.  
  83. /**
  84.  * Filters the post status menu counts on the All Reviews page
  85.  * @param array $views
  86.  * @return array
  87.  */
  88. add_filter('views_edit-site-review', function ($views) {
  89.     if (!glsr_is_query_restricted_to_assigned_users()) {
  90.         return $views;
  91.     }
  92.     global $wp_query;
  93.     unset($views['mine']);
  94.     $counts = glsr_get_restricted_review_counts();
  95.     $total = array_sum((array) $counts);
  96.     $userId = get_current_user_id();
  97.     foreach (get_post_stati(['show_in_admin_all_list' => false]) as $state ) {
  98.         $total -= $counts->{$state};
  99.     }
  100.     foreach ($views as $status => &$link) {
  101.         $num = 'all' === $status ? $total : $counts->{$status};
  102.         $link = preg_replace('/(<span class="count">)([\d\(\),]+)(<\/span>)/', "$1({$num})$3", $link);
  103.     }
  104.     return $views;
  105. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement