diff --git a/inc/class-shortcode-ui.php b/inc/class-shortcode-ui.php index 60e2d60e..fd8687b5 100644 --- a/inc/class-shortcode-ui.php +++ b/inc/class-shortcode-ui.php @@ -50,6 +50,21 @@ public function register_shortcode_ui( $shortcode_tag, $args = array() ) { $args['shortcode_tag'] = $shortcode_tag; $this->shortcodes[ $shortcode_tag ] = $args; + // filter the attrs to decode fields with escape=true + add_filter( "shortcode_atts_{$shortcode_tag}", function( $out, $pairs, $atts ) use ( $args ) { + + $fields = Shortcode_UI_Fields::get_instance()->get_fields(); + + foreach ( $args['attrs'] as $attr ) { + if ( ! $fields[ $attr['type'] ]['escape'] ) { + continue; + } + $out[ $attr['attr'] ] = rawurldecode( $out[ $attr['attr'] ] ); + } + + return $out; + }, 1, 3 ); + } public function get_shortcodes() { diff --git a/inc/fields/class-shortcode-ui-fields.php b/inc/fields/class-shortcode-ui-fields.php index 125b855d..4a606504 100644 --- a/inc/fields/class-shortcode-ui-fields.php +++ b/inc/fields/class-shortcode-ui-fields.php @@ -8,6 +8,7 @@ class Shortcode_UI_Fields { private $field_defaults = array( 'template' => 'shortcode-ui-field-text', 'view' => 'editAttributeField', + 'escape' => false, ); // Field Settings. @@ -15,6 +16,7 @@ class Shortcode_UI_Fields { 'text' => array(), 'textarea' => array( 'template' => 'shortcode-ui-field-textarea', + 'escape' => true, ), 'url' => array( 'template' => 'shortcode-ui-field-url', diff --git a/js-tests/build/specs.js b/js-tests/build/specs.js index b1291abd..4735a541 100644 --- a/js-tests/build/specs.js +++ b/js-tests/build/specs.js @@ -38,6 +38,7 @@ describe( "Shortcode Attribute Model", function() { type: 'text', value: 'test value', description: 'test description', + escape: false, meta: { placeholder: 'test placeholder' } @@ -73,7 +74,7 @@ describe( "Shortcode Model", function() { label: 'Attribute', type: 'text', value: 'test value', - placeholder: 'test placeholder', + placeholder: 'test placeholder' } ], inner_content: { @@ -359,6 +360,7 @@ var ShortcodeAttribute = Backbone.Model.extend({ type: '', value: '', description: '', + escape: false, meta: { placeholder: '', } @@ -443,6 +445,13 @@ Shortcode = Backbone.Model.extend({ return; } + var type = attr.get( 'type' ); + + // Encode textareas incase HTML + if ( shortcodeUIFieldData[ type ] && shortcodeUIFieldData[ type ].escape ) { + attr.set( 'value', encodeURIComponent( decodeURIComponent( attr.get( 'value' ) ) ) ); + } + attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' ); } ); diff --git a/js-tests/src/shortcodeAttributeModelSpec.js b/js-tests/src/shortcodeAttributeModelSpec.js index f9088700..175253ec 100644 --- a/js-tests/src/shortcodeAttributeModelSpec.js +++ b/js-tests/src/shortcodeAttributeModelSpec.js @@ -8,6 +8,7 @@ describe( "Shortcode Attribute Model", function() { type: 'text', value: 'test value', description: 'test description', + escape: false, meta: { placeholder: 'test placeholder' } diff --git a/js-tests/src/shortcodeModelSpec.js b/js-tests/src/shortcodeModelSpec.js index 27d51a37..c6433aec 100644 --- a/js-tests/src/shortcodeModelSpec.js +++ b/js-tests/src/shortcodeModelSpec.js @@ -17,7 +17,7 @@ describe( "Shortcode Model", function() { label: 'Attribute', type: 'text', value: 'test value', - placeholder: 'test placeholder', + placeholder: 'test placeholder' } ], inner_content: { diff --git a/js/build/field-attachment.js b/js/build/field-attachment.js index 336b5c70..87ac2206 100644 --- a/js/build/field-attachment.js +++ b/js/build/field-attachment.js @@ -214,6 +214,7 @@ var ShortcodeAttribute = Backbone.Model.extend({ type: '', value: '', description: '', + escape: false, meta: { placeholder: '', } @@ -298,6 +299,13 @@ Shortcode = Backbone.Model.extend({ return; } + var type = attr.get( 'type' ); + + // Encode textareas incase HTML + if ( shortcodeUIFieldData[ type ] && shortcodeUIFieldData[ type ].escape ) { + attr.set( 'value', encodeURIComponent( decodeURIComponent( attr.get( 'value' ) ) ) ); + } + attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' ); } ); diff --git a/js/build/field-color.js b/js/build/field-color.js index 508021ed..ba92bf0e 100644 --- a/js/build/field-color.js +++ b/js/build/field-color.js @@ -85,6 +85,7 @@ var ShortcodeAttribute = Backbone.Model.extend({ type: '', value: '', description: '', + escape: false, meta: { placeholder: '', } @@ -169,6 +170,13 @@ Shortcode = Backbone.Model.extend({ return; } + var type = attr.get( 'type' ); + + // Encode textareas incase HTML + if ( shortcodeUIFieldData[ type ] && shortcodeUIFieldData[ type ].escape ) { + attr.set( 'value', encodeURIComponent( decodeURIComponent( attr.get( 'value' ) ) ) ); + } + attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' ); } ); diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index a50eabf5..356f2b79 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -118,6 +118,7 @@ var ShortcodeAttribute = Backbone.Model.extend({ type: '', value: '', description: '', + escape: false, meta: { placeholder: '', } @@ -202,6 +203,13 @@ Shortcode = Backbone.Model.extend({ return; } + var type = attr.get( 'type' ); + + // Encode textareas incase HTML + if ( shortcodeUIFieldData[ type ] && shortcodeUIFieldData[ type ].escape ) { + attr.set( 'value', encodeURIComponent( decodeURIComponent( attr.get( 'value' ) ) ) ); + } + attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' ); } ); @@ -715,16 +723,14 @@ var EditShortcodeForm = wp.Backbone.View.extend({ return; } - var templateData = { - value: attr.get('value'), - attr_raw: { - name: attr.get('value') - } - } - var viewObjName = shortcodeUIFieldData[ type ].view; var tmplName = shortcodeUIFieldData[ type ].template; + // decode textareas / html + if ( shortcodeUIFieldData[ type ].escape ) { + attr.set( 'value', decodeURIComponent( attr.get( 'value' ) ) ); + } + var view = new sui.views[viewObjName]( { model: attr } ); view.template = wp.media.template( tmplName ); view.shortcode = t.model; diff --git a/js/src/models/shortcode-attribute.js b/js/src/models/shortcode-attribute.js index 698ed4b3..c14334b7 100644 --- a/js/src/models/shortcode-attribute.js +++ b/js/src/models/shortcode-attribute.js @@ -7,6 +7,7 @@ var ShortcodeAttribute = Backbone.Model.extend({ type: '', value: '', description: '', + escape: false, meta: { placeholder: '', } diff --git a/js/src/models/shortcode.js b/js/src/models/shortcode.js index eb9cae80..19d9107f 100644 --- a/js/src/models/shortcode.js +++ b/js/src/models/shortcode.js @@ -71,6 +71,13 @@ Shortcode = Backbone.Model.extend({ return; } + var type = attr.get( 'type' ); + + // Encode textareas incase HTML + if ( shortcodeUIFieldData[ type ] && shortcodeUIFieldData[ type ].escape ) { + attr.set( 'value', encodeURIComponent( decodeURIComponent( attr.get( 'value' ) ) ) ); + } + attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' ); } ); diff --git a/js/src/views/edit-shortcode-form.js b/js/src/views/edit-shortcode-form.js index 3e49c297..64dcc672 100644 --- a/js/src/views/edit-shortcode-form.js +++ b/js/src/views/edit-shortcode-form.js @@ -36,16 +36,14 @@ var EditShortcodeForm = wp.Backbone.View.extend({ return; } - var templateData = { - value: attr.get('value'), - attr_raw: { - name: attr.get('value') - } - } - var viewObjName = shortcodeUIFieldData[ type ].view; var tmplName = shortcodeUIFieldData[ type ].template; + // decode textareas / html + if ( shortcodeUIFieldData[ type ].escape ) { + attr.set( 'value', decodeURIComponent( attr.get( 'value' ) ) ); + } + var view = new sui.views[viewObjName]( { model: attr } ); view.template = wp.media.template( tmplName ); view.shortcode = t.model;