

function kerim_alp_get_user_course_ids_from_orders($user_id = 0) {
    if (!$user_id || !function_exists('wc_get_orders')) return [];
    $orders = wc_get_orders([
        'customer_id' => $user_id,
        'status' => ['completed','processing','on-hold'],
        'limit' => -1,
    ]);
    if (!$orders) return [];
    $product_ids = [];
    foreach ($orders as $order) {
        foreach ($order->get_items() as $item) {
            $product_ids[] = $item->get_product_id();
        }
    }
    $product_ids = array_unique(array_filter($product_ids));
    if (!$product_ids) return [];
    $courses = get_posts([
        'post_type' => 'ka_course',
        'numberposts' => -1,
        'post_status' => 'publish',
        'meta_query' => [[
            'key' => '_ka_course_product_id',
            'value' => $product_ids,
            'compare' => 'IN',
        ]]
    ]);
    return $courses ?: [];
}

function kerim_alp_wc_thankyou_redirect($order_id) {
    if (!$order_id || is_admin()) return;
    $redirect = kerim_alp_get_option('post_purchase_redirect', home_url('/panelim/'));
    if ($redirect) {
        $redirect = add_query_arg('ka_purchase', 'success', $redirect);
        wp_safe_redirect($redirect);
        exit;
    }
}
add_action('woocommerce_thankyou', 'kerim_alp_wc_thankyou_redirect', 20);

function kerim_alp_payment_success_notice() {
    if (!is_user_logged_in()) return;
    if (empty($_GET['ka_purchase']) || $_GET['ka_purchase'] !== 'success') return;
    $text = kerim_alp_get_option('welcome_notice_text', 'Satın alma başarılı. Eğitim paneline hoş geldin.');
    echo '<div class="ka-floating-success">' . esc_html($text) . '</div>';
}
add_action('wp_footer', 'kerim_alp_payment_success_notice');

function kerim_alp_append_order_email_text($order, $sent_to_admin, $plain_text, $email) {
    if ($sent_to_admin || !$order instanceof WC_Order) return;
    $panel = home_url('/panelim/');
    $courses = home_url('/egitimler/');
    $title = kerim_alp_get_option('mail_cta_title', 'Eğitimine Erişim Hazır');
    $text = kerim_alp_get_option('mail_cta_text', 'Satın aldığın eğitime panelinden hemen erişebilirsin.');

    if ($plain_text) {
        echo "\n" . wp_strip_all_tags($title) . "\n";
        echo wp_strip_all_tags($text) . "\n";
        echo 'Panel: ' . esc_url($panel) . "\n";
        echo 'Eğitimler: ' . esc_url($courses) . "\n";
        return;
    }

    echo '<div style="margin:24px 0;padding:20px;border:1px solid #e7d39b;border-radius:14px;background:#fffaf0;">';
    echo '<h3 style="margin:0 0 10px;color:#3b2f12;">' . esc_html($title) . '</h3>';
    echo '<p style="margin:0 0 14px;color:#5b4b1d;">' . esc_html($text) . '</p>';
    echo '<p style="margin:0 0 8px;"><a href="' . esc_url($panel) . '">Panelime Git</a></p>';
    echo '<p style="margin:0;"><a href="' . esc_url($courses) . '">Eğitimleri Aç</a></p>';
    echo '</div>';
}
if (function_exists('add_action')) {
    add_action('woocommerce_email_after_order_table', 'kerim_alp_append_order_email_text', 20, 4);
}

function kerim_alp_user_has_course_access($course_id) {
    if (!is_user_logged_in()) return false;
    $product_id = (int) get_post_meta($course_id, '_ka_course_product_id', true);
    if (!$product_id) return true;

    if (function_exists('wc_customer_bought_product')) {
        $user = wp_get_current_user();
        if (wc_customer_bought_product($user->user_email, $user->ID, $product_id)) {
            return true;
        }
    }

    return false;
}

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://katreworks.com/wp-sitemap.xsl" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://katreworks.com/sample-page/</loc><lastmod>2026-03-20T08:07:10+00:00</lastmod></url><url><loc>https://katreworks.com/magaza/</loc><lastmod>2026-03-20T08:55:31+00:00</lastmod></url><url><loc>https://katreworks.com/sepet/</loc><lastmod>2026-03-20T08:55:31+00:00</lastmod></url><url><loc>https://katreworks.com/odeme/</loc><lastmod>2026-03-20T08:55:31+00:00</lastmod></url><url><loc>https://katreworks.com/hesabim/</loc><lastmod>2026-03-20T08:55:31+00:00</lastmod></url><url><loc>https://katreworks.com/blog/</loc><lastmod>2026-03-20T12:07:31+00:00</lastmod></url><url><loc>https://katreworks.com/</loc><lastmod>2026-03-20T12:09:33+00:00</lastmod></url><url><loc>https://katreworks.com/panelim/</loc><lastmod>2026-03-20T13:02:41+00:00</lastmod></url><url><loc>https://katreworks.com/egitimler/</loc><lastmod>2026-03-20T13:02:41+00:00</lastmod></url><url><loc>https://katreworks.com/bonus-icerikler/</loc><lastmod>2026-03-20T13:02:41+00:00</lastmod></url></urlset>
