Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit ac93a5055246f47e35478f39a3f8141090d28bcc @GeertDD committed
5 images/email-button-credits.txt
@@ -0,0 +1,5 @@
+E-mail icon by Yusuke Kamiyamane
+http://p.yusukekamiyamane.com/
+
+License: Creative Commons - Attribution 3.0 Unported (CC BY 3.0)
+http://creativecommons.org/licenses/by/3.0/
BIN images/email-button.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 javascript/tinymce-email-button.js
@@ -0,0 +1,41 @@
+;(function () {
+
+ tinymce.create('tinymce.plugins.email', {
+
+ init: function (editor, url) {
+
+ editor.addButton('email', {
+ cmd: 'email'
+ ,title: tinymce_email_button.title
+ ,image: tinymce_email_button.url + 'images/email-button.png'
+ });
+
+ editor.addCommand('email', function() {
+ // Grab the selected text
+ var linkText = editor.selection.getContent({ format:'text' });
+
+ // If an email address was selected, use that email as default input in the prompt
+ var defaultInput = (linkText.match(/^\S+@\S+\.\S+$/)) ? linkText : '';
+
+ // Ask for the email address to link to
+ var email = prompt(tinymce_email_button.prompt, defaultInput);
+
+ // If nothing was entered, don't create a link
+ if ( ! email) return;
+
+ // If no text was selected, use the email address as text for the link
+ if (linkText === '') {
+ linkText = email;
+ }
+
+ // Create and insert the email link
+ var newText = '<a href="mailto:' + email + '">' + linkText + '</a>';
+ editor.execCommand('mceInsertContent', false, newText);
+ });
+ }
+
+ });
+
+ tinymce.PluginManager.add('email', tinymce.plugins.email);
+
+}());
100 tinymce-email-button.php
@@ -0,0 +1,100 @@
+<?php
+/*
+Plugin Name: TinyMCE Email Button
+Plugin URI: https://github.com/GeertDD/tinymce-email-button
+Description: Adds a button for creating email links to the TinyMCE editor
+Version: 1.0
+Author: Geert De Deckere
+Author URI: http://www.geertdedeckere.be/
+*/
+
+new TinyMCE_Email_Button;
+
+class TinyMCE_Email_Button {
+
+ /**
+ * Constructor
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ add_action('init', array($this, 'init'));
+ }
+
+ /**
+ * Initialize the plugin
+ *
+ * @return void
+ */
+ public function init()
+ {
+ // This plugin only applies to Wysiwyg areas in the admin
+ if ( ! is_admin() || get_user_option('rich_editing') !== 'true')
+ return;
+
+ // Setup internationalisation first
+ $this->i18n();
+
+ // Note: the TinyMCE Advanced plugin also uses these hooks and uses priority 999.
+ // We need a higher priority in order for the button to be added succesfully.
+ // See: http://wordpress.org/extend/plugins/tinymce-advanced/
+ add_filter('mce_buttons', array($this, 'mce_buttons'), 1000);
+ add_filter('mce_external_plugins', array($this, 'mce_external_plugins'), 1000);
+ }
+
+ /**
+ * Internationalization setup
+ *
+ * @return void
+ */
+ public function i18n()
+ {
+ // TODO: load language files
+
+ // Localization for the tinymce-email-button.js file
+ wp_localize_script('editor', 'tinymce_email_button', array(
+ 'url' => plugin_dir_url(__FILE__),
+ 'title' => __('Insert e-mail link', 'tinymce-email-button'),
+ 'prompt' => __('Enter the email address to link to:', 'tinymce-email-button'),
+ ));
+ }
+
+ /**
+ * Add a mailto button to the first row of the TinyMCE editor
+ *
+ * @param array $buttons ordered button list
+ * @return array $buttons
+ */
+ public function mce_buttons($buttons)
+ {
+ // Look for the regular link button
+ if (FALSE === ($key = array_search('link', $buttons)))
+ {
+ // Append the button to the end of the row if no link button was found
+ $buttons[] = 'email';
+ }
+ else
+ {
+ // Insert the email button before the link button
+ $before = array_slice($buttons, 0, $key);
+ $after = array_slice($buttons, $key);
+ $buttons = array_merge($before, array('email'), $after);
+ }
+
+ return $buttons;
+ }
+
+ /**
+ * Register the JavaScript file for the mailto button
+ *
+ * @param array $plugins TinyMCE plugin files
+ * @return array $plugins
+ */
+ public function mce_external_plugins($plugins)
+ {
+ $plugins['email'] = plugin_dir_url(__FILE__).'javascript/tinymce-email-button.js';
+ return $plugins;
+ }
+
+}

0 comments on commit ac93a50

Please sign in to comment.
Something went wrong with that request. Please try again.