Wednesday, September 1, 2010

WP: hide photos uploaded by other in Media Upload and Media Library

Tested on WordPress 3.0

A user with the capacity of uploading doesn't need to see what other have uploaded before.

To do so we need to hack a little bit WP core because this process is unpluggable right now.

We need to change two files:
wp-admin/includes/media.php
wp-admin/edit-attachements-row.php (for this one css can be used instead of a hack)

1. media.php
Around line 1122 replace function get_media_items with this

function get_media_items( $post_id, $errors ) {
$attachments = array();
if ( $post_id ) {
$post = get_post($post_id);
if ( $post && $post->post_type == 'attachment' )
$attachments = array($post->ID => $post);
else
$attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
} else {
if ( is_array($GLOBALS['wp_the_query']->posts) )
foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
$attachments[$attachment->ID] = $attachment;
}

$output = '';
foreach ( (array) $attachments as $id => $attachment ) {
if ( $attachment->post_status == 'trash' )
continue;

#HACK#
global $current_user;
$getid = get_post($id);
$aut = $getid->post_author;

if (( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )&& ($current_user->user_level == 10)) {
$output .= "\n<div id='media-item-$id' class='admin media-item child-of-$attachment->post_parent preloaded'><div class='progress'><div class='bar'></div></div><div id='media-upload-error-$id'></div><div class='filename'></div>$item\n</div>";
}
elseif (( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )&& ($aut==$current_user->ID)){
$output .= "\n<div id='media-item-$id' class='user media-item child-of-$attachment->post_parent preloaded'><div class='progress'><div class='bar'></div></div><div id='media-upload-error-$id'></div><div class='filename'></div>$item\n</div>";

}
#END HACK#
}

return $output;
}

2.edit-attachements-row.php

Around line 45 Before
<tr id='post-<?php echo $id; ?>' class='<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>' valign="top">
add
<?php if ($current_user->ID == $post->post_author){#HACK# ?>

Around line 229 Before
<?php endwhile; ?>
add
<?php } #END HACK# ?>

0 comments:

Post a Comment