Advertisement
deepbevel

authors own content only in dashboard

Jul 13th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. //
  2. // Mostrar sólo las Publicaciones, Comentarios y Multimedia que sean Propios
  3. // Más info en > PC-SERVEIS, Diseño Web - http://www.pc-serveis.es
  4. //
  5.  
  6. // Show only posts and media related to logged in author
  7. add_action('pre_get_posts', 'query_set_only_author' );
  8. function query_set_only_author( $wp_query ) {
  9. global $current_user;
  10. if( is_admin() && !current_user_can('edit_others_posts') ) {
  11. $wp_query->set( 'author', $current_user->ID );
  12. add_filter('views_edit-post', 'fix_post_counts');
  13. add_filter('views_upload', 'fix_media_counts');
  14. }
  15. }
  16.  
  17. // Fix post counts
  18. function fix_post_counts($views) {
  19. global $current_user, $wp_query;
  20. unset($views['mine']);
  21. $types = array(
  22. array( 'status' => NULL ),
  23. array( 'status' => 'publish' ),
  24. array( 'status' => 'draft' ),
  25. array( 'status' => 'pending' ),
  26. array( 'status' => 'trash' )
  27. );
  28. foreach( $types as $type ) {
  29. $query = array(
  30. 'author' => $current_user->ID,
  31. 'post_type' => 'post',
  32. 'post_status' => $type['status']
  33. );
  34. $result = new WP_Query($query);
  35. if( $type['status'] == NULL ):
  36. $class = ($wp_query->query_vars['post_status'] == NULL) ? ' class="current"' : '';
  37. $views['all'] = sprintf(__('<a href="%s"'. $class .'>Todas <span class="count">(%d)</span></a>', 'all'),
  38. admin_url('edit.php?post_type=post'),
  39. $result->found_posts);
  40. elseif( $type['status'] == 'publish' ):
  41. $class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : '';
  42. $views['publish'] = sprintf(__('<a href="%s"'. $class .'>Publicadas <span class="count">(%d)</span></a>', 'publish'),
  43. admin_url('edit.php? post_status=publish&post_type=post'),
  44. $result->found_posts);
  45. elseif( $type['status'] == 'draft' ):
  46. $class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : '';
  47. $views['draft'] = sprintf(__('<a href="%s"'. $class .'>Borrador'. ((sizeof($result->posts) > 1) ? "s" : "") .' <span class="count">(%d)</span></a>', 'draft'),
  48. admin_url('edit.php?post_status=draft&post_type=post'),
  49. $result->found_posts);
  50. elseif( $type['status'] == 'pending' ):
  51. $class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : '';
  52. $views['pending'] = sprintf(__('<a href="%s"'. $class .'>Pendiente <span class="count">(%d)</span></a>', 'pending'),
  53. admin_url('edit.php?post_status=pending&post_type=post'),
  54. $result->found_posts);
  55. elseif( $type['status'] == 'trash' ):
  56. $class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : '';
  57. $views['trash'] = sprintf(__('<a href="%s"'. $class .'>Papelera <span class="count">(%d)</span></a>', 'trash'),
  58. admin_url('edit.php?post_status=trash&post_type=post'),
  59. $result->found_posts);
  60. endif;
  61. }
  62. return $views;
  63. }
  64.  
  65. // Fix media counts
  66. function fix_media_counts($views) {
  67. global $wpdb, $current_user, $post_mime_types, $avail_post_mime_types;
  68. $views = array();
  69. $count = $wpdb->get_results( "
  70. SELECT post_mime_type, COUNT( * ) AS num_posts
  71. FROM $wpdb->posts
  72. WHERE post_type = 'attachment'
  73. AND post_author = $current_user->ID
  74. AND post_status != 'trash'
  75. GROUP BY post_mime_type
  76. ", ARRAY_A );
  77. foreach( $count as $row )
  78. if ($count && $row != 0) {
  79. $_num_posts[$row['post_mime_type']] = $row['num_posts'];
  80. $_total_posts = array_sum($_num_posts);
  81. $detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
  82. };
  83. if ( !isset( $total_orphans ) )
  84. $total_orphans = $wpdb->get_var("
  85. SELECT COUNT( * )
  86. FROM $wpdb->posts
  87. WHERE post_type = 'attachment'
  88. AND post_author = $current_user->ID
  89. AND post_status != 'trash'
  90. AND post_parent < 1
  91. ");
  92. if ($matches != 0) {
  93. $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  94. foreach ( $matches as $type => $reals )
  95. foreach ( $reals as $real )
  96. $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
  97. $class = ( empty($_GET['post_mime_type']) && !$detached && !isset($_GET['status']) ) ? ' class="current"' : '';
  98. $views['all'] = "<a href='upload.php'$class>" . sprintf( __('Todas <span class="count">(%s)</span>', 'uploaded files' ), number_format_i18n( $_total_posts )) . '</a>';
  99. foreach ( $post_mime_types as $mime_type => $label ) {
  100. $class = '';
  101. if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  102. continue;
  103. if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  104. $class = ' class="current"';
  105. if ( !empty( $num_posts[$mime_type] ) )
  106. $views[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), $num_posts[$mime_type] ) . '</a>';
  107. }
  108. $views['detached'] = '<a href="upload.php?detached=1"' . ( $detached ? ' class="current"' : '' ) . '>' . sprintf( __( 'Sin Adjuntar <span class="count">(%s)</span>', 'detached files' ), $total_orphans ) . '</a>';
  109. return $views;
  110. }
  111. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement