<?php

if (!defined('ABSPATH')) {
    exit;
}

function wnc_get_qr_url($url) {
    $options = wnc_get_options();
    $size = $options['pro_qr_size'] ?? '180';

    $post_id = get_the_ID();

    $redirect_url = add_query_arg(
        [
            'wnc_open_post' => $post_id,
        ],
        home_url('/')
    );

    return add_query_arg(
        [
            'size' => $size . 'x' . $size,
            'data' => rawurlencode($redirect_url),
        ],
        'https://api.qrserver.com/v1/create-qr-code/'
    );
}

function wnc_render_qr_box($deep_link) {
    $qr_url = wnc_get_qr_url($deep_link);

    ob_start();
    ?>
    <div class="wnc-qr-box">
        <div class="wnc-qr-box-content">
            <strong>Continue in Where Next</strong>
            <p>Scan this with your phone to open this place in the app.</p>
        </div>

        <img
            src="<?php echo esc_url($qr_url); ?>"
            alt="Where Next QR code"
            loading="lazy"
        >
    </div>
    <?php
    return ob_get_clean();
}