Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Admin for Authors
- Version: 0.2
- Plugin URI: http://wordpress.org/
- Description: Restrict users with limited publishing privelages to only see their own posts and other post types in the admin area
- Author: Marcus Sykes
- Author URI: http://msyk.es
- */
- /*
- Copyright (c) 2013, Marcus Sykes
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- class Admin_For_Authors {
- //these vars prevent need for rewriting lines of code copied from WP
- var $sticky_posts_count = 0;
- var $user_posts_count = 0;
- var $screen;
- static function init(){
- global $Admin_For_Authors;
- $Admin_For_Authors = new Admin_For_Authors();
- add_action('parse_query', 'Admin_For_Authors::parse_query');
- $post_types = get_post_types();
- foreach($post_types as $post_type ){
- add_filter('views_edit-'.$post_type, array(&$Admin_For_Authors, 'get_views'));
- }
- }
- static function parse_query(){
- global $wp_query;
- if( !empty($wp_query->query_vars['post_type']) ){
- $post_type_object = get_post_type_object($wp_query->query_vars['post_type']);
- if ( !current_user_can( $post_type_object->cap->edit_others_posts ) ) {
- $wp_query->query_vars['author'] = get_current_user_id();
- }
- }
- }
- static function wp_count_posts( $type = 'post', $perm = '' ) {
- global $wpdb;
- $user = wp_get_current_user();
- $cache_key = $type.'_'.$user->ID;
- $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
- if( is_user_logged_in() )
- $query .= " AND post_author = {$user->ID}";
- if ( 'readable' == $perm && is_user_logged_in() ) {
- $post_type_object = get_post_type_object($type);
- if ( !current_user_can( $post_type_object->cap->read_private_posts ) ) {
- $cache_key .= '_' . $perm . '_' . $user->ID;
- $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
- }
- }
- $query .= ' GROUP BY post_status';
- $count = wp_cache_get($cache_key, 'counts');
- if ( false !== $count )
- return $count;
- $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
- $stats = array();
- foreach ( get_post_stati() as $state )
- $stats[$state] = 0;
- foreach ( (array) $count as $row )
- $stats[$row['post_status']] = $row['num_posts'];
- $stats = (object) $stats;
- wp_cache_set($cache_key, $stats, 'counts');
- return $stats;
- }
- /**
- * Almost-exact copy of WP_Posts_List_Table::get_views(), but makes subtle changes for $this references and calls internal Admin_For_Authors::wp_count_posts() function instead
- * Changes highlighted with comments starting //EDIT
- * @return array
- */
- function get_views() {
- global $locked_post_status, $avail_post_stati;
- $this->screen = get_current_screen(); //EDIT - get $screen for use on $this->screen
- $post_type = $this->screen->post_type;
- if ( !empty($locked_post_status) )
- return array();
- $status_links = array();
- $num_posts = self::wp_count_posts( $post_type, 'readable' );
- $class = '';
- $allposts = '';
- $current_user_id = get_current_user_id();
- if ( $this->user_posts_count ) {
- if ( isset( $_GET['author'] ) && ( $_GET['author'] == $current_user_id ) )
- $class = ' class="current"';
- $status_links['mine'] = "<a href='edit.php?post_type=$post_type&author=$current_user_id'$class>" . sprintf( _nx( 'Mine <span class="count">(%s)</span>', 'Mine <span class="count">(%s)</span>', $this->user_posts_count, 'posts' ), number_format_i18n( $this->user_posts_count ) ) . '</a>';
- $allposts = '&all_posts=1';
- }
- $total_posts = array_sum( (array) $num_posts );
- // Subtract post types that are not included in the admin all list.
- foreach ( get_post_stati( array('show_in_admin_all_list' => false) ) as $state )
- $total_posts -= $num_posts->$state;
- $class = empty( $class ) && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['show_sticky'] ) ? ' class="current"' : '';
- $status_links['all'] = "<a href='edit.php?post_type=$post_type{$allposts}'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts' ), number_format_i18n( $total_posts ) ) . '</a>';
- foreach ( get_post_stati(array('show_in_admin_status_list' => true), 'objects') as $status ) {
- $class = '';
- $status_name = $status->name;
- if ( !in_array( $status_name, $avail_post_stati ) )
- continue;
- if ( empty( $num_posts->$status_name ) )
- continue;
- if ( isset($_REQUEST['post_status']) && $status_name == $_REQUEST['post_status'] )
- $class = ' class="current"';
- $status_links[$status_name] = "<a href='edit.php?post_status=$status_name&post_type=$post_type'$class>" . sprintf( translate_nooped_plural( $status->label_count, $num_posts->$status_name ), number_format_i18n( $num_posts->$status_name ) ) . '</a>';
- }
- //EDIT - START this whole if statement gets sticky posts stat, copied from WP_Posts_List_Table::_construct() but there's maybe a better way for this
- global $wpdb;
- if ( 'post' == $post_type && $sticky_posts = get_option( 'sticky_posts' ) ) {
- $sticky_posts = implode( ', ', array_map( 'absint', (array) $sticky_posts ) );
- $this->sticky_posts_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( 1 ) FROM $wpdb->posts WHERE post_type = %s AND post_status != 'trash' AND ID IN ($sticky_posts)", $post_type ) );
- }
- //EDIT - END
- if ( ! empty( $this->sticky_posts_count ) ) {
- $class = ! empty( $_REQUEST['show_sticky'] ) ? ' class="current"' : '';
- $sticky_link = array( 'sticky' => "<a href='edit.php?post_type=$post_type&show_sticky=1'$class>" . sprintf( _nx( 'Sticky <span class="count">(%s)</span>', 'Sticky <span class="count">(%s)</span>', $this->sticky_posts_count, 'posts' ), number_format_i18n( $this->sticky_posts_count ) ) . '</a>' );
- // Sticky comes after Publish, or if not listed, after All.
- $split = 1 + array_search( ( isset( $status_links['publish'] ) ? 'publish' : 'all' ), array_keys( $status_links ) );
- $status_links = array_merge( array_slice( $status_links, 0, $split ), $sticky_link, array_slice( $status_links, $split ) );
- }
- return $status_links;
- }
- }
- add_action('admin_init','Admin_For_Authors::init');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement