Permalink
Browse files

Total rewrite.

  • Loading branch information...
Anton Lindqvist
Anton Lindqvist committed Dec 12, 2009
1 parent 7bb52ce commit a4ae5433ca299de6ce0825caa1706026295bf110
Showing with 341 additions and 2 deletions.
  1. +28 −0 goodold-email-templates.css
  2. +49 −0 goodold-email-templates.js
  3. +264 −2 goodold-email-templates.php
  4. 0 language/.gitignore
@@ -0,0 +1,28 @@
+#goodold_email_templates form.edit label {
+display: block;
+}
+#goodold_email_templates form.edit input.text {
+width: 99%;
+}
+#goodold_email_templates form.edit input.submit {
+margin: 0 5px 0 0;
+float: right;
+}
+#goodold_email_templates form.edit textarea {
+width: 99%;
+}
+#goodold_email_templates ul.vars {
+width: 250px;
+}
+#goodold_email_templates ul.vars li {
+background: #21759b;
+height: 16px;
+margin: 0 5px 5px 0;
+padding: 0 10px;
+line-height: 16px;
+float: left;
+display: inline;
+color: #fff;
+-moz-border-radius: 11px;
+-webkit-border-radius: 11px;
+}
View
@@ -0,0 +1,49 @@
+jQuery(function ($) {
+ $('.keyup').keyup(function (e) {
+ var matches, type;
+ matches = $(this).val().match(/\{([a-z_]+)\}/g);
+ type = ($(this).hasClass('input')) ? 'input' : 'textarea';
+
+ setTimeout(function () {
+ var $ul, i, stop, value;
+ $ul = $('ul.vars');
+
+ try {
+ stop = matches.length;
+ } catch (e) {
+ stop = 0;
+ }
+
+ $ul.find('li.' + type).remove();
+
+ for (i = 0; i < stop; i += 1) {
+ value = matches[i].replace(/([\{\}])/g, '')
+
+ if ($ul.find('li.' + value).length === 0) {
+ $ul.append(
+ $('<li></li>').addClass(value + ' ' + type).text(value)
+ );
+ }
+ }
+ }, 100);
+ }).keyup();
+
+ $('form.edit').submit(function () {
+ var $li, i, stop;
+ $li = $(this).find('ul.vars li');
+ stop = $li.length;
+
+ for (i = 0; i < stop; i += 1) {
+ $(this).append(
+ $('<input />')
+ .attr({
+ type: 'hidden',
+ name: 'goodold_email_templates[vars][]'
+ })
+ .val($li.eq(i).text())
+ );
+ }
+
+ return true;
+ });
+});
View
@@ -14,28 +14,290 @@ class Goodold_email_templates {
const ID = 'goodold_email_templates';
const VERSION = '2.0';
+ private $_action;
+ private $_key;
private $_options;
function __construct() {
+ $this->_action = (isset($_GET['action'])) ? $_GET['action'] : NULL;
+ $this->_key = (isset($_GET['key'])) ? $_GET['key'] : NULL;
$this->_options = get_option(self::ID);
}
function init() {
if (is_admin()) {
+ // Install.
register_activation_hook(__FILE__, array(&$this, 'install'));
register_deactivation_hook(__FILE__, array(&$this, 'uninstall'));
+
+ // Actions.
+ add_action('admin_init', array(&$this, 'init_admin'));
+ add_action('admin_menu', array(&$this, 'page'));
+ }
+ }
+
+ function init_admin() {
+ load_plugin_textdomain(
+ self::ID,
+ str_replace(get_bloginfo('home') . '/', '', plugins_url('good-old-templates'))
+ );
+
+ wp_register_style(
+ self::ID,
+ plugins_url('goodold-email-templates') . '/goodold-email-templates.css',
+ NULL,
+ self::VERSION
+ );
+
+ wp_register_script(
+ self::ID,
+ plugins_url('goodold-email-templates') . '/goodold-email-templates.js',
+ array('jquery'),
+ self::VERSION,
+ TRUE
+ );
+ }
+
+ function get_template($key, $values) {
+ $return = $search = $replace = array();
+ $template = $this->_get_template($key);
+
+ if (isset($template)) {
+ foreach ($values as $var => $val) {
+ $search[] = '{' . $var . '}';
+ $replace[] = $val;
+ }
+
+ $return['subject'] = str_replace($search, $replace, stripslashes($template['subject']));
+ $return['body'] = str_replace($search, $replace, stripslashes($template['body']));
+
+ return $return;
}
+
+ return FALSE;
+ }
+
+ function form() {
+ global $current_user;
+ $post_data = (isset($_POST[self::ID])) ? $_POST[self::ID] : NULL;
+
+ if ($this->_action == 'add' && isset($post_data)) {
+ if (!empty($post_data['template'])) {
+ $this->_options[] = array(
+ 'name' => $post_data['template'],
+ 'key' => str_replace('-', '_', sanitize_title($post_data['template'])),
+ 'author' => (int)$current_user->ID,
+ 'subject' => '',
+ 'body' => '',
+ 'vars' => array(),
+ 'timestamp' => time()
+ );
+
+ if (update_option(self::ID, $this->_options)) {
+ $success = __('Template added succesfully.', self::ID);
+ }
+ }
+ } elseif ($this->_action == 'edit' && isset($this->_key)) {
+ if (isset($post_data)) {
+ $i = $this->_get_template_index($this->_key);
+
+ $this->_options[$i]['subject'] = $post_data['subject'];
+ $this->_options[$i]['body'] = $post_data['body'];
+ $this->_options[$i]['vars'] = $post_data['vars'];
+
+ if (update_option(self::ID, $this->_options)) {
+ $success = __('Template edited succesfully.', self::ID);
+ }
+ }
+
+ $template = $this->_get_template($this->_key);
+ } elseif ($this->_action == 'delete') {
+ $i = $this->_get_template_index($this->_key);
+
+ if (isset($this->_options[$i])) {
+ unset($this->_options[$i]);
+
+ if (update_option(self::ID, $this->_options)) {
+ $success = __('Template deleted succesfully.', self::ID);
+ }
+ }
+ }
+?>
+<div class="wrap" id="<?php echo self::ID; ?>">
+ <h2><?php _e('Email Templates', self::ID); ?></h2>
+ <?php if (isset($success)): ?>
+ <div id="message" class="updated fade">
+ <p><?php echo $success; ?></p>
+ </div>
+ <?php endif; ?>
+ <div id="col-container">
+ <?php if ($this->_action == 'edit'): ?>
+ <form action="<?php echo $this->_url(array('action' => 'edit', 'key' => $template['key'])); ?>" method="post" class="edit">
+ <div id="col-right">
+ <p><?php _e('Variables', self::ID); ?></p>
+ <ul class="vars"></ul>
+ </div>
+ <div id="col-left">
+ <p>
+ <label for="<?php echo self::ID; ?>-subject"><?php _e('Subject', self::ID); ?></label>
+ <input class="text input keyup" type="text" name="<?php echo self::ID; ?>[subject]" id="<?php echo self::ID; ?>-subject" value="<?php echo stripslashes($template['subject']); ?>" />
+ </p>
+ <p>
+ <label for="<?php echo self::ID; ?>-body"><?php _e('Body', self::ID); ?></label>
+ <textarea class="textarea keyup" name="<?php echo self::ID; ?>[body]" rows="8" cols="40"><?php echo stripslashes($template['body']); ?></textarea>
+ </p>
+ <p>
+ <input class="button-primary submit" type="submit" name="<?php echo self::ID; ?>[submit]" value="<?php _e('Edit template', self::ID); ?>" />
+ </p>
+ </div>
+ </form>
+ <?php else: ?>
+ <div id="col-right">
+ <table class="widefat fixed" cellspacing="0">
+ <thead>
+ <tr>
+ <th id="template" class="manage-column" scope="col"><?php _e('Template', self::ID); ?></th>
+ <th id="key" class="manage-column" scope="col"><?php _e('Key', self::ID); ?></th>
+ <th id="author" class="manage-column" scope="col"><?php _e('Author', self::ID); ?></th>
+ <th id="date" class="manage-column" scope="col"><?php _e('Date', self::ID); ?></th>
+ </tr>
+ </thead>
+ <tfoot>
+ <tr>
+ <th id="template" class="manage-column" scope="col"><?php _e('Template', self::ID); ?></th>
+ <th id="key" class="manage-column" scope="col"><?php _e('Key', self::ID); ?></th>
+ <th id="author" class="manage-column" scope="col"><?php _e('Author', self::ID); ?></th>
+ <th id="date" class="manage-column" scope="col"><?php _e('Date', self::ID); ?></th>
+ </tr>
+ </tfoot>
+ <tbody>
+ <?php $i = 0; ?>
+ <?php foreach ($this->_options as $template): ?>
+ <?php $author = get_userdata($template['author']); ?>
+ <?php $class = ($i % 2) ? NULL : 'class="alternate" '; ?>
+ <tr <?php echo $class; ?>scope="row">
+ <td>
+ <strong>
+ <a class="row-title" href="<?php echo $this->_url(array('action' => 'edit', 'key' => $template['key'])); ?>"><?php echo $template['name']; ?></a>
+ </strong>
+ <div class="row-actions">
+ <span class="edit"><a href="<?php echo $this->_url(array('action' => 'edit', 'key' => $template['key'])); ?>"><?php _e('Edit', self::ID); ?></a> | </span>
+ <span class="delete"><a href="<?php echo $this->_url(array('action' => 'delete', 'key' => $template['key'])); ?>"><?php _e('Delete', self::ID); ?></span>
+ </div>
+ </td>
+ <td><?php echo $template['key']; ?></td>
+ <td><?php echo $author->display_name; ?></td>
+ <td><?php echo strftime('%Y-%m-%d %H:%M', $template['timestamp']); ?></td>
+ </tr>
+ <?php $i += 1; ?>
+ <?php endforeach; ?>
+ </tbody>
+ </table>
+ <div class="tablenav">
+ <div class="alignleft">
+ <form action="<?php echo $this->_url(array('action' => 'add')); ?>" method="post">
+ <input type="text" name="<?php echo self::ID; ?>[template]" />
+ <input class="button-primary" type="submit" name="<?php echo self::ID; ?>[submit]" value="<?php _e('Add template', self::ID); ?>" />
+ </form>
+ </div>
+ </div>
+ </div>
+ <div id="col-left">
+ <?php _e('<p>This is a plugin made for managing email templates. The first version was developed and maintained by Joel Arvidsson from <a href="http://goodold.se">Good Old</a>. The current version is developed by Anton Lindqvist from <a href="http://qvister.se">qvister</a>.</p><p>Start creating custom email templates by using the form to the right. Or view to example template just to get the hang of it.</p><p>Each template has a unique key used for retreiving the actual template in your code. There\'s a public method available called goodold_email_templates_get which takes two parameters. See example below.</p>', self::ID); ?></p>
+ <p>
+<pre><?php echo '&lt;?php
+$vars = array(
+ \'firstname\' => \'Foo\',
+ \'lastname\' => \'Bar\',
+ \'email\' => \'foo@bar.com\'
+);
+
+goodold_email_templates_get(\'template_key\', $vars);
+?&gt;' ?></pre>
+ </p>
+ </div>
+ <?php endif; ?>
+ </div>
+</div>
+<?php
}
function install() {
- add_option(self::ID, array(), 'no');
+ $option = array(
+ array(
+ 'name' => 'Example',
+ 'key' => 'example',
+ 'author' => 1,
+ 'subject' => 'Hello {firstname}!',
+ 'body' => "Welcome to {blogname}!\nYour account has been successfully created. Use the credentials below in order to login.\n\nUsername: {username}\nPassword: {password}",
+ 'vars' => array('firstname', 'blogname', 'username', 'password'),
+ 'timestamp' => time()
+ )
+ );
+ add_option(self::ID, $option, '', 'no');
}
function uninstall() {
delete_option(self::ID);
}
+ function page() {
+ $page = add_management_page(
+ __('Email Templates', self::ID),
+ __('Email Templates', self::ID),
+ 'manage_options',
+ str_replace('.php', '', basename(__FILE__)),
+ array(&$this, 'form')
+ );
+
+ add_action('admin_print_styles-' . $page, array(&$this, 'stylesheet'));
+ add_action('admin_print_scripts-' . $page, array(&$this, 'stylesheet'));
+ }
+
+ function stylesheet() {
+ wp_enqueue_style(self::ID);
+ wp_enqueue_script(self::ID);
+ }
+
+ private function _get_template($key) {
+ foreach ($this->_options as $template) {
+ if ($template['key'] == $key) {
+ return $template;
+ }
+ }
+
+ return NULL;
+ }
+
+ private function _get_template_index($key) {
+ $i = 0;
+
+ foreach ($this->_options as $template) {
+ if ($template['key'] == $key) {
+ return $i;
+ }
+
+ $i += 1;
+ }
+
+ return NULL;
+ }
+
+ private function _url($params) {
+ $defaults = array('page' => str_replace('.php', '', basename(__FILE__)));
+ $params = http_build_query(array_merge($defaults, $params), '', '&amp;');
+
+ return get_bloginfo('home') . '/wp-admin/tools.php?' . $params;
+ }
+
+}
+
+function goodold_email_templates_get($key, $values) {
+ global $goodold_email_templates;
+
+ return $goodold_email_templates->get_template($key, $values);
}
-Goodold_email_templates::init();
+$goodold_email_templates = new Goodold_email_templates();
+$goodold_email_templates->init();
?>
View
No changes.

0 comments on commit a4ae543

Please sign in to comment.