<?php
$dir = __DIR__ . '/templates';
$files = glob("$dir/landing*.php");

foreach ($files as $file) {
    $content = file_get_contents($file);
    $changed = false;

    // Pattern 1:
    $search1 = "const res = await fetch(SITE + '/api/check_status.php?tran_id=' + (typeof transactionId !== 'undefined' ? transactionId : (typeof tranId !== 'undefined' ? tranId : id)) + '&token=' + <?= json_encode(\$store_api_key) ?>);";
    $replace1 = "const cacheBuster = new Date().getTime();\n            const res = await fetch(SITE + '/api/check_status.php?tran_id=' + (typeof transactionId !== 'undefined' ? transactionId : (typeof tranId !== 'undefined' ? tranId : id)) + '&token=' + <?= json_encode(\$store_api_key) ?> + '&_=' + cacheBuster);";
    
    if (strpos($content, $search1) !== false) {
        $content = str_replace($search1, $replace1, $content);
        $changed = true;
    }

    $search1b = "const res = await fetch(baseUrl + '/api/check_status.php?tran_id=' + (typeof transactionId !== 'undefined' ? transactionId : (typeof tranId !== 'undefined' ? tranId : id)) + '&token=' + <?= json_encode(\$store_api_key) ?>);";
    $replace1b = "const cacheBuster = new Date().getTime();\n            const res = await fetch(baseUrl + '/api/check_status.php?tran_id=' + (typeof transactionId !== 'undefined' ? transactionId : (typeof tranId !== 'undefined' ? tranId : id)) + '&token=' + <?= json_encode(\$store_api_key) ?> + '&_=' + cacheBuster);";
    
    if (strpos($content, $search1b) !== false) {
        $content = str_replace($search1b, $replace1b, $content);
        $changed = true;
    }

    $search2 = "const query = new URLSearchParams({ tran_id: transactionId, token: token }).toString();";
    $replace2 = "const query = new URLSearchParams({ tran_id: transactionId, token: token, _: new Date().getTime() }).toString();";
    
    if (strpos($content, $search2) !== false) {
        $content = str_replace($search2, $replace2, $content);
        $changed = true;
    }

    if ($changed) {
        file_put_contents($file, $content);
        echo "Updated $file\n";
    }
}
echo "Done.";
