Compare commits

...

1 Commits

Author SHA1 Message Date
Mark Poljansek e11fa6fa5b email template 2025-06-23 17:14:41 +02:00
10 changed files with 1367 additions and 7127 deletions

View File

@ -0,0 +1,569 @@
<?php
/**
* Admin page for editing a single wheel and its prizes
*/
if (!defined('ABSPATH')) exit;
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'add_prize' && check_admin_referer('wheel_prizes_nonce')) {
global $wpdb;
$prizes_table = $wpdb->prefix . 'wheel_prizes';
$wheel_id = isset($_POST['wheel_id']) ? intval($_POST['wheel_id']) : 0;
$name = isset($_POST['prize_name']) ? sanitize_text_field($_POST['prize_name']) : '';
$description = isset($_POST['prize_description']) ? sanitize_textarea_field($_POST['prize_description']) : '';
$probability = isset($_POST['prize_probability']) ? floatval($_POST['prize_probability']) : 0;
$is_active = isset($_POST['prize_is_active']) ? 1 : 0;
$redemption_code = isset($_POST['prize_redemption_code']) ? sanitize_text_field($_POST['prize_redemption_code']) : '';
$is_discount = isset($_POST['prize_is_discount']) ? 1 : 0;
$discount_value = isset($_POST['prize_discount_value']) ? floatval($_POST['prize_discount_value']) : 0;
$email_subject = isset($_POST['prize_email_subject']) ? sanitize_text_field($_POST['prize_email_subject']) : '';
$email_template = isset($_POST['prize_email_template']) ? wp_kses_post($_POST['prize_email_template']) : '';
// Dodaj nova polja
$email_template_id = isset($_POST['email_template_id']) ? intval($_POST['email_template_id']) : 0;
$funded_account_name = isset($_POST['funded_account_name']) ? sanitize_text_field($_POST['funded_account_name']) : null;
$funded_account_value = isset($_POST['funded_account_value']) ? floatval($_POST['funded_account_value']) : null;
if (!empty($name) && $wheel_id > 0) {
$result = $wpdb->insert(
$prizes_table,
[
'wheel_id' => $wheel_id,
'name' => $name,
'description' => $description,
'probability' => $probability,
'is_active' => $is_active,
'redemption_code' => $redemption_code,
'is_discount' => $is_discount,
'discount_value' => $discount_value,
'email_subject' => $email_subject,
'email_template' => $email_template,
// Dodaj nova polja
'email_template_id' => $email_template_id,
'funded_account_name' => $funded_account_name,
'funded_account_value' => $funded_account_value
],
['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%f', '%s', '%s', '%d', '%s', '%f']
);
if ($result !== false) {
echo '<div class="notice notice-success is-dismissible"><p>' . __('Prize added successfully!', 'wheel-of-fortune') . '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>' . __('Error adding prize. 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_prize' && check_admin_referer('wheel_prizes_nonce')) {
global $wpdb;
$prizes_table = $wpdb->prefix . 'wheel_prizes';
$prize_id = isset($_POST['prize_id']) ? intval($_POST['prize_id']) : 0;
$wheel_id = isset($_POST['wheel_id']) ? intval($_POST['wheel_id']) : 0;
$name = isset($_POST['prize_name']) ? sanitize_text_field($_POST['prize_name']) : '';
$description = isset($_POST['prize_description']) ? sanitize_textarea_field($_POST['prize_description']) : '';
$probability = isset($_POST['prize_probability']) ? floatval($_POST['prize_probability']) : 0;
$is_active = isset($_POST['prize_is_active']) ? 1 : 0;
$redemption_code = isset($_POST['prize_redemption_code']) ? sanitize_text_field($_POST['prize_redemption_code']) : '';
$is_discount = isset($_POST['prize_is_discount']) ? 1 : 0;
$discount_value = isset($_POST['prize_discount_value']) ? floatval($_POST['prize_discount_value']) : 0;
$email_subject = isset($_POST['prize_email_subject']) ? sanitize_text_field($_POST['prize_email_subject']) : '';
$email_template = isset($_POST['prize_email_template']) ? wp_kses_post($_POST['prize_email_template']) : '';
// Dodaj nova polja
$email_template_id = isset($_POST['email_template_id']) ? intval($_POST['email_template_id']) : 0;
$funded_account_name = isset($_POST['funded_account_name']) ? sanitize_text_field($_POST['funded_account_name']) : null;
$funded_account_value = isset($_POST['funded_account_value']) ? floatval($_POST['funded_account_value']) : null;
if (!empty($name) && $prize_id > 0 && $wheel_id > 0) {
$result = $wpdb->update(
$prizes_table,
[
'wheel_id' => $wheel_id,
'name' => $name,
'description' => $description,
'probability' => $probability,
'is_active' => $is_active,
'redemption_code' => $redemption_code,
'is_discount' => $is_discount,
'discount_value' => $discount_value,
'email_subject' => $email_subject,
'email_template' => $email_template,
// Dodaj nova polja
'email_template_id' => $email_template_id,
'funded_account_name' => $funded_account_name,
'funded_account_value' => $funded_account_value
],
['id' => $prize_id],
['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%f', '%s', '%s', '%d', '%s', '%f'],
['%d']
);
if ($result !== false) {
echo '<div class="notice notice-success is-dismissible"><p>' . __('Prize updated successfully!', 'wheel-of-fortune') . '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>' . __('Error updating prize. 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>';
}
}
}
if (
$_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) &&
in_array($_POST['action'], ['add_wheel_product', 'delete_wheel_product'])
) {
global $wpdb;
$wheels_table = $wpdb->prefix . 'wof_wheels';
$wheel_products_table = $wpdb->prefix . 'wheel_of_fortune_products';
if ($_POST['action'] === 'add_wheel_product' && check_admin_referer('wheel_products_nonce')) {
$wheel_id = isset($_POST['wheel_id']) ? intval($_POST['wheel_id']) : 0;
$product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0;
$spins_per_purchase = isset($_POST['spins_per_purchase']) ? intval($_POST['spins_per_purchase']) : 1;
// Debug informacije
error_log("=== WHEEL PRODUCT DEBUG ===");
error_log("POST data: " . print_r($_POST, true));
error_log("Wheel ID: " . $wheel_id);
error_log("Product ID: " . $product_id);
error_log("Spins per purchase: " . $spins_per_purchase);
error_log("Table name: " . $wheel_products_table);
// Preveri, ali tabela obstaja
$table_exists = $wpdb->get_var("SHOW TABLES LIKE '$wheel_products_table'") == $wheel_products_table;
error_log("Table exists: " . ($table_exists ? 'YES' : 'NO'));
if (!$table_exists) {
error_log("ERROR: Tabela $wheel_products_table ne obstaja!");
echo '<div class="notice notice-error is-dismissible"><p>' . __('Napaka: Tabela za produkte ne obstaja. Prosimo, deaktivirajte in ponovno aktivirajte plugin.', 'wheel-of-fortune') . '</p></div>';
return;
}
if ($wheel_id > 0 && $product_id > 0 && $spins_per_purchase > 0) {
// Preveri, ali kolo obstaja
$wheel_exists = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wheels_table WHERE id = %d", $wheel_id));
error_log("Wheel exists: " . $wheel_exists);
if (!$wheel_exists) {
error_log("ERROR: Kolo z ID $wheel_id ne obstaja!");
echo '<div class="notice notice-error is-dismissible"><p>' . __('Napaka: Izbrano kolo ne obstaja.', 'wheel-of-fortune') . '</p></div>';
return;
}
// Preveri, ali produkt obstaja
if (class_exists('WooCommerce')) {
$product = wc_get_product($product_id);
if (!$product) {
error_log("ERROR: Produkt z ID $product_id ne obstaja!");
echo '<div class="notice notice-error is-dismissible"><p>' . __('Napaka: Izbrani produkt ne obstaja.', 'wheel-of-fortune') . '</p></div>';
return;
}
}
$data = [
'wheel_id' => $wheel_id,
'product_id' => $product_id,
'spins_per_purchase' => $spins_per_purchase
];
error_log("Data to insert: " . print_r($data, true));
// Preveri, ali je produkt že povezan s tem kolesom
$existing_product = $wpdb->get_var($wpdb->prepare(
"SELECT id FROM $wheel_products_table WHERE wheel_id = %d AND product_id = %d",
$wheel_id, $product_id
));
if ($existing_product) {
error_log("Product already exists for this wheel, updating...");
} else {
error_log("Adding new product to wheel...");
}
$result = $wpdb->replace(
$wheel_products_table,
$data,
['%d', '%d', '%d']
);
error_log("SQL result: " . $result);
error_log("Last SQL query: " . $wpdb->last_query);
error_log("Last SQL error: " . $wpdb->last_error);
if ($result !== false) {
echo '<div class="notice notice-success is-dismissible"><p>' . __('Produkt je bil uspešno dodan ali posodobljen.', 'wheel-of-fortune') . '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>' . __('Napaka pri dodajanju produkta. Preveri vnos.', 'wheel-of-fortune') . '</p></div>';
}
} else {
error_log("Validation failed - Wheel ID: $wheel_id, Product ID: $product_id, Spins: $spins_per_purchase");
echo '<div class="notice notice-error is-dismissible"><p>' . __('Napaka pri dodajanju produkta. Preveri vnos.', 'wheel-of-fortune') . '</p></div>';
}
} elseif ($_POST['action'] === 'delete_wheel_product' && check_admin_referer('wheel_products_nonce')) {
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
if ($id > 0) {
$wpdb->delete($wheel_products_table, ['id' => $id], ['%d']);
echo '<div class="notice notice-success is-dismissible"><p>' . __('Produkt je bil izbrisan.', 'wheel-of-fortune') . '</p></div>';
}
}
}
global $wpdb;
$wheels_table = $wpdb->prefix . 'wof_wheels';
$prizes_table = $wpdb->prefix . 'wheel_prizes';
// Get the current wheel ID from URL
$wheel_id = isset($_GET['wheel_id']) ? intval($_GET['wheel_id']) : 0;
// Fetch wheel data
$wheel = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wheels_table WHERE id = %d", $wheel_id), ARRAY_A);
if (!$wheel) {
echo '<div class="notice notice-error"><p>' . __('Wheel not found.', 'wheel-of-fortune') . '</p></div>';
return;
}
// Fetch prizes for this specific wheel
$prizes = $wpdb->get_results($wpdb->prepare("SELECT * FROM $prizes_table WHERE wheel_id = %d ORDER BY id ASC", $wheel_id), ARRAY_A);
$total_probability = array_sum(wp_list_pluck($prizes, 'probability'));
// Fetch povezane produkte za to kolo
$wheel_products_table = $wpdb->prefix . 'wheel_of_fortune_products';
$products = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wheel_products_table WHERE wheel_id = %d", $wheel_id), ARRAY_A);
// Pridobi vse WooCommerce produkte za dropdown
if (class_exists('WooCommerce')) {
$all_products = wc_get_products(array('limit' => -1, 'status' => 'publish'));
} else {
$all_products = array();
}
?>
<div class="wrap wheel-admin-page">
<h1><?php printf(__('Editing Wheel: %s', 'wheel-of-fortune'), esc_html($wheel['name'])); ?></h1>
<a href="<?php echo admin_url('admin.php?page=wof-wheels'); ?>">&larr; <?php _e('Back to all wheels', 'wheel-of-fortune'); ?></a>
<hr class="wp-header-end">
<div class="wheel-card">
<h2><?php echo esc_html__('Prizes for this Wheel', 'wheel-of-fortune'); ?></h2>
<?php if ($total_probability > 1): ?>
<div class="notice notice-error"><p><?php printf(__('Warning: The total probability is %s, which is greater than 1 (100%%). Please adjust.', 'wheel-of-fortune'), '<strong>' . esc_html($total_probability) . '</strong>'); ?></p></div>
<?php elseif ($total_probability > 0 && $total_probability < 1): ?>
<div class="notice notice-warning"><p><?php printf(__('Notice: The total probability is %s, which is less than 1 (100%%).', 'wheel-of-fortune'), '<strong>' . esc_html($total_probability) . '</strong>'); ?></p></div>
<?php endif; ?>
<p><button class="button button-primary add-new-prize"><?php _e('Add New Prize', 'wheel-of-fortune'); ?></button></p>
<table class="wheel-prizes-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('Description', 'wheel-of-fortune'); ?></th>
<th><?php _e('Probability', 'wheel-of-fortune'); ?></th>
<th><?php _e('Status', 'wheel-of-fortune'); ?></th>
<th><?php _e('Actions', 'wheel-of-fortune'); ?></th>
</tr>
</thead>
<tbody>
<?php if (empty($prizes)): ?>
<tr><td colspan="6"><?php _e('No prizes found. Add your first prize using the button above.', 'wheel-of-fortune'); ?></td></tr>
<?php else: ?>
<?php foreach ($prizes as $prize): ?>
<tr>
<td><?php echo esc_html($prize['id']); ?></td>
<td><?php echo esc_html($prize['name']); ?></td>
<td><?php echo esc_html($prize['description']); ?></td>
<td><?php echo esc_html($prize['probability']); ?></td>
<td><?php echo $prize['is_active'] ? __('Active', 'wheel-of-fortune') : __('Inactive', 'wheel-of-fortune'); ?></td>
<td>
<button class="button edit-prize" data-id="<?php echo esc_attr($prize['id']); ?>"><?php _e('Edit', 'wheel-of-fortune'); ?></button>
<button class="button delete-prize" data-id="<?php echo esc_attr($prize['id']); ?>"><?php _e('Delete', 'wheel-of-fortune'); ?></button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<div class="wheel-card">
<h2><?php echo esc_html__('Add New Prize to this Wheel', 'wheel-of-fortune'); ?></h2>
<form method="post" action="">
<?php wp_nonce_field('wheel_prizes_nonce'); ?>
<input type="hidden" name="action" value="add_prize">
<input type="hidden" name="wheel_id" value="<?php echo esc_attr($wheel_id); ?>">
<table class="wheel-form-table">
<?php include 'partials/prize-form-fields.php'; ?>
</table>
<p class="submit"><input type="submit" class="button button-primary" value="<?php esc_attr_e('Add Prize', 'wheel-of-fortune'); ?>"></p>
</form>
</div>
<div class="wheel-card">
<h2><?php echo esc_html__('Produkti & spini', 'wheel-of-fortune'); ?></h2>
<table class="wheel-products-table widefat fixed striped">
<thead>
<tr>
<th><?php _e('Produkt', 'wheel-of-fortune'); ?></th>
<th><?php _e('Število spinov na nakup', 'wheel-of-fortune'); ?></th>
<th><?php _e('Akcije', 'wheel-of-fortune'); ?></th>
</tr>
</thead>
<tbody>
<?php if (empty($products)): ?>
<tr><td colspan="3"><?php _e('Ni povezanih produktov.', 'wheel-of-fortune'); ?></td></tr>
<?php else: ?>
<?php foreach ($products as $prod): ?>
<?php $wc_product = wc_get_product($prod['product_id']); ?>
<tr>
<td><?php echo $wc_product ? esc_html($wc_product->get_name()) : esc_html($prod['product_id']); ?></td>
<td class="spins-editable" data-id="<?php echo esc_attr($prod['id']); ?>"><?php echo intval($prod['spins_per_purchase']); ?></td>
<td>
<button class="button edit-wheel-product" data-id="<?php echo esc_attr($prod['id']); ?>"><?php _e('Uredi', 'wheel-of-fortune'); ?></button>
<button class="button delete-wheel-product" data-id="<?php echo esc_attr($prod['id']); ?>"><?php _e('Izbriši', 'wheel-of-fortune'); ?></button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<h3><?php _e('Dodaj produkt', 'wheel-of-fortune'); ?></h3>
<form method="post" action="">
<?php wp_nonce_field('wheel_products_nonce'); ?>
<input type="hidden" name="action" value="add_wheel_product">
<input type="hidden" name="wheel_id" value="<?php echo esc_attr($wheel_id); ?>">
<p>
<label for="product_id"><?php _e('Izberi produkt:', 'wheel-of-fortune'); ?></label>
<select name="product_id" id="product_id" required>
<option value=""><?php _e('Izberi produkt', 'wheel-of-fortune'); ?></option>
<?php foreach ($all_products as $product): ?>
<option value="<?php echo esc_attr($product->get_id()); ?>"><?php echo esc_html($product->get_name()); ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="spins_per_purchase"><?php _e('Število spinov na nakup:', 'wheel-of-fortune'); ?></label>
<input type="number" name="spins_per_purchase" id="spins_per_purchase" min="1" value="1" required style="width: 80px;">
</p>
<p class="submit">
<input type="submit" class="button button-primary" value="<?php esc_attr_e('Dodaj', 'wheel-of-fortune'); ?>">
</p>
</form>
</div>
</div>
<!-- Edit Prize Modal (ostaja večinoma nespremenjen, wheel_id se doda dinamično z JS) -->
<div id="edit-prize-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);" data-wheel-id="<?php echo esc_attr($wheel_id); ?>">
<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 Prize', 'wheel-of-fortune'); ?></h2>
<form id="edit-prize-form" method="post" action="">
<?php wp_nonce_field('wheel_prizes_nonce'); ?>
<input type="hidden" name="action" value="edit_prize">
<input type="hidden" id="edit-prize-id" name="prize_id" value="">
<input type="hidden" id="edit-wheel-id" name="wheel_id" value="<?php echo esc_attr($wheel_id); ?>">
<table class="wheel-form-table">
<tr><th scope="row"><label for="edit-prize-name"><?php _e('Prize Name', 'wheel-of-fortune'); ?></label></th><td><input type="text" id="edit-prize-name" name="prize_name" class="regular-text" required></td></tr>
<tr><th scope="row"><label for="edit-prize-description"><?php _e('Description', 'wheel-of-fortune'); ?></label></th><td><textarea id="edit-prize-description" name="prize_description" rows="3" class="large-text"></textarea></td></tr>
<tr><th scope="row"><label for="edit-prize-probability"><?php _e('Probability', 'wheel-of-fortune'); ?></label></th><td><input type="number" id="edit-prize-probability" name="prize_probability" step="any" min="0" max="1" required><p class="description"><?php _e('Enter a value between 0 and 1. Use 0 for prizes that cannot be won.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><?php _e('Status', 'wheel-of-fortune'); ?></th><td><label for="edit-prize-is-active"><input type="checkbox" id="edit-prize-is-active" name="prize_is_active" value="1"><?php _e('Prize is active', 'wheel-of-fortune'); ?></label></td></tr>
<!-- DODAJ NOVA POLJA ZA FUNDED ACCOUNT -->
<tr><th scope="row"><label for="edit-funded-account-name"><?php _e('Funded Account Name', 'wheel-of-fortune'); ?></label></th><td><input type="text" id="edit-funded-account-name" name="funded_account_name" class="regular-text"><p class="description"><?php _e('e.g., Funded7 $50k. Used for the {funded_account_name} tag.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><label for="edit-funded-account-value"><?php _e('Funded Account Value', 'wheel-of-fortune'); ?></label></th><td><input type="number" id="edit-funded-account-value" name="funded_account_value" step="any" min="0"><p class="description"><?php _e('e.g., 50000. Used for the {funded_account_value} tag.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><label for="edit-prize-redemption-code"><?php _e('Redemption Code', 'wheel-of-fortune'); ?></label></th><td><input type="text" id="edit-prize-redemption-code" name="prize_redemption_code" class="regular-text"><p class="description"><?php _e('Optional code for the user to redeem the prize.', 'wheel-of-fortune'); ?></p></td></tr>
<!-- SPREMENI SEKCIJO ZA EMAIL -->
<tr>
<th scope="row"><label for="edit-prize-email-template-id"><?php _e('Email Content', 'wheel-of-fortune'); ?></label></th>
<td>
<select id="edit-prize-email-template-id" name="email_template_id">
<option value="0"><?php _e('-- Custom Email --', 'wheel-of-fortune'); ?></option>
<?php
global $wpdb;
$email_templates = $wpdb->get_results("SELECT id, name FROM {$wpdb->prefix}wheel_email_templates ORDER BY name ASC", ARRAY_A);
foreach ($email_templates as $template) : ?>
<option value="<?php echo esc_attr($template['id']); ?>"><?php echo esc_html($template['name']); ?></option>
<?php endforeach; ?>
</select>
<p class="description"><?php _e('Choose a pre-written template or select "Custom Email" to write your own below.', 'wheel-of-fortune'); ?></p>
</td>
</tr>
<tr class="custom-email-fields-edit"><th scope="row"><label for="edit-prize-email-subject"><?php _e('Email Subject', 'wheel-of-fortune'); ?></label></th><td><input type="text" id="edit-prize-email-subject" name="prize_email_subject" class="large-text"><p class="description"><?php _e('Subject for this prize\'s email. Only used if "Custom Email" is selected.', 'wheel-of-fortune'); ?></p></td></tr>
<tr class="custom-email-fields-edit"><th scope="row"><label for="edit-prize-email-template"><?php _e('Email Template', 'wheel-of-fortune'); ?></label></th><td><textarea id="edit-prize-email-template" name="prize_email_template" rows="10" class="large-text code"></textarea><p class="description"><?php _e('Available template tags:', 'wheel-of-fortune'); ?> <code>{user_name}</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></p></td></tr>
<tr><th scope="row"><label for="edit-prize-is-discount"><?php _e('Is Discount?', 'wheel-of-fortune'); ?></label></th><td><input type="checkbox" id="edit-prize-is-discount" name="prize_is_discount" value="1"><p class="description"><?php _e('Check if the prize is a discount.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><label for="edit-prize-discount-value"><?php _e('Discount Value (%)', 'wheel-of-fortune'); ?></label></th><td><input type="number" id="edit-prize-discount-value" name="prize_discount_value" step="0.01" min="0" max="100"><p class="description"><?php _e('Enter the discount value in %.', 'wheel-of-fortune'); ?></p></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>
// Funkcija za prikaz/skrivanje polj za e-pošto po meri
function handleEmailSourceChange() {
// Za formo za dodajanje
if ($('#prize_email_template_id').val() == '0') {
$('.custom-email-fields').show();
} else {
$('.custom-email-fields').hide();
}
// Za modalno okno za urejanje
if ($('#edit-prize-email-template-id').val() == '0') {
$('.custom-email-fields-edit').show();
} else {
$('.custom-email-fields-edit').hide();
}
}
// Poveži event handler
$('body').on('change', '#prize_email_template_id, #edit-prize-email-template-id', handleEmailSourceChange);
// Sproži ob nalaganju strani za formo za dodajanje
handleEmailSourceChange();
$('.delete-wheel-product').on('click', function(e) {
e.preventDefault();
if (!confirm('Ali res želiš izbrisati ta produkt?')) return;
var row = $(this).closest('tr');
var id = $(this).data('id');
$.post(ajaxurl, {
action: 'wof_delete_wheel_product',
id: id,
_ajax_nonce: '<?php echo wp_create_nonce('wof_delete_wheel_product'); ?>'
}, function(response) {
if (response.success) {
row.fadeOut(300, function() { $(this).remove(); });
} else {
alert(response.data || 'Napaka pri brisanju.');
}
});
});
$('.spins-editable').on('click', function() {
var td = $(this);
if (td.find('input').length) return;
var current = td.text();
var id = td.data('id');
var input = $('<input type="number" min="1" style="width:60px;">').val(current);
td.html(input);
input.focus();
input.on('blur', function() {
var val = input.val();
if (val && val != current) {
$.post(ajaxurl, {
action: 'wof_update_wheel_product_spins',
id: id,
spins: val,
_ajax_nonce: '<?php echo wp_create_nonce('wof_update_wheel_product_spins'); ?>'
}, function(response) {
if (response.success) {
td.text(val);
} else {
alert(response.data || 'Napaka pri shranjevanju.');
td.text(current);
}
});
} else {
td.text(current);
}
});
});
// Modalno okno za urejanje nagrad
var modal = $('#edit-prize-modal');
var closeBtn = modal.find('.close');
$('.add-new-prize').on('click', function() {
$('html, body').animate({
scrollTop: $('.wheel-card:contains("Add New Prize")').offset().top
}, 500);
});
closeBtn.on('click', function() {
modal.hide();
});
$(window).on('click', function(e) {
if (e.target === modal[0]) {
modal.hide();
}
});
$('.edit-prize').on('click', function(e) {
e.preventDefault();
var prizeId = $(this).data('id');
// AJAX klic za pridobitev podatkov o nagradi
$.post(ajaxurl, {
action: 'wheel_get_prize_details',
prize_id: prizeId,
_ajax_nonce: '<?php echo wp_create_nonce('wheel_admin_nonce'); ?>'
}, function(response) {
if (response.success) {
var prize = response.data;
// Napolni obrazec
$('#edit-prize-id').val(prize.id);
$('#edit-wheel-id').val(prize.wheel_id);
$('#edit-prize-name').val(prize.name);
$('#edit-prize-description').val(prize.description);
$('#edit-prize-probability').val(prize.probability);
$('#edit-prize-is-active').prop('checked', prize.is_active == 1);
$('#edit-prize-redemption-code').val(prize.redemption_code);
$('#edit-prize-is-discount').prop('checked', prize.is_discount == 1);
$('#edit-prize-discount-value').val(prize.discount_value);
$('#edit-prize-email-subject').val(prize.email_subject);
$('#edit-prize-email-template').val(prize.email_template);
// Napolni nova polja
$('#edit-funded-account-name').val(prize.funded_account_name);
$('#edit-funded-account-value').val(prize.funded_account_value);
$('#edit-prize-email-template-id').val(prize.email_template_id);
// Sproži spremembo, da se pravilno prikažejo/skrijejo polja
$('#edit-prize-email-template-id').trigger('change');
modal.show();
} else {
alert(response.data.message || 'Napaka pri pridobivanju podatkov o nagradi.');
}
}).fail(function() {
alert('Napaka pri komunikaciji s strežnikom.');
});
});
$('.delete-prize').on('click', function(e) {
e.preventDefault();
if (!confirm('Ali res želiš izbrisati to nagrado?')) return;
var row = $(this).closest('tr');
var prizeId = $(this).data('id');
$.post(ajaxurl, {
action: 'wheel_delete_prize',
prize_id: prizeId,
_ajax_nonce: '<?php echo wp_create_nonce('wheel_admin_nonce'); ?>'
}, function(response) {
if (response.success) {
row.fadeOut(300, function() { $(this).remove(); });
} else {
alert(response.data.message || 'Napaka pri brisanju nagrade.');
}
}).fail(function() {
alert('Napaka pri komunikaciji s strežnikom.');
});
});
});
</script>

View File

@ -21,6 +21,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$email_subject = isset($_POST['prize_email_subject']) ? sanitize_text_field($_POST['prize_email_subject']) : '';
$email_template = isset($_POST['prize_email_template']) ? wp_kses_post($_POST['prize_email_template']) : '';
// Dodaj nova polja
$email_template_id = isset($_POST['email_template_id']) ? intval($_POST['email_template_id']) : 0;
$funded_account_name = isset($_POST['funded_account_name']) ? sanitize_text_field($_POST['funded_account_name']) : null;
$funded_account_value = isset($_POST['funded_account_value']) ? floatval($_POST['funded_account_value']) : null;
if (!empty($name) && $wheel_id > 0) {
$result = $wpdb->insert(
$prizes_table,
@ -35,8 +40,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
'discount_value' => $discount_value,
'email_subject' => $email_subject,
'email_template' => $email_template,
// Dodaj nova polja
'email_template_id' => $email_template_id,
'funded_account_name' => $funded_account_name,
'funded_account_value' => $funded_account_value
],
['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%f', '%s', '%s']
['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%f', '%s', '%s', '%d', '%s', '%f']
);
if ($result !== false) {
@ -63,6 +73,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$email_subject = isset($_POST['prize_email_subject']) ? sanitize_text_field($_POST['prize_email_subject']) : '';
$email_template = isset($_POST['prize_email_template']) ? wp_kses_post($_POST['prize_email_template']) : '';
// Dodaj nova polja
$email_template_id = isset($_POST['email_template_id']) ? intval($_POST['email_template_id']) : 0;
$funded_account_name = isset($_POST['funded_account_name']) ? sanitize_text_field($_POST['funded_account_name']) : null;
$funded_account_value = isset($_POST['funded_account_value']) ? floatval($_POST['funded_account_value']) : null;
if (!empty($name) && $prize_id > 0 && $wheel_id > 0) {
$result = $wpdb->update(
$prizes_table,
@ -77,9 +92,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
'discount_value' => $discount_value,
'email_subject' => $email_subject,
'email_template' => $email_template,
// Dodaj nova polja
'email_template_id' => $email_template_id,
'funded_account_name' => $funded_account_name,
'funded_account_value' => $funded_account_value
],
['id' => $prize_id],
['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%f', '%s', '%s'],
['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%f', '%s', '%s', '%d', '%s', '%f'],
['%d']
);
@ -356,65 +376,37 @@ if (class_exists('WooCommerce')) {
<tr><th scope="row"><label for="edit-prize-description"><?php _e('Description', 'wheel-of-fortune'); ?></label></th><td><textarea id="edit-prize-description" name="prize_description" rows="3" class="large-text"></textarea></td></tr>
<tr><th scope="row"><label for="edit-prize-probability"><?php _e('Probability', 'wheel-of-fortune'); ?></label></th><td><input type="number" id="edit-prize-probability" name="prize_probability" step="any" min="0" max="1" required><p class="description"><?php _e('Enter a value between 0 and 1. Use 0 for prizes that cannot be won.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><?php _e('Status', 'wheel-of-fortune'); ?></th><td><label for="edit-prize-is-active"><input type="checkbox" id="edit-prize-is-active" name="prize_is_active" value="1"><?php _e('Prize is active', 'wheel-of-fortune'); ?></label></td></tr>
<!-- DODAJ NOVA POLJA ZA FUNDED ACCOUNT -->
<tr><th scope="row"><label for="edit-funded-account-name"><?php _e('Funded Account Name', 'wheel-of-fortune'); ?></label></th><td><input type="text" id="edit-funded-account-name" name="funded_account_name" class="regular-text"><p class="description"><?php _e('e.g., Funded7 $50k. Used for the {funded_account_name} tag.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><label for="edit-funded-account-value"><?php _e('Funded Account Value', 'wheel-of-fortune'); ?></label></th><td><input type="number" id="edit-funded-account-value" name="funded_account_value" step="any" min="0"><p class="description"><?php _e('e.g., 50000. Used for the {funded_account_value} tag.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><label for="edit-prize-redemption-code"><?php _e('Redemption Code', 'wheel-of-fortune'); ?></label></th><td><input type="text" id="edit-prize-redemption-code" name="prize_redemption_code" class="regular-text"><p class="description"><?php _e('Optional code for the user to redeem the prize.', 'wheel-of-fortune'); ?></p></td></tr>
<!-- SPREMENI SEKCIJO ZA EMAIL -->
<tr>
<th scope="row"><label for="edit-prize-email-template-id"><?php _e('Email Content', 'wheel-of-fortune'); ?></label></th>
<td>
<select id="edit-prize-email-template-id" name="email_template_id">
<option value="0"><?php _e('-- Custom Email --', 'wheel-of-fortune'); ?></option>
<?php
global $wpdb;
$email_templates = $wpdb->get_results("SELECT id, name FROM {$wpdb->prefix}wheel_email_templates ORDER BY name ASC", ARRAY_A);
foreach ($email_templates as $template) : ?>
<option value="<?php echo esc_attr($template['id']); ?>"><?php echo esc_html($template['name']); ?></option>
<?php endforeach; ?>
</select>
<p class="description"><?php _e('Choose a pre-written template or select "Custom Email" to write your own below.', 'wheel-of-fortune'); ?></p>
</td>
</tr>
<tr class="custom-email-fields-edit"><th scope="row"><label for="edit-prize-email-subject"><?php _e('Email Subject', 'wheel-of-fortune'); ?></label></th><td><input type="text" id="edit-prize-email-subject" name="prize_email_subject" class="large-text"><p class="description"><?php _e('Subject for this prize\'s email. Only used if "Custom Email" is selected.', 'wheel-of-fortune'); ?></p></td></tr>
<tr class="custom-email-fields-edit"><th scope="row"><label for="edit-prize-email-template"><?php _e('Email Template', 'wheel-of-fortune'); ?></label></th><td><textarea id="edit-prize-email-template" name="prize_email_template" rows="10" class="large-text code"></textarea><p class="description"><?php _e('Available template tags:', 'wheel-of-fortune'); ?> <code>{user_name}</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></p></td></tr>
<tr><th scope="row"><label for="edit-prize-is-discount"><?php _e('Is Discount?', 'wheel-of-fortune'); ?></label></th><td><input type="checkbox" id="edit-prize-is-discount" name="prize_is_discount" value="1"><p class="description"><?php _e('Check if the prize is a discount.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><label for="edit-prize-discount-value"><?php _e('Discount Value (%)', 'wheel-of-fortune'); ?></label></th><td><input type="number" id="edit-prize-discount-value" name="prize_discount_value" step="0.01" min="0" max="100"><p class="description"><?php _e('Enter the discount value in %.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><label for="edit-prize-email-subject"><?php _e('Email Subject', 'wheel-of-fortune'); ?></label></th><td><input type="text" id="edit-prize-email-subject" name="prize_email_subject" class="large-text"><p class="description"><?php _e('Subject for this prize\'s email. If empty, the default subject will be used.', 'wheel-of-fortune'); ?></p></td></tr>
<tr><th scope="row"><label for="edit-prize-email-template"><?php _e('Email Template', 'wheel-of-fortune'); ?></label></th><td><textarea id="edit-prize-email-template" name="prize_email_template" rows="10" class="large-text code"></textarea><p class="description"><?php _e('Available template tags:', 'wheel-of-fortune'); ?> <code>{user_name}</code>, <code>{prize_name}</code>, etc.</p></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($) {
$('.delete-wheel-product').on('click', function(e) {
e.preventDefault();
if (!confirm('Ali res želiš izbrisati ta produkt?')) return;
var row = $(this).closest('tr');
var id = $(this).data('id');
$.post(ajaxurl, {
action: 'wof_delete_wheel_product',
id: id,
_ajax_nonce: '<?php echo wp_create_nonce('wof_delete_wheel_product'); ?>'
}, function(response) {
if (response.success) {
row.fadeOut(300, function() { $(this).remove(); });
} else {
alert(response.data || 'Napaka pri brisanju.');
}
});
});
$('.spins-editable').on('click', function() {
var td = $(this);
if (td.find('input').length) return;
var current = td.text();
var id = td.data('id');
var input = $('<input type="number" min="1" style="width:60px;">').val(current);
td.html(input);
input.focus();
input.on('blur', function() {
var val = input.val();
if (val && val != current) {
$.post(ajaxurl, {
action: 'wof_update_wheel_product_spins',
id: id,
spins: val,
_ajax_nonce: '<?php echo wp_create_nonce('wof_update_wheel_product_spins'); ?>'
}, function(response) {
if (response.success) {
td.text(val);
} else {
alert(response.data || 'Napaka pri shranjevanju.');
td.text(current);
}
});
} else {
td.text(current);
}
});
});
});
</script>

View File

@ -0,0 +1,262 @@
<?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>

View File

@ -6,6 +6,29 @@ jQuery(document).ready(function($) {
return;
}
// Funkcija za prikaz/skrivanje polj za e-pošto po meri
function handleEmailSourceChange() {
// Za formo za dodajanje
if ($('#prize_email_template_id').val() == '0') {
$('.custom-email-fields').show();
} else {
$('.custom-email-fields').hide();
}
// Za modalno okno za urejanje
if ($('#edit-prize-email-template-id').val() == '0') {
$('.custom-email-fields-edit').show();
} else {
$('.custom-email-fields-edit').hide();
}
}
// Poveži event handler za spremembo izbire predloge
$('body').on('change', '#prize_email_template_id, #edit-prize-email-template-id', handleEmailSourceChange);
// Sproži ob nalaganju strani za formo za dodajanje
handleEmailSourceChange();
// Tab navigation
$('.nav-tab').on('click', function(e) {
e.preventDefault();
@ -72,6 +95,14 @@ jQuery(document).ready(function($) {
$('#edit-prize-email-subject').val(prize.email_subject);
$('#edit-prize-email-template').val(prize.email_template);
// Napolni nova polja
$('#edit-funded-account-name').val(prize.funded_account_name);
$('#edit-funded-account-value').val(prize.funded_account_value);
$('#edit-prize-email-template-id').val(prize.email_template_id);
// Sproži spremembo, da se pravilno prikažejo/skrijejo polja
$('#edit-prize-email-template-id').trigger('change');
modal.show();
} else {
alert(response.data.message || 'An error occurred.');
@ -100,6 +131,11 @@ jQuery(document).ready(function($) {
discount_value: $('#edit-prize-discount-value').val(),
email_subject: $('#edit-prize-email-subject').val(),
email_template: $('#edit-prize-email-template').val(),
// Dodaj nova polja
email_template_id: $('#edit-prize-email-template-id').val(),
funded_account_name: $('#edit-funded-account-name').val(),
funded_account_value: $('#edit-funded-account-value').val()
};
// Debug: preveri wheel_id
@ -282,4 +318,53 @@ jQuery(document).ready(function($) {
}
});
});
// Upravljanje z wheel_product elementi (preneseno iz edit-wheel-page.php)
$('.delete-wheel-product').on('click', function(e) {
e.preventDefault();
if (!confirm('Ali res želiš izbrisati ta produkt?')) return;
var row = $(this).closest('tr');
var id = $(this).data('id');
$.post(ajaxurl, {
action: 'wof_delete_wheel_product',
id: id,
_ajax_nonce: wheel_admin_nonce._ajax_nonce
}, function(response) {
if (response.success) {
row.fadeOut(300, function() { $(this).remove(); });
} else {
alert(response.data || 'Napaka pri brisanju.');
}
});
});
$('.spins-editable').on('click', function() {
var td = $(this);
if (td.find('input').length) return;
var current = td.text();
var id = td.data('id');
var input = $('<input type="number" min="1" style="width:60px;">').val(current);
td.html(input);
input.focus();
input.on('blur', function() {
var val = input.val();
if (val && val != current) {
$.post(ajaxurl, {
action: 'wof_update_wheel_product_spins',
id: id,
spins: val,
_ajax_nonce: wheel_admin_nonce._ajax_nonce
}, function(response) {
if (response.success) {
td.text(val);
} else {
alert(response.data || 'Napaka pri shranjevanju.');
td.text(current);
}
});
} else {
td.text(current);
}
});
});
});

View File

@ -3,6 +3,10 @@
* Reusable form fields for adding/editing prizes
*/
if (!defined('ABSPATH')) exit;
// Pridobi predloge (potrebno na strani, ki vključuje to datoteko)
global $wpdb;
$email_templates = $wpdb->get_results("SELECT id, name FROM {$wpdb->prefix}wheel_email_templates ORDER BY name ASC", ARRAY_A);
?>
<tr>
<th scope="row"><label for="prize_name"><?php _e('Prize Name', 'wheel-of-fortune'); ?></label></th>
@ -28,6 +32,23 @@ if (!defined('ABSPATH')) exit;
</label>
</td>
</tr>
<!-- DODAJ NOVA POLJA ZA FUNDED ACCOUNT -->
<tr>
<th scope="row"><label for="funded_account_name"><?php _e('Funded Account Name', 'wheel-of-fortune'); ?></label></th>
<td>
<input type="text" id="funded_account_name" name="funded_account_name" class="regular-text">
<p class="description"><?php _e('e.g., Funded7 $50k. Used for the {funded_account_name} tag.', 'wheel-of-fortune'); ?></p>
</td>
</tr>
<tr>
<th scope="row"><label for="funded_account_value"><?php _e('Funded Account Value', 'wheel-of-fortune'); ?></label></th>
<td>
<input type="number" id="funded_account_value" name="funded_account_value" step="any" min="0">
<p class="description"><?php _e('e.g., 50000. Used for the {funded_account_value} tag.', 'wheel-of-fortune'); ?></p>
</td>
</tr>
<tr>
<th scope="row"><label for="prize_redemption_code"><?php _e('Redemption Code', 'wheel-of-fortune'); ?></label></th>
<td>
@ -35,24 +56,42 @@ if (!defined('ABSPATH')) exit;
<p class="description"><?php _e('Optional code for the user to redeem the prize.', 'wheel-of-fortune'); ?></p>
</td>
</tr>
<!-- SPREMENJENA EMAIL SEKCIJA -->
<tr>
<th scope="row"><label for="prize_email_subject"><?php _e('Email Subject', 'wheel-of-fortune'); ?></label></th>
<th scope="row"><label for="prize_email_template_id"><?php _e('Email Content', 'wheel-of-fortune'); ?></label></th>
<td>
<select id="prize_email_template_id" name="email_template_id">
<option value="0"><?php _e('-- Custom Email --', 'wheel-of-fortune'); ?></option>
<?php foreach ($email_templates as $template) : ?>
<option value="<?php echo esc_attr($template['id']); ?>"><?php echo esc_html($template['name']); ?></option>
<?php endforeach; ?>
</select>
<p class="description"><?php _e('Choose a pre-written template or select "Custom Email" to write your own below.', 'wheel-of-fortune'); ?></p>
</td>
</tr>
<tbody class="custom-email-fields">
<tr>
<th scope="row"><label for="prize_email_subject"><?php _e('Email Subject', 'wheel-of-fortune'); ?></label></th>
<td>
<input type="text" id="prize_email_subject" name="prize_email_subject" class="large-text">
<p class="description"><?php _e('Subject for this prize\'s email. If empty, the default subject will be used.', 'wheel-of-fortune'); ?></p>
<p class="description"><?php _e('Subject for this prize\'s email. Only used if "Custom Email" is selected.', 'wheel-of-fortune'); ?></p>
</td>
</tr>
<tr>
<th scope="row"><label for="prize_email_template"><?php _e('Email Template', 'wheel-of-fortune'); ?></label></th>
<td>
<textarea id="prize_email_template" name="prize_email_template" rows="10" class="large-text code"></textarea>
<p class="description"><?php _e('Content for this prize\'s email. Leave blank for default template.', 'wheel-of-fortune'); ?></p>
<p class="description"><?php _e('Content for this prize\'s email. Only used if "Custom Email" is selected.', 'wheel-of-fortune'); ?></p>
<p><?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></li>
<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>
</tbody>
<tr>
<th scope="row"><label for="prize_is_discount"><?php _e('Je discount?', 'wheel-of-fortune'); ?></label></th>
<td>

File diff suppressed because it is too large Load Diff

0
export_code.sh Normal file → Executable file
View File

View File

@ -0,0 +1,45 @@
-- Dodajanje privzetih email predlog
-- To datoteko lahko uvozimo ročno v phpMyAdmin ali uporabimo za references
-- Predloga 1: Standardna čestitka
INSERT INTO `wp_wheel_email_templates` (`name`, `subject`, `template_body`) VALUES
('Standardna čestitka', 'Čestitamo za vašo nagrado!',
'<h2>Čestitamo, {user_name}!</h2>
<p>Uspešno ste zadeli nagrado na našem kolesu sreče.</p>
<p><strong>Nagrada:</strong> {prize_name}</p>
<p>{prize_description}</p>
<p><strong>Koda za unovčitev:</strong> {redemption_code}</p>
<p>Hvala za sodelovanje!</p>
<p>Lep pozdrav,<br>{site_name}</p>');
-- Predloga 2: Funded Account obvestilo
INSERT INTO `wp_wheel_email_templates` (`name`, `subject`, `template_body`) VALUES
('Funded Account obvestilo', 'Čestitamo! Zadeli ste Funded Account!',
'<h2>Čestitamo, {user_name}!</h2>
<p>Uspešno ste zadeli <strong>Funded Account</strong> na našem kolesu sreče!</p>
<p>Podrobnosti o vašem računu:</p>
<ul>
<li><strong>Tip računa:</strong> {funded_account_name}</li>
<li><strong>Vrednost računa:</strong> {funded_account_value}</li>
<li><strong>Koda za unovčitev:</strong> {redemption_code}</li>
</ul>
<p>Za aktivacijo vašega računa sledite naslednjim korakom:</p>
<ol>
<li>Prijavite se v svoj uporabniški račun</li>
<li>Pojdite na stran "Moji računi"</li>
<li>Kliknite na "Aktiviraj nov račun"</li>
<li>Vnesite kodo za unovčitev</li>
</ol>
<p>Če potrebujete pomoč, nas kontaktirajte na info@example.com.</p>
<p>Lep pozdrav,<br>{site_name}</p>');
-- Predloga 3: Kupon za popust
INSERT INTO `wp_wheel_email_templates` (`name`, `subject`, `template_body`) VALUES
('Kupon za popust', 'Vaš kupon za popust je pripravljen!',
'<h2>Čestitamo, {user_name}!</h2>
<p>Uspešno ste zadeli <strong>kupon za popust</strong> na našem kolesu sreče!</p>
<p>Vaš kupon vam omogoča popust pri naslednjem nakupu.</p>
<p><strong>Koda kupona:</strong> {redemption_code}</p>
<p>Za uveljavitev popusta enostavno vnesite zgornjo kodo pri zaključku nakupa.</p>
<p>Kupon je veljaven 30 dni od datuma prejema.</p>
<p>Lep pozdrav,<br>{site_name}</p>');

View File

@ -25,6 +25,17 @@
<td style="font-weight: bold;">Description:</td>
<td>{prize_description}</td>
</tr>
<!-- FUNDED ACCOUNT FIELDS - prikaži samo če obstajajo -->
<!-- BEGIN FUNDED ACCOUNT -->
<tr>
<td style="font-weight: bold;">Account Type:</td>
<td><strong>{funded_account_name}</strong></td>
</tr>
<tr>
<td style="font-weight: bold;">Account Value:</td>
<td><strong>{funded_account_value}</strong></td>
</tr>
<!-- END FUNDED ACCOUNT -->
<tr>
<td style="font-weight: bold;">Redemption code:</td>
<td><strong>{redemption_code}</strong></td>

View File

@ -127,6 +127,7 @@ class WheelOfFortune {
add_action('wp_ajax_wheel_get_prizes', array($this, 'ajax_get_prizes'));
add_action('wp_ajax_wof_delete_wheel_product', array($this, 'ajax_delete_wheel_product'));
add_action('wp_ajax_wof_update_wheel_product_spins', array($this, 'ajax_update_wheel_product_spins'));
add_action('wp_ajax_wheel_get_template_details', array($this, 'ajax_get_template_details'));
// Vključi testno skripto za kupone
if (is_admin()) {
@ -264,54 +265,23 @@ class WheelOfFortune {
private function create_database_tables() {
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$charset_collate = $wpdb->get_charset_collate();
// Ustvari tabelo za kolesa
$wheels_table = $wpdb->prefix . 'wof_wheels';
$wheel_products_table = $wpdb->prefix . 'wheel_of_fortune_products';
// Debug informacije za tabelo
error_log("=== TABLE DEBUG ===");
error_log("Wheels table: " . $wheels_table);
error_log("Products table: " . $wheel_products_table);
// Preveri, ali tabela obstaja
$wheels_table_exists = $wpdb->get_var("SHOW TABLES LIKE '$wheels_table'") == $wheels_table;
error_log("Wheels table exists: " . ($wheels_table_exists ? 'YES' : 'NO'));
$products_table_exists = $wpdb->get_var("SHOW TABLES LIKE '$wheel_products_table'") == $wheel_products_table;
error_log("Products table exists: " . ($products_table_exists ? 'YES' : 'NO'));
if ($wheels_table_exists) {
$wheels_table_structure = $wpdb->get_results("DESCRIBE $wheels_table");
error_log("Wheels table structure: " . print_r($wheels_table_structure, true));
}
if ($products_table_exists) {
$products_table_structure = $wpdb->get_results("DESCRIBE $wheel_products_table");
error_log("Products table structure: " . print_r($products_table_structure, true));
}
$sql_wheels = "CREATE TABLE $wheels_table (
// Tabela za spine uporabnikov
$table_spins = $wpdb->prefix . 'wheel_spins';
$sql_spins = "CREATE TABLE $table_spins (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
slug varchar(100) NOT NULL,
created_at datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
user_id bigint(20) NOT NULL,
wheel_id mediumint(9) NOT NULL DEFAULT 1,
spins_available int(11) NOT NULL DEFAULT 0,
last_spin_date datetime DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY slug (slug)
UNIQUE KEY user_wheel (user_id,wheel_id)
) $charset_collate;";
$sql_products = "CREATE TABLE $wheel_products_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
wheel_id mediumint(9) NOT NULL,
product_id bigint(20) NOT NULL,
spins_per_purchase int(11) NOT NULL DEFAULT 1,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY wheel_product (wheel_id, product_id)
) $charset_collate;";
// --- 2. Posodobljena tabela za nagrade ---
// Tabela za nagrade
$table_prizes = $wpdb->prefix . 'wheel_prizes';
$sql_prizes = "CREATE TABLE $table_prizes (
id mediumint(9) NOT NULL AUTO_INCREMENT,
@ -321,8 +291,16 @@ class WheelOfFortune {
probability float NOT NULL,
is_active tinyint(1) NOT NULL DEFAULT 1,
image_url varchar(255) DEFAULT '',
-- STARA POLJA ZA EMAIL --
email_subject varchar(255) DEFAULT '',
email_template text DEFAULT '',
-- NOVA POLJA --
email_template_id mediumint(9) NOT NULL DEFAULT 0, -- 0 pomeni custom email
funded_account_name varchar(255) DEFAULT NULL,
funded_account_value decimal(12,2) DEFAULT NULL,
redemption_code varchar(100) DEFAULT '',
is_discount tinyint(1) NOT NULL DEFAULT 0,
discount_value float DEFAULT 0,
@ -330,21 +308,7 @@ class WheelOfFortune {
KEY wheel_id (wheel_id)
) $charset_collate;";
// --- 3. Posodobljena tabela za spine (NOVO - spini po kolesih) ---
$table_spins = $wpdb->prefix . 'wheel_spins';
$sql_spins = "CREATE TABLE $table_spins (
id mediumint(9) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
wheel_id mediumint(9) NOT NULL DEFAULT 1,
spins_available int(11) NOT NULL DEFAULT 0,
last_spin_date datetime DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY user_wheel (user_id, wheel_id),
KEY user_id (user_id),
KEY wheel_id (wheel_id)
) $charset_collate;";
// --- 4. Posodobljena tabela za log (NOVO - log po kolesih) ---
// Tabela za dnevnik vrtljajev
$table_log = $wpdb->prefix . 'wheel_log';
$sql_log = "CREATE TABLE $table_log (
id mediumint(9) NOT NULL AUTO_INCREMENT,
@ -353,37 +317,62 @@ class WheelOfFortune {
prize_id mediumint(9) NOT NULL,
spin_date datetime NOT NULL,
redeemed tinyint(1) NOT NULL DEFAULT 0,
redemption_code varchar(100) DEFAULT '',
PRIMARY KEY (id),
KEY user_id (user_id),
KEY wheel_id (wheel_id),
KEY prize_id (prize_id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql_wheels); // Dodajanje nove tabele
dbDelta($sql_products);
dbDelta($sql_prizes);
// Tabela za kolesa
$table_wheels = $wpdb->prefix . 'wof_wheels';
$sql_wheels = "CREATE TABLE $table_wheels (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
description text DEFAULT '',
is_active tinyint(1) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
) $charset_collate;";
// Tabela za izdelke, ki podeljujejo spine
$table_products = $wpdb->prefix . 'wheel_of_fortune_products';
$sql_products = "CREATE TABLE $table_products (
id mediumint(9) NOT NULL AUTO_INCREMENT,
wheel_id mediumint(9) NOT NULL DEFAULT 1,
product_id bigint(20) NOT NULL,
spins_per_purchase int(11) NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE KEY wheel_product (wheel_id,product_id)
) $charset_collate;";
// --- DODAJ NOVO TABELO ZA EMAIL PREDLOGE ---
$table_email_templates = $wpdb->prefix . 'wheel_email_templates';
$sql_email_templates = "CREATE TABLE $table_email_templates (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
subject varchar(255) NOT NULL,
template_body text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
dbDelta($sql_spins);
dbDelta($sql_prizes);
dbDelta($sql_log);
dbDelta($sql_wheels);
dbDelta($sql_products);
dbDelta($sql_email_templates);
// Debug: preveri, ali so se tabele ustvarile
$wheels_after = $wpdb->get_var("SHOW TABLES LIKE '$wheels_table'") == $wheels_table;
$products_after = $wpdb->get_var("SHOW TABLES LIKE '$wheel_products_table'") == $wheel_products_table;
error_log("After dbDelta - Wheels table exists: " . ($wheels_after ? 'YES' : 'NO'));
error_log("After dbDelta - Products table exists: " . ($products_after ? 'YES' : 'NO'));
if (!$wheels_after) {
error_log("Wheel of Fortune: Tabela $wheels_table se ni ustvarila!");
} else {
error_log("Wheel of Fortune: Tabela $wheels_table uspešno ustvarjena.");
}
if (!$products_after) {
error_log("Wheel of Fortune: Tabela $wheel_products_table se ni ustvarila!");
} else {
error_log("Wheel of Fortune: Tabela $wheel_products_table uspešno ustvarjena.");
// Dodaj privzeto kolo, če še ne obstaja
$existing_wheel = $wpdb->get_var("SELECT COUNT(*) FROM $table_wheels");
if (!$existing_wheel) {
$wpdb->insert(
$table_wheels,
[
'name' => __('Default Wheel', 'wheel-of-fortune'),
'description' => __('Default wheel of fortune', 'wheel-of-fortune'),
'is_active' => 1
],
['%s', '%s', '%d']
);
}
}
@ -518,28 +507,51 @@ class WheelOfFortune {
public function admin_menu() {
add_menu_page(
__('Wheels of Fortune', 'wheel-of-fortune'),
__('Wheels', 'wheel-of-fortune'),
'Kolo Sreče',
'Kolo Sreče',
'manage_options',
'wof-wheels', // slug
array($this, 'wheels_page'), // callback
'wheel-of-fortune',
array($this, 'wheels_page'),
'dashicons-marker',
30
);
// Stran za urejanje posameznega kolesa (skrita iz glavnega menija, dostopna preko linkov)
add_submenu_page(
'wof-wheels', // parent slug
__('Edit Wheel', 'wheel-of-fortune'),
__('Edit Wheel', 'wheel-of-fortune'),
'wheel-of-fortune',
__('Wheels', 'wheel-of-fortune'),
__('Wheels', 'wheel-of-fortune'),
'manage_options',
'wof-edit-wheel',
array($this, 'edit_wheel_page')
'wheel-of-fortune',
array($this, 'wheels_page')
);
add_submenu_page(
'wheel-of-fortune',
__('Statistics', 'wheel-of-fortune'),
__('Statistics', 'wheel-of-fortune'),
'manage_options',
'wheel-stats',
array($this, 'stats_page')
);
add_submenu_page(
'wheel-of-fortune',
__('Users', 'wheel-of-fortune'),
__('Users', 'wheel-of-fortune'),
'manage_options',
'wheel-users',
array($this, 'users_page')
);
// Dodaj novo stran za email predloge
add_submenu_page(
'wheel-of-fortune',
__('Email Templates', 'wheel-of-fortune'),
__('Email Templates', 'wheel-of-fortune'),
'manage_options',
'wheel-email-templates',
array($this, 'email_templates_page')
);
// Statistika in uporabniki
add_submenu_page('wof-wheels', __('Statistics', 'wheel-of-fortune'), __('Statistics', 'wheel-of-fortune'), 'manage_options', 'wof-stats', array($this, 'stats_page'));
add_submenu_page('wof-wheels', __('Users & Spins', 'wheel-of-fortune'), __('Users & Spins', 'wheel-of-fortune'), 'manage_options', 'wof-users', array($this, 'users_page'));
}
public function wheels_page() {
@ -564,6 +576,10 @@ class WheelOfFortune {
include WHEEL_OF_FORTUNE_PLUGIN_DIR . 'admin/users-page.php';
}
public function email_templates_page() {
require_once WHEEL_OF_FORTUNE_PLUGIN_DIR . 'admin/email-templates-page.php';
}
public function register_rest_routes() {
register_rest_route('wheel-of-fortune/v1', '/spin', array('methods' => 'POST', 'callback' => array($this, 'process_wheel_spin'), 'permission_callback' => 'is_user_logged_in'));
register_rest_route('wheel-of-fortune/v1', '/test', array('methods' => 'GET', 'callback' => function() { return new WP_REST_Response(['success' => true, 'message' => 'REST API endpoint is working correctly.'], 200); }, 'permission_callback' => '__return_true'));
@ -918,35 +934,34 @@ class WheelOfFortune {
}
public function ajax_save_prize() {
check_ajax_referer('wheel_admin_nonce', '_ajax_nonce');
check_ajax_referer('wheel_of_fortune_admin', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(['message' => __('You do not have permission to perform this action.', 'wheel-of-fortune')]);
wp_send_json_error(__('Nimate dovoljenja za to dejanje.', 'wheel-of-fortune'));
return;
}
// Dodamo wheel_id
$wheel_id = isset($_POST['wheel_id']) ? intval($_POST['wheel_id']) : 0;
$prize_id = isset($_POST['prize_id']) ? intval($_POST['prize_id']) : 0;
$wheel_id = isset($_POST['wheel_id']) ? intval($_POST['wheel_id']) : 1;
$name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
$description = isset($_POST['description']) ? sanitize_textarea_field($_POST['description']) : '';
$description = isset($_POST['description']) ? wp_kses_post($_POST['description']) : '';
$probability = isset($_POST['probability']) ? floatval($_POST['probability']) : 0;
$is_active = isset($_POST['is_active']) ? intval($_POST['is_active']) : 0;
$redemption_code = isset($_POST['redemption_code']) ? sanitize_text_field($_POST['redemption_code']) : '';
$is_discount = isset($_POST['is_discount']) ? intval($_POST['is_discount']) : 0;
$is_active = isset($_POST['is_active']) ? 1 : 0;
$image_url = isset($_POST['image_url']) ? esc_url_raw($_POST['image_url']) : '';
$email_subject = isset($_POST['email_subject']) ? sanitize_text_field($_POST['email_subject']) : '';
$email_template = isset($_POST['email_template']) ? wp_kses_post($_POST['email_template']) : '';
$redemption_code = isset($_POST['redemption_code']) ? sanitize_text_field($_POST['redemption_code']) : '';
$is_discount = isset($_POST['is_discount']) ? 1 : 0;
$discount_value = isset($_POST['discount_value']) ? floatval($_POST['discount_value']) : 0;
// Dodaj nova polja
$email_template_id = isset($_POST['email_template_id']) ? intval($_POST['email_template_id']) : 0;
$funded_account_name = isset($_POST['funded_account_name']) ? sanitize_text_field($_POST['funded_account_name']) : null;
$funded_account_value = isset($_POST['funded_account_value']) ? floatval($_POST['funded_account_value']) : null;
if (empty($name)) {
wp_send_json_error(['message' => __('Prize name is required.', 'wheel-of-fortune')]);
}
if ($wheel_id === 0) {
wp_send_json_error(['message' => __('Wheel ID is missing.', 'wheel-of-fortune')]);
}
if ($probability < 0 || $probability > 1) {
wp_send_json_error(['message' => __('Probability must be between 0 and 1.', 'wheel-of-fortune')]);
wp_send_json_error(__('Ime nagrade je obvezno.', 'wheel-of-fortune'));
return;
}
global $wpdb;
@ -958,26 +973,37 @@ class WheelOfFortune {
'description' => $description,
'probability' => $probability,
'is_active' => $is_active,
'redemption_code' => $redemption_code,
'is_discount' => $is_discount,
'image_url' => $image_url,
'email_subject' => $email_subject,
'email_template' => $email_template,
'redemption_code' => $redemption_code,
'is_discount' => $is_discount,
'discount_value' => $discount_value,
// Dodaj nova polja
'email_template_id' => $email_template_id,
'funded_account_name' => $funded_account_name,
'funded_account_value' => $funded_account_value
];
$format = ['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%s', '%s', '%f'];
$format = ['%d', '%s', '%s', '%f', '%d', '%s', '%s', '%s', '%s', '%d', '%f', '%d', '%s', '%f'];
if ($prize_id > 0) {
// Posodobi obstoječo nagrado
$result = $wpdb->update($table_name, $data, ['id' => $prize_id], $format, ['%d']);
} else {
// Dodaj novo nagrado
$result = $wpdb->insert($table_name, $data, $format);
$prize_id = $wpdb->insert_id;
}
if ($result === false) {
wp_send_json_error(['message' => $wpdb->last_error]);
if ($result !== false) {
wp_send_json_success([
'id' => $prize_id,
'message' => __('Nagrada uspešno shranjena.', 'wheel-of-fortune')
]);
} else {
wp_send_json_success(['message' => __('Prize saved successfully!', 'wheel-of-fortune'), 'prize_id' => $prize_id]);
wp_send_json_error(__('Napaka pri shranjevanju nagrade.', 'wheel-of-fortune'));
}
}
@ -1403,92 +1429,93 @@ class WheelOfFortune {
* @param array $prize The prize details array.
*/
public function send_prize_email($user_id, $prize) {
if (!$user_id || !is_array($prize)) {
wheel_of_fortune_debug_log("send_prize_email: Invalid parameters");
return false;
}
$user = get_userdata($user_id);
if (!$user) {
wheel_of_fortune_debug_log("send_prize_email: Uporabnik z ID {$user_id} ni bil najden.");
return;
wheel_of_fortune_debug_log("send_prize_email: User not found - ID: $user_id");
return false;
}
// Določi vsebino emaila - uporabi specifično predlogo za nagrado, če obstaja, sicer splošno
$subject = !empty($prize['email_subject'])
? $prize['email_subject']
: sprintf(__('Congratulations! You won a prize on the Wheel of Fortune - %s', 'wheel-of-fortune'), get_bloginfo('name'));
$body = !empty($prize['email_template'])
? $prize['email_template']
: file_get_contents(WHEEL_OF_FORTUNE_PLUGIN_DIR . 'templates/emails/default-prize-email.html');
// PREVERI, ALI NAGRADA UPORABLJA PREDLOGO
if (!empty($prize['email_template_id'])) {
global $wpdb;
$template_table = $wpdb->prefix . 'wheel_email_templates';
$email_template_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM $template_table WHERE id = %d", $prize['email_template_id']), ARRAY_A);
if ($email_template_data) {
$prize['email_subject'] = $email_template_data['subject'];
$prize['email_template'] = $email_template_data['template_body'];
}
}
// Določi vsebino emaila
$email_subject = !empty($prize['email_subject']) ? $prize['email_subject'] : sprintf(__('Congratulations on your prize: %s', 'wheel-of-fortune'), $prize['name']);
$email_content = !empty($prize['email_template']) ? $prize['email_template'] : $this->get_default_email_template($prize);
// Pripravi nadomestne oznake (placeholders)
$replacements = [
'{user_name}' => $user->display_name,
'{user_email}' => $user->user_email,
'{prize_name}' => $prize['name'],
'{prize_description}' => $prize['description'],
'{redemption_code}' => !empty($prize['redemption_code']) ? $prize['redemption_code'] : __('N/A', 'wheel-of-fortune'),
'{site_name}' => get_bloginfo('name'),
'{site_url}' => home_url(),
'{date}' => date_i18n(get_option('date_format')),
'{time}' => date_i18n(get_option('time_format')),
];
'{user_name}' => $user->display_name,
'{user_email}' => $user->user_email,
'{prize_name}' => $prize['name'],
'{prize_description}' => $prize['description'],
'{redemption_code}' => !empty($prize['redemption_code']) ? $prize['redemption_code'] : __('N/A', 'wheel-of-fortune'),
// DODAJ NOVE OZNAČEVALCE
'{funded_account_name}' => $prize['funded_account_name'] ?? '',
'{funded_account_value}' => isset($prize['funded_account_value']) ? number_format_i18n($prize['funded_account_value'], 0) : '',
// Zamenjaj oznake v vsebini in zadevi
$final_subject = str_replace(array_keys($replacements), array_values($replacements), $subject);
$final_body = str_replace(array_keys($replacements), array_values($replacements), $body);
// Pridobi nastavitve pošiljatelja
$from_name = get_option('wheel_email_from_name', get_bloginfo('name'));
$from_email = get_option('wheel_email_from_email', get_bloginfo('admin_email'));
$headers = [
'Content-Type: text/html; charset=UTF-8',
'From: ' . $from_name . ' <' . $from_email . '>'
'{site_name}' => get_bloginfo('name'),
'{site_url}' => home_url(),
'{date}' => date_i18n(get_option('date_format')),
'{time}' => date_i18n(get_option('time_format')),
];
// Preveri, ali je omogočen SMTP
if (get_option('wheel_smtp_enabled', false)) {
// Pošlji preko SMTP z uporabo PHPMailer
require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = get_option('wheel_smtp_host');
$mail->SMTPAuth = !empty(get_option('wheel_smtp_username'));
$mail->Username = get_option('wheel_smtp_username');
$mail->Password = get_option('wheel_smtp_password');
$mail->Port = get_option('wheel_smtp_port', 587);
$smtp_encryption = get_option('wheel_smtp_encryption', 'tls');
if ($smtp_encryption === 'ssl') {
$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS;
} elseif ($smtp_encryption === 'tls') {
$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
}
$mail->setFrom($from_email, $from_name);
$mail->addAddress($user->user_email, $user->display_name);
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = $final_subject;
$mail->Body = $final_body;
$mail->AltBody = wp_strip_all_tags($final_body);
$mail->send();
wheel_of_fortune_debug_log("Email uspešno poslan preko SMTP na {$user->user_email}.");
} catch (Exception $e) {
wheel_of_fortune_debug_log("Napaka pri pošiljanju emaila preko SMTP: {$mail->ErrorInfo}");
}
// Zamenjaj vse oznake
$email_subject = str_replace(array_keys($replacements), array_values($replacements), $email_subject);
$email_content = str_replace(array_keys($replacements), array_values($replacements), $email_content);
// Dodaj HTML wrapper, če ga že ni
if (strpos($email_content, '<html') === false) {
$email_content = $this->get_email_html_wrapper($email_content);
}
// Nastavi naslov za pošiljanje
$admin_email = get_option('admin_email');
$site_name = get_bloginfo('name');
$headers = [];
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = "From: {$site_name} <{$admin_email}>";
// Pošlji email
$mail_sent = wp_mail($user->user_email, $email_subject, $email_content, $headers);
if ($mail_sent) {
wheel_of_fortune_debug_log("Email sent to user {$user->user_email} for prize {$prize['name']}");
// Shrani log
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'wheel_email_log',
[
'user_id' => $user_id,
'prize_id' => $prize['id'],
'email' => $user->user_email,
'subject' => $email_subject,
'content' => $email_content,
'sent_date' => current_time('mysql')
],
['%d', '%d', '%s', '%s', '%s', '%s']
);
return true;
} else {
// Pošlji preko standardne wp_mail() funkcije
if (wp_mail($user->user_email, $final_subject, $final_body, $headers)) {
wheel_of_fortune_debug_log("Email uspešno poslan preko wp_mail() na {$user->user_email}.");
} else {
wheel_of_fortune_debug_log("Napaka pri pošiljanju emaila preko wp_mail().");
}
wheel_of_fortune_debug_log("Failed to send email to user {$user->user_email} for prize {$prize['name']}");
return false;
}
}
@ -1551,6 +1578,92 @@ class WheelOfFortune {
}
wp_send_json_error(__('Napaka pri brisanju.', 'wheel-of-fortune'));
}
/**
* Vrne privzeto email predlogo za nagrado
*
* @param array $prize Podatki o nagradi
* @return string HTML vsebina email predloge
*/
private function get_default_email_template($prize) {
$template_path = WHEEL_OF_FORTUNE_PLUGIN_DIR . 'templates/emails/default-prize-email.html';
if (file_exists($template_path)) {
return file_get_contents($template_path);
} else {
// Če datoteka ne obstaja, vrni osnovno HTML predlogo
return '
<h2>Čestitamo, {user_name}!</h2>
<p>Uspešno ste zadeli nagrado na našem kolesu sreče.</p>
<p><strong>Nagrada:</strong> {prize_name}</p>
<p>{prize_description}</p>
' . (isset($prize['funded_account_name']) ? '<p><strong>Račun:</strong> {funded_account_name}</p>' : '') . '
' . (isset($prize['funded_account_value']) ? '<p><strong>Vrednost:</strong> {funded_account_value}</p>' : '') . '
' . (!empty($prize['redemption_code']) ? '<p><strong>Koda za unovčitev:</strong> {redemption_code}</p>' : '') . '
<p>Hvala za sodelovanje!</p>
<p>Lep pozdrav,<br>{site_name}</p>
';
}
}
/**
* Ovije vsebino emaila v HTML wrapper
*
* @param string $content Vsebina emaila
* @return string Vsebina z HTML wrapperjem
*/
private function get_email_html_wrapper($content) {
return '
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Kolo sreče - Nagrada</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px; }
h1, h2 { color: #0066cc; }
.footer { margin-top: 30px; font-size: 12px; color: #666; border-top: 1px solid #eee; padding-top: 10px; }
</style>
</head>
<body>
<div class="content">
' . $content . '
</div>
<div class="footer">
<p>© ' . date('Y') . ' ' . get_bloginfo('name') . ' - Vsi pravice pridržane.</p>
</div>
</body>
</html>
';
}
/**
* AJAX handler za pridobivanje podatkov o email predlogi
*/
public function ajax_get_template_details() {
check_ajax_referer('wheel_admin_nonce', '_ajax_nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(['message' => __('You do not have permission to perform this action.', 'wheel-of-fortune')]);
}
$template_id = isset($_POST['template_id']) ? intval($_POST['template_id']) : 0;
if (!$template_id) {
wp_send_json_error(['message' => __('Template ID was not provided.', 'wheel-of-fortune')]);
}
global $wpdb;
$table_name = $wpdb->prefix . 'wheel_email_templates';
$template = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $template_id), ARRAY_A);
if (!$template) {
wp_send_json_error(['message' => __('Template not found.', 'wheel-of-fortune')]);
}
wp_send_json_success($template);
}
}
// Initialize plugin