Advertisement
geminilabs

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

Jul 2nd, 2021 (edited)
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.60 KB | None | 0 0
  1. /**
  2.  * Restrict visible reviews on the "All Reviews" page to the assigned_user
  3.  * @updated 2024-04-30
  4.  */
  5. class GLSR_Restrict_Reviews_Assigned_Users
  6. {
  7.     protected static $instance;
  8.  
  9.     public static function load(): self
  10.     {
  11.         if (empty(static::$instance)) {
  12.             static::$instance = new static();
  13.         }
  14.         return static::$instance;
  15.     }
  16.    
  17.     /**
  18.      * @filter site-reviews/database/sql/query-flagged-count
  19.      */
  20.     public function filterFlaggedCountSql(string $sql): string
  21.     {
  22.         global $wpdb;
  23.         $userId = get_current_user_id();
  24.         $search = "AS p ON (p.ID = r.review_id)";
  25.         $replace = "{$search} LEFT JOIN {$wpdb->prefix}glsr_assigned_users AS au ON (au.rating_id = r.ID)";
  26.         $sql = str_replace($search, $replace, $sql);
  27.         $search = "WHERE 1=1";
  28.         $replace = "{$search} AND (au.user_id = {$userId} OR p.post_author = {$userId})";
  29.         $sql = str_replace($search, $replace, $sql);
  30.         return $sql;
  31.     }
  32.  
  33.     /**
  34.      * @filter site-reviews/review-table/clauses
  35.      */
  36.     public function filterQueryClauses(array $clauses): array
  37.     {
  38.         global $wpdb;
  39.         $userId = get_current_user_id();
  40.         $clauses['join']['clauses'][] = "INNER JOIN {$wpdb->prefix}glsr_ratings ON ({$wpdb->prefix}glsr_ratings.review_id = {$wpdb->posts}.ID)";
  41.         $clauses['join']['clauses'][] = "LEFT JOIN {$wpdb->prefix}glsr_assigned_users ON ({$wpdb->prefix}glsr_assigned_users.rating_id = {$wpdb->prefix}glsr_ratings.ID)";
  42.         $clauses['where']['clauses'][] = "AND ({$wpdb->prefix}glsr_assigned_users.user_id = {$userId} OR {$wpdb->posts}.post_author = {$userId})";
  43.         $clauses['where']['replace'] = false;
  44.         return $clauses;
  45.     }
  46.  
  47.     /**
  48.      * @filter views_edit-site-review
  49.      */
  50.     public function filterReviewCounts(array $views): array
  51.     {
  52.         global $wp_query;
  53.         unset($views['mine']);
  54.         $counts = $this->restrictedCounts();
  55.         $total = array_sum($counts);
  56.         foreach (get_post_stati(['show_in_admin_all_list' => false]) as $state) {
  57.             $total -= ($counts[$state] ?? 0);
  58.         }
  59.         foreach ($views as $status => &$link) {
  60.             $num = 'all' === $status ? $total : ($counts[$status] ?? 0);
  61.             $link = preg_replace('/(<span class="count">)([\d\(\),]+)(<\/span>)/', "$1({$num})$3", $link);
  62.         }
  63.         return $views;
  64.     }
  65.  
  66.     public function run(): void
  67.     {
  68.         add_action('current_screen', function () {
  69.             if (!$this->isQueryRestricted()) {
  70.                 return;
  71.             }
  72.             add_filter('site-reviews/review-table/clauses', [$this, 'filterQueryClauses'], 5);
  73.             add_filter('site-reviews/database/sql/query-flagged-count', [$this, 'filterFlaggedCountSql'], 5);
  74.             add_filter('views_edit-'.glsr()->post_type, [$this, 'filterReviewCounts'], 5);
  75.         });
  76.     }
  77.  
  78.     protected function isQueryRestricted(): bool
  79.     {
  80.         if (!function_exists('glsr')) {
  81.             return false;
  82.         }
  83.         if (glsr()->filterBool('snippet/disable/GLSR_Restrict_Reviews_Assigned_Users', false)) {
  84.             return false;
  85.         }
  86.         if (get_current_screen()->id !== 'edit-'.glsr()->post_type) {
  87.             return false;
  88.         }
  89.         if (current_user_can('edit_others_site-reviews')) {
  90.             return false;
  91.         }
  92.         return true;
  93.     }
  94.  
  95.     protected function restrictedCounts(): array
  96.     {
  97.         $userId = get_current_user_id();
  98.         $cache_key = sprintf('%s_readable_%s', glsr()->post_status, $userId);
  99.         $counts = wp_cache_get($cache_key, 'counts');
  100.         $post_type = glsr()->post_type;
  101.         if (false === $counts) {
  102.             global $wpdb;
  103.             $results = $wpdb->get_results("
  104.                SELECT p.post_status, COUNT(*) AS count
  105.                FROM {$wpdb->posts} AS p
  106.                INNER JOIN {$wpdb->prefix}glsr_ratings AS r ON (r.review_id = p.ID)
  107.                LEFT JOIN {$wpdb->prefix}glsr_assigned_users AS au ON (au.rating_id = r.ID)
  108.                WHERE p.post_type = '{$post_type}' AND (au.user_id = {$userId} OR p.post_author = {$userId})
  109.                GROUP BY p.post_status
  110.            ", ARRAY_A);
  111.             $results = wp_list_pluck($results, 'count', 'post_status');
  112.             $counts = array_map('intval', $results);
  113.         }
  114.         $defaults = array_fill_keys(get_post_stati(), 0);
  115.         $counts = wp_parse_args((array) $counts, $defaults);
  116.         wp_cache_set($cache_key, $counts, 'counts');
  117.         return $counts;
  118.     }
  119. }
  120.  
  121. GLSR_Restrict_Reviews_Assigned_Users::load()->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement