Prvi commit: Začetna verzija vtičnika Wheel of Fortune
This commit is contained in:
commit
0d28b08727
21 files changed
+13814
No files matched your search
@@ -0,0 +1,357 @@
|
||||
<?php
|
||||
/**
|
||||
* Testna skripta za preverjanje različnih načinov ustvarjanja WooCommerce kuponov
|
||||
*/
|
||||
|
||||
// Prepreči neposreden dostop
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Razred za testiranje različnih metod ustvarjanja WooCommerce kuponov
|
||||
*/
|
||||
class WheelCouponTester {
|
||||
|
||||
/**
|
||||
* Zažene različne teste za ustvarjanje kuponov
|
||||
*
|
||||
* @return array Rezultati testov
|
||||
*/
|
||||
public static function run_tests() {
|
||||
$results = array(
|
||||
'success' => false,
|
||||
'message' => '',
|
||||
'tests' => array(),
|
||||
'working_method' => null
|
||||
);
|
||||
|
||||
// Preveri, ali je WooCommerce aktiven
|
||||
if (!class_exists('WooCommerce')) {
|
||||
$results['message'] = 'WooCommerce ni aktiven. Prosim aktivirajte WooCommerce vtičnik.';
|
||||
return $results;
|
||||
}
|
||||
|
||||
// Pridobi trenutnega uporabnika za testiranje
|
||||
$user = wp_get_current_user();
|
||||
if (!$user || !$user->exists()) {
|
||||
$results['message'] = 'Uporabnik ni prijavljen. Prijavite se kot administrator.';
|
||||
return $results;
|
||||
}
|
||||
|
||||
// Nastavi testno kodo
|
||||
$test_code = 'TEST-' . strtoupper(substr(bin2hex(random_bytes(4)), 0, 8));
|
||||
|
||||
// Test 1: Standardna WooCommerce API metoda
|
||||
$results['tests']['standard_api'] = self::test_standard_api($test_code . '-1', $user);
|
||||
|
||||
// Test 2: Programsko ustvarjanje kupona
|
||||
$results['tests']['programmatic'] = self::test_programmatic($test_code . '-2', $user);
|
||||
|
||||
// Test 3: Uporaba WC_Coupon z dodatnimi preverjanji
|
||||
$results['tests']['wc_coupon_extended'] = self::test_wc_coupon_extended($test_code . '-3', $user);
|
||||
|
||||
// Test 4: Neposredno vstavljanje v podatkovno bazo
|
||||
$results['tests']['direct_db'] = self::test_direct_db($test_code . '-4', $user);
|
||||
|
||||
// Počisti testne kupone
|
||||
self::cleanup_test_coupons($test_code);
|
||||
|
||||
// Določi, katera metoda deluje
|
||||
foreach ($results['tests'] as $method => $test_result) {
|
||||
if ($test_result['success']) {
|
||||
$results['working_method'] = $method;
|
||||
$results['success'] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($results['success']) {
|
||||
$results['message'] = 'Našli smo delujočo metodo za ustvarjanje kuponov: ' . $results['working_method'];
|
||||
} else {
|
||||
$results['message'] = 'Nobena testirana metoda ne deluje. Preverite dnevnik napak za več informacij.';
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test 1: Standardna WooCommerce API metoda
|
||||
*/
|
||||
private static function test_standard_api($code, $user) {
|
||||
$result = array(
|
||||
'success' => false,
|
||||
'message' => '',
|
||||
'coupon_id' => 0
|
||||
);
|
||||
|
||||
try {
|
||||
$coupon = new WC_Coupon();
|
||||
$coupon->set_code($code);
|
||||
$coupon->set_description('Test coupon');
|
||||
$coupon->set_discount_type('percent');
|
||||
$coupon->set_amount(10);
|
||||
$coupon->set_individual_use(true);
|
||||
$coupon->set_usage_limit(1);
|
||||
$coupon->set_usage_limit_per_user(1);
|
||||
$coupon->set_email_restrictions(array($user->user_email));
|
||||
$coupon_id = $coupon->save();
|
||||
|
||||
if ($coupon_id) {
|
||||
$result['success'] = true;
|
||||
$result['message'] = 'Kupon uspešno ustvarjen z ID: ' . $coupon_id;
|
||||
$result['coupon_id'] = $coupon_id;
|
||||
} else {
|
||||
$result['message'] = 'Napaka: Kupon ni bil ustvarjen (save() je vrnil 0)';
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$result['message'] = 'Napaka: ' . $e->getMessage();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test 2: Programsko ustvarjanje kupona
|
||||
*/
|
||||
private static function test_programmatic($code, $user) {
|
||||
$result = array(
|
||||
'success' => false,
|
||||
'message' => '',
|
||||
'coupon_id' => 0
|
||||
);
|
||||
|
||||
try {
|
||||
// Ustvari nov post tipa shop_coupon
|
||||
$coupon_data = array(
|
||||
'post_title' => $code,
|
||||
'post_content' => '',
|
||||
'post_status' => 'publish',
|
||||
'post_author' => $user->ID,
|
||||
'post_type' => 'shop_coupon'
|
||||
);
|
||||
|
||||
$coupon_id = wp_insert_post($coupon_data);
|
||||
|
||||
if (!is_wp_error($coupon_id)) {
|
||||
// Dodaj meta podatke za kupon
|
||||
update_post_meta($coupon_id, 'discount_type', 'percent');
|
||||
update_post_meta($coupon_id, 'coupon_amount', '10');
|
||||
update_post_meta($coupon_id, 'individual_use', 'yes');
|
||||
update_post_meta($coupon_id, 'usage_limit', '1');
|
||||
update_post_meta($coupon_id, 'usage_limit_per_user', '1');
|
||||
update_post_meta($coupon_id, 'customer_email', array($user->user_email));
|
||||
|
||||
$result['success'] = true;
|
||||
$result['message'] = 'Kupon uspešno ustvarjen z ID: ' . $coupon_id;
|
||||
$result['coupon_id'] = $coupon_id;
|
||||
} else {
|
||||
$result['message'] = 'Napaka: ' . $coupon_id->get_error_message();
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$result['message'] = 'Napaka: ' . $e->getMessage();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test 3: Uporaba WC_Coupon z dodatnimi preverjanji
|
||||
*/
|
||||
private static function test_wc_coupon_extended($code, $user) {
|
||||
$result = array(
|
||||
'success' => false,
|
||||
'message' => '',
|
||||
'coupon_id' => 0
|
||||
);
|
||||
|
||||
try {
|
||||
// Preveri, ali koda že obstaja
|
||||
$existing_id = wc_get_coupon_id_by_code($code);
|
||||
if ($existing_id) {
|
||||
$result['message'] = 'Napaka: Kupon s to kodo že obstaja (ID: ' . $existing_id . ')';
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Ustvari nov kupon z dodatnimi preverjanji
|
||||
$coupon = new WC_Coupon();
|
||||
$coupon->set_code($code);
|
||||
$coupon->set_description('Test coupon with extended checks');
|
||||
$coupon->set_discount_type('percent');
|
||||
$coupon->set_amount(10);
|
||||
$coupon->set_individual_use(true);
|
||||
$coupon->set_usage_limit(1);
|
||||
$coupon->set_usage_limit_per_user(1);
|
||||
$coupon->set_email_restrictions(array($user->user_email));
|
||||
|
||||
// Dodatna nastavitev
|
||||
$coupon->set_date_expires(strtotime('+7 days'));
|
||||
|
||||
$coupon_id = $coupon->save();
|
||||
|
||||
// Preveri, ali je kupon res ustvarjen
|
||||
if ($coupon_id) {
|
||||
$verification_id = wc_get_coupon_id_by_code($code);
|
||||
if ($verification_id && $verification_id == $coupon_id) {
|
||||
$result['success'] = true;
|
||||
$result['message'] = 'Kupon uspešno ustvarjen in preverjen z ID: ' . $coupon_id;
|
||||
$result['coupon_id'] = $coupon_id;
|
||||
} else {
|
||||
$result['message'] = 'Napaka: Kupon je bil ustvarjen, vendar preverjanje ni uspelo';
|
||||
}
|
||||
} else {
|
||||
$result['message'] = 'Napaka: Kupon ni bil ustvarjen (save() je vrnil 0)';
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$result['message'] = 'Napaka: ' . $e->getMessage();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test 4: Neposredno vstavljanje v podatkovno bazo
|
||||
*/
|
||||
private static function test_direct_db($code, $user) {
|
||||
$result = array(
|
||||
'success' => false,
|
||||
'message' => '',
|
||||
'coupon_id' => 0
|
||||
);
|
||||
|
||||
try {
|
||||
global $wpdb;
|
||||
|
||||
// Ustvari nov post tipa shop_coupon
|
||||
$wpdb->insert(
|
||||
$wpdb->posts,
|
||||
array(
|
||||
'post_title' => $code,
|
||||
'post_name' => sanitize_title($code),
|
||||
'post_content' => '',
|
||||
'post_status' => 'publish',
|
||||
'post_author' => $user->ID,
|
||||
'post_type' => 'shop_coupon',
|
||||
'post_date' => current_time('mysql'),
|
||||
'post_date_gmt' => current_time('mysql', 1)
|
||||
)
|
||||
);
|
||||
|
||||
$coupon_id = $wpdb->insert_id;
|
||||
|
||||
if ($coupon_id) {
|
||||
// Dodaj meta podatke za kupon
|
||||
$meta_data = array(
|
||||
'discount_type' => 'percent',
|
||||
'coupon_amount' => '10',
|
||||
'individual_use' => 'yes',
|
||||
'usage_limit' => '1',
|
||||
'usage_limit_per_user' => '1',
|
||||
'customer_email' => serialize(array($user->user_email))
|
||||
);
|
||||
|
||||
foreach ($meta_data as $meta_key => $meta_value) {
|
||||
$wpdb->insert(
|
||||
$wpdb->postmeta,
|
||||
array(
|
||||
'post_id' => $coupon_id,
|
||||
'meta_key' => $meta_key,
|
||||
'meta_value' => $meta_value
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$result['success'] = true;
|
||||
$result['message'] = 'Kupon uspešno ustvarjen neposredno v bazi z ID: ' . $coupon_id;
|
||||
$result['coupon_id'] = $coupon_id;
|
||||
} else {
|
||||
$result['message'] = 'Napaka: Kupon ni bil ustvarjen v bazi';
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$result['message'] = 'Napaka: ' . $e->getMessage();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Počisti testne kupone
|
||||
*/
|
||||
private static function cleanup_test_coupons($prefix) {
|
||||
global $wpdb;
|
||||
|
||||
// Poišči vse testne kupone
|
||||
$test_coupons = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT ID FROM {$wpdb->posts} WHERE post_title LIKE %s AND post_type = 'shop_coupon'",
|
||||
$prefix . '%'
|
||||
)
|
||||
);
|
||||
|
||||
// Izbriši najdene kupone
|
||||
foreach ($test_coupons as $coupon) {
|
||||
wp_delete_post($coupon->ID, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Funkcija za obdelavo AJAX zahteve za testiranje kuponov
|
||||
*/
|
||||
function wheel_test_coupons_ajax() {
|
||||
// Preveri varnostno kodo
|
||||
check_ajax_referer('wheel_admin_nonce', 'nonce');
|
||||
|
||||
// Preveri pravice
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_send_json_error(array('message' => 'Nimate pravic za izvedbo tega dejanja.'));
|
||||
return;
|
||||
}
|
||||
|
||||
// Zaženi teste
|
||||
$results = WheelCouponTester::run_tests();
|
||||
|
||||
// Vrni rezultate
|
||||
if ($results['success']) {
|
||||
wp_send_json_success($results);
|
||||
} else {
|
||||
wp_send_json_error($results);
|
||||
}
|
||||
}
|
||||
add_action('wp_ajax_wheel_test_coupons', 'wheel_test_coupons_ajax');
|
||||
|
||||
/**
|
||||
* Shrani najboljšo metodo za ustvarjanje kuponov
|
||||
*/
|
||||
function wheel_save_best_coupon_method() {
|
||||
// Preveri varnostno kodo
|
||||
check_ajax_referer('wheel_admin_nonce', 'nonce');
|
||||
|
||||
// Preveri pravice
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_send_json_error(array('message' => 'Nimate pravic za izvedbo tega dejanja.'));
|
||||
return;
|
||||
}
|
||||
|
||||
// Preveri, ali je metoda podana
|
||||
if (!isset($_POST['method']) || empty($_POST['method'])) {
|
||||
wp_send_json_error(array('message' => 'Metoda ni podana.'));
|
||||
return;
|
||||
}
|
||||
|
||||
$method = sanitize_text_field($_POST['method']);
|
||||
$valid_methods = array('standard_api', 'programmatic', 'wc_coupon_extended', 'direct_db');
|
||||
|
||||
if (!in_array($method, $valid_methods)) {
|
||||
wp_send_json_error(array('message' => 'Neveljavna metoda.'));
|
||||
return;
|
||||
}
|
||||
|
||||
// Shrani najboljšo metodo
|
||||
update_option('wheel_best_coupon_method', $method);
|
||||
|
||||
wp_send_json_success(array(
|
||||
'message' => sprintf(__('Metoda %s je bila uspešno shranjena kot najboljša metoda za ustvarjanje kuponov.', 'wheel-of-fortune'), $method)
|
||||
));
|
||||
}
|
||||
add_action('wp_ajax_wheel_save_best_coupon_method', 'wheel_save_best_coupon_method');
|
||||
Reference in new issue
Block a user