262 lines
13 KiB
PHP
262 lines
13 KiB
PHP
<?php
|
||
/**
|
||
* Admin page for managing email templates
|
||
*/
|
||
if (!defined('ABSPATH')) exit;
|
||
|
||
// Handle form submissions
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
||
if ($_POST['action'] === 'add_template' && check_admin_referer('wheel_email_templates_nonce')) {
|
||
global $wpdb;
|
||
$table_name = $wpdb->prefix . 'wheel_email_templates';
|
||
|
||
$name = isset($_POST['template_name']) ? sanitize_text_field($_POST['template_name']) : '';
|
||
$subject = isset($_POST['template_subject']) ? sanitize_text_field($_POST['template_subject']) : '';
|
||
$template_body = isset($_POST['template_body']) ? wp_kses_post($_POST['template_body']) : '';
|
||
|
||
if (!empty($name) && !empty($subject) && !empty($template_body)) {
|
||
$result = $wpdb->insert(
|
||
$table_name,
|
||
[
|
||
'name' => $name,
|
||
'subject' => $subject,
|
||
'template_body' => $template_body,
|
||
],
|
||
['%s', '%s', '%s']
|
||
);
|
||
|
||
if ($result !== false) {
|
||
echo '<div class="notice notice-success is-dismissible"><p>' . __('Email template added successfully!', 'wheel-of-fortune') . '</p></div>';
|
||
} else {
|
||
echo '<div class="notice notice-error is-dismissible"><p>' . __('Error adding email template. Please try again.', 'wheel-of-fortune') . '</p></div>';
|
||
}
|
||
} else {
|
||
echo '<div class="notice notice-error is-dismissible"><p>' . __('Please fill in all required fields.', 'wheel-of-fortune') . '</p></div>';
|
||
}
|
||
} elseif ($_POST['action'] === 'edit_template' && check_admin_referer('wheel_email_templates_nonce')) {
|
||
global $wpdb;
|
||
$table_name = $wpdb->prefix . 'wheel_email_templates';
|
||
|
||
$template_id = isset($_POST['template_id']) ? intval($_POST['template_id']) : 0;
|
||
$name = isset($_POST['template_name']) ? sanitize_text_field($_POST['template_name']) : '';
|
||
$subject = isset($_POST['template_subject']) ? sanitize_text_field($_POST['template_subject']) : '';
|
||
$template_body = isset($_POST['template_body']) ? wp_kses_post($_POST['template_body']) : '';
|
||
|
||
if (!empty($name) && !empty($subject) && !empty($template_body) && $template_id > 0) {
|
||
$result = $wpdb->update(
|
||
$table_name,
|
||
[
|
||
'name' => $name,
|
||
'subject' => $subject,
|
||
'template_body' => $template_body,
|
||
],
|
||
['id' => $template_id],
|
||
['%s', '%s', '%s'],
|
||
['%d']
|
||
);
|
||
|
||
if ($result !== false) {
|
||
echo '<div class="notice notice-success is-dismissible"><p>' . __('Email template updated successfully!', 'wheel-of-fortune') . '</p></div>';
|
||
} else {
|
||
echo '<div class="notice notice-error is-dismissible"><p>' . __('Error updating email template. Please try again.', 'wheel-of-fortune') . '</p></div>';
|
||
}
|
||
} else {
|
||
echo '<div class="notice notice-error is-dismissible"><p>' . __('Please fill in all required fields.', 'wheel-of-fortune') . '</p></div>';
|
||
}
|
||
} elseif ($_POST['action'] === 'delete_template' && check_admin_referer('wheel_email_templates_nonce')) {
|
||
global $wpdb;
|
||
$table_name = $wpdb->prefix . 'wheel_email_templates';
|
||
|
||
$template_id = isset($_POST['template_id']) ? intval($_POST['template_id']) : 0;
|
||
|
||
if ($template_id > 0) {
|
||
$result = $wpdb->delete($table_name, ['id' => $template_id], ['%d']);
|
||
|
||
if ($result !== false) {
|
||
echo '<div class="notice notice-success is-dismissible"><p>' . __('Email template deleted successfully!', 'wheel-of-fortune') . '</p></div>';
|
||
} else {
|
||
echo '<div class="notice notice-error is-dismissible"><p>' . __('Error deleting email template. Please try again.', 'wheel-of-fortune') . '</p></div>';
|
||
}
|
||
} else {
|
||
echo '<div class="notice notice-error is-dismissible"><p>' . __('Invalid template ID.', 'wheel-of-fortune') . '</p></div>';
|
||
}
|
||
}
|
||
}
|
||
|
||
// Get all email templates
|
||
global $wpdb;
|
||
$table_name = $wpdb->prefix . 'wheel_email_templates';
|
||
$templates = $wpdb->get_results("SELECT * FROM $table_name ORDER BY name ASC", ARRAY_A);
|
||
|
||
?>
|
||
|
||
<div class="wrap wheel-admin-page">
|
||
<h1><?php echo esc_html__('Email Templates', 'wheel-of-fortune'); ?></h1>
|
||
|
||
<div class="wheel-card">
|
||
<h2><?php echo esc_html__('All Email Templates', 'wheel-of-fortune'); ?></h2>
|
||
|
||
<p><button class="button button-primary add-new-template"><?php _e('Add New Template', 'wheel-of-fortune'); ?></button></p>
|
||
|
||
<table class="wheel-templates-table widefat fixed striped">
|
||
<thead>
|
||
<tr>
|
||
<th><?php _e('ID', 'wheel-of-fortune'); ?></th>
|
||
<th><?php _e('Name', 'wheel-of-fortune'); ?></th>
|
||
<th><?php _e('Subject', 'wheel-of-fortune'); ?></th>
|
||
<th><?php _e('Actions', 'wheel-of-fortune'); ?></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php if (empty($templates)): ?>
|
||
<tr><td colspan="4"><?php _e('No email templates found. Add your first template using the button above.', 'wheel-of-fortune'); ?></td></tr>
|
||
<?php else: ?>
|
||
<?php foreach ($templates as $template): ?>
|
||
<tr>
|
||
<td><?php echo esc_html($template['id']); ?></td>
|
||
<td><?php echo esc_html($template['name']); ?></td>
|
||
<td><?php echo esc_html($template['subject']); ?></td>
|
||
<td>
|
||
<button class="button edit-template" data-id="<?php echo esc_attr($template['id']); ?>"><?php _e('Edit', 'wheel-of-fortune'); ?></button>
|
||
<button class="button delete-template" data-id="<?php echo esc_attr($template['id']); ?>"><?php _e('Delete', 'wheel-of-fortune'); ?></button>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php endif; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<div class="wheel-card" id="add-template-section">
|
||
<h2><?php echo esc_html__('Add New Email Template', 'wheel-of-fortune'); ?></h2>
|
||
<form method="post" action="">
|
||
<?php wp_nonce_field('wheel_email_templates_nonce'); ?>
|
||
<input type="hidden" name="action" value="add_template">
|
||
<table class="form-table">
|
||
<tr>
|
||
<th scope="row"><label for="template_name"><?php _e('Template Name', 'wheel-of-fortune'); ?></label></th>
|
||
<td><input type="text" id="template_name" name="template_name" class="regular-text" required></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><label for="template_subject"><?php _e('Email Subject', 'wheel-of-fortune'); ?></label></th>
|
||
<td><input type="text" id="template_subject" name="template_subject" class="large-text" required></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><label for="template_body"><?php _e('Email Body', 'wheel-of-fortune'); ?></label></th>
|
||
<td>
|
||
<textarea id="template_body" name="template_body" rows="10" class="large-text code" required></textarea>
|
||
<p class="description"><?php _e('Available template tags:', 'wheel-of-fortune'); ?></p>
|
||
<ul class="wheel-template-tags">
|
||
<li><code>{user_name}</code>, <code>{user_email}</code>, <code>{prize_name}</code>, <code>{prize_description}</code>, <code>{redemption_code}</code>, <code>{site_name}</code>, <code>{site_url}</code>, <code>{date}</code>, <code>{time}</code>, <strong><code>{funded_account_name}</code></strong>, <strong><code>{funded_account_value}</code></strong></li>
|
||
</ul>
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
<p class="submit"><input type="submit" class="button button-primary" value="<?php esc_attr_e('Add Template', 'wheel-of-fortune'); ?>"></p>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Edit Template Modal -->
|
||
<div id="edit-template-modal" style="display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0,0,0,0.4);">
|
||
<div style="background-color: #fefefe; margin: 5% auto; padding: 20px; border: 1px solid #888; width: 60%; max-width: 700px;">
|
||
<span class="close" style="color: #aaa; float: right; font-size: 28px; font-weight: bold; cursor: pointer;">×</span>
|
||
<h2><?php echo esc_html__('Edit Email Template', 'wheel-of-fortune'); ?></h2>
|
||
<form id="edit-template-form" method="post" action="">
|
||
<?php wp_nonce_field('wheel_email_templates_nonce'); ?>
|
||
<input type="hidden" name="action" value="edit_template">
|
||
<input type="hidden" id="edit-template-id" name="template_id" value="">
|
||
<table class="form-table">
|
||
<tr>
|
||
<th scope="row"><label for="edit-template-name"><?php _e('Template Name', 'wheel-of-fortune'); ?></label></th>
|
||
<td><input type="text" id="edit-template-name" name="template_name" class="regular-text" required></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><label for="edit-template-subject"><?php _e('Email Subject', 'wheel-of-fortune'); ?></label></th>
|
||
<td><input type="text" id="edit-template-subject" name="template_subject" class="large-text" required></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><label for="edit-template-body"><?php _e('Email Body', 'wheel-of-fortune'); ?></label></th>
|
||
<td>
|
||
<textarea id="edit-template-body" name="template_body" rows="10" class="large-text code" required></textarea>
|
||
<p class="description"><?php _e('Available template tags:', 'wheel-of-fortune'); ?></p>
|
||
<ul class="wheel-template-tags">
|
||
<li><code>{user_name}</code>, <code>{user_email}</code>, <code>{prize_name}</code>, <code>{prize_description}</code>, <code>{redemption_code}</code>, <code>{site_name}</code>, <code>{site_url}</code>, <code>{date}</code>, <code>{time}</code>, <strong><code>{funded_account_name}</code></strong>, <strong><code>{funded_account_value}</code></strong></li>
|
||
</ul>
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
<p class="submit"><input type="submit" class="button button-primary" value="<?php esc_attr_e('Save Changes', 'wheel-of-fortune'); ?>"></p>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
jQuery(document).ready(function($) {
|
||
// Modalno okno za urejanje predlog
|
||
var modal = $('#edit-template-modal');
|
||
var closeBtn = modal.find('.close');
|
||
|
||
$('.add-new-template').on('click', function() {
|
||
$('html, body').animate({
|
||
scrollTop: $('#add-template-section').offset().top
|
||
}, 500);
|
||
});
|
||
|
||
closeBtn.on('click', function() {
|
||
modal.hide();
|
||
});
|
||
|
||
$(window).on('click', function(e) {
|
||
if (e.target === modal[0]) {
|
||
modal.hide();
|
||
}
|
||
});
|
||
|
||
$('.edit-template').on('click', function(e) {
|
||
e.preventDefault();
|
||
var templateId = $(this).data('id');
|
||
|
||
// AJAX klic za pridobitev podatkov o predlogi
|
||
$.post(ajaxurl, {
|
||
action: 'wheel_get_template_details',
|
||
template_id: templateId,
|
||
_ajax_nonce: '<?php echo wp_create_nonce('wheel_admin_nonce'); ?>'
|
||
}, function(response) {
|
||
if (response.success) {
|
||
var template = response.data;
|
||
|
||
// Napolni obrazec
|
||
$('#edit-template-id').val(template.id);
|
||
$('#edit-template-name').val(template.name);
|
||
$('#edit-template-subject').val(template.subject);
|
||
$('#edit-template-body').val(template.template_body);
|
||
|
||
modal.show();
|
||
} else {
|
||
alert(response.data.message || 'Napaka pri pridobivanju podatkov o predlogi.');
|
||
}
|
||
}).fail(function() {
|
||
alert('Napaka pri komunikaciji s strežnikom.');
|
||
});
|
||
});
|
||
|
||
$('.delete-template').on('click', function(e) {
|
||
e.preventDefault();
|
||
if (!confirm('Ali res želiš izbrisati to predlogo?')) return;
|
||
|
||
var row = $(this).closest('tr');
|
||
var templateId = $(this).data('id');
|
||
|
||
// Ustvari obrazec za POST zahtevo
|
||
var form = $('<form method="post"></form>');
|
||
form.append('<input type="hidden" name="action" value="delete_template">');
|
||
form.append('<input type="hidden" name="template_id" value="' + templateId + '">');
|
||
form.append('<?php echo wp_nonce_field('wheel_email_templates_nonce', '_wpnonce', true, false); ?>');
|
||
|
||
// Dodaj obrazec na stran in ga pošlji
|
||
$('body').append(form);
|
||
form.submit();
|
||
});
|
||
});
|
||
</script>
|