Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
295 lines (257 sloc) 11.9 KB
<?php
/*
Plugin Name: Good Old Email Templates
Plugin URI: http://github.com/mptre/wp-goodold-email-templates
Description: Plugin for managing email templates.
Author: Joel Arvidsson
Version: 2.0
Author URI: http://goodold.se
Contributors: qvister
*/
class Goodold_email_templates {
const ID = 'goodold_email_templates';
const VERSION = '2.0';
private $_action;
private $_dir;
private $_key;
private $_options;
function __construct() {
$this->_action = (isset($_GET['action'])) ? $_GET['action'] : NULL;
$this->_dir = basename(dirname(__FILE__));
$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(ABSPATH, '', dirname(__FILE__))
);
wp_register_style(
self::ID,
plugins_url($this->_dir) . '/goodold-email-templates.css',
NULL,
self::VERSION
);
wp_register_script(
self::ID,
plugins_url($this->_dir) . '/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($this->_key, TRUE);
$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($this->_key, TRUE);
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() {
$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, $index = FALSE) {
$i = 0;
foreach ($this->_options as $template) {
if ($template['key'] == $key) {
return ($index === FALSE) ? $template : $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 = new Goodold_email_templates();
$goodold_email_templates->init();
?>
Jump to Line
Something went wrong with that request. Please try again.