Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Restrict visible reviews on the "All Reviews" page to the assigned_user
- * @updated 2023-12-12
- */
- /**
- * Helper function to get the review counts for the different post statuses
- * restricted to reviews which have been assigned to the current logged in user
- * when the user does not have the "edit_others_site-reviews" capability.
- * @return object
- */
- function glsr_get_restricted_review_counts() {
- if (!glsr_is_query_restricted_to_assigned_users()) {
- return wp_count_posts('site-review', 'readable');
- }
- $userId = get_current_user_id();
- $cache_key = sprintf('site-review_readable_%s', $userId);
- $counts = wp_cache_get($cache_key, 'counts');
- 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 (p.ID = r.review_id)
- INNER JOIN {$wpdb->prefix}glsr_assigned_users AS au ON (r.ID = au.rating_id)
- WHERE p.post_type = 'site-review' AND (au.user_id = {$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 (object) $counts;
- }
- /**
- * Helper function to check if the reviews need to be restricted to those
- * which have been assigned to the current logged in user
- * @return bool
- */
- function glsr_is_query_restricted_to_assigned_users() {
- if (!is_admin()
- || !function_exists('glsr')
- || !function_exists('get_current_screen')
- || !isset(get_current_screen()->id)
- || get_current_screen()->id !== 'edit-site-review'
- || current_user_can('edit_others_site-reviews')) {
- return false;
- }
- return true;
- }
- /**
- * Filters the SQL Joins of the WP_Query
- * @param string $joins
- * @return string
- */
- add_filter('posts_join', function ($join) {
- if (glsr_is_query_restricted_to_assigned_users()) {
- global $wpdb;
- $join .= " INNER JOIN {$wpdb->prefix}glsr_ratings ON ({$wpdb->posts}.ID = {$wpdb->prefix}glsr_ratings.review_id)";
- $join .= " INNER JOIN {$wpdb->prefix}glsr_assigned_users ON ({$wpdb->prefix}glsr_ratings.ID = {$wpdb->prefix}glsr_assigned_users.rating_id)";
- }
- return $join;
- });
- /**
- * Filters the SQL Where of the WP_Query
- * @param string $where
- * @return string
- */
- add_filter('posts_where', function ($where) {
- if (glsr_is_query_restricted_to_assigned_users()) {
- global $wpdb;
- $userId = get_current_user_id();
- $where .= " AND {$wpdb->prefix}glsr_assigned_users.user_id = {$userId}";
- }
- return $where;
- });
- /**
- * Filters the post status menu counts on the All Reviews page
- * @param array $views
- * @return array
- */
- add_filter('views_edit-site-review', function ($views) {
- if (!glsr_is_query_restricted_to_assigned_users()) {
- return $views;
- }
- global $wp_query;
- unset($views['mine']);
- $counts = glsr_get_restricted_review_counts();
- $total = array_sum((array) $counts);
- $userId = get_current_user_id();
- foreach (get_post_stati(['show_in_admin_all_list' => false]) as $state ) {
- $total -= $counts->{$state};
- }
- foreach ($views as $status => &$link) {
- $num = 'all' === $status ? $total : $counts->{$status};
- $link = preg_replace('/(<span class="count">)([\d\(\),]+)(<\/span>)/', "$1({$num})$3", $link);
- }
- return $views;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement