Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Restrict visible reviews on the "All Reviews" page to the review's assigned_post author
- * @updated 2024-04-29
- */
- class GLSR_Restrict_Reviews_Assigned_Post_Author
- {
- protected static $instance;
- public static function load(): self
- {
- if (empty(static::$instance)) {
- static::$instance = new static();
- }
- return static::$instance;
- }
- /**
- * @filter site-reviews/database/sql/query-flagged-count
- */
- public function filterFlaggedCountSql(string $sql): string
- {
- global $wpdb;
- $userId = get_current_user_id();
- $search = "AS p ON (p.ID = r.review_id)";
- $replace = "{$search} LEFT JOIN {$wpdb->prefix}glsr_assigned_posts AS ap ON (ap.rating_id = r.ID)";
- $replace = "{$replace} LEFT JOIN {$wpdb->posts} AS p2 ON (p2.ID = ap.post_id)";
- $sql = str_replace($search, $replace, $sql);
- $search = "WHERE 1=1";
- $replace = "{$search} AND (p2.post_author = {$userId} OR p.post_author = {$userId})";
- $sql = str_replace($search, $replace, $sql);
- return $sql;
- }
- /**
- * @filter site-reviews/review-table/clauses
- */
- public function filterQueryClauses(array $clauses): array
- {
- global $wpdb;
- $userId = get_current_user_id();
- $clauses['join']['clauses'][] = "INNER JOIN {$wpdb->prefix}glsr_ratings ON ({$wpdb->prefix}glsr_ratings.review_id = {$wpdb->posts}.ID)";
- $clauses['join']['clauses'][] = "LEFT JOIN {$wpdb->prefix}glsr_assigned_posts ON ({$wpdb->prefix}glsr_assigned_posts.rating_id = {$wpdb->prefix}glsr_ratings.ID)";
- $clauses['join']['clauses'][] = "LEFT JOIN {$wpdb->posts} AS posts2 ON (posts2.ID = {$wpdb->prefix}glsr_assigned_posts.post_id)";
- $clauses['where']['clauses'][] = "AND (posts2.post_author = {$userId} OR {$wpdb->posts}.post_author = {$userId})";
- $clauses['where']['replace'] = false;
- return $clauses;
- }
- /**
- * @filter views_edit-site-review
- */
- public function filterReviewCounts(array $views): array
- {
- global $wp_query;
- unset($views['mine']);
- $counts = $this->restrictedCounts();
- $total = array_sum($counts);
- foreach (get_post_stati(['show_in_admin_all_list' => false]) as $state) {
- $total -= ($counts[$state] ?? 0);
- }
- foreach ($views as $status => &$link) {
- $num = 'all' === $status ? $total : ($counts[$status] ?? 0);
- $link = preg_replace('/(<span class="count">)([\d\(\),]+)(<\/span>)/', "$1({$num})$3", $link);
- }
- return $views;
- }
- public function run(): void
- {
- add_action('current_screen', function () {
- if (!$this->isQueryRestricted()) {
- return;
- }
- add_filter('site-reviews/review-table/clauses', [$this, 'filterQueryClauses'], 5);
- add_filter('site-reviews/database/sql/query-flagged-count', [$this, 'filterFlaggedCountSql'], 5);
- add_filter('views_edit-'.glsr()->post_type, [$this, 'filterReviewCounts'], 5);
- });
- }
- protected function isQueryRestricted(): bool
- {
- if (!function_exists('glsr')) {
- return false;
- }
- if (glsr()->filterBool('snippet/disable/GLSR_Restrict_Reviews_Assigned_Post_Author', false)) {
- return false;
- }
- if (get_current_screen()->id !== 'edit-'.glsr()->post_type) {
- return false;
- }
- if (current_user_can('edit_others_site-reviews')) {
- return false;
- }
- return true;
- }
- protected function restrictedCounts(): array
- {
- $userId = get_current_user_id();
- $cache_key = sprintf('%s_readable_%s', glsr()->post_status, $userId);
- $counts = wp_cache_get($cache_key, 'counts');
- $post_type = glsr()->post_type;
- if (false === $counts) {
- global $wpdb;
- $results = $wpdb->get_results("
- SELECT p.post_status, COUNT(*) AS count
- FROM {$wpdb->posts} AS p
- INNER JOIN {$wpdb->prefix}glsr_ratings AS r ON (r.review_id = p.ID)
- LEFT JOIN {$wpdb->prefix}glsr_assigned_posts AS ap ON (ap.rating_id = r.ID)
- LEFT JOIN {$wpdb->posts} AS p2 ON (p2.ID = ap.post_id)
- WHERE p.post_type = '{$post_type}' AND (p2.post_author = {$userId} OR p.post_author = {$userId})
- GROUP BY p.post_status
- ", ARRAY_A);
- $results = wp_list_pluck($results, 'count', 'post_status');
- $counts = array_map('intval', $results);
- }
- $defaults = array_fill_keys(get_post_stati(), 0);
- $counts = wp_parse_args((array) $counts, $defaults);
- wp_cache_set($cache_key, $counts, 'counts');
- return $counts;
- }
- }
- GLSR_Restrict_Reviews_Assigned_Post_Author::load()->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement