Incorrect "edit" link
This is the code that places a link around the icon. Notice that there is no test for user permissions.
<td class="media-icon"><?php if ( $thumb = wp_get_attachment_image( $post->ID, array(80, 60), true ) ) { ?> <a href="media.php?action=edit&attachment_id=<?php echo $post->ID; ?>" title="<?php echo esc_attr(sprintf(__('Edit “%s”'), $att_title)); ?>"><?php echo $thumb; ?></a> <?php } ?></td> |
<td class="media column-media"><strong><a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo esc_attr(sprintf(__('Edit “%s”'), $att_title)); ?>"><?php echo $att_title; ?></a></strong><br /> |
And this is the code that determines what links should be displayed under the title.
if ( current_user_can('edit_post', $post->ID) ) $actions['edit'] = '<a href="' . get_edit_post_link($post->ID, true) . '">' . __('Edit') . '</a>'; if ( current_user_can('delete_post', $post->ID) ) $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url("post.php?action=delete&post=$post->ID", 'delete-post_' . $post->ID) . "' onclick=" code not shown ">" . __('Delete') . "</a>"; $actions['view'] = '<a href="' . get_permalink($post->ID) . '" title="' . esc_attr(sprintf(__('View “%s”'), $title)) . '" rel="permalink">' . __('View') . '</a>'; |
The same fixes had to be placed in both files (great design).
Basic Fix
The first thing I noticed was that the code checked to see if the current user had permission to edit the information more than 4 times. Well, that made no sense at all. Therefore, I rewrote the code to check for permission only once and stored the result in a local variable ($edit_link). Basically, get_edit_post_link performs the check and returns an appropriately formatted "edit link" (if the permissions are correct) or it returns a null string (if they are not).
Long statements should not be repeated - it is better to save the result is a variable and then just reuse the variable (if for no other reason than it reduces the chance for error). It takes 3 subroutine calls to produce the string stored in $edit_title. In the original code, this identical string was created 3 separate times ... when only one is enough, and it speeds up the code.
The following code was placed inside the loop right after $att_title was defined.
// code fixed 09-01-2009 by rlc $edit_link = get_edit_post_link( $post->ID ); // this is used 3 times $edit_title = esc_attr(sprintf(__('Edit “%s”'), $att_title)); // this is used 3 times |
case 'icon': $attributes = 'class="column-icon media-icon"' . $style; ?> <td <?php echo $attributes ?>><?php if ( $thumb = wp_get_attachment_image( $post->ID, array(80, 60), true ) ) { // code fixed 09-01-2009 by rlc if ($edit_link){ echo "<a href=\"{$edit_link}\" title=\"{$edit_title}\">{$thumb}</a>"; } else { echo $thumb; } } ?></td> <?php // TODO break; case 'media': ?><td <?php echo $attributes ?>><strong><?php if ($edit_link){ echo "<a href=\"{$edit_link}\" title=\"{$edit_title}\">{$att_title}</a>"; } else { echo $att_title; } |
There is a similar design error associated with the parent post (if any) which I did not fix.
Better Fix
case 'icon': $attributes = 'class="column-icon media-icon"' . $style; ?> <td <?php echo $attributes ?>><?php if ( $thumb = wp_get_attachment_image( $post->ID, array(80, 60), true ) ) { echo $thumb; // code fixed 09-01-2009 by rlc } ?></td><?php // TODO break; case 'media': ?><td <?php echo $attributes ?>><strong><?php echo $att_title; // code fixed 09-01-2009 by rlc |
if ( current_user_can('edit_post', $post->ID) ) $actions['edit'] = '<a href="' . get_edit_post_link($post->ID, true) . '">' . __('Edit') . '</a>'; |
$edit_link = get_edit_post_link( $post->ID ); if ( $edit_link ) $actions['edit'] = "<a href=\"{$edit_link}\">" . __('Edit') . '</a>'; |
Why two files
$style = ''; if ( in_array($column_name, $hidden) ) $style = ' style="display:none;"'; // This supports hidden columns $attributes = "$class$style"; switch($column_name) { case 'icon': $attributes = 'class="column-icon media-icon"' . $style; ?> <td <?php echo $attributes ?>><?php |
<td class="media-icon"><?php |
Otherwise, they contain a lot of the same code and both need exactly the same patches. (This is almost the definition of a bad design.)
Note: Orphan attachments do not have parents - parent ID < 1.
500 - Internal server error
Details of this design problem are presented on their own page. Basically, there is a design problem (feature) in Internet Explorer 6 that throws away useful information when a server error is indicated.
In my opinion, the WordPress developers should have provided a useful (and user friendly) work around for this problem ... but decided to ignore it. (That's right, they knew about the problem and decided not to do anything about it.)
My page contains a partial fix - it works for the problems I have seen. Not perfect, but better than nothing.
Author: Robert Clemenzi