Classic editor support

This commit is contained in:
Michael Thomas 2022-01-22 22:17:27 -05:00
parent b986b11441
commit fc2f9df23c
5 changed files with 103 additions and 123 deletions

View File

@ -49,7 +49,7 @@ if ( !class_exists('CognitoFormsPlugin') ) {
add_action($action, array($this, $action)); add_action($action, array($this, $action));
} }
// Registers tinyMCE filters // Registers plug-in filters
private function addFilters($filters) { private function addFilters($filters) {
foreach($filters as $filter) foreach($filters as $filter)
add_filter($filter, array($this, $filter)); add_filter($filter, array($this, $filter));
@ -60,6 +60,11 @@ if ( !class_exists('CognitoFormsPlugin') ) {
$this->addActions(self::$actions); $this->addActions(self::$actions);
} }
public function init() {
// Initialize Gutenberg Block
$this->block_init();
}
// Initialize plug-in // Initialize plug-in
public function admin_init() { public function admin_init() {
if(!current_user_can('edit_posts') && !current_user_can('edit_pages')) return; if(!current_user_can('edit_posts') && !current_user_can('edit_pages')) return;
@ -77,17 +82,8 @@ if ( !class_exists('CognitoFormsPlugin') ) {
delete_option('cognito_organization'); delete_option('cognito_organization');
} }
// Add tinyMCE plug-in // Initialize TinyMCE Plugin
if(get_user_option('rich_editing') == 'true') { $this->tinymce_init();
$this->addfilters(array(
'mce_buttons',
'mce_external_plugins'
));
}
}
public function init() {
$this->block_init();
} }
// Initialize block // Initialize block
@ -131,16 +127,26 @@ if ( !class_exists('CognitoFormsPlugin') ) {
) ); ) );
} }
// Set up tinyMCE buttons // Initialize classic editor (TinyMCE)
public function tinymce_init() {
if(get_user_option('rich_editing') == 'true') {
$this->addFilters(array(
'mce_buttons',
'mce_external_plugins'
));
}
}
// Set up TinyMCE buttons
public function mce_buttons($buttons) { public function mce_buttons($buttons) {
array_push($buttons, '|', 'cognito'); array_push($buttons, '|', 'cognito');
return $buttons; return $buttons;
} }
// Initialize tinyMCE plug-in // Initialize TinyMCE plug-in
public function mce_external_plugins($plugins) { public function mce_external_plugins( $plugin_array ) {
$plugins['cognito'] = plugin_dir_url( __FILE__ ) . 'tinymce/plugin.js'; $plugin_array['cognito_mce_plugin'] = plugins_url( '/tinymce/plugin.js', __FILE__ );
return $plugins; return $plugin_array;
} }
// Initialize administration menu (left-bar) // Initialize administration menu (left-bar)
@ -162,5 +168,6 @@ if ( !class_exists('CognitoFormsPlugin') ) {
include 'templates/options.php'; include 'templates/options.php';
} }
} }
new CognitoFormsPlugin; new CognitoFormsPlugin;
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

BIN
tinymce/cogicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

115
tinymce/dialog.php Executable file → Normal file
View File

@ -16,88 +16,53 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="en">
<head> <head>
<style> <meta charset="UTF-8">
body { overflow: hidden; } <meta http-equiv="X-UA-Compatible" content="IE=edge">
button{margin-top:20px;color: #fff !important;text-shadow: 0 1px 1px rgba(0,0,0,.3);outline: none;cursor: pointer;text-align: center;text-decoration: none;font: 14px/100% 'Open Sans Condensed', sans-serif !important;font-weight: 700 !important;padding: 5px 15px 6px;border: solid 1px #95ba14;background: #aed136;} <meta name="viewport" content="width=device-width, initial-scale=1.0">
.clearlooks2 .mceTop .mceLeft, .clearlooks2 .mceTop .mceRight{background:#00B4AC;padding:20px;font-family: 'Open Sans Condensed', Arial, Helvetica, sans-serif;font-size: 18px;font-weight: 700;color: #fff;}
body{padding: 10px 20px!important;font-family: 'Open Sans', Arial, Helvetica, sans-serif;font-size: 14px;font-weight: 400;}
h3 {margin-bottom:20px!important;color:#444!important;}
</style>
<script src="../../../../wp-includes/js/jquery/jquery.js"></script>
<script src="../../../../wp-includes/js/tinymce/tiny_mce_popup.js"></script>
<script src="../../../../wp-includes/js/tinymce/utils/mctabs.js"></script>
<script src="../../../../wp-includes/js/tinymce/utils/form_utils.js"></script>
<title>Cognito Forms</title> <title>Cognito Forms</title>
<script> <style>
var titleElements = tinyMCEPopup.getWin().document.querySelectorAll('.mceTop .mceLeft, .mceTop .mceRight, .mce-title'); html, body {
for (var i = 0; i < titleElements.length; i++) { margin: 0;
titleElements[i].style.background = '#00B4AC'; padding: 0;
titleElements[i].style.color = '#fff'; height: 100%;
} }
#cognito-frame {
var closeElement = tinyMCEPopup.getWin().document.querySelector('.mceClose, .mce-close'); width: 100%;
if (closeElement) closeElement.style.color = '#fff'; height: 100%;
border: none;
function cognito_submit() {
var formSelect = document.getElementById('formSelect');
var shortcode = '[CognitoForms id="' + formSelect.value + '"]';
if (window.tinyMCE) {
if (window.tinyMCE.execInstanceCommand) {
window.tinyMCE.execInstanceCommand('content', 'mceInsertContent', false, shortcode);
tinyMCEPopup.editor.execCommand('mceRepaint');
} }
else if (window.tinyMCE.focusedEditor) { </style>
window.tinyMCE.focusedEditor.insertContent(shortcode);
}
tinyMCEPopup.close();
}
return false;
}
jQuery(function() {
var data = {
action: "get_forms"
};
jQuery.post(tinyMCEPopup.params.ajax_url, data, function(response) {
if (response) {
var forms = JSON.parse(response);
var formSelect = jQuery("#formSelect");
jQuery.each(forms, function() {
formSelect.append(jQuery("<option></option>")
.attr("value", this.Id)
.text(this.Name));
});
jQuery("#form-list").show();
} else {
jQuery("#no-forms").show();
}
});
});
</script>
</head> </head>
<body> <body>
<div id="no-forms" style="display:none;"> <iframe id="cognito-frame" src="https://forms.cognito.test/integrations/cms"></iframe>
<h3>You are not logged into your Cognito Forms account.</h3>
<p>Please click on the "Cognito Forms" link in the menu on the left and log in to register this plug-in with your account.</p>
</div>
<div id="form-list"> <script type="text/javascript">
<h3>Embed a Form</h3> window.addEventListener('message', handleMessage);
<form method="post" action="">
<label for="formSelect">Choose a form</label> function handleMessage(event) {
<select id="formSelect"> var frame = document.getElementById('cognito-frame');
</select><br/> var baseUrl = new URL(frame.getAttribute('src')).origin;
<button id="cognito-insert-form" type="button" onclick="cognito_submit();">Insert Form</button>
</form> if (event.origin === baseUrl) {
</div> console.log('Message received!');
sendData(event.data);
}
}
function sendData(data) {
embedCode = data.embedCodes.Seamless;
console.log(embedCode);
if (top.tinyMCE && top.tinyMCE.activeEditor) {
top.tinyMCE.activeEditor.insertContent(embedCode);
}
top.tinymce.activeEditor.windowManager.close();
}
</script>
</body> </body>
</html> </html>

View File

@ -1,24 +1,32 @@
( function() { ( function() {
tinymce.create('tinymce.plugins.cognito', { tinymce.create( 'tinymce.plugins.cognito_mce_plugin', {
init : function(ed, url) { getInfo: function() {
ed.addCommand('cognito_embed_window', function() { return {
ed.windowManager.open({ longname: 'Cognito Forms',
file : url + '/dialog.php', author: 'cognitoapps',
width : 400, authorurl: 'https://cognitoforms.com',
height : 160, infourl: 'https://cognitoforms.com',
inline: 1 version: "2.0",
}, { plugin_url : url, ajax_url: ajaxurl }); };
},
init: function( editor, url ) {
editor.addCommand( 'cognito_embed_window', function() {
editor.windowManager.open( {
title: 'Cognito Forms',
url: url + '/dialog.php',
width: 500,
height: 500,
} );
} ); } );
ed.addButton('cognito', { editor.addButton( 'cognito', {
title: 'Cognito Forms', title: 'Cognito Forms',
cmd: 'cognito_embed_window', cmd: 'cognito_embed_window',
image : url + '/cogicon.ico' image: url + '/cogicon.png',
} ); } );
} },
} ); } );
tinymce.PluginManager.add('cognito', tinymce.plugins.cognito); tinymce.PluginManager.add( 'cognito_mce_plugin', tinymce.plugins.cognito_mce_plugin );
} )();
})()