Advertisement
Guest User

restrict_current_user

a guest
Oct 1st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. //Get usr role as the restruictions don't work properly src: http://docs.appthemes.com/tutorials/wordpress-check-user-role-function/
  2.  
  3. function check_user_role( $role, $user_id = null ) {
  4.  
  5.     if ( is_numeric( $user_id ) )
  6.         $user = get_userdata( $user_id );
  7.     else
  8.         $user = wp_get_current_user();
  9.  
  10.     if ( empty( $user ) )
  11.         return false;
  12.  
  13.     return in_array( $role, (array) $user->roles );}
  14.  
  15.  
  16. //Source from this site http://wordpress.stackexchange.com/questions/30331/update-post-counts-published-draft-unattached-in-admin-interface/53791#53791
  17. //Update with only current user counts
  18. foreach( array( 'edit-post', 'edit-page', 'upload' ) as $hook )
  19.     add_filter( "views_$hook" , 'wpse_30331_custom_view_count', 10, 1);
  20.  
  21. function wpse_30331_custom_view_count( $views )
  22. {
  23.     global $current_screen;
  24.     switch( $current_screen->id )
  25.     {
  26.         case 'edit-post':
  27.             $views = wpse_30331_manipulate_views( 'post', $views );
  28.             break;
  29.         case 'edit-page':
  30.             $views = wpse_30331_manipulate_views( 'page', $views );
  31.             break;        case 'upload':
  32.             $views = wpse_30331_manipulate_views( 'attachment', $views );
  33.             break;
  34.     }
  35.     return $views;}
  36.  
  37. function wpse_30331_manipulate_views( $what, $views ){
  38.     global $user_ID, $wpdb;
  39.  
  40.     /*
  41.      * This is not working for me, 'artist' and 'administrator' are passing this condition (?)
  42.      */
  43.     if ( !current_user_can('delete_users') )
  44.         return $views;
  45.  
  46.     /*
  47.      * This needs refining, and maybe a better method
  48.      * e.g. Attachments have completely different counts
  49.      */
  50.     $total = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE (post_status = 'publish' OR post_status = 'draft' OR post_status = 'pending') AND (post_author = '$user_ID'  AND post_type = '$what' ) ");
  51.     $publish = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = '$user_ID' AND post_type = '$what' ");
  52.     $draft = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'draft' AND post_author = '$user_ID' AND post_type = '$what' ");
  53.     $pending = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'pending' AND post_author = '$user_ID' AND post_type = '$what' ");
  54.  
  55.     /*
  56.      * Only tested with Posts/Pages
  57.      * - there are moments where Draft and Pending shouldn't return any value
  58.      */
  59.     $views['all'] = preg_replace( '/\(.+\)/U', '('.$total.')', $views['all'] );
  60.     $views['publish'] = preg_replace( '/\(.+\)/U', '('.$publish.')', $views['publish'] );
  61.     $views['draft'] = preg_replace( '/\(.+\)/U', '('.$draft.')', $views['draft'] );
  62.     $views['pending'] = preg_replace( '/\(.+\)/U', '('.$pending.')', $views['pending'] );
  63.  
  64.     // Debug info
  65.     //echo 'Default counts: <pre>'.print_r($views,true).'</pre>';
  66.     //echo '<hr><hr>';
  67.     //echo 'Query for this screen of this post_type: <b>'.$what.'</b><pre>'.print_r($wp_query,true).'</pre>';
  68.  
  69.     return $views;}
  70.      if (!current_user_can('delete_users')) {
  71.   add_filter('parse_query', 'wpse_30331_manipulate_views' );
  72.   add_filter('parse_query', 'wpse_30331_custom_view_count' );
  73. }
  74.  
  75. //end counts update
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement