email template
This commit is contained in:
1 parent
101d8bc623
commit
e11fa6fa5b
10 files changed
+1367
-7127
No files matched your search
+305
-192
@@ -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
|
||||
|
||||
Reference in new issue
Block a user