Open marcin-lawrowski opened 8 years ago
@marcin-lawrowski any idea how to implement this until this in core? I'm looking for the way to prevent invalid shortcode insertions.
We'd probably want to insert a filter in here, right before the send_to_editor()
function is called. I'm not sure what context would be useful to pass in, but getting the entire shortcode
model might be enough.
What kind of validation would you want to be doing here that couldn't be done on updating each attribute? Are you just thinking of checking to make sure all required attributes are filled out?
@goldenapples in my particular case I want to prevent invalid values to be provided in params since my shortcode talks to an external webservice. In order to make it user friendly I need to make a prior request before inserting shortcode into post and ofc an ability to report an error (field based or form general error)
Ah, makes sense.
The system of JS hooks we use might not be great for that purpose, since they'd tie up execution while you made your external API call. What if we implemented a JS filter at that spot that could return a Promise as a return value, and then wait until that promise resolves to close the modal and send the shortcode to the editor?
@goldenapples sounds ok :)
I think it would be interesting to have a general validation filter which one can mention in shortcode_ui_register_for_shortcode
by simply providing callback name. It would be up to developer whether this JS callable is defined and what it does. It's just require to respond with specific format.
We can even scaffold some solution that would tie JS to call some php callback with ajax. Which might be even easier solution for developers.
It would be great to have a hook that is triggered after clicking "Insert Element" button. There should be a possibility to discard closing of the layer. This hook would be an ideal place to implement the client-side validation.