📁 File Manager Pro
v10.0.3 | PHP: 7.4.33
Server: LiteSpeed
2026-06-26 16:16:25
📂
/ (Root)
/
home
/
supecsoq
/
public_html
/
domains
/
migalexpark.com
/
wp-content
/
plugins
/
shortcode-ui
/
js
/
src
/
utils
📍 /home/supecsoq/public_html/domains/migalexpark.com/wp-content/plugins/shortcode-ui/js/src/utils
🔄 Refresh
✏️
Editing: shortcode-view-constructor.js
Writable
var sui = require('sui-utils/sui'), fetcher = require('sui-utils/fetcher'), wp = require('wp'), $ = require('jquery'); /** * Generic shortcode MCE view constructor. * * A Backbone-like View constructor intended for use when rendering a TinyMCE View. * The main difference is that the TinyMCE View is not tied to a particular DOM node. * This is cloned and used by each shortcode when registering a view. * */ var shortcodeViewConstructor = { /** * Initialize a shortcode preview View. * * Fetches the preview by making a delayed Ajax call, and renders if a preview can be fetched. * * @constructor * @this {Shortcode} Model registered with sui.shortcodes * @param {Object} options Options */ initialize: function( options ) { var self = this; this.shortcodeModel = this.getShortcodeModel( this.shortcode ); this.fetching = this.delayedFetch(); this.fetching.done( function( queryResponse ) { var response = queryResponse.response; if ( '' === response ) { var span = $('<span />').addClass('shortcake-notice shortcake-empty').text( self.shortcodeModel.formatShortcode() ); var wrapper = $('<div />').html( span ); self.content = wrapper.html(); } else { self.content = response; } }).fail( function() { var span = $('<span />').addClass('shortcake-error').text( shortcodeUIData.strings.mce_view_error ); var wrapper = $('<div />').html( span ); self.content = wrapper.html(); } ).always( function() { delete self.fetching; self.render( null, true ); } ); }, /** * Get the shortcode model given the view shortcode options. * * If the shortcode found in the view is registered with Shortcake, this * will clone the shortcode's Model and assign appropriate attribute * values. * * @this {Shortcode} * @param {Object} options Options formatted as wp.shortcode. */ getShortcodeModel: function( options ) { var shortcodeModel; shortcodeModel = sui.shortcodes.findWhere( { shortcode_tag: options.tag } ); if ( ! shortcodeModel ) { return; } currentShortcode = shortcodeModel.clone(); var attributes_backup = {}; var attributes = options.attrs; for ( var key in attributes.named ) { if ( ! attributes.named.hasOwnProperty( key ) ) { continue; } value = attributes.named[ key ]; attr = currentShortcode.get( 'attrs' ).findWhere({ attr: key }); // Reverse the effects of wpautop: https://core.trac.wordpress.org/ticket/34329 value = this.unAutoP( value ); if ( attr && attr.get('encode') ) { value = decodeURIComponent( value ); value = value.replace( /%/g, "%" ); } if ( attr ) { attr.set( 'value', value ); } else { attributes_backup[ key ] = value; } } currentShortcode.set( 'attributes_backup', attributes_backup ); if ( options.content ) { var inner_content = currentShortcode.get( 'inner_content' ); // Reverse the effects of wpautop: https://core.trac.wordpress.org/ticket/34329 options.content = this.unAutoP( options.content ); if ( inner_content ) { inner_content.set( 'value', options.content ); } else { currentShortcode.set( 'inner_content_backup', options.content ); } } return currentShortcode; }, /** * Queue a request with Fetcher class, and return a promise. * * @return {Promise} */ delayedFetch: function() { return fetcher.queueToFetch({ post_id: $( '#post_ID' ).val(), shortcode: this.shortcodeModel.formatShortcode(), nonce: shortcodeUIData.nonces.preview, }); }, /** * Fetch a preview of a single shortcode. * * Async. Sets this.content and calls this.render. * * @return undefined */ fetch: function() { var self = this; if ( ! this.fetching ) { this.fetching = true; wp.ajax.post( 'do_shortcode', { post_id: $( '#post_ID' ).val(), shortcode: this.shortcodeModel.formatShortcode(), nonce: shortcodeUIData.nonces.preview, }).done( function( response ) { if ( '' === response ) { self.content = '<span class="shortcake-notice shortcake-empty">' + self.shortcodeModel.formatShortcode() + '</span>'; } else { self.content = response; } }).fail( function() { self.content = '<span class="shortcake-error">' + shortcodeUIData.strings.mce_view_error + '</span>'; } ).always( function() { delete self.fetching; self.render( null, true ); } ); } }, /** * Get the shortcode model and open modal UI for editing. * * @param {string} shortcodeString String representation of the shortcode */ edit: function( shortcodeString, update ) { var currentShortcode = this.parseShortcodeString( shortcodeString ); if ( currentShortcode ) { var frame = wp.media.editor.get( window.wpActiveEditor ); if ( frame ) { frame.mediaController.setActionUpdate( currentShortcode ); frame.mediaController.props.set( 'editor', wpActiveEditor ); frame.open(); } else { frame = wp.media.editor.open( window.wpActiveEditor, { frame : "post", state : 'shortcode-ui', currentShortcode : currentShortcode, editor : wpActiveEditor, }); } frame.mediaController.props.set( 'insertCallback', function( shortcode ) { update( shortcode.formatShortcode() ); } ); // Make sure to reset state when closed. frame.once( 'close submit', function() { frame.mediaController.reset(); } ); } }, /** * Parse a shortcode string and return shortcode model. * Must be a shortcode which has UI registered with Shortcake - see * `window.sui.shortcodes`. * * @todo - I think there must be a cleaner way to get the * shortcode & args here that doesn't use regex. * * @param {string} shortcodeString * @return Shortcode */ parseShortcodeString: function( shortcodeString ) { var model, attr; var shortcode_tags = _.map( sui.shortcodes.pluck( 'shortcode_tag' ), this.pregQuote ).join( '|' ); var regexp = wp.shortcode.regexp( shortcode_tags ); regexp.lastIndex = 0; var matches = regexp.exec( shortcodeString ); if ( ! matches ) { return; } defaultShortcode = sui.shortcodes.findWhere({ shortcode_tag : matches[2] }); if ( ! defaultShortcode ) { return; } var shortcode = wp.shortcode.fromMatch( matches ); return this.getShortcodeModel( shortcode ); }, /** * Strip 'p' and 'br' tags, replace with line breaks. * * Reverses the effect of the WP editor autop functionality. * * @param {string} content Content with `<p>` and `<br>` tags inserted * @return {string} */ unAutoP: function( content ) { if ( switchEditors && switchEditors.pre_wpautop ) { content = switchEditors.pre_wpautop( content ); } return content; }, /** * Escape any special characters in a string to be used as a regular expression. * * JS version of PHP's preg_quote() * * @see http://phpjs.org/functions/preg_quote/ * * @param {string} str String to parse * @param {string} delimiter Delimiter character to be also escaped - not used here * @return {string} */ pregQuote: function( str, delimiter ) { return String(str) .replace( new RegExp( '[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + ( delimiter || '' ) + '-]', 'g' ), '\\$&' ); }, }; module.exports = sui.utils.shortcodeViewConstructor = shortcodeViewConstructor;
💾 Save Changes
❌ Cancel