Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload by media category #1414

Open
wants to merge 50 commits into
base: develop
Choose a base branch
from

Conversation

KTS915
Copy link
Member

@KTS915 KTS915 commented Apr 26, 2024

This PR follows PR #1403 and provides an additional option to upload a file according to media category:
Screenshot at 2024-04-26 12-05-57

If no media category has yet been created, the option is disabled with a message explaining that one needs to be created (together with a hyperlink to the place to do so):
Screenshot at 2024-04-26 12-10-08

Once that's done, a new dropdown will appear on the Add Media page, which will look like one of the following according to which uploader the user has chosen:
Screenshot at 2024-04-26 12-11-36
Screenshot at 2024-04-26 12-12-16

However, until the user has chosen a category in the dropdown, the uploader will be disabled (i.e. it will have the inert attribute applied to it). Once a category is chosen, this fires some JS to a new action at admin-ajax.php, which then registers the media category as the file destination and removes the inert attribute from the uploader, so that it can then function as normal.

When the file is uploaded to the media category folder, it will also automatically be added the media category within the admin, as will be shown by going to the List view of the media library.

@KTS915 KTS915 added this to the 2.1 milestone Apr 26, 2024
@mattyrob
Copy link
Collaborator

I have been testing with a Media category called media and in Safari I have managed to upload files without a Media category selected and I've also noted that the files have uploaded to wp-content/uploads/ when I suspect it should have been wp-content/uploads/media/ - am I wrong?

When 'year' is selected the image is correctly added to wp-content/uploads/2024/.

@KTS915
Copy link
Member Author

KTS915 commented Apr 27, 2024

It looks like the sanitizing method I've used is causing this (on line 3884 of ~/wp-admin/includes/media.php ). I used sanitize_url to sanitize the option to set the correct subfolder. I used that because the value is appended to a URL, but it seems it then gets prepended with http:// or https:// in many cases.

Unfortunately sanitize_option and sanitize_key also won't work, because the strings being sanitized may contain a /. So what's the correct sanitization method here? Or is it OK not to use any?

@KTS915
Copy link
Member Author

KTS915 commented Apr 27, 2024

Hmm, sanitize_text_field seems to work. What do you think?

@KTS915
Copy link
Member Author

KTS915 commented Apr 27, 2024

I think sanitize_text_field is too permissive. However, after trying a few things, I think that sanitize_url is the correct sanitization method. I just need to ensure that the value being sanitized is prepended with /. Then it is seen as the relative URL that it is and everything works as intended. This means that I then need to remove that / from the place where it's currently added in the ~/wp-includes/functions.php file. New commit incoming!

And thanks for checking and spotting this!

@mattyrob
Copy link
Collaborator

Works are described now - the only irritation for me is in the standard media uploader, if no category is selected and am image is dragged and dropped, it results in the browser loading the image into the window. I doubt there is much that can do about that unless the code can intercept the upload but prevent it when no category is selected.

@KTS915
Copy link
Member Author

KTS915 commented Apr 28, 2024

I'll look further into that later today.

@KTS915
Copy link
Member Author

KTS915 commented Apr 28, 2024

Latest commit should address the problem of loading the image into the browser window when uploading is disabled. It turns out that there's no need to intercept the plupload script but just need to make use of the vanilla JavaScript API.

@xxsimoxx
Copy link
Member

Usually I upload images directly dropping to the Media Library, and not from the "Add New".
From here I can't decide the category, and I get the image uploaded under the first category slug, and no category added.

@mattyrob mattyrob added this to the 2.2 milestone May 2, 2024
@KTS915
Copy link
Member Author

KTS915 commented May 2, 2024

Thanks to a MutationObserver, the current status of this PR is that this should now be working as expected, except in the following case.

If someone goes to add a file from a post, page, or text widget, then uploads the file but then does not insert it into the post, page, or widget, and then uploads a new file, any attempt to change the location for this new file will not work.

What happens is that any attempt to select a new location is sent to and stored in the database, but the failure to insert the first file seems to mess up the modal itself; it continues to operate as if the location for the first file must be the correct location.

If the first file is actually inserted into the post, page, or widget, everything works as intended. It also works if the page is saved or refreshed after the abortive first upload.

So this is a bit of an edge case, and the fix is probably going to be hard to find, but it's where we are now.

@KTS915
Copy link
Member Author

KTS915 commented May 4, 2024

@mattyrob @xxsimoxx With my latest commit, I think I've solved this now!

@xxsimoxx
Copy link
Member

xxsimoxx commented May 6, 2024

Still getting similar errors, sometime the category is not added, sometime it's added the one that is the default for the dropdown.

@KTS915
Copy link
Member Author

KTS915 commented May 6, 2024

That's disappointing! But thanks as always for checking, @xxsimoxx. Backbone.js is a black box that's hard to penetrate.

I'm going to put this on the back-burner for now and come back to it again later.

@KTS915
Copy link
Member Author

KTS915 commented Jun 6, 2024

With my latest commits, I think this should work now! (I certainly hope so!)

@xxsimoxx
Copy link
Member

I'm already not getting the category assigned. Seem that this happens when I leave the dropdown unchanged.
Also (but don't know if this issue is related to this PR) I'm getting, in the Categories page, a wrong count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants