Advertisement
geminilabs

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

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