Implementacija funkcionalnosti brezplačnega dnevnega vrtenja
Deploy to Test Server / deploy (push) Failing after 9s
Deploy to Test Server / deploy (push) Failing after 9s
This commit is contained in:
1 parent
175f79e303
commit
d77be150d9
7 files changed
+366
-149
No files matched your search
+89
-14
@@ -296,6 +296,7 @@ class WheelOfFortune {
|
||||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(255) NOT NULL,
|
||||
slug varchar(100) NOT NULL,
|
||||
has_daily_spin tinyint(1) NOT NULL DEFAULT 0,
|
||||
created_at datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY slug (slug)
|
||||
@@ -319,6 +320,7 @@ class WheelOfFortune {
|
||||
name varchar(255) NOT NULL,
|
||||
description text DEFAULT '',
|
||||
probability float NOT NULL,
|
||||
daily_spin_probability float DEFAULT NULL,
|
||||
is_active tinyint(1) NOT NULL DEFAULT 1,
|
||||
image_url varchar(255) DEFAULT '',
|
||||
email_subject varchar(255) DEFAULT '',
|
||||
@@ -576,29 +578,65 @@ class WheelOfFortune {
|
||||
}
|
||||
|
||||
$wheel_id = $request->get_param('wheel_id');
|
||||
$spin_type = $request->get_param('spin_type') === 'daily' ? 'daily' : 'paid'; // NOVO
|
||||
|
||||
if (empty($wheel_id)) {
|
||||
return new WP_Error('no_wheel_id', __('Wheel ID is missing.', 'wheel-of-fortune'), ['status' => 400]);
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$spins_table = $wpdb->prefix . 'wheel_spins';
|
||||
$user_spins = $wpdb->get_row($wpdb->prepare("SELECT spins_available FROM $spins_table WHERE user_id = %d AND wheel_id = %d", $user_id, $wheel_id));
|
||||
$spins = $user_spins ? intval($user_spins->spins_available) : 0;
|
||||
|
||||
if ($spins <= 0) {
|
||||
return new WP_Error('no_spins', __('You have no spins left.', 'wheel-of-fortune'), ['status' => 403]);
|
||||
$wheels_table = $wpdb->prefix . 'wof_wheels';
|
||||
$wheel_data = $wpdb->get_row($wpdb->prepare("SELECT has_daily_spin FROM $wheels_table WHERE id = %d", $wheel_id), ARRAY_A);
|
||||
|
||||
$spins_left = 0;
|
||||
$is_eligible_for_daily = false;
|
||||
|
||||
// Preverjanje za DNEVNI SPIN
|
||||
if ($spin_type === 'daily') {
|
||||
if (!empty($wheel_data) && $wheel_data['has_daily_spin']) {
|
||||
$last_spin_meta_key = '_wof_last_daily_spin_' . $wheel_id;
|
||||
$last_spin_timestamp = get_user_meta($user_id, $last_spin_meta_key, true);
|
||||
|
||||
if (empty($last_spin_timestamp) || strtotime('today') > $last_spin_timestamp) {
|
||||
$is_eligible_for_daily = true;
|
||||
}
|
||||
}
|
||||
if (!$is_eligible_for_daily) {
|
||||
return new WP_Error('daily_spin_used', __('You have already used your free daily spin today.', 'wheel-of-fortune'), ['status' => 403]);
|
||||
}
|
||||
}
|
||||
// Preverjanje za PLAČAN SPIN
|
||||
else {
|
||||
$spins_table = $wpdb->prefix . 'wheel_spins';
|
||||
$user_spins = $wpdb->get_row($wpdb->prepare("SELECT spins_available FROM $spins_table WHERE user_id = %d AND wheel_id = %d", $user_id, $wheel_id));
|
||||
$spins_left = $user_spins ? intval($user_spins->spins_available) : 0;
|
||||
|
||||
if ($spins_left <= 0) {
|
||||
return new WP_Error('no_spins', __('You have no spins left.', 'wheel-of-fortune'), ['status' => 403]);
|
||||
}
|
||||
}
|
||||
|
||||
$prizes = $this->get_wheel_prizes($wheel_id);
|
||||
// Nadaljuj z logiko vrtenja
|
||||
$prizes = $this->get_wheel_prizes($wheel_id, $is_eligible_for_daily);
|
||||
if (empty($prizes)) {
|
||||
return new WP_Error('no_prizes', __('No prizes available.', 'wheel-of-fortune'), ['status' => 500]);
|
||||
}
|
||||
|
||||
// Uporabi delujočo logiko za izbiro nagrade
|
||||
$prize = $this->select_random_prize($prizes);
|
||||
$new_spins = $spins - 1;
|
||||
|
||||
$wpdb->update($spins_table, ['spins_available' => $new_spins, 'last_spin_date' => current_time('mysql')], ['user_id' => $user_id, 'wheel_id' => $wheel_id]);
|
||||
// Zmanjšaj spine ali zabeleži dnevni spin
|
||||
if ($spin_type === 'daily') {
|
||||
$last_spin_meta_key = '_wof_last_daily_spin_' . $wheel_id;
|
||||
update_user_meta($user_id, $last_spin_meta_key, time());
|
||||
// Pridobimo trenutno število spinov za response
|
||||
$spins_table = $wpdb->prefix . 'wheel_spins';
|
||||
$user_spins_obj = $wpdb->get_row($wpdb->prepare("SELECT spins_available FROM $spins_table WHERE user_id = %d AND wheel_id = %d", $user_id, $wheel_id));
|
||||
$new_spins = $user_spins_obj ? intval($user_spins_obj->spins_available) : 0;
|
||||
} else {
|
||||
$new_spins = $spins_left - 1;
|
||||
$spins_table = $wpdb->prefix . 'wheel_spins';
|
||||
$wpdb->update($spins_table, ['spins_available' => $new_spins, 'last_spin_date' => current_time('mysql')], ['user_id' => $user_id, 'wheel_id' => $wheel_id]);
|
||||
}
|
||||
|
||||
$log_table = $wpdb->prefix . 'wheel_log';
|
||||
$wpdb->insert($log_table, ['user_id' => $user_id, 'wheel_id' => $wheel_id, 'prize_id' => $prize['id'], 'spin_date' => current_time('mysql'), 'redeemed' => 0]);
|
||||
@@ -625,15 +663,33 @@ class WheelOfFortune {
|
||||
$this->send_prize_email($user_id, $prize);
|
||||
}
|
||||
|
||||
$response_data = [ 'prize' => $prize, 'degree' => $degree, 'remaining_spins' => $new_spins, 'discount_code' => $redemption_code ];
|
||||
$response_data = [
|
||||
'prize' => $prize,
|
||||
'degree' => $degree,
|
||||
'remaining_spins' => $new_spins,
|
||||
'discount_code' => $redemption_code,
|
||||
'spin_type' => $spin_type
|
||||
];
|
||||
|
||||
return new WP_REST_Response(['success' => true, 'data' => $response_data], 200);
|
||||
}
|
||||
|
||||
public function get_wheel_prizes($wheel_id = 1) {
|
||||
public function get_wheel_prizes($wheel_id = 1, $is_daily_spin = false) {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'wheel_prizes';
|
||||
return $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE wheel_id = %d AND is_active = 1 ORDER BY id ASC", $wheel_id), ARRAY_A);
|
||||
$prizes = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE wheel_id = %d AND is_active = 1 ORDER BY id ASC", $wheel_id), ARRAY_A);
|
||||
|
||||
if ($is_daily_spin) {
|
||||
foreach ($prizes as &$prize) {
|
||||
// Če je daily_spin_probability nastavljena, jo uporabi. Sicer uporabi standardno.
|
||||
if (isset($prize['daily_spin_probability']) && $prize['daily_spin_probability'] !== null) {
|
||||
$prize['probability'] = $prize['daily_spin_probability'];
|
||||
}
|
||||
}
|
||||
unset($prize); // Prekini referenco
|
||||
}
|
||||
|
||||
return $prizes;
|
||||
}
|
||||
|
||||
private function select_random_prize($prizes) {
|
||||
@@ -739,11 +795,23 @@ class WheelOfFortune {
|
||||
$spins_left = $wpdb->get_var($wpdb->prepare("SELECT spins_available FROM $spins_table WHERE user_id = %d AND wheel_id = %d", $user_id, $wheel_id));
|
||||
$spins_left = ($spins_left === null) ? 0 : (int)$spins_left;
|
||||
|
||||
// Preveri, ali je dnevni spin na voljo
|
||||
$daily_spin_available = false;
|
||||
if (isset($wheel['has_daily_spin']) && $wheel['has_daily_spin']) {
|
||||
$last_spin_meta_key = '_wof_last_daily_spin_' . $wheel_id;
|
||||
$last_spin_timestamp = get_user_meta($user_id, $last_spin_meta_key, true);
|
||||
|
||||
if (empty($last_spin_timestamp) || strtotime('today') > $last_spin_timestamp) {
|
||||
$daily_spin_available = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Definiramo polje za prevode, da bo dostopno v PHP in JS
|
||||
$l10n = [
|
||||
'spin_button' => __('SPIN', 'wheel-of-fortune'),
|
||||
'spinning' => __('Spinning...', 'wheel-of-fortune'),
|
||||
'no_spins_left' => __('No More Spins', 'wheel-of-fortune'),
|
||||
'free_daily_spin' => __('FREE DAILY SPIN', 'wheel-of-fortune'),
|
||||
'congratulations' => __('Congratulations!', 'wheel-of-fortune'),
|
||||
'you_won' => __('You won:', 'wheel-of-fortune'),
|
||||
'close' => __('Close', 'wheel-of-fortune'),
|
||||
@@ -757,6 +825,7 @@ class WheelOfFortune {
|
||||
'nonce' => wp_create_nonce('wp_rest'),
|
||||
'wheel_id' => $wheel_id,
|
||||
'spins_left' => $spins_left,
|
||||
'daily_spin_available' => $daily_spin_available,
|
||||
'prizes' => $prizes,
|
||||
'l10n' => $l10n // Uporabimo definirano polje
|
||||
));
|
||||
@@ -930,6 +999,7 @@ class WheelOfFortune {
|
||||
$name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
|
||||
$description = isset($_POST['description']) ? sanitize_textarea_field($_POST['description']) : '';
|
||||
$probability = isset($_POST['probability']) ? floatval($_POST['probability']) : 0;
|
||||
$daily_spin_probability = isset($_POST['daily_spin_probability']) && $_POST['daily_spin_probability'] !== '' ? floatval($_POST['daily_spin_probability']) : null;
|
||||
$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;
|
||||
@@ -949,6 +1019,10 @@ class WheelOfFortune {
|
||||
wp_send_json_error(['message' => __('Probability must be between 0 and 1.', 'wheel-of-fortune')]);
|
||||
}
|
||||
|
||||
if ($daily_spin_probability !== null && ($daily_spin_probability < 0 || $daily_spin_probability > 1)) {
|
||||
wp_send_json_error(['message' => __('Daily spin probability must be between 0 and 1.', 'wheel-of-fortune')]);
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'wheel_prizes';
|
||||
|
||||
@@ -957,6 +1031,7 @@ class WheelOfFortune {
|
||||
'name' => $name,
|
||||
'description' => $description,
|
||||
'probability' => $probability,
|
||||
'daily_spin_probability' => $daily_spin_probability,
|
||||
'is_active' => $is_active,
|
||||
'redemption_code' => $redemption_code,
|
||||
'is_discount' => $is_discount,
|
||||
@@ -965,7 +1040,7 @@ class WheelOfFortune {
|
||||
'discount_value' => $discount_value,
|
||||
];
|
||||
|
||||
$format = ['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%s', '%s', '%f'];
|
||||
$format = ['%d', '%s', '%s', '%f', '%f', '%d', '%s', '%d', '%s', '%s', '%f'];
|
||||
|
||||
if ($prize_id > 0) {
|
||||
$result = $wpdb->update($table_name, $data, ['id' => $prize_id], $format, ['%d']);
|
||||
|
||||
Reference in new issue
Block a user