`);
$advanced.find('select').html(app.$select.find('option').clone());
if ($advanced.find('.om-monsterlink-upgrade').length) {
const $clone = $('#om-monsterlink-upgrade').clone();
$advanced.find('.om-monsterlink-upgrade span').html($clone.html());
}
$('#link-options').append($advanced);
app.$linkSelect = $('#om-link-campaign');
// Monkey-patch the wpLink.getAttrs method to handle monster-link target/rel attributes.
if (typeof window.wpLink !== 'undefined') {
const orig = wpLink.getAttrs;
wpLink.getAttrs = function () {
const attrs = orig();
const ml = getMonsterlink(app.$linkSelect.val());
if (attrs.href === ml) {
attrs.target = '_blank';
attrs.rel = 'noopener noreferrer';
}
return attrs;
};
}
};
/**
* Handles modifying the wplink modals to inject monsterlink options.
*
* @since 2.3.0
*
* @param {Object} editor The editor object.
*
* @returns {void}
*/
app.initEditorMods = function (editor) {
if (!editor || editor.hasInitiatedOm) {
return;
}
editor.hasInitiatedOm = true;
editor.on('ExecCommand', function (e) {
if ('WP_Link' === e.command) {
app.initLinkButton();
}
});
if (!app.$linkSelect) {
app.initAdvancedSettings();
}
};
/**
* Setup our event listeners.
*
* @since 2.3.0
*
* @returns {void}
*/
app.setupListeners = function () {
$(document)
// Open inline modal when media button is clicked
.on('click', '.optin-monster-insert-campaign-button', function (event) {
event.preventDefault();
app.modalOpenInline();
})
// Open link modal when monsterlink button is clicked
.on('click', '.optin-monster-insert-monsterlink', function (event) {
event.preventDefault();
app.modalOpenLink();
})
// Close modal on close or cancel links or background click.
.on(
'click',
'#optin-monster-modal-backdrop, #optin-monster-modal-close, #optin-monster-modal-cancel a',
function (event) {
event.preventDefault();
app.modalClose();
}
)
// When submitting the inline campaign selection,
// Insert the shortcode, and close the modal.
.on('click', '#optin-monster-modal-submit-inline', function (event) {
event.preventDefault();
app.insertShortcode();
app.modalClose();
})
// When submitting the link modal selection,
// Insert the link, and close the modal.
.on('click', '#optin-monster-modal-submit', function (event) {
event.preventDefault();
app.mceLinkifyText();
app.modalClose();
})
// When changing our campaigns select in the wplink modal,
// update the link url/target values as well.
.on('change', '#om-link-campaign', function () {
const id = app.$linkSelect.val();
if (id) {
$('#wp-link-url').val(getMonsterlink(id));
$('#wp-link-target').prop('checked', true);
}
})
// When opening wplink modal, set "selected" option.
.on('wplink-open', function (wrap) {
app.updateLinkSelectOptions(app.$linkSelect);
})
// When closing wplink modal, close our modals too.
.on('wplink-close', function (wrap) {
app.modalClose();
})
// When closing our link modal, also close the wplink modal
.on('om-modal-close-monsterlink', function (wrap) {
if (wpLink) {
// If in tinymce mode, close the (hidden) wplink modal as well.
const editor = app.getActiveEditor();
if (editor && !editor.isHidden()) {
wpLink.close();
}
}
});
};
/**
* Kicks things off when the DOM is ready.
*
* @since 2.3.0
*
* @returns {void}
*/
app.init = function () {
// Store cached nodes.
app.$body = $(document.body);
app.$modalWrap = $('#optin-monster-modal-wrap');
app.$toToggle = $('#optin-monster-modal-backdrop, #optin-monster-modal-wrap');
app.$select = $('#optin-monster-modal-select-campaign');
app.$inlineSelect = $('#optin-monster-modal-select-inline-campaign');
app.$linkSelect = null;
app.setupListeners();
// Init the editor mods if we have an active editor.
app.initEditorMods(app.getActiveEditor());
if (typeof tinymce !== 'undefined') {
// Also init the editor mods whenever a new editor
// is initiated (looking at you, Elementor).
tinymce.on('SetupEditor', function ({ editor }) {
app.initEditorMods(editor);
});
}
};
$(app.init);
})(window, document, jQuery, window.OMAPI_Editor);