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

Add send_to_editor filter #681

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

goldenapples
Copy link
Contributor

Adds a filter which can be used for client-side validation of a shortcode before sending it to the editor. Basic concept for addressing issue #615; for discussion only at this point. (I think this needs some
kind of syntactic sugar to help work with it, as the API this exposes doesn't follow any established conventions in the WP universe and isn't self-explanatory at all.)

@pySilver @marcin-lawrowski This seems like the bare minimum to make the use case you described in #615 possible.

Hooking into this and blocking sending the shortcode to the editor would be a matter of returning a rejected promise from that filter, like this:

wp.shortcake.hooks.addFilter( 'shortcode-ui.send_to_editor', 
    function( okToSend$, shortcode ) {
        console.log( shortcode );
        okToSend$.reject();
        return okToSend$;
    }
);

Any thoughts on making this easier to work with? I like the idea of registering validation methods in the shortcode_ui_register_for_shortcode function, but I'm torn as to whether they belong on the attributes, the shortcode as a whole, or both.

Adds a filter which can be used for client-side validation of a
shortcode before sending it to the editor. Basic concept for addressing
issue #615; for discussion only at this point. (I think this needs some
kind of syntactic sugar to help work with it, as the API this exposes
doesn't follow any established conventions in the WP universe and isn't
self-explanatory at all.)
var sendToEditor$ = wp.shortcake.hooks.applyFilters( 'shortcode-ui.send_to_editor', okToInsert$, shortcode );

// Unless a filter has interfered, resolve the promise, sending the shortcode to the editor.
setTimeout( function() { okToInsert$.resolve(true); } );
Copy link
Member

@davisshaver davisshaver Jun 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not that familiar with $.Deferred.() and using it with setTimeout(), so let me try parroting this back first. If there's a hook applied, sendToEditor$ could be a rejected promise. Otherwise it should be the value of okToInsert$, which is an unfulfilled promise.

What does the timeout do? Could we set okToInsert$ as a resolved promise up top and remove the timeout portion? Seems like it might be the same result.

@davisshaver
Copy link
Member

Re validation, my inclination is that shortcode-level validation would make the most sense and not limit any field-specific use cases.

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

Successfully merging this pull request may close these issues.

2 participants