From 264008875a7c12a47e3375f3d96fb04ee18f6d68 Mon Sep 17 00:00:00 2001 From: Nathaniel Taintor Date: Thu, 29 Dec 2016 11:58:44 -0800 Subject: [PATCH 1/2] Add `send_to_editor` filter 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.) --- js/build/shortcode-ui.js | 34 ++++++++++++++++++++++---- js/src/controllers/media-controller.js | 34 ++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index a3c466f0..07c9512a 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -36,6 +36,7 @@ module.exports = Shortcodes; },{"./../models/shortcode.js":6}],3:[function(require,module,exports){ (function (global){ var Backbone = (typeof window !== "undefined" ? window['Backbone'] : typeof global !== "undefined" ? global['Backbone'] : null), + $ = (typeof window !== "undefined" ? window['jQuery'] : typeof global !== "undefined" ? global['jQuery'] : null), wp = (typeof window !== "undefined" ? window['wp'] : typeof global !== "undefined" ? global['wp'] : null), sui = require('./../utils/sui.js'), Shortcodes = require('./../collections/shortcodes.js'); @@ -70,12 +71,35 @@ var MediaController = wp.media.controller.State.extend({ }, insert: function() { + var self = this; var shortcode = this.props.get('currentShortcode'); - if ( shortcode ) { - send_to_editor( shortcode.formatShortcode() ); - this.reset(); - this.frame.close(); - } + var okToInsert$ = $.Deferred(); + + /* + * Filter run before a shortcode is sent to the editor. Can be used for + * client-side validation before closing the media modal. + * + * Called as `shortcode_ui.send_to_editor`. + * + * + * + * @param $.Deferred A promise which is expected to either resolve if + * the shortcode can be sent to the editor, or reject + * if not. + * @param Shortcode The current shortcode model. + */ + var sendToEditor$ = wp.shortcake.hooks.applyFilters( 'shortcode-ui.send_to_editor', okToInsert$, shortcode ); + + // Unless a filter has ch, resolve the promise, sending the shortcode to the editor. + setTimeout( function() { okToInsert$.resolve(true); } ); + + sendToEditor$.then( + function() { + send_to_editor( shortcode.formatShortcode() ); + self.reset(); + self.frame.close(); + } + ); }, reset: function() { diff --git a/js/src/controllers/media-controller.js b/js/src/controllers/media-controller.js index 64fc481a..9c0e712d 100644 --- a/js/src/controllers/media-controller.js +++ b/js/src/controllers/media-controller.js @@ -1,4 +1,5 @@ var Backbone = require('backbone'), + $ = require('jquery'), wp = require('wp'), sui = require('sui-utils/sui'), Shortcodes = require('sui-collections/shortcodes'); @@ -33,12 +34,35 @@ var MediaController = wp.media.controller.State.extend({ }, insert: function() { + var self = this; var shortcode = this.props.get('currentShortcode'); - if ( shortcode ) { - send_to_editor( shortcode.formatShortcode() ); - this.reset(); - this.frame.close(); - } + var okToInsert$ = $.Deferred(); + + /* + * Filter run before a shortcode is sent to the editor. Can be used for + * client-side validation before closing the media modal. + * + * Called as `shortcode_ui.send_to_editor`. + * + * + * + * @param $.Deferred A promise which is expected to either resolve if + * the shortcode can be sent to the editor, or reject + * if not. + * @param Shortcode The current shortcode model. + */ + var sendToEditor$ = wp.shortcake.hooks.applyFilters( 'shortcode-ui.send_to_editor', okToInsert$, shortcode ); + + // Unless a filter has ch, resolve the promise, sending the shortcode to the editor. + setTimeout( function() { okToInsert$.resolve(true); } ); + + sendToEditor$.then( + function() { + send_to_editor( shortcode.formatShortcode() ); + self.reset(); + self.frame.close(); + } + ); }, reset: function() { From 8ced216d3b0f6e23add0d512dd44666d4a5b29dc Mon Sep 17 00:00:00 2001 From: Nathaniel Taintor Date: Thu, 29 Dec 2016 12:23:44 -0800 Subject: [PATCH 2/2] Fix hook name in docblock and explanatory comment --- js/build/shortcode-ui.js | 6 ++---- js/src/controllers/media-controller.js | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 07c9512a..56d33a23 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -79,9 +79,7 @@ var MediaController = wp.media.controller.State.extend({ * Filter run before a shortcode is sent to the editor. Can be used for * client-side validation before closing the media modal. * - * Called as `shortcode_ui.send_to_editor`. - * - * + * Called as `shortcode-ui.send_to_editor`. * * @param $.Deferred A promise which is expected to either resolve if * the shortcode can be sent to the editor, or reject @@ -90,7 +88,7 @@ var MediaController = wp.media.controller.State.extend({ */ var sendToEditor$ = wp.shortcake.hooks.applyFilters( 'shortcode-ui.send_to_editor', okToInsert$, shortcode ); - // Unless a filter has ch, resolve the promise, sending the shortcode to the editor. + // Unless a filter has interfered, resolve the promise, sending the shortcode to the editor. setTimeout( function() { okToInsert$.resolve(true); } ); sendToEditor$.then( diff --git a/js/src/controllers/media-controller.js b/js/src/controllers/media-controller.js index 9c0e712d..0ba2d843 100644 --- a/js/src/controllers/media-controller.js +++ b/js/src/controllers/media-controller.js @@ -42,9 +42,7 @@ var MediaController = wp.media.controller.State.extend({ * Filter run before a shortcode is sent to the editor. Can be used for * client-side validation before closing the media modal. * - * Called as `shortcode_ui.send_to_editor`. - * - * + * Called as `shortcode-ui.send_to_editor`. * * @param $.Deferred A promise which is expected to either resolve if * the shortcode can be sent to the editor, or reject @@ -53,7 +51,7 @@ var MediaController = wp.media.controller.State.extend({ */ var sendToEditor$ = wp.shortcake.hooks.applyFilters( 'shortcode-ui.send_to_editor', okToInsert$, shortcode ); - // Unless a filter has ch, resolve the promise, sending the shortcode to the editor. + // Unless a filter has interfered, resolve the promise, sending the shortcode to the editor. setTimeout( function() { okToInsert$.resolve(true); } ); sendToEditor$.then(