Instantly share code, notes, and snippets.
khromov/limit-author-view-scope.php
Created May 28, 2014
Limit author view scope WordPress
<?php | |
/* | |
Plugin Name: Limit Author View scope | |
Plugin URI: | |
Description: Limits non-admins to only see their own media and comments. | |
Version: 2014.05.28 | |
Author: khromov | |
Author URI: https://profiles.wordpress.org/khromov | |
License: GPL2 | |
*/ | |
/** ---- Media ---- **/ | |
/** | |
* Filters media | |
*/ | |
add_action('pre_get_posts', function($wp_query) | |
{ | |
global $current_user; | |
if(!current_user_can('manage_options') && (is_admin() && $wp_query->query['post_type'] === 'attachment')) | |
$wp_query->set('author', $current_user->ID); | |
}, 11); | |
/** | |
* Fix unattached count | |
*/ | |
add_filter('wp_count_unattached_attachments', function($attachments) | |
{ | |
global $wpdb; | |
global $current_user; | |
return $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1 AND post_author = {$current_user->ID}" ); | |
}); | |
/* | |
* Fix regular counts | |
*/ | |
add_filter('wp_count_attachments', function($counts_in) | |
{ | |
global $wpdb; | |
global $current_user; | |
$and = wp_post_mime_type_where(''); //Default mime type //AND post_author = {$current_user->ID} | |
$count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_author = {$current_user->ID} $and GROUP BY post_mime_type", ARRAY_A ); | |
$counts = array(); | |
foreach((array)$count as $row) | |
$counts[ $row['post_mime_type'] ] = $row['num_posts']; | |
$counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_author = {$current_user->ID} AND post_status = 'trash' $and"); | |
return $counts; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment