Skip to content

Instantly share code, notes, and snippets.

@kmgdevelopment
Last active September 9, 2015 23:42
Show Gist options
  • Save kmgdevelopment/c24d560f13178c70169c to your computer and use it in GitHub Desktop.
Save kmgdevelopment/c24d560f13178c70169c to your computer and use it in GitHub Desktop.
Wygwam CKEditor Code Snippet Extension
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Wygwam_codesnippet_ext
{
var $name = 'Wygwam Code Snippet';
var $version = '1.0';
var $description = 'Wygwam integration of the Code Snippet CKEditor plugin.';
var $docs_url = '';
var $settings_exist = 'n';
private $_hooks = array(
'wygwam_config',
'wygwam_tb_groups'
);
private static $_included_resources = FALSE;
private $_plugin_name = 'codesnippet';
public function activate_extension()
{
foreach ($this->_hooks as $hook)
{
ee()->db->insert('extensions', array(
'class' => get_class($this),
'method' => $hook,
'hook' => $hook,
'settings' => '',
'priority' => 10,
'version' => $this->version,
'enabled' => 'y'
));
}
}
public function update_extension($current = NULL)
{
return FALSE;
}
public function disable_extension()
{
ee()->db->where('class', get_class($this))->delete('extensions');
}
public function wygwam_config($config, $settings)
{
global $_plugin_name;
if (($last_call = ee()->extensions->last_call) !== FALSE)
{
$config = $last_call;
}
// Add our plugin to CKEditor
if (!empty($config['extraPlugins']))
{
$config['extraPlugins'] .= ',';
}
$config['extraPlugins'] .= $this->_plugin_name;
$this->_include_resources();
// Check if our toolbar button has been added
$include_btn = FALSE;
foreach ($config['toolbar'] as $tbgroup)
{
if (in_array($this->_plugin_name, $tbgroup))
{
$include_btn = TRUE;
break;
}
}
if ($include_btn)
{
// Add our plugin to CKEditor
if (!empty($config['extraPlugins']))
{
$config['extraPlugins'] .= ',';
}
$config['extraPlugins'] .= $this->_plugin_name;
$this->_include_resources();
}
return $config;
}
private function _include_resources()
{
global $_plugin_name;
// Is this the first time we've been called?
if (!self::$_included_resources)
{
// Tell CKEditor where to find our plugin
$plugin_url = URL_THIRD_THEMES.'wygwam_' . $this->_plugin_name . '/';
ee()->cp->add_to_foot('<script type="text/javascript">CKEDITOR.plugins.addExternal("' . $this->_plugin_name . '", "'.$plugin_url.'");</script>');
// Don't do that again
self::$_included_resources = TRUE;
}
}
public function wygwam_tb_groups($tb_groups)
{
global $_plugin_name;
if (($last_call = ee()->extensions->last_call) !== FALSE)
{
$tb_groups = $last_call;
}
$tb_groups[] = array($this->_plugin_name);
// Is this the toolbar editor?
if (ee()->input->get('M') == 'show_module_cp')
{
// Give our toolbar button an icon
$icon_url = URL_THIRD_THEMES . 'wygwam_' . $this->_plugin_name . '/icons/' . $this->_plugin_name . '.png';
ee()->cp->add_to_head('<style type="text/css">.cke_button__' . $this->_plugin_name . '_icon { background-image: url(' . $icon_url . '); }</style>');
}
return $tb_groups;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment