WordPress - Media Upload

There are at least 2 design problems in the media upload section In total, I had to modify 3 core WordPress files to fix these problems.

Incorrect "edit" link | Basic Fix | Better Fix | Why two files | 500 - Internal server error

Incorrect "edit" link

There are 2 failures in upload.php (and edit-attachment-rows.php) when the user does not have permission to edit the attachment. The code fragments shown below are modified to improve readability.

This is the code that places a link around the icon. Notice that there is no test for user permissions.

This code places an "edit" link around the image title. get_edit_post_link() checks that the user is able to edit the post and returns either the "correct" string or null. However, the link should not even exist if the action is not permitted.

And this is the code that determines what links should be displayed under the title.

The important point is the test to see if the person is allowed to edit the "post" before adding the link. This test should have also been added to the icon.

The same fixes had to be placed in both files (great design).

Basic Fix

Normally, I never delete other peoples code - but I do comment it out so it can be used as a reference. However, there is no way I know to comment out a block of code that hops into and out of php mode. (I tried - it produced a lot of errors.) As a result, I decided not to leave in the original code.

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.

The existing code switches in and out of php-mode many times and, therefore, is very hard to read. The following places the icon (if it exists) in a table column. The rest of the code was not changed.

There is a similar design error associated with the parent post (if any) which I did not fix.

Better Fix

The code is way more complicated than necessary. The following places the icon (if it exists) in a table column (as before), but does not make it clickable. (What's the point?) The second case condition displays the attachment title and also does not make it clickable. Since neither of these can be clicked, there is no reason for the fancy quoted title and that is removed. This is also in the code. It should be replaced with (Using double quotes permits variable substitution ... and, therefore, requires escaping double quotes desired in the final string.)

Why two files

upload.php and edit-attachment-rows.php are similar and different. The primary differences I see are This is from edit-attachment-rows.php when defining the styles used on icons and this is the similar code from upload.php (which is just straight code, no case structure). It is hard to tell if this is an intentional design difference, or simply an error. (Opinions may differ.)

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

I found the design problems presented on this page because clicking icons on the Library (attachments) page produced 500 - Internal server error's with absolutely no useful information.

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
URL: http:// mc-computing.com / ISPs / WordPress / Media_Upload.html